Debian Codegraph @

visa/iga/IGALibrary/api/kv.cpp

raw
1 /*========================== begin_copyright_notice ============================
2
3 Copyright (C) 2018-2021 Intel Corporation
4
5 SPDX-License-Identifier: MIT
6
7 ============================= end_copyright_notice ===========================*/
8
9 /*
10 * The IGA - KernelView API
11 */
12 #include "kv.h"
13
14 #include "../Backend/GED/Decoder.hpp"
15 #include "../Backend/GED/GEDUtil.hpp"
16 #include "../Frontend/Formatter.hpp"
17 #include "../IR/Block.hpp"
18 #include "../IR/Messages.hpp"
19 #include "../strings.hpp"
20
21 #include <mutex>
22 #include <sstream>
23
24 ///////////////////////////////////////////////////////////////////////////////
25 //
26 // KernelView binary interface
27 //
28 ///////////////////////////////////////////////////////////////////////////////
29 using namespace iga;
30
31 class KernelViewImpl {
32 private:
33 KernelViewImpl(const KernelViewImpl &k)
34 : m_model(*Model::LookupModel(k.m_model.platform)) {}
35 KernelViewImpl& operator=(const KernelViewImpl&) = delete;
36
37 public:
38 const Model &m_model;
39 Kernel *m_kernel = nullptr;
40 ErrorHandler m_errHandler;
41 std::map<uint32_t, const Instruction *> m_instsByPc;
42 std::map<uint32_t, const Block *> m_blockToPcMap;
43 DepAnalysis *m_liveAnalysis = nullptr;
44
45 KernelViewImpl(const Model &model, const void *bytes, size_t bytesLength,
46 SWSB_ENCODE_MODE swsbOverride)
47 : m_model(model)
48
49 {
50 Decoder decoder(model, m_errHandler);
51 if (swsbOverride != SWSB_ENCODE_MODE::SWSBInvalidMode) {
52 decoder.setSWSBEncodingMode(swsbOverride);
53 }
54 m_kernel = decoder.decodeKernelBlocks(bytes, bytesLength);
55
56 for (const Block *b : m_kernel->getBlockList()) {
57 m_blockToPcMap[b->getPC()] = b;
58 for (const Instruction *inst : b->getInstList()) {
59 m_instsByPc[inst->getPC()] = inst;
60 }
61 }
62 }
63
64 ~KernelViewImpl() {
65 if (m_liveAnalysis) {
66 delete m_liveAnalysis;
67 }
68 if (m_kernel) {
69 delete m_kernel;
70 }
71 }
72
73 const Instruction *getInstruction(int32_t pc) const {
74 auto itr = m_instsByPc.find(pc);
75 if (itr == m_instsByPc.end()) {
76 return nullptr;
77 }
78 return itr->second;
79 }
80
81 const Block *getBlock(int32_t pc) const {
82 auto itr = m_blockToPcMap.find(pc);
83 if (itr == m_blockToPcMap.end()) {
84 return nullptr;
85 }
86 return itr->second;
87 }
88 };
89
90 kv_t *kv_create(iga_gen_t gen_platf, const void *bytes, size_t bytes_len,
91 iga_status_t *status, char *errbuf, size_t errbuf_cap,
92 uint32_t swsbMode
93 ) {
94 if (errbuf && errbuf_cap > 0)
95 *errbuf = 0;
96
97 Platform p = ToPlatform(gen_platf);
98 const Model *model = Model::LookupModel(p);
99 if (model == nullptr) {
100 if (status)
101 *status = IGA_UNSUPPORTED_PLATFORM;
102 if (errbuf) {
103 iga::copyOutString(errbuf, errbuf_cap, nullptr, "unsupported platform");
104 }
105 return nullptr;
106 }
107
108 KernelViewImpl *kvImpl = nullptr;
109 try {
110 kvImpl =
111 new (std::nothrow) KernelViewImpl(
112 *model, bytes, bytes_len,
113 static_cast<iga::SWSB_ENCODE_MODE>(swsbMode));
114
115 if (!kvImpl) {
116 if (errbuf) {
117 iga::copyOutString(errbuf, errbuf_cap, nullptr, "failed to allocate");
118 }
119 if (status)
120 *status = IGA_OUT_OF_MEM;
121 return nullptr;
122 }
123 } catch (const FatalError &fe) {
124 if (errbuf) {
125 std::string msgS = iga::format("decoding error: ", fe.what());
126 iga::copyOutString(errbuf, errbuf_cap, nullptr, msgS.c_str());
127 }
128 if (status)
129 *status = IGA_DECODE_ERROR;
130 if (kvImpl) {
131 delete kvImpl;
132 }
133 return nullptr;
134 }
135
136 // copy out the errors and warnings
137 if (kvImpl) {
138 std::stringstream ss;
139 auto fmtDiag = [&](const char *what, const Diagnostic &d) {
140 ss << what << ": PC[0x" << iga::hex(d.at.offset) << "] " << d.message
141 << "\n";
142 };
143 if (kvImpl->m_errHandler.hasErrors()) {
144 for (const auto& d : kvImpl->m_errHandler.getErrors()) {
145 fmtDiag("ERROR", d);
146 }
147 }
148 for (const auto& d : kvImpl->m_errHandler.getWarnings()) {
149 fmtDiag("WARNING", d);
150 }
151 (void)copyOut(errbuf, errbuf_cap, ss);
152 }
153
154 if (kvImpl->m_errHandler.hasErrors()) {
155 // copy out the error status
156 if (status)
157 *status = IGA_DECODE_ERROR;
158 } else {
159 // copy out the error status
160 if (status)
161 *status = IGA_SUCCESS;
162 }
163
164 return (kv_t *)kvImpl;
165 }
166
167 void kv_delete(kv_t *kv) {
168 if (kv)
169 delete ((KernelViewImpl *)kv);
170 }
171
172 int32_t kv_get_inst_size(const kv_t *kv, int32_t pc) {
173 if (!kv)
174 return 0;
175
176 const Instruction *inst = ((KernelViewImpl *)kv)->getInstruction(pc);
177 if (!inst) {
178 return 0;
179 }
180 return inst->hasInstOpt(InstOpt::COMPACTED) ? 8 : 16;
181 }
182
183 kv_status_t kv_get_inst_msg_info(const kv_t *kv, int32_t pc, bool *isAtomic,
184 bool *isSlm, bool *isScratch) {
185 if (!kv) {
186 return KV_ERROR;
187 }
188
189 const Instruction* inst = ((KernelViewImpl*)kv)->getInstruction(pc);
190 if (!inst) {
191 return KV_INVALID_PC;
192 }
193 const DecodeResult di = tryDecode(*inst, nullptr);
194 if (!di) {
195 return KV_DECODE_ERROR;
196 }
197
198 *isSlm = di.info.hasAttr(iga::MessageInfo::Attr::SLM);
199 *isScratch = di.info.hasAttr(iga::MessageInfo::Attr::SCRATCH);
200 *isAtomic = di.info.isAtomic();
201
202 return KV_SUCCESS;
203 }
204
205 bool kv_has_inst_opt(const kv_t *kv, int32_t pc, uint32_t opt) {
206 KernelViewImpl *kvImpl = (KernelViewImpl *)kv;
207 const Instruction *inst = kvImpl->getInstruction(pc);
208 if (!inst)
209 return false;
210 return inst->hasInstOpt((InstOpt)opt);
211 }
212
213 uint32_t kv_get_inst_targets(const kv_t *kv, int32_t pc, int32_t *pcs) {
214 if (!kv)
215 return 0;
216
217 const Instruction *inst = ((KernelViewImpl *)kv)->getInstruction(pc);
218 if (!inst || inst->getOp() == Op::ILLEGAL) {
219 return 0;
220 }
221
222 if (!inst->getOpSpec().isBranching()) {
223 return 0;
224 }
225
226 uint32_t nSrcs = 0;
227
228 if (inst->getSourceCount() > 0) {
229 const Operand &op = inst->getSource(SourceIndex::SRC0);
230 if (op.getKind() == Operand::Kind::LABEL) {
231 if (pcs)
232 pcs[nSrcs] = inst->getJIP()->getPC();
233 nSrcs++;
234 }
235 }
236
237 if (inst->getSourceCount() > 1) {
238 const Operand &op = inst->getSource(SourceIndex::SRC1);
239 if (op.getKind() == Operand::Kind::LABEL) {
240 if (pcs)
241 pcs[nSrcs] = inst->getUIP()->getPC();
242 nSrcs++;
243 }
244 }
245
246 return nSrcs;
247 }
248
249 size_t kv_get_inst_syntax(const kv_t *kv, int32_t pc, char *sbuf,
250 size_t sbuf_cap, uint32_t fmt_opts,
251 const char *(*labeler)(int32_t, void *),
252 void *labeler_env) {
253 if (!kv) {
254 if (sbuf && sbuf_cap > 0)
255 *sbuf = 0;
256 return 0;
257 }
258
259 KernelViewImpl *kvImpl = (KernelViewImpl *)kv;
260 const Instruction *inst = kvImpl->getInstruction(pc);
261 if (!inst) {
262 if (sbuf && sbuf_cap > 0)
263 *sbuf = 0;
264 return 0;
265 }
266
267 std::stringstream ss;
268 FormatOpts fopts(kvImpl->m_model, labeler, labeler_env);
269 if (fopts.printInstDefs) {
270 static std::mutex m;
271 const std::lock_guard<std::mutex> g(m);
272 if (kvImpl->m_liveAnalysis == nullptr) {
273 kvImpl->m_liveAnalysis = new (std::nothrow) DepAnalysis();
274 *kvImpl->m_liveAnalysis = ComputeDepAnalysis(kvImpl->m_kernel);
275 }
276 fopts.liveAnalysis = kvImpl->m_liveAnalysis;
277 }
278 fopts.addApiOpts(fmt_opts);
279 fopts.setSWSBEncodingMode(kvImpl->m_model.getSWSBEncodeMode());
280 FormatInstruction(kvImpl->m_errHandler, ss, fopts, *inst);
281
282 return copyOut(sbuf, sbuf_cap, ss);
283 }
284
285 size_t kv_get_default_label_name(int32_t pc, char *sbuf, size_t sbuf_cap) {
286 if (!sbuf || sbuf_cap == 0) {
287 return 0;
288 }
289 std::stringstream strm;
290 GetDefaultLabelName(strm, pc);
291 return copyOut(sbuf, sbuf_cap, strm);
292 }
293
294 uint32_t kv_is_inst_target(const kv_t *kv, int32_t pc) {
295 if (!kv)
296 return 0;
297 return ((KernelViewImpl *)kv)->getBlock(pc) == nullptr ? 0 : 1;
298 }
299
300 int32_t kv_get_opgroup(const kv_t *kv, int32_t pc) {
301 if (!kv)
302 return (int32_t)kv_opgroup_t::KV_OPGROUP_INVALID;
303
304 KernelViewImpl *kvImpl = (KernelViewImpl *)kv;
305 const Instruction *inst = kvImpl->getInstruction(pc);
306 if (!inst) {
307 return (int32_t)kv_opgroup_t::KV_OPGROUP_INVALID;
308 }
309 switch (inst->getOp()) {
310 case Op::IF:
311 return (int32_t)kv_opgroup_t::KV_OPGROUP_IF;
312 case Op::ENDIF:
313 return (int32_t)kv_opgroup_t::KV_OPGROUP_ENDIF;
314 case Op::ELSE:
315 return (int32_t)kv_opgroup_t::KV_OPGROUP_ELSE;
316 case Op::WHILE:
317 return (int32_t)kv_opgroup_t::KV_OPGROUP_WHILE;
318 default:
319 if (inst->getOpSpec().isAnySendFormat() && inst->hasInstOpt(InstOpt::EOT)) {
320 return (int32_t)kv_opgroup_t::KV_OPGROUP_SEND_EOT;
321 } else {
322 return (int32_t)kv_opgroup_t::KV_OPGROUP_OTHER;
323 }
324 }
325 }
326
327 uint32_t kv_get_send_descs(const kv_t *kv, int32_t pc, uint32_t *ex_desc,
328 uint32_t *desc) {
329 if (!kv || !ex_desc || !desc)
330 return 0;
331 const Instruction *inst = ((KernelViewImpl *)kv)->getInstruction(pc);
332 if (!inst || !inst->getOpSpec().isAnySendFormat()) {
333 *ex_desc = *desc = KV_INVALID_SEND_DESC;
334 return 0;
335 }
336
337 uint32_t n = 0;
338 if (!inst->getOpSpec().isSendgFormat()) {
339 if (inst->getExtMsgDescriptor().isImm()) {
340 n++;
341 *ex_desc = inst->getExtMsgDescriptor().imm;
342 }
343 else {
344 *ex_desc = KV_INVALID_SEND_DESC;
345 }
346 if (inst->getMsgDescriptor().isImm()) {
347 n++;
348 *desc = inst->getMsgDescriptor().imm;
349 }
350 else {
351 *desc = KV_INVALID_SEND_DESC;
352 }
353 }
354 else {
355 *ex_desc = KV_INVALID_SEND_DESC;
356 *desc = KV_INVALID_SEND_DESC;
357 }
358
359 return n;
360 }
361
362 kv_status_t kv_get_send_exdesc_immoff(const kv_t *kv, int32_t pc,
363 uint32_t *exdesc_immoff) {
364 if (!kv || !exdesc_immoff)
365 return KV_INVALID_ARGUMENT;
366 const Instruction *inst = ((KernelViewImpl *)kv)->getInstruction(pc);
367 if (!inst) {
368 return KV_INVALID_ARGUMENT;
369 } else if (!inst->getOpSpec().isAnySendFormat()) {
370 return KV_NON_SEND_INSTRUCTION;
371 } else if (inst->getSendFc() != SFID::UGM) {
372 return KV_DESCRIPTOR_INVALID;
373 }
374 if (inst->getOpSpec().isSendgFormat())
375 return KV_NON_SEND_INSTRUCTION;
376
377 const auto desc = inst->getMsgDescriptor();
378
379 auto addrType = ((desc.imm >> 29) & 0x3); // Desc[30:29]
380 // when desc is reg, we don't know the addrType so are not able to do this
381 // check
382 if (desc.isImm() && addrType != 1 && addrType != 2) // BSS/SS
383 return KV_DESCRIPTOR_INVALID;
384
385 *exdesc_immoff = inst->getExtImmOffDescriptor();
386 return KV_SUCCESS;
387 }
388
389 kv_status_t kv_get_send_indirect_descs(const kv_t *kv, int32_t pc,
390 uint8_t *ex_desc_reg,
391 uint8_t *ex_desc_subreg,
392 uint8_t *desc_reg,
393 uint8_t *desc_subreg) {
394 if (!kv || !ex_desc_reg || !ex_desc_subreg || !desc_reg || !desc_subreg)
395 return KV_INVALID_ARGUMENT;
396
397 const Instruction *inst = ((const KernelViewImpl *)kv)->getInstruction(pc);
398 if (!inst) {
399 return KV_INVALID_PC;
400 } else if (!inst->getOpSpec().isAnySendFormat()) {
401 return KV_NON_SEND_INSTRUCTION;
402 }
403 if (inst->getOpSpec().isSendgFormat())
404 return KV_NON_SEND_INSTRUCTION;
405 if (inst->getExtMsgDescriptor().isReg()) {
406 *ex_desc_reg = (uint8_t)inst->getExtMsgDescriptor().reg.regNum;
407 *ex_desc_subreg = (uint8_t)inst->getExtMsgDescriptor().reg.subRegNum;
408 } else {
409 *ex_desc_reg = *ex_desc_subreg = KV_INVALID_REG;
410 }
411
412 if (inst->getMsgDescriptor().isReg()) {
413 *desc_reg = (uint8_t)inst->getMsgDescriptor().reg.regNum;
414 *desc_subreg = (uint8_t)inst->getMsgDescriptor().reg.subRegNum;
415 } else {
416 *desc_reg = *desc_subreg = KV_INVALID_REG;
417 }
418 return KV_SUCCESS;
419 }
420
421 kv_status_t kv_get_sendg_desc(const kv_t *kv, int32_t pc, uint64_t *desc) {
422 if (!kv || !desc)
423 return KV_INVALID_ARGUMENT;
424 const Instruction *i = ((KernelViewImpl *)kv)->getInstruction(pc);
425 if (!i) {
426 return KV_INVALID_PC;
427 } else if (i->platform() <= Platform::XE3) {
428 return KV_INCAPABLE_PLATFORM;
429 } else if (!i->getOpSpec().isSendgFormat()) {
430 return KV_NON_SEND_INSTRUCTION;
431 }
432 *desc = i->getSendgDesc();
433 return KV_SUCCESS;
434 }
435 kv_status_t kv_get_sendg_ind_desc0(const kv_t *kv, int32_t pc,
436 uint32_t *subregPresent, uint32_t *subreg) {
437 if (!kv || !subreg || !subregPresent)
438 return KV_INVALID_ARGUMENT;
439 const Instruction *i = ((KernelViewImpl *)kv)->getInstruction(pc);
440 if (!i) {
441 return KV_INVALID_PC;
442 } else if (i->platform() <= Platform::XE3) {
443 return KV_INCAPABLE_PLATFORM;
444 } else if (!i->getOpSpec().isSendgFormat()) {
445 return KV_NON_SEND_INSTRUCTION;
446 }
447 const auto rr = i->getSendgIndDesc0Reg();
448 *subregPresent = rr != REGREF_INVALID ? 1 : 0;
449 if (rr != REGREF_INVALID)
450 *subreg = rr.subRegNum;
451 return KV_SUCCESS;
452 }
453 kv_status_t kv_get_sendg_ind_desc1(const kv_t *kv, int32_t pc,
454 uint32_t *subregPresent, uint32_t *subreg) {
455 if (!kv || !subreg || !subregPresent)
456 return KV_INVALID_ARGUMENT;
457 const Instruction *i = ((KernelViewImpl *)kv)->getInstruction(pc);
458 if (!i) {
459 return KV_INVALID_PC;
460 } else if (i->platform() <= Platform::XE3) {
461 return KV_INCAPABLE_PLATFORM;
462 } else if (!i->getOpSpec().isSendgFormat()) {
463 return KV_NON_SEND_INSTRUCTION;
464 }
465 const auto rr = i->getSendgIndDesc1Reg();
466 *subregPresent = rr != REGREF_INVALID ? 1 : 0;
467 if (rr != REGREF_INVALID)
468 *subreg = rr.subRegNum;
469 return KV_SUCCESS;
470 }
471
472 /******************** KernelView analysis APIs *******************************/
473 static const Instruction *getInstruction(const kv_t *kv, int32_t pc) {
474 if (!kv) {
475 return nullptr;
476 }
477 const Instruction *inst = ((KernelViewImpl *)kv)->getInstruction(pc);
478 return inst;
479 }
480
481 kv_status_t kv_get_send_exbso(const kv_t *kv, int32_t pc, int32_t *exbso) {
482 if (!kv || !exbso) {
483 return kv_status_t::KV_INVALID_ARGUMENT;
484 }
485
486 *exbso = -1;
487
488 Platform p = ((KernelViewImpl *)kv)->m_model.platform;
489 if (p < Platform::XE_HP)
490 return kv_status_t::KV_INCAPABLE_PLATFORM;
491
492 const Instruction *inst = getInstruction(kv, pc);
493 if (!inst) {
494 return kv_status_t::KV_INVALID_PC;
495 } else if (!inst || !inst->getOpSpec().isAnySendFormat()) {
496 return kv_status_t::KV_NON_SEND_INSTRUCTION;
497 }
498
499 if (inst->hasInstOpt(InstOpt::EXBSO))
500 *exbso = 1;
501 else
502 *exbso = 0;
503 return kv_status_t::KV_SUCCESS;
504 }
505
506 kv_status_t kv_get_message_type(const kv_t *kv, int32_t pc,
507 int32_t *message_type_enum) {
508 if (!kv || !message_type_enum) {
509 return kv_status_t::KV_INVALID_ARGUMENT;
510 }
511
512 const Instruction *inst = getInstruction(kv, pc);
513 if (!inst) {
514 return kv_status_t::KV_INVALID_PC;
515 } else if (!inst || !inst->getOpSpec().isAnySendFormat()) {
516 return kv_status_t::KV_NON_SEND_INSTRUCTION;
517 }
518
519 if (inst->platform() > Platform::XE3)
520 return KV_INCAPABLE_PLATFORM;
521 auto desc = inst->getMsgDescriptor();
522
523 // NOTE: we could probably get the message just from desc
524 if (desc.isReg())
525 return kv_status_t::KV_DESCRIPTOR_INDIRECT;
526
527 Platform p = ((KernelViewImpl *)kv)->m_model.platform;
528 SFMessageType msgType = getMessageType(p, inst->getSendFc(), desc.imm);
529 *message_type_enum = static_cast<int32_t>(msgType);
530
531 if (msgType == SFMessageType::INVALID)
532 return kv_status_t::KV_DESCRIPTOR_INVALID;
533
534 return kv_status_t::KV_SUCCESS;
535 }
536
537 kv_status_t kv_get_message_type_ext(const kv_t *kv, int32_t pc, uint32_t desc,
538 int32_t sfid, int32_t *message_type_enum) {
539 if (!kv || !message_type_enum) {
540 return kv_status_t::KV_INVALID_ARGUMENT;
541 }
542
543 const Instruction *inst = getInstruction(kv, pc);
544 if (!inst) {
545 return kv_status_t::KV_INVALID_PC;
546 } else if (!inst || !inst->getOpSpec().isAnySendFormat()) {
547 return kv_status_t::KV_NON_SEND_INSTRUCTION;
548 }
549
550 Platform p = ((KernelViewImpl *)kv)->m_model.platform;
551 SFMessageType msgType = getMessageType(p, (iga::SFID)sfid, desc);
552
553 *message_type_enum = static_cast<int32_t>(msgType);
554
555 if (msgType == SFMessageType::INVALID)
556 return kv_status_t::KV_DESCRIPTOR_INVALID;
557
558 return kv_status_t::KV_SUCCESS;
559 }
560
561 kv_status_t kv_get_message_sfid(const kv_t *kv, int32_t pc,
562 int32_t *sfid_enum) {
563 if (!kv || !sfid_enum) {
564 return kv_status_t::KV_INVALID_ARGUMENT;
565 }
566
567 const Instruction *inst = getInstruction(kv, pc);
568 if (!inst) {
569 return kv_status_t::KV_INVALID_PC;
570 } else if (!inst || !inst->getOpSpec().isAnySendFormat()) {
571 return kv_status_t::KV_NON_SEND_INSTRUCTION;
572 }
573
574 Platform p = ((KernelViewImpl *)kv)->m_model.platform;
575 if (p < Platform::XE) {
576 const auto exDesc = inst->getExtMsgDescriptor();
577
578 // <TGL: SFID is ExDesc[3:0]; if it's in a0, we're sunk
579 if (exDesc.isReg() && p < Platform::XE)
580 return kv_status_t::KV_DESCRIPTOR_INDIRECT;
581 }
582
583 SFID sfid = inst->getSendFc();
584 *sfid_enum = static_cast<int32_t>(sfid);
585
586 if (sfid == SFID::INVALID)
587 return kv_status_t::KV_DESCRIPTOR_INVALID;
588
589 return kv_status_t::KV_SUCCESS;
590 }
591
592 uint32_t kv_get_message_len(const kv_t *kv, int32_t pc, uint32_t *mLen,
593 uint32_t *emLen, uint32_t *rLen) {
594 if (!kv || !mLen || !emLen || !rLen)
595 return 0;
596
597 const Instruction *inst = getInstruction(kv, pc);
598 if (!inst)
599 return 0;
600 else if (!inst->getOpSpec().isAnySendFormat())
601 return 0;
602
603 // set the values and count how many are valid
604 uint32_t numSet = 0;
605 //
606 auto setOne = [&numSet](int n) {
607 if (n < 0)
608 return KV_INVALID_LEN;
609 numSet++;
610 return (uint32_t)n;
611 };
612
613 auto dstLen = inst->getDstLength();
614 // With sendg format, the DstLength is in the unit of bytes in Instruction
615 // Make it number of grfs
616 if (inst->getOpSpec().isSendgFormat())
617 dstLen /= 64;
618 *rLen = setOne(dstLen);
619 *mLen = setOne(inst->getSrc0Length());
620 *emLen = setOne(inst->getSrc1Length());
621
622 return numSet;
623 }
624
625 uint32_t kv_get_message_len_ext(const kv_t *kv, int32_t pc, uint32_t desc,
626 uint32_t exDesc, uint32_t *mLen,
627 uint32_t *emLen, uint32_t *rLen) {
628 if (!mLen || !emLen || !rLen)
629 return 0;
630
631 const Instruction *inst = getInstruction(kv, pc);
632 if (!inst) {
633 return 0;
634 }
635
636 Platform p = ((KernelViewImpl *)kv)->m_model.platform;
637
638 return getMessageLengths(p, inst->getOpSpec(), exDesc, desc, mLen, emLen,
639 rLen);
640 }
641
642 uint32_t kv_get_execution_size(const kv_t *kv, int32_t pc) {
643 if (!kv) {
644 return static_cast<uint32_t>(ExecSize::INVALID);
645 }
646 const Instruction *inst = getInstruction(kv, pc);
647 if (!inst) {
648 return static_cast<uint32_t>(ExecSize::INVALID);
649 }
650
651 return static_cast<uint32_t>(inst->getExecSize());
652 }
653
654 bool kv_get_swsb_info(const kv_t *kv, int32_t pc, SWSB_ENCODE_MODE,
655 SWSB &swsb) {
656 if (!kv) {
657 return false;
658 }
659 const Instruction *inst = getInstruction(kv, pc);
660 if (!inst) {
661 return false;
662 }
663 swsb = inst->getSWSB();
664 return true;
665 }
666
667 int32_t kv_get_number_sources(const kv_t *kv, int32_t pc) {
668 if (!kv) {
669 return -1;
670 }
671 const Instruction *inst = getInstruction(kv, pc);
672 if (!inst) {
673 return -1;
674 }
675
676 return inst->getSourceCount();
677 }
678
679 uint32_t kv_get_opcode(const kv_t *kv, int32_t pc) {
680 if (!kv) {
681 return static_cast<uint32_t>(Op::INVALID);
682 }
683 const Instruction *inst = getInstruction(kv, pc);
684 if (!inst) {
685 return static_cast<uint32_t>(Op::INVALID);
686 }
687 return static_cast<uint32_t>(inst->getOpSpec().op);
688 }
689
690 kv_status_t kv_get_subfunction(const kv_t *kv, int32_t pc, uint32_t *subfunc) {
691 *subfunc = static_cast<uint32_t>(InvalidFC::INVALID);
692
693 if (!kv) {
694 return KV_INVALID_ARGUMENT;
695 }
696 const Instruction *inst = getInstruction(kv, pc);
697 if (!inst) {
698 return KV_INVALID_PC;
699 }
700
701 // for send, get_message_sfid to support decoding SFID from exDesc
702 if (inst->getOpSpec().isAnySendFormat()) {
703 int32_t sfid = -1;
704 kv_status_t st = kv_get_message_sfid(kv, pc, &sfid);
705 *subfunc = static_cast<uint32_t>(sfid);
706 return st;
707 }
708
709 *subfunc = inst->getSubfunction().bits;
710
711 if (!inst->getSubfunction().isValid())
712 return KV_NO_SUBFUNCTION;
713
714 return KV_SUCCESS;
715 }
716
717 int32_t kv_get_has_destination(const kv_t *kv, int32_t pc) {
718 if (!kv) {
719 return -1;
720 }
721 const Instruction *inst = getInstruction(kv, pc);
722 if (!inst || inst->getOp() == Op::ILLEGAL) {
723 return -1;
724 }
725
726 const OpSpec &instSpec = inst->getOpSpec();
727 return instSpec.supportsDestination() ? 1 : 0;
728 }
729
730 int32_t kv_get_destination_register(const kv_t *kv, int32_t pc) {
731 if (!kv) {
732 return -1;
733 }
734 const Instruction *inst = getInstruction(kv, pc);
735 if (!inst) {
736 return -1;
737 }
738 if (!inst->getOpSpec().supportsDestination()) {
739 return -1;
740 }
741 const Operand &dst = inst->getDestination();
742 if (dst.getKind() == Operand::Kind::DIRECT ||
743 dst.getKind() == Operand::Kind::MACRO) {
744 return dst.getDirRegRef().regNum;
745 }
746
747 if (dst.getKind() == Operand::Kind::INDIRECT) {
748 return dst.getIndAddrReg().regNum;
749 }
750 return -1;
751 }
752
753 int32_t kv_get_destination_sub_register(const kv_t *kv, int32_t pc) {
754 if (!kv) {
755 return -1;
756 }
757 const Instruction *inst = getInstruction(kv, pc);
758 if (!inst || inst->getOp() == Op::ILLEGAL) {
759 return -1;
760 }
761
762 const OpSpec &instSpec = inst->getOpSpec();
763 if (!instSpec.supportsDestination()) {
764 return -1;
765 }
766 const Operand &dst = inst->getDestination();
767 if (dst.getKind() == Operand::Kind::DIRECT) {
768 return dst.getDirRegRef().subRegNum;
769 }
770
771 if (dst.getKind() == Operand::Kind::INDIRECT) {
772 return dst.getIndAddrReg().subRegNum;
773 }
774
775 return -1;
776 }
777
778 uint32_t kv_get_destination_data_type(const kv_t *kv, int32_t pc) {
779 if (!kv) {
780 return static_cast<uint32_t>(Type::INVALID);
781 }
782 const Instruction *inst = getInstruction(kv, pc);
783 if (!inst || inst->getOp() == Op::ILLEGAL) {
784 return static_cast<uint32_t>(Type::INVALID);
785 }
786 return (int32_t)inst->getDestination().getType();
787 }
788
789 uint32_t kv_get_destination_register_type(const kv_t *kv, int32_t pc) {
790 if (!kv) {
791 return static_cast<uint32_t>(RegName::INVALID);
792 }
793 const Instruction *inst = getInstruction(kv, pc);
794 if (!inst || inst->getOp() == Op::ILLEGAL) {
795 return static_cast<uint32_t>(RegName::INVALID);
796 }
797 return static_cast<uint32_t>(inst->getDestination().getDirRegName());
798 }
799
800 uint32_t kv_get_destination_register_kind(const kv_t *kv, int32_t pc) {
801 if (!kv) {
802 return static_cast<uint32_t>(Kind::INVALID);
803 }
804 const Instruction *inst = getInstruction(kv, pc);
805 if (!inst || inst->getOp() == Op::ILLEGAL) {
806 return static_cast<uint32_t>(Kind::INVALID);
807 }
808 return (uint32_t)inst->getDestination().getKind();
809 }
810
811 int32_t kv_get_source_register(const kv_t *kv, int32_t pc,
812 uint32_t sourceNumber) {
813 if (!kv) {
814 return -1;
815 }
816 const Instruction *inst = getInstruction(kv, pc);
817 if (!inst || sourceNumber >= inst->getSourceCount()) {
818 return -1;
819 }
820 const auto &src = inst->getSource((size_t)sourceNumber);
821 if (src.getKind() == Operand::Kind::DIRECT ||
822 src.getKind() == Operand::Kind::MACRO) {
823 return (int32_t)src.getDirRegRef().regNum;
824 }
825
826 if (src.getKind() == Operand::Kind::INDIRECT) {
827 return (int32_t)src.getIndAddrReg().regNum;
828 }
829
830 return -1;
831 }
832
833 int32_t kv_get_source_sub_register(const kv_t *kv, int32_t pc,
834 uint32_t sourceNumber) {
835 if (!kv) {
836 return -1;
837 }
838 const Instruction *inst = getInstruction(kv, pc);
839 if (!inst || inst->getOp() == Op::ILLEGAL ||
840 sourceNumber >= inst->getSourceCount()) {
841 return -1;
842 }
843 const auto &src = inst->getSource((size_t)sourceNumber);
844 if (src.getKind() == Operand::Kind::DIRECT) {
845 return (int32_t)src.getDirRegRef().subRegNum;
846 }
847
848 if (src.getKind() == Operand::Kind::INDIRECT) {
849 return (int32_t)src.getIndAddrReg().subRegNum;
850 }
851 return -1;
852 }
853
854 uint32_t kv_get_source_data_type(const kv_t *kv, int32_t pc,
855 uint32_t sourceNumber) {
856 if (!kv) {
857 return static_cast<uint32_t>(Type::INVALID);
858 }
859 const Instruction *inst = getInstruction(kv, pc);
860 if (!inst || sourceNumber >= inst->getSourceCount()) {
861 return static_cast<uint32_t>(Type::INVALID);
862 }
863 const auto &src = inst->getSource((size_t)sourceNumber);
864 if (src.getKind() == Operand::Kind::INVALID) {
865 return static_cast<uint32_t>(Type::INVALID);
866 }
867 return static_cast<uint32_t>(src.getType());
868 }
869
870 uint32_t kv_get_source_register_type(const kv_t *kv, int32_t pc,
871 uint32_t sourceNumber) {
872 if (!kv) {
873 return static_cast<uint32_t>(RegName::INVALID);
874 }
875 const Instruction *inst = getInstruction(kv, pc);
876 if (!inst || sourceNumber >= inst->getSourceCount()) {
877 return static_cast<uint32_t>(RegName::INVALID);
878 }
879 const auto &src = inst->getSource((size_t)sourceNumber);
880 if (src.getKind() == Operand::Kind::INVALID) {
881 return (uint32_t)-1;
882 }
883 return static_cast<uint32_t>(src.getDirRegName());
884 }
885
886 uint32_t kv_get_source_register_kind(const kv_t *kv, int32_t pc,
887 uint32_t sourceNumber) {
888 if (!kv) {
889 return static_cast<uint32_t>(Kind::INVALID);
890 }
891 const Instruction *inst = getInstruction(kv, pc);
892 if (!inst || sourceNumber >= inst->getSourceCount()) {
893 return static_cast<uint32_t>(Kind::INVALID);
894 }
895 return static_cast<uint32_t>(
896 inst->getSource((uint8_t)sourceNumber).getKind());
897 }
898
899 int32_t kv_is_source_vector(const kv_t *kv, int32_t pc, uint32_t sourceNumber) {
900 if (!kv) {
901 return -1;
902 }
903 const Instruction *inst = getInstruction(kv, pc);
904 if (!inst || sourceNumber >= inst->getSourceCount()) {
905 return -1;
906 }
907
908 const auto &src = inst->getSource((uint8_t)sourceNumber);
909 if (src.getKind() != Operand::Kind::DIRECT &&
910 src.getKind() != Operand::Kind::INDIRECT) {
911 return -1;
912 }
913
914 auto rgn = src.getRegion();
915 if (rgn == Region::SRC010 || rgn == Region::SRC0X0 || rgn == Region::SRCXX0) {
916 return 0;
917 }
918
919 return 1;
920 }
921
922 uint32_t kv_get_channel_offset(const kv_t *kv, int32_t pc) {
923 if (!kv) {
924 return static_cast<uint32_t>(ChannelOffset::M0);
925 }
926 const Instruction *inst = getInstruction(kv, pc);
927 if (!inst || inst->getOp() == Op::ILLEGAL) {
928 return static_cast<uint32_t>(ChannelOffset::M0);
929 }
930 return (uint32_t)inst->getChannelOffset();
931 }
932
933 uint32_t kv_get_mask_control(const kv_t *kv, int32_t pc) {
934 if (!kv) {
935 return static_cast<uint32_t>(MaskCtrl::NORMAL);
936 }
937 const Instruction *inst = getInstruction(kv, pc);
938 if (!inst || inst->getOp() == Op::ILLEGAL) {
939 return static_cast<uint32_t>(MaskCtrl::NORMAL);
940 }
941 return (uint32_t)inst->getMaskCtrl();
942 }
943
944 int32_t kv_get_destination_region(const kv_t *kv, int32_t pc, uint32_t *hz) {
945 uint32_t DstRgnHz = static_cast<uint32_t>(Region::Horz::HZ_INVALID);
946 if (!kv) {
947 *hz = DstRgnHz;
948 return -1;
949 }
950 const Instruction *inst = getInstruction(kv, pc);
951 if (!inst || !inst->getOpSpec().supportsDestination()) {
952 *hz = DstRgnHz;
953 return -1;
954 }
955 const Operand &dst = inst->getDestination();
956 DstRgnHz = static_cast<uint32_t>(dst.getRegion().getHz());
957 *hz = DstRgnHz;
958 return 0;
959 }
960
961 int32_t kv_get_source_region(const kv_t *kv, int32_t pc, uint32_t src_op,
962 uint32_t *vt, uint32_t *wi, uint32_t *hz) {
963 uint32_t SrcRgnVt = static_cast<uint32_t>(Region::Vert::VT_INVALID);
964 uint32_t SrcRgnWi = static_cast<uint32_t>(Region::Width::WI_INVALID);
965 uint32_t SrcRgnHz = static_cast<uint32_t>(Region::Horz::HZ_INVALID);
966 const uint32_t c_maxSrcOperands = 3;
967 if (!kv && src_op < c_maxSrcOperands) {
968 *vt = SrcRgnVt;
969 *wi = SrcRgnWi;
970 *hz = SrcRgnHz;
971 return -1;
972 }
973 const Instruction *inst = getInstruction(kv, pc);
974 if (!inst || inst->getSourceCount() <= src_op) {
975 *vt = SrcRgnVt;
976 *wi = SrcRgnWi;
977 *hz = SrcRgnHz;
978 return -1;
979 }
980 const Operand &src = inst->getSource(src_op);
981 if (!(src.getKind() == Operand::Kind::DIRECT ||
982 src.getKind() == Operand::Kind::INDIRECT) ||
983 !(src.getDirRegName() == RegName::GRF_R ||
984 src.getDirRegName() == RegName::ARF_SR ||
985 src.getDirRegName() == RegName::ARF_ACC)) {
986 *vt = SrcRgnVt;
987 *wi = SrcRgnWi;
988 *hz = SrcRgnHz;
989 return -1;
990 }
991 SrcRgnVt = static_cast<uint32_t>(src.getRegion().getVt());
992 SrcRgnWi = static_cast<uint32_t>(src.getRegion().getWi());
993 SrcRgnHz = static_cast<uint32_t>(src.getRegion().getHz());
994 *vt = SrcRgnVt;
995 *wi = SrcRgnWi;
996 *hz = SrcRgnHz;
997 return 0;
998 }
999
1000 int32_t kv_get_source_immediate(const kv_t *kv, int32_t pc, uint32_t src_op,
1001 uint64_t *imm) {
1002 if (!kv) {
1003 return -1;
1004 }
1005 const Instruction *inst = getInstruction(kv, pc);
1006 if (!inst || src_op >= inst->getSourceCount()) {
1007 return -1;
1008 }
1009 const auto &src = inst->getSource((size_t)src_op);
1010 if (src.getKind() != Operand::Kind::IMMEDIATE) {
1011 return -1;
1012 }
1013 *imm = src.getImmediateValue().u64;
1014 return 0;
1015 }
1016
1017 int32_t kv_get_source_indirect_imm_off(const kv_t *kv, int32_t pc,
1018 uint32_t src_op, int16_t *immoff) {
1019 if (!kv) {
1020 return -1;
1021 }
1022 const Instruction *inst = getInstruction(kv, pc);
1023 if (!inst || src_op >= inst->getSourceCount()) {
1024 return -1;
1025 }
1026 const auto &src = inst->getSource((size_t)src_op);
1027 if (src.getKind() != Operand::Kind::INDIRECT) {
1028 return -1;
1029 }
1030 *immoff = src.getIndImmAddr();
1031 return 0;
1032 }
1033
1034 int32_t kv_get_destination_indirect_imm_off(const kv_t *kv, int32_t pc,
1035 int16_t *immoff) {
1036 if (!kv) {
1037 return -1;
1038 }
1039 const Instruction *inst = getInstruction(kv, pc);
1040 if (!inst)
1041 return -1;
1042
1043 if (!inst->getOpSpec().supportsDestination()) {
1044 return -1;
1045 }
1046 const Operand &dst = inst->getDestination();
1047 if (dst.getKind() != Operand::Kind::INDIRECT) {
1048 return -1;
1049 }
1050
1051 *immoff = dst.getIndImmAddr();
1052 return 0;
1053 }
1054
1055 static int16_t getMathMacroNum(MathMacroExt mme) {
1056 switch (mme) {
1057 case MathMacroExt::MME0:
1058 return 0;
1059 case MathMacroExt::MME1:
1060 return 1;
1061 case MathMacroExt::MME2:
1062 return 2;
1063 case MathMacroExt::MME3:
1064 return 3;
1065 case MathMacroExt::MME4:
1066 return 4;
1067 case MathMacroExt::MME5:
1068 return 5;
1069 case MathMacroExt::MME6:
1070 return 6;
1071 case MathMacroExt::MME7:
1072 return 7;
1073 case MathMacroExt::NOMME: // follow the encoding value, set NOMME to 8
1074 return 8;
1075 case MathMacroExt::INVALID:
1076 break;
1077 }
1078 return -1;
1079 }
1080
1081 int32_t kv_get_source_mme_number(const kv_t *kv, int32_t pc, uint32_t src_op,
1082 int16_t *mme) {
1083 *mme = -1;
1084 if (!kv) {
1085 return -1;
1086 }
1087 const Instruction *inst = getInstruction(kv, pc);
1088 if (!inst || src_op >= inst->getSourceCount()) {
1089 return -1;
1090 }
1091 const auto &src = inst->getSource((size_t)src_op);
1092
1093 *mme = getMathMacroNum(src.getMathMacroExt());
1094 if (*mme == -1)
1095 return -1;
1096 return 0;
1097 }
1098
1099 int32_t kv_get_destination_mme_number(const kv_t *kv, int32_t pc,
1100 int16_t *mme) {
1101 *mme = -1;
1102 if (!kv)
1103 return -1;
1104
1105 const Instruction *inst = getInstruction(kv, pc);
1106 if (!inst)
1107 return -1;
1108
1109 if (!inst->getOpSpec().supportsDestination()) {
1110 return -1;
1111 }
1112 const Operand &dst = inst->getDestination();
1113 *mme = getMathMacroNum(dst.getMathMacroExt());
1114 if (*mme == -1)
1115 return -1;
1116 return 0;
1117 }
1118
1119 uint32_t kv_get_flag_modifier(const kv_t *kv, int32_t pc) {
1120 if (!kv) {
1121 return static_cast<uint32_t>(FlagModifier::NONE);
1122 }
1123 const Instruction *inst = getInstruction(kv, pc);
1124 if (!inst || inst->getOp() == Op::ILLEGAL) {
1125 return static_cast<uint32_t>(FlagModifier::NONE);
1126 }
1127 return (uint32_t)inst->getFlagModifier();
1128 }
1129
1130 uint32_t kv_get_source_modifier(const kv_t *kv, int32_t pc, uint32_t src_op) {
1131 if (!kv) {
1132 return static_cast<uint32_t>(SrcModifier::NONE);
1133 }
1134 const Instruction *inst = getInstruction(kv, pc);
1135
1136 if (!inst || inst->getOp() == Op::ILLEGAL) {
1137 return static_cast<uint32_t>(SrcModifier::NONE);
1138 }
1139 if (!inst || src_op >= inst->getSourceCount()) {
1140 return static_cast<uint32_t>(SrcModifier::NONE);
1141 }
1142 const auto &src = inst->getSource((size_t)src_op);
1143 if (src.getKind() == Operand::Kind::IMMEDIATE) {
1144 return static_cast<uint32_t>(SrcModifier::NONE);
1145 }
1146
1147 return (uint32_t)src.getSrcModifier();
1148 }
1149
1150 uint32_t kv_get_destination_modifier(const kv_t *kv, int32_t pc) {
1151 if (!kv) {
1152 return static_cast<uint32_t>(DstModifier::NONE);
1153 }
1154 const Instruction *inst = getInstruction(kv, pc);
1155
1156 if (!inst || inst->getOp() == Op::ILLEGAL) {
1157 return static_cast<uint32_t>(DstModifier::NONE);
1158 }
1159
1160 const Operand &dst = inst->getDestination();
1161
1162 return (uint32_t)dst.getDstModifier();
1163 }
1164
1165 int32_t kv_get_flag_register(const kv_t *kv, int32_t pc) {
1166 if (!kv) {
1167 return -1;
1168 }
1169 const Instruction *inst = getInstruction(kv, pc);
1170
1171 if (!inst || inst->getOp() == Op::ILLEGAL) {
1172 return -1;
1173 }
1174
1175 return (int32_t)inst->getFlagReg().regNum;
1176 }
1177
1178 int32_t kv_get_flag_sub_register(const kv_t *kv, int32_t pc) {
1179 if (!kv) {
1180 return -1;
1181 }
1182 const Instruction *inst = getInstruction(kv, pc);
1183 if (!inst || inst->getOp() == Op::ILLEGAL) {
1184 return -1;
1185 }
1186
1187 return (int32_t)inst->getFlagReg().subRegNum;
1188 }
1189
1190 uint32_t kv_get_predicate(const kv_t *kv, int32_t pc) {
1191 if (!kv) {
1192 return static_cast<uint32_t>(PredCtrl::NONE);
1193 }
1194 const Instruction *inst = getInstruction(kv, pc);
1195 if (!inst || inst->getOp() == Op::ILLEGAL) {
1196 return static_cast<uint32_t>(PredCtrl::NONE);
1197 }
1198 return (uint32_t)inst->getPredication().function;
1199 }
1200
1201 uint32_t kv_get_is_inverse_predicate(const kv_t *kv, int32_t pc) {
1202 if (!kv) {
1203 return 0;
1204 }
1205 const Instruction *inst = getInstruction(kv, pc);
1206 if (!inst || inst->getOp() == Op::ILLEGAL) {
1207 return 0;
1208 }
1209 return (uint32_t)inst->getPredication().inverse;
1210 }
1211
1212 kv_status_t kv_get_cache_opt(const kv_t *kv, int32_t pc, int32_t cache_level,
1213 int32_t *cacheopt_enum) {
1214 if (!kv) {
1215 return kv_status_t::KV_INVALID_ARGUMENT;
1216 }
1217
1218 const Instruction *inst = getInstruction(kv, pc);
1219 if (!inst) {
1220 return kv_status_t::KV_INVALID_PC;
1221 } else if (!inst->getOpSpec().isAnySendFormat()) {
1222 return kv_status_t::KV_NON_SEND_INSTRUCTION;
1223 }
1224
1225 const DecodeResult di = tryDecode(*inst, nullptr);
1226 if (!di) {
1227 return kv_status_t::KV_DECODE_ERROR;
1228 }
1229
1230 switch (static_cast<CacheLevel>(cache_level)) {
1231 case CacheLevel::L1:
1232 *cacheopt_enum = static_cast<int32_t>(di.info.cachingL1);
1233 break;
1234 case CacheLevel::L3:
1235 *cacheopt_enum = static_cast<int32_t>(di.info.cachingL3);
1236 break;
1237 default:
1238 return kv_status_t::KV_INVALID_ARGUMENT;
1239 }
1240
1241 return kv_status_t::KV_SUCCESS;
1242 }
1243
1244 int32_t kv_get_syncfc(const kv_t *kv, int32_t pc) {
1245 if (!kv) {
1246 return static_cast<int32_t>(iga::SyncFC::INVALID);
1247 }
1248 const Instruction *inst = getInstruction(kv, pc);
1249 if (!inst || inst->getOp() != iga::Op::SYNC) {
1250 return static_cast<int32_t>(iga::SyncFC::INVALID);
1251 }
1252
1253 // for send, get_message_sfid to support decoding SFID from exDesc
1254 return static_cast<int32_t>(inst->getSyncFc());
1255 }