Skip to content

Commit

Permalink
[FuncSpec][NFC] Improve debug messages.
Browse files Browse the repository at this point in the history
Adds diagnostic messages when debugging the pass.

Differential Revision: https://reviews.llvm.org/D119875
  • Loading branch information
labrinea committed Mar 1, 2022
1 parent a6f3fed commit b803aee
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 23 deletions.
66 changes: 44 additions & 22 deletions llvm/lib/Transforms/IPO/FunctionSpecialization.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -302,6 +302,9 @@ class FunctionSpecializer {
continue;
}

LLVM_DEBUG(dbgs() << "FnSpecialization: Specialization cost for "
<< F->getName() << " is " << Cost << "\n");

auto ConstArgs = calculateGains(F, Cost);
if (ConstArgs.empty()) {
LLVM_DEBUG(dbgs() << "FnSpecialization: no possible constants found\n");
Expand Down Expand Up @@ -390,8 +393,6 @@ class FunctionSpecializer {
// argument can take on. If specialization is not profitable, we continue
// on to the next argument.
for (Argument &FormalArg : F->args()) {
LLVM_DEBUG(dbgs() << "FnSpecialization: Analysing arg: "
<< FormalArg.getName() << "\n");
// Determine if this argument is interesting. If we know the argument can
// take on any constant values, they are collected in Constants. If the
// argument can only ever equal a constant value in Constants, the
Expand All @@ -401,7 +402,9 @@ class FunctionSpecializer {
bool IsPartial = true;
SmallVector<Constant *> ActualArgs;
if (!isArgumentInteresting(&FormalArg, ActualArgs, IsPartial)) {
LLVM_DEBUG(dbgs() << "FnSpecialization: Argument is not interesting\n");
LLVM_DEBUG(dbgs() << "FnSpecialization: Argument "
<< FormalArg.getNameOrAsOperand()
<< " is not interesting\n");
continue;
}

Expand All @@ -427,10 +430,10 @@ class FunctionSpecializer {
// Truncate the worklist to 'MaxClonesThreshold' candidates if
// necessary.
if (Worklist.size() > MaxClonesThreshold) {
LLVM_DEBUG(dbgs() << "FnSpecialization: number of candidates exceed "
<< "the maximum number of clones threshold.\n"
<< "Truncating worklist to " << MaxClonesThreshold
<< " candidates.\n");
LLVM_DEBUG(dbgs() << "FnSpecialization: Number of candidates exceed "
<< "the maximum number of clones threshold.\n"
<< "FnSpecialization: Truncating worklist to "
<< MaxClonesThreshold << " candidates.\n");
Worklist.erase(Worklist.begin() + MaxClonesThreshold,
Worklist.end());
}
Expand All @@ -439,14 +442,16 @@ class FunctionSpecializer {
for (auto &ActualArg : Worklist)
ActualArg.Partial = true;

LLVM_DEBUG(dbgs() << "Sorted list of candidates by gain:\n";
for (auto &C
: Worklist) {
dbgs() << "- Function = " << C.Fn->getName() << ", ";
dbgs() << "FormalArg = " << C.Formal->getName() << ", ";
dbgs() << "ActualArg = " << C.Actual->getName() << ", ";
dbgs() << "Gain = " << C.Gain << "\n";
});
LLVM_DEBUG(
dbgs() << "FnSpecialization: Specializations for function "
<< F->getName() << "\n";
for (auto &C : Worklist) {
dbgs() << "FnSpecialization: FormalArg = "
<< C.Formal->getNameOrAsOperand() << ", ActualArg = "
<< C.Actual->getNameOrAsOperand() << ", Gain = "
<< C.Gain << "\n";
}
);

// FIXME: Only one argument per function.
break;
Expand Down Expand Up @@ -556,13 +561,13 @@ class FunctionSpecializer {
DominatorTree DT(*F);
LoopInfo LI(DT);
auto &TTI = (GetTTI)(*F);
LLVM_DEBUG(dbgs() << "FnSpecialization: Analysing bonus for: " << *A
<< "\n");
LLVM_DEBUG(dbgs() << "FnSpecialization: Analysing bonus for constant: "
<< C->getNameOrAsOperand() << "\n");

InstructionCost TotalCost = 0;
for (auto *U : A->users()) {
TotalCost += getUserBonus(U, TTI, LI);
LLVM_DEBUG(dbgs() << "FnSpecialization: User cost ";
LLVM_DEBUG(dbgs() << "FnSpecialization: User cost ";
TotalCost.print(dbgs()); dbgs() << " for: " << *U << "\n");
}

Expand Down Expand Up @@ -620,6 +625,9 @@ class FunctionSpecializer {
Bonus += Params.DefaultThreshold;
else if (IC.isVariable() && IC.getCostDelta() > 0)
Bonus += IC.getCostDelta();

LLVM_DEBUG(dbgs() << "FnSpecialization: Inlining bonus " << Bonus
<< " for user " << *U << "\n");
}

return TotalCost + Bonus;
Expand Down Expand Up @@ -649,8 +657,9 @@ class FunctionSpecializer {
// If the argument isn't overdefined, there's nothing to do. It should
// already be constant.
if (!Solver.getLatticeValueFor(A).isOverdefined()) {
LLVM_DEBUG(dbgs() << "FnSpecialization: nothing to do, arg is already "
<< "constant?\n");
LLVM_DEBUG(dbgs() << "FnSpecialization: Nothing to do, argument "
<< A->getNameOrAsOperand()
<< " is already constant?\n");
return false;
}

Expand All @@ -668,7 +677,8 @@ class FunctionSpecializer {
// TODO 2: this currently does not support constants, i.e. integer ranges.
//
IsPartial = !getPossibleConstants(A, Constants);
LLVM_DEBUG(dbgs() << "FnSpecialization: interesting arg: " << *A << "\n");
LLVM_DEBUG(dbgs() << "FnSpecialization: Found interesting argument "
<< A->getNameOrAsOperand() << "\n");
return true;
}

Expand Down Expand Up @@ -753,7 +763,15 @@ class FunctionSpecializer {
continue;
CallSitesToRewrite.push_back(&CS);
}

LLVM_DEBUG(dbgs() << "FnSpecialization: Replacing call sites of "
<< F->getName() << " with "
<< Clone->getName() << "\n");

for (auto *CS : CallSitesToRewrite) {
LLVM_DEBUG(dbgs() << "FnSpecialization: "
<< CS->getFunction()->getName() << " ->"
<< *CS << "\n");
if ((CS->getFunction() == Clone && CS->getArgOperand(ArgNo) == &Arg) ||
CS->getArgOperand(ArgNo) == C) {
CS->setCalledFunction(Clone);
Expand All @@ -779,7 +797,7 @@ class FunctionSpecializer {
for (Argument &Arg : F->args())
if (!Arg.use_empty() && tryToReplaceWithConstant(&Arg))
LLVM_DEBUG(dbgs() << "FnSpecialization: Replaced constant argument: "
<< Arg.getName() << "\n");
<< Arg.getNameOrAsOperand() << "\n");
}
}
};
Expand Down Expand Up @@ -890,6 +908,7 @@ bool llvm::runFunctionSpecialization(
unsigned I = 0;
while (FuncSpecializationMaxIters != I++ &&
FS.specializeFunctions(FuncDecls, WorkList)) {
LLVM_DEBUG(dbgs() << "FnSpecialization: Finished iteration " << I << "\n");

// Run the solver for the specialized functions.
RunSCCPSolver(WorkList);
Expand All @@ -901,6 +920,9 @@ bool llvm::runFunctionSpecialization(
Changed = true;
}

LLVM_DEBUG(dbgs() << "FnSpecialization: Number of specializations = "
<< NumFuncSpecialized <<"\n");

// Clean up the IR by removing dead instructions and ssa_copy intrinsics.
FS.removeDeadInstructions();
removeSSACopy(M);
Expand Down
7 changes: 6 additions & 1 deletion llvm/lib/Transforms/Utils/SCCPSolver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -538,7 +538,11 @@ void SCCPInstVisitor::markArgInFuncSpecialization(Function *F, Argument *A,
// over from the old function.
for (Argument *OldArg = F->arg_begin(), *NewArg = A->getParent()->arg_begin(),
*End = F->arg_end();
OldArg != End; ++OldArg, ++NewArg)
OldArg != End; ++OldArg, ++NewArg) {

LLVM_DEBUG(dbgs() << "SCCP: Marking argument "
<< NewArg->getNameOrAsOperand() << "\n");

if (NewArg != A && ValueState.count(OldArg)) {
// Note: This previously looked like this:
// ValueState[NewArg] = ValueState[OldArg];
Expand All @@ -549,6 +553,7 @@ void SCCPInstVisitor::markArgInFuncSpecialization(Function *F, Argument *A,
NewValue = ValueState[OldArg];
pushToWorkList(NewValue, NewArg);
}
}
}

void SCCPInstVisitor::visitInstruction(Instruction &I) {
Expand Down

0 comments on commit b803aee

Please sign in to comment.