Skip to content

Commit 2af6ddb

Browse files
authored
[mlir][ods] Fix missing property elision for variadic segment properties (#115930)
This fixes a bug where variadic segment properties would not be elided when printing `prop-dict`.
1 parent 4d4a353 commit 2af6ddb

File tree

3 files changed

+37
-8
lines changed

3 files changed

+37
-8
lines changed

mlir/test/IR/properties.mlir

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// # RUN: mlir-opt %s -split-input-file | mlir-opt |FileCheck %s
1+
// # RUN: mlir-opt %s -split-input-file | mlir-opt | FileCheck %s
22
// # RUN: mlir-opt %s -mlir-print-op-generic -split-input-file | mlir-opt -mlir-print-op-generic | FileCheck %s --check-prefix=GENERIC
33

44
// CHECK: test.with_properties
@@ -38,6 +38,14 @@ test.using_property_in_custom [1, 4, 20]
3838
// GENERIC-SAME: }>
3939
test.using_property_ref_in_custom 1 + 4 = 5
4040

41+
// Tests that the variadic segment size properties are elided.
42+
// CHECK: %[[CI64:.*]] = arith.constant
43+
// CHECK-NEXT: test.variadic_segment_prop %[[CI64]], %[[CI64]] : %[[CI64]] : i64, i64 : i64 end
44+
// GENERIC: %[[CI64:.*]] = "arith.constant"()
45+
// GENERIC-NEXT: "test.variadic_segment_prop"(%[[CI64]], %[[CI64]], %[[CI64]]) <{operandSegmentSizes = array<i32: 2, 1>, resultSegmentSizes = array<i32: 2, 1>}> : (i64, i64, i64) -> (i64, i64, i64)
46+
%ci64 = arith.constant 0 : i64
47+
test.variadic_segment_prop %ci64, %ci64 : %ci64 : i64, i64 : i64 end
48+
4149
// CHECK: test.with_default_valued_properties na{{$}}
4250
// GENERIC: "test.with_default_valued_properties"()
4351
// GENERIC-SAME: <{a = 0 : i32, b = "", c = -1 : i32, unit = false}> : () -> ()

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3047,6 +3047,15 @@ def TestOpUsingPropertyInCustomAndOther
30473047
);
30483048
}
30493049

3050+
def TestOpWithVariadicSegmentProperties : TEST_Op<"variadic_segment_prop",
3051+
[AttrSizedOperandSegments, AttrSizedResultSegments]> {
3052+
let arguments = (ins Variadic<I64>:$a1, Variadic<I64>:$a2);
3053+
let results = (outs Variadic<I64>:$b1, Variadic<I64>:$b2);
3054+
let assemblyFormat = [{
3055+
$a1 `:` $a2 `:` type($b1) `:` type($b2) prop-dict attr-dict `end`
3056+
}];
3057+
}
3058+
30503059
def TestOpUsingPropertyRefInCustom : TEST_Op<"using_property_ref_in_custom"> {
30513060
let assemblyFormat = "custom<IntProperty>($first) `+` custom<SumProperty>($second, ref($first)) attr-dict";
30523061
let arguments = (ins IntProperty<"int64_t">:$first, IntProperty<"int64_t">:$second);

mlir/tools/mlir-tblgen/OpFormatGen.cpp

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2008,10 +2008,26 @@ static void genNonDefaultValueCheck(MethodBody &body, const Operator &op,
20082008
<< "() != " << propElement.getVar()->prop.getDefaultValue();
20092009
}
20102010

2011+
/// Elide the variadic segment size attributes if necessary.
2012+
/// This pushes elided attribute names in `elidedStorage`.
2013+
static void genVariadicSegmentElision(OperationFormat &fmt, Operator &op,
2014+
MethodBody &body,
2015+
const char *elidedStorage) {
2016+
if (!fmt.allOperands &&
2017+
op.getTrait("::mlir::OpTrait::AttrSizedOperandSegments"))
2018+
body << " " << elidedStorage << ".push_back(\"operandSegmentSizes\");\n";
2019+
if (!fmt.allResultTypes &&
2020+
op.getTrait("::mlir::OpTrait::AttrSizedResultSegments"))
2021+
body << " " << elidedStorage << ".push_back(\"resultSegmentSizes\");\n";
2022+
}
2023+
20112024
/// Generate the printer for the 'prop-dict' directive.
20122025
static void genPropDictPrinter(OperationFormat &fmt, Operator &op,
20132026
MethodBody &body) {
20142027
body << " ::llvm::SmallVector<::llvm::StringRef, 2> elidedProps;\n";
2028+
2029+
genVariadicSegmentElision(fmt, op, body, "elidedProps");
2030+
20152031
for (const NamedProperty *namedProperty : fmt.usedProperties)
20162032
body << " elidedProps.push_back(\"" << namedProperty->name << "\");\n";
20172033
for (const NamedAttribute *namedAttr : fmt.usedAttributes)
@@ -2057,13 +2073,9 @@ static void genPropDictPrinter(OperationFormat &fmt, Operator &op,
20572073
static void genAttrDictPrinter(OperationFormat &fmt, Operator &op,
20582074
MethodBody &body, bool withKeyword) {
20592075
body << " ::llvm::SmallVector<::llvm::StringRef, 2> elidedAttrs;\n";
2060-
// Elide the variadic segment size attributes if necessary.
2061-
if (!fmt.allOperands &&
2062-
op.getTrait("::mlir::OpTrait::AttrSizedOperandSegments"))
2063-
body << " elidedAttrs.push_back(\"operandSegmentSizes\");\n";
2064-
if (!fmt.allResultTypes &&
2065-
op.getTrait("::mlir::OpTrait::AttrSizedResultSegments"))
2066-
body << " elidedAttrs.push_back(\"resultSegmentSizes\");\n";
2076+
2077+
genVariadicSegmentElision(fmt, op, body, "elidedAttrs");
2078+
20672079
for (const StringRef key : fmt.inferredAttributes.keys())
20682080
body << " elidedAttrs.push_back(\"" << key << "\");\n";
20692081
for (const NamedAttribute *attr : fmt.usedAttributes)

0 commit comments

Comments
 (0)