This is an archive of the discontinued LLVM Phabricator instance.

[flang] use greedy mlir driver for stack arrays pass
ClosedPublic

Authored by tblah on May 18 2023, 3:18 AM.

Details

Summary

In upstream mlir, the dialect conversion infrastructure is used for
lowering from one dialect to another: the passes are of the form
XToYPass. Whereas, transformations within the same dialect tend to use
applyPatternsAndFoldGreedily.

The Greedy Pattern Rewrite Driver is more convenient for this sort of
conversion because it lets the pattern decide if it matches, instead of
requiring ops to be marked as illegal (which is most convenient when
lowering from one dialect to another).

In this case, the full complexity of applyPatternsAndFoldGreedily is
not needed so we can get away with the simpler
applyOpPatternsAndFold.

This change was suggested by @jeanPerier

Diff Detail

Event Timeline

tblah created this revision.May 18 2023, 3:18 AM
Herald added projects: Restricted Project, Restricted Project. · View Herald TranscriptMay 18 2023, 3:18 AM
tblah requested review of this revision.May 18 2023, 3:18 AM
jeanPerier accepted this revision.May 19 2023, 12:33 AM

Makes sense to me, thanks.

flang/lib/Optimizer/Transforms/StackArrays.cpp
197

Shouldn't this be const? As I understand, pattern members must not be modified.

727

Given the list of operations that will be rewritten is already decided, you may even go for applyOpPatternsAndFold from https://github.com/llvm/llvm-project/blob/main/mlir/include/mlir/Transforms/GreedyPatternRewriteDriver.h#LL141C1-L141C23 that takes the list of operations on which the pattern should be applied.

I would imagine applyOpPatternsAndFold to be faster than applyPatternsAndFoldGreedily that must visit all the funcOp IR, but I did not check that.

This revision is now accepted and ready to land.May 19 2023, 12:33 AM
tblah updated this revision to Diff 523831.May 19 2023, 9:51 AM
tblah marked an inline comment as done.

Thanks for review! Changes:

  • Refactor error handling
  • make sure the rewrite pass uses const data
  • Use applyOpPatternsAndFold instead of applyPatternsAndFoldGreedily

This reverts all of the changes to the tests because the simpler driver doesn't
canonicalise source first.

tblah marked an inline comment as done.May 19 2023, 9:51 AM
tblah edited the summary of this revision. (Show Details)
This revision was automatically updated to reflect the committed changes.

Thanks for the update, looks great

Could you check exchange2 or cam4 benchmark from Spec. They seem to be failing after this change.