Skip to content

fix(mir-lower): support packed aggregates by value - #941

Merged
nihalpasham merged 4 commits into
NVIDIA:mainfrom
uurl:fix/packed-aggregate-by-value-abi
Aug 17, 2026
Merged

nihalpasham merged 4 commits into
NVIDIA:mainfrom
uurl:fix/packed-aggregate-by-value-abi

Conversation

@uurl

@uurl uurl commented Aug 16, 2026 •

Copy link
Copy Markdown
Contributor

Closes #940

Dependency

This PR is stacked on #931, which in turn is stacked on #919.

#919 updates cuda-oxide to the explicit StructLayout::{Packed, Unpacked} API, and #931 adds the packed LLVM struct sizing/export infrastructure reused by this change.

Once the prerequisite PRs land, this branch will be rebased onto main, leaving only the packed aggregate by-value and ABI alignment changes in this PR.

Summary

Adds by-value support for Rust packed aggregates while preserving their physical rustc layout through MIR lowering, LLVM export, and the NVVM kernel ABI.

This extends the existing packed-field addressing support to whole aggregate values.

What changed

Packed aggregate representation

Struct lowering now distinguishes between:

  • layouts that differ from LLVM's natural struct layout; and
  • layouts that can actually be represented faithfully by an LLVM packed struct.

A divergent layout is lowered as StructLayout::Packed only when sequential LLVM packed layout reproduces the recorded rustc field offsets and total size exactly.

This avoids incorrectly treating overlapping or union-like layouts as ordinary packed structs.

For example:

#[repr(C, packed)]
struct Packed1 {
    a: u8,
    b: u32,
}

lowers to:

<{ i8, i32 }>

with size 5.

And:

#[repr(C, packed(2))]
struct Packed2 {
    a: u8,
    b: u32,
}

lowers to:

<{ i8, [1 x i8], i32 }>

with size 6.

The explicit byte slot preserves the rustc field offset of b.

By-value operations

Packed aggregates are now supported through:

  • aggregate construction;
  • whole-value loads;
  • whole-value stores;
  • arrays of packed aggregates;
  • kernel arguments passed by value;
  • internal device-function aggregate arguments and returns.

Layouts that cannot be represented faithfully still fail closed.

Kernel ABI alignment

LLVM packed structs have natural alignment 1, which is insufficient for layouts such as:

#[repr(C, packed(2))]

whose Rust ABI alignment is 2.

MIR lowering now records non-natural source ABI alignment when it exceeds the natural alignment of the lowered LLVM type.

The LLVM exporter translates that into NVVM's "align" function property annotation.

NVVM encodes this as:

(position << 16) | alignment

where argument positions are 1-based and position 0 denotes the return value.

For the first Packed2 kernel parameter:

(1 << 16) | 2 = 65538

This produces the expected PTX:

.param .align 2 .b8 packed2_param_0[6]

rather than the previous:

.param .align 1 .b8 packed2_param_0[6]

Shared-memory pointer aggregates

Packed aggregates containing address-space-3/shared pointers remain unsupported by value.

Their physical pointer width is target-dependent between the rustc NVPTX model and modern NVVM, so these cases continue to fail closed rather than silently producing an incorrect ABI.

This does not affect existing packed field addressing.

End-to-end coverage

Adds the packed_aggregate_abi example covering:

  • repr(C, packed);
  • repr(C, packed(2));
  • host-to-kernel by-value argument passing;
  • device helper argument/return paths;
  • whole-value load/store;
  • physical field offsets;
  • PTX aggregate size;
  • PTX parameter alignment.

The runtime test executes successfully on CUDA and verifies:

Packed1: size 5, align 1
Packed2: size 6, align 2

with generated PTX including:

.param .align 2 .b8 packed2_param_0[6]
.param .align 2 .b8 store_packed2_param_0[6]

Validation

mir-lower unit tests:        234 passed
mir-lower integration tests: 105 passed

cargo clippy -p mir-lower --all-targets -- -D warnings
cargo clippy -p llvm-export --all-targets -- -D warnings
cargo clippy --workspace --all-targets -- -D warnings

cargo fmt --all -- --check
git diff --check

End-to-end:

cargo oxide run packed_aggregate_abi

packed_aggregate_abi: PASS
(runtime values, whole-value load/store, and PTX parameter shapes)

PTX ABI checks:

.param .align 2 .b8 packed2_param_0[6]
.param .align 2 .b8 store_packed2_param_0[6]

Out of scope

This PR does not attempt to support:

  • packed aggregates containing target-dependent shared-memory pointers by value;
  • arbitrary overlapping/union-like layouts as LLVM packed structs;
  • changes to the existing byte-offset packed field addressing path.

@nihalpasham nihalpasham added bug Something isn't working codegen Device code-generation pipeline (Rust MIR to IR to PTX) IR-lowering Lowering between dialects (dialect-mir to LLVM dialect) labels Aug 17, 2026
@nihalpasham
nihalpasham force-pushed the fix/packed-aggregate-by-value-abi branch from 5a489f4 to 7495e43 Compare August 17, 2026 09:33
@nihalpasham

Copy link
Copy Markdown
Collaborator

Gist: by-value packed aggregates get the correct param ABI: .param .align 2 .b8 ...[6] instead of natural alignment, plus whole-value load/store handling.

What I verified and did:

  • rebased onto main and force-pushed: 24df1fc and 5e25ead duplicated the already-merged pliron bump and fix(mir-lower): preserve relocations in packed static initializers #931, so the branch now carries only your packed-aggregates commit (now 7c23486, 11 files) plus my fixes
  • pushed 5efdf84: renamed the attr key from cuda_oxide_kernel_param_abi_align_ to cuda_oxide_param_abi_align_. The reserved-symbols guard polices the cuda_oxide_kernel_ link-symbol family; pliron op-attribute keys live outside it, matching the sibling cuda_oxide_return_abi_align
  • pushed 7495e43: rustfmt on the example
  • GPU on sm_120: packed_aggregate_abi PASS (runtime values, whole-value load/store, PTX parameter checks), with .param .align 2 .b8 packed2_param_0[6] in the PTX
  • mir-lower tests green; full smoketest 210/210
  • CI fully green

Closes #940 on merge.

nihalpasham
nihalpasham previously approved these changes Aug 17, 2026
… scalar peel takes priority over packed by-value conversion)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working codegen Device code-generation pipeline (Rust MIR to IR to PTX) IR-lowering Lowering between dialects (dialect-mir to LLVM dialect)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Support packed aggregates passed by value across the kernel ABI

2 participants