Skip to content

Commit e6f1f06

Browse files
committed
ManagedStatic: remove many straightforward uses in llvm
Bulk remove many of the more trivial uses of ManagedStatic in the llvm directory, either by defining a new getter function or, in many cases, moving the static variable directly into the only function that uses it. Differential Revision: https://reviews.llvm.org/D129120
1 parent da6a14b commit e6f1f06

File tree

34 files changed

+164
-162
lines changed

34 files changed

+164
-162
lines changed

llvm/include/llvm/IR/OptBisect.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
#define LLVM_IR_OPTBISECT_H
1616

1717
#include "llvm/ADT/StringRef.h"
18-
#include "llvm/Support/ManagedStatic.h"
1918
#include <limits>
2019

2120
namespace llvm {
@@ -90,7 +89,8 @@ class OptBisect : public OptPassGate {
9089

9190
/// Singleton instance of the OptBisect class, so multiple pass managers don't
9291
/// need to coordinate their uses of OptBisect.
93-
extern ManagedStatic<OptBisect> OptBisector;
92+
OptBisect &getOptBisector();
93+
9494
} // end namespace llvm
9595

9696
#endif // LLVM_IR_OPTBISECT_H

llvm/lib/Analysis/TFUtils.cpp

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
#include "llvm/Support/CommandLine.h"
1919
#include "llvm/Support/Debug.h"
2020
#include "llvm/Support/JSON.h"
21-
#include "llvm/Support/ManagedStatic.h"
2221
#include "llvm/Support/MemoryBuffer.h"
2322
#include "llvm/Support/Path.h"
2423
#include "llvm/Support/raw_ostream.h"
@@ -49,19 +48,17 @@ using TFStatusPtr = std::unique_ptr<TF_Status, decltype(&TF_DeleteStatus)>;
4948

5049
struct TFInitializer {
5150
TFInitializer() {
52-
assert(!IsInitialized && "TFInitialized should be called only once");
5351
int Argc = 1;
5452
const char *Name = "";
5553
const char **NamePtr = &Name;
5654
TF_InitMain(Name, &Argc, const_cast<char ***>(&NamePtr));
57-
IsInitialized = true;
5855
}
59-
bool IsInitialized = false;
6056
};
6157

62-
llvm::ManagedStatic<TFInitializer> TFLibInitializer;
63-
64-
bool ensureInitTF() { return TFLibInitializer->IsInitialized; }
58+
bool ensureInitTF() {
59+
static TFInitializer TFLibInitializer;
60+
return true;
61+
}
6562

6663
TFGraphPtr createTFGraph() {
6764
return TFGraphPtr(TF_NewGraph(), &TF_DeleteGraph);

llvm/lib/Bitcode/Reader/BitcodeReader.cpp

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,6 @@
6969
#include "llvm/Support/Error.h"
7070
#include "llvm/Support/ErrorHandling.h"
7171
#include "llvm/Support/ErrorOr.h"
72-
#include "llvm/Support/ManagedStatic.h"
7372
#include "llvm/Support/MathExtras.h"
7473
#include "llvm/Support/MemoryBuffer.h"
7574
#include "llvm/Support/raw_ostream.h"
@@ -7446,10 +7445,9 @@ class BitcodeErrorCategoryType : public std::error_category {
74467445

74477446
} // end anonymous namespace
74487447

7449-
static ManagedStatic<BitcodeErrorCategoryType> ErrorCategory;
7450-
74517448
const std::error_category &llvm::BitcodeErrorCategory() {
7452-
return *ErrorCategory;
7449+
static BitcodeErrorCategoryType ErrorCategory;
7450+
return ErrorCategory;
74537451
}
74547452

74557453
static Expected<StringRef> readBlobInRecord(BitstreamCursor &Stream,

llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,6 @@
6060
#include "llvm/Support/ErrorHandling.h"
6161
#include "llvm/Support/KnownBits.h"
6262
#include "llvm/Support/MachineValueType.h"
63-
#include "llvm/Support/ManagedStatic.h"
6463
#include "llvm/Support/MathExtras.h"
6564
#include "llvm/Support/Mutex.h"
6665
#include "llvm/Support/raw_ostream.h"
@@ -10754,19 +10753,19 @@ namespace {
1075410753

1075510754
} // end anonymous namespace
1075610755

10757-
static ManagedStatic<std::set<EVT, EVT::compareRawBits>> EVTs;
10758-
static ManagedStatic<EVTArray> SimpleVTArray;
10759-
static ManagedStatic<sys::SmartMutex<true>> VTMutex;
10760-
1076110756
/// getValueTypeList - Return a pointer to the specified value type.
1076210757
///
1076310758
const EVT *SDNode::getValueTypeList(EVT VT) {
10759+
static std::set<EVT, EVT::compareRawBits> EVTs;
10760+
static EVTArray SimpleVTArray;
10761+
static sys::SmartMutex<true> VTMutex;
10762+
1076410763
if (VT.isExtended()) {
10765-
sys::SmartScopedLock<true> Lock(*VTMutex);
10766-
return &(*EVTs->insert(VT).first);
10764+
sys::SmartScopedLock<true> Lock(VTMutex);
10765+
return &(*EVTs.insert(VT).first);
1076710766
}
1076810767
assert(VT.getSimpleVT() < MVT::VALUETYPE_SIZE && "Value type out of range!");
10769-
return &SimpleVTArray->VTs[VT.getSimpleVT().SimpleTy];
10768+
return &SimpleVTArray.VTs[VT.getSimpleVT().SimpleTy];
1077010769
}
1077110770

1077210771
/// hasNUsesOfValue - Return true if there are exactly NUSES uses of the

llvm/lib/DebugInfo/CodeView/CodeViewError.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88

99
#include "llvm/DebugInfo/CodeView/CodeViewError.h"
1010
#include "llvm/Support/ErrorHandling.h"
11-
#include "llvm/Support/ManagedStatic.h"
1211
#include <string>
1312

1413
using namespace llvm;
@@ -42,9 +41,9 @@ class CodeViewErrorCategory : public std::error_category {
4241
};
4342
} // namespace
4443

45-
static llvm::ManagedStatic<CodeViewErrorCategory> CodeViewErrCategory;
4644
const std::error_category &llvm::codeview::CVErrorCategory() {
47-
return *CodeViewErrCategory;
45+
static CodeViewErrorCategory CodeViewErrCategory;
46+
return CodeViewErrCategory;
4847
}
4948

5049
char CodeViewError::ID;

llvm/lib/DebugInfo/MSF/MSFError.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88

99
#include "llvm/DebugInfo/MSF/MSFError.h"
1010
#include "llvm/Support/ErrorHandling.h"
11-
#include "llvm/Support/ManagedStatic.h"
1211
#include <string>
1312

1413
using namespace llvm;
@@ -50,7 +49,9 @@ class MSFErrorCategory : public std::error_category {
5049
};
5150
} // namespace
5251

53-
static llvm::ManagedStatic<MSFErrorCategory> MSFCategory;
54-
const std::error_category &llvm::msf::MSFErrCategory() { return *MSFCategory; }
52+
const std::error_category &llvm::msf::MSFErrCategory() {
53+
static MSFErrorCategory MSFCategory;
54+
return MSFCategory;
55+
}
5556

5657
char MSFError::ID;

llvm/lib/DebugInfo/PDB/DIA/DIAError.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
#include "llvm/DebugInfo/PDB/DIA/DIAError.h"
22
#include "llvm/Support/ErrorHandling.h"
3-
#include "llvm/Support/ManagedStatic.h"
43

54
using namespace llvm;
65
using namespace llvm::pdb;
@@ -31,7 +30,9 @@ class DIAErrorCategory : public std::error_category {
3130
}
3231
};
3332

