Skip to content

Commit 80532eb

Browse files
committed
[AMDGPU][InstCombine] Remove zero image offset
Remove the offset parameter if it is zero. Differential Revision: https://reviews.llvm.org/D117876
1 parent 7cd441f commit 80532eb

File tree

5 files changed

+649
-0
lines changed

5 files changed

+649
-0
lines changed

llvm/lib/Target/AMDGPU/AMDGPUInstCombineIntrinsic.cpp

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,23 @@ simplifyAMDGCNImageIntrinsic(const GCNSubtarget *ST,
182182
}
183183
}
184184

185+
// Optimize _offset away when 'offset' is zero
186+
if (const auto *OffsetMappingInfo =
187+
AMDGPU::getMIMGOffsetMappingInfo(ImageDimIntr->BaseOpcode)) {
188+
if (auto *ConstantOffset =
189+
dyn_cast<ConstantInt>(II.getOperand(ImageDimIntr->OffsetIndex))) {
190+
if (ConstantOffset->isZero()) {
191+
const AMDGPU::ImageDimIntrinsicInfo *NewImageDimIntr =
192+
AMDGPU::getImageDimIntrinsicByBaseOpcode(
193+
OffsetMappingInfo->NoOffset, ImageDimIntr->Dim);
194+
return modifyIntrinsicCall(
195+
II, NewImageDimIntr->Intr, IC, [&](auto &Args, auto &ArgTys) {
196+
Args.erase(Args.begin() + ImageDimIntr->OffsetIndex);
197+
});
198+
}
199+
}
200+
}
201+
185202
// Try to use A16 or G16
186203
if (!ST->hasA16() && !ST->hasG16())
187204
return None;

llvm/lib/Target/AMDGPU/MIMGInstructions.td

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,22 @@ def MIMGBiasMappingTable : GenericTable {
147147
let PrimaryKeyName = "getMIMGBiasMappingInfo";
148148
}
149149

