Skip to content

[CostModel] Add -cost-kind=all costmodel output #130490

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 2 commits into from
Mar 11, 2025
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
77 changes: 58 additions & 19 deletions llvm/lib/Analysis/CostModel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,17 +28,25 @@

using namespace llvm;

static cl::opt<TargetTransformInfo::TargetCostKind> CostKind(
enum class OutputCostKind {
RecipThroughput,
Latency,
CodeSize,
SizeAndLatency,
All,
};

static cl::opt<OutputCostKind> CostKind(
"cost-kind", cl::desc("Target cost kind"),
cl::init(TargetTransformInfo::TCK_RecipThroughput),
cl::values(clEnumValN(TargetTransformInfo::TCK_RecipThroughput,
"throughput", "Reciprocal throughput"),
clEnumValN(TargetTransformInfo::TCK_Latency,
"latency", "Instruction latency"),
clEnumValN(TargetTransformInfo::TCK_CodeSize,
"code-size", "Code size"),
clEnumValN(TargetTransformInfo::TCK_SizeAndLatency,
"size-latency", "Code size and latency")));
cl::init(OutputCostKind::RecipThroughput),
cl::values(clEnumValN(OutputCostKind::RecipThroughput, "throughput",
"Reciprocal throughput"),
clEnumValN(OutputCostKind::Latency, "latency",
"Instruction latency"),
clEnumValN(OutputCostKind::CodeSize, "code-size", "Code size"),
clEnumValN(OutputCostKind::SizeAndLatency, "size-latency",
"Code size and latency"),
clEnumValN(OutputCostKind::All, "all", "Print all cost kinds")));

enum class IntrinsicCostStrategy {
InstructionCost,
Expand Down Expand Up @@ -79,22 +87,53 @@ static InstructionCost getCost(Instruction &Inst, TTI::TargetCostKind CostKind,
return TTI.getInstructionCost(&Inst, CostKind);
}

static TTI::TargetCostKind
OutputCostKindToTargetCostKind(OutputCostKind CostKind) {
switch (CostKind) {
case OutputCostKind::RecipThroughput:
return TTI::TCK_RecipThroughput;
case OutputCostKind::Latency:
return TTI::TCK_Latency;
case OutputCostKind::CodeSize:
return TTI::TCK_CodeSize;
case OutputCostKind::SizeAndLatency:
return TTI::TCK_SizeAndLatency;
default:
llvm_unreachable("Unexpected OutputCostKind!");
};
}

PreservedAnalyses CostModelPrinterPass::run(Function &F,
FunctionAnalysisManager &AM) {
auto &TTI = AM.getResult<TargetIRAnalysis>(F);
auto &TLI = AM.getResult<TargetLibraryAnalysis>(F);
OS << "Printing analysis 'Cost Model Analysis' for function '" << F.getName() << "':\n";
for (BasicBlock &B : F) {
for (Instruction &Inst : B) {
// TODO: Use a pass parameter instead of cl::opt CostKind to determine
// which cost kind to print.
InstructionCost Cost = getCost(Inst, CostKind, TTI, TLI);
if (auto CostVal = Cost.getValue())
OS << "Cost Model: Found an estimated cost of " << *CostVal;
else
OS << "Cost Model: Invalid cost";

OS << " for instruction: " << Inst << "\n";
OS << "Cost Model: ";
if (CostKind == OutputCostKind::All) {
OS << "Found costs of ";
InstructionCost RThru =
getCost(Inst, TTI::TCK_RecipThroughput, TTI, TLI);
InstructionCost CodeSize = getCost(Inst, TTI::TCK_CodeSize, TTI, TLI);
InstructionCost Lat = getCost(Inst, TTI::TCK_Latency, TTI, TLI);
InstructionCost SizeLat =
getCost(Inst, TTI::TCK_SizeAndLatency, TTI, TLI);
if (RThru == CodeSize && RThru == Lat && RThru == SizeLat)
OS << RThru;
else
OS << "RThru:" << RThru << " CodeSize:" << CodeSize << " Lat:" << Lat
<< " SizeLat:" << SizeLat;
OS << " for: " << Inst << "\n";
} else {
InstructionCost Cost =
getCost(Inst, OutputCostKindToTargetCostKind(CostKind), TTI, TLI);
if (auto CostVal = Cost.getValue())
OS << "Found an estimated cost of " << *CostVal;
else
OS << "Invalid cost";
OS << " for instruction: " << Inst << "\n";
}
}
}
return PreservedAnalyses::all();
Expand Down
Loading
Loading