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