Skip to content

fix(mir-importer): follow provenance for non-struct pointer constants - #155

Merged
nihalpasham merged 3 commits into
mainfrom
fix/issue132-const-ref-provenance
Jun 10, 2026
Merged

nihalpasham merged 3 commits into
mainfrom
fix/issue132-const-ref-provenance

Conversation

@nihalpasham

Copy link
Copy Markdown
Collaborator

Fixes #132.

Option<&T>::unwrap_or(&literal) kernels hit an illegal memory access (CUDA error 700).

Before: %v14 = inttoptr i64 0 to ptr ; load i32, ptr %v14  (null deref; opt -O2 deletes the store)
After:  %v14 = alloca i32 ; store i32 77 ; load i32, ptr %v14

Under -O, MIR const-folds the None arm of unwrap_or(&77) into an Operand::Constant of type &u32: ConstantKind::Allocated with pointer placeholder bytes plus a provenance entry naming the literal's allocation. The pointer-constant arm of translate_operand followed provenance only for MirStructType pointees (arrays have their own path); every other pointee fell through to the raw-pointer path and lowered to MirConstantOp(0) + inttoptr, a null pointer.

Fix: after the existing struct branch, follow provenance for ALL remaining pointee types. Resolve via GlobalAlloc (Memory directly, Static via eval_initializer), honor the byte offset encoded in the pointer's own data bytes, materialize the pointee through the existing translate_constant_value_from_bytes path (int/f16/f32/f64/enum/ZST/nested ptr), and wrap it in MirRefOp exactly like the struct branch (mem2reg/lowering turn it into alloca+store+address; sound since promoted constants are immutable). The raw-pointer fallthrough now only handles constants WITHOUT provenance (true null / int-to-ptr); provenance to a non-memory allocation is a hard translation error instead of a silent null, matching the array path.

Test: tracked example option_ref_unwrap_or (smoketest standard category, exit 1 on mismatch) covering the Some path (5 / 1.5), None with integer literal default (77), None with float literal default (2.5), and None with a named-const default (123, promoted to its own allocation, same provenance shape).

GPU-verified on sm_120 (RTX 5090): all 32 lanes match. Prove-it-bites: reverting the fix reintroduces three inttoptr i64 0 in the example's .ll. Regression sweep (option_ref_transmute, step_by, compiler_features, printf, fp_special_literal_smoke, constant_memory_simple, array_index) all compile clean.

This is the const-operand half of the Option<&T> story; the niche-optimized aggregate (Transmute) half was issue #64 / option_ref_transmute.

Under -O, MIR const-folds the None arm of
`Option<&u32>::unwrap_or(&77)` into an Operand::Constant of type
`&u32`: a ConstantKind::Allocated whose data bytes are a pointer-sized
placeholder and whose provenance entry names the allocation holding the
literal 77.

The pointer-constant arm of translate_operand only followed that
provenance when the pointee was a MirStructType (arrays take a separate
path). Every other pointee fell through to the raw-pointer path, which
parses the placeholder bytes as an address and emits
MirConstantOp(0) + inttoptr, i.e. a null pointer. The subsequent
dereference became `load i32, ptr null`: an illegal memory access
(CUDA error 700) at runtime, and under opt -O2 the UB even deletes the
out[1] store outright.

Fix: after the existing struct branch, follow the provenance for ALL
remaining pointee types. Resolve the entry via GlobalAlloc
(GlobalAlloc::Memory directly, GlobalAlloc::Static via
eval_initializer), honor the byte offset that the pointer's own data
bytes encode into the target allocation, materialize the pointee
through the existing shared constant-from-bytes path
(translate_constant_value_from_bytes: integer / f16 / f32 / f64 /
enum / ZST / nested pointer), and take its address with MirRefOp
exactly like the struct branch. mem2reg/lowering turn the MirRefOp
into alloca + store + address, which is sound because promoted
constants are immutable.

