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