Skip to content

TempRValueOpt: fix a wrong assert. #60902

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
Sep 1, 2022
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
13 changes: 9 additions & 4 deletions lib/SILOptimizer/Transforms/TempRValueElimination.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -400,10 +400,15 @@ bool TempRValueOptPass::extendAccessScopes(
if (endAccessToMove)
return false;
// Is this the end of an access scope of the copy-source?
if (!aa->isNoAlias(copySrc, endAccess->getSource())) {
assert(endAccess->getBeginAccess()->getAccessKind() ==
SILAccessKind::Read &&
"a may-write end_access should not be in the copysrc lifetime");
if (!aa->isNoAlias(copySrc, endAccess->getSource()) &&

// There cannot be any aliasing modifying accesses within the liferange
// of the temporary, because we would have cought this in
// `getLastUseWhileSourceIsNotModified`.
// But there are cases where `AliasAnalysis::isNoAlias` is less precise
// than `AliasAnalysis::mayWriteToMemory`. Therefore, just ignore any
// non-read accesses.
endAccess->getBeginAccess()->getAccessKind() == SILAccessKind::Read) {

// Don't move instructions beyond the block's terminator.
if (isa<TermInst>(lastUseInst))
Expand Down
20 changes: 20 additions & 0 deletions test/SILOptimizer/temp_rvalue_opt.sil
Original file line number Diff line number Diff line change
Expand Up @@ -739,3 +739,23 @@ bb0(%0 : $*Klass):
%9999 = tuple()
return %9999 : $()
}

// CHECK-LABEL: sil @test_aliasing_modify_access
// CHECK: [[A:%.*]] = ref_element_addr [immutable] %0 : $Klass, #Klass.a
// CHECK-NOT: copy_addr
// CHECK: [[L:%.*]] = load [[A]]
// CHECK: return [[L]]
// CHECK: } // end sil function 'test_aliasing_modify_access'
sil @test_aliasing_modify_access : $@convention(thin) (@guaranteed Klass, @guaranteed Klass) -> @owned String {
bb0(%0 : $Klass, %1 : $Klass):
%2 = ref_element_addr [immutable] %0 : $Klass, #Klass.a
%3 = alloc_stack $String
copy_addr %2 to [initialization] %3 : $*String
%4 = ref_element_addr %1 : $Klass, #Klass.a
%5 = begin_access [modify] [dynamic] [no_nested_conflict] %4 : $*String
end_access %5 : $*String
%323 = load %3 : $*String
destroy_addr %3 : $*String
dealloc_stack %3 : $*String
return %323 : $String
}