[Clang] CodeGen assertion when __attribute__((device_kernel)) is used outside OpenCL mode
- Dominant language
- LLVM
- Stars
- 40.5k
- Forks
- 18.7k
- PR merge metrics
- PR metrics pending
Description
Applying `__attribute__((device_kernel))` to an ordinary C function (ie without OpenCL compilation mode enabled) triggers an assertion failure during CodeGen in assertion-enabled builds.
Sema emits `-Wignored-attributes`, but CodeGen still encounters the attribute and attempts to use OpenCL-specific calling convention logic, leading to an assertion failure in `TargetCodeGenInfo`.
### Reproducer
Tested on `clang version 23.0.0git (https://github.com/llvm/llvm-project.git ee5e682f4db85afca52ac6519a4b9bf0bfab4cf9)`
See https://godbolt.org/z/Mr1v8WfGe
```c
__attribute__((device_kernel)) void kernel(void) {}
```
Compile on Clang trunk with assertions enabled:
```
clang test.c -S
```
### Expected behaviour:
Either attribute is rejected with an error in Sema, or CodeGen ignores it safely in non-OpenCL mode.
### Actual behaviour:
Assertion failure in CodeGen
```
clang::CodeGen::TargetCodeGenInfo::getDeviceKernelCallingConv() const: Assertion `getABIInfo().getContext().getLangOpts().OpenCL && "Kernel calling convention only defined for OpenCL"' failed.
```
Backtrace
```txt
:1:16: warning: 'device_kernel' calling convention is not supported for this target [-Wignored-attributes]
1 | __attribute__((device_kernel)) void kernel(void) {}
| ^
clang: /root/llvm-project/llvm/tools/clang/lib/CodeGen/TargetInfo.cpp:123: virtual unsigned int clang::CodeGen::TargetCodeGenInfo::getDeviceKernelCallingConv() const: Assertion `getABIInfo().getContext().getLangOpts().OpenCL && "Kernel calling convention only defined for OpenCL"' failed.
PLEASE submit a bug report to https://github.com/llvm/llvm-project/issues/ and include the crash backtrace and dumped files.
Stack dump:
0. Program arguments: /opt/compiler-explorer/clang-assertions-trunk/bin/clang -g -o /app/output.s -mllvm --x86-asm-syntax=intel -fno-verbose-asm -S --gcc-toolchain=/opt/compiler-explorer/gcc-snapshot -fcolor-diagnostics -fno-crash-diagnostics -S
1. parser at end of file
2. :1:37: LLVM IR generation of declaration 'kernel'
3. :1:37: Generating code for declaration 'kernel'
#0 0x0000000004371078 llvm::sys::PrintStackTrace(llvm::raw_ostream&, int) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+0x4371078)
#1 0x000000000436deb4 llvm::sys::RunSignalHandlers() (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+0x436deb4)
#2 0x000000000436e4d4 llvm::sys::CleanupOnSignal(unsigned long) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+0x436e4d4)
#3 0x00000000042aa0f8 CrashRecoverySignalHandler(int) CrashRecoveryContext.cpp:0:0
#4 0x00007e07ed445330 (/lib/x86_64-linux-gnu/libc.so.6+0x45330)
#5 0x00007e07ed49eb2c pthread_kill (/lib/x86_64-linux-gnu/libc.so.6+0x9eb2c)
#6 0x00007e07ed44527e raise (/lib/x86_64-linux-gnu/libc.so.6+0x4527e)
#7 0x00007e07ed4288ff abort (/lib/x86_64-linux-gnu/libc.so.6+0x288ff)
#8 0x00007e07ed42881b (/lib/x86_64-linux-gnu/libc.so.6+0x2881b)
#9 0x00007e07ed43b517 (/lib/x86_64-linux-gnu/libc.so.6+0x3b517)
#10 0x0000000004c6dde4 (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+0x4c6dde4)
#11 0x0000000004d50580 clang::CodeGen::CodeGenTypes::arrangeLLVMFunctionInfo(clang::CanQual, clang::CodeGen::FnInfoOpts, llvm::ArrayRef>, clang::FunctionType::ExtInfo, llvm::ArrayRef, clang::CodeGen::RequiredArgs) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+0x4d50580)
#12 0x0000000004d50f13 arrangeLLVMFunctionInfo(clang::CodeGen::CodeGenTypes&, bool, llvm::SmallVectorImpl>&, clang::CanQual) CGCall.cpp:0:0
#13 0x0000000004d55b56 clang::CodeGen::CodeGenTypes::arrangeFunctionDeclaration(clang::GlobalDecl) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+0x4d55b56)
#14 0x0000000004adce06 clang::CodeGen::CodeGenModule::EmitGlobalFunctionDefinition(clang::GlobalDecl, llvm::GlobalValue*) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+0x4adce06)
#15 0x0000000004ad7c44 clang::CodeGen::CodeGenModule::EmitGlobalDefinition(clang::GlobalDecl, llvm::GlobalValue*) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+0x4ad7c44)
#16 0x0000000004ad8433 clang::CodeGen::CodeGenModule::EmitGlobal(clang::GlobalDecl) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+0x4ad8433)
#17 0x0000000004ae4633 clang::CodeGen::CodeGenModule::EmitTopLevelDecl(clang::Decl*) (.part.0) CodeGenModule.cpp:0:0
#18 0x0000000004e616e9 (anonymous namespace)::CodeGeneratorImpl::HandleTopLevelDecl(clang::DeclGroupRef) ModuleBuilder.cpp:0:0
#19 0x0000000004e51c04 clang::BackendConsumer::HandleTopLevelDecl(clang::DeclGroupRef) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+0x4e51c04)
#20 0x0000000006c1b374 clang::ParseAST(clang::Sema&, bool, bool) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+0x6c1b374)
#21 0x0000000004e5f478 clang::CodeGenAction::ExecuteAction() (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+0x4e5f478)
#22 0x0000000005195578 clang::FrontendAction::Execute() (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+0x5195578)
#23 0x0000000005112d8d clang::CompilerInstance::ExecuteAction(clang::FrontendAction&) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+0x5112d8d)
#24 0x00000000052938dd clang::ExecuteCompilerInvocation(clang::CompilerInstance*) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+0x52938dd)
#25 0x0000000000e2048b cc1_main(llvm::ArrayRef, char const*, void*) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+0xe2048b)
#26 0x0000000000e16cca ExecuteCC1Tool(llvm::SmallVectorImpl&, llvm::ToolContext const&, llvm::IntrusiveRefCntPtr) driver.cpp:0:0
#27 0x0000000000e16e4d int llvm::function_ref&)>::callback_fn&)>(long, llvm::SmallVectorImpl&) driver.cpp:0:0
#28 0x0000000004ed1a19 void llvm::function_ref::callback_fn>, std::__cxx11::basic_string, std::allocator>*, bool*) const::'lambda'()>(long) Job.cpp:0:0
#29 0x00000000042aa534 llvm::CrashRecoveryContext::RunSafely(llvm::function_ref) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+0x42aa534)
#30 0x0000000004ed2066 clang::driver::CC1Command::Execute(llvm::ArrayRef>, std::__cxx11::basic_string, std::allocator>*, bool*) const (.part.0) Job.cpp:0:0
#31 0x0000000004e8e992 clang::driver::Compilation::ExecuteCommand(clang::driver::Command const&, clang::driver::Command const*&, bool) const (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+0x4e8e992)
#32 0x0000000004e8f93e clang::driver::Compilation::ExecuteJobs(clang::driver::JobList const&, llvm::SmallVectorImpl>&, bool) const (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+0x4e8f93e)
#33 0x0000000004e97b45 clang::driver::Driver::ExecuteCompilation(clang::driver::Compilation&, llvm::SmallVectorImpl>&) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+0x4e97b45)
#34 0x0000000000e1c794 clang_main(int, char**, llvm::ToolContext const&) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+0xe1c794)
#35 0x0000000000cc029a main (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+0xcc029a)
#36 0x00007e07ed42a1ca (/lib/x86_64-linux-gnu/libc.so.6+0x2a1ca)
#37 0x00007e07ed42a28b __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x2a28b)
#38 0x0000000000e16765 _start (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+0xe16765)
clang: error: clang frontend command failed due to signal (use -v to see invocation)
Compiler returned: 134
```
Contributor guide
Assessment
This issue has not been assessed yet.