Skip to content

Commit 8541fb1

Browse files
bcahoonjrbyrnes
andauthored
[AMDGPU] Add off-by-default flag to control LiveRegOpt (llvm#712)
Co-authored-by: Jeffrey Byrnes <[email protected]>
1 parent 2176e0e commit 8541fb1

File tree

11 files changed

+3032
-31
lines changed

11 files changed

+3032
-31
lines changed

llvm/lib/Target/AMDGPU/AMDGPUTargetTransformInfo.cpp

Lines changed: 34 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -313,6 +313,24 @@ bool GCNTTIImpl::hasBranchDivergence(const Function *F) const {
313313
return !F || !ST->isSingleLaneExecution(*F);
314314
}
315315

316+
unsigned GCNTTIImpl::getNumberOfParts(Type *Tp) {
317+
// For certain 8 bit ops, we can pack a v4i8 into a single part
318+
// (e.g. v4i8 shufflevectors -> v_perm v4i8, v4i8). Thus, we
319+
// do not limit the numberOfParts for 8 bit vectors to the
320+
// legalization costs of such. It is left up to other target
321+
// queries (e.g. get*InstrCost) to decide the proper handling
322+
// of 8 bit vectors.
323+
if (FixedVectorType *VTy = dyn_cast<FixedVectorType>(Tp)) {
324+
if (ST->shouldCoerceIllegalTypes() &&
325+
DL.getTypeSizeInBits(VTy->getElementType()) == 8) {
326+
unsigned ElCount = VTy->getElementCount().getFixedValue();
327+
return PowerOf2Ceil(ElCount / 4);
328+
}
329+
}
330+
331+
return BaseT::getNumberOfParts(Tp);
332+
}
333+
316334
unsigned GCNTTIImpl::getNumberOfRegisters(unsigned RCID) const {
317335
// NB: RCID is not an RCID. In fact it is 0 or 1 for scalar or vector
318336
// registers. See getRegisterClassForType for the implementation.
@@ -344,9 +362,11 @@ unsigned GCNTTIImpl::getMinVectorRegisterBitWidth() const {
344362
unsigned GCNTTIImpl::getMaximumVF(unsigned ElemWidth, unsigned Opcode) const {
345363
if (Opcode == Instruction::Load || Opcode == Instruction::Store)
346364
return 32 * 4 / ElemWidth;
347-
return (ElemWidth == 16 && ST->has16BitInsts()) ? 2
348-
: (ElemWidth == 32 && ST->hasPackedFP32Ops()) ? 2
349-
: 1;
365+
366+
return (ST->shouldCoerceIllegalTypes() && ElemWidth == 8) ? 4
367+
: (ElemWidth == 16) ? 2
368+
: (ElemWidth == 32 && ST->hasPackedFP32Ops()) ? 2
369+
: 1;
350370
}
351371

352372
unsigned GCNTTIImpl::getLoadVectorFactor(unsigned VF, unsigned LoadSize,
@@ -1133,14 +1153,16 @@ InstructionCost GCNTTIImpl::getShuffleCost(TTI::ShuffleKind Kind,
11331153

11341154
Kind = improveShuffleKindFromMask(Kind, Mask, VT, Index, SubTp);
11351155

1136-
// Larger vector widths may require additional instructions, but are
1137-
// typically cheaper than scalarized versions.
1138-
unsigned NumVectorElts = cast<FixedVectorType>(VT)->getNumElements();
1156+
unsigned ScalarSize = DL.getTypeSizeInBits(VT->getElementType());
11391157
if (ST->getGeneration() >= AMDGPUSubtarget::VOLCANIC_ISLANDS &&
1140-
DL.getTypeSizeInBits(VT->getElementType()) == 16) {
1141-
bool HasVOP3P = ST->hasVOP3PInsts();
1158+
(ScalarSize == 16 ||
1159+
(ScalarSize == 8 && ST->shouldCoerceIllegalTypes()))) {
1160+
// Larger vector widths may require additional instructions, but are
1161+
// typically cheaper than scalarized versions.
1162+
unsigned NumVectorElts = cast<FixedVectorType>(VT)->getNumElements();
11421163
unsigned RequestedElts =
11431164
count_if(Mask, [](int MaskElt) { return MaskElt != -1; });
1165+
unsigned EltsPerReg = 32 / ScalarSize;
11441166
if (RequestedElts == 0)
11451167
return 0;
11461168
switch (Kind) {
@@ -1149,9 +1171,9 @@ InstructionCost GCNTTIImpl::getShuffleCost(TTI::ShuffleKind Kind,
11491171
case TTI::SK_PermuteSingleSrc: {
11501172
// With op_sel VOP3P instructions freely can access the low half or high
11511173
// half of a register, so any swizzle of two elements is free.
1152-
if (HasVOP3P && NumVectorElts == 2)
1174+
if (ST->hasVOP3PInsts() && ScalarSize == 16 && NumVectorElts == 2)
11531175
return 0;
1154-
unsigned NumPerms = alignTo(RequestedElts, 2) / 2;
1176+
unsigned NumPerms = alignTo(RequestedElts, EltsPerReg) / EltsPerReg;
11551177
// SK_Broadcast just reuses the same mask
11561178
unsigned NumPermMasks = Kind == TTI::SK_Broadcast ? 1 : NumPerms;
11571179
return NumPerms + NumPermMasks;
@@ -1163,12 +1185,12 @@ InstructionCost GCNTTIImpl::getShuffleCost(TTI::ShuffleKind Kind,
11631185
return 0;
11641186
// Insert/extract subvectors only require shifts / extract code to get the
11651187
// relevant bits
1166-
return alignTo(RequestedElts, 2) / 2;
1188+
return alignTo(RequestedElts, EltsPerReg) / EltsPerReg;
11671189
}
11681190
case TTI::SK_PermuteTwoSrc:
11691191
case TTI::SK_Splice:
11701192
case TTI::SK_Select: {
1171-
unsigned NumPerms = alignTo(RequestedElts, 2) / 2;
1193+
unsigned NumPerms = alignTo(RequestedElts, EltsPerReg) / EltsPerReg;
11721194
// SK_Select just reuses the same mask
11731195
unsigned NumPermMasks = Kind == TTI::SK_Select ? 1 : NumPerms;
11741196
return NumPerms + NumPermMasks;

llvm/lib/Target/AMDGPU/AMDGPUTargetTransformInfo.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,7 @@ class GCNTTIImpl final : public BasicTTIImplBase<GCNTTIImpl> {
118118
return TTI::PSK_FastHardware;
119119
}
120120

121+
unsigned getNumberOfParts(Type *Tp);
121122
unsigned getNumberOfRegisters(unsigned RCID) const;
122123
TypeSize getRegisterBitWidth(TargetTransformInfo::RegisterKind Vector) const;
123124
unsigned getMinVectorRegisterBitWidth() const;

llvm/lib/Target/AMDGPU/GCNSubtarget.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,11 @@ static cl::opt<unsigned>
6060
cl::desc("Number of addresses from which to enable MIMG NSA."),
6161
cl::init(3), cl::Hidden);
6262

63+
static cl::opt<bool>
64+
CoerceIllegal("amdgpu-coerce-illegal-types",
65+
cl::desc("Whether or not to coerce illegal types"),
66+
cl::ReallyHidden, cl::init(false));
67+
6368
GCNSubtarget::~GCNSubtarget() = default;
6469

6570
GCNSubtarget &GCNSubtarget::initializeSubtargetDependencies(const Triple &TT,
@@ -198,6 +203,8 @@ GCNSubtarget::GCNSubtarget(const Triple &TT, StringRef GPU, StringRef FS,
198203
RegBankInfo = std::make_unique<AMDGPURegisterBankInfo>(*this);
199204
InstSelector =
200205
std::make_unique<AMDGPUInstructionSelector>(*this, *RegBankInfo, TM);
206+
207+
ShouldCoerceIllegalTypes = CoerceIllegal;
201208
}
202209

203210
unsigned GCNSubtarget::getConstantBusLimit(unsigned Opcode) const {

llvm/lib/Target/AMDGPU/GCNSubtarget.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -259,6 +259,8 @@ class GCNSubtarget final : public AMDGPUGenSubtargetInfo,
259259
bool FeatureDisable = false;
260260

261261
SelectionDAGTargetInfo TSInfo;
262+
bool ShouldCoerceIllegalTypes = false;
263+
262264
private:
263265
SIInstrInfo InstrInfo;
264266
SITargetLowering TLInfo;
@@ -1438,6 +1440,10 @@ class GCNSubtarget final : public AMDGPUGenSubtargetInfo,
14381440
// of sign-extending.
14391441
bool hasGetPCZeroExtension() const { return GFX12Insts; }
14401442

1443+
/// \returns whether or not we should coerce illegal types into vectors of
1444+
// legal types for values that span basic blocks.
1445+
bool shouldCoerceIllegalTypes() const { return ShouldCoerceIllegalTypes; }
1446+
14411447
/// \returns SGPR allocation granularity supported by the subtarget.
14421448
unsigned getSGPRAllocGranule() const {
14431449
return AMDGPU::IsaInfo::getSGPRAllocGranule(this);

0 commit comments

Comments
 (0)