Skip to content

Commit e9ce1a5

Browse files
committed
Revert "ManagedStatic: remove many straightforward uses in llvm"
This reverts commit e6f1f06. Reverting due to a failure on the fuchsia-x86_64-linux buildbot.
1 parent e6f1f06 commit e9ce1a5

File tree

34 files changed

+162
-164
lines changed

34 files changed

+162
-164
lines changed

llvm/include/llvm/IR/OptBisect.h

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

1717
#include "llvm/ADT/StringRef.h"
18+
#include "llvm/Support/ManagedStatic.h"
1819
#include <limits>
1920

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

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

9696
#endif // LLVM_IR_OPTBISECT_H

llvm/lib/Analysis/TFUtils.cpp

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

4950
struct TFInitializer {
5051
TFInitializer() {
52+
assert(!IsInitialized && "TFInitialized should be called only once");
5153
int Argc = 1;
5254
const char *Name = "";
5355
const char **NamePtr = &Name;
5456
TF_InitMain(Name, &Argc, const_cast<char ***>(&NamePtr));
57+
IsInitialized = true;
5558
}
59+
bool IsInitialized = false;
5660
};
5761

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

6366
TFGraphPtr createTFGraph() {
6467
return TFGraphPtr(TF_NewGraph(), &TF_DeleteGraph);

llvm/lib/Bitcode/Reader/BitcodeReader.cpp

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

74467447
} // end anonymous namespace
74477448

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

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

llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp

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

1075410755
} // end anonymous namespace
1075510756

10757+
static ManagedStatic<std::set<EVT, EVT::compareRawBits>> EVTs;
10758+
static ManagedStatic<EVTArray> SimpleVTArray;
10759+
static ManagedStatic<sys::SmartMutex<true>> VTMutex;
10760+
1075610761
/// getValueTypeList - Return a pointer to the specified value type.
1075710762
///
1075810763
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-
1076310764
if (VT.isExtended()) {
10764-
sys::SmartScopedLock<true> Lock(VTMutex);
10765-
return &(*EVTs.insert(VT).first);
10765+
sys::SmartScopedLock<true> Lock(*VTMutex);
10766+
return &(*EVTs->insert(VT).first);
1076610767
}
1076710768
assert(VT.getSimpleVT() < MVT::VALUETYPE_SIZE && "Value type out of range!");
10768-
return &SimpleVTArray.VTs[VT.getSimpleVT().SimpleTy];
10769+
return &SimpleVTArray->VTs[VT.getSimpleVT().SimpleTy];
1076910770
}
1077010771

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

llvm/lib/DebugInfo/CodeView/CodeViewError.cpp

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

99
#include "llvm/DebugInfo/CodeView/CodeViewError.h"
1010
#include "llvm/Support/ErrorHandling.h"
11+
#include "llvm/Support/ManagedStatic.h"
1112
#include <string>
1213

1314
using namespace llvm;
@@ -41,9 +42,9 @@ class CodeViewErrorCategory : public std::error_category {
4142
};
4243
} // namespace
4344

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

4950
char CodeViewError::ID;

llvm/lib/DebugInfo/MSF/MSFError.cpp

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

99
#include "llvm/DebugInfo/MSF/MSFError.h"
1010
#include "llvm/Support/ErrorHandling.h"
11+
#include "llvm/Support/ManagedStatic.h"
1112
#include <string>
1213

1314
using namespace llvm;
@@ -49,9 +50,7 @@ class MSFErrorCategory : public std::error_category {
4950
};
5051
} // namespace
5152

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

5756
char MSFError::ID;

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

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

45
using namespace llvm;
56
using namespace llvm::pdb;
@@ -30,9 +31,7 @@ class DIAErrorCategory : public std::error_category {
3031
}
3132
};
3233

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

3837
char DIAError::ID;

llvm/lib/DebugInfo/PDB/GenericError.cpp

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

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

1213
using namespace llvm;
1314
using namespace llvm::pdb;
@@ -41,9 +42,7 @@ class PDBErrorCategory : public std::error_category {
4142
};
4243
} // namespace
4344

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

4948
char PDBError::ID;

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

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

