[clang] 70193bd - Re-land [GreedyRA ORE] Add Cost of spill locations into remark

Serguei Katkov via cfe-commits cfe-commits at lists.llvm.org
Tue Apr 20 02:21:44 PDT 2021


Author: Serguei Katkov
Date: 2021-04-20T16:21:07+07:00
New Revision: 70193bdfc06fe85114f1dcea463991dbd0c7b630

URL: https://github.com/llvm/llvm-project/commit/70193bdfc06fe85114f1dcea463991dbd0c7b630
DIFF: https://github.com/llvm/llvm-project/commit/70193bdfc06fe85114f1dcea463991dbd0c7b630.diff

LOG: Re-land [GreedyRA ORE] Add Cost of spill locations into remark

Re-land the patch with a fix of clang test.

Cost of spill location is computed basing on relative branch frequency
where corresponding spill/reload/copy are located.

While the number itself is highly depends on incoming IR,
the total cost can be used when do some changes in RA.

Revert "Revert "[GreedyRA ORE] Add Cost of spill locations into remark""
This reverts commit 680f3d6de79f7dd75ee0cda256a541d18e504a22.

Added: 
    

Modified: 
    clang/test/CodeGen/opt-record-MIR.c
    llvm/lib/CodeGen/RegAllocGreedy.cpp
    llvm/test/CodeGen/AArch64/arm64-spill-remarks-treshold-hotness.ll
    llvm/test/CodeGen/AArch64/arm64-spill-remarks.ll
    llvm/test/CodeGen/X86/statepoint-ra.ll

Removed: 
    


################################################################################
diff  --git a/clang/test/CodeGen/opt-record-MIR.c b/clang/test/CodeGen/opt-record-MIR.c
index 40832d6e776a7..d2d8655a316c2 100644
--- a/clang/test/CodeGen/opt-record-MIR.c
+++ b/clang/test/CodeGen/opt-record-MIR.c
@@ -19,7 +19,7 @@ void foo(float *p, int i) {
   }
 }
 
-// REMARK: opt-record-MIR.c:{{[1-9][0-9]*}}:{{[1-9][0-9]*}}: remark: {{.}} spills {{.}} reloads generated in loop
+// REMARK: opt-record-MIR.c:{{[1-9][0-9]*}}:{{[1-9][0-9]*}}: remark: {{[0-9]*}} spills {{[0-9\.e+-]*}} total spills cost {{[0-9]*}} reloads {{[0-9\.e+-]*}} total reloads cost generated in loop
 // NO_REMARK-NOT: remark:
 
 // YAML: --- !Missed

diff  --git a/llvm/lib/CodeGen/RegAllocGreedy.cpp b/llvm/lib/CodeGen/RegAllocGreedy.cpp
index c1da51e6f5934..340453b9af113 100644
--- a/llvm/lib/CodeGen/RegAllocGreedy.cpp
+++ b/llvm/lib/CodeGen/RegAllocGreedy.cpp
@@ -555,6 +555,11 @@ class RAGreedy : public MachineFunctionPass,
     unsigned Spills = 0;
     unsigned FoldedSpills = 0;
     unsigned Copies = 0;
