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
Open
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
18 changes: 16 additions & 2 deletions llvm/lib/CodeGen/RegAllocEvictionAdvisor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,13 @@ 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 ratio of weights needed in order for the live range "
"with bigger weight to evict the other live range which"
"satisfies a hint"),
cl::init(1.0));

namespace llvm {
cl::opt<unsigned> EvictInterferenceCutoff(
"regalloc-eviction-max-interference-cutoff", cl::Hidden,
Expand Down Expand Up @@ -156,8 +163,15 @@ bool DefaultEvictionAdvisor::shouldEvict(const LiveInterval &A, bool IsHint,
if (CanSplit && IsHint && !BreaksHint)
return true;

if (A.weight() > B.weight()) {
LLVM_DEBUG(dbgs() << "should evict: " << B << " w= " << B.weight() << '\n');
float AWeight = A.weight();
float BWeight = B.weight();
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?

(WeightRatio < MinWeightRatioNeededToEvictHint))
return false;
LLVM_DEBUG(dbgs() << "should evict: " << B << " w= " << BWeight << '\n');
return true;
}
return false;
Expand Down
76 changes: 76 additions & 0 deletions llvm/test/CodeGen/RISCV/min-weight-ratio.mir
Original file line number Diff line number Diff line change
@@ -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".


# Due to the minimum weight heuristic (see the explanation of `MinWeightRatioNeededToEvictHint`) we are able to get rid of ALL the copies in the code below. When heuristic is disabled (`-min-weight-ratio-needed-to-evict-hint=1.0`) we end up with 3 copies.

--- |
source_filename = "test.c"
target datalayout = "e-m:e-p:64:64-i64:64-i128:128-n32:64-S128"
target triple = "riscv64"

define dso_local i64 @caller(i64 %x) {
entry:
%add_ = add i64 %x, 1
call void @foo()
ret i64 %add_
}

declare void @foo()

...
---
name: caller
alignment: 2
tracksRegLiveness: true
body: |
bb.0.entry:
liveins: $x10, $x1, $x8, $x9, $x18, $x19, $x20, $x21, $x22, $x23, $x24, $x25, $x26, $x27

; CHECK-LABEL: name: caller
; CHECK: liveins: $x1, $x8, $x9, $x10, $x18, $x19, $x20, $x21, $x22, $x23, $x24, $x25, $x26, $x27
; CHECK-NEXT: {{ $}}
; CHECK-NEXT: SD $x1, %stack.0, 0 :: (store (s64) into %stack.0)
; CHECK-NEXT: renamable $x10 = ADDI killed renamable $x10, 1
; CHECK-NEXT: SD killed renamable $x10, %stack.1, 0 :: (store (s64) into %stack.1)
; CHECK-NEXT: ADJCALLSTACKDOWN 0, 0, implicit-def dead $x2, implicit $x2
; CHECK-NEXT: PseudoCALL target-flags(riscv-call) @foo, csr_ilp32d_lp64d, implicit-def dead $x1, implicit-def $x2
; CHECK-NEXT: ADJCALLSTACKUP 0, 0, implicit-def dead $x2, implicit $x2
; CHECK-NEXT: $x10 = LD %stack.1, 0 :: (load (s64) from %stack.1)
; CHECK-NEXT: $x1 = LD %stack.0, 0 :: (load (s64) from %stack.0)
; CHECK-NEXT: PseudoRET implicit $x10, implicit $x1, implicit $x8, implicit $x9, implicit $x18, implicit $x19, implicit $x20, implicit $x21, implicit $x22, implicit $x23, implicit $x24, implicit $x25, implicit $x26, implicit $x27
%14:gpr = COPY $x27
%13:gpr = COPY $x26
%12:gpr = COPY $x25
%11:gpr = COPY $x24
%10:gpr = COPY $x23
%9:gpr = COPY $x22
%8:gpr = COPY $x21
%7:gpr = COPY $x20
%6:gpr = COPY $x19
%5:gpr = COPY $x18
%4:gprc_and_sr07 = COPY $x9
%3:gprc_and_sr07 = COPY $x8
%2:gpr = COPY $x1
%0:gpr = COPY $x10
%1:gpr = ADDI %0, 1
ADJCALLSTACKDOWN 0, 0, implicit-def dead $x2, implicit $x2
PseudoCALL target-flags(riscv-call) @foo, csr_ilp32d_lp64d, implicit-def dead $x1, implicit-def $x2
ADJCALLSTACKUP 0, 0, implicit-def dead $x2, implicit $x2
$x10 = COPY %1
$x1 = COPY %2
$x8 = COPY %3
$x9 = COPY %4
$x18 = COPY %5
$x19 = COPY %6
$x20 = COPY %7
$x21 = COPY %8
$x22 = COPY %9
$x23 = COPY %10
$x24 = COPY %11
$x25 = COPY %12
$x26 = COPY %13
$x27 = COPY %14
PseudoRET implicit $x10, implicit $x1, implicit $x8, implicit $x9, implicit $x18, implicit $x19, implicit $x20, implicit $x21, implicit $x22, implicit $x23, implicit $x24, implicit $x25, implicit $x26, implicit $x27

...
Loading