[llvm] [BOLT] fix race condition in multi-threaded DWO type unit processing (PR #197359)
via llvm-commits
llvm-commits at lists.llvm.org
Fri May 22 02:33:55 PDT 2026
https://github.com/Thrrreeee updated https://github.com/llvm/llvm-project/pull/197359
>From 7daaff29a6d0cd9bb2b4566f686068293ab4d140 Mon Sep 17 00:00:00 2001
From: shijinrui <shijinrui at bytedance.com>
Date: Fri, 22 May 2026 17:33:37 +0800
Subject: [PATCH] [BOLT] Fix race condition in multi-threaded DWP type unit
processing
---
bolt/include/bolt/Core/BinaryContext.h | 7 +++++
bolt/include/bolt/Core/DIEBuilder.h | 8 ++++++
bolt/lib/Core/DIEBuilder.cpp | 16 ++++++++++++
bolt/lib/Rewrite/RewriteInstance.cpp | 8 +++---
.../X86/dwarf5-df-types-dup-dwp-input.test | 2 +-
bolt/test/X86/dwarf5-dwp-tsan-data-race.test | 26 +++++++++++++++++++
6 files changed, 62 insertions(+), 5 deletions(-)
create mode 100644 bolt/test/X86/dwarf5-dwp-tsan-data-race.test
diff --git a/bolt/include/bolt/Core/BinaryContext.h b/bolt/include/bolt/Core/BinaryContext.h
index 4489bbb7bee52..f670bb2d4d839 100644
--- a/bolt/include/bolt/Core/BinaryContext.h
+++ b/bolt/include/bolt/Core/BinaryContext.h
@@ -48,6 +48,7 @@
#include <functional>
#include <list>
#include <map>
+#include <mutex>
#include <optional>
#include <set>
#include <string>
@@ -284,11 +285,17 @@ class BinaryContext {
/// Internal helper for removing section name from a lookup table.
void deregisterSectionName(const BinarySection &Section);
+ /// Mutex used for parallel processing of DWP type units.
+ mutable std::recursive_mutex DWPUnitsMutex;
+
public:
static Expected<std::unique_ptr<BinaryContext>> createBinaryContext(
Triple TheTriple, std::shared_ptr<orc::SymbolStringPool> SSP,
StringRef InputFileName, SubtargetFeatures *Features, bool IsPIC,
std::unique_ptr<DWARFContext> DwCtx, JournalingStreams Logger);
+
+ /// Returns the mutex guarding concurrent access to DWP units.
+ std::recursive_mutex &getUnitsMutex() { return DWPUnitsMutex; }
/// Superset of compiler units that will contain overwritten code that needs
/// new debug info. In a few cases, functions may end up not being
diff --git a/bolt/include/bolt/Core/DIEBuilder.h b/bolt/include/bolt/Core/DIEBuilder.h
index 95e958f16cffd..2a26fafa01483 100644
--- a/bolt/include/bolt/Core/DIEBuilder.h
+++ b/bolt/include/bolt/Core/DIEBuilder.h
@@ -220,6 +220,14 @@ class DIEBuilder {
/// Returns true if DWARFUnit is registered successfully.
bool registerUnit(DWARFUnit &DU, bool NeedSort);
+ /// Builds type units without extra synchronization.
+ void buildTypeUnitsImpl(DebugStrOffsetsWriter *StrOffsetWriter,
+ const bool Init);
+
+ /// Serializes type-unit construction for DWP-backed split DWARF contexts.
+ void buildTypeUnitsThreadSafe(DebugStrOffsetsWriter *StrOffsetWriter,
+ const bool Init);
+
/// \return the unique ID of \p U if it exists.
std::optional<uint32_t> getUnitId(const DWARFUnit &DU);
diff --git a/bolt/lib/Core/DIEBuilder.cpp b/bolt/lib/Core/DIEBuilder.cpp
index ef7ba54ff6ddc..b2757c145cba9 100644
--- a/bolt/lib/Core/DIEBuilder.cpp
+++ b/bolt/lib/Core/DIEBuilder.cpp
@@ -268,6 +268,22 @@ static unsigned int getCUNum(DWARFContext *DwarfContext, bool IsDWO) {
void DIEBuilder::buildTypeUnits(DebugStrOffsetsWriter *StrOffsetWriter,
const bool Init) {
+ if (isDWO() && DwarfContext && DwarfContext->isDWP()) {
+ buildTypeUnitsThreadSafe(StrOffsetWriter, Init);
+ return;
+ }
+
+ buildTypeUnitsImpl(StrOffsetWriter, Init);
+}
+
+void DIEBuilder::buildTypeUnitsThreadSafe(
+ DebugStrOffsetsWriter *StrOffsetWriter, const bool Init) {
+ std::unique_lock<std::recursive_mutex> LockGuard(BC.getUnitsMutex());
+ buildTypeUnitsImpl(StrOffsetWriter, Init);
+}
+
+void DIEBuilder::buildTypeUnitsImpl(DebugStrOffsetsWriter *StrOffsetWriter,
+ const bool Init) {
if (Init)
BuilderState.reset(new State());
diff --git a/bolt/lib/Rewrite/RewriteInstance.cpp b/bolt/lib/Rewrite/RewriteInstance.cpp
index 3d933cf24b458..06cd1c64ace0f 100644
--- a/bolt/lib/Rewrite/RewriteInstance.cpp
+++ b/bolt/lib/Rewrite/RewriteInstance.cpp
@@ -441,10 +441,10 @@ RewriteInstance::RewriteInstance(ELFObjectFileBase *File, const int Argc,
auto BCOrErr = BinaryContext::createBinaryContext(
TheTriple, std::make_shared<orc::SymbolStringPool>(), File->getFileName(),
Features.get(), IsPIC,
- DWARFContext::create(*File, DWARFContext::ProcessDebugRelocations::Ignore,
- nullptr, opts::DWPPathName,
- WithColor::defaultErrorHandler,
- WithColor::defaultWarningHandler),
+ DWARFContext::create(
+ *File, DWARFContext::ProcessDebugRelocations::Ignore, nullptr,
+ opts::DWPPathName, WithColor::defaultErrorHandler,
+ WithColor::defaultWarningHandler, !opts::DWPPathName.empty()),
JournalingStreams{Stdout, Stderr});
if (Error E = BCOrErr.takeError()) {
Err = std::move(E);
diff --git a/bolt/test/X86/dwarf5-df-types-dup-dwp-input.test b/bolt/test/X86/dwarf5-df-types-dup-dwp-input.test
index 754f05dc96328..923d13dafdaf4 100644
--- a/bolt/test/X86/dwarf5-df-types-dup-dwp-input.test
+++ b/bolt/test/X86/dwarf5-df-types-dup-dwp-input.test
@@ -7,7 +7,7 @@
; RUN: -split-dwarf-file=helper.dwo -o helper.o
; RUN: %clang %cflags -gdwarf-5 -gsplit-dwarf=split main.o helper.o -o main.exe
; RUN: llvm-dwp -e main.exe -o main.exe.dwp
-; RUN: llvm-bolt main.exe -o main.exe.bolt --update-debug-sections
+; RUN: llvm-bolt main.exe -o main.exe.bolt --update-debug-sections --debug-thread-count=4 --cu-processing-batch-size=4
; RUN: llvm-dwarfdump --debug-info -r 0 main.dwo.dwo | FileCheck -check-prefix=BOLT-DWO-DWO-MAIN %s
; RUN: llvm-dwarfdump --debug-info -r 0 helper.dwo.dwo | FileCheck -check-prefix=BOLT-DWO-DWO-HELPER %s
diff --git a/bolt/test/X86/dwarf5-dwp-tsan-data-race.test b/bolt/test/X86/dwarf5-dwp-tsan-data-race.test
new file mode 100644
index 0000000000000..f961cc0b1abf2
--- /dev/null
+++ b/bolt/test/X86/dwarf5-dwp-tsan-data-race.test
@@ -0,0 +1,26 @@
+; RUN: rm -rf %t
+; RUN: mkdir %t
+; RUN: cd %t
+; RUN: llvm-mc -dwarf-version=5 -filetype=obj -triple x86_64-unknown-linux %p/Inputs/dwarf5-df-types-dup-main.s \
+; RUN: -split-dwarf-file=main.dwo -o main.o
+; RUN: llvm-mc -dwarf-version=5 -filetype=obj -triple x86_64-unknown-linux %p/Inputs/dwarf5-df-types-dup-helper.s \
+; RUN: -split-dwarf-file=helper.dwo -o helper.o
+; RUN: %clang %cflags -gdwarf-5 -gsplit-dwarf=split main.o helper.o -o main.exe
+; RUN: llvm-dwp -e main.exe -o main.exe.dwp
+; RUN: llvm-bolt main.exe -o main.exe.bolt --update-debug-sections \
+; RUN: --thread-count=4 --cu-processing-batch-size=4 --dwp=main.exe.dwp 2>&1 | FileCheck %s
+; RUN: llvm-dwarfdump --debug-info -r 0 main.dwo.dwo | FileCheck -check-prefix=BOLT-DWO-DWO-MAIN %s
+; RUN: llvm-dwarfdump --debug-info -r 0 helper.dwo.dwo | FileCheck -check-prefix=BOLT-DWO-DWO-HELPER %s
+
+;; Tests that BOLT correctly handles DWARF5 DWP file as input in a
+;; multi-threaded configuration without triggering ThreadSanitizer (TSan)
+;; data race reports. The binary must be built with TSan instrumentation
+;; (i.e. LLVM_USE_SANITIZER=Thread) for this test to be effective.
+;; REQUIRES: tsan
+
+; CHECK-NOT: ThreadSanitizer: data race
+; CHECK-NOT: ThreadSanitizer: reported
+; CHECK-NOT: WARNING: ThreadSanitizer
+
+; BOLT-DWO-DWO-MAIN: DW_TAG_compile_unit
+; BOLT-DWO-DWO-HELPER: DW_TAG_compile_unit
\ No newline at end of file
More information about the llvm-commits
mailing list