Skip to content

Commit 3c70980

Browse files
authored
[mlir][drr] Fix getValueAndRangeUse for Optional operands (#138742)
Optional operands should just return one single value.
1 parent 0e75810 commit 3c70980

File tree

3 files changed

+30
-0
lines changed

3 files changed

+30
-0
lines changed

mlir/lib/TableGen/Pattern.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -303,6 +303,12 @@ std::string SymbolInfoMap::SymbolInfo::getValueAndRangeUse(
303303
case Kind::Operand: {
304304
assert(index < 0);
305305
auto *operand = cast<NamedTypeConstraint *>(op->getArg(getArgIndex()));
306+
if (operand->isOptional()) {
307+
auto repl =
308+
formatv(fmt, formatv("({0}.empty() ? Value() : *{0}.begin())", name));
309+
LLVM_DEBUG(dbgs() << repl << " (OptionalOperand)\n");
310+
return std::string(repl);
311+
}
306312
// If this operand is variadic and this SymbolInfo doesn't have a range
307313
// index, then return the full variadic operand_range. Otherwise, return
308314
// the value itself.

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

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1850,6 +1850,20 @@ def : Pat<
18501850
(MixedVOperandOp5 $input2a, $input2b, $input1b, $attr1,
18511851
ConstantStrAttr<StrAttr, "MatchMultiVariadicSubSymbol">)>;
18521852

1853+
def MixedVOperandOp7 : TEST_Op<"mixed_variadic_optional_in7",
1854+
[AttrSizedOperandSegments]> {
1855+
let arguments = (ins
1856+
Variadic<I32>:$input1,
1857+
Optional<I32>:$input2,
1858+
I32Attr:$attr1
1859+
);
1860+
}
1861+
1862+
def : Pat<
1863+
(MixedVOperandOp7 $input1, $input2, ConstantAttr<I32Attr, "2">:$attr1),
1864+
(MixedVOperandOp6 $input1, (variadic $input2), $attr1),
1865+
[(Constraint<CPred<"$0 != Value()">> $input2)]>;
1866+
18531867
//===----------------------------------------------------------------------===//
18541868
// Test Patterns (either)
18551869
//===----------------------------------------------------------------------===//

mlir/test/mlir-tblgen/pattern.mlir

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -584,6 +584,16 @@ func.func @testMatchMultiVariadicSubSymbol(%arg0: i32, %arg1: i32, %arg2: i32, %
584584
return
585585
}
586586

587+
// CHECK-LABEL: @testMatchMixedVaradicOptional
588+
func.func @testMatchMixedVaradicOptional(%arg0: i32, %arg1: i32, %arg2: i32, %arg3: i32) -> () {
589+
// CHECK: "test.mixed_variadic_in6"(%arg0, %arg1, %arg2) <{attr1 = 2 : i32}> : (i32, i32, i32) -> ()
590+
"test.mixed_variadic_optional_in7"(%arg0, %arg1, %arg2) {attr1 = 2 : i32, operandSegmentSizes = array<i32: 2, 1>} : (i32, i32, i32) -> ()
591+
// CHECK: test.mixed_variadic_optional_in7
592+
"test.mixed_variadic_optional_in7"(%arg0, %arg1) {attr1 = 2 : i32, operandSegmentSizes = array<i32: 2, 0>} : (i32, i32) -> ()
593+
594+
return
595+
}
596+
587597
//===----------------------------------------------------------------------===//
588598
// Test that natives calls are only called once during rewrites.
589599
//===----------------------------------------------------------------------===//

0 commit comments

Comments
 (0)