blob: d1f168fe43bfdb5f5996bae2344926bd31c36ab2 [file] [log] [blame]
Clement Courbetd939f6d2018-09-13 07:40:53 +00001//===-- SnippetGenerator.cpp ------------------------------------*- C++ -*-===//
2//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// 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 Courbetd939f6d2018-09-13 07:40:53 +00006//
7//===----------------------------------------------------------------------===//
8
9#include <array>
10#include <string>
11
12#include "Assembler.h"
Clement Courbet03a3d292019-09-30 13:53:50 +000013#include "Error.h"
Clement Courbetd939f6d2018-09-13 07:40:53 +000014#include "MCInstrDescView.h"
15#include "SnippetGenerator.h"
Roman Lebedev404bdb12019-04-06 14:16:26 +000016#include "Target.h"
Clement Courbetd939f6d2018-09-13 07:40:53 +000017#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 Song32401af2018-10-22 17:10:47 +000024namespace llvm {
Clement Courbetd939f6d2018-09-13 07:40:53 +000025namespace exegesis {
26
Guillaume Chateletfcbb6f32018-10-17 11:37:28 +000027std::vector<CodeTemplate> getSingleton(CodeTemplate &&CT) {
Guillaume Chatelet296a8622018-10-15 09:09:19 +000028 std::vector<CodeTemplate> Result;
29 Result.push_back(std::move(CT));
30 return Result;
31}
32
Clement Courbet50cdd562019-10-09 11:58:42 +000033SnippetGeneratorFailure::SnippetGeneratorFailure(const Twine &S)
34 : StringError(S, inconvertibleErrorCode()) {}
Clement Courbetd939f6d2018-09-13 07:40:53 +000035
Clement Courbet2cd0f282019-10-08 14:30:24 +000036SnippetGenerator::SnippetGenerator(const LLVMState &State, const Options &Opts)
37 : State(State), Opts(Opts) {}
Clement Courbetd939f6d2018-09-13 07:40:53 +000038
39SnippetGenerator::~SnippetGenerator() = default;
40
Clement Courbet50cdd562019-10-09 11:58:42 +000041Expected<std::vector<BenchmarkCode>> SnippetGenerator::generateConfigurations(
42 const Instruction &Instr, const BitVector &ExtraForbiddenRegs) const {
43 BitVector ForbiddenRegs = State.getRATC().reservedRegisters();
Clement Courbet9431b722019-09-27 12:56:24 +000044 ForbiddenRegs |= ExtraForbiddenRegs;
Clement Courbet8ef97e12019-09-27 08:04:10 +000045 // 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 Courbet03a3d292019-09-30 13:53:50 +000052 return make_error<Failure>(
Clement Courbet8ef97e12019-09-27 08:04:10 +000053 "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 Courbet03a3d292019-09-30 13:53:50 +000061 return make_error<Failure>(
Clement Courbet8ef97e12019-09-27 08:04:10 +000062 "Infeasible : memory instruction uses scratch memory register");
63 }
64 ForbiddenRegs |= ScratchRegAliases;
65 }
66
67 if (auto E = generateCodeTemplates(Instr, ForbiddenRegs)) {
Clement Courbetd939f6d2018-09-13 07:40:53 +000068 std::vector<BenchmarkCode> Output;
Guillaume Chatelet296a8622018-10-15 09:09:19 +000069 for (CodeTemplate &CT : E.get()) {
Guillaume Chatelet296a8622018-10-15 09:09:19 +000070 // TODO: Generate as many BenchmarkCode as needed.
71 {
72 BenchmarkCode BC;
73 BC.Info = CT.Info;
74 for (InstructionTemplate &IT : CT.Instructions) {
Roman Lebedev404bdb12019-04-06 14:16:26 +000075 randomizeUnsetVariables(State.getExegesisTarget(), ForbiddenRegs, IT);
Clement Courbet49195342019-10-08 09:06:48 +000076 BC.Key.Instructions.push_back(IT.build());
Guillaume Chatelet296a8622018-10-15 09:09:19 +000077 }
78 if (CT.ScratchSpacePointerInReg)
79 BC.LiveIns.push_back(CT.ScratchSpacePointerInReg);
Clement Courbet49195342019-10-08 09:06:48 +000080 BC.Key.RegisterInitialValues =
Clement Courbet0d79aaf2018-11-08 12:09:45 +000081 computeRegisterInitialValues(CT.Instructions);
Clement Courbet49195342019-10-08 09:06:48 +000082 BC.Key.Config = CT.Config;
Guillaume Chatelet296a8622018-10-15 09:09:19 +000083 Output.push_back(std::move(BC));
Clement Courbet2cd0f282019-10-08 14:30:24 +000084 if (Output.size() >= Opts.MaxConfigsPerOpcode)
85 return Output; // Early exit if we exceeded the number of allowed
86 // configs.
Clement Courbetd939f6d2018-09-13 07:40:53 +000087 }
Clement Courbetd939f6d2018-09-13 07:40:53 +000088 }
89 return Output;
90 } else
91 return E.takeError();
92}
93
Guillaume Chateletc96a97b2018-09-20 12:22:18 +000094std::vector<RegisterValue> SnippetGenerator::computeRegisterInitialValues(
Guillaume Chatelet70ac0192018-09-27 09:23:04 +000095 const std::vector<InstructionTemplate> &Instructions) const {
Clement Courbetd939f6d2018-09-13 07:40:53 +000096 // Collect all register uses and create an assignment for each of them.
97 // Ignore memory operands which are handled separately.
98 // Loop invariant: DefinedRegs[i] is true iif it has been set at least once
99 // before the current instruction.
Clement Courbet50cdd562019-10-09 11:58:42 +0000100 BitVector DefinedRegs = State.getRATC().emptyRegisters();
Guillaume Chateletc96a97b2018-09-20 12:22:18 +0000101 std::vector<RegisterValue> RIV;
Guillaume Chatelet70ac0192018-09-27 09:23:04 +0000102 for (const InstructionTemplate &IT : Instructions) {
Clement Courbetd939f6d2018-09-13 07:40:53 +0000103 // Returns the register that this Operand sets or uses, or 0 if this is not
104 // a register.
Guillaume Chatelet70ac0192018-09-27 09:23:04 +0000105 const auto GetOpReg = [&IT](const Operand &Op) -> unsigned {
Guillaume Chatelet09c28392018-10-09 08:59:10 +0000106 if (Op.isMemory())
Clement Courbetd939f6d2018-09-13 07:40:53 +0000107 return 0;
Guillaume Chatelet09c28392018-10-09 08:59:10 +0000108 if (Op.isImplicitReg())
109 return Op.getImplicitReg();
110 if (Op.isExplicit() && IT.getValueFor(Op).isReg())
Guillaume Chatelet70ac0192018-09-27 09:23:04 +0000111 return IT.getValueFor(Op).getReg();
Clement Courbetd939f6d2018-09-13 07:40:53 +0000112 return 0;
113 };
114 // Collect used registers that have never been def'ed.
Guillaume Chatelet32d384c2019-12-18 12:08:38 +0100115 for (const Operand &Op : IT.getInstr().Operands) {
Guillaume Chatelet09c28392018-10-09 08:59:10 +0000116 if (Op.isUse()) {
Clement Courbetd939f6d2018-09-13 07:40:53 +0000117 const unsigned Reg = GetOpReg(Op);
118 if (Reg > 0 && !DefinedRegs.test(Reg)) {
Clement Courbet54c2fa12018-11-08 12:37:56 +0000119 RIV.push_back(RegisterValue::zero(Reg));
Clement Courbetd939f6d2018-09-13 07:40:53 +0000120 DefinedRegs.set(Reg);
121 }
122 }
123 }
124 // Mark defs as having been def'ed.
Guillaume Chatelet32d384c2019-12-18 12:08:38 +0100125 for (const Operand &Op : IT.getInstr().Operands) {
Guillaume Chatelet09c28392018-10-09 08:59:10 +0000126 if (Op.isDef()) {
Clement Courbetd939f6d2018-09-13 07:40:53 +0000127 const unsigned Reg = GetOpReg(Op);
128 if (Reg > 0)
129 DefinedRegs.set(Reg);
130 }
131 }
132 }
Guillaume Chateletc96a97b2018-09-20 12:22:18 +0000133 return RIV;
Clement Courbetd939f6d2018-09-13 07:40:53 +0000134}
135
Clement Courbet50cdd562019-10-09 11:58:42 +0000136Expected<std::vector<CodeTemplate>>
Guillaume Chatelet296a8622018-10-15 09:09:19 +0000137generateSelfAliasingCodeTemplates(const Instruction &Instr) {
Clement Courbetd939f6d2018-09-13 07:40:53 +0000138 const AliasingConfigurations SelfAliasing(Instr, Instr);
Guillaume Chatelet296a8622018-10-15 09:09:19 +0000139 if (SelfAliasing.empty())
Clement Courbet50cdd562019-10-09 11:58:42 +0000140 return make_error<SnippetGeneratorFailure>("empty self aliasing");
Guillaume Chatelet296a8622018-10-15 09:09:19 +0000141 std::vector<CodeTemplate> Result;
142 Result.emplace_back();
143 CodeTemplate &CT = Result.back();
Guillaume Chatelet32d384c2019-12-18 12:08:38 +0100144 InstructionTemplate IT(&Instr);
Clement Courbetd939f6d2018-09-13 07:40:53 +0000145 if (SelfAliasing.hasImplicitAliasing()) {
146 CT.Info = "implicit Self cycles, picking random values.";
147 } else {
148 CT.Info = "explicit self cycles, selecting one aliasing Conf.";
149 // This is a self aliasing instruction so defs and uses are from the same
Guillaume Chatelet70ac0192018-09-27 09:23:04 +0000150 // instance, hence twice IT in the following call.
151 setRandomAliasing(SelfAliasing, IT, IT);
Clement Courbetd939f6d2018-09-13 07:40:53 +0000152 }
Guillaume Chatelet70ac0192018-09-27 09:23:04 +0000153 CT.Instructions.push_back(std::move(IT));
Guillaume Chateleta3849492018-10-15 09:21:21 +0000154 return std::move(Result);
Clement Courbetd939f6d2018-09-13 07:40:53 +0000155}
156
Clement Courbet50cdd562019-10-09 11:58:42 +0000157Expected<std::vector<CodeTemplate>>
158generateUnconstrainedCodeTemplates(const Instruction &Instr, StringRef Msg) {
Guillaume Chatelet296a8622018-10-15 09:09:19 +0000159 std::vector<CodeTemplate> Result;
160 Result.emplace_back();
161 CodeTemplate &CT = Result.back();
Clement Courbet50cdd562019-10-09 11:58:42 +0000162 CT.Info = formatv("{0}, repeating an unconstrained assignment", Msg);
Guillaume Chatelet32d384c2019-12-18 12:08:38 +0100163 CT.Instructions.emplace_back(&Instr);
Guillaume Chateleta3849492018-10-15 09:21:21 +0000164 return std::move(Result);
Clement Courbetd939f6d2018-09-13 07:40:53 +0000165}
Guillaume Chatelet70ac0192018-09-27 09:23:04 +0000166
Guillaume Chatelet415b2fb2018-10-01 12:19:10 +0000167std::mt19937 &randomGenerator() {
168 static std::random_device RandomDevice;
169 static std::mt19937 RandomGenerator(RandomDevice());
170 return RandomGenerator;
171}
172
Roman Lebedeva8223582019-04-08 10:11:00 +0000173size_t randomIndex(size_t Max) {
174 std::uniform_int_distribution<> Distribution(0, Max);
Guillaume Chatelet415b2fb2018-10-01 12:19:10 +0000175 return Distribution(randomGenerator());
176}
177
178template <typename C>
179static auto randomElement(const C &Container) -> decltype(Container[0]) {
Roman Lebedeva8223582019-04-08 10:11:00 +0000180 assert(!Container.empty() &&
181 "Can't pick a random element from an empty container)");
182 return Container[randomIndex(Container.size() - 1)];
Guillaume Chatelet415b2fb2018-10-01 12:19:10 +0000183}
184
Guillaume Chatelet415b2fb2018-10-01 12:19:10 +0000185static void setRegisterOperandValue(const RegisterOperandAssignment &ROV,
186 InstructionTemplate &IB) {
187 assert(ROV.Op);
Guillaume Chatelet09c28392018-10-09 08:59:10 +0000188 if (ROV.Op->isExplicit()) {
Guillaume Chatelet415b2fb2018-10-01 12:19:10 +0000189 auto &AssignedValue = IB.getValueFor(*ROV.Op);
190 if (AssignedValue.isValid()) {
191 assert(AssignedValue.isReg() && AssignedValue.getReg() == ROV.Reg);
192 return;
193 }
Clement Courbet50cdd562019-10-09 11:58:42 +0000194 AssignedValue = MCOperand::createReg(ROV.Reg);
Guillaume Chatelet415b2fb2018-10-01 12:19:10 +0000195 } else {
Guillaume Chatelet09c28392018-10-09 08:59:10 +0000196 assert(ROV.Op->isImplicitReg());
197 assert(ROV.Reg == ROV.Op->getImplicitReg());
Guillaume Chatelet415b2fb2018-10-01 12:19:10 +0000198 }
199}
200
Clement Courbet50cdd562019-10-09 11:58:42 +0000201size_t randomBit(const BitVector &Vector) {
Guillaume Chatelet415b2fb2018-10-01 12:19:10 +0000202 assert(Vector.any());
203 auto Itr = Vector.set_bits_begin();
Roman Lebedeva8223582019-04-08 10:11:00 +0000204 for (size_t I = randomIndex(Vector.count() - 1); I != 0; --I)
Guillaume Chatelet415b2fb2018-10-01 12:19:10 +0000205 ++Itr;
206 return *Itr;
207}
208
209void setRandomAliasing(const AliasingConfigurations &AliasingConfigurations,
210 InstructionTemplate &DefIB, InstructionTemplate &UseIB) {
211 assert(!AliasingConfigurations.empty());
212 assert(!AliasingConfigurations.hasImplicitAliasing());
213 const auto &RandomConf = randomElement(AliasingConfigurations.Configurations);
214 setRegisterOperandValue(randomElement(RandomConf.Defs), DefIB);
215 setRegisterOperandValue(randomElement(RandomConf.Uses), UseIB);
216}
217
Roman Lebedev404bdb12019-04-06 14:16:26 +0000218void randomizeUnsetVariables(const ExegesisTarget &Target,
Clement Courbet50cdd562019-10-09 11:58:42 +0000219 const BitVector &ForbiddenRegs,
Guillaume Chatelet415b2fb2018-10-01 12:19:10 +0000220 InstructionTemplate &IT) {
Guillaume Chatelet32d384c2019-12-18 12:08:38 +0100221 for (const Variable &Var : IT.getInstr().Variables) {
Clement Courbet50cdd562019-10-09 11:58:42 +0000222 MCOperand &AssignedValue = IT.getValueFor(Var);
Guillaume Chatelet415b2fb2018-10-01 12:19:10 +0000223 if (!AssignedValue.isValid())
Guillaume Chatelet32d384c2019-12-18 12:08:38 +0100224 Target.randomizeMCOperand(IT.getInstr(), Var, AssignedValue,
225 ForbiddenRegs);
Guillaume Chatelet415b2fb2018-10-01 12:19:10 +0000226 }
227}
228
Clement Courbetd939f6d2018-09-13 07:40:53 +0000229} // namespace exegesis
Fangrui Song32401af2018-10-22 17:10:47 +0000230} // namespace llvm