Skip to content

feat(llvm-export): support i8/u8/i16/u16/f16/bool in device extern signatures - #412

Merged
nihalpasham merged 3 commits into
NVIDIA:mainfrom
honeyspoon:add-device-extern-small-types
Jul 22, 2026
Merged

nihalpasham merged 3 commits into
NVIDIA:mainfrom
honeyspoon:add-device-extern-small-types

Conversation

@honeyspoon

Copy link
Copy Markdown
Contributor

Summary

Remove the hard errors that reject small types (i8, u8, i16, u16, f16, bool) in device extern function signatures. Emit proper LLVM signext/zeroext ABI attributes for sub-32-bit integer types, enabling clean FFI boundaries for INT8 and FP16 kernels without pointer indirection.

Motivation

Device extern functions currently reject any parameter or return type smaller than 32 bits. This forces users to widen all arguments to i32/u32 on both sides of the FFI boundary, adding unnecessary casting and losing type safety. INT8 quantized inference and FP16 kernels are common use cases that need clean small-type signatures.

; Before: rejected with error
; After:
declare void @my_kernel(i32 signext, i32 zeroext, half)

Implementation

  • rustc-codegen-cuda (device_codegen.rs): replace rejection errors with proper type mapping -- signed types (i8, i16) map to SignExtInteger(32), unsigned types (u8, u16) map to ZeroExtInteger(32), f16 maps to Float16, bool maps to ZeroExtInteger(32)
  • llvm-export (externs.rs): add SignExtInteger(u32) and ZeroExtInteger(u32) variants to DeviceExternType, add write_llvm_with_attr() for attribute emission
  • llvm-export (module.rs, ops.rs): emit signext/zeroext attributes on both declarations and call sites
  • cuda-oxide-codegen (lower.rs): handle new variants in device_extern_type_to_pliron()

Testing

Validated on RTX A2000 (sm_86), nightly-2026-04-03:

cargo test -p llvm-export -p cuda-oxide-codegen -p mir-importer  # 1002 passed
cargo fmt --all -- --check                                        # clean
cargo clippy -p llvm-export -p cuda-oxide-codegen --tests -- -D warnings  # clean

@honeyspoon
honeyspoon force-pushed the add-device-extern-small-types branch from 4c425e3 to bd746b2 Compare July 20, 2026 13:15
…gnatures

Remove the hard errors that reject small types in device extern function
signatures and emit proper LLVM signext/zeroext ABI attributes for
sub-32-bit integer types.

Changes:
- rustc-codegen-cuda (device_codegen.rs): replace rejection errors with
  proper type mapping -- signed types use SignExtInteger(32), unsigned
  use ZeroExtInteger(32), f16 uses Float16, bool uses ZeroExtInteger(32)
- llvm-export (externs.rs): add SignExtInteger(u32) and
  ZeroExtInteger(u32) variants to DeviceExternType, add
  write_llvm_with_attr() for attribute emission
- llvm-export (module.rs, ops.rs): emit signext/zeroext attributes on
  both declarations and call sites
- cuda-oxide-codegen (lower.rs): handle new variants in
  device_extern_type_to_pliron()

Signed-off-by: abder <[email protected]>
@honeyspoon
honeyspoon force-pushed the add-device-extern-small-types branch from bd746b2 to f7ec081 Compare July 22, 2026 01:16
…vice externs

Redo the small-scalar device-extern ABI on top of the previous commit.
Widening the declared IR type to i32 mismatched both the clang/nvcc
LTOIR definitions the declaration must resolve against at nvJitLink LTO
time and cuda-oxide's own narrow i8/i16/i1 SSA values, so every
by-value integer/bool extern still failed to compile, just later and
with a worse error. Follow the clang NVPTXABIInfo / rustc nvptx64
shape instead: SignExtInteger/ZeroExtInteger store the ORIGINAL width
(signed 8/16, unsigned 8/16, bool as zeroext width 1), the declaration
keeps the narrow LLVM type, and only the attribute marks the NVPTX
.param.b32 widening, so no value conversions are needed anywhere.

Also fix return-position attribute placement: LLVM's grammar requires
the attribute BEFORE the type there (declare signext i8 @f(), call
signext i8 @f()); the previous suffix form did not parse. Parameter
position (i8 signext inside the parens) was already correct.

The legacy LLVM 7 libNVVM dialect now rejects sub-32-bit by-value
externs with a clean diagnostic mirroring the existing half rejection
instead of silently emitting unvalidated attributes.

Tests assert the canonical strings and add an llvm-as parse gate over
an emitted module with small params and a small return in both declare
and call positions.

