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