Skip to content

Commit

Permalink
Merge pull request #466 from Xilinx/matthias.cfs_config
Browse files Browse the repository at this point in the history
[MLIR] control-flow-sink: Allow to configure shouldMoveIntoRegion
  • Loading branch information
mgehre-amd authored Feb 10, 2025
2 parents e8be3be + 666ce0f commit 8bf67e1
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 13 deletions.
3 changes: 2 additions & 1 deletion mlir/include/mlir/Transforms/Passes.h
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,8 @@ createCanonicalizerPass(const GreedyRewriteConfig &config,
ArrayRef<std::string> enabledPatterns = std::nullopt);

/// Creates a pass to perform control-flow sinking.
std::unique_ptr<Pass> createControlFlowSinkPass();
std::unique_ptr<Pass> createControlFlowSinkPass(
function_ref<bool(Operation *, Region *)> shouldMoveIntoRegion = nullptr);

/// Creates a pass to perform common sub expression elimination.
std::unique_ptr<Pass> createCSEPass();
Expand Down
35 changes: 23 additions & 12 deletions mlir/lib/Transforms/ControlFlowSink.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,12 @@ using namespace mlir;
namespace {
/// A control-flow sink pass.
struct ControlFlowSink : public impl::ControlFlowSinkBase<ControlFlowSink> {
ControlFlowSink(
function_ref<bool(Operation *, Region *)> shouldMoveIntoRegion)
: shouldMoveIntoRegion(shouldMoveIntoRegion) {}
void runOnOperation() override;

function_ref<bool(Operation *, Region *)> shouldMoveIntoRegion;
};
} // end anonymous namespace

Expand All @@ -40,19 +45,25 @@ void ControlFlowSink::runOnOperation() {
SmallVector<Region *> regionsToSink;
// Get the regions are that known to be executed at most once.
getSinglyExecutedRegionsToSink(branch, regionsToSink);
// Sink side-effect free operations.
numSunk = controlFlowSink(
regionsToSink, domInfo,
[](Operation *op, Region *) { return isMemoryEffectFree(op); },
[](Operation *op, Region *region) {
// Move the operation to the beginning of the region's entry block.
// This guarantees the preservation of SSA dominance of all of the
// operation's uses are in the region.
op->moveBefore(&region->front(), region->front().begin());
});
numSunk = controlFlowSink(regionsToSink, domInfo, shouldMoveIntoRegion,
[](Operation *op, Region *region) {
// Move the operation to the beginning of the
// region's entry block. This guarantees the
// preservation of SSA dominance of all of the
// operation's uses are in the region.
op->moveBefore(&region->front(),
region->front().begin());
});
});
}

std::unique_ptr<Pass> mlir::createControlFlowSinkPass() {
return std::make_unique<ControlFlowSink>();
std::unique_ptr<Pass> mlir::createControlFlowSinkPass(
function_ref<bool(Operation *, Region *)> shouldMoveIntoRegion) {
if (!shouldMoveIntoRegion) {
// Sink side-effect free operations.
shouldMoveIntoRegion = [](Operation *op, Region *) {
return isMemoryEffectFree(op);
};
}
return std::make_unique<ControlFlowSink>(shouldMoveIntoRegion);
}

0 comments on commit 8bf67e1

Please sign in to comment.