Skip to content

Commit d71f36b

Browse files
committed
GlobalPropertyOpt: handle load_borrow and destroy_addr
1 parent 66621d8 commit d71f36b

File tree

2 files changed

+53
-4
lines changed

2 files changed

+53
-4
lines changed

lib/SILOptimizer/IPO/GlobalPropertyOpt.cpp

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -219,9 +219,11 @@ bool GlobalPropertyOpt::canAddressEscape(SILValue V, bool acceptStore) {
219219

220220
// These instructions do not cause the address to escape.
221221
if (isa<LoadInst>(User) ||
222+
isa<LoadBorrowInst>(User) ||
222223
isa<DebugValueInst>(User) ||
223224
isa<StrongReleaseInst>(User) ||
224225
isa<StrongRetainInst>(User) ||
226+
isa<DestroyAddrInst>(User) ||
225227
isa<DeallocBoxInst>(User) ||
226228
isa<DeallocStackInst>(User))
227229
continue;
@@ -285,10 +287,11 @@ void GlobalPropertyOpt::scanInstruction(swift::SILInstruction *Inst) {
285287
default:
286288
break;
287289
}
288-
} else if (auto *LI = dyn_cast<LoadInst>(Inst)) {
290+
} else if (isa<LoadInst>(Inst) || isa<LoadBorrowInst>(Inst)) {
291+
auto *LI = cast<SingleValueInstruction>(Inst);
289292
if (isArrayType(LI->getType())) {
290293
// Add a dependency from the value at the address to the loaded value.
291-
SILValue loadAddr = LI->getOperand();
294+
SILValue loadAddr = LI->getOperand(0);
292295
assert(loadAddr->getType().isAddress());
293296
addDependency(getAddrEntry(loadAddr), getValueEntry(LI));
294297
return;

test/SILOptimizer/global_property_opt.sil

Lines changed: 48 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ struct DepStruct {
3333
private var depField2: [X]
3434
}
3535

36-
// CHECK-LABEL: sil @test_alloc_stack
36+
// CHECK-LABEL: sil @test_alloc_stack :
3737
// CHECK: [[I:%[0-9]+]] = integer_literal $Builtin.Int1, -1
3838
// CHECK: [[R:%[0-9]+]] = struct $Bool ([[I]] : $Builtin.Int1)
3939
// CHECK: return [[R]]
@@ -54,7 +54,30 @@ bb0:
5454
return %10 : $Bool
5555
}
5656

57-
// CHECK-LABEL: sil @test_alloc_stack_escapes
57+
// CHECK-LABEL: sil [ossa] @test_alloc_stack_ossa :
58+
// CHECK: [[I:%[0-9]+]] = integer_literal $Builtin.Int1, -1
59+
// CHECK: [[R:%[0-9]+]] = struct $Bool ([[I]] : $Builtin.Int1)
60+
// CHECK: return [[R]]
61+
sil [ossa] @test_alloc_stack_ossa : $@convention(thin) () -> Bool {
62+
bb0:
63+
%1 = alloc_stack $Array<X>
64+
%3 = function_ref @_array_X_init : $@convention(thin) () -> @owned Array<X>
65+
%4 = apply %3() : $@convention(thin) () -> @owned Array<X>
66+
store %4 to [init] %1 : $*Array<X>
67+
68+
%m1 = function_ref @_array_X_mutate : $@convention(method) (@inout Array<X>) -> ()
69+
%m2 = apply %m1(%1) : $@convention(method) (@inout Array<X>) -> ()
70+
71+
%5 = load_borrow %1 : $*Array<X>
72+
%6 = function_ref @_is_native_X_no_type_check : $@convention(method) (@guaranteed Array<X>) -> Bool
73+
%10 = apply %6(%5) : $@convention(method) (@guaranteed Array<X>) -> Bool
74+
end_borrow %5
75+
destroy_addr %1
76+
dealloc_stack %1 : $*Array<X>
77+
return %10 : $Bool
78+
}
79+
80+
// CHECK-LABEL: sil @test_alloc_stack_escapes :
5881
// CHECK: [[F:%[0-9]+]] = function_ref @_is_native_X_no_type_check
5982
// CHECK: [[R:%[0-9]+]] = apply [[F]]
6083
// CHECK: return [[R]]
@@ -75,6 +98,29 @@ bb0:
7598
return %10 : $Bool
7699
}
77100

101+
// CHECK-LABEL: sil [ossa] @test_alloc_stack_escapes_ossa :
102+
// CHECK: [[F:%[0-9]+]] = function_ref @_is_native_X_no_type_check
103+
// CHECK: [[R:%[0-9]+]] = apply [[F]]
104+
// CHECK: return [[R]]
105+
sil [ossa] @test_alloc_stack_escapes_ossa : $@convention(thin) () -> Bool {
106+
bb0:
107+
%1 = alloc_stack $Array<X>
108+
%3 = function_ref @_array_X_init : $@convention(thin) () -> @owned Array<X>
109+
%4 = apply %3() : $@convention(thin) () -> @owned Array<X>
110+
store %4 to [init] %1 : $*Array<X>
111+
112+
%m1 = function_ref @_take_array_addr : $@convention(method) (@inout Array<X>) -> ()
113+
%m2 = apply %m1(%1) : $@convention(method) (@inout Array<X>) -> ()
114+
115+
%5 = load_borrow %1 : $*Array<X>
116+
%6 = function_ref @_is_native_X_no_type_check : $@convention(method) (@guaranteed Array<X>) -> Bool
117+
%10 = apply %6(%5) : $@convention(method) (@guaranteed Array<X>) -> Bool
118+
end_borrow %5
119+
destroy_addr %1
120+
dealloc_stack %1 : $*Array<X>
121+
return %10 : $Bool
122+
}
123+
78124
// CHECK-LABEL: sil @test_alloc_write_unknown
79125
// CHECK: [[F:%[0-9]+]] = function_ref @_is_native_X_no_type_check
80126
// CHECK: [[R:%[0-9]+]] = apply [[F]]

0 commit comments

Comments
 (0)