1 //===-- BenchmarkRunner.cpp -------------------------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #include "BenchmarkRunner.h"
11 #include "InMemoryAssembler.h"
12 #include "llvm/ADT/StringExtras.h"
13 #include "llvm/ADT/StringRef.h"
14 #include "llvm/ADT/Twine.h"
19 BenchmarkRunner::InstructionFilter::~InstructionFilter() = default;
21 BenchmarkRunner::~BenchmarkRunner() = default;
24 BenchmarkRunner::run(const LLVMState &State, const unsigned Opcode,
25 unsigned NumRepetitions,
26 const InstructionFilter &Filter) const {
27 InstructionBenchmark InstrBenchmark;
29 InstrBenchmark.AsmTmpl.Name =
30 llvm::Twine(getDisplayName())
32 .concat(State.getInstrInfo().getName(Opcode))
34 InstrBenchmark.CpuName = State.getCpuName();
35 InstrBenchmark.LLVMTriple = State.getTriple();
36 InstrBenchmark.NumRepetitions = NumRepetitions;
38 // Ignore instructions that we cannot run.
39 if (State.getInstrInfo().get(Opcode).isPseudo()) {
40 InstrBenchmark.Error = "Unsupported opcode: isPseudo";
41 return InstrBenchmark;
43 if (llvm::Error E = Filter.shouldRun(State, Opcode)) {
44 InstrBenchmark.Error = llvm::toString(std::move(E));
45 return InstrBenchmark;
48 JitFunctionContext Context(State.createTargetMachine());
49 auto ExpectedInstructions =
50 createCode(State, Opcode, NumRepetitions, Context);
51 if (llvm::Error E = ExpectedInstructions.takeError()) {
52 InstrBenchmark.Error = llvm::toString(std::move(E));
53 return InstrBenchmark;
56 const std::vector<llvm::MCInst> Instructions = *ExpectedInstructions;
57 const JitFunction Function(std::move(Context), Instructions);
58 const llvm::StringRef CodeBytes = Function.getFunctionBytes();
60 std::string AsmExcerpt;
61 constexpr const int ExcerptSize = 100;
62 constexpr const int ExcerptTailSize = 10;
63 if (CodeBytes.size() <= ExcerptSize) {
64 AsmExcerpt = llvm::toHex(CodeBytes);
67 llvm::toHex(CodeBytes.take_front(ExcerptSize - ExcerptTailSize + 3));
69 AsmExcerpt += llvm::toHex(CodeBytes.take_back(ExcerptTailSize));
71 llvm::outs() << "# Asm excerpt: " << AsmExcerpt << "\n";
72 llvm::outs().flush(); // In case we crash.
74 InstrBenchmark.Measurements =
75 runMeasurements(State, Function, NumRepetitions);
76 return InstrBenchmark;
79 } // namespace exegesis