MLIR  16.0.0git
ControlFlowSink.cpp
Go to the documentation of this file.
1 //===- ControlFlowSink.cpp - Code to perform control-flow sinking ---------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file implements a basic control-flow sink pass. Control-flow sinking
10 // moves operations whose only uses are in conditionally-executed blocks in to
11 // those blocks so that they aren't executed on paths where their results are
12 // not needed.
13 //
14 //===----------------------------------------------------------------------===//
15 
16 #include "PassDetail.h"
17 #include "mlir/IR/Dominance.h"
21 #include "mlir/Transforms/Passes.h"
23 
24 using namespace mlir;
25 
26 namespace {
27 /// A control-flow sink pass.
28 struct ControlFlowSink : public ControlFlowSinkBase<ControlFlowSink> {
29  void runOnOperation() override;
30 };
31 } // end anonymous namespace
32 
33 void ControlFlowSink::runOnOperation() {
34  auto &domInfo = getAnalysis<DominanceInfo>();
35  getOperation()->walk([&](RegionBranchOpInterface branch) {
36  SmallVector<Region *> regionsToSink;
37  // Get the regions are that known to be executed at most once.
38  getSinglyExecutedRegionsToSink(branch, regionsToSink);
39  // Sink side-effect free operations.
40  numSunk = controlFlowSink(
41  regionsToSink, domInfo,
42  [](Operation *op, Region *) { return isSideEffectFree(op); },
43  [](Operation *op, Region *region) {
44  // Move the operation to the beginning of the region's entry block.
45  // This guarantees the preservation of SSA dominance of all of the
46  // operation's uses are in the region.
47  op->moveBefore(&region->front(), region->front().begin());
48  });
49  });
50 }
51 
52 std::unique_ptr<Pass> mlir::createControlFlowSinkPass() {
53  return std::make_unique<ControlFlowSink>();
54 }
void moveBefore(Operation *existingOp)
Unlink this operation from its current block and insert it right before existingOp which may be in th...
Definition: Operation.cpp:430
Include the generated interface declarations.
This class contains a list of basic blocks and a link to the parent operation it is attached to...
Definition: Region.h:26
Operation is a basic unit of execution within MLIR.
Definition: Operation.h:28
std::unique_ptr< Pass > createControlFlowSinkPass()
Creates a pass to perform control-flow sinking.
bool isSideEffectFree(Operation *op)
Returns true if the given operation is side-effect free.
size_t controlFlowSink(RegionRange regions, DominanceInfo &domInfo, function_ref< bool(Operation *, Region *)> shouldMoveIntoRegion, function_ref< void(Operation *, Region *)> moveIntoRegion)
Given a list of regions, perform control flow sinking on them.
void getSinglyExecutedRegionsToSink(RegionBranchOpInterface branch, SmallVectorImpl< Region *> &regions)
Populates regions with regions of the provided region branch op that are executed at most once at tha...