Debian Codegraph @

visa/iga/IGALibrary/IR/InstBuilder.hpp

raw
1 /*========================== begin_copyright_notice ============================
2
3 Copyright (C) 2017-2021 Intel Corporation
4
5 SPDX-License-Identifier: MIT
6
7 ============================= end_copyright_notice ===========================*/
8
9 #ifndef _IGA_INST_BUILDER_HPP_
10 #define _IGA_INST_BUILDER_HPP_
11
12 #include "../ErrorHandler.hpp"
13 #include "../Frontend/IRToString.hpp"
14 #include "../Models/Models.hpp"
15 #include "../asserts.hpp"
16 #include "Kernel.hpp"
17 #include "Messages.hpp"
18
19 #include <map>
20 #include <sstream>
21 #include <string>
22 #include <tuple>
23 #include <vector>
24
25 namespace iga {
26 // The IR Builder is called by various instruction generators that build
27 // full instructions from partial state. This allows us to separate IR
28 // construction from the actual syntax processing or decoding.
29 //
30 // This is needed since we have to save information about the operands
31 // during the parse. We can't create operands as we see them since we don't
32 // have an existing Instruction * until InstEnd(). E.g. createSend
33 // requires us having seen exDesc and desc. Since these follow the operands;
34 // hence, we must save operand info as state as we go. This allows us to
35 // treat instructions as more immutable entities rather than complex state
36 // machines (via mutation) and allows us to be a little more sure that
37 // Instruction values are correct states.
38 //
39 // A better decision would be to make the IR types mutable by at least this
40 // class
41 struct OperandInfo {
42 Loc loc;
43 Operand::Kind kind = Operand::Kind::INVALID;
44
45 union // optional modifier (e.g. -r12, ~r12, (abs) (sat))
46 {
47 SrcModifier regOpSrcMod = SrcModifier::NONE;
48 DstModifier regOpDstMod;
49 };
50 RegName regOpName = RegName::INVALID; // e.g. r#, a#, null, ...
51 Region regOpRgn = Region::INVALID; // e.g. <1>, <8;8,1>
52 MathMacroExt regOpMathMacroExtReg =
53 MathMacroExt::INVALID; // e.g. math macro spc acc
54
55 // direct/indirect register info
56 RegRef regOpReg; // direct operands
57
58 // indirect register offset
59 int16_t regOpIndOff = 0; // e.g. "16" in "r[a0.4,16]"
60
61 // imm field
62 ImmVal immValue;
63
64 std::string immLabel;
65 Type type = Type::INVALID;
66
67 OperandInfo() {
68 kind = Operand::Kind::INVALID;
69 regOpSrcMod = SrcModifier::NONE;
70 regOpName = RegName::INVALID;
71 regOpRgn = Region::INVALID;
72 regOpMathMacroExtReg = MathMacroExt::INVALID;
73 regOpReg = REGREF_ZERO_ZERO;
74 regOpIndOff = 0;
75 immValue.u64 = 0;
76 immValue.kind = ImmVal::Kind::UNDEF;
77 immLabel.clear();
78 type = Type::INVALID;
79 }
80
81 void reset() {
82 kind = Operand::Kind::INVALID;
83 regOpSrcMod = SrcModifier::NONE;
84 regOpName = RegName::INVALID;
85 regOpRgn = Region::INVALID;
86 regOpMathMacroExtReg = MathMacroExt::INVALID;
87 regOpReg = REGREF_ZERO_ZERO;
88 regOpIndOff = 0;
89 immValue.u64 = 0;
90 immValue.kind = ImmVal::Kind::UNDEF;
91 immLabel.clear();
92 type = Type::INVALID;
93 }
94 };
95
96 // Larger constructs, such as blocks, instructions, and whatnot
97 // generally consist of a ***Start() and ***End() method to denote the
98 // start and end of each feature. Upon parse or semantic error, the
99 // parser may terminate early, and corresponding ***End() functions may
100 // not be called in those cases.
101 //
102 // The methods are roughly called in syntax order, but this isn't a strict
103 // requirement and the handler should not make any assumptions about that.
104 class InstBuilder {
105 const Model &m_model;
106 ErrorHandler &m_errorHandler;
107 Kernel *m_kernel;
108
109 // per-instruction state
110 Loc m_loc;
111 Predication m_predication;
112 const OpSpec *m_opSpec = nullptr;
113
114 Subfunction m_subfunc;
115
116 RegRef m_flagReg; // shared by predication / condition modifier
117
118 ExecSize m_execSize = ExecSize::INVALID;
119 ChannelOffset m_chOff = ChannelOffset::M0;
120 MaskCtrl m_maskCtrl = MaskCtrl::NOMASK;
121
122 FlagModifier m_flagModifier = FlagModifier::NONE;
123 DstModifier m_dstModifier = DstModifier::NONE;
124
125 OperandInfo m_dst;
126 OperandInfo m_srcs[5];
127 int m_nSrcs = 0;
128
129 uint32_t m_exImmOffDesc;
130 SendDesc m_exDesc;
131 SendDesc m_desc;
132
133 uint64_t m_sendgDesc = 0x0;
134 RegRef m_sendgIndDesc0Reg{REGREF_INVALID};
135 RegRef m_sendgIndDesc1Reg{REGREF_INVALID};
136
137 // int m_sendDstLen; // (extracted later)
138 int m_sendSrc0Len = -1;
139 int m_sendSrc1Len = -1;
140
141 InstOptSet m_instOpts;
142
143 Instruction::InlineBinaryType m_inlineInstBinary = {0};
144 bool m_isInlineBinaryInst = false;
145
146 std::string m_comment;
147
148 ////////////////////////////////////////////////////////////////////
149 // During parse, we add all instructions to one list, record label
150 // locations and unresolved operands. At the end of the parse,
151 // we'll resolve everything to numeric form and then split things into
152 // blocks at the end (if desired)
153 //
154 // one full linear list of instructions,
155 InstList m_insts;
156 //
157 // labels defined (block starts)
158 // (start-loc,start-pc,end-loc,end-pc
159 using LabelInfo = std::tuple<Loc, uint32_t>;
160 std::map<std::string, LabelInfo> m_labelMap;
161 LabelInfo *m_currBlock = nullptr;
162 // unresolved operand labels
163 struct UnresolvedLabel {
164 Loc loc;
165 std::string symbol;
166 Operand &operand;
167 Instruction &inst;
168 };
169 std::vector<UnresolvedLabel> m_unresolvedLabels;
170
171 uint32_t m_pc = 0; // current PC
172 uint32_t m_nextId = 0; // next instruction id
173
174 SWSB_ENCODE_MODE m_swsbEncodeMode;
175
176 public:
177 struct SWSBInfo {
178 SWSB::DistType distType = SWSB::DistType::NO_DIST;
179 uint32_t regDist = 0; // e.g. @3 0 = not set
180 int32_t memSBidAlloc = -1; // e.g. $2 -1 = not set
181 int32_t memSBidDst = -1; // e.g. $2.dst -1 = not set
182 int32_t memSBidSrc = -1; // e.g. $2.src -1 = not set
183 int32_t memSBidCntr = -1; // e.g. $2.inc -1 = not set
184 SWSB::SpecialToken spToken = SWSB::SpecialToken::NONE;
185
186 SWSBInfo() {} // sets default values as above
187
188 bool anyBarrierSet() const {
189 return sbidAllocSet() || sbidDstSet() || sbidSrcSet();
190 }
191 bool sbidAllocSet() const { return memSBidAlloc >= 0; }
192 bool sbidDstSet() const { return memSBidDst >= 0; }
193 bool sbidSrcSet() const { return memSBidSrc >= 0; }
194 bool sbidCntrSet() const { return memSBidCntr >= 0; }
195 bool specialTokenSet() const { return spToken != SWSB::SpecialToken::NONE; }
196
197 bool operator==(const SWSBInfo &sbi) const {
198 return distType == sbi.distType && regDist == sbi.regDist &&
199 memSBidAlloc == sbi.memSBidAlloc && memSBidDst == sbi.memSBidDst &&
200 memSBidSrc == sbi.memSBidSrc && spToken == sbi.spToken;
201 }
202 bool operator!=(const SWSBInfo &sbi) const { return !(*this == sbi); }
203 };
204
205 private:
206 SWSBInfo m_depInfo;
207
208 Platform platform() const { return m_model.platform; }
209
210 void clearInstState() {
211 m_predication.function = PredCtrl::NONE;
212 m_predication.inverse = false;
213
214 m_flagReg = REGREF_ZERO_ZERO;
215
216 m_opSpec = nullptr;
217
218 m_execSize = ExecSize::SIMD1;
219 m_chOff = ChannelOffset::M0;
220 m_subfunc = InvalidFC::INVALID; // invalid
221 m_maskCtrl = MaskCtrl::NORMAL;
222
223 m_flagModifier = FlagModifier::NONE;
224
225 m_dstModifier = DstModifier::NONE;
226
227 m_dst.reset();
228 for (auto &m_src : m_srcs) {
229 m_src.reset();
230 }
231 m_nSrcs = 0;
232
233 m_exImmOffDesc = 0;
234 m_exDesc.imm = 0;
235 m_desc.imm = 0;
236
237 m_sendgDesc = 0x0;
238 m_sendgIndDesc0Reg = REGREF_INVALID;
239 m_sendgIndDesc1Reg = REGREF_INVALID;
240
241 m_sendSrc0Len = m_sendSrc1Len = -1;
242
243 m_instOpts.clear();
244
245 m_depInfo = SWSBInfo();
246
247 m_comment.clear();
248
249 m_isInlineBinaryInst = false;
250 m_inlineInstBinary.fill(0);
251 }
252
253 public:
254 InstBuilder(Kernel *kernel, ErrorHandler &e)
255 : m_model(kernel->getModel()), m_errorHandler(e), m_kernel(kernel) {
256 m_swsbEncodeMode = m_model.getSWSBEncodeMode();
257 }
258
259 InstList &getInsts() { return m_insts; }
260
261 ErrorHandler &errorHandler() { return m_errorHandler; }
262
263 ///////////////////////////////////////////////////////////////////////////
264 // specific IR accessors
265 bool isMacroOp() const {
266 return m_opSpec->is(Op::MADM) ||
267 (m_opSpec->is(Op::MATH) && IsMacro(m_subfunc.math));
268 }
269
270 const SendDesc getExDesc() const { return m_exDesc; }
271
272 Subfunction getSubfunction() const { return m_subfunc; }
273
274 ///////////////////////////////////////////////////////////////////////////
275 // specific IR setters
276 void setSWSBEncodingMode(SWSB_ENCODE_MODE mode) { m_swsbEncodeMode = mode; }
277
278 // Called at the beginning of the program
279 void ProgramStart() {
280 m_pc = 0;
281 m_nextId = 0;
282 }
283
284 void ProgramEnd() {
285 for (const UnresolvedLabel &u : m_unresolvedLabels) {
286 auto itr = m_labelMap.find(u.symbol);
287 if (itr == m_labelMap.end()) {
288 m_errorHandler.reportError(u.loc, "undefined label");
289 } else {
290 const LabelInfo &li = itr->second;
291 int32_t val = (int32_t)std::get<1>(li);
292 if (!u.inst.getOpSpec().isJipAbsolute()) {
293 val -= u.inst.getPC();
294 }
295 u.operand.setLabelSource(val, u.operand.getType());
296 }
297 }
298 // at this point m_instList has instructions with all labels in
299 // numeric form
300 }
301
302 void BlockStart(const Loc &loc, const std::string &label) {
303 auto itr = m_labelMap.find(label);
304 if (itr != m_labelMap.end()) {
305 std::stringstream err;
306 err << "label redefinition " << label << " (defined "
307 << "on line " << std::get<0>(itr->second).line << ")";
308 m_errorHandler.reportError(loc, err.str());
309 } else {
310 m_labelMap[label] = LabelInfo(loc, m_pc);
311 m_currBlock = &m_labelMap[label];
312 }
313 }
314
315 void BlockEnd(uint32_t extent) {
316 if (m_currBlock) {
317 // could be null if error in BlockStart
318 std::get<0>(*m_currBlock).extent = extent;
319 m_currBlock = nullptr;
320 }
321 }
322
323 void InstStart(const Loc &loc) {
324 clearInstState();
325 m_loc = loc;
326 }
327
328 void InstEnd(uint32_t extent) {
329 // set the full instruction length in chars (for text)
330 // or bytes (for decoding bits)
331 m_loc.extent = extent;
332 IGA_ASSERT(m_opSpec != nullptr, "OpSpec never set");
333
334 Instruction *inst = nullptr;
335 if (m_opSpec->is(Op::MATH)) {
336 inst = m_kernel->createBasicInstruction(
337 *m_opSpec, m_predication, m_flagReg, m_execSize, m_chOff, m_maskCtrl,
338 m_flagModifier, m_subfunc);
339 } else if (m_opSpec->format == OpSpec::Format::SYNC_UNARY) {
340 inst = m_kernel->createBasicInstruction(
341 *m_opSpec, m_predication, m_flagReg, m_execSize, m_chOff, m_maskCtrl,
342 FlagModifier::NONE, m_subfunc);
343 } else if (m_opSpec->isBranching()) {
344 inst = m_kernel->createBranchInstruction(*m_opSpec, m_predication,
345 m_flagReg, m_execSize, m_chOff,
346 m_maskCtrl, m_subfunc);
347 } else if (m_opSpec->isAnySendFormat()) {
348 if (m_subfunc.send == SFID::INVALID) {
349 if (platform() <= Platform::GEN11 && m_exDesc.isImm()) {
350 m_subfunc.send = sfidFromEncoding(platform(), m_exDesc.imm);
351 }
352 }
353 if (m_opSpec->isSendgFormat()) {
354 inst = m_kernel->createSendgInstruction(
355 *m_opSpec, m_subfunc.send, m_predication, m_flagReg, m_execSize,
356 m_chOff, m_maskCtrl, m_sendSrc0Len, m_sendSrc1Len,
357 m_sendgIndDesc0Reg, m_sendgIndDesc1Reg, m_sendgDesc);
358 } else {
359 inst = m_kernel->createSendInstruction(
360 *m_opSpec, m_subfunc.send, m_predication, m_flagReg, m_execSize,
361 m_chOff, m_maskCtrl, m_exImmOffDesc, m_exDesc, m_desc);
362 }
363 if (m_sendSrc0Len >= 0)
364 inst->setSrc0Length(m_sendSrc0Len);
365 if (m_sendSrc1Len >= 0)
366 inst->setSrc1Length(m_sendSrc1Len);
367 } else if (m_opSpec->op == Op::NOP) {
368 inst = m_kernel->createNopInstruction();
369 } else if (m_opSpec->op == Op::ILLEGAL) {
370 if (m_isInlineBinaryInst)
371 inst = m_kernel->createInlineBinaryInstruction(m_inlineInstBinary);
372 else
373 inst = m_kernel->createIllegalInstruction();
374 } else if (m_opSpec->op == Op::THRYLD) {
375 inst = m_kernel->createThryldInstruction();
376 } else {
377 inst = m_kernel->createBasicInstruction(
378 *m_opSpec, m_predication, m_flagReg, m_execSize, m_chOff, m_maskCtrl,
379 m_flagModifier, m_subfunc);
380 }
381
382 inst->setLoc(m_loc);
383 m_insts.emplace_back(inst);
384
385 if (m_opSpec->supportsDestination()) {
386 IGA_ASSERT(m_dst.kind != Operand::Kind::INVALID, "destination never set");
387 if (m_dst.kind == Operand::Kind::DIRECT) {
388 inst->setDirectDestination(m_dstModifier, m_dst.regOpName,
389 m_dst.regOpReg, m_dst.regOpRgn.getHz(),
390 m_dst.type);
391 } else if (m_dst.kind == Operand::Kind::MACRO) {
392 inst->setMacroDestination(m_dstModifier, m_dst.regOpName,
393 m_dst.regOpReg, m_dst.regOpMathMacroExtReg,
394 m_dst.regOpRgn.getHz(), m_dst.type);
395 } else { // Operand::Kind::INDIRECT
396 inst->setIndirectDestination(m_dstModifier, m_dst.regOpReg,
397 m_dst.regOpIndOff, m_dst.regOpRgn.getHz(),
398 m_dst.type);
399 }
400 } // end setting destinations
401
402 // set source operands
403 for (int i = 0; i < m_nSrcs; i++) {
404 const OperandInfo &src = m_srcs[i];
405 IGA_ASSERT(src.kind != Operand::Kind::INVALID, "source never set");
406
407 SourceIndex opIx = (SourceIndex)((int)SourceIndex::SRC0 + i);
408 if (src.kind == Operand::Kind::DIRECT) {
409 inst->setDirectSource(opIx, src.regOpSrcMod, src.regOpName,
410 src.regOpReg, src.regOpRgn, src.type);
411 } else if (src.kind == Operand::Kind::MACRO) {
412 inst->setMacroSource(opIx, src.regOpSrcMod, src.regOpName, src.regOpReg,
413 src.regOpMathMacroExtReg, src.regOpRgn, src.type);
414 } else if (src.kind == Operand::Kind::INDIRECT) {
415 inst->setIndirectSource(opIx, src.regOpSrcMod, src.regOpName,
416 src.regOpReg, src.regOpIndOff, src.regOpRgn,
417 src.type);
418 } else if (src.kind == Operand::Kind::LABEL) {
419 if (src.immLabel.empty()) {
420 // numeric label was used
421 inst->setLabelSource(opIx, src.immValue.s32, src.type);
422 } else {
423 // label (unresolved)
424 //
425 // we'll backpatch later, but set it for the type
426 inst->setLabelSource(opIx, 0, src.type);
427 UnresolvedLabel u{src.loc, src.immLabel, inst->getSource(opIx),
428 *inst};
429 m_unresolvedLabels.push_back(std::move(u));
430 }
431 } else if (src.kind == Operand::Kind::IMMEDIATE) {
432 inst->setImmediateSource(opIx, src.immValue, src.type);
433 } else {
434 IGA_ASSERT_FALSE("unexpected src kind");
435 }
436 } // for: sources
437
438 inst->addInstOpts(m_instOpts);
439 inst->setID(m_nextId++);
440 inst->setPC(m_pc);
441 if (!m_comment.empty()) {
442 inst->setComment(m_comment);
443 }
444
445 SWSB swInfo;
446 // this assumes checks for incompatible stuff are done during parsing
447 if (m_depInfo.sbidSrcSet()) {
448 swInfo.sbid = m_depInfo.memSBidSrc;
449 swInfo.tokenType = SWSB::TokenType::SRC;
450 }
451 if (m_depInfo.sbidDstSet()) {
452 swInfo.sbid = m_depInfo.memSBidDst;
453 swInfo.tokenType = SWSB::TokenType::DST;
454 }
455 if (m_depInfo.sbidAllocSet()) {
456 swInfo.sbid = m_depInfo.memSBidAlloc;
457 swInfo.tokenType = SWSB::TokenType::SET;
458 }
459 if (m_depInfo.sbidCntrSet()) {
460 swInfo.sbid = m_depInfo.memSBidCntr;
461 swInfo.tokenType = SWSB::TokenType::INC;
462 }
463 if (m_depInfo.regDist > 0) {
464 swInfo.minDist = m_depInfo.regDist;
465 swInfo.distType = m_depInfo.distType;
466 }
467 swInfo.spToken = m_depInfo.spToken;
468
469 SWSB::InstType instType = inst->getSWSBInstType(m_swsbEncodeMode);
470 if (!swInfo.verify(m_swsbEncodeMode, instType)) {
471 m_errorHandler.reportError(inst->getLoc(), "invalid SWSB");
472 }
473
474 inst->setSWSB(swInfo);
475
476 m_pc += inst->hasInstOpt(InstOpt::COMPACTED) ? 8 : 16;
477
478 // after any branching instruction or EOT, split the basic block
479 // Also split when there is mov with label src
480 if (inst->isBranching() || inst->hasInstOpt(InstOpt::EOT) ||
481 inst->isMovWithLabel()) {
482 BlockEnd(m_pc);
483 }
484 }
485
486 void InstPredication(const Loc &, bool inv, const RegRef &flagReg,
487 PredCtrl predCtrl) {
488 m_predication.inverse = inv;
489 m_predication.function = predCtrl;
490 m_flagReg = flagReg;
491 }
492
493 void InstOp(const OpSpec *spec) { m_opSpec = spec; }
494
495 void InstSubfunction(Subfunction sf) {
496 IGA_ASSERT(!m_subfunc.isValid(), "subfunction already set");
497 m_subfunc = sf;
498 }
499
500 void InstExecInfo(const Loc &, ExecSize execSize, const Loc &,
501 ChannelOffset execOff) {
502 m_execSize = execSize;
503 m_chOff = execOff;
504 }
505
506 void InstNoMask(const Loc &) { m_maskCtrl = MaskCtrl::NOMASK; }
507
508 // The flag modifier (condition modifier)
509 void InstFlagModifier(RegRef flagReg, FlagModifier flmodf) {
510 m_flagModifier = flmodf;
511 m_flagReg = flagReg;
512 }
513
514 /////////////////////////////////////////////
515 // destination operand callbacks
516
517 // (sat) applied to the destination operand
518 void InstDstOpSaturate() { m_dstModifier = DstModifier::SAT; }
519
520 // direct access
521 //
522 // e.g. r13.4<2>:t
523 void InstDstOpRegDirect(const Loc &loc, const RegInfo &ri, RegRef reg,
524 Region::Horz rgnHorz, Type ty) {
525 InstDstOpRegDirect(loc, ri.regName, reg, rgnHorz, ty);
526 }
527 void InstDstOpRegDirect(const Loc &loc, RegName rn, int reg,
528 Region::Horz rgnHorz, Type ty) {
529 RegRef rr;
530 rr.regNum = (uint16_t)reg;
531 InstDstOpRegDirect(loc, rn, rr, rgnHorz, ty);
532 }
533 void InstDstOpRegDirect(const Loc &loc, RegName rn, RegRef reg,
534 Region::Horz rgnHorz, Type ty) {
535 m_dst.kind = Operand::Kind::DIRECT;
536 m_dst.loc = loc;
537
538 m_dst.regOpDstMod = m_dstModifier;
539 m_dst.regOpName = rn;
540 m_dst.regOpReg = reg;
541 m_dst.regOpRgn.setDstHz(rgnHorz);
542 m_dst.type = ty;
543 }
544 // math macro register access (implicit accumulator)
545 //
546 // e.g. r13.acc4:t
547 void InstDstOpRegMathMacroExtReg(const Loc &loc, RegName rnm, int regNum,
548 MathMacroExt mme, Region::Horz rgnH,
549 Type ty) {
550 m_dst.kind = Operand::Kind::MACRO;
551 m_dst.loc = loc;
552
553 m_dst.regOpDstMod = m_dstModifier;
554 m_dst.regOpName = rnm;
555 m_dst.regOpReg = RegRef((uint16_t)regNum, (uint16_t)0);
556 m_dst.regOpReg.regNum = (uint16_t)regNum;
557 m_dst.regOpRgn.setDstHz(rgnH);
558 m_dst.regOpMathMacroExtReg = mme;
559 m_dst.type = ty;
560 }
561 void InstDstOpRegMathMacroExtReg(const Loc &loc, const RegInfo &ri,
562 int regNum, MathMacroExt mme,
563 Region::Horz rgnH, Type ty) {
564 InstDstOpRegMathMacroExtReg(loc, ri.regName, regNum, mme, rgnH, ty);
565 }
566
567 // e.g. r[a0.4,16]<2>:t
568 void InstDstOpRegIndirect(const Loc &loc, RegRef addrReg, int addrOff,
569 Region::Horz rgnHorz, Type ty) {
570 m_dst.kind = Operand::Kind::INDIRECT;
571 m_dst.loc = loc;
572
573 m_dst.regOpDstMod = m_dstModifier;
574 m_dst.regOpName = RegName::GRF_R;
575 m_dst.regOpReg = addrReg;
576 m_dst.regOpIndOff = (uint16_t)addrOff;
577 m_dst.regOpRgn.setDstHz(rgnHorz);
578 m_dst.type = ty;
579 }
580
581 // a more generic setter
582 void InstDstOp(const OperandInfo &opInfo) {
583 m_dst = opInfo;
584 validateOperandInfo(opInfo);
585 }
586
587 /////////////////////////////////////////////
588 // source operand callbacks
589
590 // Direct register source operand
591 //
592 // e.g. r13.4<2>:t
593 void InstSrcOpRegDirect(int srcOpIx, // index of the current source operand
594 const Loc &loc,
595 RegName rnm, // the type of register
596 int reg, // register/subregister
597 Region rgn, // region parameters
598 Type ty) {
599 RegRef rr((uint16_t)reg, (uint16_t)0);
600 InstSrcOpRegDirect(srcOpIx, loc, SrcModifier::NONE, rnm, rr, rgn, ty);
601 }
602 void
603 InstSrcOpRegDirect(int srcOpIx, // index of the current source operand
604 const Loc &loc,
605 SrcModifier srcMod, // source modifiers on this operand
606 RegName rnm, // the type of register
607 RegRef rr, // register/subregister
608 Region rgn, // region parameters
609 Type ty) {
610 OperandInfo src = m_srcs[srcOpIx]; // copy init values
611 src.loc = loc;
612 src.kind = Operand::Kind::DIRECT;
613 src.regOpSrcMod = srcMod;
614 src.regOpName = rnm;
615 src.regOpReg = rr;
616 src.regOpRgn = rgn;
617 src.type = ty;
618
619 InstSrcOp(srcOpIx, src);
620 }
621 // math macro register access
622 //
623 // e.g. r13.acc4:t
624 void InstSrcOpRegMathMacroExtReg(
625 int srcOpIx, // index of the current source operand
626 const Loc &loc,
627 SrcModifier srcMod, // source modifiers on this operand
628 RegName rnm, // the type of register
629 int regNum, MathMacroExt MathMacroExt, Region rgn, Type ty) {
630 OperandInfo src = m_srcs[srcOpIx]; // copy init values
631 src.loc = loc;
632 src.kind = Operand::Kind::MACRO;
633 src.regOpSrcMod = srcMod;
634 src.regOpName = rnm;
635 src.regOpReg.regNum = (uint16_t)regNum;
636 src.regOpReg.subRegNum = 0;
637 src.regOpRgn = rgn;
638 src.regOpMathMacroExtReg = MathMacroExt;
639 src.type = ty;
640
641 InstSrcOp(srcOpIx, src);
642 }
643 // parsed a source indirect operand
644 //
645 // e.g. "r[a0.4,16]<1,0>:f"
646 void InstSrcOpRegIndirect(
647 int srcOpIx, // index of the current source operand
648 const Loc &loc,
649 const SrcModifier &srcMod, // source modifiers on this operand
650 RegName regName, RegRef addrReg,
651 int addrOff, // e.g. 16 in r[a0.3,16] (0 if absent)
652 Region rgn, Type ty) {
653 OperandInfo src = m_srcs[srcOpIx]; // copy init values
654 src.loc = loc;
655 src.kind = Operand::Kind::INDIRECT;
656 src.regOpSrcMod = srcMod;
657 src.regOpName = RegName::GRF_R;
658 // specail case for handling sendg src0: r[s0.0]
659 // It's indirect register access to grf but the register file
660 // should be set to ARF_S
661 if (regName == RegName::ARF_S)
662 src.regOpName = RegName::ARF_S;
663 src.regOpReg = addrReg;
664 src.regOpIndOff = (uint16_t)addrOff;
665 src.regOpRgn = rgn;
666 src.type = ty;
667
668 InstSrcOp(srcOpIx, src);
669 }
670
671 // Called on a source immediate operand. Some immedidate operands
672 // will not have explicit types. E.g. send descriptors.
673 //
674 // e.g. 14:d
675 // 0x21424
676 void InstSrcOpImmValue(int srcOpIx, const Loc &loc, const ImmVal &val,
677 Type ty) {
678 OperandInfo src = m_srcs[srcOpIx]; // copy init values
679 src.loc = loc;
680 src.kind = Operand::Kind::IMMEDIATE;
681 src.immValue = val;
682 src.type = ty;
683
684 InstSrcOp(srcOpIx, src);
685 }
686
687 // Called when an immediate label is encountered (e.g. on branches)
688 void InstSrcOpImmLabel(int srcOpIx, const Loc &loc, const std::string &sym,
689 Type type) {
690 OperandInfo src = m_srcs[srcOpIx]; // copy init values
691 src.loc = loc;
692 src.kind = Operand::Kind::LABEL;
693 src.immLabel = sym;
694 src.type = type;
695
696 InstSrcOp(srcOpIx, src);
697 }
698
699 // almost all cases
700 void InstSrcOpImmLabelRelative(int srcOpIx, const Loc &loc, int64_t relPc,
701 Type type) {
702 // NOTE: even though jmpi is relative post-increment, remember
703 // that IGA keeps the offsets normalized as pre-increment and in
704 // bytes for uniformity (HSW had some QWord labels)
705 // (after XE jmpi is also pre-increment)
706
707 OperandInfo src = m_srcs[srcOpIx]; // copy init values
708 src.loc = loc;
709 src.kind = Operand::Kind::LABEL;
710 src.immValue = relPc;
711 src.type = type;
712
713 InstSrcOp(srcOpIx, src);
714 }
715
716 // calla directly calls this, everything else goes through relative
717 // (see above)
718 void InstSrcOpImmLabelAbsolute(
719 int srcOpIx, const Loc &loc,
720 int64_t absPc, // the actual PC relative to program start
721 Type type) {
722 OperandInfo src = m_srcs[srcOpIx]; // copy init values
723 src.loc = loc;
724 src.kind = Operand::Kind::LABEL;
725 src.immValue = absPc;
726 src.type = type;
727
728 InstSrcOp(srcOpIx, src);
729 }
730
731 // a more generic setter
732 void InstSrcOp(int srcOpIx, const OperandInfo &opInfo) {
733 m_nSrcs = m_nSrcs < srcOpIx + 1 ? srcOpIx + 1 : m_nSrcs;
734
735 validateOperandInfo(opInfo);
736 m_srcs[srcOpIx] = opInfo;
737 }
738
739 void validateOperandInfo(const OperandInfo &opInfo) {
740 #ifdef _DEBUG
741 // some sanity validation
742 switch (opInfo.kind) {
743 case Operand::Kind::DIRECT:
744 case Operand::Kind::MACRO:
745 case Operand::Kind::INDIRECT:
746 case Operand::Kind::IMMEDIATE:
747 case Operand::Kind::LABEL:
748 break;
749 default:
750 IGA_ASSERT_FALSE("OperandInfo::kind: invalid value");
751 break;
752 }
753 #endif
754 }
755
756 // send descriptors
757 // E.g. "send ... 0xC a0.0"
758 // (we translate this to: a0.0<0;1,0>:ud)
759 void InstSendDescs(const Loc &,
760 uint32_t exImmOffDesc,
761 const SendDesc &exDesc, const Loc &,
762 const SendDesc &desc) {
763 m_exImmOffDesc = exImmOffDesc;
764 m_exDesc = exDesc;
765 m_desc = desc;
766 }
767
768 void InstSendDescs(const Loc &exDescLoc, const SendDesc &exDesc,
769 const Loc &descLoc, const SendDesc &desc) {
770 InstSendDescs(exDescLoc, 0, exDesc, descLoc, desc);
771 }
772
773 void InstSendDescs(uint64_t desc, const RegRef &id0, const RegRef &id1) {
774 m_sendgDesc = desc;
775 m_sendgIndDesc0Reg = id0;
776 m_sendgIndDesc1Reg = id1;
777 }
778
779 void InstSendSrc0Length(int src0Length) { m_sendSrc0Len = src0Length; }
780 void InstSendSrc1Length(int src1Length) { m_sendSrc1Len = src1Length; }
781
782 ///////////////////////////////////////////////////////////////////////////
783 // instruction option callbacks
784 void InstOptsAdd(const InstOptSet &instOpts) { m_instOpts.add(instOpts); }
785
786 void InstOptAdd(InstOpt instOpt) { m_instOpts.add(instOpt); }
787
788 void InstDepInfoSBidSrc(Loc loc, int32_t sbid) {
789 if (sbid > (int32_t)m_model.getMaxSWSBTokenNum())
790 m_errorHandler.reportError(loc, "Invalid SWSB ID number");
791 if (m_depInfo.anyBarrierSet())
792 m_errorHandler.reportError(loc, "More than one SWSB barrier set");
793
794 m_depInfo.memSBidSrc = sbid;
795 }
796
797 void InstDepInfoSBidDst(Loc loc, int32_t sbid) {
798 if (sbid > (int32_t)m_model.getMaxSWSBTokenNum())
799 m_errorHandler.reportError(loc, "Invalid SWSB ID number");
800 if (m_depInfo.anyBarrierSet())
801 m_errorHandler.reportError(loc, "More than one SWSB barrier set");
802
803 m_depInfo.memSBidDst = sbid;
804 }
805
806 void InstDepInfoSBidAlloc(Loc loc, int32_t sbid) {
807 if (sbid > (int32_t)m_model.getMaxSWSBTokenNum())
808 m_errorHandler.reportError(loc, "Invalid SWSB ID number");
809 if (m_depInfo.anyBarrierSet())
810 m_errorHandler.reportError(loc, "More than one SWSB barrier set");
811
812 m_depInfo.memSBidAlloc = sbid;
813 }
814 void InstDepInfoSBidCntr(Loc loc, int32_t sbid) {
815 if (sbid > (int32_t)m_model.getMaxSWSBTokenNum())
816 m_errorHandler.reportError(loc, "Invalid SWSB ID number");
817 if (m_depInfo.anyBarrierSet())
818 m_errorHandler.reportError(loc, "More than one SWSB barrier set");
819
820 m_depInfo.memSBidCntr = sbid;
821 }
822
823 void InstDepInfoDist(Loc loc, SWSB::DistType type, uint32_t dist) {
824 if (dist > m_model.getSWSBMaxValidDistance())
825 m_errorHandler.reportError(loc, "Invalid SWSB distance number");
826 if (m_depInfo.distType != SWSB::DistType::NO_DIST)
827 m_errorHandler.reportError(loc, "More than one SWSB distance set");
828 m_depInfo.distType = type;
829 m_depInfo.regDist = dist;
830 }
831
832 void InstDepInfoSpecialToken(Loc loc, SWSB::SpecialToken token) {
833 m_depInfo.spToken = token;
834 }
835
836 ///////////////////////////////////////////////
837 // for decoding from binary
838 void InstSwsb(Loc loc, SWSB swsb) {
839 IGA_ASSERT(m_depInfo == SWSBInfo(), "resetting SWSB info");
840
841 switch (swsb.tokenType) {
842 case SWSB::TokenType::NOTOKEN:
843 break;
844 case SWSB::TokenType::DST:
845 m_depInfo.memSBidDst = (int32_t)swsb.sbid;
846 break;
847 case SWSB::TokenType::SRC:
848 m_depInfo.memSBidSrc = (int32_t)swsb.sbid;
849 break;
850 case SWSB::TokenType::SET:
851 case SWSB::TokenType::INC:
852 m_depInfo.memSBidAlloc = (int32_t)swsb.sbid;
853 break;
854 }
855
856 m_depInfo.distType = swsb.distType;
857 if (swsb.distType != SWSB::DistType::NO_DIST)
858 m_depInfo.regDist = swsb.minDist;
859 }
860
861 void InstDpasDstOp(const Loc &, RegName rnm, RegRef reg, Type ty) {
862 m_dst.kind = Operand::Kind::DIRECT;
863 m_dst.regOpDstMod = DstModifier::NONE;
864 m_dst.regOpName = rnm;
865 m_dst.regOpReg = reg;
866 m_dst.regOpRgn = m_opSpec->implicitDstRegion(false);
867 m_dst.type = ty;
868 }
869 void InstDpasSrcOp(int srcOpIx, const Loc &loc, RegName rnm, RegRef reg,
870 Type ty) {
871 m_nSrcs = m_nSrcs < srcOpIx + 1 ? srcOpIx + 1 : m_nSrcs;
872
873 m_srcs[srcOpIx].loc = loc;
874 m_srcs[srcOpIx].kind = Operand::Kind::DIRECT;
875 m_srcs[srcOpIx].regOpSrcMod = SrcModifier::NONE;
876 m_srcs[srcOpIx].regOpName = rnm;
877 m_srcs[srcOpIx].regOpReg = reg;
878 m_srcs[srcOpIx].regOpRgn =
879 m_opSpec->implicitSrcRegion(srcOpIx, m_execSize, false);
880 m_srcs[srcOpIx].type = ty;
881 }
882
883 void InstSdpasSrcMeta(const Loc &loc, RegName rnm, RegRef reg) {
884 m_srcs[3].loc = loc;
885 m_srcs[3].kind = Operand::Kind::DIRECT;
886 m_srcs[3].regOpSrcMod = SrcModifier::NONE;
887 m_srcs[3].regOpName = rnm;
888 m_srcs[3].regOpReg = reg;
889 m_srcs[3].regOpRgn = Region::INVALID;
890 m_srcs[3].type = Type::INVALID;
891 }
892
893 // sets Instruction::setComment(...)
894 void InstComment(const std::string& comment) { m_comment = comment; }
895
896 // set inline binary instruction
897 void InstInlineBinary(const Instruction::InlineBinaryType &binary) {
898 m_isInlineBinaryInst = true;
899 m_inlineInstBinary = binary;
900 }
901
902 }; // class ParseHandler
903
904 } // namespace iga
905
906 #endif //_IR_BUILDER_HANDLER_HPP_