summaryrefslogtreecommitdiffstats
path: root/mlir/lib/Pass/IRPrinting.cpp
blob: 2de4b05a36c831f98df0ae76aa1a2af55793451b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
//===- IRPrinting.cpp -----------------------------------------------------===//
//
// Copyright 2019 The MLIR Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//   http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// =============================================================================

#include "PassDetail.h"
#include "mlir/IR/Module.h"
#include "mlir/Pass/PassManager.h"
#include "llvm/Support/Format.h"
#include "llvm/Support/FormatVariadic.h"

using namespace mlir;
using namespace mlir::detail;

namespace {
class IRPrinterInstrumentation : public PassInstrumentation {
public:
  /// A filter function to decide if the given ir should be printed. Returns
  /// true if the ir should be printed, false otherwise.
  using ShouldPrintFn = std::function<bool(Pass *)>;

  IRPrinterInstrumentation(ShouldPrintFn &&shouldPrintBeforePass,
                           ShouldPrintFn &&shouldPrintAfterPass,
                           bool printModuleScope, raw_ostream &out)
      : shouldPrintBeforePass(shouldPrintBeforePass),
        shouldPrintAfterPass(shouldPrintAfterPass),
        printModuleScope(printModuleScope), out(out) {
    assert((shouldPrintBeforePass || shouldPrintAfterPass) &&
           "expected atleast one valid filter function");
  }

private:
  /// Instrumentation hooks.
  void runBeforePass(Pass *pass, const llvm::Any &ir) override;
  void runAfterPass(Pass *pass, const llvm::Any &ir) override;
  void runAfterPassFailed(Pass *pass, const llvm::Any &ir) override;

  /// Filter functions for before and after pass execution.
  ShouldPrintFn shouldPrintBeforePass, shouldPrintAfterPass;

  /// Flag to toggle if the printer should always print at module scope.
  bool printModuleScope;

  /// The stream to output to.
  raw_ostream &out;
};
} // end anonymous namespace

/// Returns true if the given pass is hidden from IR printing.
static bool isHiddenPass(Pass *pass) {
  return isAdaptorPass(pass) || isVerifierPass(pass);
}

static void printIR(const llvm::Any &ir, bool printModuleScope,
                    raw_ostream &out) {
  // Check for printing at module scope.
  if (printModuleScope && llvm::any_isa<FuncOp>(ir)) {
    FuncOp function = llvm::any_cast<FuncOp>(ir);

    // Print the function name and a newline before the Module.
    out << " (function: " << function.getName() << ")\n";
    function.getParentOfType<ModuleOp>().print(out);
    return;
  }

  // Print a newline before the IR.
  out << "\n";

  // Print the given function.
  if (llvm::any_isa<FuncOp>(ir)) {
    llvm::any_cast<FuncOp>(ir).print(out);
    return;
  }

  // Print the given module.
  assert(llvm::any_isa<ModuleOp>(ir) && "unexpected IR unit");
  llvm::any_cast<ModuleOp>(ir).print(out);
}

/// Instrumentation hooks.
void IRPrinterInstrumentation::runBeforePass(Pass *pass, const llvm::Any &ir) {
  // Skip adaptor passes and passes that the user filtered out.
  if (!shouldPrintBeforePass || isHiddenPass(pass) ||
      !shouldPrintBeforePass(pass))
    return;
  out << formatv("*** IR Dump Before {0} ***", pass->getName());
  printIR(ir, printModuleScope, out);
  out << "\n\n";
}

void IRPrinterInstrumentation::runAfterPass(Pass *pass, const llvm::Any &ir) {
  // Skip adaptor passes and passes that the user filtered out.
  if (!shouldPrintAfterPass || isHiddenPass(pass) ||
      !shouldPrintAfterPass(pass))
    return;
  out << formatv("*** IR Dump After {0} ***", pass->getName());
  printIR(ir, printModuleScope, out);
  out << "\n\n";
}

void IRPrinterInstrumentation::runAfterPassFailed(Pass *pass,
                                                  const llvm::Any &ir) {
  // Skip adaptor passes and passes that the user filtered out.
  if (!shouldPrintAfterPass || isAdaptorPass(pass) ||
      !shouldPrintAfterPass(pass))
    return;
  out << formatv("*** IR Dump After {0} Failed ***", pass->getName());
  printIR(ir, printModuleScope, out);
  out << "\n\n";
}

//===----------------------------------------------------------------------===//
// PassManager
//===----------------------------------------------------------------------===//

/// Add an instrumentation to print the IR before and after pass execution.
void PassManager::enableIRPrinting(
    std::function<bool(Pass *)> shouldPrintBeforePass,
    std::function<bool(Pass *)> shouldPrintAfterPass, bool printModuleScope,
    raw_ostream &out) {
  addInstrumentation(new IRPrinterInstrumentation(
      std::move(shouldPrintBeforePass), std::move(shouldPrintAfterPass),
      printModuleScope, out));
}
OpenPOWER on IntegriCloud