Skip to content

Commit

Permalink
ManagedStatic: remove many straightforward uses in llvm
Browse files Browse the repository at this point in the history
(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
  • Loading branch information
nhaehnle committed Jul 10, 2022
1 parent e9ce1a5 commit ede6003
Show file tree
Hide file tree
Showing 31 changed files with 148 additions and 143 deletions.
4 changes: 2 additions & 2 deletions llvm/include/llvm/IR/OptBisect.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
#define LLVM_IR_OPTBISECT_H

#include "llvm/ADT/StringRef.h"
#include "llvm/Support/ManagedStatic.h"
#include <limits>

namespace llvm {
Expand Down Expand Up @@ -90,7 +89,8 @@ class OptBisect : public OptPassGate {

/// Singleton instance of the OptBisect class, so multiple pass managers don't
/// need to coordinate their uses of OptBisect.
extern ManagedStatic<OptBisect> OptBisector;
OptBisect &getOptBisector();

} // end namespace llvm

#endif // LLVM_IR_OPTBISECT_H
11 changes: 4 additions & 7 deletions llvm/lib/Analysis/TFUtils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
#include "llvm/Support/CommandLine.h"
#include "llvm/Support/Debug.h"
#include "llvm/Support/JSON.h"
#include "llvm/Support/ManagedStatic.h"
#include "llvm/Support/MemoryBuffer.h"
#include "llvm/Support/Path.h"
#include "llvm/Support/raw_ostream.h"
Expand Down Expand Up @@ -49,19 +48,17 @@ using TFStatusPtr = std::unique_ptr<TF_Status, decltype(&TF_DeleteStatus)>;

struct TFInitializer {
TFInitializer() {
assert(!IsInitialized && "TFInitialized should be called only once");
int Argc = 1;
const char *Name = "";
const char **NamePtr = &Name;
TF_InitMain(Name, &Argc, const_cast<char ***>(&NamePtr));
IsInitialized = true;
}
bool IsInitialized = false;
};

llvm::ManagedStatic<TFInitializer> TFLibInitializer;

bool ensureInitTF() { return TFLibInitializer->IsInitialized; }
bool ensureInitTF() {
static TFInitializer TFLibInitializer;
return true;
}

TFGraphPtr createTFGraph() {
return TFGraphPtr(TF_NewGraph(), &TF_DeleteGraph);
Expand Down
6 changes: 2 additions & 4 deletions llvm/lib/Bitcode/Reader/BitcodeReader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,6 @@
#include "llvm/Support/Error.h"
#include "llvm/Support/ErrorHandling.h"
#include "llvm/Support/ErrorOr.h"
#include "llvm/Support/ManagedStatic.h"
#include "llvm/Support/MathExtras.h"
#include "llvm/Support/MemoryBuffer.h"
#include "llvm/Support/raw_ostream.h"
Expand Down Expand Up @@ -7446,10 +7445,9 @@ class BitcodeErrorCategoryType : public std::error_category {

} // end anonymous namespace

static ManagedStatic<BitcodeErrorCategoryType> ErrorCategory;

const std::error_category &llvm::BitcodeErrorCategory() {
return *ErrorCategory;
static BitcodeErrorCategoryType ErrorCategory;
return ErrorCategory;
}

static Expected<StringRef> readBlobInRecord(BitstreamCursor &Stream,
Expand Down
15 changes: 7 additions & 8 deletions llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,6 @@
#include "llvm/Support/ErrorHandling.h"
#include "llvm/Support/KnownBits.h"
#include "llvm/Support/MachineValueType.h"
#include "llvm/Support/ManagedStatic.h"
#include "llvm/Support/MathExtras.h"
#include "llvm/Support/Mutex.h"
#include "llvm/Support/raw_ostream.h"
Expand Down Expand Up @@ -10754,19 +10753,19 @@ namespace {

} // end anonymous namespace

static ManagedStatic<std::set<EVT, EVT::compareRawBits>> EVTs;
static ManagedStatic<EVTArray> SimpleVTArray;
static ManagedStatic<sys::SmartMutex<true>> VTMutex;

/// getValueTypeList - Return a pointer to the specified value type.
///
const EVT *SDNode::getValueTypeList(EVT VT) {
static std::set<EVT, EVT::compareRawBits> EVTs;
static EVTArray SimpleVTArray;
static sys::SmartMutex<true> VTMutex;

if (VT.isExtended()) {
sys::SmartScopedLock<true> Lock(*VTMutex);
return &(*EVTs->insert(VT).first);
sys::SmartScopedLock<true> Lock(VTMutex);
return &(*EVTs.insert(VT).first);
}
assert(VT.getSimpleVT() < MVT::VALUETYPE_SIZE && "Value type out of range!");
return &SimpleVTArray->VTs[VT.getSimpleVT().SimpleTy];
return &SimpleVTArray.VTs[VT.getSimpleVT().SimpleTy];
}

/// hasNUsesOfValue - Return true if there are exactly NUSES uses of the
Expand Down
5 changes: 2 additions & 3 deletions llvm/lib/DebugInfo/CodeView/CodeViewError.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@

#include "llvm/DebugInfo/CodeView/CodeViewError.h"
#include "llvm/Support/ErrorHandling.h"
#include "llvm/Support/ManagedStatic.h"
#include <string>

using namespace llvm;
Expand Down Expand Up @@ -42,9 +41,9 @@ class CodeViewErrorCategory : public std::error_category {
};
} // namespace

static llvm::ManagedStatic<CodeViewErrorCategory> CodeViewErrCategory;
const std::error_category &llvm::codeview::CVErrorCategory() {
return *CodeViewErrCategory;
static CodeViewErrorCategory CodeViewErrCategory;
return CodeViewErrCategory;
}

char CodeViewError::ID;
7 changes: 4 additions & 3 deletions llvm/lib/DebugInfo/MSF/MSFError.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@

#include "llvm/DebugInfo/MSF/MSFError.h"
#include "llvm/Support/ErrorHandling.h"
#include "llvm/Support/ManagedStatic.h"
#include <string>

using namespace llvm;
Expand Down Expand Up @@ -50,7 +49,9 @@ class MSFErrorCategory : public std::error_category {
};
} // namespace

static llvm::ManagedStatic<MSFErrorCategory> MSFCategory;
const std::error_category &llvm::msf::MSFErrCategory() { return *MSFCategory; }
const std::error_category &llvm::msf::MSFErrCategory() {
static MSFErrorCategory MSFCategory;
return MSFCategory;
}

char MSFError::ID;
7 changes: 4 additions & 3 deletions llvm/lib/DebugInfo/PDB/DIA/DIAError.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
#include "llvm/DebugInfo/PDB/DIA/DIAError.h"
#include "llvm/Support/ErrorHandling.h"
#include "llvm/Support/ManagedStatic.h"

using namespace llvm;
using namespace llvm::pdb;
Expand Down Expand Up @@ -31,7 +30,9 @@ class DIAErrorCategory : public std::error_category {
}
};

static llvm::ManagedStatic<DIAErrorCategory> DIACategory;
const std::error_category &llvm::pdb::DIAErrCategory() { return *DIACategory; }
const std::error_category &llvm::pdb::DIAErrCategory() {
static DIAErrorCategory DIACategory;
return DIACategory;
}

char DIAError::ID;
7 changes: 4 additions & 3 deletions llvm/lib/DebugInfo/PDB/GenericError.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@

#include "llvm/DebugInfo/PDB/GenericError.h"
#include "llvm/Support/ErrorHandling.h"
#include "llvm/Support/ManagedStatic.h"

using namespace llvm;
using namespace llvm::pdb;
Expand Down Expand Up @@ -42,7 +41,9 @@ class PDBErrorCategory : public std::error_category {
};
} // namespace

