Skip to content

[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 3 commits into from
Jan 24, 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
14 changes: 14 additions & 0 deletions mlir/test/IR/print-attr-type-aliases.mlir
Original file line number Diff line number Diff line change
Expand Up @@ -54,3 +54,17 @@
// 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])} : () -> ()

// -----

#unalias_me = "goodbye"
#keep_aliased = "alias_test:dot_in_name"

// 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">} : () -> ()
// CHECK: #test.conditional_alias<goodbye>
"test.op"() {attr = #test.conditional_alias<#unalias_me>} : () -> ()
// CHECK-NEXT: #test.conditional_alias<#test2Ealias>
"test.op"() {attr = #test.conditional_alias<#keep_aliased>} : () -> ()
9 changes: 7 additions & 2 deletions mlir/test/lib/Dialect/Test/TestAttrDefs.td
Original file line number Diff line number Diff line change
Expand Up @@ -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
24 changes: 24 additions & 0 deletions mlir/test/lib/Dialect/Test/TestAttributes.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,30 @@ llvm::hash_code hash_value(const test::CopyCount &copyCount) {
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
//===----------------------------------------------------------------------===//
Expand Down