llvm-project icon indicating copy to clipboard operation
llvm-project copied to clipboard

clang: Assertion `VarSize == CstSize && "Emitted constant has unexpected size"' failed.

Open wierton opened this issue 1 year ago • 3 comments

The testcase:

_Atomic(unsigned int) a;
_Atomic(int *) b = 0;
void c(int x) { a << x; }

Compiler Explorer: https://gcc.godbolt.org/z/zcef1aTbP

Compiler run:

$ clang --version
clang version 17.0.0 (https://github.com/llvm/llvm-project.git 12e9c7aaa66b7624b5d7666ce2794d912bf9e4b7)
Target: x86_64-unknown-linux-gnu
Thread model: posix
InstalledDir: /opt/compiler-explorer/clang-assertions-trunk/bin
Compiler returned: 0
$ clang -O0 small.c

Assertion:

clang: /root/llvm-project/clang/lib/CodeGen/CodeGenModule.cpp:5182:
void clang::CodeGen::CodeGenModule::EmitGlobalVarDefinition(const clang::VarDecl*, bool):
Assertion `VarSize == CstSize && "Emitted constant has unexpected size"' failed.

Backtrace:

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 -gdwarf-4 -g -o /app/output.s -S --gcc-toolchain=/opt/compiler-explorer/gcc-9.2.0 -fcolor-diagnostics -fno-crash-diagnostics -O0 <source>
1.	<source>:3:1: current parser token 'void'
2.	<source>:2:16: LLVM IR generation of declaration 'b'
3.	<source>:2:16: Generating code for declaration 'b'
 #0 0x0000561c7878cf2f llvm::sys::PrintStackTrace(llvm::raw_ostream&, int) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+0x3c52f2f)
 #1 0x0000561c7878ac9c llvm::sys::CleanupOnSignal(unsigned long) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+0x3c50c9c)
 #2 0x0000561c786d5398 CrashRecoverySignalHandler(int) CrashRecoveryContext.cpp:0:0
 #3 0x00007f847dd8c420 __restore_rt (/lib/x86_64-linux-gnu/libpthread.so.0+0x14420)
 #4 0x00007f847d85900b raise (/lib/x86_64-linux-gnu/libc.so.6+0x4300b)
 #5 0x00007f847d838859 abort (/lib/x86_64-linux-gnu/libc.so.6+0x22859)
 #6 0x00007f847d838729 (/lib/x86_64-linux-gnu/libc.so.6+0x22729)
 #7 0x00007f847d849fd6 (/lib/x86_64-linux-gnu/libc.so.6+0x33fd6)
 #8 0x0000561c78b6e346 clang::CodeGen::CodeGenModule::EmitGlobalVarDefinition(clang::VarDecl const*, bool) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+0x4034346)
 #9 0x0000561c78b90d21 clang::CodeGen::CodeGenModule::EmitGlobalDefinition(clang::GlobalDecl, llvm::GlobalValue*) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+0x4056d21)
