| 1 | //===- TestTypes.h - MLIR Test Dialect Types --------------------*- C++ -*-===// |
| 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 file contains types defined by the TestDialect for testing various |
| 10 | // features of MLIR. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #ifndef MLIR_TESTTYPES_H |
| 15 | #define MLIR_TESTTYPES_H |
| 16 | |
| 17 | #include <optional> |
| 18 | #include <tuple> |
| 19 | |
| 20 | #include "mlir/Dialect/Bufferization/IR/BufferizationTypeInterfaces.h" |
| 21 | #include "mlir/IR/Diagnostics.h" |
| 22 | #include "mlir/IR/Dialect.h" |
| 23 | #include "mlir/IR/DialectImplementation.h" |
| 24 | #include "mlir/IR/Operation.h" |
| 25 | #include "mlir/IR/Types.h" |
| 26 | |
| 27 | namespace test { |
| 28 | class TestAttrWithFormatAttr; |
| 29 | |
| 30 | /// FieldInfo represents a field in the StructType data type. It is used as a |
| 31 | /// parameter in TestTypeDefs.td. |
| 32 | struct FieldInfo { |
| 33 | llvm::StringRef name; |
| 34 | mlir::Type type; |
| 35 | |
| 36 | // Custom allocation called from generated constructor code |
| 37 | FieldInfo allocateInto(mlir::TypeStorageAllocator &alloc) const { |
| 38 | return FieldInfo{.name: alloc.copyInto(str: name), .type: type}; |
| 39 | } |
| 40 | }; |
| 41 | |
| 42 | /// A custom type for a test type parameter. |
| 43 | struct CustomParam { |
| 44 | int value; |
| 45 | |
| 46 | bool operator==(const CustomParam &other) const { |
| 47 | return other.value == value; |
| 48 | } |
| 49 | }; |
| 50 | |
| 51 | inline llvm::hash_code hash_value(const test::CustomParam ¶m) { |
| 52 | return llvm::hash_value(value: param.value); |
| 53 | } |
| 54 | |
| 55 | } // namespace test |
| 56 | |
| 57 | namespace mlir { |
| 58 | template <> |
| 59 | struct FieldParser<test::CustomParam> { |
| 60 | static FailureOr<test::CustomParam> parse(AsmParser &parser) { |
| 61 | auto value = FieldParser<int>::parse(parser); |
| 62 | if (failed(Result: value)) |
| 63 | return failure(); |
| 64 | return test::CustomParam{.value: *value}; |
| 65 | } |
| 66 | }; |
| 67 | |
| 68 | inline mlir::AsmPrinter &operator<<(mlir::AsmPrinter &printer, |
| 69 | test::CustomParam param) { |
| 70 | return printer << param.value; |
| 71 | } |
| 72 | |
| 73 | /// Overload the attribute parameter parser for optional integers. |
| 74 | template <> |
| 75 | struct FieldParser<std::optional<int>> { |
| 76 | static FailureOr<std::optional<int>> parse(AsmParser &parser) { |
| 77 | std::optional<int> value; |
| 78 | value.emplace(); |
| 79 | OptionalParseResult result = parser.parseOptionalInteger(result&: *value); |
| 80 | if (result.has_value()) { |
| 81 | if (succeeded(Result: *result)) |
| 82 | return value; |
| 83 | return failure(); |
| 84 | } |
| 85 | value.reset(); |
| 86 | return value; |
| 87 | } |
| 88 | }; |
| 89 | } // namespace mlir |
| 90 | |
| 91 | #include "TestTypeInterfaces.h.inc" |
| 92 | |
| 93 | namespace test { |
| 94 | |
| 95 | /// Storage for simple named recursive types, where the type is identified by |
| 96 | /// its name and can "contain" another type, including itself. |
| 97 | struct TestRecursiveTypeStorage : public ::mlir::TypeStorage { |
| 98 | using KeyTy = ::llvm::StringRef; |
| 99 | |
| 100 | explicit TestRecursiveTypeStorage(::llvm::StringRef key) : name(key) {} |
| 101 | |
| 102 | bool operator==(const KeyTy &other) const { return name == other; } |
| 103 | |
| 104 | static TestRecursiveTypeStorage * |
| 105 | construct(::mlir::TypeStorageAllocator &allocator, const KeyTy &key) { |
| 106 | return new (allocator.allocate<TestRecursiveTypeStorage>()) |
| 107 | TestRecursiveTypeStorage(allocator.copyInto(str: key)); |
| 108 | } |
| 109 | |
| 110 | ::llvm::LogicalResult mutate(::mlir::TypeStorageAllocator &allocator, |
| 111 | ::mlir::Type newBody) { |
| 112 | // Cannot set a different body than before. |
| 113 | if (body && body != newBody) |
| 114 | return ::mlir::failure(); |
| 115 | |
| 116 | body = newBody; |
| 117 | return ::mlir::success(); |
| 118 | } |
| 119 | |
| 120 | ::llvm::StringRef name; |
| 121 | ::mlir::Type body; |
| 122 | }; |
| 123 | |
| 124 | /// Simple recursive type identified by its name and pointing to another named |
| 125 | /// type, potentially itself. This requires the body to be mutated separately |
| 126 | /// from type creation. |
| 127 | class TestRecursiveType |
| 128 | : public ::mlir::Type::TypeBase<TestRecursiveType, ::mlir::Type, |
| 129 | TestRecursiveTypeStorage, |
| 130 | ::mlir::TypeTrait::IsMutable> { |
| 131 | public: |
| 132 | using Base::Base; |
| 133 | |
| 134 | static constexpr ::mlir::StringLiteral name = "test.recursive" ; |
| 135 | |
| 136 | static TestRecursiveType get(::mlir::MLIRContext *ctx, |
| 137 | ::llvm::StringRef name) { |
| 138 | return Base::get(ctx, name); |
| 139 | } |
| 140 | |
| 141 | /// Body getter and setter. |
| 142 | ::llvm::LogicalResult setBody(Type body) { return Base::mutate(body); } |
| 143 | ::mlir::Type getBody() const { return getImpl()->body; } |
| 144 | |
| 145 | /// Name/key getter. |
| 146 | ::llvm::StringRef getName() { return getImpl()->name; } |
| 147 | }; |
| 148 | |
| 149 | } // namespace test |
| 150 | |
| 151 | #define GET_TYPEDEF_CLASSES |
| 152 | #include "TestTypeDefs.h.inc" |
| 153 | |
| 154 | #endif // MLIR_TESTTYPES_H |
| 155 | |