diff --git a/bolt/include/bolt/Core/MCPlusBuilder.h b/bolt/include/bolt/Core/MCPlusBuilder.h index f48cf21852dcb..7eb45d2a91405 100644 --- a/bolt/include/bolt/Core/MCPlusBuilder.h +++ b/bolt/include/bolt/Core/MCPlusBuilder.h @@ -29,6 +29,7 @@ #include "llvm/Support/Allocator.h" #include "llvm/Support/ErrorHandling.h" #include "llvm/Support/ErrorOr.h" +#include "llvm/Support/RWMutex.h" #include #include #include @@ -166,6 +167,10 @@ class MCPlusBuilder { /// Names of non-standard annotations. SmallVector AnnotationNames; + /// A mutex that is used to control parallel accesses to + /// AnnotationNameIndexMap and AnnotationsNames. + mutable llvm::sys::RWMutex AnnotationNameMutex; + /// Allocate the TailCall annotation value. Clients of the target-specific /// MCPlusBuilder classes must use convert/lower/create* interfaces instead. void setTailCall(MCInst &Inst); @@ -1775,6 +1780,7 @@ class MCPlusBuilder { /// Return annotation index matching the \p Name. std::optional getAnnotationIndex(StringRef Name) const { + std::shared_lock Lock(AnnotationNameMutex); auto AI = AnnotationNameIndexMap.find(Name); if (AI != AnnotationNameIndexMap.end()) return AI->second; @@ -1784,10 +1790,10 @@ class MCPlusBuilder { /// Return annotation index matching the \p Name. Create a new index if the /// \p Name wasn't registered previously. unsigned getOrCreateAnnotationIndex(StringRef Name) { - auto AI = AnnotationNameIndexMap.find(Name); - if (AI != AnnotationNameIndexMap.end()) - return AI->second; + if (std::optional Index = getAnnotationIndex(Name)) + return *Index; + std::unique_lock Lock(AnnotationNameMutex); const unsigned Index = AnnotationNameIndexMap.size() + MCPlus::MCAnnotation::kGeneric; AnnotationNameIndexMap.insert(std::make_pair(Name, Index));