Skip to content

Commit f89c11a

Browse files
authored
Merge pull request #63864 from eeckstein/fix-escape-addr-to-partial-apply
EscapeUtils: add test cases and an assert for addresses escaping to to a closure
2 parents 84a89b2 + 4c5b0b1 commit f89c11a

File tree

4 files changed

+63
-1
lines changed

4 files changed

+63
-1
lines changed

Diff for: include/swift/SIL/SILType.h

+7
Original file line numberDiff line numberDiff line change
@@ -444,6 +444,13 @@ class SILType {
444444
return isObject() && isClassOrClassMetatype(getASTType());
445445
}
446446

447+
bool isFunctionTypeWithContext() const {
448+
if (auto *fTy = getASTType()->getAs<SILFunctionType>()) {
449+
return fTy->getExtInfo().hasContext();
450+
}
451+
return false;
452+
}
453+
447454
/// True if the type involves any archetypes.
448455
bool hasArchetype() const { return getASTType()->hasArchetype(); }
449456

Diff for: lib/SIL/IR/SILType.cpp

+4-1
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,10 @@ bool SILType::isOrContainsRawPointer(const SILFunction &F) const {
121121
bool SILType::isNonTrivialOrContainsRawPointer(const SILFunction &F) const {
122122
auto contextType = hasTypeParameter() ? F.mapTypeIntoContext(*this) : *this;
123123
const TypeLowering &tyLowering = F.getTypeLowering(contextType);
124-
return !tyLowering.isTrivial() || tyLowering.isOrContainsRawPointer();
124+
bool result = !tyLowering.isTrivial() || tyLowering.isOrContainsRawPointer();
125+
assert((result || !isFunctionTypeWithContext()) &&
126+
"a function type with context must either be non trivial or marked as containing a pointer");
127+
return result;
125128
}
126129

127130
bool SILType::isEmpty(const SILFunction &F) const {

Diff for: test/SILOptimizer/addr_escape_info.sil

+27
Original file line numberDiff line numberDiff line change
@@ -606,3 +606,30 @@ bb0:
606606
%11 = tuple ()
607607
return %11 : $()
608608
}
609+
610+
sil @call_closure : $@convention(method) (@noescape @callee_guaranteed () -> ()) -> ()
611+
sil @closure : $@convention(thin) (@inout_aliasable Int) -> ()
612+
613+
// CHECK-LABEL: Address escape information for test_closure_capturing_address:
614+
// CHECK: value: %0 = alloc_stack $Int
615+
// CHECK-NEXT: ==> %7 = apply %6(%4) : $@convention(method) (@noescape @callee_guaranteed () -> ()) -> ()
616+
// CHECK: End function test_closure_capturing_address
617+
sil @test_closure_capturing_address : $@convention(thin) () -> Int {
618+
bb0:
619+
%0 = alloc_stack $Int
620+
%1 = integer_literal $Builtin.Int64, 0
621+
%2 = struct $Int (%1 : $Builtin.Int64)
622+
623+
%3 = function_ref @closure : $@convention(thin) (@inout_aliasable Int) -> ()
624+
%4 = partial_apply [callee_guaranteed] [on_stack] %3(%0) : $@convention(thin) (@inout_aliasable Int) -> ()
625+
store %2 to %0 : $*Int
626+
627+
%6 = function_ref @call_closure : $@convention(method) (@noescape @callee_guaranteed () -> ()) -> ()
628+
%7 = apply %6(%4) : $@convention(method) (@noescape @callee_guaranteed () -> ()) -> ()
629+
dealloc_stack %4 : $@noescape @callee_guaranteed () -> ()
630+
fix_lifetime %0 : $*Int
631+
%9 = load %0 : $*Int
632+
dealloc_stack %0 : $*Int
633+
return %9 : $Int
634+
}
635+

Diff for: test/SILOptimizer/redundant_load_elim.sil

+25
Original file line numberDiff line numberDiff line change
@@ -1268,3 +1268,28 @@ bb1:
12681268
return %r : $()
12691269
}
12701270

1271+
sil @call_closure : $@convention(method) (@noescape @callee_guaranteed () -> ()) -> ()
1272+
sil @closure : $@convention(thin) (@inout_aliasable Int) -> ()
1273+
1274+
// CHECK-LABEL: sil @test_closure_capturing_address :
1275+
// CHECK: [[L:%[0-9]+]] = load
1276+
// CHECK: return [[L]]
1277+
// CHECK-LABEL: } // end sil function 'test_closure_capturing_address'
1278+
sil @test_closure_capturing_address : $@convention(thin) () -> Int {
1279+
bb0:
1280+
%0 = alloc_stack $Int
1281+
%1 = integer_literal $Builtin.Int64, 0
1282+
%2 = struct $Int (%1 : $Builtin.Int64)
1283+
1284+
%3 = function_ref @closure : $@convention(thin) (@inout_aliasable Int) -> ()
1285+
%4 = partial_apply [callee_guaranteed] [on_stack] %3(%0) : $@convention(thin) (@inout_aliasable Int) -> ()
1286+
store %2 to %0 : $*Int
1287+
1288+
%6 = function_ref @call_closure : $@convention(method) (@noescape @callee_guaranteed () -> ()) -> ()
1289+
%7 = apply %6(%4) : $@convention(method) (@noescape @callee_guaranteed () -> ()) -> ()
1290+
dealloc_stack %4 : $@noescape @callee_guaranteed () -> ()
1291+
%9 = load %0 : $*Int
1292+
dealloc_stack %0 : $*Int
1293+
return %9 : $Int
1294+
}
1295+

0 commit comments

Comments
 (0)