[llvm] [AMDGPU] Refactor unit test. NFC (PR #82976)

Diana Picus via llvm-commits llvm-commits at lists.llvm.org
Mon Feb 26 02:10:32 PST 2024


https://github.com/rovka updated https://github.com/llvm/llvm-project/pull/82976

>From d3b79c8a2c952ef25aada4905177b2706a36de3e Mon Sep 17 00:00:00 2001
From: Diana Picus <Diana-Magda.Picus at amd.com>
Date: Wed, 31 Jan 2024 00:08:54 +0100
Subject: [PATCH 1/2] [AMDGPU] Refactor unit test. NFC

I'm about to add more tests here (downstream for now).

Change-Id: Ibd5edb398f544c90e6e8b5e49b1777a407f0594a
---
 .../Target/AMDGPU/AMDGPUUnitTests.cpp         | 35 ++++++++++++-------
 1 file changed, 22 insertions(+), 13 deletions(-)

diff --git a/llvm/unittests/Target/AMDGPU/AMDGPUUnitTests.cpp b/llvm/unittests/Target/AMDGPU/AMDGPUUnitTests.cpp
index f02413376759b4..94d182b5c2ce03 100644
--- a/llvm/unittests/Target/AMDGPU/AMDGPUUnitTests.cpp
+++ b/llvm/unittests/Target/AMDGPU/AMDGPUUnitTests.cpp
@@ -92,9 +92,23 @@ static const std::pair<StringRef, StringRef>
   W32FS = {"+wavefrontsize32", "w32"},
   W64FS = {"+wavefrontsize64", "w64"};
 
-static void testGPRLimits(
-    const char *RegName, bool TestW32W64,
-    std::function<bool(std::stringstream &, unsigned, GCNSubtarget &)> test) {
+using TestFuncTy =
+    std::function<bool(std::stringstream &, unsigned, const GCNSubtarget &)>;
+
+static bool testAndRecord(std::stringstream &Table, const GCNSubtarget &ST,
+                          TestFuncTy test) {
+  bool Success = true;
+  unsigned MaxOcc = ST.getMaxWavesPerEU();
+  for (unsigned Occ = MaxOcc; Occ > 0; --Occ) {
+    Table << std::right << std::setw(3) << Occ << "    ";
+    Success = test(Table, Occ, ST) && Success;
+    Table << '\n';
+  }
+  return Success;
+}
+
+static void testGPRLimits(const char *RegName, bool TestW32W64,
+                          TestFuncTy test) {
   SmallVector<StringRef> CPUs;
   AMDGPU::fillValidArchListAMDGCN(CPUs);
 
@@ -117,13 +131,7 @@ static void testGPRLimits(
         FS = &W32FS;
 
       std::stringstream Table;
-      bool Success = true;
-      unsigned MaxOcc = ST.getMaxWavesPerEU();
-      for (unsigned Occ = MaxOcc; Occ > 0; --Occ) {
-        Table << std::right << std::setw(3) << Occ << "    ";
-        Success = test(Table, Occ, ST) && Success;
-        Table << '\n';
-      }
+      bool Success = testAndRecord(Table, ST, test);
       if (!Success || PrintCpuRegLimits)
         TablePerCPUs[Table.str()].push_back((CanonCPUName + FS->second).str());
 
@@ -145,13 +153,14 @@ static void testGPRLimits(
 }
 
 TEST(AMDGPU, TestVGPRLimitsPerOccupancy) {
-  testGPRLimits("VGPR", true, [](std::stringstream &OS, unsigned Occ,
-                                 GCNSubtarget &ST) {
+  auto test = [](std::stringstream &OS, unsigned Occ, const GCNSubtarget &ST) {
     unsigned MaxVGPRNum = ST.getAddressableNumVGPRs();
     return checkMinMax(
         OS, Occ, ST.getOccupancyWithNumVGPRs(MaxVGPRNum), ST.getMaxWavesPerEU(),
         [&](unsigned NumGPRs) { return ST.getOccupancyWithNumVGPRs(NumGPRs); },
         [&](unsigned Occ) { return ST.getMinNumVGPRs(Occ); },
         [&](unsigned Occ) { return ST.getMaxNumVGPRs(Occ); });
-  });
+  };
+
+  testGPRLimits("VGPR", true, test);
 }

>From d12d67210079c4d849ca5ff9b8bb5637213c0594 Mon Sep 17 00:00:00 2001
From: Diana Picus <Diana-Magda.Picus at amd.com>
Date: Mon, 26 Feb 2024 11:10:19 +0100
Subject: [PATCH 2/2] s/std::function/function_ref

---
 llvm/unittests/Target/AMDGPU/AMDGPUUnitTests.cpp | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/llvm/unittests/Target/AMDGPU/AMDGPUUnitTests.cpp b/llvm/unittests/Target/AMDGPU/AMDGPUUnitTests.cpp
index 94d182b5c2ce03..d0a3cfa84ee018 100644
--- a/llvm/unittests/Target/AMDGPU/AMDGPUUnitTests.cpp
+++ b/llvm/unittests/Target/AMDGPU/AMDGPUUnitTests.cpp
@@ -93,7 +93,7 @@ static const std::pair<StringRef, StringRef>
   W64FS = {"+wavefrontsize64", "w64"};
 
 using TestFuncTy =
-    std::function<bool(std::stringstream &, unsigned, const GCNSubtarget &)>;
+    function_ref<bool(std::stringstream &, unsigned, const GCNSubtarget &)>;
 
 static bool testAndRecord(std::stringstream &Table, const GCNSubtarget &ST,
                           TestFuncTy test) {



More information about the llvm-commits mailing list