1 //===- TestTypes.cpp - Test passes for MLIR types -------------------------===//
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 //===----------------------------------------------------------------------===//
10 #include "TestDialect.h"
11 #include "mlir/Pass/Pass.h"
17 struct TestRecursiveTypesPass
18 : public PassWrapper
<TestRecursiveTypesPass
, OperationPass
<func::FuncOp
>> {
19 MLIR_DEFINE_EXPLICIT_INTERNAL_INLINE_TYPE_ID(TestRecursiveTypesPass
)
21 LogicalResult
createIRWithTypes();
23 StringRef
getArgument() const final
{ return "test-recursive-types"; }
24 StringRef
getDescription() const final
{
25 return "Test support for recursive types";
27 void runOnOperation() override
{
28 func::FuncOp func
= getOperation();
30 // Just make sure recursive types are printed and parsed.
31 if (func
.getName() == "roundtrip")
34 // Create a recursive type and print it as a part of a dummy op.
35 if (func
.getName() == "create") {
36 if (failed(createIRWithTypes()))
42 func
.emitOpError() << "unexpected function name";
48 LogicalResult
TestRecursiveTypesPass::createIRWithTypes() {
49 MLIRContext
*ctx
= &getContext();
50 func::FuncOp func
= getOperation();
51 auto type
= TestRecursiveType::get(ctx
, "some_long_and_unique_name");
52 if (failed(type
.setBody(type
)))
53 return func
.emitError("expected to be able to set the type body");
55 // Setting the same body is fine.
56 if (failed(type
.setBody(type
)))
57 return func
.emitError(
58 "expected to be able to set the type body to the same value");
60 // Setting a different body is not.
61 if (succeeded(type
.setBody(IndexType::get(ctx
))))
62 return func
.emitError(
63 "not expected to be able to change function body more than once");
65 // Expecting to get the same type for the same name.
66 auto other
= TestRecursiveType::get(ctx
, "some_long_and_unique_name");
68 return func
.emitError("expected type name to be the uniquing key");
70 // Create the op to check how the type is printed.
71 OperationState
state(func
.getLoc(), "test.dummy_type_test_op");
73 func
.getBody().front().push_front(Operation::create(state
));
81 void registerTestRecursiveTypesPass() {
82 PassRegistration
<TestRecursiveTypesPass
>();