static llvm::ManagedStatic<PDBErrorCategory> PDBCategory;
const std::error_category &llvm::pdb::PDBErrCategory() { return *PDBCategory; }
const std::error_category &llvm::pdb::PDBErrCategory() {
static PDBErrorCategory PDBCategory;
return PDBCategory;
}

char PDBError::ID;
7 changes: 4 additions & 3 deletions llvm/lib/DebugInfo/PDB/Native/RawError.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
#include "llvm/DebugInfo/PDB/Native/RawError.h"
#include "llvm/Support/ErrorHandling.h"
#include "llvm/Support/ManagedStatic.h"

using namespace llvm;
using namespace llvm::pdb;
Expand Down Expand Up @@ -47,7 +46,9 @@ class RawErrorCategory : public std::error_category {
};
} // namespace

static llvm::ManagedStatic<RawErrorCategory> RawCategory;
const std::error_category &llvm::pdb::RawErrCategory() { return *RawCategory; }
const std::error_category &llvm::pdb::RawErrCategory() {
static RawErrorCategory RawCategory;
return RawCategory;
}

char RawError::ID;
6 changes: 2 additions & 4 deletions llvm/lib/ExecutionEngine/JITLink/JITLink.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
#include "llvm/ExecutionEngine/JITLink/ELF.h"
#include "llvm/ExecutionEngine/JITLink/MachO.h"
#include "llvm/Support/Format.h"
#include "llvm/Support/ManagedStatic.h"
#include "llvm/Support/MemoryBuffer.h"
#include "llvm/Support/raw_ostream.h"

