CustomBehaviour.h 5.3 KB

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