Skip to content

Commit e576c6b

Browse files
committed
Address review feedbacks - 3
1 parent 7a52f8f commit e576c6b

File tree

1 file changed

+22
-20
lines changed

1 file changed

+22
-20
lines changed

mlir/lib/Transforms/Utils/RegionUtils.cpp

Lines changed: 22 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -715,30 +715,29 @@ static SmallVector<SmallVector<Value, 8>, 2> pruneRedundantArguments(
715715

716716
// Go through the first list of arguments (list 0).
717717
for (unsigned j = 0; j < numArgs; ++j) {
718-
bool shouldReplaceJ = false;
719-
unsigned replacement = 0;
720718
// Look back to see if there are possible redundancies in list 0. Please
721719
// note that we are using a map to annotate when an argument was seen first
722720
// to avoid a O(N^2) algorithm. This has the drawback that if we have two
723721
// lists like:
724722
// list0: [%a, %a, %a]
725723
// list1: [%c, %b, %b]
726-
// We cannot simplify it, because firstVlaueToIdx[%a] = 0, but we cannot
724+
// We cannot simplify it, because firstValueToIdx[%a] = 0, but we cannot
727725
// point list1[1](==%b) or list1[2](==%b) to list1[0](==%c). However, since
728726
// the number of arguments can be potentially unbounded we cannot afford a
729727
// O(N^2) algorithm (to search to all the possible pairs) and we need to
730728
// accept the trade-off.
731729
unsigned k = firstValueToIdx[newArguments[0][j]];
732-
if (k != j) {
733-
shouldReplaceJ = true;
734-
replacement = k;
735-
// If a possible redundancy is found, then scan the other lists: we
736-
// can prune the arguments if and only if they are redundant in every
737-
// list.
738-
for (unsigned i = 1; i < numLists; ++i)
739-
shouldReplaceJ =
740-
shouldReplaceJ && (newArguments[i][k] == newArguments[i][j]);
741-
}
730+
if (k == j)
731+
continue;
732+
733+
bool shouldReplaceJ = true;
734+
unsigned replacement = k;
735+
// If a possible redundancy is found, then scan the other lists: we
736+
// can prune the arguments if and only if they are redundant in every
737+
// list.
738+
for (unsigned i = 1; i < numLists; ++i)
739+
shouldReplaceJ =
740+
shouldReplaceJ && (newArguments[i][k] == newArguments[i][j]);
742741
// Save the replacement.
743742
if (shouldReplaceJ)
744743
idxToReplacement[j] = replacement;
@@ -918,6 +917,8 @@ static LogicalResult mergeIdenticalBlocks(RewriterBase &rewriter,
918917
return success(anyChanged);
919918
}
920919

920+
/// If a block's argument is always the same across different invocations, then
921+
/// drop the argument and use the value directly inside the block
921922
static LogicalResult dropRedundantArguments(RewriterBase &rewriter,
922923
Block &block) {
923924
SmallVector<size_t> argsToErase;
@@ -929,7 +930,8 @@ static LogicalResult dropRedundantArguments(RewriterBase &rewriter,
929930

930931
// Go through the block predecessor and flag if they pass to the block
931932
// different values for the same argument.
932-
for (auto predIt = block.pred_begin(), predE = block.pred_end();
933+
for (Block::pred_iterator predIt = block.pred_begin(),
934+
predE = block.pred_end();
933935
predIt != predE; ++predIt) {
934936
auto branch = dyn_cast<BranchOpInterface>((*predIt)->getTerminator());
935937
if (!branch) {
@@ -941,11 +943,11 @@ static LogicalResult dropRedundantArguments(RewriterBase &rewriter,
941943
auto branchOperands = succOperands.getForwardedOperands();
942944
if (!commonValue) {
943945
commonValue = branchOperands[argIdx];
944-
} else {
945-
if (branchOperands[argIdx] != commonValue) {
946-
sameArg = false;
947-
break;
948-
}
946+
continue;
947+
}
948+
if (branchOperands[argIdx] != commonValue) {
949+
sameArg = false;
950+
break;
949951
}
950952
}
951953

@@ -959,7 +961,7 @@ static LogicalResult dropRedundantArguments(RewriterBase &rewriter,
959961
}
960962

961963
// Remove the arguments.
962-
for (auto argIdx : llvm::reverse(argsToErase)) {
964+
for (size_t argIdx : llvm::reverse(argsToErase)) {
963965
block.eraseArgument(argIdx);
964966

965967
// Remove the argument from the branch ops.

0 commit comments

Comments
 (0)