| 1 | //===- ActionLogging.cpp - Logging Actions *- 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 | #include "mlir/Debug/Observers/ActionLogging.h" |
| 10 | #include "mlir/Debug/BreakpointManager.h" |
| 11 | #include "mlir/IR/Action.h" |
| 12 | #include "llvm/Support/InterleavedRange.h" |
| 13 | #include "llvm/Support/Threading.h" |
| 14 | #include "llvm/Support/raw_ostream.h" |
| 15 | |
| 16 | using namespace mlir; |
| 17 | using namespace mlir::tracing; |
| 18 | |
| 19 | //===----------------------------------------------------------------------===// |
| 20 | // ActionLogger |
| 21 | //===----------------------------------------------------------------------===// |
| 22 | |
| 23 | bool ActionLogger::shouldLog(const ActionActiveStack *action) { |
| 24 | // If some condition was set, we ensured it is met before logging. |
| 25 | if (breakpointManagers.empty()) |
| 26 | return true; |
| 27 | return llvm::any_of(Range&: breakpointManagers, |
| 28 | P: [&](const BreakpointManager *manager) { |
| 29 | return manager->match(action: action->getAction()); |
| 30 | }); |
| 31 | } |
| 32 | |
| 33 | void ActionLogger::beforeExecute(const ActionActiveStack *action, |
| 34 | Breakpoint *breakpoint, bool willExecute) { |
| 35 | if (!shouldLog(action)) |
| 36 | return; |
| 37 | SmallVector<char> name; |
| 38 | llvm::get_thread_name(Name&: name); |
| 39 | if (name.empty()) { |
| 40 | llvm::raw_svector_ostream os(name); |
| 41 | os << llvm::get_threadid(); |
| 42 | } |
| 43 | os << "[thread "<< name << "] "; |
| 44 | if (willExecute) |
| 45 | os << "begins "; |
| 46 | else |
| 47 | os << "skipping "; |
| 48 | if (printBreakpoints) { |
| 49 | if (breakpoint) |
| 50 | os << "(on breakpoint: "<< *breakpoint << ") "; |
| 51 | else |
| 52 | os << "(no breakpoint) "; |
| 53 | } |
| 54 | os << "Action "; |
| 55 | if (printActions) |
| 56 | action->getAction().print(os); |
| 57 | else |
| 58 | os << action->getAction().getTag(); |
| 59 | if (printIRUnits) |
| 60 | os << " ("<< llvm::interleaved(R: action->getAction().getContextIRUnits()) |
| 61 | << ")"; |
| 62 | os << "`\n"; |
| 63 | } |
| 64 | |
| 65 | void ActionLogger::afterExecute(const ActionActiveStack *action) { |
| 66 | if (!shouldLog(action)) |
| 67 | return; |
| 68 | SmallVector<char> name; |
| 69 | llvm::get_thread_name(Name&: name); |
| 70 | if (name.empty()) { |
| 71 | llvm::raw_svector_ostream os(name); |
| 72 | os << llvm::get_threadid(); |
| 73 | } |
| 74 | os << "[thread "<< name << "] completed `"<< action->getAction().getTag() |
| 75 | << "`\n"; |
| 76 | } |
| 77 |
