Skip to content

Commit 0dd6d9b

Browse files
houjenkoigcbot
authored andcommitted
[IGC feature][CodePatch] Passing context for code patching
1 parent ed1920c commit 0dd6d9b

File tree

9 files changed

+91
-6
lines changed

9 files changed

+91
-6
lines changed

IGC/Compiler/CISACodeGen/CISABuilder.cpp

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4392,7 +4392,7 @@ namespace IGC
43924392
}
43934393
}
43944394

4395-
void CEncoder::InitEncoder(bool canAbortOnSpill, bool hasStackCall)
4395+
void CEncoder::InitEncoder(bool canAbortOnSpill, bool hasStackCall, VISAKernel* prevKernel)
43964396
{
43974397
m_aliasesMap.clear();
43984398
m_encoderState.m_SubSpanDestination = false;
@@ -4431,6 +4431,10 @@ namespace IGC
44314431
V(CreateVISABuilder(vbuilder, builderMode, builderOpt, VISAPlatform, params.size(), params.data(),
44324432
&m_vISAWaTable));
44334433

4434+
if (IsCodePatchCandidate())
4435+
{
4436+
SetHasPrevKernel(prevKernel != nullptr);
4437+
}
44344438
InitVISABuilderOptions(VISAPlatform, canAbortOnSpill, hasStackCall, builderOpt == VISA_BUILDER_BOTH);
44354439

44364440
// Pass all build options to builder
@@ -4486,6 +4490,7 @@ namespace IGC
44864490
}
44874491

44884492
V(vbuilder->AddKernel(vKernel, kernelName.c_str()));
4493+
V(vbuilder->SetPrevKernel(prevKernel));
44894494
V(vKernel->AddKernelAttribute("OutputAsmPath", asmName.length(), asmName.c_str()));
44904495

44914496
SetDispatchSimdSize();

IGC/Compiler/CISACodeGen/CISABuilder.hpp

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ namespace IGC
131131
class CEncoder
132132
{
133133
public:
134-
void InitEncoder(bool canAbortOnSpill, bool hasStackCall);
134+
void InitEncoder(bool canAbortOnSpill, bool hasStackCall, VISAKernel* prevKernel);
135135
void InitBuildParams(llvm::SmallVector<std::unique_ptr< char, std::function<void(char*)>>, 10> & params);
136136
void InitVISABuilderOptions(TARGET_PLATFORM VISAPlatform, bool canAbortOnSpill, bool hasStackCall, bool enableVISA_IR);
137137
SEncoderState CopyEncoderState();
@@ -360,6 +360,10 @@ namespace IGC
360360

361361
inline void SetIsCodePatchCandidate(bool v);
362362
inline bool IsCodePatchCandidate();
363+
inline unsigned int GetPayloadEnd();
364+
inline void SetPayloadEnd(unsigned int payloadEnd);
365+
inline void SetHasPrevKernel(bool v);
366+
inline bool HasPrevKernel();
363367
inline void BeginForcedNoMaskRegion();
364368
inline void EndForcedNoMaskRegion();
365369

@@ -601,6 +605,8 @@ namespace IGC
601605
// This is for CodePatch to split payload interpolation from a shader
602606
VISAKernel* vPayloadSection;
603607
VISAKernel* vKernelTmp;
608+
bool m_hasPrevKernel = false;
609+
unsigned int m_payloadEnd = 0;
604610

605611
bool m_isCodePatchCandidate = false;
606612

@@ -929,6 +935,26 @@ namespace IGC
929935
return m_isCodePatchCandidate;
930936
}
931937

938+
inline void CEncoder::SetPayloadEnd(unsigned int payloadEnd)
939+
{
940+
m_payloadEnd = payloadEnd;
941+
}
942+
943+
inline unsigned int CEncoder::GetPayloadEnd()
944+
{
945+
return m_payloadEnd;
946+
}
947+
948+
inline void CEncoder::SetHasPrevKernel(bool v)
949+
{
950+
m_hasPrevKernel = v;
951+
}
952+
953+
inline bool CEncoder::HasPrevKernel()
954+
{
955+
return m_hasPrevKernel;
956+
}
957+
932958
inline void CEncoder::BeginForcedNoMaskRegion()
933959
{
934960
m_insideForcedNoMaskRegion = true;

IGC/Compiler/CISACodeGen/EmitVISAPass.cpp

Lines changed: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -562,6 +562,7 @@ bool EmitPass::runOnFunction(llvm::Function& F)
562562
m_currShader->SetCoalescingEngineHelper(m_CE);
563563
}
564564

