This is an archive of the discontinued LLVM Phabricator instance.

[mlir] Commutative Operand Sorting Pass
Needs ReviewPublic

Authored by amandatang on Aug 21 2023, 12:20 PM.

Details

Summary

A characteristic of commutative operations is that such operations with swapped operands are identical.

This patch creates a pass that applies the SortCommutativeOperands rewrite pattern. The intention is for the sorting pass to be applied first to reorder operands in a deterministic way. This is followed by CSE which simply compares operands in order.

Diff Detail

Event Timeline

amandatang created this revision.Aug 21 2023, 12:20 PM
Herald added a project: Restricted Project. · View Herald TranscriptAug 21 2023, 12:20 PM
amandatang requested review of this revision.Aug 21 2023, 12:20 PM
amandatang retitled this revision from Create commutative sorting pass to [mlir] Commutative Operand Sorting Pass.Aug 21 2023, 12:29 PM
amandatang edited the summary of this revision. (Show Details)
jpienaar added inline comments.Aug 21 2023, 4:26 PM
mlir/include/mlir/Transforms/Passes.h
49

Lets move this below canonicalize for some alphabetical order

mlir/include/mlir/Transforms/Passes.td
22

s/implements/uses/

This is calling the helping method that implements the sorting algo.

23

Could you add more about the sort here? Even if it is just to point to the helper method for more info, I feel that gets lost here.

mlir/lib/Transforms/CommutativeOperandSort.cpp
36

Could we freeze the pattern set in the pass initializer?

mlir/test/Transforms/commutative-operand-sort.mlir
2

Lets check with just this pass (fine to add a test with cse too if you want, but lets unit test this one in isolation).

amandatang marked 4 inline comments as done.
amandatang edited the summary of this revision. (Show Details)

Address comments

amandatang added inline comments.Aug 21 2023, 9:52 PM
mlir/test/Transforms/commutative-operand-sort.mlir
2

In https://reviews.llvm.org/D157528, there are many additional tests in mlir/test/Transforms/test-commutativity-utils.mlir that check the sorting rewrite pattern alone. Currently, a test pass that applies the rewrite pattern in mlir/test/lib/Transforms/TestCommutativityUtils.cpp is used.

I was thinking the CommutativeOperandSort pass can be used there instead of the test pass to check the pass and sorting logic in isolation.

srishti-pm requested changes to this revision.Aug 22 2023, 2:57 PM

Thanks for adding this. An initial comment:-

mlir/include/mlir/Transforms/Passes.td
55

The meaning of "ancestors" is not explained here. Thus, the right word to use is "backward slice", which is a known term in MLIR.

This revision now requires changes to proceed.Aug 22 2023, 2:57 PM
amandatang marked an inline comment as done.

Address comment

@srishti-pm @jpienaar Any other followups on this?

Please update the commit summary. It only contains the title currently. Also, change your revision and commit titles to [MLIR][transforms] Add a pass to sort commutative operands.

jpienaar accepted this revision.Aug 29 2023, 5:28 AM

@srishti-pm @jpienaar Any other followups on this?

Looks good in general.

Please update the commit summary. It only contains the title currently. Also, change your revision and commit titles to [MLIR][transforms] Add a pass to sort commutative operands.

I see a summary was added Aug 21. Perhaps it didn't refresh correctly your side?

mlir/include/mlir/Transforms/Passes.h
65

Nit: commutative and end sentence with period.

mlir/lib/Transforms/CommutativeOperandSort.cpp
36

This is marked done but I don't see the change.

mlir/test/Transforms/commutative-operand-sort.mlir
2

Thats fine but unrelated to running only one pass here. While it's good to CSE applying post, this wouldn't be a surprise if one saw the operands are similarly sorted. Just test that the sorting here resulted in the same order using filecheck should be sufficient to see pass is registered and behaving as expected. If more tests can reuse same pass then that's good.