34-
static llvm::ManagedStatic<DIAErrorCategory> DIACategory;
35-
const std::error_category &llvm::pdb::DIAErrCategory() { return *DIACategory; }
33+
const std::error_category &llvm::pdb::DIAErrCategory() {
34+
static DIAErrorCategory DIACategory;
35+
return DIACategory;
36+
}
3637

3738
char DIAError::ID;

llvm/lib/DebugInfo/PDB/GenericError.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88

99
#include "llvm/DebugInfo/PDB/GenericError.h"
1010
#include "llvm/Support/ErrorHandling.h"
11-
#include "llvm/Support/ManagedStatic.h"
1211

1312
using namespace llvm;
1413
using namespace llvm::pdb;
@@ -42,7 +41,9 @@ class PDBErrorCategory : public std::error_category {
4241
};
4342
} // namespace
4443

45-
static llvm::ManagedStatic<PDBErrorCategory> PDBCategory;
46-
const std::error_category &llvm::pdb::PDBErrCategory() { return *PDBCategory; }
44+
const std::error_category &llvm::pdb::PDBErrCategory() {
45+
static PDBErrorCategory PDBCategory;
46+
return PDBCategory;
47+
}
4748

4849
char PDBError::ID;

llvm/lib/DebugInfo/PDB/Native/RawError.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
#include "llvm/DebugInfo/PDB/Native/RawError.h"
22
#include "llvm/Support/ErrorHandling.h"
3-
#include "llvm/Support/ManagedStatic.h"
43

