2019-03-14 14:44:58 -07:00
|
|
|
//===- 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:
|
2019-08-16 17:59:03 -07:00
|
|
|
/// A filter function to decide if the given pass should be printed. Returns
|
|
|
|
/// true if the pass should be printed, false otherwise.
|
2019-03-14 14:44:58 -07:00
|
|
|
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.
|
2019-08-16 17:59:03 -07:00
|
|
|
void runBeforePass(Pass *pass, Operation *op) override;
|
|
|
|
void runAfterPass(Pass *pass, Operation *op) override;
|
|
|
|
void runAfterPassFailed(Pass *pass, Operation *op) override;
|
2019-03-14 14:44:58 -07:00
|
|
|
|
|
|
|
/// 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
|
|
|
|
|
2019-07-12 11:20:09 -07:00
|
|
|
/// Returns true if the given pass is hidden from IR printing.
|
|
|
|
static bool isHiddenPass(Pass *pass) {
|
2019-09-02 19:24:47 -07:00
|
|
|
return isAdaptorPass(pass) || isa<VerifierPass>(pass);
|
2019-07-12 11:20:09 -07:00
|
|
|
}
|
|
|
|
|
2019-10-10 21:57:24 -07:00
|
|
|
static void printIR(Operation *op, bool printModuleScope, raw_ostream &out,
|
|
|
|
OpPrintingFlags flags) {
|
2019-09-14 21:56:09 -07:00
|
|
|
// Check to see if we are printing the top-level module.
|
|
|
|
auto module = dyn_cast<ModuleOp>(op);
|
|
|
|
if (module && !op->getBlock())
|
2019-10-10 21:57:24 -07:00
|
|
|
return module.print(out << "\n", flags);
|
2019-03-14 14:44:58 -07:00
|
|
|
|
2019-09-14 21:56:09 -07:00
|
|
|
// Otherwise, check to see if we are not printing at module scope.
|
|
|
|
if (!printModuleScope)
|
2019-10-10 21:57:24 -07:00
|
|
|
return op->print(out << "\n", flags);
|
2019-03-14 14:44:58 -07:00
|
|
|
|
2019-09-14 21:56:09 -07:00
|
|
|
// Otherwise, we are printing at module scope.
|
|
|
|
out << " ('" << op->getName() << "' operation";
|
|
|
|
if (auto symbolName =
|
|
|
|
op->getAttrOfType<StringAttr>(SymbolTable::getSymbolAttrName()))
|
|
|
|
out << ": @" << symbolName.getValue();
|
|
|
|
out << ")\n";
|
|
|
|
|
|
|
|
// Find the top-level module operation.
|
|
|
|
auto *topLevelOp = op;
|
|
|
|
while (auto *parentOp = topLevelOp->getParentOp())
|
|
|
|
topLevelOp = parentOp;
|
2019-03-14 14:44:58 -07:00
|
|
|
|
2019-09-14 21:56:09 -07:00
|
|
|
// Check to see if the top-level operation is actually a module in the case of
|
|
|
|
// invalid-ir.
|
|
|
|
if (auto module = dyn_cast<ModuleOp>(topLevelOp))
|
2019-10-10 21:57:24 -07:00
|
|
|
module.print(out, flags);
|
2019-09-14 21:56:09 -07:00
|
|
|
else
|
2019-10-10 21:57:24 -07:00
|
|
|
topLevelOp->print(out, flags);
|
2019-03-14 14:44:58 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Instrumentation hooks.
|
2019-08-16 17:59:03 -07:00
|
|
|
void IRPrinterInstrumentation::runBeforePass(Pass *pass, Operation *op) {
|
|
|
|
// Skip hidden passes and passes that the user filtered out.
|
2019-07-12 11:20:09 -07:00
|
|
|
if (!shouldPrintBeforePass || isHiddenPass(pass) ||
|
2019-03-14 14:44:58 -07:00
|
|
|
!shouldPrintBeforePass(pass))
|
|
|
|
return;
|
|
|
|
out << formatv("*** IR Dump Before {0} ***", pass->getName());
|
2019-10-10 21:57:24 -07:00
|
|
|
printIR(op, printModuleScope, out, OpPrintingFlags());
|
2019-07-12 11:20:09 -07:00
|
|
|
out << "\n\n";
|
2019-03-14 14:44:58 -07:00
|
|
|
}
|
|
|
|
|
2019-08-16 17:59:03 -07:00
|
|
|
void IRPrinterInstrumentation::runAfterPass(Pass *pass, Operation *op) {
|
|
|
|
// Skip hidden passes and passes that the user filtered out.
|
2019-07-12 11:20:09 -07:00
|
|
|
if (!shouldPrintAfterPass || isHiddenPass(pass) ||
|
2019-03-14 14:44:58 -07:00
|
|
|
!shouldPrintAfterPass(pass))
|
|
|
|
return;
|
|
|
|
out << formatv("*** IR Dump After {0} ***", pass->getName());
|
2019-10-10 21:57:24 -07:00
|
|
|
printIR(op, printModuleScope, out, OpPrintingFlags());
|
2019-07-12 11:20:09 -07:00
|
|
|
out << "\n\n";
|
2019-03-14 14:44:58 -07:00
|
|
|
}
|
|
|
|
|
2019-08-16 17:59:03 -07:00
|
|
|
void IRPrinterInstrumentation::runAfterPassFailed(Pass *pass, Operation *op) {
|
2019-03-14 14:44:58 -07:00
|
|
|
// 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());
|
2019-10-10 21:57:24 -07:00
|
|
|
printIR(op, printModuleScope, out, OpPrintingFlags().printGenericOpForm());
|
2019-07-12 11:20:09 -07:00
|
|
|
out << "\n\n";
|
2019-03-14 14:44:58 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// 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) {
|
2019-09-14 17:44:10 -07:00
|
|
|
addInstrumentation(std::make_unique<IRPrinterInstrumentation>(
|
2019-03-14 14:44:58 -07:00
|
|
|
std::move(shouldPrintBeforePass), std::move(shouldPrintAfterPass),
|
|
|
|
printModuleScope, out));
|
|
|
|
}
|