150+
class MIMGOffsetMapping<MIMGBaseOpcode offset, MIMGBaseOpcode nooffset> {
151+
MIMGBaseOpcode Offset = offset;
152+
MIMGBaseOpcode NoOffset = nooffset;
153+
}
154+
155+
def MIMGOffsetMappingTable : GenericTable {
156+
let FilterClass = "MIMGOffsetMapping";
157+
let CppTypeName = "MIMGOffsetMappingInfo";
158+
let Fields = ["Offset", "NoOffset"];
159+
string TypeOf_Offset = "MIMGBaseOpcode";
160+
string TypeOf_NoOffset = "MIMGBaseOpcode";
161+
162+
let PrimaryKey = ["Offset"];
163+
let PrimaryKeyName = "getMIMGOffsetMappingInfo";
164+
}
165+
150166
class MIMGG16Mapping<MIMGBaseOpcode g, MIMGBaseOpcode g16> {
151167
MIMGBaseOpcode G = g;
152168
MIMGBaseOpcode G16 = g16;
@@ -1174,6 +1190,48 @@ def : MIMGBiasMapping<IMAGE_GATHER4_B_CL_O, IMAGE_GATHER4_CL_O>;
11741190
def : MIMGBiasMapping<IMAGE_GATHER4_C_B_O, IMAGE_GATHER4_C_O>;
11751191
def : MIMGBiasMapping<IMAGE_GATHER4_C_B_CL_O, IMAGE_GATHER4_C_CL_O>;
11761192

1193+
// Offset to NoOffset Optimization Mapping
1194+
def : MIMGOffsetMapping<IMAGE_SAMPLE_O, IMAGE_SAMPLE>;
1195+
def : MIMGOffsetMapping<IMAGE_SAMPLE_CL_O, IMAGE_SAMPLE_CL>;
1196+
def : MIMGOffsetMapping<IMAGE_SAMPLE_D_O, IMAGE_SAMPLE_D>;
1197+
def : MIMGOffsetMapping<IMAGE_SAMPLE_D_CL_O, IMAGE_SAMPLE_D_CL>;
1198+
def : MIMGOffsetMapping<IMAGE_SAMPLE_D_O_G16, IMAGE_SAMPLE_D_G16>;
1199+
def : MIMGOffsetMapping<IMAGE_SAMPLE_D_CL_O_G16, IMAGE_SAMPLE_D_CL_G16>;
1200+
def : MIMGOffsetMapping<IMAGE_SAMPLE_L_O, IMAGE_SAMPLE_L>;
1201+
def : MIMGOffsetMapping<IMAGE_SAMPLE_B_O, IMAGE_SAMPLE_B>;
1202+
def : MIMGOffsetMapping<IMAGE_SAMPLE_B_CL_O, IMAGE_SAMPLE_B_CL>;
1203+
def : MIMGOffsetMapping<IMAGE_SAMPLE_LZ_O, IMAGE_SAMPLE_LZ>;
1204+
def : MIMGOffsetMapping<IMAGE_SAMPLE_C_O, IMAGE_SAMPLE_C>;
1205+
def : MIMGOffsetMapping<IMAGE_SAMPLE_C_CL_O, IMAGE_SAMPLE_C_CL>;
1206+
def : MIMGOffsetMapping<IMAGE_SAMPLE_C_D_O, IMAGE_SAMPLE_C_D>;
1207+
def : MIMGOffsetMapping<IMAGE_SAMPLE_C_D_CL_O, IMAGE_SAMPLE_C_D_CL>;
1208+
def : MIMGOffsetMapping<IMAGE_SAMPLE_C_D_O_G16, IMAGE_SAMPLE_C_D_G16>;
1209+
def : MIMGOffsetMapping<IMAGE_SAMPLE_C_D_CL_O_G16, IMAGE_SAMPLE_C_D_CL_G16>;
1210+
def : MIMGOffsetMapping<IMAGE_SAMPLE_C_L_O, IMAGE_SAMPLE_C_L>;
1211+
def : MIMGOffsetMapping<IMAGE_SAMPLE_C_B_CL_O, IMAGE_SAMPLE_C_B_CL>;
1212+
def : MIMGOffsetMapping<IMAGE_SAMPLE_C_B_O, IMAGE_SAMPLE_C_B>;
1213+
def : MIMGOffsetMapping<IMAGE_SAMPLE_C_LZ_O, IMAGE_SAMPLE_C_LZ>;
1214+
def : MIMGOffsetMapping<IMAGE_GATHER4_O, IMAGE_GATHER4>;
1215+
def : MIMGOffsetMapping<IMAGE_GATHER4_CL_O, IMAGE_GATHER4_CL>;
1216+
def : MIMGOffsetMapping<IMAGE_GATHER4_L_O, IMAGE_GATHER4_L>;
1217+
def : MIMGOffsetMapping<IMAGE_GATHER4_B_O, IMAGE_GATHER4_B>;
1218+
def : MIMGOffsetMapping<IMAGE_GATHER4_B_CL_O, IMAGE_GATHER4_B_CL>;
1219+
def : MIMGOffsetMapping<IMAGE_GATHER4_LZ_O, IMAGE_GATHER4_LZ>;
1220+
def : MIMGOffsetMapping<IMAGE_GATHER4_C_O, IMAGE_GATHER4_C>;
1221+
def : MIMGOffsetMapping<IMAGE_GATHER4_C_CL_O, IMAGE_GATHER4_C_CL>;
1222+
def : MIMGOffsetMapping<IMAGE_GATHER4_C_L_O, IMAGE_GATHER4_C_L>;
1223+
def : MIMGOffsetMapping<IMAGE_GATHER4_C_B_O, IMAGE_GATHER4_C_B>;
1224+
def : MIMGOffsetMapping<IMAGE_GATHER4_C_B_CL_O, IMAGE_GATHER4_C_B_CL>;
1225+
def : MIMGOffsetMapping<IMAGE_GATHER4_C_LZ_O, IMAGE_GATHER4_C_LZ>;
1226+
def : MIMGOffsetMapping<IMAGE_SAMPLE_CD_O, IMAGE_SAMPLE_CD>;
1227+
def : MIMGOffsetMapping<IMAGE_SAMPLE_CD_CL_O, IMAGE_SAMPLE_CD_CL>;
1228+
def : MIMGOffsetMapping<IMAGE_SAMPLE_C_CD_O, IMAGE_SAMPLE_C_CD>;
1229+
def : MIMGOffsetMapping<IMAGE_SAMPLE_C_CD_CL_O, IMAGE_SAMPLE_C_CD_CL>;
1230+
def : MIMGOffsetMapping<IMAGE_SAMPLE_CD_O_G16, IMAGE_SAMPLE_CD_G16>;
1231+
def : MIMGOffsetMapping<IMAGE_SAMPLE_CD_CL_O_G16, IMAGE_SAMPLE_CD_CL_G16>;
1232+
def : MIMGOffsetMapping<IMAGE_SAMPLE_C_CD_O_G16, IMAGE_SAMPLE_C_CD_G16>;
1233+
def : MIMGOffsetMapping<IMAGE_SAMPLE_C_CD_CL_O_G16, IMAGE_SAMPLE_C_CD_CL_G16>;
1234+
11771235
// G to G16 Optimization Mapping
11781236
def : MIMGG16Mapping<IMAGE_SAMPLE_D, IMAGE_SAMPLE_D_G16>;
11791237
def : MIMGG16Mapping<IMAGE_SAMPLE_D_CL, IMAGE_SAMPLE_D_CL_G16>;

llvm/lib/Target/AMDGPU/Utils/AMDGPUBaseInfo.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,7 @@ bool isHsaAbiVersion3Or4(const MCSubtargetInfo *STI) {
133133
#define GET_MIMGLZMappingTable_IMPL
134134
#define GET_MIMGMIPMappingTable_IMPL
135135
#define GET_MIMGBiasMappingTable_IMPL
136+
#define GET_MIMGOffsetMappingTable_IMPL
136137
#define GET_MIMGG16MappingTable_IMPL
137138
#include "AMDGPUGenSearchableTables.inc"
138139

llvm/lib/Target/AMDGPU/Utils/AMDGPUBaseInfo.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -336,6 +336,11 @@ struct MIMGBiasMappingInfo {
336336
MIMGBaseOpcode NoBias;
337337
};
338338

339+
struct MIMGOffsetMappingInfo {
340+
MIMGBaseOpcode Offset;
341+
MIMGBaseOpcode NoOffset;
342+
};
343+
339344
struct MIMGG16MappingInfo {
340345
MIMGBaseOpcode G;
341346
MIMGBaseOpcode G16;
@@ -350,6 +355,9 @@ const MIMGMIPMappingInfo *getMIMGMIPMappingInfo(unsigned MIP);
350355
LLVM_READONLY
351356
const MIMGBiasMappingInfo *getMIMGBiasMappingInfo(unsigned Bias);
352357

358+
LLVM_READONLY
359+
const MIMGOffsetMappingInfo *getMIMGOffsetMappingInfo(unsigned Offset);
360+
353361
LLVM_READONLY
354362
const MIMGG16MappingInfo *getMIMGG16MappingInfo(unsigned G);
355363

0 commit comments

Comments
 (0)