-
Notifications
You must be signed in to change notification settings - Fork 12.7k
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
Conversation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
…/Attr definitions
@llvm/pr-subscribers-mlir Author: Mehdi Amini (joker-eph) ChangesFull diff: https://github.com/llvm/llvm-project/pull/117719.diff 3 Files Affected:
diff --git a/mlir/test/lib/Dialect/Test/TestAttrDefs.td b/mlir/test/lib/Dialect/Test/TestAttrDefs.td
index 907184b2e1ce4c..0fd272f85d39bd 100644
--- a/mlir/test/lib/Dialect/Test/TestAttrDefs.td
+++ b/mlir/test/lib/Dialect/Test/TestAttrDefs.td
@@ -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);
diff --git a/mlir/test/mlir-tblgen/attr-or-type-format-roundtrip.mlir b/mlir/test/mlir-tblgen/attr-or-type-format-roundtrip.mlir
index f0d1571d583877..77b94698a278e9 100644
--- a/mlir/test/mlir-tblgen/attr-or-type-format-roundtrip.mlir
+++ b/mlir/test/mlir-tblgen/attr-or-type-format-roundtrip.mlir
@@ -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
diff --git a/mlir/tools/mlir-tblgen/EnumsGen.cpp b/mlir/tools/mlir-tblgen/EnumsGen.cpp
index 47ffd291cae687..d11aa9b27c2d86 100644
--- a/mlir/tools/mlir-tblgen/EnumsGen.cpp
+++ b/mlir/tools/mlir-tblgen/EnumsGen.cpp
@@ -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 {
|
@llvm/pr-subscribers-mlir-core Author: Mehdi Amini (joker-eph) ChangesFull diff: https://github.com/llvm/llvm-project/pull/117719.diff 3 Files Affected:
diff --git a/mlir/test/lib/Dialect/Test/TestAttrDefs.td b/mlir/test/lib/Dialect/Test/TestAttrDefs.td
index 907184b2e1ce4c..0fd272f85d39bd 100644
--- a/mlir/test/lib/Dialect/Test/TestAttrDefs.td
+++ b/mlir/test/lib/Dialect/Test/TestAttrDefs.td
@@ -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);
diff --git a/mlir/test/mlir-tblgen/attr-or-type-format-roundtrip.mlir b/mlir/test/mlir-tblgen/attr-or-type-format-roundtrip.mlir
index f0d1571d583877..77b94698a278e9 100644
--- a/mlir/test/mlir-tblgen/attr-or-type-format-roundtrip.mlir
+++ b/mlir/test/mlir-tblgen/attr-or-type-format-roundtrip.mlir
@@ -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
diff --git a/mlir/tools/mlir-tblgen/EnumsGen.cpp b/mlir/tools/mlir-tblgen/EnumsGen.cpp
index 47ffd291cae687..d11aa9b27c2d86 100644
--- a/mlir/tools/mlir-tblgen/EnumsGen.cpp
+++ b/mlir/tools/mlir-tblgen/EnumsGen.cpp
@@ -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 {
|
Mogball
approved these changes
Nov 28, 2024
kuhar
added a commit
to iree-org/iree
that referenced
this pull request
Nov 28, 2024
…19334) Still carrying a revert for 1004865f1ca41a9581da8747f34b29862d3ebc3d and a cherry pick for llvm/llvm-project#116650. Removed `FieldParser`s for optional enums that get autogenerated as of llvm/llvm-project#117719.
giacs-epic
pushed a commit
to giacs-epic/iree
that referenced
this pull request
Dec 4, 2024
…ree-org#19334) Still carrying a revert for 1004865f1ca41a9581da8747f34b29862d3ebc3d and a cherry pick for llvm/llvm-project#116650. Removed `FieldParser`s for optional enums that get autogenerated as of llvm/llvm-project#117719. Signed-off-by: Giacomo Serafini <[email protected]>
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
No description provided.