Debian Codegraph @

llvm/tools/llvm-exegesis/lib/SerialSnippetGenerator.cpp

raw
1 //===-- SerialSnippetGenerator.cpp ------------------------------*- C++ -*-===//
2 //
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
6 //
7 //===----------------------------------------------------------------------===//
8
9 #include "SerialSnippetGenerator.h"
10
11 #include "CodeTemplate.h"
12 #include "MCInstrDescView.h"
13 #include "Target.h"
14 #include <algorithm>
15 #include <numeric>
16 #include <vector>
17
18 namespace llvm {
19 namespace exegesis {
20
21 struct ExecutionClass {
22 ExecutionMode Mask;
23 const char *Description;
24 } static const kExecutionClasses[] = {
25 {ExecutionMode::ALWAYS_SERIAL_IMPLICIT_REGS_ALIAS |
26 ExecutionMode::ALWAYS_SERIAL_TIED_REGS_ALIAS,
27 "Repeating a single implicitly serial instruction"},
28 {ExecutionMode::SERIAL_VIA_EXPLICIT_REGS,
29 "Repeating a single explicitly serial instruction"},
30 {ExecutionMode::SERIAL_VIA_MEMORY_INSTR |
31 ExecutionMode::SERIAL_VIA_NON_MEMORY_INSTR,
32 "Repeating two instructions"},
33 };
34
35 static constexpr size_t kMaxAliasingInstructions = 10;
36
37 static std::vector<const Instruction *>
38 computeAliasingInstructions(const LLVMState &State, const Instruction *Instr,
39 size_t MaxAliasingInstructions,
40 const BitVector &ForbiddenRegisters) {
41 const auto &ET = State.getExegesisTarget();
42 const auto AvailableFeatures = State.getSubtargetInfo().getFeatureBits();
43 // Randomly iterate the set of instructions.
44 std::vector<unsigned> Opcodes;
45 Opcodes.resize(State.getInstrInfo().getNumOpcodes());
46 std::iota(Opcodes.begin(), Opcodes.end(), 0U);
47 llvm::shuffle(Opcodes.begin(), Opcodes.end(), randomGenerator());
48
49 std::vector<const Instruction *> AliasingInstructions;
50 for (const unsigned OtherOpcode : Opcodes) {
51 if (!ET.isOpcodeAvailable(OtherOpcode, AvailableFeatures))
52 continue;
53 if (OtherOpcode == Instr->Description.getOpcode())
54 continue;
55 const Instruction &OtherInstr = State.getIC().getInstr(OtherOpcode);
56 if (ET.getIgnoredOpcodeReasonOrNull(State, OtherInstr.getOpcode()))
57 continue;
58 if (OtherInstr.hasMemoryOperands())
59 continue;
60 if (!ET.allowAsBackToBack(OtherInstr))
61 continue;
62 if (Instr->hasAliasingRegistersThrough(OtherInstr, ForbiddenRegisters))
63 AliasingInstructions.push_back(&OtherInstr);
64 if (AliasingInstructions.size() >= MaxAliasingInstructions)
65 break;
66 }
67 return AliasingInstructions;
68 }
69
70 static ExecutionMode getExecutionModes(const Instruction &Instr,
71 const BitVector &ForbiddenRegisters) {
72 ExecutionMode EM = ExecutionMode::UNKNOWN;
73 if (Instr.hasAliasingImplicitRegisters())
74 EM |= ExecutionMode::ALWAYS_SERIAL_IMPLICIT_REGS_ALIAS;
75 if (Instr.hasTiedRegisters())
76 EM |= ExecutionMode::ALWAYS_SERIAL_TIED_REGS_ALIAS;
77 if (Instr.hasMemoryOperands())
78 EM |= ExecutionMode::SERIAL_VIA_MEMORY_INSTR;
79 if (Instr.hasAliasingNotMemoryRegisters(ForbiddenRegisters))
80 EM |= ExecutionMode::SERIAL_VIA_EXPLICIT_REGS;
81 if (Instr.hasOneUseOrOneDef())
82 EM |= ExecutionMode::SERIAL_VIA_NON_MEMORY_INSTR;
83 return EM;
84 }
85
86 static void appendCodeTemplates(const LLVMState &State,
87 InstructionTemplate Variant,
88 const BitVector &ForbiddenRegisters,
89 ExecutionMode ExecutionModeBit,
90 StringRef ExecutionClassDescription,
91 std::vector<CodeTemplate> &CodeTemplates) {
92 assert(isEnumValue(ExecutionModeBit) && "Bit must be a power of two");
93 switch (ExecutionModeBit) {
94 case ExecutionMode::ALWAYS_SERIAL_IMPLICIT_REGS_ALIAS:
95 // Nothing to do, the instruction is always serial.
96 [[fallthrough]];
97 case ExecutionMode::ALWAYS_SERIAL_TIED_REGS_ALIAS: {
98 // Picking whatever value for the tied variable will make the instruction
99 // serial.
100 CodeTemplate CT;
101 CT.Execution = ExecutionModeBit;
102 CT.Info = std::string(ExecutionClassDescription);
103 CT.Instructions.push_back(std::move(Variant));
104 CodeTemplates.push_back(std::move(CT));
105 return;
106 }
107 case ExecutionMode::SERIAL_VIA_MEMORY_INSTR: {
108 // Select back-to-back memory instruction.
109
110 auto &I = Variant.getInstr();
111 if (I.Description.mayLoad()) {
112 // If instruction is load, we can self-alias it in case when instruction
113 // overrides whole address register. For that we use provided scratch
114 // memory.
115
116 // TODO: now it is not checked if load writes the whole register.
117
118 auto DefOpIt = find_if(I.Operands, [](Operand const &Op) {
119 return Op.isDef() && Op.isReg();
120 });
121
122 if (DefOpIt == I.Operands.end())
123 return;
124
125 const Operand &DefOp = *DefOpIt;
126 const ExegesisTarget &ET = State.getExegesisTarget();
127 unsigned ScratchMemoryRegister = ET.getScratchMemoryRegister(
128 State.getTargetMachine().getTargetTriple());
129 const llvm::MCRegisterClass &RegClass =
130 State.getTargetMachine().getMCRegisterInfo()->getRegClass(
131 DefOp.getExplicitOperandInfo().RegClass);
132
133 // Register classes of def operand and memory operand must be the same
134 // to perform aliasing.
135 if (!RegClass.contains(ScratchMemoryRegister))
136 return;
137
138 ET.fillMemoryOperands(Variant, ScratchMemoryRegister, 0);
139 Variant.getValueFor(DefOp) = MCOperand::createReg(ScratchMemoryRegister);
140
141 CodeTemplate CT;
142 CT.Execution = ExecutionModeBit;
143 CT.ScratchSpacePointerInReg = ScratchMemoryRegister;
144
145 CT.Info = std::string(ExecutionClassDescription);
146 CT.Instructions.push_back(std::move(Variant));
147 CodeTemplates.push_back(std::move(CT));
148 }
149
150 // TODO: implement more cases
151 return;
152 }
153 case ExecutionMode::SERIAL_VIA_EXPLICIT_REGS: {
154 // Making the execution of this instruction serial by selecting one def
155 // register to alias with one use register.
156 const AliasingConfigurations SelfAliasing(
157 Variant.getInstr(), Variant.getInstr(), ForbiddenRegisters);
158 assert(!SelfAliasing.empty() && !SelfAliasing.hasImplicitAliasing() &&
159 "Instr must alias itself explicitly");
160 // This is a self aliasing instruction so defs and uses are from the same
161 // instance, hence twice Variant in the following call.
162 setRandomAliasing(SelfAliasing, Variant, Variant);
163 CodeTemplate CT;
164 CT.Execution = ExecutionModeBit;
165 CT.Info = std::string(ExecutionClassDescription);
166 CT.Instructions.push_back(std::move(Variant));
167 CodeTemplates.push_back(std::move(CT));
168 return;
169 }
170 case ExecutionMode::SERIAL_VIA_NON_MEMORY_INSTR: {
171 const Instruction &Instr = Variant.getInstr();
172 // Select back-to-back non-memory instruction.
173 for (const auto *OtherInstr : computeAliasingInstructions(
174 State, &Instr, kMaxAliasingInstructions, ForbiddenRegisters)) {
175 const AliasingConfigurations Forward(Instr, *OtherInstr,
176 ForbiddenRegisters);
177 const AliasingConfigurations Back(*OtherInstr, Instr, ForbiddenRegisters);
178 InstructionTemplate ThisIT(Variant);
179 InstructionTemplate OtherIT(OtherInstr);
180 if (!Forward.hasImplicitAliasing())
181 setRandomAliasing(Forward, ThisIT, OtherIT);
182 else if (!Back.hasImplicitAliasing())
183 setRandomAliasing(Back, OtherIT, ThisIT);
184 CodeTemplate CT;
185 CT.Execution = ExecutionModeBit;
186 CT.Info = std::string(ExecutionClassDescription);
187 CT.Instructions.push_back(std::move(ThisIT));
188 CT.Instructions.push_back(std::move(OtherIT));
189 CodeTemplates.push_back(std::move(CT));
190 }
191 return;
192 }
193 default:
194 llvm_unreachable("Unhandled enum value");
195 }
196 }
197
198 SerialSnippetGenerator::~SerialSnippetGenerator() = default;
199
200 Expected<std::vector<CodeTemplate>>
201 SerialSnippetGenerator::generateCodeTemplates(
202 InstructionTemplate Variant, const BitVector &ForbiddenRegisters) const {
203 std::vector<CodeTemplate> Results;
204 const ExecutionMode EM =
205 getExecutionModes(Variant.getInstr(), ForbiddenRegisters);
206 for (const auto EC : kExecutionClasses) {
207 for (const auto ExecutionModeBit : getExecutionModeBits(EM & EC.Mask))
208 appendCodeTemplates(State, Variant, ForbiddenRegisters, ExecutionModeBit,
209 EC.Description, Results);
210 if (!Results.empty())
211 break;
212 }
213 if (Results.empty())
214 return make_error<Failure>(
215 "No strategy found to make the execution serial");
216 return std::move(Results);
217 }
218
219 } // namespace exegesis
220 } // namespace llvm