This is an archive of the discontinued LLVM Phabricator instance.

[mlir] Update LICM to support Graph Regions
ClosedPublic

Authored by Mogball on Mar 25 2022, 12:52 AM.

Details

Summary

Changes the algorithm of LICM to support graph regions (no guarantee of topologically sorted order). Also fixes an issue where ops with recursive side effects and regions would not be hoisted if any nested ops used operands that were defined within the nested region.

Diff Detail

Event Timeline

Mogball created this revision.Mar 25 2022, 12:52 AM
Herald added projects: Restricted Project, Restricted Project. · View Herald Transcript
Mogball requested review of this revision.Mar 25 2022, 12:52 AM
Mogball added inline comments.
mlir/test/Transforms/loop-invariant-code-motion.mlir
171

@nicolasvasilache Can you verify whether this is acceptable behaviour?

Mogball updated this revision to Diff 418153.Mar 25 2022, 1:20 AM

change to worklist algorithm

Mogball retitled this revision from [mlir] Simply LoopLikeOpInterface and Update LICM to support Graph Regions to [mlir] Simplify LoopLikeOpInterface and Update LICM to support Graph Regions.Mar 25 2022, 12:55 PM

Adds default implementations of isDefinedOutsideOfLoop and moveOutOfLoop since 99% of all implementations of these functions were identical

Can you split this as a NFC commit?

Mogball updated this revision to Diff 418611.Mar 28 2022, 9:12 AM

rebase (fix bazel build maybe)

Yeah let me split the patch

Mogball updated this revision to Diff 418651.Mar 28 2022, 11:14 AM

split out simplifications to LoopLikeOpInterface

Mogball retitled this revision from [mlir] Simplify LoopLikeOpInterface and Update LICM to support Graph Regions to [mlir] Update LICM to support Graph Regions.Mar 28 2022, 11:14 AM
Mogball edited the summary of this revision. (Show Details)
bondhugula added inline comments.Apr 6 2022, 1:27 AM
mlir/lib/Interfaces/LoopLikeInterface.cpp
34

This would be a useful function and can directly be added in MLIRAnalysis library?

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

Sorted order here.

2761

Nit: ... with a graph region.

2780

Nit: extra blank line.

Mogball updated this revision to Diff 421288.Apr 7 2022, 11:08 AM
Mogball marked 4 inline comments as done.
Mogball edited the summary of this revision. (Show Details)

review comments

mlir/lib/Interfaces/LoopLikeInterface.cpp
34

I will be submitting a patch after this one that moves moveLoopInvariantCode to TransformUtils. At the same time, I'll move this function to MLIRAnalysis (the same function exists elsewhere in the codebase and I will refactor those as well)

rriddle added inline comments.Apr 7 2022, 11:46 AM
mlir/lib/Interfaces/LoopLikeInterface.cpp
57

-> /

57

Sigh, I hate phab. Use /// comments here.

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

Do you know why this dialect is depending on Linalg?

2773–2775
Mogball updated this revision to Diff 421563.Apr 8 2022, 9:58 AM
Mogball marked 3 inline comments as done.

review comments

Mogball marked an inline comment as done.Apr 8 2022, 9:59 AM
Mogball added inline comments.
mlir/test/lib/Dialect/Test/TestOps.td
14

There's a test op for LinalgConvolutionOpInterface

rriddle added inline comments.Apr 11 2022, 10:38 AM
mlir/lib/Interfaces/LoopLikeInterface.cpp
67

Probably more idiomatic to check the trait for IsTerminator.

97

nit: Add a newline before the comment.

mlir/test/Transforms/loop-invariant-code-motion.mlir
362

Can you add a test that checks that we don't hoist from graph regions into non-graph regions?

366

Do we have a test in a non-graph region where operations that can be hoisted feed into each other? Something like:

%v0 = arith.addi %c0, %c0 : i32
%v1 = arith.addi %c0, %v0 : i32

to make sure that dominance still holds after hoisting?

Mogball added inline comments.Apr 11 2022, 3:29 PM
mlir/test/Transforms/loop-invariant-code-motion.mlir
362

Is the behaviour that we want that ops don't cross between non-graph and graph regions (in either direction)?

Right now, LICM can move ops from graph regions to non-graph regions but only in topological order (dominance will be preserved). Ops that form cycles won't be moved because it doesn't detect them.

366

Oddly enough, I don't see one. I'll add one.

rriddle added inline comments.Apr 11 2022, 3:31 PM
mlir/test/Transforms/loop-invariant-code-motion.mlir
362

It's more about being conservatively correct. We can hoist from a graph into a non-graph(and vice-versa), but we, for example, have to ensure that the dominance of the hoisted operations is valid. We have to be careful about the contract changes between the two regions.

Mogball added inline comments.Apr 11 2022, 4:03 PM
mlir/test/Transforms/loop-invariant-code-motion.mlir
362

We will be okay to move ops from graph regions to non-graph regions; since LICM only considers one operation at a time, ops are moved only when all their operands are defined outside the graph region body. If the outer region is a non-graph region, the defining ops must precede the loop op.

I'll add some test cases.

Mogball updated this revision to Diff 422082.Apr 11 2022, 5:50 PM
Mogball marked 5 inline comments as done.

review comments -- adding some tests

rriddle accepted this revision.Apr 14 2022, 7:04 PM
This revision is now accepted and ready to land.Apr 14 2022, 7:04 PM
This revision was automatically updated to reflect the committed changes.