@@ -239,17 +239,14 @@ static void processSimpleOp(Operation *op, RunLivenessAnalysis &la,
239239
240240// / Process a function-like operation `funcOp` using the liveness analysis `la`
241241// / and the IR in `module`. If it is not public or external:
242- // / Process a function-like op `funcOp`, given the liveness information in `la`
243- // / and the IR in `module`. Here, processing means:
244242// / (1) Adding its non-live arguments to a list for future removal.
245243// / (2) Marking their corresponding operands in its callers for removal.
246244// / (3) Identifying and enqueueing unnecessary terminator operands
247245// / (return values that are non-live across all callers) for removal.
248- // / (4) Enqueueing the non-live arguments themselves for removal.
246+ // / (4) Enqueueing the non-live arguments and return values for removal.
249247// / (5) Collecting the uses of these return values in its callers for future
250248// / removal.
251- // / 6. Marking all its results as non-live values.
252- // / iff it is not public or external.
249+ // / (6) Marking all its results as non-live values.
253250static void processFuncOp (FunctionOpInterface funcOp, Operation *module ,
254251 RunLivenessAnalysis &la, DenseSet<Value> &nonLiveSet,
255252 RDVFinalCleanupList &cl) {
@@ -637,22 +634,22 @@ static void processRegionBranchOp(RegionBranchOpInterface regionBranchOp,
637634}
638635
639636// / Steps to process a `BranchOpInterface` operation:
640- // / 1. Iterate through each successor block of the operation.
641- // / 2. For each successor block, gather all operands from all successors
642- // / along with their associated liveness analysis data.
643- // / 3. Identify and collect the dead operands from the branch operation
644- // / as well as their corresponding arguments.
637+ // / Iterate through each successor block of the operation.
638+ // / (1) For each successor block, gather all operands from all successors.
639+ // / along with their associated liveness analysis data.
640+ // / (2) Fetch their associated liveness analysis data.
641+ // / (3) Identify and collect the dead operands from the branch operation
642+ // / as well as their corresponding arguments.
645643
646644static void processBranchOp (BranchOpInterface branchOp, RunLivenessAnalysis &la,
647645 DenseSet<Value> &nonLiveSet,
648646 RDVFinalCleanupList &cl) {
649647 unsigned numSuccessors = branchOp->getNumSuccessors ();
650648
651- // Do (1)
652649 for (unsigned succIdx = 0 ; succIdx < numSuccessors; ++succIdx) {
653650 Block *successorBlock = branchOp->getSuccessor (succIdx);
654651
655- // Do (2 )
652+ // Do (1 )
656653 SuccessorOperands successorOperands =
657654 branchOp.getSuccessorOperands (succIdx);
658655 SmallVector<Value> operandValues;
@@ -661,46 +658,50 @@ static void processBranchOp(BranchOpInterface branchOp, RunLivenessAnalysis &la,
661658 operandValues.push_back (successorOperands[operandIdx]);
662659 }
663660
664- // Do (3 )
661+ // Do (2 )
665662 BitVector successorNonLive =
666663 markLives (operandValues, nonLiveSet, la).flip ();
667664 collectNonLiveValues (nonLiveSet, successorBlock->getArguments (),
668665 successorNonLive);
666+
667+ // Do (3)
669668 cl.blocks .push_back ({successorBlock, successorNonLive});
670669 cl.successorOperands .push_back ({branchOp, succIdx, successorNonLive});
671670 }
672671}
673672
674- static void cleanUpDeadVals (RDVFinalCleanupList &RDVFinalCleanupList) {
673+ // / Removes dead values collected in RDVFinalCleanupList.
674+ // / To be run once when all dead values have been collected.
675+ static void cleanUpDeadVals (RDVFinalCleanupList &list) {
675676 // 1. Operations
676- for (auto &op : RDVFinalCleanupList .operations ) {
677+ for (auto &op : list .operations ) {
677678 op->dropAllUses ();
678679 op->erase ();
679680 }
680681
681682 // 2. Values
682- for (auto &v : RDVFinalCleanupList .values ) {
683+ for (auto &v : list .values ) {
683684 v.dropAllUses ();
684685 }
685686
686687 // 3. Functions
687- for (auto &f : RDVFinalCleanupList .functions ) {
688+ for (auto &f : list .functions ) {
688689 f.funcOp .eraseArguments (f.nonLiveArgs );
689690 f.funcOp .eraseResults (f.nonLiveRets );
690691 }
691692
692693 // 4. Operands
693- for (auto &o : RDVFinalCleanupList .operands ) {
694+ for (auto &o : list .operands ) {
694695 o.op ->eraseOperands (o.nonLive );
695696 }
696697
697698 // 5. Results
698- for (auto &r : RDVFinalCleanupList .results ) {
699+ for (auto &r : list .results ) {
699700 dropUsesAndEraseResults (r.op , r.nonLive );
700701 }
701702
702703 // 6. Blocks
703- for (auto &b : RDVFinalCleanupList .blocks ) {
704+ for (auto &b : list .blocks ) {
704705 // blocks that are accessed via multiple codepaths processed once
705706 if (b.b ->getNumArguments () != b.nonLiveArgs .size ())
706707 continue ;
@@ -713,7 +714,7 @@ static void cleanUpDeadVals(RDVFinalCleanupList &RDVFinalCleanupList) {
713714 }
714715
715716 // 7. Successor Operands
716- for (auto &op : RDVFinalCleanupList .successorOperands ) {
717+ for (auto &op : list .successorOperands ) {
717718 SuccessorOperands successorOperands =
718719 op.branch .getSuccessorOperands (op.successorIndex );
719720 // blocks that are accessed via multiple codepaths processed once
@@ -742,28 +743,28 @@ void RemoveDeadValues::runOnOperation() {
742743
743744 // Maintains a list of Ops, values, branches, etc., slated for cleanup at the
744745 // end of this pass.
745- RDVFinalCleanupList finalRDVFinalCleanupList ;
746+ RDVFinalCleanupList finalCleanupList ;
746747
747748 module ->walk ([&](Operation *op) {
748749 if (auto funcOp = dyn_cast<FunctionOpInterface>(op)) {
749- processFuncOp (funcOp, module , la, deadVals, finalRDVFinalCleanupList );
750+ processFuncOp (funcOp, module , la, deadVals, finalCleanupList );
750751 } else if (auto regionBranchOp = dyn_cast<RegionBranchOpInterface>(op)) {
751752 processRegionBranchOp (regionBranchOp, la, deadVals,
752- finalRDVFinalCleanupList );
753+ finalCleanupList );
753754 } else if (auto branchOp = dyn_cast<BranchOpInterface>(op)) {
754- processBranchOp (branchOp, la, deadVals, finalRDVFinalCleanupList );
755+ processBranchOp (branchOp, la, deadVals, finalCleanupList );
755756 } else if (op->hasTrait <::mlir::OpTrait::IsTerminator>()) {
756757 // Nothing to do here because this is a terminator op and it should be
757758 // honored with respect to its parent
758759 } else if (isa<CallOpInterface>(op)) {
759760 // Nothing to do because this op is associated with a function op and gets
760761 // cleaned when the latter is cleaned.
761762 } else {
762- processSimpleOp (op, la, deadVals, finalRDVFinalCleanupList );
763+ processSimpleOp (op, la, deadVals, finalCleanupList );
763764 }
764765 });
765766
766- cleanUpDeadVals (finalRDVFinalCleanupList );
767+ cleanUpDeadVals (finalCleanupList );
767768}
768769
769770std::unique_ptr<Pass> mlir::createRemoveDeadValuesPass () {
0 commit comments