@@ -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
0 commit comments