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