[PATCH] D136351: [llvm-exegesis] getNonRedundantWriteProcRes - perform basic toplogical sorting (PR58500)

Simon Pilgrim via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Thu Oct 20 06:52:49 PDT 2022


RKSimon created this revision.
RKSimon added reviewers: courbet, gchatelet, andreadb.
Herald added a subscriber: mstojanovic.
Herald added a project: All.
RKSimon requested review of this revision.
Herald added a project: LLVM.

getNonRedundantWriteProcRes was assuming that tblgen topologically sorted the cpu ModelProcResources[] arrays so that resource units were declared before the resource groups that used them, but unfortunately that doesn't appear to be true - in most cases it was just getting lucky based off the alphanumeric sorting that was being performed and the choice of the resource pipe names in most scheduler models (Intel models in particular).

This patch adds an explicit sort, based off llvm-mca's initializeUsedResources, that sorts by resource mask - I'm not sure whether this sorting is really enough, I don't think overlapping groups or Super resources are a problem, but somebody with more experience with this might be able to advise me....

I'd like to take this further in the future and start sharing more code between llvm-mca and llvm-exegesis - while triaging this bug I saw how similar both approaches are, but are just dissimilar enough that any refactor isn't going to be trivial :(

What is the best way to add test coverage here?


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D136351

Files:
  llvm/tools/llvm-exegesis/lib/CMakeLists.txt
  llvm/tools/llvm-exegesis/lib/SchedClassResolution.cpp


Index: llvm/tools/llvm-exegesis/lib/SchedClassResolution.cpp
===================================================================
--- llvm/tools/llvm-exegesis/lib/SchedClassResolution.cpp
+++ llvm/tools/llvm-exegesis/lib/SchedClassResolution.cpp
@@ -10,6 +10,7 @@
 #include "BenchmarkResult.h"
 #include "llvm/ADT/STLExtras.h"
 #include "llvm/MC/MCAsmInfo.h"
+#include "llvm/MCA/Support.h"
 #include "llvm/Support/FormatVariadic.h"
 #include <limits>
 #include <unordered_set>
@@ -45,7 +46,7 @@
 //
 // Note that in this case, P016 does not contribute any cycles, so it would
 // be removed by this function.
-// FIXME: Move this to MCSubtargetInfo and use it in llvm-mca.
+// FIXME: Merge this with the equivalent in llvm-mca.
 static SmallVector<MCWriteProcResEntry, 8>
 getNonRedundantWriteProcRes(const MCSchedClassDesc &SCDesc,
                             const MCSubtargetInfo &STI) {
@@ -53,12 +54,32 @@
   const auto &SM = STI.getSchedModel();
   const unsigned NumProcRes = SM.getNumProcResourceKinds();
 
-  // This assumes that the ProcResDescs are sorted in topological order, which
-  // is guaranteed by the tablegen backend.
+  // Collect resource masks.
+  SmallVector<uint64_t> ProcResourceMasks(NumProcRes);
+  mca::computeProcResourceMasks(SM, ProcResourceMasks);
+
+  // Sort entries by smaller resources for (basic) topological ordering.
+  using ResourceMaskAndEntry = std::pair<uint64_t, const MCWriteProcResEntry *>;
+  SmallVector<ResourceMaskAndEntry, 8> ResourceMaskAndEntries;
+  for (unsigned I = 0; I != SCDesc.NumWriteProcResEntries; ++I) {
+    const MCWriteProcResEntry *WPR = STI.getWriteProcResBegin(&SCDesc) + I;
+    uint64_t Mask = ProcResourceMasks[WPR->ProcResourceIdx];
+    ResourceMaskAndEntries.push_back({Mask, WPR});
+  }
+  sort(ResourceMaskAndEntries,
+       [](const ResourceMaskAndEntry &A, const ResourceMaskAndEntry &B) {
+         unsigned popcntA = countPopulation(A.first);
+         unsigned popcntB = countPopulation(B.first);
+         if (popcntA < popcntB)
+           return true;
+         if (popcntA > popcntB)
+           return false;
+         return A.first < B.first;
+       });
+
   SmallVector<float, 32> ProcResUnitUsage(NumProcRes);
-  for (const auto *WPR = STI.getWriteProcResBegin(&SCDesc),
-                  *const WPREnd = STI.getWriteProcResEnd(&SCDesc);
-       WPR != WPREnd; ++WPR) {
+  for (const ResourceMaskAndEntry &Entry : ResourceMaskAndEntries) {
+    const MCWriteProcResEntry *WPR = Entry.second;
     const MCProcResourceDesc *const ProcResDesc =
         SM.getProcResource(WPR->ProcResourceIdx);
     if (ProcResDesc->SubUnitsIdxBegin == nullptr) {
Index: llvm/tools/llvm-exegesis/lib/CMakeLists.txt
===================================================================
--- llvm/tools/llvm-exegesis/lib/CMakeLists.txt
+++ llvm/tools/llvm-exegesis/lib/CMakeLists.txt
@@ -26,6 +26,7 @@
   ExecutionEngine
   GlobalISel
   MC
+  MCA
   MCDisassembler
   MCJIT
   MCParser


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D136351.469212.patch
Type: text/x-patch
Size: 2964 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20221020/b04ecdbd/attachment.bin>


More information about the llvm-commits mailing list