xref: /llvm-project/mlir/test/lib/IR/TestPrintInvalid.cpp (revision 58ceae9561fbae75fa175244db4e0e4bfe7416fd)
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>> {
MLIR_DEFINE_EXPLICIT_INTERNAL_INLINE_TYPE_ID__anonefe362e90111::TestPrintInvalidPass24   MLIR_DEFINE_EXPLICIT_INTERNAL_INLINE_TYPE_ID(TestPrintInvalidPass)
25 
26   StringRef getArgument() const final { return "test-print-invalid"; }
getDescription__anonefe362e90111::TestPrintInvalidPass27   StringRef getDescription() const final {
28     return "Test printing invalid ops.";
29   }
getDependentDialects__anonefe362e90111::TestPrintInvalidPass30   void getDependentDialects(DialectRegistry &registry) const override {
31     registry.insert<func::FuncDialect>();
32   }
33 
runOnOperation__anonefe362e90111::TestPrintInvalidPass34   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 {
registerTestPrintInvalidPass()52 void registerTestPrintInvalidPass() {
53   PassRegistration<TestPrintInvalidPass>{};
54 }
55 } // namespace mlir
56