Skip to content

Commit 40f2f1b

Browse files
authored
Merge pull request #60902 from eeckstein/fix-temp-rvalue-opt
TempRValueOpt: fix a wrong assert.
2 parents 270fde9 + bd16511 commit 40f2f1b

File tree

2 files changed

+29
-4
lines changed

2 files changed

+29
-4
lines changed

lib/SILOptimizer/Transforms/TempRValueElimination.cpp

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -400,10 +400,15 @@ bool TempRValueOptPass::extendAccessScopes(
400400
if (endAccessToMove)
401401
return false;
402402
// Is this the end of an access scope of the copy-source?
403-
if (!aa->isNoAlias(copySrc, endAccess->getSource())) {
404-
assert(endAccess->getBeginAccess()->getAccessKind() ==
405-
SILAccessKind::Read &&
406-
"a may-write end_access should not be in the copysrc lifetime");
403+
if (!aa->isNoAlias(copySrc, endAccess->getSource()) &&
404+
405+
// There cannot be any aliasing modifying accesses within the liferange
406+
// of the temporary, because we would have cought this in
407+
// `getLastUseWhileSourceIsNotModified`.
408+
// But there are cases where `AliasAnalysis::isNoAlias` is less precise
409+
// than `AliasAnalysis::mayWriteToMemory`. Therefore, just ignore any
410+
// non-read accesses.
411+
endAccess->getBeginAccess()->getAccessKind() == SILAccessKind::Read) {
407412

408413
// Don't move instructions beyond the block's terminator.
409414
if (isa<TermInst>(lastUseInst))

test/SILOptimizer/temp_rvalue_opt.sil

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -739,3 +739,23 @@ bb0(%0 : $*Klass):
739739
%9999 = tuple()
740740
return %9999 : $()
741741
}
742+
743+
// CHECK-LABEL: sil @test_aliasing_modify_access
744+
// CHECK: [[A:%.*]] = ref_element_addr [immutable] %0 : $Klass, #Klass.a
745+
// CHECK-NOT: copy_addr
746+
// CHECK: [[L:%.*]] = load [[A]]
747+
// CHECK: return [[L]]
748+
// CHECK: } // end sil function 'test_aliasing_modify_access'
749+
sil @test_aliasing_modify_access : $@convention(thin) (@guaranteed Klass, @guaranteed Klass) -> @owned String {
750+
bb0(%0 : $Klass, %1 : $Klass):
751+
%2 = ref_element_addr [immutable] %0 : $Klass, #Klass.a
752+
%3 = alloc_stack $String
753+
copy_addr %2 to [initialization] %3 : $*String
754+
%4 = ref_element_addr %1 : $Klass, #Klass.a
755+
%5 = begin_access [modify] [dynamic] [no_nested_conflict] %4 : $*String
756+
end_access %5 : $*String
757+
%323 = load %3 : $*String
758+
destroy_addr %3 : $*String
759+
dealloc_stack %3 : $*String
760+
return %323 : $String
761+
}

0 commit comments

Comments
 (0)