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