-
Couldn't load subscription status.
- Fork 15k
[mlir] move if-condition propagation to a standalone pass #150278
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,98 @@ | ||
| //===- IfConditionPropagation.cpp -----------------------------------------===// | ||
| // | ||
| // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. | ||
| // See https://llvm.org/LICENSE.txt for license information. | ||
| // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||
| // | ||
| //===----------------------------------------------------------------------===// | ||
| // | ||
| // This file contains a pass for constant propagation of the condition of an | ||
| // `scf.if` into its then and else regions as true and false respectively. | ||
| // | ||
| //===----------------------------------------------------------------------===// | ||
|
|
||
| #include "mlir/Dialect/Arith/IR/Arith.h" | ||
| #include "mlir/Dialect/SCF/IR/SCF.h" | ||
| #include "mlir/Dialect/SCF/Transforms/Passes.h" | ||
|
|
||
| using namespace mlir; | ||
|
|
||
| namespace mlir { | ||
| #define GEN_PASS_DEF_SCFIFCONDITIONPROPAGATION | ||
| #include "mlir/Dialect/SCF/Transforms/Passes.h.inc" | ||
| } // namespace mlir | ||
|
|
||
| /// Traverses the IR recursively (on region tree) and updates the uses of a | ||
| /// value also as the condition of an `scf.if` to either `true` or `false` | ||
| /// constants in the `then` and `else regions. This is done as a single | ||
| /// post-order sweep over the IR (without `walk`) for efficiency reasons. While | ||
| /// traversing, the function maintains the set of visited regions to quickly | ||
| /// identify whether the value belong to a region that is known to be nested in | ||
| /// the `then` or `else` branch of a specific loop. | ||
| static void propagateIfConditionsImpl(Operation *root, | ||
| llvm::SmallPtrSet<Region *, 8> &visited) { | ||
| if (auto scfIf = dyn_cast<scf::IfOp>(root)) { | ||
| llvm::SmallPtrSet<Region *, 8> thenChildren, elseChildren; | ||
| // Visit the "then" region, collect children. | ||
| for (Block &block : scfIf.getThenRegion()) { | ||
| for (Operation &op : block) { | ||
| propagateIfConditionsImpl(&op, thenChildren); | ||
| } | ||
| } | ||
|
|
||
| // Visit the "else" region, collect children. | ||
| for (Block &block : scfIf.getElseRegion()) { | ||
| for (Operation &op : block) { | ||
| propagateIfConditionsImpl(&op, elseChildren); | ||
| } | ||
| } | ||
|
|
||
| // Update uses to point to constants instead. | ||
| OpBuilder builder(scfIf); | ||
| Value trueValue = arith::ConstantIntOp::create(builder, scfIf.getLoc(), | ||
| /*value=*/true, /*width=*/1); | ||
| Value falseValue = | ||
| arith::ConstantIntOp::create(builder, scfIf.getLoc(), | ||
| /*value=*/false, /*width=*/1); | ||
|
|
||
| for (OpOperand &use : scfIf.getCondition().getUses()) { | ||
| if (thenChildren.contains(use.getOwner()->getParentRegion())) | ||
| use.set(trueValue); | ||
| else if (elseChildren.contains(use.getOwner()->getParentRegion())) | ||
| use.set(falseValue); | ||
| } | ||
| if (trueValue.getUses().empty()) | ||
| trueValue.getDefiningOp()->erase(); | ||
| if (falseValue.getUses().empty()) | ||
| falseValue.getDefiningOp()->erase(); | ||
|
|
||
| // Append the two lists of children and return them. | ||
| visited.insert_range(thenChildren); | ||
| visited.insert_range(elseChildren); | ||
| return; | ||
| } | ||
|
|
||
| for (Region ®ion : root->getRegions()) { | ||
| for (Block &block : region) { | ||
| for (Operation &op : block) { | ||
| propagateIfConditionsImpl(&op, visited); | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| /// Traverses the IR recursively (on region tree) and updates the uses of a | ||
| /// value also as the condition of an `scf.if` to either `true` or `false` | ||
| /// constants in the `then` and `else regions | ||
| static void propagateIfConditions(Operation *root) { | ||
| llvm::SmallPtrSet<Region *, 8> visited; | ||
| propagateIfConditionsImpl(root, visited); | ||
| } | ||
|
|
||
| namespace { | ||
| /// Pass entrypoint. | ||
| struct SCFIfConditionPropagationPass | ||
| : impl::SCFIfConditionPropagationBase<SCFIfConditionPropagationPass> { | ||
| void runOnOperation() override { propagateIfConditions(getOperation()); } | ||
| }; | ||
| } // namespace | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,34 @@ | ||
| // RUN: mlir-opt %s --scf-if-condition-propagation --allow-unregistered-dialect | FileCheck %s | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please remove There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ping @ftynse on post-merge comments. |
||
|
|
||
| // CHECK-LABEL: @cond_prop | ||
| func.func @cond_prop(%arg0 : i1) -> index { | ||
| %res = scf.if %arg0 -> index { | ||
| %res1 = scf.if %arg0 -> index { | ||
| %v1 = "test.get_some_value1"() : () -> index | ||
| scf.yield %v1 : index | ||
| } else { | ||
| %v2 = "test.get_some_value2"() : () -> index | ||
| scf.yield %v2 : index | ||
| } | ||
| scf.yield %res1 : index | ||
| } else { | ||
| %res2 = scf.if %arg0 -> index { | ||
| %v3 = "test.get_some_value3"() : () -> index | ||
| scf.yield %v3 : index | ||
| } else { | ||
| %v4 = "test.get_some_value4"() : () -> index | ||
| scf.yield %v4 : index | ||
| } | ||
| scf.yield %res2 : index | ||
| } | ||
| return %res : index | ||
| } | ||
| // CHECK: %[[if:.+]] = scf.if %arg0 -> (index) { | ||
| // CHECK: %[[c1:.+]] = "test.get_some_value1"() : () -> index | ||
| // CHECK: scf.yield %[[c1]] : index | ||
| // CHECK: } else { | ||
| // CHECK: %[[c4:.+]] = "test.get_some_value4"() : () -> index | ||
| // CHECK: scf.yield %[[c4]] : index | ||
| // CHECK: } | ||
| // CHECK: return %[[if]] : index | ||
| // CHECK:} | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't quite understand the logic for the
visitedset, the comment on the function says:But that does not seem to be the case, the visited set is only ever used for inserting here, never checked as far as I can see?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh I see the other recursions above, where "visited" will populate the
thenChildrenandelseChildren.But if we hit a
ifOpabove, why do we need to recurse below again?