Skip to content

Commit c972a93

Browse files
Merge pull request #65309 from nate-chandler/rdar98542123
[SIL] Accesses to globals are deinit barriers.
2 parents 24a1d01 + 5b6c1d6 commit c972a93

File tree

4 files changed

+38
-12
lines changed

4 files changed

+38
-12
lines changed

include/swift/SIL/MemAccessUtils.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -237,8 +237,8 @@ inline bool accessKindMayConflict(SILAccessKind a, SILAccessKind b) {
237237
return !(a == SILAccessKind::Read && b == SILAccessKind::Read);
238238
}
239239

240-
/// Whether \p instruction accesses storage whose representation is unidentified
241-
/// such as by reading a pointer.
240+
/// Whether \p instruction accesses storage whose representation is either (1)
241+
/// unidentified such as by reading a pointer or (2) global.
242242
bool mayAccessPointer(SILInstruction *instruction);
243243

244244
/// Whether this instruction loads or copies a value whose storage does not

lib/SIL/Utils/MemAccessUtils.cpp

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -406,13 +406,15 @@ bool swift::isLetAddress(SILValue address) {
406406
bool swift::mayAccessPointer(SILInstruction *instruction) {
407407
if (!instruction->mayReadOrWriteMemory())
408408
return false;
409-
bool isUnidentified = false;
410-
visitAccessedAddress(instruction, [&isUnidentified](Operand *operand) {
409+
bool retval = false;
410+
visitAccessedAddress(instruction, [&retval](Operand *operand) {
411411
auto accessStorage = AccessStorage::compute(operand->get());
412-
if (accessStorage.getKind() == AccessRepresentation::Kind::Unidentified)
413-
isUnidentified = true;
412+
auto kind = accessStorage.getKind();
413+
if (kind == AccessRepresentation::Kind::Unidentified ||
414+
kind == AccessRepresentation::Kind::Global)
415+
retval = true;
414416
});
415-
return isUnidentified;
417+
return retval;
416418
}
417419

418420
bool swift::mayLoadWeakOrUnowned(SILInstruction *instruction) {

test/SILOptimizer/deinit_barrier.sil

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,13 @@
22

33
// REQUIRES: swift_in_compiler
44

5+
import Builtin
6+
7+
typealias Int32 = Builtin.Int32
8+
9+
sil_global public @my_errno : $Int32
10+
sil_global shared @global_var : $Int32
11+
512
sil [ossa] @unknown : $@convention(thin) () -> ()
613

714
sil [ossa] @unknown_caller : $@convention(thin) () -> () {
@@ -90,10 +97,10 @@ actor A {}
9097
sil @getA : $() -> (@owned A)
9198
sil @borrowA : $@yield_once @convention(thin) () -> @yields @guaranteed A
9299

93-
// CHECK-LABEL: begin running test 1 of 1 on test_hop_to_executor: is-deinit-barrier
100+
// CHECK-LABEL: begin running test 1 of {{[0-9]+}} on test_hop_to_executor: is-deinit-barrier
94101
// CHECK: hop_to_executor
95102
// CHECK: true
96-
// CHECK-LABEL: end running test 1 of 1 on test_hop_to_executor: is-deinit-barrier
103+
// CHECK-LABEL: end running test 1 of {{[0-9]+}} on test_hop_to_executor: is-deinit-barrier
97104
sil [ossa] @test_hop_to_executor : $@convention(thin) () -> () {
98105
%borrowA = function_ref @borrowA : $@yield_once @convention(thin) () -> @yields @guaranteed A
99106
(%a, %token) = begin_apply %borrowA() : $@yield_once @convention(thin) () -> @yields @guaranteed A
@@ -104,14 +111,31 @@ sil [ossa] @test_hop_to_executor : $@convention(thin) () -> () {
104111
return %retval : $()
105112
}
106113

107-
// CHECK-LABEL: begin running test 1 of 1 on test_instructions_1: is-deinit-barrier
114+
// CHECK-LABEL: begin running test 1 of {{[0-9]+}} on test_instructions_1: is-deinit-barrier
108115
// CHECK: debug_step
109116
// CHECK: false
110-
// CHECK-LABEL: end running test 1 of 1 on test_instructions_1: is-deinit-barrier
117+
// CHECK-LABEL: end running test 1 of {{[0-9]+}} on test_instructions_1: is-deinit-barrier
118+
// CHECK-LABEL: begin running test 2 of {{[0-9]+}} on test_instructions_1: is-deinit-barrier
119+
// CHECK: load [trivial] {{%[^,]+}} : $*Builtin.Int32
120+
// CHECK: true
121+
// CHECK-LABEL: end running test 2 of {{[0-9]+}} on test_instructions_1: is-deinit-barrier
122+
// CHECK-LABEL: begin running test 3 of {{[0-9]+}} on test_instructions_1: is-deinit-barrier
123+
// CHECK: load [trivial] {{%[^,]+}} : $*Builtin.Int32
124+
// CHECK: true
125+
// CHECK-LABEL: end running test 3 of {{[0-9]+}} on test_instructions_1: is-deinit-barrier
111126
sil [ossa] @test_instructions_1 : $@convention(thin) () -> () {
112127
entry:
113128
test_specification "is-deinit-barrier @instruction"
114129
debug_step
130+
131+
%my_errno = global_addr @my_errno : $*Builtin.Int32
132+
test_specification "is-deinit-barrier @instruction"
133+
%my_errno_value = load [trivial] %my_errno : $*Int32
134+
135+
%global_var = global_addr @global_var : $*Builtin.Int32
136+
test_specification "is-deinit-barrier @instruction"
137+
%global_var_value = load [trivial] %global_var : $*Int32
138+
115139
%retval = tuple ()
116140
return %retval : $()
117141
}

test/SILOptimizer/side_effects.sil

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -429,7 +429,7 @@ bb5(%10 : $X):
429429
}
430430

431431
// CHECK-LABEL: sil @load_from_global_var
432-
// CHECK-NEXT: [global: read]
432+
// CHECK-NEXT: [global: read,deinit_barrier]
433433
// CHECK-NEXT: {{^[^[]}}
434434
sil @load_from_global_var : $@convention(thin) () -> Int32 {
435435
bb0:

0 commit comments

Comments
 (0)