llvm / llvm/llvm-project

Clang ICE when casting between vector types with same size but different element types (long double → double)

Open
#173,554 1 comment 0 reactions 0 assignees View on GitHub
clang:codegen crash-on-valid
Dominant language
LLVM
Stars
40.5k
Forks
18.7k
PR merge metrics
PR metrics pending

Description

### Link for quick verification: https://godbolt.org/z/566M875G7
```c
typedef long double __attribute__((__vector_size__(16))) V;
typedef double __attribute__((__vector_size__(16))) W;

W foo(V t) {
return (W)t; // Direct cast from V to W
}
```
Go back to `clang 2.7.0 (assertions)`. May relate to [63548](https://github.com/llvm/llvm-project/issues/63548).
The crash happens in IR generation (`CGExprScalar.cpp`) when creating a `CastInst` for the vector-to-vector conversion.
## Stack dump:
```console
clang++: /root/llvm-project/llvm/lib/IR/Instructions.cpp:3041: static llvm::CastInst* llvm::CastInst::Create(llvm::Instruction::CastOps, llvm::Value*, llvm::Type*, const llvm::Twine&, llvm::InsertPosition): Assertion `castIsValid(op, S, Ty) && "Invalid cast!"' failed.
PLEASE submit a bug report to https://github.com/llvm/llvm-project/issues/ and include the crash backtrace, preprocessed source, and associated run script.
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
1. parser at end of file
2. :8:3: LLVM IR generation of declaration 'foo'
3. :8:3: Generating code for declaration 'foo'
#0 0x00000000042ecc08 llvm::sys::PrintStackTrace(llvm::raw_ostream&, int) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x42ecc08)
#1 0x00000000042ea034 llvm::sys::CleanupOnSignal(unsigned long) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x42ea034)
#2 0x000000000422edd8 CrashRecoverySignalHandler(int) CrashRecoveryContext.cpp:0:0
#3 0x000074d4cee42520 (/lib/x86_64-linux-gnu/libc.so.6+0x42520)
#4 0x000074d4cee969fc pthread_kill (/lib/x86_64-linux-gnu/libc.so.6+0x969fc)
#5 0x000074d4cee42476 gsignal (/lib/x86_64-linux-gnu/libc.so.6+0x42476)
#6 0x000074d4cee287f3 abort (/lib/x86_64-linux-gnu/libc.so.6+0x287f3)
#7 0x000074d4cee2871b (/lib/x86_64-linux-gnu/libc.so.6+0x2871b)
#8 0x000074d4cee39e96 (/lib/x86_64-linux-gnu/libc.so.6+0x39e96)
#9 0x0000000003ba97d6 (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x3ba97d6)
#10 0x00000000046b7bfb llvm::IRBuilderBase::CreateCast(llvm::Instruction::CastOps, llvm::Value*, llvm::Type*, llvm::Twine const&, llvm::MDNode*, llvm::FMFSource) (.constprop.0) CGExprScalar.cpp:0:0
#11 0x00000000046d3bcc (anonymous namespace)::ScalarExprEmitter::VisitCastExpr(clang::CastExpr*) CGExprScalar.cpp:0:0
#12 0x00000000046caf26 clang::StmtVisitorBase::Visit(clang::Stmt*) CGExprScalar.cpp:0:0
#13 0x00000000046cd12b (anonymous namespace)::ScalarExprEmitter::Visit(clang::Expr*) CGExprScalar.cpp:0:0
#14 0x00000000046d376b (anonymous namespace)::ScalarExprEmitter::VisitCastExpr(clang::CastExpr*) CGExprScalar.cpp:0:0
#15 0x00000000046caee8 clang::StmtVisitorBase::Visit(clang::Stmt*) CGExprScalar.cpp:0:0
#16 0x00000000046d052c clang::CodeGen::CodeGenFunction::EmitScalarExpr(clang::Expr const*, bool) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x46d052c)
#17 0x00000000047eded4 clang::CodeGen::CodeGenFunction::EmitReturnStmt(clang::ReturnStmt const&) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x47eded4)
#18 0x00000000047f72ad clang::CodeGen::CodeGenFunction::EmitStmt(clang::Stmt const*, llvm::ArrayRef) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x47f72ad)
#19 0x00000000047ff5cb clang::CodeGen::CodeGenFunction::EmitCompoundStmtWithoutScope(clang::CompoundStmt const&, bool, clang::CodeGen::AggValueSlot) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x47ff5cb)
#20 0x000000000486760e clang::CodeGen::CodeGenFunction::EmitFunctionBody(clang::Stmt const*) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x486760e)
#21 0x0000000004878a64 clang::CodeGen::CodeGenFunction::GenerateCode(clang::GlobalDecl, llvm::Function*, clang::CodeGen::CGFunctionInfo const&) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x4878a64)
#22 0x00000000048e6950 clang::CodeGen::CodeGenModule::EmitGlobalFunctionDefinition(clang::GlobalDecl, llvm::GlobalValue*) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x48e6950)
#23 0x00000000048e15f4 clang::CodeGen::CodeGenModule::EmitGlobalDefinition(clang::GlobalDecl, llvm::GlobalValue*) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x48e15f4)
#24 0x00000000048e1dd3 clang::CodeGen::CodeGenModule::EmitGlobal(clang::GlobalDecl) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x48e1dd3)
#25 0x00000000048ed363 clang::CodeGen::CodeGenModule::EmitTopLevelDecl(clang::Decl*) (.part.0) CodeGenModule.cpp:0:0
#26 0x0000000004c50b79 (anonymous namespace)::CodeGeneratorImpl::HandleTopLevelDecl(clang::DeclGroupRef) ModuleBuilder.cpp:0:0
#27 0x0000000004c416a4 clang::BackendConsumer::HandleTopLevelDecl(clang::DeclGroupRef) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x4c416a4)
#28 0x0000000006a089f4 clang::ParseAST(clang::Sema&, bool, bool) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x6a089f4)
#29 0x0000000004c4df88 clang::CodeGenAction::ExecuteAction() (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x4c4df88)
#30 0x0000000004f44885 clang::FrontendAction::Execute() (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x4f44885)
#31 0x0000000004ec4b1e clang::CompilerInstance::ExecuteAction(clang::FrontendAction&) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x4ec4b1e)
#32 0x000000000503ec0d clang::ExecuteCompilerInvocation(clang::CompilerInstance*) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x503ec0d)
#33 0x0000000000de5b1c cc1_main(llvm::ArrayRef, char const*, void*) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0xde5b1c)
#34 0x0000000000ddc56a ExecuteCC1Tool(llvm::SmallVectorImpl&, llvm::ToolContext const&, llvm::IntrusiveRefCntPtr) driver.cpp:0:0
#35 0x0000000000ddc6ed int llvm::function_ref&)>::callback_fn&)>(long, llvm::SmallVectorImpl&) driver.cpp:0:0
#36 0x0000000004cc08d9 void llvm::function_ref::callback_fn>, std::__cxx11::basic_string, std::allocator>*, bool*) const::'lambda'()>(long) Job.cpp:0:0
#37 0x000000000422f274 llvm::CrashRecoveryContext::RunSafely(llvm::function_ref) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x422f274)
#38 0x0000000004cc0eef clang::driver::CC1Command::Execute(llvm::ArrayRef>, std::__cxx11::basic_string, std::allocator>*, bool*) const (.part.0) Job.cpp:0:0
#39 0x0000000004c819d2 clang::driver::Compilation::ExecuteCommand(clang::driver::Command const&, clang::driver::Command const*&, bool) const (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x4c819d2)
#40 0x0000000004c8297e clang::driver::Compilation::ExecuteJobs(clang::driver::JobList const&, llvm::SmallVectorImpl>&, bool) const (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x4c8297e)
#41 0x0000000004c89dd5 clang::driver::Driver::ExecuteCompilation(clang::driver::Compilation&, llvm::SmallVectorImpl>&) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x4c89dd5)
#42 0x0000000000de1f11 clang_main(int, char**, llvm::ToolContext const&) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0xde1f11)
#43 0x0000000000c8cf64 main (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0xc8cf64)
#44 0x000074d4cee29d90 (/lib/x86_64-linux-gnu/libc.so.6+0x29d90)
#45 0x000074d4cee29e40 __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x29e40)
#46 0x0000000000ddc005 _start (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0xddc005)
clang++: error: clang frontend command failed with exit code 134 (use -v to see invocation)
Compiler returned: 134
```

Contributor guide

Open the contributing guide

Research direction

Reproduce the crash with the vector-cast example and begin in CGExprScalar.cpp, where the stack trace shows LLVM IR generation creating the cast. Check the resulting CastInst request against the assertion in llvm/lib/IR/Instructions.cpp; done means this input no longer aborts during IR generation and has defined compiler behavior.

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
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.