9 #ifndef MLIR_PASS_PASS_H
10 #define MLIR_PASS_PASS_H
15 #include "llvm/ADT/PointerIntPair.h"
16 #include "llvm/ADT/Statistic.h"
21 class OpToOpPassAdaptor;
22 struct OpPassManagerImpl;
83 std::optional<StringRef>
getOpName()
const {
return opName; }
90 template <
typename DataType,
93 template <
typename... Args>
95 : detail::PassOptions::
Option<DataType, OptionParser>(
96 parent.passOptions, arg, std::forward<Args>(args)...) {}
101 template <
typename DataType,
105 template <
typename... Args>
107 : detail::PassOptions::
ListOption<DataType, OptionParser>(
108 parent.passOptions, arg, std::forward<Args>(args)...) {}
116 virtual LogicalResult
118 function_ref<LogicalResult(
const Twine &)> errorHandler);
136 Statistic(
Pass *owner,
const char *name,
const char *description);
158 return threadingSibling ? threadingSibling :
this;
162 explicit Pass(
TypeID passID, std::optional<StringRef> opName = std::nullopt)
163 : passID(passID), opName(opName) {}
171 assert(passState &&
"pass state was never initialized");
200 return passState->pipelineExecutor(pipeline, op);
204 std::unique_ptr<Pass>
clone()
const {
206 newInst->copyOptionValuesFrom(
this);
220 template <
typename AnalysisT>
227 template <
typename AnalysisT,
typename OpT>
234 template <
typename AnalysisT>
245 template <
typename... AnalysesT>
254 template <
typename AnalysisT>
255 std::optional<std::reference_wrapper<AnalysisT>>
261 template <
typename AnalysisT>
268 template <
typename AnalysisT>
269 std::optional<std::reference_wrapper<AnalysisT>>
276 template <
typename AnalysisT>
283 template <
typename AnalysisT,
typename OpTy>
303 virtual void anchor();
310 std::optional<StringRef> opName;
313 std::optional<detail::PassExecutionState> passState;
316 std::vector<Statistic *> statistics;
323 const Pass *threadingSibling =
nullptr;
354 template <
typename OpT =
void>
368 return pass->
getOpName() == OpT::getOperationName();
373 return opName.getStringRef() ==
getOpName();
381 template <
typename AnalysisT>
383 return Pass::getAnalysis<AnalysisT, OpT>();
430 template <
typename InterfaceT>
439 return opName.hasInterface<InterfaceT>();
446 template <
typename AnalysisT>
448 return Pass::getAnalysis<AnalysisT, InterfaceT>();
456 template <
typename PassT,
typename BaseT>
461 return pass->
getTypeID() == TypeID::get<PassT>();
473 StringRef
getName()
const override {
return llvm::getTypeName<PassT>(); }
477 return std::make_unique<PassT>(*
static_cast<const PassT *
>(
this));
503 static constexpr StringLiteral
tag =
"pass-execution";
506 void print(raw_ostream &os)
const override;
static llvm::ManagedStatic< PassManagerOptions > options
#define MLIR_DEFINE_EXPLICIT_INTERNAL_INLINE_TYPE_ID(CLASS_NAME)
This class represents an analysis manager for a particular operation instance.
std::optional< std::reference_wrapper< AnalysisT > > getCachedChildAnalysis(Operation *op) const
Query for a cached analysis of a child operation, or return null.
std::optional< std::reference_wrapper< AnalysisT > > getCachedAnalysis() const
Query for a cached entry of the given analysis on the current operation.
AnalysisT & getAnalysis()
Query for the given analysis for the current operation.
std::optional< std::reference_wrapper< AnalysisT > > getCachedParentAnalysis(Operation *parentOp) const
Query for a cached analysis on the given parent operation.
AnalysisT & getChildAnalysis(Operation *op)
Query for an analysis of a child operation, constructing it if necessary.
The DialectRegistry maps a dialect namespace to a constructor for the matching dialect.
Pass to transform an operation that implements the given interface.
AnalysisT & getAnalysis()
Query an analysis for the current operation.
bool canScheduleOn(RegisteredOperationName opName) const final
Indicate if the current pass can be scheduled on the given operation type.
InterfaceT getOperation()
Return the current operation being transformed.
MLIRContext is the top-level object for a collection of MLIR operations.
This class represents a pass manager that runs passes on either a specific operation type,...
OperationPass & operator=(const OperationPass &)=delete
OperationPass(TypeID passID)
OperationPass(const OperationPass &)=default
OperationPass(OperationPass &&)=delete
~OperationPass() override=default
bool canScheduleOn(RegisteredOperationName opName) const override
Indicate if the current pass can be scheduled on the given operation type.
OperationPass & operator=(OperationPass &&)=delete
Pass to transform an operation of a specific type.
OperationPass & operator=(OperationPass &&)=delete
OperationPass(TypeID passID)
static bool classof(const Pass *pass)
Support isa/dyn_cast functionality.
AnalysisT & getAnalysis()
Query an analysis for the current operation of the specific derived operation type.
OperationPass(const OperationPass &)=default
OperationPass & operator=(const OperationPass &)=delete
OpT getOperation()
Return the current operation being transformed.
bool canScheduleOn(RegisteredOperationName opName) const final
Indicate if the current pass can be scheduled on the given operation type.
OperationPass(OperationPass &&)=delete
~OperationPass() override=default
Operation is the basic unit of execution within MLIR.
MLIRContext * getContext()
Return the context this operation is associated with.
Operation * getParentOp()
Returns the closest surrounding operation that contains this operation or nullptr if this is a top-le...
This class encapsulates the "action" of executing a single pass.
void print(raw_ostream &os) const override
Print a textual version of this action to os.
const Pass & getPass() const
Get the pass that will be executed by this action.
PassExecutionAction(ArrayRef< IRUnit > irUnits, const Pass &pass)
Define a TypeID for this PassExecutionAction.
Operation * op
The base op for this pass.
Operation * getOp() const
Get the operation that is the base of this pass.
static constexpr StringLiteral tag
The tag required by ActionImpl to identify this action.
const Pass & pass
Reference to the pass being run.
A structure to represent the information for a derived pass class.
static const PassInfo * lookup(StringRef passArg)
Returns the pass info for the specified pass class or null if unknown.
This class provides a CRTP wrapper around a base pass class to define several necessary utility metho...
PassWrapper(const PassWrapper &)=default
PassWrapper & operator=(PassWrapper &&)=delete
std::unique_ptr< Pass > clonePass() const override
A clone method to create a copy of this pass.
StringRef getName() const override
Returns the derived pass name.
PassWrapper & operator=(const PassWrapper &)=delete
~PassWrapper() override=default
PassWrapper(PassWrapper &&)=delete
static bool classof(const Pass *pass)
Support isa/dyn_cast functionality for the derived pass class.
This class represents a single pass statistic.
Statistic(Pass *owner, const char *name, const char *description)
The statistic is initialized by the pass owner, a name, and a description.
Statistic & operator=(unsigned value)
Assign the statistic to the given value.
The abstract base pass class.
void copyOptionValuesFrom(const Pass *other)
Copy the option values from 'other', which is another instance of this pass.
void printAsTextualPipeline(raw_ostream &os)
Prints out the pass in the textual representation of pipelines.
virtual LogicalResult initializeOptions(StringRef options, function_ref< LogicalResult(const Twine &)> errorHandler)
Attempt to initialize the options of this pass from the given string.
AnalysisManager getAnalysisManager()
Returns the current analysis manager.
MutableArrayRef< Statistic * > getStatistics()
AnalysisT & getChildAnalysis(OpTy child)
Returns the analysis for the given child operation of specific derived operation type,...
virtual std::unique_ptr< Pass > clonePass() const =0
Create a copy of this pass, ignoring statistics and options.
void markAllAnalysesPreserved()
Mark all analyses as preserved.
virtual bool canScheduleOn(RegisteredOperationName opName) const =0
Indicate if the current pass can be scheduled on the given operation type.
TypeID getTypeID() const
Returns the unique identifier that corresponds to this pass.
std::optional< std::reference_wrapper< AnalysisT > > getCachedChildAnalysis(Operation *child)
Returns the analysis for the given child operation if it exists.
Pass(TypeID passID, std::optional< StringRef > opName=std::nullopt)
virtual StringRef getDescription() const
Return the command line description used when registering this pass.
std::optional< std::reference_wrapper< AnalysisT > > getCachedAnalysis()
Query a cached instance of an analysis for the current ir unit if one exists.
AnalysisT & getAnalysis()
Query an analysis for the current ir unit of a specific derived operation type.
std::optional< std::reference_wrapper< AnalysisT > > getCachedParentAnalysis()
Returns the analysis for the parent operation if it exists.
LogicalResult runPipeline(OpPassManager &pipeline, Operation *op)
Schedule an arbitrary pass pipeline on the provided operation.
std::optional< StringRef > getOpName() const
Returns the name of the operation that this pass operates on, or std::nullopt if this is a generic Op...
virtual void runOnOperation()=0
The polymorphic API that runs the pass over the currently held operation.
ArrayRef< Statistic * > getStatistics() const
Returns the main statistics for this pass instance.
std::unique_ptr< Pass > clone() const
A clone method to create a copy of this pass.
detail::PassExecutionState & getPassState()
Returns the current pass state.
const Pass * getThreadingSiblingOrThis() const
Returns the thread sibling of this pass, or the pass itself it has no sibling.
const Pass * getThreadingSibling() const
Returns the thread sibling of this pass.
AnalysisT & getChildAnalysis(Operation *child)
Returns the analysis for the given child operation, or creates it if it doesn't exist.
virtual LogicalResult initialize(MLIRContext *context)
Initialize any complex state necessary for running this pass.
virtual void getDependentDialects(DialectRegistry ®istry) const
Register dependent dialects for the current pass.
void markAnalysesPreserved(TypeID id)
Pass & operator=(Pass &&)=delete
virtual StringRef getName() const =0
Returns the derived pass name.
void markAnalysesPreserved()
Mark the provided analyses as preserved.
Operation * getOperation()
Return the current operation being transformed.
void signalPassFailure()
Signal that some invariant was broken when running.
std::optional< std::reference_wrapper< AnalysisT > > getCachedParentAnalysis(Operation *parent)
Returns the analysis for the given parent operation if it exists.
Pass & operator=(const Pass &)=delete
const PassInfo * lookupPassInfo() const
Returns the pass info for this pass, or null if unknown.
virtual StringRef getArgument() const
Return the command line argument used when registering this pass.
MLIRContext & getContext()
Return the MLIR context for the current operation being transformed.
AnalysisT & getAnalysis()
Query an analysis for the current ir unit.
This is a "type erased" representation of a registered operation.
This class provides an efficient unique identifier for a specific C++ type.
An adaptor pass used to run operation passes over nested operations.
This class represents a specific pass option that contains a list of values of the provided data type...
This class represents a specific pass option, with a provided data type.
Base container class and manager for all pass options.
std::conditional_t< std::is_base_of_v< PassOptions, DataType >, PassOptionsParser< DataType >, std::conditional_t< std::is_base_of< llvm::cl::generic_parser_base, llvm::cl::parser< DataType > >::value, GenericOptionParser< DataType >, llvm::cl::parser< DataType > >> OptionParser
The specific parser to use.
A utility class to represent the analyses that are known to be preserved.
void preserveAll()
Mark all analyses as preserved.
void preserve()
Preserve the given analyses.
CRTP Implementation of an action.
ArrayRef< IRUnit > irUnits
Set of IR units (operations, regions, blocks, values) that are associated with this action.
Include the generated interface declarations.
auto get(MLIRContext *context, Ts &&...params)
Helper method that injects context only if needed, this helps unify some of the attribute constructio...
This class represents a specific pass option that contains a list of values of the provided data type...
ListOption(Pass &parent, StringRef arg, Args &&...args)
This class represents a specific pass option, with a provided data type.
Option(Pass &parent, StringRef arg, Args &&...args)
The state for a single execution of a pass.
detail::PreservedAnalyses preservedAnalyses
The set of preserved analyses for the current execution.
function_ref< LogicalResult(OpPassManager &, Operation *)> pipelineExecutor
This is a callback in the PassManager that allows to schedule dynamic pipelines that will be rooted a...
AnalysisManager analysisManager
The analysis manager for the operation.
llvm::PointerIntPair< Operation *, 1, bool > irAndPassFailed
The current operation being transformed and a bool for if the pass signaled a failure.
PassExecutionState(Operation *ir, AnalysisManager analysisManager, function_ref< LogicalResult(OpPassManager &, Operation *)> pipelineExecutor)