Skip to content

LoopRotate: don't rotate loops where the header block branches to a dead-end block. #78781

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
Jan 22, 2025
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
10 changes: 10 additions & 0 deletions lib/SILOptimizer/LoopTransforms/LoopRotate.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
#include "swift/SIL/SILBuilder.h"
#include "swift/SIL/SILInstruction.h"
#include "swift/SILOptimizer/Analysis/Analysis.h"
#include "swift/SILOptimizer/Analysis/DeadEndBlocksAnalysis.h"
#include "swift/SILOptimizer/Analysis/DominanceAnalysis.h"
#include "swift/SILOptimizer/Analysis/LoopAnalysis.h"
#include "swift/SILOptimizer/PassManager/Passes.h"
Expand Down Expand Up @@ -374,6 +375,15 @@ static bool rotateLoop(SILLoop *loop, DominanceInfo *domInfo,
return false;
}

// Incomplete liveranges in the dead-end exit block can cause a missing adjacent
// phi-argument for a re-borrow if there is a borrow-scope is in the loop.
// But even when we have complete lifetimes, it's probably not worth rotating
// a loop where the header block branches to a dead-end block.
auto *deBlocks = pm->getAnalysis<DeadEndBlocksAnalysis>()->get(exit->getParent());
if (deBlocks->isDeadEnd(exit)) {
return false;
}

// We don't want to rotate such that we merge two headers of separate loops
// into one. This can be turned into an assert again once we have guaranteed
// preheader insertions.
Expand Down
37 changes: 37 additions & 0 deletions test/SILOptimizer/looprotate_ossa.sil
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ class Bar {
@objc func foo_objc()
}

sil [ossa] @getBar : $@convention(thin) () -> @owned Bar
sil [ossa] @_TFC4main3Bar3foofS0_FT_T_ : $@convention(method) (@guaranteed Bar) -> ()
sil [ossa] @_TFC4main3Bar3boofS0_FT_T_ : $@convention(method) (@guaranteed Bar) -> Int64
sil [ossa] @_TFC4main3Bar3foo_objcfS0_FT_T_ : $@convention(objc_method) (Bar) -> ()
Expand Down Expand Up @@ -713,3 +714,39 @@ bb14:
return %27
}

// Incomplete liveranges in dead-end exit blocks can cause a missing phi in the rotated loop.
// CHECK-LABEL: sil [ossa] @incomplete_liverange_in_dead_end_exit :
// CHECK: bb1:
// CHECK: apply
// CHECK: bb2:
// CHECK-LABEL: } // end sil function 'incomplete_liverange_in_dead_end_exit'
sil [ossa] @incomplete_liverange_in_dead_end_exit : $@convention(thin) () -> () {
bb0:
br bb1

bb1:
%1 = function_ref @getBar : $@convention(thin) () -> @owned Bar
%2 = apply %1() : $@convention(thin) () -> @owned Bar
%3 = begin_borrow %2
cond_br undef, bb3, bb2

bb2:
br bb4

bb3:
end_borrow %3
unreachable

bb4:
end_borrow %3
destroy_value %2
cond_br undef, bb6, bb5

bb5:
br bb1

bb6:
%12 = tuple ()
return %12
}