Debian Codegraph @

visa/iga/IGALibrary/Backend/Messages/MessageDecoder.hpp

raw
1 /*========================== begin_copyright_notice ============================
2
3 Copyright (C) 2020-2021 Intel Corporation
4
5 SPDX-License-Identifier: MIT
6
7 ============================= end_copyright_notice ===========================*/
8
9 #ifndef _IGA_BACKEND_MESSAGES_MESSAGEDECODER_HPP_
10 #define _IGA_BACKEND_MESSAGES_MESSAGEDECODER_HPP_
11
12 #include "../../Frontend/IRToString.hpp"
13 #include "../../IR/Messages.hpp"
14 #include "../../Models/Models.hpp"
15 #include "../../asserts.hpp"
16 #include "../Native/Field.hpp"
17
18 #include <algorithm>
19 #include <functional>
20 #include <sstream>
21 #include <tuple>
22 #include <vector>
23
24 namespace iga {
25 using DescFieldFormatter = std::function<void(std::stringstream &, uint32_t)>;
26 static inline void NO_DECODE(std::stringstream &, uint32_t) {}
27
28 struct MessageDecoder {
29 // inputs
30 const Model &decodeModel;
31 const SFID sfid;
32 ExecSize instExecSize;
33 const SendDesc desc, exDesc;
34 uint32_t exImmOffDesc;
35
36 // outputs
37 DecodeResult &result;
38
39 const int DEFAULT_EXEC_SIZE, BITS_PER_REGISTER;
40
41 MessageDecoder(Platform _platform, SFID _sfid, ExecSize _instExecSize,
42 uint32_t _exImmOffDesc, SendDesc _exDesc, SendDesc _desc,
43 DecodeResult &_result)
44 : decodeModel(Model::LookupModelRef(_platform)), sfid(_sfid),
45 instExecSize(_instExecSize), desc(_desc), exDesc(_exDesc),
46 exImmOffDesc(_exImmOffDesc), result(_result),
47 DEFAULT_EXEC_SIZE((_platform >= Platform::XE_HPC) ? 32 : 16),
48 BITS_PER_REGISTER((_platform >= Platform::XE_HPC) ? 512 : 256) {
49 result.info.op = SendOp::INVALID;
50 result.info.cachingL3 = result.info.cachingL1 = CacheOpt::DEFAULT;
51 result.info.elemSizeBitsRegFile = result.info.elemSizeBitsMemory = 0;
52 result.info.channelsEnabled = result.info.elemsPerAddr = 0;
53 result.info.execWidth =
54 _instExecSize != ExecSize::INVALID ? int(_instExecSize) : 0;
55 result.info.attributeSet = MessageInfo::Attr::NONE;
56 result.info.addrType = AddrType::FLAT;
57 result.info.surfaceId = 0;
58 result.info.immediateOffset = 0;
59 //
60 // syntax.sfid = _sfid;
61 result.syntax.controls = "." + ToSyntax(_sfid);
62 //
63 decodePayloadSizes();
64 }
65
66 Platform platform() const { return model().platform; }
67
68 const Model &model() const { return decodeModel; }
69
70 bool platformInRange(Platform lo, Platform hi) const {
71 return platform() >= lo && platform() <= hi;
72 }
73
74 void setDoc(const char *doc) { setDoc(doc, doc, doc); }
75 void setDoc(const char *preXe, const char *xe, const char *xe23) {
76 addDoc(DocRef::INVALID, nullptr, chooseDoc(preXe, xe, xe23));
77 }
78 void addDocXe(DocRef::Kind k, const char *name, const char *linkXe,
79 const char *linkXe2) {
80 if (platform() >= Platform::XE2) {
81 addDoc(k, name, linkXe2);
82 } else {
83 addDoc(k, name, linkXe);
84 }
85 }
86 void addDoc(DocRef::Kind k, const char *name, const char *link) {
87 if (k == DocRef::INVALID && !name && !link)
88 return;
89 result.info.refs.emplace_back(k, name, link);
90 }
91 void addDocs(DocRef::Kind k0, const char *name0, const char *link0,
92 DocRef::Kind k1, const char *name1, const char *link1,
93 DocRef::Kind k2 = DocRef::INVALID, const char *name2 = nullptr,
94 const char *link2 = nullptr,
95 DocRef::Kind k3 = DocRef::INVALID, const char *name3 = nullptr,
96 const char *link3 = nullptr) {
97 addDoc(k0, name0, link0);
98 addDoc(k1, name1, link1);
99 addDoc(k2, name2, link2);
100 addDoc(k3, name3, link3);
101 }
102 const char *chooseDoc(const char *preXe, const char *xe,
103 const char *xe23) const {
104 preXe = preXe ? preXe : "?";
105 xe = xe ? xe : "?";
106 xe23 = xe23 ? xe23 : "?";
107 return platform() < Platform::XE ? preXe
108 : platform() < Platform::XE2 ? xe
109 : xe23;
110 }
111 void addDocRefDESC(const char *type, const char *xeRef, const char *xe2Ref) {
112 if (platform() >= Platform::XE2) {
113 addDoc(DocRef::DESC, type, xe2Ref);
114 } else if (platform() >= Platform::XE) {
115 addDoc(DocRef::DESC, type, xeRef);
116 }
117 }
118 void addDocRefDESCXE2(
119 const char *xe2Type, const char *xe2Ref)
120 {
121 if (platform() >= Platform::XE2) {
122 addDoc(DocRef::DESC, xe2Type, xe2Ref);
123 }
124 }
125
126 /////////////////////////////////////////////////////////////
127 // diagnostics
128 template <typename T1, typename T2 = const char *, typename T3 = const char *>
129 void addDiag(DiagnosticList &dl, int off, int len, T1 t1, T2 t2 = "",
130 T3 t3 = "") {
131 std::stringstream ss;
132 ss << t1 << t2 << t3;
133 dl.emplace_back(DescField(off, len), ss.str());
134 }
135 template <typename T1, typename T2 = const char *, typename T3 = const char *>
136 void warning(int off, int len, T1 t1, T2 t2 = "", T3 t3 = "") {
137 addDiag(result.warnings, off, len, t1, t2, t3);
138 }
139 template <typename T1, typename T2 = const char *, typename T3 = const char *>
140 void error(int off, int len, T1 t1, T2 t2 = "", T3 t3 = "") {
141 addDiag(result.errors, off, len, t1, t2, t3);
142 }
143
144 // offset +32 to 64 fetch from exDesc
145 // peeks at a field without adding it
146 uint32_t getDescBits(int off, int len) const {
147 uint32_t bits = desc.imm;
148 if (off >= 32) {
149 off -= 32;
150 bits = exDesc.imm;
151 }
152 uint32_t mask = len == 32 ? 0xFFFFFFFF : ((1 << len) - 1);
153 return (int)((bits >> off) & mask);
154 }
155
156 uint32_t getDescBit(int off) const { return getDescBits(off, 1) != 0; }
157
158 uint32_t decodeExDescField(const char *fieldName, int off, int len,
159 DescFieldFormatter fmtMeaning = NO_DECODE) {
160 auto val = getDescBits(off + 32, len);
161 std::stringstream ss;
162 fmtMeaning(ss, val);
163 addField(fieldName, off + 32, len, val, ss.str());
164 return val;
165 }
166 uint32_t decodeExDescReserved(int off, int len) {
167 return decodeReserved(off + 32, len);
168 }
169 uint32_t decodeDescField(const char *fieldName, int off, int len,
170 DescFieldFormatter fmtMeaning = NO_DECODE) {
171 auto val = getDescBits(off, len);
172 std::stringstream ss;
173 fmtMeaning(ss, val);
174 addField(fieldName, off, len, val, ss.str());
175 return val;
176 }
177 uint32_t decodeDescBitField(const char *fieldName, int off, const char *zero,
178 const char *one) {
179 return decodeDescField(
180 fieldName, off, 1,
181 [&](std::stringstream &ss, uint32_t val) { ss << (val ? one : zero); });
182 }
183 uint32_t decodeDescBitField(const char *fieldName, int off, const char *one) {
184 return decodeDescBitField(fieldName, off, "", one);
185 }
186
187 // normally use getDescBitsField, but in cases where you've already
188 // decoded, the meaning and just want to record the result
189 void addField(const char *fieldName, int off, int len, uint32_t val,
190 const std::string& meaning) {
191 Fragment f(fieldName, off, len);
192 for (const auto &fvs : result.fields) {
193 const auto &f1 = std::get<0>(fvs);
194 if (f1.overlaps(f)) {
195 // uncomment for debugging
196 // std::stringstream ss;
197 // ss << "overlapped fields: " << f1.name << " and " << f.name;
198 // IGA_ASSERT_FALSE(ss.str().c_str());
199 return; // replicated access (don't record again)
200 }
201 }
202 result.fields.emplace_back(f, val, meaning);
203 }
204
205 ///////////////////////////////////////////////////////////////////////////
206 // decoder helpers
207 bool decodeExpected(int off, int len, const char *fieldName,
208 uint32_t expected) {
209 auto val = getDescBits(off, len);
210 if (val != expected) {
211 warning(off, len, "field should be ", expected);
212 }
213 addField(fieldName, off, len, val, "");
214 return val == expected;
215 }
216 uint32_t decodeReserved(int off, int len) {
217 auto val = getDescBits(off, len);
218 if (val != 0) {
219 warning(off, len, "reserved field should be zero");
220 }
221 addField("Reserved", off, len, val, "");
222 return val;
223 }
224
225 // decodes MLen, RLen, and XLen if present
226 // (Src0.Length, Dst.Length, Src1.Length)
227 void decodePayloadSizes();
228
229 ///////////////////////////////////////////////////////////////////////////
230 // the most generic setter
231 void
232 setScatterGatherOpX(const std::string& msgSym, const std::string& msgImpl, const SendOp& op,
233 const AddrType& addrType, const SendDesc& surfaceId, const CacheOpt& l1,
234 const CacheOpt& l3, int addrSize, int bitsPerElemReg,
235 int bitsPerElemMem, int elemsPerAddr, int simd,
236 const MessageInfo::Attr& extraAttrs = MessageInfo::Attr::NONE) {
237 MessageInfo &mi = result.info;
238 mi.symbol = msgSym;
239 mi.description = msgImpl;
240 mi.op = op;
241 mi.cachingL1 = l1;
242 mi.cachingL3 = l3;
243 mi.addrType = addrType;
244 mi.surfaceId = surfaceId;
245 mi.attributeSet |= extraAttrs;
246 mi.addrSizeBits = addrSize;
247 mi.elemSizeBitsRegFile = bitsPerElemReg;
248 mi.elemSizeBitsMemory = bitsPerElemMem;
249 mi.elemsPerAddr = elemsPerAddr;
250 mi.channelsEnabled = 0;
251 mi.execWidth = simd;
252 }
253
254 void
255 setScatterGatherOp(const std::string &msgSym, const std::string &msgDesc, SendOp op,
256 AddrType addrType, SendDesc surfaceId, int addrSize,
257 int bitsPerElem, int elemsPerAddr, int simd,
258 MessageInfo::Attr extraAttrs = MessageInfo::Attr::NONE) {
259 setScatterGatherOpX(msgSym, msgDesc, op, addrType, surfaceId,
260 CacheOpt::DEFAULT, CacheOpt::DEFAULT, addrSize,
261 bitsPerElem, bitsPerElem, elemsPerAddr, simd,
262 extraAttrs);
263 }
264
265 // for miscellaneous stuff such as fences and whatnot
266 //
267 // treat the payloads as full register units and set the op to SIMD1
268 void setSpecialOpX(const std::string& msgSym, const std::string& msgDesc, SendOp op,
269 AddrType addrType, SendDesc surfaceId, int mlen, int rlen,
270 MessageInfo::Attr extraAttrs = MessageInfo::Attr::NONE) {
271 MessageInfo &mi = result.info;
272 mi.symbol = msgSym;
273 mi.description = msgDesc;
274 mi.op = op;
275 mi.cachingL1 = CacheOpt::DEFAULT;
276 mi.cachingL3 = CacheOpt::DEFAULT;
277 mi.addrType = addrType;
278 mi.surfaceId = surfaceId;
279 mi.addrSizeBits = mlen * BITS_PER_REGISTER;
280 // e.g. SIMD16 platforms are 256b (two full registers)
281 mi.elemSizeBitsRegFile = rlen * BITS_PER_REGISTER;
282 mi.elemSizeBitsMemory = mi.elemSizeBitsRegFile;
283 mi.channelsEnabled = 0;
284 mi.elemsPerAddr = 1;
285 mi.execWidth = 1;
286 mi.attributeSet = extraAttrs | MessageInfo::Attr::VALID;
287 }
288
289 // shared by subclasses
290 void addLscFenceFields(std::stringstream &sym, std::stringstream &desc);
291 void addLscFenceScopeField(std::stringstream &sym, std::stringstream &desc);
292 void addLscFencePortFields(std::stringstream &sym, std::stringstream &desc);
293 }; // MessageDecoder
294
295 ///////////////////////////////////////////////////////////////////////////
296 // shared by MessageDecoderHDC, MessageDecoderOther
297 struct MessageDecoderLegacy : MessageDecoder {
298 static const int SLM_BTI = 0xFE;
299 static const int COHERENT_BTI = 0xFF;
300 static const int NONCOHERENT_BTI = 0xFD;
301
302 MessageDecoderLegacy(Platform _platform, SFID _sfid, ExecSize _instExecSize,
303 uint32_t exImmOffDesc, SendDesc _exDesc, SendDesc _desc,
304 DecodeResult &_result)
305 : MessageDecoder(_platform, _sfid, _instExecSize, exImmOffDesc, _exDesc,
306 _desc, _result) {}
307
308 // from legacy encodings
309 int decodeBTI(int addrBits) {
310 int bti = (int)getDescBits(0, 8);
311 std::stringstream ss;
312 ss << "surface " << bti;
313 if (bti == SLM_BTI) {
314 ss << " (SLM)";
315 } else if (bti == COHERENT_BTI) {
316 if (addrBits == 64)
317 ss << " A64 ";
318 else
319 ss << " A32 ";
320 ss << " (coherent stateless)";
321 } else if (bti == NONCOHERENT_BTI) {
322 if (addrBits == 64)
323 ss << " A64";
324 else
325 ss << " A32";
326 ss << " (incoherent stateless)";
327 } else if (bti == 0xFC) {
328 ss << " (SSO)";
329 }
330 addField("BTI", 0, 8, bti, ss.str());
331 return bti;
332 }
333
334 /////////////////////////////////////////////////////
335 // "header" decoding
336 bool decodeMDC_H() { // optional
337 return decodeDescBitField("Header", 19, "absent", "included") != 0;
338 }
339 void decodeMDC_HF() {
340 if (getDescBit(19) != 0)
341 warning(19, 1, "this message forbids a header (and it's included)");
342 }
343 void decodeMDC_HR() {
344 if (!decodeMDC_H())
345 warning(19, 1, "this message requires a header (and it's absent)");
346 }
347 bool decodeMDC_H2() {
348 return decodeDescBitField("DualHeader", 19, "absent",
349 "included (two register header)") != 0;
350 }
351
352 ///////////////////////////////////////////////////////////////////////
353 // some shared decoder helpers
354 int decodeMDC_SM2(int off) {
355 // yeah SM2 is really 1 bit (2 means two values)
356 int bits = decodeDescBitField("SimdMode:MDC_SM2", off, "SIMD8", "SIMD16");
357 return bits ? 16 : 8;
358 }
359 }; // MessageDecoderLegacy
360
361 // see MessageDecoderHDC.cpp
362 void decodeDescriptorsHDC(Platform platform, SFID sfid, ExecSize execSize,
363 uint32_t exImmOffDesc, SendDesc exDesc, SendDesc desc,
364 DecodeResult &result);
365
366 // see MessageDecoderOther.cpp
367 void decodeDescriptorsOther(Platform platform, SFID sfid, ExecSize execSize,
368 uint32_t exImmOffDesc, SendDesc exDesc,
369 SendDesc desc, DecodeResult &result);
370
371 void decodeDescriptorsLSC(Platform platform, SFID sfid, ExecSize execSize,
372 uint32_t exImmOffDesc, SendDesc exDesc, SendDesc desc,
373 DecodeResult &result);
374
375 bool encodeDescriptorsLSC(Platform p, const VectorMessageArgs &vma,
376 uint32_t &exImmOffDesc, SendDesc &exDesc,
377 SendDesc &desc, std::string &err);
378
379 // Different parameters that sampler messages allow.
380 enum class SamplerParam {
381 NONE = 0,
382 AI, // array index
383 BIAS, // depth bias
384 BIAS_AI, // bias packed with AI
385 DUDX, // u derivative with respect to x
386 DUDY, // u derivative with respect to y
387 DUMMY, // dummy parameter for messages with no arguments
388 // sampler send requires src0 to be something
389 DVDX, // v derivative with respect to x
390 DVDY, // v derivative with respect to y
391 LOD, // level of detail
392 LOD_AI, // legel of detail packed with array index
393 MCS0, // multi control surface buffer 0
394 MCS1, // multi control surface buffer 1
395 MCS2, // multi control surface buffer 2
396 MCS3, // multi control surface buffer 3
397 MLOD, // min level of detail
398 MLOD_R, // mlod packed with R
399 R, // r-coordinate (depth)
400 REF, // reference value (_c)
401 SI, // sampler index?
402 U, // u-coordinate
403 V, // v-coordinate
404 OFFUV_R, // r parameter combined with u and v offsets
405 OFFUVR, // u, v, and r offsets combined into a single parameter
406 OFFUV, // u and v offsets combined into a single parameter
407 OFFUVR_R, // r parameter combine with u, v, and r offsets
408 BIAS_OFFUVR, // bias parameter and u, v, r offsets combined
409 BIAS_OFFUV, // bias parameter and u, v offsets combined
410 LOD_OFFUV, // LOD parameter and u, v offsets combined
411 LOD_OFFUVR, // LOD parameter and u, v, and r offsets combined
412 }; // SamplerParam
413
414 struct SamplerMessageDescription {
415 SendOp op;
416 const char *mnemonic;
417 int required;
418 SamplerParam params[8];
419
420 constexpr SamplerMessageDescription(SendOp o, const char *mne, int reqd,
421 SamplerParam p0 = SamplerParam::NONE,
422 SamplerParam p1 = SamplerParam::NONE,
423 SamplerParam p2 = SamplerParam::NONE,
424 SamplerParam p3 = SamplerParam::NONE,
425 SamplerParam p4 = SamplerParam::NONE,
426 SamplerParam p5 = SamplerParam::NONE,
427 SamplerParam p6 = SamplerParam::NONE,
428 SamplerParam p7 = SamplerParam::NONE)
429 : op(o), mnemonic(mne),
430 required(reqd), params{p0, p1, p2, p3, p4, p5, p6, p7} {}
431
432 int countParams() const;
433
434 std::string describe(int srcsLen) const;
435 std::string describeParams(int srcsLen) const;
436 }; // SamplerMessageDescription
437
438 std::string ToSymbol(SamplerParam sp);
439 SamplerMessageDescription
440 LookupSamplerMessageDescription(uint32_t smplOpEnc, int execSize, bool isFp16);
441 } // namespace iga
442
443 #endif // _IGA_BACKEND_MESSAGES_MESSAGEDECODER_HPP_