[PATCH] D156889: [mlir][cf] Add ControlFlow to SCF lifting pass

Markus Böck via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Wed Aug 2 06:28:30 PDT 2023


zero9178 created this revision.
zero9178 added reviewers: Hardcode84, gysit, definelicht, mehdi_amini, ftynse, jpienaar, Mogball.
Herald added subscribers: bviyer, Moerafaat, bzcheeseman, sdasgup3, wenzhicui, wrengr, cota, teijeong, rdzhabarov, tatianashp, msifontes, jurahul, Kayjukh, grosul1, Joonsoo, liufengdb, aartbik, mgester, arpith-jacob, antiagainst, shauheen, rriddle.
Herald added a reviewer: rriddle.
Herald added a project: All.
zero9178 requested review of this revision.
Herald added subscribers: llvm-commits, stephenneuendorffer, nicolasvasilache.
Herald added projects: MLIR, LLVM.

Structured control flow ops have proven very useful for many transformations doing analysis on conditional flow and loops. Doing these transformations on CFGs requires repeated analysis of the IR possibly leading to more complicated or less capable implementations. With structured control flow, a lot of the information is already present in the structure.

This patch therefore adds a transformation making it possible to lift arbitrary control flow graphs to structured control flow operations. The algorithm used is outlined in https://dl.acm.org/doi/10.1145/2693261. The complexity in implementing the algorithm was mostly spent correctly handling block arguments in MLIR (the paper only addresses the control flow graph part of it).

Note that the transformation has been implemented fully generically and does not depend on any dialect. An interface implemented by the caller is used to construct any operation necessary for the transformation, making it possible to create an interface implementation purpose fit for ones IR.

For the purpose of testing and due to likely being a very common scenario, this patch adds an interface implementation lifting the control flow dialect to the SCF dialect.
Note the use of the word "lifting". Unlike other conversion passes, this pass is not 100% guaranteed to convert all ControlFlow ops.
Only if the input region being transformed contains a single kind of return-like operations is it guaranteed to replace all control flow ops. If that is not the case, exactly one control flow op will remain branching to regions terminating with a given return-like operation (e.g. one region terminates with `llvm.return` the other with `llvm.unreachable`).

-----

Additional notes:
Special thanks to the numba-mlir developers for the inspiration and some valuable testcases. 
Their implementation of the same algorithm can be found here https://github.com/numba/numba-mlir/blob/9a3356a7d0ab99c69591fd5d26a98a9c5b93133a/mlir/lib/Conversion/CfgToScf.cpp 
No code has been taken from their implementation, however.

I am also unsure who else is interested in such a pass, feel free to add more reviewers if anyone knows.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D156889

Files:
  llvm/include/llvm/ADT/STLExtras.h
  mlir/include/mlir/Conversion/ControlFlowToSCF/ControlFlowToSCF.h
  mlir/include/mlir/Conversion/Passes.h
  mlir/include/mlir/Conversion/Passes.td
  mlir/include/mlir/IR/Block.h
  mlir/include/mlir/IR/ValueRange.h
  mlir/include/mlir/Interfaces/ControlFlowInterfaces.h
  mlir/include/mlir/Transforms/CFGToSCF.h
  mlir/lib/Conversion/CMakeLists.txt
  mlir/lib/Conversion/ControlFlowToSCF/CMakeLists.txt
  mlir/lib/Conversion/ControlFlowToSCF/ControlFlowToSCF.cpp
  mlir/lib/IR/Block.cpp
  mlir/lib/Transforms/Utils/CFGToSCF.cpp
  mlir/lib/Transforms/Utils/CMakeLists.txt
  mlir/test/Conversion/ControlFlowToSCF/test.mlir

-------------- next part --------------
A non-text attachment was scrubbed...
Name: D156889.546436.patch
Type: text/x-patch
Size: 92006 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20230802/b31c34ac/attachment-0001.bin>


More information about the llvm-commits mailing list