Debian Codegraph @

visa/iga/IGALibrary/Backend/Native/InstEncoder.hpp

raw
1 /*========================== begin_copyright_notice ============================
2
3 Copyright (C) 2017-2021 Intel Corporation
4
5 SPDX-License-Identifier: MIT
6
7 ============================= end_copyright_notice ===========================*/
8
9 #ifndef IGA_BACKEND_NATIVE_INSTENCODER_HPP
10 #define IGA_BACKEND_NATIVE_INSTENCODER_HPP
11
12 #include "../../IR/Kernel.hpp"
13 #include "../../asserts.hpp"
14 #include "../../bits.hpp"
15 #include "../../strings.hpp"
16 #include "../BitProcessor.hpp"
17 #include "../EncoderOpts.hpp"
18 #include "Field.hpp"
19 #include "MInst.hpp"
20 //
21 #include <cstdint>
22 #include <functional>
23 #include <sstream>
24 #include <string>
25 #include <vector>
26 //
27 #ifdef _DEBUG
28 // IGA_IGA_VALIDATE_BITS adds extra structures and code to ensure that each bit
29 // the instruction encoded gets written at most once. This can catch
30 // accidental field overlaps quite effectively.
31 //
32 // The neceessary assumption follows that encoders must not get lazy or sloppy
33 // and just clobber bits. Set it once only.
34 #define IGA_VALIDATE_BITS
35 #endif
36
37 namespace iga {
38 // immLo <= offset < immHi
39 static inline bool rangeContains(int immLo, int immHi, int offset) {
40 return immLo <= offset && offset < immHi;
41 }
42
43 struct InstEncoderState {
44 int instIndex = 0;
45 const Instruction *inst = nullptr;
46 #ifdef IGA_VALIDATE_BITS
47 // All the bit fields set by some field during this instruction encoding
48 // e.g. if a field with bits [127:96] is set to 00000000....0001b
49 // this bit mask will contain 1111....111b's for that field
50 // This allows us to detect writing to two overlapped fields, which
51 // indicates an internal logical error in the encoder.
52 MInst dirty;
53 // This contains a list of all the fields we set during an instruction
54 // encoding so we can run through the list and determine which fields
55 // overlapped.
56 std::vector<const Fragment *> fragmentsSet;
57 #endif
58 InstEncoderState(int _instIndex, const Instruction *_inst
59 #ifdef IGA_VALIDATE_BITS
60 ,
61 const MInst &_dirty,
62 const std::vector<const Fragment *> &_fieldsSet
63 #endif
64 )
65 : instIndex(_instIndex), inst(_inst)
66 #ifdef IGA_VALIDATE_BITS
67 ,
68 dirty(_dirty), fragmentsSet(_fieldsSet)
69 #endif
70 {
71 }
72 InstEncoderState() {}
73 };
74
75 struct Backpatch {
76 InstEncoderState state;
77 const Block *target;
78 const Field &fragment;
79 enum Type { REL, ABS } type;
80
81 Backpatch(InstEncoderState &_state, const Block *_target, const Field &_field,
82 Type _type = REL)
83 : state(_state), target(_target), fragment(_field), type(_type) {}
84 // TODO: determine where copy construction used to eliminate
85 // Backpatch(const Backpatch&) = delete;
86 };
87 typedef std::vector<Backpatch> BackpatchList;
88
89 // can be passed into the encoding if we want to know the result
90 struct CompactionDebugInfo {
91 std::vector<Op> fieldOps; // the op we were trying to compact
92 std::vector<const CompactionMapping *> fieldMisses; // which indices missed
93 std::vector<uint64_t> fieldMapping; // what we tried to match (parallel)
94 };
95 enum class CompactionResult {
96 CR_SUCCESS,
97 CR_NO_COMPACT, // {NoCompact} (or -Xnoautocompact and {}
98 CR_NO_FORMAT, // e.g. send or jump
99 CR_NO_AUTOCOMPACT, // no annotation and {Compacted} not set
100 CR_MISS // fragment misses
101 };
102
103 //////////////////////////////////////////////////////////
104 // generic encoder for all platforms
105 // subclasses specialize the necessary functions
106 class InstEncoder : public BitProcessor {
107 // the encoder options (e.g. for auto-compaction)
108 EncoderOpts opts;
109 //
110 // the platform we're encoding for
111 const Model &model;
112 //
113 // The target bits to encode to
114 MInst *bits = nullptr;
115 //
116 // A backpatch list that we can add to. A parent encoder is permitted
117 // to copy or shadow this list. Hence, this class may not assume the
118 // state is preserved between the calls of encodeInstruction and
119 // resolveBackpatch.
120 BackpatchList backpatches;
121 //
122 // state used by the encoder (can be saved for backpatches)
123 InstEncoderState state;
124
125 public:
126 InstEncoder(const EncoderOpts &_opts, BitProcessor &_parent,
127 const Model &_model)
128 : BitProcessor(_parent), opts(_opts), model(_model) {}
129 InstEncoder(const InstEncoder &) = delete;
130 InstEncoder& operator=(const InstEncoder&) = delete;
131
132 BackpatchList &getBackpatches() { return backpatches; }
133
134 const Instruction &getInst() const { return *state.inst; }
135 const OpSpec &getOpSpec() const { return getInst().getOpSpec(); }
136 const Model &getModel() const { return model; }
137 Platform platform() const { return getModel().platform; }
138
139 // void setBits(MInst mi) {*bits = mi;}
140 // MInst getBits() const {return *bits;}
141
142 ////////////////////////////////////////////////////////////////////////
143 // external interface (called by parent encoder; e.g. SerialEncoder) //
144 ////////////////////////////////////////////////////////////////////////
145
146 // Called externally by our parent encoder algorithm to start the encoding
147 // of an instruction. The instruction size is determined by the compaction
148 // bit in the target instruction.
149 void encodeInstruction(int ix, const Instruction &i, MInst *_bits) {
150 setCurrInst(&i);
151
152 state.instIndex = ix;
153 state.inst = &i;
154 #ifdef IGA_VALIDATE_BITS
155 state.dirty.qw0 = 0;
156 state.dirty.qw1 = 0;
157 state.fragmentsSet.clear();
158 #endif
159 _bits->reset();
160 encodeForPlatform(i);
161 }
162
163 void resolveBackpatch(Backpatch &bp, MInst *_bits) {
164 bits = _bits;
165 state = bp.state;
166 setCurrInst(bp.state.inst);
167 if (bp.type == Backpatch::ABS) {
168 encode(bp.fragment, bp.target->getPC());
169 } else {
170 encode(bp.fragment, bp.target->getPC() - bp.state.inst->getPC());
171 }
172 }
173
174 //////////////////////////////////////////////////////
175 // internal (to the encoder package, not private) instances of
176 // encodeForPlatform<P> and their subtrees will call these methods.
177 //////////////////////////////////////////////////////
178 void registerBackpatch(const Field &f, const Block *b,
179 Backpatch::Type t = Backpatch::Type::REL) {
180 backpatches.emplace_back(state, b, f, t);
181 }
182
183 void encode(const Field &f, int32_t val) {
184 encodeFieldBits(f, (uint32_t)val);
185 }
186 void encode(const Field &f, int64_t val) {
187 encodeFieldBits(f, (uint64_t)val);
188 }
189 void encode(const Field &f, uint32_t val) {
190 encodeFieldBits(f, (uint64_t)val);
191 }
192 void encode(const Field &f, uint64_t val) { encodeFieldBits(f, val); }
193 void encode(const Field &f, bool val) { encodeFieldBits(f, val ? 1 : 0); }
194 void encode(const Field &f, const OpSpec &os);
195 void encode(const Field &f, ExecSize es);
196 void encode(const Field &f, MathMacroExt acc);
197 void encode(const Field &f, Region::Vert vt);
198 void encode(const Field &f, Region::Width wi);
199 void encode(const Field &f, Region::Horz hz);
200 void encode(const Field &f, SrcModifier mods);
201 template <OpIx IX>
202 void encodeSubreg(const Field &f, RegName reg, RegRef rr, Type ty);
203 void encodeReg(const Field &fREGFILE, const Field &fREG, RegName reg,
204 int regNum);
205
206 //////////////////////////////////////////////////////
207 // Helper Functions
208 //////////////////////////////////////////////////////
209 void encodeFieldBits(const Field &f, uint64_t val0);
210
211 void encodingError(const std::string &msg) { encodingError("", msg); }
212 void encodingError(const Field *f, const std::string &msg) {
213 encodingError(f ? f->name : "", msg);
214 }
215 void encodingError(const Field &f, const std::string &msg) {
216 encodingError(f.name, msg);
217 }
218 void encodingError(OpIx ix, const std::string &msg) {
219 encodingError(ToStringOpIx(ix), msg);
220 }
221 void encodingError(const std::string &ctx, const std::string &msg) {
222 if (ctx.empty()) {
223 errorT(msg);
224 } else {
225 errorT(ctx, ": ", msg);
226 }
227 }
228 void internalErrorBadIR(const std::string &what) {
229 errorT("INTERNAL ERROR: malformed IR: ", what);
230 }
231 void internalErrorBadIR(const Field &f, const char *msg = nullptr) {
232 if (msg) {
233 internalErrorBadIR(iga::format(f.name, ": ", msg));
234 } else {
235 internalErrorBadIR(f.name);
236 }
237 }
238
239 template <typename T> void encodeEnum(const Field &f, T lo, T hi, T x) {
240 if (x < lo || x > hi) {
241 encodingError(f, "invalid value");
242 }
243 encodeFieldBits(f, static_cast<uint64_t>(x));
244 }
245
246 private:
247 #ifdef IGA_VALIDATE_BITS
248 void reportFieldOverlap(const Fragment &fr);
249 #endif
250 private:
251 void encodeForPlatform(const Instruction &i);
252 void encodeForPlatformFamilyXE(const Instruction &i);
253 }; // end class InstEncoder
254
255 ///////////////////////////////////////////////////////////////////////////
256 // longer method implementations (declared above)
257 ///////////////////////////////////////////////////////////////////////////
258
259 inline void InstEncoder::encode(const Field &f, const OpSpec &os) {
260 if (!os.isValid()) {
261 encodingError(f, "invalid opcode");
262 }
263 encodeFieldBits(f, os.opcode);
264 }
265
266 #define ENCODING_CASE(X, V) \
267 case X: \
268 val = (V); \
269 break
270 inline void InstEncoder::encode(const Field &f, ExecSize es) {
271 uint64_t val = 0;
272 switch (es) {
273 ENCODING_CASE(ExecSize::SIMD1, 0);
274 ENCODING_CASE(ExecSize::SIMD2, 1);
275 ENCODING_CASE(ExecSize::SIMD4, 2);
276 ENCODING_CASE(ExecSize::SIMD8, 3);
277 ENCODING_CASE(ExecSize::SIMD16, 4);
278 ENCODING_CASE(ExecSize::SIMD32, 5);
279 default:
280 internalErrorBadIR(f);
281 }
282 encodeFieldBits(f, val);
283 }
284
285 // encodes this as an math macro operand reference (e.g. r12.mme2)
286 // *not* as an explicit operand (for context save and restore)
287 inline void InstEncoder::encode(const Field &f, MathMacroExt mme) {
288 uint64_t val = 0;
289 switch (mme) {
290 ENCODING_CASE(MathMacroExt::MME0, 0x0);
291 ENCODING_CASE(MathMacroExt::MME1, 0x1);
292 ENCODING_CASE(MathMacroExt::MME2, 0x2);
293 ENCODING_CASE(MathMacroExt::MME3, 0x3);
294 ENCODING_CASE(MathMacroExt::MME4, 0x4);
295 ENCODING_CASE(MathMacroExt::MME5, 0x5);
296 ENCODING_CASE(MathMacroExt::MME6, 0x6);
297 ENCODING_CASE(MathMacroExt::MME7, 0x7);
298 ENCODING_CASE(MathMacroExt::NOMME, 0x8);
299 default:
300 internalErrorBadIR(f);
301 }
302 encodeFieldBits(f, val);
303 }
304
305 inline void InstEncoder::encode(const Field &f, Region::Vert vt) {
306 uint64_t val = 0;
307 switch (vt) {
308 ENCODING_CASE(Region::Vert::VT_0, 0x0);
309 ENCODING_CASE(Region::Vert::VT_1, 0x1);
310 ENCODING_CASE(Region::Vert::VT_2, 0x2);
311 ENCODING_CASE(Region::Vert::VT_4, 0x3);
312 ENCODING_CASE(Region::Vert::VT_8, 0x4);
313 ENCODING_CASE(Region::Vert::VT_16, 0x5);
314 ENCODING_CASE(Region::Vert::VT_32, 0x6);
315 case Region::Vert::VT_VxH:
316 val = 0xF;
317 if (f.encodedLength() == 3) {
318 val = 0x7; // XeHPC+
319 }
320 break;
321 default:
322 internalErrorBadIR(f);
323 }
324 encodeFieldBits(f, val);
325 }
326
327 inline void InstEncoder::encode(const Field &f, Region::Width wi) {
328 uint64_t val = 0;
329 switch (wi) {
330 ENCODING_CASE(Region::Width::WI_1, 0x0);
331 ENCODING_CASE(Region::Width::WI_2, 0x1);
332 ENCODING_CASE(Region::Width::WI_4, 0x2);
333 ENCODING_CASE(Region::Width::WI_8, 0x3);
334 ENCODING_CASE(Region::Width::WI_16, 0x4);
335 default:
336 internalErrorBadIR(f);
337 }
338 encodeFieldBits(f, val);
339 }
340
341 inline void InstEncoder::encode(const Field &f, Region::Horz hz) {
342 uint64_t val = 0;
343 switch (hz) {
344 ENCODING_CASE(Region::Horz::HZ_0, 0);
345 ENCODING_CASE(Region::Horz::HZ_1, 1);
346 ENCODING_CASE(Region::Horz::HZ_2, 2);
347 ENCODING_CASE(Region::Horz::HZ_4, 3);
348 default:
349 internalErrorBadIR(f);
350 }
351 encodeFieldBits(f, val);
352 }
353
354 inline void InstEncoder::encode(const Field &f, SrcModifier mods) {
355 uint64_t val = 0;
356 switch (mods) {
357 ENCODING_CASE(SrcModifier::NONE, 0x0);
358 ENCODING_CASE(SrcModifier::ABS, 0x1);
359 ENCODING_CASE(SrcModifier::NEG, 0x2);
360 ENCODING_CASE(SrcModifier::NEG_ABS, 0x3);
361 default:
362 internalErrorBadIR(f);
363 }
364 encodeFieldBits(f, val);
365 }
366 #undef ENCODING_CASE
367
368 inline void InstEncoder::encodeReg(const Field &fREGFILE, const Field &fREG,
369 RegName reg, int regNum) {
370 const RegInfo *ri = model.lookupRegInfoByRegName(reg);
371 if (ri == nullptr) {
372 internalErrorBadIR(fREG, "unsupported register for platform");
373 return;
374 }
375 uint16_t regNumBits = 0;
376 if (!ri->encode(regNum, regNumBits)) {
377 internalErrorBadIR(fREG, "invalid register");
378 return;
379 }
380 encodeFieldBits(fREGFILE, reg == RegName::GRF_R ? 1 : 0);
381 encodeFieldBits(fREG, regNumBits);
382 }
383
384 template <OpIx IX>
385 inline void InstEncoder::encodeSubreg(const Field &f, RegName reg, RegRef rr,
386 Type ty) {
387 uint64_t val = (uint64_t)rr.subRegNum;
388 // branches have implicit :d
389 val = ty == Type::INVALID
390 ? 4 * val
391 : SubRegToBinaryOffset((int)val, reg, ty, model.platform);
392 encodeFieldBits(f, val);
393 }
394
395 inline void InstEncoder::encodeForPlatform(const Instruction &i) {
396 switch (platform()) {
397 case Platform::XE:
398 case Platform::XE_HP:
399 case Platform::XE_HPG:
400 case Platform::XE_HPC:
401 case Platform::XE2:
402 case Platform::XE3:
403 break;
404 case Platform::FUTURE:
405 default:
406 // caller checks this and gives a soft error
407 IGA_ASSERT_FALSE("unsupported platform for native encoder");
408 }
409 }
410 } // namespace iga
411 #endif /* IGA_BACKEND_NATIVE_INSTENCODER_HPP */