xref: /llvm-project/mlir/include/mlir/Debug/CLOptionsSetup.h (revision db791b278a414fb6df1acc1799adcf11d8fb9169)
1 //===- CLOptionsSetup.h - Helpers to setup debug CL options -----*- 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_DEBUG_CLOPTIONSSETUP_H
10 #define MLIR_DEBUG_CLOPTIONSSETUP_H
11 
12 #include "mlir/Debug/BreakpointManagers/FileLineColLocBreakpointManager.h"
13 #include "llvm/ADT/StringRef.h"
14 
15 #include <memory>
16 
17 namespace mlir {
18 class MLIRContext;
19 namespace tracing {
20 class BreakpointManager;
21 
22 class DebugConfig {
23 public:
24   /// Register the options as global LLVM command line options.
25   static void registerCLOptions();
26 
27   /// Create a new config with the default set from the CL options.
28   static DebugConfig createFromCLOptions();
29 
30   ///
31   /// Options.
32   ///
33 
34   /// Enable the Debugger action hook: it makes a debugger (like gdb or lldb)
35   /// able to intercept MLIR Actions.
36   void enableDebuggerActionHook(bool enabled = true) {
37     enableDebuggerActionHookFlag = enabled;
38   }
39 
40   /// Return true if the debugger action hook is enabled.
isDebuggerActionHookEnabled()41   bool isDebuggerActionHookEnabled() const {
42     return enableDebuggerActionHookFlag;
43   }
44 
45   /// Set the filename to use for logging actions, use "-" for stdout.
logActionsTo(StringRef filename)46   DebugConfig &logActionsTo(StringRef filename) {
47     logActionsToFlag = filename;
48     return *this;
49   }
50   /// Get the filename to use for logging actions.
getLogActionsTo()51   StringRef getLogActionsTo() const { return logActionsToFlag; }
52 
53   /// Get the filename to use for profiling actions.
getProfileActionsTo()54   StringRef getProfileActionsTo() const { return profileActionsToFlag; }
55 
56   /// Set a location breakpoint manager to filter out action logging based on
57   /// the attached IR location in the Action context. Ownership stays with the
58   /// caller.
addLogActionLocFilter(tracing::BreakpointManager * breakpointManager)59   void addLogActionLocFilter(tracing::BreakpointManager *breakpointManager) {
60     logActionLocationFilter.push_back(breakpointManager);
61   }
62 
63   /// Get the location breakpoint managers to use to filter out action logging.
getLogActionsLocFilters()64   ArrayRef<tracing::BreakpointManager *> getLogActionsLocFilters() const {
65     return logActionLocationFilter;
66   }
67 
68 protected:
69   /// Enable the Debugger action hook: a debugger (like gdb or lldb) can
70   /// intercept MLIR Actions.
71   bool enableDebuggerActionHookFlag = false;
72 
73   /// Log action execution to the given file (or "-" for stdout)
74   std::string logActionsToFlag;
75 
76   /// Profile action execution to the given file (or "-" for stdout)
77   std::string profileActionsToFlag;
78 
79   /// Location Breakpoints to filter the action logging.
80   std::vector<tracing::BreakpointManager *> logActionLocationFilter;
81 };
82 
83 /// This is a RAII class that installs the debug handlers on the context
84 /// based on the provided configuration.
85 class InstallDebugHandler {
86 public:
87   InstallDebugHandler(MLIRContext &context, const DebugConfig &config);
88   ~InstallDebugHandler();
89 
90 private:
91   class Impl;
92   std::unique_ptr<Impl> impl;
93 };
94 
95 } // namespace tracing
96 } // namespace mlir
97 
98 #endif // MLIR_DEBUG_CLOPTIONSSETUP_H
99