45
using namespace llvm;
56
using namespace llvm::pdb;
@@ -46,9 +47,7 @@ class RawErrorCategory : public std::error_category {
4647
};
4748
} // namespace
4849

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

5453
char RawError::ID;

llvm/lib/ExecutionEngine/GDBRegistrationListener.cpp

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

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

130128
/// Do the registration.
131129
void NotifyDebugger(jit_code_entry* JITCodeEntry) {
@@ -145,7 +143,7 @@ void NotifyDebugger(jit_code_entry* JITCodeEntry) {
145143

146144
GDBJITRegistrationListener::~GDBJITRegistrationListener() {
147145
// Free all registered object files.
148-
std::lock_guard<llvm::sys::Mutex> locked(getJITDebugLock());
146+
std::lock_guard<llvm::sys::Mutex> locked(*JITDebugLock);
149147
for (RegisteredObjectBufferMap::iterator I = ObjectBufferMap.begin(),
150148
E = ObjectBufferMap.end();
151149
I != E; ++I) {
@@ -169,7 +167,7 @@ void GDBJITRegistrationListener::notifyObjectLoaded(
169167
const char *Buffer = DebugObj.getBinary()->getMemoryBufferRef().getBufferStart();
170168
size_t Size = DebugObj.getBinary()->getMemoryBufferRef().getBufferSize();
171169

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

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

194192
if (I != ObjectBufferMap.end()) {
@@ -230,13 +228,14 @@ void GDBJITRegistrationListener::deregisterObjectInternal(
230228
JITCodeEntry = nullptr;
231229
}
232230

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

235235
namespace llvm {
236236

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

242241
} // namespace llvm

llvm/lib/ExecutionEngine/JITLink/JITLink.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
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"
1516
#include "llvm/Support/MemoryBuffer.h"
1617
#include "llvm/Support/raw_ostream.h"
1718

@@ -40,6 +41,8 @@ class JITLinkerErrorCategory : public std::error_category {
4041
}
4142
};
4243

44+
static ManagedStatic<JITLinkerErrorCategory> JITLinkerErrorCategory;
45+
4346
} // namespace
4447

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

5255
std::error_code JITLinkError::convertToErrorCode() const {
53-
static JITLinkerErrorCategory TheJITLinkerErrorCategory;
54-
return std::error_code(GenericJITLinkError, TheJITLinkerErrorCategory);
56+
return std::error_code(GenericJITLinkError, *JITLinkerErrorCategory);
5557
}
5658

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

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

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

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

1617
#include <type_traits>
1718

@@ -69,10 +70,7 @@ class OrcErrorCategory : public std::error_category {
6970
}
7071
};
7172

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

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

8482
std::error_code orcError(OrcErrorCode ErrCode) {
8583
typedef std::underlying_type<OrcErrorCode>::type UT;
86-
return std::error_code(static_cast<UT>(ErrCode), getOrcErrCat());
84+
return std::error_code(static_cast<UT>(ErrCode), *OrcErrCat);
8785
}
8886

8987
DuplicateDefinition::DuplicateDefinition(std::string SymbolName)
@@ -107,7 +105,7 @@ JITSymbolNotFound::JITSymbolNotFound(std::string SymbolName)
107105
std::error_code JITSymbolNotFound::convertToErrorCode() const {
108106
typedef std::underlying_type<OrcErrorCode>::type UT;
109107
return std::error_code(static_cast<UT>(OrcErrorCode::JITSymbolNotFound),
110-
getOrcErrCat());
108+
*OrcErrCat);
111109
}
112110

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

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

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

1516
#include <cstdint>
1617
#include <mutex>
@@ -66,6 +67,9 @@ LLVM_ATTRIBUTE_NOINLINE void __jit_debug_register_code() {
6667
using namespace llvm;
6768
using namespace llvm::orc;
6869

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

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);
88+
std::lock_guard<std::mutex> Lock(*JITDebugLock);
8789

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

llvm/lib/ExecutionEngine/PerfJITEvents/PerfJITEventListener.cpp

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

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

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

500502
} // namespace llvm

0 commit comments

Comments
 (0)