MLIR  20.0.0git
ActionLogging.cpp
Go to the documentation of this file.
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 
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 
22 bool 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 
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 
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 }
virtual void print(raw_ostream &os) const
Definition: Action.h:50
virtual ArrayRef< IRUnit > getContextIRUnits() const
Return the set of IR units that are associated with this action.
Definition: Action.h:55
virtual StringRef getTag() const =0
Return a string "tag" which intends to uniquely identify this type of action.
A breakpoint manager is responsible for managing a set of breakpoints and matching them to a given ac...
virtual Breakpoint * match(const Action &action) const =0
Try to match a Breakpoint to a given Action.
This abstract class represents a breakpoint.
Include the generated interface declarations.
This class is used to keep track of the active actions in the stack.
const Action & getAction() const
void beforeExecute(const ActionActiveStack *action, Breakpoint *breakpoint, bool willExecute) override
This method is called before the Action is executed If a breakpoint was hit, it is passed as an argum...
void afterExecute(const ActionActiveStack *action) override
This method is called after the Action is executed, if it was executed.