This is an archive of the discontinued LLVM Phabricator instance.

[mlir] Enable DRR variadic operand matching
ClosedPublic

Authored by logan on Aug 7 2023, 9:30 PM.

Details

Summary

This commit enables DRR rewriter to match a fixed number of sub-operands as a variadic operand:

def : Pat<(ConcatenateOp (variadic $input0, $input1)),
          (SomeOtherOp $input0, $input1)>;

Diff Detail

Event Timeline

logan created this revision.Aug 7 2023, 9:30 PM
Herald added a project: Restricted Project. · View Herald Transcript
logan requested review of this revision.Aug 7 2023, 9:30 PM

LGTM, given that I'm not currently working on MLIR. Will leave @jpienaar to make the decision

mlir/include/mlir/IR/PatternBase.td
219

space?

mlir/lib/TableGen/Pattern.cpp
304–305

Could you update the comment a little bit?

305–308

I'm thinking if we can hide the -1 here and there. Like if we do

dagAndConstant->hasRangeIndex()

For the function's default argument

bindOpArgument(..., int rangeIndex = dagAndConstant::kInvalidRangeIndex) { ...}

It may look better.

mlir/tools/mlir-tblgen/RewriterGen.cpp
714

Nit: single line statement

Nice, so this is a more refined variant of https://reviews.llvm.org/D121046 that enables more general support?

logan added a comment.Aug 11 2023, 2:16 PM

Nice, so this is a more refined variant of https://reviews.llvm.org/D121046 that enables more general support?

Kind of. This enables the sub-dag pattern matching into a variadic operand and checks HasExactValues<n> automatically. It also avoids the need of Front and DropFront sequences.

However, the limitation of this approach is that I only match variadic operand with exactly N sub-trees. I don't support "N sub-tree + tails". Technically doable by defining a (variantFrontNAndTails $sub0, $sub1, ..., $sub_n_m1, $tails) where $tails matches the remaining range. But to keep this simple, I didn't implement variantFrontNAndTails.

logan updated this revision to Diff 549568.Aug 11 2023, 8:11 PM

Changes in Revision 2:

  1. Allow binding a symbol to variadic DAG node, which means the full operand_range
  2. Fix staticMatcher problem rooted at variadic DAG node. Static matcher must not start from variadic node
  3. Introduces kInvalidRangeIndex and replace relevant -1 with kInvalidRangeIndex.
  4. Add comments to describe the behavior of DagAndConstant.
logan updated this revision to Diff 549569.Aug 11 2023, 8:19 PM
logan marked 3 inline comments as done.

Changes in Revision 3:

  1. Update some comments to clarify how we map SymbolInfo to the variable name (depending on whether SymbolInfo has a range index).
logan marked an inline comment as done.Aug 11 2023, 8:19 PM
logan added inline comments.
mlir/include/mlir/IR/PatternBase.td
219

No. This character is intended to be colon :. Like the symbol binding to the dag result, this symbol binding refers to the full operand_range.

Revision 1 forgot to implement this feature. I added them in Revision 2 and added more description on Line 224-227.

Still LGTM! The DagAndConstant looks better now!

Does variadic also work in the target dag?

mlir/include/mlir/IR/PatternBase.td
226

Just to avoid folks thinking there is a convention lets call it input_a and input_b (given we have the __n convention elsewhere that is load bearing).

mlir/include/mlir/TableGen/Pattern.h
257

Why is this one public? Looking at usage, it feels more like optional<int64_t>. Range is also confusing as I'd expect 2 integers for a range. (looking below it almost feels like this is as if index is an array with at most 2 elements)

313

Does this mean only one level of nesting is allowed here? E.g., if there was a variadic of variadic what would this be?

332

Why 3?

logan marked 2 inline comments as done.Aug 21 2023, 10:06 PM

Does variadic also work in the target dag?

No, this commit doesn't support that. I couldn't figure it out at this moment.

mlir/include/mlir/TableGen/Pattern.h
257

I am not a fan of std::optional because it takes an extra bool. But I don't have strong feeling on this, I updated the change anyway.

313

Correct. I don't support nested variadic DAG. mlir/tools/mlir-tblgen/RewriterGen.cpp Line 612 emit an error if we detect a variadic DAG.

def : Pat<(SomeOp (variadic $a, $b, (variadic $c, $d))), ...>;  // error
332

I didn't write the op definition, but I tried to say OpWithThreeOutput has 3 outputs. Since it is a multi-result value, DagAndConstant keep tracks of the number of outputs.

logan updated this revision to Diff 552213.Aug 21 2023, 10:08 PM
logan marked an inline comment as done.

Address comments.

jpienaar accepted this revision.Aug 28 2023, 12:40 PM
This revision is now accepted and ready to land.Aug 28 2023, 12:40 PM