LLVM 24.0.0git
PassManager.cpp
Go to the documentation of this file.
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
10#include "llvm/IR/LLVMContext.h"
11#include "llvm/IR/Module.h"
12#include "llvm/IR/OptBisect.h"
16#include <optional>
17
18using namespace llvm;
19
20namespace llvm {
21
24 LLVMContext *Ctx = nullptr;
25 std::string IRName = "";
26 if (const auto *M = dyn_cast<Module>(IR)) {
27 Ctx = &M->getContext();
28 IRName = "[module]";
29 } else if (const auto *F = dyn_cast<Function>(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, IRName);
37}
38
39// Explicit template instantiations and specialization defininitions for core
40// template typedefs.
47template class LLVM_EXPORT_TEMPLATE
49template class LLVM_EXPORT_TEMPLATE
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.
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 =
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>(F))
88 for (const auto &OuterInvalidationPair :
89 OuterProxy->getOuterInvalidations()) {
90 AnalysisKey *OuterAnalysisID = OuterInvalidationPair.first;
91 const auto &InnerAnalysisIDs = OuterInvalidationPair.second;
92 if (Inv.invalidate(OuterAnalysisID, M, PA)) {
93 if (!FunctionPA)
94 FunctionPA = PA;
95 for (AnalysisKey *InnerAnalysisID : InnerAnalysisIDs)
96 FunctionPA->abandon(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(F, *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(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
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
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>(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, F))
146 continue;
147
148 PreservedAnalyses PassPA = Pass->run(F, 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(F, EagerlyInvalidate ? PreservedAnalyses::none() : PassPA);
154
155 PI.runAfterPass(*Pass, F, 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(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>>();
169 return PA;
170}
171
172template <>
174 const Module &IR) {
175 OS << "module \"" << IR.getName() << "\"";
176}
177
178template <>
180 const Function &IR) {
181 OS << "function \"" << IR.getName() << "\"";
182}
183
184AnalysisSetKey CFGAnalyses::SetKey;
185
186AnalysisSetKey PreservedAnalyses::AllAnalysesKey;
#define LLVM_EXPORT_TEMPLATE
Definition Compiler.h:217
Module.h This file contains the declarations for the Module class.
This header defines various interfaces for pass management in LLVM.
Legalize the Machine IR a function s Machine IR
Definition Legalizer.cpp:85
#define F(x, y, z)
Definition MD5.cpp:54
This file declares the interface for bisecting optimizations.
FunctionAnalysisManager FAM
Provides implementations for PassManager and AnalysisManager template methods.
static const char PassName[]
This templated class represents "all analyses that operate over <aparticular IR unit>" (e....
Definition Analysis.h:50
A container for analyses that lazily runs them and caches their results.
PassT::Result & getResult(IRUnitT &IR, ExtraArgTs... ExtraArgs)
Get the result of an analysis pass for a given IR unit.
A type-erased reference to the IR unit a pass or analysis is running on, together with the kind of IR...
Definition IRUnitRef.h:60
An analysis over an "outer" IR unit that provides access to an analysis manager over an "inner" IR un...
This is an important class for using LLVM in a threaded context.
Definition LLVMContext.h:68
LLVM_ABI OptPassGate & getOptPassGate() const
Access the object which can disable optional passes and individual optimizations at compile time.
LLVM_ABI PreservedAnalyses run(Module &M, ModuleAnalysisManager &AM)
Runs the function pass across every function in the module.
LLVM_ABI void printPipeline(raw_ostream &OS, function_ref< StringRef(StringRef)> MapClassName2PassName)
A Module instance is used to store all the information related to an LLVM module.
Definition Module.h:68
Extensions to this class implement mechanisms to disable passes and individual optimizations at compi...
Definition OptBisect.h:26
virtual bool isEnabled() const
isEnabled() should return true before calling shouldRunPass().
Definition OptBisect.h:38
virtual bool shouldRunPass(StringRef PassName, StringRef IRDescription) const
IRDescription is a textual description of the IR unit the pass is running over.
Definition OptBisect.h:32
An analysis over an "inner" IR unit that provides access to an analysis manager over a "outer" IR uni...
void runAfterPass(const PassT &Pass, const IRUnitT &IR, const PreservedAnalyses &PA) const
AfterPass instrumentation point - takes Pass instance that has just been executed and constant refere...
bool runBeforePass(const PassT &Pass, const IRUnitT &IR) const
BeforePass instrumentation point - takes Pass instance to be executed and constant reference to IR it...
Manages a sequence of passes over a particular unit of IR.
A set of analyses that are preserved following a run of a transformation pass.
Definition Analysis.h:112
static PreservedAnalyses none()
Convenience factory function for the empty preserved set.
Definition Analysis.h:115
bool areAllPreserved() const
Test whether all analyses are preserved (and none are abandoned).
Definition Analysis.h:292
static PreservedAnalyses all()
Construct a special preserved set that preserves all passes.
Definition Analysis.h:118
bool allAnalysesInSetPreserved() const
Directly test whether a set of analyses is preserved.
Definition Analysis.h:300
PreservedAnalysisChecker getChecker() const
Build a checker for this PreservedAnalyses and the specified analysis type.
Definition Analysis.h:275
Represent a constant reference to a string, i.e.
Definition StringRef.h:56
An efficient, type-erasing, non-owning reference to a callable.
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition raw_ostream.h:53
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
LLVM_ABI bool shouldSkipOptimizationForOptBisect(IRUnitRef IR, StringRef PassName)
This is an optimization pass for GlobalISel generic memory operations.
decltype(auto) dyn_cast(const From &Val)
dyn_cast<X> - Return the argument parameter cast to the specified type.
Definition Casting.h:643
OuterAnalysisManagerProxy< ModuleAnalysisManager, Function > ModuleAnalysisManagerFunctionProxy
Provide the ModuleAnalysisManager to Function proxy.
LLVM_ABI void printIRUnitNameForStackTrace< Function >(raw_ostream &OS, const Function &IR)
InnerAnalysisManagerProxy< FunctionAnalysisManager, Module > FunctionAnalysisManagerModuleProxy
Provide the FunctionAnalysisManager to Module proxy.
LLVM_ABI void printIRUnitNameForStackTrace< Module >(raw_ostream &OS, const Module &IR)
template class LLVM_TEMPLATE_ABI AllAnalysesOn< Module >
template class LLVM_TEMPLATE_ABI AllAnalysesOn< Function >
AnalysisManager< Function > FunctionAnalysisManager
Convenience typedef for the Function analysis manager.
AnalysisManager< Module > ModuleAnalysisManager
Convenience typedef for the Module analysis manager.
Definition MIRParser.h:39
A special type used to provide an address that identifies a set of related analyses.
Definition Analysis.h:39