Skip to content

ICE: Architecture x86_64 does not support GpuKernel calling convention #138738

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
matthiaskrgr opened this issue Mar 20, 2025 · 3 comments · May be fixed by #141877 or #142134
Open

ICE: Architecture x86_64 does not support GpuKernel calling convention #138738

matthiaskrgr opened this issue Mar 20, 2025 · 3 comments · May be fixed by #141877 or #142134
Assignees
Labels
A-ABI Area: Concerning the application binary interface (ABI) A-LLVM Area: Code generation parts specific to LLVM. Both correctness bugs and optimization-related issues. C-bug Category: This is a bug. F-abi_ptx `#![feature(abi_ptx)]` I-ICE Issue: The compiler panicked, giving an Internal Compilation Error (ICE) ❄️ S-bug-has-test Status: This bug is tracked inside the repo by a `known-bug` test. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@matthiaskrgr
Copy link
Member

auto-reduced (treereduce-rust):

#![feature(abi_ptx)]
fn main() {
    let non_secure_function = unsafe {
        core::mem::transmute::<usize, extern "ptx-kernel" fn(i32, i32, i32, i32) -> i32>(0x10000004)
    };
    let mut toto = 5;
    toto += non_secure_function(toto, 2, 3, 5);
}

original:

// gate-test-abi_c_cmse_nonsecure_call
#[allow(unsupported_fn_ptr_calling_conventions)]
fn main() {
    let non_secure_function = unsafe {
        core::mem::transmute::<usize, extern "ptx-kernel" fn(i32, i32, i32, i32) -> i32>(
            //~^ ERROR [E0658]
            0x10000004,
        )
    };
    let mut toto = 5;
    toto += non_secure_function(toto, 2, 3, 5);
}

Version information

rustc 1.87.0-nightly (70237a8cb 2025-03-20)
binary: rustc
commit-hash: 70237a8cb905fc48ed6148d2f2dba756535a5940
commit-date: 2025-03-20
host: x86_64-unknown-linux-gnu
release: 1.87.0-nightly
LLVM version: 20.1.0

Possibly related line of code:

Conv::GpuKernel => {
if arch == "amdgpu" {
llvm::AmdgpuKernel
} else if arch == "nvptx64" {
llvm::PtxKernel
} else {
panic!("Architecture {arch} does not support GpuKernel calling convention");
}
}
Conv::AvrInterrupt => llvm::AvrInterrupt,
Conv::AvrNonBlockingInterrupt => llvm::AvrNonBlockingInterrupt,
Conv::ArmAapcs => llvm::ArmAapcsCallConv,
Conv::Msp430Intr => llvm::Msp430Intr,

Command:
/home/matthias/.rustup/toolchains/master/bin/rustc -Zcrate-attr=feature(abi_ptx)

Program output

warning: the calling convention "ptx-kernel" is not supported on this target
 --> /tmp/icemaker_global_tempdir.5RiKb1aS18Mo/rustc_testrunner_tmpdir_reporting.vC9uw8ssapd6/mvce.rs:3:39
  |
3 |         core::mem::transmute::<usize, extern "ptx-kernel" fn(i32, i32, i32, i32) -> i32>(0x10000004)
  |                                       ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  |
  = warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
  = note: for more information, see issue #130260 <https://github.com/rust-lang/rust/issues/130260>
  = note: `#[warn(unsupported_fn_ptr_calling_conventions)]` on by default

warning: value assigned to `toto` is never read
 --> /tmp/icemaker_global_tempdir.5RiKb1aS18Mo/rustc_testrunner_tmpdir_reporting.vC9uw8ssapd6/mvce.rs:6:5
  |
6 |     toto += non_secure_function(toto, 2, 3, 5);
  |     ^^^^
  |
  = help: maybe it is overwritten before being read?
  = note: `#[warn(unused_assignments)]` on by default


