Skip to content

Commit 4ec5f04

Browse files
fftzengigcbot
authored andcommitted
check array index range for tex folding
check array index range for tex folding
1 parent 62c05d6 commit 4ec5f04

File tree

3 files changed

+22
-5
lines changed

3 files changed

+22
-5
lines changed

IGC/Compiler/DynamicTextureFolding.cpp

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -54,11 +54,22 @@ void DynamicTextureFolding::FoldSingleTextureValue(CallInst& I)
5454
bool directIdx = false;
5555
uint textureIndex = 0;
5656
DecodeAS4GFXResource(addrSpace, directIdx, textureIndex);
57-
57+
IRBuilder<> builder(&I);
5858
// if the current texture index is found in modMD as uniform texture, replace the texture load/sample as constant.
5959
auto it = modMD->inlineDynTextures.find(textureIndex);
6060
if (it != modMD->inlineDynTextures.end())
6161
{
62+
builder.SetInsertPoint(&I);
63+
// check if the array index is out of bound. The array index size is passed in from UMD
64+
// we might check xyz coordinate later but skip for now
65+
Value* cmpInstA = nullptr;
66+
if (I.getOperand(3)->getType()->isIntOrIntVectorTy())
67+
cmpInstA = builder.CreateICmp(ICmpInst::ICMP_SLE, I.getOperand(3), ConstantInt::get(Type::getInt32Ty(I.getContext()), it->second[7]));
68+
else if (I.getOperand(3)->getType()->isFPOrFPVectorTy())
69+
cmpInstA = builder.CreateICmp(ICmpInst::FCMP_ULE, I.getOperand(3), ConstantFP::get(Type::getFloatTy(I.getContext()), it->second[7]));
70+
else
71+
return;
72+
6273
for (auto iter = I.user_begin(); iter != I.user_end(); iter++)
6374
{
6475
if (llvm::ExtractElementInst* pExtract = llvm::dyn_cast<llvm::ExtractElementInst>(*iter))
@@ -67,11 +78,17 @@ void DynamicTextureFolding::FoldSingleTextureValue(CallInst& I)
6778
{
6879
if ((&I)->getType()->isIntOrIntVectorTy())
6980
{
70-
pExtract->replaceAllUsesWith(ConstantInt::get((pExtract)->getType(), (it->second[(uint32_t)(pIdx->getZExtValue())])));
81+
llvm::Value* Zero = ConstantInt::get(Type::getInt32Ty(I.getContext()), 0);
82+
Value* IntInst = ConstantInt::get((pExtract)->getType(), (it->second[(uint32_t)(pIdx->getZExtValue())]));
83+
Value* newInst = builder.CreateSelect(cmpInstA, IntInst, Zero);
84+
pExtract->replaceAllUsesWith(newInst);
7185
}
7286
else if ((&I)->getType()->isFPOrFPVectorTy())
7387
{
74-
pExtract->replaceAllUsesWith(ConstantFP::get((pExtract)->getType(), *(float*)&(it->second[(uint32_t)(pIdx->getZExtValue())])));
88+
llvm::Value* ZeroFP = ConstantFP::get(Type::getFloatTy(I.getContext()), 0.0);
89+
Value* fpInst = ConstantFP::get((pExtract)->getType(), *(float*)&(it->second[(uint32_t)(pIdx->getZExtValue())]));
90+
Value* newInst = builder.CreateSelect(cmpInstA, fpInst, ZeroFP);
91+
pExtract->replaceAllUsesWith(newInst);
7592
}
7693
}
7794
}

IGC/Compiler/tests/DynamicTextureFolding/basic.ll

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818

1919
define void @test(float %src1, float %src2, float %src3, float* %dst) {
2020
; CHECK-LABEL: @test(
21-
; CHECK: store float 0x36B0000000000000, float* [[DST:%.*]], align 4
21+
; CHECK: store float 0.000000e+00, float* [[DST:%.*]], align 4
2222
; CHECK: store float 0.000000e+00, float* [[DST]], align 4
2323
; CHECK: store float 0.000000e+00, float* [[DST]], align 4
2424
; CHECK: store float 0.000000e+00, float* [[DST]], align 4

IGC/common/MDFrameWork.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -639,7 +639,7 @@ namespace IGC
639639
uint32_t NBarrierCnt = 0;
640640
RayTraceModuleInfo rtInfo;
641641
uint32_t CurUniqueIndirectIdx = DefaultIndirectIdx;
642-
std::map<uint32_t, std::array<uint32_t, 4>> inlineDynTextures;
642+
std::map<uint32_t, std::array<uint32_t, 8>> inlineDynTextures;
643643
std::vector<InlineResInfo> inlineResInfoData;
644644
ImmConstantInfo immConstant;
645645
std::set<llvm::GlobalVariable*> stringConstants;

0 commit comments

Comments
 (0)