Clement Courbet | d939f6d | 2018-09-13 07:40:53 +0000 | [diff] [blame] | 1 | //===-- SnippetGenerator.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 <array> |
| 11 | #include <string> |
| 12 | |
| 13 | #include "Assembler.h" |
| 14 | #include "MCInstrDescView.h" |
| 15 | #include "SnippetGenerator.h" |
| 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 | |
| 23 | namespace exegesis { |
| 24 | |
Guillaume Chatelet | 296a862 | 2018-10-15 09:09:19 +0000 | [diff] [blame^] | 25 | std::vector<CodeTemplate> getSingleton(CodeTemplate &CT) { |
| 26 | std::vector<CodeTemplate> Result; |
| 27 | Result.push_back(std::move(CT)); |
| 28 | return Result; |
| 29 | } |
| 30 | |
Clement Courbet | d939f6d | 2018-09-13 07:40:53 +0000 | [diff] [blame] | 31 | SnippetGeneratorFailure::SnippetGeneratorFailure(const llvm::Twine &S) |
| 32 | : llvm::StringError(S, llvm::inconvertibleErrorCode()) {} |
| 33 | |
Guillaume Chatelet | ee9c2a17 | 2018-10-10 14:22:48 +0000 | [diff] [blame] | 34 | SnippetGenerator::SnippetGenerator(const LLVMState &State) : State(State) {} |
Clement Courbet | d939f6d | 2018-09-13 07:40:53 +0000 | [diff] [blame] | 35 | |
| 36 | SnippetGenerator::~SnippetGenerator() = default; |
| 37 | |
| 38 | llvm::Expected<std::vector<BenchmarkCode>> |
Guillaume Chatelet | 9b59238 | 2018-10-10 14:57:32 +0000 | [diff] [blame] | 39 | SnippetGenerator::generateConfigurations(const Instruction &Instr) const { |
Guillaume Chatelet | 296a862 | 2018-10-15 09:09:19 +0000 | [diff] [blame^] | 40 | if (auto E = generateCodeTemplates(Instr)) { |
Guillaume Chatelet | ee9c2a17 | 2018-10-10 14:22:48 +0000 | [diff] [blame] | 41 | const auto &RATC = State.getRATC(); |
Clement Courbet | d939f6d | 2018-09-13 07:40:53 +0000 | [diff] [blame] | 42 | std::vector<BenchmarkCode> Output; |
Guillaume Chatelet | 296a862 | 2018-10-15 09:09:19 +0000 | [diff] [blame^] | 43 | for (CodeTemplate &CT : E.get()) { |
| 44 | const llvm::BitVector &ForbiddenRegs = |
| 45 | CT.ScratchSpacePointerInReg |
| 46 | ? RATC.getRegister(CT.ScratchSpacePointerInReg).aliasedBits() |
| 47 | : RATC.emptyRegisters(); |
| 48 | // TODO: Generate as many BenchmarkCode as needed. |
| 49 | { |
| 50 | BenchmarkCode BC; |
| 51 | BC.Info = CT.Info; |
| 52 | for (InstructionTemplate &IT : CT.Instructions) { |
| 53 | randomizeUnsetVariables(ForbiddenRegs, IT); |
| 54 | BC.Instructions.push_back(IT.build()); |
| 55 | } |
| 56 | if (CT.ScratchSpacePointerInReg) |
| 57 | BC.LiveIns.push_back(CT.ScratchSpacePointerInReg); |
| 58 | BC.RegisterInitialValues = |
| 59 | computeRegisterInitialValues(CT.Instructions); |
| 60 | Output.push_back(std::move(BC)); |
Clement Courbet | d939f6d | 2018-09-13 07:40:53 +0000 | [diff] [blame] | 61 | } |
Clement Courbet | d939f6d | 2018-09-13 07:40:53 +0000 | [diff] [blame] | 62 | } |
| 63 | return Output; |
| 64 | } else |
| 65 | return E.takeError(); |
| 66 | } |
| 67 | |
Guillaume Chatelet | c96a97b | 2018-09-20 12:22:18 +0000 | [diff] [blame] | 68 | std::vector<RegisterValue> SnippetGenerator::computeRegisterInitialValues( |
Guillaume Chatelet | 70ac019 | 2018-09-27 09:23:04 +0000 | [diff] [blame] | 69 | const std::vector<InstructionTemplate> &Instructions) const { |
Clement Courbet | d939f6d | 2018-09-13 07:40:53 +0000 | [diff] [blame] | 70 | // Collect all register uses and create an assignment for each of them. |
| 71 | // Ignore memory operands which are handled separately. |
| 72 | // Loop invariant: DefinedRegs[i] is true iif it has been set at least once |
| 73 | // before the current instruction. |
Guillaume Chatelet | ee9c2a17 | 2018-10-10 14:22:48 +0000 | [diff] [blame] | 74 | llvm::BitVector DefinedRegs = State.getRATC().emptyRegisters(); |
Guillaume Chatelet | c96a97b | 2018-09-20 12:22:18 +0000 | [diff] [blame] | 75 | std::vector<RegisterValue> RIV; |
Guillaume Chatelet | 70ac019 | 2018-09-27 09:23:04 +0000 | [diff] [blame] | 76 | for (const InstructionTemplate &IT : Instructions) { |
Clement Courbet | d939f6d | 2018-09-13 07:40:53 +0000 | [diff] [blame] | 77 | // Returns the register that this Operand sets or uses, or 0 if this is not |
| 78 | // a register. |
Guillaume Chatelet | 70ac019 | 2018-09-27 09:23:04 +0000 | [diff] [blame] | 79 | const auto GetOpReg = [&IT](const Operand &Op) -> unsigned { |
Guillaume Chatelet | 09c2839 | 2018-10-09 08:59:10 +0000 | [diff] [blame] | 80 | if (Op.isMemory()) |
Clement Courbet | d939f6d | 2018-09-13 07:40:53 +0000 | [diff] [blame] | 81 | return 0; |
Guillaume Chatelet | 09c2839 | 2018-10-09 08:59:10 +0000 | [diff] [blame] | 82 | if (Op.isImplicitReg()) |
| 83 | return Op.getImplicitReg(); |
| 84 | if (Op.isExplicit() && IT.getValueFor(Op).isReg()) |
Guillaume Chatelet | 70ac019 | 2018-09-27 09:23:04 +0000 | [diff] [blame] | 85 | return IT.getValueFor(Op).getReg(); |
Clement Courbet | d939f6d | 2018-09-13 07:40:53 +0000 | [diff] [blame] | 86 | return 0; |
| 87 | }; |
| 88 | // Collect used registers that have never been def'ed. |
Guillaume Chatelet | 70ac019 | 2018-09-27 09:23:04 +0000 | [diff] [blame] | 89 | for (const Operand &Op : IT.Instr.Operands) { |
Guillaume Chatelet | 09c2839 | 2018-10-09 08:59:10 +0000 | [diff] [blame] | 90 | if (Op.isUse()) { |
Clement Courbet | d939f6d | 2018-09-13 07:40:53 +0000 | [diff] [blame] | 91 | const unsigned Reg = GetOpReg(Op); |
| 92 | if (Reg > 0 && !DefinedRegs.test(Reg)) { |
Guillaume Chatelet | c96a97b | 2018-09-20 12:22:18 +0000 | [diff] [blame] | 93 | RIV.push_back(RegisterValue{Reg, llvm::APInt()}); |
Clement Courbet | d939f6d | 2018-09-13 07:40:53 +0000 | [diff] [blame] | 94 | DefinedRegs.set(Reg); |
| 95 | } |
| 96 | } |
| 97 | } |
| 98 | // Mark defs as having been def'ed. |
Guillaume Chatelet | 70ac019 | 2018-09-27 09:23:04 +0000 | [diff] [blame] | 99 | for (const Operand &Op : IT.Instr.Operands) { |
Guillaume Chatelet | 09c2839 | 2018-10-09 08:59:10 +0000 | [diff] [blame] | 100 | if (Op.isDef()) { |
Clement Courbet | d939f6d | 2018-09-13 07:40:53 +0000 | [diff] [blame] | 101 | const unsigned Reg = GetOpReg(Op); |
| 102 | if (Reg > 0) |
| 103 | DefinedRegs.set(Reg); |
| 104 | } |
| 105 | } |
| 106 | } |
Guillaume Chatelet | c96a97b | 2018-09-20 12:22:18 +0000 | [diff] [blame] | 107 | return RIV; |
Clement Courbet | d939f6d | 2018-09-13 07:40:53 +0000 | [diff] [blame] | 108 | } |
| 109 | |
Guillaume Chatelet | 296a862 | 2018-10-15 09:09:19 +0000 | [diff] [blame^] | 110 | llvm::Expected<std::vector<CodeTemplate>> |
| 111 | generateSelfAliasingCodeTemplates(const Instruction &Instr) { |
Clement Courbet | d939f6d | 2018-09-13 07:40:53 +0000 | [diff] [blame] | 112 | const AliasingConfigurations SelfAliasing(Instr, Instr); |
Guillaume Chatelet | 296a862 | 2018-10-15 09:09:19 +0000 | [diff] [blame^] | 113 | if (SelfAliasing.empty()) |
Clement Courbet | d939f6d | 2018-09-13 07:40:53 +0000 | [diff] [blame] | 114 | return llvm::make_error<SnippetGeneratorFailure>("empty self aliasing"); |
Guillaume Chatelet | 296a862 | 2018-10-15 09:09:19 +0000 | [diff] [blame^] | 115 | std::vector<CodeTemplate> Result; |
| 116 | Result.emplace_back(); |
| 117 | CodeTemplate &CT = Result.back(); |
Guillaume Chatelet | 70ac019 | 2018-09-27 09:23:04 +0000 | [diff] [blame] | 118 | InstructionTemplate IT(Instr); |
Clement Courbet | d939f6d | 2018-09-13 07:40:53 +0000 | [diff] [blame] | 119 | if (SelfAliasing.hasImplicitAliasing()) { |
| 120 | CT.Info = "implicit Self cycles, picking random values."; |
| 121 | } else { |
| 122 | CT.Info = "explicit self cycles, selecting one aliasing Conf."; |
| 123 | // 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] | 124 | // instance, hence twice IT in the following call. |
| 125 | setRandomAliasing(SelfAliasing, IT, IT); |
Clement Courbet | d939f6d | 2018-09-13 07:40:53 +0000 | [diff] [blame] | 126 | } |
Guillaume Chatelet | 70ac019 | 2018-09-27 09:23:04 +0000 | [diff] [blame] | 127 | CT.Instructions.push_back(std::move(IT)); |
Guillaume Chatelet | 296a862 | 2018-10-15 09:09:19 +0000 | [diff] [blame^] | 128 | return Result; |
Clement Courbet | d939f6d | 2018-09-13 07:40:53 +0000 | [diff] [blame] | 129 | } |
| 130 | |
Guillaume Chatelet | 296a862 | 2018-10-15 09:09:19 +0000 | [diff] [blame^] | 131 | llvm::Expected<std::vector<CodeTemplate>> |
| 132 | generateUnconstrainedCodeTemplates(const Instruction &Instr, |
| 133 | llvm::StringRef Msg) { |
| 134 | std::vector<CodeTemplate> Result; |
| 135 | Result.emplace_back(); |
| 136 | CodeTemplate &CT = Result.back(); |
Clement Courbet | d939f6d | 2018-09-13 07:40:53 +0000 | [diff] [blame] | 137 | CT.Info = llvm::formatv("{0}, repeating an unconstrained assignment", Msg); |
| 138 | CT.Instructions.emplace_back(Instr); |
Guillaume Chatelet | 296a862 | 2018-10-15 09:09:19 +0000 | [diff] [blame^] | 139 | return Result; |
Clement Courbet | d939f6d | 2018-09-13 07:40:53 +0000 | [diff] [blame] | 140 | } |
Guillaume Chatelet | 70ac019 | 2018-09-27 09:23:04 +0000 | [diff] [blame] | 141 | |
Guillaume Chatelet | 415b2fb | 2018-10-01 12:19:10 +0000 | [diff] [blame] | 142 | std::mt19937 &randomGenerator() { |
| 143 | static std::random_device RandomDevice; |
| 144 | static std::mt19937 RandomGenerator(RandomDevice()); |
| 145 | return RandomGenerator; |
| 146 | } |
| 147 | |
| 148 | static size_t randomIndex(size_t Size) { |
| 149 | assert(Size > 0); |
| 150 | std::uniform_int_distribution<> Distribution(0, Size - 1); |
| 151 | return Distribution(randomGenerator()); |
| 152 | } |
| 153 | |
| 154 | template <typename C> |
| 155 | static auto randomElement(const C &Container) -> decltype(Container[0]) { |
| 156 | return Container[randomIndex(Container.size())]; |
| 157 | } |
| 158 | |
| 159 | static void randomize(const Instruction &Instr, const Variable &Var, |
| 160 | llvm::MCOperand &AssignedValue, |
| 161 | const llvm::BitVector &ForbiddenRegs) { |
Guillaume Chatelet | 09c2839 | 2018-10-09 08:59:10 +0000 | [diff] [blame] | 162 | const Operand &Op = Instr.getPrimaryOperand(Var); |
| 163 | switch (Op.getExplicitOperandInfo().OperandType) { |
Guillaume Chatelet | 415b2fb | 2018-10-01 12:19:10 +0000 | [diff] [blame] | 164 | case llvm::MCOI::OperandType::OPERAND_IMMEDIATE: |
| 165 | // FIXME: explore immediate values too. |
| 166 | AssignedValue = llvm::MCOperand::createImm(1); |
| 167 | break; |
| 168 | case llvm::MCOI::OperandType::OPERAND_REGISTER: { |
Guillaume Chatelet | 09c2839 | 2018-10-09 08:59:10 +0000 | [diff] [blame] | 169 | assert(Op.isReg()); |
| 170 | auto AllowedRegs = Op.getRegisterAliasing().sourceBits(); |
Guillaume Chatelet | 415b2fb | 2018-10-01 12:19:10 +0000 | [diff] [blame] | 171 | assert(AllowedRegs.size() == ForbiddenRegs.size()); |
| 172 | for (auto I : ForbiddenRegs.set_bits()) |
| 173 | AllowedRegs.reset(I); |
| 174 | AssignedValue = llvm::MCOperand::createReg(randomBit(AllowedRegs)); |
| 175 | break; |
| 176 | } |
| 177 | default: |
| 178 | break; |
| 179 | } |
| 180 | } |
| 181 | |
| 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(); |
| 201 | for (size_t I = randomIndex(Vector.count()); I != 0; --I) |
| 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 | |
| 215 | void randomizeUnsetVariables(const llvm::BitVector &ForbiddenRegs, |
| 216 | InstructionTemplate &IT) { |
| 217 | for (const Variable &Var : IT.Instr.Variables) { |
| 218 | llvm::MCOperand &AssignedValue = IT.getValueFor(Var); |
| 219 | if (!AssignedValue.isValid()) |
| 220 | randomize(IT.Instr, Var, AssignedValue, ForbiddenRegs); |
| 221 | } |
| 222 | } |
| 223 | |
Clement Courbet | d939f6d | 2018-09-13 07:40:53 +0000 | [diff] [blame] | 224 | } // namespace exegesis |