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