blob: 2615a8299026111a5cdb06b1e4f9af25c4a48a51 [file] [log] [blame]
Clement Courbetac74acd2018-04-04 11:37:06 +00001//===-- BenchmarkRunner.cpp -------------------------------------*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
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"
15#include <string>
16
17namespace exegesis {
18
19BenchmarkRunner::InstructionFilter::~InstructionFilter() = default;
20
21BenchmarkRunner::~BenchmarkRunner() = default;
22
23InstructionBenchmark
24BenchmarkRunner::run(const LLVMState &State, const unsigned Opcode,
25 unsigned NumRepetitions,
26 const InstructionFilter &Filter) const {
27 InstructionBenchmark InstrBenchmark;
28
Clement Courbeta66bfaa42018-05-15 13:07:05 +000029 InstrBenchmark.Key.OpcodeName = State.getInstrInfo().getName(Opcode);
30 InstrBenchmark.Key.Mode = getDisplayName();
Clement Courbetac74acd2018-04-04 11:37:06 +000031 InstrBenchmark.CpuName = State.getCpuName();
32 InstrBenchmark.LLVMTriple = State.getTriple();
33 InstrBenchmark.NumRepetitions = NumRepetitions;
34
35 // Ignore instructions that we cannot run.
36 if (State.getInstrInfo().get(Opcode).isPseudo()) {
37 InstrBenchmark.Error = "Unsupported opcode: isPseudo";
38 return InstrBenchmark;
39 }
40 if (llvm::Error E = Filter.shouldRun(State, Opcode)) {
41 InstrBenchmark.Error = llvm::toString(std::move(E));
42 return InstrBenchmark;
43 }
44
45 JitFunctionContext Context(State.createTargetMachine());
46 auto ExpectedInstructions =
47 createCode(State, Opcode, NumRepetitions, Context);
48 if (llvm::Error E = ExpectedInstructions.takeError()) {
49 InstrBenchmark.Error = llvm::toString(std::move(E));
50 return InstrBenchmark;
51 }
52
53 const std::vector<llvm::MCInst> Instructions = *ExpectedInstructions;
54 const JitFunction Function(std::move(Context), Instructions);
55 const llvm::StringRef CodeBytes = Function.getFunctionBytes();
56
57 std::string AsmExcerpt;
58 constexpr const int ExcerptSize = 100;
59 constexpr const int ExcerptTailSize = 10;
60 if (CodeBytes.size() <= ExcerptSize) {
61 AsmExcerpt = llvm::toHex(CodeBytes);
62 } else {
63 AsmExcerpt =
64 llvm::toHex(CodeBytes.take_front(ExcerptSize - ExcerptTailSize + 3));
65 AsmExcerpt += "...";
66 AsmExcerpt += llvm::toHex(CodeBytes.take_back(ExcerptTailSize));
67 }
68 llvm::outs() << "# Asm excerpt: " << AsmExcerpt << "\n";
69 llvm::outs().flush(); // In case we crash.
70
71 InstrBenchmark.Measurements =
72 runMeasurements(State, Function, NumRepetitions);
73 return InstrBenchmark;
74}
75
76} // namespace exegesis