Skip to content

fix(mir-importer): resolve opt and llc as a matched LLVM-major pair - #153

Merged
nihalpasham merged 3 commits into
mainfrom
fix/issue150-matched-opt-llc
Jun 10, 2026
Merged

nihalpasham merged 3 commits into
mainfrom
fix/issue150-matched-opt-llc

Conversation

@nihalpasham

Copy link
Copy Markdown
Collaborator

Fixes #150.

What was wrong

optimize_ll (crates/mir-importer/src/pipeline.rs) discovered opt with 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. Pinning CUDA_OXIDE_LLC to an LLVM 21 llc (documented as supported) still ran 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 verifier with Intrinsic has incorrect argument type! ptr @llvm.lifetime.start.p0.

The fix

Before: opt resolved on its own -> LLVM 22 opt feeds an LLVM 21 llc -> verifier error
After:  llc resolved first, major parsed from --version -> opt picked to MATCH that major
        (or middle-end skipped, loudly, when no same-major opt exists)

New LlvmToolchain (crates/mir-importer/src/llvm_tools.rs), resolved once per generate_ptx and shared with optimize_ll, so future version-conditional emission has a single hook (llc_major). The opt precedence: (1) CUDA_OXIDE_OPT always wins, with a prominent warning naming both binaries and majors on mismatch; (2) the opt co-located with the chosen llc (opt-21 next to llc-21, then plain opt in the same directory) if its major matches; (3) remaining candidates (sysroot opt, opt-22/opt-21/opt on PATH) filtered to llc's major; (4) otherwise the middle-end is skipped (the CUDA_OXIDE_NO_OPT=1 code path) with a warning listing every rejected candidate and its major. All env vars keep working.

Deliberate behavior changes

  • llc is now picked by probing --version instead 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_OPT no longer falls through to other candidates on failure; a failing matched opt warns and falls back to unoptimised IR.

Verification

Scenario main this PR
CUDA_OXIDE_LLC=llc-21 (issue repro) lifetime verifier error opt-21 matched, builds
no pins (vecadd) builds (22 opt + 22 llc) unchanged (sysroot 22 + 22)
CUDA_OXIDE_OPT=opt-22 + LLC=llc-21 silent mismatch loud warning, override honoured
llc = LLVM 20, no same-major opt LLVM 22 IR into llc-20 clean skip, builds unoptimised
CUDA_OXIDE_NO_OPT=1 skip skip (unchanged)

Verified 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_repro example (kernel verbatim from the issue; compile-only host main with a SUCCESS marker for the smoketest standard category).

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
@nihalpasham nihalpasham added bug Something isn't working build-related Build scripts, toolchain discovery, or compilation setup codegen Device code-generation pipeline (Rust MIR to IR to PTX) labels Jun 10, 2026
…_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
nihalpasham merged commit 664fe3d 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/issue150-matched-opt-llc branch June 12, 2026 10:19
meefs pushed a commit to meefs/cuda-oxide that referenced this pull request Sep 30, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working build-related Build scripts, toolchain discovery, or compilation setup codegen Device code-generation pipeline (Rust MIR to IR to PTX)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

PTX generation failed; Intrinsic has incorrect argument type!; ptr @llvm.lifetime.start.p0

1 participant