Skip to content

Commit 6063dec

Browse files
authored
Merge pull request swiftlang#86784 from eeckstein/temp-rvalue-elimination-6.3
[6.3] TempRValueElimination: fix a problem with `load`-`store` copy elimination
2 parents c2e3c19 + 9624737 commit 6063dec

File tree

2 files changed

+27
-3
lines changed

2 files changed

+27
-3
lines changed

SwiftCompilerSources/Sources/Optimizer/FunctionPasses/TempRValueElimination.swift

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -430,10 +430,15 @@ private func getLastUseWhileSourceIsNotModified(of copy: CopyLikeInstruction,
430430
uses: InstructionSetWithCount,
431431
_ context: FunctionPassContext) -> Instruction?
432432
{
433-
if uses.isEmpty {
433+
var numUsesFound = 0
434+
if copy == copy.loadingInstruction {
435+
// As we are starting the iteration at the next instruction after the copy's load instruction
436+
// we pretend to having seen the copy instruction already.
437+
numUsesFound += 1
438+
}
439+
if uses.count == numUsesFound {
434440
return copy
435441
}
436-
var numUsesFound = 0
437442
let aliasAnalysis = context.aliasAnalysis
438443

439444
// We already checked that the useful lifetime of the `alloc_stack` ends in the same block as the `copy`.
@@ -484,7 +489,7 @@ private struct UseCollector : AddressDefUseWalker {
484489
for use in allocStack.uses {
485490
switch use.instruction {
486491
case copy:
487-
break
492+
uses.insert(copy)
488493
case is DeallocStackInst:
489494
// Deallocations are allowed to be in a different block.
490495
break

test/SILOptimizer/temp_rvalue_opt_ossa.sil

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1790,3 +1790,22 @@ bb2:
17901790
return %9
17911791
}
17921792

1793+
// CHECK-LABEL: sil [ossa] @dealloc_stack_between_load_and_store :
1794+
// CHECK: %4 = load [take] %2
1795+
// CHECK-NEXT: dealloc_stack %2
1796+
// CHECK-NEXT: store %4 to [init] %1
1797+
// CHECK-LABEL: } // end sil function 'dealloc_stack_between_load_and_store'
1798+
sil [ossa] @dealloc_stack_between_load_and_store : $@convention(thin) (@owned AnyObject) -> () {
1799+
bb0(%0 : @owned $AnyObject):
1800+
%1 = alloc_stack $AnyObject
1801+
%2 = alloc_stack [lexical] $AnyObject
1802+
store %0 to [init] %2
1803+
%4 = load [take] %2
1804+
dealloc_stack %2
1805+
store %4 to [init] %1
1806+
destroy_addr %1
1807+
dealloc_stack %1
1808+
%9 = tuple ()
1809+
return %9
1810+
}
1811+

0 commit comments

Comments
 (0)