565+
CShader* prevShader = m_pCtx->m_prevShader;
565566
bool hasStackCall = m_FGA && m_FGA->getGroup(&F)->hasStackCall();
566567
if (!m_FGA || m_FGA->isGroupHead(&F))
567568
{
@@ -573,11 +574,24 @@ bool EmitPass::runOnFunction(llvm::Function& F)
573574
return false;
574575
}
575576

577+
VISAKernel* prevKernel = nullptr;
578+
579+
if (prevShader &&
580+
m_currShader->IsPatchablePS() &&
581+
m_encoder->GetSimdSize() == prevShader->GetEncoder().GetSimdSize() &&
582+
prevShader->GetEncoder().IsCodePatchCandidate() &&
583+
prevShader->ProgramOutput()->m_programBin &&
584+
prevShader->ProgramOutput()->m_scratchSpaceUsedBySpills == 0)
585+
{
586+
prevKernel = prevShader->GetEncoder().GetVISAKernel();
587+
m_encoder->SetPayloadEnd(prevShader->GetEncoder().GetPayloadEnd());
588+
}
589+
576590
if (IGC_GET_FLAG_VALUE(CodePatch) &&
577591
((!m_pCtx->hash.nosHash) || IGC_GET_FLAG_VALUE(CodePatch) > CodePatch_Enable_NoLTO) &&
578592
m_currShader->IsPatchablePS() &&
579593
m_SimdMode == SIMDMode::SIMD16 &&
580-
(m_ShaderDispatchMode != ShaderDispatchMode::NOT_APPLICABLE) &&
594+
(m_ShaderDispatchMode != ShaderDispatchMode::NOT_APPLICABLE || prevKernel) &&
581595
(IGC_GET_FLAG_VALUE(CodePatchLimit) == 0 || 2 <= IGC_GET_FLAG_VALUE(CodePatchLimit)))
582596
{
583597
m_encoder->SetIsCodePatchCandidate(true);
@@ -616,7 +630,7 @@ bool EmitPass::runOnFunction(llvm::Function& F)
616630
}
617631

618632
// call builder after pre-analysis pass where scratchspace offset to VISA is calculated
619-
m_encoder->InitEncoder(m_canAbortOnSpill, hasStackCall);
633+
m_encoder->InitEncoder(m_canAbortOnSpill, hasStackCall, prevKernel);
620634
initDefaultRoundingMode();
621635
m_currShader->PreCompile();
622636

@@ -900,6 +914,13 @@ bool EmitPass::runOnFunction(llvm::Function& F)
900914
errs() << IGC_GET_FLAG_VALUE(CodePatchLimit) << " Prologue/CodePatch : " << m_encoder->GetShaderName() << "\n";
901915
}
902916
}
917+
else
918+
{
919+
if (IGC_GET_FLAG_VALUE(CodePatchExperiments))
920+
{
921+
errs() << IGC_GET_FLAG_VALUE(CodePatchLimit) << " not : " << m_encoder->GetShaderName() << "\n";
922+
}
923+
}
903924
}
904925

905926
if (m_currShader->GetDebugInfoData())
@@ -925,6 +946,7 @@ bool EmitPass::runOnFunction(llvm::Function& F)
925946
compileWithSymbolTable = true;
926947
}
927948
m_encoder->Compile(compileWithSymbolTable);
949+
m_pCtx->m_prevShader = m_currShader;
928950
// if we are doing stack-call, do the following:
929951
// - Hard-code a large scratch-space for visa
930952
if (hasStackCall)
@@ -945,9 +967,17 @@ bool EmitPass::runOnFunction(llvm::Function& F)
945967
{
946968
IF_DEBUG_INFO(IDebugEmitter::Release(m_pDebugEmitter);)
947969

970+
if(!m_encoder->IsCodePatchCandidate() || m_encoder->HasPrevKernel())
971+
{
972+
m_pCtx->m_prevShader = nullptr;
948973
// Postpone destroying VISA builder to
949-
// after emitting debug info
974+
// after emitting debug info and passing context for code patching
950975
m_encoder->DestroyVISABuilder();
976+
}
977+
if (m_encoder->IsCodePatchCandidate() && m_encoder->HasPrevKernel())
978+
{
979+
prevShader->GetEncoder().DestroyVISABuilder();
980+
}
951981
}
952982
}
953983

