Debian Codegraph @

visa/iga/IGALibrary/Frontend/Formatter.cpp

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 #include "Formatter.hpp"
10 #include "../ErrorHandler.hpp"
11 #include "../IR/Instruction.hpp"
12 #include "../IR/Messages.hpp"
13 #include "../IR/Types.hpp"
14 #include "../Models/Models.hpp"
15 #include "../strings.hpp"
16 #include "Floats.hpp"
17 #include "FormatterJSON.hpp"
18 #include "IRToString.hpp"
19 #include "SendDescriptorDecoding.hpp"
20 #include "ged.h"
21 #ifndef IGA_DISABLE_ENCODER_EXCEPTIONS
22 #include "../Backend/GED/Interface.hpp"
23 #include "../Backend/Native/Interface.hpp"
24 #endif
25 #include "../Backend/GED/IGAToGEDTranslation.hpp"
26 #include "../Backend/Native/MInst.hpp"
27 #include "../api/iga.h"
28
29 #include <algorithm>
30 #include <cstring>
31 #include <iomanip>
32 #include <sstream>
33 #include <vector>
34
35 namespace iga {
36
37 class Formatter : public BasicFormatter {
38 protected:
39 ErrorHandler &errorHandler;
40 const Model &model;
41 const FormatOpts &opts;
42 struct ColumnPreferences cols;
43 const Instruction *currInst;
44 const uint8_t *currInstBits = nullptr; // optional to render encoding inline
45
46 ansi_esc ANSI_FADED;
47 ansi_esc ANSI_REGISTER(RegName rnm) const {
48 return rnm == RegName::GRF_R ? ANSI_REGISTER_GRF : ANSI_REGISTER_ARF;
49 }
50 ansi_esc ANSI_REGISTER_GRF;
51 ansi_esc ANSI_REGISTER_ARF;
52 ansi_esc ANSI_FLAGMODIFIER;
53 ansi_esc ANSI_IMMEDIATE;
54 ansi_esc ANSI_MNEMONIC;
55 ansi_esc ANSI_SUBFUNCTION;
56 ansi_esc ANSI_COMMENT;
57
58 void formatDstOp(const Instruction &i);
59 // prior platform used regular fmtDstOp with "implicit types"...
60 void formatSendDstOp(const Instruction &i);
61
62 void formatSrcOp(SourceIndex ix, const Instruction &i);
63
64 void formatSyncAllSrc0(const Instruction &i);
65 void formatBareRegisterUnescaped(RegName regName, int regNum);
66 void formatRegister(RegName rnm, RegRef reg, bool emitSubReg = true,
67 bool isSIMT = false);
68 void formatSendSrcWithLength(const Operand &src, int srcLen);
69 void formatDstType(const OpSpec &os, Type type);
70 void formatSrcBare(const Operand &src);
71
72 public:
73 Formatter(ErrorHandler &err, std::ostream &out, const FormatOpts &fopts,
74 const ColumnPreferences &colPrefs = ColumnPreferences())
75 : BasicFormatter(fopts.printAnsi, out), errorHandler(err),
76 model(fopts.model), opts(fopts), cols(colPrefs), currInst(nullptr) {
77 // TODO: could make these mappable via environment variable
78 //
79 // export IGA_FormatAnsiRegisterArf="\033[38;2;138;43;211m"
80 // export IGA_FormatAnsiMnemonic=...
81 // export IGA_FormatAnsiComment=...
82 //
83 // (Put this in system.cpp; iga::LookupEnvVar(...))
84 if (fopts.printAnsi) {
85 ANSI_FADED = "\033[38;2;128;128;128m";
86 ANSI_REGISTER_GRF = "\033[38;2;255;192;203m";
87 ANSI_REGISTER_ARF = "\033[38;2;255;218;185m";
88 ANSI_IMMEDIATE = "\033[38;2;221;160;221m";
89 ANSI_FLAGMODIFIER = "\033[38;2;221;160;221m";
90 ANSI_MNEMONIC = "\033[38;2;10;153;215m"; // Intel Blue
91 ANSI_SUBFUNCTION = "\033[38;2;176;224;230m";
92 ANSI_COMMENT = "\033[38;2;128;128;128m";
93 }
94 }
95
96 Platform platform() const { return model.platform; }
97
98 // labels are of the form "L" + off, but we prefix a
99 // "_N" for negative.
100 // E.g. "L64" is PC 64.
101 // E.g. "L_N16" is -16 ("(N)egative")
102 static void getDefaultLabelDefinition(std::ostream &o, int32_t pc) {
103 o << "L";
104 if (pc < 0) {
105 o << "_N";
106 pc = -pc;
107 }
108 o << pc;
109 }
110
111 void formatLabel(int32_t pc) {
112 bool labeled = false;
113 if (opts.labeler) {
114 const char *userLabel = (*opts.labeler)(pc, opts.labelerContext);
115 if (userLabel) {
116 emit(userLabel);
117 labeled = true;
118 }
119 }
120
121 // use a default label if there isn't a labeler or the user callback
122 // returned nullptr
123 if (!labeled) {
124 if (opts.numericLabels) {
125 // Raw numbers for labels
126 emitDecimal(pc);
127 } else {
128 Formatter::getDefaultLabelDefinition(o, pc);
129 }
130 }
131 }
132
133 void formatKernel(const Kernel &k, const void *vbits) {
134 currInstBits = (const uint8_t *)vbits;
135 if (opts.printInstDefs && opts.liveAnalysis) {
136 std::stringstream ss;
137 ss << "// itrs: " << opts.liveAnalysis->iterations << "\n";
138 ss << "// live-in:\n";
139 std::vector<const Dep *> liveIn;
140 for (const Dep &d : opts.liveAnalysis->liveIn) {
141 liveIn.push_back(&d);
142 }
143 std::sort(
144 liveIn.begin(), liveIn.end(), [&](const Dep *d1, const Dep *d2) {
145 if (d1->use->getID() < d2->use->getID())
146 return true;
147 else if (d1->def && d2->def && d1->def->getID() < d2->def->getID())
148 return true;
149 else
150 return false;
151 });
152 for (const Dep *d : liveIn) {
153 ss << "// ";
154 d->str(ss);
155 ss << "\n";
156 }
157 emitAnsi(ANSI_FADED, ss.str());
158 }
159
160 for (const Block *b : k.getBlockList()) {
161 if (!opts.numericLabels) {
162 formatLabel(b->getPC());
163 emit(':');
164 newline();
165 }
166
167 formatBlockContents(*b);
168 }
169 }
170
171 void formatBlockContents(const Block &b) {
172 for (const auto &i : b.getInstList()) {
173 formatInstruction(*i);
174 newline();
175
176 if (currInstBits) {
177 currInstBits += i->hasInstOpt(InstOpt::COMPACTED) ? 8 : 16;
178 }
179 }
180 }
181
182 void formatPrefixComment(const Instruction &i, const void *vbits) {
183 bool printBits = opts.printInstBits && currInstBits != nullptr;
184 bool printInstId = opts.printInstDefs;
185 bool printPc = opts.printInstPc;
186 if (!printBits && !printInstId && !printPc) {
187 return; // nothing to print
188 }
189
190 const uint32_t *bits = (const uint32_t *)vbits;
191 emit(ANSI_COMMENT);
192 emit("/* ");
193 bool first = true;
194 if (printInstId) {
195 std::stringstream ss;
196 ss << '#' << i.getID();
197 o << std::right << std::setw(4) << ss.str();
198 first = false;
199 }
200
201 if (printPc) {
202 if (first) {
203 first = false;
204 } else {
205 emit(" ");
206 }
207 emit("[");
208 // emit PC. if basePCOffset is specified, PC is printed after adding
209 // the offset specified in command line. By default, basePCOffset
210 // is 0
211 emitHexDigits<PC>(i.getPC() + opts.basePCOffset, 4);
212 emit("] ");
213 }
214
215 if (printBits) {
216 if (first) {
217 first = false;
218 } else {
219 emit(" ");
220 }
221 if (i.hasInstOpt(InstOpt::COMPACTED)) {
222 emit(" ");
223 emit(' ');
224 emit(" ");
225 emit(' ');
226 } else {
227 emitHexDigits<uint32_t>(bits[3], 8);
228 emit('`');
229 emitHexDigits<uint32_t>(bits[2], 8);
230 emit('`');
231 }
232 emitHexDigits<uint32_t>(bits[1], 8);
233 emit('`');
234 emitHexDigits<uint32_t>(bits[0], 8);
235 }
236 emit(" */ ");
237 o << std::setfill(' ');
238 o << std::dec;
239 emit(ANSI_RESET);
240 }
241
242 void formatInlineBinaryInstruction(const Instruction &i) {
243 emit(".inline_inst ");
244 for (auto val : i.getInlineBinary()) {
245 emitHex(val);
246 emit(" ");
247 }
248 }
249
250 // for single instruction
251 void formatInstruction(const Instruction &i, const void *vbits) {
252 currInstBits = (const uint8_t *)vbits;
253 formatInstruction(i);
254 }
255
256 void formatInstruction(const Instruction &i) {
257 currInst = &i;
258 if (i.isInlineBinaryInstruction()) {
259 formatInlineBinaryInstruction(i);
260 currInst = nullptr;
261 return;
262 }
263
264 if (opts.printInstDeps) {
265 formatInstructionDeps(i);
266 }
267 formatPrefixComment(i, currInstBits);
268
269 const bool isSend = i.is(Op::SEND); // not for sendc
270 if (isSend) {
271 bool sendPrinted = false;
272 if (platform() >= Platform::XE_HPG && opts.printLdSt) {
273 sendPrinted = formatLoadStoreSyntax(i);
274 }
275 if (!sendPrinted) {
276 formatNormalInstructionBody(i, "");
277 }
278 } else {
279 formatNormalInstructionBody(i, "");
280 }
281
282 currInst = nullptr;
283 }
284
285 void formatInstructionDeps(const Instruction &i) {
286 bool first = true;
287 auto emitSet = [&](const std::string& what, const RegSet &rs) {
288 if (!rs.empty()) {
289 if (first) {
290 first = false;
291 emitAnsi(ANSI_FADED, "// ");
292 } else
293 emitAnsi(ANSI_FADED, ", ");
294 emitAnsi(ANSI_FADED, what, ":", rs.str());
295 }
296 };
297 RegSet rsDst(model);
298 rsDst.addExplicitDestinationOutputs(i);
299 emitSet("d", rsDst);
300
301 RegSet rsDfl(model);
302 rsDfl.addFlagModifierOutputs(i);
303 emitSet("d-fl", rsDfl);
304
305 RegSet rsAcc(model);
306 rsAcc.addDestinationImplicit(i);
307 emitSet("d-impl", rsAcc);
308
309 if (!first) {
310 newline();
311 first = true;
312 }
313 for (int ix = 0; ix < (int)i.getSourceCount(); ix++) {
314 RegSet rs(model);
315 rs.addSourceOperandInput(i, ix);
316 std::stringstream ss;
317 ss << "s" << ix;
318 emitSet(ss.str(), rs);
319 }
320
321 RegSet rsPrS(model);
322 rsPrS.addPredicationInputs(i);
323 emitSet("s-pr", rsPrS);
324
325 RegSet rsAccS(model);
326 rsAccS.addSourceImplicit(i);
327 emitSet("s-impl", rsAccS);
328
329 RegSet rsDescS(model);
330 rsDescS.addSourceDescriptorInputs(i);
331 emitSet("s-desc", rsDescS);
332
333 if (!first)
334 newline();
335 }
336
337 bool formatLoadStoreSyntax(const Instruction &i);
338
339 static bool useSyncAllSetSyntax(const Instruction &i) {
340 return i.is(Op::SYNC) && (i.getSubfunction().sync == SyncFC::ALLRD ||
341 i.getSubfunction().sync == SyncFC::ALLWR);
342 }
343
344 void formatNormalInstructionBody(const Instruction &i,
345 const std::string &debugSendDecode) {
346 if (platform() == Platform::XE2 && i.getOpSpec().isAnySendFormat()) {
347 formatInstructionBodySendXe2(i);
348 return;
349 }
350 if (i.getOpSpec().isSendgFormat()) {
351 formatInstructionBodySendgXe3p(i);
352 return;
353 }
354 if (platform() >= Platform::XE3 && i.getOpSpec().isAnySendFormat()) {
355 // Xe3 backwards compatible (non-ld/st/raw) send syntax
356 // send (..) dst src0 src1:src1len [exDescOff:]exDesc desc
357 // Gather send
358 // send (..) dst r[src0.x] [exDescOff:]exDesc desc
359 formatInstructionBodySendXe3(i);
360 return;
361 }
362
363 formatInstructionPrefix(i);
364
365 int nSrcs = i.getSourceCount();
366 bool hasNonEmptyInstOpts = hasInstOptTokens(i);
367 const OpSpec &os = i.getOpSpec();
368
369 if (os.supportsDestination()) {
370 // destination ops
371 emit(" ");
372 formatDstOp(i);
373 } else {
374 if (nSrcs > 0 || hasNonEmptyInstOpts) {
375 // We only print this if there is something on the line following
376 // this. Checking this prevents useless spaces at the end of line.
377 emitSpaces(cols.dstOp);
378 }
379 }
380
381 // source ops
382 if (nSrcs == 0) {
383 // no source ops, but add spacing if we have instruction options
384 // thus to prevent EOL spaces
385 if (hasNonEmptyInstOpts) {
386 emitSpaces(cols.srcOp);
387 }
388 } else {
389 if (nSrcs >= 1) {
390 emit(" ");
391 if (useSyncAllSetSyntax(i)) {
392 formatSyncAllSrc0(i);
393 } else {
394 formatSrcOp(SourceIndex::SRC0, i);
395 }
396 }
397 if (nSrcs >= 2) {
398 emit(" ");
399 formatSrcOp(SourceIndex::SRC1, i);
400 } else if (!os.isSendFormat()) {
401 // ensure at least two columns worth of sources, with
402 // an exception for send. We poach those spaces for the
403 // descriptors.
404 startColumn(cols.srcOp);
405 finishColumn();
406 }
407 if (nSrcs >= 3) {
408 emit(" ");
409 formatSrcOp(SourceIndex::SRC2, i);
410 }
411 if (nSrcs >= 4) {
412 emit(" ");
413 //
414 if (i.getOpSpec().is(Op::BDPAS)) {
415 // src3 and src4 have subreg and data type
416 // format as a regular src operand
417 formatSrcOp(SourceIndex::SRC3, i);
418 }
419 }
420 if (nSrcs >= 5) {
421 emit(" ");
422 // bdpas is the only quinary op at the moment
423 IGA_ASSERT(i.getOpSpec().is(Op::BDPAS), "not bdpas");
424 // src3 and src4 have subreg and data type
425 // format as a regular src operand
426 formatSrcOp(SourceIndex::SRC4, i);
427 }
428 }
429
430 // send descriptors
431 if (os.isAnySendFormat()) {
432 emit(" ");
433 formatSendDesc(i.getExtMsgDescriptor());
434 emit(" ");
435 formatSendDesc(i.getMsgDescriptor(), 8);
436 }
437
438 // instruction options
439 std::vector<const char *> emptyExtraOpts;
440 formatInstOpts(i, emptyExtraOpts);
441
442 // EOL comments
443 formatEolComments(i, debugSendDecode);
444 }
445
446 private:
447 // Xe2 send is same as earlier send + funny ExDescImm business
448 void formatInstructionBodySendXe2(const Instruction &i) {
449 formatInstructionPrefix(i);
450
451 emit(" ");
452 formatSendDstOp(i);
453
454 emit(" ");
455 if (i.isGatherSend()) {
456 formatRegIndRef(i.getSource(SourceIndex::SRC0));
457 emit(ANSI_RESET);
458 } else
459 formatSrcBare(i.getSource(SourceIndex::SRC0));
460
461 emit(" ");
462
463 auto src1NeedsLenSuffix = i.getExtMsgDescriptor().isImm() ||
464 i.getSendFc() == SFID::UGM ||
465 i.hasInstOpt(InstOpt::EXBSO);
466 if (src1NeedsLenSuffix)
467 formatSendSrcWithLength(i.getSource(SourceIndex::SRC1),
468 i.getSrc1Length());
469 else
470 formatSrcBare(i.getSource(SourceIndex::SRC1));
471
472 emit(" ");
473
474 auto immOff = i.getExtImmOffDescriptor();
475 if (immOff != 0) {
476 emitHex(immOff);
477 emit(":");
478 }
479 formatSendExDesc(i.getExtMsgDescriptor());
480 emit(" ");
481 formatSendDesc(i.getMsgDescriptor(), 8);
482
483 // instruction options
484 formatInstOpts(i);
485
486 // EOL comments
487 formatEolComments(i);
488 }
489
490 // xe3 backwards compatible send with Gather Send support
491 void formatInstructionBodySendXe3(const Instruction &i) {
492 formatInstructionPrefix(i);
493
494 emit(" ");
495 formatSendDstOp(i);
496
497 emit(" ");
498 if (i.isGatherSend()) {
499 formatRegIndRef(i.getSource(SourceIndex::SRC0));
500 emit(ANSI_RESET);
501 } else {
502 formatSrcBare(i.getSource(SourceIndex::SRC0));
503 }
504 emit(" ");
505
506 // Gather Send lacks src1 (implicitly null)
507 if (!i.isGatherSend()) {
508 auto src1NeedsLenSuffix = i.getExtMsgDescriptor().isImm() ||
509 i.getSendFc() == SFID::UGM ||
510 i.hasInstOpt(InstOpt::EXBSO);
511 if (src1NeedsLenSuffix)
512 formatSendSrcWithLength(i.getSource(SourceIndex::SRC1),
513 i.getSrc1Length());
514 else
515 formatSrcBare(i.getSource(SourceIndex::SRC1));
516
517 emit(" ");
518 }
519
520 auto immOff = i.getExtImmOffDescriptor();
521 if (immOff != 0) {
522 emitHex(immOff);
523 emit(":");
524 }
525 formatSendExDesc(i.getExtMsgDescriptor());
526 emit(" ");
527 formatSendDesc(i.getMsgDescriptor(), 8);
528
529 // instruction options
530 formatInstOpts(i);
531
532 // EOL comments
533 formatEolComments(i);
534 }
535
536 // XE3P:
537 // Direct Send Generalized (non-gathering)
538 // sendg[c] (...) dst src0:src0Len src1:src1len ID0 ID1 imm42
539 // sendg[c] (...) dst src0:src0Len src1:src1len [ID0] imm47
540 //
541 // Gathering Send Generalized:
542 // sendg[c] (..) dst r[s0.x]:src0Len ID0 ID1 imm42
543 // sendg[c] (..) dst r[s0.x]:src0Len [ID0] imm47
544 void formatInstructionBodySendgXe3p(const Instruction &i) {
545 formatInstructionPrefix(i);
546
547 emit(" ");
548 formatSendDstOp(i);
549
550 emit(" ");
551 if (i.isGatherSend()) {
552 formatRegIndRef(i.getSource(SourceIndex::SRC0));
553 emit(":");
554 emitDecimal(i.getSrc0Length());
555 emit(ANSI_RESET);
556 } else {
557 formatSendSrcWithLength(i.getSource(SourceIndex::SRC0),
558 i.getSrc0Length());
559 }
560 emit(" ");
561
562 // sendg has no src1 (implicit null)
563 // looking forward sendg will have no src1 at all
564 // Omit src1 from the asm syntax to accomodate the change
565 if (!i.isGatherSend()) {
566 formatSendSrcWithLength(i.getSource(SourceIndex::SRC1),
567 i.getSrc1Length());
568 emit(" ");
569 }
570
571 auto id0 = i.getSendgIndDesc0Reg();
572 if (id0 != REGREF_INVALID) {
573 formatScalarRegRead(RegName::ARF_S, id0);
574 emit(" ");
575 }
576 auto id1 = i.getSendgIndDesc1Reg();
577 if (id1 != REGREF_INVALID) {
578 formatScalarRegRead(RegName::ARF_S, id1);
579 emit(" ");
580 }
581 emit(ANSI_IMMEDIATE);
582 emitHex<uint64_t>(i.getSendgDesc());
583 emit(ANSI_RESET);
584
585 // instruction options
586 formatInstOpts(i);
587
588 // EOL comments
589 formatEolComments(i);
590 }
591
592
593 void formatInstructionPrefix(const Instruction &i) {
594 formatMaskAndPredication(i);
595 emit(' ');
596 formatOpMnemonicExecInfo(i);
597 emit(' ');
598 formatFlagModifier(i);
599 }
600
601 void formatMaskAndPredication(const Instruction &i) {
602 formatMaskAndPredication(i.getMaskCtrl(), i.getPredication(),
603 i.getFlagReg());
604 }
605
606 void formatMaskAndPredication(const MaskCtrl &mc, const Predication &p,
607 const RegRef &f) {
608 startColumn(cols.predication);
609 bool hasPred = p.function != PredCtrl::NONE;
610 if (mc == MaskCtrl::NOMASK || hasPred) {
611 emit('(');
612 if (mc == MaskCtrl::NOMASK) {
613 emit(ToSyntax(mc));
614 if (hasPred) {
615 emit('&');
616 }
617 }
618 if (hasPred) {
619 if (p.inverse) {
620 emit('~');
621 }
622 formatScalarRegRead(RegName::ARF_F, f);
623 emitAnsi(ANSI_FLAGMODIFIER, ToSyntax(p.function));
624 }
625 emit(')');
626 }
627
628 finishColumn();
629 }
630
631 // even if they ask for short names, some ops may be ambiguous that way
632 // e.g. "sync.nop" may not be shortened to "nop" because then it would
633 // parse back as the wrong thing
634 bool shortOpWouldBeAmbiguous(const OpSpec &us) const {
635 for (const OpSpec *them : model.ops()) {
636 if (us.op != them->op && us.mnemonic.str() == them->mnemonic.str()) {
637 return true;
638 }
639 }
640 return false;
641 }
642
643 void formatExecInfo(const Instruction &i) {
644 const OpSpec &os = i.getOpSpec();
645 if (!os.hasImpicitEm()) {
646 auto es = i.getExecSize();
647 auto coff = i.getChannelOffset();
648 emit('(');
649 emit(ToSyntax(es));
650 emit('|');
651 emitAnsi(coff == ChannelOffset::M0, ANSI_FADED, ToSyntax(coff));
652 emit(')');
653 }
654 }
655
656 void formatOpMnemonicExecInfo(const Instruction &i) {
657 startColumn(cols.opCodeExecInfo);
658
659 const OpSpec &os = i.getOpSpec();
660
661 emitAnsi(ANSI_MNEMONIC, os.mnemonic.str());
662
663 std::string subfunc;
664 switch (os.op) {
665 // case Op::SEL:
666 // TODO: with syntax extensions we could treat
667 // sel (lt)f1.1 ...
668 // as
669 // sel.lt ... f1.1
670 //
671 // and
672 //
673 // (f0.1) sel ...
674 // (~f1.1) sel ...
675 // (~f1.1.any*) sel ...
676 // as
677 // sel.esc ... f0.1
678 // sel.any* ... f0.1
679 case Op::MATH:
680 subfunc = ToSyntax(i.getMathFc());
681 break;
682 case Op::SHFL:
683 subfunc = ToSyntax(i.getShuffleFc());
684 break;
685 case Op::LFSR:
686 subfunc = ToSyntax(i.getLfsrFc());
687 break;
688 case Op::DNSCL:
689 subfunc = ToSyntax(i.getDnsclFc());
690 break;
691 case Op::SEND:
692 case Op::SENDC:
693 case Op::SENDG:
694 case Op::SENDGC:
695 case Op::SENDGX:
696 case Op::SENDGXC:
697 if (platform() >= Platform::XE) {
698 subfunc = ToSyntax(i.getSendFc());
699 } // else part of ex_desc
700 break;
701 case Op::SYNC:
702 subfunc = ToSyntax(i.getSyncFc());
703 break;
704 case Op::BFN:
705 if (opts.syntaxBfnSymbolicFunctions) {
706 // decode binary function names to symbolic
707 // bfn.(s0&s1|~s2)
708 subfunc += "(";
709 subfunc += i.getBfnFc().c_str();
710 subfunc += ")";
711 } else {
712 // use a raw value
713 // emit<uint32_t>(i.getBfnFc().value);
714 std::stringstream ss;
715 ss << "0x" << std::hex << std::setw(2) << std::setfill('0')
716 << std::uppercase << i.getBfnFc().value;
717 subfunc = ss.str();
718 }
719 break;
720 case Op::DPAS:
721 case Op::DPASW:
722 case Op::BDPAS:
723 subfunc = ToSyntax(i.getDpasFc());
724 break;
725 default:
726 if (os.supportsBranchCtrl() && i.getBranchCtrl() == BranchCntrl::ON) {
727 subfunc += "b";
728 }
729 break;
730 }
731
732 if (!subfunc.empty()) {
733 emit('.');
734 emitAnsi(ANSI_SUBFUNCTION, std::move(subfunc));
735 }
736
737 emit(' ');
738 formatExecInfo(i);
739
740 finishColumn();
741 }
742
743 void formatFlagModifier(const Instruction &i) {
744 startColumn(cols.flagMod);
745 // should be check for suportsFlagModifier, but looks like our
746 // models are wrong on math instruction.
747 /*&& i.getOpSpec().supportsFlagModifier()*/
748 if (i.hasFlagModifier() && !i.getOpSpec().isAnySendFormat()) {
749 emitAnsi(ANSI_FLAGMODIFIER, '(', ToSyntax(i.getFlagModifier()), ')');
750
751 if (i.is(Op::SEL)) {
752 // select actually uses the conditional modifier as a read
753 // source, not a write: insanity
754 formatScalarRegRead(RegName::ARF_F, i.getFlagReg());
755 } else {
756 formatScalarRegWrite(RegName::ARF_F, i.getFlagReg());
757 }
758 }
759
760 finishColumn();
761 }
762
763 void formatDstRegion(const Region &rgn) {
764 bool isVector = rgn.getHz() == Region::Horz::HZ_1;
765 emitAnsi(isVector, ANSI_FADED, ToSyntax(rgn));
766 }
767
768 void formatSourceModifier(const OpSpec &os, SrcModifier sm) {
769 if (sm != SrcModifier::NONE) {
770 emit(ToSyntax(os.op, sm));
771 }
772 }
773
774 void formatSourceRegion(SourceIndex srcIx, const Instruction &i) {
775 int srcIxVal = static_cast<int>(srcIx);
776 const OpSpec &os = i.getOpSpec();
777 const Operand &src = i.getSource(srcIx);
778 // breaks reversibiliy with:
779 // movi (8|M0) r37.0<1>:uw r[a0.0,64]<8;8,1>:uw null<0;1,0>:ud
780 // writes null out as null:ud and parses it as <1;1,0>
781 // either need to complicate the default regioning rules by defaulting
782 // null:ud to <0;1,0>
783 // but this makes default region a function of the register used
784 // (additional complexity to those already complex rules)
785 //
786 // if (src.getDirRegName() == RegName::ARF_NULL) {
787 // // drop the region on all null operands
788 // return;
789 // }
790 const Region &rgn = src.getRegion();
791 if (os.hasImplicitSrcRegion(srcIxVal, i.getExecSize(), i.isMacro()) ||
792 os.isBranching()) {
793 // e.g. on some platforms certain branches have an implicit <0;1,0>
794 // meaning we can omit it if the IR matches
795 return;
796 }
797
798 // region e.g. <8;8,1> or <1;0> ternary src0/src1 <1> for tern src2
799 bool isVector = false, isScalar = false;
800 if (os.isTernary()) {
801 // align1 ternary has form
802 // op (...) dst<H> src0<v;h> src1<v;h> src2<h>
803 if (srcIxVal < 2) {
804 isVector = rgn == Region::SRC1X0;
805 isScalar = rgn == Region::SRC0X0;
806 } else {
807 isVector = rgn == Region::SRCXX1;
808 isScalar = rgn == Region::SRCXX0;
809 }
810 } else {
811 // all other operands use the full <v;w,h>
812 // this also handles <w,h> (VxH or Vx1 mode)
813 isVector = rgn == Region::SRC110;
814 isScalar = rgn == Region::SRC010;
815 }
816 bool fadeRegion =
817 (i.getExecSize() == ExecSize::SIMD1 && isScalar) || isVector;
818 emitAnsi(fadeRegion, ANSI_FADED, ToSyntax(rgn));
819 }
820
821 void formatSourceType(int srcIx, const OpSpec &os, const Operand &op) {
822 if ((os.isBranching() && model.supportsSimplifiedBranches())) {
823 // doesn't support types
824 return;
825 }
826 if (os.is(Op::SYNC) && op.isNull())
827 return;
828 const Type &type = op.getType();
829 bool lblArg = op.getKind() == Operand::Kind::LABEL ||
830 op.getKind() == Operand::Kind::IMMEDIATE;
831 if (os.hasImplicitSrcType(srcIx, lblArg)) {
832 auto expectedType = os.implicitSrcType(srcIx, lblArg);
833 if (expectedType == type) {
834 return;
835 }
836 }
837
838 // fade the type if it's uniform
839 bool isUniformType =
840 currInst && currInst->getDestination().getType() == type;
841 emitAnsi(isUniformType, ANSI_FADED, ToSyntax(type));
842 }
843
844 void formatSendDesc(const SendDesc &sd, int cw = 0) {
845 startColumn(cols.sendDesc);
846
847 emit(ANSI_SUBFUNCTION);
848 if (sd.isImm()) {
849 emitHex(sd.imm, cw);
850 } else {
851 emit('a');
852 formatRegRef(sd.reg);
853 }
854 emit(ANSI_RESET);
855
856 finishColumn();
857 }
858 void formatSendExDesc(const SendDesc &sd, int cw = 0) {
859 startColumn(cols.sendDesc);
860 if (sd.isImm()) {
861 emit(ANSI_IMMEDIATE);
862 emitHex(sd.imm, cw);
863 } else {
864 emit(ANSI_REGISTER_ARF);
865 emit('a');
866 formatRegRef(sd.reg);
867 }
868 emit(ANSI_RESET);
869
870 finishColumn();
871 }
872
873 // e.g. op (8) ... {Compacted}
874 // ^^^^^^^^^^^
875 void formatInstOpts(const Instruction &i,
876 const std::vector<const char *> &instOpts);
877 void formatInstOpts(const Instruction &i) {
878 std::vector<const char *> emptyExtraOpts;
879 formatInstOpts(i, emptyExtraOpts);
880 }
881
882 bool hasInstOptTokens(const Instruction &i) const {
883 bool hasDepInfo = platform() >= Platform::XE && i.getSWSB().hasSWSB();
884 return !i.getInstOpts().empty() || hasDepInfo;
885 }
886
887 void formatEolComments(const Instruction &i,
888 const std::string &debugSendDecode = "",
889 bool decodeSendDesc = true) {
890 std::stringstream ss;
891
892 // separate all comments with a semicolon
893 Intercalator semiColon(ss, "; ");
894
895 if (opts.printInstDefs && opts.liveAnalysis) {
896 // -Xprint-defs
897 // emit all live ranges where this instruction is the use
898 // this will indicate the source instruction
899 std::stringstream ss2;
900 intercalate(
901 ss2, ",", opts.liveAnalysis->deps,
902 [&](const Dep &d) { return d.use == &i; },
903 [&](const Dep &d) {
904 if (d.def) {
905 ss2 << " #" << d.def->getID();
906 } else {
907 ss2 << " IN";
908 }
909 d.values.str(ss2);
910 });
911 if (ss2.tellp() > 0) {
912 semiColon.insert();
913 ss << ss2.str();
914 }
915
916 #if 0
917 // for debugging (normally used by JSON only)
918 const auto &live = opts.liveAnalysis->sums[i.getID()];
919 semiColon.insert();
920 ss << "r=" << live.grfBytes;
921 ss << ",acc=" << live.accBytes;
922 ss << ",f=" << live.flagBytes;
923 ss << ",a=" << live.indexBytes;
924 ss << ",s=" << live.scalarBytes;
925 #endif
926 }
927
928 const std::string &comment = i.getComment();
929 if (!comment.empty()) {
930 // custom comment attached to the instruction by some processor
931 semiColon.insert();
932 //
933 // if the comment contains newlines, change them to ; so that
934 // the EOL comment won't be broken
935 for (size_t i = 0; i < comment.size(); i++) {
936 if (comment[i] == '\r' || comment[i] == '\n')
937 ss << "; ";
938 else
939 ss << comment[i];
940 }
941 }
942
943 if (i.getOpSpec().isSendgFormat()) {
944 if (decodeSendDesc) {
945 if (ss.tellp() != 0) {
946 semiColon.insert();
947 }
948 bool dstNonNull = !i.getDestination().isNull();
949 EmitSendgDescriptorInfoXe3p(
950 platform(), i.getSendFc(), i.getExecSize(), dstNonNull,
951 i.getSrc0Length(), i.getSrc1Length(), i.getSendgIndDesc0Reg(),
952 i.getSendgIndDesc1Reg(), i.getSendgDesc(), ss);
953 } else if (opts.printLdSt) {
954 if (ss.tellp() != 0) {
955 semiColon.insert();
956 }
957 fmtHex(ss, i.getSendgDesc(), 12);
958 }
959 } else if (i.getOpSpec().isAnySendFormat()) {
960 // send with immediate descriptors, we can decode this to
961 // something more sane in comments
962 const SendDesc exDesc = i.getExtMsgDescriptor(),
963 desc = i.getMsgDescriptor();
964 //
965 if (decodeSendDesc) {
966 // ld/st syntax not enabled
967 if (ss.tellp() != 0) {
968 semiColon.insert();
969 }
970 EmitSendDescriptorInfo(platform(), i.getSendFc(), i.getExecSize(),
971 !i.getDestination().isNull(), i.getDstLength(),
972 i.getSrc0Length(), i.getSrc1Length(),
973 i.getExtImmOffDescriptor(),
974 exDesc, desc, ss);
975 } else if (opts.printLdSt) {
976 // tried to format with ld/st syntax and ...
977 if (ss.tellp() != 0) {
978 semiColon.insert();
979 }
980 // success, show the descriptors for debugging
981 if (debugSendDecode.empty()) {
982 if (exDesc.isImm())
983 fmtHex(ss, exDesc.imm);
984 else
985 ss << "???";
986 ss << " ";
987 if (desc.isImm())
988 fmtHex(ss, desc.imm);
989 else
990 ss << "???";
991 }
992 }
993 if (!debugSendDecode.empty()) {
994 semiColon.insert();
995 ss << debugSendDecode;
996 }
997 } else if (i.is(Op::BFN) && !opts.syntaxBfnSymbolicFunctions) {
998 semiColon.insert();
999 ss << i.getBfnFc().c_str();
1000 }
1001
1002 if (ss.tellp() > 0) {
1003 // only add the comment if we emitted something
1004 emitAnsi(ANSI_COMMENT, " // ", ss.str());
1005 }
1006 }
1007
1008 void formatRegIndRef(const Operand &op) {
1009 emitAnsi(ANSI_REGISTER_GRF, "r");
1010 emit("[");
1011 if (op.getDirRegName() == RegName::ARF_S)
1012 formatScalarRegRead(op.getDirRegName(), op.getDirRegRef());
1013 else
1014 formatScalarRegRead(RegName::ARF_A, op.getIndAddrReg());
1015
1016 if (op.getIndImmAddr() != 0) {
1017 int16_t val = op.getIndImmAddr();
1018 if (!opts.syntaxExtensions) {
1019 emit(',');
1020 } else if (op.getIndImmAddr() > 0) {
1021 emit("+");
1022 } else {
1023 emit("-");
1024 val = -val;
1025 }
1026 emitAnsi(ANSI_IMMEDIATE, val);
1027 }
1028 emit(']');
1029 }
1030
1031 void formatScalarRegWrite(RegName rnm, RegRef rr) {
1032 // we may distinguish in the future
1033 formatScalarRegRead(rnm, rr);
1034 }
1035 void formatScalarRegRead(RegName rnm, RegRef rr) {
1036 const auto *rinfo = model.lookupRegInfoByRegName(rnm);
1037 if (rinfo) {
1038 emit(ANSI_REGISTER(rnm));
1039 emit(rinfo->syntax);
1040 } else {
1041 emit("???");
1042 }
1043 formatRegRef(rr);
1044 emit(ANSI_RESET);
1045 }
1046
1047 void formatRegRef(const RegRef &ref) {
1048 emit((int)ref.regNum, '.', (int)ref.subRegNum);
1049 }
1050 }; // end: class Formatter
1051
1052 void Formatter::formatSrcBare(const Operand &src) {
1053 emit(ANSI_REGISTER(src.getDirRegName()));
1054
1055 const RegInfo *ri = model.lookupRegInfoByRegName(src.getDirRegName());
1056 if (ri == nullptr) {
1057 emit("???");
1058 return;
1059 }
1060 emit(ri->syntax);
1061 int regNum = (int)src.getDirRegRef().regNum;
1062 if (regNum != 0 || ri->hasRegNum()) {
1063 emit(regNum);
1064 }
1065 emit(ANSI_RESET);
1066 }
1067
1068 void Formatter::formatDstType(const OpSpec &os, Type type) {
1069 if (os.isBranching() && model.supportsSimplifiedBranches()) {
1070 return; // doesn't support types
1071 }
1072 if (os.hasImplicitDstType()) {
1073 if (os.implicitDstType() != type) {
1074 emit(ToSyntax(type));
1075 } // else: it's already in in binary normal form
1076 } else {
1077 // type
1078 emit(ToSyntax(type));
1079 }
1080 }
1081
1082 void Formatter::formatSendSrcWithLength(const Operand &src, int srcLen) {
1083 emit(ANSI_REGISTER(src.getDirRegName()));
1084
1085 const RegInfo *ri = model.lookupRegInfoByRegName(src.getDirRegName());
1086 if (ri == nullptr) {
1087 emit("???");
1088 return;
1089 }
1090 emit(ri->syntax);
1091 int regNum = (int)src.getDirRegRef().regNum;
1092 if (regNum != 0 || ri->hasRegNum()) {
1093 emit(regNum);
1094 }
1095
1096 // src register format: r10:4 means r10-r13 (used to be r10#4)
1097 emit(":");
1098 emit(srcLen);
1099
1100 emit(ANSI_RESET);
1101 }
1102
1103 void Formatter::formatBareRegisterUnescaped(RegName regName, int regNum) {
1104 const RegInfo *ri = model.lookupRegInfoByRegName(regName);
1105 if (ri == nullptr) {
1106 emit("???");
1107 return;
1108 }
1109
1110 emit(ri->syntax);
1111
1112 if (regNum != 0 || ri->hasRegNum()) {
1113 // some registers don't have numbers
1114 // e.g. null or ce
1115 emit(regNum);
1116 }
1117 }
1118
1119 void Formatter::formatRegister(RegName regName, RegRef reg, bool emitSubReg,
1120 bool isSIMT) {
1121 emit(ANSI_REGISTER(regName));
1122
1123 // show the subreg if:
1124 // - caller demands it (e.g. it's a nonsend) AND
1125 // the register chosen has subregisters (e.g. not ce and null)
1126 // - OR it's non-zero (either bad IR or something's there)
1127 const RegInfo *ri = model.lookupRegInfoByRegName(regName);
1128 if (ri == nullptr) {
1129 emit("RegName::???");
1130 return;
1131 }
1132 formatBareRegisterUnescaped(regName, (int)reg.regNum);
1133
1134 if ((emitSubReg && ri->hasSubregs()) || reg.subRegNum != 0) {
1135 if (isSIMT)
1136 emit(ANSI_FADED); // a boring SIMT access => fade the text
1137 emit('.');
1138 emit((int)reg.subRegNum);
1139 }
1140
1141 emit(ANSI_RESET);
1142 }
1143
1144 void Formatter::formatDstOp(const Instruction &i) {
1145 const OpSpec &os = i.getOpSpec();
1146 const Operand &dst = i.getDestination();
1147
1148 startColumn(os.isAnySendFormat() ? cols.sendDstOp : cols.dstOp);
1149
1150 if (dst.getDstModifier() == DstModifier::SAT) {
1151 emit("(sat)");
1152 }
1153
1154 switch (dst.getKind()) {
1155 case Operand::Kind::DIRECT: {
1156 bool isSIMT =
1157 i.getExecSize() > ExecSize::SIMD1 && // non-scalar op
1158 dst.getRegion() == Region::DST1 && // packed (non-strided) write
1159 dst.getDirRegRef().subRegNum == 0; // writing to the mid. of reg.
1160 formatRegister(dst.getDirRegName(), dst.getDirRegRef(),
1161 os.hasDstSubregister(i.isMacro()), isSIMT);
1162 break;
1163 }
1164 case Operand::Kind::MACRO:
1165 formatRegister(dst.getDirRegName(), dst.getDirRegRef(),
1166 os.hasDstSubregister(true));
1167 emitAnsi(ANSI_REGISTER(RegName::ARF_MME), ToSyntax(dst.getMathMacroExt()));
1168 break;
1169 case Operand::Kind::INDIRECT:
1170 formatRegIndRef(dst);
1171 break;
1172 default:
1173 emit("Operand::Kind::?");
1174 }
1175
1176 Region dstRgn = dst.getRegion();
1177 if (!os.hasImplicitDstRegion(i.isMacro()) ||
1178 (dstRgn != os.implicitDstRegion(i.isMacro()) &&
1179 dstRgn != Region::INVALID)) {
1180 // some instructions don't have dst regions
1181 formatDstRegion(dst.getRegion());
1182 }
1183 formatDstType(os, dst.getType());
1184
1185 finishColumn();
1186 }
1187
1188 // Normal backwards compatible send or newer generalized
1189 void Formatter::formatSendDstOp(const Instruction &i) {
1190 const Operand &dst = i.getDestination();
1191
1192 startColumn(cols.sendDstOp);
1193
1194 formatRegister(dst.getDirRegName(), dst.getDirRegRef(), false, true);
1195
1196 finishColumn();
1197 }
1198
1199 static bool sendSrc1NeedsLengthSuffix(const Formatter &f,
1200 const Instruction &i) {
1201 // If Src1.Length is not encoded in the ExDesc (but rather in EU ISA)
1202 // then we need to emit Src1.Length somewhere else. We've chosen
1203 // to the suffix the source register.
1204 // e.g. r1:4
1205 // This holds for XeHP with ExBSO
1206 // and for imm descs in XeHPG+ as well
1207 auto exDesc = i.getExtMsgDescriptor();
1208 return i.hasInstOpt(InstOpt::EXBSO) || // includes the XeHPG+ imm cases
1209 (exDesc.isImm() && f.platform() >= Platform::XE_HPG);
1210 }
1211
1212 void Formatter::formatSrcOp(SourceIndex srcIx, const Instruction &i) {
1213 const Operand &src = i.getSource(srcIx);
1214 const OpSpec &os = i.getOpSpec();
1215
1216 startColumn(os.isAnySendFormat() ? cols.sendSrcOp : cols.srcOp);
1217
1218 switch (src.getKind()) {
1219 case Operand::Kind::DIRECT: {
1220 // NOTE: this path is not called for send messages after a given platform;
1221 // in other words:
1222 IGA_ASSERT(!os.isAnySendFormat() || platform() < Platform::XE2,
1223 "unexpected send formatting");
1224 if (os.isAnySendFormat() && srcIx == SourceIndex::SRC1 &&
1225 sendSrc1NeedsLengthSuffix(*this, i)) {
1226 formatSendSrcWithLength(src, i.getSrc1Length());
1227 break;
1228 }
1229 bool hasSubreg = os.hasSrcSubregister(int(srcIx), i.isMacro());
1230 bool isSimt = i.getExecSize() > ExecSize::SIMD1 &&
1231 (src.getRegion() != Region::SRC110 ||
1232 src.getRegion() != Region::SRC1X0 ||
1233 src.getRegion() != Region::SRCXX1) &&
1234 src.getDirRegRef().subRegNum == 0;
1235 formatSourceModifier(os, src.getSrcModifier());
1236 formatRegister(src.getDirRegName(), src.getDirRegRef(), hasSubreg, isSimt);
1237 formatSourceRegion(srcIx, i);
1238 break;
1239 }
1240 case Operand::Kind::MACRO: {
1241 formatSourceModifier(os, src.getSrcModifier());
1242 formatRegister(src.getDirRegName(), src.getDirRegRef(), false);
1243 emit(ToSyntax(src.getMathMacroExt()));
1244 formatSourceRegion(srcIx, i);
1245 ;
1246 break;
1247 }
1248 case Operand::Kind::INDIRECT:
1249 formatSourceModifier(os, src.getSrcModifier());
1250 formatRegIndRef(src);
1251 formatSourceRegion(srcIx, i);
1252 break;
1253 case Operand::Kind::IMMEDIATE:
1254 emit(ANSI_IMMEDIATE);
1255 switch (src.getType()) {
1256 case Type::B:
1257 emitDecimal(src.getImmediateValue().s8);
1258 break;
1259 case Type::W:
1260 emitDecimal(src.getImmediateValue().s16);
1261 break;
1262 case Type::D:
1263 emitDecimal(src.getImmediateValue().s32);
1264 break;
1265 case Type::Q:
1266 emitDecimal(src.getImmediateValue().s64);
1267 break;
1268 case Type::UB:
1269 emitHex(src.getImmediateValue().u8);
1270 break;
1271 case Type::UW:
1272 emitHex(src.getImmediateValue().u16);
1273 break;
1274 case Type::UD:
1275 emitHex(src.getImmediateValue().u32);
1276 break;
1277 case Type::UQ:
1278 emitHex(src.getImmediateValue().u64);
1279 break;
1280 case Type::BF:
1281 emitHex(src.getImmediateValue().u16);
1282 break;
1283 case Type::HF:
1284 if (opts.hexFloats) {
1285 emitHex(src.getImmediateValue().u16);
1286 } else {
1287 FormatFloat(o, src.getImmediateValue().u16);
1288 }
1289 break;
1290 case Type::F:
1291 case Type::TF32:
1292 if (opts.hexFloats) {
1293 emitHex(src.getImmediateValue().u32);
1294 } else {
1295 emitFloat(src.getImmediateValue().f32);
1296 }
1297 break;
1298 case Type::DF:
1299 if (opts.hexFloats) {
1300 emitHex(src.getImmediateValue().u64);
1301 } else {
1302 emitFloat(src.getImmediateValue().f64);
1303 }
1304 break;
1305 case Type::BF8:
1306 case Type::HF8:
1307 emitHex(src.getImmediateValue().u8);
1308 break;
1309 case Type::V:
1310 emitHex(src.getImmediateValue().u32);
1311 break;
1312 case Type::UV:
1313 emitHex(src.getImmediateValue().u32);
1314 break;
1315 case Type::VF:
1316 // packed float format
1317 // each of four 8 - bit immediate vector elements has 1 sign bit,
1318 // 3 bits for the biased exponent (bias of 3), and 4 bits
1319 // for the significand
1320 emitHex(src.getImmediateValue().u32);
1321 break;
1322 default:
1323 emit("???");
1324 };
1325 emit(ANSI_RESET);
1326 break;
1327 case Operand::Kind::LABEL: {
1328 emit(ANSI_IMMEDIATE);
1329 const Block *b = src.getTargetBlock();
1330 formatLabel(b ? b->getPC() : src.getImmediateValue().s32);
1331 emit(ANSI_RESET);
1332 break;
1333 }
1334 default:
1335 emit("???");
1336 }
1337
1338 if (!model.supportsSimplifiedBranches() ||
1339 src.getKind() != Operand::Kind::LABEL) {
1340 formatSourceType(int(srcIx), os, src);
1341 }
1342 finishColumn();
1343 } // end formatSrcOp()
1344
1345 // e.g. formats: sync.allrd ($1,$3,$7)
1346 void Formatter::formatSyncAllSrc0(const Instruction &i) {
1347 const auto &src0 = i.getSource(0);
1348 if (src0.getKind() != Operand::Kind::IMMEDIATE) {
1349 // e.g. sync.allrd null
1350 formatSrcOp(SourceIndex::SRC0, i);
1351 } else {
1352 // e.g. sync.allrd ($1,$3,$7,$13)
1353 emit(ANSI_IMMEDIATE);
1354 auto sbids = src0.getImmediateValue().u32;
1355 bool first = true;
1356 emit("(");
1357 for (int i = 0; i < 32; i++) {
1358 if ((1 << i) & sbids) {
1359 if (first)
1360 first = false;
1361 else
1362 emit(",");
1363 emit("$", i);
1364 }
1365 }
1366 emit(")");
1367 emit(ANSI_RESET);
1368 }
1369 }
1370
1371 void Formatter::formatInstOpts(const Instruction &i,
1372 const std::vector<const char *> &extraInstOpts) {
1373 const auto &iopts = i.getInstOpts();
1374
1375 const auto &di = i.getSWSB();
1376 bool hasDepInfo = platform() >= Platform::XE && di.hasSWSB();
1377 if (iopts.empty() && !hasDepInfo && extraInstOpts.empty()) {
1378 return; // early out (no braces)
1379 }
1380
1381 emit(" {");
1382 ToSyntaxNoBraces(o, iopts);
1383 // extra options germane to such ld/st syntax
1384 for (size_t opIx = 0; opIx < extraInstOpts.size(); opIx++) {
1385 if (opIx > 0) {
1386 emit(",");
1387 }
1388 emit(extraInstOpts[opIx]);
1389 }
1390 if (!iopts.empty() && (hasDepInfo || !extraInstOpts.empty())) {
1391 // something was output, prepend a , for the depinfo
1392 emit(",");
1393 }
1394
1395 // special token and dist/token won't co-exist in the same swsb
1396 // no need to insert "," after this since there must not be
1397 // dist/token swsb
1398 if (di.hasSpecialToken()) {
1399 assert(!di.hasDist() && !di.hasToken());
1400 if (di.spToken == SWSB::SpecialToken::NOACCSBSET)
1401 emit("NoAccSBSet");
1402 }
1403
1404 if (di.hasDist() || di.hasToken()) {
1405 switch (di.distType) {
1406 case SWSB::DistType::REG_DIST:
1407 emit("@");
1408 emit((int)di.minDist);
1409 break;
1410 case SWSB::DistType::REG_DIST_ALL:
1411 emit("A@");
1412 emit((int)di.minDist);
1413 break;
1414 case SWSB::DistType::REG_DIST_FLOAT:
1415 emit("F@");
1416 emit((int)di.minDist);
1417 break;
1418 case SWSB::DistType::REG_DIST_INT:
1419 emit("I@");
1420 emit((int)di.minDist);
1421 break;
1422 case SWSB::DistType::REG_DIST_LONG:
1423 emit("L@");
1424 emit((int)di.minDist);
1425 break;
1426 case SWSB::DistType::REG_DIST_MATH:
1427 emit("M@");
1428 emit((int)di.minDist);
1429 break;
1430 case SWSB::DistType::REG_DIST_SCALAR:
1431 emit("S@");
1432 emit((int)di.minDist);
1433 break;
1434 default:
1435 break;
1436 }
1437
1438 if (di.hasBothDistAndToken())
1439 emit(",");
1440
1441 switch (di.tokenType) {
1442 case SWSB::TokenType::DST:
1443 emit("$");
1444 emit((int)di.sbid);
1445 emit(".dst");
1446 break;
1447 case SWSB::TokenType::SRC:
1448 emit("$");
1449 emit((int)di.sbid);
1450 emit(".src");
1451 break;
1452 case SWSB::TokenType::SET:
1453 emit("$");
1454 emit((int)di.sbid);
1455 break;
1456 case SWSB::TokenType::INC:
1457 emit("$");
1458 emit((int)di.sbid);
1459 emit(".inc");
1460 default:
1461 break;
1462 }
1463 }
1464 emit('}');
1465 } // end formatInstOpts
1466
1467 bool Formatter::formatLoadStoreSyntax(const Instruction &i) {
1468 // TODO: maybe re-enable this if we can find a way to make this non-ambiguous
1469 if (platform() >= Platform::XE3) {
1470 return false;
1471 }
1472 if (platform() < Platform::XE_HPG) {
1473 // We will not even try on <=XeHPG
1474 return false;
1475 }
1476 const auto desc = i.getMsgDescriptor();
1477 if (desc.isReg()) {
1478 // given a register descriptor, we've no hope of decoding the op
1479 return false;
1480 }
1481 const auto exDesc = i.getExtMsgDescriptor();
1482
1483 const auto sfid = i.getSendFc();
1484 const auto di = tryDecode(platform(), sfid, i.getExecSize(),
1485 i.getExtImmOffDescriptor(),
1486 exDesc, desc, nullptr);
1487 if (!di) {
1488 // if decode failed fallback to the canonical send syntax
1489 return false;
1490 } else if (!sendOpSupportsSyntax(platform(), di.info.op, sfid)) {
1491 // if decode succeeded, but the syntax isn't supported, then back off
1492 return false;
1493 }
1494
1495 formatMaskAndPredication(i);
1496
1497 const auto &syntax = di.syntax;
1498 startColumn(cols.opCodeExecInfo + 12);
1499 emit(' ');
1500 emit(ANSI_MNEMONIC);
1501 emit(syntax.mnemonic); // e.g. load
1502 emit(ANSI_SUBFUNCTION);
1503 emit(syntax.controls); // e.g. .ugm.d32.a64.uc.ca
1504 emit(ANSI_RESET);
1505 emit(' ');
1506 formatExecInfo(i);
1507 finishColumn();
1508
1509 auto emitPayload = [&](const Operand &op, int len) {
1510 emit(ANSI_REGISTER(op.getDirRegName()));
1511 //
1512 formatBareRegisterUnescaped(op.getDirRegName(),
1513 (int)op.getDirRegRef().regNum);
1514 if (len >= 0) {
1515 emit(ANSI_FADED);
1516 emit(':');
1517 emitDecimal(len);
1518 }
1519 emit(ANSI_RESET);
1520 //
1521 };
1522
1523 // emits the destination data register part (for load and atomic)
1524 auto emitDstDataReg = [&]() {
1525 startColumn(cols.dstOp);
1526 //
1527 int len = i.getDstLength();
1528 if (i.getDestination().getDirRegName() == RegName::ARF_NULL) {
1529 if (platform() > Platform::XE3) {
1530 len = -1;
1531 }
1532 }
1533 emitPayload(i.getDestination(), len);
1534 //
1535 finishColumn();
1536 };
1537
1538 // emits the [...]:2:a64 part
1539 auto emitAddrReg = [&](int cols) {
1540 startColumn(cols);
1541 emit(syntax.surface);
1542 emit('[');
1543 emit(syntax.scale);
1544 //
1545 emitPayload(i.getSource(0), i.getSrc0Length());
1546 //
1547 emit(syntax.immOffset);
1548 emit(']');
1549 finishColumn();
1550 };
1551
1552 emit(" ");
1553 if (syntax.isLoad()) {
1554 emitDstDataReg();
1555 emit(' ');
1556 emitAddrReg(cols.srcOp);
1557 //
1558 } else if (syntax.isStore()) {
1559 emitAddrReg(cols.dstOp);
1560 emit(' ');
1561 startColumn(cols.srcOp);
1562 emitPayload(i.getSource(1), i.getSrc1Length());
1563 finishColumn();
1564 //
1565 } else if (syntax.isAtomic()) {
1566 // atomic_fadd....d32.a64 ... null [r10:2] r12:2
1567 emitDstDataReg();
1568 emit(' ');
1569 emitAddrReg(cols.srcOp);
1570 emit(' ');
1571 startColumn(cols.srcOp);
1572 emitPayload(i.getSource(1), i.getSrc1Length());
1573 finishColumn();
1574 //
1575 } else {
1576 const auto &dst = i.getDestination();
1577 if (dst.getDirRegName() != RegName::ARF_NULL) {
1578 emitDstDataReg();
1579 emit(' ');
1580 }
1581 emitAddrReg(cols.srcOp);
1582 }
1583
1584 emit(' ');
1585
1586 // instruction options
1587 formatInstOpts(i);
1588
1589 // EOL comments
1590 //
1591 // provide the descriptor for debugging
1592 std::stringstream ss;
1593 if (exDesc.isImm()) {
1594 ss << "ex_desc:" << fmtHex(exDesc.imm);
1595 } else { // exDesc.isReg()
1596 ss << "ex_desc:a0." << (int)exDesc.reg.subRegNum;
1597 }
1598 // desc has to be imm or we wouldn't be decoding the message in
1599 // ld/st syntax
1600 ss << "; desc:" << fmtHex(desc.imm);
1601 // provide the rlen=... (Dst.Length)
1602 if (platform() > Platform::XE3) {
1603 // it's directly encoded in the descriptor
1604 ss << "; rlen=";
1605 if (i.getDestination().isNull()) {
1606 // null dst implies 0 on this platform
1607 ss << "0";
1608 } else {
1609 // non-null means we have to try and deduce rlen
1610 // TODO: IR generators should set this and we should use
1611 // getDstLength() here
1612 PayloadLengths lens(platform(), i.getSendFc(), i.getExecSize(), desc.imm);
1613 int rlen = lens.dstLen;
1614 if (rlen >= 0)
1615 ss << rlen;
1616 else
1617 ss << "?";
1618 }
1619 }
1620
1621 formatEolComments(i, ss.str(), false);
1622
1623 return true;
1624 }
1625
1626 ///////////////////////////////////////////////////////////////////////////////
1627 //
1628 // Public interfaces into the kernel
1629 void FormatOpts::addApiOpts(uint32_t fmtOpts, uint32_t pcOff) {
1630 numericLabels = (fmtOpts & IGA_FORMATTING_OPT_NUMERIC_LABELS) != 0;
1631 syntaxExtensions = (fmtOpts & IGA_FORMATTING_OPT_SYNTAX_EXTS) != 0;
1632 hexFloats = (fmtOpts & IGA_FORMATTING_OPT_PRINT_HEX_FLOATS) != 0;
1633 printInstPc = (fmtOpts & IGA_FORMATTING_OPT_PRINT_PC) != 0;
1634 printInstBits = (fmtOpts & IGA_FORMATTING_OPT_PRINT_BITS) != 0;
1635 printInstDefs = (fmtOpts & IGA_FORMATTING_OPT_PRINT_DEFS) != 0;
1636 printInstDeps = (fmtOpts & IGA_FORMATTING_OPT_PRINT_DEPS) != 0;
1637 printLdSt = (fmtOpts & IGA_FORMATTING_OPT_PRINT_LDST) != 0;
1638 syntaxBfnSymbolicFunctions =
1639 (fmtOpts & IGA_FORMATTING_OPT_PRINT_BFNEXPRS) != 0;
1640 printAnsi = (fmtOpts & IGA_FORMATTING_OPT_PRINT_ANSI) != 0;
1641 const auto jsonOpts =
1642 IGA_FORMATTING_OPT_PRINT_JSON | IGA_FORMATTING_OPT_PRINT_JSON_V1;
1643 printJson = (fmtOpts & jsonOpts) != 0;
1644 printJsonVersion = (fmtOpts & IGA_FORMATTING_OPT_PRINT_JSON_V1) ? 1 : 2;
1645 basePCOffset = pcOff;
1646 }
1647
1648 void FormatKernel(ErrorHandler &e, std::ostream &o, const FormatOpts &opts,
1649 const Kernel &k, const void *bits) {
1650 IGA_ASSERT(k.getModel().platform == opts.model.platform,
1651 "kernel and options must have same platform");
1652 if (opts.printInstDefs && opts.liveAnalysis == nullptr) {
1653 DepAnalysis la = ComputeDepAnalysis(&k);
1654 FormatOpts optsCopy = opts;
1655 optsCopy.liveAnalysis = &la;
1656 FormatKernel(e, o, optsCopy, k, bits);
1657 return;
1658 }
1659 if (!opts.printJson) {
1660 Formatter f(e, o, opts);
1661 f.formatKernel(k, (const uint8_t *)bits);
1662 } else {
1663 FormatJSON(o, opts, k, bits);
1664 }
1665 }
1666
1667 void FormatInstruction(ErrorHandler &e, std::ostream &o, const FormatOpts &opts,
1668 const Instruction &i, const void *bits) {
1669 if (opts.printJson) {
1670 if (opts.printJsonVersion == 1)
1671 FormatInstructionJSON1(o, opts, i, bits);
1672 else
1673 FormatInstructionJSON2(o, opts, i, bits);
1674 } else {
1675 Formatter f(e, o, opts);
1676 f.formatInstruction(i, (const uint8_t *)bits);
1677 }
1678 }
1679
1680 #ifndef IGA_DISABLE_ENCODER_EXCEPTIONS
1681 void FormatInstruction(ErrorHandler &e, std::ostream &o, const FormatOpts &opts,
1682 const void *bits) {
1683 FormatInstruction(e, o, opts, 0, bits);
1684 }
1685 void FormatInstruction(ErrorHandler &e, std::ostream &o, const FormatOpts &opts,
1686 size_t startPc, const void *bits,
1687 bool useNativeDecoder) {
1688 size_t instLen = ((const MInst *)bits)->isCompact() ? 8 : 16;
1689
1690 DecoderOpts dopts;
1691 dopts.useNumericLabels = true;
1692 iga::Kernel *k = nullptr;
1693 if (useNativeDecoder && iga::native::IsDecodeSupported(opts.model, dopts)) {
1694 k = iga::native::Decode(opts.model, dopts, e, bits, instLen);
1695 } else {
1696 k = iga::ged::Decode(opts.model, dopts, e, bits, instLen);
1697 }
1698 if (e.hasErrors()) {
1699 for (const auto &err : e.getErrors()) {
1700 o << err.message << "\n";
1701 }
1702 }
1703 if (k) {
1704 const auto &bl = k->getBlockList();
1705 if (bl.size() > 0) {
1706 const auto &il = bl.front()->getInstList();
1707 if (il.size() > 0) {
1708 Instruction &i = *il.front();
1709 i.setPC((int32_t)startPc);
1710 FormatInstruction(e, o, opts, i, bits);
1711 }
1712 }
1713 delete k;
1714 }
1715 }
1716 #endif
1717
1718 void GetDefaultLabelName(std::ostream &o, int32_t pc) {
1719 Formatter::getDefaultLabelDefinition(o, pc);
1720 }
1721
1722 ///////////////////////////////////////////////////////////////////////////////
1723 // Following functions generally used for testing, debugging, or
1724 // producing friendly diagnostics
1725
1726 ///////////////////////////////////////////////////////////////////////////////
1727 // The opname should be resolved
1728 std::string FormatOpName(const iga::Model &model, const void *bits) {
1729 std::string s;
1730 iga::OpSpecMissInfo missInfo = {0};
1731 const iga::OpSpec &os = model.lookupOpSpecFromBits(bits, missInfo);
1732
1733 if (!os.isValid()) {
1734 std::stringstream ss;
1735 ss << "OP[" << std::hex << "]" << missInfo.opcode << "?";
1736 s = ss.str();
1737 } else {
1738 s = os.mnemonic;
1739 }
1740
1741 s += ':';
1742 while (s.length() < 8)
1743 s += ' ';
1744 return s;
1745 }
1746 std::string FormatOpBits(const iga::Model &model, const void *bitsV) {
1747 const uint8_t *bits = (const uint8_t *)bitsV;
1748 bool compact = ((const iga::MInst *)bits)->isCompact();
1749 int iLen = compact ? 8 : 16;
1750
1751 std::stringstream ss;
1752 ss << iga::PadR(12, FormatOpName(model, bitsV));
1753 for (int i = 0; i < iLen; i++) {
1754 if (i > 0)
1755 ss << ' ';
1756 ss << std::hex << std::setw(2) << std::setfill('0') << (uint32_t)bits[i];
1757 if (i == 7) {
1758 ss << ' ';
1759 }
1760 }
1761 return ss.str();
1762 }
1763
1764 } // namespace iga