@@ -3885,8 +3885,8 @@ struct WhileRemoveDuplicatedResults : public OpRewritePattern<WhileOp> {
3885
3885
}
3886
3886
};
3887
3887
3888
- // / If both ranges contain same values return mappping indices from args1 to
3889
- // / args2 . Otherwise return std::nullopt
3888
+ // / If both ranges contain same values return mappping indices from args2 to
3889
+ // / args1 . Otherwise return std::nullopt.
3890
3890
static std::optional<SmallVector<unsigned >> getArgsMapping (ValueRange args1,
3891
3891
ValueRange args2) {
3892
3892
if (args1.size () != args2.size ())
@@ -3898,16 +3898,26 @@ static std::optional<SmallVector<unsigned>> getArgsMapping(ValueRange args1,
3898
3898
if (it == args2.end ())
3899
3899
return std::nullopt;
3900
3900
3901
- auto j = it - args2.begin ();
3902
- ret[j] = static_cast <unsigned >(i);
3901
+ ret[std::distance (args2.begin (), it)] = static_cast <unsigned >(i);
3903
3902
}
3904
3903
3905
3904
return ret;
3906
3905
}
3907
3906
3907
+ static bool hasDuplicates (ValueRange args) {
3908
+ llvm::SmallDenseSet<Value> set;
3909
+ for (Value arg : args) {
3910
+ if (set.contains (arg))
3911
+ return true ;
3912
+
3913
+ set.insert (arg);
3914
+ }
3915
+ return false ;
3916
+ }
3917
+
3908
3918
// / If `before` block args are directly forwarded to `scf.condition`, rearrange
3909
3919
// / `scf.condition` args into same order as block args. Update `after` block
3910
- // args and results values accordingly.
3920
+ // args and op result values accordingly.
3911
3921
// / Needed to simplify `scf.while` -> `scf.for` uplifting.
3912
3922
struct WhileOpAlignBeforeArgs : public OpRewritePattern <WhileOp> {
3913
3923
using OpRewritePattern::OpRewritePattern;
@@ -3921,6 +3931,9 @@ struct WhileOpAlignBeforeArgs : public OpRewritePattern<WhileOp> {
3921
3931
if (beforeArgs == termArgs)
3922
3932
return failure ();
3923
3933
3934
+ if (hasDuplicates (termArgs))
3935
+ return failure ();
3936
+
3924
3937
auto mapping = getArgsMapping (beforeArgs, termArgs);
3925
3938
if (!mapping)
3926
3939
return failure ();
0 commit comments