Skip to content

Commit 6f5bffd

Browse files
authored
[mlir-tblgen] Relax builder ambiguity check (#118310)
The mlir-tblgen tool prevents the parameter of the build() constructor for the first default-valued attribute of an operation from having a default value to avoid ambiguity with the corresponding build() constructor taking unwrapped value. However it does so even when earlier wrapped unwrappable attribute would lift the ambiguity. This commit relax the logic accordingly, which allows to remove a manual constructor in Arith dialect.
1 parent 72aefbb commit 6f5bffd

File tree

4 files changed

+24
-31
lines changed

4 files changed

+24
-31
lines changed

mlir/include/mlir/Dialect/Arith/IR/ArithOps.td

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1556,15 +1556,6 @@ def Arith_CmpFOp : Arith_CompareOp<"cmpf",
15561556
static arith::CmpFPredicate getPredicateByName(StringRef name);
15571557
}];
15581558

1559-
let builders = [
1560-
OpBuilder<(ins "::mlir::arith::CmpFPredicateAttr":$predicate,
1561-
"Value":$lhs, "Value":$rhs), [{
1562-
build($_builder, $_state, predicate, lhs, rhs,
1563-
mlir::arith::FastMathFlagsAttr::get($_builder.getContext(),
1564-
mlir::arith::FastMathFlags::none));
1565-
}]>
1566-
];
1567-
15681559
let hasFolder = 1;
15691560
let hasCanonicalizer = 1;
15701561
let assemblyFormat = [{ $predicate `,` $lhs `,` $rhs (`fastmath` `` $fastmath^)?

mlir/include/mlir/Dialect/Tosa/IR/TosaOps.td

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1905,24 +1905,6 @@ def Tosa_RescaleOp: Tosa_Op<"rescale", [Pure,
19051905
Tosa_Tensor:$output
19061906
);
19071907

1908-
// Custom builder that does not require optional input_unsigned and
1909-
// output_unsigned.
1910-
let builders = [
1911-
OpBuilder<(ins "::mlir::Type":$output,
1912-
"::mlir::Value":$input,
1913-
"::mlir::IntegerAttr":$input_zp,
1914-
"::mlir::IntegerAttr":$output_zp,
1915-
"::mlir::DenseI32ArrayAttr":$multiplier,
1916-
"::mlir::DenseI8ArrayAttr":$shift,
1917-
"::mlir::BoolAttr":$scale32,
1918-
"::mlir::BoolAttr":$double_round,
1919-
"::mlir::BoolAttr":$per_channel), [{
1920-
auto FalseAttr = BoolAttr::get($_builder.getContext(), false);
1921-
build($_builder, $_state, output, input, input_zp, output_zp, multiplier,
1922-
shift, scale32, double_round, per_channel, FalseAttr, FalseAttr);
1923-
}]>
1924-
];
1925-
19261908
let assemblyFormat = "operands attr-dict `:` functional-type(operands, results)";
19271909
}
19281910

mlir/test/mlir-tblgen/op-default-builder.td

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,9 @@ def AOp : NS_Op<"a_op", []> {
3131
// CHECK-LABEL: AOp declarations
3232
// Note: `cAttr` below could be conditionally optional and so the generation is
3333
// currently conservative.
34-
// CHECK-DAG: ::mlir::Value lhs, some-attr-kind aAttr, some-attr-kind bAttr, /*optional*/some-attr-kind cAttr, /*optional*/some-attr-kind dAttr);
34+
// CHECK-DAG: ::mlir::Value lhs, some-attr-kind aAttr, some-attr-kind bAttr, /*optional*/some-attr-kind cAttr, /*optional*/some-attr-kind dAttr = nullptr);
3535
// CHECK-DAG: ::mlir::Value lhs, some-return-type aAttr, some-return-type bAttr, /*optional*/some-attr-kind cAttr, /*optional*/some-return-type dAttr = 7.2);
36-
// CHECK-DAG: ::mlir::TypeRange resultTypes, ::mlir::Value lhs, some-attr-kind aAttr, some-attr-kind bAttr, /*optional*/some-attr-kind cAttr, /*optional*/some-attr-kind dAttr);
36+
// CHECK-DAG: ::mlir::TypeRange resultTypes, ::mlir::Value lhs, some-attr-kind aAttr, some-attr-kind bAttr, /*optional*/some-attr-kind cAttr, /*optional*/some-attr-kind dAttr = nullptr);
3737
// CHECK-DAG: ::mlir::TypeRange resultTypes, ::mlir::Value lhs, some-return-type aAttr, some-return-type bAttr, /*optional*/some-attr-kind cAttr, /*optional*/some-return-type dAttr = 7.2);
3838

3939
def BOp : NS_Op<"b_op", []> {

mlir/tools/mlir-tblgen/OpDefinitionsGen.cpp

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3088,6 +3088,26 @@ void OpEmitter::buildParamList(SmallVectorImpl<MethodParameter> &paramList,
30883088
defaultValuedAttrStartIndex = i;
30893089
}
30903090
}
3091+
3092+
// Check if parameters besides default valued one are enough to distinguish
3093+
// between builders with wrapped and unwrapped arguments.
3094+
bool hasBuilderAmbiguity = true;
3095+
for (const auto &arg : op.getArgs()) {
3096+
auto *namedAttr = dyn_cast<NamedAttribute *>(arg);
3097+
if (!namedAttr)
3098+
continue;
3099+
Attribute attr = namedAttr->attr;
3100+
if (attr.hasDefaultValue() || attr.isDerivedAttr())
3101+
continue;
3102+
3103+
if (attrParamKind != AttrParamKind::WrappedAttr ||
3104+
!canUseUnwrappedRawValue(attr))
3105+
continue;
3106+
3107+
hasBuilderAmbiguity = false;
3108+
break;
3109+
}
3110+
30913111
// Avoid generating build methods that are ambiguous due to default values by
30923112
// requiring at least one attribute.
30933113
if (defaultValuedAttrStartIndex < op.getNumArgs()) {
@@ -3098,9 +3118,9 @@ void OpEmitter::buildParamList(SmallVectorImpl<MethodParameter> &paramList,
30983118
cast<NamedAttribute *>(op.getArg(defaultValuedAttrStartIndex));
30993119
Attribute attr = namedAttr->attr;
31003120
if ((attrParamKind == AttrParamKind::WrappedAttr &&
3101-
canUseUnwrappedRawValue(attr)) ||
3121+
canUseUnwrappedRawValue(attr) && hasBuilderAmbiguity) ||
31023122
(attrParamKind == AttrParamKind::UnwrappedValue &&
3103-
!canUseUnwrappedRawValue(attr))) {
3123+
!canUseUnwrappedRawValue(attr) && hasBuilderAmbiguity)) {
31043124
++defaultValuedAttrStartIndex;
31053125
defaultValuedAttrLikeStartIndex = defaultValuedAttrStartIndex;
31063126
}

0 commit comments

Comments
 (0)