Skip to content

Commit 757de6e

Browse files
authored
Merge pull request #84711 from eeckstein/fix-licm
LoopInvariantCodeMotion: fix check for hoisting `load_borrow` instructions
2 parents e4fb881 + 55cdc31 commit 757de6e

File tree

2 files changed

+44
-18
lines changed

2 files changed

+44
-18
lines changed

SwiftCompilerSources/Sources/Optimizer/FunctionPasses/LoopInvariantCodeMotion.swift

Lines changed: 16 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1206,27 +1206,25 @@ private extension ScopedInstruction {
12061206
case is BeginApplyInst:
12071207
return true // Has already been checked with other full applies.
12081208
case let loadBorrowInst as LoadBorrowInst:
1209-
for case let destroyAddrInst as DestroyAddrInst in analyzedInstructions.loopSideEffects {
1210-
if context.aliasAnalysis.mayAlias(loadBorrowInst.address, destroyAddrInst.destroyedAddress) {
1211-
if !scope.contains(destroyAddrInst) {
1212-
return false
1213-
}
1209+
for sideEffectInst in analyzedInstructions.loopSideEffects {
1210+
if let endBorrow = sideEffectInst as? EndBorrowInst,
1211+
let begin = endBorrow.borrow as? LoadBorrowInst,
1212+
begin == self
1213+
{
1214+
continue
12141215
}
1215-
}
1216-
1217-
for storeInst in analyzedInstructions.stores {
1218-
if storeInst.mayWrite(toAddress: loadBorrowInst.address, context.aliasAnalysis) {
1219-
if !scope.contains(storeInst) {
1220-
return false
1221-
}
1216+
if sideEffectInst.mayWrite(toAddress: loadBorrowInst.address, context.aliasAnalysis),
1217+
!scope.contains(sideEffectInst)
1218+
{
1219+
return false
12221220
}
12231221
}
1224-
1225-
fallthrough
1226-
case is BeginAccessInst:
1222+
return true
1223+
1224+
case let beginAccess as BeginAccessInst:
12271225
for fullApplyInst in analyzedInstructions.fullApplies {
1228-
guard mayWriteToMemory && fullApplyInst.mayReadOrWrite(address: operands.first!.value, context.aliasAnalysis) ||
1229-
!mayWriteToMemory && fullApplyInst.mayWrite(toAddress: operands.first!.value, context.aliasAnalysis) else {
1226+
guard mayWriteToMemory && fullApplyInst.mayReadOrWrite(address: beginAccess.address, context.aliasAnalysis) ||
1227+
!mayWriteToMemory && fullApplyInst.mayWrite(toAddress: beginAccess.address, context.aliasAnalysis) else {
12301228
continue
12311229
}
12321230

@@ -1236,7 +1234,7 @@ private extension ScopedInstruction {
12361234
}
12371235
}
12381236

1239-
switch operands.first!.value.accessPath.base {
1237+
switch beginAccess.address.accessPath.base {
12401238
case .class, .global:
12411239
for sideEffect in analyzedInstructions.loopSideEffects where sideEffect.mayRelease {
12421240
// Since a class might have a deinitializer, hoisting begin/end_access pair could violate

test/SILOptimizer/licm.sil

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1771,6 +1771,34 @@ bb3:
17711771
return %r : $()
17721772
}
17731773

1774+
// CHECK-LABEL: sil [ossa] @dont_hoist_load_borrow :
1775+
// CHECK: bb1:
1776+
// CHECK-NEXT: copy_addr
1777+
// CHECK-NEXT: load_borrow
1778+
// CHECK: } // end sil function 'dont_hoist_load_borrow'
1779+
sil [ossa] @dont_hoist_load_borrow : $@convention(thin) (@in_guaranteed String) -> () {
1780+
bb0(%0 : $*String):
1781+
%1 = alloc_stack $String
1782+
br bb1
1783+
1784+
bb1:
1785+
copy_addr %0 to [init] %1
1786+
%3 = load_borrow %1
1787+
fix_lifetime %3
1788+
end_borrow %3
1789+
%6 = load [take] %1
1790+
destroy_value %6
1791+
cond_br undef, bb2, bb3
1792+
1793+
bb2:
1794+
br bb1
1795+
1796+
bb3:
1797+
dealloc_stack %1
1798+
%r = tuple()
1799+
return %r : $()
1800+
}
1801+
17741802
// CHECK-LABEL: sil [ossa] @dont_hoist_struct :
17751803
// CHECK: bb1:
17761804
// CHECK-NEXT: struct $NonCopyable

0 commit comments

Comments
 (0)