This is an archive of the discontinued LLVM Phabricator instance.

[mlir] [mem2reg] Adapt to be pattern-friendly.
ClosedPublic

Authored by Moxinilian on May 12 2023, 1:56 AM.

Details

Summary

This revision modifies the mem2reg interfaces and algorithm to be more
omfortable to use as a pattern. The motivation behind this is that
currently the pattern needs to be applied to the scope op of the region
in which allocators should be promoted. However, a more natural way to
apply the pattern would be to apply it on the allocator directly. This
is not only clearer but easier to parallelize.

This revision changes the mem2reg pattern to operate this way. This
required restraining the interfaces to only mutate IR using
RewriterBase, as the previously used escape hatch is not granular enough
to match on the region that is modified only. This has the unfortunate
cost of preventing batching allocator promotion and making the block
argument adding logic more complex. Because batching no longer made any
sense, I made the internal analyzer/promoter decoupling private again.

This also adds statistics to the mem2reg infrastructure.

Diff Detail

Event Timeline

Moxinilian created this revision.May 12 2023, 1:56 AM
Herald added a reviewer: dcaballe. · View Herald Transcript
Herald added a project: Restricted Project. · View Herald Transcript
Moxinilian requested review of this revision.May 12 2023, 1:56 AM
gysit added a comment.May 12 2023, 4:26 AM

Nice! I have some minor comments.

mlir/lib/Transforms/Mem2Reg.cpp
455–482

nit: back() may be more elegant here?

476–478

I believe drop_back may allow us to save the copy here?

633

Can we enable the region simplification and add a pass option to disable it during testing?

The canonicalize pass already has such a pass flag:

Option<"enableRegionSimplification", "region-simplify", "bool",
       /*default=*/"true",
       "Perform control flow optimizations to the region tree">,
636

nit: You can pass the op directly to the rewriter and the rewriter will iterate all regions, i.e.:

(void)applyPatternsAndFoldGreedily(scopeOp, frozen, config)
Moxinilian edited the summary of this revision. (Show Details)

Fix forgotten direct IR mutation + fail if patterns do not converge.

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

Address comments + add statistics to mem2reg.

mlir/lib/Transforms/Mem2Reg.cpp
636

Am I allowed to do this? What if the scopeOp is an allocator and I delete it? Wouldn't that break the isolation constraint of passes?

gysit added inline comments.May 12 2023, 6:30 AM
mlir/lib/Transforms/Mem2Reg.cpp
636

The patterns apply only to the regions and not to the scope op, see implementation:

inline LogicalResult applyPatternsAndFoldGreedily(
    Operation *op, const FrozenRewritePatternSet &patterns,
    GreedyRewriteConfig config = GreedyRewriteConfig()) {
  bool failed = false;
  for (Region &region : op->getRegions())
    failed |= applyPatternsAndFoldGreedily(region, patterns, config).failed();
  return failure(failed);
}
gysit added a comment.May 12 2023, 7:03 AM

Nice cleanup!

Can you add a small test case for the statistics so that I can see it in action?

mlir/include/mlir/Transforms/Mem2Reg.h
21

nit: I would probably use mlir::Pass::Statistic here?

mlir/lib/Transforms/Mem2Reg.cpp
628

If I understand correctly Mem2RegPattern stores a reference to Mem2RegStatistics? Are you sure this is not resulting in a use-after-free. A temporary is normally only valid during the function execution - here the construction of Mem2RegPattern - but not later on once the pattern is applied?

Allocating Mem2RegPattern on the stack of runOnOperation seems the safe choice here?

Moxinilian marked 3 inline comments as done.

Address comments.

mlir/include/mlir/Transforms/Mem2Reg.h
21

I feel like "Pass::Statistic" has a connotation for passes. What if you want to have your own custom statistics? Pass::Statistic inherits from llvm::Statistic anyway so there is no practical advantage.

gysit accepted this revision.May 15 2023, 2:21 AM

LGTM!

This revision is now accepted and ready to land.May 15 2023, 2:21 AM

Fix memory issue with default constructor.

This revision was automatically updated to reflect the committed changes.