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
|
/*========================== begin_copyright_notice ============================
Copyright (C) 2019-2021 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 <llvm/IR/Constants.h>
#include <llvm/IR/Function.h>
#include "common/LLVMWarningsPop.hpp"
#include "common/IGCIRBuilder.h"
#include "Compiler/CISACodeGen/helper.h"
#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_AccessToSrcPtrMap.clear();
m_AddressUsedSrcPtrMap.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)
{
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;
case GenISAIntrinsic::GenISA_intatomicrawA64:
// Ignore a buffer in this intrinsic, keep it stateless.
return;
default:
IGC_ASSERT_MESSAGE(0, "Unsupported Instruction");
return;
}
}
else
return;
}
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.
return;
}
// Save the instruction, which makes access (load/store/intrinsic) to the buffer
m_AccessToSrcPtrMap[inst] = srcPtr;
// 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.
if (tempList.size() > 1)
{
m_AddressUsedSrcPtrMap[tempList[tempList.size()-2]] = srcPtr;
}
else
{
m_AddressUsedSrcPtrMap[inst] = srcPtr;
}
}
void PromoteStatelessToBindless::PromoteStatelessToBindlessBuffers(Function& F) const
{
ModuleMetaData* modMD = getAnalysis<MetaDataUtilsWrapper>().getModuleMetaData();
MetaDataUtils * pMdUtils = getAnalysis<MetaDataUtilsWrapper>().getMetaDataUtils();
ImplicitArgs implicitArgs(F, pMdUtils);
// Modify the reference to the buffer not through all users but only in instructions
// which are used in accesing (load/store) the buffer.
for (auto inst : m_AddressUsedSrcPtrMap)
{
Instruction* accessInst = cast<Instruction>(inst.first);
Argument* srcPtr = cast<Argument>(inst.second);
Value* nullSrcPtr = ConstantPointerNull::get(cast<PointerType>(srcPtr->getType()));
accessInst->replaceUsesOfWith(srcPtr, nullSrcPtr);
if (modMD->FuncMD.find(&F) != modMD->FuncMD.end())
{
FunctionMetaData* funcMD = &modMD->FuncMD[&F];
ResourceAllocMD* resourceAlloc = &funcMD->resAllocMD;
ArgAllocMD* argInfo = &resourceAlloc->argAllocMDList[srcPtr->getArgNo()];
IGC_ASSERT_MESSAGE((size_t)srcPtr->getArgNo() < resourceAlloc->argAllocMDList.size(), "ArgAllocMD List Out of Bounds");
if (argInfo->type == ResourceTypeEnum::UAVResourceType)
{
// Update metadata to show bindless resource type
argInfo->type = ResourceTypeEnum::BindlessUAVResourceType;
}
}
}
for (auto inst : m_AccessToSrcPtrMap)
{
Instruction* accessInst = cast<Instruction>(inst.first);
Argument* srcPtr = cast<Argument>(inst.second);
// Get the base bindless pointer
IGCIRBuilder<> builder(accessInst);
Value* resourcePtr = IGC::GetBufferOperand(accessInst);
unsigned bindlessAS = IGC::EncodeAS4GFXResource(*UndefValue::get(builder.getInt32Ty()), IGC::BINDLESS);
PointerType* basePointerType = PointerType::get(resourcePtr->getType()->getPointerElementType(), 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();
}
}
}
}
|