Repository: llvm/llvm-project
File: llvm/lib/Target/AMDGPU/AMDGPUIGroupLP.cpp (C++)

Code before:
      return SG.getSGID() == CandSGID;
    });
    assert(Match);

    LLVM_DEBUG(dbgs() << "Trying SGID # " << CandSGID << " with Mask "
                      << (int)Match->getMask() << "\n");

    if (Match->isFull()) {
      LLVM_DEBUG(dbgs() << "SGID # " << CandSGID << " is full\n");
      continue;
    }
    if (!Match->allowedByRules(CurrSU.first, SyncPipeline)) {
      LLVM_DEBUG(dbgs() << "SGID # " << CandSGID << " has conflicting rule\n");
      continue;
    }

    std::list<std::pair<SUnit *, SUnit *>> TempEdges;
    TempCost = addEdges(SyncPipeline, CurrSU.first, CandSGID, TempEdges);
    LLVM_DEBUG(dbgs() << "Cost of Group " << TempCost << "\n");

    if (TempCost < BestNodeCost || BestNodeCost == -1) {
      BestEdges = TempEdges;
      BestGroup = Match;
      BestNodeCost = TempCost;
      BestGroupID = CandSGID;

      if (BestNodeCost == 0)
        break;

      removeEdges(BestEdges);
    }

    removeEdges(TempEdges);
  }

  if (BestGroupID != -1) {
    BestGroup->add(*CurrSU.first);
    if (AddedEdges.empty())
      AddedEdges = BestEdges;
    else
      AddedEdges.splice(std::prev(AddedEdges.cend()), BestEdges);
    std::for_each(BestEdges.begin(), BestEdges.end(),
                  [this](std::pair<SUnit *, SUnit *> E) {
                    if (!tryAddEdge(DAG, E.first, E.second))
                      llvm_unreachable("Edges known to be insertable.");
                  });

    LLVM_DEBUG(dbgs() << "Best Group has ID: " << BestGroupID << " and Mask"
                      << (int)BestGroup->getMask() << "\n");
    BestCost += TempCost;
  } else

Review comment (suggestion):
In the case that the current solution is the current best, this call seems redundant with the other removeEdges call.

Code after:
                      << (int)Match->getMask() << "\n");

    if (Match->isFull()) {
      LLVM_DEBUG(dbgs() << "SGID # " << CandSGID << " is full\n");
      continue;
    }
    if (!Match->allowedByRules(CurrSU.first, SyncPipeline)) {
      LLVM_DEBUG(dbgs() << "SGID # " << CandSGID << " has conflicting rule\n");
      continue;
    }

    std::list<std::pair<SUnit *, SUnit *>> TempEdges;
    TempCost = addEdges(SyncPipeline, CurrSU.first, CandSGID, TempEdges);
    LLVM_DEBUG(dbgs() << "Cost of Group " << TempCost << "\n");

    if (TempCost < BestNodeCost || BestNodeCost == -1) {
      BestEdges = TempEdges;
      BestGroup = Match;
      BestNodeCost = TempCost;
      BestGroupID = CandSGID;

      if (BestNodeCost == 0)
        break;
    }

    removeEdges(TempEdges);
  }

  if (BestGroupID != -1) {
    BestGroup->add(*CurrSU.first);
    if (AddedEdges.empty())
      AddedEdges = BestEdges;
    else
      AddedEdges.splice(std::prev(AddedEdges.cend()), BestEdges);

    for (const std::pair<SUnit *, SUnit *> &E : BestEdges) {
      if (!BestGroup->tryAddEdge(E.first, E.second))
        llvm_unreachable("Edges known to be insertable.");
    }

    LLVM_DEBUG(dbgs() << "Best Group has ID: " << BestGroupID << " and Mask"
                      << (int)BestGroup->getMask() << "\n");
    BestCost += TempCost;
  } else
    BestCost += MissPenalty;

  CurrPipeline[CurrSyncGroupIdx] = SyncPipeline;
}

bool PipelineSolver::solveGreedy() {
  BestCost = 0;