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