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