CodeRegion.h 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. //===-------------------------- CodeRegion.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 implements class CodeRegion and CodeRegions, InstrumentRegion,
  11. /// AnalysisRegions, and InstrumentRegions.
  12. ///
  13. /// A CodeRegion describes a region of assembly code guarded by special LLVM-MCA
  14. /// comment directives.
  15. ///
  16. /// # LLVM-MCA-BEGIN foo
  17. /// ... ## asm
  18. /// # LLVM-MCA-END
  19. ///
  20. /// A comment starting with substring LLVM-MCA-BEGIN marks the beginning of a
  21. /// new region of code.
  22. /// A comment starting with substring LLVM-MCA-END marks the end of the
  23. /// last-seen region of code.
  24. ///
  25. /// Code regions are not allowed to overlap. Each region can have a optional
  26. /// description; internally, regions are described by a range of source
  27. /// locations (SMLoc objects).
  28. ///
  29. /// An instruction (a MCInst) is added to a CodeRegion R only if its
  30. /// location is in range [R.RangeStart, R.RangeEnd].
  31. ///
  32. /// A InstrumentRegion describes a region of assembly code guarded by
  33. /// special LLVM-MCA comment directives.
  34. ///
  35. /// # LLVM-MCA-<INSTRUMENTATION_TYPE> <data>
  36. /// ... ## asm
  37. ///
  38. /// where INSTRUMENTATION_TYPE is a type defined in llvm and expects to use
  39. /// data.
  40. ///
  41. /// A comment starting with substring LLVM-MCA-<INSTRUMENTATION_TYPE>
  42. /// brings data into scope for llvm-mca to use in its analysis for
  43. /// all following instructions.
  44. ///
  45. /// If the same INSTRUMENTATION_TYPE is found later in the instruction list,
  46. /// then the original InstrumentRegion will be automatically ended,
  47. /// and a new InstrumentRegion will begin.
  48. ///
  49. /// If there are comments containing the different INSTRUMENTATION_TYPEs,
  50. /// then both data sets remain available. In contrast with a CodeRegion,
  51. /// an InstrumentRegion does not need a comment to end the region.
  52. //
  53. // An instruction (a MCInst) is added to an InstrumentRegion R only
  54. // if its location is in range [R.RangeStart, R.RangeEnd].
  55. //
  56. //===----------------------------------------------------------------------===//
  57. #ifndef LLVM_TOOLS_LLVM_MCA_CODEREGION_H
  58. #define LLVM_TOOLS_LLVM_MCA_CODEREGION_H
  59. #include "llvm/ADT/ArrayRef.h"
  60. #include "llvm/ADT/SmallVector.h"
  61. #include "llvm/ADT/StringMap.h"
  62. #include "llvm/ADT/StringRef.h"
  63. #include "llvm/MC/MCInst.h"
  64. #include "llvm/MCA/CustomBehaviour.h"
  65. #include "llvm/Support/Error.h"
  66. #include "llvm/Support/SMLoc.h"
  67. #include "llvm/Support/SourceMgr.h"
  68. #include <vector>
  69. namespace llvm {
  70. namespace mca {
  71. /// A region of assembly code.
  72. ///
  73. /// It identifies a sequence of machine instructions.
  74. class CodeRegion {
  75. // An optional descriptor for this region.
  76. llvm::StringRef Description;
  77. // Instructions that form this region.
  78. llvm::SmallVector<llvm::MCInst, 16> Instructions;
  79. // Source location range.
  80. llvm::SMLoc RangeStart;
  81. llvm::SMLoc RangeEnd;
  82. CodeRegion(const CodeRegion &) = delete;
  83. CodeRegion &operator=(const CodeRegion &) = delete;
  84. public:
  85. CodeRegion(llvm::StringRef Desc, llvm::SMLoc Start)
  86. : Description(Desc), RangeStart(Start) {}
  87. void addInstruction(const llvm::MCInst &Instruction) {
  88. Instructions.emplace_back(Instruction);
  89. }
  90. llvm::SMLoc startLoc() const { return RangeStart; }
  91. llvm::SMLoc endLoc() const { return RangeEnd; }
  92. void setEndLocation(llvm::SMLoc End) { RangeEnd = End; }
  93. bool empty() const { return Instructions.empty(); }
  94. bool isLocInRange(llvm::SMLoc Loc) const;
  95. llvm::ArrayRef<llvm::MCInst> getInstructions() const { return Instructions; }
  96. llvm::StringRef getDescription() const { return Description; }
  97. };
  98. /// Alias AnalysisRegion with CodeRegion since CodeRegionGenerator
  99. /// is absract and AnalysisRegionGenerator operates on AnalysisRegions
  100. using AnalysisRegion = CodeRegion;
  101. /// A CodeRegion that contains instrumentation that can be used
  102. /// in analysis of the region.
  103. class InstrumentRegion : public CodeRegion {
  104. /// Instrument for this region.
  105. SharedInstrument Instrument;
  106. public:
  107. InstrumentRegion(llvm::StringRef Desc, llvm::SMLoc Start, SharedInstrument I)
  108. : CodeRegion(Desc, Start), Instrument(I) {}
  109. public:
  110. SharedInstrument getInstrument() const { return Instrument; }
  111. };
  112. class CodeRegionParseError final : public Error {};
  113. class CodeRegions {
  114. CodeRegions(const CodeRegions &) = delete;
  115. CodeRegions &operator=(const CodeRegions &) = delete;
  116. protected:
  117. // A source manager. Used by the tool to generate meaningful warnings.
  118. llvm::SourceMgr &SM;
  119. using UniqueCodeRegion = std::unique_ptr<CodeRegion>;
  120. std::vector<UniqueCodeRegion> Regions;
  121. llvm::StringMap<unsigned> ActiveRegions;
  122. bool FoundErrors;
  123. public:
  124. CodeRegions(llvm::SourceMgr &S) : SM(S), FoundErrors(false) {}
  125. typedef std::vector<UniqueCodeRegion>::iterator iterator;
  126. typedef std::vector<UniqueCodeRegion>::const_iterator const_iterator;
  127. iterator begin() { return Regions.begin(); }
  128. iterator end() { return Regions.end(); }
  129. const_iterator begin() const { return Regions.cbegin(); }
  130. const_iterator end() const { return Regions.cend(); }
  131. void addInstruction(const llvm::MCInst &Instruction);
  132. llvm::SourceMgr &getSourceMgr() const { return SM; }
  133. llvm::ArrayRef<llvm::MCInst> getInstructionSequence(unsigned Idx) const {
  134. return Regions[Idx]->getInstructions();
  135. }
  136. bool empty() const {
  137. return llvm::all_of(Regions, [](const UniqueCodeRegion &Region) {
  138. return Region->empty();
  139. });
  140. }
  141. bool isValid() const { return !FoundErrors; }
  142. bool isRegionActive(llvm::StringRef Description) const {
  143. return ActiveRegions.find(Description) != ActiveRegions.end();
  144. }
  145. };
  146. struct AnalysisRegions : public CodeRegions {
  147. AnalysisRegions(llvm::SourceMgr &S);
  148. void beginRegion(llvm::StringRef Description, llvm::SMLoc Loc);
  149. void endRegion(llvm::StringRef Description, llvm::SMLoc Loc);
  150. };
  151. struct InstrumentRegions : public CodeRegions {
  152. InstrumentRegions(llvm::SourceMgr &S);
  153. void beginRegion(llvm::StringRef Description, llvm::SMLoc Loc,
  154. SharedInstrument Instrument);
  155. void endRegion(llvm::StringRef Description, llvm::SMLoc Loc);
  156. const SmallVector<SharedInstrument>
  157. getActiveInstruments(llvm::SMLoc Loc) const;
  158. };
  159. } // namespace mca
  160. } // namespace llvm
  161. #endif