2018-06-19 19:28:59 +08:00
|
|
|
//===-- Target.h ------------------------------------------------*- C++ -*-===//
|
|
|
|
//
|
2019-01-19 16:50:56 +08: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-06-19 19:28:59 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
///
|
|
|
|
/// \file
|
|
|
|
///
|
|
|
|
/// Classes that handle the creation of target-specific objects. This is
|
2019-10-09 19:58:42 +08:00
|
|
|
/// similar to Target/TargetRegistry.
|
2018-06-19 19:28:59 +08:00
|
|
|
///
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#ifndef LLVM_TOOLS_LLVM_EXEGESIS_TARGET_H
|
|
|
|
#define LLVM_TOOLS_LLVM_EXEGESIS_TARGET_H
|
|
|
|
|
2018-06-26 16:49:30 +08:00
|
|
|
#include "BenchmarkResult.h"
|
|
|
|
#include "BenchmarkRunner.h"
|
2020-01-22 22:49:10 +08:00
|
|
|
#include "Error.h"
|
2018-06-26 16:49:30 +08:00
|
|
|
#include "LlvmState.h"
|
2020-05-28 06:06:40 +08:00
|
|
|
#include "PerfHelper.h"
|
2018-09-13 15:40:53 +08:00
|
|
|
#include "SnippetGenerator.h"
|
2018-06-19 19:28:59 +08:00
|
|
|
#include "llvm/ADT/Triple.h"
|
2018-06-20 19:54:35 +08:00
|
|
|
#include "llvm/CodeGen/TargetPassConfig.h"
|
2018-08-01 22:41:45 +08:00
|
|
|
#include "llvm/IR/CallingConv.h"
|
2018-06-20 19:54:35 +08:00
|
|
|
#include "llvm/IR/LegacyPassManager.h"
|
2018-06-25 21:12:02 +08:00
|
|
|
#include "llvm/MC/MCInst.h"
|
|
|
|
#include "llvm/MC/MCRegisterInfo.h"
|
2020-05-28 06:06:40 +08:00
|
|
|
#include "llvm/Support/Error.h"
|
2018-06-19 19:28:59 +08:00
|
|
|
|
2018-10-23 01:10:47 +08:00
|
|
|
namespace llvm {
|
2018-06-19 19:28:59 +08:00
|
|
|
namespace exegesis {
|
|
|
|
|
2018-10-25 15:44:01 +08:00
|
|
|
struct PfmCountersInfo {
|
|
|
|
// An optional name of a performance counter that can be used to measure
|
|
|
|
// cycles.
|
2018-10-25 18:45:38 +08:00
|
|
|
const char *CycleCounter;
|
2018-10-25 15:44:01 +08:00
|
|
|
|
|
|
|
// An optional name of a performance counter that can be used to measure
|
|
|
|
// uops.
|
2018-10-25 18:45:38 +08:00
|
|
|
const char *UopsCounter;
|
2018-10-25 15:44:01 +08:00
|
|
|
|
|
|
|
// An IssueCounter specifies how to measure uops issued to specific proc
|
|
|
|
// resources.
|
|
|
|
struct IssueCounter {
|
2018-10-25 18:45:38 +08:00
|
|
|
const char *Counter;
|
2018-10-25 15:44:01 +08:00
|
|
|
// The name of the ProcResource that this counter measures.
|
2018-10-25 18:45:38 +08:00
|
|
|
const char *ProcResName;
|
2018-10-25 15:44:01 +08:00
|
|
|
};
|
|
|
|
// An optional list of IssueCounters.
|
2018-10-25 18:45:38 +08:00
|
|
|
const IssueCounter *IssueCounters;
|
|
|
|
unsigned NumIssueCounters;
|
2018-10-25 15:44:01 +08:00
|
|
|
|
|
|
|
static const PfmCountersInfo Default;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct CpuAndPfmCounters {
|
2018-10-25 18:45:38 +08:00
|
|
|
const char *CpuName;
|
|
|
|
const PfmCountersInfo *PCI;
|
2019-10-09 19:58:42 +08:00
|
|
|
bool operator<(StringRef S) const { return StringRef(CpuName) < S; }
|
2018-10-25 15:44:01 +08:00
|
|
|
};
|
|
|
|
|
2018-06-19 19:28:59 +08:00
|
|
|
class ExegesisTarget {
|
|
|
|
public:
|
2019-10-09 19:58:42 +08:00
|
|
|
explicit ExegesisTarget(ArrayRef<CpuAndPfmCounters> CpuPfmCounters)
|
2018-10-25 15:44:01 +08:00
|
|
|
: CpuPfmCounters(CpuPfmCounters) {}
|
|
|
|
|
2020-05-28 06:06:40 +08:00
|
|
|
// Targets can use this to create target-specific perf counters.
|
|
|
|
virtual Expected<std::unique_ptr<pfm::Counter>>
|
2020-06-02 15:36:11 +08:00
|
|
|
createCounter(StringRef CounterName, const LLVMState &State) const;
|
2020-05-28 06:06:40 +08:00
|
|
|
|
2018-06-20 19:54:35 +08:00
|
|
|
// Targets can use this to add target-specific passes in assembleToStream();
|
2019-10-09 19:58:42 +08:00
|
|
|
virtual void addTargetSpecificPasses(PassManagerBase &PM) const {}
|
2018-06-20 19:54:35 +08:00
|
|
|
|
2018-09-18 23:38:16 +08:00
|
|
|
// Generates code to move a constant into a the given register.
|
2018-09-20 20:22:18 +08:00
|
|
|
// Precondition: Value must fit into Reg.
|
2019-10-09 19:58:42 +08:00
|
|
|
virtual std::vector<MCInst> setRegTo(const MCSubtargetInfo &STI, unsigned Reg,
|
|
|
|
const APInt &Value) const = 0;
|
2018-09-18 19:26:27 +08:00
|
|
|
|
|
|
|
// Returns the register pointing to scratch memory, or 0 if this target
|
|
|
|
// does not support memory operands. The benchmark function uses the
|
|
|
|
// default calling convention.
|
2019-10-09 19:58:42 +08:00
|
|
|
virtual unsigned getScratchMemoryRegister(const Triple &) const { return 0; }
|
2018-08-01 22:41:45 +08:00
|
|
|
|
|
|
|
// Fills memory operands with references to the address at [Reg] + Offset.
|
2018-09-27 17:23:04 +08:00
|
|
|
virtual void fillMemoryOperands(InstructionTemplate &IT, unsigned Reg,
|
2018-09-20 20:22:18 +08:00
|
|
|
unsigned Offset) const {
|
|
|
|
llvm_unreachable(
|
|
|
|
"fillMemoryOperands() requires getScratchMemoryRegister() > 0");
|
|
|
|
}
|
2018-08-01 22:41:45 +08:00
|
|
|
|
2019-09-27 20:56:24 +08:00
|
|
|
// Returns a counter usable as a loop counter.
|
2019-10-09 19:58:42 +08:00
|
|
|
virtual unsigned getLoopCounterRegister(const Triple &) const { return 0; }
|
2019-09-27 20:56:24 +08:00
|
|
|
|
|
|
|
// Adds the code to decrement the loop counter and
|
2019-10-03 15:56:56 +08:00
|
|
|
virtual void decrementLoopCounterAndJump(MachineBasicBlock &MBB,
|
|
|
|
MachineBasicBlock &TargetMBB,
|
2019-10-09 19:58:42 +08:00
|
|
|
const MCInstrInfo &MII) const {
|
2019-09-27 20:56:24 +08:00
|
|
|
llvm_unreachable("decrementLoopCounterAndBranch() requires "
|
|
|
|
"getLoopCounterRegister() > 0");
|
|
|
|
}
|
|
|
|
|
2019-03-26 23:44:57 +08:00
|
|
|
// Returns a list of unavailable registers.
|
|
|
|
// Targets can use this to prevent some registers to be automatically selected
|
|
|
|
// for use in snippets.
|
|
|
|
virtual ArrayRef<unsigned> getUnavailableRegisters() const { return {}; }
|
|
|
|
|
2018-08-01 22:41:45 +08:00
|
|
|
// Returns the maximum number of bytes a load/store instruction can access at
|
|
|
|
// once. This is typically the size of the largest register available on the
|
|
|
|
// processor. Note that this only used as a hint to generate independant
|
|
|
|
// load/stores to/from memory, so the exact returned value does not really
|
|
|
|
// matter as long as it's large enough.
|
2018-09-20 20:22:18 +08:00
|
|
|
virtual unsigned getMaxMemoryAccessSize() const { return 0; }
|
2018-08-01 22:41:45 +08:00
|
|
|
|
2019-04-06 22:16:26 +08:00
|
|
|
// Assigns a random operand of the right type to variable Var.
|
2020-01-22 22:49:10 +08:00
|
|
|
// The target is responsible for handling any operand starting from
|
|
|
|
// OPERAND_FIRST_TARGET.
|
|
|
|
virtual Error randomizeTargetMCOperand(const Instruction &Instr,
|
|
|
|
const Variable &Var,
|
|
|
|
MCOperand &AssignedValue,
|
|
|
|
const BitVector &ForbiddenRegs) const {
|
|
|
|
return make_error<Failure>(
|
|
|
|
"targets with target-specific operands should implement this");
|
|
|
|
}
|
2019-04-06 22:16:26 +08:00
|
|
|
|
2020-01-22 16:33:50 +08:00
|
|
|
// Returns true if this instruction is supported as a back-to-back
|
|
|
|
// instructions.
|
|
|
|
// FIXME: Eventually we should discover this dynamically.
|
|
|
|
virtual bool allowAsBackToBack(const Instruction &Instr) const {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
[llvm-exegesis] Exploring X86::OperandType::OPERAND_COND_CODE
Summary:
Currently, we only have nice exploration for LEA instruction,
while for the rest, we rely on `randomizeUnsetVariables()`
to sometimes generate something interesting.
While that works, it isn't very reliable in coverage :)
Here, i'm making an assumption that while we may want to explore
multi-instruction configs, we are most interested in the
characteristics of the main instruction we were asked about.
Which we can do, by taking the existing `randomizeMCOperand()`,
and turning it on it's head - instead of relying on it to randomly fill
one of the interesting values, let's pregenerate all the possible interesting
values for the variable, and then generate as much `InstructionTemplate`
combinations of these possible values for variables as needed/possible.
Of course, that requires invasive changes to no longer pass just the
naked `Instruction`, but sometimes partially filled `InstructionTemplate`.
As it can be seen from the test, this allows us to explore
`X86::OperandType::OPERAND_COND_CODE` for instructions
that take such an operand.
I'm hoping this will greatly simplify exploration.
Reviewers: courbet, gchatelet
Reviewed By: gchatelet
Subscribers: orodley, mgorny, sdardis, tschuett, jrtc27, atanasyan, mstojanovic, andreadb, RKSimon, llvm-commits
Tags: #llvm
Differential Revision: https://reviews.llvm.org/D74156
2020-02-13 01:54:39 +08:00
|
|
|
// For some instructions, it is interesting to measure how it's performance
|
|
|
|
// characteristics differ depending on it's operands.
|
|
|
|
// This allows us to produce all the interesting variants.
|
|
|
|
virtual std::vector<InstructionTemplate>
|
|
|
|
generateInstructionVariants(const Instruction &Instr,
|
|
|
|
unsigned MaxConfigsPerOpcode) const {
|
|
|
|
// By default, we're happy with whatever randomizer will give us.
|
|
|
|
return {&Instr};
|
|
|
|
}
|
|
|
|
|
2020-10-02 00:12:34 +08:00
|
|
|
// Checks hardware and software support for current benchmark mode.
|
|
|
|
// Returns an error if the target host does not have support to run the
|
|
|
|
// benchmark.
|
|
|
|
virtual Error checkFeatureSupport() const { return Error::success(); }
|
|
|
|
|
2018-09-13 15:40:53 +08:00
|
|
|
// Creates a snippet generator for the given mode.
|
|
|
|
std::unique_ptr<SnippetGenerator>
|
|
|
|
createSnippetGenerator(InstructionBenchmark::ModeE Mode,
|
2019-10-08 22:30:24 +08:00
|
|
|
const LLVMState &State,
|
|
|
|
const SnippetGenerator::Options &Opts) const;
|
2018-06-26 16:49:30 +08:00
|
|
|
// Creates a benchmark runner for the given mode.
|
2020-06-25 23:15:16 +08:00
|
|
|
Expected<std::unique_ptr<BenchmarkRunner>> createBenchmarkRunner(
|
|
|
|
InstructionBenchmark::ModeE Mode, const LLVMState &State,
|
|
|
|
InstructionBenchmark::ResultAggregationModeE ResultAggMode =
|
|
|
|
InstructionBenchmark::Min) const;
|
2018-06-26 16:49:30 +08:00
|
|
|
|
2018-06-19 19:28:59 +08:00
|
|
|
// Returns the ExegesisTarget for the given triple or nullptr if the target
|
|
|
|
// does not exist.
|
2019-10-09 19:58:42 +08:00
|
|
|
static const ExegesisTarget *lookup(Triple TT);
|
2018-06-26 16:49:30 +08:00
|
|
|
// Returns the default (unspecialized) ExegesisTarget.
|
|
|
|
static const ExegesisTarget &getDefault();
|
2018-06-19 19:28:59 +08:00
|
|
|
// Registers a target. Not thread safe.
|
|
|
|
static void registerTarget(ExegesisTarget *T);
|
|
|
|
|
2018-06-19 19:58:10 +08:00
|
|
|
virtual ~ExegesisTarget();
|
2018-06-19 19:28:59 +08:00
|
|
|
|
2018-10-25 15:44:01 +08:00
|
|
|
// Returns the Pfm counters for the given CPU (or the default if no pfm
|
|
|
|
// counters are defined for this CPU).
|
2019-10-09 19:58:42 +08:00
|
|
|
const PfmCountersInfo &getPfmCounters(StringRef CpuName) const;
|
2018-10-25 15:44:01 +08:00
|
|
|
|
2020-11-04 15:56:27 +08:00
|
|
|
// Saves the CPU state that needs to be preserved when running a benchmark,
|
|
|
|
// and returns and RAII object that restores the state on destruction.
|
|
|
|
// By default no state is preserved.
|
|
|
|
struct SavedState {
|
|
|
|
virtual ~SavedState();
|
|
|
|
};
|
|
|
|
virtual std::unique_ptr<SavedState> withSavedState() const {
|
|
|
|
return std::make_unique<SavedState>();
|
|
|
|
}
|
|
|
|
|
2018-06-19 19:28:59 +08:00
|
|
|
private:
|
2019-10-09 19:58:42 +08:00
|
|
|
virtual bool matchesArch(Triple::ArchType Arch) const = 0;
|
2018-06-26 16:49:30 +08:00
|
|
|
|
2018-09-13 15:40:53 +08:00
|
|
|
// Targets can implement their own snippet generators/benchmarks runners by
|
2018-06-26 16:49:30 +08:00
|
|
|
// implementing these.
|
2020-01-17 21:28:54 +08:00
|
|
|
std::unique_ptr<SnippetGenerator> virtual createSerialSnippetGenerator(
|
2019-10-08 22:30:24 +08:00
|
|
|
const LLVMState &State, const SnippetGenerator::Options &Opts) const;
|
2020-01-17 21:28:54 +08:00
|
|
|
std::unique_ptr<SnippetGenerator> virtual createParallelSnippetGenerator(
|
2019-10-08 22:30:24 +08:00
|
|
|
const LLVMState &State, const SnippetGenerator::Options &Opts) const;
|
2018-06-26 16:49:30 +08:00
|
|
|
std::unique_ptr<BenchmarkRunner> virtual createLatencyBenchmarkRunner(
|
2020-06-25 23:15:16 +08:00
|
|
|
const LLVMState &State, InstructionBenchmark::ModeE Mode,
|
|
|
|
InstructionBenchmark::ResultAggregationModeE ResultAggMode) const;
|
2018-06-26 16:49:30 +08:00
|
|
|
std::unique_ptr<BenchmarkRunner> virtual createUopsBenchmarkRunner(
|
2020-06-25 23:15:16 +08:00
|
|
|
const LLVMState &State,
|
|
|
|
InstructionBenchmark::ResultAggregationModeE ResultAggMode) const;
|
2018-06-26 16:49:30 +08:00
|
|
|
|
2018-06-25 19:22:23 +08:00
|
|
|
const ExegesisTarget *Next = nullptr;
|
2019-10-09 19:58:42 +08:00
|
|
|
const ArrayRef<CpuAndPfmCounters> CpuPfmCounters;
|
2018-06-19 19:28:59 +08:00
|
|
|
};
|
|
|
|
|
2018-06-25 19:22:23 +08:00
|
|
|
} // namespace exegesis
|
2018-10-23 01:10:47 +08:00
|
|
|
} // namespace llvm
|
2018-06-19 19:28:59 +08:00
|
|
|
|
|
|
|
#endif // LLVM_TOOLS_LLVM_EXEGESIS_TARGET_H
|