Skip to content

EscapeUtils: handle load_borrow #69722

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
merged 2 commits into from
Nov 9, 2023
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
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ extension ProjectedValue {
_ context: some Context) -> V.Result? {
var walker = EscapeWalker(visitor: visitor, complexityBudget: complexityBudget, context)
if walker.walkUp(addressOrValue: value, path: path.escapePath) == .abortWalk {
walker.visitor.cleanupOnAbort()
return nil
}
return walker.visitor.result
Expand All @@ -119,6 +120,7 @@ extension ProjectedValue {
_ context: some Context) -> V.Result? {
var walker = EscapeWalker(visitor: visitor, context)
if walker.walkDown(addressOrValue: value, path: path.escapePath) == .abortWalk {
walker.visitor.cleanupOnAbort()
return nil
}
return walker.visitor.result
Expand Down Expand Up @@ -182,6 +184,12 @@ extension EscapeVisitor {
protocol EscapeVisitorWithResult : EscapeVisitor {
associatedtype Result
var result: Result { get }

mutating func cleanupOnAbort()
}

extension EscapeVisitorWithResult {
mutating func cleanupOnAbort() {}
}

// FIXME: This ought to be marked private, but that triggers a compiler bug
Expand Down Expand Up @@ -482,7 +490,7 @@ fileprivate struct EscapeWalker<V: EscapeVisitor> : ValueDefUseWalker,
// 1. the closure (with the captured values) itself can escape
// 2. something can escape in a destructor when the context is destroyed
return walkDownUses(ofValue: pai, path: path.with(knownType: nil))
case is LoadInst, is LoadWeakInst, is LoadUnownedInst:
case is LoadInst, is LoadWeakInst, is LoadUnownedInst, is LoadBorrowInst:
if !followLoads(at: path.projectionPath) {
return .continueWalk
}
Expand Down Expand Up @@ -693,7 +701,7 @@ fileprivate struct EscapeWalker<V: EscapeVisitor> : ValueDefUseWalker,
}
case let ap as ApplyInst:
return walkUpApplyResult(apply: ap, path: path.with(knownType: nil))
case is LoadInst, is LoadWeakInst, is LoadUnownedInst:
case is LoadInst, is LoadWeakInst, is LoadUnownedInst, is LoadBorrowInst:
if !followLoads(at: path.projectionPath) {
// When walking up we shouldn't end up at a load where followLoads is false,
// because going from a (non-followLoads) address to a load always involves a class indirection.
Expand Down
28 changes: 28 additions & 0 deletions test/SILOptimizer/escape_info.sil
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,8 @@ enum E {
case B(Z)
}

sil @escaping_argument : $@convention(thin) (@guaranteed Y) -> ()

sil @not_escaping_argument : $@convention(thin) (@guaranteed Z) -> () {
[%0: noescape **]
}
Expand Down Expand Up @@ -801,6 +803,32 @@ bb0:
return %9 : $()
}

// CHECK-LABEL: Escape information for test_load_borrow:
// CHECK: global: %0 = alloc_ref $Y
// CHECK: - : %1 = alloc_ref $Y
// CHECK: End function test_load_borrow
sil [ossa] @test_load_borrow : $@convention(thin) () -> () {
bb0:
%0 = alloc_ref $Y
%1 = alloc_ref $Y
%2 = alloc_stack $Y
%3 = alloc_stack $Y
store %0 to [init] %2 : $*Y
store %1 to [init] %3 : $*Y
%6 = load_borrow %2 : $*Y
%7 = load_borrow %3 : $*Y
%8 = function_ref @escaping_argument : $@convention(thin) (@guaranteed Y) -> ()
%9 = apply %8(%6) : $@convention(thin) (@guaranteed Y) -> ()
end_borrow %7 : $Y
end_borrow %6 : $Y
destroy_addr %3 : $*Y
destroy_addr %2 : $*Y
dealloc_stack %3 : $*Y
dealloc_stack %2 : $*Y
%r = tuple ()
return %r : $()
}

// CHECK-LABEL: Escape information for call_unknown:
// CHECK: global: %0 = alloc_ref $X
// CHECK: - : %1 = alloc_ref $Y
Expand Down