Skip to content

feat: lower modf and remquo through libdevice out parameters - #1651

Closed
uurl wants to merge 6 commits into
NVIDIA:mainfrom
uurl:feat/libdevice-math-out-params
Closed

uurl wants to merge 6 commits into
NVIDIA:mainfrom
uurl:feat/libdevice-math-out-params

Conversation

@uurl

@uurl uurl commented Oct 26, 2026 •

Copy link
Copy Markdown
Contributor

Summary

  • Add f32 and f64 mappings for libm::modf{f} and libm::remquo{f} to the generated libdevice math shard.
  • Reuse the frexp out-parameter call helper and pack the results according to the Rust tuple layout. Normalize the remquo quotient to zero when the remainder is NaN.
  • Extend CUDA 13.0 ABI evidence, generator and lowering tests, and add the math_out_params GPU example.

The shard now covers 108 of the 124 public f32/f64 exports in libm 0.2.16.

Validation

  • cargo oxide fmt --check
  • cargo run -p cuda-intrinsics-gen --locked -- check
  • cargo test -p cuda-intrinsics-gen -p dialect-mir -p mir-importer -p mir-lower --all-targets --locked
  • cargo clippy -p cuda-intrinsics-gen -p dialect-mir -p mir-importer -p mir-lower --all-targets --locked -- -D warnings
  • cargo oxide run math_out_params: 180 GPU checks passed across f32 and f64
  • cargo oxide run math_special: 776 GPU checks passed
  • just check

Depends on #1650. Refs #1224.

This completes the planned out-parameter mappings. The remaining 16 libm exports require separate semantic handling; this PR does not close #1224.

Testing

  • just -f cuda-oxide/Justfile check passes (the local mirror of CI: fmt, clippy, tests, guards, docs)
  • cargo oxide run <example> passes, or cuda-oxide/scripts/smoketest.sh -o '^<example>$'
  • New example added (if applicable)

Checklist

  • All commits signed off (git commit -s)
  • SPDX headers on new source files

uurl added 6 commits October 25, 2026 19:16
Signed-off-by: Raul Estrada <raulestradaa@gmail.com>
Signed-off-by: Raul Estrada <raulestradaa@gmail.com>
Signed-off-by: Raul Estrada <raulestradaa@gmail.com>
Signed-off-by: Raul Estrada <raulestradaa@gmail.com>
Signed-off-by: Raul Estrada <raulestradaa@gmail.com>
Signed-off-by: Raul Estrada <raulestradaa@gmail.com>
@copy-pr-bot

copy-pr-bot Bot commented Oct 26, 2026

Copy link
Copy Markdown

This pull request requires additional validation before any workflows can run on NVIDIA's runners.

Pull request vetters can view their responsibilities here.

Contributors can view more details about this message here.

@uurl
uurl marked this pull request as ready for review October 26, 2026 06:55
@uurl
uurl requested a review from nihalpasham as a code owner October 26, 2026 06:55
@nihalpasham nihalpasham added the cuda-oxide SIMT programming model: rustc backend, cargo oxide, cuda-device, cuda-host, book label Oct 29, 2026
@nihalpasham

Copy link
Copy Markdown
Collaborator

Thanks! Since #1660 contains the full 9-commit #1224 stack in linear order (including this commit with your authorship preserved), I am landing the entire series together through #1660 and closing this intermediate stack PR.

