Skip to content

[mlir][drr] Fix getValueAndRangeUse for Optional operands #138742

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 1 commit into from
May 7, 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
6 changes: 6 additions & 0 deletions mlir/lib/TableGen/Pattern.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -303,6 +303,12 @@ std::string SymbolInfoMap::SymbolInfo::getValueAndRangeUse(
case Kind::Operand: {
assert(index < 0);
auto *operand = cast<NamedTypeConstraint *>(op->getArg(getArgIndex()));
if (operand->isOptional()) {
auto repl =
formatv(fmt, formatv("({0}.empty() ? Value() : *{0}.begin())", name));
Copy link
Member

@akuegel akuegel May 13, 2025

Choose a reason for hiding this comment

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

Could this use mlir::Value() instead of just Value()? Otherwise if the generated code is included from outside mlir namespace, it will not work. Or do we have a rule that all code that uses MLIR needs to be inside a mlir namespace?

Copy link
Contributor

Choose a reason for hiding this comment

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

Nah there's no rule like that. If you send a PR with the proper namespacing I'll stamp/merge it (or I can send it tomorrow)

Copy link
Member

Choose a reason for hiding this comment

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

I'll send one (elsehwere in this file we do fully namespace already)

LLVM_DEBUG(dbgs() << repl << " (OptionalOperand)\n");
return std::string(repl);
}
// If this operand is variadic and this SymbolInfo doesn't have a range
// index, then return the full variadic operand_range. Otherwise, return
// the value itself.
Expand Down
14 changes: 14 additions & 0 deletions mlir/test/lib/Dialect/Test/TestOps.td
Original file line number Diff line number Diff line change
Expand Up @@ -1850,6 +1850,20 @@ def : Pat<
(MixedVOperandOp5 $input2a, $input2b, $input1b, $attr1,
ConstantStrAttr<StrAttr, "MatchMultiVariadicSubSymbol">)>;

def MixedVOperandOp7 : TEST_Op<"mixed_variadic_optional_in7",
[AttrSizedOperandSegments]> {
let arguments = (ins
Variadic<I32>:$input1,
Optional<I32>:$input2,
I32Attr:$attr1
);
}

def : Pat<
(MixedVOperandOp7 $input1, $input2, ConstantAttr<I32Attr, "2">:$attr1),
(MixedVOperandOp6 $input1, (variadic $input2), $attr1),
[(Constraint<CPred<"$0 != Value()">> $input2)]>;

//===----------------------------------------------------------------------===//
// Test Patterns (either)
//===----------------------------------------------------------------------===//
Expand Down
10 changes: 10 additions & 0 deletions mlir/test/mlir-tblgen/pattern.mlir
Original file line number Diff line number Diff line change
Expand Up @@ -584,6 +584,16 @@ func.func @testMatchMultiVariadicSubSymbol(%arg0: i32, %arg1: i32, %arg2: i32, %
return
}

// CHECK-LABEL: @testMatchMixedVaradicOptional
func.func @testMatchMixedVaradicOptional(%arg0: i32, %arg1: i32, %arg2: i32, %arg3: i32) -> () {
// CHECK: "test.mixed_variadic_in6"(%arg0, %arg1, %arg2) <{attr1 = 2 : i32}> : (i32, i32, i32) -> ()
"test.mixed_variadic_optional_in7"(%arg0, %arg1, %arg2) {attr1 = 2 : i32, operandSegmentSizes = array<i32: 2, 1>} : (i32, i32, i32) -> ()
// CHECK: test.mixed_variadic_optional_in7
"test.mixed_variadic_optional_in7"(%arg0, %arg1) {attr1 = 2 : i32, operandSegmentSizes = array<i32: 2, 0>} : (i32, i32) -> ()

return
}

//===----------------------------------------------------------------------===//
// Test that natives calls are only called once during rewrites.
//===----------------------------------------------------------------------===//
Expand Down