Skip to content

feat(intrinsics): lower carrying_mul_add; refuse unrecognized rustc intrinsics loudly - #161

Merged
nihalpasham merged 1 commit into
mainfrom
fix/issue137-carrying-mul-add
Jun 11, 2026
Merged

nihalpasham merged 1 commit into
mainfrom
fix/issue137-carrying-mul-add

Conversation

@nihalpasham

Copy link
Copy Markdown
Collaborator

What

u32::carrying_mul_add (and carrying_mul / widening_mul, which funnel into the same intrinsic) now compiles and runs in kernels. Separately, ANY unrecognized core::intrinsics::* callee now fails at translation with an actionable message instead of the late "Symbol ... not found" verifier error.

Before: mir.call to the intrinsic's mangled symbol -> llc: Symbol _RINvNt...carrying_mul_add not found
After:  wide = zext/sext(a) * b + c + d at 2N bits; (low, high) = (trunc, trunc(lshr N))

How

  • New bigint intrinsic family in the importer (modelled on the sibling saturating family) emits a placeholder call with the (T, T) tuple return.
  • mir-lower expands it: all four operands widened to 2N bits (zext unsigned / sext signed, recovered from the pre-conversion MIR type), wide = a*b + c + d (cannot overflow 2N bits; the proof is in the function doc), low/high via trunc and lshr. 128-bit elements are rejected with a targeted diagnostic (would need 256-bit intermediates).
  • The robustness half: the call fall-through now refuses unrecognized core::intrinsics::* / std::intrinsics::* callees at translation time (the collector skips intrinsic instances by design, so such symbols can never link).

Evidence

  • Regression example carrying_mul_add: literal shape from the issue, all-MAX u32 (exact 2^64 - 1), u64 (i128 wide path), signed i32 (sext path); executes on sm_120 (SUCCESS).
  • Revert-proof by both the implementer and an independent adversarial reviewer: without the fix, the exact issue PTX generation failed if you use carrying_mul_add #137 error reproduces.
  • Reviewer also verified the no-wrap flags are NOT set on the wide mul/add (an nsw default would be poison for all-MAX zext'd inputs) and that the lowering matches core's impl_carrying_mul_add_by_widening exactly.
  • PTX inspected: mul.wide.u32 / carry chains as expected. Tests/clippy/fmt clean; full compile-only smoketest green.

Fixes #137

…ing call

Root cause: `u32::carrying_mul_add` (and the sibling bigint helpers
`carrying_mul` / `widening_mul` / signed `carrying_mul_add`) compile to
a call to the rustc intrinsic `core::intrinsics::carrying_mul_add` in
MIR. `try_dispatch_intrinsic` had no arm for it, so the call fell
through to the regular `mir.call` path against the intrinsic's mangled
symbol. The collector skips `InstanceKind::Intrinsic` by design, so no
definition for that symbol ever exists, and the build died late with
the unhelpful "Symbol _RINv..carrying_mul_add.. not found" from the
LLVM dialect module verifier.

The fix has two parts:

1. A new `bigint` intrinsic family (modelled on the saturating family)
   carries the call as a placeholder `mir.call` through the importer.
   The MIR-to-LLVM lowering then expands it the same way core's
   fallback computes it: widen all four operands to 2*N bits (zext for
   unsigned, sext for signed), compute `a * b + c + d` (which cannot
   overflow 2*N bits), and split the wide value into the `(low, high)`
   tuple via trunc / lshr / trunc. NVPTX folds the idiom into
   mul.wide / mul.lo / mul.hi / mad instructions. 128-bit element
   types are rejected with a clear diagnostic since they would need
   256-bit intermediate arithmetic.

2. The fall-through in `translate_call` now refuses to emit a regular
   call to any unrecognized `core::intrinsics::*` /
   `std::intrinsics::*` callee and instead returns an "Unsupported
   construct: rustc intrinsic `...` is not yet supported on the
   device" error with the source span. Such a call can never link, so
   every future intrinsic gap now surfaces as an actionable per-site
   diagnostic at translation time instead of a late verifier failure.

The new `carrying_mul_add` example exercises the literal-argument
shape from the issue plus runtime-argument u32 / u64 / i32 cases
(including the all-MAX inputs whose exact result is 2^64 - 1) and
verifies device results against the host methods.

Fixes #137
@nihalpasham nihalpasham added bug Something isn't working intrinsics Device intrinsics and libdevice math mappings codegen Device code-generation pipeline (Rust MIR to IR to PTX) IR-lowering Lowering between dialects (dialect-mir to LLVM dialect) labels Jun 11, 2026
@nihalpasham
nihalpasham merged commit 1b1468c into main Jun 11, 2026
24 checks passed
@nihalpasham
nihalpasham deleted the fix/issue137-carrying-mul-add 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 codegen Device code-generation pipeline (Rust MIR to IR to PTX) intrinsics Device intrinsics and libdevice math mappings IR-lowering Lowering between dialects (dialect-mir to LLVM dialect)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

PTX generation failed if you use carrying_mul_add

1 participant