Skip to content

Commit

Permalink
Reuse constant vector payload for subsequent stores
Browse files Browse the repository at this point in the history
Reuse constant vector payload for subsequent stores when possible
  • Loading branch information
admitric authored and igcbot committed Jun 29, 2023
1 parent 936a2cc commit 8578146
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 4 deletions.
42 changes: 39 additions & 3 deletions IGC/Compiler/CISACodeGen/EmitVISAPass.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11257,6 +11257,7 @@ void EmitPass::emitStoreRawIndexed(
varOffset,
immOffset,
pValToStore,
inst->getParent(),
cacheOpts,
inst->getAlignment(),
false);
Expand Down Expand Up @@ -11441,6 +11442,7 @@ void EmitPass::emitStore(
varOffset,
immOffset,
inst->getValueOperand(),
inst->getParent(),
cacheOpts,
IGCLLVM::getAlignmentValue(inst),
inst->getMetadata("enable.vmask"));
Expand Down Expand Up @@ -18170,15 +18172,16 @@ void EmitPass::emitLSCVectorStore_uniform(
void EmitPass::emitLSCVectorStore(
Value* Ptr,
Value* varOffset, ConstantInt* immOffset,
Value* storedVal, LSC_CACHE_OPTS cacheOpts, alignment_t align, bool dontForceDmask)
Value* storedVal, BasicBlock* BB,
LSC_CACHE_OPTS cacheOpts, alignment_t align, bool dontForceDmask)
{
PointerType* ptrType = cast<PointerType>(Ptr->getType());
Type* Ty = storedVal->getType();
IGCLLVM::FixedVectorType* VTy = dyn_cast<IGCLLVM::FixedVectorType>(Ty);
Type* eltTy = VTy ? VTy->getElementType() : Ty;
uint32_t eltBytes = GetScalarTypeSizeInRegister(eltTy);
uint32_t elts = VTy ? int_cast<uint32_t>(VTy->getNumElements()) : 1;
CVariable* storedVar = GetSymbol(storedVal);

unsigned int width = numLanes(m_currShader->m_SIMDSize);

ResourceDescriptor resource = GetResourceVariable(Ptr);
Expand Down Expand Up @@ -18210,6 +18213,28 @@ void EmitPass::emitLSCVectorStore(
// when true, one simd instance is generated only!
bool isOCLUniform = isUniformStoreOCL(varOffset, storedVal);

CVariable* storedVar;

// If constant vector is stored and there is already var instance for it
// try reusing it (if it was defined in the same basic block)
// or create a new var instance and make it available for reusing in further stores
Constant* constantStoredVal = dyn_cast<Constant>(storedVal);
ConstVectorStoreData* constVectorData = nullptr;
if (constantStoredVal && constantStoredVal->getType()->isVectorTy())
{
if (!m_constantVectorStores.count(constantStoredVal) ||
!(m_constantVectorStores[constantStoredVal].BB == BB))
{
m_constantVectorStores[constantStoredVal] = {GetSymbol(storedVal), nullptr, BB};
}
constVectorData = &m_constantVectorStores[constantStoredVal];
storedVar = constVectorData->var;
}
else
{
storedVar = GetSymbol(storedVal);
}

// In case eOffset isn't GRF aligned, need to create a copy
// For non-uniform variable, it should be already GRF-aligned.
eOffset = ReAlignUniformVariable(eOffset, EALIGN_GRF);
Expand Down Expand Up @@ -18246,7 +18271,18 @@ void EmitPass::emitLSCVectorStore(
VecMessInfo.getLSCInfo(Ty, align, m_currShader->GetContext(), useA32, false);

eOffset = BroadcastIfUniform(eOffset);
storedVar = BroadcastIfUniform(storedVar);

// try reusing the old broadcasted data as well
if (constVectorData)
{
storedVar = constVectorData->broadcastedVar ?
constVectorData->broadcastedVar : BroadcastIfUniform(storedVar);
constVectorData->broadcastedVar = storedVar;
}
else
{
storedVar = BroadcastIfUniform(storedVar);
}

ResourceLoop(resource, [&](CVariable* flag) {
for (uint32_t i = 0; i < VecMessInfo.numInsts; ++i)
Expand Down
10 changes: 9 additions & 1 deletion IGC/Compiler/CISACodeGen/EmitVISAPass.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -432,7 +432,8 @@ class EmitPass : public llvm::FunctionPass
uint64_t align);
void emitLSCVectorStore(llvm::Value *Ptr,
llvm::Value *offset, llvm::ConstantInt *immOffset,
llvm::Value *storedVal, LSC_CACHE_OPTS cacheOpts,
llvm::Value *storedVal, llvm::BasicBlock* BB,
LSC_CACHE_OPTS cacheOpts,
alignment_t align, bool dontForceDMask);
void emitGenISACopy(llvm::GenIntrinsicInst* GenCopyInst);
void emitUniformVectorCopy(CVariable* Dst, CVariable* Src, uint32_t nElts,
Expand Down Expand Up @@ -955,6 +956,13 @@ class EmitPass : public llvm::FunctionPass
bool m_isDuplicate;
CVariable *m_tmpDest = nullptr;
std::set<CoalescingEngine::CCTuple *> lifetimeStartAdded;

typedef struct {
CVariable* var;
CVariable* broadcastedVar;
llvm::BasicBlock* BB;
} ConstVectorStoreData;
llvm::DenseMap<llvm::Constant*, ConstVectorStoreData> m_constantVectorStores;
};

} // namespace IGC

0 comments on commit 8578146

Please sign in to comment.