[PATCH] D99876: [GreedyRA] Add some stats to remark emitter for Greedy RA.

Serguei Katkov via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Mon Apr 5 00:05:35 PDT 2021


skatkov created this revision.
skatkov added reviewers: reames, dantrushin.
Herald added subscribers: hiraditya, qcolombet, MatzeB.
skatkov requested review of this revision.
Herald added a project: LLVM.

Adds couple of stats to remark emitter after Greedy RA:

1. Number of spill/reload instructions without used in statepoint like instruction which does not suffer from stack usage.
2. Cost of spill/reloads computed as block frequency for each stack usage.
3. Number of copy instructions with virtual register operands.
4. Cost of copies  computed as block frequency for each copy.

While these numbers highly depends on MIR, they can show interesting information about changes in RA and even can be used as some metric.


https://reviews.llvm.org/D99876

Files:
  llvm/lib/CodeGen/RegAllocGreedy.cpp


Index: llvm/lib/CodeGen/RegAllocGreedy.cpp
===================================================================
--- llvm/lib/CodeGen/RegAllocGreedy.cpp
+++ llvm/lib/CodeGen/RegAllocGreedy.cpp
@@ -551,6 +551,8 @@
                                     unsigned &FoldedReloads, unsigned &Spills,
                                     unsigned &FoldedSpills);
 
+  void reportCostOfSplillsReloads();
+
   /// Report the number of spills and reloads for each loop.
   void reportNumberOfSplillsReloads() {
     for (MachineLoop *L : *Loops) {
@@ -558,6 +560,7 @@
       reportNumberOfSplillsReloads(L, Reloads, FoldedReloads, Spills,
                                    FoldedSpills);
     }
+    reportCostOfSplillsReloads();
   }
 };
 
@@ -3112,6 +3115,52 @@
   return 0;
 }
 
+void RAGreedy::reportCostOfSplillsReloads() {
+  using namespace ore;
+
+  ORE->emit([&]() {
+    float TotalMemWeight = 0.0f;
+    unsigned TotalMemCount = 0;
+    float TotalCopyWeight = 0.0f;
+    unsigned TotalCopyCount = 0;
+    const MachineFrameInfo &MFI = MF->getFrameInfo();
+    for (MachineBasicBlock &MBB : *MF) {
+      unsigned MemCount = 0;
+      unsigned CopyCount = 0;
+      for (MachineInstr &MI : MBB) {
+        std::pair<unsigned, unsigned> NonFoldabelRange =
+            TII->getNonFoldableRange(MI);
+        if (any_of(MI.operands(), [&](MachineOperand &MO) {
+              return MO.isFI() && MFI.isSpillSlotObjectIndex(MO.getIndex()) &&
+                     MI.getOperandNo(&MO) >= NonFoldabelRange.first &&
+                     MI.getOperandNo(&MO) < NonFoldabelRange.second;
+            }))
+          ++MemCount;
+        if (MI.isCopy()) {
+          MachineOperand &Dest = MI.getOperand(0);
+          MachineOperand &Src = MI.getOperand(1);
+          if (Dest.isReg() && Src.isReg() && Dest.getReg().isVirtual() &&
+              Src.getReg().isVirtual())
+            ++CopyCount;
+        }
+      }
+      float BlocFreq = MBFI->getBlockFreqRelativeToEntryBlock(&MBB);
+      TotalMemWeight += MemCount * BlocFreq;
+      TotalMemCount += MemCount;
+      TotalCopyWeight += CopyCount * BlocFreq;
+      TotalCopyCount += CopyCount;
+    }
+
+    MachineOptimizationRemarkAnalysis R(DEBUG_TYPE, "SpillReloadCost",
+                                        DebugLoc(), &MF->front());
+    R << NV("Total Spill Count", TotalMemCount);
+    R << NV("Total Spill Cost", TotalMemWeight);
+    R << NV("Total Copy Count", TotalCopyCount);
+    R << NV("Total Copy Cost", TotalCopyWeight);
+    return R;
+  });
+}
+
 void RAGreedy::reportNumberOfSplillsReloads(MachineLoop *L, unsigned &Reloads,
                                             unsigned &FoldedReloads,
                                             unsigned &Spills,


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D99876.335212.patch
Type: text/x-patch
Size: 2737 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20210405/236c7995/attachment.bin>


More information about the llvm-commits mailing list