Skip to content

Commit b637f06

Browse files
committed
[SIL] Bridge findPointerEscape() and fix OnoneSimplifyable
Do not bridge the hasPointerEscape flag until it is implemented.
1 parent d40fa08 commit b637f06

File tree

9 files changed

+28
-11
lines changed

9 files changed

+28
-11
lines changed

SwiftCompilerSources/Sources/Optimizer/InstructionSimplification/SimplifyBeginBorrow.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ extension BeginBorrowInst : OnoneSimplifyable {
1818
// We need to keep lexical lifetimes in place.
1919
!isLexical,
2020
// The same for borrow-scopes which encapsulated pointer escapes.
21-
!hasPointerEscape
21+
!findPointerEscapingUse(of: borrowedValue)
2222
{
2323
tryReplaceBorrowWithOwnedOperand(beginBorrow: self, context)
2424
}

SwiftCompilerSources/Sources/Optimizer/Utilities/ForwardingUtils.swift

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,20 @@
1212

1313
import SIL
1414

15+
/// Return true if any use in `value`s forward-extend lifetime has
16+
/// .pointerEscape operand ownership.
17+
///
18+
/// TODO: the C++ implementation does not yet gather all
19+
/// forward-extended lifetime introducers.
20+
///
21+
/// TODO: Add [pointer_escape] flags to MoveValue, BeginBorrow, and
22+
/// Allocation. Then add a `Value.hasPointerEscapingUse` property that
23+
/// performs the use-def walk to pickup the flags. Then only call into
24+
/// this def-use walk to initially set the flags.
25+
func findPointerEscapingUse(of value: Value) -> Bool {
26+
value.bridged.findPointerEscape()
27+
}
28+
1529
// Visit the introducers of a forwarded lifetime (Value -> LifetimeIntroducer).
1630
//
1731
// A lifetime introducer produces an initial OSSA lifetime which may be extended by forwarding instructions. The introducer is never itself the result of a ForwardingInstruction. Example:

SwiftCompilerSources/Sources/Optimizer/Utilities/OptUtils.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
//===--- OptUtils.swift - Utilities for optimizations ----------------------===//
1+
//===--- OptUtils.swift - Utilities for optimizations ---------------------===//
22
//
33
// This source file is part of the Swift.org open source project
44
//

SwiftCompilerSources/Sources/SIL/Instruction.swift

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -880,7 +880,6 @@ final public class BeginBorrowInst : SingleValueInstruction, UnaryInstruction, B
880880
public var borrowedValue: Value { operand.value }
881881

882882
public var isLexical: Bool { bridged.BeginBorrow_isLexical() }
883-
public var hasPointerEscape: Bool { bridged.BeginBorrow_hasPointerEscape() }
884883
}
885884

886885
final public class ProjectBoxInst : SingleValueInstruction, UnaryInstruction {

include/swift/SIL/SILBridging.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,8 @@ struct BridgedValue {
193193
SWIFT_IMPORT_UNSAFE BRIDGED_INLINE OptionalBridgedOperand getFirstUse() const;
194194
SWIFT_IMPORT_UNSAFE BRIDGED_INLINE BridgedType getType() const;
195195
BRIDGED_INLINE Ownership getOwnership() const;
196+
197+
bool findPointerEscape() const;
196198
};
197199

198200
struct OptionalBridgedValue {
@@ -625,7 +627,6 @@ struct BridgedInstruction {
625627
BRIDGED_INLINE OptionalBridgedValue StructInst_getUniqueNonTrivialFieldValue() const;
626628
BRIDGED_INLINE SwiftInt StructElementAddrInst_fieldIndex() const;
627629
BRIDGED_INLINE bool BeginBorrow_isLexical() const;
628-
BRIDGED_INLINE bool BeginBorrow_hasPointerEscape() const;
629630
BRIDGED_INLINE SwiftInt ProjectBoxInst_fieldIndex() const;
630631
BRIDGED_INLINE bool EndCOWMutationInst_doKeepUnique() const;
631632
BRIDGED_INLINE SwiftInt EnumInst_caseIndex() const;

include/swift/SIL/SILBridgingImpl.h

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

821-
bool BridgedInstruction::BeginBorrow_hasPointerEscape() const {
822-
return getAs<swift::BeginBorrowInst>()->hasPointerEscape();
823-
}
824-
825821
SwiftInt BridgedInstruction::ProjectBoxInst_fieldIndex() const {
826822
return getAs<swift::ProjectBoxInst>()->getFieldIndex();
827823
}

include/swift/SIL/SILInstruction.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4494,6 +4494,7 @@ class BeginBorrowInst
44944494
/// doesn't have a lexical bit, do not do anything.
44954495
void removeIsLexical() { sharedUInt8().BeginBorrowInst.lexical = false; }
44964496

4497+
/// WARNING: this flag is not yet implemented!
44974498
bool hasPointerEscape() const {
44984499
return sharedUInt8().BeginBorrowInst.pointerEscape;
44994500
}

lib/SIL/Utils/SILBridging.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
#include "swift/AST/Attr.h"
2121
#include "swift/AST/SemanticAttrs.h"
2222
#include "swift/SIL/MemAccessUtils.h"
23+
#include "swift/SIL/OwnershipUtils.h"
2324
#include "swift/SIL/ParseTestSpecification.h"
2425
#include "swift/SIL/SILBuilder.h"
2526
#include "swift/SIL/SILGlobalVariable.h"
@@ -273,6 +274,9 @@ ArrayRef<SILValue> BridgedValueArray::getValues(SmallVectorImpl<SILValue> &stora
273274
return storage;
274275
}
275276

277+
bool BridgedValue::findPointerEscape() const {
278+
return swift::findPointerEscape(getSILValue());
279+
}
276280

277281
//===----------------------------------------------------------------------===//
278282
// SILArgument

test/SILOptimizer/simplify_begin_borrow.sil

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -159,9 +159,11 @@ 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'
162+
// FIXME: re-enable this test when the [pointer_escape] flag is implemented.
163+
//
164+
// HECK-LABEL: sil [ossa] @dont_replace_pointer_escape :
165+
// HECK: begin_borrow
166+
// HECK: } // end sil function 'dont_replace_pointer_escape'
165167
sil [ossa] @dont_replace_pointer_escape : $@convention(thin) (@owned C) -> () {
166168
bb0(%0 : @owned $C):
167169
%1 = begin_borrow [pointer_escape] %0 : $C

0 commit comments

Comments
 (0)