1 //===---------------------- CustomBehaviour.h -------------------*- C++ -*-===// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===----------------------------------------------------------------------===// 8 /// \file 9 /// 10 /// This file defines the base class CustomBehaviour which can be inherited from 11 /// by specific targets (ex. llvm/tools/llvm-mca/lib/X86CustomBehaviour.h). 12 /// CustomBehaviour is designed to enforce custom behaviour and dependencies 13 /// within the llvm-mca pipeline simulation that llvm-mca isn't already capable 14 /// of extracting from the Scheduling Models. 15 /// 16 //===----------------------------------------------------------------------===// 17 18 #ifndef LLVM_MCA_CUSTOMBEHAVIOUR_H 19 #define LLVM_MCA_CUSTOMBEHAVIOUR_H 20 21 #include "llvm/ADT/SmallVector.h" 22 #include "llvm/MC/MCInst.h" 23 #include "llvm/MC/MCInstrInfo.h" 24 #include "llvm/MC/MCSubtargetInfo.h" 25 #include "llvm/MCA/SourceMgr.h" 26 #include "llvm/MCA/View.h" 27 28 namespace llvm { 29 namespace mca { 30 31 /// Class which can be overriden by targets to modify the 32 /// mca::Instruction objects before the pipeline starts. 33 /// A common usage of this class is to add immediate operands to certain 34 /// instructions or to remove Defs/Uses from an instruction where the 35 /// schedulinng model is incorrect. 36 class InstrPostProcess { 37 protected: 38 const MCSubtargetInfo &STI; 39 const MCInstrInfo &MCII; 40 41 public: InstrPostProcess(const MCSubtargetInfo & STI,const MCInstrInfo & MCII)42 InstrPostProcess(const MCSubtargetInfo &STI, const MCInstrInfo &MCII) 43 : STI(STI), MCII(MCII) {} 44 45 virtual ~InstrPostProcess() = default; 46 47 /// This method can be overriden by targets to modify the mca::Instruction 48 /// object after it has been lowered from the MCInst. 49 /// This is generally a less disruptive alternative to modifying the 50 /// scheduling model. postProcessInstruction(std::unique_ptr<Instruction> & Inst,const MCInst & MCI)51 virtual void postProcessInstruction(std::unique_ptr<Instruction> &Inst, 52 const MCInst &MCI) {} 53 54 // The resetState() method gets invoked at the beginning of each code region 55 // so that targets that override this function can clear any state that they 56 // have left from the previous code region. resetState()57 virtual void resetState() {} 58 }; 59 60 /// Class which can be overriden by targets to enforce instruction 61 /// dependencies and behaviours that aren't expressed well enough 62 /// within the scheduling model for mca to automatically simulate 63 /// them properly. 64 /// If you implement this class for your target, make sure to also implement 65 /// a target specific InstrPostProcess class as well. 66 class CustomBehaviour { 67 protected: 68 const MCSubtargetInfo &STI; 69 const mca::SourceMgr &SrcMgr; 70 const MCInstrInfo &MCII; 71 72 public: CustomBehaviour(const MCSubtargetInfo & STI,const mca::SourceMgr & SrcMgr,const MCInstrInfo & MCII)73 CustomBehaviour(const MCSubtargetInfo &STI, const mca::SourceMgr &SrcMgr, 74 const MCInstrInfo &MCII) 75 : STI(STI), SrcMgr(SrcMgr), MCII(MCII) {} 76 77 virtual ~CustomBehaviour(); 78 79 /// Before the llvm-mca pipeline dispatches an instruction, it first checks 80 /// for any register or resource dependencies / hazards. If it doesn't find 81 /// any, this method will be invoked to determine if there are any custom 82 /// hazards that the instruction needs to wait for. 83 /// The return value of this method is the number of cycles that the 84 /// instruction needs to wait for. 85 /// It's safe to underestimate the number of cycles to wait for since these 86 /// checks will be invoked again before the intruction gets dispatched. 87 /// However, it's not safe (accurate) to overestimate the number of cycles 88 /// to wait for since the instruction will wait for AT LEAST that number of 89 /// cycles before attempting to be dispatched again. 90 virtual unsigned checkCustomHazard(ArrayRef<InstRef> IssuedInst, 91 const InstRef &IR); 92 93 // Functions that target CBs can override to return a list of 94 // target specific Views that need to live within /lib/Target/ so that 95 // they can benefit from the target CB or from backend functionality that is 96 // not already exposed through MC-layer classes. Keep in mind that how this 97 // function is used is that the function is called within llvm-mca.cpp and 98 // then each unique_ptr<View> is passed into the PipelinePrinter::addView() 99 // function. This function will then std::move the View into its own vector of 100 // Views. So any CB that overrides this function needs to make sure that they 101 // are not relying on the current address or reference of the View 102 // unique_ptrs. If you do need the CB and View to be able to communicate with 103 // each other, consider giving the View a reference or pointer to the CB when 104 // the View is constructed. Then the View can query the CB for information 105 // when it needs it. 106 /// Return a vector of Views that will be added before all other Views. 107 virtual std::vector<std::unique_ptr<View>> 108 getStartViews(llvm::MCInstPrinter &IP, llvm::ArrayRef<llvm::MCInst> Insts); 109 /// Return a vector of Views that will be added after the InstructionInfoView. 110 virtual std::vector<std::unique_ptr<View>> 111 getPostInstrInfoViews(llvm::MCInstPrinter &IP, 112 llvm::ArrayRef<llvm::MCInst> Insts); 113 /// Return a vector of Views that will be added after all other Views. 114 virtual std::vector<std::unique_ptr<View>> 115 getEndViews(llvm::MCInstPrinter &IP, llvm::ArrayRef<llvm::MCInst> Insts); 116 }; 117 118 class Instrument { 119 /// The description of Instrument kind 120 const StringRef Desc; 121 122 /// The instrumentation data 123 const StringRef Data; 124 125 public: Instrument(StringRef Desc,StringRef Data)126 Instrument(StringRef Desc, StringRef Data) : Desc(Desc), Data(Data) {} 127 Instrument()128 Instrument() : Instrument("", "") {} 129 130 virtual ~Instrument() = default; 131 getDesc()132 StringRef getDesc() const { return Desc; } getData()133 StringRef getData() const { return Data; } 134 }; 135 136 using SharedInstrument = std::shared_ptr<Instrument>; 137 138 /// This class allows targets to optionally customize the logic that resolves 139 /// scheduling class IDs. Targets can use information encoded in Instrument 140 /// objects to make more informed scheduling decisions. 141 class InstrumentManager { 142 protected: 143 const MCSubtargetInfo &STI; 144 const MCInstrInfo &MCII; 145 146 public: InstrumentManager(const MCSubtargetInfo & STI,const MCInstrInfo & MCII)147 InstrumentManager(const MCSubtargetInfo &STI, const MCInstrInfo &MCII) 148 : STI(STI), MCII(MCII) {} 149 150 virtual ~InstrumentManager() = default; 151 152 /// Returns true if llvm-mca should ignore instruments. shouldIgnoreInstruments()153 virtual bool shouldIgnoreInstruments() const { return true; } 154 155 // Returns true if this supports processing Instrument with 156 // Instrument.Desc equal to Type supportsInstrumentType(StringRef Type)157 virtual bool supportsInstrumentType(StringRef Type) const { return false; } 158 159 /// Allocate an Instrument, and return a shared pointer to it. 160 virtual SharedInstrument createInstrument(StringRef Desc, StringRef Data); 161 162 /// Given an MCInst and a vector of Instrument, a target can 163 /// return a SchedClassID. This can be used by a subtarget to return a 164 /// PseudoInstruction SchedClassID instead of the one that belongs to the 165 /// BaseInstruction This can be useful when a BaseInstruction does not convey 166 /// the correct scheduling information without additional data. By default, 167 /// it returns the SchedClassID that belongs to MCI. 168 virtual unsigned 169 getSchedClassID(const MCInstrInfo &MCII, const MCInst &MCI, 170 const SmallVector<SharedInstrument> &IVec) const; 171 }; 172 173 } // namespace mca 174 } // namespace llvm 175 176 #endif /* LLVM_MCA_CUSTOMBEHAVIOUR_H */ 177