Skip to content

Commit d03b23c

Browse files
Merge pull request #40458 from nate-chandler/loop-rotate/dont-reborrow-when-ownership-forwarded
[LoopRotate] Don't phi forwarded guaranteed value.
2 parents f95748c + 3e8df52 commit d03b23c

File tree

2 files changed

+57
-0
lines changed

2 files changed

+57
-0
lines changed

lib/SILOptimizer/LoopTransforms/LoopRotate.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ canDuplicateOrMoveToPreheader(SILLoop *loop, SILBasicBlock *preheader,
6969
llvm::DenseSet<SILInstruction *> invariants;
7070
int cost = 0;
7171
for (auto &instRef : *bb) {
72+
OwnershipForwardingMixin *ofm = nullptr;
7273
auto *inst = &instRef;
7374
if (auto *MI = dyn_cast<MethodInst>(inst)) {
7475
if (MI->getMember().isForeign)
@@ -95,6 +96,9 @@ canDuplicateOrMoveToPreheader(SILLoop *loop, SILBasicBlock *preheader,
9596
} else if (isa<IntegerLiteralInst>(inst)) {
9697
moves.push_back(inst);
9798
invariants.insert(inst);
99+
} else if ((ofm = OwnershipForwardingMixin::get(inst)) &&
100+
ofm->getForwardingOwnershipKind() == OwnershipKind::Guaranteed) {
101+
return false;
98102
} else if (!inst->mayHaveSideEffects() && !inst->mayReadFromMemory()
99103
&& !isa<TermInst>(inst) && !isa<AllocationInst>(inst)
100104
&& /* not marked mayhavesideffects */

test/SILOptimizer/looprotate_nontrivial_ossa.sil

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@ class Klass {
88

99
}
1010

11+
struct BoxStruct {
12+
var guts: Klass
13+
}
14+
1115
sil [ossa] @useKlass : $@convention(thin) (@guaranteed Klass) -> ()
1216

1317
sil [ossa] @klassIdentity : $@convention(thin) (@owned Klass) -> @owned Klass
@@ -195,3 +199,52 @@ exit:
195199
%retval = tuple ()
196200
return %retval : $()
197201
}
202+
203+
// A guaranteed value whose ownership has been forwarded must not be reborrowed.
204+
//
205+
// CHECK-LABEL: sil [ossa] @forwarded_borrow_cant_be_reborrowed : $@convention(thin) (@owned BoxStruct) -> () {
206+
// CHECK-NOT: {{bb[0-9]+}}({{%[^,]+}} : @guaranteed $BoxStruct, {{%[^,]+}} : @guaranteed $Klass):
207+
// CHECK-LABEL: } // end sil function 'forwarded_borrow_cant_be_reborrowed'
208+
sil [ossa] @forwarded_borrow_cant_be_reborrowed : $@convention(thin) (@owned BoxStruct) -> () {
209+
bb0(%0 : @owned $BoxStruct):
210+
br bb1
211+
212+
bb1:
213+
%2 = integer_literal $Builtin.Int1, 0
214+
cond_br %2, bb2, bb3
215+
216+
bb2:
217+
br bb10
218+
219+
bb3:
220+
%5 = begin_borrow %0 : $BoxStruct
221+
%6 = struct_extract %5 : $BoxStruct, #BoxStruct.guts
222+
%7 = integer_literal $Builtin.Int1, 0
223+
cond_br %7, bb4, bb5
224+
225+
bb4:
226+
unreachable
227+
228+
bb5:
229+
%10 = begin_borrow %6 : $Klass
230+
end_borrow %10 : $Klass
231+
end_borrow %5 : $BoxStruct
232+
br bb6
233+
234+
bb6:
235+
br bb7
236+
237+
238+
bb7:
239+
%16 = integer_literal $Builtin.Int1, -1
240+
cond_br %16, bb8, bb9
241+
242+
bb8:
243+
br bb10
244+
245+
bb9:
246+
br bb1
247+
248+
bb10:
249+
unreachable
250+
}

0 commit comments

Comments
 (0)