Skip to content

Commit c768d36

Browse files
authored
Merge pull request #69847 from eeckstein/fix-simplify-begin-borrow
SimplifyBeginBorrow: don't remove begin_borrow instructions which have the `[point_escape]` flag set.
2 parents 79ac431 + 3bfabf6 commit c768d36

File tree

5 files changed

+23
-1
lines changed

5 files changed

+23
-1
lines changed

SwiftCompilerSources/Sources/Optimizer/InstructionSimplification/SimplifyBeginBorrow.swift

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,9 @@ extension BeginBorrowInst : OnoneSimplifyable {
1616
func simplify(_ context: SimplifyContext) {
1717
if borrowedValue.ownership == .owned,
1818
// We need to keep lexical lifetimes in place.
19-
!isLexical
19+
!isLexical,
20+
// The same for borrow-scopes which encapsulated pointer escapes.
21+
!hasPointerEscape
2022
{
2123
tryReplaceBorrowWithOwnedOperand(beginBorrow: self, context)
2224
}

SwiftCompilerSources/Sources/SIL/Instruction.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -881,6 +881,7 @@ final public class BeginBorrowInst : SingleValueInstruction, UnaryInstruction {
881881
}
882882

883883
public var isLexical: Bool { bridged.BeginBorrow_isLexical() }
884+
public var hasPointerEscape: Bool { bridged.BeginBorrow_hasPointerEscape() }
884885
}
885886

886887
final public class ProjectBoxInst : SingleValueInstruction, UnaryInstruction {

include/swift/SIL/SILBridging.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -594,6 +594,7 @@ struct BridgedInstruction {
594594
BRIDGED_INLINE OptionalBridgedValue StructInst_getUniqueNonTrivialFieldValue() const;
595595
BRIDGED_INLINE SwiftInt StructElementAddrInst_fieldIndex() const;
596596
BRIDGED_INLINE bool BeginBorrow_isLexical() const;
597+
BRIDGED_INLINE bool BeginBorrow_hasPointerEscape() const;
597598
BRIDGED_INLINE SwiftInt ProjectBoxInst_fieldIndex() const;
598599
BRIDGED_INLINE bool EndCOWMutationInst_doKeepUnique() const;
599600
BRIDGED_INLINE SwiftInt EnumInst_caseIndex() const;

include/swift/SIL/SILBridgingImpl.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -784,6 +784,10 @@ bool BridgedInstruction::BeginBorrow_isLexical() const {
784784
return getAs<swift::BeginBorrowInst>()->isLexical();
785785
}
786786

787+
bool BridgedInstruction::BeginBorrow_hasPointerEscape() const {
788+
return getAs<swift::BeginBorrowInst>()->hasPointerEscape();
789+
}
790+
787791
SwiftInt BridgedInstruction::ProjectBoxInst_fieldIndex() const {
788792
return getAs<swift::ProjectBoxInst>()->getFieldIndex();
789793
}

test/SILOptimizer/simplify_begin_borrow.sil

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,20 @@ bb0(%0 : @owned $C):
159159
return %6 : $()
160160
}
161161

162+
// CHECK-LABEL: sil [ossa] @dont_replace_pointer_escape :
163+
// CHECK: begin_borrow
164+
// CHECK: } // end sil function 'dont_replace_pointer_escape'
165+
sil [ossa] @dont_replace_pointer_escape : $@convention(thin) (@owned C) -> () {
166+
bb0(%0 : @owned $C):
167+
%1 = begin_borrow [pointer_escape] %0 : $C
168+
%2 = function_ref @takeC : $@convention(thin) (@guaranteed C) -> ()
169+
%3 = apply %2(%1) : $@convention(thin) (@guaranteed C) -> ()
170+
end_borrow %1 : $C
171+
destroy_value %0: $C
172+
%6 = tuple ()
173+
return %6 : $()
174+
}
175+
162176
// CHECK-LABEL: sil [ossa] @dont_replace_with_multiple_uses :
163177
// CHECK: begin_borrow
164178
// CHECK: } // end sil function 'dont_replace_with_multiple_uses'

0 commit comments

Comments
 (0)