Clement Courbet | ac74acd | 2018-04-04 11:37:06 +0000 | [diff] [blame] | 1 | //===-- Latency.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 "Latency.h" |
Clement Courbet | 0e69e2d | 2018-05-17 10:52:18 +0000 | [diff] [blame] | 11 | |
| 12 | #include "Assembler.h" |
| 13 | #include "BenchmarkRunner.h" |
| 14 | #include "MCInstrDescView.h" |
Clement Courbet | ac74acd | 2018-04-04 11:37:06 +0000 | [diff] [blame] | 15 | #include "PerfHelper.h" |
Clement Courbet | 0e69e2d | 2018-05-17 10:52:18 +0000 | [diff] [blame] | 16 | #include "llvm/ADT/STLExtras.h" |
| 17 | #include "llvm/MC/MCInst.h" |
| 18 | #include "llvm/MC/MCInstBuilder.h" |
Clement Courbet | ac74acd | 2018-04-04 11:37:06 +0000 | [diff] [blame] | 19 | |
| 20 | namespace exegesis { |
| 21 | |
Guillaume Chatelet | c9f727b | 2018-06-13 13:24:41 +0000 | [diff] [blame^] | 22 | static bool hasUnknownOperand(const llvm::MCOperandInfo &OpInfo) { |
Clement Courbet | 0e69e2d | 2018-05-17 10:52:18 +0000 | [diff] [blame] | 23 | return OpInfo.OperandType == llvm::MCOI::OPERAND_UNKNOWN; |
| 24 | } |
| 25 | |
Clement Courbet | ac74acd | 2018-04-04 11:37:06 +0000 | [diff] [blame] | 26 | // FIXME: Handle memory, see PR36905. |
Guillaume Chatelet | c9f727b | 2018-06-13 13:24:41 +0000 | [diff] [blame^] | 27 | static bool hasMemoryOperand(const llvm::MCOperandInfo &OpInfo) { |
Clement Courbet | 0e69e2d | 2018-05-17 10:52:18 +0000 | [diff] [blame] | 28 | return OpInfo.OperandType == llvm::MCOI::OPERAND_MEMORY; |
| 29 | } |
| 30 | |
Clement Courbet | ac74acd | 2018-04-04 11:37:06 +0000 | [diff] [blame] | 31 | LatencyBenchmarkRunner::~LatencyBenchmarkRunner() = default; |
| 32 | |
Clement Courbet | 62b34fa | 2018-06-06 09:42:36 +0000 | [diff] [blame] | 33 | InstructionBenchmark::ModeE LatencyBenchmarkRunner::getMode() const { |
| 34 | return InstructionBenchmark::Latency; |
Clement Courbet | 2cb97b9 | 2018-06-04 11:43:40 +0000 | [diff] [blame] | 35 | } |
Clement Courbet | ac74acd | 2018-04-04 11:37:06 +0000 | [diff] [blame] | 36 | |
Guillaume Chatelet | c9f727b | 2018-06-13 13:24:41 +0000 | [diff] [blame^] | 37 | llvm::Error LatencyBenchmarkRunner::isInfeasible( |
| 38 | const llvm::MCInstrDesc &MCInstrDesc) const { |
| 39 | if (MCInstrDesc.isPseudo()) |
| 40 | return llvm::make_error<BenchmarkFailure>("Infeasible : is pseudo"); |
| 41 | if (llvm::any_of(MCInstrDesc.operands(), hasUnknownOperand)) |
| 42 | return llvm::make_error<BenchmarkFailure>( |
| 43 | "Infeasible : has unknown operands"); |
| 44 | if (llvm::any_of(MCInstrDesc.operands(), hasMemoryOperand)) |
| 45 | return llvm::make_error<BenchmarkFailure>( |
| 46 | "Infeasible : has memory operands"); |
| 47 | return llvm::Error::success(); |
| 48 | } |
Clement Courbet | ac74acd | 2018-04-04 11:37:06 +0000 | [diff] [blame] | 49 | |
Guillaume Chatelet | c9f727b | 2018-06-13 13:24:41 +0000 | [diff] [blame^] | 50 | llvm::Expected<BenchmarkConfiguration> |
| 51 | LatencyBenchmarkRunner::generateSelfAliasingConfiguration( |
| 52 | const Instruction &Instr, |
| 53 | const AliasingConfigurations &SelfAliasing) const { |
Guillaume Chatelet | b4f1582 | 2018-06-07 14:00:29 +0000 | [diff] [blame] | 54 | BenchmarkConfiguration Conf; |
Guillaume Chatelet | c9f727b | 2018-06-13 13:24:41 +0000 | [diff] [blame^] | 55 | InstructionInstance II(Instr); |
| 56 | if (SelfAliasing.hasImplicitAliasing()) { |
| 57 | Conf.Info = "implicit Self cycles, picking random values."; |
| 58 | } else { |
| 59 | Conf.Info = "explicit self cycles, selecting one aliasing Conf."; |
| 60 | // This is a self aliasing instruction so defs and uses are from the same |
| 61 | // instance, hence twice II in the following call. |
| 62 | setRandomAliasing(SelfAliasing, II, II); |
Clement Courbet | ac74acd | 2018-04-04 11:37:06 +0000 | [diff] [blame] | 63 | } |
Guillaume Chatelet | c9f727b | 2018-06-13 13:24:41 +0000 | [diff] [blame^] | 64 | Conf.Snippet = {II.randomizeUnsetVariablesAndBuild()}; |
| 65 | return Conf; |
| 66 | } |
Clement Courbet | ac74acd | 2018-04-04 11:37:06 +0000 | [diff] [blame] | 67 | |
Guillaume Chatelet | c9f727b | 2018-06-13 13:24:41 +0000 | [diff] [blame^] | 68 | llvm::Expected<BenchmarkConfiguration> |
| 69 | LatencyBenchmarkRunner::generateTwoInstructionConfiguration( |
| 70 | const Instruction &Instr, |
| 71 | const AliasingConfigurations &SelfAliasing) const { |
Clement Courbet | 0e69e2d | 2018-05-17 10:52:18 +0000 | [diff] [blame] | 72 | std::vector<unsigned> Opcodes; |
| 73 | Opcodes.resize(MCInstrInfo.getNumOpcodes()); |
| 74 | std::iota(Opcodes.begin(), Opcodes.end(), 0U); |
| 75 | std::shuffle(Opcodes.begin(), Opcodes.end(), randomGenerator()); |
| 76 | for (const unsigned OtherOpcode : Opcodes) { |
Guillaume Chatelet | c9f727b | 2018-06-13 13:24:41 +0000 | [diff] [blame^] | 77 | if (OtherOpcode == Instr.Description.Opcode) |
Clement Courbet | 0e69e2d | 2018-05-17 10:52:18 +0000 | [diff] [blame] | 78 | continue; |
Guillaume Chatelet | c9f727b | 2018-06-13 13:24:41 +0000 | [diff] [blame^] | 79 | const auto &OtherInstrDesc = MCInstrInfo.get(OtherOpcode); |
| 80 | if (auto E = isInfeasible(OtherInstrDesc)) { |
| 81 | llvm::consumeError(std::move(E)); |
Clement Courbet | 0e69e2d | 2018-05-17 10:52:18 +0000 | [diff] [blame] | 82 | continue; |
Guillaume Chatelet | c9f727b | 2018-06-13 13:24:41 +0000 | [diff] [blame^] | 83 | } |
| 84 | const Instruction OtherInstr(OtherInstrDesc, RATC); |
| 85 | const AliasingConfigurations Forward(Instr, OtherInstr); |
| 86 | const AliasingConfigurations Back(OtherInstr, Instr); |
Clement Courbet | 0e69e2d | 2018-05-17 10:52:18 +0000 | [diff] [blame] | 87 | if (Forward.empty() || Back.empty()) |
| 88 | continue; |
Guillaume Chatelet | c9f727b | 2018-06-13 13:24:41 +0000 | [diff] [blame^] | 89 | InstructionInstance ThisII(Instr); |
| 90 | InstructionInstance OtherII(OtherInstr); |
| 91 | if (!Forward.hasImplicitAliasing()) |
| 92 | setRandomAliasing(Forward, ThisII, OtherII); |
| 93 | if (!Back.hasImplicitAliasing()) |
| 94 | setRandomAliasing(Back, OtherII, ThisII); |
| 95 | BenchmarkConfiguration Conf; |
Guillaume Chatelet | b4f1582 | 2018-06-07 14:00:29 +0000 | [diff] [blame] | 96 | Conf.Info = llvm::Twine("creating cycle through ") |
| 97 | .concat(MCInstrInfo.getName(OtherOpcode)) |
| 98 | .concat(".") |
| 99 | .str(); |
Guillaume Chatelet | c9f727b | 2018-06-13 13:24:41 +0000 | [diff] [blame^] | 100 | Conf.Snippet.push_back(ThisII.randomizeUnsetVariablesAndBuild()); |
| 101 | Conf.Snippet.push_back(OtherII.randomizeUnsetVariablesAndBuild()); |
| 102 | return Conf; |
Clement Courbet | 0e69e2d | 2018-05-17 10:52:18 +0000 | [diff] [blame] | 103 | } |
Guillaume Chatelet | c9f727b | 2018-06-13 13:24:41 +0000 | [diff] [blame^] | 104 | return llvm::make_error<BenchmarkFailure>( |
| 105 | "Infeasible : Didn't find any scheme to make the instruction serial"); |
| 106 | } |
Clement Courbet | 0e69e2d | 2018-05-17 10:52:18 +0000 | [diff] [blame] | 107 | |
Guillaume Chatelet | c9f727b | 2018-06-13 13:24:41 +0000 | [diff] [blame^] | 108 | llvm::Expected<BenchmarkConfiguration> |
| 109 | LatencyBenchmarkRunner::generateConfiguration(unsigned Opcode) const { |
| 110 | const auto &InstrDesc = MCInstrInfo.get(Opcode); |
| 111 | if (auto E = isInfeasible(InstrDesc)) |
| 112 | return std::move(E); |
| 113 | const Instruction Instr(InstrDesc, RATC); |
| 114 | const AliasingConfigurations SelfAliasing(Instr, Instr); |
| 115 | if (SelfAliasing.empty()) { |
| 116 | // No self aliasing, trying to create a dependency through another opcode. |
| 117 | return generateTwoInstructionConfiguration(Instr, SelfAliasing); |
| 118 | } else { |
| 119 | return generateSelfAliasingConfiguration(Instr, SelfAliasing); |
| 120 | } |
| 121 | } |
| 122 | |
| 123 | llvm::Expected<std::vector<BenchmarkConfiguration>> |
| 124 | LatencyBenchmarkRunner::createConfigurations(unsigned Opcode) const { |
| 125 | if (auto E = generateConfiguration(Opcode)) |
| 126 | return std::vector<BenchmarkConfiguration>{E.get()}; |
| 127 | else |
| 128 | return E.takeError(); |
Clement Courbet | ac74acd | 2018-04-04 11:37:06 +0000 | [diff] [blame] | 129 | } |
| 130 | |
| 131 | std::vector<BenchmarkMeasure> |
Clement Courbet | 0e69e2d | 2018-05-17 10:52:18 +0000 | [diff] [blame] | 132 | LatencyBenchmarkRunner::runMeasurements(const ExecutableFunction &Function, |
Clement Courbet | ac74acd | 2018-04-04 11:37:06 +0000 | [diff] [blame] | 133 | const unsigned NumRepetitions) const { |
| 134 | // Cycle measurements include some overhead from the kernel. Repeat the |
| 135 | // measure several times and take the minimum value. |
| 136 | constexpr const int NumMeasurements = 30; |
| 137 | int64_t MinLatency = std::numeric_limits<int64_t>::max(); |
Clement Courbet | b449379 | 2018-04-10 08:16:37 +0000 | [diff] [blame] | 138 | const char *CounterName = State.getSubtargetInfo() |
| 139 | .getSchedModel() |
| 140 | .getExtraProcessorInfo() |
| 141 | .PfmCounters.CycleCounter; |
| 142 | if (!CounterName) |
| 143 | llvm::report_fatal_error("sched model does not define a cycle counter"); |
| 144 | const pfm::PerfEvent CyclesPerfEvent(CounterName); |
Clement Courbet | ac74acd | 2018-04-04 11:37:06 +0000 | [diff] [blame] | 145 | if (!CyclesPerfEvent.valid()) |
Clement Courbet | b449379 | 2018-04-10 08:16:37 +0000 | [diff] [blame] | 146 | llvm::report_fatal_error("invalid perf event"); |
Clement Courbet | ac74acd | 2018-04-04 11:37:06 +0000 | [diff] [blame] | 147 | for (size_t I = 0; I < NumMeasurements; ++I) { |
| 148 | pfm::Counter Counter(CyclesPerfEvent); |
| 149 | Counter.start(); |
| 150 | Function(); |
| 151 | Counter.stop(); |
| 152 | const int64_t Value = Counter.read(); |
| 153 | if (Value < MinLatency) |
| 154 | MinLatency = Value; |
| 155 | } |
Clement Courbet | 3f20fee | 2018-04-04 12:01:38 +0000 | [diff] [blame] | 156 | return {{"latency", static_cast<double>(MinLatency) / NumRepetitions, ""}}; |
Clement Courbet | ac74acd | 2018-04-04 11:37:06 +0000 | [diff] [blame] | 157 | } |
| 158 | |
| 159 | } // namespace exegesis |