Skip to content

EscapeUtils: add test cases and an assert for addresses escaping to to a closure #63864

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
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/SILType.h
Original file line number Diff line number Diff line change
Expand Up @@ -444,6 +444,13 @@ class SILType {
return isObject() && isClassOrClassMetatype(getASTType());
}

bool isFunctionTypeWithContext() const {
if (auto *fTy = getASTType()->getAs<SILFunctionType>()) {
return fTy->getExtInfo().hasContext();
}
return false;
}

/// True if the type involves any archetypes.
bool hasArchetype() const { return getASTType()->hasArchetype(); }

Expand Down
5 changes: 4 additions & 1 deletion lib/SIL/IR/SILType.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,10 @@ bool SILType::isOrContainsRawPointer(const SILFunction &F) const {
bool SILType::isNonTrivialOrContainsRawPointer(const SILFunction &F) const {
auto contextType = hasTypeParameter() ? F.mapTypeIntoContext(*this) : *this;
const TypeLowering &tyLowering = F.getTypeLowering(contextType);
return !tyLowering.isTrivial() || tyLowering.isOrContainsRawPointer();
bool result = !tyLowering.isTrivial() || tyLowering.isOrContainsRawPointer();
assert((result || !isFunctionTypeWithContext()) &&
"a function type with context must either be non trivial or marked as containing a pointer");
return result;
}

bool SILType::isEmpty(const SILFunction &F) const {
Expand Down
27 changes: 27 additions & 0 deletions test/SILOptimizer/addr_escape_info.sil
Original file line number Diff line number Diff line change
Expand Up @@ -606,3 +606,30 @@ bb0:
%11 = tuple ()
return %11 : $()
}

sil @call_closure : $@convention(method) (@noescape @callee_guaranteed () -> ()) -> ()
sil @closure : $@convention(thin) (@inout_aliasable Int) -> ()

// CHECK-LABEL: Address escape information for test_closure_capturing_address:
// CHECK: value: %0 = alloc_stack $Int
// CHECK-NEXT: ==> %7 = apply %6(%4) : $@convention(method) (@noescape @callee_guaranteed () -> ()) -> ()
// CHECK: End function test_closure_capturing_address
sil @test_closure_capturing_address : $@convention(thin) () -> Int {
bb0:
%0 = alloc_stack $Int
%1 = integer_literal $Builtin.Int64, 0
%2 = struct $Int (%1 : $Builtin.Int64)

%3 = function_ref @closure : $@convention(thin) (@inout_aliasable Int) -> ()
%4 = partial_apply [callee_guaranteed] [on_stack] %3(%0) : $@convention(thin) (@inout_aliasable Int) -> ()
store %2 to %0 : $*Int

%6 = function_ref @call_closure : $@convention(method) (@noescape @callee_guaranteed () -> ()) -> ()
%7 = apply %6(%4) : $@convention(method) (@noescape @callee_guaranteed () -> ()) -> ()
dealloc_stack %4 : $@noescape @callee_guaranteed () -> ()
fix_lifetime %0 : $*Int
%9 = load %0 : $*Int
dealloc_stack %0 : $*Int
return %9 : $Int
}

25 changes: 25 additions & 0 deletions test/SILOptimizer/redundant_load_elim.sil
Original file line number Diff line number Diff line change
Expand Up @@ -1268,3 +1268,28 @@ bb1:
return %r : $()
}

sil @call_closure : $@convention(method) (@noescape @callee_guaranteed () -> ()) -> ()
sil @closure : $@convention(thin) (@inout_aliasable Int) -> ()

// CHECK-LABEL: sil @test_closure_capturing_address :
// CHECK: [[L:%[0-9]+]] = load
// CHECK: return [[L]]
// CHECK-LABEL: } // end sil function 'test_closure_capturing_address'
sil @test_closure_capturing_address : $@convention(thin) () -> Int {
bb0:
%0 = alloc_stack $Int
%1 = integer_literal $Builtin.Int64, 0
%2 = struct $Int (%1 : $Builtin.Int64)

%3 = function_ref @closure : $@convention(thin) (@inout_aliasable Int) -> ()
%4 = partial_apply [callee_guaranteed] [on_stack] %3(%0) : $@convention(thin) (@inout_aliasable Int) -> ()
store %2 to %0 : $*Int

%6 = function_ref @call_closure : $@convention(method) (@noescape @callee_guaranteed () -> ()) -> ()
%7 = apply %6(%4) : $@convention(method) (@noescape @callee_guaranteed () -> ()) -> ()
dealloc_stack %4 : $@noescape @callee_guaranteed () -> ()
%9 = load %0 : $*Int
dealloc_stack %0 : $*Int
return %9 : $Int
}