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