Skip to content

Commit 4058cec

Browse files
committed
[TargetInstrInfo] Add target hook for InstrSchedModel latency. [NFCI]
Allows targets to dynamically set latency values in the DAG builder. This is useful in multi-pass schedulers like in the AMDGUP backend where we may want to schedule a region multiple times with a different machine model or tweaked latencies for a specific instruction type.
1 parent ee5bc57 commit 4058cec

File tree

4 files changed

+39
-8
lines changed

4 files changed

+39
-8
lines changed

llvm/include/llvm/CodeGen/TargetInstrInfo.h

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1849,13 +1849,26 @@ class LLVM_ABI TargetInstrInfo : public MCInstrInfo {
18491849
const MachineInstr &DefMI, unsigned DefIdx,
18501850
const MachineInstr &UseMI, unsigned UseIdx) const;
18511851

1852+
/// Compute the latency of a register data dependence (DefIdx -> UseIdx)
1853+
/// using the InstrSchedModel.
1854+
virtual unsigned getOperandLatency(const TargetSchedModel &SchedModel,
1855+
const MachineInstr *DefMI,
1856+
unsigned DefIdx,
1857+
const MachineInstr *UseMI,
1858+
unsigned UseIdx) const;
1859+
18521860
/// Compute the instruction latency of a given instruction.
18531861
/// If the instruction has higher cost when predicated, it's returned via
18541862
/// PredCost.
18551863
virtual unsigned getInstrLatency(const InstrItineraryData *ItinData,
18561864
const MachineInstr &MI,
18571865
unsigned *PredCost = nullptr) const;
18581866

1867+
/// Compute the instruction latency using the InstrSchedModel or the default
1868+
/// def latency when no model is available.
1869+
virtual unsigned getInstrLatency(const TargetSchedModel &TargetSchedModel,
1870+
const MachineInstr &MI) const;
1871+
18591872
virtual unsigned getPredicationCost(const MachineInstr &MI) const;
18601873

18611874
virtual unsigned getInstrLatency(const InstrItineraryData *ItinData,

llvm/include/llvm/CodeGen/TargetSchedule.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,6 @@ class TargetSchedModel {
4444
// Resource units per cycle. Latency normalization factor.
4545
unsigned ResourceLCM = 0;
4646

47-
unsigned computeInstrLatency(const MCSchedClassDesc &SCDesc) const;
48-
4947
// EnableSchedModel and EnableSchedItins are used to control whether or not to
5048
// use the Target's {SchedMachineModel, InstrItins} for hardware infor based
5149
// Scheduling decisions. If both are enabled, as is the default, preference
@@ -203,6 +201,7 @@ class TargetSchedModel {
203201
bool UseDefaultDefLatency = true) const;
204202
LLVM_ABI unsigned computeInstrLatency(const MCInst &Inst) const;
205203
LLVM_ABI unsigned computeInstrLatency(unsigned Opcode) const;
204+
LLVM_ABI unsigned computeInstrLatency(const MCSchedClassDesc &SCDesc) const;
206205

207206
/// Output dependency latency of a pair of defs of the same register.
208207
///

llvm/lib/CodeGen/TargetInstrInfo.cpp

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1831,6 +1831,28 @@ unsigned TargetInstrInfo::getInstrLatency(const InstrItineraryData *ItinData,
18311831
return ItinData->getStageLatency(MI.getDesc().getSchedClass());
18321832
}
18331833

1834+
unsigned
1835+
TargetInstrInfo::getInstrLatency(const TargetSchedModel &TargetSchedModel,
1836+
const MachineInstr &MI) const {
1837+
if (TargetSchedModel.hasInstrSchedModel()) {
1838+
const MCSchedClassDesc *SCDesc = TargetSchedModel.resolveSchedClass(&MI);
1839+
if (SCDesc->isValid())
1840+
return TargetSchedModel.computeInstrLatency(*SCDesc);
1841+
}
1842+
1843+
return defaultDefLatency(*TargetSchedModel.getMCSchedModel(), MI);
1844+
}
1845+
1846+
unsigned TargetInstrInfo::getOperandLatency(const TargetSchedModel &SchedModel,
1847+
const MachineInstr *DefMI,
1848+
unsigned DefOperIdx,
1849+
const MachineInstr *UseMI,
1850+
unsigned UseOperIdx) const {
1851+
// Keep the default hook minimal: delegate to the scheduling model so
1852+
// targets can override either side as needed.
1853+
return SchedModel.computeOperandLatency(DefMI, DefOperIdx, UseMI, UseOperIdx);
1854+
}
1855+
18341856
bool TargetInstrInfo::hasLowDefLatency(const TargetSchedModel &SchedModel,
18351857
const MachineInstr &DefMI,
18361858
unsigned DefIdx) const {

llvm/lib/CodeGen/TargetSchedule.cpp

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -259,12 +259,9 @@ TargetSchedModel::computeInstrLatency(const MachineInstr *MI,
259259
(!hasInstrSchedModel() && !UseDefaultDefLatency))
260260
return TII->getInstrLatency(&InstrItins, *MI);
261261

262-
if (hasInstrSchedModel()) {
263-
const MCSchedClassDesc *SCDesc = resolveSchedClass(MI);
264-
if (SCDesc->isValid())
265-
return computeInstrLatency(*SCDesc);
266-
}
267-
return TII->defaultDefLatency(SchedModel, *MI);
262+
// This is used by targets that define an InstrSchedModel or want to use the
263+
// default def latency.
264+
return TII->getInstrLatency(*this, *MI);
268265
}
269266

270267
unsigned TargetSchedModel::

0 commit comments

Comments
 (0)