MLIR  19.0.0git
Action.h
Go to the documentation of this file.
1 //===- Action.h - Action Support ---------------------*- 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 // This file contains definitions for the action framework. This framework
10 // allows for external entities to control certain actions taken by the compiler
11 // by registering handler functions.
12 //
13 //===----------------------------------------------------------------------===//
14 
15 #ifndef MLIR_IR_ACTION_H
16 #define MLIR_IR_ACTION_H
17 
18 #include "mlir/IR/Unit.h"
20 #include "mlir/Support/TypeID.h"
21 #include "llvm/ADT/ArrayRef.h"
22 #include "llvm/ADT/Sequence.h"
23 #include "llvm/ADT/StringMap.h"
24 #include "llvm/Support/TypeName.h"
25 #include "llvm/Support/raw_ostream.h"
26 #include <functional>
27 #include <type_traits>
28 
29 namespace mlir {
30 namespace tracing {
31 
32 /// An action is a specific action that is to be taken by the compiler,
33 /// that can be toggled and controlled by an external user. There are no
34 /// constraints on the granularity of an action, it could be as simple as
35 /// "perform this fold" and as complex as "run this pass pipeline".
36 ///
37 /// This class represents the base class of the ActionImpl class (see below).
38 /// This holds the template-invariant elements of the Action class.
39 class Action {
40 public:
41  virtual ~Action() = default;
42 
43  /// Return the unique action id of this action, use for casting
44  /// functionality.
45  TypeID getActionID() const { return actionID; }
46 
47  /// Return a string "tag" which intends to uniquely identify this type of
48  /// action. For example "pass-application" or "pattern-rewrite".
49  virtual StringRef getTag() const = 0;
50 
51  virtual void print(raw_ostream &os) const {
52  os << "Action \"" << getTag() << "\"";
53  }
54 
55  /// Return the set of IR units that are associated with this action.
56  virtual ArrayRef<IRUnit> getContextIRUnits() const { return irUnits; }
57 
58 protected:
61 
62  /// The type of the derived action class, used for `isa`/`dyn_cast`.
64 
65  /// Set of IR units (operations, regions, blocks, values) that are associated
66  /// with this action.
68 };
69 
70 /// CRTP Implementation of an action. This class provides a base class for
71 /// implementing specific actions.
72 /// Derived classes are expected to provide the following:
73 /// * static constexpr StringLiteral tag = "...";
74 /// - This method returns a unique string identifier, similar to a command
75 /// line flag or DEBUG_TYPE.
76 template <typename Derived>
77 class ActionImpl : public Action {
78 public:
80  : Action(TypeID::get<Derived>(), irUnits) {}
81 
82  /// Provide classof to allow casting between action types.
83  static bool classof(const Action *action) {
84  return action->getActionID() == TypeID::get<Derived>();
85  }
86 
87  /// Forward tag access to the derived class.
88  StringRef getTag() const final { return Derived::tag; }
89 };
90 
91 } // namespace tracing
92 } // namespace mlir
93 
94 #endif // MLIR_IR_ACTION_H
This class provides an efficient unique identifier for a specific C++ type.
Definition: TypeID.h:104
CRTP Implementation of an action.
Definition: Action.h:77
static bool classof(const Action *action)
Provide classof to allow casting between action types.
Definition: Action.h:83
ActionImpl(ArrayRef< IRUnit > irUnits={})
Definition: Action.h:79
StringRef getTag() const final
Forward tag access to the derived class.
Definition: Action.h:88
An action is a specific action that is to be taken by the compiler, that can be toggled and controlle...
Definition: Action.h:39
virtual ~Action()=default
TypeID getActionID() const
Return the unique action id of this action, use for casting functionality.
Definition: Action.h:45
Action(TypeID actionID, ArrayRef< IRUnit > irUnits)
Definition: Action.h:59
virtual void print(raw_ostream &os) const
Definition: Action.h:51
virtual ArrayRef< IRUnit > getContextIRUnits() const
Return the set of IR units that are associated with this action.
Definition: Action.h:56
virtual StringRef getTag() const =0
Return a string "tag" which intends to uniquely identify this type of action.
ArrayRef< IRUnit > irUnits
Set of IR units (operations, regions, blocks, values) that are associated with this action.
Definition: Action.h:67
TypeID actionID
The type of the derived action class, used for isa/dyn_cast.
Definition: Action.h:63
Include the generated interface declarations.