Skip to content

Commit 8d6b241

Browse files
[mlir] Make TypedStrAttr actually enforce the string type. (llvm#124770)
The tablgen definition `TypedStrAttr` is an attribute constraints that is meant to restrict the type of a `StringAttr` to the type given as parameter. However, the definition did not previously restrict the type; any `StringAttr` was accepted. This PR makes the definition actually enforce the type. To test the constraints, the PR also changes the test op that was previously used to test this constraint such that the enforced type is `AnyInteger` instead of `AnyType`. The latter allowed any type, so not enforcing that constraint had no observable effect. The PR then adds a test case with a wrong type and ensures that diagnostics are produced. Signed-off-by: Ingo Müller <[email protected]>
1 parent 2f40145 commit 8d6b241

File tree

3 files changed

+40
-12
lines changed

3 files changed

+40
-12
lines changed

mlir/include/mlir/IR/CommonAttrConstraints.td

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -334,17 +334,19 @@ class StringBasedAttr<Pred condition, string descr> : Attr<condition, descr> {
334334
let valueType = NoneType;
335335
}
336336

337-
def StrAttr : StringBasedAttr<CPred<"::llvm::isa<::mlir::StringAttr>($_self)">,
338-
"string attribute">;
337+
def StrAttrPred : CPred<"::llvm::isa<::mlir::StringAttr>($_self)">;
338+
339+
def StrAttr : StringBasedAttr<StrAttrPred, "string attribute">;
339340

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

344344
// String attribute that has a specific value type.
345345
class TypedStrAttr<Type ty>
346-
: StringBasedAttr<CPred<"::llvm::isa<::mlir::StringAttr>($_self)">,
347-
"string attribute"> {
346+
: StringBasedAttr<And<[StrAttrPred,
347+
SubstLeaves<"$_self", "::mlir::cast<StringAttr>($_self).getType()",
348+
ty.predicate>]>,
349+
"string attribute of " # ty.summary> {
348350
let valueType = ty;
349351
}
350352

mlir/test/IR/attribute.mlir

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -416,10 +416,29 @@ func.func @non_type_in_type_array_attr_fail() {
416416
// Test StringAttr with custom type
417417
//===----------------------------------------------------------------------===//
418418

419-
// CHECK-LABEL: func @string_attr_custom_type
420-
func.func @string_attr_custom_type() {
421-
// CHECK: "string_data" : !foo.string
422-
test.string_attr_with_type "string_data" : !foo.string
419+
// CHECK-LABEL: func @string_attr_custom_type_valid
420+
func.func @string_attr_custom_type_valid() {
421+
// CHECK: "string_data" : i64
422+
test.string_attr_with_type "string_data" : i64
423+
return
424+
}
425+
426+
// -----
427+
428+
func.func @string_attr_custom_type_invalid() {
429+
// expected-error @+1 {{'attr' failed to satisfy constraint: string attribute of integer}}
430+
test.string_attr_with_type "string_data" : f32
431+
return
432+
}
433+
434+
// -----
435+
436+
// CHECK-LABEL: func @string_attr_custom_mixed_type
437+
func.func @string_attr_custom_mixed_type() {
438+
// CHECK: "string_data" : i64
439+
test.string_attr_with_mixed_type "string_data" : i64
440+
// CHECK: 42 : i64
441+
test.string_attr_with_mixed_type 42 : i64
423442
return
424443
}
425444

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

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -193,8 +193,15 @@ def TypeArrayAttrWithDefaultOp : TEST_Op<"type_array_attr_with_default"> {
193193
let arguments = (ins DefaultValuedAttr<TypeArrayAttr, "{}">:$attr);
194194
}
195195

196-
def TypeStringAttrWithTypeOp : TEST_Op<"string_attr_with_type"> {
197-
let arguments = (ins TypedStrAttr<AnyType>:$attr);
196+
def TypedStringAttrWithTypeOp : TEST_Op<"string_attr_with_type"> {
197+
let arguments = (ins TypedStrAttr<AnyInteger>:$attr);
198+
let assemblyFormat = "$attr attr-dict";
199+
}
200+
201+
def TypedStringAttrWithMixedTypeOp : TEST_Op<"string_attr_with_mixed_type"> {
202+
let arguments = (ins
203+
AnyAttrOf<[TypedStrAttr<AnyInteger>, I64Attr]>:$attr
204+
);
198205
let assemblyFormat = "$attr attr-dict";
199206
}
200207

0 commit comments

Comments
 (0)