Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-05-10 08:44:14

0001 //===-- llvm/MC/MCSchedule.h - Scheduling -----------------------*- C++ -*-===//
0002 //
0003 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
0004 // See https://llvm.org/LICENSE.txt for license information.
0005 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
0006 //
0007 //===----------------------------------------------------------------------===//
0008 //
0009 // This file defines the classes used to describe a subtarget's machine model
0010 // for scheduling and other instruction cost heuristics.
0011 //
0012 //===----------------------------------------------------------------------===//
0013 
0014 #ifndef LLVM_MC_MCSCHEDULE_H
0015 #define LLVM_MC_MCSCHEDULE_H
0016 
0017 #include "llvm/ADT/StringRef.h"
0018 #include "llvm/MC/MCInstrDesc.h"
0019 #include "llvm/Support/ErrorHandling.h"
0020 #include <cassert>
0021 #include <optional>
0022 
0023 namespace llvm {
0024 
0025 template <typename T> class ArrayRef;
0026 struct InstrItinerary;
0027 class MCSubtargetInfo;
0028 class MCInstrInfo;
0029 class MCInst;
0030 class MCInstrDesc;
0031 class InstrItineraryData;
0032 
0033 /// Define a kind of processor resource that will be modeled by the scheduler.
0034 struct MCProcResourceDesc {
0035   const char *Name;
0036   unsigned NumUnits; // Number of resource of this kind
0037   unsigned SuperIdx; // Index of the resources kind that contains this kind.
0038 
0039   // Number of resources that may be buffered.
0040   //
0041   // Buffered resources (BufferSize != 0) may be consumed at some indeterminate
0042   // cycle after dispatch. This should be used for out-of-order cpus when
0043   // instructions that use this resource can be buffered in a reservaton
0044   // station.
0045   //
0046   // Unbuffered resources (BufferSize == 0) always consume their resource some
0047   // fixed number of cycles after dispatch. If a resource is unbuffered, then
0048   // the scheduler will avoid scheduling instructions with conflicting resources
0049   // in the same cycle. This is for in-order cpus, or the in-order portion of
0050   // an out-of-order cpus.
0051   int BufferSize;
0052 
0053   // If the resource has sub-units, a pointer to the first element of an array
0054   // of `NumUnits` elements containing the ProcResourceIdx of the sub units.
0055   // nullptr if the resource does not have sub-units.
0056   const unsigned *SubUnitsIdxBegin;
0057 
0058   bool operator==(const MCProcResourceDesc &Other) const {
0059     return NumUnits == Other.NumUnits && SuperIdx == Other.SuperIdx
0060       && BufferSize == Other.BufferSize;
0061   }
0062 };
0063 
0064 /// Identify one of the processor resource kinds consumed by a
0065 /// particular scheduling class for the specified number of cycles.
0066 struct MCWriteProcResEntry {
0067   uint16_t ProcResourceIdx;
0068   /// Cycle at which the resource will be released by an instruction,
0069   /// relatively to the cycle in which the instruction is issued
0070   /// (assuming no stalls inbetween).
0071   uint16_t ReleaseAtCycle;
0072   /// Cycle at which the resource will be aquired by an instruction,
0073   /// relatively to the cycle in which the instruction is issued
0074   /// (assuming no stalls inbetween).
0075   uint16_t AcquireAtCycle;
0076 
0077   bool operator==(const MCWriteProcResEntry &Other) const {
0078     return ProcResourceIdx == Other.ProcResourceIdx &&
0079            ReleaseAtCycle == Other.ReleaseAtCycle &&
0080            AcquireAtCycle == Other.AcquireAtCycle;
0081   }
0082 };
0083 
0084 /// Specify the latency in cpu cycles for a particular scheduling class and def
0085 /// index. -1 indicates an invalid latency. Heuristics would typically consider
0086 /// an instruction with invalid latency to have infinite latency.  Also identify
0087 /// the WriteResources of this def. When the operand expands to a sequence of
0088 /// writes, this ID is the last write in the sequence.
0089 struct MCWriteLatencyEntry {
0090   int16_t Cycles;
0091   uint16_t WriteResourceID;
0092 
0093   bool operator==(const MCWriteLatencyEntry &Other) const {
0094     return Cycles == Other.Cycles && WriteResourceID == Other.WriteResourceID;
0095   }
0096 };
0097 
0098 /// Specify the number of cycles allowed after instruction issue before a
0099 /// particular use operand reads its registers. This effectively reduces the
0100 /// write's latency. Here we allow negative cycles for corner cases where
0101 /// latency increases. This rule only applies when the entry's WriteResource
0102 /// matches the write's WriteResource.
0103 ///
0104 /// MCReadAdvanceEntries are sorted first by operand index (UseIdx), then by
0105 /// WriteResourceIdx.
0106 struct MCReadAdvanceEntry {
0107   unsigned UseIdx;
0108   unsigned WriteResourceID;
0109   int Cycles;
0110 
0111   bool operator==(const MCReadAdvanceEntry &Other) const {
0112     return UseIdx == Other.UseIdx && WriteResourceID == Other.WriteResourceID
0113       && Cycles == Other.Cycles;
0114   }
0115 };
0116 
0117 /// Summarize the scheduling resources required for an instruction of a
0118 /// particular scheduling class.
0119 ///
0120 /// Defined as an aggregate struct for creating tables with initializer lists.
0121 struct MCSchedClassDesc {
0122   static const unsigned short InvalidNumMicroOps = (1U << 13) - 1;
0123   static const unsigned short VariantNumMicroOps = InvalidNumMicroOps - 1;
0124 
0125 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
0126   const char* Name;
0127 #endif
0128   uint16_t NumMicroOps : 13;
0129   uint16_t BeginGroup : 1;
0130   uint16_t EndGroup : 1;
0131   uint16_t RetireOOO : 1;
0132   uint16_t WriteProcResIdx; // First index into WriteProcResTable.
0133   uint16_t NumWriteProcResEntries;
0134   uint16_t WriteLatencyIdx; // First index into WriteLatencyTable.
0135   uint16_t NumWriteLatencyEntries;
0136   uint16_t ReadAdvanceIdx; // First index into ReadAdvanceTable.
0137   uint16_t NumReadAdvanceEntries;
0138 
0139   bool isValid() const {
0140     return NumMicroOps != InvalidNumMicroOps;
0141   }
0142   bool isVariant() const {
0143     return NumMicroOps == VariantNumMicroOps;
0144   }
0145 };
0146 
0147 /// Specify the cost of a register definition in terms of number of physical
0148 /// register allocated at register renaming stage. For example, AMD Jaguar.
0149 /// natively supports 128-bit data types, and operations on 256-bit registers
0150 /// (i.e. YMM registers) are internally split into two COPs (complex operations)
0151 /// and each COP updates a physical register. Basically, on Jaguar, a YMM
0152 /// register write effectively consumes two physical registers. That means,
0153 /// the cost of a YMM write in the BtVer2 model is 2.
0154 struct MCRegisterCostEntry {
0155   unsigned RegisterClassID;
0156   unsigned Cost;
0157   bool AllowMoveElimination;
0158 };
0159 
0160 /// A register file descriptor.
0161 ///
0162 /// This struct allows to describe processor register files. In particular, it
0163 /// helps describing the size of the register file, as well as the cost of
0164 /// allocating a register file at register renaming stage.
0165 /// FIXME: this struct can be extended to provide information about the number
0166 /// of read/write ports to the register file.  A value of zero for field
0167 /// 'NumPhysRegs' means: this register file has an unbounded number of physical
0168 /// registers.
0169 struct MCRegisterFileDesc {
0170   const char *Name;
0171   uint16_t NumPhysRegs;
0172   uint16_t NumRegisterCostEntries;
0173   // Index of the first cost entry in MCExtraProcessorInfo::RegisterCostTable.
0174   uint16_t RegisterCostEntryIdx;
0175   // A value of zero means: there is no limit in the number of moves that can be
0176   // eliminated every cycle.
0177   uint16_t MaxMovesEliminatedPerCycle;
0178   // Ture if this register file only knows how to optimize register moves from
0179   // known zero registers.
0180   bool AllowZeroMoveEliminationOnly;
0181 };
0182 
0183 /// Provide extra details about the machine processor.
0184 ///
0185 /// This is a collection of "optional" processor information that is not
0186 /// normally used by the LLVM machine schedulers, but that can be consumed by
0187 /// external tools like llvm-mca to improve the quality of the peformance
0188 /// analysis.
0189 struct MCExtraProcessorInfo {
0190   // Actual size of the reorder buffer in hardware.
0191   unsigned ReorderBufferSize;
0192   // Number of instructions retired per cycle.
0193   unsigned MaxRetirePerCycle;
0194   const MCRegisterFileDesc *RegisterFiles;
0195   unsigned NumRegisterFiles;
0196   const MCRegisterCostEntry *RegisterCostTable;
0197   unsigned NumRegisterCostEntries;
0198   unsigned LoadQueueID;
0199   unsigned StoreQueueID;
0200 };
0201 
0202 /// Machine model for scheduling, bundling, and heuristics.
0203 ///
0204 /// The machine model directly provides basic information about the
0205 /// microarchitecture to the scheduler in the form of properties. It also
0206 /// optionally refers to scheduler resource tables and itinerary
0207 /// tables. Scheduler resource tables model the latency and cost for each
0208 /// instruction type. Itinerary tables are an independent mechanism that
0209 /// provides a detailed reservation table describing each cycle of instruction
0210 /// execution. Subtargets may define any or all of the above categories of data
0211 /// depending on the type of CPU and selected scheduler.
0212 ///
0213 /// The machine independent properties defined here are used by the scheduler as
0214 /// an abstract machine model. A real micro-architecture has a number of
0215 /// buffers, queues, and stages. Declaring that a given machine-independent
0216 /// abstract property corresponds to a specific physical property across all
0217 /// subtargets can't be done. Nonetheless, the abstract model is
0218 /// useful. Futhermore, subtargets typically extend this model with processor
0219 /// specific resources to model any hardware features that can be exploited by
0220 /// scheduling heuristics and aren't sufficiently represented in the abstract.
0221 ///
0222 /// The abstract pipeline is built around the notion of an "issue point". This
0223 /// is merely a reference point for counting machine cycles. The physical
0224 /// machine will have pipeline stages that delay execution. The scheduler does
0225 /// not model those delays because they are irrelevant as long as they are
0226 /// consistent. Inaccuracies arise when instructions have different execution
0227 /// delays relative to each other, in addition to their intrinsic latency. Those
0228 /// special cases can be handled by TableGen constructs such as, ReadAdvance,
0229 /// which reduces latency when reading data, and ReleaseAtCycles, which consumes
0230 /// a processor resource when writing data for a number of abstract
0231 /// cycles.
0232 ///
0233 /// TODO: One tool currently missing is the ability to add a delay to
0234 /// ReleaseAtCycles. That would be easy to add and would likely cover all cases
0235 /// currently handled by the legacy itinerary tables.
0236 ///
0237 /// A note on out-of-order execution and, more generally, instruction
0238 /// buffers. Part of the CPU pipeline is always in-order. The issue point, which
0239 /// is the point of reference for counting cycles, only makes sense as an
0240 /// in-order part of the pipeline. Other parts of the pipeline are sometimes
0241 /// falling behind and sometimes catching up. It's only interesting to model
0242 /// those other, decoupled parts of the pipeline if they may be predictably
0243 /// resource constrained in a way that the scheduler can exploit.
0244 ///
0245 /// The LLVM machine model distinguishes between in-order constraints and
0246 /// out-of-order constraints so that the target's scheduling strategy can apply
0247 /// appropriate heuristics. For a well-balanced CPU pipeline, out-of-order
0248 /// resources would not typically be treated as a hard scheduling
0249 /// constraint. For example, in the GenericScheduler, a delay caused by limited
0250 /// out-of-order resources is not directly reflected in the number of cycles
0251 /// that the scheduler sees between issuing an instruction and its dependent
0252 /// instructions. In other words, out-of-order resources don't directly increase
0253 /// the latency between pairs of instructions. However, they can still be used
0254 /// to detect potential bottlenecks across a sequence of instructions and bias
0255 /// the scheduling heuristics appropriately.
0256 struct MCSchedModel {
0257   // IssueWidth is the maximum number of instructions that may be scheduled in
0258   // the same per-cycle group. This is meant to be a hard in-order constraint
0259   // (a.k.a. "hazard"). In the GenericScheduler strategy, no more than
0260   // IssueWidth micro-ops can ever be scheduled in a particular cycle.
0261   //
0262   // In practice, IssueWidth is useful to model any bottleneck between the
0263   // decoder (after micro-op expansion) and the out-of-order reservation
0264   // stations or the decoder bandwidth itself. If the total number of
0265   // reservation stations is also a bottleneck, or if any other pipeline stage
0266   // has a bandwidth limitation, then that can be naturally modeled by adding an
0267   // out-of-order processor resource.
0268   unsigned IssueWidth;
0269   static const unsigned DefaultIssueWidth = 1;
0270 
0271   // MicroOpBufferSize is the number of micro-ops that the processor may buffer
0272   // for out-of-order execution.
0273   //
0274   // "0" means operations that are not ready in this cycle are not considered
0275   // for scheduling (they go in the pending queue). Latency is paramount. This
0276   // may be more efficient if many instructions are pending in a schedule.
0277   //
0278   // "1" means all instructions are considered for scheduling regardless of
0279   // whether they are ready in this cycle. Latency still causes issue stalls,
0280   // but we balance those stalls against other heuristics.
0281   //
0282   // "> 1" means the processor is out-of-order. This is a machine independent
0283   // estimate of highly machine specific characteristics such as the register
0284   // renaming pool and reorder buffer.
0285   unsigned MicroOpBufferSize;
0286   static const unsigned DefaultMicroOpBufferSize = 0;
0287 
0288   // LoopMicroOpBufferSize is the number of micro-ops that the processor may
0289   // buffer for optimized loop execution. More generally, this represents the
0290   // optimal number of micro-ops in a loop body. A loop may be partially
0291   // unrolled to bring the count of micro-ops in the loop body closer to this
0292   // number.
0293   unsigned LoopMicroOpBufferSize;
0294   static const unsigned DefaultLoopMicroOpBufferSize = 0;
0295 
0296   // LoadLatency is the expected latency of load instructions.
0297   unsigned LoadLatency;
0298   static const unsigned DefaultLoadLatency = 4;
0299 
0300   // HighLatency is the expected latency of "very high latency" operations.
0301   // See TargetInstrInfo::isHighLatencyDef().
0302   // By default, this is set to an arbitrarily high number of cycles
0303   // likely to have some impact on scheduling heuristics.
0304   unsigned HighLatency;
0305   static const unsigned DefaultHighLatency = 10;
0306 
0307   // MispredictPenalty is the typical number of extra cycles the processor
0308   // takes to recover from a branch misprediction.
0309   unsigned MispredictPenalty;
0310   static const unsigned DefaultMispredictPenalty = 10;
0311 
0312   bool PostRAScheduler; // default value is false
0313 
0314   bool CompleteModel;
0315 
0316   // Tells the MachineScheduler whether or not to track resource usage
0317   // using intervals via ResourceSegments (see
0318   // llvm/include/llvm/CodeGen/MachineScheduler.h).
0319   bool EnableIntervals;
0320 
0321   unsigned ProcID;
0322   const MCProcResourceDesc *ProcResourceTable;
0323   const MCSchedClassDesc *SchedClassTable;
0324   unsigned NumProcResourceKinds;
0325   unsigned NumSchedClasses;
0326   // Instruction itinerary tables used by InstrItineraryData.
0327   friend class InstrItineraryData;
0328   const InstrItinerary *InstrItineraries;
0329 
0330   const MCExtraProcessorInfo *ExtraProcessorInfo;
0331 
0332   bool hasExtraProcessorInfo() const { return ExtraProcessorInfo; }
0333 
0334   unsigned getProcessorID() const { return ProcID; }
0335 
0336   /// Does this machine model include instruction-level scheduling.
0337   bool hasInstrSchedModel() const { return SchedClassTable; }
0338 
0339   const MCExtraProcessorInfo &getExtraProcessorInfo() const {
0340     assert(hasExtraProcessorInfo() &&
0341            "No extra information available for this model");
0342     return *ExtraProcessorInfo;
0343   }
0344 
0345   /// Return true if this machine model data for all instructions with a
0346   /// scheduling class (itinerary class or SchedRW list).
0347   bool isComplete() const { return CompleteModel; }
0348 
0349   /// Return true if machine supports out of order execution.
0350   bool isOutOfOrder() const { return MicroOpBufferSize > 1; }
0351 
0352   unsigned getNumProcResourceKinds() const {
0353     return NumProcResourceKinds;
0354   }
0355 
0356   const MCProcResourceDesc *getProcResource(unsigned ProcResourceIdx) const {
0357     assert(hasInstrSchedModel() && "No scheduling machine model");
0358 
0359     assert(ProcResourceIdx < NumProcResourceKinds && "bad proc resource idx");
0360     return &ProcResourceTable[ProcResourceIdx];
0361   }
0362 
0363   const MCSchedClassDesc *getSchedClassDesc(unsigned SchedClassIdx) const {
0364     assert(hasInstrSchedModel() && "No scheduling machine model");
0365 
0366     assert(SchedClassIdx < NumSchedClasses && "bad scheduling class idx");
0367     return &SchedClassTable[SchedClassIdx];
0368   }
0369 
0370   /// Returns the latency value for the scheduling class.
0371   static int computeInstrLatency(const MCSubtargetInfo &STI,
0372                                  const MCSchedClassDesc &SCDesc);
0373 
0374   int computeInstrLatency(const MCSubtargetInfo &STI, unsigned SClass) const;
0375 
0376   int computeInstrLatency(const MCSubtargetInfo &STI, const MCInstrInfo &MCII,
0377                           const MCInst &Inst) const;
0378 
0379   template <typename MCSubtargetInfo, typename MCInstrInfo,
0380             typename InstrItineraryData, typename MCInstOrMachineInstr>
0381   int computeInstrLatency(
0382       const MCSubtargetInfo &STI, const MCInstrInfo &MCII,
0383       const MCInstOrMachineInstr &Inst,
0384       llvm::function_ref<const MCSchedClassDesc *(const MCSchedClassDesc *)>
0385           ResolveVariantSchedClass =
0386               [](const MCSchedClassDesc *SCDesc) { return SCDesc; }) const;
0387 
0388   // Returns the reciprocal throughput information from a MCSchedClassDesc.
0389   static double
0390   getReciprocalThroughput(const MCSubtargetInfo &STI,
0391                           const MCSchedClassDesc &SCDesc);
0392 
0393   static double
0394   getReciprocalThroughput(unsigned SchedClass, const InstrItineraryData &IID);
0395 
0396   double
0397   getReciprocalThroughput(const MCSubtargetInfo &STI, const MCInstrInfo &MCII,
0398                           const MCInst &Inst) const;
0399 
0400   /// Returns the maximum forwarding delay for register reads dependent on
0401   /// writes of scheduling class WriteResourceIdx.
0402   static unsigned getForwardingDelayCycles(ArrayRef<MCReadAdvanceEntry> Entries,
0403                                            unsigned WriteResourceIdx = 0);
0404 
0405   /// Returns the default initialized model.
0406   static const MCSchedModel Default;
0407 };
0408 
0409 // The first three are only template'd arguments so we can get away with leaving
0410 // them as incomplete types below. The third is a template over
0411 // MCInst/MachineInstr so as to avoid a layering violation here that would make
0412 // the MC layer depend on CodeGen.
0413 template <typename MCSubtargetInfo, typename MCInstrInfo,
0414           typename InstrItineraryData, typename MCInstOrMachineInstr>
0415 int MCSchedModel::computeInstrLatency(
0416     const MCSubtargetInfo &STI, const MCInstrInfo &MCII,
0417     const MCInstOrMachineInstr &Inst,
0418     llvm::function_ref<const MCSchedClassDesc *(const MCSchedClassDesc *)>
0419         ResolveVariantSchedClass) const {
0420   static const int NoInformationAvailable = -1;
0421   // Check if we have a scheduling model for instructions.
0422   if (!hasInstrSchedModel()) {
0423     // Try to fall back to the itinerary model if the scheduling model doesn't
0424     // have a scheduling table.  Note the default does not have a table.
0425 
0426     llvm::StringRef CPU = STI.getCPU();
0427 
0428     // Check if we have a CPU to get the itinerary information.
0429     if (CPU.empty())
0430       return NoInformationAvailable;
0431 
0432     // Get itinerary information.
0433     InstrItineraryData IID = STI.getInstrItineraryForCPU(CPU);
0434     // Get the scheduling class of the requested instruction.
0435     const MCInstrDesc &Desc = MCII.get(Inst.getOpcode());
0436     unsigned SCClass = Desc.getSchedClass();
0437 
0438     unsigned Latency = 0;
0439 
0440     for (unsigned Idx = 0, IdxEnd = Inst.getNumOperands(); Idx != IdxEnd; ++Idx)
0441       if (std::optional<unsigned> OperCycle = IID.getOperandCycle(SCClass, Idx))
0442         Latency = std::max(Latency, *OperCycle);
0443 
0444     return int(Latency);
0445   }
0446 
0447   unsigned SchedClass = MCII.get(Inst.getOpcode()).getSchedClass();
0448   const MCSchedClassDesc *SCDesc = getSchedClassDesc(SchedClass);
0449   SCDesc = ResolveVariantSchedClass(SCDesc);
0450 
0451   if (!SCDesc || !SCDesc->isValid())
0452     return NoInformationAvailable;
0453 
0454   return MCSchedModel::computeInstrLatency(STI, *SCDesc);
0455 }
0456 
0457 } // namespace llvm
0458 
0459 #endif