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
19using namespace mlir;
20
21namespace {
22struct 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 &registry) 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 = func::FuncOp::create(
38 builder, location: loc, name: "test",
39 type: FunctionType::get(context: getOperation().getContext(), inputs: {}, results: {}));
40 funcOp.addEntryBlock();
41 // The created function is invalid because there is no return op.
42 llvm::outs() << "Invalid operation:\n" << funcOp << "\n";
43 builder.setInsertionPointToEnd(&funcOp.getBody().front());
44 func::ReturnOp::create(builder, location: loc);
45 // Now this function is valid.
46 llvm::outs() << "Valid operation:\n" << funcOp << "\n";
47 funcOp.erase();
48 }
49};
50} // namespace
51
52namespace mlir {
53void registerTestPrintInvalidPass() {
54 PassRegistration<TestPrintInvalidPass>{};
55}
56} // namespace mlir
57