| 1 |
//===--------- llvm/unittests/Target/AMDGPU/AMDGPUUnitTests.cpp -----------===// |
| 2 |
// |
| 3 |
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 |
// See https://llvm.org/LICENSE.txt for license information. |
| 5 |
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 6 |
// |
| 7 |
//===----------------------------------------------------------------------===// |
| 8 |
|
| 9 |
#include "AMDGPUUnitTests.h" |
| 10 |
#include "AMDGPUTargetMachine.h" |
| 11 |
#include "GCNSubtarget.h" |
| 12 |
#include "llvm/MC/TargetRegistry.h" |
| 13 |
#include "llvm/Support/TargetParser.h" |
| 14 |
#include "llvm/Support/TargetSelect.h" |
| 15 |
#include "gtest/gtest.h" |
| 16 |
|
| 17 |
#include "AMDGPUGenSubtargetInfo.inc" |
| 18 |
|
| 19 |
using namespace llvm; |
| 20 |
|
| 21 |
std::once_flag flag; |
| 22 |
|
| 23 |
void InitializeAMDGPUTarget() { |
| 24 |
std::call_once(flag, []() { |
| 25 |
LLVMInitializeAMDGPUTargetInfo(); |
| 26 |
LLVMInitializeAMDGPUTarget(); |
| 27 |
LLVMInitializeAMDGPUTargetMC(); |
| 28 |
}); |
| 29 |
} |
| 30 |
|
| 31 |
std::unique_ptr<const GCNTargetMachine> |
| 32 |
llvm::createAMDGPUTargetMachine(std::string TStr, StringRef CPU, StringRef FS) { |
| 33 |
InitializeAMDGPUTarget(); |
| 34 |
|
| 35 |
std::string Error; |
| 36 |
const Target *T = TargetRegistry::lookupTarget(TStr, Error); |
| 37 |
if (!T) |
| 38 |
return nullptr; |
| 39 |
|
| 40 |
TargetOptions Options; |
| 41 |
return std::unique_ptr<GCNTargetMachine>( |
| 42 |
static_cast<GCNTargetMachine *>(T->createTargetMachine( |
| 43 |
TStr, CPU, FS, Options, std::nullopt, std::nullopt))); |
| 44 |
} |
| 45 |
|
| 46 |
static cl::opt<bool> PrintCpuRegLimits( |
| 47 |
"print-cpu-reg-limits", cl::NotHidden, cl::init(false), |
| 48 |
cl::desc("force printing per AMDGPU CPU register limits")); |
| 49 |
|
| 50 |
static bool checkMinMax(std::stringstream &OS, unsigned Occ, unsigned MinOcc, |
| 51 |
unsigned MaxOcc, |
| 52 |
std::function<unsigned(unsigned)> GetOcc, |
| 53 |
std::function<unsigned(unsigned)> GetMinGPRs, |
| 54 |
std::function<unsigned(unsigned)> GetMaxGPRs) { |
| 55 |
bool MinValid = true, MaxValid = true, RangeValid = true; |
| 56 |
unsigned MinGPRs = GetMinGPRs(Occ); |
| 57 |
unsigned MaxGPRs = GetMaxGPRs(Occ); |
| 58 |
unsigned RealOcc; |
| 59 |
|
| 60 |
if (MinGPRs >= MaxGPRs) |
| 61 |
RangeValid = false; |
| 62 |
else { |
| 63 |
RealOcc = GetOcc(MinGPRs); |
| 64 |
for (unsigned NumRegs = MinGPRs + 1; NumRegs <= MaxGPRs; ++NumRegs) { |
| 65 |
if (RealOcc != GetOcc(NumRegs)) { |
| 66 |
RangeValid = false; |
| 67 |
break; |
| 68 |
} |
| 69 |
} |
| 70 |
} |
| 71 |
|
| 72 |
if (RangeValid && RealOcc > MinOcc && RealOcc <= MaxOcc) { |
| 73 |
if (MinGPRs > 0 && GetOcc(MinGPRs - 1) <= RealOcc) |
| 74 |
MinValid = false; |
| 75 |
|
| 76 |
if (GetOcc(MaxGPRs + 1) >= RealOcc) |
| 77 |
MaxValid = false; |
| 78 |
} |
| 79 |
|
| 80 |
std::stringstream MinStr; |
| 81 |
MinStr << (MinValid ? ' ' : '<') << ' ' << std::setw(3) << MinGPRs << " (O" |
| 82 |
<< GetOcc(MinGPRs) << ") " << (RangeValid ? ' ' : 'R'); |
| 83 |
|
| 84 |
OS << std::left << std::setw(15) << MinStr.str() << std::setw(3) << MaxGPRs |
| 85 |
<< " (O" << GetOcc(MaxGPRs) << ')' << (MaxValid ? "" : " >"); |
| 86 |
|
| 87 |
return MinValid && MaxValid && RangeValid; |
| 88 |
} |
| 89 |
|
| 90 |
static const std::pair<StringRef, StringRef> |
| 91 |
EmptyFS = {"", ""}, |
| 92 |
W32FS = {"+wavefrontsize32", "w32"}, |
| 93 |
W64FS = {"+wavefrontsize64", "w64"}; |
| 94 |
|
| 95 |
static void testGPRLimits( |
| 96 |
const char *RegName, bool TestW32W64, |
| 97 |
std::function<bool(std::stringstream &, unsigned, GCNSubtarget &)> test) { |
| 98 |
SmallVector<StringRef> CPUs; |
| 99 |
AMDGPU::fillValidArchListAMDGCN(CPUs); |
| 100 |
|
| 101 |
std::map<std::string, SmallVector<std::string>> TablePerCPUs; |
| 102 |
for (auto CPUName : CPUs) { |
| 103 |
auto CanonCPUName = |
| 104 |
AMDGPU::getArchNameAMDGCN(AMDGPU::parseArchAMDGCN(CPUName)); |
| 105 |
|
| 106 |
auto *FS = &EmptyFS; |
| 107 |
while (true) { |
| 108 |
auto TM = createAMDGPUTargetMachine("amdgcn-amd-", CPUName, FS->first); |
| 109 |
if (!TM) |
| 110 |
break; |
| 111 |
|
| 112 |
GCNSubtarget ST(TM->getTargetTriple(), std::string(TM->getTargetCPU()), |
| 113 |
std::string(TM->getTargetFeatureString()), *TM); |
| 114 |
|
| 115 |
if (TestW32W64 && |
| 116 |
ST.getFeatureBits().test(AMDGPU::FeatureWavefrontSize32)) |
| 117 |
FS = &W32FS; |
| 118 |
|
| 119 |
std::stringstream Table; |
| 120 |
bool Success = true; |
| 121 |
unsigned MaxOcc = ST.getMaxWavesPerEU(); |
| 122 |
for (unsigned Occ = MaxOcc; Occ > 0; --Occ) { |
| 123 |
Table << std::right << std::setw(3) << Occ << " "; |
| 124 |
Success = test(Table, Occ, ST) && Success; |
| 125 |
Table << '\n'; |
| 126 |
} |
| 127 |
if (!Success || PrintCpuRegLimits) |
| 128 |
TablePerCPUs[Table.str()].push_back((CanonCPUName + FS->second).str()); |
| 129 |
|
| 130 |
if (FS != &W32FS) |
| 131 |
break; |
| 132 |
|
| 133 |
FS = &W64FS; |
| 134 |
} |
| 135 |
} |
| 136 |
std::stringstream OS; |
| 137 |
for (auto &P : TablePerCPUs) { |
| 138 |
for (auto &CPUName : P.second) |
| 139 |
OS << ' ' << CPUName; |
| 140 |
OS << ":\nOcc Min" << RegName << " Max" << RegName << '\n' |
| 141 |
<< P.first << '\n'; |
| 142 |
} |
| 143 |
auto ErrStr = OS.str(); |
| 144 |
EXPECT_TRUE(ErrStr.empty()) << ErrStr; |
| 145 |
} |
| 146 |
|
| 147 |
TEST(AMDGPU, TestVGPRLimitsPerOccupancy) { |
| 148 |
testGPRLimits("VGPR", true, [](std::stringstream &OS, unsigned Occ, |
| 149 |
GCNSubtarget &ST) { |
| 150 |
unsigned MaxVGPRNum = ST.getAddressableNumVGPRs(); |
| 151 |
return checkMinMax( |
| 152 |
OS, Occ, ST.getOccupancyWithNumVGPRs(MaxVGPRNum), ST.getMaxWavesPerEU(), |
| 153 |
[&](unsigned NumGPRs) { return ST.getOccupancyWithNumVGPRs(NumGPRs); }, |
| 154 |
[&](unsigned Occ) { return ST.getMinNumVGPRs(Occ); }, |
| 155 |
[&](unsigned Occ) { return ST.getMaxNumVGPRs(Occ); }); |
| 156 |
}); |
| 157 |
} |