Expand Down Expand Up @@ -41,8 +40,6 @@ class JITLinkerErrorCategory : public std::error_category {
}
};

static ManagedStatic<JITLinkerErrorCategory> JITLinkerErrorCategory;

} // namespace

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

std::error_code JITLinkError::convertToErrorCode() const {
return std::error_code(GenericJITLinkError, *JITLinkerErrorCategory);
static JITLinkerErrorCategory TheJITLinkerErrorCategory;
return std::error_code(GenericJITLinkError, TheJITLinkerErrorCategory);
}

const char *getGenericEdgeKindName(Edge::Kind K) {
Expand Down
10 changes: 6 additions & 4 deletions llvm/lib/ExecutionEngine/Orc/Shared/OrcError.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@

#include "llvm/ExecutionEngine/Orc/Shared/OrcError.h"
#include "llvm/Support/ErrorHandling.h"
#include "llvm/Support/ManagedStatic.h"

#include <type_traits>

Expand Down Expand Up @@ -70,7 +69,10 @@ class OrcErrorCategory : public std::error_category {
}
};

static ManagedStatic<OrcErrorCategory> OrcErrCat;
OrcErrorCategory &getOrcErrCat() {
static OrcErrorCategory OrcErrCat;
return OrcErrCat;
}
} // namespace

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

std::error_code orcError(OrcErrorCode ErrCode) {
typedef std::underlying_type<OrcErrorCode>::type UT;
return std::error_code(static_cast<UT>(ErrCode), *OrcErrCat);
return std::error_code(static_cast<UT>(ErrCode), getOrcErrCat());
}

DuplicateDefinition::DuplicateDefinition(std::string SymbolName)
Expand All @@ -105,7 +107,7 @@ JITSymbolNotFound::JITSymbolNotFound(std::string SymbolName)
std::error_code JITSymbolNotFound::convertToErrorCode() const {
typedef std::underlying_type<OrcErrorCode>::type UT;
return std::error_code(static_cast<UT>(OrcErrorCode::JITSymbolNotFound),
*OrcErrCat);
getOrcErrCat());
}

void JITSymbolNotFound::log(raw_ostream &OS) const {
Expand Down
6 changes: 2 additions & 4 deletions llvm/lib/ExecutionEngine/RuntimeDyld/RuntimeDyld.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
#include "llvm/Object/ELFObjectFile.h"
#include "llvm/Support/Alignment.h"
#include "llvm/Support/MSVCErrorWorkarounds.h"
#include "llvm/Support/ManagedStatic.h"
#include "llvm/Support/MathExtras.h"
#include <mutex>

Expand Down Expand Up @@ -51,8 +50,6 @@ class RuntimeDyldErrorCategory : public std::error_category {
}
};

