Debian Codegraph @

visa/iga/IGALibrary/Backend/GED/Decoder.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_GED_DECODER_HPP_
10 #define _IGA_BACKEND_GED_DECODER_HPP_
11
12 #include "GEDBitProcessor.hpp"
13 #include "GEDToIGATranslation.hpp"
14 #include "ged.h"
15
16 #include <optional>
17
18 #define GED_DECODE_TO(FIELD, TRANS, DST) \
19 do { \
20 GED_RETURN_VALUE _status; \
21 DST = TRANS(GED_Get##FIELD(&m_currGedInst, &_status)); \
22 if (_status != GED_RETURN_VALUE_SUCCESS) { \
23 handleGedDecoderError(__LINE__, #FIELD, _status); \
24 } \
25 } while (0)
26
27 #define GED_DECODE_RAW_TO(FIELD, DST) \
28 do { \
29 GED_RETURN_VALUE _status; \
30 DST = GED_Get##FIELD(&m_currGedInst, &_status); \
31 if (_status != GED_RETURN_VALUE_SUCCESS) { \
32 handleGedDecoderError(__LINE__, #FIELD, _status); \
33 } \
34 } while (0)
35
36 #define GED_DECODE_RAW(GED_TYPE, ID, FIELD) \
37 [[maybe_unused]] GED_TYPE ID; \
38 GED_DECODE_RAW_TO(FIELD, ID);
39
40 #define GED_DECODE(IGA_TYPE, GED_TYPE, ID, FIELD) \
41 GED_DECODE_RAW(GED_TYPE, GED_##ID, FIELD); \
42 IGA_TYPE ID = translate(GED_##ID);
43
44 #define GED_DECODE_RAW_TO_SRC(DST, TYPE, FIELD) \
45 do { \
46 GED_RETURN_VALUE _STATUS; \
47 DST = GED_Get##FIELD(&m_currGedInst, &_STATUS); \
48 if (_STATUS != GED_RETURN_VALUE_SUCCESS) { \
49 handleGedDecoderError(__LINE__, #FIELD, _STATUS); \
50 } \
51 } while (0)
52
53 #define RETURN_GED_DECODE_RAW_TO_SRC(TYPE, FIELD) \
54 { \
55 TYPE _DST; \
56 GED_DECODE_RAW_TO_SRC(_DST, TYPE, FIELD); \
57 return _DST; \
58 }
59 // the extra namespace declaration is needed to make g++ happy
60 #define DEFINE_SOURCE_ACCESSOR(TYPE, FIELD, I) \
61 namespace iga { \
62 template <> TYPE Decoder::decodeSrc##FIELD<SourceIndex::SRC##I>() { \
63 RETURN_GED_DECODE_RAW_TO_SRC(TYPE, Src##I##FIELD); \
64 } \
65 }
66
67 /* #define DEFINE_SOURCE_ACCESSOR_INLINE(TYPE, FIELD, I) \
68 template <> TYPE decodeSrc ## FIELD <SourceIndex::SRC##I>() { \
69 RETURN_GED_DECODE_RAW_TO_SRC(TYPE, Src ## I ## FIELD); \
70 }
71 */
72 /* #define DEFINE_GED_SOURCE_ACCESSORS_INLINE_01(TYPE, FIELD) \
73 DEFINE_SOURCE_ACCESSOR_INLINE(TYPE, FIELD, 0) \
74 DEFINE_SOURCE_ACCESSOR_INLINE(TYPE, FIELD, 1)
75 */
76 #define DEFINE_GED_SOURCE_ACCESSORS_01(TYPE, FIELD) \
77 DEFINE_SOURCE_ACCESSOR(TYPE, FIELD, 0) \
78 DEFINE_SOURCE_ACCESSOR(TYPE, FIELD, 1)
79
80 /* #define DEFINE_GED_SOURCE_ACCESSORS_INLINE_012(TYPE, FIELD) \
81 DEFINE_SOURCE_ACCESSOR_INLINE(TYPE, FIELD, 0) \
82 DEFINE_SOURCE_ACCESSOR_INLINE(TYPE, FIELD, 1) \
83 DEFINE_SOURCE_ACCESSOR_INLINE(TYPE, FIELD, 2)
84 */
85 #define DEFINE_GED_SOURCE_ACCESSORS_012(TYPE, FIELD) \
86 DEFINE_SOURCE_ACCESSOR(TYPE, FIELD, 0) \
87 DEFINE_SOURCE_ACCESSOR(TYPE, FIELD, 1) \
88 DEFINE_SOURCE_ACCESSOR(TYPE, FIELD, 2)
89
90 #define DEFINE_GED_SOURCE_ACCESSORS_01234(TYPE, FIELD) \
91 DEFINE_SOURCE_ACCESSOR(TYPE, FIELD, 0) \
92 DEFINE_SOURCE_ACCESSOR(TYPE, FIELD, 1) \
93 DEFINE_SOURCE_ACCESSOR(TYPE, FIELD, 2) \
94 DEFINE_SOURCE_ACCESSOR(TYPE, FIELD, 3) \
95 DEFINE_SOURCE_ACCESSOR(TYPE, FIELD, 4)
96
97 namespace iga {
98 struct FlagRegInfo {
99 Predication pred;
100 FlagModifier modifier;
101 RegRef reg;
102 };
103 struct DirRegOpInfo {
104 RegName regName = RegName::INVALID; // e.g. "r" or "acc"
105 RegRef regRef; // 13
106 Type type = Type::INVALID;
107 };
108
109 class Decoder : public GEDBitProcessor {
110 public:
111 // Constructs a new decoder with an error handler and an empty kernel
112 Decoder(const Model &model, ErrorHandler &errHandler);
113
114 // the main entry point for decoding a kernel
115 Kernel *decodeKernelBlocks(const void *binary, size_t binarySize);
116 Kernel *decodeKernelNumeric(const void *binary, size_t binarySize);
117
118 // Set the SWSB endcoding mode, if not set, derived from platform
119 void setSWSBEncodingMode(SWSB_ENCODE_MODE mode) {
120 if (mode != SWSB_ENCODE_MODE::SWSBInvalidMode) {
121 m_SWSBEncodeMode = mode;
122 }
123 }
124
125
126 bool isMacro() const;
127
128 private:
129 Kernel *decodeKernel(const void *binary, size_t binarySize,
130 bool numericLabels);
131
132 // pass 1 decodes instructions with numeric labels
133 void decodeInstructions(Kernel &kernel, const void *binary, size_t binarySize,
134 InstList &insts);
135 const OpSpec *decodeOpSpec(Op op);
136
137 Instruction *decodeNextInstruction(Kernel &kernel);
138
139 void decodeSWSB(Instruction *inst);
140
141 protected:
142 GED_ACCESS_MODE decodeAccessMode();
143 MaskCtrl decodeMaskCtrl();
144 Predication decodePredication();
145 void decodePredInv(Predication &pred);
146 FlagRegInfo
147 decodeFlagRegInfo(bool imm64Src0Overlap = false); // pred, cond, ...
148 ExecSize decodeExecSize();
149 ChannelOffset decodeChannelOffset();
150
151 void decodeJipToSrc(Instruction *inst, SourceIndex s = SourceIndex::SRC0,
152 Type type = Type::INVALID);
153 void decodeUipToSrc1(Instruction *inst, Type type);
154 int32_t decodeJip();
155 int32_t decodeUip();
156
157 // Reads a source from a the 'fromSrc'th source operand
158 // and creates the operand in IR as 'toSrc'.
159 // Typically this will be used directly as
160 // createSoureOp(inst, SourceIndex::SRC0, SourceIndex::SRC0);
161 // However, for some operands with implicit operands such as
162 // jmpi, ths will show up as
163 // createSoureOp(inst, SourceIndex::SRC1, SourceIndex::SRC0);
164 // since the first syntactic source is stored in src1's bits
165 // (src0 is ip for that op)
166
167 ///////////////////////////////////////////////////////////////////////
168 // BASIC INSTRUCTIONS
169 ///////////////////////////////////////////////////////////////////////
170 Instruction *decodeBasicInstruction(Kernel &kernel);
171 void decodeBasicUnaryInstruction(Instruction *inst,
172 GED_ACCESS_MODE accessMode);
173
174 void decodeBasicDestination(Instruction *inst, GED_ACCESS_MODE a) {
175 if (a == GED_ACCESS_MODE_Align16)
176 decodeBasicDestinationAlign16(inst);
177 else
178 decodeBasicDestinationAlign1(inst);
179 }
180 void decodeBasicDestinationAlign16(
181 Instruction *inst); // e.g. for math.invm and context save restore
182 void decodeBasicDestinationAlign1(Instruction *inst);
183 template <SourceIndex S>
184 void decodeSourceBasic(Instruction *inst, GED_ACCESS_MODE a) {
185 if (a == GED_ACCESS_MODE_Align16)
186 decodeSourceBasicAlign16<S>(inst, S);
187 else
188 decodeSourceBasicAlign1<S>(inst, S);
189 }
190 template <SourceIndex S>
191 void decodeSourceBasic(Instruction *inst, SourceIndex toSrcIx,
192 GED_ACCESS_MODE a) {
193 if (a == GED_ACCESS_MODE_Align16)
194 decodeSourceBasicAlign16<S>(inst, toSrcIx);
195 else
196 decodeSourceBasicAlign1<S>(inst, toSrcIx);
197 }
198 template <SourceIndex S> void decodeSourceBasicAlign1(Instruction *inst) {
199 decodeSourceBasicAlign1<S>(inst, S);
200 }
201 template <SourceIndex S> void decodeSourceBasicAlign16(Instruction *inst) {
202 decodeSourceBasicAlign16<S>(inst, S);
203 }
204 template <SourceIndex S>
205 void decodeSourceBasicAlign1(Instruction *inst, SourceIndex toSrcIx);
206 template <SourceIndex S>
207 void decodeSourceBasicAlign16(Instruction *inst, SourceIndex toSrcIx);
208
209
210 ///////////////////////////////////////////////////////////////////////
211 // TERNARY INSTRUCTIONS
212 ///////////////////////////////////////////////////////////////////////
213 Instruction *decodeTernaryInstruction(Kernel &kernel);
214 void decodeTernaryInstructionOperands(Kernel &kernel, Instruction *inst,
215 GED_ACCESS_MODE accessMode);
216 // Align16
217 void decodeTernaryDestinationAlign16(Instruction *inst);
218 template <SourceIndex S> void decodeTernarySourceAlign16(Instruction *inst);
219 // Align1
220 void decodeTernaryDestinationAlign1(Instruction *inst);
221 template <SourceIndex S> void decodeTernarySourceAlign1(Instruction *inst);
222
223 ///////////////////////////////////////////////////////////////////////
224 // NARY INSTRUCTIONS
225 ///////////////////////////////////////////////////////////////////////
226 Instruction *decodeQuinaryInstruction(Kernel &kernel);
227 void decodeQuinaryInstructionOperands(Instruction *inst);
228 void decodeInstructionOperandsForBDPAS(Instruction *inst);
229 void decodeBDPASDestination(Instruction* inst);
230 template <SourceIndex S> void decodeBDPASSource(Instruction *inst);
231 template <SourceIndex S> void decodeBDPASScalingSource(Instruction *inst);
232 ///////////////////////////////////////////////////////////////////////
233 // SEND INSTRUCTIONS
234 ///////////////////////////////////////////////////////////////////////
235 Instruction *decodeSendInstruction(Kernel &kernel);
236 Instruction *decodeSendgInstructionXe3(Kernel &kernel);
237 Instruction *decodeSendgxInstructionXe3p(Kernel &kernel);
238 void decodeSendDestination(Instruction *inst);
239 GED_ADDR_MODE decodeSendSource0AddressMode();
240 void decodeSendSource0(Instruction *inst);
241 void decodeSendSource1(Instruction *inst);
242 void decodeSendgDestinationXe3(Instruction *inst);
243 bool decodeSendgSource0Xe3(Instruction *inst); // returns true for sendg
244 void decodeSendgSource1Xe3(Instruction *inst);
245 void decodeSendgxDestinationXe3p(Instruction *inst);
246 void decodeSendgxSource0Xe3p(Instruction *inst); // returns true for sendg
247 void decodeSendgxSource1Xe3p(Instruction *inst);
248 SendDesc decodeSendExDesc();
249 SendDesc decodeSendDesc();
250
251 // facilitates internal decoding logic
252 struct SendDescodeInfo {
253 SFID sfid = SFID::INVALID;
254 int dstLen = -1, src0Len = -1, src1Len = -1;
255 bool hasCps = false, hasExBSO = false;
256 SendDesc desc, exDesc;
257 uint32_t exImmOffDesc = 0;
258 };
259 void decodeSendInfoPreXe(SendDescodeInfo &sdi);
260 void decodeSendInfoXe(SendDescodeInfo &sdi);
261 void decodeSendInfoXeHP(SendDescodeInfo &sdi);
262 void decodeSendInfoXeHPG(SendDescodeInfo &sdi);
263 void decodeSendInfoXe2(SendDescodeInfo &sdi);
264
265 ///////////////////////////////////////////////////////////////////////
266 // BRANCH INSTRUCTIONS
267 ///////////////////////////////////////////////////////////////////////
268 Instruction *decodeBranchInstruction(Kernel &kernel);
269 Instruction *decodeBranchSimplifiedInstruction(Kernel &kernel);
270 void decodeBranchDestination(Instruction *inst);
271
272 ///////////////////////////////////////////////////////////////////////
273 // OTHER INSTRUCTIONS
274 ///////////////////////////////////////////////////////////////////////
275 Instruction *decodeWaitInstruction(Kernel &kernel);
276 Instruction *decodeSyncInstruction(Kernel &kernel);
277
278 ///////////////////////////////////////////////////////////////////////
279 // OTHER HELPERS
280 ///////////////////////////////////////////////////////////////////////
281 void decodeReg(int opIx, // dst => opIx<0
282 GED_REG_FILE regFile, uint32_t regNumBits, RegName &regName,
283 RegRef &regRef);
284
285 DirRegOpInfo decodeDstDirRegInfo();
286 Type decodeDstType();
287 int decodeDestinationRegNumAccBitsFromChEn();
288 MathMacroExt decodeDestinationMathMacroRegFromChEn();
289
290 Subfunction decodeSubfunction(bool &valid);
291 bool hasImm64Src0Overlap();
292
293 void decodeDstDirSubRegNum(DirRegOpInfo &dri);
294 bool hasImplicitScalingType(Type &type, DirRegOpInfo &dri);
295 void decodeNextInstructionEpilog(Instruction *inst);
296
297 void decodeThreadOptions(Instruction *inst, GED_THREAD_CTRL trdCntrl);
298
299 template <SourceIndex S> ImmVal decodeTernarySrcImmVal(Type t);
300
301 // various GED source accessors
302 // some definitions created in Decoder.cpp using macros
303 // i.e. DEFINE_GED_SOURCE_ACCESSORS...
304 template <SourceIndex S> GED_ADDR_MODE decodeSrcAddrMode();
305
306 // #define DEFINE_SOURCE_ACCESSOR_INLINE(TYPE, FIELD, I)
307 // template <> GED_ADDR_MODE decodeSrcAddrMode <SourceIndex::SRC0>() {
308 // RETURN_GED_DECODE_RAW_TO_SRC(GED_ADDR_MODE, Src0AddrMode);
309 // }
310
311 template <SourceIndex S> GED_REG_FILE decodeSrcRegFile();
312 template <SourceIndex S> GED_DATA_TYPE decodeSrcDataType();
313
314 // register direct fields
315 template <SourceIndex S> uint32_t decodeSrcRegNum();
316 template <SourceIndex S> uint32_t decodeSrcSubRegNum();
317 // implicit accumulators
318 template <SourceIndex S> GED_MATH_MACRO_EXT decodeSrcMathMacroExt();
319
320 // register indirect fields
321 template <SourceIndex S> int32_t decodeSrcAddrImm();
322 template <SourceIndex S> uint32_t decodeSrcAddrSubRegNum();
323 // immediate fields
324 ImmVal decodeSrcImmVal(Type type);
325
326 // register direct and indirect fields
327 // yeah, the naming convention dictates this name
328 // (on for Src vs Dst) and one for the field name SrcMod
329 template <SourceIndex S> GED_SRC_MOD decodeSrcSrcMod();
330
331 template <SourceIndex S> Type decodeSrcType() {
332 return translate(decodeSrcDataType<S>());
333 }
334
335 template <SourceIndex S>
336 std::optional<Region> decodeSrcReducedRegionTernary() {
337 IGA_ASSERT(S == SourceIndex::SRC1, "Invalid SourceIndex");
338 GED_DECODE_RAW(bool, isScalar, Src1ScalarReg);
339 if (isScalar)
340 return Region::SRC0X0;
341 else
342 return {};
343 }
344
345 template <SourceIndex S>
346 std::optional<Region> decodeSrcReducedRegion() {
347 IGA_ASSERT(S == SourceIndex::SRC1, "Invalid SourceIndex");
348 GED_DECODE_RAW(bool, isScalar, Src1ScalarReg);
349 if (isScalar)
350 return Region::SRC010;
351 else
352 return {};
353 }
354
355 template <SourceIndex S> std::optional<Region> decodeSrcRegionVWH() {
356 if (m_model.srcHasReducedRegion(static_cast<uint32_t>(S)))
357 return decodeSrcReducedRegion<S>();
358 return transateGEDtoIGARegion(decodeSrcVertStride<S>(), decodeSrcWidth<S>(),
359 decodeSrcHorzStride<S>());
360 }
361
362 template <SourceIndex S> Region decodeSrcRegionTernaryAlign1(const OpSpec &);
363
364 template <SourceIndex S> MathMacroExt decodeSrcMathMacroReg() {
365 return translate(decodeSrcMathMacroExt<S>());
366 }
367 template <SourceIndex S> SrcModifier decodeSrcModifier() {
368 if (m_opSpec->supportsSourceModifiers()) {
369 return translate(decodeSrcSrcMod<S>());
370 }
371 return SrcModifier::NONE;
372 }
373
374 // decodes regname, regnum, and subreg num (if applicable)
375 // does *not* scale the subreg num
376 template <SourceIndex S> RegName decodeSourceReg(RegRef &regRef) {
377 uint32_t regNumBits = decodeSrcRegNum<S>();
378 RegName regName = RegName::INVALID;
379 GED_REG_FILE regFile = decodeSrcRegFile<S>();
380 decodeReg((int)S, regFile, regNumBits, regName, regRef);
381 if (!m_opSpec->isAnySendFormat() && !isMacro()) {
382 regRef.subRegNum = (uint16_t)decodeSrcSubRegNum<S>();
383 } else {
384 regRef.subRegNum = 0;
385 }
386 return regName;
387 }
388
389 template <SourceIndex S> void decodeGrfSourceReg(RegRef& regRef) {
390 uint32_t regNumBits = decodeSrcRegNum<S>();
391 RegName regName = RegName::INVALID;
392 decodeReg((int)S, GED_REG_FILE_GRF, regNumBits, regName, regRef);
393 if (!m_opSpec->isAnySendFormat() && !isMacro()) {
394 regRef.subRegNum = (uint16_t)decodeSrcSubRegNum<S>();
395 } else {
396 regRef.subRegNum = 0;
397 }
398 }
399
400 template <SourceIndex S> DirRegOpInfo decodeSrcDirRegOpInfo() {
401 DirRegOpInfo dri;
402 dri.regName = decodeSourceReg<S>(dri.regRef);
403
404 Type scalingType = Type::INVALID;
405 // FIXME: not sure what "hasImplicitScalingType" for, this cause the
406 // in-consistent between encoder and decoder. Still using it to keep it the
407 // same as before...
408 if (!hasImplicitScalingType(scalingType, dri)) {
409 scalingType = dri.type = decodeSrcType<S>();
410 }
411
412 if (scalingType == Type::INVALID) {
413 scalingType = Type::UB;
414 if (m_opSpec->isBranching())
415 scalingType = Type::D;
416 }
417
418 dri.regRef.subRegNum = (uint16_t)BinaryOffsetToSubReg(
419 dri.regRef.subRegNum, dri.regName, scalingType, m_model.platform);
420
421 return dri;
422 }
423
424 template <SourceIndex S> uint32_t decodeSrcVertStride();
425 template <SourceIndex S> uint32_t decodeSrcWidth();
426 template <SourceIndex S> uint32_t decodeSrcHorzStride();
427 template <SourceIndex S> uint32_t decodeSrcChanSel();
428 template <SourceIndex S> GED_REP_CTRL decodeSrcRepCtrl();
429 template <SourceIndex S> uint8_t decodeSrcCtxSvRstAccBitsToRegNum();
430 void decodeChSelToSwizzle(uint32_t chanSel, GED_SWIZZLE swizzle[4]);
431 template <SourceIndex S> bool isChanSelPacked();
432
433 void decodeOptions(Instruction *inst);
434
435 protected:
436 GED_MODEL m_gedModel;
437
438 // decode-level state (valid below decodeKernel variants)
439 Kernel *m_kernel = nullptr;
440
441 // state shared below decodeInstToBlock()
442 // info about the instruction being converted to IGA IR
443 ged_ins_t m_currGedInst;
444 const OpSpec *m_opSpec = nullptr;
445 Subfunction m_subfunc;
446 const void *m_binary = nullptr;
447
448 // SWSB encoding mode
449 SWSB_ENCODE_MODE m_SWSBEncodeMode = SWSB_ENCODE_MODE::SWSBInvalidMode;
450
451 // for GED workarounds: grab specific bits from the current instruction
452 uint32_t getBitField(int ix, int len) const;
453
454 // helper for inserting instructions for decode errors
455 Instruction *createErrorInstruction(Kernel &kernel, const char *message,
456 const void *binary, int32_t iLen);
457
458 // used by the GED_DECODE macros
459 void handleGedDecoderError(int line, const char *field,
460 GED_RETURN_VALUE status);
461 }; // end class Decoder
462
463 } // namespace iga
464
465 namespace iga {
466 typedef Decoder Decoder;
467 }
468
469 #endif // end: _IGA_DECODER_H_