Skip to content

[BUG]: libm::sqrtf / libm::sqrt fail device codegen with "Terminator kind InlineAsm ... not yet implemented" #1704

Open

Description

@praveenperera

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

  1. added 4 commits that reference this issue on Nov 4, 2026
    2ec8bbe
    15ba936
    aa6367f
    4b88b09
  2. praveenperera commented on Nov 4, 2026

    @praveenperera
    ContributorAuthor

    #1733 fixes this. mir-importer now unwraps the rustc_public Debug template, so libm's inlined sqrtss/sqrtsd asm (and the aarch64 fsqrt, frintn and fmadd bodies) lowers to the sqrt, rint and fma intrinsics. I've closed #1730, which overrode cross_crate_inlinable instead and made host-side libm::sqrtf slower.

  3. added
    cuda-oxideSIMT programming model: rustc backend, cargo oxide, cuda-device, cuda-host, book
    bugSomething isn't working
    intrinsicsDevice intrinsics and libdevice math mappings
    codegenDevice code-generation pipeline (Rust MIR to IR to PTX)
    on Nov 9, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workingcodegenDevice code-generation pipeline (Rust MIR to IR to PTX)cuda-oxideSIMT programming model: rustc backend, cargo oxide, cuda-device, cuda-host, bookintrinsicsDevice intrinsics and libdevice math mappings

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions