[GlobalISel][LLT] Data race on the LLT::ExtendedLLT global when TargetMachines are created concurrently
- Dominant language
- LLVM
- Stars
- 40.5k
- Forks
- 18.7k
- PR merge metrics
- PR metrics pending
Description
`llvm::LLT::ExtendedLLT` is a process-global mutable static bool that three `TargetMachine` constructors write unconditionally. Any program that constructs two of those TargetMachines on different threads has a data race. For example, `dsymutil` creates one TargetMachine per DWARF emitter on multiple threads (affecting both the parallel and classic linker).
```
WARNING: ThreadSanitizer: data race (pid=55588)
Write of size 1 at 0x00010e250360 by thread T2:
#0 llvm::LLT::setUseExtended(bool) LowLevelType.h:708
#1 llvm::AArch64TargetMachine::AArch64TargetMachine(...) AArch64TargetMachine.cpp:414
#4 llvm::RegisterTargetMachine::Allocator(...) TargetRegistry.h:1298
#5 llvm::Target::createTargetMachine(...) const TargetRegistry.h:485
#6 llvm::dwarf_linker::classic::DwarfStreamer::init(llvm::Triple, llvm::StringRef) DWARFStreamer.cpp:126
[...]
Previous write of size 1 at 0x00010e250360 by thread T1:
Location is global 'llvm::LLT::ExtendedLLT' at 0x00010e250360
SUMMARY: ThreadSanitizer: data race LowLevelType.h:708 in llvm::LLT::setUseExtended(bool)
```
Looks like this was introduced by 5e0efc0f1d79. The ad-hoc fix is to make `ExtendedLLT` a `std::atomic`. The real fix however is avoiding mutable global and probably making it a target property?
Contributor guide
Research direction
Start at llvm/include/llvm/CodeGen/LowLevelType.h:708 and trace the writes from the TargetMachine constructors, including llvm/lib/Target/AArch64/AArch64TargetMachine.cpp:414. Follow the creation path through TargetRegistry.h and llvm/lib/DWARFLinkerClassic/DWARFStreamer.cpp:126. Done means concurrent TargetMachine creation no longer produces the reported ThreadSanitizer race without relying on an ad-hoc global fix.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- cpp
- Domain
- compilers
- Issue type
- Bug
- Difficulty
- 5/5
- Estimated time
- Over a week
- Activity status
- Active
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100