SnippetFile.cpp 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. //===-- SnippetFile.cpp -----------------------------------------*- 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. #include "SnippetFile.h"
  9. #include "Error.h"
  10. #include "llvm/MC/MCContext.h"
  11. #include "llvm/MC/MCInstPrinter.h"
  12. #include "llvm/MC/MCObjectFileInfo.h"
  13. #include "llvm/MC/MCParser/MCAsmLexer.h"
  14. #include "llvm/MC/MCParser/MCAsmParser.h"
  15. #include "llvm/MC/MCParser/MCTargetAsmParser.h"
  16. #include "llvm/MC/MCRegisterInfo.h"
  17. #include "llvm/MC/MCStreamer.h"
  18. #include "llvm/MC/TargetRegistry.h"
  19. #include "llvm/Support/Format.h"
  20. #include "llvm/Support/Path.h"
  21. #include "llvm/Support/SourceMgr.h"
  22. #include <string>
  23. namespace llvm {
  24. namespace exegesis {
  25. namespace {
  26. // An MCStreamer that reads a BenchmarkCode definition from a file.
  27. class BenchmarkCodeStreamer : public MCStreamer, public AsmCommentConsumer {
  28. public:
  29. explicit BenchmarkCodeStreamer(
  30. MCContext *Context, const DenseMap<StringRef, unsigned> &RegNameToRegNo,
  31. BenchmarkCode *Result)
  32. : MCStreamer(*Context), RegNameToRegNo(RegNameToRegNo), Result(Result) {}
  33. // Implementation of the MCStreamer interface. We only care about
  34. // instructions.
  35. void emitInstruction(const MCInst &Instruction,
  36. const MCSubtargetInfo &STI) override {
  37. Result->Key.Instructions.push_back(Instruction);
  38. }
  39. // Implementation of the AsmCommentConsumer.
  40. void HandleComment(SMLoc Loc, StringRef CommentText) override {
  41. CommentText = CommentText.trim();
  42. if (!CommentText.consume_front("LLVM-EXEGESIS-"))
  43. return;
  44. if (CommentText.consume_front("DEFREG")) {
  45. // LLVM-EXEGESIS-DEFREF <reg> <hex_value>
  46. RegisterValue RegVal;
  47. SmallVector<StringRef, 2> Parts;
  48. CommentText.split(Parts, ' ', /*unlimited splits*/ -1,
  49. /*do not keep empty strings*/ false);
  50. if (Parts.size() != 2) {
  51. errs() << "invalid comment 'LLVM-EXEGESIS-DEFREG " << CommentText
  52. << "', expected two parameters <REG> <HEX_VALUE>\n";
  53. ++InvalidComments;
  54. return;
  55. }
  56. if (!(RegVal.Register = findRegisterByName(Parts[0].trim()))) {
  57. errs() << "unknown register '" << Parts[0]
  58. << "' in 'LLVM-EXEGESIS-DEFREG " << CommentText << "'\n";
  59. ++InvalidComments;
  60. return;
  61. }
  62. const StringRef HexValue = Parts[1].trim();
  63. RegVal.Value = APInt(
  64. /* each hex digit is 4 bits */ HexValue.size() * 4, HexValue, 16);
  65. Result->Key.RegisterInitialValues.push_back(std::move(RegVal));
  66. return;
  67. }
  68. if (CommentText.consume_front("LIVEIN")) {
  69. // LLVM-EXEGESIS-LIVEIN <reg>
  70. const auto RegName = CommentText.ltrim();
  71. if (unsigned Reg = findRegisterByName(RegName))
  72. Result->LiveIns.push_back(Reg);
  73. else {
  74. errs() << "unknown register '" << RegName
  75. << "' in 'LLVM-EXEGESIS-LIVEIN " << CommentText << "'\n";
  76. ++InvalidComments;
  77. }
  78. return;
  79. }
  80. }
  81. unsigned numInvalidComments() const { return InvalidComments; }
  82. private:
  83. // We only care about instructions, we don't implement this part of the API.
  84. void emitCommonSymbol(MCSymbol *Symbol, uint64_t Size,
  85. Align ByteAlignment) override {}
  86. bool emitSymbolAttribute(MCSymbol *Symbol, MCSymbolAttr Attribute) override {
  87. return false;
  88. }
  89. void emitValueToAlignment(Align Alignment, int64_t Value, unsigned ValueSize,
  90. unsigned MaxBytesToEmit) override {}
  91. void emitZerofill(MCSection *Section, MCSymbol *Symbol, uint64_t Size,
  92. Align ByteAlignment, SMLoc Loc) override {}
  93. unsigned findRegisterByName(const StringRef RegName) const {
  94. auto Iter = RegNameToRegNo.find(RegName);
  95. if (Iter != RegNameToRegNo.end())
  96. return Iter->second;
  97. errs() << "'" << RegName
  98. << "' is not a valid register name for the target\n";
  99. return 0;
  100. }
  101. const DenseMap<StringRef, unsigned> &RegNameToRegNo;
  102. BenchmarkCode *const Result;
  103. unsigned InvalidComments = 0;
  104. };
  105. } // namespace
  106. // Reads code snippets from file `Filename`.
  107. Expected<std::vector<BenchmarkCode>> readSnippets(const LLVMState &State,
  108. StringRef Filename) {
  109. ErrorOr<std::unique_ptr<MemoryBuffer>> BufferPtr =
  110. MemoryBuffer::getFileOrSTDIN(Filename);
  111. if (std::error_code EC = BufferPtr.getError()) {
  112. return make_error<Failure>("cannot read snippet: " + Filename + ": " +
  113. EC.message());
  114. }
  115. SourceMgr SM;
  116. SM.AddNewSourceBuffer(std::move(BufferPtr.get()), SMLoc());
  117. BenchmarkCode Result;
  118. const TargetMachine &TM = State.getTargetMachine();
  119. MCContext Context(TM.getTargetTriple(), TM.getMCAsmInfo(),
  120. TM.getMCRegisterInfo(), TM.getMCSubtargetInfo());
  121. std::unique_ptr<MCObjectFileInfo> ObjectFileInfo(
  122. TM.getTarget().createMCObjectFileInfo(Context, /*PIC=*/false));
  123. Context.setObjectFileInfo(ObjectFileInfo.get());
  124. Context.initInlineSourceManager();
  125. BenchmarkCodeStreamer Streamer(&Context, State.getRegNameToRegNoMapping(),
  126. &Result);
  127. std::string Error;
  128. raw_string_ostream ErrorStream(Error);
  129. formatted_raw_ostream InstPrinterOStream(ErrorStream);
  130. const std::unique_ptr<MCInstPrinter> InstPrinter(
  131. TM.getTarget().createMCInstPrinter(
  132. TM.getTargetTriple(), TM.getMCAsmInfo()->getAssemblerDialect(),
  133. *TM.getMCAsmInfo(), *TM.getMCInstrInfo(), *TM.getMCRegisterInfo()));
  134. // The following call will take care of calling Streamer.setTargetStreamer.
  135. TM.getTarget().createAsmTargetStreamer(Streamer, InstPrinterOStream,
  136. InstPrinter.get(),
  137. TM.Options.MCOptions.AsmVerbose);
  138. if (!Streamer.getTargetStreamer())
  139. return make_error<Failure>("cannot create target asm streamer");
  140. const std::unique_ptr<MCAsmParser> AsmParser(
  141. createMCAsmParser(SM, Context, Streamer, *TM.getMCAsmInfo()));
  142. if (!AsmParser)
  143. return make_error<Failure>("cannot create asm parser");
  144. AsmParser->getLexer().setCommentConsumer(&Streamer);
  145. const std::unique_ptr<MCTargetAsmParser> TargetAsmParser(
  146. TM.getTarget().createMCAsmParser(*TM.getMCSubtargetInfo(), *AsmParser,
  147. *TM.getMCInstrInfo(),
  148. MCTargetOptions()));
  149. if (!TargetAsmParser)
  150. return make_error<Failure>("cannot create target asm parser");
  151. AsmParser->setTargetParser(*TargetAsmParser);
  152. if (AsmParser->Run(false))
  153. return make_error<Failure>("cannot parse asm file");
  154. if (Streamer.numInvalidComments())
  155. return make_error<Failure>(Twine("found ")
  156. .concat(Twine(Streamer.numInvalidComments()))
  157. .concat(" invalid LLVM-EXEGESIS comments"));
  158. return std::vector<BenchmarkCode>{std::move(Result)};
  159. }
  160. } // namespace exegesis
  161. } // namespace llvm