Debian Codegraph @

visa/iga/IGALibrary/IR/Checker/IRChecker.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 "IRChecker.hpp"
10 #include "../../api/iga.h"
11
12 #include <sstream>
13
14 using namespace iga;
15
16 struct Checker {
17 ErrorHandler *m_errHandler;
18 const Model &m_model;
19
20 Checker(const Model &model, ErrorHandler *err)
21 : m_errHandler(err), m_model(model) {}
22
23 void warning(const Loc &loc, const char *msg) {
24 m_errHandler->reportWarning(loc, msg);
25 }
26 void error(const Loc &loc, const char *msg) {
27 m_errHandler->reportError(loc, msg);
28 }
29 };
30
31 struct LOCChecker : Checker {
32 Loc m_loc;
33
34 LOCChecker(const Model &model, Loc loc, ErrorHandler *err)
35 : Checker(model, err), m_loc(loc) {}
36
37 void warning(const char *msg) { m_errHandler->reportWarning(m_loc, msg); }
38 void error(const char *msg) { m_errHandler->reportError(m_loc, msg); }
39 };
40
41 struct SemanticChecker : LOCChecker {
42 const Instruction *m_inst;
43 uint32_t m_enabled_warnings;
44
45 SemanticChecker(const Model &model, ErrorHandler *err,
46 uint32_t enabled_warnings)
47 : LOCChecker(model, Loc::INVALID, err), m_inst(nullptr),
48 m_enabled_warnings(enabled_warnings) {}
49
50 void checkKernel(const Kernel &k) {
51 for (auto b : k.getBlockList()) {
52 for (auto i : b->getInstList()) {
53 checkInstruction(*i);
54 }
55 }
56 }
57
58 void checkInstruction(const Instruction &i) {
59 m_inst = &i;
60 m_loc = i.getLoc();
61 checkInstImpl(i);
62 m_inst = nullptr;
63 m_loc = Loc::INVALID;
64 }
65
66 void checkInstImpl(const Instruction &i) {
67 if (i.getOpSpec().supportsDestination()) {
68 checkDst(i, i.getDestination());
69 }
70 int srcs = i.getSourceCount();
71 checkExecSize(i);
72 if (srcs > 0) {
73 checkSrc(i, 0);
74 }
75 if (srcs > 1) {
76 checkSrc(i, 1);
77 }
78 if (srcs > 2) {
79 checkSrc(i, 2);
80 }
81 }
82
83 void checkExecSize(const Instruction &i) {
84 bool execSizeTooLarge = i.getExecSize() > ExecSize::SIMD16;
85 if (m_model.platform >= Platform::XE3P_XPC) {
86 execSizeTooLarge = false;
87 }
88 if (i.is(Op::MULLH) && execSizeTooLarge) {
89 warning("mullh exec size must be less than or 16");
90 }
91 }
92
93 // Scalar register on dst restrictions:
94 // - Opcode must be mov
95 // - Source and destination datatypes must be the same and must be integers
96 // with sizes 16, 32 or 64 bits
97 // - When with immediate source, Execution size must be 1
98 void checkDstScalarReg(const Instruction &i, const Operand &op) {
99 if (op.getDirRegName() != RegName::ARF_S)
100 return;
101
102 if (i.getOp() != Op::MOV)
103 error("Opcode must be mov for scalar dst instructions");
104 if (op.getType() != i.getSource(0).getType() ||
105 TypeIsFloating(op.getType()) ||
106 (TypeSizeInBits(op.getType()) != 16 &&
107 TypeSizeInBits(op.getType()) != 32 &&
108 TypeSizeInBits(op.getType()) != 64))
109 error("Invalid type for scalar dst instructions");
110 if (i.getSource(0).isImm() && i.getExecSize() != ExecSize::SIMD1)
111 error("Invalid execution size for scalar dst instruction");
112 }
113
114 // Scalar register on src restrictions:
115 // - Opcode must be mov, send/send, sendg/sendgc. Cannot be sendgx
116 // - When opcode is mov the scalar (broadcast) regioning must be used
117 // - When opcode is send/sendc register-gather access must be used, and
118 // Src1 must be NULL.
119 // - Scalar register operand must be on Source 0
120 // - Destination must not be a scalar register
121 void checkSrcScalarReg(const Instruction &i, const Operand &src) {
122 if (src.isImm() || src.getDirRegName() != RegName::ARF_S)
123 return;
124
125 if (src.getKind() == Operand::Kind::DIRECT) {
126 if (i.getOp() != Op::MOV)
127 error("Invalid Opcode for direct scalar src instructions");
128 if (src.getRegion() != Region::SRC010)
129 error("Invalid region for direct scalar src instructions");
130 } else if (src.getKind() == Operand::Kind::INDIRECT) {
131 // sendgx/sendgxc cannot be gather-send
132 if ((i.getOp() == Op::SENDGX || i.getOp() == Op::SENDGXC)
133 )
134 error("Invalid Opcode for indirect scalar src instructions");
135 if (!i.getOpSpec().isAnySendFormat())
136 error("Invalid Opcode for indirect scalar src instructions");
137 else if (!i.isGatherSend() || !i.getSource(1).isNull())
138 error("Invalid send format for indirect scalar src register");
139 }
140 if (i.getDestination().getDirRegName() == RegName::ARF_S)
141 error("Dst and src cannot both be scalar");
142 }
143
144 void checkDst(const Instruction &i, const Operand &op) {
145 switch (op.getKind()) {
146 case Operand::Kind::DIRECT:
147 // if (op.getDirRegName() == RegName::ARF_CE) {
148 // warning("register is not writable (except in SIP)");
149 // }
150 if ((m_enabled_warnings & IGA_WARNINGS_SCHED) &&
151 !i.hasInstOpt(InstOpt::SWITCH) &&
152 arfNeedsSwitch(op.getDirRegName()) && m_model.supportsHwDeps()) {
153 warning("destination register ARF access requires {Switch} ThreadCtrl");
154 }
155 checkDstScalarReg(i, op);
156 break;
157 case Operand::Kind::MACRO:
158 if (!i.isMacro()) {
159 error("not a macro instruction");
160 }
161 break;
162 default:
163 break;
164 }
165
166 // conservatively check operands
167 if (m_enabled_warnings & IGA_WARNINGS_TYPES) {
168 auto ispec = i.getOpSpec();
169 switch (ispec.format) {
170 case OpSpec::BASIC_UNARY_REG:
171 case OpSpec::BASIC_UNARY_REGIMM:
172 checkOperandTypes(i);
173 break;
174 case OpSpec::BASIC_BINARY_REG_IMM:
175 case OpSpec::BASIC_BINARY_REG_REG:
176 case OpSpec::BASIC_BINARY_REG_REGIMM:
177 case OpSpec::MATH_BINARY_REG_REGIMM:
178 checkOperandTypes(i);
179 break;
180 case OpSpec::TERNARY_REGIMM_REG_REGIMM:
181 checkOperandTypes(i);
182 break;
183
184 // punt on anything harder
185 default:
186 break;
187 }
188 }
189 }
190
191 void checkOperandTypes(const Instruction &i) {
192 auto ispec = i.getOpSpec();
193 for (size_t ti = 0;
194 ti < sizeof(ispec.typeMappings) / sizeof(ispec.typeMappings[0]);
195 ti++) {
196 // type mappings are a zero-padded list of enum bitset pairs
197 // the raw bits are stored in statically allocated structures
198 // we construct them as EnumBitset<Type,uint32_t> and look
199 // for a pair that matches all the source and destination
200 // operand pairs
201 auto tm = ispec.typeMappings[ti];
202 const auto dsts = EnumBitset<Type>(tm.dsts);
203 if (tm.dsts == 0) { // end of array (null dst)
204 break;
205 }
206 if (!dsts.contains(i.getDestination().getType())) {
207 // destination type mismatch, try the next destination
208 continue;
209 }
210
211 const auto srcs = EnumBitset<Type>(tm.srcs);
212 for (size_t s_ix = 0; s_ix < i.getSourceCount(); s_ix++) {
213 if (srcs.contains(i.getSource(s_ix).getType())) {
214 // we found a valid mapping
215 return;
216 }
217
218 // WORKAROUNDS for busted BXML
219 // TODO: fix these in the BXML
220 // after they are all fixed, we can freshen BXML
221 auto t = i.getSource(s_ix).getType();
222 if (t == Type::UV || t == Type::V || t == Type::VF) {
223 return;
224 }
225 }
226 }
227 // we ran through all the type mappings without finding a match
228 // throw a fit
229 warning("invalid operand type combination for instruction");
230 }
231
232 void checkSrc(const Instruction &i, int srcIx) {
233 const Operand &src = i.getSource(srcIx);
234 const OpSpec &instSpec = i.getOpSpec();
235 if ((m_enabled_warnings & IGA_WARNINGS_REGIONS) &&
236 instSpec.supportsDestination() && !instSpec.isAnySendFormat()) {
237 checkRegRegioningRestrictions(i.getExecSize(), i.getDestination(), src);
238 }
239 Type srcType = src.getType();
240 bool lblArg = src.getKind() == Operand::Kind::LABEL ||
241 src.getKind() == Operand::Kind::IMMEDIATE;
242 if ((m_enabled_warnings & IGA_WARNINGS_NORMFORM) &&
243 srcType != Type::INVALID &&
244 instSpec.hasImplicitSrcType(srcIx, lblArg) &&
245 instSpec.implicitSrcType(srcIx, lblArg) != srcType) {
246 warning("src type is not binary normal form");
247 }
248 checkSrcScalarReg(i, src);
249 checkSrcReducedRegion(i, srcIx);
250 checkMathSource(i, srcIx);
251 checkTernarySource(i, srcIx);
252 }
253
254 void checkTernarySource(const Instruction& i, int srcIx) {
255 if (i.getSourceCount() != 3)
256 return;
257 auto& src = i.getSource(srcIx);
258 if (src.isImm() || src.getDirRegName() == RegName::GRF_R)
259 return;
260
261 // valid arf kind is acc and null
262 if (src.getDirRegName() == RegName::ARF_ACC ||
263 src.getDirRegName() == RegName::ARF_NULL)
264 return;
265 warning("Invalid ARF register file for ternary instructions");
266 }
267
268 // Math can only have grf source with mme
269 void checkMathSource(const Instruction &i, int srcIx) {
270 if (!i.isMacro())
271 return;
272
273 // Math can have acc source on XE3P+
274 if (m_model.platform > Platform::XE3)
275 return;
276 auto& src = i.getSource(srcIx);
277 if (src.getKind() != Operand::Kind::MACRO)
278 return;
279
280 if (src.getDirRegName() != RegName::GRF_R)
281 error("Invalid register: math must have grf sources");
282 }
283
284 // Check if the src is in reduced region format (flat or scalar). If it is,
285 // check if it is a legal region according to inst's execSize,
286 // src/dst type and dst stride. The register access of the src must aligned
287 // with dst.
288 void checkSrcReducedRegion(const Instruction &i, int srcIx) {
289 const OpSpec &instSpec = i.getOpSpec();
290 if (!m_model.srcHasReducedRegion(srcIx) || instSpec.isAnySendFormat() ||
291 instSpec.isDpasFormat() || instSpec.isBranching())
292 return;
293
294 const Operand &src = i.getSource(srcIx);
295 if (src.getDirRegName() != RegName::GRF_R &&
296 src.getKind() != Operand::Kind::INDIRECT)
297 return;
298
299 const Region &rgn = src.getRegion();
300 // scalar region
301 if (rgn.isScalar())
302 return;
303
304 // reducedRegion must be in <V;W,H> or <V;H> formats
305 if (rgn.getVt() == Region::Vert::VT_INVALID ||
306 rgn.getVt() == Region::Vert::VT_VxH ||
307 rgn.getHz() == Region::Horz::HZ_INVALID) {
308 error("Invalid flat region format");
309 return;
310 }
311
312 uint32_t V = rgn.v;
313 uint32_t W = rgn.w;
314 uint32_t H = rgn.h;
315 // get Width value from <V;H> format region
316 if (rgn.getWi() == Region::Width::WI_INVALID) {
317 if (H)
318 W = V / H;
319 else
320 W = V;
321 }
322
323 // Check if VWH region is legal
324 uint32_t singleStride = 0;
325 const uint32_t execSize = static_cast<uint32_t>(i.getExecSize());
326 auto isContinuous = [&singleStride, &execSize](uint32_t V, uint32_t W,
327 uint32_t H) {
328 if ((V == 1 && W == 1) || (V == W && H == 1) || execSize == 1 ||
329 (execSize <= W && H == 1)) {
330 singleStride = 1;
331 return true;
332 }
333 return false;
334 };
335
336 auto isSingleStride = [&singleStride, &execSize](uint32_t V, uint32_t W,
337 uint32_t H) {
338 if ((V == W * H) || (W == execSize && H)) {
339 singleStride = H;
340 return true;
341 }
342 if (W == 1 && H == 0) {
343 singleStride = V;
344 return true;
345 }
346 return false;
347 };
348
349 if (isContinuous(V, W, H) || isSingleStride(V, W, H)) {
350 // return if the dst and src access is aligned
351 if (TypeSizeInBits(src.getType()) * singleStride ==
352 TypeSizeInBits(i.getDestination().getType()) *
353 i.getDestination().getRegion().h)
354 return;
355 }
356 error("Invalid flat region");
357 }
358
359 void checkRegRegioningRestrictions(ExecSize es, const Operand &dst,
360 const Operand &src) {
361 // only for the form:
362 // op (...) dst.#<H> ... src.#<V;W,H>
363 if (src.getKind() != Operand::Kind::DIRECT)
364 return; // TODO: check indirect someday too
365 int dstTypeSz = TypeSizeInBitsWithDefault(dst.getType(), 0) / 8, // to bytes
366 srcTypeSz = TypeSizeInBitsWithDefault(src.getType(), 0) / 8; // to bytes
367 if (dstTypeSz == 0 || srcTypeSz == 0)
368 return; // e.g. Type::INVALID or a sub-byte type
369 if (dst.getRegion().getHz() == Region::Horz::HZ_INVALID)
370 return; // e.g. if the dst has no region
371 if (!src.getRegion().isVWH())
372 return; // e.g. if the src has no region
373 int srcRgnV = static_cast<int>(src.getRegion().getVt()),
374 srcRgnW = static_cast<int>(src.getRegion().getWi()),
375 srcRgnH = static_cast<int>(src.getRegion().getHz());
376 int execSize = ExecSizeToInt(es);
377 // int dstReg = dst.getDirRegRef().regNum;
378 // int srcReg = src.getDirRegRef().regNum;
379 int srcSubReg = src.getDirRegRef().subRegNum;
380 // int dstSubReg = dst.getDirRegRef().subRegNum;
381
382 // Restriction 1.1: Where n is the largest element size in bytes for
383 // any source or destination operand type, ExecSize * n must be <= 64.
384 int restrictSize = 64;
385 if (m_model.platform >= Platform::XE_HPC)
386 restrictSize = 128;
387 if (m_model.platform >= Platform::XE3P_XPC)
388 restrictSize = 256;
389 if (execSize * srcTypeSz > restrictSize) {
390 warning("register regioning restriction warning: "
391 "ExecSize * sizeof(Type) exceeds maximum GRFs\n"
392 "see Programmer's Reference Manual (Restriction 1.1)");
393 }
394 // Restriction 1.2:
395 // When the Execution Data Type is wider than the destination data type,
396 // the destination must be aligned as required by the wider execution
397 // data type and specify a HorzStride equal to the ratio in sizes of
398 // the two data types. For example, a mov with a D source and B destination
399 // must use a 4-byte aligned destination and a Dst.HorzStride of 4.
400 #if 0
401 // TODO: this fails on:
402 // mov (8|M0) r1.0<1>:b r2.0<8;8,1>:b
403 // The "Execution Data Type" ends up being 2, but the dst is 0
404 int execTypeSz = srcTypeSz == 1 ? 2 : srcTypeSz; // "Execution Data Type" is max(2,..)
405 int dstRgnH = static_cast<int>(dst.getRegion().getHz());
406 bool dstIsDirect = dst.getKind() == Operand::Kind::DIRECT;
407 if (dstIsDirect && execTypeSz > dstTypeSz) {
408 if (dstTypeSz * dstRgnH != execTypeSz) {
409 // e.g. the following violate this
410 // mov (8) r1.0<1>:b r2...:d
411 // mov (8) r1.0<2>:b r2...:d
412 // but this is okay (correct)
413 // mov (8) r1.0<4>:b r2...:d
414 warning(
415 "register regioning restriction warning: "
416 "Dst.Hz * sizeof(Dst.Type) != Execution Data Type Size (destination misaligned for type)\n"
417 "see Programmer's Reference Manual (Restriction 1.2)");
418 }
419 }
420 #endif
421
422 // Restriction 2.1: ExecSize must be greater than or equal to Width.
423 if (execSize < srcRgnW) {
424 // mov (1) r1.0<1>:d r2.0<8;8,1>:d
425 warning("register regioning restriction warning: "
426 "ExecSize <= Src.W (partial row)\n"
427 "see Programmer's Reference Manual (Restriction 2.1)");
428 }
429
430 // Restriction 2.2: If ExecSize = Width and HorzStride != 0,
431 // VertStride must be set to Width * HorzStride.
432 if (execSize == srcRgnW && srcRgnH != 0 && srcRgnV != srcRgnW * srcRgnH) {
433 warning("register regioning restriction warning: "
434 "ExecSize == Src.W && Src.H != 0 && Src.V != Src.W * Src.H "
435 "(vertical misalignment)\n"
436 "see Programmer's Reference Manual (Restriction 2.2)");
437 }
438
439 // Restriction 2.3: If ExecSize = Width and HorzStride = 0,
440 // there is no restriction on VertStride.
441 // The above checks this.
442
443 // Restriction 2.4: If Width = 1, HorzStride must be 0 regardless of
444 // the values of ExecSize and VertStride.
445 if (execSize == 1 && srcRgnH != 0) {
446 warning("register regioning restriction warning: "
447 "SIMD1 requires horizontal stride of 0 (scalar region access)\n"
448 "see Programmer's Reference Manual (Restriction 2.4)");
449 }
450
451 // Restriction 2.5: If ExecSize = Width = 1, both VertStride and
452 // HorzStride must be 0.
453 if (execSize == 1 && srcRgnW == 1 && (srcRgnV != 0 || srcRgnH != 0)) {
454 warning("register regioning restriction warning: "
455 "SIMD1 requires vertical and horiztonal to be 0 (scalar region "
456 "access)\n"
457 "see Programmer's Reference Manual (Restriction 2.5)");
458 }
459
460 // Restriction 2.6: If VertStride = HorzStride = 0, Width must be 1
461 // regardless of the value of ExecSize.
462 if (srcRgnV == 0 && srcRgnH == 0 && srcRgnW != 1) {
463 warning(
464 "register regioning restriction warning: "
465 "If vertical stride and horizontal stride are 0, width must be 1.\n"
466 "see Programmer's Reference Manual (Restriction 2.6)");
467 }
468
469 // Restriction 2.7: Dst.HorzStride must not be 0.
470 // This will be checked higher up since this method gets called on
471 // all source operands (and would duplicate error reports)
472
473 // Restriction 2.8: VertStride must be used to cross GRF register
474 // boundaries. This rule implies that elements within a 'Width' cannot cross
475 // GRF boundaries.
476 //
477 // Examples:
478 // LEGAL: mov (16) r3:ub r11.1<32;16,2>:ub
479 // e.g. the bytes accessed are
480 // |.0.1.2.3.4.5.6.7.8.9.A.B.C.D.E.F|
481 // LEGAL: mov (4) r3:d r11.1<8;4,2>:d
482 // e.g. the bytes accessed are
483 // |....0000....1111....2222....3333|
484 //
485 // ILLEGAL: mov (8) r3:d r2.1<8;8,1>:d
486 // |....0000111122223333444455556666|7777
487 // ^ bad
488 if (src.getDirRegName() == RegName::GRF_R) {
489 // int slotsInReg = 32/srcTypeSz/srcRgnH;
490 if (srcSubReg + (srcRgnW - 1) * srcRgnH >= restrictSize / 2 / srcTypeSz) {
491 // the last element is >= the maximum number of elements of
492 // this type size
493 warning("register regioning restriction warning: "
494 "Vertical stride must be used to cross GRF boundaries.\n"
495 "see Programmer's Reference Manual (Restriction 2.8)");
496 }
497 }
498 }
499 };
500
501 void iga::CheckSemantics(const Kernel &k, ErrorHandler &err,
502 uint32_t enabled_warnings) {
503 SemanticChecker checker(k.getModel(), &err, enabled_warnings);
504 checker.checkKernel(k);
505 }
506
507 // checks basic IR structure (very incomplete)
508 struct SanityChecker {
509 ErrorHandler *m_errHandler;
510 const Instruction *m_inst;
511
512 SanityChecker() : m_errHandler(nullptr), m_inst(nullptr) {}
513
514 void checkKernel(const Kernel &k) {
515 for (auto b : k.getBlockList()) {
516 for (auto i : b->getInstList()) {
517 checkInstruction(*i);
518 }
519 }
520 }
521
522 void checkInstruction(const Instruction &i) {
523 m_inst = &i;
524 if (i.getOpSpec().supportsDestination()) {
525 checkDst(i.getDestination());
526 } else if (i.getDestination().getKind() != Operand::Kind::INVALID) {
527 IGA_ASSERT_FALSE("unsupported destination should be .kind=INVALID");
528 }
529
530 checkExecSize(i);
531 int srcs = i.getSourceCount();
532 if (srcs > 0) {
533 checkSrc(i, 0);
534 }
535 if (srcs > 1) {
536 checkSrc(i, 1);
537 }
538 if (srcs > 2) {
539 checkSrc(i, 2);
540 }
541 if (srcs > 3) {
542 checkSrc(i, 3);
543 }
544 if (srcs > 4) {
545 checkSrc(i, 4);
546 }
547 m_inst = nullptr;
548 }
549
550 void checkDst(const Operand &op) {
551 switch (op.getKind()) {
552 case Operand::Kind::DIRECT:
553 IGA_ASSERT(op.getDirRegName() != RegName::INVALID, "invalid register");
554 break;
555 case Operand::Kind::INDIRECT:
556 break;
557 case Operand::Kind::MACRO:
558 IGA_ASSERT(m_inst->isMacro(), "instruction is not macro");
559 IGA_ASSERT(op.getMathMacroExt() != MathMacroExt::INVALID,
560 "invalid accumulator for macro");
561 break;
562 default:
563 IGA_ASSERT_FALSE("wrong kind for destination");
564 }
565 }
566
567 void checkExecSize(const Instruction &i) {
568 }
569
570 void checkSrc(const Instruction &inst, int srcIx) {
571 const Operand &op = inst.getSource(srcIx);
572 switch (op.getKind()) {
573 case Operand::Kind::DIRECT:
574 IGA_ASSERT(op.getDirRegName() != RegName::INVALID, "invalid register");
575 break;
576 case Operand::Kind::INDIRECT:
577 break;
578 case Operand::Kind::MACRO:
579 IGA_ASSERT(m_inst->isMacro(), "instruction is not macro");
580 IGA_ASSERT(op.getMathMacroExt() != MathMacroExt::INVALID,
581 "invalid accumulator for macro");
582 break;
583 case Operand::Kind::IMMEDIATE:
584 case Operand::Kind::LABEL:
585 break;
586 default: {
587 std::stringstream ss;
588 auto loc = inst.getLoc();
589 ss << loc.line << ":" << loc.col << ":[" << loc.offset
590 << "]: wrong kind for src" << srcIx;
591 IGA_ASSERT_FALSE(ss.str().c_str());
592 }
593 }
594 }
595 };
596
597 void iga::SanityCheckIR(const Kernel &k) {
598 SanityChecker checker;
599 checker.checkKernel(k);
600 }
601
602 void iga::SanityCheckIR(const Instruction &i) {
603 SanityChecker checker;
604 checker.checkInstruction(i);
605 }