CIRCT  19.0.0git
Passes.h
Go to the documentation of this file.
1 //===- Passes.h - Helpers for pipeline instrumentation ----------*- 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 #ifndef CIRCT_SUPPORT_PASSES_H
10 #define CIRCT_SUPPORT_PASSES_H
11 
12 #include "circt/Support/LLVM.h"
13 #include "mlir/Pass/Pass.h"
14 #include "mlir/Pass/PassInstrumentation.h"
15 #include "llvm/Support/Chrono.h"
16 #include "llvm/Support/Format.h"
17 
18 namespace circt {
19 // This class prints logs before and after of pass executions when its pass
20 // operation is in `LoggedOpTypes`. Note that `runBeforePass` and `runAfterPass`
21 // are not thread safe so `LoggedOpTypes` must be a set of operations whose
22 // passes are ran sequentially (e.g. mlir::ModuleOp, firrtl::CircuitOp).
23 template <class... LoggedOpTypes>
24 class VerbosePassInstrumentation : public mlir::PassInstrumentation {
25  // This stores start time points of passes.
26  using TimePoint = llvm::sys::TimePoint<>;
27  llvm::SmallVector<TimePoint> timePoints;
28  int level = 0;
29  const char *toolName;
30 
31 public:
33  void runBeforePass(Pass *pass, Operation *op) override {
34  if (isa<LoggedOpTypes...>(op)) {
35  timePoints.push_back(TimePoint::clock::now());
36  auto &os = llvm::errs();
37  os << llvm::format("[%s] ", toolName);
38  os.indent(2 * level++);
39  os << "Running \"";
40  pass->printAsTextualPipeline(llvm::errs());
41  os << "\"\n";
42  }
43  }
44 
45  void runAfterPass(Pass *pass, Operation *op) override {
46  using namespace std::chrono;
47  if (isa<LoggedOpTypes...>(op)) {
48  auto &os = llvm::errs();
49  auto elapsed = duration<double>(TimePoint::clock::now() -
50  timePoints.pop_back_val()) /
51  seconds(1);
52  os << llvm::format("[%s] ", toolName);
53  os.indent(2 * --level);
54  os << "-- Done in " << llvm::format("%.3f", elapsed) << " sec\n";
55  }
56  }
57 };
58 
59 /// Create a simple canonicalizer pass.
60 std::unique_ptr<Pass> createSimpleCanonicalizerPass();
61 
62 } // namespace circt
63 
64 #endif // CIRCT_SUPPORT_PASSES_H
llvm::SmallVector< TimePoint > timePoints
Definition: Passes.h:27
void runAfterPass(Pass *pass, Operation *op) override
Definition: Passes.h:45
void runBeforePass(Pass *pass, Operation *op) override
Definition: Passes.h:33
VerbosePassInstrumentation(const char *toolName)
Definition: Passes.h:32
llvm::sys::TimePoint<> TimePoint
Definition: Passes.h:26
The InstanceGraph op interface, see InstanceGraphInterface.td for more details.
Definition: DebugAnalysis.h:21
std::unique_ptr< Pass > createSimpleCanonicalizerPass()
Create a simple canonicalizer pass.
Definition: Passes.cpp:15