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 return Ctx->getOptPassGate().shouldRunPass(PassName, IRName);
36}
37
38// Explicit template instantiations and specialization defininitions for core
39// template typedefs.
46template class LLVM_EXPORT_TEMPLATE
48template class LLVM_EXPORT_TEMPLATE
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.
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 =
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>(F))
87 for (const auto &OuterInvalidationPair :
88 OuterProxy->getOuterInvalidations()) {
89 AnalysisKey *OuterAnalysisID = OuterInvalidationPair.first;
90 const auto &InnerAnalysisIDs = OuterInvalidationPair.second;
91 if (Inv.invalidate(OuterAnalysisID, M, PA)) {
92 if (!FunctionPA)
93 FunctionPA = PA;
94 for (AnalysisKey *InnerAnalysisID : InnerAnalysisIDs)
95 FunctionPA->abandon(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(F, *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(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
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
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>(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, F))
145 continue;
146
147 PreservedAnalyses PassPA = Pass->run(F, 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(F, EagerlyInvalidate ? PreservedAnalyses::none() : PassPA);
153
154 PI.runAfterPass(*Pass, F, 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(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>>();
168 return PA;
169}
170
171template <>
173 const Module &IR) {
174 OS << "module \"" << IR.getName() << "\"";
175}
176
177template <>
179 const Function &IR) {
180 OS << "function \"" << IR.getName() << "\"";
181}
182
183AnalysisSetKey CFGAnalyses::SetKey;
184
185AnalysisSetKey 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
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