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