1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388
|
/*========================== begin_copyright_notice ============================
Copyright (C) 2017-2021 Intel Corporation
SPDX-License-Identifier: MIT
============================= end_copyright_notice ===========================*/
#include "common/debug/Debug.hpp"
#include "common/debug/Dump.hpp"
#include "Compiler/CISACodeGen/helper.h"
#include "Compiler/FixResourcePtr.hpp"
#include "Compiler/CodeGenPublic.h"
#include "Compiler/IGCPassSupport.h"
#include "GenISAIntrinsics/GenIntrinsics.h"
#include "GenISAIntrinsics/GenIntrinsicInst.h"
#include "common/LLVMWarningsPush.hpp"
#include <llvm/IR/IRBuilder.h>
#include <llvm/IR/Module.h>
#include <llvm/IR/Function.h>
#include <llvm/IR/Instructions.h>
#include <llvm/IR/InstIterator.h>
#include "common/LLVMWarningsPop.hpp"
#include "Probe/Assertion.h"
using namespace llvm;
using namespace IGC;
// Register pass to igc-opt
#define PASS_FLAG "igc-fix-resource-ptr"
#define PASS_DESCRIPTION "Fix the usage of GetBufferPtr, no combination of GetBufferPtr and GetResourcePtr"
#define PASS_CFG_ONLY false
#define PASS_ANALYSIS false
IGC_INITIALIZE_PASS_BEGIN(FixResourcePtr, PASS_FLAG, PASS_DESCRIPTION, PASS_CFG_ONLY, PASS_ANALYSIS)
IGC_INITIALIZE_PASS_END(FixResourcePtr, PASS_FLAG, PASS_DESCRIPTION, PASS_CFG_ONLY, PASS_ANALYSIS)
char FixResourcePtr::ID = 0;
FixResourcePtr::FixResourcePtr() : FunctionPass(ID)
{
initializeFixResourcePtrPass(*PassRegistry::getPassRegistry());
}
bool FixResourcePtr::runOnFunction(llvm::Function& F)
{
llvm::IRBuilder<> __builder(F.getContext());
builder = &__builder;
DL = &F.getParent()->getDataLayout();
m_changed = false;
curFunc = &F;
std::vector<Instruction*> fixlist;
// initialize worklist with all the GetBufferPtrs with immed resource/sampler id
inst_iterator it = inst_begin(&F);
inst_iterator e = inst_end(&F);
for (; it != e; ++it)
{
GenIntrinsicInst* inst = dyn_cast<GenIntrinsicInst>(&*it);
if (inst && inst->getIntrinsicID() == GenISAIntrinsic::GenISA_GetBufferPtr)
{
unsigned int as = inst->getType()->getPointerAddressSpace();
Value* bufferIndex = ResolveBufferIndex(inst->getOperand(0));
if (isa<ConstantInt>(bufferIndex) && IGC::IsDirectIdx(as))
{
RemoveGetBufferPtr(inst, bufferIndex);
}
else
{
fixlist.push_back(inst);
}
}
}
// fix the load/store that uses pointer coming from a GetBufferPtr/GetElementPtr,
// change it to intrinsic that directly uses buf-pointer
while (!fixlist.empty())
{
Instruction* inst = fixlist.back();
fixlist.pop_back();
FindGetElementPtr(inst, inst);
}
while (!eraseList.empty())
{
Instruction* inst = eraseList.back();
eraseList.pop_back();
inst->eraseFromParent();
}
return m_changed;
}
void FixResourcePtr::RemoveGetBufferPtr(GenIntrinsicInst* bufPtr, Value* bufIdx)
{
uint outAS = bufPtr->getType()->getPointerAddressSpace();
uint origAS = outAS;
BufferType bufType = (BufferType)(cast<ConstantInt>(bufPtr->getOperand(1))->getZExtValue());
uint encodeAS = EncodeAS4GFXResource(*bufIdx, bufType);
if (outAS != encodeAS &&
(bufType == CONSTANT_BUFFER || bufType == RESOURCE || bufType == UAV))
{
// happens to OGL, need to fix if address-space encoding is wrong
outAS = encodeAS;
}
std::vector<Instruction*> foldlist;
foldlist.push_back(bufPtr);
// fold instructions on the worklist to constant null-pointer value
while (!foldlist.empty())
{
Instruction* inst = foldlist.back();
foldlist.pop_back();
PointerType* const instType = dyn_cast<PointerType>(inst->getType());
IGC_ASSERT(nullptr != instType);
Type* eltType = instType->getPointerElementType();
PointerType* ptrType = PointerType::get(eltType, outAS);
inst->mutateType(ptrType);
// iterate all the uses, put bitcast on the worklist
for (auto UI = inst->user_begin(), E = inst->user_end(); UI != E; ++UI)
{
if (BitCastInst * use = dyn_cast<BitCastInst>(*UI))
{
foldlist.push_back(use);
}
else if (GetElementPtrInst * gep = dyn_cast<GetElementPtrInst>(*UI))
{
Value* byteOffset = GetByteOffset(gep);
builder->SetInsertPoint(gep);
Value* int2ptr = builder->CreateIntToPtr(byteOffset, ptrType);
gep->mutateType(ptrType);
gep->replaceAllUsesWith(int2ptr);
if (outAS != origAS)
{
FixAddressSpaceInAllUses(int2ptr, outAS, origAS);
}
}
}
ConstantPointerNull* basePtr = ConstantPointerNull::get(ptrType);
inst->replaceAllUsesWith(basePtr);
}
}
void FixResourcePtr::FindGetElementPtr(Instruction* bufPtr, Instruction* searchPtr)
{
// iterate all the uses, recursively find the GetElementPtr
for (auto UI = searchPtr->user_begin(), E = searchPtr->user_end(); UI != E; ++UI)
{
if (BitCastInst * use = dyn_cast<BitCastInst>(*UI))
{
FindGetElementPtr(bufPtr, use);
}
else if (GetElementPtrInst * use = dyn_cast<GetElementPtrInst>(*UI))
{
FindLoadStore(bufPtr, use, use);
}
else if (LoadInst * ld = dyn_cast<LoadInst>(*UI))
{
// fix load
Value* offsetValue = builder->getInt32(0);
Value* loadIndexed = CreateLoadIntrinsic(ld, bufPtr, offsetValue);
ld->replaceAllUsesWith(loadIndexed);
eraseList.push_back(ld);
}
else if (StoreInst * st = dyn_cast<StoreInst>(*UI))
{
// fix store
Value* offsetValue = builder->getInt32(0);
Value* storeIndexed = CreateStoreIntrinsic(st, bufPtr, offsetValue);
st->replaceAllUsesWith(storeIndexed);
eraseList.push_back(st);
}
}
}
void FixResourcePtr::FindLoadStore(Instruction* bufPtr, Instruction* eltPtr, Instruction* searchPtr)
{
// iterate all the uses, put bitcast on the worklist, recursively find the load/store
for (auto UI = searchPtr->user_begin(), E = searchPtr->user_end(); UI != E; ++UI)
{
if (BitCastInst * use = dyn_cast<BitCastInst>(*UI))
{
FindLoadStore(bufPtr, eltPtr, use);
}
else if (LoadInst * use = dyn_cast<LoadInst>(*UI))
{
// fix load
Value* offsetValue = GetByteOffset(eltPtr);
Value* loadIndexed = CreateLoadIntrinsic(use, bufPtr, offsetValue);
use->replaceAllUsesWith(loadIndexed);
eraseList.push_back(use);
}
else if (StoreInst * use = dyn_cast<StoreInst>(*UI))
{
// fix store
Value* offsetValue = GetByteOffset(eltPtr);
Value* storeIndexed = CreateStoreIntrinsic(use, bufPtr, offsetValue);
use->replaceAllUsesWith(storeIndexed);
eraseList.push_back(use);
}
}
}
Value* FixResourcePtr::GetByteOffset(Instruction* eltPtr)
{
IGC_ASSERT(eltPtr->getNumOperands() == 2);
Value* ptrOp = eltPtr->getOperand(0);
PointerType* ptrTy = dyn_cast<PointerType>(ptrOp->getType());
IGC_ASSERT(ptrTy);
Value* eltIdx = eltPtr->getOperand(1);
builder->SetInsertPoint(eltPtr);
// decide offset in bytes
// may need to create shift
uint eltBytes = int_cast<uint>(DL->getTypeStoreSize(ptrTy->getPointerElementType()));
APInt eltSize = APInt(32, eltBytes);
Value* offsetValue = eltIdx;
if (eltSize != 1)
{
if (const ConstantInt * CI = dyn_cast<ConstantInt>(eltIdx))
{
uint32_t byteOffset = int_cast<uint32_t>(eltBytes * CI->getSExtValue());
offsetValue = ConstantInt::get(eltIdx->getType(), byteOffset);
}
else if (eltSize.isPowerOf2())
{
APInt shift = APInt(32, eltSize.logBase2());
offsetValue = builder->CreateShl(eltIdx, shift);
}
else
{
offsetValue = builder->CreateMul(eltIdx, builder->getInt(eltSize));
}
}
return offsetValue;
}
Value* FixResourcePtr::CreateLoadIntrinsic(LoadInst* inst, Instruction* bufPtr, Value* offsetVal)
{
Function* l;
builder->SetInsertPoint(inst);
llvm::Type* tys[2];
tys[0] = inst->getType();
tys[1] = bufPtr->getType();
l = GenISAIntrinsic::getDeclaration(curFunc->getParent(),
inst->getType()->isVectorTy() ? llvm::GenISAIntrinsic::GenISA_ldrawvector_indexed : llvm::GenISAIntrinsic::GenISA_ldraw_indexed,
tys);
unsigned alignment = std::max(inst->getType()->getScalarSizeInBits() / 8,
(unsigned)inst->getAlignment());
Value* attr[] =
{
bufPtr,
offsetVal,
builder->getInt32(alignment),
builder->getInt1(inst->isVolatile())
};
Value* ld = builder->CreateCall(l, attr);
if (!inst->getType()->isVectorTy())
{
if (!inst->getType()->isFloatTy())
{
Value* bitcast = dyn_cast<Instruction>(builder->CreateBitCast(ld, inst->getType()));
ld = bitcast;
}
}
return ld;
}
Value* FixResourcePtr::CreateStoreIntrinsic(StoreInst* inst, Instruction* bufPtr, Value* offsetVal)
{
Function* l;
builder->SetInsertPoint(inst);
Value* storeVal = inst->getValueOperand();
if (storeVal->getType()->isVectorTy())
{
llvm::Type* tys[2];
tys[0] = bufPtr->getType();
tys[1] = inst->getValueOperand()->getType();
l = GenISAIntrinsic::getDeclaration(curFunc->getParent(),
llvm::GenISAIntrinsic::GenISA_storerawvector_indexed,
tys);
}
else
{
llvm::Type* dataType = storeVal->getType();
IGC_ASSERT(dataType->getPrimitiveSizeInBits() == 16 || dataType->getPrimitiveSizeInBits() == 32);
if (!dataType->isFloatingPointTy())
{
storeVal = builder->CreateBitCast(
storeVal,
dataType->getPrimitiveSizeInBits() == 32 ? builder->getFloatTy() : builder->getHalfTy());
}
llvm::Type* types[2] = {
bufPtr->getType(),
storeVal->getType() };
l = GenISAIntrinsic::getDeclaration(curFunc->getParent(),
llvm::GenISAIntrinsic::GenISA_storeraw_indexed,
types);
}
unsigned alignment = std::max(storeVal->getType()->getScalarSizeInBits() / 8,
(unsigned)inst->getAlignment());
Value* attr[] =
{
bufPtr,
offsetVal,
storeVal,
builder->getInt32(alignment),
builder->getInt1(inst->isVolatile())
};
Value* st = builder->CreateCall(l, attr);
return st;
}
///
/// @brief Resolves the buffer index argument of GetBufferPtr intrinsic.
///
/// This method looks for a ConstInt value of the buffer index. It handles
/// cases where the input buffer index is an extracted element of a vector,
/// e.g.
///
/// %17 = extractelement <2 x i32> zeroinitializer, i32 0
/// %19 = insertelement <4 x i32> undef, i32 %17, i32 0
/// %20 = insertelement <4 x i32> %19, i32 %15, i32 1
/// %21 = insertelement <4 x i32> %20, i32 %15, i32 2
/// %Temp-47.i = insertelement <4 x i32> %21, i32 0, i32 3
/// %22 = extractelement <4 x i32> %Temp-47.i, i32 0
/// %23 = extractelement <4 x i32> %Temp-47.i, i32 1
/// %24 = extractelement <4 x i32> %Temp-47.i, i32 2
/// %25 = extractelement <4 x i32> %Temp-47.i, i32 3
/// %26 = call i32 addrspace(1179648)* @llvm.genx.GenISA.GetBufferPtr.p1179648i32(i32 %22, i32 1)
///
/// @param bufferIndex Buffer index value to resolve
/// @param vectorIndex Index inside of a vector where the buffer index is stored
/// @returns Value* ConstantInt buffer index if resolved or the input bufferIndex
///
Value* FixResourcePtr::ResolveBufferIndex(Value* bufferIndex, Value* vectorIndex)
{
if (Constant * c = dyn_cast<Constant>(bufferIndex))
{
if (vectorIndex && isa<ConstantInt>(vectorIndex))
{
return c->getAggregateElement(cast<ConstantInt>(vectorIndex));
}
else if (isa<ConstantInt>(bufferIndex))
{
return bufferIndex;
}
else
{
IGC_ASSERT(0);
}
}
else if (ExtractElementInst * ee = dyn_cast<ExtractElementInst>(bufferIndex))
{
return ResolveBufferIndex(
ee->getOperand(0), // vector argument to extract from
ee->getOperand(1)); // element index in vector
}
else if (InsertElementInst * ie = dyn_cast<InsertElementInst>(bufferIndex))
{
if (vectorIndex &&
isa<ConstantInt>(vectorIndex) &&
isa<ConstantInt>(ie->getOperand(2)))
{
// compare indexes in vectors
if ((cast<ConstantInt>(vectorIndex))->getZExtValue() == (cast<ConstantInt>(ie->getOperand(2)))->getZExtValue())
{
// indexes match,this is the insert element instruction with buffer index
return ResolveBufferIndex(ie->getOperand(1));
}
else
{
// indexes don't match, keep looking for a matching insert element instruction
return ResolveBufferIndex(ie->getOperand(0), vectorIndex);
}
}
}
return bufferIndex;
}
|