Skip to content

Commit db273c6

Browse files
authored
[MLIR][ODS] Add support for wrapping enums with std::optional in Type/Attr definitions (#117719)
1 parent 1f422dc commit db273c6

File tree

3 files changed

+34
-1
lines changed

3 files changed

+34
-1
lines changed

mlir/test/lib/Dialect/Test/TestAttrDefs.td

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -226,6 +226,12 @@ def TestAttrWithOptionalUnsigned : Test_Attr<"TestAttrWithOptionalUnsigned"> {
226226
let mnemonic = "attr_with_optional_unsigned";
227227
}
228228

229+
def TestAttrWithOptionalEnum : Test_Attr<"TestAttrWithOptionalEnum"> {
230+
let parameters = (ins OptionalParameter<"std::optional<SimpleEnum>">:$value);
231+
let assemblyFormat = "`<` $value `>`";
232+
let mnemonic = "attr_with_optional_enum";
233+
}
234+
229235
def TestAttrUgly : Test_Attr<"TestAttrUgly"> {
230236
let parameters = (ins "::mlir::Attribute":$attr);
231237

mlir/test/mlir-tblgen/attr-or-type-format-roundtrip.mlir

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,13 @@ attributes {
2626
// CHECK: #test.attr_with_optional_signed<-9223372036854775808>
2727
attr9 = #test.attr_with_optional_signed<-9223372036854775808>,
2828
// CHECK: #test.attr_with_optional_unsigned<18446744073709551615>
29-
attr_10 = #test.attr_with_optional_unsigned<18446744073709551615>
29+
attr_10 = #test.attr_with_optional_unsigned<18446744073709551615>,
30+
// CHECK: #test.attr_with_optional_enum<>
31+
attr_11 = #test.attr_with_optional_enum<>,
32+
// CHECK: #test.attr_with_optional_enum<a>
33+
attr_12 = #test.attr_with_optional_enum<a>,
34+
// CHECK: #test.attr_with_optional_enum<b>
35+
attr_13 = #test.attr_with_optional_enum<b>
3036
}
3137

3238
// CHECK-LABEL: @test_roundtrip_default_parsers_struct

mlir/tools/mlir-tblgen/EnumsGen.cpp

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,27 @@ struct FieldParser<{0}, {0}> {{
101101
return parser.emitError(loc, "invalid {2} specification: ") << enumKeyword;
102102
}
103103
};
104+
105+
/// Support for std::optional, useful in attribute/type definition where the enum is
106+
/// used as:
107+
///
108+
/// let parameters = (ins OptionalParameter<"std::optional<TheEnumName>">:$value);
109+
template<>
110+
struct FieldParser<std::optional<{0}>, std::optional<{0}>> {{
111+
template <typename ParserT>
112+
static FailureOr<std::optional<{0}>> parse(ParserT &parser) {{
113+
// Parse the keyword/string containing the enum.
114+
std::string enumKeyword;
115+
auto loc = parser.getCurrentLocation();
116+
if (failed(parser.parseOptionalKeywordOrString(&enumKeyword)))
117+
return std::optional<{0}>{{};
118+
119+
// Symbolize the keyword.
120+
if (::std::optional<{0}> attr = {1}::symbolizeEnum<{0}>(enumKeyword))
121+
return attr;
122+
return parser.emitError(loc, "invalid {2} specification: ") << enumKeyword;
123+
}
124+
};
104125
} // namespace mlir
105126
106127
namespace llvm {

0 commit comments

Comments
 (0)