MLIR  21.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/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(breakpointManagers,
28  [&](const BreakpointManager *manager) {
29  return manager->match(action->getAction());
30  });
31 }
32 
34  Breakpoint *breakpoint, bool willExecute) {
35  if (!shouldLog(action))
36  return;
37  SmallVector<char> name;
38  llvm::get_thread_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(action->getAction().getContextIRUnits())
61  << ")";
62  os << "`\n";
63 }
64 
66  if (!shouldLog(action))
67  return;
68  SmallVector<char> name;
69  llvm::get_thread_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 }
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.