1 //===- TestPrintInvalid.cpp - Test printing invalid ops -------------------===//
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
7 //===----------------------------------------------------------------------===//
9 // This pass creates and prints to the standard output an invalid operation and
12 //===----------------------------------------------------------------------===//
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"
22 struct TestPrintInvalidPass
23 : public PassWrapper
<TestPrintInvalidPass
, OperationPass
<ModuleOp
>> {
24 MLIR_DEFINE_EXPLICIT_INTERNAL_INLINE_TYPE_ID(TestPrintInvalidPass
)
26 StringRef
getArgument() const final
{ return "test-print-invalid"; }
27 StringRef
getDescription() const final
{
28 return "Test printing invalid ops.";
30 void getDependentDialects(DialectRegistry
®istry
) const override
{
31 registry
.insert
<func::FuncDialect
>();
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";
52 void registerTestPrintInvalidPass() {
53 PassRegistration
<TestPrintInvalidPass
>{};