Skip to content

Commit d25e4a2

Browse files
committed
[llvm-mca] Add command line option -call-latency
Currently we assume a constant latency of 100 cycles for a call instruction. This commit allows the user to specify a custom value for the same as a command line argument. Default latency is set to 100.
1 parent dfdf1c5 commit d25e4a2

File tree

3 files changed

+18
-8
lines changed

3 files changed

+18
-8
lines changed

llvm/include/llvm/MCA/InstrBuilder.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ class InstrBuilder {
7878

7979
bool FirstCallInst;
8080
bool FirstReturnInst;
81+
unsigned CallLatency;
8182

8283
using InstRecycleCallback = std::function<Instruction *(const InstrDesc &)>;
8384
InstRecycleCallback InstRecycleCB;
@@ -111,6 +112,8 @@ class InstrBuilder {
111112
/// or null if there isn't any.
112113
void setInstRecycleCallback(InstRecycleCallback CB) { InstRecycleCB = CB; }
113114

115+
void setCallLatency(unsigned CL) { CallLatency = CL; }
116+
114117
Expected<std::unique_ptr<Instruction>>
115118
createInstruction(const MCInst &MCI, const SmallVector<Instrument *> &IVec);
116119
};

llvm/lib/MCA/InstrBuilder.cpp

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ InstrBuilder::InstrBuilder(const llvm::MCSubtargetInfo &sti,
3333
const llvm::MCInstrAnalysis *mcia,
3434
const mca::InstrumentManager &im)
3535
: STI(sti), MCII(mcii), MRI(mri), MCIA(mcia), IM(im), FirstCallInst(true),
36-
FirstReturnInst(true) {
36+
FirstReturnInst(true), CallLatency(100U) {
3737
const MCSchedModel &SM = STI.getSchedModel();
3838
ProcResourceMasks.resize(SM.getNumProcResourceKinds());
3939
computeProcResourceMasks(STI.getSchedModel(), ProcResourceMasks);
@@ -220,17 +220,18 @@ static void initializeUsedResources(InstrDesc &ID,
220220

221221
static void computeMaxLatency(InstrDesc &ID, const MCInstrDesc &MCDesc,
222222
const MCSchedClassDesc &SCDesc,
223-
const MCSubtargetInfo &STI) {
223+
const MCSubtargetInfo &STI,
224+
unsigned CallLatency) {
224225
if (MCDesc.isCall()) {
225226
// We cannot estimate how long this call will take.
226-
// Artificially set an arbitrarily high latency (100cy).
227-
ID.MaxLatency = 100U;
227+
// Artificially set an arbitrarily high latency (default: 100cy).
228+
ID.MaxLatency = CallLatency;
228229
return;
229230
}
230231

231232
int Latency = MCSchedModel::computeInstrLatency(STI, SCDesc);
232-
// If latency is unknown, then conservatively assume a MaxLatency of 100cy.
233-
ID.MaxLatency = Latency < 0 ? 100U : static_cast<unsigned>(Latency);
233+
// If latency is unknown, then conservatively assume a MaxLatency set for calls (default: 100cy).
234+
ID.MaxLatency = Latency < 0 ? CallLatency : static_cast<unsigned>(Latency);
234235
}
235236

236237
static Error verifyOperands(const MCInstrDesc &MCDesc, const MCInst &MCI) {
@@ -568,7 +569,7 @@ InstrBuilder::createInstrDescImpl(const MCInst &MCI,
568569
// We don't correctly model calls.
569570
WithColor::warning() << "found a call in the input assembly sequence.\n";
570571
WithColor::note() << "call instructions are not correctly modeled. "
571-
<< "Assume a latency of 100cy.\n";
572+
<< "Assume a latency of " << CallLatency << "cy.\n";
572573
FirstCallInst = false;
573574
}
574575

@@ -580,7 +581,7 @@ InstrBuilder::createInstrDescImpl(const MCInst &MCI,
580581
}
581582

582583
initializeUsedResources(*ID, SCDesc, STI, ProcResourceMasks);
583-
computeMaxLatency(*ID, MCDesc, SCDesc, STI);
584+
computeMaxLatency(*ID, MCDesc, SCDesc, STI, CallLatency);
584585

585586
if (Error Err = verifyOperands(MCDesc, MCI))
586587
return std::move(Err);

llvm/tools/llvm-mca/llvm-mca.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,11 @@ static cl::opt<unsigned>
135135
"(instructions per cycle)"),
136136
cl::cat(ToolOptions), cl::init(0));
137137

138+
static cl::opt<unsigned>
139+
CallLatency("call-latency", cl::Hidden,
140+
cl::desc("Number of cycles to assume for a call instruction"),
141+
cl::cat(ToolOptions), cl::init(100U));
142+
138143
enum class SkipType { NONE, LACK_SCHED, PARSE_FAILURE, ANY_FAILURE };
139144

140145
static cl::opt<enum SkipType> SkipUnsupportedInstructions(
@@ -569,6 +574,7 @@ int main(int argc, char **argv) {
569574

570575
// Create an instruction builder.
571576
mca::InstrBuilder IB(*STI, *MCII, *MRI, MCIA.get(), *IM);
577+
IB.setCallLatency(CallLatency);
572578

573579
// Create a context to control ownership of the pipeline hardware.
574580
mca::Context MCA(*MRI, *STI);

0 commit comments

Comments
 (0)