This is an archive of the discontinued LLVM Phabricator instance.

[MLIR][SCF] Add for-to-while loop transformation pass
ClosedPublic

Authored by mortbopet on Aug 20 2021, 2:52 AM.

Details

Summary

This pass transforms SCF.ForOp operations to SCF.WhileOp. The For loop condition is placed in the 'before' region of the while operation, and indctuion variable incrementation + the loop body in the 'after' region. The loop carried values of the while op are the induction variable (IV) of the for-loop + any iter_args specified for the for-loop.
Any 'yield' ops in the for-loop are rewritten to additionally yield the (incremented) induction variable.

This transformation is useful for passes where we want to consider structured control flow solely on the basis of a loop body and the computation of a loop condition. As an example, when doing high-level synthesis in CIRCT, the incrementation of an IV in a for-loop is "just another part" of a circuit datapath, and what we really care about is the distinction between our datapath and our control logic (the condition variable).

Diff Detail

Event Timeline

mortbopet created this revision.Aug 20 2021, 2:52 AM
mortbopet requested review of this revision.Aug 20 2021, 2:52 AM
mortbopet updated this revision to Diff 367755.Aug 20 2021, 3:28 AM

Fix clang-tidy nits

mortbopet updated this revision to Diff 373196.Sep 17 2021, 5:26 AM
  • Moved induction variable incrementation to start of while body
  • updated tests
mortbopet updated this revision to Diff 373205.Sep 17 2021, 6:26 AM

scf.execute_region ops are no longer necessary.

Could you add some context as to when this is expected to be useful?
Is there also a plan to replace scf -> block lowering by scf -> while -> blocks ?

mortbopet updated this revision to Diff 373212.EditedSep 17 2021, 7:04 AM

I've added a rationale to the PR summary.

Could you elaborate on what you mean by "scf -> block lowering by scf -> while -> blocks"?

mortbopet edited the summary of this revision. (Show Details)Sep 17 2021, 7:05 AM

I've added a rationale to the PR summary.

Could you elaborate on what you mean by "scf -> block lowering by scf -> while -> blocks"?

thanks!

mortbopet updated this revision to Diff 373254.Sep 17 2021, 9:33 AM
mortbopet edited the summary of this revision. (Show Details)

mising newline

Thanks, Morten!

mlir/include/mlir/Dialect/SCF/Passes.td
69

Even though this is pretty simple, can you add a description here for the generated documentation?

mlir/lib/Dialect/SCF/Transforms/ForToWhile.cpp
1

Name wrong?

41

I didn't think this was necessary? Isn't the insertionPoint set to the op being lowered already?

89

"erase the it"?

mlir/test/Dialect/SCF/for-loop-to-while-loop.mlir
6

The preference is not to assume anything about naming in tests and to use the regular expression matching capabilities of FileCheck.

This revision is now accepted and ready to land.Sep 17 2021, 9:52 AM
mortbopet updated this revision to Diff 373406.Sep 18 2021, 3:25 AM
mortbopet marked 4 inline comments as done.

Address review comments

This revision was automatically updated to reflect the committed changes.

I think you had tested this on an old revision but it failed CI at HEAD, can you rebase and fix the test before recommitting?

I think you had tested this on an old revision but it failed CI at HEAD, can you rebase and fix the test before recommitting?

Sorry about that; rebased and tested locally, and the tests seemed to run successfully. I'll update this PR and rerun CI - thank you for notifying me.