Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[MLIR][ODS] Add support for wrapping enums with std::optional in Type/Attr definitions #117719

Merged
merged 1 commit into from
Nov 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions mlir/test/lib/Dialect/Test/TestAttrDefs.td
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,12 @@ def TestAttrWithOptionalUnsigned : Test_Attr<"TestAttrWithOptionalUnsigned"> {
let mnemonic = "attr_with_optional_unsigned";
}

def TestAttrWithOptionalEnum : Test_Attr<"TestAttrWithOptionalEnum"> {
let parameters = (ins OptionalParameter<"std::optional<SimpleEnum>">:$value);
let assemblyFormat = "`<` $value `>`";
let mnemonic = "attr_with_optional_enum";
}

def TestAttrUgly : Test_Attr<"TestAttrUgly"> {
let parameters = (ins "::mlir::Attribute":$attr);

Expand Down
8 changes: 7 additions & 1 deletion mlir/test/mlir-tblgen/attr-or-type-format-roundtrip.mlir
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,13 @@ attributes {
// CHECK: #test.attr_with_optional_signed<-9223372036854775808>
attr9 = #test.attr_with_optional_signed<-9223372036854775808>,
// CHECK: #test.attr_with_optional_unsigned<18446744073709551615>
attr_10 = #test.attr_with_optional_unsigned<18446744073709551615>
attr_10 = #test.attr_with_optional_unsigned<18446744073709551615>,
// CHECK: #test.attr_with_optional_enum<>
attr_11 = #test.attr_with_optional_enum<>,
// CHECK: #test.attr_with_optional_enum<a>
attr_12 = #test.attr_with_optional_enum<a>,
// CHECK: #test.attr_with_optional_enum<b>
attr_13 = #test.attr_with_optional_enum<b>
}

// CHECK-LABEL: @test_roundtrip_default_parsers_struct
Expand Down
21 changes: 21 additions & 0 deletions mlir/tools/mlir-tblgen/EnumsGen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,27 @@ struct FieldParser<{0}, {0}> {{
return parser.emitError(loc, "invalid {2} specification: ") << enumKeyword;
}
};

/// Support for std::optional, useful in attribute/type definition where the enum is
/// used as:
///
/// let parameters = (ins OptionalParameter<"std::optional<TheEnumName>">:$value);
template<>
struct FieldParser<std::optional<{0}>, std::optional<{0}>> {{
template <typename ParserT>
static FailureOr<std::optional<{0}>> parse(ParserT &parser) {{
// Parse the keyword/string containing the enum.
std::string enumKeyword;
auto loc = parser.getCurrentLocation();
if (failed(parser.parseOptionalKeywordOrString(&enumKeyword)))
return std::optional<{0}>{{};

// Symbolize the keyword.
if (::std::optional<{0}> attr = {1}::symbolizeEnum<{0}>(enumKeyword))
return attr;
return parser.emitError(loc, "invalid {2} specification: ") << enumKeyword;
}
};
} // namespace mlir

namespace llvm {
Expand Down