PassRegistry.cpp 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. //===- PassRegistry.cpp - Pass Registration Implementation ----------------===//
  2. //
  3. // The LLVM Compiler Infrastructure
  4. //
  5. // This file is distributed under the University of Illinois Open Source
  6. // License. See LICENSE.TXT for details.
  7. //
  8. //===----------------------------------------------------------------------===//
  9. //
  10. // This file implements the PassRegistry, with which passes are registered on
  11. // initialization, and supports the PassManager in dependency resolution.
  12. //
  13. //===----------------------------------------------------------------------===//
  14. #include "llvm/PassRegistry.h"
  15. #include "llvm/IR/Function.h"
  16. #include "llvm/PassSupport.h"
  17. #include "llvm/Support/Compiler.h"
  18. #include "llvm/Support/ManagedStatic.h"
  19. #include "llvm/Support/RWMutex.h"
  20. #include <vector>
  21. using namespace llvm;
  22. #ifdef LLVM_ON_WIN32
  23. // HLSL Change Starts - managed statics are tied to DLL lifetime
  24. // Passes exist only in dxcompiler.dll or in a tool like opt that is updated.
  25. //
  26. // These usage patterns imply the following:
  27. // - llvm_shutdown need not allow resurrection, as it's only called at the
  28. // end of main() or at DLL-unload time, and
  29. // - because there is a fixed number of passes (dynamic loading is currently
  30. // unsupported), there is no need to make this thread-safe.
  31. //
  32. // A simple global initialized at DllMain-time will do (still does more work
  33. // than we should likely perform though).
  34. static uint32_t g_PassRegistryTid;
  35. extern "C" uint32_t __stdcall GetCurrentThreadId(void);
  36. static void CheckThreadId() {
  37. if (g_PassRegistryTid == 0)
  38. g_PassRegistryTid = GetCurrentThreadId();
  39. else
  40. assert(g_PassRegistryTid == GetCurrentThreadId() &&
  41. "else updating PassRegistry from incorrect thread");
  42. }
  43. // HLSL Change Ends
  44. #endif
  45. // FIXME: We use ManagedStatic to erase the pass registrar on shutdown.
  46. // Unfortunately, passes are registered with static ctors, and having
  47. // llvm_shutdown clear this map prevents successful resurrection after
  48. // llvm_shutdown is run. Ideally we should find a solution so that we don't
  49. // leak the map, AND can still resurrect after shutdown.
  50. static ManagedStatic<PassRegistry> PassRegistryObj;
  51. PassRegistry *PassRegistry::getPassRegistry() {
  52. return &*PassRegistryObj;
  53. }
  54. //===----------------------------------------------------------------------===//
  55. // Accessors
  56. //
  57. PassRegistry::~PassRegistry() {}
  58. const PassInfo *PassRegistry::getPassInfo(const void *TI) const {
  59. #ifndef LLVM_ON_WIN32 // HLSL Change
  60. sys::SmartScopedReader<true> Guard(Lock);
  61. #endif
  62. MapType::const_iterator I = PassInfoMap.find(TI);
  63. return I != PassInfoMap.end() ? I->second : nullptr;
  64. }
  65. const PassInfo *PassRegistry::getPassInfo(StringRef Arg) const {
  66. #ifndef LLVM_ON_WIN32 // HLSL Change
  67. sys::SmartScopedReader<true> Guard(Lock);
  68. #endif
  69. StringMapType::const_iterator I = PassInfoStringMap.find(Arg);
  70. return I != PassInfoStringMap.end() ? I->second : nullptr;
  71. }
  72. //===----------------------------------------------------------------------===//
  73. // Pass Registration mechanism
  74. //
  75. void PassRegistry::registerPass(const PassInfo &PI, bool ShouldFree) {
  76. #ifdef LLVM_ON_WIN32 // HLSL Change
  77. CheckThreadId();
  78. #else
  79. sys::SmartScopedReader<true> Guard(Lock);
  80. #endif
  81. bool Inserted =
  82. PassInfoMap.insert(std::make_pair(PI.getTypeInfo(), &PI)).second;
  83. assert(Inserted && "Pass registered multiple times!");
  84. (void)Inserted;
  85. PassInfoStringMap[PI.getPassArgument()] = &PI;
  86. // Notify any listeners.
  87. for (auto *Listener : Listeners)
  88. Listener->passRegistered(&PI);
  89. if (ShouldFree)
  90. ToFree.push_back(std::unique_ptr<const PassInfo>(&PI));
  91. }
  92. void PassRegistry::enumerateWith(PassRegistrationListener *L) {
  93. #ifndef LLVM_ON_WIN32 // HLSL Change
  94. sys::SmartScopedReader<true> Guard(Lock);
  95. #endif
  96. for (auto PassInfoPair : PassInfoMap)
  97. L->passEnumerate(PassInfoPair.second);
  98. }
  99. /// Analysis Group Mechanisms.
  100. void PassRegistry::registerAnalysisGroup(const void *InterfaceID,
  101. const void *PassID,
  102. PassInfo &Registeree, bool isDefault,
  103. bool ShouldFree) {
  104. PassInfo *InterfaceInfo = const_cast<PassInfo *>(getPassInfo(InterfaceID));
  105. if (!InterfaceInfo) {
  106. // First reference to Interface, register it now.
  107. registerPass(Registeree);
  108. InterfaceInfo = &Registeree;
  109. }
  110. assert(Registeree.isAnalysisGroup() &&
  111. "Trying to join an analysis group that is a normal pass!");
  112. if (PassID) {
  113. PassInfo *ImplementationInfo = const_cast<PassInfo *>(getPassInfo(PassID));
  114. assert(ImplementationInfo &&
  115. "Must register pass before adding to AnalysisGroup!");
  116. #ifdef LLVM_ON_WIN32 // HLSL Change
  117. CheckThreadId();
  118. #else
  119. sys::SmartScopedReader<true> Guard(Lock);
  120. #endif
  121. // Make sure we keep track of the fact that the implementation implements
  122. // the interface.
  123. ImplementationInfo->addInterfaceImplemented(InterfaceInfo);
  124. if (isDefault) {
  125. assert(InterfaceInfo->getNormalCtor() == nullptr &&
  126. "Default implementation for analysis group already specified!");
  127. assert(
  128. ImplementationInfo->getNormalCtor() &&
  129. "Cannot specify pass as default if it does not have a default ctor");
  130. InterfaceInfo->setNormalCtor(ImplementationInfo->getNormalCtor());
  131. InterfaceInfo->setTargetMachineCtor(
  132. ImplementationInfo->getTargetMachineCtor());
  133. }
  134. }
  135. if (ShouldFree)
  136. ToFree.push_back(std::unique_ptr<const PassInfo>(&Registeree));
  137. }
  138. void PassRegistry::addRegistrationListener(PassRegistrationListener *L) {
  139. #ifdef LLVM_ON_WIN32 // HLSL Change
  140. CheckThreadId();
  141. #else
  142. sys::SmartScopedReader<true> Guard(Lock);
  143. #endif
  144. Listeners.push_back(L);
  145. }
  146. void PassRegistry::removeRegistrationListener(PassRegistrationListener *L) {
  147. #ifdef LLVM_ON_WIN32 // HLSL Change
  148. CheckThreadId();
  149. #else
  150. sys::SmartScopedReader<true> Guard(Lock);
  151. #endif
  152. auto I = std::find(Listeners.begin(), Listeners.end(), L);
  153. Listeners.erase(I);
  154. }