#include "mlir/IR/BuiltinOps.h"
#include "mlir/Pass/Pass.h"
using namespace mlir;
namespace {
struct TestPrintNestingPass
: public PassWrapper<TestPrintNestingPass, OperationPass<>> {
MLIR_DEFINE_EXPLICIT_INTERNAL_INLINE_TYPE_ID(TestPrintNestingPass)
StringRef getArgument() const final { return "test-print-nesting"; }
StringRef getDescription() const final { return "Test various printing."; }
void runOnOperation() override {
Operation *op = getOperation();
resetIndent();
printOperation(op);
}
void printOperation(Operation *op) {
printIndent() << "visiting op: '" << op->getName() << "' with "
<< op->getNumOperands() << " operands and "
<< op->getNumResults() << " results\n";
if (!op->getAttrs().empty()) {
printIndent() << op->getAttrs().size() << " attributes:\n";
for (NamedAttribute attr : op->getAttrs())
printIndent() << " - '" << attr.getName().getValue() << "' : '"
<< attr.getValue() << "'\n";
}
printIndent() << " " << op->getNumRegions() << " nested regions:\n";
auto indent = pushIndent();
for (Region ®ion : op->getRegions())
printRegion(region);
}
void printRegion(Region ®ion) {
printIndent() << "Region with " << region.getBlocks().size()
<< " blocks:\n";
auto indent = pushIndent();
for (Block &block : region.getBlocks())
printBlock(block);
}
void printBlock(Block &block) {
printIndent()
<< "Block with " << block.getNumArguments() << " arguments, "
<< block.getNumSuccessors()
<< " successors, and "
<< block.getOperations().size() << " operations\n";
auto indent = pushIndent();
for (Operation &op : block.getOperations())
printOperation(&op);
}
int indent;
struct IdentRAII {
int &indent;
IdentRAII(int &indent) : indent(indent) {}
~IdentRAII() { --indent; }
};
void resetIndent() { indent = 0; }
IdentRAII pushIndent() { return IdentRAII(++indent); }
llvm::raw_ostream &printIndent() {
for (int i = 0; i < indent; ++i)
llvm::outs() << " ";
return llvm::outs();
}
};
}
namespace mlir {
void registerTestPrintNestingPass() {
PassRegistration<TestPrintNestingPass>();
}
}