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