IGC/Compiler/CISACodeGen/PixelShaderCodeGen.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -306,6 +306,15 @@ void CPixelShader::AllocatePSPayload()
306306
if (encoder.IsCodePatchCandidate())
307307
{
308308
encoder.SetPayloadSectionAsSecondary();
309+
if (encoder.HasPrevKernel())
310+
{
311+
// Get previous context's PayloadEnd to ensure it is patchable
312+
offset = encoder.GetPayloadEnd();
313+
}
314+
else
315+
{
316+
encoder.SetPayloadEnd(payloadEnd);
317+
}
309318
}
310319
if (offset % getGRFSize() != 0)
311320
{

IGC/Compiler/CodeGenPublic.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -843,6 +843,9 @@ namespace IGC
843843
std::vector<unsigned> m_indexableTempSize;
844844
bool m_highPsRegisterPressure = 0;
845845

846+
// Record previous simd for code patching
847+
CShader* m_prevShader = nullptr;
848+
846849
// For IR dump after pass
847850
unsigned m_numPasses = 0;
848851
bool m_threadCombiningOptDone = false;

visa/BuildCISAIR.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ class CISA_IR_Builder : public VISABuilder
6060
mBuildOption = buildOption;
6161
m_kernel_count = 0;
6262
m_function_count = 0;
63+
m_prevKernel = nullptr;
6364

6465
m_header.major_version = majorVersion;
6566
m_header.minor_version = minorVersion;
@@ -82,6 +83,7 @@ class CISA_IR_Builder : public VISABuilder
8283
const PWA_TABLE pWaTable = nullptr);
8384
static int DestroyBuilder(CISA_IR_Builder *builder);
8485
VISA_BUILDER_API virtual int AddKernel(VISAKernel *& kernel, const char* kernelName);
86+
VISA_BUILDER_API virtual int SetPrevKernel(VISAKernel *& prevKernel);
8587
VISA_BUILDER_API virtual int AddFunction(VISAFunction *& function, const char* functionName);
8688
VISA_BUILDER_API virtual int AddPayloadSection(VISAFunction *& function, const char* functionName);
8789
VISA_BUILDER_API virtual int Compile(const char * isaFileNameint, std::ostream * os = nullptr, bool emit_visa_only = false);
@@ -106,6 +108,7 @@ class CISA_IR_Builder : public VISABuilder
106108

107109
// the current vISA kernel/function being processed
108110
VISAKernelImpl *m_kernel;
111+
VISAKernelImpl *m_prevKernel;
109112
CisaFramework::CisaBinary *m_cisaBinary;
110113
VISAKernelImpl * get_kernel() { return m_kernel; }
111114

visa/BuildCISAIRImpl.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -356,6 +356,15 @@ int CISA_IR_Builder::AddKernel(VISAKernel *& kernel, const char* kernelName)
356356
return VISA_SUCCESS;
357357
}
358358

359+
int CISA_IR_Builder::SetPrevKernel(VISAKernel *& prevKernel)
360+
{
361+
if (prevKernel)
362+
{
363+
m_prevKernel = (VISAKernelImpl*)prevKernel;
364+
}
365+
return VISA_SUCCESS;
366+
}
367+
359368
int CISA_IR_Builder::AddFunction(VISAFunction *& function, const char* functionName)
360369
{
361370
if (function)

visa/VISAKernelImpl.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -619,7 +619,6 @@ void VISAKernelImpl::CopyVars(VISAKernelImpl* from)
619619

620620
int VISAKernelImpl::InitializeKernel(const char *kernel_name)
621621
{
622-
623622
int status = VISA_SUCCESS;
624623
m_num_pred_vars = Get_CISA_PreDefined_Var_Count();
625624
setName(kernel_name);

visa/include/VISABuilderAPIDefinition.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -846,6 +846,7 @@ class VISABuilder
846846
{
847847
public:
848848
VISA_BUILDER_API virtual int AddKernel(VISAKernel *& kernel, const char* kernelName) = 0;
849+
VISA_BUILDER_API virtual int SetPrevKernel(VISAKernel*& prevKernel) = 0;
849850
VISA_BUILDER_API virtual int AddFunction(VISAFunction *& function, const char* functionName) = 0;
850851
VISA_BUILDER_API virtual int AddPayloadSection(VISAFunction *& function, const char* functionName) = 0;
851852
VISA_BUILDER_API virtual int Compile(const char * isaFileNameint, std::ostream* os = nullptr, bool emit_visa_only = false) = 0;

0 commit comments

Comments
 (0)