1 //===- TestPrintInvalid.cpp - Test printing invalid ops -------------------===// 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 // This pass creates and prints to the standard output an invalid operation and 10 // a valid operation. 11 // 12 //===----------------------------------------------------------------------===// 13 14 #include "mlir/Dialect/Func/IR/FuncOps.h" 15 #include "mlir/Pass/Pass.h" 16 #include "llvm/Support/raw_ostream.h" 17 18 using namespace mlir; 19 20 namespace { 21 struct TestPrintInvalidPass 22 : public PassWrapper<TestPrintInvalidPass, OperationPass<ModuleOp>> { 23 StringRef getArgument() const final { return "test-print-invalid"; } 24 StringRef getDescription() const final { 25 return "Test printing invalid ops."; 26 } 27 void getDependentDialects(DialectRegistry ®istry) const override { 28 registry.insert<func::FuncDialect>(); 29 } 30 31 void runOnOperation() override { 32 Location loc = getOperation().getLoc(); 33 OpBuilder builder(getOperation().getBodyRegion()); 34 auto funcOp = builder.create<FuncOp>( 35 loc, "test", FunctionType::get(getOperation().getContext(), {}, {})); 36 funcOp.addEntryBlock(); 37 // The created function is invalid because there is no return op. 38 llvm::outs() << "Invalid operation:\n" << funcOp << "\n"; 39 builder.setInsertionPointToEnd(&funcOp.getBody().front()); 40 builder.create<func::ReturnOp>(loc); 41 // Now this function is valid. 42 llvm::outs() << "Valid operation:\n" << funcOp << "\n"; 43 funcOp.erase(); 44 } 45 }; 46 } // namespace 47 48 namespace mlir { 49 void registerTestPrintInvalidPass() { 50 PassRegistration<TestPrintInvalidPass>{}; 51 } 52 } // namespace mlir 53