thread 'rustc' panicked at compiler/rustc_codegen_llvm/src/abi.rs:680:21:
Architecture x86_64 does not support GpuKernel calling convention
stack backtrace:
   0:     0x79ada6830003 - <std::sys::backtrace::BacktraceLock::print::DisplayBacktrace as core::fmt::Display>::fmt::h879fffc75fdea4ca
   1:     0x79ada7005a58 - core::fmt::write::h4872d0510b46ecc6
   2:     0x79ada83c5751 - std::io::Write::write_fmt::hbd981463303fde9c
   3:     0x79ada682fe62 - std::sys::backtrace::BacktraceLock::print::hcf9aac352b7eac82
   4:     0x79ada683271a - std::panicking::default_hook::{{closure}}::hedab5cca755b9743
   5:     0x79ada683229f - std::panicking::default_hook::h1fcdc03d8389a618
   6:     0x79ada589bf20 - std[c9702a20cd481928]::panicking::update_hook::<alloc[49ba14cf04ef8a65]::boxed::Box<rustc_driver_impl[8a35e1c27f9e238f]::install_ice_hook::{closure#1}>>::{closure#0}
   7:     0x79ada6832f93 - std::panicking::rust_panic_with_hook::ha0e51fe2d11054e2
   8:     0x79ada6832c8a - std::panicking::begin_panic_handler::{{closure}}::hc5cbb737c532592b
   9:     0x79ada68304d9 - std::sys::backtrace::__rust_end_short_backtrace::hf92f52b58bf43613
  10:     0x79ada683294d - __rustc[bd409a3545e2ef9f]::rust_begin_unwind
  11:     0x79ada326b750 - core::panicking::panic_fmt::hfabf34321fa7b35b
  12:     0x79ada72be178 - <rustc_target[4ee8a907d88b8e02]::callconv::FnAbi<rustc_middle[d57d21cb3f97984b]::ty::Ty> as rustc_codegen_llvm[3ffc3f5f98c79521]::abi::FnAbiLlvmExt>::apply_attrs_callsite
  13:     0x79ada72bbcbf - <rustc_codegen_llvm[3ffc3f5f98c79521]::builder::GenericBuilder<rustc_codegen_llvm[3ffc3f5f98c79521]::context::FullCx> as rustc_codegen_ssa[91b4b525e278393]::traits::builder::BuilderMethods>::call
  14:     0x79ada72b521c - <rustc_codegen_ssa[91b4b525e278393]::mir::block::TerminatorCodegenHelper>::do_call::<rustc_codegen_llvm[3ffc3f5f98c79521]::builder::GenericBuilder<rustc_codegen_llvm[3ffc3f5f98c79521]::context::FullCx>>
  15:     0x79ada3f29b53 - <rustc_codegen_ssa[91b4b525e278393]::mir::FunctionCx<rustc_codegen_llvm[3ffc3f5f98c79521]::builder::GenericBuilder<rustc_codegen_llvm[3ffc3f5f98c79521]::context::FullCx>>>::codegen_terminator
  16:     0x79ada80b87a9 - rustc_codegen_ssa[91b4b525e278393]::mir::codegen_mir::<rustc_codegen_llvm[3ffc3f5f98c79521]::builder::GenericBuilder<rustc_codegen_llvm[3ffc3f5f98c79521]::context::FullCx>>
  17:     0x79ada809c351 - rustc_codegen_llvm[3ffc3f5f98c79521]::base::compile_codegen_unit::module_codegen
  18:     0x79ada8165995 - <rustc_codegen_llvm[3ffc3f5f98c79521]::LlvmCodegenBackend as rustc_codegen_ssa[91b4b525e278393]::traits::backend::ExtraBackendMethods>::compile_codegen_unit
  19:     0x79ada81622d7 - <rustc_codegen_llvm[3ffc3f5f98c79521]::LlvmCodegenBackend as rustc_codegen_ssa[91b4b525e278393]::traits::backend::CodegenBackend>::codegen_crate
  20:     0x79ada8191405 - <rustc_interface[1dd91e2665f7489c]::queries::Linker>::codegen_and_build_linker
  21:     0x79ada818ecb0 - rustc_interface[1dd91e2665f7489c]::passes::create_and_enter_global_ctxt::<core[fb772cbc65c9782a]::option::Option<rustc_interface[1dd91e2665f7489c]::queries::Linker>, rustc_driver_impl[8a35e1c27f9e238f]::run_compiler::{closure#0}::{closure#2}>::{closure#2}::{closure#0}
  22:     0x79ada813fcaf - rustc_interface[1dd91e2665f7489c]::interface::run_compiler::<(), rustc_driver_impl[8a35e1c27f9e238f]::run_compiler::{closure#0}>::{closure#1}
  23:     0x79ada7fbbfc8 - std[c9702a20cd481928]::sys::backtrace::__rust_begin_short_backtrace::<rustc_interface[1dd91e2665f7489c]::util::run_in_thread_with_globals<rustc_interface[1dd91e2665f7489c]::util::run_in_thread_pool_with_globals<rustc_interface[1dd91e2665f7489c]::interface::run_compiler<(), rustc_driver_impl[8a35e1c27f9e238f]::run_compiler::{closure#0}>::{closure#1}, ()>::{closure#0}, ()>::{closure#0}::{closure#0}, ()>
  24:     0x79ada7fbc8b4 - <<std[c9702a20cd481928]::thread::Builder>::spawn_unchecked_<rustc_interface[1dd91e2665f7489c]::util::run_in_thread_with_globals<rustc_interface[1dd91e2665f7489c]::util::run_in_thread_pool_with_globals<rustc_interface[1dd91e2665f7489c]::interface::run_compiler<(), rustc_driver_impl[8a35e1c27f9e238f]::run_compiler::{closure#0}>::{closure#1}, ()>::{closure#0}, ()>::{closure#0}::{closure#0}, ()>::{closure#1} as core[fb772cbc65c9782a]::ops::function::FnOnce<()>>::call_once::{shim:vtable#0}
  25:     0x79ada7fbdcb7 - std::sys::pal::unix::thread::Thread::new::thread_start::h2500dd9e4f747739
  26:     0x79ada1ea370a - <unknown>
  27:     0x79ada1f27aac - <unknown>
  28:                0x0 - <unknown>

error: the compiler unexpectedly panicked. this is a bug.

note: we would appreciate a bug report: https://github.com/rust-lang/rust/issues/new?labels=C-bug%2C+I-ICE%2C+T-compiler&template=ice.md

note: please make sure that you have updated to the latest nightly

note: rustc 1.87.0-nightly (70237a8cb 2025-03-20) running on x86_64-unknown-linux-gnu

note: compiler flags: -Z crate-attr=feature(abi_ptx) -Z dump-mir-dir=dir

query stack during panic:
end of query stack
warning: 2 warnings emitted


@rustbot label +F-abi_ptx

@matthiaskrgr matthiaskrgr added C-bug Category: This is a bug. I-ICE Issue: The compiler panicked, giving an Internal Compilation Error (ICE) ❄️ T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Mar 20, 2025
@rustbot rustbot added the needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. label Mar 20, 2025
@matthiaskrgr
Copy link
Member Author

bisects to is #136807

@jieyouxu jieyouxu added the F-abi_ptx `#![feature(abi_ptx)]` label Mar 20, 2025
@Noratrieb
Copy link
Member

@workingjubilee looks like you forgot a check

@matthiaskrgr
Copy link
Member Author

smaller

#![feature(abi_ptx)]
fn main() {
    let a = unsafe { core::mem::transmute::<usize, extern "ptx-kernel" fn(i32)>(4) }(2);
}

@jieyouxu jieyouxu added A-LLVM Area: Code generation parts specific to LLVM. Both correctness bugs and optimization-related issues. A-ABI Area: Concerning the application binary interface (ABI) and removed needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. labels Apr 30, 2025
@matthiaskrgr matthiaskrgr added the S-bug-has-test Status: This bug is tracked inside the repo by a `known-bug` test. label May 2, 2025
@workingjubilee workingjubilee self-assigned this May 31, 2025
rust-bors bot added a commit that referenced this issue Jun 6, 2025
Reject `extern "{abi}"` when the target does not support it

## What

Promote [`unsupported_fn_ptr_calling_conventions`] from a warning to a hard error, making sure edge-cases will not escape. We now emit hard errors for every case we would return `Invalid` from `AbiMap::canonize_abi` during AST to HIR lowering. In particular, these architecture-specific ABIs now only compile on their architectures[^1]:
  - amdgpu: "gpu-kernel"
  - arm: "aapcs", "C-cmse-nonsecure-entry"
  - avr: "avr-interrupt", "avr-non-blocking-interrupt"
  - msp430: "msp430-interrupt"
  - nvptx64: "gpu-kernel", "ptx-kernel"
  - riscv32 and riscv64: "riscv-interrupt-machine", "riscv-interrupt-supervisor"
  - x86: "thiscall"
  - x86 and x86_64: "x86-interrupt"
  - x86_64: "sysv64", "win64"

The panoply of ABIs that are logically x86-specific but actually permitted on all Windows targets remain supported on Windows, as they were before. For non-Windows targets they error if the architecture does not match.

Moving the check into AST lowering **is itself a breaking change in rare cases**, above and beyond the cases rustc currently warns about. See "Why or Why Not" for details.

## How

We modify rustc_ast_lowering to prevent unsupported ABIs from leaking through the HIR without being checked for target support. Previously ad-hoc checking on various HIR items required making sure we check every HIR item which could contain an `extern "{abi}"` string. This is a losing proposition compared to gating the lowering itself.

As a consequence, unsupported ABI strings will now hard-error instead of triggering the FCW `unsupported_fn_ptr_calling_conventions`.

However, per #86232 this does cause errors for rare usages of `extern "{abi}"` that were theoretically possible to write in Rust source, without previous warning or error. For instance, trait declarations without impls were never checked. These are the exact kinds of leakages that this new approach prevents.

This differs from the following PRs:
- #141435 is orthogonal, as it adds a new lint for ABIs we have not warned on and are not touched by this PR
- #141877 is subsumed by this, in that this simply cuts out bad functionality instead of adding epicycles for stable code

## Why or Why Not

We already made the decision to issue the `unsupported_fn_ptr_calling_conventions` future compatibility warning. It has warned in dependencies since #135767, which reached stable with Rust 1.87. That was released on 2025 May 17, and it is now June. As we already had erred on these ABI strings in most other positions, and warn on stable for function pointer types, this breakage has had reasonable foreshadowing.

Upgrading the warning to an error addresses a real problem. In some cases the Rust compiler can attempt to actually compute the ABI for calling a function. We could accept this case and compute unsupported ABIs according to some other ABI, silently[^0]. However, this obviously exposes Rust to errors in codegen. We cannot lower directly to the "obvious" ABI and then trust code generators like LLVM to reliably error on these cases, either.

Refactoring the compiler so we could defer more ABI computations would be possible, but seems weakly motivated. Even if we succeeded, we would at minimum risk:
- exposing the "whack-a-mole" problem but "approaching linking" instead of "leaving AST"
- making it harder to reason about functions we *can* lower further
- complicating the compiler for no clear benefit

A deprecation cycle for the edge-cases could be implemented first, but it is not very useful for such marginal cases, like this trait declaration without a definition:
```rust
pub trait UsedToSneakBy {
    pub extern "gpu-kernel" fn sneaky();
}
```

Upon any impl, even for provided fn within trait declarations, e.g. `pub extern "gpu-kernel" fn sneaky() {}`, different HIR types were used which would, in fact, get checked. Likewise with anything with function pointers. Thus we would be discussing deprecation cycles for code that is impotent or forewarned[^2].

Implementing a deprecation cycle _is_ possible, but it would likely require emitting multiple of a functionally identical warning or error on code that would not have multiple warnings or errors before. It is also not clear to me we would not find **another**, even more marginal edge-case that slipped through, as "things slip through" is the motivation for checking earlier. Additional effort spent on additional warnings should require committing to a hard limit first.

r? lang

Fixes #86232
Fixes #132430
Fixes #138738
Fixes #142107

[`unsupported_fn_ptr_calling_conventions`]: #130260
[^1]: Some already will not compile, due to reaching ICEs or LLVM errors.
[^0]:  We already do this for all `AbiStr` we cannot parse, pretending they are `ExternAbi::Rust`, but we also emit an error to prevent reaching too far into codegen.
[^2]: It actually did appear in two cases in rustc's test suite because we are a collection of Rust edge-cases by the simple fact that we don't care if the code actually runs. These cases were excised in c1db989.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-ABI Area: Concerning the application binary interface (ABI) A-LLVM Area: Code generation parts specific to LLVM. Both correctness bugs and optimization-related issues. C-bug Category: This is a bug. F-abi_ptx `#![feature(abi_ptx)]` I-ICE Issue: The compiler panicked, giving an Internal Compilation Error (ICE) ❄️ S-bug-has-test Status: This bug is tracked inside the repo by a `known-bug` test. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
5 participants