Repository navigation
fix(mir-importer): follow provenance for non-struct pointer constants - #155
Merged
Merged
Conversation
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
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
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #132.
Option<&T>::unwrap_or(&literal)kernels hit an illegal memory access (CUDA error 700).Under -O, MIR const-folds the None arm of
unwrap_or(&77)into anOperand::Constantof type&u32:ConstantKind::Allocatedwith pointer placeholder bytes plus a provenance entry naming the literal's allocation. The pointer-constant arm oftranslate_operandfollowed provenance only forMirStructTypepointees (arrays have their own path); every other pointee fell through to the raw-pointer path and lowered toMirConstantOp(0)+ inttoptr, a null pointer.Fix: after the existing struct branch, follow provenance for ALL remaining pointee types. Resolve via
GlobalAlloc(Memory directly, Static viaeval_initializer), honor the byte offset encoded in the pointer's own data bytes, materialize the pointee through the existingtranslate_constant_value_from_bytespath (int/f16/f32/f64/enum/ZST/nested ptr), and wrap it inMirRefOpexactly 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 0in 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.