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 | |
Guillaume Chatelet | c9f727b | 2018-06-13 13:24:41 +0000 | [diff] [blame] | 34 | llvm::Error LatencyBenchmarkRunner::isInfeasible( |
| 35 | const llvm::MCInstrDesc &MCInstrDesc) const { |
Guillaume Chatelet | c9f727b | 2018-06-13 13:24:41 +0000 | [diff] [blame] | 36 | if (llvm::any_of(MCInstrDesc.operands(), hasUnknownOperand)) |
| 37 | return llvm::make_error<BenchmarkFailure>( |
| 38 | "Infeasible : has unknown operands"); |
| 39 | if (llvm::any_of(MCInstrDesc.operands(), hasMemoryOperand)) |
| 40 | return llvm::make_error<BenchmarkFailure>( |
| 41 | "Infeasible : has memory operands"); |
| 42 | return llvm::Error::success(); |
| 43 | } |
Clement Courbet | ac74acd | 2018-04-04 11:37:06 +0000 | [diff] [blame] | 44 | |
Guillaume Chatelet | e60866a | 2018-08-03 09:29:38 +0000 | [diff] [blame] | 45 | llvm::Expected<CodeTemplate> |
Guillaume Chatelet | ef6cef5 | 2018-06-20 08:52:30 +0000 | [diff] [blame] | 46 | LatencyBenchmarkRunner::generateTwoInstructionPrototype( |
Clement Courbet | 717c976 | 2018-06-28 07:41:16 +0000 | [diff] [blame] | 47 | const Instruction &Instr) const { |
Clement Courbet | 0e69e2d | 2018-05-17 10:52:18 +0000 | [diff] [blame] | 48 | std::vector<unsigned> Opcodes; |
Clement Courbet | 0e8bf4e | 2018-06-25 13:44:27 +0000 | [diff] [blame] | 49 | Opcodes.resize(State.getInstrInfo().getNumOpcodes()); |
Clement Courbet | 0e69e2d | 2018-05-17 10:52:18 +0000 | [diff] [blame] | 50 | std::iota(Opcodes.begin(), Opcodes.end(), 0U); |
| 51 | std::shuffle(Opcodes.begin(), Opcodes.end(), randomGenerator()); |
| 52 | for (const unsigned OtherOpcode : Opcodes) { |
Guillaume Chatelet | ef6cef5 | 2018-06-20 08:52:30 +0000 | [diff] [blame] | 53 | if (OtherOpcode == Instr.Description->Opcode) |
Clement Courbet | 0e69e2d | 2018-05-17 10:52:18 +0000 | [diff] [blame] | 54 | continue; |
Clement Courbet | 0e8bf4e | 2018-06-25 13:44:27 +0000 | [diff] [blame] | 55 | const auto &OtherInstrDesc = State.getInstrInfo().get(OtherOpcode); |
Guillaume Chatelet | c9f727b | 2018-06-13 13:24:41 +0000 | [diff] [blame] | 56 | if (auto E = isInfeasible(OtherInstrDesc)) { |
| 57 | llvm::consumeError(std::move(E)); |
Clement Courbet | 0e69e2d | 2018-05-17 10:52:18 +0000 | [diff] [blame] | 58 | continue; |
Guillaume Chatelet | c9f727b | 2018-06-13 13:24:41 +0000 | [diff] [blame] | 59 | } |
| 60 | const Instruction OtherInstr(OtherInstrDesc, RATC); |
| 61 | const AliasingConfigurations Forward(Instr, OtherInstr); |
| 62 | const AliasingConfigurations Back(OtherInstr, Instr); |
Clement Courbet | 0e69e2d | 2018-05-17 10:52:18 +0000 | [diff] [blame] | 63 | if (Forward.empty() || Back.empty()) |
| 64 | continue; |
Guillaume Chatelet | 171f3f4 | 2018-08-02 11:12:02 +0000 | [diff] [blame] | 65 | InstructionBuilder ThisIB(Instr); |
| 66 | InstructionBuilder OtherIB(OtherInstr); |
Guillaume Chatelet | c9f727b | 2018-06-13 13:24:41 +0000 | [diff] [blame] | 67 | if (!Forward.hasImplicitAliasing()) |
Guillaume Chatelet | 171f3f4 | 2018-08-02 11:12:02 +0000 | [diff] [blame] | 68 | setRandomAliasing(Forward, ThisIB, OtherIB); |
Guillaume Chatelet | c9f727b | 2018-06-13 13:24:41 +0000 | [diff] [blame] | 69 | if (!Back.hasImplicitAliasing()) |
Guillaume Chatelet | 171f3f4 | 2018-08-02 11:12:02 +0000 | [diff] [blame] | 70 | setRandomAliasing(Back, OtherIB, ThisIB); |
Guillaume Chatelet | e60866a | 2018-08-03 09:29:38 +0000 | [diff] [blame] | 71 | CodeTemplate CT; |
| 72 | CT.Info = llvm::formatv("creating cycle through {0}.", |
| 73 | State.getInstrInfo().getName(OtherOpcode)); |
| 74 | CT.Instructions.push_back(std::move(ThisIB)); |
| 75 | CT.Instructions.push_back(std::move(OtherIB)); |
| 76 | return std::move(CT); |
Clement Courbet | 0e69e2d | 2018-05-17 10:52:18 +0000 | [diff] [blame] | 77 | } |
Guillaume Chatelet | c9f727b | 2018-06-13 13:24:41 +0000 | [diff] [blame] | 78 | return llvm::make_error<BenchmarkFailure>( |
| 79 | "Infeasible : Didn't find any scheme to make the instruction serial"); |
| 80 | } |
Clement Courbet | 0e69e2d | 2018-05-17 10:52:18 +0000 | [diff] [blame] | 81 | |
Guillaume Chatelet | e60866a | 2018-08-03 09:29:38 +0000 | [diff] [blame] | 82 | llvm::Expected<CodeTemplate> |
| 83 | LatencyBenchmarkRunner::generateCodeTemplate(unsigned Opcode) const { |
Clement Courbet | 0e8bf4e | 2018-06-25 13:44:27 +0000 | [diff] [blame] | 84 | const auto &InstrDesc = State.getInstrInfo().get(Opcode); |
Guillaume Chatelet | c9f727b | 2018-06-13 13:24:41 +0000 | [diff] [blame] | 85 | if (auto E = isInfeasible(InstrDesc)) |
| 86 | return std::move(E); |
| 87 | const Instruction Instr(InstrDesc, RATC); |
Guillaume Chatelet | e60866a | 2018-08-03 09:29:38 +0000 | [diff] [blame] | 88 | if (auto CT = generateSelfAliasingCodeTemplate(Instr)) |
| 89 | return CT; |
Clement Courbet | 717c976 | 2018-06-28 07:41:16 +0000 | [diff] [blame] | 90 | else |
Guillaume Chatelet | e60866a | 2018-08-03 09:29:38 +0000 | [diff] [blame] | 91 | llvm::consumeError(CT.takeError()); |
Clement Courbet | 717c976 | 2018-06-28 07:41:16 +0000 | [diff] [blame] | 92 | // No self aliasing, trying to create a dependency through another opcode. |
| 93 | return generateTwoInstructionPrototype(Instr); |
Guillaume Chatelet | c9f727b | 2018-06-13 13:24:41 +0000 | [diff] [blame] | 94 | } |
| 95 | |
John Brawn | 8fc5ec7 | 2018-07-02 13:14:49 +0000 | [diff] [blame] | 96 | const char *LatencyBenchmarkRunner::getCounterName() const { |
| 97 | if (!State.getSubtargetInfo().getSchedModel().hasExtraProcessorInfo()) |
| 98 | llvm::report_fatal_error("sched model is missing extra processor info!"); |
| 99 | const char *CounterName = State.getSubtargetInfo() |
| 100 | .getSchedModel() |
| 101 | .getExtraProcessorInfo() |
| 102 | .PfmCounters.CycleCounter; |
| 103 | if (!CounterName) |
| 104 | llvm::report_fatal_error("sched model does not define a cycle counter"); |
| 105 | return CounterName; |
| 106 | } |
| 107 | |
Clement Courbet | ac74acd | 2018-04-04 11:37:06 +0000 | [diff] [blame] | 108 | std::vector<BenchmarkMeasure> |
Clement Courbet | 0e69e2d | 2018-05-17 10:52:18 +0000 | [diff] [blame] | 109 | LatencyBenchmarkRunner::runMeasurements(const ExecutableFunction &Function, |
Guillaume Chatelet | fb94354 | 2018-08-01 14:41:45 +0000 | [diff] [blame] | 110 | ScratchSpace &Scratch, |
Clement Courbet | ac74acd | 2018-04-04 11:37:06 +0000 | [diff] [blame] | 111 | const unsigned NumRepetitions) const { |
| 112 | // Cycle measurements include some overhead from the kernel. Repeat the |
| 113 | // measure several times and take the minimum value. |
| 114 | constexpr const int NumMeasurements = 30; |
| 115 | int64_t MinLatency = std::numeric_limits<int64_t>::max(); |
John Brawn | 8fc5ec7 | 2018-07-02 13:14:49 +0000 | [diff] [blame] | 116 | const char *CounterName = getCounterName(); |
Clement Courbet | b449379 | 2018-04-10 08:16:37 +0000 | [diff] [blame] | 117 | if (!CounterName) |
John Brawn | 8fc5ec7 | 2018-07-02 13:14:49 +0000 | [diff] [blame] | 118 | llvm::report_fatal_error("could not determine cycle counter name"); |
Clement Courbet | b449379 | 2018-04-10 08:16:37 +0000 | [diff] [blame] | 119 | const pfm::PerfEvent CyclesPerfEvent(CounterName); |
Clement Courbet | ac74acd | 2018-04-04 11:37:06 +0000 | [diff] [blame] | 120 | if (!CyclesPerfEvent.valid()) |
Clement Courbet | b449379 | 2018-04-10 08:16:37 +0000 | [diff] [blame] | 121 | llvm::report_fatal_error("invalid perf event"); |
Clement Courbet | ac74acd | 2018-04-04 11:37:06 +0000 | [diff] [blame] | 122 | for (size_t I = 0; I < NumMeasurements; ++I) { |
| 123 | pfm::Counter Counter(CyclesPerfEvent); |
Guillaume Chatelet | fb94354 | 2018-08-01 14:41:45 +0000 | [diff] [blame] | 124 | Scratch.clear(); |
Clement Courbet | ac74acd | 2018-04-04 11:37:06 +0000 | [diff] [blame] | 125 | Counter.start(); |
Guillaume Chatelet | fb94354 | 2018-08-01 14:41:45 +0000 | [diff] [blame] | 126 | Function(Scratch.ptr()); |
Clement Courbet | ac74acd | 2018-04-04 11:37:06 +0000 | [diff] [blame] | 127 | Counter.stop(); |
| 128 | const int64_t Value = Counter.read(); |
| 129 | if (Value < MinLatency) |
| 130 | MinLatency = Value; |
| 131 | } |
Clement Courbet | 3f20fee | 2018-04-04 12:01:38 +0000 | [diff] [blame] | 132 | return {{"latency", static_cast<double>(MinLatency) / NumRepetitions, ""}}; |
Clement Courbet | ac74acd | 2018-04-04 11:37:06 +0000 | [diff] [blame] | 133 | } |
| 134 | |
| 135 | } // namespace exegesis |