Skip to content

Commit a50796e

Browse files
authored
Revert "[semantic-arc-opts] Teach load [copy] -> load_borrow about forwarding…"
1 parent 097b1b4 commit a50796e

File tree

2 files changed

+48
-76
lines changed

2 files changed

+48
-76
lines changed

lib/SILOptimizer/Mandatory/SemanticARCOpts.cpp

Lines changed: 48 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,9 @@ STATISTIC(NumLoadCopyConvertedToLoadBorrow,
4444
///
4545
/// Semantically this implies that a value is never passed off as +1 to memory
4646
/// or another function implying it can be used everywhere at +0.
47-
static bool
48-
isDeadLiveRange(SILValue v, SmallVectorImpl<DestroyValueInst *> &destroys,
49-
SmallVectorImpl<SILInstruction *> &forwardingInsts) {
47+
static bool isDeadLiveRange(
48+
SILValue v, SmallVectorImpl<DestroyValueInst *> &destroys,
49+
NullablePtr<SmallVectorImpl<SILInstruction *>> forwardingInsts = nullptr) {
5050
assert(v.getOwnershipKind() == ValueOwnershipKind::Owned);
5151
SmallVector<Operand *, 32> worklist(v->use_begin(), v->use_end());
5252
while (!worklist.empty()) {
@@ -84,7 +84,8 @@ isDeadLiveRange(SILValue v, SmallVectorImpl<DestroyValueInst *> &destroys,
8484
//
8585
// NOTE: Today we do not support TermInsts for simplicity... we /could/
8686
// support it though if we need to.
87-
if (isa<TermInst>(user) || !isGuaranteedForwardingInst(user) ||
87+
if (forwardingInsts.isNull() || isa<TermInst>(user) ||
88+
!isGuaranteedForwardingInst(user) ||
8889
1 != count_if(user->getOperandValues(
8990
true /*ignore type dependent operands*/),
9091
[&](SILValue v) {
@@ -97,7 +98,7 @@ isDeadLiveRange(SILValue v, SmallVectorImpl<DestroyValueInst *> &destroys,
9798
// Ok, this is a forwarding instruction whose ownership we can flip from
9899
// owned -> guaranteed. Visit its users recursively to see if the the
99100
// users force the live range to be alive.
100-
forwardingInsts.push_back(user);
101+
forwardingInsts.get()->push_back(user);
101102
for (SILValue v : user->getResults()) {
102103
if (v.getOwnershipKind() != ValueOwnershipKind::Owned)
103104
continue;
@@ -297,49 +298,6 @@ static bool canHandleOperand(SILValue operand, SmallVectorImpl<SILValue> &out) {
297298
return all_of(out, canHandleValue);
298299
}
299300

300-
static void convertForwardingInstsFromOwnedToGuaranteed(
301-
SmallVectorImpl<SILInstruction *> &guaranteedForwardingInsts) {
302-
// Then change all of our guaranteed forwarding insts to have guaranteed
303-
// ownership kind instead of what ever they previously had (ignoring trivial
304-
// results);
305-
while (!guaranteedForwardingInsts.empty()) {
306-
auto *i = guaranteedForwardingInsts.pop_back_val();
307-
assert(i->hasResults());
308-
309-
for (SILValue result : i->getResults()) {
310-
if (auto *svi = dyn_cast<OwnershipForwardingSingleValueInst>(result)) {
311-
if (svi->getOwnershipKind() == ValueOwnershipKind::Owned) {
312-
svi->setOwnershipKind(ValueOwnershipKind::Guaranteed);
313-
}
314-
continue;
315-
}
316-
317-
if (auto *ofci = dyn_cast<OwnershipForwardingConversionInst>(result)) {
318-
if (ofci->getOwnershipKind() == ValueOwnershipKind::Owned) {
319-
ofci->setOwnershipKind(ValueOwnershipKind::Guaranteed);
320-
}
321-
continue;
322-
}
323-
324-
if (auto *sei = dyn_cast<OwnershipForwardingSelectEnumInstBase>(result)) {
325-
if (sei->getOwnershipKind() == ValueOwnershipKind::Owned) {
326-
sei->setOwnershipKind(ValueOwnershipKind::Guaranteed);
327-
}
328-
continue;
329-
}
330-
331-
if (auto *mvir = dyn_cast<MultipleValueInstructionResult>(result)) {
332-
if (mvir->getOwnershipKind() == ValueOwnershipKind::Owned) {
333-
mvir->setOwnershipKind(ValueOwnershipKind::Guaranteed);
334-
}
335-
continue;
336-
}
337-
338-
llvm_unreachable("unhandled forwarding instruction?!");
339-
}
340-
}
341-
}
342-
343301
// Eliminate a copy of a borrowed value, if:
344302
//
345303
// 1. All of the copies users do not consume the copy (and thus can accept a
@@ -387,7 +345,7 @@ bool SemanticARCOptVisitor::performGuaranteedCopyValueOptimization(CopyValueInst
387345
// value (e.x. storing into memory).
388346
SmallVector<DestroyValueInst *, 16> destroys;
389347
SmallVector<SILInstruction *, 16> guaranteedForwardingInsts;
390-
if (!isDeadLiveRange(cvi, destroys, guaranteedForwardingInsts))
348+
if (!isDeadLiveRange(cvi, destroys, &guaranteedForwardingInsts))
391349
return false;
392350

393351
// Next check if we have any destroys at all of our copy_value and an operand
@@ -494,8 +452,47 @@ bool SemanticARCOptVisitor::performGuaranteedCopyValueOptimization(CopyValueInst
494452
}
495453

496454
eraseAndRAUWSingleValueInstruction(cvi, cvi->getOperand());
497-
convertForwardingInstsFromOwnedToGuaranteed(guaranteedForwardingInsts);
498455

456+
// Then change all of our guaranteed forwarding insts to have guaranteed
457+
// ownership kind instead of what ever they previously had (ignoring trivial
458+
// results);
459+
while (!guaranteedForwardingInsts.empty()) {
460+
auto *i = guaranteedForwardingInsts.pop_back_val();
461+
462+
assert(i->hasResults());
463+
464+
for (SILValue result : i->getResults()) {
465+
if (auto *svi = dyn_cast<OwnershipForwardingSingleValueInst>(result)) {
466+
if (svi->getOwnershipKind() == ValueOwnershipKind::Owned) {
467+
svi->setOwnershipKind(ValueOwnershipKind::Guaranteed);
468+
}
469+
continue;
470+
}
471+
472+
if (auto *ofci = dyn_cast<OwnershipForwardingConversionInst>(result)) {
473+
if (ofci->getOwnershipKind() == ValueOwnershipKind::Owned) {
474+
ofci->setOwnershipKind(ValueOwnershipKind::Guaranteed);
475+
}
476+
continue;
477+
}
478+
479+
if (auto *sei = dyn_cast<OwnershipForwardingSelectEnumInstBase>(result)) {
480+
if (sei->getOwnershipKind() == ValueOwnershipKind::Owned) {
481+
sei->setOwnershipKind(ValueOwnershipKind::Guaranteed);
482+
}
483+
continue;
484+
}
485+
486+
if (auto *mvir = dyn_cast<MultipleValueInstructionResult>(result)) {
487+
if (mvir->getOwnershipKind() == ValueOwnershipKind::Owned) {
488+
mvir->setOwnershipKind(ValueOwnershipKind::Guaranteed);
489+
}
490+
continue;
491+
}
492+
493+
llvm_unreachable("unhandled forwarding instruction?!");
494+
}
495+
}
499496
++NumEliminatedInsts;
500497
return true;
501498
}
@@ -738,8 +735,7 @@ bool SemanticARCOptVisitor::visitLoadInst(LoadInst *li) {
738735
// -> load_borrow if we can put a copy_value on a cold path and thus
739736
// eliminate RR traffic on a hot path.
740737
SmallVector<DestroyValueInst *, 32> destroyValues;
741-
SmallVector<SILInstruction *, 16> guaranteedForwardingInsts;
742-
if (!isDeadLiveRange(li, destroyValues, guaranteedForwardingInsts))
738+
if (!isDeadLiveRange(li, destroyValues))
743739
return false;
744740

745741
// Then check if our address is ever written to. If it is, then we
@@ -764,8 +760,6 @@ bool SemanticARCOptVisitor::visitLoadInst(LoadInst *li) {
764760
}
765761

766762
eraseAndRAUWSingleValueInstruction(li, lbi);
767-
convertForwardingInstsFromOwnedToGuaranteed(guaranteedForwardingInsts);
768-
769763
++NumEliminatedInsts;
770764
++NumLoadCopyConvertedToLoadBorrow;
771765
return true;

test/SILOptimizer/semantic-arc-opts.sil

Lines changed: 0 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -437,28 +437,6 @@ bb0(%x : @guaranteed $ClassLet):
437437
return undef : $()
438438
}
439439

440-
// CHECK-LABEL: sil [ossa] @dont_copy_let_properties_with_guaranteed_base_and_forwarding_uses :
441-
// CHECK: ref_element_addr
442-
// CHECK-NEXT: load_borrow
443-
// CHECK-NEXT: unchecked_ref_cast
444-
// CHECK-NEXT: apply
445-
// CHECK-NEXT: end_borrow
446-
// CHECK-NEXT: return
447-
// CHECK: } // end sil function 'dont_copy_let_properties_with_guaranteed_base_and_forwarding_uses'
448-
sil [ossa] @dont_copy_let_properties_with_guaranteed_base_and_forwarding_uses : $@convention(thin) (@guaranteed ClassLet) -> () {
449-
bb0(%x : @guaranteed $ClassLet):
450-
%f = function_ref @black_hole : $@convention(thin) (@guaranteed Klass) -> ()
451-
452-
%p = ref_element_addr %x : $ClassLet, #ClassLet.aLet
453-
%v = load [copy] %p : $*Klass
454-
%c = unchecked_ref_cast %v : $Klass to $Klass
455-
%b = begin_borrow %c : $Klass
456-
apply %f(%b) : $@convention(thin) (@guaranteed Klass) -> ()
457-
end_borrow %b : $Klass
458-
destroy_value %c : $Klass
459-
return undef : $()
460-
}
461-
462440
// CHECK-LABEL: sil [ossa] @dont_copy_let_properties_with_guaranteed_upcast_base
463441
// CHECK: ref_element_addr
464442
// CHECK-NEXT: load_borrow

0 commit comments

Comments
 (0)