From 328377307ad2da961b3be0f2bbf1814a6f1f4ed3 Mon Sep 17 00:00:00 2001 From: Serguei Katkov Date: Fri, 9 Apr 2021 17:22:36 +0700 Subject: [PATCH] [GreedyRA ORE] Add Cost of spill locations into remark 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. Reviewers: reames, MatzeB, anemet, thegameg Reviewed By: reames Subscribers: llvm-commits Differential Revision: https://reviews.llvm.org/D100020 --- llvm/lib/CodeGen/RegAllocGreedy.cpp | 40 +++++++++++++++++++--- .../arm64-spill-remarks-treshold-hotness.ll | 2 +- llvm/test/CodeGen/AArch64/arm64-spill-remarks.ll | 36 ++++++++++++++----- llvm/test/CodeGen/X86/statepoint-ra.ll | 6 ++++ 4 files changed, 70 insertions(+), 14 deletions(-) diff --git a/llvm/lib/CodeGen/RegAllocGreedy.cpp b/llvm/lib/CodeGen/RegAllocGreedy.cpp index c1da51e..340453b 100644 --- a/llvm/lib/CodeGen/RegAllocGreedy.cpp +++ b/llvm/lib/CodeGen/RegAllocGreedy.cpp @@ -555,6 +555,11 @@ private: 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 @@ private: 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 6a99988..e51cdbf 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 f17932f..72d486d 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 ea460da..38a1401 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 { -- 2.7.4