CustomBehaviour.h 7.4 KB

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