1//===- TestOpsProperties.h - Test dialect ops in `Properties` ---*- 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#ifndef MLIR_TEST_DIALECT_TEST_TESTOPSPROPERTIES_H
10#define MLIR_TEST_DIALECT_TEST_TESTOPSPROPERTIES_H
11
12#include "TestDialect.h"
13#include "llvm/ADT/SmallVector.h"
14#include <optional>
15
16namespace test {
17// Keep this type distinct from SmallVector<int32_t>, which another test parser
18// uses with the generic FieldParser specialization in a separate translation
19// unit.
20struct KeyValueSpecializedList : llvm::SmallVector<int32_t> {
21 using llvm::SmallVector<int32_t>::SmallVector;
22};
23} // namespace test
24
25namespace mlir {
26// Self-delimiting full specializations remain usable in a keyed prop-dict even
27// when their storage type is optional or container-like.
28template <>
29struct FieldParser<test::KeyValueSpecializedList> {
30 static FailureOr<test::KeyValueSpecializedList> parse(AsmParser &parser) {
31 test::KeyValueSpecializedList values;
32 if (parser.parseCommaSeparatedList(delimiter: AsmParser::Delimiter::Square, parseElementFn: [&]() {
33 int32_t value;
34 if (parser.parseInteger(result&: value))
35 return failure();
36 values.push_back(Elt: value);
37 return success();
38 }))
39 return failure();
40 return values;
41 }
42};
43
44template <>
45struct FieldParser<std::optional<int16_t>> {
46 static FailureOr<std::optional<int16_t>> parse(AsmParser &parser) {
47 if (succeeded(Result: parser.parseOptionalKeyword(keyword: "none")))
48 return std::optional<int16_t>{};
49 int16_t value;
50 if (parser.parseKeyword(keyword: "some") || parser.parseLess() ||
51 parser.parseInteger(result&: value) || parser.parseGreater())
52 return failure();
53 return std::optional<int16_t>{value};
54 }
55};
56} // namespace mlir
57
58namespace test {
59//===----------------------------------------------------------------------===//
60// PropertiesWithCustomPrint
61//===----------------------------------------------------------------------===//
62
63struct PropertiesWithCustomPrint {
64 /// A shared_ptr to a const object is safe: it is equivalent to a value-based
65 /// member. Here the label will be deallocated when the last operation
66 /// refering to it is destroyed. However there is no pool-allocation: this is
67 /// offloaded to the client.
68 std::shared_ptr<const std::string> label;
69 int value;
70 bool operator==(const PropertiesWithCustomPrint &rhs) const {
71 return value == rhs.value && *label == *rhs.label;
72 }
73};
74
75llvm::LogicalResult setPropertiesFromAttribute(
76 PropertiesWithCustomPrint &prop, mlir::Attribute attr,
77 llvm::function_ref<mlir::InFlightDiagnostic()> emitError);
78mlir::DictionaryAttr
79getPropertiesAsAttribute(mlir::MLIRContext *ctx,
80 const PropertiesWithCustomPrint &prop);
81llvm::hash_code computeHash(const PropertiesWithCustomPrint &prop);
82void customPrintProperties(mlir::OpAsmPrinter &p,
83 const PropertiesWithCustomPrint &prop);
84mlir::ParseResult customParseProperties(mlir::OpAsmParser &parser,
85 PropertiesWithCustomPrint &prop);
86
87//===----------------------------------------------------------------------===//
88// MyPropStruct
89//===----------------------------------------------------------------------===//
90namespace test_properties {
91class MyPropStruct {
92public:
93 std::string content;
94 // These three methods are invoked through the `MyStructProperty` wrapper
95 // defined in TestOps.td
96 mlir::Attribute asAttribute(mlir::MLIRContext *ctx) const;
97 static llvm::LogicalResult
98 setFromAttr(MyPropStruct &prop, mlir::Attribute attr,
99 llvm::function_ref<mlir::InFlightDiagnostic()> emitError);
100 llvm::hash_code hash() const;
101 bool operator==(const MyPropStruct &rhs) const {
102 return content == rhs.content;
103 }
104};
105inline llvm::hash_code hash_value(const MyPropStruct &S) { return S.hash(); }
106} // namespace test_properties
107using test_properties::MyPropStruct;
108
109llvm::LogicalResult readFromMlirBytecode(mlir::DialectBytecodeReader &reader,
110 MyPropStruct &prop);
111void writeToMlirBytecode(mlir::DialectBytecodeWriter &writer,
112 MyPropStruct &prop);
113
114//===----------------------------------------------------------------------===//
115// VersionedProperties
116//===----------------------------------------------------------------------===//
117
118struct VersionedProperties {
119 // For the sake of testing, assume that this object was associated to version
120 // 1.2 of the test dialect when having only one int value. In the current
121 // version 2.0, the property has two values. We also assume that the class is
122 // upgrade-able if value2 = 0.
123 int value1;
124 int value2;
125 bool operator==(const VersionedProperties &rhs) const {
126 return value1 == rhs.value1 && value2 == rhs.value2;
127 }
128};
129
130llvm::LogicalResult setPropertiesFromAttribute(
131 VersionedProperties &prop, mlir::Attribute attr,
132 llvm::function_ref<mlir::InFlightDiagnostic()> emitError);
133mlir::DictionaryAttr getPropertiesAsAttribute(mlir::MLIRContext *ctx,
134 const VersionedProperties &prop);
135llvm::hash_code computeHash(const VersionedProperties &prop);
136void customPrintProperties(mlir::OpAsmPrinter &p,
137 const VersionedProperties &prop);
138mlir::ParseResult customParseProperties(mlir::OpAsmParser &parser,
139 VersionedProperties &prop);
140
141//===----------------------------------------------------------------------===//
142// Bytecode Support
143//===----------------------------------------------------------------------===//
144
145llvm::LogicalResult readFromMlirBytecode(mlir::DialectBytecodeReader &reader,
146 llvm::MutableArrayRef<int64_t> prop);
147void writeToMlirBytecode(mlir::DialectBytecodeWriter &writer,
148 llvm::ArrayRef<int64_t> prop);
149} // namespace test
150
151#define GET_OP_CLASSES
152#include "TestOpsProperties.h.inc"
153
154#endif // MLIR_TEST_DIALECT_TEST_TESTOPSPROPERTIES_H
155