Skip to content

fix(abi): preserve array alignment and apply 16-byte workaround generically#388

Open
Snehal-Reddy wants to merge 1 commit into
Rust-GPU:mainfrom
Snehal-Reddy:fix-array-alignment
Open

fix(abi): preserve array alignment and apply 16-byte workaround generically#388
Snehal-Reddy wants to merge 1 commit into
Rust-GPU:mainfrom
Snehal-Reddy:fix-array-alignment

Conversation

@Snehal-Reddy

Copy link
Copy Markdown
Contributor

This fixes an IllegalAddress error when passing 16-byte aligned arrays (e.g. [u128; 4]) or tuples as kernel parameters.

Previously, arrays indiscriminately forced PassMode::Direct(ArgAttributes::new()), discarding any alignment metadata computed by rustc. Furthermore, the 16-byte PassMode::Cast workaround was only applied to ADTs.

This change abstracts the 16-byte workaround to apply to any aggregate type (arrays, ADTs, tuples) with >=16-byte alignment, and properly preserves alignment metadata for arrays when PassMode::Direct is used.

Summary

This PR fixes PTX ABI code generation for arrays and tuples that require 16-byte (or greater) alignment. It resolves an internal compiler error (entered unreachable code: Align is given as power of 2 no larger than 16 bytes) and runtime IllegalAddress traps when launching kernels that take types like [AlignedStruct; 2] or large tuples.

Closes #387

Changes

  • Introduced an is_aggregate condition that encompasses ADTs, Arrays, and Tuples.
  • Applied the existing 16-byte PassMode::Cast workaround to all aggregate types.
  • Updated the fallback PassMode::Direct assignment to correctly inherit arg.layout.align.abi, preventing alignment metadata from being blindly discarded.

Testing

  • cargo build passes
  • cargo clippy --workspace passes
  • Tested on: Ubuntu 24.04.1 LTS, NVIDIA GeForce RTX 5060 Ti, CUDA 12.8.93
…ically

This fixes an IllegalAddress error when passing 16-byte aligned arrays (e.g. [u128; 4]) or tuples as kernel parameters.

Previously, arrays indiscriminately forced PassMode::Direct(ArgAttributes::new()), discarding any alignment metadata computed by rustc. Furthermore, the 16-byte PassMode::Cast workaround was only applied to ADTs.

This change abstracts the 16-byte workaround to apply to any aggregate type (arrays, ADTs, tuples) with >=16-byte alignment, and properly preserves alignment metadata for arrays when PassMode::Direct is used.
@Snehal-Reddy Snehal-Reddy force-pushed the fix-array-alignment branch 2 times, most recently from 4c7c89c to 9b2522b Compare May 1, 2026 21:35
@Snehal-Reddy

Copy link
Copy Markdown
Contributor Author

#389 should hopefully fix the CI?

@Snehal-Reddy

Copy link
Copy Markdown
Contributor Author

Closed #389, #386 should now take care of windows CI failures.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

1 participant