Repository navigation
evaluate unevaluated array length constant for code generation - #236
haixuanTao wants to merge 3 commits into
Conversation
[Vector; SUB_LEN] (a generic/anonymous const array length) reaches the type
translator as TyConstKind::Unevaluated, which stable_mir eval_target_usize()
cannot evaluate ("cannot be encoded as u64").
The codegen backend holds TyCtxt, which can. It installs a lifetime-erased eval
callback into a mir-importer thread-local for the duration of one
rustc_internal::run scope; the callback does
tcx.normalize_erasing_regions(fully_monomorphized, const) then try_to_target_usize.
The array type arm falls back to this when eval_target_usize() returns None.
mir-importer gains install/clear/eval_array_len; rustc-codegen-cuda builds and
installs the callback around run_pipeline.
Replace manual clear_array_len_eval() call after run_pipeline with a drop guard, so the thread-local is always cleared even if run_pipeline panics — preventing a dangling lifetime-erased closure from remaining in the thread-local after tcx is released. https://claude.ai/code/session_01A5hja53wWguuVfwGxzzhdU
5f2cee7 to
d758839
Compare
The lifetime-erasing transmute on the array-length eval callback spelled out Box<dyn Fn(&TyConst) -> Option<u64>> three times, tripping clippy::type_complexity (-D warnings) in the rustc-codegen-cuda clippy job. Factor it into a local `EvalFn<'a>` alias.
|
Thanks for the PR. A couple of points: Upon reviewing this, I don't think we can take it in its current form. Wanted to explain why rather than just close it. The change moves the compiler context (
Before we land any fix, could you share a small self-contained kernel that reproduces the failure? I tried to trigger the "unevaluated array length" path from ordinary If it turns out to be a real gap, I'm fairly sure it lives upstream: |
|
I'm closing this. Feel free to re-open if you have any updates. |
The thread-local tcx bridge (rejected upstream in NVIDIA#236 for crossing the rustc_public-only boundary of mir-importer and for the lifetime-erasing unsafe) is dead code after the rebase onto upstream main: the nexus shader crate — the original trigger, with const-expr array lengths like `SPATIAL_DIM * 2` — now builds with plain `eval_target_usize()`. Verified: full cubin builds + pendulum PPO on CUDA, all tests green. Keeps `eval_target_usize()` in translate_type (rather than upstream's manual little-endian byte parse of the Value allocation), which is the API-shaped version of the same read. Co-Authored-By: Claude Fable 5 <[email protected]>
|
Following up to close the loop: this is now obsolete — the problem no longer reproduces on current I retested the original trigger (the dimforge nexus physics shaders, whose constraint banks use const-expression array lengths like We've dropped the tcx bridge from our fork accordingly. Thanks for the review notes — agreed in hindsight on both the crate-boundary and soundness points. No need to reopen. |
NVIDIA#236) * refactor: resolve check-placement policy once per compile The bounds-check gates were scattered environment reads: check_hoisting_disabled() and force_device_checks() consulted process globals at seven sites across classification, relocation, and proof discharge. They become one CheckOptimizations policy — three independent axes (hoist_to_preheaders, relocate_to_launch, discharge_proofs) with named constructors: release() (all on), device_debug() (checks stay where the source puts them; proofs still discharge), and disabled() (the differential harness's semantic reference, which inherits no proofs; 2026-08-12 review, S2). The policy is resolved from the environment once at compiler construction and carried on the function compiler, so it cannot change between classification and emission within a compile. The ablation env vars keep their names and semantics; behavior is unchanged under every existing switch combination (differential placement harness green). This is groundwork for NVIDIA#210, where --device-debug selects device_debug() per compile and joins the cache keys. Signed-off-by: Melih Elibol <[email protected]> * feat: device-debug, lineinfo, sanitizer, and opt-level compile options CompileOptions gains opt_level, device_debug, lineinfo, and sanitize_memcheck; a TileirasOptions carrier resolves them once and threads through run_tileiras (--opt-level / --device-debug / --lineinfo / --sanitize=memcheck), the disk-cache path, and both cache keys. device_debug implies --opt-level 0 unless a level is set explicitly, and selects the device_debug check policy: no preheader hoisting, so every device-resident bounds check sits at the source line that wrote it and inlined regions stay contiguous for the rangeless -O0 -G DWARF contract. Proof discharge and launch relocation are deliberately untouched — both remove a check from device code entirely, and the bounded family's launch checks are load-bearing (without relocation its undischarged checks are compile errors). Cache correctness: the flags join the L1 key via CompileOptions and the L2 key as new key material (existing disk entries recompile once), and cache entries validate the flags through a formerly-padding header byte — old release entries carry 0 there, which is exactly the release encoding, so they keep validating without a format bump. Tests: placement pins under device_debug (a hoistable check falls back in place; discharge and launch checks undisturbed), key separation per flag, entry-validation flag mismatches with legacy-zero acceptance, option resolution, and GPU end-to-end launches of debug, lineinfo, and memcheck-instrumented cubins through the real tileiras. Closes NVIDIA#210. Closes NVIDIA#219 (opt_level reaches KernelCompiler via .options(CompileOptions::new().opt_level(n)) and both cache keys). Signed-off-by: Melih Elibol <[email protected]> * docs: align device_debug and cache-entry comments with the implementation The CompileOptions doc claimed device_debug disables launch relocation; it only disables hoisting (relocation is load-bearing for the bounded family). And three comments claimed old cache entries keep validating via the flags byte — they cannot be reached at all, since the flags also joined the key material and orphaned pre-flags entries; the byte is defense in depth, now described as such, with a note on why the entry format version was deliberately not bumped. Signed-off-by: Melih Elibol <[email protected]> --------- Signed-off-by: Melih Elibol <[email protected]>
* fix: version persisted tuning cache keys Persist the L2 key encoding alongside each tuning-record digest so key-preimage migrations are distinguishable from workspace drift. Keep legacy bare-string records wire-compatible and preserve strict verification whenever stored and current keys are comparable. Signed-off-by: Sam, Joochul Chang <[email protected]> * test: pin the l2 key preimage to L2_KEY_SCHEMA The schema bump was enforced by convention only; nothing failed when the preimage changed, which is exactly how NVIDIA#236 changed every key without a test noticing. This asserts one exact digest from fixed inputs, so any preimage change fails loudly with instructions: bump L2_KEY_SCHEMA, update the digest, extend the schema history. Signed-off-by: Melih Elibol <[email protected]> --------- Signed-off-by: Sam, Joochul Chang <[email protected]> Signed-off-by: Melih Elibol <[email protected]> Co-authored-by: Melih Elibol <[email protected]>
Summary
Here's the core constraint: by the time you're in codegen, array length consts should be fully monomorphized and evaluatable. But stable_mir's TyConst::eval_target_usize() fails on TyConstKind::Unevaluated — it can't reduce those to a u64 through stable_mir's API alone.
The TyCtxt bridge is necessary because:
So the bridge is a pragmatic workaround for a gap in stable_mir's const evaluation API. If stable_mir ever exposes a way to evaluate Unevaluated consts in a fully-monomorphized context, the whole bridge could be deleted.