Skip to content

Commit b018645

Browse files
committed
[semantic-arc] Add __swift_endBorrow support to LLVMPasses and make sure it is removed by LLVMARCContract.cpp.
1 parent 1ed04f8 commit b018645

File tree

5 files changed

+27
-2
lines changed

5 files changed

+27
-2
lines changed

lib/LLVMPasses/LLVMARCContract.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -273,9 +273,10 @@ bool SwiftARCContractImpl::run() {
273273
case RT_UnknownReleaseN:
274274
case RT_BridgeReleaseN:
275275
llvm_unreachable("These are only created by LLVMARCContract !");
276-
// Delete all fix lifetime instructions. After llvm-ir they have no use
277-
// and show up as calls in the final binary.
276+
// Delete all fix lifetime and end borrow instructions. After llvm-ir they
277+
// have no use and show up as calls in the final binary.
278278
case RT_FixLifetime:
279+
case RT_EndBorrow:
279280
Inst.eraseFromParent();
280281
++NumNoopDeleted;
281282
continue;

lib/LLVMPasses/LLVMARCOpts.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,7 @@ static bool canonicalizeInputFunction(Function &F, ARCEntryPointBuilder &B,
102102
case RT_BridgeRelease:
103103
case RT_AllocObject:
104104
case RT_FixLifetime:
105+
case RT_EndBorrow:
105106
case RT_NoMemoryAccessed:
106107
case RT_RetainUnowned:
107108
case RT_CheckUnowned:
@@ -368,6 +369,7 @@ static bool performLocalReleaseMotion(CallInst &Release, BasicBlock &BB,
368369
}
369370

370371
case RT_FixLifetime:
372+
case RT_EndBorrow:
371373
case RT_RetainUnowned:
372374
case RT_CheckUnowned:
373375
case RT_Unknown:
@@ -441,6 +443,7 @@ static bool performLocalRetainMotion(CallInst &Retain, BasicBlock &BB,
441443
break;
442444

443445
case RT_FixLifetime: // This only stops release motion. Retains can move over it.
446+
case RT_EndBorrow:
444447
break;
445448

446449
case RT_Retain:
@@ -588,6 +591,7 @@ static DtorKind analyzeDestructor(Value *P) {
588591
case RT_NoMemoryAccessed:
589592
case RT_AllocObject:
590593
case RT_FixLifetime:
594+
case RT_EndBorrow:
591595
case RT_CheckUnowned:
592596
// Skip over random instructions that don't touch memory in the caller.
593597
continue;
@@ -717,6 +721,7 @@ static bool performStoreOnlyObjectElimination(CallInst &Allocation,
717721
case RT_Release:
718722
case RT_Retain:
719723
case RT_FixLifetime:
724+
case RT_EndBorrow:
720725
case RT_CheckUnowned:
721726
// It is perfectly fine to eliminate various retains and releases of this
722727
// object: we are zapping all accesses or none.

lib/LLVMPasses/LLVMSwift.def

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,15 @@ SWIFT_FUNC(BridgeRelease, ModRef, bridgeObjectRelease)
129129
/// void swift_bridgeObjectRelease_n(%swift.refcounted* %P)
130130
SWIFT_FUNC(BridgeReleaseN, ModRef, bridgeObjectRelease_n)
131131

132+
/// borrow source is the value that was borrowed from. borrow_dest is the
133+
/// borrowed ref.
134+
///
135+
/// TODO: We may want to communicate to the optimizer that this does not have
136+
/// global effects.
137+
///
138+
/// void __swift_endBorrow(i8* %borrow_source, i8* %borrow_dest)
139+
SWIFT_INTERNAL_FUNC_NEVER_NONATOMIC(EndBorrow, ModRef, endBorrow)
140+
132141
/// This is not a runtime function that we support. Maybe it is not a call,
133142
/// or is a call to something we don't care about.
134143
KIND(Unknown, ModRef)

lib/LLVMPasses/LLVMSwiftRCIdentity.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ SwiftRCIdentity::stripReferenceForwarding(llvm::Value *Val) {
5757
case RT_RetainUnowned:
5858
case RT_CheckUnowned:
5959
case RT_ObjCRelease:
60+
case RT_EndBorrow:
6061
break;
6162
// ObjC forwards references.
6263
case RT_ObjCRetain:

test/LLVMPasses/contract.ll

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ declare void @noread_user(%swift.refcounted*) readnone
1919
declare void @user(%swift.refcounted*)
2020
declare void @noread_user_bridged(%swift.bridge*) readnone
2121
declare void @user_bridged(%swift.bridge*)
22+
declare void @__swift_endBorrow(i8*, i8*)
2223

2324
; CHECK-LABEL: define{{( protected)?}} void @fixlifetime_removal(i8*) {
2425
; CHECK-NOT: call void @__swift_fixLifetime
@@ -29,6 +30,14 @@ entry:
2930
ret void
3031
}
3132

33+
; CHECK-LABEL: define{{( protected)?}} void @endBorrow_removal(i8*, i8*) {
34+
; CHECK-NOT: call void @__swift_endBorrow
35+
define void @endBorrow_removal(i8*, i8*) {
36+
entry:
37+
call void @__swift_endBorrow(i8* %0, i8* %1)
38+
ret void
39+
}
40+
3241
; CHECK-LABEL: define{{( protected)?}} %swift.refcounted* @swift_contractRetainN(%swift.refcounted* %A) {
3342
; CHECK: entry:
3443
; CHECK-NEXT: br i1 undef

0 commit comments

Comments
 (0)