@@ -670,12 +670,21 @@ void SIFrameLowering::emitEntryFunctionFlatScratchInit(
670670}
671671
672672// Note SGPRSpill stack IDs should only be used for SGPR spilling to VGPRs, not
673- // memory. They should have been removed by now.
674- static bool allStackObjectsAreDead (const MachineFrameInfo &MFI) {
673+ // memory. They should have been removed by now, except CFI Saved Reg spills.
674+ static bool allStackObjectsAreDead (const MachineFunction &MF) {
675+ const MachineFrameInfo &MFI = MF.getFrameInfo ();
676+ const SIMachineFunctionInfo *FuncInfo = MF.getInfo <SIMachineFunctionInfo>();
675677 for (int I = MFI.getObjectIndexBegin (), E = MFI.getObjectIndexEnd ();
676678 I != E; ++I) {
677- if (!MFI.isDeadObjectIndex (I))
679+ if (!MFI.isDeadObjectIndex (I)) {
680+ // determineCalleeSaves() might have added the SGPRSpill stack IDs for
681+ // CFI saves into scratch VGPR, ignore them
682+ if (MFI.getStackID (I) == TargetStackID::SGPRSpill &&
683+ FuncInfo->checkIndexInPrologEpilogSGPRSpills (I)) {
684+ continue ;
685+ }
678686 return false ;
687+ }
679688 }
680689
681690 return true ;
@@ -695,8 +704,8 @@ Register SIFrameLowering::getEntryFunctionReservedScratchRsrcReg(
695704
696705 Register ScratchRsrcReg = MFI->getScratchRSrcReg ();
697706
698- if (!ScratchRsrcReg || (!MRI. isPhysRegUsed (ScratchRsrcReg) &&
699- allStackObjectsAreDead (MF. getFrameInfo () )))
707+ if (!ScratchRsrcReg ||
708+ (!MRI. isPhysRegUsed (ScratchRsrcReg) && allStackObjectsAreDead (MF)))
700709 return Register ();
701710
702711 if (ST.hasSGPRInitBug () ||
@@ -923,7 +932,7 @@ void SIFrameLowering::emitEntryFunctionPrologue(MachineFunction &MF,
923932 bool NeedsFlatScratchInit =
924933 MFI->getUserSGPRInfo ().hasFlatScratchInit () &&
925934 (MRI.isPhysRegUsed (AMDGPU::FLAT_SCR) || FrameInfo.hasCalls () ||
926- (!allStackObjectsAreDead (FrameInfo ) && ST.enableFlatScratch ()));
935+ (!allStackObjectsAreDead (MF ) && ST.enableFlatScratch ()));
927936
928937 if ((NeedsFlatScratchInit || ScratchRsrcReg) &&
929938 PreloadedScratchWaveOffsetReg && !ST.flatScratchIsArchitected ()) {
@@ -1314,6 +1323,11 @@ void SIFrameLowering::emitCSRSpillStores(
13141323 LiveUnits.addReg (Reg);
13151324 }
13161325 }
1326+
1327+ // Remove the spill entry created for EXEC. It is needed only for CFISaves in
1328+ // the prologue.
1329+ if (TRI.isCFISavedRegsSpillEnabled ())
1330+ FuncInfo->removePrologEpilogSGPRSpillEntry (TRI.getExec ());
13171331}
13181332
13191333void SIFrameLowering::emitCSRSpillRestores (
@@ -1796,14 +1810,14 @@ void SIFrameLowering::processFunctionBeforeFrameFinalized(
17961810 // can. Any remaining SGPR spills will go to memory, so move them back to the
17971811 // default stack.
17981812 bool HaveSGPRToVMemSpill =
1799- FuncInfo->removeDeadFrameIndices (MFI , /* ResetSGPRSpillStackIDs*/ true );
1813+ FuncInfo->removeDeadFrameIndices (MF , /* ResetSGPRSpillStackIDs*/ true );
18001814 assert (allSGPRSpillsAreDead (MF) &&
18011815 " SGPR spill should have been removed in SILowerSGPRSpills" );
18021816
18031817 // FIXME: The other checks should be redundant with allStackObjectsAreDead,
18041818 // but currently hasNonSpillStackObjects is set only from source
18051819 // allocas. Stack temps produced from legalization are not counted currently.
1806- if (!allStackObjectsAreDead (MFI )) {
1820+ if (!allStackObjectsAreDead (MF )) {
18071821 assert (RS && " RegScavenger required if spilling" );
18081822
18091823 // Add an emergency spill slot
@@ -1903,6 +1917,18 @@ void SIFrameLowering::determinePrologEpilogSGPRSaves(
19031917 MFI->setSGPRForEXECCopy (AMDGPU::NoRegister);
19041918 }
19051919
1920+ if (TRI->isCFISavedRegsSpillEnabled ()) {
1921+ Register Exec = TRI->getExec ();
1922+ assert (!MFI->hasPrologEpilogSGPRSpillEntry (Exec) &&
1923+ " Re-reserving spill slot for EXEC" );
1924+ // FIXME: Machine Copy Propagation currently optimizes away the EXEC copy to
1925+ // the scratch as we emit it only in the prolog. This optimization should
1926+ // not happen for frame related instructions. Until this is fixed ignore
1927+ // copy to scratch SGPR.
1928+ getVGPRSpillLaneOrTempRegister (MF, LiveUnits, Exec, RC,
1929+ /* IncludeScratchCopy=*/ false );
1930+ }
1931+
19061932 // hasFP only knows about stack objects that already exist. We're now
19071933 // determining the stack slots that will be created, so we have to predict
19081934 // them. Stack objects force FP usage with calls.
@@ -1912,8 +1938,7 @@ void SIFrameLowering::determinePrologEpilogSGPRSaves(
19121938 //
19131939 // FIXME: Is this really hasReservedCallFrame?
19141940 const bool WillHaveFP =
1915- FrameInfo.hasCalls () &&
1916- (SavedVGPRs.any () || !allStackObjectsAreDead (FrameInfo));
1941+ FrameInfo.hasCalls () && (SavedVGPRs.any () || !allStackObjectsAreDead (MF));
19171942
19181943 if (WillHaveFP || hasFP (MF)) {
19191944 Register FramePtrReg = MFI->getFrameOffsetReg ();
0 commit comments