Clement Courbet | d939f6d | 2018-09-13 07:40:53 +0000 | [diff] [blame] | 1 | //===-- SnippetGenerator.cpp ------------------------------------*- C++ -*-===// |
| 2 | // |
Chandler Carruth | 2946cd7 | 2019-01-19 08:50:56 +0000 | [diff] [blame] | 3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | // See https://llvm.org/LICENSE.txt for license information. |
| 5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
Clement Courbet | d939f6d | 2018-09-13 07:40:53 +0000 | [diff] [blame] | 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | |
| 9 | #include <array> |
| 10 | #include <string> |
| 11 | |
| 12 | #include "Assembler.h" |
Clement Courbet | 03a3d29 | 2019-09-30 13:53:50 +0000 | [diff] [blame^] | 13 | #include "Error.h" |
Clement Courbet | d939f6d | 2018-09-13 07:40:53 +0000 | [diff] [blame] | 14 | #include "MCInstrDescView.h" |
| 15 | #include "SnippetGenerator.h" |
Roman Lebedev | 404bdb1 | 2019-04-06 14:16:26 +0000 | [diff] [blame] | 16 | #include "Target.h" |
Clement Courbet | d939f6d | 2018-09-13 07:40:53 +0000 | [diff] [blame] | 17 | #include "llvm/ADT/StringExtras.h" |
| 18 | #include "llvm/ADT/StringRef.h" |
| 19 | #include "llvm/ADT/Twine.h" |
| 20 | #include "llvm/Support/FileSystem.h" |
| 21 | #include "llvm/Support/FormatVariadic.h" |
| 22 | #include "llvm/Support/Program.h" |
| 23 | |
Fangrui Song | 32401af | 2018-10-22 17:10:47 +0000 | [diff] [blame] | 24 | namespace llvm { |
Clement Courbet | d939f6d | 2018-09-13 07:40:53 +0000 | [diff] [blame] | 25 | namespace exegesis { |
| 26 | |
Guillaume Chatelet | fcbb6f3 | 2018-10-17 11:37:28 +0000 | [diff] [blame] | 27 | std::vector<CodeTemplate> getSingleton(CodeTemplate &&CT) { |
Guillaume Chatelet | 296a862 | 2018-10-15 09:09:19 +0000 | [diff] [blame] | 28 | std::vector<CodeTemplate> Result; |
| 29 | Result.push_back(std::move(CT)); |
| 30 | return Result; |
| 31 | } |
| 32 | |
Clement Courbet | d939f6d | 2018-09-13 07:40:53 +0000 | [diff] [blame] | 33 | SnippetGeneratorFailure::SnippetGeneratorFailure(const llvm::Twine &S) |
| 34 | : llvm::StringError(S, llvm::inconvertibleErrorCode()) {} |
| 35 | |
Guillaume Chatelet | ee9c2a17 | 2018-10-10 14:22:48 +0000 | [diff] [blame] | 36 | SnippetGenerator::SnippetGenerator(const LLVMState &State) : State(State) {} |
Clement Courbet | d939f6d | 2018-09-13 07:40:53 +0000 | [diff] [blame] | 37 | |
| 38 | SnippetGenerator::~SnippetGenerator() = default; |
| 39 | |
| 40 | llvm::Expected<std::vector<BenchmarkCode>> |
Clement Courbet | 9431b72 | 2019-09-27 12:56:24 +0000 | [diff] [blame] | 41 | SnippetGenerator::generateConfigurations( |
| 42 | const Instruction &Instr, const llvm::BitVector &ExtraForbiddenRegs) const { |
Clement Courbet | 8ef97e1 | 2019-09-27 08:04:10 +0000 | [diff] [blame] | 43 | llvm::BitVector ForbiddenRegs = State.getRATC().reservedRegisters(); |
Clement Courbet | 9431b72 | 2019-09-27 12:56:24 +0000 | [diff] [blame] | 44 | ForbiddenRegs |= ExtraForbiddenRegs; |
Clement Courbet | 8ef97e1 | 2019-09-27 08:04:10 +0000 | [diff] [blame] | 45 | // If the instruction has memory registers, prevent the generator from |
| 46 | // using the scratch register and its aliasing registers. |
| 47 | if (Instr.hasMemoryOperands()) { |
| 48 | const auto &ET = State.getExegesisTarget(); |
| 49 | unsigned ScratchSpacePointerInReg = |
| 50 | ET.getScratchMemoryRegister(State.getTargetMachine().getTargetTriple()); |
| 51 | if (ScratchSpacePointerInReg == 0) |
Clement Courbet | 03a3d29 | 2019-09-30 13:53:50 +0000 | [diff] [blame^] | 52 | return make_error<Failure>( |
Clement Courbet | 8ef97e1 | 2019-09-27 08:04:10 +0000 | [diff] [blame] | 53 | "Infeasible : target does not support memory instructions"); |
| 54 | const auto &ScratchRegAliases = |
| 55 | State.getRATC().getRegister(ScratchSpacePointerInReg).aliasedBits(); |
| 56 | // If the instruction implicitly writes to ScratchSpacePointerInReg , abort. |
| 57 | // FIXME: We could make a copy of the scratch register. |
| 58 | for (const auto &Op : Instr.Operands) { |
| 59 | if (Op.isDef() && Op.isImplicitReg() && |
| 60 | ScratchRegAliases.test(Op.getImplicitReg())) |
Clement Courbet | 03a3d29 | 2019-09-30 13:53:50 +0000 | [diff] [blame^] | 61 | return make_error<Failure>( |
Clement Courbet | 8ef97e1 | 2019-09-27 08:04:10 +0000 | [diff] [blame] | 62 | "Infeasible : memory instruction uses scratch memory register"); |
| 63 | } |
| 64 | ForbiddenRegs |= ScratchRegAliases; |
| 65 | } |
| 66 | |
| 67 | if (auto E = generateCodeTemplates(Instr, ForbiddenRegs)) { |
Clement Courbet | d939f6d | 2018-09-13 07:40:53 +0000 | [diff] [blame] | 68 | std::vector<BenchmarkCode> Output; |
Guillaume Chatelet | 296a862 | 2018-10-15 09:09:19 +0000 | [diff] [blame] | 69 | for (CodeTemplate &CT : E.get()) { |
Guillaume Chatelet | 296a862 | 2018-10-15 09:09:19 +0000 | [diff] [blame] | 70 | // TODO: Generate as many BenchmarkCode as needed. |
| 71 | { |
| 72 | BenchmarkCode BC; |
| 73 | BC.Info = CT.Info; |
| 74 | for (InstructionTemplate &IT : CT.Instructions) { |
Roman Lebedev | 404bdb1 | 2019-04-06 14:16:26 +0000 | [diff] [blame] | 75 | randomizeUnsetVariables(State.getExegesisTarget(), ForbiddenRegs, IT); |
Guillaume Chatelet | 296a862 | 2018-10-15 09:09:19 +0000 | [diff] [blame] | 76 | BC.Instructions.push_back(IT.build()); |
| 77 | } |
| 78 | if (CT.ScratchSpacePointerInReg) |
| 79 | BC.LiveIns.push_back(CT.ScratchSpacePointerInReg); |
| 80 | BC.RegisterInitialValues = |
Clement Courbet | 0d79aaf | 2018-11-08 12:09:45 +0000 | [diff] [blame] | 81 | computeRegisterInitialValues(CT.Instructions); |
Guillaume Chatelet | 296a862 | 2018-10-15 09:09:19 +0000 | [diff] [blame] | 82 | Output.push_back(std::move(BC)); |
Clement Courbet | d939f6d | 2018-09-13 07:40:53 +0000 | [diff] [blame] | 83 | } |
Clement Courbet | d939f6d | 2018-09-13 07:40:53 +0000 | [diff] [blame] | 84 | } |
| 85 | return Output; |
| 86 | } else |
| 87 | return E.takeError(); |
| 88 | } |
| 89 | |
Guillaume Chatelet | c96a97b | 2018-09-20 12:22:18 +0000 | [diff] [blame] | 90 | std::vector<RegisterValue> SnippetGenerator::computeRegisterInitialValues( |
Guillaume Chatelet | 70ac019 | 2018-09-27 09:23:04 +0000 | [diff] [blame] | 91 | const std::vector<InstructionTemplate> &Instructions) const { |
Clement Courbet | d939f6d | 2018-09-13 07:40:53 +0000 | [diff] [blame] | 92 | // Collect all register uses and create an assignment for each of them. |
| 93 | // Ignore memory operands which are handled separately. |
| 94 | // Loop invariant: DefinedRegs[i] is true iif it has been set at least once |
| 95 | // before the current instruction. |
Guillaume Chatelet | ee9c2a17 | 2018-10-10 14:22:48 +0000 | [diff] [blame] | 96 | llvm::BitVector DefinedRegs = State.getRATC().emptyRegisters(); |
Guillaume Chatelet | c96a97b | 2018-09-20 12:22:18 +0000 | [diff] [blame] | 97 | std::vector<RegisterValue> RIV; |
Guillaume Chatelet | 70ac019 | 2018-09-27 09:23:04 +0000 | [diff] [blame] | 98 | for (const InstructionTemplate &IT : Instructions) { |
Clement Courbet | d939f6d | 2018-09-13 07:40:53 +0000 | [diff] [blame] | 99 | // Returns the register that this Operand sets or uses, or 0 if this is not |
| 100 | // a register. |
Guillaume Chatelet | 70ac019 | 2018-09-27 09:23:04 +0000 | [diff] [blame] | 101 | const auto GetOpReg = [&IT](const Operand &Op) -> unsigned { |
Guillaume Chatelet | 09c2839 | 2018-10-09 08:59:10 +0000 | [diff] [blame] | 102 | if (Op.isMemory()) |
Clement Courbet | d939f6d | 2018-09-13 07:40:53 +0000 | [diff] [blame] | 103 | return 0; |
Guillaume Chatelet | 09c2839 | 2018-10-09 08:59:10 +0000 | [diff] [blame] | 104 | if (Op.isImplicitReg()) |
| 105 | return Op.getImplicitReg(); |
| 106 | if (Op.isExplicit() && IT.getValueFor(Op).isReg()) |
Guillaume Chatelet | 70ac019 | 2018-09-27 09:23:04 +0000 | [diff] [blame] | 107 | return IT.getValueFor(Op).getReg(); |
Clement Courbet | d939f6d | 2018-09-13 07:40:53 +0000 | [diff] [blame] | 108 | return 0; |
| 109 | }; |
| 110 | // Collect used registers that have never been def'ed. |
Guillaume Chatelet | 70ac019 | 2018-09-27 09:23:04 +0000 | [diff] [blame] | 111 | for (const Operand &Op : IT.Instr.Operands) { |
Guillaume Chatelet | 09c2839 | 2018-10-09 08:59:10 +0000 | [diff] [blame] | 112 | if (Op.isUse()) { |
Clement Courbet | d939f6d | 2018-09-13 07:40:53 +0000 | [diff] [blame] | 113 | const unsigned Reg = GetOpReg(Op); |
| 114 | if (Reg > 0 && !DefinedRegs.test(Reg)) { |
Clement Courbet | 54c2fa1 | 2018-11-08 12:37:56 +0000 | [diff] [blame] | 115 | RIV.push_back(RegisterValue::zero(Reg)); |
Clement Courbet | d939f6d | 2018-09-13 07:40:53 +0000 | [diff] [blame] | 116 | DefinedRegs.set(Reg); |
| 117 | } |
| 118 | } |
| 119 | } |
| 120 | // Mark defs as having been def'ed. |
Guillaume Chatelet | 70ac019 | 2018-09-27 09:23:04 +0000 | [diff] [blame] | 121 | for (const Operand &Op : IT.Instr.Operands) { |
Guillaume Chatelet | 09c2839 | 2018-10-09 08:59:10 +0000 | [diff] [blame] | 122 | if (Op.isDef()) { |
Clement Courbet | d939f6d | 2018-09-13 07:40:53 +0000 | [diff] [blame] | 123 | const unsigned Reg = GetOpReg(Op); |
| 124 | if (Reg > 0) |
| 125 | DefinedRegs.set(Reg); |
| 126 | } |
| 127 | } |
| 128 | } |
Guillaume Chatelet | c96a97b | 2018-09-20 12:22:18 +0000 | [diff] [blame] | 129 | return RIV; |
Clement Courbet | d939f6d | 2018-09-13 07:40:53 +0000 | [diff] [blame] | 130 | } |
| 131 | |
Guillaume Chatelet | 296a862 | 2018-10-15 09:09:19 +0000 | [diff] [blame] | 132 | llvm::Expected<std::vector<CodeTemplate>> |
| 133 | generateSelfAliasingCodeTemplates(const Instruction &Instr) { |
Clement Courbet | d939f6d | 2018-09-13 07:40:53 +0000 | [diff] [blame] | 134 | const AliasingConfigurations SelfAliasing(Instr, Instr); |
Guillaume Chatelet | 296a862 | 2018-10-15 09:09:19 +0000 | [diff] [blame] | 135 | if (SelfAliasing.empty()) |
Clement Courbet | d939f6d | 2018-09-13 07:40:53 +0000 | [diff] [blame] | 136 | return llvm::make_error<SnippetGeneratorFailure>("empty self aliasing"); |
Guillaume Chatelet | 296a862 | 2018-10-15 09:09:19 +0000 | [diff] [blame] | 137 | std::vector<CodeTemplate> Result; |
| 138 | Result.emplace_back(); |
| 139 | CodeTemplate &CT = Result.back(); |
Guillaume Chatelet | 70ac019 | 2018-09-27 09:23:04 +0000 | [diff] [blame] | 140 | InstructionTemplate IT(Instr); |
Clement Courbet | d939f6d | 2018-09-13 07:40:53 +0000 | [diff] [blame] | 141 | if (SelfAliasing.hasImplicitAliasing()) { |
| 142 | CT.Info = "implicit Self cycles, picking random values."; |
| 143 | } else { |
| 144 | CT.Info = "explicit self cycles, selecting one aliasing Conf."; |
| 145 | // This is a self aliasing instruction so defs and uses are from the same |
Guillaume Chatelet | 70ac019 | 2018-09-27 09:23:04 +0000 | [diff] [blame] | 146 | // instance, hence twice IT in the following call. |
| 147 | setRandomAliasing(SelfAliasing, IT, IT); |
Clement Courbet | d939f6d | 2018-09-13 07:40:53 +0000 | [diff] [blame] | 148 | } |
Guillaume Chatelet | 70ac019 | 2018-09-27 09:23:04 +0000 | [diff] [blame] | 149 | CT.Instructions.push_back(std::move(IT)); |
Guillaume Chatelet | a384949 | 2018-10-15 09:21:21 +0000 | [diff] [blame] | 150 | return std::move(Result); |
Clement Courbet | d939f6d | 2018-09-13 07:40:53 +0000 | [diff] [blame] | 151 | } |
| 152 | |
Guillaume Chatelet | 296a862 | 2018-10-15 09:09:19 +0000 | [diff] [blame] | 153 | llvm::Expected<std::vector<CodeTemplate>> |
| 154 | generateUnconstrainedCodeTemplates(const Instruction &Instr, |
| 155 | llvm::StringRef Msg) { |
| 156 | std::vector<CodeTemplate> Result; |
| 157 | Result.emplace_back(); |
| 158 | CodeTemplate &CT = Result.back(); |
Clement Courbet | d939f6d | 2018-09-13 07:40:53 +0000 | [diff] [blame] | 159 | CT.Info = llvm::formatv("{0}, repeating an unconstrained assignment", Msg); |
| 160 | CT.Instructions.emplace_back(Instr); |
Guillaume Chatelet | a384949 | 2018-10-15 09:21:21 +0000 | [diff] [blame] | 161 | return std::move(Result); |
Clement Courbet | d939f6d | 2018-09-13 07:40:53 +0000 | [diff] [blame] | 162 | } |
Guillaume Chatelet | 70ac019 | 2018-09-27 09:23:04 +0000 | [diff] [blame] | 163 | |
Guillaume Chatelet | 415b2fb | 2018-10-01 12:19:10 +0000 | [diff] [blame] | 164 | std::mt19937 &randomGenerator() { |
| 165 | static std::random_device RandomDevice; |
| 166 | static std::mt19937 RandomGenerator(RandomDevice()); |
| 167 | return RandomGenerator; |
| 168 | } |
| 169 | |
Roman Lebedev | a822358 | 2019-04-08 10:11:00 +0000 | [diff] [blame] | 170 | size_t randomIndex(size_t Max) { |
| 171 | std::uniform_int_distribution<> Distribution(0, Max); |
Guillaume Chatelet | 415b2fb | 2018-10-01 12:19:10 +0000 | [diff] [blame] | 172 | return Distribution(randomGenerator()); |
| 173 | } |
| 174 | |
| 175 | template <typename C> |
| 176 | static auto randomElement(const C &Container) -> decltype(Container[0]) { |
Roman Lebedev | a822358 | 2019-04-08 10:11:00 +0000 | [diff] [blame] | 177 | assert(!Container.empty() && |
| 178 | "Can't pick a random element from an empty container)"); |
| 179 | return Container[randomIndex(Container.size() - 1)]; |
Guillaume Chatelet | 415b2fb | 2018-10-01 12:19:10 +0000 | [diff] [blame] | 180 | } |
| 181 | |
Guillaume Chatelet | 415b2fb | 2018-10-01 12:19:10 +0000 | [diff] [blame] | 182 | static void setRegisterOperandValue(const RegisterOperandAssignment &ROV, |
| 183 | InstructionTemplate &IB) { |
| 184 | assert(ROV.Op); |
Guillaume Chatelet | 09c2839 | 2018-10-09 08:59:10 +0000 | [diff] [blame] | 185 | if (ROV.Op->isExplicit()) { |
Guillaume Chatelet | 415b2fb | 2018-10-01 12:19:10 +0000 | [diff] [blame] | 186 | auto &AssignedValue = IB.getValueFor(*ROV.Op); |
| 187 | if (AssignedValue.isValid()) { |
| 188 | assert(AssignedValue.isReg() && AssignedValue.getReg() == ROV.Reg); |
| 189 | return; |
| 190 | } |
| 191 | AssignedValue = llvm::MCOperand::createReg(ROV.Reg); |
| 192 | } else { |
Guillaume Chatelet | 09c2839 | 2018-10-09 08:59:10 +0000 | [diff] [blame] | 193 | assert(ROV.Op->isImplicitReg()); |
| 194 | assert(ROV.Reg == ROV.Op->getImplicitReg()); |
Guillaume Chatelet | 415b2fb | 2018-10-01 12:19:10 +0000 | [diff] [blame] | 195 | } |
| 196 | } |
| 197 | |
| 198 | size_t randomBit(const llvm::BitVector &Vector) { |
| 199 | assert(Vector.any()); |
| 200 | auto Itr = Vector.set_bits_begin(); |
Roman Lebedev | a822358 | 2019-04-08 10:11:00 +0000 | [diff] [blame] | 201 | for (size_t I = randomIndex(Vector.count() - 1); I != 0; --I) |
Guillaume Chatelet | 415b2fb | 2018-10-01 12:19:10 +0000 | [diff] [blame] | 202 | ++Itr; |
| 203 | return *Itr; |
| 204 | } |
| 205 | |
| 206 | void setRandomAliasing(const AliasingConfigurations &AliasingConfigurations, |
| 207 | InstructionTemplate &DefIB, InstructionTemplate &UseIB) { |
| 208 | assert(!AliasingConfigurations.empty()); |
| 209 | assert(!AliasingConfigurations.hasImplicitAliasing()); |
| 210 | const auto &RandomConf = randomElement(AliasingConfigurations.Configurations); |
| 211 | setRegisterOperandValue(randomElement(RandomConf.Defs), DefIB); |
| 212 | setRegisterOperandValue(randomElement(RandomConf.Uses), UseIB); |
| 213 | } |
| 214 | |
Roman Lebedev | 404bdb1 | 2019-04-06 14:16:26 +0000 | [diff] [blame] | 215 | void randomizeUnsetVariables(const ExegesisTarget &Target, |
| 216 | const llvm::BitVector &ForbiddenRegs, |
Guillaume Chatelet | 415b2fb | 2018-10-01 12:19:10 +0000 | [diff] [blame] | 217 | InstructionTemplate &IT) { |
| 218 | for (const Variable &Var : IT.Instr.Variables) { |
| 219 | llvm::MCOperand &AssignedValue = IT.getValueFor(Var); |
| 220 | if (!AssignedValue.isValid()) |
Roman Lebedev | 404bdb1 | 2019-04-06 14:16:26 +0000 | [diff] [blame] | 221 | Target.randomizeMCOperand(IT.Instr, Var, AssignedValue, ForbiddenRegs); |
Guillaume Chatelet | 415b2fb | 2018-10-01 12:19:10 +0000 | [diff] [blame] | 222 | } |
| 223 | } |
| 224 | |
Clement Courbet | d939f6d | 2018-09-13 07:40:53 +0000 | [diff] [blame] | 225 | } // namespace exegesis |
Fangrui Song | 32401af | 2018-10-22 17:10:47 +0000 | [diff] [blame] | 226 | } // namespace llvm |