Repository navigation
fix(mir-importer): resolve opt and llc as a matched LLVM-major pair - #153
Merged
Merged
Conversation
The middle-end (opt -O2) and the backend (llc) were discovered independently: optimize_ll walked its own precedence (CUDA_OXIDE_OPT, sysroot llvm-tools opt, opt-22/opt-21/opt) and never consulted the llc that generate_ptx would invoke. A user pinning CUDA_OXIDE_LLC to an LLVM 21 llc (documented as supported) still got the rustc sysroot's LLVM 22 opt. LLVM 22 removed the i64 size parameter from llvm.lifetime.start/end, so the 22-optimised IR fails the 21 llc's verifier with "Intrinsic has incorrect argument type\! ptr @llvm.lifetime.start.p0" (issue #150). Resolution is now centralised in LlvmToolchain (llvm_tools.rs), resolved once per generate_ptx and shared with optimize_ll, so future version-conditional emission has a single hook: - llc resolves first, with the existing precedence (CUDA_OXIDE_LLC used exclusively, then the sysroot llvm-tools llc, then llc-22 / llc-21 on PATH; first candidate whose --version runs wins), and its LLVM major is parsed from the version banner. - opt is then chosen to match that major: 1. CUDA_OXIDE_OPT always wins, but a major mismatch prints a prominent warning naming both binaries and majors; 2. else the opt co-located with llc (basename llc->opt twin such as opt-21 next to llc-21, then plain opt in the same directory), if its major matches (trusted without a parsed llc major: the same install directory implies the same release); 3. else the remaining candidates (sysroot opt, opt-22/opt-21/opt on PATH), filtered to the same major as llc. - If no same-major opt exists, the middle-end is skipped (the CUDA_OXIDE_NO_OPT=1 code path) with a warning naming the chosen llc and every rejected candidate with its major: unoptimised IR into the right llc always beats optimised IR into the wrong one. CUDA_OXIDE_NO_OPT / CUDA_OXIDE_OPT / CUDA_OXIDE_LLC keep working. Behaviour changes beyond the pairing: - llc is picked by probing --version instead of try-compiling each candidate in turn, so a present-but-failing llc reports its own error instead of silently falling through to a different major's llc fed with mismatched IR. - A failing matched opt warns and falls back to unoptimised IR instead of retrying the next (potentially mismatched) candidate. The pure parts (version-banner parsing, sibling-path derivation, and the candidate-choice logic) are unit-tested without spawning binaries. Verified without a GPU on a machine whose sysroot llvm-tools are LLVM 22 and /usr/bin has llc-18/20/21/22: - CUDA_OXIDE_LLC=/usr/bin/llc-21: previously failed with the verifier error above; now resolves opt = /usr/bin/opt-21 (sibling) and the optimised IR carries the sized form "call void @llvm.lifetime.start.p0(i64 12, ptr nonnull %v14.i)". - No pins: sysroot llc (LLVM 22) + sysroot opt (LLVM 22); vecadd builds unchanged. - CUDA_OXIDE_OPT=/usr/bin/opt-22 + CUDA_OXIDE_LLC=/usr/bin/llc-21: explicit override honoured, mismatch warning printed, llc failure surfaces with the env var named. - llc pinned to an LLVM 20 binary with no same-major opt anywhere: middle-end skipped with the full rejected-candidate list and the build completes on unoptimised IR. Issue: #150
Kernel taken verbatim from issue #150. The inlined gpu_printf arg buffers make opt's inliner insert llvm.lifetime.start/end markers, which is exactly the IR shape whose form changed in LLVM 22 (the i64 size parameter was removed). Building with CUDA_OXIDE_LLC pinned to an LLVM 21 llc reproduced the mismatched-toolchain verifier failure before the matched-pair fix; it now compiles with either a matched opt or a clean middle-end skip. Host main is compile-only (no GPU needed) and prints the SUCCESS marker for the smoketest standard category. Issue: #150
…_repro The if-let-Some(_) shape is verbatim from issue #150 and is what makes opt's inliner introduce the lifetime markers; rewriting it to is_some() would change the MIR shape the regression exists to pin. Verified the .opt.ll still contains the llvm.lifetime markers with the allow in place.
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.
meefs
pushed a commit
to meefs/cuda-oxide
that referenced
this pull request
Sep 30, 2026
Signed-off-by: Melih Elibol <[email protected]>
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 #150.
What was wrong
optimize_ll(crates/mir-importer/src/pipeline.rs) discoveredoptwith its own precedence (CUDA_OXIDE_OPT, sysroot llvm-toolsopt,opt-22/opt-21/opt) and never consulted thellcthatgenerate_ptxwould invoke. PinningCUDA_OXIDE_LLCto an LLVM 21llc(documented as supported) still ran the rustc sysroot's LLVM 22opt. LLVM 22 removed thei64size parameter fromllvm.lifetime.start/end, so the 22-optimised IR fails the 21llcverifier withIntrinsic has incorrect argument type! ptr @llvm.lifetime.start.p0.The fix
New
LlvmToolchain(crates/mir-importer/src/llvm_tools.rs), resolved once pergenerate_ptxand shared withoptimize_ll, so future version-conditional emission has a single hook (llc_major). The opt precedence: (1)CUDA_OXIDE_OPTalways wins, with a prominent warning naming both binaries and majors on mismatch; (2) the opt co-located with the chosen llc (opt-21next tollc-21, then plainoptin the same directory) if its major matches; (3) remaining candidates (sysroot opt,opt-22/opt-21/opton PATH) filtered to llc's major; (4) otherwise the middle-end is skipped (theCUDA_OXIDE_NO_OPT=1code path) with a warning listing every rejected candidate and its major. All env vars keep working.Deliberate behavior changes
--versioninstead of try-compiling each candidate in turn; a present-but-failing llc reports its own error instead of silently falling through to a different major fed with mismatched IR.CUDA_OXIDE_OPTno longer falls through to other candidates on failure; a failing matched opt warns and falls back to unoptimised IR.Verification
CUDA_OXIDE_LLC=llc-21(issue repro)CUDA_OXIDE_OPT=opt-22+LLC=llc-21CUDA_OXIDE_NO_OPT=1Verified end-to-end on sm_120 (RTX 5090): the pinned scenario resolves
llc = /usr/bin/llc-21 (LLVM 21), opt = /usr/bin/opt-21 (LLVM 21)and builds; the default path (no pins) still picks the sysroot 22 pair and vecadd runs correctly. The pure resolution logic (version-banner parsing, sibling-path derivation, candidate choice) is unit-tested without spawning binaries (9 new tests). Revert-proof: rebuilding the backend with main's pipeline re-breaks the pinned repro with the exact verifier error.Includes the
issue150_reproexample (kernel verbatim from the issue; compile-only host main with a SUCCESS marker for the smoketest standard category).