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 | |
Clement Courbet | 0e69e2d | 2018-05-17 10:52:18 +0000 | [diff] [blame] | 22 | static bool HasUnknownOperand(const llvm::MCOperandInfo &OpInfo) { |
| 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. |
Clement Courbet | 0e69e2d | 2018-05-17 10:52:18 +0000 | [diff] [blame] | 27 | static bool HasMemoryOperand(const llvm::MCOperandInfo &OpInfo) { |
| 28 | return OpInfo.OperandType == llvm::MCOI::OPERAND_MEMORY; |
| 29 | } |
| 30 | |
| 31 | static bool IsInfeasible(const Instruction &Instruction, std::string &Error) { |
| 32 | const auto &MCInstrDesc = Instruction.Description; |
| 33 | if (MCInstrDesc.isPseudo()) { |
| 34 | Error = "is pseudo"; |
Clement Courbet | ac74acd | 2018-04-04 11:37:06 +0000 | [diff] [blame] | 35 | return true; |
Clement Courbet | ac74acd | 2018-04-04 11:37:06 +0000 | [diff] [blame] | 36 | } |
Clement Courbet | 0e69e2d | 2018-05-17 10:52:18 +0000 | [diff] [blame] | 37 | if (llvm::any_of(MCInstrDesc.operands(), HasUnknownOperand)) { |
| 38 | Error = "has unknown operands"; |
| 39 | return true; |
| 40 | } |
| 41 | if (llvm::any_of(MCInstrDesc.operands(), HasMemoryOperand)) { |
| 42 | Error = "has memory operands"; |
| 43 | return true; |
| 44 | } |
| 45 | return false; |
Clement Courbet | ac74acd | 2018-04-04 11:37:06 +0000 | [diff] [blame] | 46 | } |
| 47 | |
Clement Courbet | ac74acd | 2018-04-04 11:37:06 +0000 | [diff] [blame] | 48 | LatencyBenchmarkRunner::~LatencyBenchmarkRunner() = default; |
| 49 | |
Clement Courbet | 62b34fa | 2018-06-06 09:42:36 +0000 | [diff] [blame] | 50 | InstructionBenchmark::ModeE LatencyBenchmarkRunner::getMode() const { |
| 51 | return InstructionBenchmark::Latency; |
Clement Courbet | 2cb97b9 | 2018-06-04 11:43:40 +0000 | [diff] [blame] | 52 | } |
Clement Courbet | ac74acd | 2018-04-04 11:37:06 +0000 | [diff] [blame] | 53 | |
Guillaume Chatelet | b4f1582 | 2018-06-07 14:00:29 +0000 | [diff] [blame] | 54 | llvm::Expected<std::vector<BenchmarkConfiguration>> |
| 55 | LatencyBenchmarkRunner::createConfigurations(RegisterAliasingTrackerCache &RATC, |
| 56 | unsigned Opcode) const { |
Clement Courbet | 0e69e2d | 2018-05-17 10:52:18 +0000 | [diff] [blame] | 57 | const llvm::MCInstrDesc &MCInstrDesc = MCInstrInfo.get(Opcode); |
| 58 | const Instruction ThisInstruction(MCInstrDesc, RATC); |
Clement Courbet | ac74acd | 2018-04-04 11:37:06 +0000 | [diff] [blame] | 59 | |
Clement Courbet | 0e69e2d | 2018-05-17 10:52:18 +0000 | [diff] [blame] | 60 | std::string Error; |
| 61 | if (IsInfeasible(ThisInstruction, Error)) |
Guillaume Chatelet | b4f1582 | 2018-06-07 14:00:29 +0000 | [diff] [blame] | 62 | return llvm::make_error<llvm::StringError>( |
| 63 | llvm::Twine("Infeasible : ").concat(Error), |
| 64 | llvm::inconvertibleErrorCode()); |
Clement Courbet | 0e69e2d | 2018-05-17 10:52:18 +0000 | [diff] [blame] | 65 | |
Guillaume Chatelet | b4f1582 | 2018-06-07 14:00:29 +0000 | [diff] [blame] | 66 | BenchmarkConfiguration Conf; |
Clement Courbet | 0e69e2d | 2018-05-17 10:52:18 +0000 | [diff] [blame] | 67 | const AliasingConfigurations SelfAliasing(ThisInstruction, ThisInstruction); |
| 68 | if (!SelfAliasing.empty()) { |
| 69 | if (!SelfAliasing.hasImplicitAliasing()) { |
Guillaume Chatelet | b4f1582 | 2018-06-07 14:00:29 +0000 | [diff] [blame] | 70 | Conf.Info = "explicit self cycles, selecting one aliasing Conf."; |
Clement Courbet | 0e69e2d | 2018-05-17 10:52:18 +0000 | [diff] [blame] | 71 | setRandomAliasing(SelfAliasing); |
| 72 | } else { |
Guillaume Chatelet | b4f1582 | 2018-06-07 14:00:29 +0000 | [diff] [blame] | 73 | Conf.Info = "implicit Self cycles, picking random values."; |
Clement Courbet | 0e69e2d | 2018-05-17 10:52:18 +0000 | [diff] [blame] | 74 | } |
Guillaume Chatelet | b4f1582 | 2018-06-07 14:00:29 +0000 | [diff] [blame] | 75 | Conf.Snippet = {randomizeUnsetVariablesAndBuild(ThisInstruction)}; |
| 76 | return std::vector<BenchmarkConfiguration>{Conf}; |
Clement Courbet | ac74acd | 2018-04-04 11:37:06 +0000 | [diff] [blame] | 77 | } |
| 78 | |
Clement Courbet | 0e69e2d | 2018-05-17 10:52:18 +0000 | [diff] [blame] | 79 | // Let's try to create a dependency through another opcode. |
| 80 | std::vector<unsigned> Opcodes; |
| 81 | Opcodes.resize(MCInstrInfo.getNumOpcodes()); |
| 82 | std::iota(Opcodes.begin(), Opcodes.end(), 0U); |
| 83 | std::shuffle(Opcodes.begin(), Opcodes.end(), randomGenerator()); |
| 84 | for (const unsigned OtherOpcode : Opcodes) { |
| 85 | clearVariableAssignments(ThisInstruction); |
| 86 | if (OtherOpcode == Opcode) |
| 87 | continue; |
| 88 | const Instruction OtherInstruction(MCInstrInfo.get(OtherOpcode), RATC); |
| 89 | if (IsInfeasible(OtherInstruction, Error)) |
| 90 | continue; |
| 91 | const AliasingConfigurations Forward(ThisInstruction, OtherInstruction); |
| 92 | const AliasingConfigurations Back(OtherInstruction, ThisInstruction); |
| 93 | if (Forward.empty() || Back.empty()) |
| 94 | continue; |
| 95 | setRandomAliasing(Forward); |
| 96 | setRandomAliasing(Back); |
Guillaume Chatelet | b4f1582 | 2018-06-07 14:00:29 +0000 | [diff] [blame] | 97 | Conf.Info = llvm::Twine("creating cycle through ") |
| 98 | .concat(MCInstrInfo.getName(OtherOpcode)) |
| 99 | .concat(".") |
| 100 | .str(); |
| 101 | Conf.Snippet.push_back(randomizeUnsetVariablesAndBuild(ThisInstruction)); |
| 102 | Conf.Snippet.push_back(randomizeUnsetVariablesAndBuild(OtherInstruction)); |
| 103 | return std::vector<BenchmarkConfiguration>{Conf}; |
Clement Courbet | 0e69e2d | 2018-05-17 10:52:18 +0000 | [diff] [blame] | 104 | } |
| 105 | |
Guillaume Chatelet | b4f1582 | 2018-06-07 14:00:29 +0000 | [diff] [blame] | 106 | return llvm::make_error<llvm::StringError>( |
| 107 | "Infeasible : Didn't find any scheme to make the instruction serial", |
| 108 | llvm::inconvertibleErrorCode()); |
Clement Courbet | ac74acd | 2018-04-04 11:37:06 +0000 | [diff] [blame] | 109 | } |
| 110 | |
| 111 | std::vector<BenchmarkMeasure> |
Clement Courbet | 0e69e2d | 2018-05-17 10:52:18 +0000 | [diff] [blame] | 112 | LatencyBenchmarkRunner::runMeasurements(const ExecutableFunction &Function, |
Clement Courbet | ac74acd | 2018-04-04 11:37:06 +0000 | [diff] [blame] | 113 | const unsigned NumRepetitions) const { |
| 114 | // Cycle measurements include some overhead from the kernel. Repeat the |
| 115 | // measure several times and take the minimum value. |
| 116 | constexpr const int NumMeasurements = 30; |
| 117 | int64_t MinLatency = std::numeric_limits<int64_t>::max(); |
Clement Courbet | b449379 | 2018-04-10 08:16:37 +0000 | [diff] [blame] | 118 | const char *CounterName = State.getSubtargetInfo() |
| 119 | .getSchedModel() |
| 120 | .getExtraProcessorInfo() |
| 121 | .PfmCounters.CycleCounter; |
| 122 | if (!CounterName) |
| 123 | llvm::report_fatal_error("sched model does not define a cycle counter"); |
| 124 | const pfm::PerfEvent CyclesPerfEvent(CounterName); |
Clement Courbet | ac74acd | 2018-04-04 11:37:06 +0000 | [diff] [blame] | 125 | if (!CyclesPerfEvent.valid()) |
Clement Courbet | b449379 | 2018-04-10 08:16:37 +0000 | [diff] [blame] | 126 | llvm::report_fatal_error("invalid perf event"); |
Clement Courbet | ac74acd | 2018-04-04 11:37:06 +0000 | [diff] [blame] | 127 | for (size_t I = 0; I < NumMeasurements; ++I) { |
| 128 | pfm::Counter Counter(CyclesPerfEvent); |
| 129 | Counter.start(); |
| 130 | Function(); |
| 131 | Counter.stop(); |
| 132 | const int64_t Value = Counter.read(); |
| 133 | if (Value < MinLatency) |
| 134 | MinLatency = Value; |
| 135 | } |
Clement Courbet | 3f20fee | 2018-04-04 12:01:38 +0000 | [diff] [blame] | 136 | return {{"latency", static_cast<double>(MinLatency) / NumRepetitions, ""}}; |
Clement Courbet | ac74acd | 2018-04-04 11:37:06 +0000 | [diff] [blame] | 137 | } |
| 138 | |
| 139 | } // namespace exegesis |