Skip to content

fix(mir-lower): preserve relocations in packed static initializers - #931

Merged
nihalpasham merged 5 commits into
NVIDIA:mainfrom
uurl:fix/packed-static-initializer-relocations
Aug 16, 2026
Merged

nihalpasham merged 5 commits into
NVIDIA:mainfrom
uurl:fix/packed-static-initializer-relocations

Conversation

@uurl

@uurl uurl commented Aug 16, 2026 •

Copy link
Copy Markdown
Contributor

Closes #930

Problem

  • A device static with repr(packed) layout could not hold a pointer, because the pointer lands at an unaligned offset:
#[repr(C, packed)]
struct Entry { tag: u8, ptr: &'static u64 }

#[device_global]
static ENTRY: Entry = Entry { tag: 123, ptr: &VALUE };   // ptr sits at offset 1

Before:  error: 8-byte pointer slots must be naturally aligned
After:   @ENTRY = addrspace(1) global <{ [1 x i8], i64 }>
                  <{ c"\7B", i64 ptrtoint(@VALUE) }>, align 1
         // packed carrier: pointer slot at offset 1, exactly where rustc put it
  • The initializer needs a relocation (the address of VALUE is only known at link time), and the old lowering refused any relocation slot that was not naturally aligned.

Fix

  • storage for relocated initializers switches to a packed LLVM struct whenever the allocation's alignment or a relocation's offset requires it, instead of bailing out
  • a layout validator checks the carrier before emission: top-level explicit-layout structs only, non-overlapping ranges; nested packed stays rejected
  • the textual exporter learns to print packed structs (<{ ... }>), replacing chore(deps): update Pliron for explicit struct layouts #919's fail-closed guard
  • kernels read the fields with unaligned loads, matching the align-1 storage

Gotchas

  • merge order: after chore(deps): update Pliron for explicit struct layouts #919 (this builds on the explicit StructLayout API it introduced)
  • verified against the question the review left open: legacy LLVM-7 libNVVM accepts the packed carrier. An sm_90 --materialize-cubin build compiled the typed-pointer form (pointer at offset 1, and a getelementptr addend variant at offset 3) through libNVVM and ptxas with zero errors
  • follow-up worth adding later: a kernel that reads a packed pointer field by value (let p = ENTRY.ptr;), which exercises a different translator path than the addr_of! + read_unaligned route the example covers

Testing and validation

  • mir-importer, mir-lower, and llvm-export suites green
  • GPU (sm_120): device_global PASS with the expected values [123, 305419896, 17, 1090519040, 17493]
  • modern NVVM path (sm_120) and legacy path (sm_90) both accept and compile the packed carrier
  • CI green (43/43)

@nihalpasham nihalpasham added bug Something isn't working miscompile Produces incorrect PTX/IR (wrong generated code) IR-lowering Lowering between dialects (dialect-mir to LLVM dialect) labels Aug 16, 2026
@nihalpasham nihalpasham added codegen Device code-generation pipeline (Rust MIR to IR to PTX) llvm-export Textual LLVM/NVVM IR exporter (llvm-export crate) and removed miscompile Produces incorrect PTX/IR (wrong generated code) labels Aug 16, 2026
@uurl
uurl force-pushed the fix/packed-static-initializer-relocations branch from 0128aab to 5e25ead Compare August 16, 2026 05:09
@nihalpasham

nihalpasham commented Aug 16, 2026 •

Copy link
Copy Markdown
Collaborator

Gist: this is the carrier #930 asked for: packed storage only when needed, layout validation before emission, and the exporter prints the real <{ ... }> form. The verify-time invariants and transactional bail-outs made this easy to review.

What I verified and did:

  • GPU on sm_120: device_global PASS with the expected values [123, 305419896, 17, 1090519040, 17493]
  • the open review question is settled: legacy LLVM-7 libNVVM accepts the packed carrier. An sm_90 --materialize-cubin build compiled the typed-pointer form (<{ [1 x i8], i64 }> with the pointer at offset 1, and the getelementptr-addend variant at offset 3) through libNVVM and ptxas with zero errors, so no legacy gating is needed
  • modern path at sm_120 emits the same carrier and runs correctly
  • mir-importer, mir-lower, and llvm-export suites all green
  • pushed c9e4c41: rustfmt on the example
  • CI fully green

Two notes for after #919 lands: rebase to drop the duplicated pliron-migration hunks (your relocation work is untouched by it), and consider adding a by-value packed pointer read (let p = PACKED_RELOCATION.ptr;) since that MIR path differs from the addr_of! + read_unaligned route the example covers today. Closes #930 on merge.

# Conflicts:
#	crates/llvm-export/src/export/types.rs
#	crates/mir-lower/src/convert/ops/memory.rs
@nihalpasham
nihalpasham merged commit 807db92 into NVIDIA:main Aug 16, 2026
44 checks passed
@uurl
uurl deleted the fix/packed-static-initializer-relocations branch August 16, 2026 18:16
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) llvm-export Textual LLVM/NVVM IR exporter (llvm-export crate)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Support pointer relocations in packed static initializers

2 participants