LLVM 22.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/Module.h"
13#include <optional>
14
15using namespace llvm;
16
17namespace llvm {
18// Explicit template instantiations and specialization defininitions for core
19// template typedefs.
26template class LLVM_EXPORT_TEMPLATE
28template class LLVM_EXPORT_TEMPLATE
30
31template <>
32bool FunctionAnalysisManagerModuleProxy::Result::invalidate(
33 Module &M, const PreservedAnalyses &PA,
35 // If literally everything is preserved, we're done.
36 if (PA.areAllPreserved())
37 return false; // This is still a valid proxy.
38
39 // If this proxy isn't marked as preserved, then even if the result remains
40 // valid, the key itself may no longer be valid, so we clear everything.
41 //
42 // Note that in order to preserve this proxy, a module pass must ensure that
43 // the FAM has been completely updated to handle the deletion of functions.
44 // Specifically, any FAM-cached results for those functions need to have been
45 // forcibly cleared. When preserved, this proxy will only invalidate results
46 // cached on functions *still in the module* at the end of the module pass.
48 if (!PAC.preserved() && !PAC.preservedSet<AllAnalysesOn<Module>>()) {
49 InnerAM->clear();
50 return true;
51 }
52
53 // Directly check if the relevant set is preserved.
54 bool AreFunctionAnalysesPreserved =
56
57 // Now walk all the functions to see if any inner analysis invalidation is
58 // necessary.
59 for (Function &F : M) {
60 std::optional<PreservedAnalyses> FunctionPA;
61
62 // Check to see whether the preserved set needs to be pruned based on
63 // module-level analysis invalidation that triggers deferred invalidation
64 // registered with the outer analysis manager proxy for this function.
65 if (auto *OuterProxy =
66 InnerAM->getCachedResult<ModuleAnalysisManagerFunctionProxy>(F))
67 for (const auto &OuterInvalidationPair :
68 OuterProxy->getOuterInvalidations()) {
69 AnalysisKey *OuterAnalysisID = OuterInvalidationPair.first;
70 const auto &InnerAnalysisIDs = OuterInvalidationPair.second;
71 if (Inv.invalidate(OuterAnalysisID, M, PA)) {
72 if (!FunctionPA)
73 FunctionPA = PA;
74 for (AnalysisKey *InnerAnalysisID : InnerAnalysisIDs)
75 FunctionPA->abandon(InnerAnalysisID);
76 }
77 }
78
79 // Check if we needed a custom PA set, and if so we'll need to run the
80 // inner invalidation.
81 if (FunctionPA) {
82 InnerAM->invalidate(F, *FunctionPA);
83 continue;
84 }
85
86 // Otherwise we only need to do invalidation if the original PA set didn't
87 // preserve all function analyses.
88 if (!AreFunctionAnalysesPreserved)
89 InnerAM->invalidate(F, PA);
90 }
91
92 // Return false to indicate that this result is still a valid proxy.
93 return false;
94}
95} // namespace llvm
96
98 raw_ostream &OS, function_ref<StringRef(StringRef)> MapClassName2PassName) {
99 OS << "function";
100 if (EagerlyInvalidate)
101 OS << "<eager-inv>";
102 OS << '(';
103 Pass->printPipeline(OS, MapClassName2PassName);
104 OS << ')';
105}
106
111
112 // Request PassInstrumentation from analysis manager, will use it to run
113 // instrumenting callbacks for the passes later.
115
117 for (Function &F : M) {
118 if (F.isDeclaration())
119 continue;
120
121 // Check the PassInstrumentation's BeforePass callbacks before running the
122 // pass, skip its execution completely if asked to (callback returns
123 // false).
124 if (!PI.runBeforePass<Function>(*Pass, F))
125 continue;
126
127 PreservedAnalyses PassPA = Pass->run(F, FAM);
128
129 // We know that the function pass couldn't have invalidated any other
130 // function's analyses (that's the contract of a function pass), so
131 // directly handle the function analysis manager's invalidation here.
132 FAM.invalidate(F, EagerlyInvalidate ? PreservedAnalyses::none() : PassPA);
133
134 PI.runAfterPass(*Pass, F, PassPA);
135
136 // Then intersect the preserved set so that invalidation of module
137 // analyses will eventually occur when the module pass completes.
138 PA.intersect(std::move(PassPA));
139 }
140
141 // The FunctionAnalysisManagerModuleProxy is preserved because (we assume)
142 // the function passes we ran didn't add or remove any functions.
143 //
144 // We also preserve all analyses on Functions, because we did all the
145 // invalidation we needed to do above.
146 PA.preserveSet<AllAnalysesOn<Function>>();
148 return PA;
149}
150
151template <>
153 const Module &IR) {
154 OS << "module \"" << IR.getName() << "\"";
155}
156
157template <>
159 const Function &IR) {
160 OS << "function \"" << IR.getName() << "\"";
161}
162
163AnalysisSetKey CFGAnalyses::SetKey;
164
165AnalysisSetKey PreservedAnalyses::AllAnalysesKey;
#define LLVM_EXPORT_TEMPLATE
Definition: Compiler.h:215
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:80
#define F(x, y, z)
Definition: MD5.cpp:55
FunctionAnalysisManager FAM
Provides implementations for PassManager and AnalysisManager template methods.
raw_pwrite_stream & OS
This templated class represents "all analyses that operate over <a particular IR unit>" (e....
Definition: Analysis.h:50
API to communicate dependencies between analyses during invalidation.
Definition: PassManager.h:294
bool invalidate(IRUnitT &IR, const PreservedAnalyses &PA)
Trigger the invalidation of some other analysis pass if not already handled and return whether it was...
Definition: PassManager.h:312
A container for analyses that lazily runs them and caches their results.
Definition: PassManager.h:255
PassT::Result & getResult(IRUnitT &IR, ExtraArgTs... ExtraArgs)
Get the result of an analysis pass for a given IR unit.
Definition: PassManager.h:412
An analysis over an "outer" IR unit that provides access to an analysis manager over an "inner" IR un...
Definition: PassManager.h:585
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)
Definition: PassManager.cpp:97
A Module instance is used to store all the information related to an LLVM module.
Definition: Module.h:67
An analysis over an "inner" IR unit that provides access to an analysis manager over a "outer" IR uni...
Definition: PassManager.h:716
Pseudo-analysis pass that exposes the PassInstrumentation to pass managers.
This class provides instrumentation entry points for the Pass Manager, doing calls to callbacks regis...
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.
Definition: PassManager.h:163
Pass interface - Implemented by all 'passes'.
Definition: Pass.h:99
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
PreservedAnalyses & abandon()
Mark an analysis as abandoned.
Definition: Analysis.h:171
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
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:55
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
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
LLVM_ABI void printIRUnitNameForStackTrace< Function >(raw_ostream &OS, const Function &IR)
LLVM_ABI void printIRUnitNameForStackTrace< Module >(raw_ostream &OS, const Module &IR)
A special type used by analysis passes to provide an address that identifies that particular analysis...
Definition: Analysis.h:29
A special type used to provide an address that identifies a set of related analyses.
Definition: Analysis.h:39