Skip to content

Commit 7365f9f

Browse files
Merge pull request #70774 from nate-chandler/nfc/20240108/1/deinit-barrier-component-predicates
[NFC] SIL: Clarified deinit barrier APIs.
2 parents 5a78608 + 82b7495 commit 7365f9f

File tree

6 files changed

+54
-21
lines changed

6 files changed

+54
-21
lines changed

SwiftCompilerSources/Sources/Optimizer/Analysis/CalleeAnalysis.swift

Lines changed: 29 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -100,30 +100,50 @@ public struct CalleeAnalysis {
100100
}
101101
}
102102

103-
extension FullApplySite {
103+
extension Value {
104104
fileprivate func isBarrier(_ analysis: CalleeAnalysis) -> Bool {
105-
guard let callees = analysis.getCallees(callee: callee) else {
105+
guard let callees = analysis.getCallees(callee: self) else {
106106
return true
107107
}
108108
return callees.contains { $0.isDeinitBarrier }
109109
}
110110
}
111111

112-
extension Instruction {
113-
public final func maySynchronize(_ analysis: CalleeAnalysis) -> Bool {
114-
if let site = self as? FullApplySite {
115-
return site.isBarrier(analysis)
116-
}
117-
return maySynchronizeNotConsideringSideEffects
112+
extension FullApplySite {
113+
fileprivate func isBarrier(_ analysis: CalleeAnalysis) -> Bool {
114+
return callee.isBarrier(analysis)
115+
}
116+
}
117+
118+
extension EndApplyInst {
119+
fileprivate func isBarrier(_ analysis: CalleeAnalysis) -> Bool {
120+
return (operand.value.definingInstruction as! FullApplySite).isBarrier(analysis)
118121
}
122+
}
123+
124+
extension AbortApplyInst {
125+
fileprivate func isBarrier(_ analysis: CalleeAnalysis) -> Bool {
126+
return (operand.value.definingInstruction as! FullApplySite).isBarrier(analysis)
127+
}
128+
}
119129

130+
extension Instruction {
120131
/// Whether lifetime ends of lexical values may safely be hoisted over this
121132
/// instruction.
122133
///
123134
/// Deinitialization barriers constrain variable lifetimes. Lexical
124135
/// end_borrow, destroy_value, and destroy_addr cannot be hoisted above them.
125136
public final func isDeinitBarrier(_ analysis: CalleeAnalysis) -> Bool {
126-
return mayAccessPointer || mayLoadWeakOrUnowned || maySynchronize(analysis)
137+
if let site = self as? FullApplySite {
138+
return site.isBarrier(analysis)
139+
}
140+
if let eai = self as? EndApplyInst {
141+
return eai.isBarrier(analysis)
142+
}
143+
if let aai = self as? AbortApplyInst {
144+
return aai.isBarrier(analysis)
145+
}
146+
return mayAccessPointer || mayLoadWeakOrUnowned || maySynchronize
127147
}
128148
}
129149

SwiftCompilerSources/Sources/SIL/Instruction.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -119,8 +119,8 @@ public class Instruction : CustomStringConvertible, Hashable {
119119
return bridged.mayLoadWeakOrUnowned()
120120
}
121121

122-
public final var maySynchronizeNotConsideringSideEffects: Bool {
123-
return bridged.maySynchronizeNotConsideringSideEffects()
122+
public final var maySynchronize: Bool {
123+
return bridged.maySynchronize()
124124
}
125125

126126
public final var mayBeDeinitBarrierNotConsideringSideEffects: Bool {

include/swift/SIL/MemAccessUtils.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -247,7 +247,7 @@ bool mayLoadWeakOrUnowned(SILInstruction* instruction);
247247

248248
/// Conservatively, whether this instruction could involve a synchronization
249249
/// point like a memory barrier, lock or syscall.
250-
bool maySynchronizeNotConsideringSideEffects(SILInstruction* instruction);
250+
bool maySynchronize(SILInstruction* instruction);
251251

252252
/// Conservatively, whether this instruction could be a barrier to hoisting
253253
/// destroys.

include/swift/SIL/SILBridging.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -687,7 +687,7 @@ struct BridgedInstruction {
687687
BRIDGED_INLINE bool maySuspend() const;
688688
bool mayAccessPointer() const;
689689
bool mayLoadWeakOrUnowned() const;
690-
bool maySynchronizeNotConsideringSideEffects() const;
690+
bool maySynchronize() const;
691691
bool mayBeDeinitBarrierNotConsideringSideEffects() const;
692692

693693
// =========================================================================//

lib/SIL/Utils/MemAccessUtils.cpp

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -437,6 +437,8 @@ bool swift::isLetAddress(SILValue address) {
437437
//===----------------------------------------------------------------------===//
438438

439439
bool swift::mayAccessPointer(SILInstruction *instruction) {
440+
assert(!FullApplySite::isa(instruction) && !isa<EndApplyInst>(instruction) &&
441+
!isa<AbortApplyInst>(instruction));
440442
if (!instruction->mayReadOrWriteMemory())
441443
return false;
442444
if (isa<BuiltinInst>(instruction)) {
@@ -455,6 +457,11 @@ bool swift::mayAccessPointer(SILInstruction *instruction) {
455457
}
456458

457459
bool swift::mayLoadWeakOrUnowned(SILInstruction *instruction) {
460+
assert(!FullApplySite::isa(instruction) && !isa<EndApplyInst>(instruction) &&
461+
!isa<AbortApplyInst>(instruction));
462+
if (isa<BuiltinInst>(instruction)) {
463+
return instruction->mayReadOrWriteMemory();
464+
}
458465
return isa<LoadWeakInst>(instruction)
459466
|| isa<LoadUnownedInst>(instruction)
460467
|| isa<StrongCopyUnownedValueInst>(instruction)
@@ -463,17 +470,23 @@ bool swift::mayLoadWeakOrUnowned(SILInstruction *instruction) {
463470

464471
/// Conservatively, whether this instruction could involve a synchronization
465472
/// point like a memory barrier, lock or syscall.
466-
bool swift::maySynchronizeNotConsideringSideEffects(SILInstruction *instruction) {
467-
return FullApplySite::isa(instruction)
468-
|| isa<EndApplyInst>(instruction)
469-
|| isa<AbortApplyInst>(instruction)
470-
|| isa<HopToExecutorInst>(instruction);
473+
bool swift::maySynchronize(SILInstruction *instruction) {
474+
assert(!FullApplySite::isa(instruction) && !isa<EndApplyInst>(instruction) &&
475+
!isa<AbortApplyInst>(instruction));
476+
if (isa<BuiltinInst>(instruction)) {
477+
return instruction->mayReadOrWriteMemory();
478+
}
479+
return isa<HopToExecutorInst>(instruction);
471480
}
472481

473482
bool swift::mayBeDeinitBarrierNotConsideringSideEffects(SILInstruction *instruction) {
483+
if (FullApplySite::isa(instruction) || isa<EndApplyInst>(instruction) ||
484+
isa<AbortApplyInst>(instruction)) {
485+
return true;
486+
}
474487
bool retval = mayAccessPointer(instruction)
475488
|| mayLoadWeakOrUnowned(instruction)
476-
|| maySynchronizeNotConsideringSideEffects(instruction);
489+
|| maySynchronize(instruction);
477490
assert(!retval || !isa<BranchInst>(instruction) && "br as deinit barrier!?");
478491
return retval;
479492
}

lib/SIL/Utils/SILBridging.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -443,8 +443,8 @@ bool BridgedInstruction::mayLoadWeakOrUnowned() const {
443443
return ::mayLoadWeakOrUnowned(unbridged());
444444
}
445445

446-
bool BridgedInstruction::maySynchronizeNotConsideringSideEffects() const {
447-
return ::maySynchronizeNotConsideringSideEffects(unbridged());
446+
bool BridgedInstruction::maySynchronize() const {
447+
return ::maySynchronize(unbridged());
448448
}
449449

450450
bool BridgedInstruction::mayBeDeinitBarrierNotConsideringSideEffects() const {

0 commit comments

Comments
 (0)