intel/eu/validate: Look up types on demand in execution_type()
[mesa.git] / src / intel / compiler / brw_eu_validate.c
1 /*
2 * Copyright © 2015 Intel Corporation
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21 * IN THE SOFTWARE.
22 */
23
24 /** @file brw_eu_validate.c
25 *
26 * This file implements a pass that validates shader assembly.
27 */
28
29 #include "brw_eu.h"
30
31 /* We're going to do lots of string concatenation, so this should help. */
32 struct string {
33 char *str;
34 size_t len;
35 };
36
37 static void
38 cat(struct string *dest, const struct string src)
39 {
40 dest->str = realloc(dest->str, dest->len + src.len + 1);
41 memcpy(dest->str + dest->len, src.str, src.len);
42 dest->str[dest->len + src.len] = '\0';
43 dest->len = dest->len + src.len;
44 }
45 #define CAT(dest, src) cat(&dest, (struct string){src, strlen(src)})
46
47 #define error(str) "\tERROR: " str "\n"
48 #define ERROR_INDENT "\t "
49
50 #define ERROR(msg) ERROR_IF(true, msg)
51 #define ERROR_IF(cond, msg) \
52 do { \
53 if (cond) { \
54 CAT(error_msg, error(msg)); \
55 } \
56 } while(0)
57
58 #define CHECK(func, args...) \
59 do { \
60 struct string __msg = func(devinfo, inst, ##args); \
61 if (__msg.str) { \
62 cat(&error_msg, __msg); \
63 free(__msg.str); \
64 } \
65 } while (0)
66
67 static bool
68 inst_is_send(const struct gen_device_info *devinfo, const brw_inst *inst)
69 {
70 switch (brw_inst_opcode(devinfo, inst)) {
71 case BRW_OPCODE_SEND:
72 case BRW_OPCODE_SENDC:
73 case BRW_OPCODE_SENDS:
74 case BRW_OPCODE_SENDSC:
75 return true;
76 default:
77 return false;
78 }
79 }
80
81 static unsigned
82 signed_type(unsigned type)
83 {
84 switch (type) {
85 case BRW_REGISTER_TYPE_UD: return BRW_REGISTER_TYPE_D;
86 case BRW_REGISTER_TYPE_UW: return BRW_REGISTER_TYPE_W;
87 case BRW_REGISTER_TYPE_UB: return BRW_REGISTER_TYPE_B;
88 case BRW_REGISTER_TYPE_UQ: return BRW_REGISTER_TYPE_Q;
89 default: return type;
90 }
91 }
92
93 static bool
94 inst_is_raw_move(const struct gen_device_info *devinfo, const brw_inst *inst)
95 {
96 unsigned dst_type = signed_type(brw_inst_dst_type(devinfo, inst));
97 unsigned src_type = signed_type(brw_inst_src0_type(devinfo, inst));
98
99 if (brw_inst_src0_reg_file(devinfo, inst) == BRW_IMMEDIATE_VALUE) {
100 /* FIXME: not strictly true */
101 if (brw_inst_src0_type(devinfo, inst) == BRW_REGISTER_TYPE_VF ||
102 brw_inst_src0_type(devinfo, inst) == BRW_REGISTER_TYPE_UV ||
103 brw_inst_src0_type(devinfo, inst) == BRW_REGISTER_TYPE_V) {
104 return false;
105 }
106 } else if (brw_inst_src0_negate(devinfo, inst) ||
107 brw_inst_src0_abs(devinfo, inst)) {
108 return false;
109 }
110
111 return brw_inst_opcode(devinfo, inst) == BRW_OPCODE_MOV &&
112 brw_inst_saturate(devinfo, inst) == 0 &&
113 dst_type == src_type;
114 }
115
116 static bool
117 dst_is_null(const struct gen_device_info *devinfo, const brw_inst *inst)
118 {
119 return brw_inst_dst_reg_file(devinfo, inst) == BRW_ARCHITECTURE_REGISTER_FILE &&
120 brw_inst_dst_da_reg_nr(devinfo, inst) == BRW_ARF_NULL;
121 }
122
123 static bool
124 src0_is_null(const struct gen_device_info *devinfo, const brw_inst *inst)
125 {
126 return brw_inst_src0_reg_file(devinfo, inst) == BRW_ARCHITECTURE_REGISTER_FILE &&
127 brw_inst_src0_da_reg_nr(devinfo, inst) == BRW_ARF_NULL;
128 }
129
130 static bool
131 src1_is_null(const struct gen_device_info *devinfo, const brw_inst *inst)
132 {
133 return brw_inst_src1_reg_file(devinfo, inst) == BRW_ARCHITECTURE_REGISTER_FILE &&
134 brw_inst_src1_da_reg_nr(devinfo, inst) == BRW_ARF_NULL;
135 }
136
137 static bool
138 src0_is_grf(const struct gen_device_info *devinfo, const brw_inst *inst)
139 {
140 return brw_inst_src0_reg_file(devinfo, inst) == BRW_GENERAL_REGISTER_FILE;
141 }
142
143 static bool
144 src0_has_scalar_region(const struct gen_device_info *devinfo, const brw_inst *inst)
145 {
146 return brw_inst_src0_vstride(devinfo, inst) == BRW_VERTICAL_STRIDE_0 &&
147 brw_inst_src0_width(devinfo, inst) == BRW_WIDTH_1 &&
148 brw_inst_src0_hstride(devinfo, inst) == BRW_HORIZONTAL_STRIDE_0;
149 }
150
151 static bool
152 src1_has_scalar_region(const struct gen_device_info *devinfo, const brw_inst *inst)
153 {
154 return brw_inst_src1_vstride(devinfo, inst) == BRW_VERTICAL_STRIDE_0 &&
155 brw_inst_src1_width(devinfo, inst) == BRW_WIDTH_1 &&
156 brw_inst_src1_hstride(devinfo, inst) == BRW_HORIZONTAL_STRIDE_0;
157 }
158
159 static unsigned
160 num_sources_from_inst(const struct gen_device_info *devinfo,
161 const brw_inst *inst)
162 {
163 const struct opcode_desc *desc =
164 brw_opcode_desc(devinfo, brw_inst_opcode(devinfo, inst));
165 unsigned math_function;
166
167 if (brw_inst_opcode(devinfo, inst) == BRW_OPCODE_MATH) {
168 math_function = brw_inst_math_function(devinfo, inst);
169 } else if (devinfo->gen < 6 &&
170 brw_inst_opcode(devinfo, inst) == BRW_OPCODE_SEND) {
171 if (brw_inst_sfid(devinfo, inst) == BRW_SFID_MATH) {
172 /* src1 must be a descriptor (including the information to determine
173 * that the SEND is doing an extended math operation), but src0 can
174 * actually be null since it serves as the source of the implicit GRF
175 * to MRF move.
176 *
177 * If we stop using that functionality, we'll have to revisit this.
178 */
179 return 2;
180 } else {
181 /* Send instructions are allowed to have null sources since they use
182 * the base_mrf field to specify which message register source.
183 */
184 return 0;
185 }
186 } else {
187 assert(desc->nsrc < 4);
188 return desc->nsrc;
189 }
190
191 switch (math_function) {
192 case BRW_MATH_FUNCTION_INV:
193 case BRW_MATH_FUNCTION_LOG:
194 case BRW_MATH_FUNCTION_EXP:
195 case BRW_MATH_FUNCTION_SQRT:
196 case BRW_MATH_FUNCTION_RSQ:
197 case BRW_MATH_FUNCTION_SIN:
198 case BRW_MATH_FUNCTION_COS:
199 case BRW_MATH_FUNCTION_SINCOS:
200 case GEN8_MATH_FUNCTION_INVM:
201 case GEN8_MATH_FUNCTION_RSQRTM:
202 return 1;
203 case BRW_MATH_FUNCTION_FDIV:
204 case BRW_MATH_FUNCTION_POW:
205 case BRW_MATH_FUNCTION_INT_DIV_QUOTIENT_AND_REMAINDER:
206 case BRW_MATH_FUNCTION_INT_DIV_QUOTIENT:
207 case BRW_MATH_FUNCTION_INT_DIV_REMAINDER:
208 return 2;
209 default:
210 unreachable("not reached");
211 }
212 }
213
214 static struct string
215 sources_not_null(const struct gen_device_info *devinfo,
216 const brw_inst *inst)
217 {
218 unsigned num_sources = num_sources_from_inst(devinfo, inst);
219 struct string error_msg = { .str = NULL, .len = 0 };
220
221 /* Nothing to test. 3-src instructions can only have GRF sources, and
222 * there's no bit to control the file.
223 */
224 if (num_sources == 3)
225 return (struct string){};
226
227 if (num_sources >= 1)
228 ERROR_IF(src0_is_null(devinfo, inst), "src0 is null");
229
230 if (num_sources == 2)
231 ERROR_IF(src1_is_null(devinfo, inst), "src1 is null");
232
233 return error_msg;
234 }
235
236 static struct string
237 send_restrictions(const struct gen_device_info *devinfo,
238 const brw_inst *inst)
239 {
240 struct string error_msg = { .str = NULL, .len = 0 };
241
242 if (brw_inst_opcode(devinfo, inst) == BRW_OPCODE_SEND) {
243 ERROR_IF(brw_inst_src0_address_mode(devinfo, inst) != BRW_ADDRESS_DIRECT,
244 "send must use direct addressing");
245
246 if (devinfo->gen >= 7) {
247 ERROR_IF(!src0_is_grf(devinfo, inst), "send from non-GRF");
248 ERROR_IF(brw_inst_eot(devinfo, inst) &&
249 brw_inst_src0_da_reg_nr(devinfo, inst) < 112,
250 "send with EOT must use g112-g127");
251 }
252 }
253
254 return error_msg;
255 }
256
257 static bool
258 is_unsupported_inst(const struct gen_device_info *devinfo,
259 const brw_inst *inst)
260 {
261 return brw_opcode_desc(devinfo, brw_inst_opcode(devinfo, inst)) == NULL;
262 }
263
264 static enum brw_reg_type
265 execution_type_for_type(enum brw_reg_type type)
266 {
267 switch (type) {
268 case BRW_REGISTER_TYPE_DF:
269 case BRW_REGISTER_TYPE_F:
270 case BRW_REGISTER_TYPE_HF:
271 return type;
272
273 case BRW_REGISTER_TYPE_VF:
274 return BRW_REGISTER_TYPE_F;
275
276 case BRW_REGISTER_TYPE_Q:
277 case BRW_REGISTER_TYPE_UQ:
278 return BRW_REGISTER_TYPE_Q;
279
280 case BRW_REGISTER_TYPE_D:
281 case BRW_REGISTER_TYPE_UD:
282 return BRW_REGISTER_TYPE_D;
283
284 case BRW_REGISTER_TYPE_W:
285 case BRW_REGISTER_TYPE_UW:
286 case BRW_REGISTER_TYPE_B:
287 case BRW_REGISTER_TYPE_UB:
288 case BRW_REGISTER_TYPE_V:
289 case BRW_REGISTER_TYPE_UV:
290 return BRW_REGISTER_TYPE_W;
291 }
292 unreachable("not reached");
293 }
294
295 /**
296 * Returns the execution type of an instruction \p inst
297 */
298 static enum brw_reg_type
299 execution_type(const struct gen_device_info *devinfo, const brw_inst *inst)
300 {
301 unsigned num_sources = num_sources_from_inst(devinfo, inst);
302 enum brw_reg_type src0_exec_type, src1_exec_type;
303
304 /* Execution data type is independent of destination data type, except in
305 * mixed F/HF instructions on CHV and SKL+.
306 */
307 enum brw_reg_type dst_exec_type = brw_inst_dst_type(devinfo, inst);
308
309 src0_exec_type = execution_type_for_type(brw_inst_src0_type(devinfo, inst));
310 if (num_sources == 1) {
311 if ((devinfo->gen >= 9 || devinfo->is_cherryview) &&
312 src0_exec_type == BRW_REGISTER_TYPE_HF) {
313 return dst_exec_type;
314 }
315 return src0_exec_type;
316 }
317
318 src1_exec_type = execution_type_for_type(brw_inst_src1_type(devinfo, inst));
319 if (src0_exec_type == src1_exec_type)
320 return src0_exec_type;
321
322 /* Mixed operand types where one is float is float on Gen < 6
323 * (and not allowed on later platforms)
324 */
325 if (devinfo->gen < 6 &&
326 (src0_exec_type == BRW_REGISTER_TYPE_F ||
327 src1_exec_type == BRW_REGISTER_TYPE_F))
328 return BRW_REGISTER_TYPE_F;
329
330 if (src0_exec_type == BRW_REGISTER_TYPE_Q ||
331 src1_exec_type == BRW_REGISTER_TYPE_Q)
332 return BRW_REGISTER_TYPE_Q;
333
334 if (src0_exec_type == BRW_REGISTER_TYPE_D ||
335 src1_exec_type == BRW_REGISTER_TYPE_D)
336 return BRW_REGISTER_TYPE_D;
337
338 if (src0_exec_type == BRW_REGISTER_TYPE_W ||
339 src1_exec_type == BRW_REGISTER_TYPE_W)
340 return BRW_REGISTER_TYPE_W;
341
342 if (src0_exec_type == BRW_REGISTER_TYPE_DF ||
343 src1_exec_type == BRW_REGISTER_TYPE_DF)
344 return BRW_REGISTER_TYPE_DF;
345
346 if (devinfo->gen >= 9 || devinfo->is_cherryview) {
347 if (dst_exec_type == BRW_REGISTER_TYPE_F ||
348 src0_exec_type == BRW_REGISTER_TYPE_F ||
349 src1_exec_type == BRW_REGISTER_TYPE_F) {
350 return BRW_REGISTER_TYPE_F;
351 } else {
352 return BRW_REGISTER_TYPE_HF;
353 }
354 }
355
356 assert(src0_exec_type == BRW_REGISTER_TYPE_F);
357 return BRW_REGISTER_TYPE_F;
358 }
359
360 /**
361 * Returns whether a region is packed
362 *
363 * A region is packed if its elements are adjacent in memory, with no
364 * intervening space, no overlap, and no replicated values.
365 */
366 static bool
367 is_packed(unsigned vstride, unsigned width, unsigned hstride)
368 {
369 if (vstride == width) {
370 if (vstride == 1) {
371 return hstride == 0;
372 } else {
373 return hstride == 1;
374 }
375 }
376
377 return false;
378 }
379
380 /**
381 * Checks restrictions listed in "General Restrictions Based on Operand Types"
382 * in the "Register Region Restrictions" section.
383 */
384 static struct string
385 general_restrictions_based_on_operand_types(const struct gen_device_info *devinfo,
386 const brw_inst *inst)
387 {
388 const struct opcode_desc *desc =
389 brw_opcode_desc(devinfo, brw_inst_opcode(devinfo, inst));
390 unsigned num_sources = num_sources_from_inst(devinfo, inst);
391 unsigned exec_size = 1 << brw_inst_exec_size(devinfo, inst);
392 struct string error_msg = { .str = NULL, .len = 0 };
393
394 if (num_sources == 3)
395 return (struct string){};
396
397 if (inst_is_send(devinfo, inst))
398 return (struct string){};
399
400 if (exec_size == 1)
401 return (struct string){};
402
403 if (desc->ndst == 0)
404 return (struct string){};
405
406 /* The PRMs say:
407 *
408 * Where n is the largest element size in bytes for any source or
409 * destination operand type, ExecSize * n must be <= 64.
410 *
411 * But we do not attempt to enforce it, because it is implied by other
412 * rules:
413 *
414 * - that the destination stride must match the execution data type
415 * - sources may not span more than two adjacent GRF registers
416 * - destination may not span more than two adjacent GRF registers
417 *
418 * In fact, checking it would weaken testing of the other rules.
419 */
420
421 unsigned dst_stride = 1 << (brw_inst_dst_hstride(devinfo, inst) - 1);
422 enum brw_reg_type dst_type = brw_inst_dst_type(devinfo, inst);
423 bool dst_type_is_byte =
424 brw_inst_dst_type(devinfo, inst) == BRW_REGISTER_TYPE_B ||
425 brw_inst_dst_type(devinfo, inst) == BRW_REGISTER_TYPE_UB;
426
427 if (dst_type_is_byte) {
428 if (is_packed(exec_size * dst_stride, exec_size, dst_stride)) {
429 if (!inst_is_raw_move(devinfo, inst)) {
430 ERROR("Only raw MOV supports a packed-byte destination");
431 return error_msg;
432 } else {
433 return (struct string){};
434 }
435 }
436 }
437
438 unsigned exec_type = execution_type(devinfo, inst);
439 unsigned exec_type_size = brw_reg_type_to_size(exec_type);
440 unsigned dst_type_size = brw_reg_type_to_size(dst_type);
441
442 /* On IVB/BYT, region parameters and execution size for DF are in terms of
443 * 32-bit elements, so they are doubled. For evaluating the validity of an
444 * instruction, we halve them.
445 */
446 if (devinfo->gen == 7 && !devinfo->is_haswell &&
447 exec_type_size == 8 && dst_type_size == 4)
448 dst_type_size = 8;
449
450 if (exec_type_size > dst_type_size) {
451 ERROR_IF(dst_stride * dst_type_size != exec_type_size,
452 "Destination stride must be equal to the ratio of the sizes of "
453 "the execution data type to the destination type");
454
455 unsigned subreg = brw_inst_dst_da1_subreg_nr(devinfo, inst);
456
457 if (brw_inst_access_mode(devinfo, inst) == BRW_ALIGN_1 &&
458 brw_inst_dst_address_mode(devinfo, inst) == BRW_ADDRESS_DIRECT) {
459 /* The i965 PRM says:
460 *
461 * Implementation Restriction: The relaxed alignment rule for byte
462 * destination (#10.5) is not supported.
463 */
464 if ((devinfo->gen > 4 || devinfo->is_g4x) && dst_type_is_byte) {
465 ERROR_IF(subreg % exec_type_size != 0 &&
466 subreg % exec_type_size != 1,
467 "Destination subreg must be aligned to the size of the "
468 "execution data type (or to the next lowest byte for byte "
469 "destinations)");
470 } else {
471 ERROR_IF(subreg % exec_type_size != 0,
472 "Destination subreg must be aligned to the size of the "
473 "execution data type");
474 }
475 }
476 }
477
478 return error_msg;
479 }
480
481 /**
482 * Checks restrictions listed in "General Restrictions on Regioning Parameters"
483 * in the "Register Region Restrictions" section.
484 */
485 static struct string
486 general_restrictions_on_region_parameters(const struct gen_device_info *devinfo,
487 const brw_inst *inst)
488 {
489 const struct opcode_desc *desc =
490 brw_opcode_desc(devinfo, brw_inst_opcode(devinfo, inst));
491 unsigned num_sources = num_sources_from_inst(devinfo, inst);
492 unsigned exec_size = 1 << brw_inst_exec_size(devinfo, inst);
493 struct string error_msg = { .str = NULL, .len = 0 };
494
495 if (num_sources == 3)
496 return (struct string){};
497
498 if (brw_inst_access_mode(devinfo, inst) == BRW_ALIGN_16) {
499 if (desc->ndst != 0 && !dst_is_null(devinfo, inst))
500 ERROR_IF(brw_inst_dst_hstride(devinfo, inst) != BRW_HORIZONTAL_STRIDE_1,
501 "Destination Horizontal Stride must be 1");
502
503 if (num_sources >= 1) {
504 if (devinfo->is_haswell || devinfo->gen >= 8) {
505 ERROR_IF(brw_inst_src0_reg_file(devinfo, inst) != BRW_IMMEDIATE_VALUE &&
506 brw_inst_src0_vstride(devinfo, inst) != BRW_VERTICAL_STRIDE_0 &&
507 brw_inst_src0_vstride(devinfo, inst) != BRW_VERTICAL_STRIDE_2 &&
508 brw_inst_src0_vstride(devinfo, inst) != BRW_VERTICAL_STRIDE_4,
509 "In Align16 mode, only VertStride of 0, 2, or 4 is allowed");
510 } else {
511 ERROR_IF(brw_inst_src0_reg_file(devinfo, inst) != BRW_IMMEDIATE_VALUE &&
512 brw_inst_src0_vstride(devinfo, inst) != BRW_VERTICAL_STRIDE_0 &&
513 brw_inst_src0_vstride(devinfo, inst) != BRW_VERTICAL_STRIDE_4,
514 "In Align16 mode, only VertStride of 0 or 4 is allowed");
515 }
516 }
517
518 if (num_sources == 2) {
519 if (devinfo->is_haswell || devinfo->gen >= 8) {
520 ERROR_IF(brw_inst_src1_reg_file(devinfo, inst) != BRW_IMMEDIATE_VALUE &&
521 brw_inst_src1_vstride(devinfo, inst) != BRW_VERTICAL_STRIDE_0 &&
522 brw_inst_src1_vstride(devinfo, inst) != BRW_VERTICAL_STRIDE_2 &&
523 brw_inst_src1_vstride(devinfo, inst) != BRW_VERTICAL_STRIDE_4,
524 "In Align16 mode, only VertStride of 0, 2, or 4 is allowed");
525 } else {
526 ERROR_IF(brw_inst_src1_reg_file(devinfo, inst) != BRW_IMMEDIATE_VALUE &&
527 brw_inst_src1_vstride(devinfo, inst) != BRW_VERTICAL_STRIDE_0 &&
528 brw_inst_src1_vstride(devinfo, inst) != BRW_VERTICAL_STRIDE_4,
529 "In Align16 mode, only VertStride of 0 or 4 is allowed");
530 }
531 }
532
533 return error_msg;
534 }
535
536 for (unsigned i = 0; i < num_sources; i++) {
537 unsigned vstride, width, hstride, element_size, subreg;
538 enum brw_reg_type type;
539
540 #define DO_SRC(n) \
541 if (brw_inst_src ## n ## _reg_file(devinfo, inst) == \
542 BRW_IMMEDIATE_VALUE) \
543 continue; \
544 \
545 vstride = brw_inst_src ## n ## _vstride(devinfo, inst) ? \
546 (1 << (brw_inst_src ## n ## _vstride(devinfo, inst) - 1)) : 0; \
547 width = 1 << brw_inst_src ## n ## _width(devinfo, inst); \
548 hstride = brw_inst_src ## n ## _hstride(devinfo, inst) ? \
549 (1 << (brw_inst_src ## n ## _hstride(devinfo, inst) - 1)) : 0; \
550 type = brw_inst_src ## n ## _type(devinfo, inst); \
551 element_size = brw_reg_type_to_size(type); \
552 subreg = brw_inst_src ## n ## _da1_subreg_nr(devinfo, inst)
553
554 if (i == 0) {
555 DO_SRC(0);
556 } else {
557 DO_SRC(1);
558 }
559 #undef DO_SRC
560
561 /* On IVB/BYT, region parameters and execution size for DF are in terms of
562 * 32-bit elements, so they are doubled. For evaluating the validity of an
563 * instruction, we halve them.
564 */
565 if (devinfo->gen == 7 && !devinfo->is_haswell &&
566 element_size == 8)
567 element_size = 4;
568
569 /* ExecSize must be greater than or equal to Width. */
570 ERROR_IF(exec_size < width, "ExecSize must be greater than or equal "
571 "to Width");
572
573 /* If ExecSize = Width and HorzStride ≠ 0,
574 * VertStride must be set to Width * HorzStride.
575 */
576 if (exec_size == width && hstride != 0) {
577 ERROR_IF(vstride != width * hstride,
578 "If ExecSize = Width and HorzStride ≠ 0, "
579 "VertStride must be set to Width * HorzStride");
580 }
581
582 /* If Width = 1, HorzStride must be 0 regardless of the values of
583 * ExecSize and VertStride.
584 */
585 if (width == 1) {
586 ERROR_IF(hstride != 0,
587 "If Width = 1, HorzStride must be 0 regardless "
588 "of the values of ExecSize and VertStride");
589 }
590
591 /* If ExecSize = Width = 1, both VertStride and HorzStride must be 0. */
592 if (exec_size == 1 && width == 1) {
593 ERROR_IF(vstride != 0 || hstride != 0,
594 "If ExecSize = Width = 1, both VertStride "
595 "and HorzStride must be 0");
596 }
597
598 /* If VertStride = HorzStride = 0, Width must be 1 regardless of the
599 * value of ExecSize.
600 */
601 if (vstride == 0 && hstride == 0) {
602 ERROR_IF(width != 1,
603 "If VertStride = HorzStride = 0, Width must be "
604 "1 regardless of the value of ExecSize");
605 }
606
607 /* VertStride must be used to cross GRF register boundaries. This rule
608 * implies that elements within a 'Width' cannot cross GRF boundaries.
609 */
610 const uint64_t mask = (1ULL << element_size) - 1;
611 unsigned rowbase = subreg;
612
613 for (int y = 0; y < exec_size / width; y++) {
614 uint64_t access_mask = 0;
615 unsigned offset = rowbase;
616
617 for (int x = 0; x < width; x++) {
618 access_mask |= mask << offset;
619 offset += hstride * element_size;
620 }
621
622 rowbase += vstride * element_size;
623
624 if ((uint32_t)access_mask != 0 && (access_mask >> 32) != 0) {
625 ERROR("VertStride must be used to cross GRF register boundaries");
626 break;
627 }
628 }
629 }
630
631 /* Dst.HorzStride must not be 0. */
632 if (desc->ndst != 0 && !dst_is_null(devinfo, inst)) {
633 ERROR_IF(brw_inst_dst_hstride(devinfo, inst) == BRW_HORIZONTAL_STRIDE_0,
634 "Destination Horizontal Stride must not be 0");
635 }
636
637 return error_msg;
638 }
639
640 /**
641 * Creates an \p access_mask for an \p exec_size, \p element_size, and a region
642 *
643 * An \p access_mask is a 32-element array of uint64_t, where each uint64_t is
644 * a bitmask of bytes accessed by the region.
645 *
646 * For instance the access mask of the source gX.1<4,2,2>F in an exec_size = 4
647 * instruction would be
648 *
649 * access_mask[0] = 0x00000000000000F0
650 * access_mask[1] = 0x000000000000F000
651 * access_mask[2] = 0x0000000000F00000
652 * access_mask[3] = 0x00000000F0000000
653 * access_mask[4-31] = 0
654 *
655 * because the first execution channel accesses bytes 7-4 and the second
656 * execution channel accesses bytes 15-12, etc.
657 */
658 static void
659 align1_access_mask(uint64_t access_mask[static 32],
660 unsigned exec_size, unsigned element_size, unsigned subreg,
661 unsigned vstride, unsigned width, unsigned hstride)
662 {
663 const uint64_t mask = (1ULL << element_size) - 1;
664 unsigned rowbase = subreg;
665 unsigned element = 0;
666
667 for (int y = 0; y < exec_size / width; y++) {
668 unsigned offset = rowbase;
669
670 for (int x = 0; x < width; x++) {
671 access_mask[element++] = mask << offset;
672 offset += hstride * element_size;
673 }
674
675 rowbase += vstride * element_size;
676 }
677
678 assert(element == 0 || element == exec_size);
679 }
680
681 /**
682 * Returns the number of registers accessed according to the \p access_mask
683 */
684 static int
685 registers_read(const uint64_t access_mask[static 32])
686 {
687 int regs_read = 0;
688
689 for (unsigned i = 0; i < 32; i++) {
690 if (access_mask[i] > 0xFFFFFFFF) {
691 return 2;
692 } else if (access_mask[i]) {
693 regs_read = 1;
694 }
695 }
696
697 return regs_read;
698 }
699
700 /**
701 * Checks restrictions listed in "Region Alignment Rules" in the "Register
702 * Region Restrictions" section.
703 */
704 static struct string
705 region_alignment_rules(const struct gen_device_info *devinfo,
706 const brw_inst *inst)
707 {
708 const struct opcode_desc *desc =
709 brw_opcode_desc(devinfo, brw_inst_opcode(devinfo, inst));
710 unsigned num_sources = num_sources_from_inst(devinfo, inst);
711 unsigned exec_size = 1 << brw_inst_exec_size(devinfo, inst);
712 uint64_t dst_access_mask[32], src0_access_mask[32], src1_access_mask[32];
713 struct string error_msg = { .str = NULL, .len = 0 };
714
715 if (num_sources == 3)
716 return (struct string){};
717
718 if (brw_inst_access_mode(devinfo, inst) == BRW_ALIGN_16)
719 return (struct string){};
720
721 if (inst_is_send(devinfo, inst))
722 return (struct string){};
723
724 memset(dst_access_mask, 0, sizeof(dst_access_mask));
725 memset(src0_access_mask, 0, sizeof(src0_access_mask));
726 memset(src1_access_mask, 0, sizeof(src1_access_mask));
727
728 for (unsigned i = 0; i < num_sources; i++) {
729 unsigned vstride, width, hstride, element_size, subreg;
730 enum brw_reg_type type;
731
732 /* In Direct Addressing mode, a source cannot span more than 2 adjacent
733 * GRF registers.
734 */
735
736 #define DO_SRC(n) \
737 if (brw_inst_src ## n ## _address_mode(devinfo, inst) != \
738 BRW_ADDRESS_DIRECT) \
739 continue; \
740 \
741 if (brw_inst_src ## n ## _reg_file(devinfo, inst) == \
742 BRW_IMMEDIATE_VALUE) \
743 continue; \
744 \
745 vstride = brw_inst_src ## n ## _vstride(devinfo, inst) ? \
746 (1 << (brw_inst_src ## n ## _vstride(devinfo, inst) - 1)) : 0; \
747 width = 1 << brw_inst_src ## n ## _width(devinfo, inst); \
748 hstride = brw_inst_src ## n ## _hstride(devinfo, inst) ? \
749 (1 << (brw_inst_src ## n ## _hstride(devinfo, inst) - 1)) : 0; \
750 type = brw_inst_src ## n ## _type(devinfo, inst); \
751 element_size = brw_reg_type_to_size(type); \
752 subreg = brw_inst_src ## n ## _da1_subreg_nr(devinfo, inst); \
753 align1_access_mask(src ## n ## _access_mask, \
754 exec_size, element_size, subreg, \
755 vstride, width, hstride)
756
757 if (i == 0) {
758 DO_SRC(0);
759 } else {
760 DO_SRC(1);
761 }
762 #undef DO_SRC
763
764 unsigned num_vstride = exec_size / width;
765 unsigned num_hstride = width;
766 unsigned vstride_elements = (num_vstride - 1) * vstride;
767 unsigned hstride_elements = (num_hstride - 1) * hstride;
768 unsigned offset = (vstride_elements + hstride_elements) * element_size +
769 subreg;
770 ERROR_IF(offset >= 64,
771 "A source cannot span more than 2 adjacent GRF registers");
772 }
773
774 if (desc->ndst == 0 || dst_is_null(devinfo, inst))
775 return error_msg;
776
777 unsigned stride = 1 << (brw_inst_dst_hstride(devinfo, inst) - 1);
778 enum brw_reg_type dst_type = brw_inst_dst_type(devinfo, inst);
779 unsigned element_size = brw_reg_type_to_size(dst_type);
780 unsigned subreg = brw_inst_dst_da1_subreg_nr(devinfo, inst);
781 unsigned offset = ((exec_size - 1) * stride * element_size) + subreg;
782 ERROR_IF(offset >= 64,
783 "A destination cannot span more than 2 adjacent GRF registers");
784
785 if (error_msg.str)
786 return error_msg;
787
788 /* On IVB/BYT, region parameters and execution size for DF are in terms of
789 * 32-bit elements, so they are doubled. For evaluating the validity of an
790 * instruction, we halve them.
791 */
792 if (devinfo->gen == 7 && !devinfo->is_haswell &&
793 element_size == 8)
794 element_size = 4;
795
796 align1_access_mask(dst_access_mask, exec_size, element_size, subreg,
797 exec_size == 1 ? 0 : exec_size * stride,
798 exec_size == 1 ? 1 : exec_size,
799 exec_size == 1 ? 0 : stride);
800
801 unsigned dst_regs = registers_read(dst_access_mask);
802 unsigned src0_regs = registers_read(src0_access_mask);
803 unsigned src1_regs = registers_read(src1_access_mask);
804
805 /* The SNB, IVB, HSW, BDW, and CHV PRMs say:
806 *
807 * When an instruction has a source region spanning two registers and a
808 * destination region contained in one register, the number of elements
809 * must be the same between two sources and one of the following must be
810 * true:
811 *
812 * 1. The destination region is entirely contained in the lower OWord
813 * of a register.
814 * 2. The destination region is entirely contained in the upper OWord
815 * of a register.
816 * 3. The destination elements are evenly split between the two OWords
817 * of a register.
818 */
819 if (devinfo->gen <= 8) {
820 if (dst_regs == 1 && (src0_regs == 2 || src1_regs == 2)) {
821 unsigned upper_oword_writes = 0, lower_oword_writes = 0;
822
823 for (unsigned i = 0; i < exec_size; i++) {
824 if (dst_access_mask[i] > 0x0000FFFF) {
825 upper_oword_writes++;
826 } else {
827 assert(dst_access_mask[i] != 0);
828 lower_oword_writes++;
829 }
830 }
831
832 ERROR_IF(lower_oword_writes != 0 &&
833 upper_oword_writes != 0 &&
834 upper_oword_writes != lower_oword_writes,
835 "Writes must be to only one OWord or "
836 "evenly split between OWords");
837 }
838 }
839
840 /* The IVB and HSW PRMs say:
841 *
842 * When an instruction has a source region that spans two registers and
843 * the destination spans two registers, the destination elements must be
844 * evenly split between the two registers [...]
845 *
846 * The SNB PRM contains similar wording (but written in a much more
847 * confusing manner).
848 *
849 * The BDW PRM says:
850 *
851 * When destination spans two registers, the source may be one or two
852 * registers. The destination elements must be evenly split between the
853 * two registers.
854 *
855 * The SKL PRM says:
856 *
857 * When destination of MATH instruction spans two registers, the
858 * destination elements must be evenly split between the two registers.
859 *
860 * It is not known whether this restriction applies to KBL other Gens after
861 * SKL.
862 */
863 if (devinfo->gen <= 8 ||
864 brw_inst_opcode(devinfo, inst) == BRW_OPCODE_MATH) {
865
866 /* Nothing explicitly states that on Gen < 8 elements must be evenly
867 * split between two destination registers in the two exceptional
868 * source-region-spans-one-register cases, but since Broadwell requires
869 * evenly split writes regardless of source region, we assume that it was
870 * an oversight and require it.
871 */
872 if (dst_regs == 2) {
873 unsigned upper_reg_writes = 0, lower_reg_writes = 0;
874
875 for (unsigned i = 0; i < exec_size; i++) {
876 if (dst_access_mask[i] > 0xFFFFFFFF) {
877 upper_reg_writes++;
878 } else {
879 assert(dst_access_mask[i] != 0);
880 lower_reg_writes++;
881 }
882 }
883
884 ERROR_IF(upper_reg_writes != lower_reg_writes,
885 "Writes must be evenly split between the two "
886 "destination registers");
887 }
888 }
889
890 /* The IVB and HSW PRMs say:
891 *
892 * When an instruction has a source region that spans two registers and
893 * the destination spans two registers, the destination elements must be
894 * evenly split between the two registers and each destination register
895 * must be entirely derived from one source register.
896 *
897 * Note: In such cases, the regioning parameters must ensure that the
898 * offset from the two source registers is the same.
899 *
900 * The SNB PRM contains similar wording (but written in a much more
901 * confusing manner).
902 *
903 * There are effectively three rules stated here:
904 *
905 * For an instruction with a source and a destination spanning two
906 * registers,
907 *
908 * (1) destination elements must be evenly split between the two
909 * registers
910 * (2) all destination elements in a register must be derived
911 * from one source register
912 * (3) the offset (i.e. the starting location in each of the two
913 * registers spanned by a region) must be the same in the two
914 * registers spanned by a region
915 *
916 * It is impossible to violate rule (1) without violating (2) or (3), so we
917 * do not attempt to validate it.
918 */
919 if (devinfo->gen <= 7 && dst_regs == 2) {
920 for (unsigned i = 0; i < num_sources; i++) {
921 #define DO_SRC(n) \
922 if (src ## n ## _regs <= 1) \
923 continue; \
924 \
925 for (unsigned i = 0; i < exec_size; i++) { \
926 if ((dst_access_mask[i] > 0xFFFFFFFF) != \
927 (src ## n ## _access_mask[i] > 0xFFFFFFFF)) { \
928 ERROR("Each destination register must be entirely derived " \
929 "from one source register"); \
930 break; \
931 } \
932 } \
933 \
934 unsigned offset_0 = \
935 brw_inst_src ## n ## _da1_subreg_nr(devinfo, inst); \
936 unsigned offset_1 = offset_0; \
937 \
938 for (unsigned i = 0; i < exec_size; i++) { \
939 if (src ## n ## _access_mask[i] > 0xFFFFFFFF) { \
940 offset_1 = __builtin_ctzll(src ## n ## _access_mask[i]) - 32; \
941 break; \
942 } \
943 } \
944 \
945 ERROR_IF(offset_0 != offset_1, \
946 "The offset from the two source registers " \
947 "must be the same")
948
949 if (i == 0) {
950 DO_SRC(0);
951 } else {
952 DO_SRC(1);
953 }
954 #undef DO_SRC
955 }
956 }
957
958 /* The IVB and HSW PRMs say:
959 *
960 * When destination spans two registers, the source MUST span two
961 * registers. The exception to the above rule:
962 * 1. When source is scalar, the source registers are not
963 * incremented.
964 * 2. When source is packed integer Word and destination is packed
965 * integer DWord, the source register is not incremented by the
966 * source sub register is incremented.
967 *
968 * The SNB PRM does not contain this rule, but the internal documentation
969 * indicates that it applies to SNB as well. We assume that the rule applies
970 * to Gen <= 5 although their PRMs do not state it.
971 *
972 * While the documentation explicitly says in exception (2) that the
973 * destination must be an integer DWord, the hardware allows at least a
974 * float destination type as well. We emit such instructions from
975 *
976 * fs_visitor::emit_interpolation_setup_gen6
977 * fs_visitor::emit_fragcoord_interpolation
978 *
979 * and have for years with no ill effects.
980 *
981 * Additionally the simulator source code indicates that the real condition
982 * is that the size of the destination type is 4 bytes.
983 */
984 if (devinfo->gen <= 7 && dst_regs == 2) {
985 enum brw_reg_type dst_type = brw_inst_dst_type(devinfo, inst);
986 bool dst_is_packed_dword =
987 is_packed(exec_size * stride, exec_size, stride) &&
988 brw_reg_type_to_size(dst_type) == 4;
989
990 for (unsigned i = 0; i < num_sources; i++) {
991 #define DO_SRC(n) \
992 unsigned vstride, width, hstride; \
993 vstride = brw_inst_src ## n ## _vstride(devinfo, inst) ? \
994 (1 << (brw_inst_src ## n ## _vstride(devinfo, inst) - 1)) : 0; \
995 width = 1 << brw_inst_src ## n ## _width(devinfo, inst); \
996 hstride = brw_inst_src ## n ## _hstride(devinfo, inst) ? \
997 (1 << (brw_inst_src ## n ## _hstride(devinfo, inst) - 1)) : 0; \
998 bool src ## n ## _is_packed_word = \
999 is_packed(vstride, width, hstride) && \
1000 (brw_inst_src ## n ## _type(devinfo, inst) == BRW_REGISTER_TYPE_W || \
1001 brw_inst_src ## n ## _type(devinfo, inst) == BRW_REGISTER_TYPE_UW); \
1002 \
1003 ERROR_IF(src ## n ## _regs == 1 && \
1004 !src ## n ## _has_scalar_region(devinfo, inst) && \
1005 !(dst_is_packed_dword && src ## n ## _is_packed_word), \
1006 "When the destination spans two registers, the source must " \
1007 "span two registers\n" ERROR_INDENT "(exceptions for scalar " \
1008 "source and packed-word to packed-dword expansion)")
1009
1010 if (i == 0) {
1011 DO_SRC(0);
1012 } else {
1013 DO_SRC(1);
1014 }
1015 #undef DO_SRC
1016 }
1017 }
1018
1019 return error_msg;
1020 }
1021
1022 static struct string
1023 vector_immediate_restrictions(const struct gen_device_info *devinfo,
1024 const brw_inst *inst)
1025 {
1026 unsigned num_sources = num_sources_from_inst(devinfo, inst);
1027 struct string error_msg = { .str = NULL, .len = 0 };
1028
1029 if (num_sources == 3 || num_sources == 0)
1030 return (struct string){};
1031
1032 unsigned file = num_sources == 1 ?
1033 brw_inst_src0_reg_file(devinfo, inst) :
1034 brw_inst_src1_reg_file(devinfo, inst);
1035 if (file != BRW_IMMEDIATE_VALUE)
1036 return (struct string){};
1037
1038 enum brw_reg_type dst_type = brw_inst_dst_type(devinfo, inst);
1039 unsigned dst_type_size = brw_reg_type_to_size(dst_type);
1040 unsigned dst_subreg = brw_inst_access_mode(devinfo, inst) == BRW_ALIGN_1 ?
1041 brw_inst_dst_da1_subreg_nr(devinfo, inst) : 0;
1042 unsigned dst_stride = 1 << (brw_inst_dst_hstride(devinfo, inst) - 1);
1043 enum brw_reg_type type = num_sources == 1 ?
1044 brw_inst_src0_type(devinfo, inst) :
1045 brw_inst_src1_type(devinfo, inst);
1046
1047 /* The PRMs say:
1048 *
1049 * When an immediate vector is used in an instruction, the destination
1050 * must be 128-bit aligned with destination horizontal stride equivalent
1051 * to a word for an immediate integer vector (v) and equivalent to a
1052 * DWord for an immediate float vector (vf).
1053 *
1054 * The text has not been updated for the addition of the immediate unsigned
1055 * integer vector type (uv) on SNB, but presumably the same restriction
1056 * applies.
1057 */
1058 switch (type) {
1059 case BRW_REGISTER_TYPE_V:
1060 case BRW_REGISTER_TYPE_UV:
1061 case BRW_REGISTER_TYPE_VF:
1062 ERROR_IF(dst_subreg % (128 / 8) != 0,
1063 "Destination must be 128-bit aligned in order to use immediate "
1064 "vector types");
1065
1066 if (type == BRW_REGISTER_TYPE_VF) {
1067 ERROR_IF(dst_type_size * dst_stride != 4,
1068 "Destination must have stride equivalent to dword in order "
1069 "to use the VF type");
1070 } else {
1071 ERROR_IF(dst_type_size * dst_stride != 2,
1072 "Destination must have stride equivalent to word in order "
1073 "to use the V or UV type");
1074 }
1075 break;
1076 default:
1077 break;
1078 }
1079
1080 return error_msg;
1081 }
1082
1083 bool
1084 brw_validate_instructions(const struct gen_device_info *devinfo,
1085 void *assembly, int start_offset, int end_offset,
1086 struct annotation_info *annotation)
1087 {
1088 bool valid = true;
1089
1090 for (int src_offset = start_offset; src_offset < end_offset;) {
1091 struct string error_msg = { .str = NULL, .len = 0 };
1092 const brw_inst *inst = assembly + src_offset;
1093 bool is_compact = brw_inst_cmpt_control(devinfo, inst);
1094 brw_inst uncompacted;
1095
1096 if (is_compact) {
1097 brw_compact_inst *compacted = (void *)inst;
1098 brw_uncompact_instruction(devinfo, &uncompacted, compacted);
1099 inst = &uncompacted;
1100 }
1101
1102 if (is_unsupported_inst(devinfo, inst)) {
1103 ERROR("Instruction not supported on this Gen");
1104 } else {
1105 CHECK(sources_not_null);
1106 CHECK(send_restrictions);
1107 CHECK(general_restrictions_based_on_operand_types);
1108 CHECK(general_restrictions_on_region_parameters);
1109 CHECK(region_alignment_rules);
1110 CHECK(vector_immediate_restrictions);
1111 }
1112
1113 if (error_msg.str && annotation) {
1114 annotation_insert_error(annotation, src_offset, error_msg.str);
1115 }
1116 valid = valid && error_msg.len == 0;
1117 free(error_msg.str);
1118
1119 if (is_compact) {
1120 src_offset += sizeof(brw_compact_inst);
1121 } else {
1122 src_offset += sizeof(brw_inst);
1123 }
1124 }
1125
1126 return valid;
1127 }