static ManagedStatic<RuntimeDyldErrorCategory> RTDyldErrorCategory;

}

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

std::error_code RuntimeDyldError::convertToErrorCode() const {
return std::error_code(GenericRTDyldError, *RTDyldErrorCategory);
static RuntimeDyldErrorCategory RTDyldErrorCategory;
return std::error_code(GenericRTDyldError, RTDyldErrorCategory);
}

// Empty out-of-line virtual destructor as the key function.
Expand Down
9 changes: 6 additions & 3 deletions llvm/lib/IR/Core.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -74,13 +74,16 @@ void LLVMDisposeMessage(char *Message) {

/*===-- Operations on contexts --------------------------------------------===*/

static ManagedStatic<LLVMContext> GlobalContext;
static LLVMContext &getGlobalContext() {
static LLVMContext GlobalContext;
return GlobalContext;
}

LLVMContextRef LLVMContextCreate() {
return wrap(new LLVMContext());
}

LLVMContextRef LLVMGetGlobalContext() { return wrap(&*GlobalContext); }
LLVMContextRef LLVMGetGlobalContext() { return wrap(&getGlobalContext()); }

void LLVMContextSetDiagnosticHandler(LLVMContextRef C,
LLVMDiagnosticHandler Handler,
Expand Down Expand Up @@ -251,7 +254,7 @@ LLVMDiagnosticSeverity LLVMGetDiagInfoSeverity(LLVMDiagnosticInfoRef DI) {
/*===-- Operations on modules ---------------------------------------------===*/

LLVMModuleRef LLVMModuleCreateWithName(const char *ModuleID) {
return wrap(new Module(ModuleID, *GlobalContext));
return wrap(new Module(ModuleID, getGlobalContext()));
}

LLVMModuleRef LLVMModuleCreateWithNameInContext(const char *ModuleID,
Expand Down
3 changes: 1 addition & 2 deletions llvm/lib/IR/LLVMContextImpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
#include "llvm/Support/CommandLine.h"
#include "llvm/Support/Compiler.h"
#include "llvm/Support/ErrorHandling.h"
#include "llvm/Support/ManagedStatic.h"
#include "llvm/Support/TypeSize.h"
#include <cassert>
#include <utility>
Expand Down Expand Up @@ -241,7 +240,7 @@ void LLVMContextImpl::getSyncScopeNames(
/// singleton OptBisect if not explicitly set.
OptPassGate &LLVMContextImpl::getOptPassGate() const {
if (!OPG)
OPG = &(*OptBisector);
OPG = &getOptBisector();
return *OPG;
}

Expand Down
7 changes: 5 additions & 2 deletions llvm/lib/IR/OptBisect.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ using namespace llvm;
static cl::opt<int> OptBisectLimit("opt-bisect-limit", cl::Hidden,
cl::init(OptBisect::Disabled), cl::Optional,
cl::cb<void, int>([](int Limit) {
llvm::OptBisector->setLimit(Limit);
llvm::getOptBisector().setLimit(Limit);
}),
cl::desc("Maximum optimization to perform"));

Expand Down Expand Up @@ -52,4 +52,7 @@ bool OptBisect::checkPass(const StringRef PassName,

const int OptBisect::Disabled;

ManagedStatic<OptBisect> llvm::OptBisector;
OptBisect &llvm::getOptBisector() {
static OptBisect OptBisector;
return OptBisector;
}
10 changes: 2 additions & 8 deletions llvm/lib/IR/PassRegistry.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,21 +15,15 @@
#include "llvm/ADT/STLExtras.h"
#include "llvm/Pass.h"
#include "llvm/PassInfo.h"
#include "llvm/Support/ManagedStatic.h"
#include <cassert>
#include <memory>
#include <utility>

using namespace llvm;

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

//===----------------------------------------------------------------------===//
Expand Down
Loading

0 comments on commit ede6003

Please sign in to comment.