Back to home page

EIC code displayed by LXR

 
 

    


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

0001 //===----------------------- HWEventListener.h ------------------*- 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 /// \file
0009 ///
0010 /// This file defines the main interface for hardware event listeners.
0011 ///
0012 //===----------------------------------------------------------------------===//
0013 
0014 #ifndef LLVM_MCA_HWEVENTLISTENER_H
0015 #define LLVM_MCA_HWEVENTLISTENER_H
0016 
0017 #include "llvm/ADT/ArrayRef.h"
0018 #include "llvm/MCA/Instruction.h"
0019 #include "llvm/MCA/Support.h"
0020 
0021 namespace llvm {
0022 namespace mca {
0023 
0024 // An HWInstructionEvent represents state changes of instructions that
0025 // listeners might be interested in. Listeners can choose to ignore any event
0026 // they are not interested in.
0027 class HWInstructionEvent {
0028 public:
0029   // This is the list of event types that are shared by all targets, that
0030   // generic subtarget-agnostic classes (e.g., Pipeline, HWInstructionEvent,
0031   // ...) and generic Views can manipulate.
0032   // Subtargets are free to define additional event types, that are going to be
0033   // handled by generic components as opaque values, but can still be
0034   // emitted by subtarget-specific pipeline stages (e.g., ExecuteStage,
0035   // DispatchStage, ...) and interpreted by subtarget-specific EventListener
0036   // implementations.
0037   enum GenericEventType {
0038     Invalid = 0,
0039     // Events generated by the Retire Control Unit.
0040     Retired,
0041     // Events generated by the Scheduler.
0042     Pending,
0043     Ready,
0044     Issued,
0045     Executed,
0046     // Events generated by the Dispatch logic.
0047     Dispatched,
0048 
0049     LastGenericEventType,
0050   };
0051 
0052   HWInstructionEvent(unsigned type, const InstRef &Inst)
0053       : Type(type), IR(Inst) {}
0054 
0055   // The event type. The exact meaning depends on the subtarget.
0056   const unsigned Type;
0057 
0058   // The instruction this event was generated for.
0059   const InstRef &IR;
0060 };
0061 
0062 // ResourceRef::first is the index of the associated Resource.
0063 // ResourceRef::second is a bitmask of the referenced sub-unit of the resource.
0064 using ResourceRef = std::pair<uint64_t, uint64_t>;
0065 
0066 using ResourceUse = std::pair<ResourceRef, ReleaseAtCycles>;
0067 
0068 class HWInstructionIssuedEvent : public HWInstructionEvent {
0069 public:
0070   HWInstructionIssuedEvent(const InstRef &IR, ArrayRef<ResourceUse> UR)
0071       : HWInstructionEvent(HWInstructionEvent::Issued, IR), UsedResources(UR) {}
0072 
0073   ArrayRef<ResourceUse> UsedResources;
0074 };
0075 
0076 class HWInstructionDispatchedEvent : public HWInstructionEvent {
0077 public:
0078   HWInstructionDispatchedEvent(const InstRef &IR, ArrayRef<unsigned> Regs,
0079                                unsigned UOps)
0080       : HWInstructionEvent(HWInstructionEvent::Dispatched, IR),
0081         UsedPhysRegs(Regs), MicroOpcodes(UOps) {}
0082   // Number of physical register allocated for this instruction. There is one
0083   // entry per register file.
0084   ArrayRef<unsigned> UsedPhysRegs;
0085   // Number of micro opcodes dispatched.
0086   // This field is often set to the total number of micro-opcodes specified by
0087   // the instruction descriptor of IR.
0088   // The only exception is when IR declares a number of micro opcodes
0089   // which exceeds the processor DispatchWidth, and - by construction - it
0090   // requires multiple cycles to be fully dispatched. In that particular case,
0091   // the dispatch logic would generate more than one dispatch event (one per
0092   // cycle), and each event would declare how many micro opcodes are effectively
0093   // been dispatched to the schedulers.
0094   unsigned MicroOpcodes;
0095 };
0096 
0097 class HWInstructionRetiredEvent : public HWInstructionEvent {
0098 public:
0099   HWInstructionRetiredEvent(const InstRef &IR, ArrayRef<unsigned> Regs)
0100       : HWInstructionEvent(HWInstructionEvent::Retired, IR),
0101         FreedPhysRegs(Regs) {}
0102   // Number of register writes that have been architecturally committed. There
0103   // is one entry per register file.
0104   ArrayRef<unsigned> FreedPhysRegs;
0105 };
0106 
0107 // A HWStallEvent represents a pipeline stall caused by the lack of hardware
0108 // resources.
0109 class HWStallEvent {
0110 public:
0111   enum GenericEventType {
0112     Invalid = 0,
0113     // Generic stall events generated by the DispatchStage.
0114     RegisterFileStall,
0115     RetireControlUnitStall,
0116     // Generic stall events generated by the Scheduler.
0117     DispatchGroupStall,
0118     SchedulerQueueFull,
0119     LoadQueueFull,
0120     StoreQueueFull,
0121     CustomBehaviourStall,
0122     LastGenericEvent
0123   };
0124 
0125   HWStallEvent(unsigned type, const InstRef &Inst) : Type(type), IR(Inst) {}
0126 
0127   // The exact meaning of the stall event type depends on the subtarget.
0128   const unsigned Type;
0129 
0130   // The instruction this event was generated for.
0131   const InstRef &IR;
0132 };
0133 
0134 // A HWPressureEvent describes an increase in backend pressure caused by
0135 // the presence of data dependencies or unavailability of pipeline resources.
0136 class HWPressureEvent {
0137 public:
0138   enum GenericReason {
0139     INVALID = 0,
0140     // Scheduler was unable to issue all the ready instructions because some
0141     // pipeline resources were unavailable.
0142     RESOURCES,
0143     // Instructions could not be issued because of register data dependencies.
0144     REGISTER_DEPS,
0145     // Instructions could not be issued because of memory dependencies.
0146     MEMORY_DEPS
0147   };
0148 
0149   HWPressureEvent(GenericReason reason, ArrayRef<InstRef> Insts,
0150                   uint64_t Mask = 0)
0151       : Reason(reason), AffectedInstructions(Insts), ResourceMask(Mask) {}
0152 
0153   // Reason for this increase in backend pressure.
0154   GenericReason Reason;
0155 
0156   // Instructions affected (i.e. delayed) by this increase in backend pressure.
0157   ArrayRef<InstRef> AffectedInstructions;
0158 
0159   // A mask of unavailable processor resources.
0160   const uint64_t ResourceMask;
0161 };
0162 
0163 class HWEventListener {
0164 public:
0165   // Generic events generated by the pipeline.
0166   virtual void onCycleBegin() {}
0167   virtual void onCycleEnd() {}
0168 
0169   virtual void onEvent(const HWInstructionEvent &Event) {}
0170   virtual void onEvent(const HWStallEvent &Event) {}
0171   virtual void onEvent(const HWPressureEvent &Event) {}
0172 
0173   virtual void onResourceAvailable(const ResourceRef &RRef) {}
0174 
0175   // Events generated by the Scheduler when buffered resources are
0176   // consumed/freed for an instruction.
0177   virtual void onReservedBuffers(const InstRef &Inst,
0178                                  ArrayRef<unsigned> Buffers) {}
0179   virtual void onReleasedBuffers(const InstRef &Inst,
0180                                  ArrayRef<unsigned> Buffers) {}
0181 
0182   virtual ~HWEventListener() = default;
0183 
0184 private:
0185   virtual void anchor();
0186 };
0187 } // namespace mca
0188 } // namespace llvm
0189 
0190 #endif // LLVM_MCA_HWEVENTLISTENER_H