PassRegistry.cpp 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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. // HLSL Change Starts - managed statics are tied to DLL lifetime
  23. // Passes exist only in dxcompiler.dll or in a tool like opt that is updated.
  24. //
  25. // These usage patterns imply the following:
  26. // - llvm_shutdown need not allow resurrection, as it's only called at the
  27. // end of main() or at DLL-unload time, and
  28. // - because there is a fixed number of passes (dynamic loading is currently
  29. // unsupported), there is no need to make this thread-safe.
  30. //
  31. // A simple global initialized at DllMain-time will do (still does more work
  32. // than we should likely perform though).
  33. static uint32_t g_PassRegistryTid;
  34. extern "C" uint32_t __stdcall GetCurrentThreadId(void);
  35. static void CheckThreadId() {
  36. if (g_PassRegistryTid == 0)
  37. g_PassRegistryTid = GetCurrentThreadId();
  38. else
  39. assert(g_PassRegistryTid == GetCurrentThreadId() &&
  40. "else updating PassRegistry from incorrect thread");
  41. }
  42. // HLSL Change Ends
  43. // FIXME: We use ManagedStatic to erase the pass registrar on shutdown.
  44. // Unfortunately, passes are registered with static ctors, and having
  45. // llvm_shutdown clear this map prevents successful resurrection after
  46. // llvm_shutdown is run. Ideally we should find a solution so that we don't
  47. // leak the map, AND can still resurrect after shutdown.
  48. static ManagedStatic<PassRegistry> PassRegistryObj;
  49. PassRegistry *PassRegistry::getPassRegistry() {
  50. return &*PassRegistryObj;
  51. }
  52. //===----------------------------------------------------------------------===//
  53. // Accessors
  54. //
  55. PassRegistry::~PassRegistry() {}
  56. const PassInfo *PassRegistry::getPassInfo(const void *TI) const {
  57. // sys::SmartScopedReader<true> Guard(Lock); // HLSL Change
  58. MapType::const_iterator I = PassInfoMap.find(TI);
  59. return I != PassInfoMap.end() ? I->second : nullptr;
  60. }
  61. const PassInfo *PassRegistry::getPassInfo(StringRef Arg) const {
  62. // sys::SmartScopedReader<true> Guard(Lock); // HLSL Change
  63. StringMapType::const_iterator I = PassInfoStringMap.find(Arg);
  64. return I != PassInfoStringMap.end() ? I->second : nullptr;
  65. }
  66. //===----------------------------------------------------------------------===//
  67. // Pass Registration mechanism
  68. //
  69. void PassRegistry::registerPass(const PassInfo &PI, bool ShouldFree) {
  70. CheckThreadId(); // sys::SmartScopedReader<true> Guard(Lock); // HLSL Change
  71. bool Inserted =
  72. PassInfoMap.insert(std::make_pair(PI.getTypeInfo(), &PI)).second;
  73. assert(Inserted && "Pass registered multiple times!");
  74. (void)Inserted;
  75. PassInfoStringMap[PI.getPassArgument()] = &PI;
  76. // Notify any listeners.
  77. for (auto *Listener : Listeners)
  78. Listener->passRegistered(&PI);
  79. if (ShouldFree)
  80. ToFree.push_back(std::unique_ptr<const PassInfo>(&PI));
  81. }
  82. void PassRegistry::enumerateWith(PassRegistrationListener *L) {
  83. // sys::SmartScopedReader<true> Guard(Lock); // HLSL Change
  84. for (auto PassInfoPair : PassInfoMap)
  85. L->passEnumerate(PassInfoPair.second);
  86. }
  87. /// Analysis Group Mechanisms.
  88. void PassRegistry::registerAnalysisGroup(const void *InterfaceID,
  89. const void *PassID,
  90. PassInfo &Registeree, bool isDefault,
  91. bool ShouldFree) {
  92. PassInfo *InterfaceInfo = const_cast<PassInfo *>(getPassInfo(InterfaceID));
  93. if (!InterfaceInfo) {
  94. // First reference to Interface, register it now.
  95. registerPass(Registeree);
  96. InterfaceInfo = &Registeree;
  97. }
  98. assert(Registeree.isAnalysisGroup() &&
  99. "Trying to join an analysis group that is a normal pass!");
  100. if (PassID) {
  101. PassInfo *ImplementationInfo = const_cast<PassInfo *>(getPassInfo(PassID));
  102. assert(ImplementationInfo &&
  103. "Must register pass before adding to AnalysisGroup!");
  104. CheckThreadId(); // sys::SmartScopedReader<true> Guard(Lock); // HLSL Change
  105. // Make sure we keep track of the fact that the implementation implements
  106. // the interface.
  107. ImplementationInfo->addInterfaceImplemented(InterfaceInfo);
  108. if (isDefault) {
  109. assert(InterfaceInfo->getNormalCtor() == nullptr &&
  110. "Default implementation for analysis group already specified!");
  111. assert(
  112. ImplementationInfo->getNormalCtor() &&
  113. "Cannot specify pass as default if it does not have a default ctor");
  114. InterfaceInfo->setNormalCtor(ImplementationInfo->getNormalCtor());
  115. InterfaceInfo->setTargetMachineCtor(
  116. ImplementationInfo->getTargetMachineCtor());
  117. }
  118. }
  119. if (ShouldFree)
  120. ToFree.push_back(std::unique_ptr<const PassInfo>(&Registeree));
  121. }
  122. void PassRegistry::addRegistrationListener(PassRegistrationListener *L) {
  123. CheckThreadId(); // sys::SmartScopedReader<true> Guard(Lock); // HLSL Change
  124. Listeners.push_back(L);
  125. }
  126. void PassRegistry::removeRegistrationListener(PassRegistrationListener *L) {
  127. CheckThreadId(); // sys::SmartScopedReader<true> Guard(Lock); // HLSL Change
  128. auto I = std::find(Listeners.begin(), Listeners.end(), L);
  129. Listeners.erase(I);
  130. }