| 1 | //===- PassManager.cpp - Infrastructure for managing & running IR passes --===// |
| 2 | // |
| 3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | // See https://llvm.org/LICENSE.txt for license information. |
| 5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | |
| 9 | #include "llvm/IR/PassManager.h" |
| 10 | #include "llvm/IR/Module.h" |
| 11 | #include "llvm/IR/PassManagerImpl.h" |
| 12 | #include "llvm/Support/Compiler.h" |
| 13 | #include <optional> |
| 14 | |
| 15 | using namespace llvm; |
| 16 | |
| 17 | namespace llvm { |
| 18 | // Explicit template instantiations and specialization defininitions for core |
| 19 | // template typedefs. |
| 20 | template class LLVM_EXPORT_TEMPLATE AllAnalysesOn<Module>; |
| 21 | template class LLVM_EXPORT_TEMPLATE AllAnalysesOn<Function>; |
| 22 | template class LLVM_EXPORT_TEMPLATE PassManager<Module>; |
| 23 | template class LLVM_EXPORT_TEMPLATE PassManager<Function>; |
| 24 | template class LLVM_EXPORT_TEMPLATE AnalysisManager<Module>; |
| 25 | template class LLVM_EXPORT_TEMPLATE AnalysisManager<Function>; |
| 26 | template class LLVM_EXPORT_TEMPLATE |
| 27 | InnerAnalysisManagerProxy<FunctionAnalysisManager, Module>; |
| 28 | template class LLVM_EXPORT_TEMPLATE |
| 29 | OuterAnalysisManagerProxy<ModuleAnalysisManager, Function>; |
| 30 | |
| 31 | template <> |
| 32 | bool FunctionAnalysisManagerModuleProxy::Result::invalidate( |
| 33 | Module &M, const PreservedAnalyses &PA, |
| 34 | ModuleAnalysisManager::Invalidator &Inv) { |
| 35 | // If literally everything is preserved, we're done. |
| 36 | if (PA.areAllPreserved()) |
| 37 | return false; // This is still a valid proxy. |
| 38 | |
| 39 | // If this proxy isn't marked as preserved, then even if the result remains |
| 40 | // valid, the key itself may no longer be valid, so we clear everything. |
| 41 | // |
| 42 | // Note that in order to preserve this proxy, a module pass must ensure that |
| 43 | // the FAM has been completely updated to handle the deletion of functions. |
| 44 | // Specifically, any FAM-cached results for those functions need to have been |
| 45 | // forcibly cleared. When preserved, this proxy will only invalidate results |
| 46 | // cached on functions *still in the module* at the end of the module pass. |
| 47 | auto PAC = PA.getChecker<FunctionAnalysisManagerModuleProxy>(); |
| 48 | if (!PAC.preserved() && !PAC.preservedSet<AllAnalysesOn<Module>>()) { |
| 49 | InnerAM->clear(); |
| 50 | return true; |
| 51 | } |
| 52 | |
| 53 | // Directly check if the relevant set is preserved. |
| 54 | bool AreFunctionAnalysesPreserved = |
| 55 | PA.allAnalysesInSetPreserved<AllAnalysesOn<Function>>(); |
| 56 | |
| 57 | // Now walk all the functions to see if any inner analysis invalidation is |
| 58 | // necessary. |
| 59 | for (Function &F : M) { |
| 60 | std::optional<PreservedAnalyses> FunctionPA; |
| 61 | |
| 62 | // Check to see whether the preserved set needs to be pruned based on |
| 63 | // module-level analysis invalidation that triggers deferred invalidation |
| 64 | // registered with the outer analysis manager proxy for this function. |
| 65 | if (auto *OuterProxy = |
| 66 | InnerAM->getCachedResult<ModuleAnalysisManagerFunctionProxy>(IR&: F)) |
| 67 | for (const auto &OuterInvalidationPair : |
| 68 | OuterProxy->getOuterInvalidations()) { |
| 69 | AnalysisKey *OuterAnalysisID = OuterInvalidationPair.first; |
| 70 | const auto &InnerAnalysisIDs = OuterInvalidationPair.second; |
| 71 | if (Inv.invalidate(ID: OuterAnalysisID, IR&: M, PA)) { |
| 72 | if (!FunctionPA) |
| 73 | FunctionPA = PA; |
| 74 | for (AnalysisKey *InnerAnalysisID : InnerAnalysisIDs) |
| 75 | FunctionPA->abandon(ID: InnerAnalysisID); |
| 76 | } |
| 77 | } |
| 78 | |
| 79 | // Check if we needed a custom PA set, and if so we'll need to run the |
| 80 | // inner invalidation. |
| 81 | if (FunctionPA) { |
| 82 | InnerAM->invalidate(IR&: F, PA: *FunctionPA); |
| 83 | continue; |
| 84 | } |
| 85 | |
| 86 | // Otherwise we only need to do invalidation if the original PA set didn't |
| 87 | // preserve all function analyses. |
| 88 | if (!AreFunctionAnalysesPreserved) |
| 89 | InnerAM->invalidate(IR&: F, PA); |
| 90 | } |
| 91 | |
| 92 | // Return false to indicate that this result is still a valid proxy. |
| 93 | return false; |
| 94 | } |
| 95 | } // namespace llvm |
| 96 | |
| 97 | void ModuleToFunctionPassAdaptor::printPipeline( |
| 98 | raw_ostream &OS, function_ref<StringRef(StringRef)> MapClassName2PassName) { |
| 99 | OS << "function" ; |
| 100 | if (EagerlyInvalidate) |
| 101 | OS << "<eager-inv>" ; |
| 102 | OS << '('; |
| 103 | Pass->printPipeline(OS, MapClassName2PassName); |
| 104 | OS << ')'; |
| 105 | } |
| 106 | |
| 107 | PreservedAnalyses ModuleToFunctionPassAdaptor::run(Module &M, |
| 108 | ModuleAnalysisManager &AM) { |
| 109 | FunctionAnalysisManager &FAM = |
| 110 | AM.getResult<FunctionAnalysisManagerModuleProxy>(IR&: M).getManager(); |
| 111 | |
| 112 | // Request PassInstrumentation from analysis manager, will use it to run |
| 113 | // instrumenting callbacks for the passes later. |
| 114 | PassInstrumentation PI = AM.getResult<PassInstrumentationAnalysis>(IR&: M); |
| 115 | |
| 116 | PreservedAnalyses PA = PreservedAnalyses::all(); |
| 117 | for (Function &F : M) { |
| 118 | if (F.isDeclaration()) |
| 119 | continue; |
| 120 | |
| 121 | // Check the PassInstrumentation's BeforePass callbacks before running the |
| 122 | // pass, skip its execution completely if asked to (callback returns |
| 123 | // false). |
| 124 | if (!PI.runBeforePass<Function>(Pass: *Pass, IR: F)) |
| 125 | continue; |
| 126 | |
| 127 | PreservedAnalyses PassPA = Pass->run(IR&: F, AM&: FAM); |
| 128 | |
| 129 | // We know that the function pass couldn't have invalidated any other |
| 130 | // function's analyses (that's the contract of a function pass), so |
| 131 | // directly handle the function analysis manager's invalidation here. |
| 132 | FAM.invalidate(IR&: F, PA: EagerlyInvalidate ? PreservedAnalyses::none() : PassPA); |
| 133 | |
| 134 | PI.runAfterPass(Pass: *Pass, IR: F, PA: PassPA); |
| 135 | |
| 136 | // Then intersect the preserved set so that invalidation of module |
| 137 | // analyses will eventually occur when the module pass completes. |
| 138 | PA.intersect(Arg: std::move(PassPA)); |
| 139 | } |
| 140 | |
| 141 | // The FunctionAnalysisManagerModuleProxy is preserved because (we assume) |
| 142 | // the function passes we ran didn't add or remove any functions. |
| 143 | // |
| 144 | // We also preserve all analyses on Functions, because we did all the |
| 145 | // invalidation we needed to do above. |
| 146 | PA.preserveSet<AllAnalysesOn<Function>>(); |
| 147 | PA.preserve<FunctionAnalysisManagerModuleProxy>(); |
| 148 | return PA; |
| 149 | } |
| 150 | |
| 151 | template <> |
| 152 | void llvm::printIRUnitNameForStackTrace<Module>(raw_ostream &OS, |
| 153 | const Module &IR) { |
| 154 | OS << "module \"" << IR.getName() << "\"" ; |
| 155 | } |
| 156 | |
| 157 | template <> |
| 158 | void llvm::printIRUnitNameForStackTrace<Function>(raw_ostream &OS, |
| 159 | const Function &IR) { |
| 160 | OS << "function \"" << IR.getName() << "\"" ; |
| 161 | } |
| 162 | |
| 163 | AnalysisSetKey CFGAnalyses::SetKey; |
| 164 | |
| 165 | AnalysisSetKey PreservedAnalyses::AllAnalysesKey; |
| 166 | |