Skip to content

[LICM]: support hosting ref_element_addr even if they are not guaranteed to be executed #18931

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
Aug 23, 2018
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
28 changes: 18 additions & 10 deletions lib/SILOptimizer/LoopTransforms/LICM.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -332,20 +332,23 @@ hoistSpecialInstruction(std::unique_ptr<LoopNestSummary> &LoopSummary,
bool Changed = false;

for (auto *Inst : Special) {
auto *BI = dyn_cast<BeginAccessInst>(Inst);
assert(BI && "Only BeginAccessInst are supported");
SmallVector<EndAccessInst *, 2> Ends;
getEndAccesses(BI, Ends);
if (!hoistInstruction(DT, BI, Loop, Preheader)) {
if (!hoistInstruction(DT, Inst, Loop, Preheader)) {
continue;
}
LLVM_DEBUG(llvm::dbgs() << "Hoisted " << *BI);
for (auto *instSink : Ends) {
if (!sinkInstruction(DT, LoopSummary, instSink, LI)) {
llvm_unreachable("LICM: Could not perform must-sink instruction");
if (auto *BI = dyn_cast<BeginAccessInst>(Inst)) {
SmallVector<EndAccessInst *, 2> Ends;
getEndAccesses(BI, Ends);
LLVM_DEBUG(llvm::dbgs() << "Hoisted BeginAccess " << *BI);
for (auto *instSink : Ends) {
if (!sinkInstruction(DT, LoopSummary, instSink, LI)) {
llvm_unreachable("LICM: Could not perform must-sink instruction");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That looks pretty scary. Can you prove that whenever hoistInstruction succeeds also sinkInstruction will succeed?
The logic in both functions is very different. Just a simple example: what about an infinite loop? Wouldn't hoisting succeed but sinking not?

Is there a way to bail if on of hoisting/sinking fails? Just to be on the safe side?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's no way to bail, but, all the appropriate checks where done when analyzing the begin access instruction. as long as the loop is contains at least one exiting block this should succeed. sinkInstruction can only fail in the non-special-instructions case. just re-using the code.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok, as long as you are sure about it :-)

}
}
LLVM_DEBUG(llvm::errs() << " Successfully hosited and sank pair\n");
} else {
auto *REA = static_cast<RefElementAddrInst *>(Inst);
LLVM_DEBUG(llvm::dbgs() << "Hoisted RefElementAddr " << *REA);
}
LLVM_DEBUG(llvm::errs() << " Successfully hosited and sank pair\n");
Changed = true;
}

Expand Down Expand Up @@ -615,6 +618,11 @@ void LoopTreeOptimization::analyzeCurrentLoop(
checkSideEffects(Inst, MayWrites);
break;
}
case SILInstructionKind::RefElementAddrInst: {
auto *REA = static_cast<RefElementAddrInst *>(&Inst);
SpecialHoist.insert(REA);
break;
}
case swift::SILInstructionKind::CondFailInst: {
// We can (and must) hoist cond_fail instructions if the operand is
// invariant. We must hoist them so that we preserve memory safety. A
Expand Down
32 changes: 32 additions & 0 deletions test/SILOptimizer/licm.sil
Original file line number Diff line number Diff line change
Expand Up @@ -319,3 +319,35 @@ bb3:
dealloc_stack %0 : $*Builtin.Int32
return %9999 : $()
}

class RefElemClass {
var x : Int32

init()
}

// Check hoisting of ref_element_addr in conditional control flow (for exclusivity)
//
// CHECK-LABEL: sil @hoist_ref_elem
// CHECK: bb0(%0 : $RefElemClass):
// CHECK-NEXT: ref_element_addr %0 : $RefElemClass, #RefElemClass.x
// CHECK-NEXT: br bb1
sil @hoist_ref_elem : $@convention(thin) (RefElemClass) -> () {
bb0(%0 : $RefElemClass):
br bb1

// loop.
bb1:
cond_br undef, bb2, bb3

bb2:
cond_br undef, bb4, bb1

bb3:
%x = ref_element_addr %0 : $RefElemClass, #RefElemClass.x
br bb1

bb4:
%10 = tuple ()
return %10 : $()
}