Debian Codegraph @

visa/iga/IGALibrary/IR/Messages.cpp

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 #include "Messages.hpp"
10 #include "../Backend/Messages/MessageDecoder.hpp"
11 #include "../Backend/Messages/E64/MessageDecoderE64.hpp"
12 #include "../Frontend/IRToString.hpp"
13
14 #include <sstream>
15 #include <string>
16
17 using namespace iga;
18
19 ///////////////////////////////////////////////////////////////////////////////
20 static void deducePayloadSizes(PayloadLengths &lens, Platform p, SFID sfid,
21 ExecSize execSize, uint32_t _desc) {
22 SendDesc desc(_desc), exDesc(0);
23
24 const auto result = tryDecode(p, sfid, execSize, exDesc, desc, nullptr);
25 if (!result) {
26 return;
27 }
28 const MessageInfo &mi = result.info;
29
30 const int REG_SIZE_BITS = p >= Platform::XE_HPC ? 512 : 256;
31 const int FULL_EXEC_SIZE = p >= Platform::XE_HPC ? 32 : 16;
32
33 int execElems = int(execSize);
34 if (execElems < FULL_EXEC_SIZE / 2)
35 execElems = FULL_EXEC_SIZE / 2;
36
37 auto isVectorMessageSimd1 = [&]() {
38 bool isSimd1 = mi.isTransposed() || mi.op == SendOp::LOAD_STRIDED ||
39 mi.op == SendOp::STORE_STRIDED;
40 isSimd1 |= mi.op == SendOp::LOAD_BLOCK2D || mi.op == SendOp::STORE_BLOCK2D;
41 return isSimd1;
42 };
43
44 // messages of the form:
45 // send* null reg null ...
46 // with src0 len of 1
47 auto message = [&](int dstLen = 0, int src0Len = 1, int src1Len = 0) {
48 lens.dstLen = dstLen;
49 lens.src0Len = src0Len;
50 lens.src1Len = src1Len;
51 };
52
53 auto numAddrRegsForVector = [&]() {
54 if (isVectorMessageSimd1()) {
55 return 1;
56 } else {
57 int alen = execElems * mi.addrSizeBits / REG_SIZE_BITS;
58 if (execElems * mi.addrSizeBits % REG_SIZE_BITS)
59 alen++;
60 return alen;
61 }
62 };
63
64 auto handleVectorMessage = [&](int atomicParams = -1) {
65 int dlen = -1;
66 if (mi.isTransposed()) {
67 dlen = mi.elemSizeBitsRegFile * mi.elemsPerAddr / REG_SIZE_BITS;
68 if ((mi.elemSizeBitsRegFile * mi.elemsPerAddr) % REG_SIZE_BITS != 0)
69 dlen++;
70 } else {
71 dlen = execElems * mi.elemSizeBitsRegFile / REG_SIZE_BITS;
72 if ((execElems * mi.elemSizeBitsRegFile) % REG_SIZE_BITS != 0)
73 dlen++;
74 dlen *= mi.elemsPerAddr;
75 }
76 //
77 const auto &opInfo = lookupSendOp(mi.op);
78 lens.src0Len = numAddrRegsForVector();
79 if (opInfo.isLoad()) {
80 lens.dstLen = dlen;
81 lens.src1Len = 0;
82 } else if (opInfo.isStore()) {
83 lens.dstLen = 0;
84 lens.src1Len = dlen;
85 } else if (opInfo.isAtomic()) {
86 lens.dstLen = dlen;
87 lens.src1Len = atomicParams;
88 } else {
89 IGA_ASSERT_FALSE("invalid message type");
90 }
91 };
92 lens.uvrlod = mi.hasAttr(MessageInfo::Attr::TYPED);
93
94 switch (mi.op) {
95 ///////////////////////////////////////////////////////////////////////
96 // easy and common cases
97 case SendOp::LOAD:
98 case SendOp::LOAD_STRIDED:
99 case SendOp::LOAD_QUAD:
100 case SendOp::STORE:
101 case SendOp::STORE_STRIDED:
102 case SendOp::STORE_QUAD:
103 case SendOp::STORE_UNCOMPRESSED:
104 case SendOp::STORE_UNCOMPRESSED_QUAD:
105 // normal vector message
106 handleVectorMessage();
107 break;
108 ///////////////////////////////////////////////////////////////////////
109 // slightly harder
110 case SendOp::LOAD_STATUS:
111 // returns one status register only
112 // cheat so num addr regs and src1 are set;
113 // then stomping the return value to reflect the 32b return value
114 handleVectorMessage();
115 lens.dstLen = 1;
116 break;
117 case SendOp::READ_STATE:
118 // src0 is U, V, R, LOD 32b each (128b total)
119 // dst is 1 reg (with room to spare)
120 message(1, 1, 0);
121 if (p > Platform::XE_HPG)
122 lens.src0Len = 2; // XeHPG passes 2 GRFs
123 break;
124 case SendOp::LOAD_BLOCK2D:
125 // we can't deduce data size because the tile sizes are part of
126 // the payload
127 message(-1, 1, 0);
128 break;
129 case SendOp::STORE_BLOCK2D:
130 // we can't deduce data size because the tile sizes are part of
131 // the payload
132 message(0, 1, -1);
133 break;
134 case SendOp::ATOMIC_LOAD:
135 case SendOp::ATOMIC_IINC:
136 case SendOp::ATOMIC_IDEC:
137 case SendOp::ATOMIC_IPDEC:
138 handleVectorMessage(0);
139 break;
140 case SendOp::ATOMIC_STORE:
141 case SendOp::ATOMIC_AND:
142 case SendOp::ATOMIC_XOR:
143 case SendOp::ATOMIC_OR:
144 case SendOp::ATOMIC_IADD:
145 case SendOp::ATOMIC_ISUB:
146 case SendOp::ATOMIC_IRSUB:
147 case SendOp::ATOMIC_SMIN:
148 case SendOp::ATOMIC_SMAX:
149 case SendOp::ATOMIC_UMIN:
150 case SendOp::ATOMIC_UMAX:
151 case SendOp::ATOMIC_FADD:
152 case SendOp::ATOMIC_FSUB:
153 case SendOp::ATOMIC_FMIN:
154 case SendOp::ATOMIC_FMAX:
155 handleVectorMessage(1);
156 break;
157 case SendOp::ATOMIC_ACADD:
158 case SendOp::ATOMIC_ACSUB:
159 case SendOp::ATOMIC_ACSTORE:
160 // similar to other atomics except
161 // - addresses are implied (not in src0)
162 // - src0 takes the data (not src1)
163 // - src1 is null
164 // make handleVectorMessage do the heavy lifting and then just
165 // shuffle results around as needed
166 handleVectorMessage(1);
167 lens.src0Len = lens.src1Len;
168 lens.src1Len = 0;
169 break;
170 case SendOp::ATOMIC_ICAS:
171 case SendOp::ATOMIC_FCAS:
172 handleVectorMessage(2);
173 break;
174 ///////////////////////////////////////////////////////////////////////
175 // barrier is 1, 1, 0 until XeHP
176 case SendOp::SIGNAL_BARRIER:
177 message(p < Platform::XE_HP ? 1 : 0, 1, 0);
178 break;
179 // other control messages that are 0, 1, 0
180 case SendOp::MONITOR:
181 case SendOp::UNMONITOR:
182 case SendOp::SIGNAL:
183 case SendOp::EOT:
184 message();
185 break;
186 ///////////////////////////////////////////////////////////////////////
187 // control messages that return a register
188 case SendOp::FENCE:
189 message(1, 1, 0);
190 break;
191 case SendOp::WAIT:
192 message(1, 1, 0);
193 if (p >= Platform::XE_HPG) // except on XeHPG+ (uses n0.0 bit 0)
194 message(0, 1, 0);
195 break;
196 case SendOp::CCS_PC:
197 case SendOp::CCS_PU:
198 message(0, 1, 0);
199 break;
200 case SendOp::CCS_SC:
201 case SendOp::CCS_SU:
202 message(0, numAddrRegsForVector(), 0);
203 break;
204 default:
205 break;
206 }
207 }
208
209 PayloadLengths::PayloadLengths(Platform p, SFID sfid, ExecSize execSize,
210 uint32_t desc) {
211 deducePayloadSizes(*this, p, sfid, execSize, desc);
212 }
213
214 PayloadLengths::PayloadLengths(Platform p, ExecSize execSize, uint32_t desc,
215 uint32_t exDesc) {
216 IGA_ASSERT(p <= Platform::GEN11, "wrong constructor for platform");
217 SFID sfid = sfidFromEncoding(p, exDesc);
218 deducePayloadSizes(*this, p, sfid, execSize, desc);
219 }
220
221 SFID iga::sfidFromEncoding(Platform p, uint32_t sfidBits) {
222 SFID sfid = SFID::INVALID;
223 switch (sfidBits & 0xF) {
224 case 0x0:
225 sfid = SFID::NULL_;
226 break;
227 case 0x1:
228 sfid = SFID::UGML;
229 break;
230 case 0x2:
231 sfid = SFID::SMPL;
232 break;
233 case 0x3:
234 sfid = SFID::GTWY;
235 break;
236 case 0x4:
237 sfid = SFID::DC2;
238 break;
239 case 0x5:
240 sfid = SFID::RC;
241 break;
242 case 0x6:
243 sfid = SFID::URB;
244 break;
245 case 0x7:
246 sfid = SFID::TS;
247 if (p >= Platform::XE_HPG) {
248 sfid = SFID::BTD;
249 }
250 break;
251 case 0x8:
252 sfid = SFID::VME;
253 if (p >= Platform::XE_HPG) {
254 sfid = SFID::RTA;
255 }
256 break;
257 case 0x9:
258 sfid = SFID::DCRO;
259 break;
260 case 0xA:
261 sfid = SFID::DC0;
262 break;
263 case 0xB:
264 sfid = SFID::PIXI;
265 break;
266 case 0xC:
267 sfid = SFID::DC1;
268 break;
269 case 0xD:
270 sfid = SFID::CRE;
271 if (p >= Platform::XE_HPG) {
272 sfid = SFID::TGM;
273 }
274 break;
275 case 0xE:
276 sfid = SFID::SLM;
277 break;
278 case 0xF:
279 sfid = SFID::UGM;
280 break;
281 default:
282 sfid = SFID::INVALID;
283 }
284 return sfid;
285 }
286
287 ///////////////////////////////////////////////////////////////////////////////
288 std::string MessageSyntax::str(std::string execInfo, std::string dataReg,
289 std::string addrReg,
290 std::string atmoicArgReg) const {
291 std::stringstream ss;
292
293 auto emitAddr = [&]() {
294 if (addrReg.empty())
295 return;
296 ss << " " << surface;
297 ss << "[";
298 ss << scale;
299 ss << addrReg;
300 ss << immOffset;
301 ss << "]";
302 };
303 auto emitData = [&]() {
304 if (!dataReg.empty()) {
305 ss << " " << dataReg;
306 }
307 };
308 auto emitExtraArg = [&]() {
309 if (!atmoicArgReg.empty())
310 ss << " " << atmoicArgReg;
311 };
312
313 ss << mnemonic << controls;
314 if (!execInfo.empty())
315 ss << " " << execInfo;
316
317 if (isLoad()) {
318 emitData();
319 emitAddr();
320 } else if (isStore()) {
321 emitAddr();
322 emitData();
323 } else if (isAtomic()) {
324 emitData();
325 emitAddr();
326 emitExtraArg();
327 } else if (isControl()) {
328 // not sure the cleanest way to do this
329 emitData();
330 emitAddr();
331 emitExtraArg();
332 }
333
334 return ss.str();
335 }
336
337 std::string MessageSyntax::sym() const {
338 std::stringstream ss;
339 ss << mnemonic << controls;
340 if (!surface.empty()) {
341 ss << "." << surface;
342 }
343 if (!scale.empty() || !immOffset.empty()) {
344 if (surface.empty())
345 ss << ".flat";
346 ss << "[" << scale << "A" << immOffset << "]";
347 }
348 return ss.str();
349 }
350
351 static constexpr SendOpDefinition SEND_OPS[]{
352 #define DEFINE_SEND_OP(ENUM, MNE, DESC, ATTRS) {SendOp::ENUM, MNE, DESC, ATTRS},
353 #include "EnumSendOpInfo.hpp"
354 #undef DEFINE_SEND_OP
355 };
356
357 std::string iga::ToSyntax(SendOp op) {
358 const auto &opInfo = lookupSendOp(op);
359 if (opInfo.isValid()) {
360 return opInfo.mnemonic;
361 } else {
362 return iga::fmtHex(int(op), 2) + "?";
363 }
364 }
365
366 std::string iga::ToSymbol(AddrType op) {
367 #define MK_CASE(X) \
368 case AddrType::X: \
369 return #X
370 switch (op) {
371 MK_CASE(INVALID);
372 MK_CASE(FLAT);
373 MK_CASE(BSS);
374 MK_CASE(SS);
375 MK_CASE(BTI);
376 MK_CASE(SURF);
377 default:
378 std::stringstream ss;
379 ss << "0x" << std::hex << (int)op << "?";
380 return ss.str();
381 }
382 #undef MK_CASE
383 }
384
385 static bool isLSC(Platform platform, SFID sfid) {
386 switch (sfid) {
387 case SFID::UGM:
388 case SFID::UGML:
389 case SFID::SLM:
390 case SFID::TGM:
391 return true;
392 case SFID::URB:
393 return platform >= Platform::XE2;
394 default:
395 return false;
396 }
397 }
398
399 static bool isHDC(SFID sfid) {
400 switch (sfid) {
401 case SFID::DCRO:
402 case SFID::DC0:
403 case SFID::DC1:
404 case SFID::DC2:
405 return true;
406 default:
407 return false;
408 }
409 }
410
411 ///////////////////////////////////////////////////////////////////////////////
412 // e.g. set valid bit, re-order fields etc...
413 static void postProcessDecode(SendDesc desc, SendDesc exDesc,
414 DecodeResult &result, DecodedDescFields *fields) {
415 if (!result.errors.empty())
416 result.info.attributeSet |= MessageInfo::Attr::VALID;
417 if (fields) {
418 std::sort(result.fields.begin(), result.fields.end(),
419 [&](const auto &f1, const auto &f2) {
420 return std::get<0>(f1).offset > std::get<0>(f2).offset;
421 });
422 *fields = result.fields;
423 //
424 // make sure there aren't unmapped bits
425 // run through all 64 bits of [ExDesc:Desc] and find maximal spans
426 // that have no owner field (breaking between descriptors)
427 auto fieldOwnsBit = [&](int i) {
428 for (const auto &fv : result.fields) {
429 const auto &f = std::get<0>(fv);
430 if (i >= f.offset && i < f.offset + f.length) {
431 return true;
432 }
433 }
434 return false;
435 };
436
437 int len = exDesc.isImm() ? 64 : 32; // 32 if a0
438 auto testDescBit = [&](int ix) {
439 uint32_t val = desc.imm;
440 if (ix >= 32) {
441 ix -= 32;
442 val = exDesc.imm;
443 }
444 return (val & (1 << ix)) != 0;
445 };
446
447 for (int i = 0; i < len;) {
448 if (testDescBit(i) && !fieldOwnsBit(i)) {
449 // beginning of an undefined field
450 int undefLen = 1;
451 while ((i + undefLen) % 32 != 0 && testDescBit(i + undefLen)) {
452 // don't span undefined fields across ExDesc:Desc
453 undefLen++;
454 }
455 //
456 result.warnings.emplace_back(DescField(i, undefLen),
457 "bits set in undefined field");
458 i += undefLen;
459 //
460 // uncomment for linting bad descriptors
461 // std::stringstream ss;
462 // ss << "[" << i << "] = 1 not mapped by field";
463 // IGA_ASSERT_FALSE(ss.str().c_str());
464 } else {
465 i++;
466 }
467 }
468 } // fields
469
470 // set the syntax kind
471 if (result && !result.syntax.mnemonic.empty()) {
472 // !result.syntax.mnemonic.empty(): to filter out HDC stuff until
473 // we get that working
474 if (result.info.isLoad()) {
475 result.syntax.layout = MessageSyntax::Layout::LOAD;
476 } else if (result.info.isStore()) {
477 result.syntax.layout = MessageSyntax::Layout::STORE;
478 } else if (result.info.isAtomic()) {
479 result.syntax.layout = MessageSyntax::Layout::ATOMIC;
480 } else {
481 result.syntax.layout = MessageSyntax::Layout::CONTROL;
482 }
483 }
484 } // postProcessDecode
485
486 DecodeResult iga::tryDecode(const Instruction &i, DecodedDescFields *fields) {
487 if (!i.getOpSpec().isAnySendFormat()) {
488 return DecodeResult();
489 }
490 if (!i.getOpSpec().isSendgFormat()) {
491 return tryDecode(i.getOpSpec().platform, i.getSubfunction().send,
492 i.getExecSize(),
493 i.getExtImmOffDescriptor(),
494 i.getExtMsgDescriptor(), i.getMsgDescriptor(), nullptr);
495 } else {
496 return tryDecode(i.getOpSpec().platform, i.getSubfunction().send,
497 i.getExecSize(), i.getSrc0Length(), i.getSrc1Length(),
498 i.getSendgIndDesc0Reg(), i.getSendgIndDesc1Reg(),
499 i.getSendgDesc(), fields);
500 }
501 }
502
503 DecodeResult iga::tryDecode(Platform platform, SFID sfid, ExecSize execSize,
504 uint32_t exImmOffDesc, SendDesc exDesc,
505 SendDesc desc, DecodedDescFields *fields) {
506 DecodeResult result;
507
508 if (isHDC(sfid)) {
509 decodeDescriptorsHDC(platform, sfid, execSize, exImmOffDesc, exDesc, desc,
510 result);
511 } else if (isLSC(platform, sfid)) {
512 decodeDescriptorsLSC(platform, sfid, execSize, exImmOffDesc, exDesc, desc,
513 result);
514 } else {
515 decodeDescriptorsOther(platform, sfid, execSize, exImmOffDesc, exDesc, desc,
516 result);
517 }
518
519 postProcessDecode(desc, exDesc, result, fields);
520
521 return result;
522 }
523
524 DecodeResult iga::tryDecode(Platform p, SFID sfid, ExecSize execSize,
525 int src0Len, int src1Len, RegRef id0, RegRef id1,
526 uint64_t desc, DecodedDescFields *fields) {
527 DecodeResult dr;
528
529 if (isLSC(p, sfid)) {
530 DecodeMessageLscE64(p, sfid, execSize, src0Len, src1Len, desc, id0, id1, dr);
531 } else if (sfid == SFID::SMPL) {
532 DecodeMessageSamplerE64(p, sfid, execSize, src0Len, src1Len, desc, id0, id1, dr);
533 } else {
534 DecodeMessageOtherE64(p, sfid, execSize, src0Len, src1Len, desc, id0, id1, dr);
535 }
536
537 if (fields) { // Xdsd
538 std::sort(dr.fields.begin(), dr.fields.end(),
539 [&](const auto &f0, const auto &f1) {
540 return std::get<0>(f0).offset > std::get<0>(f1).offset;
541 });
542 auto isUndefined = [&](int ix) {
543 // std::tuple<Fragment,uint32_t,std::string>
544 // top down walk
545 for (int i = (int)dr.fields.size() - 1; i >= 0; i--) {
546 const auto &df = dr.fields[i];
547 const auto &fr = std::get<0>(df);
548 if (ix >= fr.offset && ix < fr.offset + fr.length) {
549 return false;
550 } else if (ix < fr.offset) {
551 break;
552 }
553 }
554 return true;
555 };
556 int bitIx = 0;
557 while (bitIx < 47) {
558 if (isUndefined(bitIx)) {
559 int off = bitIx;
560 bitIx++;
561 while (bitIx < 47 && isUndefined(bitIx)) {
562 bitIx++;
563 }
564 dr.warnings.emplace_back(
565 DescField(off, bitIx - off),
566 "undefined bits (should be field or reserved)");
567 } else {
568 bitIx++;
569 }
570 }
571 *fields = dr.fields;
572 }
573
574 return dr;
575 }
576
577 const SendOpDefinition &iga::lookupSendOp(SendOp op) {
578 for (int i = 0; i < sizeof(SEND_OPS) / sizeof(SEND_OPS[0]); i++) {
579 if (op == SEND_OPS[i].op) {
580 return SEND_OPS[i];
581 }
582 }
583
584 static constexpr SendOpDefinition INVALID(SendOp::INVALID, "?", "?");
585 return INVALID;
586 }
587
588 const SendOpDefinition &iga::lookupSendOp(const char *mnemonic) {
589 std::string mne = mnemonic;
590 for (int i = 0; i < sizeof(SEND_OPS) / sizeof(SEND_OPS[0]); i++) {
591 if (mne == SEND_OPS[i].mnemonic) {
592 return SEND_OPS[i];
593 }
594 }
595 if (mne == "load_cmask")
596 return lookupSendOp(SendOp::LOAD_QUAD);
597 if (mne == "load_cmask_msrt")
598 return lookupSendOp(SendOp::LOAD_QUAD_MSRT);
599 else if (mne == "load_block")
600 return lookupSendOp(SendOp::LOAD_STRIDED);
601 else if (mne == "store_cmask")
602 return lookupSendOp(SendOp::STORE_QUAD);
603 else if (mne == "store_cmask_msrt")
604 return lookupSendOp(SendOp::STORE_QUAD_MSRT);
605 else if (mne == "store_block")
606 return lookupSendOp(SendOp::STORE_STRIDED);
607 static constexpr SendOpDefinition INVALID {SendOp::INVALID, "?", "?"};
608 return INVALID;
609 }
610
611 bool iga::sendOpSupportsSyntax(Platform p, SendOp op, SFID sfid) {
612 bool supported = isLSC(p, sfid);
613 supported &= op == SendOp::LOAD || op == SendOp::LOAD_STRIDED ||
614 op == SendOp::LOAD_QUAD || op == SendOp::STORE ||
615 op == SendOp::STORE_STRIDED || op == SendOp::STORE_QUAD ||
616 op == SendOp::LOAD_BLOCK2D || op == SendOp::STORE_BLOCK2D ||
617 op == SendOp::STORE_UNCOMPRESSED ||
618 op == SendOp::STORE_UNCOMPRESSED_QUAD ||
619 op == SendOp::LOAD_QUAD_MSRT || op == SendOp::STORE_QUAD_MSRT ||
620 // op == SendOp::LOAD_STATUS ||
621 // op == SendOp::FENCE ||
622 // op == SendOp::BARRIER ||
623 lookupSendOp(op).isAtomic();
624 return supported;
625 }
626
627 bool iga::isLscSFID(Platform p, SFID sfid) { return isLSC(p, sfid); }
628
629 bool iga::encodeDescriptors(Platform p, const VectorMessageArgs &vma,
630 uint32_t &exImmOffDesc, SendDesc &exDesc,
631 SendDesc &desc, std::string &err) {
632 if (desc.isReg()) {
633 err = "cannot encode with register desc";
634 return false;
635 }
636 if (isLSC(p, vma.sfid)) {
637 return encodeDescriptorsLSC(p, vma, exImmOffDesc, exDesc, desc, err);
638 }
639 // TODO: support HDC here
640 // encodeVectorMessageHDC(p, vma, exDesc, desc);
641 err = "unsupported message for SFID";
642 return false;
643 }