Skip to content

Commit 0325e29

Browse files
committed
Add an isDeinitBarrier() utility.
Needs to be common across ShrinkBorrowScopes and SSADestroyHoisting.
1 parent ec9d4b4 commit 0325e29

File tree

2 files changed

+43
-0
lines changed

2 files changed

+43
-0
lines changed

include/swift/SIL/MemAccessUtils.h

+6
Original file line numberDiff line numberDiff line change
@@ -232,6 +232,12 @@ inline bool accessKindMayConflict(SILAccessKind a, SILAccessKind b) {
232232
return !(a == SILAccessKind::Read && b == SILAccessKind::Read);
233233
}
234234

235+
/// Return true if \p instruction is a deinitialization barrier.
236+
///
237+
/// Deinitialization barriers constrain variable lifetimes. Lexical end_borrow
238+
/// and destroy_addr cannot be hoisted above them.
239+
bool isDeinitBarrier(SILInstruction *instruction);
240+
235241
} // end namespace swift
236242

237243
//===----------------------------------------------------------------------===//

lib/SIL/Utils/MemAccessUtils.cpp

+37
Original file line numberDiff line numberDiff line change
@@ -393,6 +393,43 @@ bool swift::isLetAddress(SILValue address) {
393393
return isLetForBase(base);
394394
}
395395

396+
//===----------------------------------------------------------------------===//
397+
// MARK: Deinitialization barriers.
398+
//===----------------------------------------------------------------------===//
399+
400+
static bool isBarrierApply(FullApplySite) {
401+
// TODO: check side effect analysis
402+
return true;
403+
}
404+
405+
static bool mayAccessPointer(SILInstruction *instruction) {
406+
if (!instruction->mayReadOrWriteMemory())
407+
return false;
408+
bool fail = false;
409+
visitAccessedAddress(instruction, [&fail](Operand *operand) {
410+
auto accessStorage = AccessStorage::compute(operand->get());
411+
if (accessStorage.getKind() != AccessRepresentation::Kind::Unidentified)
412+
fail = true;
413+
});
414+
return fail;
415+
}
416+
417+
static bool mayLoadWeakOrUnowned(SILInstruction *instruction) {
418+
// TODO: It is possible to do better here by looking at the address that is
419+
// being loaded.
420+
return isa<LoadWeakInst>(instruction) || isa<LoadUnownedInst>(instruction);
421+
}
422+
423+
bool swift::isDeinitBarrier(SILInstruction *instruction) {
424+
if (instruction->maySynchronize()) {
425+
if (auto apply = FullApplySite::isa(instruction)) {
426+
return isBarrierApply(apply);
427+
}
428+
return true;
429+
}
430+
return mayLoadWeakOrUnowned(instruction) || mayAccessPointer(instruction);
431+
}
432+
396433
//===----------------------------------------------------------------------===//
397434
// MARK: AccessRepresentation
398435
//===----------------------------------------------------------------------===//

0 commit comments

Comments
 (0)