Skip to content

Commit 6e9e9c4

Browse files
authored
Merge pull request #72086 from Azoy/kill-dead-atomics
[DeadObjectElimination] Zero init is not a barrier
2 parents d4abcde + 0a8e8ea commit 6e9e9c4

File tree

3 files changed

+33
-0
lines changed

3 files changed

+33
-0
lines changed

lib/SILOptimizer/Transforms/DeadObjectElimination.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -310,6 +310,14 @@ static bool canZapInstruction(SILInstruction *Inst, bool acceptRefCountInsts,
310310
if (isa<BeginAccessInst>(Inst) || isa<EndAccessInst>(Inst))
311311
return true;
312312

313+
// The value form of zero init is not a user of any operand. The address
314+
// form however is easily zappable because it's always a trivial store.
315+
if (auto bi = dyn_cast<BuiltinInst>(Inst)) {
316+
if (bi->getBuiltinKind() == BuiltinValueKind::ZeroInitializer) {
317+
return true;
318+
}
319+
}
320+
313321
// If Inst does not read or write to memory, have side effects, and is not a
314322
// terminator, we can zap it.
315323
if (!Inst->mayHaveSideEffects() && !Inst->mayReadFromMemory() &&

test/SILOptimizer/dead_alloc_elim.sil

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -710,3 +710,14 @@ bb0(%0 : $_ContiguousArrayStorage<Element>):
710710
return %15 : $()
711711
}
712712

713+
// CHECK-LABEL: @dead_zero_init
714+
// CHECK-NOT: alloc_stack
715+
// // CHECK: } // end sil function 'dead_zero_init'
716+
sil @dead_zero_init : $@convention(thin) () -> () {
717+
bb0:
718+
%0 = alloc_stack $Kl
719+
%empty = builtin "zeroInitializer"(%0 : $*Kl) : $()
720+
dealloc_stack %0 : $*Kl
721+
%1 = tuple ()
722+
return %1 : $()
723+
}

test/SILOptimizer/stdlib/Atomics.swift

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,3 +63,17 @@ func localCompareExchange() -> (exchanged: Bool, original: Int) {
6363
ordering: .sequentiallyConsistent
6464
)
6565
}
66+
67+
//===----------------------------------------------------------------------===//
68+
// Dead Object Elimination
69+
//===----------------------------------------------------------------------===//
70+
71+
// CHECK-LABEL: sil {{.*}} @deadAtomic {{.*}} {
72+
// CHECK: %0 = tuple ()
73+
// CHECK-NEXT: return %0 : $()
74+
// CHECK-LABEL: } // end sil function 'deadAtomic'
75+
@_silgen_name("deadAtomic")
76+
func deadAtomic() {
77+
let _ = Atomic(0)
78+
let _ = Atomic<UnsafeRawPointer?>(nil)
79+
}

0 commit comments

Comments
 (0)