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 //===----------------------------------------------------------------------===//
13 #include "Assembler.h"
14 #include "BenchmarkRunner.h"
15 #include "MCInstrDescView.h"
16 #include "llvm/ADT/StringExtras.h"
17 #include "llvm/ADT/StringRef.h"
18 #include "llvm/ADT/Twine.h"
19 #include "llvm/Support/FileSystem.h"
20 #include "llvm/Support/MemoryBuffer.h"
21 #include "llvm/Support/Program.h"
25 BenchmarkFailure::BenchmarkFailure(const llvm::Twine
&S
)
26 : llvm::StringError(S
, llvm::inconvertibleErrorCode()) {}
28 BenchmarkRunner::BenchmarkRunner(const LLVMState
&State
,
29 InstructionBenchmark::ModeE Mode
)
30 : State(State
), Mode(Mode
), Scratch(llvm::make_unique
<ScratchSpace
>()) {}
32 BenchmarkRunner::~BenchmarkRunner() = default;
34 // Repeat the snippet until there are at least NumInstructions in the resulting
36 static std::vector
<llvm::MCInst
>
37 GenerateInstructions(const BenchmarkCode
&BC
, const int MinInstructions
) {
38 std::vector
<llvm::MCInst
> Code
= BC
.Instructions
;
39 for (int I
= 0; I
< MinInstructions
; ++I
)
40 Code
.push_back(BC
.Instructions
[I
% BC
.Instructions
.size()]);
45 BenchmarkRunner::runConfiguration(const BenchmarkCode
&BC
,
46 unsigned NumRepetitions
) const {
47 InstructionBenchmark InstrBenchmark
;
48 InstrBenchmark
.Mode
= Mode
;
49 InstrBenchmark
.CpuName
= State
.getTargetMachine().getTargetCPU();
50 InstrBenchmark
.LLVMTriple
=
51 State
.getTargetMachine().getTargetTriple().normalize();
52 InstrBenchmark
.NumRepetitions
= NumRepetitions
;
53 InstrBenchmark
.Info
= BC
.Info
;
55 const std::vector
<llvm::MCInst
> &Instructions
= BC
.Instructions
;
56 if (Instructions
.empty()) {
57 InstrBenchmark
.Error
= "Empty snippet";
58 return InstrBenchmark
;
61 InstrBenchmark
.Key
.Instructions
= Instructions
;
63 // Assemble at least kMinInstructionsForSnippet instructions by repeating the
64 // snippet for debug/analysis. This is so that the user clearly understands
65 // that the inside instructions are repeated.
66 constexpr const int kMinInstructionsForSnippet
= 16;
68 auto ObjectFilePath
= writeObjectFile(
69 BC
, GenerateInstructions(BC
, kMinInstructionsForSnippet
));
70 if (llvm::Error E
= ObjectFilePath
.takeError()) {
71 InstrBenchmark
.Error
= llvm::toString(std::move(E
));
72 return InstrBenchmark
;
74 const ExecutableFunction
EF(State
.createTargetMachine(),
75 getObjectFromFile(*ObjectFilePath
));
76 const auto FnBytes
= EF
.getFunctionBytes();
77 InstrBenchmark
.AssembledSnippet
.assign(FnBytes
.begin(), FnBytes
.end());
80 // Assemble NumRepetitions instructions repetitions of the snippet for
82 auto ObjectFilePath
= writeObjectFile(
83 BC
, GenerateInstructions(BC
, InstrBenchmark
.NumRepetitions
));
84 if (llvm::Error E
= ObjectFilePath
.takeError()) {
85 InstrBenchmark
.Error
= llvm::toString(std::move(E
));
86 return InstrBenchmark
;
88 llvm::outs() << "Check generated assembly with: /usr/bin/objdump -d "
89 << *ObjectFilePath
<< "\n";
90 const ExecutableFunction
EF(State
.createTargetMachine(),
91 getObjectFromFile(*ObjectFilePath
));
92 InstrBenchmark
.Measurements
= runMeasurements(EF
, *Scratch
, NumRepetitions
);
94 return InstrBenchmark
;
97 llvm::Expected
<std::string
>
98 BenchmarkRunner::writeObjectFile(const BenchmarkCode
&BC
,
99 llvm::ArrayRef
<llvm::MCInst
> Code
) const {
101 llvm::SmallString
<256> ResultPath
;
102 if (llvm::Error E
= llvm::errorCodeToError(llvm::sys::fs::createTemporaryFile(
103 "snippet", "o", ResultFD
, ResultPath
)))
105 llvm::raw_fd_ostream
OFS(ResultFD
, true /*ShouldClose*/);
106 assembleToStream(State
.getExegesisTarget(), State
.createTargetMachine(),
107 BC
.LiveIns
, BC
.RegisterInitialValues
, Code
, OFS
);
108 return ResultPath
.str();
111 } // namespace exegesis