<table border="1" cellspacing="0" cellpadding="8">
<tr>
<th>Issue</th>
<td>
<a href=https://github.com/llvm/llvm-project/issues/138651>138651</a>
</td>
</tr>
<tr>
<th>Summary</th>
<td>
[clang][CodeGen] __builtin_addressof does not mark declaration 'used' (compiler crash)
</td>
</tr>
<tr>
<th>Labels</th>
<td>
clang
</td>
</tr>
<tr>
<th>Assignees</th>
<td>
</td>
</tr>
<tr>
<th>Reporter</th>
<td>
andyg1001
</td>
</tr>
</table>
<pre>
The following minimal code crashes the compiler during code-gen when built with asserts:
```
const int* test(int idx) {
static constexpr int Values[2] = { 10, 20 };
return __builtin_addressof(Values[idx]);
}
```
Using '&' rather than '__builtin_addressof', or changing 'constexpr' to 'const' doesn't crash the compiler, so the root of the issue is in the combination of __builtin_addressof and a constexpr array.
Here is the compiler output (https://godbolt.org/z/9xTG1MdGs):
```
clang++: /root/llvm-project/llvm/tools/clang/lib/CodeGen/CGExpr.cpp:3191: clang::CodeGen::LValue clang::CodeGen::CodeGenFunction::EmitDeclRefLValue(const clang::DeclRefExpr*): Assertion `(ND->isUsed(false) || !isa<VarDecl>(ND) || E->isNonOdrUse() || !E->getLocation().isValid()) && "Should not use decl without marking it used!"' 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++ -gdwarf-4 -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 -std=c++20 -O2 <source> -isystem/opt/compiler-explorer/libs/fmt/trunk/include
1. <eof> parser at end of file
2. <source>:4:12: LLVM IR generation of declaration 'test'
3. <source>:4:12: Generating code for declaration 'test'
#0 0x0000000003f70b58 llvm::sys::PrintStackTrace(llvm::raw_ostream&, int) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x3f70b58)
#1 0x0000000003f6e7e4 llvm::sys::CleanupOnSignal(unsigned long) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x3f6e7e4)
#2 0x0000000003eb36a8 CrashRecoverySignalHandler(int) CrashRecoveryContext.cpp:0:0
#3 0x00007a5fb9c42520 (/lib/x86_64-linux-gnu/libc.so.6+0x42520)
#4 0x00007a5fb9c969fc pthread_kill (/lib/x86_64-linux-gnu/libc.so.6+0x969fc)
#5 0x00007a5fb9c42476 gsignal (/lib/x86_64-linux-gnu/libc.so.6+0x42476)
#6 0x00007a5fb9c287f3 abort (/lib/x86_64-linux-gnu/libc.so.6+0x287f3)
#7 0x00007a5fb9c2871b (/lib/x86_64-linux-gnu/libc.so.6+0x2871b)
#8 0x00007a5fb9c39e96 (/lib/x86_64-linux-gnu/libc.so.6+0x39e96)
#9 0x00000000042cfa70 clang::CodeGen::CodeGenFunction::EmitDeclRefLValue(clang::DeclRefExpr const*) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x42cfa70)
#10 0x00000000042dcdf1 clang::CodeGen::CodeGenFunction::EmitLValueHelper(clang::Expr const*, clang::CodeGen::KnownNonNull_t) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x42dcdf1)
#11 0x00000000042dd2d7 void llvm::function_ref<void ()>::callback_fn<clang::CodeGen::CodeGenFunction::EmitLValue(clang::Expr const*, clang::CodeGen::KnownNonNull_t)::'lambda'()>(long) CGExpr.cpp:0:0
#12 0x0000000007ed1c01 clang::StackExhaustionHandler::runWithSufficientStackSpace(clang::SourceLocation, llvm::function_ref<void ()>) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x7ed1c01)
#13 0x00000000042a8773 clang::CodeGen::CodeGenFunction::EmitLValue(clang::Expr const*, clang::CodeGen::KnownNonNull_t) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x42a8773)
#14 0x00000000042c8650 clang::CodeGen::CodeGenFunction::EmitArraySubscriptExpr(clang::ArraySubscriptExpr const*, bool) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x42c8650)
#15 0x00000000042dc80b clang::CodeGen::CodeGenFunction::EmitLValueHelper(clang::Expr const*, clang::CodeGen::KnownNonNull_t) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x42dc80b)
#16 0x00000000042dd2d7 void llvm::function_ref<void ()>::callback_fn<clang::CodeGen::CodeGenFunction::EmitLValue(clang::Expr const*, clang::CodeGen::KnownNonNull_t)::'lambda'()>(long) CGExpr.cpp:0:0
#17 0x0000000007ed1c01 clang::StackExhaustionHandler::runWithSufficientStackSpace(clang::SourceLocation, llvm::function_ref<void ()>) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x7ed1c01)
#18 0x00000000042a8773 clang::CodeGen::CodeGenFunction::EmitLValue(clang::Expr const*, clang::CodeGen::KnownNonNull_t) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x42a8773)
#19 0x00000000047e1ec0 clang::CodeGen::CodeGenFunction::EmitBuiltinExpr(clang::GlobalDecl, unsigned int, clang::CallExpr const*, clang::CodeGen::ReturnValueSlot) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x47e1ec0)
#20 0x00000000042d7a4e clang::CodeGen::CodeGenFunction::EmitCallExpr(clang::CallExpr const*, clang::CodeGen::ReturnValueSlot, llvm::CallBase**) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x42d7a4e)
#21 0x00000000043492f4 (anonymous namespace)::ScalarExprEmitter::VisitCallExpr(clang::CallExpr const*) CGExprScalar.cpp:0:0
#22 0x000000000433ca40 clang::StmtVisitorBase<std::add_pointer, (anonymous namespace)::ScalarExprEmitter, llvm::Value*>::Visit(clang::Stmt*) CGExprScalar.cpp:0:0
#23 0x000000000434288c clang::CodeGen::CodeGenFunction::EmitScalarExpr(clang::Expr const*, bool) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x434288c)
#24 0x0000000004441274 clang::CodeGen::CodeGenFunction::EmitReturnStmt(clang::ReturnStmt const&) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x4441274)
#25 0x0000000004449a60 clang::CodeGen::CodeGenFunction::EmitStmt(clang::Stmt const*, llvm::ArrayRef<clang::Attr const*>) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x4449a60)
#26 0x000000000445175c clang::CodeGen::CodeGenFunction::EmitCompoundStmtWithoutScope(clang::CompoundStmt const&, bool, clang::CodeGen::AggValueSlot) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x445175c)
#27 0x00000000044b7792 clang::CodeGen::CodeGenFunction::EmitFunctionBody(clang::Stmt const*) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x44b7792)
#28 0x00000000044cabe5 clang::CodeGen::CodeGenFunction::GenerateCode(clang::GlobalDecl, llvm::Function*, clang::CodeGen::CGFunctionInfo const&) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x44cabe5)
#29 0x000000000453481a clang::CodeGen::CodeGenModule::EmitGlobalFunctionDefinition(clang::GlobalDecl, llvm::GlobalValue*) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x453481a)
#30 0x000000000452f9f4 clang::CodeGen::CodeGenModule::EmitGlobalDefinition(clang::GlobalDecl, llvm::GlobalValue*) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x452f9f4)
#31 0x000000000452fe0b clang::CodeGen::CodeGenModule::EmitGlobal(clang::GlobalDecl) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x452fe0b)
#32 0x000000000453a613 clang::CodeGen::CodeGenModule::EmitTopLevelDecl(clang::Decl*) (.part.0) CodeGenModule.cpp:0:0
#33 0x0000000004916776 (anonymous namespace)::CodeGeneratorImpl::HandleTopLevelDecl(clang::DeclGroupRef) ModuleBuilder.cpp:0:0
#34 0x0000000004906264 clang::BackendConsumer::HandleTopLevelDecl(clang::DeclGroupRef) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x4906264)
#35 0x00000000065e19c4 clang::ParseAST(clang::Sema&, bool, bool) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x65e19c4)
#36 0x0000000004912908 clang::CodeGenAction::ExecuteAction() (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x4912908)
#37 0x0000000004c05bb5 clang::FrontendAction::Execute() (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x4c05bb5)
#38 0x0000000004b851be clang::CompilerInstance::ExecuteAction(clang::FrontendAction&) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x4b851be)
#39 0x0000000004cf9a09 clang::ExecuteCompilerInvocation(clang::CompilerInstance*) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x4cf9a09)
#40 0x0000000000da625f cc1_main(llvm::ArrayRef<char const*>, char const*, void*) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0xda625f)
#41 0x0000000000d9c42a ExecuteCC1Tool(llvm::SmallVectorImpl<char const*>&, llvm::ToolContext const&) driver.cpp:0:0
#42 0x00000000049791b9 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
#43 0x0000000003eb3b44 llvm::CrashRecoveryContext::RunSafely(llvm::function_ref<void ()>) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x3eb3b44)
#44 0x00000000049797cf 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
#45 0x000000000493bc1d clang::driver::Compilation::ExecuteCommand(clang::driver::Command const&, clang::driver::Command const*&, bool) const (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x493bc1d)
#46 0x000000000493ccae 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+++0x493ccae)
#47 0x0000000004944b05 clang::driver::Driver::ExecuteCompilation(clang::driver::Compilation&, llvm::SmallVectorImpl<std::pair<int, clang::driver::Command const*>>&) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x4944b05)
#48 0x0000000000da21c8 clang_main(int, char**, llvm::ToolContext const&) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0xda21c8)
#49 0x0000000000c27864 main (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0xc27864)
#50 0x00007a5fb9c29d90 (/lib/x86_64-linux-gnu/libc.so.6+0x29d90)
#51 0x00007a5fb9c29e40 __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x29e40)
#52 0x0000000000d9bed5 _start (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0xd9bed5)
clang++: error: clang frontend command failed with exit code 134 (use -v to see invocation)
Compiler returned: 134
```
</pre>
<img width="1" height="1" alt="" src="http://email.email.llvm.org/o/eJzsW11z4yiz_jXkhnJKQt8XuXCcj509s7NTk9l5L10IIZszEqgAZZzz608BsiUUJzPOibfeOvtuZacsBE0_TzfdDcZYKbbhlF6B5BokNxe411shrzCvnjZhEIQXpaierr5uKaxF04gfjG9gyzhrcQOJqCgkEqstVVBvKSSi7VhDJax6aTqaDosN5fDHlnJY9qzR8AfTW4iVolIrEC1BYP_SYPgLlkRwpSHjGqAl1FRpgHLGNWTVDqACguwaBEsIlcaaEWh7010nzQj4DTc9VSC5RiC5gSC6Md1hGAC0giiAILsBkRsuqe4lh-u11YrxNa4qSZUSNUD5QYyZM7kBqHDDzHhfWRAs_1IGKkAZQClAGZRYb6mEeou5aT06Q2YUEhKSLeabYfgBiRGixaHJPFaCKg5Qph3dHtlGlBK2SQqhoajtZ6ZUb_6FjO-7l4xjzQQ3XY6oBTGvIJ4wiqXET5cO5W9UWmmemUWvu15DgPKt1p01J7oD6G4jqlI0-lLIDUB3_wPQ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