Repository navigation
Description
I was matching on an element of an array of enums inside a kernel — match xs[i] { E::A(x) => ..., ... } — and it wouldn't import:
Downcast on non-ADT type: Array
Happens with both a runtime index and a constant index.
Reproduce
#[derive(Clone, Copy)]
pub enum E { A(u32), B(u32), C }
#[kernel]
pub fn match_runtime_index(index: u32, mut out: DisjointSlice<u32>) {
let idx = thread::index_1d();
if let Some(out_elem) = out.get_mut(idx) {
let xs: [E; 4] = [E::A(7), E::B(8), E::C, E::A(100)];
// matching an element of the array -> projection chain [Index, Downcast, Field]
*out_elem = match xs[index as usize] {
E::A(x) => x,
E::B(y) => y + 1000,
E::C => 9999,
};
}
}Launch with index = 0..3; expected [7, 1008, 9999, 100]. Pre-fix the kernel fails to import with Downcast on non-ADT type: Array. (A literal index — match xs[0] { … } — hits the ConstantIndex arm and fails the same way.)
What I found digging in
Root cause (mir-importer / rvalue.rs)
match xs[i] lowers to a projection chain [Index, Downcast, Field]. In translate_place_iterative, the Index / ConstantIndex arms advanced the projected SSA value but never narrowed the running current_rust_ty, so it stayed at the outer Array(Adt(E), N). The following Downcast/Field then handed that array type to apply_enum_field_projection, which rejected it with Downcast on non-ADT type: Array.
Fix that worked: narrow current_rust_ty to the element type on Index/ConstantIndex (a small element_rust_ty helper), mirroring how the Field arm already advances the running type.
While I was in there: translate_place_iterative has no Subslice arm, so something like match xs[a..][i] would hit the same stale-type class — probably worth a follow-up.
Related
- Rvalue::Ref lowering can drop runtime Index projections #120/fix(mir-importer): preserve runtime Index projections in Rvalue::Ref #121 (open) — same
Index-projection-handling gap, but in the address-producing walker (translate_place_addr_from_slot, viaRvalue::Ref). This one is the value-producing walker (translate_place_iterative). Different functions, non-overlapping hunks — they cooperate, don't conflict. - kernel silently reads the wrong struct field on the GPU (reordered/padded repr(Rust) struct) #128 (my other issue) — same "stale projection metadata" family; there it's the LLVM struct index, here it's the running Rust type.
- MIR importer lowers explicit repr enum discriminants with the wrong width #118/fix(mir-importer): honour explicit repr integer widths for enums #119 — sibling enum-importer fix, but a different mechanism (discriminant width).
Suggested labels: bug, codegen, IR-lowering (to match #120).
This one fails loudly at compile time (not a silent miscompile), so lower urgency than #128/#120. Fix + example on a fork (DCO-signed); happy to PR.
Activity
[-]match xs[i] over an array/slice of an enum fails import: `Downcast on non-ADT type: Array`[/-][+]match xs[i] over an array of an enum fails to import (Downcast on non-ADT type: Array)[/+]refactor(mir-importer): unify Ref/AddressOf address materialization
fix(mir-importer): advance place-walker type via ProjectionElem::ty
test(examples): add enum_array_match regression for issue NVIDIA#131
chore(pliron): bump to master 222dd962 + test reorg + clippy cleanup
feat(unroll): loop-unrolling pass with opt-independent constant foldi…
Address an enum payload in memory, so a payload can be mutated in pla…
fix: skip nested partition offset metadata for dynamic outer tiles (N…