MLIR 22.0.0git
PassInstrumentation.h
Go to the documentation of this file.
1//===- PassInstrumentation.h ------------------------------------*- C++ -*-===//
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#ifndef MLIR_PASS_PASSINSTRUMENTATION_H_
10#define MLIR_PASS_PASSINSTRUMENTATION_H_
11
12#include "mlir/Support/LLVM.h"
13#include "mlir/Support/TypeID.h"
14#include <optional>
15
16namespace mlir {
17class OperationName;
18class Operation;
19class Pass;
20
21namespace detail {
23} // namespace detail
24
25/// PassInstrumentation provides several entry points into the pass manager
26/// infrastructure. Instrumentations should be added directly to a PassManager
27/// before running a pipeline.
29public:
30 /// This struct represents information related to the parent pass of pipeline.
31 /// It includes information that allows for effectively linking pipelines that
32 /// run on different threads.
34 /// The thread of the parent pass that the current pipeline was spawned
35 /// from. Note: This is acquired from llvm::get_threadid().
37
38 /// The pass that spawned this pipeline.
40 };
41
42 virtual ~PassInstrumentation() = 0;
43
44 /// A callback to run before a pass pipeline is executed. This function takes
45 /// the name of the operation type being operated on, or std::nullopt if the
46 /// pipeline is op-agnostic, and information related to the parent that
47 /// spawned this pipeline.
48 virtual void runBeforePipeline(std::optional<OperationName> name,
49 const PipelineParentInfo &parentInfo);
50
51 /// A callback to run after a pass pipeline has executed. This function takes
52 /// the name of the operation type being operated on, or std::nullopt if the
53 /// pipeline is op-agnostic, and information related to the parent that
54 /// spawned this pipeline.
55 virtual void runAfterPipeline(std::optional<OperationName> name,
56 const PipelineParentInfo &parentInfo);
57
58 /// A callback to run before a pass is executed. This function takes a pointer
59 /// to the pass to be executed, as well as the current operation being
60 /// operated on.
61 virtual void runBeforePass(Pass *pass, Operation *op) {}
62
63 /// A callback to run after a pass is successfully executed. This function
64 /// takes a pointer to the pass to be executed, as well as the current
65 /// operation being operated on.
66 virtual void runAfterPass(Pass *pass, Operation *op) {}
67
68 /// A callback to run when a pass execution fails. This function takes a
69 /// pointer to the pass that was being executed, as well as the current
70 /// operation being operated on. Note that the operation may be in an invalid
71 /// state.
72 virtual void runAfterPassFailed(Pass *pass, Operation *op) {}
73
74 /// A callback to run before an analysis is computed. This function takes the
75 /// name of the analysis to be computed, its TypeID, as well as the
76 /// current operation being analyzed.
77 virtual void runBeforeAnalysis(StringRef name, TypeID id, Operation *op) {}
78
79 /// A callback to run before an analysis is computed. This function takes the
80 /// name of the analysis that was computed, its TypeID, as well as the
81 /// current operation being analyzed.
82 virtual void runAfterAnalysis(StringRef name, TypeID id, Operation *op) {}
83
84 /// Helper method to enable analysis to signal pass failure. Used, for
85 /// example, when pre- or post-conditions fail.
86 void signalPassFailure(Pass *pass);
87};
88
89/// This class holds a collection of PassInstrumentation objects, and invokes
90/// their respective call backs.
92public:
97
98 /// See PassInstrumentation::runBeforePipeline for details.
99 void
100 runBeforePipeline(std::optional<OperationName> name,
102
103 /// See PassInstrumentation::runAfterPipeline for details.
104 void
105 runAfterPipeline(std::optional<OperationName> name,
107
108 /// See PassInstrumentation::runBeforePass for details.
109 void runBeforePass(Pass *pass, Operation *op);
110
111 /// See PassInstrumentation::runAfterPass for details.
112 void runAfterPass(Pass *pass, Operation *op);
113
114 /// See PassInstrumentation::runAfterPassFailed for details.
115 void runAfterPassFailed(Pass *pass, Operation *op);
116
117 /// See PassInstrumentation::runBeforeAnalysis for details.
118 void runBeforeAnalysis(StringRef name, TypeID id, Operation *op);
119
120 /// See PassInstrumentation::runAfterAnalysis for details.
121 void runAfterAnalysis(StringRef name, TypeID id, Operation *op);
122
123 /// Add the given instrumentation to the collection.
124 void addInstrumentation(std::unique_ptr<PassInstrumentation> pi);
125
126private:
127 std::unique_ptr<detail::PassInstrumentorImpl> impl;
128};
129
130} // namespace mlir
131
132namespace llvm {
133template <>
134struct DenseMapInfo<mlir::PassInstrumentation::PipelineParentInfo> {
137
138 static T getEmptyKey() {
139 auto pair = PairInfo::getEmptyKey();
140 return {pair.first, reinterpret_cast<mlir::Pass *>(pair.second)};
141 }
143 auto pair = PairInfo::getTombstoneKey();
144 return {pair.first, reinterpret_cast<mlir::Pass *>(pair.second)};
145 }
146 static unsigned getHashValue(T val) {
147 return PairInfo::getHashValue({val.parentThreadID, val.parentPass});
148 }
149 static bool isEqual(T lhs, T rhs) {
150 return lhs.parentThreadID == rhs.parentThreadID &&
151 lhs.parentPass == rhs.parentPass;
152 }
153};
154} // namespace llvm
155
156#endif // MLIR_PASS_PASSINSTRUMENTATION_H_
lhs
Operation is the basic unit of execution within MLIR.
Definition Operation.h:88
PassInstrumentation provides several entry points into the pass manager infrastructure.
virtual void runAfterPipeline(std::optional< OperationName > name, const PipelineParentInfo &parentInfo)
A callback to run after a pass pipeline has executed.
Definition Pass.cpp:1204
virtual void runBeforeAnalysis(StringRef name, TypeID id, Operation *op)
A callback to run before an analysis is computed.
void signalPassFailure(Pass *pass)
Helper method to enable analysis to signal pass failure.
Definition Pass.cpp:1207
virtual ~PassInstrumentation()=0
virtual void runBeforePass(Pass *pass, Operation *op)
A callback to run before a pass is executed.
virtual void runAfterAnalysis(StringRef name, TypeID id, Operation *op)
A callback to run before an analysis is computed.
virtual void runAfterPass(Pass *pass, Operation *op)
A callback to run after a pass is successfully executed.
virtual void runAfterPassFailed(Pass *pass, Operation *op)
A callback to run when a pass execution fails.
virtual void runBeforePipeline(std::optional< OperationName > name, const PipelineParentInfo &parentInfo)
A callback to run before a pass pipeline is executed.
Definition Pass.cpp:1201
void runAfterPassFailed(Pass *pass, Operation *op)
See PassInstrumentation::runAfterPassFailed for details.
Definition Pass.cpp:1263
void addInstrumentation(std::unique_ptr< PassInstrumentation > pi)
Add the given instrumentation to the collection.
Definition Pass.cpp:1286
PassInstrumentor(const PassInstrumentor &)=delete
void runBeforeAnalysis(StringRef name, TypeID id, Operation *op)
See PassInstrumentation::runBeforeAnalysis for details.
Definition Pass.cpp:1270
void runAfterPass(Pass *pass, Operation *op)
See PassInstrumentation::runAfterPass for details.
Definition Pass.cpp:1256
void runAfterAnalysis(StringRef name, TypeID id, Operation *op)
See PassInstrumentation::runAfterAnalysis for details.
Definition Pass.cpp:1278
void runBeforePass(Pass *pass, Operation *op)
See PassInstrumentation::runBeforePass for details.
Definition Pass.cpp:1249
PassInstrumentor(PassInstrumentor &&)=delete
void runBeforePipeline(std::optional< OperationName > name, const PassInstrumentation::PipelineParentInfo &parentInfo)
See PassInstrumentation::runBeforePipeline for details.
Definition Pass.cpp:1231
void runAfterPipeline(std::optional< OperationName > name, const PassInstrumentation::PipelineParentInfo &parentInfo)
See PassInstrumentation::runAfterPipeline for details.
Definition Pass.cpp:1240
The abstract base pass class.
Definition Pass.h:52
This class provides an efficient unique identifier for a specific C++ type.
Definition TypeID.h:107
The OpAsmOpInterface, see OpAsmInterface.td for more details.
Definition CallGraph.h:229
AttrTypeReplacer.
Include the generated interface declarations.
This struct represents information related to the parent pass of pipeline.
Pass * parentPass
The pass that spawned this pipeline.
uint64_t parentThreadID
The thread of the parent pass that the current pipeline was spawned from.