@@ -679,12 +679,15 @@ static bool ableToUpdatePredOperands(Block *block) {
679
679
return true ;
680
680
}
681
681
682
- // / Prunes the redundant list of arguments. E.g., if we are passing an argument
683
- // / list like [x, y, z, x] this would return [x, y, z] and it would update the
684
- // / `block` (to whom the argument are passed to) accordingly.
682
+ // / Prunes the redundant list of new arguments. E.g., if we are passing an
683
+ // / argument list like [x, y, z, x] this would return [x, y, z] and it would
684
+ // / update the `block` (to whom the argument are passed to) accordingly. The new
685
+ // / arguments are passed as arguments at the back of the block, hence we need to
686
+ // / know how many `numOldArguments` were before, in order to correctly replace
687
+ // / the new arguments in the block
685
688
static SmallVector<SmallVector<Value, 8 >, 2 > pruneRedundantArguments (
686
689
const SmallVector<SmallVector<Value, 8 >, 2 > &newArguments,
687
- RewriterBase &rewriter, Block *block) {
690
+ RewriterBase &rewriter, unsigned numOldArguments, Block *block) {
688
691
689
692
SmallVector<SmallVector<Value, 8 >, 2 > newArgumentsPruned (
690
693
newArguments.size (), SmallVector<Value, 8 >());
@@ -751,10 +754,11 @@ static SmallVector<SmallVector<Value, 8>, 2> pruneRedundantArguments(
751
754
SmallVector<unsigned > toErase;
752
755
for (auto [idx, arg] : llvm::enumerate (block->getArguments ())) {
753
756
if (idxToReplacement.contains (idx)) {
754
- Value oldArg = block->getArgument (idx);
755
- Value newArg = block->getArgument (idxToReplacement[idx]);
757
+ Value oldArg = block->getArgument (numOldArguments + idx);
758
+ Value newArg =
759
+ block->getArgument (numOldArguments + idxToReplacement[idx]);
756
760
rewriter.replaceAllUsesWith (oldArg, newArg);
757
- toErase.push_back (idx);
761
+ toErase.push_back (numOldArguments + idx);
758
762
}
759
763
}
760
764
@@ -793,6 +797,7 @@ LogicalResult BlockMergeCluster::merge(RewriterBase &rewriter) {
793
797
1 + blocksToMerge.size (),
794
798
SmallVector<Value, 8 >(operandsToMerge.size ()));
795
799
unsigned curOpIndex = 0 ;
800
+ unsigned numOldArguments = leaderBlock->getNumArguments ();
796
801
for (const auto &it : llvm::enumerate (operandsToMerge)) {
797
802
unsigned nextOpOffset = it.value ().first - curOpIndex;
798
803
curOpIndex = it.value ().first ;
@@ -814,7 +819,8 @@ LogicalResult BlockMergeCluster::merge(RewriterBase &rewriter) {
814
819
}
815
820
816
821
// Prune redundant arguments and update the leader block argument list
817
- newArguments = pruneRedundantArguments (newArguments, rewriter, leaderBlock);
822
+ newArguments = pruneRedundantArguments (newArguments, rewriter,
823
+ numOldArguments, leaderBlock);
818
824
819
825
// Update the predecessors for each of the blocks.
820
826
auto updatePredecessors = [&](Block *block, unsigned clusterIndex) {
0 commit comments