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