Skip to content

Experimental standalone API cannot compile libdevice calls, though the pipeline can now link them #485

Closed

Description

@alejandro-soto-franco

Summary

cuda_oxide_codegen::experimental cannot compile a module that calls sqrt, exp, tanh, or any other float intrinsic that lowers to libdevice. Those calls return CompileError::UnsupportedLinking, so a frontend generating ordinary elementwise kernels stops at the first transcendental.

This was a stated limitation of the experimental v1 when #314 merged:

v1 is revision-pinned and standalone compilation does not yet provide libdevice/device-extern linking, runtime loading.

Since then the shared pipeline gained IR-level libdevice linking, and the standalone path already supplies everything that linking needs. Opening this to ask how you want the gap closed, before writing any code, since it touches the API boundary.

Current behaviour

mir-lower routes float intrinsics to libdevice entry points, for example crates/mir-lower/src/convert/ops/call.rs:

Self::SqrtF32 => Ok("__nv_sqrtf"),
Self::SqrtF64 => Ok("__nv_sqrt"),

The standalone request is built with OutputPolicy::SelfContainedPtx:

pub(crate) fn for_standalone_ptx(...) -> Self {
    Self {
        device_externs: &[],
        output_policy: OutputPolicy::SelfContainedPtx,
        ...
    }
}

which is checked in run_module_pipeline:

if matches!(request.output_policy, OutputPolicy::SelfContainedPtx) {
    let symbols = unresolved_external_symbols(ctx, module);
    if !symbols.is_empty() {
        return Err(PipelineError::UnsupportedLinking { symbols });
    }
}

collect_unresolved_external_symbols records every body-less FuncOp whose name does not begin with llvm_, so the __nv_* declarations lowering just emitted are collected and the compilation is rejected.

The rustc frontend takes the other branch, OutputPolicy::ExternalLinkAllowed, and reaches the libdevice link further down the same function.

The capability is already wired to this path

The self-contained check sits at the top of the function; the libdevice link sits below it. The module is rejected before reaching the step that would resolve the symbols it was rejected for.

can_ir_link_libdevice is computed from the request's own toolchain:

let can_ir_link_libdevice = libdevice_path.is_some()
    && match request.toolchain {
        ToolchainPolicy::Discover => libdevice_ir_linking_available(request.backend),
        ToolchainPolicy::Explicit(toolchain) => toolchain.llvm_link.is_some(),
    };

The standalone path passes ToolchainPolicy::Explicit, and both Toolchain::discover and Toolchain::from_paths already resolve llvm-link against the chosen llc's LLVM major, documented on those constructors. So the predicate the PTX path uses to decide whether it can link libdevice is already true or false correctly for a standalone caller, and nothing consumes it there.

Impact

Any frontend that emits elementwise math is blocked. Compiling sqrt through the experimental API today gives:

standalone PTX cannot resolve external symbols: ["__nv_sqrtf"]

The API has no in-tree consumer other than mir-importer, so this stays invisible until someone drives it from outside. I hit it while building a frontend against the experimental API, which is what prompted this.

Direction I would suggest

Let the caller opt in, and keep the current behaviour as the default so no existing consumer changes:

  • Add a switch on CompileOptions, defaulting to self-contained, which selects ExternalLinkAllowed for the standalone request.
  • When it is on and can_ir_link_libdevice is false, fail with a distinct error naming the missing piece (libdevice.10.bc or a same-major llvm-link), so a caller can tell an unavailable toolchain from a module that genuinely cannot be linked.
  • Keep UnsupportedLinking for symbols that are not libdevice, since device externs are a separate question with a separate answer.
  • Update the v1 contract in lib.rs, which currently states the self-contained rule without qualification.

Questions before any code

  1. Does opting into linking belong in the experimental v1 at all, or should v1 stay self-contained by definition and linking wait for a v2 surface?
  2. If it does belong, should the switch be a CompileOptions field, or should the caller pass an explicit libdevice path so the API never searches the filesystem on its own?
  3. Should the NVVM IR fallback be available to standalone callers, or should the standalone path link at the IR level only and fail when it cannot?

Happy to take this to Discord instead if that suits the design discussion better. Related: #314, #96.

Activity

  1. added
    enhancement
    interop
    codegen
    on Jul 31, 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

    codegenenhancementinterop

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions