Skip to content

fix(mir-lower): support packed address-space 3 aggregates across the internal device ABI - #1001

Merged
nihalpasham merged 3 commits into
NVIDIA:mainfrom
uurl:fix/packed-as3-internal-device-abi
Aug 19, 2026
Merged

nihalpasham merged 3 commits into
NVIDIA:mainfrom
uurl:fix/packed-as3-internal-device-abi

Conversation

@uurl

@uurl uurl commented Aug 18, 2026

Copy link
Copy Markdown
Contributor

Closes #1000

Summary

Adds support for the first packed aggregate shape containing an AS3/shared-memory pointer across the internal device ABI.

The implementation keeps the packed aggregate's semantic representation unchanged inside device-function SSA, but uses a target-stable packed carrier containing a generic pointer when the value crosses an internal return boundary.

This extends the aggregate ABI work from #867, the scalar AS3 representation strategy from #884, and the packed aggregate support from #941.

Problem

#941 intentionally kept packed aggregates containing shared-memory pointers unsupported by value because their physical representation depends on the selected NVPTX target mode.

That restriction is still correct for:

  • kernel parameters;
  • whole-value memory loads;
  • whole-value memory stores.

It is unnecessarily restrictive for an internal device-function return, where the compiler controls both sides of the ABI boundary.

A packed semantic value such as:

<{ i8, ptr addrspace(3) }>

can cross that boundary through a target-stable carrier:

<{ i8, ptr }>

and be reconstructed back to the semantic AS3 representation at the caller.

Design

The new internal return path is:

semantic packed aggregate
<{ scalar, p3 }>
        ↓ target-stable coercion
internal ABI carrier
<{ scalar, p0 }>
        ↓ call / return
internal ABI carrier
<{ scalar, p0 }>
        ↓ target-stable coercion
semantic packed aggregate
<{ scalar, p3 }>

The AS3 ↔ generic conversion is represented explicitly with addrspacecast.

The function body itself continues to use the semantic packed AS3 type.

Target-stable aggregate storage

This PR factors the recursive physical-storage rewrite previously used by enum payload storage into a shared helper.

The helper supports:

  • shared pointer → generic pointer rewriting;
  • optional bool i1 ↔ i8 canonicalization;
  • recursive structs;
  • recursive arrays where allowed by the caller;
  • symmetric value reconstruction.

Enum payload storage retains its existing bounded-array and bool-canonicalization policy on top of the shared implementation.

Packed AS3 internal ABI classification

The new packed-AS3 ABI path is intentionally narrow.

It accepts only:

  • a byte-faithful packed struct;
  • exactly one direct AS3/shared pointer field;
  • scalar non-ZST sibling fields.

It rejects:

  • nested aggregate AS3 leaves;
  • arrays;
  • vectors;
  • multiple AS3 pointer leaves;
  • layouts that cannot be represented faithfully.

The target-stable carrier must preserve the recorded aggregate size.

Arguments and returns

Internal aggregate arguments already use the existing flattened field ABI path, so no new aggregate argument carrier is required.

For eligible packed-AS3 returns:

  • the callee rebuilds the semantic packed value into the generic-pointer carrier before llvm.return;
  • the call result is typed as the carrier;
  • the caller reconstructs the semantic packed AS3 aggregate after the call.

Kernel entry returns and parameters are unchanged.

Memory behavior

Whole-value packed AS3 loads and stores remain fail closed.

This PR does not make the target-dependent packed memory image legal.

The new exception applies only to semantic SSA construction and the controlled internal device ABI boundary.

End-to-end coverage

packed_aggregate_abi now includes a device-only packed shared-pointer case:

#[repr(C, packed)]
struct PackedShared {
    tag: u8,
    ptr: *mut SharedArray<u32, 1>,
}

The value crosses internal device-function boundaries while remaining in SSA.

The shared pointer is created on device and is never exposed through the host/kernel parameter ABI.

Runtime validation confirms that the shared-memory pointer survives the round trip.

Validation

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

cargo test -p mir-lower packed_struct_construction -- --nocapture
cargo test -p mir-lower packed_shared -- --nocapture
cargo test -p mir-lower --all-targets

cargo clippy -p mir-lower --all-targets -- -D warnings

Results:

mir-lower unit tests:        248 passed
mir-lower integration tests: 107 passed

End-to-end:

cargo oxide build packed_aggregate_abi
cargo oxide run packed_aggregate_abi
cargo oxide run addressof_sharedarray

Results:

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

PASS addressof_sharedarray:
shared address is non-null, enum storage and struct/tuple/array/enum
device-call round trips preserved shared pointers

Out of scope

This PR intentionally does not support:

  • packed AS3 kernel parameters;
  • whole-value packed AS3 memory loads or stores;
  • nested AS3 aggregates;
  • arrays containing AS3 pointers;
  • vectors containing AS3 pointers;
  • multiple AS3 pointer leaves;
  • broader recursive packed-AS3 ABI legalization.

@nihalpasham nihalpasham changed the title fix(mir-lower): support packed AS3 aggregates across the internal device ABI fix(mir-lower): support packed address-space 3 aggregates across the internal device ABI Aug 19, 2026
@nihalpasham nihalpasham added bug Something isn't working enhancement New feature or request IR-lowering Lowering between dialects (dialect-mir to LLVM dialect) codegen Device code-generation pipeline (Rust MIR to IR to PTX) labels Aug 19, 2026
@nihalpasham

Copy link
Copy Markdown
Collaborator
  • Verified on the 5090: packed_aggregate_abi and addressof_sharedarray both pass, mir-lower suite green including the target_stable_storage refactor.
  • Tried strengthening the example to read value.ptr inside consume_packed_shared: any field projection forces the whole-value store that Support packed aggregates containing AS3 pointers across the internal device ABI #1000 lists as out of scope, and it fails closed with the documented diagnostic. So the runtime check stays as authored; the coercion itself is proven by the unit tests (insertvalue slots, address spaces, both directions).
  • Filing a follow-up for the next rung: carrier-typed local slots so field projections of packed AS3 aggregates work.

@nihalpasham
nihalpasham merged commit a184459 into NVIDIA:main Aug 19, 2026
76 of 82 checks passed
@uurl
uurl deleted the fix/packed-as3-internal-device-abi branch August 19, 2026 21:37
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) enhancement New feature or request 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 containing AS3 pointers across the internal device ABI

2 participants