@@ -915,16 +915,13 @@ namespace IGC {
915915
916916 bool Changed = loopSink (F);
917917
918- if (Changed)
919- {
920- IGC_ASSERT (false == verifyFunction (F, &dbgs ()));
921- }
922-
923918 if (IGC_IS_FLAG_ENABLED (DumpLoopSink) && IGC_IS_FLAG_DISABLED (PrintToConsole))
924919 {
925920 dumpToFile (Log);
926921 }
927922
923+ IGC_ASSERT (false == verifyFunction (F, &dbgs ()));
924+
928925 return Changed;
929926 }
930927
@@ -1163,6 +1160,16 @@ namespace IGC {
11631160 for (BasicBlock *BB: L->blocks ())
11641161 AffectedBBs.insert (BB);
11651162
1163+ // Save original positions for rollback
1164+ DenseMap<BasicBlock *, InstrVec> OriginalPositions;
1165+ for (BasicBlock *BB : AffectedBBs)
1166+ {
1167+ InstrVec BBInstructions;
1168+ for (Instruction &I : *BB)
1169+ BBInstructions.push_back (&I);
1170+ OriginalPositions[BB] = BBInstructions;
1171+ }
1172+
11661173 auto rerunLiveness = [&]()
11671174 {
11681175 for (BasicBlock *BB: AffectedBBs)
@@ -1706,27 +1713,20 @@ namespace IGC {
17061713
17071714 if (NeedToRollback && IGC_IS_FLAG_DISABLED (LoopSinkDisableRollback))
17081715 {
1709- PrintDump (VerbosityLevel::Low, " >> Reverting the changes.\n " );
1716+ PrintDump (VerbosityLevel::Low, " >> Reverting the changes.\n\n " );
17101717
1711- CandidatePtrSet RevertedCandidates;
1712-
1713- for (auto CI = SinkedCandidates.rbegin (), CE = SinkedCandidates.rend (); CI != CE; CI++)
1718+ for (auto &[BB, BBInstructions] : OriginalPositions)
17141719 {
1715- Candidate *C = CI->get ();
1716- Instruction *UndoPos = C->UndoPos ;
1717- IGC_ASSERT (UndoPos);
1718- while (InstToCandidate.count (UndoPos))
1719- {
1720- if (RevertedCandidates.count (InstToCandidate[UndoPos]))
1721- break ;
1722- UndoPos = InstToCandidate[UndoPos]->UndoPos ;
1723- }
1724- for (Instruction *I : *C)
1720+ Instruction *InsertPoint = nullptr ;
1721+ for (Instruction *I : BBInstructions)
17251722 {
1726- I->moveBefore (UndoPos);
1727- UndoPos = I;
1723+ if (InsertPoint)
1724+ I->moveAfter (InsertPoint);
1725+ else
1726+ I->moveBefore (&*BB->getFirstInsertionPt ());
1727+
1728+ InsertPoint = I;
17281729 }
1729- RevertedCandidates.insert (C);
17301730 }
17311731
17321732 rerunLiveness ();
0 commit comments