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/LogicalResult.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: |
59 | Action(TypeID actionID, ArrayRef<IRUnit> irUnits) |
60 | : actionID(actionID), irUnits(irUnits) {} |
61 | |
62 | /// The type of the derived action class, used for `isa`/`dyn_cast`. |
63 | TypeID actionID; |
64 | |
65 | /// Set of IR units (operations, regions, blocks, values) that are associated |
66 | /// with this action. |
67 | ArrayRef<IRUnit> irUnits; |
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: |
79 | ActionImpl(ArrayRef<IRUnit> irUnits = {}) |
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 |
95 | |