12
12
#include " mlir/Debug/DebuggerExecutionContextHook.h"
13
13
#include " mlir/Debug/ExecutionContext.h"
14
14
#include " mlir/Debug/Observers/ActionLogging.h"
15
+ #include " mlir/Debug/Observers/ActionProfiler.h"
15
16
#include " mlir/IR/MLIRContext.h"
16
17
#include " mlir/Support/FileUtilities.h"
17
18
#include " llvm/Support/CommandLine.h"
@@ -30,6 +31,12 @@ struct DebugConfigCLOptions : public DebugConfig {
30
31
" '-' is passed" ),
31
32
cl::location (logActionsToFlag)};
32
33
34
+ static cl::opt<std::string, /* ExternalStorage=*/ true > profileActionsTo{
35
+ " profile-actions-to" ,
36
+ cl::desc (" Profile action execution to a file, or stderr if "
37
+ " '-' is passed" ),
38
+ cl::location (profileActionsToFlag)};
39
+
33
40
static cl::list<std::string> logActionLocationFilter (
34
41
" log-mlir-actions-filter" ,
35
42
cl::desc (
@@ -71,6 +78,7 @@ class InstallDebugHandler::Impl {
71
78
public:
72
79
Impl (MLIRContext &context, const DebugConfig &config) {
73
80
if (config.getLogActionsTo ().empty () &&
81
+ config.getProfileActionsTo ().empty () &&
74
82
!config.isDebuggerActionHookEnabled ()) {
75
83
if (tracing::DebugCounter::isActivated ())
76
84
context.registerActionHandler (tracing::DebugCounter ());
@@ -97,6 +105,24 @@ class InstallDebugHandler::Impl {
97
105
actionLogger->addBreakpointManager (locationBreakpoint);
98
106
executionContext.registerObserver (actionLogger.get ());
99
107
}
108
+
109
+ if (!config.getProfileActionsTo ().empty ()) {
110
+ std::string errorMessage;
111
+ profileActionsFile =
112
+ openOutputFile (config.getProfileActionsTo (), &errorMessage);
113
+ if (!profileActionsFile) {
114
+ emitError (UnknownLoc::get (&context),
115
+ " Opening file for --profile-actions-to failed: " )
116
+ << errorMessage << " \n " ;
117
+ return ;
118
+ }
119
+ profileActionsFile->keep ();
120
+ raw_fd_ostream &profileActionsStream = profileActionsFile->os ();
121
+ actionProfiler =
122
+ std::make_unique<tracing::ActionProfiler>(profileActionsStream);
123
+ executionContext.registerObserver (actionProfiler.get ());
124
+ }
125
+
100
126
if (config.isDebuggerActionHookEnabled ()) {
101
127
errs () << " (with Debugger hook)" ;
102
128
setupDebuggerExecutionContextHook (executionContext);
@@ -111,6 +137,8 @@ class InstallDebugHandler::Impl {
111
137
std::unique_ptr<tracing::ActionLogger> actionLogger;
112
138
std::vector<std::unique_ptr<tracing::FileLineColLocBreakpoint>>
113
139
locationBreakpoints;
140
+ std::unique_ptr<ToolOutputFile> profileActionsFile;
141
+ std::unique_ptr<tracing::ActionProfiler> actionProfiler;
114
142
};
115
143
116
144
InstallDebugHandler::InstallDebugHandler (MLIRContext &context,
0 commit comments