Skip to content

Commit 7a963fc

Browse files
srividyakarumurisys_zuul
authored and
sys_zuul
committed
Constant fold with 0 for OOB access to immediate constant buffer
Change-Id: I5ff335d0b540ec0cb19dbb61f83cd5ea64c5aace
1 parent 0f646f3 commit 7a963fc

File tree

5 files changed

+30
-12
lines changed

5 files changed

+30
-12
lines changed

IGC/Compiler/CISACodeGen/helper.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -599,7 +599,7 @@ namespace IGC
599599
return false;
600600
}
601601

602-
bool EvalConstantAddress(Value* address, unsigned int& offset, const llvm::DataLayout* pDL, Value* ptrSrc)
602+
bool EvalConstantAddress(Value* address, int& offset, const llvm::DataLayout* pDL, Value* ptrSrc)
603603
{
604604

605605
if ((ptrSrc == nullptr && isa<ConstantPointerNull>(address)) ||
@@ -616,7 +616,7 @@ namespace IGC
616616
ConstantInt* eltIdx = dyn_cast<ConstantInt>(eltIdxVal);
617617
if (!eltIdx)
618618
return false;
619-
offset = int_cast<unsigned>(eltIdx->getZExtValue());
619+
offset = int_cast<int>(eltIdx->getZExtValue());
620620
return true;
621621
}
622622
}
@@ -633,7 +633,7 @@ namespace IGC
633633
ConstantInt * eltIdx = dyn_cast<ConstantInt>(eltIdxVal);
634634
if (!eltIdx)
635635
return false;
636-
offset = int_cast<unsigned>(eltIdx->getZExtValue());
636+
offset = int_cast<int>(eltIdx->getZExtValue());
637637
return true;
638638
}
639639
else if (ptrExpr->getOpcode() == Instruction::GetElementPtr)
@@ -650,14 +650,14 @@ namespace IGC
650650
if (StructType * StTy = GTI.getStructTypeOrNull()) {
651651
unsigned Field = int_cast<unsigned>(cast<ConstantInt>(Idx)->getZExtValue());
652652
if (Field) {
653-
offset += int_cast<unsigned int>(pDL->getStructLayout(StTy)->getElementOffset(Field));
653+
offset += int_cast<int>(pDL->getStructLayout(StTy)->getElementOffset(Field));
654654
}
655655
Ty = StTy->getElementType(Field);
656656
}
657657
else {
658658
Ty = GTI.getIndexedType();
659659
if (const ConstantInt * CI = dyn_cast<ConstantInt>(Idx)) {
660-
offset += int_cast<unsigned int>(
660+
offset += int_cast<int>(
661661
pDL->getTypeAllocSize(Ty) * CI->getSExtValue());
662662

663663
}

IGC/Compiler/CISACodeGen/helper.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ namespace IGC
158158
bool GetGRFOffsetFromRTV(llvm::Value* pointerSrc, unsigned& GRFOffset);
159159
bool GetStatelessBufferInfo(llvm::Value* pointer, unsigned& bufIdOrGRFOffset, IGC::BufferType& bufferTy, llvm::Value*& bufferSrcPtr, bool& isDirectBuf);
160160
// try to evaluate the address if it is constant.
161-
bool EvalConstantAddress(llvm::Value* address, unsigned int& offset, const llvm::DataLayout* pDL, llvm::Value* ptrSrc = nullptr);
161+
bool EvalConstantAddress(llvm::Value* address, int& offset, const llvm::DataLayout* pDL, llvm::Value* ptrSrc = nullptr);
162162

163163

164164
bool isSampleLoadGather4InfoInstruction(llvm::Instruction* inst);

IGC/Compiler/CustomSafeOptPass.cpp

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2565,7 +2565,7 @@ Constant* IGCConstProp::replaceShaderConstant(LoadInst* inst)
25652565
if (modMD && ((directBuf && (bufType == CONSTANT_BUFFER)) || statelessBuf))
25662566
{
25672567
Value* ptrVal = inst->getPointerOperand();
2568-
unsigned eltId = 0;
2568+
int eltId = 0;
25692569
size_in_bytes = (unsigned int)inst->getType()->getPrimitiveSizeInBits() / 8;
25702570
if (!EvalConstantAddress(ptrVal, eltId, m_TD, pointerSrc))
25712571
{
@@ -2586,18 +2586,36 @@ Constant* IGCConstProp::replaceShaderConstant(LoadInst* inst)
25862586
uint32_t eltSize_in_bytes = (unsigned int)srcEltTy->getPrimitiveSizeInBits() / 8;
25872587
IRBuilder<> builder(inst);
25882588
Value* vectorValue = UndefValue::get(inst->getType());
2589+
char* pEltValue; // Pointer to element value
25892590
for (uint i = 0; i < srcNElts; i++)
25902591
{
2592+
if (eltId < 0 || eltId >= (int)modMD->immConstant.data.size())
2593+
{
2594+
int OOBvalue = 0; // OOB access to immediate constant buffer should return 0
2595+
char* pOOBvalue = (char*)& OOBvalue; // Pointer to value 0 which is a OOB access value
2596+
pEltValue = pOOBvalue;
2597+
}
2598+
else
2599+
pEltValue = offset + eltId + (i * eltSize_in_bytes);
25912600
vectorValue = builder.CreateInsertElement(
25922601
vectorValue,
2593-
GetConstantValue(srcEltTy, offset + eltId + (i * eltSize_in_bytes)),
2602+
GetConstantValue(srcEltTy, pEltValue),
25942603
builder.getInt32(i));
25952604
}
25962605
return dyn_cast<Constant>(vectorValue);
25972606
}
25982607
else
25992608
{
2600-
return GetConstantValue(inst->getType(), offset + eltId);
2609+
char* pEltValue; // Pointer to element value
2610+
if (eltId < 0 || eltId >= (int)modMD->immConstant.data.size())
2611+
{
2612+
int OOBvalue = 0; // OOB access to immediate constant buffer should return 0
2613+
char* pOOBvalue = (char*)& OOBvalue; // Pointer to value 0 which is a OOB access value
2614+
pEltValue = pOOBvalue;
2615+
}
2616+
else
2617+
pEltValue = offset + eltId;
2618+
return GetConstantValue(inst->getType(), pEltValue);
26012619
}
26022620
}
26032621
else if ((!IGC_IS_FLAG_ENABLED(DisableDynamicConstantFolding)) && (modMD->inlineDynConstants.size() > 0))

IGC/Compiler/FindInterestingConstants.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,7 @@ void FindInterestingConstants::FoldsToSourcePropagate(llvm::Instruction* I)
192192
}
193193

194194
// Get constant address from load instruction
195-
bool FindInterestingConstants::getConstantAddress(llvm::LoadInst& I, unsigned& bufIdOrGRFOffset, unsigned& eltId, int& size_in_bytes)
195+
bool FindInterestingConstants::getConstantAddress(llvm::LoadInst& I, unsigned& bufIdOrGRFOffset, int& eltId, int& size_in_bytes)
196196
{
197197
// Check if the load instruction is with constant buffer address
198198
unsigned as = I.getPointerAddressSpace();
@@ -306,7 +306,7 @@ void FindInterestingConstants::visitLoadInst(llvm::LoadInst& I)
306306
}
307307

308308
unsigned bufIdOrGRFOffset;
309-
unsigned eltId;
309+
int eltId;
310310
int size_in_bytes;
311311

312312
m_foldsToZero = 0;

IGC/Compiler/FindInterestingConstants.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ namespace IGC
7070
const llvm::DataLayout* m_DL;
7171

7272
// Helper functions
73-
bool getConstantAddress(llvm::LoadInst& I, unsigned& bufIdOrGRFOffset, unsigned& eltId, int& size_in_bytes);
73+
bool getConstantAddress(llvm::LoadInst& I, unsigned& bufIdOrGRFOffset, int& eltId, int& size_in_bytes);
7474
bool FoldsToConst(llvm::Instruction* inst, llvm::Instruction* use, bool& propagate);
7575
bool FoldsToZero(llvm::Instruction* inst, llvm::Instruction* use);
7676
bool FoldsToSource(llvm::Instruction* inst, llvm::Instruction* use);

0 commit comments

Comments
 (0)