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
|
/*========================== begin_copyright_notice ============================
Copyright (C) 2019-2024 Intel Corporation
SPDX-License-Identifier: MIT
============================= end_copyright_notice ===========================*/
#include "PromoteStatelessToBindless.h"
#include "AdaptorCommon/ImplicitArgs.hpp"
#include "Compiler/IGCPassSupport.h"
#include "common/LLVMWarningsPush.hpp"
#include "llvmWrapper/IR/DerivedTypes.h"
#include <llvm/IR/Constants.h>
#include <llvm/IR/Function.h>
#include "common/LLVMWarningsPop.hpp"
#include "common/IGCIRBuilder.h"
#include "Compiler/CISACodeGen/helper.h"
#include "Compiler/CISACodeGen/OpenCLKernelCodeGen.hpp"
#include "Probe/Assertion.h"
using namespace IGC::IGCMD;
using namespace llvm;
using namespace IGC;
using namespace GenISAIntrinsic;
// Register pass to igc-opt
#define PASS_FLAG "igc-promote-stateless-to-bindless"
#define PASS_DESCRIPTION "Pass promotes stateless accesses to bindless accesses"
#define PASS_CFG_ONLY false
#define PASS_ANALYSIS false
IGC_INITIALIZE_PASS_BEGIN(PromoteStatelessToBindless, PASS_FLAG, PASS_DESCRIPTION, PASS_CFG_ONLY, PASS_ANALYSIS)
IGC_INITIALIZE_PASS_DEPENDENCY(CodeGenContextWrapper)
IGC_INITIALIZE_PASS_END(PromoteStatelessToBindless, PASS_FLAG, PASS_DESCRIPTION, PASS_CFG_ONLY, PASS_ANALYSIS)
char PromoteStatelessToBindless::ID = 0;
PromoteStatelessToBindless::PromoteStatelessToBindless()
: FunctionPass(ID),
m_PrintfBuffer(nullptr)
{
initializePromoteStatelessToBindlessPass(*PassRegistry::getPassRegistry());
}
bool PromoteStatelessToBindless::runOnFunction(Function& F)
{
CodeGenContext* ctx = getAnalysis<CodeGenContextWrapper>().getCodeGenContext();
auto ClContext = static_cast<OpenCLProgramContext*>(ctx);
bool HasStackCall = F.hasFnAttribute("visaStackCall");
// Skip functions marked with stackcall.
if (HasStackCall)
return false;
m_SrcPtrNeedStatelessAccess.clear();
m_SrcPtrToAccessMap.clear();
if (!ClContext->m_InternalOptions.UseBindlessPrintf)
{
CheckPrintfBuffer(F);
}
visit(F);
PromoteStatelessToBindlessBuffers(F);
return true;
}
void PromoteStatelessToBindless::visitInstruction(Instruction& I)
{
Value* bufptr = IGC::GetBufferOperand(&I);
if (bufptr && bufptr->getType()->isPointerTy())
{
GetAccessInstToSrcPointerMap(&I, bufptr);
}
}
void PromoteStatelessToBindless::CheckPrintfBuffer(Function& F)
{
MetaDataUtils* MdUtils = getAnalysis<MetaDataUtilsWrapper>().getMetaDataUtils();
ImplicitArgs implicitArgs(F, MdUtils);
m_PrintfBuffer = implicitArgs.getImplicitArgValue(F, ImplicitArg::PRINTF_BUFFER, MdUtils);
}
void PromoteStatelessToBindless::GetAccessInstToSrcPointerMap(Instruction* inst, Value* resourcePtr)
{
bool canPromoteAccess = true;
auto modMD = getAnalysis<MetaDataUtilsWrapper>().getModuleMetaData();
unsigned addrSpace = resourcePtr->getType()->getPointerAddressSpace();
if (addrSpace != ADDRESS_SPACE_GLOBAL && addrSpace != ADDRESS_SPACE_CONSTANT)
{
// Only try to promote stateless buffer pointers ( as(1) or as(2) )
return;
}
//We only support LoadInst, StoreInst, GenISA_simdBlockRead, and GenISA_simdBlockWrite intrinsic
if (!isa<LoadInst>(inst) && !isa<StoreInst>(inst))
{
if (GenIntrinsicInst * GInst = dyn_cast<GenIntrinsicInst>(inst))
{
switch (GInst->getIntrinsicID())
{
case GenISAIntrinsic::GenISA_simdBlockRead:
case GenISAIntrinsic::GenISA_simdBlockWrite:
break;
default:
canPromoteAccess = false;
break;
}
}
else
canPromoteAccess = false;
}
std::vector<Value*> tempList;
Value* srcPtr = IGC::TracePointerSource(resourcePtr, false, true, true, tempList);
if (!srcPtr ||
!srcPtr->getType()->isPointerTy() ||
!isa<Argument>(srcPtr))
{
// Cannot trace the resource pointer back to it's source, cannot promote
return;
}
if (m_PrintfBuffer && srcPtr == m_PrintfBuffer)
{
// Process PrintfBuffer separately. Printf implementation required operations with
// printf buffer address (through atomic add), see printf implementation in
// OpenCLPrintfResolution.cpp. Currently keep printf implementation as stateless and
// thus skip printf buffer for now.
canPromoteAccess = false;
}
if (modMD->compOpt.UseLegacyBindlessMode)
{
if (!canPromoteAccess)
{
// In this case, the srcPtr is traced to a kernel arg, but the access instruction does not support
// bindless access, so we have to make all access stateless.
// Remove all access instructions of this srcPtr that may have been added in previous passes, to
// prevent promoting it to bindless.
m_SrcPtrNeedStatelessAccess.insert(srcPtr);
m_SrcPtrToAccessMap.erase(srcPtr);
return;
}
else if (m_SrcPtrNeedStatelessAccess.count(srcPtr) != 0)
{
return;
}
}
if (canPromoteAccess)
{
// Save the instruction, which makes access (load/store/intrinsic) to the buffer
Value* accessInst = inst;
// Save the instruction, which generate an address of the buffer. This is the
// instruction right before the last one. The last one has to be the buffer itself.
Value* addrUsedInst = (tempList.size() > 1) ? tempList[tempList.size() - 2] : inst;
m_SrcPtrToAccessMap[srcPtr].push_back(std::make_pair(accessInst, addrUsedInst));
}
}
void PromoteStatelessToBindless::PromoteStatelessToBindlessBuffers(Function& F) const
{
CodeGenContext* ctx = getAnalysis<CodeGenContextWrapper>().getCodeGenContext();
ModuleMetaData* modMD = getAnalysis<MetaDataUtilsWrapper>().getModuleMetaData();
MetaDataUtils * pMdUtils = getAnalysis<MetaDataUtilsWrapper>().getMetaDataUtils();
ImplicitArgs implicitArgs(F, pMdUtils);
if (modMD->FuncMD.find(&F) == modMD->FuncMD.end())
return;
FunctionMetaData* funcMD = &modMD->FuncMD[&F];
ResourceAllocMD* resourceAlloc = &funcMD->resAllocMD;
bool supportDynamicBTIsAllocation = ctx->platform.supportDynamicBTIsAllocation() && ctx->enableZEBinary();
for (auto &iter : m_SrcPtrToAccessMap)
{
Argument* srcPtr = cast<Argument>(iter.first);
ArgAllocMD* argInfo = &resourceAlloc->argAllocMDList[srcPtr->getArgNo()];
IGC_ASSERT_MESSAGE((size_t)srcPtr->getArgNo() < resourceAlloc->argAllocMDList.size(), "ArgAllocMD List Out of Bounds");
if (modMD->compOpt.UseLegacyBindlessMode)
{
// Update metadata to show bindless resource type.
// Do this only for legacy mode, since the resource type of the original
// kernel arg needs to be bindless for it to be reinterpreted as a bindless offset.
// In advanced mode, always keep the original kernel arg as stateless, and use the
// BINDLESS_OFFSET arg for bindless access.
argInfo->type = ResourceTypeEnum::BindlessUAVResourceType;
}
else
{
// In advanced mode, there must be a corresponding implicit arg BINDLESS_OFFSET for every
// explicit buffer arg for it to be promoted to bindless. Check if this implcit arg exists,
// and skip promotion if we can't find it.
if (!implicitArgs.isImplicitArgExistForNumberedArg(ImplicitArg::BINDLESS_OFFSET, srcPtr->getArgNo()))
continue;
}
if (supportDynamicBTIsAllocation)
{
argInfo->indexType =
resourceAlloc->uavsNumType +
(unsigned)std::distance(m_SrcPtrToAccessMap.begin(), m_SrcPtrToAccessMap.find(srcPtr));
}
// Loop through all access instructions for srcPtr
for (auto &insts : iter.second)
{
Instruction* accessInst = cast<Instruction>(insts.first);
Instruction* addrUsedInst = cast<Instruction>(insts.second);
// Modify the reference to the buffer not through all users but only in instructions
// which are used in accesing (load/store) the buffer.
Value* nullSrcPtr = ConstantPointerNull::get(cast<PointerType>(srcPtr->getType()));
addrUsedInst->replaceUsesOfWith(srcPtr, nullSrcPtr);
// Get the base bindless pointer
IGCIRBuilder<> builder(accessInst);
Value* resourcePtr = IGC::GetBufferOperand(accessInst);
IGC_ASSERT(resourcePtr);
unsigned bindlessAS = IGC::EncodeAS4GFXResource(*UndefValue::get(builder.getInt32Ty()), IGC::BINDLESS);
PointerType* basePointerType = IGCLLVM::getWithSamePointeeType(dyn_cast<PointerType>(resourcePtr->getType()), bindlessAS);
Value* bufferOffset = builder.CreatePtrToInt(resourcePtr, builder.getInt32Ty());
Value* basePointer = nullptr;
if (!modMD->compOpt.UseLegacyBindlessMode) {
Argument* srcOffset = implicitArgs.getNumberedImplicitArg(F, ImplicitArg::BINDLESS_OFFSET, srcPtr->getArgNo());
basePointer = builder.CreateIntToPtr(srcOffset, basePointerType);
}
else {
basePointer = builder.CreatePointerCast(srcPtr, basePointerType);
}
if (LoadInst* load = dyn_cast<LoadInst>(accessInst))
{
Value* ldraw = IGC::CreateLoadRawIntrinsic(load, cast<Instruction>(basePointer), bufferOffset);
load->replaceAllUsesWith(ldraw);
load->eraseFromParent();
}
else if (StoreInst* store = dyn_cast<StoreInst>(accessInst))
{
IGC::CreateStoreRawIntrinsic(store, cast<Instruction>(basePointer), bufferOffset);
store->eraseFromParent();
}
else if (GenIntrinsicInst* pIntr = dyn_cast<GenIntrinsicInst>(accessInst))
{
if (pIntr->getIntrinsicID() == GenISAIntrinsic::GenISA_simdBlockRead)
{
Function* newBlockReadFunc = GenISAIntrinsic::getDeclaration(F.getParent(),
GenISAIntrinsic::GenISA_simdBlockReadBindless,
{ accessInst->getType(), basePointer->getType(),Type::getInt32Ty(accessInst->getContext()) });
Instruction* newBlockRead = CallInst::Create(newBlockReadFunc, { basePointer, bufferOffset }, "", accessInst);
newBlockRead->setDebugLoc(pIntr->getDebugLoc());
accessInst->replaceAllUsesWith(newBlockRead);
accessInst->eraseFromParent();
}
else if (pIntr->getIntrinsicID() == GenISAIntrinsic::GenISA_simdBlockWrite)
{
Function* newBlockWriteFunc = GenISAIntrinsic::getDeclaration(F.getParent(),
GenISAIntrinsic::GenISA_simdBlockWriteBindless,
{ basePointer->getType(), pIntr->getOperand(1)->getType(), Type::getInt32Ty(accessInst->getContext()) });
Instruction* newBlockWrite = CallInst::Create(newBlockWriteFunc, { basePointer, pIntr->getOperand(1), bufferOffset }, "", accessInst);
newBlockWrite->setDebugLoc(pIntr->getDebugLoc());
accessInst->replaceAllUsesWith(newBlockWrite);
accessInst->eraseFromParent();
}
}
}
}
if(supportDynamicBTIsAllocation)
resourceAlloc->uavsNumType += m_SrcPtrToAccessMap.size();
}
|