1//===- ExecutionContext.cpp - Debug Execution Context Support -------------===//
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#include "mlir/Debug/ExecutionContext.h"
10
11#include "llvm/ADT/ScopeExit.h"
12#include "llvm/Support/FormatVariadic.h"
13
14using namespace mlir;
15using namespace mlir::tracing;
16
17//===----------------------------------------------------------------------===//
18// ActionActiveStack
19//===----------------------------------------------------------------------===//
20
21void ActionActiveStack::print(raw_ostream &os, bool withContext) const {
22 os << "ActionActiveStack depth " << getDepth() << "\n";
23 const ActionActiveStack *current = this;
24 int count = 0;
25 while (current) {
26 llvm::errs() << llvm::formatv(Fmt: "#{0,3}: ", Vals: count++);
27 current->action.print(os&: llvm::errs());
28 llvm::errs() << "\n";
29 ArrayRef<IRUnit> context = current->action.getContextIRUnits();
30 if (withContext && !context.empty()) {
31 llvm::errs() << "Context:\n";
32 llvm::interleave(
33 c: current->action.getContextIRUnits(),
34 each_fn: [&](const IRUnit &unit) {
35 llvm::errs() << " - ";
36 unit.print(os&: llvm::errs());
37 },
38 between_fn: [&]() { llvm::errs() << "\n"; });
39 llvm::errs() << "\n";
40 }
41 current = current->parent;
42 }
43}
44
45#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
46void ActionActiveStack::dump() const {
47 print(llvm::errs(), /*withContext=*/true);
48 llvm::errs() << "\n";
49}
50#endif
51
52//===----------------------------------------------------------------------===//
53// ExecutionContext
54//===----------------------------------------------------------------------===//
55
56static const LLVM_THREAD_LOCAL ActionActiveStack *actionStack = nullptr;
57
58void ExecutionContext::registerObserver(Observer *observer) {
59 observers.push_back(Elt: observer);
60}
61
62void ExecutionContext::operator()(llvm::function_ref<void()> transform,
63 const Action &action) {
64 // Update the top of the stack with the current action.
65 int depth = 0;
66 if (actionStack)
67 depth = actionStack->getDepth() + 1;
68 ActionActiveStack info{actionStack, action, depth};
69 actionStack = &info;
70 llvm::scope_exit raii([&]() { actionStack = info.getParent(); });
71 Breakpoint *breakpoint = nullptr;
72
73 // Invoke the callback here and handles control requests here.
74 auto handleUserInput = [&]() -> bool {
75 if (!onBreakpointControlExecutionCallback)
76 return true;
77 auto todoNext = onBreakpointControlExecutionCallback(actionStack);
78 switch (todoNext) {
79 case ExecutionContext::Apply:
80 depthToBreak = std::nullopt;
81 return true;
82 case ExecutionContext::Skip:
83 depthToBreak = std::nullopt;
84 return false;
85 case ExecutionContext::Step:
86 depthToBreak = depth + 1;
87 return true;
88 case ExecutionContext::Next:
89 depthToBreak = depth;
90 return true;
91 case ExecutionContext::Finish:
92 depthToBreak = depth - 1;
93 return true;
94 case ExecutionContext::Rerun:
95 depthToBreak = std::nullopt;
96 rerunControlStack.push_back(Elt: depth);
97 return true;
98 }
99 llvm::report_fatal_error(reason: "Unknown control request");
100 };
101
102 auto rerunCurrentActionIfRequested = [&]() -> bool {
103 if (!rerunControlStack.empty() && rerunControlStack.back() == depth) {
104 // If the user requested to rerun this action, we do it here.
105 rerunControlStack.pop_back();
106 actionStack = info.getParent();
107 (*this)(transform, action);
108 return true;
109 }
110 return false;
111 };
112
113 // Try to find a breakpoint that would hit on this action.
114 // Right now there is no way to collect them all, we stop at the first one.
115 for (auto *breakpointManager : breakpoints) {
116 breakpoint = breakpointManager->match(action);
117 if (breakpoint)
118 break;
119 }
120 info.setBreakpoint(breakpoint);
121
122 bool shouldExecuteAction = true;
123 // If we have a breakpoint, or if `depthToBreak` was previously set and the
124 // current depth matches, we invoke the user-provided callback.
125 if (breakpoint || (depthToBreak && depth <= depthToBreak))
126 shouldExecuteAction = handleUserInput();
127
128 // Notify the observers about the current action.
129 for (auto *observer : observers)
130 observer->beforeExecute(action: actionStack, breakpoint, willExecute: shouldExecuteAction);
131
132 if (shouldExecuteAction) {
133 // Execute the action here.
134 transform();
135
136 // Notify the observers about completion of the action.
137 for (auto *observer : observers)
138 observer->afterExecute(action: actionStack);
139 }
140
141 if (rerunCurrentActionIfRequested())
142 return;
143
144 if (depthToBreak && depth <= depthToBreak) {
145 handleUserInput();
146 rerunCurrentActionIfRequested();
147 }
148}
149