54
using namespace llvm;
65
using namespace llvm::pdb;
@@ -47,7 +46,9 @@ class RawErrorCategory : public std::error_category {
4746
};
4847
} // namespace
4948

50-
static llvm::ManagedStatic<RawErrorCategory> RawCategory;
51-
const std::error_category &llvm::pdb::RawErrCategory() { return *RawCategory; }
49+
const std::error_category &llvm::pdb::RawErrCategory() {
50+
static RawErrorCategory RawCategory;
51+
return RawCategory;
52+
}
5253

5354
char RawError::ID;

llvm/lib/ExecutionEngine/GDBRegistrationListener.cpp

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
#include "llvm/Object/ObjectFile.h"
1313
#include "llvm/Support/Compiler.h"
1414
#include "llvm/Support/ErrorHandling.h"
15-
#include "llvm/Support/ManagedStatic.h"
1615
#include "llvm/Support/MemoryBuffer.h"
1716
#include "llvm/Support/Mutex.h"
1817
#include <mutex>
@@ -123,7 +122,10 @@ class GDBJITRegistrationListener : public JITEventListener {
123122

124123
/// Lock used to serialize all jit registration events, since they
125124
/// modify global variables.
126-
ManagedStatic<sys::Mutex> JITDebugLock;
125+
sys::Mutex &getJITDebugLock() {
126+
static sys::Mutex JITDebugLock;
127+
return JITDebugLock;
128+
}
127129

128130
/// Do the registration.
129131
void NotifyDebugger(jit_code_entry* JITCodeEntry) {
@@ -143,7 +145,7 @@ void NotifyDebugger(jit_code_entry* JITCodeEntry) {
143145

144146
GDBJITRegistrationListener::~GDBJITRegistrationListener() {
145147
// Free all registered object files.
146-
std::lock_guard<llvm::sys::Mutex> locked(*JITDebugLock);
148+
std::lock_guard<llvm::sys::Mutex> locked(getJITDebugLock());
147149
for (RegisteredObjectBufferMap::iterator I = ObjectBufferMap.begin(),
148150
E = ObjectBufferMap.end();
149151
I != E; ++I) {
@@ -167,7 +169,7 @@ void GDBJITRegistrationListener::notifyObjectLoaded(
167169
const char *Buffer = DebugObj.getBinary()->getMemoryBufferRef().getBufferStart();
168170
size_t Size = DebugObj.getBinary()->getMemoryBufferRef().getBufferSize();
169171

170-
std::lock_guard<llvm::sys::Mutex> locked(*JITDebugLock);
172+
std::lock_guard<llvm::sys::Mutex> locked(getJITDebugLock());
171173
assert(ObjectBufferMap.find(K) == ObjectBufferMap.end() &&
172174
"Second attempt to perform debug registration.");
173175
jit_code_entry* JITCodeEntry = new jit_code_entry();
@@ -186,7 +188,7 @@ void GDBJITRegistrationListener::notifyObjectLoaded(
186188
}
187189

188190
void GDBJITRegistrationListener::notifyFreeingObject(ObjectKey K) {
189-
std::lock_guard<llvm::sys::Mutex> locked(*JITDebugLock);
191+
std::lock_guard<llvm::sys::Mutex> locked(getJITDebugLock());
190192
RegisteredObjectBufferMap::iterator I = ObjectBufferMap.find(K);
191193

192194
if (I != ObjectBufferMap.end()) {
@@ -228,14 +230,13 @@ void GDBJITRegistrationListener::deregisterObjectInternal(
228230
JITCodeEntry = nullptr;
229231
}
230232

231-
llvm::ManagedStatic<GDBJITRegistrationListener> GDBRegListener;
232-
233233
} // end namespace
234234

235235
namespace llvm {
236236

237237
JITEventListener* JITEventListener::createGDBRegistrationListener() {
238-
return &*GDBRegListener;
238+
static GDBJITRegistrationListener GDBRegListener;
239+
return &GDBRegListener;
239240
}
240241

241242
} // namespace llvm

llvm/lib/ExecutionEngine/JITLink/JITLink.cpp

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
#include "llvm/ExecutionEngine/JITLink/ELF.h"
1313
#include "llvm/ExecutionEngine/JITLink/MachO.h"
1414
#include "llvm/Support/Format.h"
15-
#include "llvm/Support/ManagedStatic.h"
1615
#include "llvm/Support/MemoryBuffer.h"
1716
#include "llvm/Support/raw_ostream.h"
1817

@@ -41,8 +40,6 @@ class JITLinkerErrorCategory : public std::error_category {
4140
}
4241
};
4342

44-
static ManagedStatic<JITLinkerErrorCategory> JITLinkerErrorCategory;
45-
4643
} // namespace
4744

4845
namespace llvm {
@@ -53,7 +50,8 @@ char JITLinkError::ID = 0;
5350
void JITLinkError::log(raw_ostream &OS) const { OS << ErrMsg; }
5451

5552
std::error_code JITLinkError::convertToErrorCode() const {
56-
return std::error_code(GenericJITLinkError, *JITLinkerErrorCategory);
53+
static JITLinkerErrorCategory TheJITLinkerErrorCategory;
54+
return std::error_code(GenericJITLinkError, TheJITLinkerErrorCategory);
5755
}
5856

5957
const char *getGenericEdgeKindName(Edge::Kind K) {

llvm/lib/ExecutionEngine/Orc/Shared/OrcError.cpp

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212

1313
#include "llvm/ExecutionEngine/Orc/Shared/OrcError.h"
1414
#include "llvm/Support/ErrorHandling.h"
15-
#include "llvm/Support/ManagedStatic.h"
1615

1716
#include <type_traits>
1817

@@ -70,7 +69,10 @@ class OrcErrorCategory : public std::error_category {
7069
}
7170
};
7271

73-
static ManagedStatic<OrcErrorCategory> OrcErrCat;
72+
OrcErrorCategory &getOrcErrCat() {
73+
static OrcErrorCategory OrcErrCat;
74+
return OrcErrCat;
75+
}
7476
} // namespace
7577

7678
namespace llvm {
@@ -81,7 +83,7 @@ char JITSymbolNotFound::ID = 0;
8183

8284
std::error_code orcError(OrcErrorCode ErrCode) {
8385
typedef std::underlying_type<OrcErrorCode>::type UT;
84-
return std::error_code(static_cast<UT>(ErrCode), *OrcErrCat);
86+
return std::error_code(static_cast<UT>(ErrCode), getOrcErrCat());
8587
}
8688

8789
DuplicateDefinition::DuplicateDefinition(std::string SymbolName)
@@ -105,7 +107,7 @@ JITSymbolNotFound::JITSymbolNotFound(std::string SymbolName)
105107
std::error_code JITSymbolNotFound::convertToErrorCode() const {
106108
typedef std::underlying_type<OrcErrorCode>::type UT;
107109
return std::error_code(static_cast<UT>(OrcErrorCode::JITSymbolNotFound),
108-
*OrcErrCat);
110+
getOrcErrCat());
109111
}
110112

111113
void JITSymbolNotFound::log(raw_ostream &OS) const {

llvm/lib/ExecutionEngine/Orc/TargetProcess/JITLoaderGDB.cpp

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
#include "llvm/ExecutionEngine/JITSymbol.h"
1212
#include "llvm/Support/BinaryStreamReader.h"
1313
#include "llvm/Support/FormatVariadic.h"
14-
#include "llvm/Support/ManagedStatic.h"
1514

1615
#include <cstdint>
1716
#include <mutex>
@@ -67,9 +66,6 @@ LLVM_ATTRIBUTE_NOINLINE void __jit_debug_register_code() {
6766
using namespace llvm;
6867
using namespace llvm::orc;
6968

70-
// Serialize rendezvous with the debugger as well as access to shared data.
71-
ManagedStatic<std::mutex> JITDebugLock;
72-
7369
// Register debug object, return error message or null for success.
7470
static void registerJITLoaderGDBImpl(const char *ObjAddr, size_t Size) {
7571
LLVM_DEBUG({
@@ -85,7 +81,9 @@ static void registerJITLoaderGDBImpl(const char *ObjAddr, size_t Size) {
8581
E->symfile_size = Size;
8682
E->prev_entry = nullptr;
8783

88-
std::lock_guard<std::mutex> Lock(*JITDebugLock);
84+
// Serialize rendezvous with the debugger as well as access to shared data.
85+
static std::mutex JITDebugLock;
86+
std::lock_guard<std::mutex> Lock(JITDebugLock);
8987

9088
// Insert this entry at the head of the list.
9189
jit_code_entry *NextEntry = __jit_debug_descriptor.first_entry;

llvm/lib/ExecutionEngine/PerfJITEvents/PerfJITEventListener.cpp

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424
#include "llvm/Support/Debug.h"
2525
#include "llvm/Support/Errno.h"
2626
#include "llvm/Support/FileSystem.h"
27-
#include "llvm/Support/ManagedStatic.h"
2827
#include "llvm/Support/MemoryBuffer.h"
2928
#include "llvm/Support/Mutex.h"
3029
#include "llvm/Support/Path.h"
@@ -488,15 +487,14 @@ void PerfJITEventListener::NotifyDebug(uint64_t CodeAddr,
488487
}
489488
}
490489

491-
// There should be only a single event listener per process, otherwise perf gets
492-
// confused.
493-
llvm::ManagedStatic<PerfJITEventListener> PerfListener;
494-
495490
} // end anonymous namespace
496491

497492
namespace llvm {
498493
JITEventListener *JITEventListener::createPerfJITEventListener() {
499-
return &*PerfListener;
494+
// There should be only a single event listener per process, otherwise perf
495+
// gets confused.
496+
static PerfJITEventListener PerfListener;
497+
return &PerfListener;
500498
}
501499

502500
} // namespace llvm

0 commit comments

Comments
 (0)