2018-04-04 13:37:06 +02:00
|
|
|
//===-- BenchmarkRunner.h ---------------------------------------*- C++ -*-===//
|
|
|
|
//
|
2019-01-19 09:50:56 +01:00
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
2018-04-04 13:37:06 +02:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
///
|
|
|
|
/// \file
|
|
|
|
/// Defines the abstract BenchmarkRunner class for measuring a certain execution
|
|
|
|
/// property of instructions (e.g. latency).
|
|
|
|
///
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#ifndef LLVM_TOOLS_LLVM_EXEGESIS_BENCHMARKRUNNER_H
|
|
|
|
#define LLVM_TOOLS_LLVM_EXEGESIS_BENCHMARKRUNNER_H
|
|
|
|
|
2018-05-17 12:52:18 +02:00
|
|
|
#include "Assembler.h"
|
2018-09-13 09:40:53 +02:00
|
|
|
#include "BenchmarkCode.h"
|
2018-04-04 13:37:06 +02:00
|
|
|
#include "BenchmarkResult.h"
|
|
|
|
#include "LlvmState.h"
|
2018-06-20 10:52:30 +02:00
|
|
|
#include "MCInstrDescView.h"
|
2018-04-04 13:37:06 +02:00
|
|
|
#include "llvm/MC/MCInst.h"
|
|
|
|
#include "llvm/Support/Error.h"
|
2018-08-01 16:41:45 +02:00
|
|
|
#include <cstdlib>
|
|
|
|
#include <memory>
|
2018-04-04 13:37:06 +02:00
|
|
|
#include <vector>
|
|
|
|
|
2018-10-22 19:10:47 +02:00
|
|
|
namespace llvm {
|
2018-04-04 13:37:06 +02:00
|
|
|
namespace exegesis {
|
|
|
|
|
2018-06-13 15:24:41 +02:00
|
|
|
// A class representing failures that happened during Benchmark, they are used
|
|
|
|
// to report informations to the user.
|
|
|
|
class BenchmarkFailure : public llvm::StringError {
|
|
|
|
public:
|
|
|
|
BenchmarkFailure(const llvm::Twine &S);
|
|
|
|
};
|
|
|
|
|
2018-04-04 13:37:06 +02:00
|
|
|
// Common code for all benchmark modes.
|
|
|
|
class BenchmarkRunner {
|
|
|
|
public:
|
2018-07-05 14:26:12 +02:00
|
|
|
explicit BenchmarkRunner(const LLVMState &State,
|
|
|
|
InstructionBenchmark::ModeE Mode);
|
2018-04-04 13:37:06 +02:00
|
|
|
|
|
|
|
virtual ~BenchmarkRunner();
|
|
|
|
|
2018-09-13 09:40:53 +02:00
|
|
|
InstructionBenchmark runConfiguration(const BenchmarkCode &Configuration,
|
|
|
|
unsigned NumRepetitions) const;
|
2018-06-25 15:12:02 +02:00
|
|
|
|
2018-08-01 16:41:45 +02:00
|
|
|
// Scratch space to run instructions that touch memory.
|
|
|
|
struct ScratchSpace {
|
|
|
|
static constexpr const size_t kAlignment = 1024;
|
|
|
|
static constexpr const size_t kSize = 1 << 20; // 1MB.
|
|
|
|
ScratchSpace()
|
|
|
|
: UnalignedPtr(llvm::make_unique<char[]>(kSize + kAlignment)),
|
|
|
|
AlignedPtr(
|
|
|
|
UnalignedPtr.get() + kAlignment -
|
|
|
|
(reinterpret_cast<intptr_t>(UnalignedPtr.get()) % kAlignment)) {}
|
|
|
|
char *ptr() const { return AlignedPtr; }
|
|
|
|
void clear() { std::memset(ptr(), 0, kSize); }
|
|
|
|
|
|
|
|
private:
|
|
|
|
const std::unique_ptr<char[]> UnalignedPtr;
|
|
|
|
char *const AlignedPtr;
|
|
|
|
};
|
|
|
|
|
2018-10-17 17:04:15 +02:00
|
|
|
// A helper to measure counters while executing a function in a sandboxed
|
|
|
|
// context.
|
|
|
|
class FunctionExecutor {
|
|
|
|
public:
|
2018-10-18 04:06:16 +02:00
|
|
|
virtual ~FunctionExecutor();
|
2018-10-17 17:04:15 +02:00
|
|
|
virtual llvm::Expected<int64_t>
|
|
|
|
runAndMeasure(const char *Counters) const = 0;
|
|
|
|
};
|
|
|
|
|
2018-05-17 12:52:18 +02:00
|
|
|
protected:
|
|
|
|
const LLVMState &State;
|
2019-01-30 17:02:20 +01:00
|
|
|
const InstructionBenchmark::ModeE Mode;
|
2018-06-28 09:41:16 +02:00
|
|
|
|
2018-04-04 13:37:06 +02:00
|
|
|
private:
|
2018-10-17 17:04:15 +02:00
|
|
|
virtual llvm::Expected<std::vector<BenchmarkMeasure>>
|
|
|
|
runMeasurements(const FunctionExecutor &Executor) const = 0;
|
2018-06-26 10:49:30 +02:00
|
|
|
|
2018-05-17 12:52:18 +02:00
|
|
|
llvm::Expected<std::string>
|
2018-08-03 11:29:38 +02:00
|
|
|
writeObjectFile(const BenchmarkCode &Configuration,
|
2018-06-25 15:12:02 +02:00
|
|
|
llvm::ArrayRef<llvm::MCInst> Code) const;
|
2018-06-26 10:49:30 +02:00
|
|
|
|
2018-08-01 16:41:45 +02:00
|
|
|
|
|
|
|
const std::unique_ptr<ScratchSpace> Scratch;
|
2018-04-04 13:37:06 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace exegesis
|
2018-10-22 19:10:47 +02:00
|
|
|
} // namespace llvm
|
2018-04-04 13:37:06 +02:00
|
|
|
|
|
|
|
#endif // LLVM_TOOLS_LLVM_EXEGESIS_BENCHMARKRUNNER_H
|