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