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/MC/MCInst.h" 22 #include "llvm/MC/MCInstrInfo.h" 23 #include "llvm/MC/MCSubtargetInfo.h" 24 #include "llvm/MCA/SourceMgr.h" 25 #include "llvm/MCA/View.h" 26 27 namespace llvm { 28 namespace mca { 29 30 /// Class which can be overriden by targets to modify the 31 /// mca::Instruction objects before the pipeline starts. 32 /// A common usage of this class is to add immediate operands to certain 33 /// instructions or to remove Defs/Uses from an instruction where the 34 /// schedulinng model is incorrect. 35 class InstrPostProcess { 36 protected: 37 const MCSubtargetInfo &STI; 38 const MCInstrInfo &MCII; 39 40 public: 41 InstrPostProcess(const MCSubtargetInfo &STI, const MCInstrInfo &MCII) 42 : STI(STI), MCII(MCII) {} 43 44 virtual ~InstrPostProcess() = default; 45 46 /// This method can be overriden by targets to modify the mca::Instruction 47 /// object after it has been lowered from the MCInst. 48 /// This is generally a less disruptive alternative to modifying the 49 /// scheduling model. 50 virtual void postProcessInstruction(std::unique_ptr<Instruction> &Inst, 51 const MCInst &MCI) {} 52 53 // The resetState() method gets invoked at the beginning of each code region 54 // so that targets that override this function can clear any state that they 55 // have left from the previous code region. 56 virtual void resetState() {} 57 }; 58 59 /// Class which can be overriden by targets to enforce instruction 60 /// dependencies and behaviours that aren't expressed well enough 61 /// within the scheduling model for mca to automatically simulate 62 /// them properly. 63 /// If you implement this class for your target, make sure to also implement 64 /// a target specific InstrPostProcess class as well. 65 class CustomBehaviour { 66 protected: 67 const MCSubtargetInfo &STI; 68 const mca::SourceMgr &SrcMgr; 69 const MCInstrInfo &MCII; 70 71 public: 72 CustomBehaviour(const MCSubtargetInfo &STI, const mca::SourceMgr &SrcMgr, 73 const MCInstrInfo &MCII) 74 : STI(STI), SrcMgr(SrcMgr), MCII(MCII) {} 75 76 virtual ~CustomBehaviour(); 77 78 /// Before the llvm-mca pipeline dispatches an instruction, it first checks 79 /// for any register or resource dependencies / hazards. If it doesn't find 80 /// any, this method will be invoked to determine if there are any custom 81 /// hazards that the instruction needs to wait for. 82 /// The return value of this method is the number of cycles that the 83 /// instruction needs to wait for. 84 /// It's safe to underestimate the number of cycles to wait for since these 85 /// checks will be invoked again before the intruction gets dispatched. 86 /// However, it's not safe (accurate) to overestimate the number of cycles 87 /// to wait for since the instruction will wait for AT LEAST that number of 88 /// cycles before attempting to be dispatched again. 89 virtual unsigned checkCustomHazard(ArrayRef<InstRef> IssuedInst, 90 const InstRef &IR); 91 92 // Functions that target CBs can override to return a list of 93 // target specific Views that need to live within /lib/Target/ so that 94 // they can benefit from the target CB or from backend functionality that is 95 // not already exposed through MC-layer classes. Keep in mind that how this 96 // function is used is that the function is called within llvm-mca.cpp and 97 // then each unique_ptr<View> is passed into the PipelinePrinter::addView() 98 // function. This function will then std::move the View into its own vector of 99 // Views. So any CB that overrides this function needs to make sure that they 100 // are not relying on the current address or reference of the View 101 // unique_ptrs. If you do need the CB and View to be able to communicate with 102 // each other, consider giving the View a reference or pointer to the CB when 103 // the View is constructed. Then the View can query the CB for information 104 // when it needs it. 105 /// Return a vector of Views that will be added before all other Views. 106 virtual std::vector<std::unique_ptr<View>> 107 getStartViews(llvm::MCInstPrinter &IP, llvm::ArrayRef<llvm::MCInst> Insts); 108 /// Return a vector of Views that will be added after the InstructionInfoView. 109 virtual std::vector<std::unique_ptr<View>> 110 getPostInstrInfoViews(llvm::MCInstPrinter &IP, 111 llvm::ArrayRef<llvm::MCInst> Insts); 112 /// Return a vector of Views that will be added after all other Views. 113 virtual std::vector<std::unique_ptr<View>> 114 getEndViews(llvm::MCInstPrinter &IP, llvm::ArrayRef<llvm::MCInst> Insts); 115 }; 116 117 } // namespace mca 118 } // namespace llvm 119 120 #endif /* LLVM_MCA_CUSTOMBEHAVIOUR_H */ 121