MLIR 24.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 /// Whether callbacks may run concurrently on different threads. By default,
45 /// the pass instrumentor serializes callbacks. Opting in requires all shared
46 /// state to support concurrent use.
47 /// Callbacks on a single thread still run in registration order before a
48 /// pipeline, pass, or analysis, and in reverse order afterwards.
49 virtual bool isThreadSafe() const { return false; }
50
51 /// A callback to run before a pass pipeline is 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 runBeforePipeline(std::optional<OperationName> name,
56 const PipelineParentInfo &parentInfo);
57
58 /// A callback to run after a pass pipeline has executed. This function takes
59 /// the name of the operation type being operated on, or std::nullopt if the
60 /// pipeline is op-agnostic, and information related to the parent that
61 /// spawned this pipeline.
62 virtual void runAfterPipeline(std::optional<OperationName> name,
63 const PipelineParentInfo &parentInfo);
64
65 /// A callback to run before a pass is executed. This function takes a pointer
66 /// to the pass to be executed, as well as the current operation being
67 /// operated on.
68 virtual void runBeforePass(Pass *pass, Operation *op) {}
69
70 /// A callback to run after a pass is successfully executed. This function
71 /// takes a pointer to the pass to be executed, as well as the current
72 /// operation being operated on.
73 virtual void runAfterPass(Pass *pass, Operation *op) {}
74
75 /// A callback to run when a pass execution fails. This function takes a
76 /// pointer to the pass that was being executed, as well as the current
77 /// operation being operated on. Note that the operation may be in an invalid
78 /// state.
79 virtual void runAfterPassFailed(Pass *pass, Operation *op) {}
80
81 /// A callback to run before an analysis is computed. This function takes the
82 /// name of the analysis to be computed, its TypeID, as well as the
83 /// current operation being analyzed.
84 virtual void runBeforeAnalysis(StringRef name, TypeID id, Operation *op) {}
85
86 /// A callback to run before an analysis is computed. This function takes the
87 /// name of the analysis that was computed, its TypeID, as well as the
88 /// current operation being analyzed.
89 virtual void runAfterAnalysis(StringRef name, TypeID id, Operation *op) {}
90
91 /// Helper method to enable analysis to signal pass failure. Used, for
92 /// example, when pre- or post-conditions fail.
93 void signalPassFailure(Pass *pass);
94};
95
96/// This class holds a collection of PassInstrumentation objects, and invokes
97/// their respective call backs.
99public:
104
105 /// See PassInstrumentation::runBeforePipeline for details.
106 void
107 runBeforePipeline(std::optional<OperationName> name,
109
110 /// See PassInstrumentation::runAfterPipeline for details.
111 void
112 runAfterPipeline(std::optional<OperationName> name,
114
115 /// See PassInstrumentation::runBeforePass for details.
116 void runBeforePass(Pass *pass, Operation *op);
117
118 /// See PassInstrumentation::runAfterPass for details.
119 void runAfterPass(Pass *pass, Operation *op);
120
121 /// See PassInstrumentation::runAfterPassFailed for details.
122 void runAfterPassFailed(Pass *pass, Operation *op);
123
124 /// See PassInstrumentation::runBeforeAnalysis for details.
125 void runBeforeAnalysis(StringRef name, TypeID id, Operation *op);
126
127 /// See PassInstrumentation::runAfterAnalysis for details.
128 void runAfterAnalysis(StringRef name, TypeID id, Operation *op);
129
130 /// Add the given instrumentation to the collection.
131 void addInstrumentation(std::unique_ptr<PassInstrumentation> pi);
132
133private:
134 std::unique_ptr<detail::PassInstrumentorImpl> impl;
135};
136
137} // namespace mlir
138
139namespace llvm {
140template <>
141struct DenseMapInfo<mlir::PassInstrumentation::PipelineParentInfo> {
144
145 static unsigned getHashValue(T val) {
146 return PairInfo::getHashValue({val.parentThreadID, val.parentPass});
147 }
148 static bool isEqual(T lhs, T rhs) {
149 return lhs.parentThreadID == rhs.parentThreadID &&
150 lhs.parentPass == rhs.parentPass;
151 }
152};
153} // namespace llvm
154
155#endif // MLIR_PASS_PASSINSTRUMENTATION_H_
Operation is the basic unit of execution within MLIR.
Definition Operation.h:87
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:1203
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:1206
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 bool isThreadSafe() const
Whether callbacks may run concurrently on different threads.
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:1200
void runAfterPassFailed(Pass *pass, Operation *op)
See PassInstrumentation::runAfterPassFailed for details.
Definition Pass.cpp:1273
void addInstrumentation(std::unique_ptr< PassInstrumentation > pi)
Add the given instrumentation to the collection.
Definition Pass.cpp:1296
PassInstrumentor(const PassInstrumentor &)=delete
void runBeforeAnalysis(StringRef name, TypeID id, Operation *op)
See PassInstrumentation::runBeforeAnalysis for details.
Definition Pass.cpp:1280
void runAfterPass(Pass *pass, Operation *op)
See PassInstrumentation::runAfterPass for details.
Definition Pass.cpp:1266
void runAfterAnalysis(StringRef name, TypeID id, Operation *op)
See PassInstrumentation::runAfterAnalysis for details.
Definition Pass.cpp:1288
void runBeforePass(Pass *pass, Operation *op)
See PassInstrumentation::runBeforePass for details.
Definition Pass.cpp:1259
PassInstrumentor(PassInstrumentor &&)=delete
void runBeforePipeline(std::optional< OperationName > name, const PassInstrumentation::PipelineParentInfo &parentInfo)
See PassInstrumentation::runBeforePipeline for details.
Definition Pass.cpp:1241
void runAfterPipeline(std::optional< OperationName > name, const PassInstrumentation::PipelineParentInfo &parentInfo)
See PassInstrumentation::runAfterPipeline for details.
Definition Pass.cpp:1250
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:227
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.