llvm / llvm/llvm-project

[CIR][HIP] Assertion fail due to exception throw forced into device code by --hipstdpar

Open
#221,941 0 comments 0 reactions 0 assignees View on GitHub
ClangIR crash
Dominant language
LLVM
Stars
40.5k
Forks
18.7k
PR merge metrics
PR metrics pending

Description

I've been poking around HIP support via ClangIR and I noticed the following assertion failure:

```llvm
// reproducer.hip
inline void check(bool ok) {
throw 1;
}

int main() {
check(true);
}
```
compile with `clang-24 -cc1 -triple amdgcn reproducer.hip -emit-llvm -fcuda-is-device -fclangir --hipstdpar`

It will produce the following assertion failure:

```
clang-24: /work/mpodchis/source/llvm-cir/llvm-project/llvm/include/llvm/Support/Casting.h:560: decltype(auto) llvm::cast(const From&) [with To = cir::GlobalViewAttr; From = mlir::Attribute]: Assertion `isa(Val) && "cast() argument of incompatible type!"' 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: /work/mpodchis/source/llvm-cir/llvm-project/build/bin/clang-24 -cc1 -triple amdgcn reproducer.hip -emit-llvm -fcuda-is-device -fclangir --hipstdpar
1.»~ parser at end of file
#0 0x00005bc65e5068c9 llvm::sys::PrintStackTrace(llvm::raw_ostream&, int) (/work/mpodchis/source/llvm-cir/llvm-project/build/bin/clang-24+0x36b38c9)
#1 0x00005bc65e5035bc llvm::sys::RunSignalHandlers() (/work/mpodchis/source/llvm-cir/llvm-project/build/bin/clang-24+0x36b05bc)
#2 0x00005bc65e503726 SignalHandler(int, siginfo_t*, void*) Signals.cpp:0:0
#3 0x00005bc65e5037ec SignalHandlerTerminate(int, siginfo_t*, void*) Signals.cpp:0:0
#4 0x0000715c14242520 (/lib/x86_64-linux-gnu/libc.so.6+0x42520)
#5 0x0000715c142969fc __pthread_kill_implementation ./nptl/pthread_kill.c:44:76
#6 0x0000715c142969fc __pthread_kill_internal ./nptl/pthread_kill.c:78:10
#7 0x0000715c142969fc pthread_kill ./nptl/pthread_kill.c:89:10
#8 0x0000715c14242476 gsignal ./signal/../sysdeps/posix/raise.c:27:6
#9 0x0000715c142287f3 abort ./stdlib/abort.c:81:7
#10 0x0000715c1422871b _nl_load_domain ./intl/loadmsgcat.c:1177:9
#11 0x0000715c14239e96 (/lib/x86_64-linux-gnu/libc.so.6+0x39e96)
#12 0x00005bc65f8595f4 (anonymous namespace)::CIRGenItaniumCXXABI::emitThrow(clang::CIRGen::CIRGenFunction&, clang::CXXThrowExpr const*) CIRGenItaniumCXXABI.cpp:0:0
#13 0x00005bc65f84c183 clang::StmtVisitorBase::Visit(clang::Stmt*) CIRGenExprScalar.cpp:0:0
#14 0x00005bc65f852597 clang::CIRGen::CIRGenFunction::emitScalarExpr(clang::Expr const*, bool) (/work/mpodchis/source/llvm-cir/llvm-project/build/bin/clang-24+0x49ff597)
#15 0x00005bc65f7ee42e clang::CIRGen::CIRGenFunction::emitAnyExpr(clang::Expr const*, clang::CIRGen::AggValueSlot, bool) (/work/mpodchis/source/llvm-cir/llvm-project/build/bin/clang-24+0x499b42e)
#16 0x00005bc65f7ee76f clang::CIRGen::CIRGenFunction::emitIgnoredExpr(clang::Expr const*) (/work/mpodchis/source/llvm-cir/llvm-project/build/bin/clang-24+0x499b76f)
#17 0x00005bc65f6dff7d clang::CIRGen::CIRGenFunction::emitStmt(clang::Stmt const*, bool, llvm::ArrayRef) (/work/mpodchis/source/llvm-cir/llvm-project/build/bin/clang-24+0x488cf7d)
#18 0x00005bc65f6e5307 clang::CIRGen::CIRGenFunction::emitCompoundStmtWithoutScope(clang::CompoundStmt const&, clang::CIRGen::Address*, clang::CIRGen::AggValueSlot) (/work/mpodchis/source/llvm-cir/llvm-project/build/bin/clang-24+0x4892307)
#19 0x00005bc65f6767a8 clang::CIRGen::CIRGenFunction::emitFunctionBody(clang::Stmt const*) (/work/mpodchis/source/llvm-cir/llvm-project/build/bin/clang-24+0x48237a8)
#20 0x00005bc65f679584 clang::CIRGen::CIRGenFunction::generateCode(clang::GlobalDecl, cir::FuncOp, cir::FuncType) (/work/mpodchis/source/llvm-cir/llvm-project/build/bin/clang-24+0x4826584)
#21 0x00005bc65f69e9a9 clang::CIRGen::CIRGenModule::emitGlobalFunctionDefinition(clang::GlobalDecl, mlir::Operation*) (/work/mpodchis/source/llvm-cir/llvm-project/build/bin/clang-24+0x484b9a9)
#22 0x00005bc65f6a2f95 clang::CIRGen::CIRGenModule::emitDeferred() (/work/mpodchis/source/llvm-cir/llvm-project/build/bin/clang-24+0x484ff95)
#23 0x00005bc65f6a329e clang::CIRGen::CIRGenModule::release() (/work/mpodchis/source/llvm-cir/llvm-project/build/bin/clang-24+0x485029e)
#24 0x00005bc65f538e26 cir::CIRGenConsumer::HandleTranslationUnit(clang::ASTContext&) (/work/mpodchis/source/llvm-cir/llvm-project/build/bin/clang-24+0x46e5e26)
#25 0x00005bc662a5680c clang::ParseAST(clang::Sema&, bool, bool) (/work/mpodchis/source/llvm-cir/llvm-project/build/bin/clang-24+0x7c0380c)
#26 0x00005bc65f1de9c1 clang::FrontendAction::Execute() (/work/mpodchis/source/llvm-cir/llvm-project/build/bin/clang-24+0x438b9c1)
#27 0x00005bc65f1687f5 clang::CompilerInstance::ExecuteAction(clang::FrontendAction&) (/work/mpodchis/source/llvm-cir/llvm-project/build/bin/clang-24+0x43157f5)
#28 0x00005bc65f2b5d60 clang::ExecuteCompilerInvocation(clang::CompilerInstance*) (/work/mpodchis/source/llvm-cir/llvm-project/build/bin/clang-24+0x4462d60)
#29 0x00005bc65c3b4dbe cc1_main(llvm::ArrayRef, char const*, void*) (/work/mpodchis/source/llvm-cir/llvm-project/build/bin/clang-24+0x1561dbe)
#30 0x00005bc65c3aac1a ExecuteCC1Tool(llvm::SmallVectorImpl&, llvm::ToolContext const&, llvm::IntrusiveRefCntPtr) driver.cpp:0:0
#31 0x00005bc65c3aeef5 clang_main(int, char**, llvm::ToolContext const&) (/work/mpodchis/source/llvm-cir/llvm-project/build/bin/clang-24+0x155bef5)
#32 0x00005bc65c315ef1 main (/work/mpodchis/source/llvm-cir/llvm-project/build/bin/clang-24+0x14c2ef1)
#33 0x0000715c14229d90 __libc_start_call_main ./csu/../sysdeps/nptl/libc_start_call_main.h:58:16
#34 0x0000715c14229e40 call_init ./csu/../csu/libc-start.c:128:20
#35 0x0000715c14229e40 __libc_start_main ./csu/../csu/libc-start.c:379:5
#36 0x00005bc65c3aa245 _start (/work/mpodchis/source/llvm-cir/llvm-project/build/bin/clang-24+0x1557245)
```

The problem is that `getAddrOfRTTIDescriptor` used by `CIRGenItaniumCXXABI::emitThrow` returns null `mlir::Attribute` because rtti is disabled (which I think is expected for GPU device compilation) which is then hard casted to `GlobalViewAttr`. Original codegen just emits null ptr for rtti descriptor, but there is no hard casts at the point where it is emitted so it just ends up in the emitted IR.

Contributor guide

Open the contributing guide

Research direction

Reproduce the assertion with the supplied reproducer and clang-24 command, then inspect CIRGenItaniumCXXABI::emitThrow and getAddrOfRTTIDescriptor in the ClangIR code path. Compare this with the original codegen behavior for device compilation and verify that the same case no longer hard-casts a missing RTTI attribute or asserts.

Written by the indexing model from the issue text.

Assessment

Tech stack
cpp
Domain
compilers
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Active
Clarity
Mostly clear
Newbie friendliness
52/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.