[lldb] Upstream a few remaining Triple::XROS patches (#126335)
[llvm-project.git] / mlir / test / lib / IR / TestTypes.cpp
blobc6bce111d3ea7f7ce26b23da6ce2a2b805c71662
1 //===- TestTypes.cpp - Test passes for MLIR types -------------------------===//
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 //===----------------------------------------------------------------------===//
9 #include "TestTypes.h"
10 #include "TestDialect.h"
11 #include "mlir/Dialect/Func/IR/FuncOps.h"
12 #include "mlir/Pass/Pass.h"
14 using namespace mlir;
15 using namespace test;
17 namespace {
18 struct TestRecursiveTypesPass
19 : public PassWrapper<TestRecursiveTypesPass, OperationPass<func::FuncOp>> {
20 MLIR_DEFINE_EXPLICIT_INTERNAL_INLINE_TYPE_ID(TestRecursiveTypesPass)
22 LogicalResult createIRWithTypes();
24 StringRef getArgument() const final { return "test-recursive-types"; }
25 StringRef getDescription() const final {
26 return "Test support for recursive types";
28 void runOnOperation() override {
29 func::FuncOp func = getOperation();
31 // Just make sure recursive types are printed and parsed.
32 if (func.getName() == "roundtrip")
33 return;
35 // Create a recursive type and print it as a part of a dummy op.
36 if (func.getName() == "create") {
37 if (failed(createIRWithTypes()))
38 signalPassFailure();
39 return;
42 // Unknown key.
43 func.emitOpError() << "unexpected function name";
44 signalPassFailure();
47 } // namespace
49 LogicalResult TestRecursiveTypesPass::createIRWithTypes() {
50 MLIRContext *ctx = &getContext();
51 func::FuncOp func = getOperation();
52 auto type = TestRecursiveType::get(ctx, "some_long_and_unique_name");
53 if (failed(type.setBody(type)))
54 return func.emitError("expected to be able to set the type body");
56 // Setting the same body is fine.
57 if (failed(type.setBody(type)))
58 return func.emitError(
59 "expected to be able to set the type body to the same value");
61 // Setting a different body is not.
62 if (succeeded(type.setBody(IndexType::get(ctx))))
63 return func.emitError(
64 "not expected to be able to change function body more than once");
66 // Expecting to get the same type for the same name.
67 auto other = TestRecursiveType::get(ctx, "some_long_and_unique_name");
68 if (type != other)
69 return func.emitError("expected type name to be the uniquing key");
71 // Create the op to check how the type is printed.
72 OperationState state(func.getLoc(), "test.dummy_type_test_op");
73 state.addTypes(type);
74 func.getBody().front().push_front(Operation::create(state));
76 return success();
79 namespace mlir {
80 namespace test {
82 void registerTestRecursiveTypesPass() {
83 PassRegistration<TestRecursiveTypesPass>();
86 } // namespace test
87 } // namespace mlir