Skip to content

[mlir] Make TypedStrAttr actually enforce the string type. #124770

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 29, 2025
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: 8 additions & 6 deletions mlir/include/mlir/IR/CommonAttrConstraints.td
Original file line number Diff line number Diff line change
Expand Up @@ -334,17 +334,19 @@ class StringBasedAttr<Pred condition, string descr> : Attr<condition, descr> {
let valueType = NoneType;
}

def StrAttr : StringBasedAttr<CPred<"::llvm::isa<::mlir::StringAttr>($_self)">,
"string attribute">;
def StrAttrPred : CPred<"::llvm::isa<::mlir::StringAttr>($_self)">;

def StrAttr : StringBasedAttr<StrAttrPred, "string attribute">;

// A string attribute that represents the name of a symbol.
def SymbolNameAttr : StringBasedAttr<CPred<"::llvm::isa<::mlir::StringAttr>($_self)">,
"string attribute">;
def SymbolNameAttr : StringBasedAttr<StrAttrPred, "string attribute">;

// String attribute that has a specific value type.
class TypedStrAttr<Type ty>
: StringBasedAttr<CPred<"::llvm::isa<::mlir::StringAttr>($_self)">,
"string attribute"> {
: StringBasedAttr<And<[StrAttrPred,
SubstLeaves<"$_self", "::mlir::cast<StringAttr>($_self).getType()",
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we assume at this point that the cast will succeed or should I test that $_self is StringAttr as well?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I suspect that the order of the checks here matters. I.e., it should be safe to assume that it is a string attr. But to be sure, you could try to add another test case for test.string_attr_with_type in generic op syntax, where the attr is not a string attr.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I had, since opening the PR, encountered a crash with the first version, which didn't test for StringAttr. The second commit fixed that but didn't add a test. A just pushed a third commit that adds a test that crashes without the fix from the second commit (but passes with the fix). Thanks for suggesting that!

ty.predicate>]>,
"string attribute of " # ty.summary> {
let valueType = ty;
}

Expand Down
27 changes: 23 additions & 4 deletions mlir/test/IR/attribute.mlir
Original file line number Diff line number Diff line change
Expand Up @@ -416,10 +416,29 @@ func.func @non_type_in_type_array_attr_fail() {
// Test StringAttr with custom type
//===----------------------------------------------------------------------===//

// CHECK-LABEL: func @string_attr_custom_type
func.func @string_attr_custom_type() {
// CHECK: "string_data" : !foo.string
test.string_attr_with_type "string_data" : !foo.string
// CHECK-LABEL: func @string_attr_custom_type_valid
func.func @string_attr_custom_type_valid() {
// CHECK: "string_data" : i64
test.string_attr_with_type "string_data" : i64
return
}

// -----

func.func @string_attr_custom_type_invalid() {
// expected-error @+1 {{'attr' failed to satisfy constraint: string attribute of integer}}
test.string_attr_with_type "string_data" : f32
return
}

// -----

// CHECK-LABEL: func @string_attr_custom_mixed_type
func.func @string_attr_custom_mixed_type() {
// CHECK: "string_data" : i64
test.string_attr_with_mixed_type "string_data" : i64
// CHECK: 42 : i64
test.string_attr_with_mixed_type 42 : i64
return
}

Expand Down
11 changes: 9 additions & 2 deletions mlir/test/lib/Dialect/Test/TestOps.td
Original file line number Diff line number Diff line change
Expand Up @@ -193,8 +193,15 @@ def TypeArrayAttrWithDefaultOp : TEST_Op<"type_array_attr_with_default"> {
let arguments = (ins DefaultValuedAttr<TypeArrayAttr, "{}">:$attr);
}

def TypeStringAttrWithTypeOp : TEST_Op<"string_attr_with_type"> {
let arguments = (ins TypedStrAttr<AnyType>:$attr);
def TypedStringAttrWithTypeOp : TEST_Op<"string_attr_with_type"> {
let arguments = (ins TypedStrAttr<AnyInteger>:$attr);
let assemblyFormat = "$attr attr-dict";
}

def TypedStringAttrWithMixedTypeOp : TEST_Op<"string_attr_with_mixed_type"> {
let arguments = (ins
AnyAttrOf<[TypedStrAttr<AnyInteger>, I64Attr]>:$attr
);
let assemblyFormat = "$attr attr-dict";
}

Expand Down