Signed-off-by: nihalp <[email protected]>
@nihalpasham nihalpasham added enhancement New feature or request llvm-export Textual LLVM/NVVM IR exporter (llvm-export crate) codegen Device code-generation pipeline (Rust MIR to IR to PTX) interop Interop with external compilers / frontends labels Jul 22, 2026
@honeyspoon
honeyspoon force-pushed the add-device-extern-small-types branch from e56b4b4 to 79b065a Compare July 22, 2026 13:28
@nihalpasham
nihalpasham force-pushed the add-device-extern-small-types branch from 79b065a to e56b4b4 Compare July 22, 2026 15:09
@nihalpasham

nihalpasham commented Jul 22, 2026 •

Copy link
Copy Markdown
Collaborator

Thanks for the PR. This is the right direction, the old rejection message itself invited this ("rejected until their signext or zeroext attributes can be emitted"). The ABI shape needed a redo, which I pushed as maintainer commits on top.

  • Widening the declared IR type to i32 mismatched both the clang/nvcc-compiled definitions being linked against and cuda-oxide's own narrow i8/i1 SSA values (no promotion was inserted, so by-value integer/bool cases still failed, just later); and the return-position attribute emission produced IR llvm-as cannot parse (declare i8 signext @f()).
  • The redo keeps the NARROW type with the attribute — the clang NVPTXABIInfo / rustc nvptx64 shape: variants store the original width, declare signext i8 @f(i8 signext) with the attribute before the type in return position, zero inserted conversions. The legacy libNVVM path cleanly rejects sub-32-bit by-value externs, mirroring its existing f16 rejection.
  • Tests now assert the canonical strings plus an llvm-as parse gate, and device_ffi_test gained eight small-scalar extern functions (nvcc -dc -dlto LTOIR) with GPU-verified round-trips.

For example:

// CUDA C++, linked as LTOIR:  __device__ int8_t scale(int8_t x);
#[device] extern "C" { fn scale(x: i8) -> i8; }

before PR:   error: i8 rejected in device extern signatures
PR as filed: declares i32 -> mismatched definition, unparseable return attrs
after redo:  declare signext i8 @scale(i8 signext) -> links and runs

Reviewer note: llvm-export, mir-lower, and backend tests pass; the emitted module parses under llvm-as-21; device_ffi_test --emit-nvvm-ir passes 5/5 on an RTX 5090 including negative-i8 sign extension, u16 zero extension, bool, and f16 round-trips; compile-only smoketest passes. All hosted CI checks passed on the final head.

… NVVM path

Exercise the narrow-type signext/zeroext extern ABI end to end against
nvcc-compiled LTOIR: i8/u8/i16/u16/bool/f16 parameters AND returns.
The kernel checks sign extension of a negative i8, zero extension of a
high-bit u16, small-return round-trips for all four integer widths,
bool in both directions, and an f16 add returned as half. Verified on
sm_120 through the nvJitLink LTO pipeline.

This lives in a new standalone example, small_type_ffi_test, instead
of device_ffi_test: CI's examples-compile job builds the ltoir category
GPU-less with --emit-nvvm-ir --arch=sm_90, i.e. the legacy CUDA 12
LLVM 7 dialect, whose guards deliberately reject these externs:

  legacy NVVM IR cannot contain scalar f16 because cuda-oxide supports
  the CUDA 12 LLVM 7 dialect; use f32/f64 or the modern NVVM/PTX path

The test can only ever compile on the modern NVVM path, so smoketest
gains an ltoir-modern category that runs it with --emit-nvvm-ir at the
host arch on Blackwell+ hosts (CC >= 10.0), where execution must
succeed, and compiles it for the sm_100 floor elsewhere, requiring
only the NVVM IR plus its .target sidecar. device_ffi_test stays on
the legacy-compatible ltoir leg, unchanged.

The example's build_ltoir.sh makes the optional nvvm-dis text dump
best-effort: an nvvm-dis older than the CUDA toolkit cannot parse
newer LTOIR and, under set -e, would abort the build of an otherwise
valid binary LTOIR.

Signed-off-by: nihalp <[email protected]>
@nihalpasham
nihalpasham force-pushed the add-device-extern-small-types branch from e56b4b4 to 9286c6d Compare July 22, 2026 15:56
@nihalpasham
nihalpasham merged commit e098943 into NVIDIA:main Jul 22, 2026
33 checks passed
@honeyspoon
honeyspoon deleted the add-device-extern-small-types branch July 23, 2026 13:53
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

codegen Device code-generation pipeline (Rust MIR to IR to PTX) enhancement New feature or request interop Interop with external compilers / frontends llvm-export Textual LLVM/NVVM IR exporter (llvm-export crate)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants