Skip to content

Commit df1e01b

Browse files
author
Jeff Niu
authored
[mlir] Add example of printAlias to test dialect (NFC) (#79232)
Follow-up from previous pull request. Motivate the API change with an attribute that decides between sugaring a sub-attribute or using an alias
1 parent 03a61d3 commit df1e01b

File tree

3 files changed

+45
-2
lines changed

3 files changed

+45
-2
lines changed

mlir/test/IR/print-attr-type-aliases.mlir

+14
Original file line numberDiff line numberDiff line change
@@ -54,3 +54,17 @@
5454
// CHECK: #loc1 = loc(fused<memref<1xi32>
5555
// CHECK-NOT: #map
5656
"test.op"() {alias_test = loc(fused<memref<1xi32, affine_map<(d0) -> (d0)>>>["test.mlir":10:8])} : () -> ()
57+
58+
// -----
59+
60+
#unalias_me = "goodbye"
61+
#keep_aliased = "alias_test:dot_in_name"
62+
63+
// CHECK: #test.conditional_alias<hello>
64+
"test.op"() {attr = #test.conditional_alias<"hello">} : () -> ()
65+
// CHECK-NEXT: #test.conditional_alias<#test_encoding>
66+
"test.op"() {attr = #test.conditional_alias<"alias_test:tensor_encoding">} : () -> ()
67+
// CHECK: #test.conditional_alias<goodbye>
68+
"test.op"() {attr = #test.conditional_alias<#unalias_me>} : () -> ()
69+
// CHECK-NEXT: #test.conditional_alias<#test2Ealias>
70+
"test.op"() {attr = #test.conditional_alias<#keep_aliased>} : () -> ()

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

+7-2
Original file line numberDiff line numberDiff line change
@@ -332,7 +332,12 @@ def TestCopyCount : Test_Attr<"TestCopyCount"> {
332332
let assemblyFormat = "`<` $copy_count `>`";
333333
}
334334

335-
336-
335+
def TestConditionalAliasAttr : Test_Attr<"TestConditionalAlias"> {
336+
let mnemonic = "conditional_alias";
337+
let parameters = (ins "mlir::StringAttr":$value);
338+
let assemblyFormat = [{
339+
`<` custom<ConditionalAlias>($value) `>`
340+
}];
341+
}
337342

338343
#endif // TEST_ATTRDEFS

mlir/test/lib/Dialect/Test/TestAttributes.cpp

+24
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,30 @@ llvm::hash_code hash_value(const test::CopyCount &copyCount) {
215215
return llvm::hash_value(copyCount.value);
216216
}
217217
} // namespace test
218+
219+
//===----------------------------------------------------------------------===//
220+
// TestConditionalAliasAttr
221+
//===----------------------------------------------------------------------===//
222+
223+
/// Attempt to parse the conditionally-aliased string attribute as a keyword or
224+
/// string, else try to parse an alias.
225+
static ParseResult parseConditionalAlias(AsmParser &p, StringAttr &value) {
226+
std::string str;
227+
if (succeeded(p.parseOptionalKeywordOrString(&str))) {
228+
value = StringAttr::get(p.getContext(), str);
229+
return success();
230+
}
231+
return p.parseAttribute(value);
232+
}
233+
234+
/// Print the string attribute as an alias if it has one, otherwise print it as
235+
/// a keyword if possible.
236+
static void printConditionalAlias(AsmPrinter &p, StringAttr value) {
237+
if (succeeded(p.printAlias(value)))
238+
return;
239+
p.printKeywordOrString(value);
240+
}
241+
218242
//===----------------------------------------------------------------------===//
219243
// Tablegen Generated Definitions
220244
//===----------------------------------------------------------------------===//

0 commit comments

Comments
 (0)