Skip to content

[5.10] Fix MemoryLifetimeVerifier to ignore thin functions. #70224

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 18 additions & 13 deletions lib/SIL/Verifier/MemoryLifetimeVerifier.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,13 +48,14 @@ class MemoryLifetimeVerifier {
/// alloc_stack memory locations which are used for store_borrow.
Bits storeBorrowLocations;

/// Returns true if the enum location \p locIdx can be proven to hold a
/// hold a trivial value (e non-payload case) at \p atInst.
bool isEnumTrivialAt(int locIdx, SILInstruction *atInst);
/// Returns true if the location \p locIdx can be proven to hold a
/// hold a trivial value (e.g. non-payload case or thin function) at
/// \p atInst.
bool isValueTrivialAt(int locIdx, SILInstruction *atInst);

/// Returns true if an instruction in the range between \p start and \p end
/// stores a trivial enum case into the enum location \p loc.
bool storesTrivialEnum(int locIdx,
bool storesTrivialValue(int locIdx,
SILBasicBlock::reverse_iterator start,
SILBasicBlock::reverse_iterator end);

Expand All @@ -70,7 +71,7 @@ class MemoryLifetimeVerifier {

/// Issue an error if any bit in \p wrongBits is set.
void require(const Bits &wrongBits, const Twine &complaint,
SILInstruction *where, bool excludeTrivialEnums = false);
SILInstruction *where, bool excludeTrivialValues = false);

/// Require that all the subLocation bits of the location, associated with
/// \p addr, are clear in \p bits.
Expand Down Expand Up @@ -149,7 +150,7 @@ class MemoryLifetimeVerifier {
void verify();
};

bool MemoryLifetimeVerifier::isEnumTrivialAt(int locIdx,
bool MemoryLifetimeVerifier::isValueTrivialAt(int locIdx,
SILInstruction *atInst) {
SILBasicBlock *startBlock = atInst->getParent();

Expand All @@ -158,7 +159,7 @@ bool MemoryLifetimeVerifier::isEnumTrivialAt(int locIdx,
while (SILBasicBlock *block = worklist.pop()) {
auto start = (block == atInst->getParent() ? atInst->getReverseIterator()
: block->rbegin());
if (storesTrivialEnum(locIdx, start, block->rend())) {
if (storesTrivialValue(locIdx, start, block->rend())) {
// Stop at trivial stores to the enum.
continue;
}
Expand Down Expand Up @@ -191,21 +192,25 @@ static bool injectsNoPayloadCase(InjectEnumAddrInst *IEAI) {
return elemType.isEmpty(*function);
}

bool MemoryLifetimeVerifier::storesTrivialEnum(int locIdx,
bool MemoryLifetimeVerifier::storesTrivialValue(int locIdx,
SILBasicBlock::reverse_iterator start,
SILBasicBlock::reverse_iterator end) {
for (SILInstruction &inst : make_range(start, end)) {
if (auto *IEI = dyn_cast<InjectEnumAddrInst>(&inst)) {
const Location *loc = locations.getLocation(IEI->getOperand());
if (loc && loc->isSubLocation(locIdx))
return isTrivialEnumElem(IEI->getElement(), IEI->getOperand()->getType(),
return isTrivialEnumElem(IEI->getElement(),
IEI->getOperand()->getType(),
function);
}
if (auto *SI = dyn_cast<StoreInst>(&inst)) {
const Location *loc = locations.getLocation(SI->getDest());
if (loc && loc->isSubLocation(locIdx) &&
SI->getSrc()->getType().isOrHasEnum()) {
return SI->getOwnershipQualifier() == StoreOwnershipQualifier::Trivial;
if (loc && loc->isSubLocation(locIdx)) {
auto ty = SI->getSrc()->getType();
if (ty.isOrHasEnum() || ty.isFunction()) {
return
SI->getOwnershipQualifier() == StoreOwnershipQualifier::Trivial;
}
}
}
}
Expand Down Expand Up @@ -256,7 +261,7 @@ void MemoryLifetimeVerifier::require(const Bits &wrongBits,
bool excludeTrivialEnums) {
for (int errorLocIdx = wrongBits.find_first(); errorLocIdx >= 0;
errorLocIdx = wrongBits.find_next(errorLocIdx)) {
if (!excludeTrivialEnums || !isEnumTrivialAt(errorLocIdx, where))
if (!excludeTrivialEnums || !isValueTrivialAt(errorLocIdx, where))
reportError(complaint, errorLocIdx, where);
}
}
Expand Down
18 changes: 18 additions & 0 deletions test/SIL/memory_lifetime.sil
Original file line number Diff line number Diff line change
Expand Up @@ -763,3 +763,21 @@ bb3:
return %22 : $()
}

sil @$testTrivialThinToThickClosure : $@convention(thin) @substituted <τ_0_0> () -> @out τ_0_0 for <Int>
sil @$testTrivialThinToThickApply : $@convention(thin) <τ_0_0> (@in_guaranteed τ_0_0) -> ()

// Test alloc_stack of a non-trivial (thick) function.
// Initialize by storing a thin function, which as ownership .none.
// Deallocation does not require a destroy_adr.
sil [ossa] @testTrivialThinToThick : $@convention(thin) () -> () {
bb0:
%0 = function_ref @$testTrivialThinToThickClosure : $@convention(thin) @substituted <τ_0_0> () -> @out τ_0_0 for <Int>
%1 = thin_to_thick_function %0 : $@convention(thin) @substituted <τ_0_0> () -> @out τ_0_0 for <Int> to $@callee_guaranteed @substituted <τ_0_0> () -> @out τ_0_0 for <Int>
%2 = alloc_stack $@callee_guaranteed @substituted <τ_0_0> () -> @out τ_0_0 for <Int>
store %1 to [trivial] %2 : $*@callee_guaranteed @substituted <τ_0_0> () -> @out τ_0_0 for <Int>
%4 = function_ref @$testTrivialThinToThickApply : $@convention(thin) <τ_0_0> (@in_guaranteed τ_0_0) -> ()
%5 = apply %4<() -> Int>(%2) : $@convention(thin) <τ_0_0> (@in_guaranteed τ_0_0) -> ()
dealloc_stack %2 : $*@callee_guaranteed @substituted <τ_0_0> () -> @out τ_0_0 for <Int>
%7 = tuple ()
return %7 : $()
}