OSDN Git Service

Re-land r329156 "Add llvm-exegesis tool."
[android-x86/external-llvm.git] / tools / llvm-exegesis / lib / BenchmarkRunner.cpp
1 //===-- 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
17 namespace exegesis {
18
19 BenchmarkRunner::InstructionFilter::~InstructionFilter() = default;
20
21 BenchmarkRunner::~BenchmarkRunner() = default;
22
23 InstructionBenchmark
24 BenchmarkRunner::run(const LLVMState &State, const unsigned Opcode,
25                      unsigned NumRepetitions,
26                      const InstructionFilter &Filter) const {
27   InstructionBenchmark InstrBenchmark;
28
29   InstrBenchmark.AsmTmpl.Name =
30       llvm::Twine(getDisplayName())
31           .concat(" ")
32           .concat(State.getInstrInfo().getName(Opcode))
33           .str();
34   InstrBenchmark.CpuName = State.getCpuName();
35   InstrBenchmark.LLVMTriple = State.getTriple();
36   InstrBenchmark.NumRepetitions = NumRepetitions;
37
38   // Ignore instructions that we cannot run.
39   if (State.getInstrInfo().get(Opcode).isPseudo()) {
40     InstrBenchmark.Error = "Unsupported opcode: isPseudo";
41     return InstrBenchmark;
42   }
43   if (llvm::Error E = Filter.shouldRun(State, Opcode)) {
44     InstrBenchmark.Error = llvm::toString(std::move(E));
45     return InstrBenchmark;
46   }
47
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;
54   }
55
56   const std::vector<llvm::MCInst> Instructions = *ExpectedInstructions;
57   const JitFunction Function(std::move(Context), Instructions);
58   const llvm::StringRef CodeBytes = Function.getFunctionBytes();
59
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);
65   } else {
66     AsmExcerpt =
67         llvm::toHex(CodeBytes.take_front(ExcerptSize - ExcerptTailSize + 3));
68     AsmExcerpt += "...";
69     AsmExcerpt += llvm::toHex(CodeBytes.take_back(ExcerptTailSize));
70   }
71   llvm::outs() << "# Asm excerpt: " << AsmExcerpt << "\n";
72   llvm::outs().flush(); // In case we crash.
73
74   InstrBenchmark.Measurements =
75       runMeasurements(State, Function, NumRepetitions);
76   return InstrBenchmark;
77 }
78
79 } // namespace exegesis