<table border="1" cellspacing="0" cellpadding="8">
<tr>
<th>Issue</th>
<td>
<a href=https://github.com/llvm/llvm-project/issues/116595>116595</a>
</td>
</tr>
<tr>
<th>Summary</th>
<td>
Assertion `HT.TopLevelMap[ThisEntry->getKey()] == ThisEntry && "Scope imbalance!"' failed in clang when built with GCC 13.3.0 LTO build
</td>
</tr>
<tr>
<th>Labels</th>
<td>
clang
</td>
</tr>
<tr>
<th>Assignees</th>
<td>
</td>
</tr>
<tr>
<th>Reporter</th>
<td>
karka228
</td>
</tr>
</table>
<pre>
When building clang with a recent gcc like version 13.3.0 and enabling -flto the resulting clang binary hit an assert `HT.TopLevelMap[ThisEntry->getKey()] == ThisEntry && "Scope imbalance!"' when used to compile code in compiler-rt (e.g. mulvti3.c).
Steps to reproduce:
Install a gcc 13.3.0 and make sure it is available in the PATH. `gcc --version` should report "gcc (GCC) 13.3.0".
I have tested with the commit e9fc2faf0c2551eb4f9f932da09bdf1af24ac7e2 from llvm-project the main branch but I guess the latest from main will do.
Reproduce:
```
cd <your llvm-project-repo>
git fetch
git co origin/main
mkdir -p build
( cd build ; env CC='gcc' CXX='g++' cmake ../llvm -G Ninja -DCMAKE_MAKE_PROGRAM=ninja -DCMAKE_BUILD_TYPE=Release -DCMAKE_CXX_FLAGS='-flto=8' -DLLVM_ENABLE_ASSERTIONS=ON -DLLVM_ENABLE_PROJECTS='clang' -DLLVM_BUILTIN_TARGETS='x86_64-unknown-linux-gnu' -DLLVM_ENABLE_PER_TARGET_RUNTIME_DIR=ON -DLLVM_ENABLE_RUNTIMES='compiler-rt' -DLLVM_RUNTIME_TARGETS='x86_64-unknown-linux-gnu' -DLLVM_STATIC_LINK_CXX_STDLIB=ON )
ninja -C build all
```
Note that -flto is turned on in gcc and it will therefore take some time to build. You will see the failed compiler-rt built in the output, and it can be reproduced with:
```
./build/bin/clang --target=x86_64-unknown-linux-gnu -DVISIBILITY_HIDDEN -O3 -DNDEBUG -fno-lto -std=c11 -fPIC -fno-builtin -fvisibility=hidden -fomit-frame-pointer -DCOMPILER_RT_HAS_FLOAT16 -o /dev/null -c compiler-rt/lib/builtins/mulvti3.c
clang: /home/git/llvm-project/llvm/include/llvm/ADT/ScopedHashTable.h:243: llvm::ScopedHashTableScope<K, V, KInfo, AllocatorTy>::~ScopedHashTableScope() [with K = {anonymous}::SimpleValue; V = llvm::Value*; KInfo = llvm::DenseMapInfo<{anonymous}::SimpleValue>; AllocatorTy = llvm::RecyclingAllocator<llvm::BumpPtrAllocatorImpl<>, llvm::ScopedHashTableVal<{anonymous}::SimpleValue, llvm::Value*> >]: Assertion `HT.TopLevelMap[ThisEntry->getKey()] == ThisEntry && "Scope imbalance!"' 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: ./build/bin/clang --target=x86_64-unknown-linux-gnu -DVISIBILITY_HIDDEN -O3 -DNDEBUG -fno-lto -std=c11 -fPIC -fno-builtin -fvisibility=hidden -fomit-frame-pointer -DCOMPILER_RT_HAS_FLOAT16 -o /dev/null -c compiler-rt/lib/builtins/mulvti3.c
1. <eof> parser at end of file
2. Optimizer
3. Running pass "function<eager-inv>(ee-instrument<>,lower-expect,simplifycfg<bonus-inst-threshold=1;no-forward-switch-cond;no-switch-range-to-icmp;no-switch-to-lookup;keep-loops;no-hoist-common-insts;no-hoist-loads-stores-with-cond-faulting;no-sink-common-insts;speculate-blocks;simplify-cond-branch;no-speculate-unpredictables>,sroa<modify-cfg>,early-cse<>,callsite-splitting)" on module "compiler-rt/lib/builtins/mulvti3.c"
4. Running pass "early-cse<>" on function "__mulvti3"
#0 0x0000000002df23cb llvm::sys::PrintStackTrace(llvm::raw_ostream&, int) (./build/bin/clang+0x2df23cb)
#1 0x0000000002d912fb llvm::sys::RunSignalHandlers() (./build/bin/clang+0x2d912fb)
#2 0x0000000002d0c128 CrashRecoverySignalHandler(int) (./build/bin/clang+0x2d0c128)
#3 0x00007f316c094cf0 __restore_rt (/lib64/libpthread.so.0+0x12cf0)
#4 0x00007f316aea9acf raise (/lib64/libc.so.6+0x4eacf)
#5 0x00007f316ae7cea5 abort (/lib64/libc.so.6+0x21ea5)
#6 0x00007f316ae7cd79 _nl_load_domain.cold.0 (/lib64/libc.so.6+0x21d79)
#7 0x00007f316aea2426 (/lib64/libc.so.6+0x47426)
#8 0x0000000002c470ff (anonymous namespace)::EarlyCSE::run() (./build/bin/clang+0x2c470ff)
#9 0x0000000002c48b02 llvm::EarlyCSEPass::run(llvm::Function&, llvm::AnalysisManager<llvm::Function>&) (./build/bin/clang+0x2c48b02)
#10 0x0000000000e4b98d llvm::detail::PassModel<llvm::Function, llvm::EarlyCSEPass, llvm::AnalysisManager<llvm::Function>>::run(llvm::Function&, llvm::AnalysisManager<llvm::Function>&) (./build/bin/clang+0xe4b98d)
#11 0x00000000028a3d98 llvm::PassManager<llvm::Function, llvm::AnalysisManager<llvm::Function>>::run(llvm::Function&, llvm::AnalysisManager<llvm::Function>&) (./build/bin/clang+0x28a3d98)
#12 0x0000000000e588fd llvm::detail::PassModel<llvm::Function, llvm::PassManager<llvm::Function, llvm::AnalysisManager<llvm::Function>>, llvm::AnalysisManager<llvm::Function>>::run(llvm::Function&, llvm::AnalysisManager<llvm::Function>&) (./build/bin/clang+0xe588fd)
#13 0x00000000028a4180 llvm::ModuleToFunctionPassAdaptor::run(llvm::Module&, llvm::AnalysisManager<llvm::Module>&) (./build/bin/clang+0x28a4180)
#14 0x0000000000e4ce3d llvm::detail::PassModel<llvm::Module, llvm::ModuleToFunctionPassAdaptor, llvm::AnalysisManager<llvm::Module>>::run(llvm::Module&, llvm::AnalysisManager<llvm::Module>&) (./build/bin/clang+0xe4ce3d)
#15 0x00000000028a38fc llvm::PassManager<llvm::Module, llvm::AnalysisManager<llvm::Module>>::run(llvm::Module&, llvm::AnalysisManager<llvm::Module>&) (./build/bin/clang+0x28a38fc)
#16 0x0000000002f9b165 (anonymous namespace)::EmitAssemblyHelper::RunOptimizationPipeline(clang::BackendAction, std::unique_ptr<llvm::raw_pwrite_stream, std::default_delete<llvm::raw_pwrite_stream>>&, std::unique_ptr<llvm::ToolOutputFile, std::default_delete<llvm::ToolOutputFile>>&, clang::BackendConsumer*) (./build/bin/clang+0x2f9b165)
#17 0x0000000002f9de8a clang::EmitBackendOutput(clang::DiagnosticsEngine&, clang::HeaderSearchOptions const&, clang::CodeGenOptions const&, clang::TargetOptions const&, clang::LangOptions const&, llvm::StringRef, llvm::Module*, clang::BackendAction, llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem>, std::unique_ptr<llvm::raw_pwrite_stream, std::default_delete<llvm::raw_pwrite_stream>>, clang::BackendConsumer*) (./build/bin/clang+0x2f9de8a)
#18 0x0000000003523d6e clang::BackendConsumer::HandleTranslationUnit(clang::ASTContext&) (./build/bin/clang+0x3523d6e)
#19 0x000000000495d67d clang::ParseAST(clang::Sema&, bool, bool) (./build/bin/clang+0x495d67d)
#20 0x0000000003667ab1 clang::FrontendAction::Execute() (./build/bin/clang+0x3667ab1)
#21 0x000000000363991d clang::CompilerInstance::ExecuteAction(clang::FrontendAction&) (./build/bin/clang+0x363991d)
#22 0x0000000003772737 clang::ExecuteCompilerInvocation(clang::CompilerInstance*) (./build/bin/clang+0x3772737)
#23 0x0000000000b56cef cc1_main(llvm::ArrayRef<char const*>, char const*, void*) (./build/bin/clang+0xb56cef)
#24 0x0000000000b5dfda ExecuteCC1Tool(llvm::SmallVectorImpl<char const*>&, llvm::ToolContext const&) (./build/bin/clang+0xb5dfda)
#25 0x00000000060dea17 llvm::CrashRecoveryContext::RunSafely(llvm::function_ref<void ()>) (.constprop.0) (./build/bin/clang+0x60dea17)
#26 0x0000000003554c25 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) (./build/bin/clang+0x3554c25)
#27 0x000000000352cf02 clang::driver::Compilation::ExecuteCommand(clang::driver::Command const&, clang::driver::Command const*&, bool) const (./build/bin/clang+0x352cf02)
#28 0x000000000352d26d clang::driver::Compilation::ExecuteJobs(clang::driver::JobList const&, llvm::SmallVectorImpl<std::pair<int, clang::driver::Command const*>>&, bool) const (./build/bin/clang+0x352d26d)
#29 0x000000000352d54c clang::driver::Driver::ExecuteCompilation(clang::driver::Compilation&, llvm::SmallVectorImpl<std::pair<int, clang::driver::Command const*>>&) (./build/bin/clang+0x352d54c)
#30 0x0000000000b5f658 clang_main(int, char**, llvm::ToolContext const&) (./build/bin/clang+0xb5f658)
#31 0x0000000000a228f3 main (./build/bin/clang+0xa228f3)
#32 0x00007f316ae95d85 __libc_start_main (/lib64/libc.so.6+0x3ad85)
#33 0x0000000000ace14e _start (./build/bin/clang+0xace14e)
clang: error: clang frontend command failed with exit code 134 (use -v to see invocation)
```
Note that if adding -fno-strict-aliasing to CMAKE_CXX_FLAGS when building the clang binary, the problem disappears, which indicate that the problem exists in the llvm-project codebase.
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJzsWltv47iS_jXKCyFDonx9yINjO92ezg2Jp7fnyaDIks0TitSSVC77sL99QUq2RSfpuAdz5uwBzmDgjshi1VdXligSY_hGApxHg4toMD8jtd0qff5I9CPBeHyWK_Z6_l9bkCivuWBcbhAVRG7QM7dbRJAGCtKiDaVI8EdAT6ANVxKlWS_rJYhIhkCSXLiFcSGsQnYLSIOphT0wy7kk-hVtuUVEImIMaIuiYfJ11Vup6gqeQFyTKhpcrLbcLKTVr3GULTZgv8FrhMcRnkSDOYqyeZTN0Z4GRXgY4SGKMH6gqgLEy5wIIilEOI0wjvAIPTvVagMMWYWoKisuAFHFAHG5e9axQ4PH0Nv0UFmLJ8uzHo3wpBcl8yiZNr8PFirjuGiotGI1hSibdgmW0lgiBCLeWh0DleQRkKk1IG4RN4g8ES5ILjwGZ6676eprz9nDLYzj1sbRMEFmq2rBnEjlMWJHEeHxl9kswpNWSIRxi3SJtuQJkAVjgTUudPypKktuEUwK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