[llvm] [ProfileData] Make FunctionSamples profile-format flags atomic (PR #208964)

Fangrui Song via llvm-commits llvm-commits at lists.llvm.org
Sat Jul 11 16:30:25 PDT 2026


https://github.com/MaskRay created https://github.com/llvm/llvm-project/pull/208964

`lld/test/ELF/lto/sample-profile.ll` is failing in ThreadSanitizer
build.

In a parallel in-process ThinLTO link, each backend thread reads the
sample profile in SampleProfileLoader::doInitialization and writes these
globals.

Make the variables std::atomic<bool> so the same-value writes are
well-defined. An architectural ideal solution that holds these states in
a container seems very intrusive.


>From 905b4b7622c528fd0601af840c8e815968093582 Mon Sep 17 00:00:00 2001
From: Fangrui Song <i at maskray.me>
Date: Sat, 11 Jul 2026 16:05:42 -0700
Subject: [PATCH] [ProfileData] Make FunctionSamples profile-format flags
 atomic

`lld/test/ELF/lto/sample-profile.ll` is failing in ThreadSanitizer
build.

In a parallel in-process ThinLTO link, each backend thread reads the
sample profile in SampleProfileLoader::doInitialization and writes these
globals.

Make the variables std::atomic<bool> so the same-value writes are
well-defined. An architectural ideal solution that holds these states in
a container seems very intrusive.
---
 llvm/include/llvm/ProfileData/SampleProf.h | 19 +++++++++++--------
 llvm/lib/ProfileData/SampleProf.cpp        | 12 ++++++------
 2 files changed, 17 insertions(+), 14 deletions(-)

diff --git a/llvm/include/llvm/ProfileData/SampleProf.h b/llvm/include/llvm/ProfileData/SampleProf.h
index b9eabcd661549c..e34dcbb2dc4640 100644
--- a/llvm/include/llvm/ProfileData/SampleProf.h
+++ b/llvm/include/llvm/ProfileData/SampleProf.h
@@ -30,6 +30,7 @@
 #include "llvm/Support/ErrorOr.h"
 #include "llvm/Support/MathExtras.h"
 #include <algorithm>
+#include <atomic>
 #include <cstdint>
 #include <list>
 #include <map>
@@ -1333,24 +1334,26 @@ class FunctionSamples {
                       const HashKeyMap<DenseMap, FunctionId, FunctionId>
                           *FuncNameToProfNameMap = nullptr) const;
 
-  LLVM_ABI static bool ProfileIsProbeBased;
+  SampleContext &getContext() const { return Context; }
 
-  LLVM_ABI static bool ProfileIsCS;
+  void setContext(const SampleContext &FContext) { Context = FContext; }
 
-  LLVM_ABI static bool ProfileIsPreInlined;
+  // These boolean variables are atomic so that parallel in-process ThinLTO
+  // backends writing the same value do not race.
+  LLVM_ABI static std::atomic<bool> ProfileIsProbeBased;
 
-  SampleContext &getContext() const { return Context; }
+  LLVM_ABI static std::atomic<bool> ProfileIsCS;
 
-  void setContext(const SampleContext &FContext) { Context = FContext; }
+  LLVM_ABI static std::atomic<bool> ProfileIsPreInlined;
 
   /// Whether the profile uses MD5 to represent string.
-  LLVM_ABI static bool UseMD5;
+  LLVM_ABI static std::atomic<bool> UseMD5;
 
   /// Whether the profile contains any ".__uniq." suffix in a name.
-  LLVM_ABI static bool HasUniqSuffix;
+  LLVM_ABI static std::atomic<bool> HasUniqSuffix;
 
   /// If this profile uses flow sensitive discriminators.
-  LLVM_ABI static bool ProfileIsFS;
+  LLVM_ABI static std::atomic<bool> ProfileIsFS;
 
   /// GUIDToFuncNameMap saves the mapping from GUID to the symbol name, for
   /// all the function symbols defined or declared in current module.
diff --git a/llvm/lib/ProfileData/SampleProf.cpp b/llvm/lib/ProfileData/SampleProf.cpp
index 7091d819257338..d560c30f176170 100644
--- a/llvm/lib/ProfileData/SampleProf.cpp
+++ b/llvm/lib/ProfileData/SampleProf.cpp
@@ -43,12 +43,12 @@ static cl::opt<bool> GenerateMergedBaseProfiles(
 
 namespace llvm {
 namespace sampleprof {
-bool FunctionSamples::ProfileIsProbeBased = false;
-bool FunctionSamples::ProfileIsCS = false;
-bool FunctionSamples::ProfileIsPreInlined = false;
-bool FunctionSamples::UseMD5 = false;
-bool FunctionSamples::HasUniqSuffix = true;
-bool FunctionSamples::ProfileIsFS = false;
+std::atomic<bool> FunctionSamples::ProfileIsProbeBased;
+std::atomic<bool> FunctionSamples::ProfileIsCS;
+std::atomic<bool> FunctionSamples::ProfileIsPreInlined;
+std::atomic<bool> FunctionSamples::UseMD5;
+std::atomic<bool> FunctionSamples::HasUniqSuffix = true;
+std::atomic<bool> FunctionSamples::ProfileIsFS;
 
 std::error_code
 serializeTypeMap(const TypeCountMap &Map,



More information about the llvm-commits mailing list