+    float ReloadsCost = 0.0f;
+    float FoldedReloadsCost = 0.0f;
+    float SpillsCost = 0.0f;
+    float FoldedSpillsCost = 0.0f;
+    float CopiesCost = 0.0f;
 
     bool isEmpty() {
       return !(Reloads || FoldedReloads || Spills || FoldedSpills ||
@@ -568,6 +573,11 @@ class RAGreedy : public MachineFunctionPass,
       Spills += other.Spills;
       FoldedSpills += other.FoldedSpills;
       Copies += other.Copies;
+      ReloadsCost += other.ReloadsCost;
+      FoldedReloadsCost += other.FoldedReloadsCost;
+      SpillsCost += other.SpillsCost;
+      FoldedSpillsCost += other.FoldedSpillsCost;
+      CopiesCost += other.CopiesCost;
     }
 
     void report(MachineOptimizationRemarkMissed &R);
@@ -3137,19 +3147,31 @@ MCRegister RAGreedy::selectOrSplitImpl(LiveInterval &VirtReg,
 
 void RAGreedy::RAGreedyStats::report(MachineOptimizationRemarkMissed &R) {
   using namespace ore;
-  if (Spills)
+  if (Spills) {
     R << NV("NumSpills", Spills) << " spills ";
-  if (FoldedSpills)
+    R << NV("TotalSpillsCost", SpillsCost) << " total spills cost ";
+  }
+  if (FoldedSpills) {
     R << NV("NumFoldedSpills", FoldedSpills) << " folded spills ";
-  if (Reloads)
+    R << NV("TotalFoldedSpillsCost", FoldedSpillsCost)
+      << " total folded spills cost ";
+  }
+  if (Reloads) {
     R << NV("NumReloads", Reloads) << " reloads ";
-  if (FoldedReloads)
+    R << NV("TotalReloadsCost", ReloadsCost) << " total reloads cost ";
+  }
+  if (FoldedReloads) {
     R << NV("NumFoldedReloads", FoldedReloads) << " folded reloads ";
+    R << NV("TotalFoldedReloadsCost", FoldedReloadsCost)
+      << " total folded reloads cost ";
+  }
   if (ZeroCostFoldedReloads)
     R << NV("NumZeroCostFoldedReloads", ZeroCostFoldedReloads)
       << " zero cost folded reloads ";
-  if (Copies)
+  if (Copies) {
     R << NV("NumVRCopies", Copies) << " virtual registers copies ";
+    R << NV("TotalCopiesCost", CopiesCost) << " total copies cost ";
+  }
 }
 
 RAGreedy::RAGreedyStats RAGreedy::computeStats(MachineBasicBlock &MBB) {
@@ -3218,6 +3240,14 @@ RAGreedy::RAGreedyStats RAGreedy::computeStats(MachineBasicBlock &MBB) {
       Stats.FoldedSpills += Accesses.size();
     }
   }
+  // Set cost of collected statistic by multiplication to relative frequency of
+  // this basic block.
+  float RelFreq = MBFI->getBlockFreqRelativeToEntryBlock(&MBB);
+  Stats.ReloadsCost = RelFreq * Stats.Reloads;
+  Stats.FoldedReloadsCost = RelFreq * Stats.FoldedReloads;
+  Stats.SpillsCost = RelFreq * Stats.Spills;
+  Stats.FoldedSpillsCost = RelFreq * Stats.FoldedSpills;
+  Stats.CopiesCost = RelFreq * Stats.Copies;
   return Stats;
 }
 

diff  --git a/llvm/test/CodeGen/AArch64/arm64-spill-remarks-treshold-hotness.ll b/llvm/test/CodeGen/AArch64/arm64-spill-remarks-treshold-hotness.ll
index 6a9998835d66c..e51cdbfac73cd 100644
--- a/llvm/test/CodeGen/AArch64/arm64-spill-remarks-treshold-hotness.ll
+++ b/llvm/test/CodeGen/AArch64/arm64-spill-remarks-treshold-hotness.ll
@@ -5,7 +5,7 @@
 ; RUN:       -pass-remarks-with-hotness -pass-remarks-hotness-threshold=1 \
 ; RUN:       2>&1 | FileCheck -check-prefix=THRESHOLD %s
 
-; CHECK: remark: /tmp/kk.c:3:20: 1 spills 1 reloads generated in loop{{$}}
+; CHECK: remark: /tmp/kk.c:3:20: 1 spills 3.187500e+01 total spills cost 1 reloads 3.187500e+01 total reloads cost generated in loop{{$}}
 ; THRESHOLD-NOT: remark
 
 define void @fpr128(<4 x float>* %p) nounwind ssp {

diff  --git a/llvm/test/CodeGen/AArch64/arm64-spill-remarks.ll b/llvm/test/CodeGen/AArch64/arm64-spill-remarks.ll
index f17932facd57f..72d486d7760d3 100644
--- a/llvm/test/CodeGen/AArch64/arm64-spill-remarks.ll
+++ b/llvm/test/CodeGen/AArch64/arm64-spill-remarks.ll
@@ -17,25 +17,25 @@
 ; then reloaded.
 
 ; (loop3:)
-; REMARK: remark: /tmp/kk.c:3:20: 1 spills 1 reloads generated in loop{{$}}
+; REMARK: remark: /tmp/kk.c:3:20: 1 spills 1.000000e+02 total spills cost 1 reloads 1.000000e+02 total reloads cost generated in loop{{$}}
 ; (loop2:)
-; REMARK: remark: /tmp/kk.c:2:20: 1 spills 1 reloads generated in loop{{$}}
+; REMARK: remark: /tmp/kk.c:2:20: 1 spills 1.000000e+04 total spills cost 1 reloads 1.000000e+04 total reloads cost generated in loop{{$}}
 ; (loop:)
-; REMARK: remark: /tmp/kk.c:1:20: 2 spills 2 reloads generated in loop{{$}}
+; REMARK: remark: /tmp/kk.c:1:20: 2 spills 1.010000e+04 total spills cost 2 reloads 1.010000e+04 total reloads cost generated in loop{{$}}
 ; (func:)
-; REMARK: remark: /tmp/kk.c:1:1: 3 spills 3 reloads generated in function{{$}}
+; REMARK: remark: /tmp/kk.c:1:1: 3 spills 1.020000e+04 total spills cost 3 reloads 1.020000e+04 total reloads cost generated in function
 
 ; (loop3:)
-; HOTNESS: remark: /tmp/kk.c:3:20: 1 spills 1 reloads generated in loop (hotness: 300)
+; HOTNESS: remark: /tmp/kk.c:3:20: 1 spills 1.000000e+02 total spills cost 1 reloads 1.000000e+02 total reloads cost generated in loop (hotness: 300)
 ; (loop2:)
-; HOTNESS: remark: /tmp/kk.c:2:20: 1 spills 1 reloads generated in loop (hotness: 30000)
+; HOTNESS: remark: /tmp/kk.c:2:20: 1 spills 1.000000e+04 total spills cost 1 reloads 1.000000e+04 total reloads cost generated in loop (hotness: 30000)
 ; (loop:)
-; HOTNESS: remark: /tmp/kk.c:1:20: 2 spills 2 reloads generated in loop (hotness: 300)
+; HOTNESS: remark: /tmp/kk.c:1:20: 2 spills 1.010000e+04 total spills cost 2 reloads 1.010000e+04 total reloads cost generated in loop (hotness: 300)
 
 ; NO_REMARK-NOT: remark
 
 ; THRESHOLD-NOT: (hotness: 300)
-; THRESHOLD: remark: /tmp/kk.c:2:20: 1 spills 1 reloads generated in loop (hotness: 30000)
+; THRESHOLD: remark: /tmp/kk.c:2:20: 1 spills 1.000000e+04 total spills cost 1 reloads 1.000000e+04 total reloads cost generated in loop (hotness: 30000)
 
 ; YAML: --- !Missed
 ; YAML: Pass:            regalloc
@@ -46,8 +46,12 @@
 ; YAML: Args:
 ; YAML:   - NumSpills:       '1'
 ; YAML:   - String:          ' spills '
+; YAML:   - TotalSpillsCost: '1.000000e+02'
+; YAML:   - String:          ' total spills cost '
 ; YAML:   - NumReloads:      '1'
 ; YAML:   - String:          ' reloads '
+; YAML:   - TotalReloadsCost: '1.000000e+02'
+; YAML:   - String:          ' total reloads cost '
 ; YAML:   - String:          generated in loop
 ; YAML: ...
 ; YAML: --- !Missed
@@ -59,8 +63,12 @@
 ; YAML: Args:
 ; YAML:   - NumSpills:       '1'
 ; YAML:   - String:          ' spills '
+; YAML:   - TotalSpillsCost: '1.000000e+04'
+; YAML:   - String:          ' total spills cost '
 ; YAML:   - NumReloads:      '1'
 ; YAML:   - String:          ' reloads '
+; YAML:   - TotalReloadsCost: '1.000000e+04'
+; YAML:   - String:          ' total reloads cost '
 ; YAML:   - String:          generated in loop
 ; YAML: ...
 ; YAML: --- !Missed
@@ -72,8 +80,12 @@
 ; YAML: Args:
 ; YAML:   - NumSpills:       '2'
 ; YAML:   - String:          ' spills '
+; YAML:   - TotalSpillsCost: '1.010000e+04'
+; YAML:   - String:          ' total spills cost '
 ; YAML:   - NumReloads:      '2'
 ; YAML:   - String:          ' reloads '
+; YAML:   - TotalReloadsCost: '1.010000e+04'
+; YAML:   - String:          ' total reloads cost '
 ; YAML:   - String:          generated in loop
 ; YAML: ...
 ; YAML: --- !Missed
@@ -85,8 +97,12 @@
 ; YAML: Args:
 ; YAML:   - NumSpills:       '3'
 ; YAML:   - String:          ' spills '
+; YAML:   - TotalSpillsCost: '1.020000e+04'
+; YAML:   - String:          ' total spills cost '
 ; YAML:   - NumReloads:      '3'
 ; YAML:   - String:          ' reloads '
+; YAML:   - TotalReloadsCost: '1.020000e+04'
+; YAML:   - String:          ' total reloads cost '
 ; YAML:   - String:          generated in function
 ; YAML: ...
 
@@ -100,8 +116,12 @@
 ; THRESHOLD_YAML: Args:
 ; THRESHOLD_YAML:   - NumSpills:       '1'
 ; THRESHOLD_YAML:   - String:          ' spills '
+; THRESHOLD_YAML:   - TotalSpillsCost: '1.000000e+04'
+; THRESHOLD_YAML:   - String:          ' total spills cost '
 ; THRESHOLD_YAML:   - NumReloads:      '1'
 ; THRESHOLD_YAML:   - String:          ' reloads '
+; THRESHOLD_YAML:   - TotalReloadsCost: '1.000000e+04'
+; THRESHOLD_YAML:   - String:          ' total reloads cost '
 ; THRESHOLD_YAML:   - String:          generated in loop
 ; THRESHOLD_YAML: ...
 

diff  --git a/llvm/test/CodeGen/X86/statepoint-ra.ll b/llvm/test/CodeGen/X86/statepoint-ra.ll
index ea460dabb6dc5..38a14017fab5d 100644
--- a/llvm/test/CodeGen/X86/statepoint-ra.ll
+++ b/llvm/test/CodeGen/X86/statepoint-ra.ll
@@ -12,12 +12,18 @@ target triple = "x86_64-unknown-linux-gnu"
 ;YAML: Args:
 ;YAML:   - NumSpills:       '10'
 ;YAML:   - String:          ' spills '
+;YAML:   - TotalSpillsCost: '7.000000e+00'
+;YAML:   - String:          ' total spills cost '
 ;YAML:   - NumReloads:      '7'
 ;YAML:   - String:          ' reloads '
+;YAML:   - TotalReloadsCost: '3.109004e-15'
+;YAML:   - String:          ' total reloads cost '
 ;YAML:   - NumZeroCostFoldedReloads: '20'
 ;YAML:   - String:          ' zero cost folded reloads '
 ;YAML:   - NumVRCopies:     '2'
 ;YAML:   - String:          ' virtual registers copies '
+;YAML:   - TotalCopiesCost: '8.882868e-16'
+;YAML:   - String:          ' total copies cost '
 ;YAML:   - String:          generated in function
 
 define void @barney(i8 addrspace(1)* %arg, double %arg1, double %arg2, double %arg3, double %arg4, double %arg5, double %arg6, double %arg7, double %arg8, double %arg9, double %arg10, double %arg11, double %arg12) gc "statepoint-example" personality i32* ()* @widget {


        


More information about the cfe-commits mailing list