[llvm] [RegAllocEvictAdvisor] Add minimum weight ratio heuristic. (PR #98109)
Mikhail Gudim via llvm-commits
llvm-commits at lists.llvm.org
Tue Jul 16 20:20:26 PDT 2024
https://github.com/mgudim updated https://github.com/llvm/llvm-project/pull/98109
>From cd0cf21ea7ea4d278dd5ca7aed3ef6e7af24cf9e Mon Sep 17 00:00:00 2001
From: Mikhail Gudim <mgudim at gmail.com>
Date: Mon, 8 Jul 2024 23:40:14 -0400
Subject: [PATCH 1/2] [RegAllocEvictAdvisor][NFC] Add minimum weight ratio
heuristic.
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.
---
llvm/lib/CodeGen/RegAllocEvictionAdvisor.cpp | 18 ++++-
llvm/test/CodeGen/RISCV/min-weight-ratio.mir | 76 ++++++++++++++++++++
2 files changed, 92 insertions(+), 2 deletions(-)
create mode 100644 llvm/test/CodeGen/RISCV/min-weight-ratio.mir
diff --git a/llvm/lib/CodeGen/RegAllocEvictionAdvisor.cpp b/llvm/lib/CodeGen/RegAllocEvictionAdvisor.cpp
index a1dccc4d59723..754999aa98762 100644
--- a/llvm/lib/CodeGen/RegAllocEvictionAdvisor.cpp
+++ b/llvm/lib/CodeGen/RegAllocEvictionAdvisor.cpp
@@ -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,
@@ -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 &&
+ (WeightRatio < MinWeightRatioNeededToEvictHint))
+ return false;
+ LLVM_DEBUG(dbgs() << "should evict: " << B << " w= " << BWeight << '\n');
return true;
}
return false;
diff --git a/llvm/test/CodeGen/RISCV/min-weight-ratio.mir b/llvm/test/CodeGen/RISCV/min-weight-ratio.mir
new file mode 100644
index 0000000000000..08fadca6ff5a5
--- /dev/null
+++ b/llvm/test/CodeGen/RISCV/min-weight-ratio.mir
@@ -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
+
+# 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
+
+...
>From 3c5578a2fbf113bb5d34d16e58a7de06a8c6090e Mon Sep 17 00:00:00 2001
From: Mikhail Gudim <mgudim at gmail.com>
Date: Thu, 11 Jul 2024 00:32:10 -0400
Subject: [PATCH 2/2] Trigger CI.
---
llvm/test/CodeGen/RISCV/min-weight-ratio.mir | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/llvm/test/CodeGen/RISCV/min-weight-ratio.mir b/llvm/test/CodeGen/RISCV/min-weight-ratio.mir
index 08fadca6ff5a5..3b777d493cc4b 100644
--- a/llvm/test/CodeGen/RISCV/min-weight-ratio.mir
+++ b/llvm/test/CodeGen/RISCV/min-weight-ratio.mir
@@ -2,7 +2,7 @@
# RUN: llc %s -mtriple=riscv64 -run-pass=greedy,virtregrewriter \
# RUN: -min-weight-ratio-needed-to-evict-hint=7.5 -o - | FileCheck %s
-# 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.
+# 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"
More information about the llvm-commits
mailing list