NVPTX: i128/u128 to float casts emit LLVM IR that the backend cannot lower
Nobody has claimed this yet.
- Dominant language
- Rust
- Stars
- 119k
- Forks
- 16.2k
- PR merge metrics
- PR metrics pending
Description
When compiling for NVPTX i128 as f__ and u128 as f__ casts produce an LLVM error. LLVM's NVPTX backend fails to lower sitofp i128 (and sitofp u128) instructions which get emitted by rustc.
Minimized example:
#![feature(abi_gpu_kernel, stdarch_nvptx)]
#![no_std]
use core::arch::nvptx::*;
#[unsafe(no_mangle)]
pub unsafe extern "gpu-kernel" fn cast_i128_to_float(dst: *mut f32, src: *const i128) {
let i = unsafe { _block_idx_x() * _block_dim_x() + _thread_idx_x() };
if (i as usize) > 0 {
return;
}
unsafe {
*dst = *src as f32; // <-- problematic cast
}
}
A relevant LLVM issue exists: https://github.com/llvm/llvm-project/issues/191013
This becomes visible to rust users even without using the unstable f128 feature.
Compiler Explorer repro: https://godbolt.org/z/WhEE8Y54x
Potential fix: Rust’s compiler_builtins already contains software implementations for these conversions. In particular, __floattisf, __floattidf, __floatuntisf, and __floatuntidf are implemented in terms of u128_to_f32_bits / u128_to_f64_bits and signed wrappers, rather than using recursive integer-to-float casts. So the missing piece may be that LLVM/NVPTX needs to legalize sitofp/uitofp i128 into these libcalls
LLVM is able to inline expand such conversions. The NVPTX backend currently does not enable this like for example AMDGPU does.
cc: @kjetilkjeka
@rustbot label +O-NVPTX
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start by running the minimized NVPTX example or its Compiler Explorer reproduction and inspect the generated LLVM IR for the i128/u128-to-float casts. Read the NVPTX backend's legalization path and compare it with AMDGPU's inline expansion support, using LLVM issue 191013 for context. Done means these casts compile for NVPTX without the backend lowering error.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- rust
- Domain
- compilers
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Quiet
- Clarity
- Mostly clear
- Newbie friendliness
- 45/100