Skip to content

[RegAllocEvictAdvisor] Add minimum weight ratio heuristic. #98109

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

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

mgudim
Copy link
Contributor

@mgudim mgudim commented Jul 9, 2024

Do not evict a live range if eviction will break existing hint without satisfying a new one and the ratio of weights is not big enough.

@mgudim mgudim requested review from mtrofin and qcolombet July 9, 2024 03:46
Copy link
Member

@mtrofin mtrofin left a comment

Choose a reason for hiding this comment

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

Shouldn't there be a test?

@@ -44,6 +44,12 @@ static cl::opt<bool> EnableLocalReassignment(
"may be compile time intensive"),
cl::init(false));

static cl::opt<float> MinWeightRatioNeededToEvictHint(
"min-weight-ratio-needed-to-evict-hint", cl::Hidden,
cl::desc("The minimum ration of weights needed in order for the live range with bigger weight to evict the other live range which"
Copy link
Member

Choose a reason for hiding this comment

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

s/ration/ratio

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Fixed, thanks!

"min-weight-ratio-needed-to-evict-hint", cl::Hidden,
cl::desc("The minimum ration of weights needed in order for the live range with bigger weight to evict the other live range which"
"satisfies a hint),
cl::init(7.5));
Copy link
Member

Choose a reason for hiding this comment

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

This should default to the value that makes this change a NFC, so -inf?

@@ -157,6 +163,10 @@ bool DefaultEvictionAdvisor::shouldEvict(const LiveInterval &A, bool IsHint,
return true;

if (A.weight() > B.weight()) {
float WeightRatio = A.weight() / B.weight();
Copy link
Member

Choose a reason for hiding this comment

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

can B.weight() be 0?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

fixed

"min-weight-ratio-needed-to-evict-hint", cl::Hidden,
cl::desc("The minimum ration of weights needed in order for the live range with bigger weight to evict the other live range which"
"satisfies a hint),
cl::init(7.5));
Copy link
Collaborator

Choose a reason for hiding this comment

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

To add on what @mtrofin said, could you explain the rationale for 7.5?

It feels random to me.

@mgudim mgudim force-pushed the min-weight-ratio branch from 7d90710 to 632e91d Compare July 10, 2024 05:24
@mgudim
Copy link
Contributor Author

mgudim commented Jul 10, 2024

Shouldn't there be a test?

Added test

Copy link

github-actions bot commented Jul 10, 2024

✅ With the latest revision this PR passed the C/C++ code formatter.

@mgudim mgudim force-pushed the min-weight-ratio branch from 632e91d to 1eda7c0 Compare July 10, 2024 05:40
@mgudim
Copy link
Contributor Author

mgudim commented Jul 10, 2024

@mtrofin @qcolombet

This patch makes a lot of difference only on top of this: #90819
(I haven't tested the impact of the patch on its own)
It actually gives good improvements in terms of dynamic instruction count on gcc and perlbench benchmarks.
I choose the number 7.5 by experimenting on xz where I originally had a degradation, but with 7.5 it's actually a slight improvement. Turned out the same 7.5 worked pretty well for the rest of the spec benchmarks. Perhaps a better number exists, but this needs a lot of experimentation. Maybe we should make the number architecture-dependent or even set it programatically depending on the code compiled? For now the value of 1.0 disables the heuristic by default.

@mgudim mgudim force-pushed the min-weight-ratio branch 2 times, most recently from 237091d to f9fc97c Compare July 11, 2024 04:34
@mgudim
Copy link
Contributor Author

mgudim commented Jul 16, 2024

I've looked at the CI error - it is some assertion triggered from SLP vectorizer while compiling something in libcxx. I can't imagine how this change could trigger this, so I suspect something in the upstream is broken.

mgudim added 2 commits July 16, 2024 23:20
Do not evict a live range if eviction will break existing hint without
satisfying a new one and the ratio of weights is not big enough. The
heuristic is disabled by default.
@mgudim mgudim force-pushed the min-weight-ratio branch from f9fc97c to 3c5578a Compare July 17, 2024 03:20
@mgudim
Copy link
Contributor Author

mgudim commented Jul 18, 2024

@mtrofin @qcolombet After rebasing, the CI passes. Is this good to merge?

if (AWeight > BWeight) {
float WeightRatio = BWeight == 0.0 ? std::numeric_limits<float>::infinity()
: AWeight / BWeight;
if (CanSplit && !IsHint && BreaksHint &&
Copy link
Collaborator

Choose a reason for hiding this comment

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

The CanSplit doesn't make much sense to me here.
Could you explain why this is desirable to check for this here?

@@ -0,0 +1,76 @@
# NOTE: Assertions have been autogenerated by utils/update_mir_test_checks.py UTC_ARGS: --version 5
# RUN: llc %s -mtriple=riscv64 -run-pass=greedy,virtregrewriter \
# RUN: -min-weight-ratio-needed-to-evict-hint=7.5 -o - | FileCheck %s
Copy link
Collaborator

Choose a reason for hiding this comment

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

To go back to what I was saying wrt 7.5. I'm not questioning that this gives you the best outcome in what you measured, what I was asking is why it is.
Essentially what is the explanation that makes 7.5 a good number aside from "it fixes my problem".

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.

3 participants