-
Notifications
You must be signed in to change notification settings - Fork 13.5k
[mlir] Add example of printAlias
to test dialect (NFC)
#79232
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
Merged
Merged
Conversation
This file contains hidden or 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
Follow-up from previous pull request.
@llvm/pr-subscribers-mlir Author: Jeff Niu (Mogball) ChangesFollow-up from previous pull request. Motivate the API change with an attribute that decides between sugaring a sub-attribute or using an alias Full diff: https://github.com/llvm/llvm-project/pull/79232.diff 3 Files Affected:
diff --git a/mlir/test/IR/print-attr-type-aliases.mlir b/mlir/test/IR/print-attr-type-aliases.mlir
index a3db1f06009d88e..8b6fc470c799017 100644
--- a/mlir/test/IR/print-attr-type-aliases.mlir
+++ b/mlir/test/IR/print-attr-type-aliases.mlir
@@ -54,3 +54,10 @@
// CHECK: #loc1 = loc(fused<memref<1xi32>
// CHECK-NOT: #map
"test.op"() {alias_test = loc(fused<memref<1xi32, affine_map<(d0) -> (d0)>>>["test.mlir":10:8])} : () -> ()
+
+// -----
+
+// CHECK: #test.conditional_alias<hello>
+"test.op"() {attr = #test.conditional_alias<"hello">} : () -> ()
+// CHECK-NEXT: #test.conditional_alias<#test_encoding>
+"test.op"() {attr = #test.conditional_alias<"alias_test:tensor_encoding">} : () -> ()
diff --git a/mlir/test/lib/Dialect/Test/TestAttrDefs.td b/mlir/test/lib/Dialect/Test/TestAttrDefs.td
index 945c54c04d47ce8..40f035a3e3a4e5e 100644
--- a/mlir/test/lib/Dialect/Test/TestAttrDefs.td
+++ b/mlir/test/lib/Dialect/Test/TestAttrDefs.td
@@ -332,7 +332,12 @@ def TestCopyCount : Test_Attr<"TestCopyCount"> {
let assemblyFormat = "`<` $copy_count `>`";
}
-
-
+def TestConditionalAliasAttr : Test_Attr<"TestConditionalAlias"> {
+ let mnemonic = "conditional_alias";
+ let parameters = (ins "mlir::StringAttr":$value);
+ let assemblyFormat = [{
+ `<` custom<ConditionalAlias>($value) `>`
+ }];
+}
#endif // TEST_ATTRDEFS
diff --git a/mlir/test/lib/Dialect/Test/TestAttributes.cpp b/mlir/test/lib/Dialect/Test/TestAttributes.cpp
index c240354e5d99044..1628da46de521fb 100644
--- a/mlir/test/lib/Dialect/Test/TestAttributes.cpp
+++ b/mlir/test/lib/Dialect/Test/TestAttributes.cpp
@@ -215,6 +215,28 @@ llvm::hash_code hash_value(const test::CopyCount ©Count) {
return llvm::hash_value(copyCount.value);
}
} // namespace test
+
+//===----------------------------------------------------------------------===//
+// TestConditionalAliasAttr
+//===----------------------------------------------------------------------===//
+
+/// Attempt to parse the conditionally-aliased string attribute as a keyword or string, else try to parse an alias.
+static ParseResult parseConditionalAlias(AsmParser &p, StringAttr &value) {
+ std::string str;
+ if (succeeded(p.parseOptionalKeywordOrString(&str))) {
+ value = StringAttr::get(p.getContext(), str);
+ return success();
+ }
+ return p.parseAttribute(value);
+}
+
+/// Print the string attribute as an alias if it has one, otherwise print it as a keyword if possible.
+static void printConditionalAlias(AsmPrinter &p, StringAttr value) {
+ if (succeeded(p.printAlias(value)))
+ return;
+ p.printKeywordOrString(value);
+}
+
//===----------------------------------------------------------------------===//
// Tablegen Generated Definitions
//===----------------------------------------------------------------------===//
|
✅ With the latest revision this PR passed the C/C++ code formatter. |
joker-eph
reviewed
Jan 24, 2024
joker-eph
approved these changes
Jan 24, 2024
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.
Follow-up from previous pull request. Motivate the API change with an attribute that decides between sugaring a sub-attribute or using an alias