This is an archive of the discontinued LLVM Phabricator instance.

Add support for processing Ops with empty region to the CSE
AcceptedPublic

Authored by Ding on Jan 17 2023, 12:24 AM.

Details

Summary

Automated commit created by applying diff 489654

Phabricator-ID: PHID-HMBT-sx4k6i2amcuosqdga4sn
Review-ID: D141880

Diff Detail

Event Timeline

Ding created this revision.Jan 17 2023, 12:24 AM
Ding requested review of this revision.Jan 17 2023, 12:24 AM
mravishankar requested changes to this revision.Jan 17 2023, 11:14 AM

Thanks! This was the fix I had in mind.

This is where the tests for ops with region are https://github.com/llvm/llvm-project/blob/d2f136920b9247a9e5874d4d3a00a880db6e2827/mlir/test/Transforms/cse.mlir#L327

This is the test op definition https://github.com/llvm/llvm-project/blob/d2f136920b9247a9e5874d4d3a00a880db6e2827/mlir/test/lib/Dialect/Test/TestOps.td#L3141 . You might need to add a new op, or change the op definition to test your case.

mlir/lib/Transforms/CSE.cpp
61–63

A bit mouthful to read... maybe we can just add a helper function

bool hasNoOrEmptyRegion(Operation *op) {
  return op->getNumRegions() == 0 || 
    (op->getNumRegions() == 1 && op->getRegion(0).empty());
}

and use that instead

if (hasNoOrEmptyRegion(lhs) && hasNoOrEmptyRegion(rhs)) {
 ...
}
274–275

I think this would be

if (!hasNoOrEmptyRegion(op)) {
   return failure();
}

?

This revision now requires changes to proceed.Jan 17 2023, 11:14 AM
Ding added a comment.Jan 17 2023, 6:46 PM

Thanks! This was the fix I had in mind.

This is where the tests for ops with region are https://github.com/llvm/llvm-project/blob/d2f136920b9247a9e5874d4d3a00a880db6e2827/mlir/test/Transforms/cse.mlir#L327

This is the test op definition https://github.com/llvm/llvm-project/blob/d2f136920b9247a9e5874d4d3a00a880db6e2827/mlir/test/lib/Dialect/Test/TestOps.td#L3141 . You might need to add a new op, or change the op definition to test your case.

I found that test.any_cond Op could have empty region, maybe we can use it for testing? Just like this

func.func @cse_ops_with_empty_region()
  -> (i32, i32) {
  %0 = "test.any_cond"() ({
  }) : () -> i32
  %1 = "test.any_cond"() ({
  }) : () -> i32
  return %0, %1 : i32, i32
}

the IR after CSE Pass will be

func.func @cse_single_block_ops_identical_bodies() -> (i32, i32) {
    %0 = "test.any_cond"() ({
    }) : () -> i32
    return %0, %0 : i32, i32
  }
Ding updated this revision to Diff 490026.Jan 17 2023, 8:09 PM

Updating D141900: Add support for processing Ops with empty region to the CSE

mravishankar added inline comments.Jan 20 2023, 9:40 AM
mlir/lib/Transforms/CSE.cpp
275

WHy cant we use if (!hasNoOrEmptyRegion(op)) here?

mlir/test/Transforms/cse.mlir
484

Nit: Please add newline.

mravishankar requested changes to this revision.Jan 20 2023, 9:42 AM

Looks good. THanks. Just a few nits.

This revision now requires changes to proceed.Jan 20 2023, 9:42 AM
rriddle added inline comments.Jan 20 2023, 10:04 AM
mlir/lib/Transforms/CSE.cpp
63

Why just one region? Why not check if all regions are empty?

Ding updated this revision to Diff 491026.Jan 20 2023, 8:48 PM
Ding marked 2 inline comments as done.

Updating D141900: Add support for processing Ops with empty region to the CSE

This revision is now accepted and ready to land.Jan 23 2023, 12:23 PM
rriddle accepted this revision.Jan 23 2023, 12:40 PM
rriddle added inline comments.
mlir/lib/Transforms/CSE.cpp
44

Please pull these out of the namespace, static functions should be top level.

46

You shouldn't need the parentheses around the all_of call.

Ding updated this revision to Diff 491559.Jan 23 2023, 4:48 PM
Ding marked 3 inline comments as done.

Updating D141900: Add support for processing Ops with empty region to the CSE

Is there anything blocking this from landing?

I don't think so. I am AFK. I can merge when I get to my machine

@Ding I tried to rebase this and land. Seems to fail tests.

Ding marked 2 inline comments as done.Feb 10 2023, 7:21 AM

@Ding I tried to rebase this and land. Seems to fail tests.

This commit is conflict with the latest commit which support multiple region CSE, I will resolve these conflicts this weekend.

Ding added a comment.Feb 11 2023, 8:10 AM

@Ding I tried to rebase this and land. Seems to fail tests.

@mravishankar, I found that commit aa4e54f2f444c266310105fccbdbb07fc71894da has fixed this issue when trying to support CSE ops with multiple regions. Should I just push this commit with the new test I added in cse.mlir?

Sure. Just push the test. Thanks!

Ding added a comment.Feb 11 2023, 7:12 PM

Sure. Just push the test. Thanks!

OK, the commit with test is here(https://reviews.llvm.org/D143839).