nihalpasham added a commit that referenced this pull request Oct 29, 2026
…120 libm exports (#1660)

## What This PR Does

Implements `#1224` across the 9-commit `libdevice_math` series (`#1638`,
`#1645`, `#1646`, `#1649`, `#1650`, `#1651`, `#1652`, `#1653`, `#1660`),
replacing hand-maintained math dispatch tables across three crates with
a single generated table backed by `libdevice.10.bc` evidence and
expanding `libm 0.2.16` coverage from 72 to **120 of 124 `f32`/`f64`
exports**:

1. **Add `intrinsics/overlay/libdevice_math.toml` + `libdevice.10.bc`
signature verification and generate `dialect-mir` math dispatch
(`cuda-intrinsics-gen`, `dialect-mir`, `mir-importer`,
`rustc-codegen-cuda`)**
2. **Extract float-math lowering into
`mir-lower/src/convert/ops/float_math.rs` driven by generated
`RustFloatMathLowering` (`mir-lower`)**
3. **Lower scalar aliases (`tgamma`, `ilogb`, `ldexp`, `erf`, `j0`,
`y0`, ...), entry-block out-parameter adapters (`frexp`, `modf`,
`remquo`), `lgamma_r`, C23 `fmaximum`/`fminimum`, and `j1` (`mir-lower`,
`examples/math_special`)**

---

### 1. Generated `libm -> libdevice` Dispatch (`#1224`)

Previously, wiring a single `libm` or `std::sys::cmath` function
required hand-editing ~10 sites across
`rustc-codegen-cuda/src/collector.rs`, `mir-importer`, `dialect-mir`,
and `mir-lower`:

```text
BEFORE (hand-maintained across 4 crates):
  collector.rs       ──► hardcoded `std::sys::cmath::*` match list
  mir-importer       ──► hand-written `RustFloatMathIntrinsic` enum + `from_core_path` / `from_libm_path`
  dialect-mir        ──► hand-written `CALLEE_*` constants + `is_libdevice_backed_placeholder`
  mir-lower          ──► hand-written placeholder -> `__nv_*` match table

AFTER (`intrinsics/overlay/libdevice_math.toml` -> `cuda-intrinsics-gen`):
  libdevice_math.toml + cuda-13.0.json (102 signatures verified against libdevice.10.bc)
    │
    ▼
  crates/dialect-mir/src/generated/libdevice_math.rs
    ├──► collector.rs   `is_intrinsic_lowered_cmath_shim(fn_path)`
    ├──► mir-importer   `from_core_path` + `from_libm_path` -> `CALLEE_*`
    ├──► codegen        `link_requirement()` -> `MathLink::{Always, Libnvvm, Never}`
    └──► mir-lower      `RustFloatMathIntrinsic::lowering(self) -> RustFloatMathLowering`
```

---

### 2. Entry-Block Out-Parameter Adapters & IEEE Edge-Case Lowerings
(`mir-lower`)

```text
Function Family                Lowering in `mir-lower/src/convert/ops/float_math.rs`
─────────────────────────────  ──────────────────────────────────────────────────────────────────────────
`frexp`, `modf`, `remquo`      Hoists `llvm.alloca` to function entry block (`0` `.local` traffic after
                               `opt -O2` SROA); normalizes `frexp(+-inf/NaN) -> (x, 0)` and `remquo` NaN -> `quo = 0`
`lgamma_r`, `lgammaf_r`        Composes `__nv_lgamma`, `__nv_floor`, and `__nv_fmod(floor(x), 2.0)` with
                               `-0.0 -> (+inf, -1)`, negative integer poles `-> (+inf, +1)`, `-inf -> (+inf, +1)`
C23 `fmaximum*` / `fminimum*`  Pure LLVM `fcmp` (empty fast-math flags) + sign-bit tie-break (`-0.0 < +0.0`)
`j1`, `j1f`                    Calls `__nv_j1{,f}` and normalizes `|x| == inf -> +0.0`
```

---

## Verification

* **RTX 5090 (`sm_120a`) GPU Execution & Sanitizer**:
* `cargo oxide run math_special`: **2,822 / 2,822 `f32` and `f64` checks
PASS** (`compute-sanitizer --tool memcheck`: `0 errors`, `0` `.local`
instructions emitted for `frexp`, `modf`, `remquo`, `lgamma_r`, and C23
`minmax`).
* `cargo oxide run libm_math` & `cargo oxide run libdevice_math`:
**PASS**.
* **Generator & Bitcode Verification**:
* `cargo run -p cuda-intrinsics-gen -- check` & `verify-libdevice`:
**PASS** (`102` `libdevice.10.bc` signatures verified).
* **Tests, Lints & Guards**:
* `cargo test -p cuda-intrinsics-gen -p dialect-mir -p mir-importer -p
mir-lower -p cuda-oxide-codegen --all-targets`: **3,494 tests PASS**
* `just fmt-check`, `just clippy`, `scripts/sync-example-locks.sh
--check`, and all 18 `scripts/check-*.sh` guards: **all PASS**

Closes #1224

---------

Signed-off-by: Raul Estrada <raulestradaa@gmail.com>
Signed-off-by: nihalpasham <nihalp@nvidia.com>
Co-authored-by: nihalpasham <nihalp@nvidia.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

cuda-oxide SIMT programming model: rustc backend, cargo oxide, cuda-device, cuda-host, book

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Generate the libm → libdevice math table instead of hand-editing three crates

2 participants