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