Repository navigation
Description
Description
Calling libm::sqrtf or libm::sqrt in a kernel fails to compile. The generated libdevice table (cuda-oxide/intrinsics/libdevice-math.md) lists both as lowered to __nv_sqrtf/__nv_sqrt with policy always, so the documented support and the build disagree. The libm_math example notes the gap in a comment ("sqrt is not yet supported via libm").
The cause is rustc's MIR inliner. libm 0.2.16 routes sqrtf through a per-host arch module; on x86_64 that is src/math/arch/x86.rs, a one-line asm!("sqrtss ..."). Both libm::sqrtf and the arch leaf are small enough that rustc marks them cross-crate inlinable (cross_crate_inlinable, checked in rustc_mir_transform/src/inline.rs), so they are folded into the kernel before the importer's libm call-site interception sees a libm::sqrtf call. What reaches the importer is an x86 InlineAsm terminator.
The same path applies to any libm function whose arch override for the build host is small enough to be auto-inlined. On x86_64 that is sqrt and sqrtf; the x86 fma/fmaf overrides are larger. On an aarch64 host, libm's arch/aarch64.rs has one-instruction asm! bodies for sqrt, sqrtf, rint, rintf, fma and fmaf, so those would likely fail the same way (not built on aarch64).
Error / actual behavior
Unsupported construct: Terminator kind InlineAsm { template: "[String(\"sqrtss \"), Placeholder { operand_idx: 0, ... }]", ... line_spans: "[.../libm-0.2.16/src/math/arch/x86.rs:13:14: 13:29 (#0)]", ... } not yet implemented
error: could not compile `sqrtf_repro` (bin "sqrtf_repro") due to 1 previous error
Expected behavior
libm::sqrtf(x) lowers to __nv_sqrtf (sqrt.rn.f32), like the other libm exports in the table.
One way to get there is for the codegen backend to override the cross_crate_inlinable query so that it returns false for items of the libm crate. The override takes effect while libm itself is compiled, and cargo-oxide builds every crate with the backend, so libm's exported symbols and the metadata downstream crates read stay consistent. Calls to libm::* then survive MIR inlining and the existing interception lowers them. The cost is on the host side of the same build, where a libm::sqrtf call is no longer inlined into the sqrtss instruction.
Minimal reproduction example
use cuda_device::{kernel, thread};
use cuda_host::cuda_module;
#[cuda_module]
mod kernels {
use super::*;
#[kernel]
pub unsafe fn k_sqrtf(o: *mut f32, x: *const f32) {
let i = thread::threadIdx_x() as usize;
unsafe { *o.add(i) = libm::sqrtf(*x.add(i)) };
}
}
fn main() {}libm = "0.2" (resolves to 0.2.16), opt-level = 3, cargo oxide build --arch sm_120. libm::floorf and libm::erff in the same setup lower fine, because their bodies are too large to be auto-inlined.
Operating System
Ubuntu 24.04, x86_64 host
GPU and GPU Architecture
Compile-time failure; built for sm_120 (RTX 5090)
CUDA driver version
n/a (no GPU needed to reproduce)
rustc version
1.99.1 (the repo's cuda-oxide/rust-toolchain.toml)
Additional context
Reproduced on main at cced2a35b (2026-11-02).
Activity
fix(cuda-oxide): keep libm calls out of MIR inline
test(libm_math): cover arch-dispatched sqrt and fma
test(libm_math): cover arch-dispatched sqrt and fma
test(libm_math): cover arch-dispatched sqrt and fma
praveenperera commentedon Nov 4, 2026
#1733 fixes this.
mir-importernow unwraps therustc_publicDebugtemplate, so libm's inlinedsqrtss/sqrtsdasm (and the aarch64fsqrt,frintnandfmaddbodies) lowers to the sqrt, rint and fma intrinsics. I've closed #1730, which overrodecross_crate_inlinableinstead and made host-sidelibm::sqrtfslower.