[Mlir-commits] [mlir] [mlir][cf] Canonicalize block args with uniform incoming values (PR #183966)
Mehdi Amini
llvmlistbot at llvm.org
Sat Feb 28 15:29:52 PST 2026
================
@@ -204,9 +204,85 @@ static LogicalResult simplifyPassThroughBr(BranchOp op,
return success();
}
+/// %c = arith.constant 0 : i32
+/// cf.br ^bb1(%c : i32) // pred 1
+/// cf.br ^bb1(%c : i32) // pred 2
+/// ^bb1(%arg0: i32):
+/// use(%arg0)
+/// ->
+/// ^bb1(%arg0: i32):
+/// use(%c) // %arg0 has no uses and can be removed
+///
+/// If all incoming values for a block argument from all predecessors are the
+/// same SSA value, replace uses of the block argument with that value. This
+/// allows the block argument to be removed by dead code elimination.
+static bool simplifyUniformBlockArgs(Block *dest, PatternRewriter &rewriter) {
+ if (dest->hasNoPredecessors() ||
+ llvm::hasSingleElement(dest->getPredecessors()))
+ return false;
+
+ bool changed = false;
+ for (BlockArgument arg : dest->getArguments()) {
+ if (arg.use_empty())
+ continue;
+
+ Value commonValue;
+ bool allSame = true;
+ for (Block *pred : dest->getPredecessors()) {
+ auto branch = dyn_cast<BranchOpInterface>(pred->getTerminator());
+ if (!branch) {
+ allSame = false;
+ break;
+ }
+
+ Value incoming;
+ for (unsigned i = 0; i < branch->getNumSuccessors(); ++i) {
----------------
joker-eph wrote:
```suggestion
for (int i : llvm::seq<int>(0, branch->getNumSuccessors())) {
```
Nit: this makes it more clear that `i` isn't modified in the loop.
https://github.com/llvm/llvm-project/pull/183966
More information about the Mlir-commits
mailing list