This is an archive of the discontinued LLVM Phabricator instance.

[mlir][mem2reg] Add mem2reg rewrite pattern.
ClosedPublic

Authored by Moxinilian on May 5 2023, 7:13 AM.

Details

Summary

This revision introduces the ability to invoke mem2reg as a rewrite pattern. This also modified the canonical mem2reg pass to use the rewrite pattern approach.

Depends on D149825

Diff Detail

Event Timeline

Moxinilian created this revision.May 5 2023, 7:13 AM
Herald added a project: Restricted Project. · View Herald TranscriptMay 5 2023, 7:13 AM
Moxinilian requested review of this revision.May 5 2023, 7:13 AM
gysit added a comment.May 8 2023, 1:33 AM

Is there logic in the existing mem2reg implementation that could possibly be simplified now that we are using a pattern. For example, applyOpPatternsAndFold should cleanup dead blocks by itself AFAIK. Or is the goal to make tryToPromoteMemorySlots completely self contained to make sure it can be used in a pass without running SimplifyRegion or similar after its execution.

Is there logic in the existing mem2reg implementation that could possibly be simplified now that we are using a pattern. For example, applyOpPatternsAndFold should cleanup dead blocks by itself AFAIK. Or is the goal to make tryToPromoteMemorySlots completely self contained to make sure it can be used in a pass without running SimplifyRegion or similar after its execution.

The intent was the latter yes. Although most of the cleanup in the current implementation is for correctness (except maybe the cleanup in the interfaces): if dead blocks are not properly rewired, the IR structure becomes invalid. So it is definitely necessary if canonicalization is not ran before applying the pattern, which feels like an unnecessary source of fragility.

gysit added a comment.May 8 2023, 1:47 AM

The intent was the latter yes. Although most of the cleanup in the current implementation is for correctness (except maybe the cleanup in the interfaces): if dead blocks are not properly rewired, the IR structure becomes invalid. So it is definitely necessary if canonicalization is not ran before applying the pattern, which feels like an unnecessary source of fragility.

Ok, I agree that tryToPromoteMemorySlots definitely should not produce invalid IR. Theoretically we could fail if canonicalization did not run but I think it is better to keep the code and have a robust version of mem2reg then!

gysit added inline comments.May 8 2023, 1:50 AM
mlir/lib/Transforms/Mem2Reg.cpp
465

I would add a check if the operation has no region right at the beginning of the patter, e.g.:

if (op->getNumRegions() == 0)
 return failure();

That way the patterns fails early in most cases. Maybe there is also a way to use an OpTrait or OpInterfaceRewritePattern to make sure we are only matching on Operations with a region?

Dinistro added inline comments.May 8 2023, 1:58 AM
mlir/lib/Transforms/Mem2Reg.cpp
466

NIT: could you use region.getOps<PromotableAllocationOpInterface>() here to simplify this?

471–474

Just for my understand: You use Operation::erase() inside the tryToPromoteMermorySlots function and this is the reason why you need a separate builder here, right?

Moxinilian updated this revision to Diff 520311.May 8 2023, 3:03 AM
Moxinilian marked 3 inline comments as done.

Address comments.

mlir/lib/Transforms/Mem2Reg.cpp
471–474

It's a bit more than that. Not only is Operation::erase used, but operations are modified in place and block argument structure gets modified, this is not supported by RewriterBase.

gysit accepted this revision.May 8 2023, 5:07 AM

LGTM modulo one minor comment regarding the debug printout.

mlir/lib/Transforms/Mem2Reg.cpp
425–426

Usually there is a:

#define DEBUG_TYPE "mem2reg"

Add the top of the file. After adding it you should be able to simplify the debug print out to:

LLVM_DEBUG(llvm::dbgs() << "[mem2reg] Promoted memory slot: " << slot.ptr << "\n");
This revision is now accepted and ready to land.May 8 2023, 5:07 AM
Moxinilian updated this revision to Diff 520656.May 9 2023, 4:31 AM

Address debug print comment.

Moxinilian updated this revision to Diff 520668.May 9 2023, 6:00 AM

Rebase to latest main.

This revision was automatically updated to reflect the committed changes.