xref: /llvm-project/mlir/include/mlir/IR/Action.h (revision db791b278a414fb6df1acc1799adcf11d8fb9169)
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.
getActionID()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 
print(raw_ostream & os)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.
getContextIRUnits()55   virtual ArrayRef<IRUnit> getContextIRUnits() const { return irUnits; }
56 
57 protected:
Action(TypeID actionID,ArrayRef<IRUnit> irUnits)58   Action(TypeID actionID, ArrayRef<IRUnit> irUnits)
59       : actionID(actionID), irUnits(irUnits) {}
60 
61   /// The type of the derived action class, used for `isa`/`dyn_cast`.
62   TypeID actionID;
63 
64   /// Set of IR units (operations, regions, blocks, values) that are associated
65   /// with this action.
66   ArrayRef<IRUnit> irUnits;
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:
78   ActionImpl(ArrayRef<IRUnit> irUnits = {})
Action(TypeID::get<Derived> (),irUnits)79       : Action(TypeID::get<Derived>(), irUnits) {}
80 
81   /// Provide classof to allow casting between action types.
classof(const Action * action)82   static bool classof(const Action *action) {
83     return action->getActionID() == TypeID::get<Derived>();
84   }
85 
86   /// Forward tag access to the derived class.
getTag()87   StringRef getTag() const final { return Derived::tag; }
88 };
89 
90 } // namespace tracing
91 } // namespace mlir
92 
93 #endif // MLIR_IR_ACTION_H
94