@@ -742,7 +742,49 @@ static void processBranchOp(BranchOpInterface branchOp, RunLivenessAnalysis &la,
742742static void cleanUpDeadVals (RDVFinalCleanupList &list) {
743743 LDBG () << " Starting cleanup of dead values..." ;
744744
745- // 1. Operations
745+ // 1. Blocks, We must remove the block arguments and successor operands before
746+ // deleting the operation, as they may reside in the region operation.
747+ LDBG () << " Cleaning up " << list.blocks .size () << " block argument lists" ;
748+ for (auto &b : list.blocks ) {
749+ // blocks that are accessed via multiple codepaths processed once
750+ if (b.b ->getNumArguments () != b.nonLiveArgs .size ())
751+ continue ;
752+ LDBG () << " Erasing " << b.nonLiveArgs .count ()
753+ << " non-live arguments from block: " << b.b ;
754+ // it iterates backwards because erase invalidates all successor indexes
755+ for (int i = b.nonLiveArgs .size () - 1 ; i >= 0 ; --i) {
756+ if (!b.nonLiveArgs [i])
757+ continue ;
758+ LDBG () << " Erasing block argument " << i << " : " << b.b ->getArgument (i);
759+ b.b ->getArgument (i).dropAllUses ();
760+ b.b ->eraseArgument (i);
761+ }
762+ }
763+
764+ // 2. Successor Operands
765+ LDBG () << " Cleaning up " << list.successorOperands .size ()
766+ << " successor operand lists" ;
767+ for (auto &op : list.successorOperands ) {
768+ SuccessorOperands successorOperands =
769+ op.branch .getSuccessorOperands (op.successorIndex );
770+ // blocks that are accessed via multiple codepaths processed once
771+ if (successorOperands.size () != op.nonLiveOperands .size ())
772+ continue ;
773+ LDBG () << " Erasing " << op.nonLiveOperands .count ()
774+ << " non-live successor operands from successor "
775+ << op.successorIndex << " of branch: "
776+ << OpWithFlags (op.branch , OpPrintingFlags ().skipRegions ());
777+ // it iterates backwards because erase invalidates all successor indexes
778+ for (int i = successorOperands.size () - 1 ; i >= 0 ; --i) {
779+ if (!op.nonLiveOperands [i])
780+ continue ;
781+ LDBG () << " Erasing successor operand " << i << " : "
782+ << successorOperands[i];
783+ successorOperands.erase (i);
784+ }
785+ }
786+
787+ // 3. Operations
746788 LDBG () << " Cleaning up " << list.operations .size () << " operations" ;
747789 for (auto &op : list.operations ) {
748790 LDBG () << " Erasing operation: "
@@ -751,14 +793,14 @@ static void cleanUpDeadVals(RDVFinalCleanupList &list) {
751793 op->erase ();
752794 }
753795
754- // 2 . Values
796+ // 4 . Values
755797 LDBG () << " Cleaning up " << list.values .size () << " values" ;
756798 for (auto &v : list.values ) {
757799 LDBG () << " Dropping all uses of value: " << v;
758800 v.dropAllUses ();
759801 }
760802
761- // 3 . Functions
803+ // 5 . Functions
762804 LDBG () << " Cleaning up " << list.functions .size () << " functions" ;
763805 // Record which function arguments were erased so we can shrink call-site
764806 // argument segments for CallOpInterface operations (e.g. ops using
@@ -780,7 +822,7 @@ static void cleanUpDeadVals(RDVFinalCleanupList &list) {
780822 (void )f.funcOp .eraseResults (f.nonLiveRets );
781823 }
782824
783- // 4 . Operands
825+ // 6 . Operands
784826 LDBG () << " Cleaning up " << list.operands .size () << " operand lists" ;
785827 for (OperationToCleanup &o : list.operands ) {
786828 // Handle call-specific cleanup only when we have a cached callee reference.
@@ -822,56 +864,14 @@ static void cleanUpDeadVals(RDVFinalCleanupList &list) {
822864 }
823865 }
824866
825- // 5 . Results
867+ // 7 . Results
826868 LDBG () << " Cleaning up " << list.results .size () << " result lists" ;
827869 for (auto &r : list.results ) {
828870 LDBG () << " Erasing " << r.nonLive .count ()
829871 << " non-live results from operation: "
830872 << OpWithFlags (r.op , OpPrintingFlags ().skipRegions ());
831873 dropUsesAndEraseResults (r.op , r.nonLive );
832874 }
833-
834- // 6. Blocks
835- LDBG () << " Cleaning up " << list.blocks .size () << " block argument lists" ;
836- for (auto &b : list.blocks ) {
837- // blocks that are accessed via multiple codepaths processed once
838- if (b.b ->getNumArguments () != b.nonLiveArgs .size ())
839- continue ;
840- LDBG () << " Erasing " << b.nonLiveArgs .count ()
841- << " non-live arguments from block: " << b.b ;
842- // it iterates backwards because erase invalidates all successor indexes
843- for (int i = b.nonLiveArgs .size () - 1 ; i >= 0 ; --i) {
844- if (!b.nonLiveArgs [i])
845- continue ;
846- LDBG () << " Erasing block argument " << i << " : " << b.b ->getArgument (i);
847- b.b ->getArgument (i).dropAllUses ();
848- b.b ->eraseArgument (i);
849- }
850- }
851-
852- // 7. Successor Operands
853- LDBG () << " Cleaning up " << list.successorOperands .size ()
854- << " successor operand lists" ;
855- for (auto &op : list.successorOperands ) {
856- SuccessorOperands successorOperands =
857- op.branch .getSuccessorOperands (op.successorIndex );
858- // blocks that are accessed via multiple codepaths processed once
859- if (successorOperands.size () != op.nonLiveOperands .size ())
860- continue ;
861- LDBG () << " Erasing " << op.nonLiveOperands .count ()
862- << " non-live successor operands from successor "
863- << op.successorIndex << " of branch: "
864- << OpWithFlags (op.branch , OpPrintingFlags ().skipRegions ());
865- // it iterates backwards because erase invalidates all successor indexes
866- for (int i = successorOperands.size () - 1 ; i >= 0 ; --i) {
867- if (!op.nonLiveOperands [i])
868- continue ;
869- LDBG () << " Erasing successor operand " << i << " : "
870- << successorOperands[i];
871- successorOperands.erase (i);
872- }
873- }
874-
875875 LDBG () << " Finished cleanup of dead values" ;
876876}
877877
0 commit comments