The raw-pointer fallthrough now only handles constants WITHOUT
provenance (true null / int-to-ptr values). Provenance pointing at a
non-memory allocation (e.g. a function) is a hard translation error
instead of a silent null, matching the array path's existing behavior.

Evidence on the issue repro (compile-only, --arch sm_90):

  before:  %v14 = inttoptr i64 0 to ptr
           %v15 = load i32, ptr %v14, align 4
  after:   %v14 = alloca i32, align 4
           store i32 77, ptr %v14, align 4
           %v15 = load i32, ptr %v14, align 4

and post-opt the kernel stores [5, 77] as expected.

Issue: #132 (const-operand half; the niche-optimized aggregate half
was issue #64).
Promotes the issue #132 repro into a tracked GPU example. With both a
Some and a None of Option<&T> live in one kernel, -O const-folds the
None arm's unwrap_or(&literal) into a reference-to-scalar constant
(pointer placeholder bytes + provenance). Pre-fix, the importer lowered
that constant to inttoptr 0 and the kernel dereferenced a null pointer
(CUDA error 700); post-opt the UB even deleted the affected store.

Two kernels, verified per-thread on the host (exit 1 on mismatch):

- opt_ref_unwrap_or_u32: Some path (5), None path with an integer
  literal default (77), and None path with a named-const default (123;
  the const promotes to its own allocation, same provenance shape).
- opt_ref_unwrap_or_f32: Some path (1.5) and None path with a float
  literal default (2.5).

A static default is deliberately not covered: &STATIC constants are
intercepted earlier by the device-global path (which requires zero
initializers today) and never reach the provenance branch under test.

Compile-only evidence (--arch sm_90): the generated .ll contains the
materialized constants

  %v17 = alloca i32, align 4
  store i32 77, ptr %v17, align 4
  ...
  store i32 123, ptr %v19, align 4
  ...
  store float 2.5, ptr %v17, align 4

and no inttoptr / load through null anywhere in the module.

Issue: #132
@nihalpasham nihalpasham added bug Something isn't working miscompile Produces incorrect PTX/IR (wrong generated code) codegen Device code-generation pipeline (Rust MIR to IR to PTX) labels Jun 10, 2026
The literal Some/None + unwrap_or shapes are the test subject (they are
what const-folds into provenance-carrying pointer constants), so they
get scoped #[allow(clippy::unnecessary_literal_unwrap)] rather than the
suggested rewrite. The incidental i % 2 selectors take clippy's
is_multiple_of suggestion; GPU-verified the kernel still lowers and all
32 lanes pass.
@nihalpasham
nihalpasham merged commit ff78a20 into main Jun 10, 2026
23 checks passed
nihalpasham added a commit to advpropsys/cuda-oxide that referenced this pull request Jun 10, 2026
…nto fix/binaryop-cmp

Semantic merge of the enum work: main's NVIDIA#119 (layout-truth tag, enum
total_size/abi_align, divergence guard) meets this branch's
variant_discriminants threading.

- dialect-mir MirEnumType: kept this branch's variant_discriminants
  field/format/constructor arity, main's richer layout doc text.
- importer translate_type: kept the discriminant-value loop and the
  variant_discriminants argument on top of the canonical layout-tag
  selection both sides share.
- mir-lower convert_construct_enum: main's converted-type construction
  (type converter is the single source of the padded struct) composed
  with this branch's declared-discriminant tag writes and loud errors.
- mir-lower memory.rs unit tests: enum constructors gain sequential
  discriminants (the layout tests exercise width/size, not values).

Known issue carried forward (pre-existing on main, fixed in a follow-up
commit on this branch): the NVIDIA#119 divergence guard rejects in-kernel
loads of multi-payload enums, breaking the enum_array_match example
that merged via PR NVIDIA#156.
@nihalpasham
nihalpasham deleted the fix/issue132-const-ref-provenance branch June 12, 2026 10:19
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) miscompile Produces incorrect PTX/IR (wrong generated code)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

kernel hits an illegal memory access (CUDA 700) when it uses Option<&T>::unwrap_or(&literal)

1 participant