// Copyright (c) 2021 PaddlePaddle Authors. All Rights Reserved. // // 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 #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include "paddle/infrt/common/global.h" #include "paddle/infrt/dialect/init_dialects.h" namespace cl = llvm::cl; static cl::opt inputFilename(cl::Positional, cl::desc(""), cl::init("-"), cl::value_desc("filename")); llvm::raw_ostream &printIndent(int indent = 0) { for (int i = 0; i < indent; ++i) llvm::outs() << " "; return llvm::outs(); } void printOperation(mlir::Operation *op, int indent); void printRegion(mlir::Region ®ion, int indent); // NOLINT void printBlock(mlir::Block &block, int indent); // NOLINT void printOperation(mlir::Operation *op, int indent) { llvm::Optional module_op = llvm::None; if (llvm::isa(op)) module_op = llvm::dyn_cast(op); llvm::Optional func_op = llvm::None; if (llvm::isa(op)) func_op = llvm::dyn_cast(op); printIndent(indent) << "op: '" << op->getName(); // This getName is inherited from Operation::getName if (module_op) { printIndent() << "@" << module_op->getName(); } // This getName is inherited from SymbolOpInterfaceTrait::getName, // which return value of "sym_name" in ModuleOp or FuncOp attributes. if (func_op) { printIndent() << "@" << func_op->getName(); } printIndent() << "' with " << op->getNumOperands() << " operands" << ", " << op->getNumResults() << " results" << ", " << op->getAttrs().size() << " attributes" << ", " << op->getNumRegions() << " regions" << ", " << op->getNumSuccessors() << " successors\n"; if (!op->getAttrs().empty()) { printIndent(indent) << op->getAttrs().size() << " attributes:\n"; for (mlir::NamedAttribute attr : op->getAttrs()) { printIndent(indent + 1) << "- {" << attr.getName() << " : " << attr.getValue() << "}\n"; } } if (op->getNumRegions() > 0) { printIndent(indent) << op->getNumRegions() << " nested regions:\n"; for (mlir::Region ®ion : op->getRegions()) { printRegion(region, indent + 1); } } } void printRegion(mlir::Region ®ion, int indent) { // NOLINT printIndent(indent) << "Region with " << region.getBlocks().size() << " blocks:\n"; for (mlir::Block &block : region.getBlocks()) { printBlock(block, indent + 1); } } void printBlock(mlir::Block &block, int indent) { // NOLINT printIndent(indent) << "Block with " << block.getNumArguments() << " arguments" << ", " << block.getNumSuccessors() << " successors" << ", " << block.getOperations().size() << " operations\n"; for (mlir::Operation &operation : block.getOperations()) { printOperation(&operation, indent + 1); } } int main(int argc, char **argv) { mlir::registerAsmPrinterCLOptions(); mlir::registerMLIRContextCLOptions(); mlir::registerPassManagerCLOptions(); cl::ParseCommandLineOptions(argc, argv, "mlir demo"); mlir::DialectRegistry registry; infrt::registerCinnDialects(registry); mlir::MLIRContext context(registry); // mlir will verify module automatically after parsing. // https://github.com/llvm/llvm-project/blob/38d18d93534d290d045bbbfa86337e70f1139dc2/mlir/lib/Parser/Parser.cpp#L2051 // mlir::OwningModuleRef module_ref = mlir::parseSourceString(mlir_source, // context); mlir::OwningModuleRef module_ref = mlir::parseSourceFile(inputFilename, &context); std::cout << "----------print IR Structure begin----------" << std::endl; printOperation(module_ref->getOperation(), 0); std::cout << "----------print IR Structure end----------" << std::endl; module_ref->dump(); return 0; }