Skip to content

[CallGraphUpdater] Remove some legacy pass manager support #98362

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 0 additions & 8 deletions llvm/include/llvm/Transforms/IPO/Attributor.h
Original file line number Diff line number Diff line change
Expand Up @@ -1828,14 +1828,6 @@ struct Attributor {
Configuration.InitializationCallback(*this, F);
}

/// Helper function to remove callsite.
void removeCallSite(CallInst *CI) {
if (!CI)
return;

Configuration.CGUpdater.removeCallSite(*CI);
}

/// Record that \p U is to be replaces with \p NV after information was
/// manifested. This also triggers deletion of trivially dead istructions.
bool changeUseAfterManifest(Use &U, Value &NV) {
Expand Down
18 changes: 0 additions & 18 deletions llvm/include/llvm/Transforms/Utils/CallGraphUpdater.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,6 @@ class CallGraphUpdater {
SmallVector<Function *, 16> DeadFunctionsInComdats;
///}

/// Old PM variables
///{
CallGraph *CG = nullptr;
CallGraphSCC *CGSCC = nullptr;
///}

/// New PM variables
///{
LazyCallGraph *LCG = nullptr;
Expand All @@ -60,10 +54,6 @@ class CallGraphUpdater {
/// Initializers for usage outside of a CGSCC pass, inside a CGSCC pass in
/// the old and new pass manager (PM).
///{
void initialize(CallGraph &CG, CallGraphSCC &SCC) {
this->CG = &CG;
this->CGSCC = &SCC;
}
void initialize(LazyCallGraph &LCG, LazyCallGraph::SCC &SCC,
CGSCCAnalysisManager &AM, CGSCCUpdateResult &UR) {
this->LCG = &LCG;
Expand Down Expand Up @@ -95,14 +85,6 @@ class CallGraphUpdater {
/// Note that \p OldFn is also removed from the call graph
/// (\see removeFunction).
void replaceFunctionWith(Function &OldFn, Function &NewFn);

/// Remove the call site \p CS from the call graph.
void removeCallSite(CallBase &CS);

/// Replace \p OldCS with the new call site \p NewCS.
/// \return True if the replacement was successful, otherwise False. In the
/// latter case the parent function of \p OldCB needs to be re-analyzed.
bool replaceCallSite(CallBase &OldCS, CallBase &NewCS);
};

} // end namespace llvm
Expand Down
3 changes: 0 additions & 3 deletions llvm/lib/Transforms/IPO/Attributor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2557,8 +2557,6 @@ ChangeStatus Attributor::cleanupIR() {
if (auto *CB = dyn_cast<CallBase>(I)) {
assert((isa<IntrinsicInst>(CB) || isRunOn(*I->getFunction())) &&
"Cannot delete an instruction outside the current SCC!");
if (!isa<IntrinsicInst>(CB))
Configuration.CGUpdater.removeCallSite(*CB);
}
I->dropDroppableUses();
CGModifiedFunctions.insert(I->getFunction());
Expand Down Expand Up @@ -3186,7 +3184,6 @@ ChangeStatus Attributor::rewriteFunctionSignatures(
assert(OldCB.getType() == NewCB.getType() &&
"Cannot handle call sites with different types!");
ModifiedFns.insert(OldCB.getFunction());
Configuration.CGUpdater.replaceCallSite(OldCB, NewCB);
OldCB.replaceAllUsesWith(&NewCB);
OldCB.eraseFromParent();
}
Expand Down
2 changes: 0 additions & 2 deletions llvm/lib/Transforms/IPO/OpenMPOpt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1453,7 +1453,6 @@ struct OpenMPOpt {
};
emitRemark<OptimizationRemark>(CI, "OMP160", Remark);

CGUpdater.removeCallSite(*CI);
CI->eraseFromParent();
Changed = true;
++NumOpenMPParallelRegionsDeleted;
Expand Down Expand Up @@ -1895,7 +1894,6 @@ struct OpenMPOpt {
else
emitRemark<OptimizationRemark>(&F, "OMP170", Remark);

CGUpdater.removeCallSite(*CI);
CI->replaceAllUsesWith(ReplVal);
CI->eraseFromParent();
++NumOpenMPRuntimeCallsDeduplicated;
Expand Down
131 changes: 30 additions & 101 deletions llvm/lib/Transforms/Utils/CallGraphUpdater.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,6 @@
//===----------------------------------------------------------------------===//

#include "llvm/Transforms/Utils/CallGraphUpdater.h"
#include "llvm/ADT/STLExtras.h"
#include "llvm/Analysis/CallGraph.h"
#include "llvm/Analysis/CallGraphSCCPass.h"
#include "llvm/IR/Constants.h"
#include "llvm/Transforms/Utils/ModuleUtils.h"

Expand All @@ -28,53 +25,33 @@ bool CallGraphUpdater::finalize() {
DeadFunctionsInComdats.end());
}

if (CG) {
// First remove all references, e.g., outgoing via called functions. This is
// necessary as we can delete functions that have circular references.
for (Function *DeadFn : DeadFunctions) {
DeadFn->removeDeadConstantUsers();
CallGraphNode *DeadCGN = (*CG)[DeadFn];
DeadCGN->removeAllCalledFunctions();
CG->getExternalCallingNode()->removeAnyCallEdgeTo(DeadCGN);
DeadFn->replaceAllUsesWith(PoisonValue::get(DeadFn->getType()));
}

// Then remove the node and function from the module.
for (Function *DeadFn : DeadFunctions) {
CallGraphNode *DeadCGN = CG->getOrInsertFunction(DeadFn);
assert(DeadCGN->getNumReferences() == 0 &&
"References should have been handled by now");
delete CG->removeFunctionFromModule(DeadCGN);
}
} else {
// This is the code path for the new lazy call graph and for the case were
// no call graph was provided.
for (Function *DeadFn : DeadFunctions) {
DeadFn->removeDeadConstantUsers();
DeadFn->replaceAllUsesWith(PoisonValue::get(DeadFn->getType()));

if (LCG && !ReplacedFunctions.count(DeadFn)) {
// Taken mostly from the inliner:
LazyCallGraph::Node &N = LCG->get(*DeadFn);
auto *DeadSCC = LCG->lookupSCC(N);
assert(DeadSCC && DeadSCC->size() == 1 &&
&DeadSCC->begin()->getFunction() == DeadFn);

FAM->clear(*DeadFn, DeadFn->getName());
AM->clear(*DeadSCC, DeadSCC->getName());
LCG->markDeadFunction(*DeadFn);

// Mark the relevant parts of the call graph as invalid so we don't
// visit them.
UR->InvalidatedSCCs.insert(LCG->lookupSCC(N));
UR->DeadFunctions.push_back(DeadFn);
} else {
// The CGSCC infrastructure batch deletes functions at the end of the
// call graph walk, so only erase the function if we're not using that
// infrastructure.
// The function is now really dead and de-attached from everything.
DeadFn->eraseFromParent();
}
// This is the code path for the new lazy call graph and for the case were
// no call graph was provided.
for (Function *DeadFn : DeadFunctions) {
DeadFn->removeDeadConstantUsers();
DeadFn->replaceAllUsesWith(PoisonValue::get(DeadFn->getType()));

if (LCG && !ReplacedFunctions.count(DeadFn)) {
// Taken mostly from the inliner:
LazyCallGraph::Node &N = LCG->get(*DeadFn);
auto *DeadSCC = LCG->lookupSCC(N);
assert(DeadSCC && DeadSCC->size() == 1 &&
&DeadSCC->begin()->getFunction() == DeadFn);

FAM->clear(*DeadFn, DeadFn->getName());
AM->clear(*DeadSCC, DeadSCC->getName());
LCG->markDeadFunction(*DeadFn);

// Mark the relevant parts of the call graph as invalid so we don't
// visit them.
UR->InvalidatedSCCs.insert(LCG->lookupSCC(N));
UR->DeadFunctions.push_back(DeadFn);
} else {
// The CGSCC infrastructure batch deletes functions at the end of the
// call graph walk, so only erase the function if we're not using that
// infrastructure.
// The function is now really dead and de-attached from everything.
DeadFn->eraseFromParent();
}
}

Expand All @@ -85,11 +62,7 @@ bool CallGraphUpdater::finalize() {
}

void CallGraphUpdater::reanalyzeFunction(Function &Fn) {
if (CG) {
CallGraphNode *OldCGN = CG->getOrInsertFunction(&Fn);
OldCGN->removeAllCalledFunctions();
CG->populateCallGraphNode(OldCGN);
} else if (LCG) {
if (LCG) {
LazyCallGraph::Node &N = LCG->get(Fn);
LazyCallGraph::SCC *C = LCG->lookupSCC(N);
updateCGAndAnalysisManagerForCGSCCPass(*LCG, *C, N, *AM, *UR, *FAM);
Expand All @@ -98,9 +71,7 @@ void CallGraphUpdater::reanalyzeFunction(Function &Fn) {

void CallGraphUpdater::registerOutlinedFunction(Function &OriginalFn,
Function &NewFn) {
if (CG)
CG->addToCallGraph(&NewFn);
else if (LCG)
if (LCG)
LCG->addSplitFunction(OriginalFn, NewFn);
}

Expand All @@ -112,59 +83,17 @@ void CallGraphUpdater::removeFunction(Function &DeadFn) {
else
DeadFunctions.push_back(&DeadFn);

// For the old call graph we remove the function from the SCC right away.
if (CG && !ReplacedFunctions.count(&DeadFn)) {
CallGraphNode *DeadCGN = (*CG)[&DeadFn];
DeadCGN->removeAllCalledFunctions();
CGSCC->DeleteNode(DeadCGN);
}
if (FAM)
FAM->clear(DeadFn, DeadFn.getName());
}

void CallGraphUpdater::replaceFunctionWith(Function &OldFn, Function &NewFn) {
OldFn.removeDeadConstantUsers();
ReplacedFunctions.insert(&OldFn);
if (CG) {
// Update the call graph for the newly promoted function.
CallGraphNode *OldCGN = (*CG)[&OldFn];
CallGraphNode *NewCGN = CG->getOrInsertFunction(&NewFn);
NewCGN->stealCalledFunctionsFrom(OldCGN);
CG->ReplaceExternalCallEdge(OldCGN, NewCGN);

// And update the SCC we're iterating as well.
CGSCC->ReplaceNode(OldCGN, NewCGN);
} else if (LCG) {
if (LCG) {
// Directly substitute the functions in the call graph.
LazyCallGraph::Node &OldLCGN = LCG->get(OldFn);
SCC->getOuterRefSCC().replaceNodeFunction(OldLCGN, NewFn);
}
removeFunction(OldFn);
}

bool CallGraphUpdater::replaceCallSite(CallBase &OldCS, CallBase &NewCS) {
// This is only necessary in the (old) CG.
if (!CG)
return true;

Function *Caller = OldCS.getCaller();
CallGraphNode *NewCalleeNode =
CG->getOrInsertFunction(NewCS.getCalledFunction());
CallGraphNode *CallerNode = (*CG)[Caller];
if (llvm::none_of(*CallerNode, [&OldCS](const CallGraphNode::CallRecord &CR) {
return CR.first && *CR.first == &OldCS;
}))
return false;
CallerNode->replaceCallEdge(OldCS, NewCS, NewCalleeNode);
return true;
}

void CallGraphUpdater::removeCallSite(CallBase &CS) {
// This is only necessary in the (old) CG.
if (!CG)
return;

Function *Caller = CS.getCaller();
CallGraphNode *CallerNode = (*CG)[Caller];
CallerNode->removeCallEdgeFor(CS);
}
Loading
Loading