Skip to content

Commit bf62e44

Browse files
authored
Merge pull request #69988 from eeckstein/fix-abc-opt
ABCOpts: fix a miscompile in case a value is added to the array index
2 parents 9a5974f + 0d596fd commit bf62e44

File tree

2 files changed

+45
-3
lines changed

2 files changed

+45
-3
lines changed

lib/SILOptimizer/LoopTransforms/ArrayBoundsCheckOpts.cpp

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -774,13 +774,15 @@ class AccessFunction {
774774
return nullptr;
775775
}
776776

777-
// We don't check if the second argument to the builtin is loop invariant
778-
// here, because only induction variables with a +1 incremenent are
779-
// considered for bounds check optimization.
780777
AsArg = dyn_cast<SILArgument>(Builtin->getArguments()[0]);
781778
if (!AsArg) {
782779
return nullptr;
783780
}
781+
782+
auto *incrVal = dyn_cast<IntegerLiteralInst>(Builtin->getArguments()[1]);
783+
if (!incrVal || incrVal->getValue() != 1)
784+
return nullptr;
785+
784786
preIncrement = true;
785787
}
786788

test/SILOptimizer/abcopts_ossa_guaranteed.sil

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,8 @@ bb0(%0: @guaranteed $ArrayInt):
6161
unreachable
6262
}
6363

64+
sil [ossa] @nonconst : $@convention(thin) () -> Builtin.Int32
65+
6466
sil [ossa] @unknown_func : $@convention(thin) () -> () {
6567
bb0:
6668
unreachable
@@ -1289,3 +1291,41 @@ bb6(%32 : $Builtin.Int32):
12891291
return %33 : $Int32
12901292
}
12911293

1294+
// CHECK-LABEL: sil [ossa] @non_const_post_increment :
1295+
// CHECK-NOT: apply
1296+
// CHECK: bb1({{.*}}):
1297+
// CHECK: function_ref @nonconst
1298+
// CHECK: apply
1299+
// CHECK: function_ref @checkbounds2
1300+
// CHECK: apply
1301+
// CHECK: bb2:
1302+
// CHECK: } // end sil function 'non_const_post_increment'
1303+
sil [ossa] @non_const_post_increment : $@convention(thin) (@guaranteed Array<Int>) -> () {
1304+
bb0(%0 : @guaranteed $Array<Int>):
1305+
%t = integer_literal $Builtin.Int1, -1
1306+
%ts = struct $Bool (%t : $Builtin.Int1)
1307+
%zero = integer_literal $Builtin.Int32, 0
1308+
%one = integer_literal $Builtin.Int32, 1
1309+
%ten = integer_literal $Builtin.Int32, 10
1310+
br bb1(%zero : $Builtin.Int32)
1311+
1312+
bb1(%i : $Builtin.Int32):
1313+
%a1 = builtin "sadd_with_overflow_Int32"(%i : $Builtin.Int32, %one : $Builtin.Int32, %t : $Builtin.Int1) : $(Builtin.Int32, Builtin.Int1)
1314+
%i1 = tuple_extract %a1 : $(Builtin.Int32, Builtin.Int1), 0
1315+
%nf = function_ref @nonconst : $@convention(thin) () -> Builtin.Int32
1316+
%nc = apply %nf() : $@convention(thin) () -> Builtin.Int32
1317+
%a2 = builtin "sadd_with_overflow_Int32"(%i : $Builtin.Int32, %nc : $Builtin.Int32, %t : $Builtin.Int1) : $(Builtin.Int32, Builtin.Int1)
1318+
%idx = tuple_extract %a2 : $(Builtin.Int32, Builtin.Int1), 0
1319+
%idxs = struct $Int32 (%idx : $Builtin.Int32)
1320+
%f2 = function_ref @checkbounds2 : $@convention(method) (Int32, Bool, @guaranteed Array<Int>) -> _DependenceToken
1321+
apply %f2(%idxs, %ts, %0) : $@convention(method) (Int32, Bool, @guaranteed Array<Int>) -> _DependenceToken
1322+
%c = builtin "cmp_eq_Int32"(%i1 : $Builtin.Int32, %ten : $Builtin.Int32) : $Builtin.Int1
1323+
cond_br %c, bb3, bb2
1324+
1325+
bb2:
1326+
br bb1(%i1 : $Builtin.Int32)
1327+
1328+
bb3:
1329+
%r = tuple ()
1330+
return %r : $()
1331+
}

0 commit comments

Comments
 (0)