#10 0x0000561c78b91988 clang::CodeGen::CodeGenModule::EmitGlobal(clang::GlobalDecl) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+0x4057988)
#11 0x0000561c78b9a734 clang::CodeGen::CodeGenModule::EmitTopLevelDecl(clang::Decl*) (.part.0) CodeGenModule.cpp:0:0
#12 0x0000561c79a09ac6 (anonymous namespace)::CodeGeneratorImpl::HandleTopLevelDecl(clang::DeclGroupRef) ModuleBuilder.cpp:0:0
#13 0x0000561c799fbb70 clang::BackendConsumer::HandleTopLevelDecl(clang::DeclGroupRef) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+0x4ec1b70)
#14 0x0000561c7af03e14 clang::ParseAST(clang::Sema&, bool, bool) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+0x63c9e14)
#15 0x0000561c79a069a8 clang::CodeGenAction::ExecuteAction() (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+0x4ecc9a8)
#16 0x0000561c79250b79 clang::FrontendAction::Execute() (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+0x4716b79)
#17 0x0000561c791d64c6 clang::CompilerInstance::ExecuteAction(clang::FrontendAction&) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+0x469c4c6)
#18 0x0000561c793352a6 clang::ExecuteCompilerInvocation(clang::CompilerInstance*) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+0x47fb2a6)
#19 0x0000561c75c2f0ed cc1_main(llvm::ArrayRef<char const*>, char const*, void*) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+0x10f50ed)
#20 0x0000561c75c2adea ExecuteCC1Tool(llvm::SmallVectorImpl<char const*>&, llvm::ToolContext const&) driver.cpp:0:0
#21 0x0000561c7903523d void llvm::function_ref<void ()>::callback_fn<clang::driver::CC1Command::Execute(llvm::ArrayRef<std::optional<llvm::StringRef>>, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>*, bool*) const::'lambda'()>(long) Job.cpp:0:0
#22 0x0000561c786d58a0 llvm::CrashRecoveryContext::RunSafely(llvm::function_ref<void ()>) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+0x3b9b8a0)
#23 0x0000561c7903585f clang::driver::CC1Command::Execute(llvm::ArrayRef<std::optional<llvm::StringRef>>, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>*, bool*) const (.part.0) Job.cpp:0:0
#24 0x0000561c78ffc9ec clang::driver::Compilation::ExecuteCommand(clang::driver::Command const&, clang::driver::Command const*&, bool) const (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+0x44c29ec)
#25 0x0000561c78ffd47d clang::driver::Compilation::ExecuteJobs(clang::driver::JobList const&, llvm::SmallVectorImpl<std::pair<int, clang::driver::Command const*>>&, bool) const (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+0x44c347d)
#26 0x0000561c7900555d clang::driver::Driver::ExecuteCompilation(clang::driver::Compilation&, llvm::SmallVectorImpl<std::pair<int, clang::driver::Command const*>>&) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+0x44cb55d)
#27 0x0000561c75c2d34a clang_main(int, char**, llvm::ToolContext const&) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+0x10f334a)
#28 0x0000561c75b335c5 main (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+0xff95c5)
#29 0x00007f847d83a083 __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x24083)
#30 0x0000561c75c25bce _start (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+0x10ebbce)
clang: error: clang frontend command failed with exit code 134 (use -v to see invocation)
Compiler returned: 134

wierton avatar Jun 27 '23 12:06 wierton

@llvm/issue-subscribers-clang-codegen

llvmbot avatar Jun 27 '23 14:06 llvmbot

May be related: https://github.com/llvm/llvm-project/issues/61666

shafik avatar Jun 27 '23 17:06 shafik

Looks like 1395cde24b3641e284bb1daae7d56c189a2635e3 isn't quite right. CC @AaronBallman .

efriedma-quic avatar Jun 27 '23 20:06 efriedma-quic

@llvm/issue-subscribers-c11

llvmbot avatar Jul 01 '23 16:07 llvmbot

Test can be reduced down to:

_Atomic(int *) b = 0;

and only triggers the assertion in C, not in C++. Casting 0 to be an _Atomic(int *) does not change the behavior but changing the initializer to a non-literal pointer value causes the assertion to go away as does casting the intitializer to void * or int *. It requires an assertions build to see the issue. The AST is:

`-VarDecl <line:1:1, col:20> col:16 b '_Atomic(int *)' cinit
  `-ImplicitCastExpr <col:20> '_Atomic(int *)' <NullToPointer>
    `-IntegerLiteral <col:20> 'int' 0

The size of the variable is 8 and the size of the constant is 4, hence the assert.

I've put up a patch at: https://reviews.llvm.org/D154284

AaronBallman avatar Jul 01 '23 17:07 AaronBallman

@llvm/issue-subscribers-clang-frontend

llvmbot avatar Jul 02 '23 22:07 llvmbot