xref: /llvm-project/mlir/include/mlir/Debug/BreakpointManager.h (revision fa51c1753a274fbb7a71d8fe91fd4e5caf2fa4d3)
1 //===- BreakpointManager.h - Breakpoint Manager 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 #ifndef MLIR_TRACING_BREAKPOINTMANAGER_H
10 #define MLIR_TRACING_BREAKPOINTMANAGER_H
11 
12 #include "mlir/IR/Action.h"
13 #include "llvm/ADT/MapVector.h"
14 
15 namespace mlir {
16 namespace tracing {
17 
18 /// This abstract class represents a breakpoint.
19 class Breakpoint {
20 public:
21   virtual ~Breakpoint() = default;
22 
23   /// TypeID for the subclass, used for casting purpose.
getTypeID()24   TypeID getTypeID() const { return typeID; }
25 
isEnabled()26   bool isEnabled() const { return enableStatus; }
enable()27   void enable() { enableStatus = true; }
disable()28   void disable() { enableStatus = false; }
29   virtual void print(raw_ostream &os) const = 0;
30 
31 protected:
Breakpoint(TypeID typeID)32   Breakpoint(TypeID typeID) : enableStatus(true), typeID(typeID) {}
33 
34 private:
35   /// The current state of the breakpoint. A breakpoint can be either enabled
36   /// or disabled.
37   bool enableStatus;
38   TypeID typeID;
39 };
40 
41 inline raw_ostream &operator<<(raw_ostream &os, const Breakpoint &breakpoint) {
42   breakpoint.print(os);
43   return os;
44 }
45 
46 /// This class provides a CRTP wrapper around a base breakpoint class to define
47 /// a few necessary utility methods.
48 template <typename Derived>
49 class BreakpointBase : public Breakpoint {
50 public:
51   /// Support isa/dyn_cast functionality for the derived pass class.
classof(const Breakpoint * breakpoint)52   static bool classof(const Breakpoint *breakpoint) {
53     return breakpoint->getTypeID() == TypeID::get<Derived>();
54   }
55 
56 protected:
BreakpointBase()57   BreakpointBase() : Breakpoint(TypeID::get<Derived>()) {}
58 };
59 
60 /// A breakpoint manager is responsible for managing a set of breakpoints and
61 /// matching them to a given action.
62 class BreakpointManager {
63 public:
64   virtual ~BreakpointManager() = default;
65 
66   /// TypeID for the subclass, used for casting purpose.
getTypeID()67   TypeID getTypeID() const { return typeID; }
68 
69   /// Try to match a Breakpoint to a given Action. If there is a match and
70   /// the breakpoint is enabled, return the breakpoint. Otherwise, return
71   /// nullptr.
72   virtual Breakpoint *match(const Action &action) const = 0;
73 
74 protected:
BreakpointManager(TypeID typeID)75   BreakpointManager(TypeID typeID) : typeID(typeID) {}
76 
77   TypeID typeID;
78 };
79 
80 /// CRTP base class for BreakpointManager implementations.
81 template <typename Derived>
82 class BreakpointManagerBase : public BreakpointManager {
83 public:
BreakpointManagerBase()84   BreakpointManagerBase() : BreakpointManager(TypeID::get<Derived>()) {}
85 
86   /// Provide classof to allow casting between breakpoint manager types.
classof(const BreakpointManager * breakpointManager)87   static bool classof(const BreakpointManager *breakpointManager) {
88     return breakpointManager->getTypeID() == TypeID::get<Derived>();
89   }
90 };
91 
92 } // namespace tracing
93 } // namespace mlir
94 
95 #endif // MLIR_TRACING_BREAKPOINTMANAGER_H
96