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