Skip to content

[closure-lifetime-fixup] When emitting a load_borrow, make sure to em… #20079

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions include/swift/SIL/SILBuilder.h
Original file line number Diff line number Diff line change
Expand Up @@ -665,6 +665,13 @@ class SILBuilder {
BeginBorrowInst(getSILDebugLocation(Loc), LV));
}

// Pass in an address or value, perform a begin_borrow/load_borrow and pass
// the value to the passed in closure. After the closure has finished
// executing, automatically insert the end_borrow. The closure can assume that
// it will receive a loaded loadable value.
void emitScopedBorrowOperation(SILLocation loc, SILValue original,
function_ref<void(SILValue)> &&fun);

/// Utility function that returns a trivial store if the stored type is
/// trivial and a \p Qualifier store if the stored type is non-trivial.
///
Expand Down
13 changes: 13 additions & 0 deletions lib/SIL/SILBuilder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -567,3 +567,16 @@ DebugValueAddrInst *SILBuilder::createDebugValueAddr(SILLocation Loc,
return insert(DebugValueAddrInst::create(getSILDebugLocation(Loc), src,
getModule(), Var));
}

void SILBuilder::emitScopedBorrowOperation(SILLocation loc, SILValue original,
function_ref<void(SILValue)> &&fun) {
if (original->getType().isAddress()) {
original = createLoadBorrow(loc, original);
} else {
original = createBeginBorrow(loc, original);
}

fun(original);

createEndBorrow(loc, original);
}
10 changes: 6 additions & 4 deletions lib/SILOptimizer/Mandatory/ClosureLifetimeFixup.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -459,10 +459,12 @@ static bool fixupCopyBlockWithoutEscaping(CopyBlockWithoutEscapingInst *CB) {

for (auto LifetimeEndPoint : LifetimeEndPoints) {
SILBuilderWithScope B(LifetimeEndPoint);
auto IsEscaping =
B.createIsEscapingClosure(Loc, B.createLoadBorrow(generatedLoc, Slot),
IsEscapingClosureInst::ObjCEscaping);
B.createCondFail(Loc, IsEscaping);
SILValue isEscaping;
B.emitScopedBorrowOperation(generatedLoc, Slot, [&](SILValue value) {
isEscaping = B.createIsEscapingClosure(
Loc, value, IsEscapingClosureInst::ObjCEscaping);
});
B.createCondFail(Loc, isEscaping);
B.createDestroyAddr(generatedLoc, Slot);
// Store None to it.
B.createStore(generatedLoc,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@

#define SWIFT_NOESCAPE __attribute__((__noescape__))

typedef void (^block_t)(void);

block_t block_create_noescape(block_t SWIFT_NOESCAPE block);
5 changes: 5 additions & 0 deletions test/SILOptimizer/Inputs/usr/include/module.map
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@

module ClosureLifetimeFixupObjC {
header "closure_lifetime_fixup_objc.h"
export *
}
2 changes: 1 addition & 1 deletion test/SILOptimizer/closure_lifetime_fixup.swift
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// RUN: %target-swift-frontend %s -emit-sil -o - | %FileCheck %s
// RUN: %target-swift-frontend %s -sil-verify-all -enable-sil-ownership -emit-sil -o - | %FileCheck %s

func use_closure(_ c : () -> () ) {
c()
Expand Down
10 changes: 9 additions & 1 deletion test/SILOptimizer/closure_lifetime_fixup_objc.swift
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
// RUN: %target-swift-frontend %s -emit-sil -o - | %FileCheck %s
// RUN: %target-swift-frontend %s -enable-sil-ownership -sil-verify-all -emit-sil -o - -I %S/Inputs/usr/include | %FileCheck %s
// REQUIRES: objc_interop

import Foundation
import ClosureLifetimeFixupObjC

@objc
public protocol DangerousEscaper {
Expand Down Expand Up @@ -100,3 +101,10 @@ class C: NSObject {
getDispatchQueue().sync(execute: { _ = self })
}
}

// Make sure that we obey ownership invariants when we emit load_borrow.
internal typealias MyBlock = @convention(block) () -> Void
func getBlock(noEscapeBlock: () -> Void ) -> MyBlock {
return block_create_noescape(noEscapeBlock)
}