Debian Codegraph @

visa/iga/IGALibrary/IR/RegSet.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 "RegSet.hpp"
10 #include "../asserts.hpp"
11 #include "Messages.hpp"
12
13 #include <sstream>
14
15 using namespace iga;
16
17 // This module implements what was the original intent for RegDeps.
18 // Unfortunately, that module was coupled certain machine state and other
19 // dependencies that we don't want for simpler things like DU analysis.
20 //
21 // FIXME: would be to
22 // (1) rename RegDeps in SWSBSetter to something like DepState
23 // (2) compose a RegSet within the DepState
24 //
25
26 static size_t BytesForRegSet(RegName rn, const Model &m) {
27 // For platforms lacking a particular register this returns a
28 // nonzero size so that m.getRegCount() doesn't assert
29 if (m.lookupRegInfoByRegName(rn) == nullptr)
30 return 1;
31 return (size_t)m.getRegCount(rn) * m.getBytesPerReg(rn);
32 }
33
34 static const RegName TRACKED[] {
35 RegName::GRF_R, RegName::ARF_A, RegName::ARF_ACC, RegName::ARF_F,
36 RegName::ARF_S,
37 };
38
39 BitSet<> *RegSet::bitSetForPtr(RegName rn) {
40 switch (rn) {
41 case RegName::GRF_R:
42 return &bitsR;
43 case RegName::ARF_A:
44 return &bitsA;
45 case RegName::ARF_ACC:
46 return &bitsAcc;
47 case RegName::ARF_F:
48 return &bitsF;
49 case RegName::ARF_S:
50 return &bitsS;
51 default:
52 break;
53 }
54 return nullptr;
55 }
56
57 BitSet<> &RegSet::bitSetFor(RegName rn) {
58 auto bs = bitSetForPtr(rn);
59 IGA_ASSERT(bs, "not a tracked bitset");
60 return *bs;
61 }
62 const BitSet<> &RegSet::bitSetFor(RegName rn) const {
63 return const_cast<RegSet *>(this)->bitSetFor(rn);
64 }
65
66 bool RegSet::isTrackedReg(RegName rn) const {
67 return const_cast<RegSet *>(this)->bitSetForPtr(rn) != nullptr;
68 }
69
70 static size_t OffsetOf(const Model &m, RegName rn, int reg) {
71 return (size_t)reg * m.getBytesPerReg(rn) * 8;
72 }
73
74 size_t RegSet::offsetOf(RegName rn, int reg) const {
75 return OffsetOf(model, rn, reg);
76 }
77 size_t RegSet::offsetOf(RegName rn, RegRef rr, size_t typeSizeBits) const {
78 return OffsetOf(model, rn, rr.regNum) + rr.subRegNum * typeSizeBits;
79 }
80 size_t RegSet::offsetOf(RegName rn, RegRef rr, Type t) const {
81 size_t typeSizeBits = TypeSizeInBitsWithDefault(t, 8);
82 return offsetOf(rn, rr, typeSizeBits);
83 }
84
85 RegSet::RegSet(const Model &m)
86 : model(m), bitsR(BytesForRegSet(RegName::GRF_R, m)),
87 bitsA(BytesForRegSet(RegName::ARF_A, m)),
88 bitsAcc(BytesForRegSet(RegName::ARF_ACC, m)),
89 bitsF(BytesForRegSet(RegName::ARF_F, m)),
90 bitsS(BytesForRegSet(RegName::ARF_S, m))
91 {
92 }
93 RegSet::RegSet(const RegSet &rs)
94 : model(rs.model), bitsR(rs.bitsR), bitsA(rs.bitsA), bitsAcc(rs.bitsAcc),
95 bitsF(rs.bitsF), bitsS(rs.bitsS)
96 {
97 }
98 RegSet &RegSet::operator=(const RegSet &rhs) {
99 IGA_ASSERT(&model == &rhs.model, "model mismatch");
100 for (RegName rn : TRACKED) {
101 bitSetFor(rn) = rhs.bitSetFor(rn);
102 }
103 return *this;
104 }
105
106 bool RegSet::operator==(const RegSet &rs) const {
107 for (RegName rn : TRACKED) {
108 if (bitSetFor(rn) != rs.bitSetFor(rn)) {
109 return false;
110 }
111 }
112 return true;
113 }
114 bool RegSet::empty() const {
115 for (RegName rn : TRACKED)
116 if (!bitSetFor(rn).empty())
117 return false;
118 return true;
119 }
120 void RegSet::reset() {
121 for (RegName rn : TRACKED)
122 bitSetFor(rn).reset();
123 }
124
125 bool RegSet::intersects(const RegSet &rhs) const {
126 for (RegName rn : TRACKED)
127 if (bitSetFor(rn).intersects(rhs.bitSetFor(rn)))
128 return true;
129 return false;
130 }
131
132 bool RegSet::destructiveUnion(const RegSet &rhs) {
133 bool changed = false;
134 for (RegName rn : TRACKED)
135 changed |= bitSetFor(rn).add(rhs.bitSetFor(rn));
136 return changed;
137 }
138
139 bool RegSet::destructiveSubtract(const RegSet &rhs) {
140 bool changed = false;
141 for (RegName rn : TRACKED)
142 changed |= bitSetFor(rn).andNot(rhs.bitSetFor(rn));
143 return changed;
144 }
145
146 bool RegSet::intersectInto(const RegSet &rhs, RegSet &into) const {
147 bool notEmpty = false;
148 for (RegName rn : TRACKED) {
149 notEmpty |=
150 bitSetFor(rn).intersectInto(rhs.bitSetFor(rn), into.bitSetFor(rn));
151 }
152 return notEmpty;
153 }
154
155 bool RegSet::addReg(RegName rn, int reg) { return addRegs(rn, reg, 1); }
156
157 bool RegSet::addRegs(RegName rn, int reg, int n) {
158 size_t bitsPerReg = 8 * (size_t)model.getBytesPerReg(rn);
159 return add(rn, bitsPerReg * reg, bitsPerReg * n);
160 }
161
162 bool RegSet::add(RegName rn, size_t regFileOffBits, size_t numBits) {
163 if (!isTrackedReg(rn)) {
164 return false;
165 }
166 // coarsen analysis to byte
167 // need std::max since numBits might be <8 (e.g. flag reg SIMD1)
168 size_t offBytes = regFileOffBits / 8;
169 size_t lenBytes = std::max<size_t>(numBits / 8, 1);
170 BitSet<> &bs = bitSetFor(rn);
171 if (offBytes + lenBytes <= BytesForRegSet(rn, model)) {
172 return bs.set(offBytes, lenBytes);
173 } else {
174 IGA_ASSERT_FALSE("access is out of bounds for register");
175 return false;
176 }
177 }
178
179 bool RegSet::add(RegName rn, RegRef rr, Type t) {
180 size_t typeSizeBits = TypeSizeInBitsWithDefault(t, 8);
181 return add(rn, offsetOf(rn, rr, t), typeSizeBits);
182 }
183
184 bool RegSet::addFlagRegArf(RegRef fr, ExecSize es, ChannelOffset co) {
185 size_t execSize = size_t(es);
186 size_t execOff = 4 * size_t(co);
187
188 // can't use subregisters here because subregisters are only a
189 // word boundary. E.g. (f1.1) op (M8|8) touches upper byte of f1.1
190 // which doesn't have a syntax representation
191 size_t off = offsetOf(RegName::ARF_F, fr, 16);
192
193 RegRef frWithOff = fr;
194 frWithOff.subRegNum += (uint16_t)execOff / 16; // 16b per subreg
195 return add(RegName::ARF_F, off + execOff, execSize);
196 }
197
198 bool RegSet::addPredicationInputs(const Instruction &i) {
199 // does it read the flag register
200 // predication does this
201 // conditional modifier on 'sel' does this
202 const Predication &pred = i.getPredication();
203 bool readsFlagRegister =
204 pred.function != PredCtrl::NONE && i.getOp() != Op::SEL;
205 if (readsFlagRegister) {
206 return addFlagAccess(i);
207 }
208 return false;
209 }
210
211 bool RegSet::addSourceInputs(const Instruction &i) {
212 bool added = false;
213
214 added |= addSourceDescriptorInputs(i);
215
216 added |= addSourceImplicit(i);
217
218 added |= addDestinationInputs(i); // e.g. indirect access uses a0
219
220 // check all the source operands
221 for (unsigned srcIx = 0; srcIx < i.getSourceCount(); srcIx++) {
222 added |= addSourceOperandInput(i, srcIx);
223 }
224 return added;
225 }
226
227 bool RegSet::addSendOperand(const Instruction &i, int opIx) {
228 IGA_ASSERT(i.getOpSpec().isAnySendFormat(), "Expected a send format");
229
230 bool changed = false;
231 const Operand &op = opIx < 0 ? i.getDestination() : i.getSource(opIx);
232 if (op.getDirRegName() != RegName::GRF_R) {
233 return false;
234 }
235
236 // new approach
237 if (i.getOpSpec().isSendgFormat()) {
238 const DecodeResult di = tryDecode(i, nullptr);
239 if (di) {
240 int bitsAccessed = -1;
241 if (opIx < 0) {
242 bitsAccessed = di.info.dstLenBytes;
243 } else if (opIx == 0) {
244 bitsAccessed = di.info.src0LenBytes;
245 } else {
246 bitsAccessed = di.info.src1LenBytes;
247 }
248 if (bitsAccessed >= 0) {
249 auto regOff = offsetOf(RegName::GRF_R, op.getDirRegRef().regNum);
250 changed |= add(RegName::GRF_R, regOff, 8 * bitsAccessed);
251 return changed;
252 }
253 }
254 }
255
256 // add special handling for send messages
257 // e.g.
258 // send (4) r10:1 ... untyped 32b read of X
259 // reads only 4 dwords
260 // send (1) r10:1 OWORD block read
261 // reads 128b ...
262 // load.ugm.d32x8t.a32.ca.ca (1|M0) ... 8 DW in a single register
263 // load.ugm.d32x4.a64 (4|M0) ... // four successive registers with 1 DW
264 bool decodeFineGrained =
265 !i.getOpSpec().isSendgFormat() || i.getMsgDescriptor().isImm();
266 if (op.getDirRegName() == RegName::GRF_R && decodeFineGrained) {
267 const DecodeResult di = tryDecode(i, nullptr);
268 if (di && (di.info.isLoad() || di.info.isStore() || di.info.isAtomic())) {
269 // the compiler won't necesssarily generate a correct exec size
270 // when it's ignored (part of the descriptor)
271 auto regOff = offsetOf(RegName::GRF_R, op.getDirRegRef().regNum);
272 if (opIx == 0) {
273 size_t bitsAccessed =
274 (size_t)(di.info.execWidth * di.info.addrSizeBits);
275 changed |= add(RegName::GRF_R, regOff, bitsAccessed);
276 } else {
277 // data type
278 if (di.info.isTransposed()) {
279 // transpose packs into GRF
280 size_t bitsAccessed =
281 (size_t)(di.info.elemsPerAddr * di.info.execWidth *
282 di.info.elemSizeBitsRegFile);
283 changed |= add(RegName::GRF_R, regOff, bitsAccessed);
284 } else {
285 // non-transpose places elements in successive registers
286 size_t bitsAccessed =
287 (size_t)(di.info.execWidth * di.info.elemSizeBitsRegFile);
288 int elemsPerAddr = di.info.elemsPerAddr;
289 if (opIx == 1 && (di.info.op == SendOp::ATOMIC_ICAS ||
290 di.info.op == SendOp::ATOMIC_FCAS)) {
291 // ICAS packs src2 in to src1's payload
292 elemsPerAddr *= 2;
293 }
294 for (int ei = 0; ei < elemsPerAddr; ei++) {
295 // each vector element starts at a new GRF
296 // vector elements might be <GRF (small SIMD) or
297 // multiple GRF (e.g. SIMD32 D64V2)
298 changed |= add(RegName::GRF_R, regOff, bitsAccessed);
299 regOff += ALIGN_UP_TO(8 * model.getGRFByteSize(), bitsAccessed);
300 }
301 }
302 }
303 return changed; // bail out early
304 }
305 }
306 // else: fallthrough on the full register counts
307
308 // send source GRF (not null reg)
309 int nregs = 0;
310 if (opIx < 0) {
311 nregs = i.getDstLength();
312 if (nregs < 0)
313 nregs = 8; // assume the worst
314 } else if (opIx == 0) { // mlen
315 nregs = i.getSrc0Length();
316 if (nregs < 0)
317 nregs = 4; // assume the worst
318 } else { // xlen
319 nregs = i.getSrc1Length();
320 if (nregs < 0)
321 nregs = 8; // assume the worst
322 }
323 int regNum = (int)op.getDirRegRef().regNum;
324 changed |= addRegs(RegName::GRF_R, regNum, nregs);
325 return changed;
326 }
327
328 // c.f. DepSet::getDPASSrcDepUpBound
329 bool RegSet::addDpasOperand(const Instruction &i, int opIx) {
330 const Operand &op = opIx < 0 ? i.getDestination() : i.getSource(opIx);
331 if (op.getDirRegName() != RegName::GRF_R) {
332 return false; // e.g. null
333 }
334
335 // The operand sizes of the multiplicands dictate how many ops per
336 // channel we process.
337 size_t mulSize =
338 std::max(TypeSizeInBitsWithDefault(i.getSource(1).getType(), 8),
339 TypeSizeInBitsWithDefault(i.getSource(2).getType(), 8));
340 size_t opsPerChannel = 32 / mulSize; // e.g. hf is 2 ...
341 size_t execSize = size_t(i.getExecSize());
342 size_t R = GetDpasRepeatCount(i.getDpasFc());
343 size_t S = GetDpasSystolicDepth(i.getDpasFc());
344 size_t typeSizeBits = TypeSizeInBitsWithDefault(op.getType(), 32);
345
346 size_t startOff = offsetOf(RegName::GRF_R, op.getDirRegRef(), op.getType());
347 bool changed = false;
348 if (opIx <= 0) { // dst/src0
349 size_t bitsAccessed = execSize * typeSizeBits * R;
350 changed |= add(RegName::GRF_R, startOff, bitsAccessed);
351 } else if (opIx == 1) { // src1
352 size_t bitsAccessed = execSize * typeSizeBits * opsPerChannel * S;
353 changed |= add(RegName::GRF_R, startOff, bitsAccessed);
354 } else if (opIx == 2) { // src2
355 // this is a little subtle
356 // given bit precisions a src2 operand can stride across the
357 // bottom or top of a GRF without touching the other half
358 //
359 // e.g. dpas.4x2 (8) r24.0:d r24.0:d r6.0:u2 r14.32:u4
360 // src2 - touches r14[16-31] and r15[16-31]
361 //
362 // TODO: can something like this happen for src1
363 for (size_t r = 0; r < R; r++) {
364 size_t startSubReg = r * opsPerChannel * typeSizeBits * 8;
365 size_t repReads = S * opsPerChannel * typeSizeBits;
366 changed |= add(RegName::GRF_R, startOff + startSubReg, repReads);
367 }
368 }
369 else {
370 // Currently only bdpas has src3/src4, and only byte type is allowed on
371 // src3/src4.
372 IGA_ASSERT(i.getOp() == Op::BDPAS, "invalid src idx");
373 IGA_ASSERT(typeSizeBits == 8, "invalid src type size");
374 auto isFP4 = [](Type ty) {
375 return ty == Type::E2M1;
376 };
377 uint16_t passes =
378 (isFP4(i.getSource(1).getType()) || isFP4(i.getSource(2).getType())) ?
379 2 : 1;
380 for (uint16_t pass = 0; pass < passes; pass++) {
381 auto tmpRR = op.getDirRegRef();
382 tmpRR.subRegNum += 32 * pass;
383 if (opIx == 3) {
384 // src3 accesses number of execSize bytes in each pass
385 changed |= setSrcRegion(op.getDirRegName(), tmpRR,
386 Region::SRC110, execSize, typeSizeBits);
387 } else if (opIx == 4) {
388 // src4 accesses number of SDepth bytes in each pass
389 changed |= setSrcRegion(op.getDirRegName(), tmpRR,
390 Region::SRC110, S, typeSizeBits);
391 }
392 }
393 }
394 return changed;
395 }
396
397 bool RegSet::addSourceOperandInput(const Instruction &i, int srcIx) {
398 size_t execSize = size_t(i.getExecSize());
399 bool added = false;
400 const Operand &op = i.getSource(srcIx);
401 auto typeSizeBits = TypeSizeInBitsWithDefault(op.getType(), 32);
402
403 // possibly need to fixup the region
404 Region rgn = op.getRegion();
405 if (i.getSourceCount() == 3) {
406 if (i.isMacro()) {
407 // macros and sends are packed access only
408 // madm (4) r3.acc1 r7.acc2 r9.acc4 r6.noacc
409 rgn = Region::SRC110;
410 } else {
411 // ternary align 1 has some implicit regions that need filling in
412 // "GEN10 Regioning Rules for Align1 Ternary Operations"
413 // 1. Width is 1 when Vertical and Horizontal Strides are both zero
414 // (broadcast access).
415 // 2. Width is equal to Vertical Stride when Horizontal Stride is zero.
416 // 3. Width is equal to Vertical Stride/Horizontal Stride when both
417 // Strides are non-zero.
418 // 4. Vertical Stride must not be zero if Horizontal Stride is non-zero.
419 // This implies Vertical Stride is always greater than Horizontal
420 // Stride.
421 // 5. For Source 2, if Horizontal Stride is non-zero, then Width is the
422 // a register's width of elements (e.g. 8 for a 32-bit data type).
423 // Otherwise, if Horizontal Stride is 0, then so is the Vertical (and
424 // rule 1 applies). This means Vertical Stride is always 'Width' *
425 // 'Horizontal Stride'.
426 if (srcIx < 2) {
427 // <V;H>
428 if (rgn.getVt() == Region::Vert::VT_0 &&
429 rgn.getHz() == Region::Horz::HZ_0) {
430 rgn.set(Region::Width::WI_1); // by #1
431 } else if (rgn.getHz() == Region::Horz::HZ_0) {
432 rgn.w = rgn.v; // by #2
433 } else if (rgn.getVt() != Region::Vert::VT_0 &&
434 rgn.getHz() != Region::Horz::HZ_0) {
435 rgn.w = rgn.v / rgn.h; // by #3
436 } else {
437 // error condition #4, just use vertical stride
438 // SPECIFY: should this be an assertion?
439 rgn.w = rgn.v;
440 }
441 } else { // (srcIx == 2)
442 // <H>
443 // <H> -> <H;1,0>
444 // if (rgn.h == Region::HZ_0) {
445 // rgn.w = Region::WI_1; // by #5 and #1
446 //} else {
447 // rgn.w =
448 // static_cast<Region::Width>(GRF_BYTES_PER_REG/typeSize); //
449 // by #5
450 //}
451 // rgn.v = static_cast<Region::Vert>(
452 // static_cast<int>(rgn.w)*static_cast<int>(rgn.h)/typeSize);
453 // // by #5
454 rgn.set(Region::Vert(size_t(rgn.h)), Region::Width::WI_1,
455 Region::Horz::HZ_0);
456 }
457 }
458 } else if (i.getSourceCount() == 2 && i.isMacro()) {
459 // math macro
460 rgn = Region::SRC110;
461 } // not ternary, not macro ==> regions are good
462
463 switch (op.getKind()) {
464 case Operand::Kind::DIRECT:
465 added = true;
466 if (i.getOpSpec().isAnySendFormat()) {
467 if (op.getDirRegName() == RegName::GRF_R) {
468 addSendOperand(i, srcIx);
469 }
470 } else if (i.getOpSpec().isDpasFormat()) {
471 added |= addDpasOperand(i, srcIx);
472 } else {
473 setSrcRegion(op.getDirRegName(), op.getDirRegRef(), rgn, execSize,
474 typeSizeBits);
475 }
476 break;
477 case Operand::Kind::MACRO:
478 added = true;
479 setSrcRegion(op.getDirRegName(), op.getDirRegRef(), rgn, execSize,
480 typeSizeBits);
481 // Don't bother tracking MME registers, technically they are 2 bits
482 // of exponent each, but they are mixed in with other ACC creating a
483 // royal mess.
484 //
485 // auto mme = op.getMathMacroExt();
486 // if (mme != MathMacroExt::NOMME && mme != MathMacroExt::INVALID) {
487 // int mmeRegNum = int(op.getMathMacroExt()) -
488 // int(MathMacroExt::MME0);
489 // rs.setSrcRegion(
490 // RegName::ARF_ACC,
491 // RegRef(mmeRegNum, 0),
492 // Region::SRC110,
493 // execSize,
494 // typeSizeBits);
495 // }
496 break;
497 case Operand::Kind::INDIRECT: {
498 if (i.isGatherSend()) {
499 // reads one byte per GRF from Src0.Length (rlen in old send)
500 int src0Len = i.getSrc0Length();
501 if (src0Len > 0) {
502 RegRef rr = i.getSource(0).getIndAddrReg();
503 for (int i = 0; i < src0Len; i++) {
504 added |= add(RegName::ARF_S, rr, Type::UB);
505 rr.subRegNum++;
506 }
507 }
508 break;
509 }
510 added = true;
511 auto rgn = op.getRegion();
512 if (rgn.getVt() == Region::Vert::VT_VxH) {
513 // VxH or Vx1 mode
514 // op (K) dst r[a0.0]<W,1>:w (reads K/W elements)
515 // op (K) dst r[a0.0]<1,0>:w (reads K elements)
516 setSrcRegion(RegName::ARF_A, op.getIndAddrReg(), Region::SRC110,
517 execSize / rgn.w, //
518 16); // :w is 16-bits a piece
519 } else {
520 // uniform: consumes one value in a0
521 // op (..) dst r[a0.0]<16,8,1>:w
522 setSrcRegion(RegName::ARF_A, op.getIndAddrReg(), Region::SRC110,
523 1, // 1 element only
524 16); // :w is 16-bits a piece
525 }
526 // we can't do anything else for this
527 break;
528 }
529 default:
530 break;
531 }
532 return added;
533 }
534
535 static bool addSendDescriptors(const Instruction &i, RegSet &rs) {
536 bool added = false;
537 // send register descriptors may touch a0.#
538 auto desc = i.getMsgDescriptor();
539 if (desc.isReg()) {
540 // send is strange: a0.# is in word offsets but reads 32b
541 // fake it by converting a0.4:uw to a0.2:ud and writing the full 32b
542 desc.reg.subRegNum /= 2;
543 added |= rs.add(RegName::ARF_A, desc.reg, Type::UD);
544 }
545 auto exDesc = i.getExtMsgDescriptor();
546 if (exDesc.isReg()) {
547 // see above
548 exDesc.reg.subRegNum /= 2;
549 added |= rs.add(RegName::ARF_A, exDesc.reg, Type::UD);
550 }
551 return added;
552 }
553
554 bool RegSet::addSourceDescriptorInputs(const Instruction &i) {
555 bool added = false;
556
557 if (i.getOpSpec().isAnySendFormat()) {
558 if (i.getOpSpec().isSendgFormat()) {
559 RegRef id0 = i.getSendgIndDesc0Reg();
560 if (id0 != REGREF_INVALID) {
561 id0.subRegNum /= 8; // normalize to QW
562 added |= add(RegName::ARF_S, id0, Type::UQ);
563 }
564 RegRef id1 = i.getSendgIndDesc1Reg();
565 if (id1 != REGREF_INVALID) {
566 id1.subRegNum /= 8; // normalize to QW
567 added |= add(RegName::ARF_S, id1, Type::UQ);
568 }
569 } else {
570 addSendDescriptors(i, *this);
571 }
572 }
573
574 return added;
575 }
576
577 bool RegSet::addSourceImplicit(const Instruction &i) {
578 bool changed = false;
579 changed |= addImplicitAccumulatorAccess(i, false);
580 if (i.getOp() == Op::SEL && i.hasPredication()) {
581 changed |= addFlagAccess(i);
582 }
583 return changed;
584 }
585
586 bool RegSet::addDestinationInputs(const Instruction &i) {
587 if (!i.getOpSpec().supportsDestination()) {
588 return false;
589 }
590 bool added = false;
591 const auto &dst = i.getDestination();
592 if (dst.getKind() == Operand::Kind::INDIRECT) {
593 added |= setDstRegion(RegName::ARF_A, dst.getIndAddrReg(), Region::DST1,
594 1, // one element only
595 16); // :w is 16-bits
596 }
597 return added;
598 }
599
600 bool RegSet::addExplicitDestinationOutputs(const Instruction &i) {
601 if (!i.getOpSpec().supportsDestination()) {
602 return false;
603 }
604 bool added = false;
605
606 int execSize = int(i.getExecSize());
607 const auto &op = i.getDestination();
608 auto typeSizeBits = TypeSizeInBitsWithDefault(op.getType(), 32);
609
610 switch (op.getKind()) {
611 case Operand::Kind::DIRECT:
612 // includes send target (a GRF, not null reg)
613 if (i.getOpSpec().isAnySendFormat() &&
614 op.getDirRegName() == RegName::GRF_R) {
615 addSendOperand(i, -1);
616 } else if (i.getOpSpec().isDpasFormat()) {
617 // destination is mirror of src0
618 added |= addDpasOperand(i, -1);
619 } else if (op.getDirRegName() == RegName::ARF_ACC && i.is(Op::MUL) &&
620 (op.getType() == Type::D || op.getType() == Type::UD)) {
621 // special case:
622 // mul (..) acc0:d ... // writes acc0[63:0]
623 // mach (..) .... // reads acc0[63:0]
624 // technically I think mul only writes the bottom 48b
625 added |= setDstRegion(op.getDirRegName(), op.getDirRegRef(),
626 op.getRegion(), execSize, 64);
627 } else {
628 // normal GRF target
629 added |= setDstRegion(op.getDirRegName(), op.getDirRegRef(),
630 op.getRegion(), execSize, typeSizeBits);
631 }
632 if (i.is(Op::MULLH)) {
633 // mullh is an odd duck; it writes out low/high pairs in
634 // - a cheap trick is to just increment the GRF and add the new region
635 // for MULLH
636 // - we increment the GRF twice if it's SIMD32 since the low half will
637 // chew up two GRFs instead of one.
638 //
639 //
640 RegRef rr = op.getDirRegRef();
641 rr.regNum++;
642 if (execSize > 16)
643 rr.regNum++;
644 added |= setDstRegion(op.getDirRegName(), rr, op.getRegion(), execSize,
645 typeSizeBits);
646 }
647 break;
648 case Operand::Kind::MACRO: {
649 // math macro
650 // GRF + ACC
651 added |= setDstRegion(op.getDirRegName(), op.getDirRegRef(), Region::DST1,
652 execSize, typeSizeBits);
653 // don't track MME registers (see RegSet::addSourceInputs for why)
654 // auto MathMacroReg = op.getMathMacroExt();
655 // if (MathMacroReg != MathMacroExt::NOMME && MathMacroReg !=
656 // MathMacroExt::INVALID) {
657 // // and the math macro register
658 // int mmeRegNum = int(MathMacroReg) - int(MathMacroExt::MME0);
659 // RegRef mmeRegRef(mmeRegNum, 0);
660 // added |= rs.setDstRegion(
661 // RegName::ARF_MME,
662 // mmeRegRef,
663 // Region::DST1,
664 // execSize,
665 // typeSizeBits);
666 // }
667 break;
668 }
669 case Operand::Kind::INDIRECT:
670 // this is an input! not an output
671 // (use addDestinationInputs or part of addSourceInputs)
672 //
673 // indirect destinations use a single a0 value
674 // (no fancy scattering)
675 // added |= setDstRegion(
676 // RegName::ARF_A,
677 // op.getIndAddrReg(),
678 // Region::DST1,
679 // 1, // one element only
680 // 16); // :w is 16-bits
681 break;
682 default:
683 break;
684 }
685 return added;
686 }
687
688 bool RegSet::addDestinationOutputs(const Instruction &i) {
689 bool changed = false;
690 changed |= addDestinationImplicit(i);
691 changed |= addExplicitDestinationOutputs(i);
692 return changed;
693 }
694
695 bool RegSet::addFlagModifierOutputs(const Instruction &i) {
696 const FlagModifier fm = i.getFlagModifier();
697 bool writesFlagRegister = // sel uses flag modifier as input
698 fm != FlagModifier::NONE && i.getOp() != Op::SEL;
699 if (writesFlagRegister) {
700 return addFlagAccess(i);
701 }
702 return false;
703 }
704
705 bool RegSet::addDestinationImplicit(const Instruction &i) {
706 // other special instructions that write things
707 return addImplicitAccumulatorAccess(i, true);
708 }
709
710 bool RegSet::addImplicitAccumulatorAccess(const Instruction &i, bool isDst) {
711 bool added = false;
712
713 bool accessesAcc = false;
714 accessesAcc |= i.is(Op::MACH); // mac and mach uses both input and output
715 accessesAcc |= isDst && i.hasInstOpt(InstOpt::ACCWREN);
716 accessesAcc |= (isDst && i.is(Op::ADDC)) || i.is(Op::SUBB);
717 accessesAcc |= !isDst && i.is(Op::MAC);
718 accessesAcc |= i.is(Op::MACL);
719 if (accessesAcc) {
720 Type type = i.getDestination().getType();
721 if (i.is(Op::MACH)) {
722 type = Type::Q; // acc0[63:0] is written
723 }
724 if (i.is(Op::MACL)) {
725 type = Type::Q; // acc0[63:0] is written according to BXML
726 }
727 auto typeSizeBytes = TypeSizeInBitsWithDefault(type, 32);
728 // unlike flag modifier and predicates the channel offset does not
729 // impact accumulator
730 added |= setDstRegion(RegName::ARF_ACC, RegRef(0, 0), // acc0
731 Region::DST1, size_t(i.getExecSize()), typeSizeBytes);
732 }
733 return added;
734 }
735
736 bool RegSet::addFlagAccess(const Instruction &i) {
737 const RegRef &fr = i.getFlagReg();
738 return addFlagRegArf(fr, i.getExecSize(), i.getChannelOffset());
739 }
740
741 bool RegSet::setDstRegion(RegName rn, RegRef rr, Region rgn, size_t execSize,
742 size_t typeSizeBits) {
743 if (!isTrackedReg(rn))
744 return false;
745
746 size_t hz = size_t(rgn.getHz());
747 // sets a region for a basic operand
748 if (rgn.getHz() == Region::Horz::HZ_INVALID) {
749 hz = 1;
750 }
751
752 bool added = false;
753 size_t baseOffBits =
754 rr.regNum * model.getBytesPerReg(rn) * 8 + rr.subRegNum * typeSizeBits;
755 for (size_t ch = 0; ch < execSize; ch++) {
756 size_t offsetBits = ch * hz * typeSizeBits;
757 added |= add(rn, baseOffBits + offsetBits, typeSizeBits);
758 }
759 return added;
760 }
761
762 // sets a region for a basic operand
763 bool RegSet::setSrcRegion(RegName rn, RegRef rr, Region rgn, size_t execSize,
764 size_t typeSizeBits) {
765 if (!isTrackedReg(rn))
766 return false;
767
768 bool changed = false;
769 size_t v = 1, w = 1, h = 0; // e.g. old-style default regions
770 if (rgn != Region::INVALID) {
771 v = size_t(rgn.getVt());
772 w = size_t(rgn.getWi());
773 w = w == 0 ? 1 : w;
774 h = size_t(rgn.getHz());
775 }
776
777 size_t rowBaseBits = offsetOf(rn, rr, typeSizeBits);
778 size_t rows = execSize / w;
779 rows = (rows != 0) ? rows : 1;
780 for (size_t y = 0; y < rows; y++) {
781 size_t offsetBits = rowBaseBits;
782 for (size_t x = 0; x < w; x++) {
783 changed |= add(rn, offsetBits, typeSizeBits);
784 offsetBits += h * typeSizeBits;
785 }
786 rowBaseBits += v * typeSizeBits;
787 }
788 return changed;
789 }
790
791 static void strForReg(const Model &model, RegName rn, const RegSet::Bits &bs,
792 bool &first, std::ostream &os) {
793 const RegInfo *ri = model.lookupRegInfoByRegName(rn);
794 if (!ri) // for cases where a platform lacks a register
795 return;
796 const size_t bytesPerReg = model.getBytesPerReg(rn);
797 auto emitReg = [&](size_t i) {
798 if (first)
799 first = false;
800 else
801 os << ",";
802 os << ri->syntax;
803 if (ri->hasRegNum()) {
804 os << (int)i;
805 }
806 };
807
808 for (size_t ri = 0; ri < model.getRegCount(rn); ri++) {
809 size_t regOff = OffsetOf(model, rn, (int)ri) / 8;
810 if (bs.testAll(regOff, bytesPerReg)) {
811 // full reg: e.g. "r13" or "r13:8"
812 emitReg(ri);
813 size_t len = 1;
814 if (rn == RegName::GRF_R || rn == RegName::ARF_ACC) {
815 // for GRF and ACC permit payload-form syntax
816 // r10:8, acc0:2
817 while (ri + len < model.getRegCount(rn) &&
818 bs.testAll(OffsetOf(model, rn, (int)ri + (int)len) / 8,
819 bytesPerReg)) {
820 len++;
821 }
822 }
823 if (len > 1) {
824 os << ":" << len;
825 ri += len - 1;
826 }
827 } else if (bs.testAny(regOff, bytesPerReg)) {
828 // partial register
829 // r13[7] (e.g. r13.7<0;1,0>:b)
830 // r13[0-3,8-11,..] (e.g. r13.0<2;1,0>:d)
831 emitReg(ri);
832 os << '[';
833 bool firstSubreg = true;
834 for (size_t byte = 0; byte < bytesPerReg; byte++) {
835 if (!bs.test(regOff + byte)) {
836 continue;
837 }
838 if (firstSubreg) {
839 firstSubreg = false;
840 } else {
841 os << ',';
842 }
843 os << byte;
844 // find the end
845 size_t len = 1;
846 while (byte + len < bytesPerReg && bs.test(regOff + byte + len))
847 len++;
848 if (len > 1) {
849 os << '-' << byte + len - 1;
850 byte += len - 1;
851 }
852 }
853 os << ']';
854 }
855 } // for
856 }
857
858 std::string RegSet::str(const Model &m, RegName rn, const RegSet::Bits &bs) {
859 bool first = true;
860 std::stringstream ss;
861 strForReg(m, rn, bs, first, ss);
862 return ss.str();
863 }
864
865 // emits a shorter description... something like
866 // {r0,r1[0-3],r2[16],r4[0-3,8-11,16-19,24-27]}
867 void RegSet::str(std::ostream &os) const {
868 bool first = true;
869 os << "{";
870 for (RegName rn : TRACKED) {
871 const BitSet<> &bs = bitSetFor(rn);
872 strForReg(model, rn, bs, first, os);
873 }
874 os << "}";
875 }
876 std::string RegSet::str() const {
877 std::stringstream ss;
878 str(ss);
879 return ss.str();
880 }
881
882 RegSet::Bits RegSet::emptyPredSet(const Model &model) {
883 return RegSet::Bits(BytesForRegSet(RegName::ARF_F, model));
884 }