llvm / llvm/llvm-project

[PowerPC] Clang ICE in ConstantFolding.cpp when compiling __builtin_ppc_national2packed on PowerPC

Open
#216,669 2 comments 0 reactions 0 assignees View on GitHub
clang:codegen crash generated by fuzzer
Dominant language
LLVM
Stars
40.5k
Forks
18.7k
PR merge metrics
PR metrics pending

Description

Reproducer:
https://godbolt.org/z/hv8nx1fsM

Compile with Clang trunk for PowerPC64LE: --target=powerpc64le-linux-gnu -mcpu=power9.
The crash is reproducible with Clang trunk and was first reproducible with Clang 21.1.

```c++
vector unsigned char tBcd_National2packed_imm1(vector unsigned char a) {
return __builtin_ppc_national2packed(a, '\1', (int)0);
}
```

Backtrace:

```
clang++: /root/llvm-project/llvm/lib/Analysis/ConstantFolding.cpp:183: llvm::Constant* {anonymous}::FoldBitCast(llvm::Constant*, llvm::Type*, const llvm::DataLayout&): Assertion `CastInst::castIsValid(Instruction::BitCast, C, DestTy) && "Invalid constantexpr bitcast!"' 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 --target=powerpc64le-linux-gnu -mcpu=power9
1. parser at end of file
2. :1:22: LLVM IR generation of declaration 'tBcd_National2packed_imm1'
3. :1:22: Generating code for declaration 'tBcd_National2packed_imm1'
#0 0x000000000446e988 llvm::sys::PrintStackTrace(llvm::raw_ostream&, int) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x446e988)
#1 0x000000000446b834 llvm::sys::RunSignalHandlers() (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x446b834)
#2 0x000000000446be6c llvm::sys::CleanupOnSignal(unsigned long) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x446be6c)
#3 0x00000000043aa748 CrashRecoverySignalHandler(int) CrashRecoveryContext.cpp:0:0
#4 0x000073efccc45330 (/lib/x86_64-linux-gnu/libc.so.6+0x45330)
#5 0x000073efccc9ec0c pthread_kill (/lib/x86_64-linux-gnu/libc.so.6+0x9ec0c)
#6 0x000073efccc4527e raise (/lib/x86_64-linux-gnu/libc.so.6+0x4527e)
#7 0x000073efccc288ff abort (/lib/x86_64-linux-gnu/libc.so.6+0x288ff)
#8 0x000073efccc2881b (/lib/x86_64-linux-gnu/libc.so.6+0x2881b)
#9 0x000073efccc3b517 (/lib/x86_64-linux-gnu/libc.so.6+0x3b517)
#10 0x000000000325ee3f (anonymous namespace)::FoldBitCast(llvm::Constant*, llvm::Type*, llvm::DataLayout const&) ConstantFolding.cpp:0:0
#11 0x000000000325efc9 llvm::ConstantFoldCastOperand(unsigned int, llvm::Constant*, llvm::Type*, llvm::DataLayout const&) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x325efc9)
#12 0x0000000004e0ded6 llvm::IRBuilderBase::CreateCast(llvm::Instruction::CastOps, llvm::Value*, llvm::Type*, llvm::Twine const&, llvm::MDNode*, llvm::FMFSource) (.constprop.0) CGBuiltin.cpp:0:0
#13 0x0000000004e2b961 clang::CodeGen::CodeGenFunction::EmitBuiltinExpr(clang::GlobalDecl, unsigned int, clang::CallExpr const*, clang::CodeGen::ReturnValueSlot) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x4e2b961)
#14 0x00000000049570ce clang::CodeGen::CodeGenFunction::EmitCallExpr(clang::CallExpr const*, clang::CodeGen::ReturnValueSlot, llvm::CallBase**) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x49570ce)
#15 0x00000000049c651c (anonymous namespace)::ScalarExprEmitter::VisitCallExpr(clang::CallExpr const*) CGExprScalar.cpp:0:0
#16 0x00000000049bba70 clang::StmtVisitorBase::Visit(clang::Stmt*) CGExprScalar.cpp:0:0
#17 0x00000000049bdcdb (anonymous namespace)::ScalarExprEmitter::Visit(clang::Expr*) CGExprScalar.cpp:0:0
#18 0x00000000049c20bb (anonymous namespace)::ScalarExprEmitter::VisitCastExpr(clang::CastExpr*) CGExprScalar.cpp:0:0
#19 0x00000000049bba96 clang::StmtVisitorBase::Visit(clang::Stmt*) CGExprScalar.cpp:0:0
#20 0x00000000049c151c clang::CodeGen::CodeGenFunction::EmitScalarExpr(clang::Expr const*, bool) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x49c151c)
#21 0x0000000004ae5ded clang::CodeGen::CodeGenFunction::EmitReturnStmt(clang::ReturnStmt const&) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x4ae5ded)
#22 0x0000000004af5e9b clang::CodeGen::CodeGenFunction::EmitCompoundStmtWithoutScope(clang::CompoundStmt const&, bool, clang::CodeGen::AggValueSlot) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x4af5e9b)
#23 0x0000000004b5e996 clang::CodeGen::CodeGenFunction::EmitFunctionBody(clang::Stmt const*) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x4b5e996)
#24 0x0000000004b743b4 clang::CodeGen::CodeGenFunction::GenerateCode(clang::GlobalDecl, llvm::Function*, clang::CodeGen::CGFunctionInfo const&) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x4b743b4)
#25 0x0000000004be4a9a clang::CodeGen::CodeGenModule::EmitGlobalFunctionDefinition(clang::GlobalDecl, llvm::GlobalValue*) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x4be4a9a)
#26 0x0000000004bdde84 clang::CodeGen::CodeGenModule::EmitGlobalDefinition(clang::GlobalDecl, llvm::GlobalValue*) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x4bdde84)
#27 0x0000000004bdebd0 clang::CodeGen::CodeGenModule::EmitGlobal(clang::GlobalDecl) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x4bdebd0)
#28 0x0000000004bebce1 clang::CodeGen::CodeGenModule::EmitTopLevelDecl(clang::Decl*) (.part.0) CodeGenModule.cpp:0:0
#29 0x0000000004f6a059 (anonymous namespace)::CodeGeneratorImpl::HandleTopLevelDecl(clang::DeclGroupRef) ModuleBuilder.cpp:0:0
#30 0x0000000004f5b0f4 clang::BackendConsumer::HandleTopLevelDecl(clang::DeclGroupRef) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x4f5b0f4)
#31 0x0000000006d80ed4 clang::ParseAST(clang::Sema&, bool, bool) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x6d80ed4)
#32 0x0000000004f67ee8 clang::CodeGenAction::ExecuteAction() (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x4f67ee8)
#33 0x0000000005293618 clang::FrontendAction::Execute() (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x5293618)
#34 0x00000000052206fd clang::CompilerInstance::ExecuteAction(clang::FrontendAction&) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x52206fd)
#35 0x000000000536838d clang::ExecuteCompilerInvocation(clang::CompilerInstance*) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x536838d)
#36 0x0000000000daaabc cc1_main(llvm::ArrayRef, char const*, void*) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0xdaaabc)
#37 0x0000000000da16da ExecuteCC1Tool(llvm::SmallVectorImpl&, llvm::ToolContext const&, llvm::IntrusiveRefCntPtr) driver.cpp:0:0
#38 0x0000000000da185d int llvm::function_ref&)>::callback_fn&)>(long, llvm::SmallVectorImpl&) driver.cpp:0:0
#39 0x0000000004fe0bb9 void llvm::function_ref::callback_fn>, std::__cxx11::basic_string, std::allocator>*, bool*) const::'lambda'()>(long) Job.cpp:0:0
#40 0x00000000043aab84 llvm::CrashRecoveryContext::RunSafely(llvm::function_ref) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x43aab84)
#41 0x0000000004fe1725 clang::driver::CC1Command::Execute(llvm::ArrayRef>, std::__cxx11::basic_string, std::allocator>*, bool*) const (.part.0) Job.cpp:0:0
#42 0x0000000004f993d2 clang::driver::Compilation::ExecuteCommand(clang::driver::Command const&, clang::driver::Command const*&, bool) const (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x4f993d2)
#43 0x0000000004f9dd37 clang::driver::Compilation::ExecuteJobs(clang::driver::JobList const&, llvm::SmallVectorImpl>&, bool) const (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x4f9dd37)
#44 0x0000000004fa8405 clang::driver::Driver::ExecuteCompilation(clang::driver::Compilation&, llvm::SmallVectorImpl>&) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x4fa8405)
#45 0x0000000000da6fba clang_main(int, char**, llvm::ToolContext const&) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0xda6fba)
#46 0x0000000000cc2d0a main (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0xcc2d0a)
#47 0x000073efccc2a1ca (/lib/x86_64-linux-gnu/libc.so.6+0x2a1ca)
#48 0x000073efccc2a28b __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x2a28b)
#49 0x0000000000da1085 _start (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0xda1085)
clang++: error: clang frontend command failed due to signal (use -v to see invocation)
Compiler returned: 134
```

This testcase was generated by fuzzer.

Contributor guide

Open the contributing guide

Research direction

Start by compiling the provided PowerPC64LE reproducer with Clang trunk and -mcpu=power9, then trace the assertion in llvm/lib/Analysis/ConstantFolding.cpp through the CGBuiltin.cpp call path. Compare the generated cast operands for __builtin_ppc_national2packed and confirm that the testcase no longer triggers the invalid ConstantExpr bitcast assertion.

Written by the indexing model from the issue text.

Assessment

Tech stack
cpp
Domain
compilers
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 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.