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

Conversation

shajrawi
Copy link

In some instances, some instructions, like ref_element_addr, can be hoisted outside of loops even if they are not guaranteed to be executed.

We currently don’t support that / bail. We only try to do so / do further analysis only for begin_access because they are extremely heavy.

However, we need to support hosting of ref_element_addr in that case, if it does not have a loop dependent operand, in order to be able to hoist begin_access instructions in some benchmarks.

Initial local testing shows that this PR, when we enable exclusivity, improves the performance of a certain internal benchmark by over 40%

See rdar://problem/43623829

@shajrawi
Copy link
Author

@atrick , @eeckstein can either one review this small change please? :)

@shajrawi
Copy link
Author

@swift-ci Please test

@swiftlang swiftlang deleted a comment from swift-ci Aug 23, 2018
Copy link
Contributor

@eeckstein eeckstein left a comment

Choose a reason for hiding this comment

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

I have some comments.
But basically it lgtm.

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 = dyn_cast<RefElementAddrInst>(Inst);
assert(REA && "Only RefElementAddr and BeginAccess are supported");
Copy link
Contributor

Choose a reason for hiding this comment

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

You can replace dyn_cast<> + assert with a static cast<>

Copy link
Author

Choose a reason for hiding this comment

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

done

@@ -615,6 +619,13 @@ void LoopTreeOptimization::analyzeCurrentLoop(
checkSideEffects(Inst, MayWrites);
break;
}
case SILInstructionKind::RefElementAddrInst: {
auto *REA = dyn_cast<RefElementAddrInst>(&Inst);
assert(REA && "ref_element_addr");
Copy link
Contributor

Choose a reason for hiding this comment

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

The same here

Copy link
Author

Choose a reason for hiding this comment

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

done

auto *REA = dyn_cast<RefElementAddrInst>(&Inst);
assert(REA && "ref_element_addr");
SpecialHoist.insert(REA);
checkSideEffects(Inst, MayWrites);
Copy link
Contributor

Choose a reason for hiding this comment

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

That's not needed. ref_element_addr cannot have any side effects

Copy link
Author

Choose a reason for hiding this comment

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

done

…eed to be executed

In some instances, some instructions, like ref_element_addr, can be hoisted outside of loops even if they are not guaranteed to be executed.

We currently don’t support that / bail. We only try to do so / do further analysis only for begin_access because they are extremely heavy.

However, we need to support hosting of ref_element_addr in that case, if it does not have a loop dependent operand, in order to be able to hoist begin_access instructions in some benchmarks.

Initial local testing shows that this PR, when we enable exclusivity, improves the performance of a certain internal benchmark by over 40%

See rdar://problem/43623829
@shajrawi
Copy link
Author

@swift-ci Please test and merge

@swift-ci swift-ci merged commit 04b5eb5 into swiftlang:master Aug 23, 2018
@shajrawi shajrawi deleted the licm_refelem branch August 23, 2018 23:39
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants