1
0
mirror of https://github.com/RPCS3/llvm-mirror.git synced 2024-11-25 20:23:11 +01:00

[InlineCost] Preparational patch for creation of Printer pass.

- Renaming the printer class, flag
- Refactoring
- Changing some tests

This patch is a preparational stage for introducing a new printing pass and new
functionality to the existing Annotation Writer. I plan to extend
this functionality for this tool to be more useful when looking at the inline
process.
This commit is contained in:
Kirill Naumov 2020-06-11 20:24:14 +00:00
parent 3ab1a8a75e
commit 57eaef3c0a
3 changed files with 45 additions and 33 deletions

View File

@ -54,9 +54,9 @@ static cl::opt<int>
cl::ZeroOrMore,
cl::desc("Default amount of inlining to perform"));
static cl::opt<bool> PrintDebugInstructionDeltas(
"print-instruction-deltas", cl::Hidden, cl::init(false),
cl::desc("Prints deltas of cost and threshold per instruction"));
static cl::opt<bool> PrintInstructionComments(
"print-instruction-comments", cl::Hidden, cl::init(false),
cl::desc("Prints comments for instruction based on inline cost analysis"));
static cl::opt<int> InlineThreshold(
"inline-threshold", cl::Hidden, cl::init(225), cl::ZeroOrMore,
@ -128,14 +128,14 @@ struct InstructionCostDetail {
bool hasThresholdChanged() const { return ThresholdAfter != ThresholdBefore; }
};
class CostAnnotationWriter : public AssemblyAnnotationWriter {
public:
// This DenseMap stores the delta change in cost and threshold after
// accounting for the given instruction.
DenseMap<const Instruction *, InstructionCostDetail> CostThresholdMap;
class InlineCostAnnotationWriter : public AssemblyAnnotationWriter {
private:
InlineCostCallAnalyzer *const ICCA;
public:
InlineCostAnnotationWriter(InlineCostCallAnalyzer *ICCA) : ICCA(ICCA) {}
virtual void emitInstructionAnnot(const Instruction *I,
formatted_raw_ostream &OS);
formatted_raw_ostream &OS) override;
};
/// Carry out call site analysis, in order to evaluate inlinability.
@ -429,6 +429,11 @@ class InlineCostCallAnalyzer final : public CallAnalyzer {
/// Tunable parameters that control the analysis.
const InlineParams &Params;
// This DenseMap stores the delta change in cost and threshold after
// accounting for the given instruction. The map is filled only with the
// flag PrintInstructionComments on.
DenseMap<const Instruction *, InstructionCostDetail> InstructionCostDetailMap;
/// Upper bound for the inlining cost. Bonuses are being applied to account
/// for speculative "expected profit" of the inlining decision.
int Threshold = 0;
@ -598,19 +603,19 @@ class InlineCostCallAnalyzer final : public CallAnalyzer {
void onInstructionAnalysisStart(const Instruction *I) override {
// This function is called to store the initial cost of inlining before
// the given instruction was assessed.
if (!PrintDebugInstructionDeltas)
if (!PrintInstructionComments)
return;
Writer.CostThresholdMap[I].CostBefore = Cost;
Writer.CostThresholdMap[I].ThresholdBefore = Threshold;
InstructionCostDetailMap[I].CostBefore = Cost;
InstructionCostDetailMap[I].ThresholdBefore = Threshold;
}
void onInstructionAnalysisFinish(const Instruction *I) override {
// This function is called to find new values of cost and threshold after
// the instruction has been assessed.
if (!PrintDebugInstructionDeltas)
if (!PrintInstructionComments)
return;
Writer.CostThresholdMap[I].CostAfter = Cost;
Writer.CostThresholdMap[I].ThresholdAfter = Threshold;
InstructionCostDetailMap[I].CostAfter = Cost;
InstructionCostDetailMap[I].ThresholdAfter = Threshold;
}
InlineResult finalizeAnalysis() override {
@ -713,13 +718,20 @@ public:
ComputeFullInlineCost(OptComputeFullInlineCost ||
Params.ComputeFullInlineCost || ORE),
Params(Params), Threshold(Params.DefaultThreshold),
BoostIndirectCalls(BoostIndirect), IgnoreThreshold(IgnoreThreshold) {}
BoostIndirectCalls(BoostIndirect), IgnoreThreshold(IgnoreThreshold),
Writer(this) {}
/// Annotation Writer for cost annotation
CostAnnotationWriter Writer;
/// Annotation Writer for instruction details
InlineCostAnnotationWriter Writer;
void dump();
Optional<InstructionCostDetail> getCostDetails(const Instruction *I) {
if (InstructionCostDetailMap.find(I) != InstructionCostDetailMap.end())
return InstructionCostDetailMap[I];
return None;
}
virtual ~InlineCostCallAnalyzer() {}
int getThreshold() { return Threshold; }
int getCost() { return Cost; }
@ -737,23 +749,23 @@ void CallAnalyzer::disableSROAForArg(AllocaInst *SROAArg) {
disableLoadElimination();
}
void CostAnnotationWriter::emitInstructionAnnot(const Instruction *I,
void InlineCostAnnotationWriter::emitInstructionAnnot(const Instruction *I,
formatted_raw_ostream &OS) {
// The cost of inlining of the given instruction is printed always.
// The threshold delta is printed only when it is non-zero. It happens
// when we decided to give a bonus at a particular instruction.
if (CostThresholdMap.count(I) == 0) {
OS << "; No analysis for the instruction\n";
return;
Optional<InstructionCostDetail> Record = ICCA->getCostDetails(I);
if (!Record)
OS << "; No analysis for the instruction";
else {
OS << "; cost before = " << Record->CostBefore
<< ", cost after = " << Record->CostAfter
<< ", threshold before = " << Record->ThresholdBefore
<< ", threshold after = " << Record->ThresholdAfter << ", ";
OS << "cost delta = " << Record->getCostDelta();
if (Record->hasThresholdChanged())
OS << ", threshold delta = " << Record->getThresholdDelta();
}
const auto &Record = CostThresholdMap[I];
OS << "; cost before = " << Record.CostBefore
<< ", cost after = " << Record.CostAfter
<< ", threshold before = " << Record.ThresholdBefore
<< ", threshold after = " << Record.ThresholdAfter << ", ";
OS << "cost delta = " << Record.getCostDelta();
if (Record.hasThresholdChanged())
OS << ", threshold delta = " << Record.getThresholdDelta();
OS << "\n";
}
@ -2159,7 +2171,7 @@ InlineResult CallAnalyzer::analyze() {
/// Dump stats about this call's analysis.
LLVM_DUMP_METHOD void InlineCostCallAnalyzer::dump() {
#define DEBUG_PRINT_STAT(x) dbgs() << " " #x ": " << x << "\n"
if (PrintDebugInstructionDeltas)
if (PrintInstructionComments)
F.print(dbgs(), &Writer);
DEBUG_PRINT_STAT(NumConstantArgs);
DEBUG_PRINT_STAT(NumConstantOffsetPtrArgs);

View File

@ -1,7 +1,7 @@
; Require asserts for -debug-only
; REQUIRES: asserts
; RUN: opt < %s -inline -debug-only=inline-cost -disable-output -print-instruction-deltas 2>&1 | FileCheck %s
; RUN: opt < %s -inline -debug-only=inline-cost -disable-output -print-instruction-comments 2>&1 | FileCheck %s
; CHECK: Analyzing call of callee1... (caller:foo)
; CHECK: define i32 @callee1(i32 %x) {

View File

@ -4,7 +4,7 @@
; This test ensures that the hadling of instructions which were not analyzed by
; '-print-instruction-deltas' flag due to the early exit was done correctly.
; RUN: opt < %s -inline -debug-only=inline-cost -disable-output -print-instruction-deltas -inline-threshold=0 2>&1 | FileCheck %s
; RUN: opt < %s -inline -debug-only=inline-cost -disable-output -print-instruction-comments -inline-threshold=0 2>&1 | FileCheck %s
; CHECK: No analysis for the instruction
; CHECK: ret void