Skip to content

Commit ede6003

Browse files
committed
ManagedStatic: remove many straightforward uses in llvm
(Reapply after revert in e9ce1a5 due to Fuchsia test failures. Removed changes in lib/ExecutionEngine/ other than error categories, to be checked in more detail and reapplied separately.) 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 e9ce1a5 commit ede6003

31 files changed

+148
-143
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/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/RuntimeDyld/RuntimeDyld.cpp

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
#include "llvm/Object/ELFObjectFile.h"
2020
#include "llvm/Support/Alignment.h"
2121
#include "llvm/Support/MSVCErrorWorkarounds.h"
22-
#include "llvm/Support/ManagedStatic.h"
2322
#include "llvm/Support/MathExtras.h"
2423
#include <mutex>
2524

@@ -51,8 +50,6 @@ class RuntimeDyldErrorCategory : public std::error_category {
5150
}
5251
};
5352

54-
static ManagedStatic<RuntimeDyldErrorCategory> RTDyldErrorCategory;
55-
5653
}
5754

5855
char RuntimeDyldError::ID = 0;
@@ -62,7 +59,8 @@ void RuntimeDyldError::log(raw_ostream &OS) const {
6259
}
6360

6461
std::error_code RuntimeDyldError::convertToErrorCode() const {
65-
return std::error_code(GenericRTDyldError, *RTDyldErrorCategory);
62+
static RuntimeDyldErrorCategory RTDyldErrorCategory;
63+
return std::error_code(GenericRTDyldError, RTDyldErrorCategory);
6664
}
6765

6866
// Empty out-of-line virtual destructor as the key function.

llvm/lib/IR/Core.cpp

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -74,13 +74,16 @@ void LLVMDisposeMessage(char *Message) {
7474

7575
/*===-- Operations on contexts --------------------------------------------===*/
7676

77-
static ManagedStatic<LLVMContext> GlobalContext;
77+
static LLVMContext &getGlobalContext() {
78+
static LLVMContext GlobalContext;
79+
return GlobalContext;
80+
}
7881

7982
LLVMContextRef LLVMContextCreate() {
8083
return wrap(new LLVMContext());
8184
}
8285

83-
LLVMContextRef LLVMGetGlobalContext() { return wrap(&*GlobalContext); }
86+
LLVMContextRef LLVMGetGlobalContext() { return wrap(&getGlobalContext()); }
8487

8588
void LLVMContextSetDiagnosticHandler(LLVMContextRef C,
8689
LLVMDiagnosticHandler Handler,
@@ -251,7 +254,7 @@ LLVMDiagnosticSeverity LLVMGetDiagInfoSeverity(LLVMDiagnosticInfoRef DI) {
251254
/*===-- Operations on modules ---------------------------------------------===*/
252255

253256
LLVMModuleRef LLVMModuleCreateWithName(const char *ModuleID) {
254-
return wrap(new Module(ModuleID, *GlobalContext));
257+
return wrap(new Module(ModuleID, getGlobalContext()));
255258
}
256259

257260
LLVMModuleRef LLVMModuleCreateWithNameInContext(const char *ModuleID,

llvm/lib/IR/LLVMContextImpl.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@
2727
#include "llvm/Support/CommandLine.h"
2828
#include "llvm/Support/Compiler.h"
2929
#include "llvm/Support/ErrorHandling.h"
30-
#include "llvm/Support/ManagedStatic.h"
3130
#include "llvm/Support/TypeSize.h"
3231
#include <cassert>
3332
#include <utility>
@@ -241,7 +240,7 @@ void LLVMContextImpl::getSyncScopeNames(
241240
/// singleton OptBisect if not explicitly set.
242241
OptPassGate &LLVMContextImpl::getOptPassGate() const {
243242
if (!OPG)
244-
OPG = &(*OptBisector);
243+
OPG = &getOptBisector();
245244
return *OPG;
246245
}
247246

llvm/lib/IR/OptBisect.cpp

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ using namespace llvm;
2323
static cl::opt<int> OptBisectLimit("opt-bisect-limit", cl::Hidden,
2424
cl::init(OptBisect::Disabled), cl::Optional,
2525
cl::cb<void, int>([](int Limit) {
26-
llvm::OptBisector->setLimit(Limit);
26+
llvm::getOptBisector().setLimit(Limit);
2727
}),
2828
cl::desc("Maximum optimization to perform"));
2929

@@ -52,4 +52,7 @@ bool OptBisect::checkPass(const StringRef PassName,
5252

5353
const int OptBisect::Disabled;
5454

55-
ManagedStatic<OptBisect> llvm::OptBisector;
55+
OptBisect &llvm::getOptBisector() {
56+
static OptBisect OptBisector;
57+
return OptBisector;
58+
}

llvm/lib/IR/PassRegistry.cpp

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,21 +15,15 @@
1515
#include "llvm/ADT/STLExtras.h"
1616
#include "llvm/Pass.h"
1717
#include "llvm/PassInfo.h"
18-
#include "llvm/Support/ManagedStatic.h"
1918
#include <cassert>
2019
#include <memory>
2120
#include <utility>
2221

2322
using namespace llvm;
2423

25-
// FIXME: We use ManagedStatic to erase the pass registrar on shutdown.
26-
// Unfortunately, passes are registered with static ctors, and having
27-
// llvm_shutdown clear this map prevents successful resurrection after
28-
// llvm_shutdown is run. Ideally we should find a solution so that we don't
29-
// leak the map, AND can still resurrect after shutdown.
30-
static ManagedStatic<PassRegistry> PassRegistryObj;
3124
PassRegistry *PassRegistry::getPassRegistry() {
32-
return &*PassRegistryObj;
25+
static PassRegistry PassRegistryObj;
26+
return &PassRegistryObj;
3327
}
3428

3529
//===----------------------------------------------------------------------===//

0 commit comments

Comments
 (0)