i965: Stop using hardware register types directly
[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 enum brw_reg_type src0_type = brw_inst_src0_type(devinfo, inst);
304 enum brw_reg_type src1_type = brw_inst_src1_type(devinfo, inst);
305
306 /* Execution data type is independent of destination data type, except in
307 * mixed F/HF instructions on CHV and SKL+.
308 */
309 enum brw_reg_type dst_exec_type = brw_inst_dst_type(devinfo, inst);
310
311 src0_exec_type = execution_type_for_type(src0_type);
312 if (num_sources == 1) {
313 if ((devinfo->gen >= 9 || devinfo->is_cherryview) &&
314 src0_exec_type == BRW_REGISTER_TYPE_HF) {
315 return dst_exec_type;
316 }
317 return src0_exec_type;
318 }
319
320 src1_exec_type = execution_type_for_type(src1_type);
321 if (src0_exec_type == src1_exec_type)
322 return src0_exec_type;
323
324 /* Mixed operand types where one is float is float on Gen < 6
325 * (and not allowed on later platforms)
326 */
327 if (devinfo->gen < 6 &&
328 (src0_exec_type == BRW_REGISTER_TYPE_F ||
329 src1_exec_type == BRW_REGISTER_TYPE_F))
330 return BRW_REGISTER_TYPE_F;
331
332 if (src0_exec_type == BRW_REGISTER_TYPE_Q ||
333 src1_exec_type == BRW_REGISTER_TYPE_Q)
334 return BRW_REGISTER_TYPE_Q;
335
336 if (src0_exec_type == BRW_REGISTER_TYPE_D ||
337 src1_exec_type == BRW_REGISTER_TYPE_D)
338 return BRW_REGISTER_TYPE_D;
339
340 if (src0_exec_type == BRW_REGISTER_TYPE_W ||
341 src1_exec_type == BRW_REGISTER_TYPE_W)
342 return BRW_REGISTER_TYPE_W;
343
344 if (src0_exec_type == BRW_REGISTER_TYPE_DF ||
345 src1_exec_type == BRW_REGISTER_TYPE_DF)
346 return BRW_REGISTER_TYPE_DF;
347
348 if (devinfo->gen >= 9 || devinfo->is_cherryview) {
349 if (dst_exec_type == BRW_REGISTER_TYPE_F ||
350 src0_exec_type == BRW_REGISTER_TYPE_F ||
351 src1_exec_type == BRW_REGISTER_TYPE_F) {
352 return BRW_REGISTER_TYPE_F;
353 } else {
354 return BRW_REGISTER_TYPE_HF;
355 }
356 }
357
358 assert(src0_exec_type == BRW_REGISTER_TYPE_F);
359 return BRW_REGISTER_TYPE_F;
360 }
361
362 /**
363 * Returns whether a region is packed
364 *
365 * A region is packed if its elements are adjacent in memory, with no
366 * intervening space, no overlap, and no replicated values.
367 */
368 static bool
369 is_packed(unsigned vstride, unsigned width, unsigned hstride)
370 {
371 if (vstride == width) {
372 if (vstride == 1) {
373 return hstride == 0;
374 } else {
375 return hstride == 1;
376 }
377 }
378
379 return false;
380 }
381
382 /**
383 * Checks restrictions listed in "General Restrictions Based on Operand Types"
384 * in the "Register Region Restrictions" section.
385 */
386 static struct string
387 general_restrictions_based_on_operand_types(const struct gen_device_info *devinfo,
388 const brw_inst *inst)
389 {
390 const struct opcode_desc *desc =
391 brw_opcode_desc(devinfo, brw_inst_opcode(devinfo, inst));
392 unsigned num_sources = num_sources_from_inst(devinfo, inst);
393 unsigned exec_size = 1 << brw_inst_exec_size(devinfo, inst);
394 struct string error_msg = { .str = NULL, .len = 0 };
395
396 if (num_sources == 3)
397 return (struct string){};
398
399 if (inst_is_send(devinfo, inst))
400 return (struct string){};
401
402 if (exec_size == 1)
403 return (struct string){};
404
405 if (desc->ndst == 0)
406 return (struct string){};
407
408 /* The PRMs say:
409 *
410 * Where n is the largest element size in bytes for any source or
411 * destination operand type, ExecSize * n must be <= 64.
412 *
413 * But we do not attempt to enforce it, because it is implied by other
414 * rules:
415 *
416 * - that the destination stride must match the execution data type
417 * - sources may not span more than two adjacent GRF registers
418 * - destination may not span more than two adjacent GRF registers
419 *
420 * In fact, checking it would weaken testing of the other rules.
421 */
422
423 unsigned dst_stride = 1 << (brw_inst_dst_hstride(devinfo, inst) - 1);
424 enum brw_reg_type dst_type = brw_inst_dst_type(devinfo, inst);
425 bool dst_type_is_byte =
426 brw_inst_dst_type(devinfo, inst) == BRW_REGISTER_TYPE_B ||
427 brw_inst_dst_type(devinfo, inst) == BRW_REGISTER_TYPE_UB;
428
429 if (dst_type_is_byte) {
430 if (is_packed(exec_size * dst_stride, exec_size, dst_stride)) {
431 if (!inst_is_raw_move(devinfo, inst)) {
432 ERROR("Only raw MOV supports a packed-byte destination");
433 return error_msg;
434 } else {
435 return (struct string){};
436 }
437 }
438 }
439
440 unsigned exec_type = execution_type(devinfo, inst);
441 unsigned exec_type_size = brw_reg_type_to_size(exec_type);
442 unsigned dst_type_size = brw_reg_type_to_size(dst_type);
443
444 /* On IVB/BYT, region parameters and execution size for DF are in terms of
445 * 32-bit elements, so they are doubled. For evaluating the validity of an
446 * instruction, we halve them.
447 */
448 if (devinfo->gen == 7 && !devinfo->is_haswell &&
449 exec_type_size == 8 && dst_type_size == 4)
450 dst_type_size = 8;
451
452 if (exec_type_size > dst_type_size) {
453 ERROR_IF(dst_stride * dst_type_size != exec_type_size,
454 "Destination stride must be equal to the ratio of the sizes of "
455 "the execution data type to the destination type");
456
457 unsigned subreg = brw_inst_dst_da1_subreg_nr(devinfo, inst);
458
459 if (brw_inst_access_mode(devinfo, inst) == BRW_ALIGN_1 &&
460 brw_inst_dst_address_mode(devinfo, inst) == BRW_ADDRESS_DIRECT) {
461 /* The i965 PRM says:
462 *
463 * Implementation Restriction: The relaxed alignment rule for byte
464 * destination (#10.5) is not supported.
465 */
466 if ((devinfo->gen > 4 || devinfo->is_g4x) && dst_type_is_byte) {
467 ERROR_IF(subreg % exec_type_size != 0 &&
468 subreg % exec_type_size != 1,
469 "Destination subreg must be aligned to the size of the "
470 "execution data type (or to the next lowest byte for byte "
471 "destinations)");
472 } else {
473 ERROR_IF(subreg % exec_type_size != 0,
474 "Destination subreg must be aligned to the size of the "
475 "execution data type");
476 }
477 }
478 }
479
480 return error_msg;
481 }
482
483 /**
484 * Checks restrictions listed in "General Restrictions on Regioning Parameters"
485 * in the "Register Region Restrictions" section.
486 */
487 static struct string
488 general_restrictions_on_region_parameters(const struct gen_device_info *devinfo,
489 const brw_inst *inst)
490 {
491 const struct opcode_desc *desc =
492 brw_opcode_desc(devinfo, brw_inst_opcode(devinfo, inst));
493 unsigned num_sources = num_sources_from_inst(devinfo, inst);
494 unsigned exec_size = 1 << brw_inst_exec_size(devinfo, inst);
495 struct string error_msg = { .str = NULL, .len = 0 };
496
497 if (num_sources == 3)
498 return (struct string){};
499
500 if (brw_inst_access_mode(devinfo, inst) == BRW_ALIGN_16) {
501 if (desc->ndst != 0 && !dst_is_null(devinfo, inst))
502 ERROR_IF(brw_inst_dst_hstride(devinfo, inst) != BRW_HORIZONTAL_STRIDE_1,
503 "Destination Horizontal Stride must be 1");
504
505 if (num_sources >= 1) {
506 if (devinfo->is_haswell || devinfo->gen >= 8) {
507 ERROR_IF(brw_inst_src0_reg_file(devinfo, inst) != BRW_IMMEDIATE_VALUE &&
508 brw_inst_src0_vstride(devinfo, inst) != BRW_VERTICAL_STRIDE_0 &&
509 brw_inst_src0_vstride(devinfo, inst) != BRW_VERTICAL_STRIDE_2 &&
510 brw_inst_src0_vstride(devinfo, inst) != BRW_VERTICAL_STRIDE_4,
511 "In Align16 mode, only VertStride of 0, 2, or 4 is allowed");
512 } else {
513 ERROR_IF(brw_inst_src0_reg_file(devinfo, inst) != BRW_IMMEDIATE_VALUE &&
514 brw_inst_src0_vstride(devinfo, inst) != BRW_VERTICAL_STRIDE_0 &&
515 brw_inst_src0_vstride(devinfo, inst) != BRW_VERTICAL_STRIDE_4,
516 "In Align16 mode, only VertStride of 0 or 4 is allowed");
517 }
518 }
519
520 if (num_sources == 2) {
521 if (devinfo->is_haswell || devinfo->gen >= 8) {
522 ERROR_IF(brw_inst_src1_reg_file(devinfo, inst) != BRW_IMMEDIATE_VALUE &&
523 brw_inst_src1_vstride(devinfo, inst) != BRW_VERTICAL_STRIDE_0 &&
524 brw_inst_src1_vstride(devinfo, inst) != BRW_VERTICAL_STRIDE_2 &&
525 brw_inst_src1_vstride(devinfo, inst) != BRW_VERTICAL_STRIDE_4,
526 "In Align16 mode, only VertStride of 0, 2, or 4 is allowed");
527 } else {
528 ERROR_IF(brw_inst_src1_reg_file(devinfo, inst) != BRW_IMMEDIATE_VALUE &&
529 brw_inst_src1_vstride(devinfo, inst) != BRW_VERTICAL_STRIDE_0 &&
530 brw_inst_src1_vstride(devinfo, inst) != BRW_VERTICAL_STRIDE_4,
531 "In Align16 mode, only VertStride of 0 or 4 is allowed");
532 }
533 }
534
535 return error_msg;
536 }
537
538 for (unsigned i = 0; i < num_sources; i++) {
539 unsigned vstride, width, hstride, element_size, subreg;
540 enum brw_reg_type type;
541
542 #define DO_SRC(n) \
543 if (brw_inst_src ## n ## _reg_file(devinfo, inst) == \
544 BRW_IMMEDIATE_VALUE) \
545 continue; \
546 \
547 vstride = brw_inst_src ## n ## _vstride(devinfo, inst) ? \
548 (1 << (brw_inst_src ## n ## _vstride(devinfo, inst) - 1)) : 0; \
549 width = 1 << brw_inst_src ## n ## _width(devinfo, inst); \
550 hstride = brw_inst_src ## n ## _hstride(devinfo, inst) ? \
551 (1 << (brw_inst_src ## n ## _hstride(devinfo, inst) - 1)) : 0; \
552 type = brw_inst_src ## n ## _type(devinfo, inst); \
553 element_size = brw_reg_type_to_size(type); \
554 subreg = brw_inst_src ## n ## _da1_subreg_nr(devinfo, inst)
555
556 if (i == 0) {
557 DO_SRC(0);
558 } else if (i == 1) {
559 DO_SRC(1);
560 }
561 #undef DO_SRC
562
563 /* On IVB/BYT, region parameters and execution size for DF are in terms of
564 * 32-bit elements, so they are doubled. For evaluating the validity of an
565 * instruction, we halve them.
566 */
567 if (devinfo->gen == 7 && !devinfo->is_haswell &&
568 element_size == 8)
569 element_size = 4;
570
571 /* ExecSize must be greater than or equal to Width. */
572 ERROR_IF(exec_size < width, "ExecSize must be greater than or equal "
573 "to Width");
574
575 /* If ExecSize = Width and HorzStride ≠ 0,
576 * VertStride must be set to Width * HorzStride.
577 */
578 if (exec_size == width && hstride != 0) {
579 ERROR_IF(vstride != width * hstride,
580 "If ExecSize = Width and HorzStride ≠ 0, "
581 "VertStride must be set to Width * HorzStride");
582 }
583
584 /* If Width = 1, HorzStride must be 0 regardless of the values of
585 * ExecSize and VertStride.
586 */
587 if (width == 1) {
588 ERROR_IF(hstride != 0,
589 "If Width = 1, HorzStride must be 0 regardless "
590 "of the values of ExecSize and VertStride");
591 }
592
593 /* If ExecSize = Width = 1, both VertStride and HorzStride must be 0. */
594 if (exec_size == 1 && width == 1) {
595 ERROR_IF(vstride != 0 || hstride != 0,
596 "If ExecSize = Width = 1, both VertStride "
597 "and HorzStride must be 0");
598 }
599
600 /* If VertStride = HorzStride = 0, Width must be 1 regardless of the
601 * value of ExecSize.
602 */
603 if (vstride == 0 && hstride == 0) {
604 ERROR_IF(width != 1,
605 "If VertStride = HorzStride = 0, Width must be "
606 "1 regardless of the value of ExecSize");
607 }
608
609 /* VertStride must be used to cross GRF register boundaries. This rule
610 * implies that elements within a 'Width' cannot cross GRF boundaries.
611 */
612 const uint64_t mask = (1ULL << element_size) - 1;
613 unsigned rowbase = subreg;
614
615 for (int y = 0; y < exec_size / width; y++) {
616 uint64_t access_mask = 0;
617 unsigned offset = rowbase;
618
619 for (int x = 0; x < width; x++) {
620 access_mask |= mask << offset;
621 offset += hstride * element_size;
622 }
623
624 rowbase += vstride * element_size;
625
626 if ((uint32_t)access_mask != 0 && (access_mask >> 32) != 0) {
627 ERROR("VertStride must be used to cross GRF register boundaries");
628 break;
629 }
630 }
631 }
632
633 /* Dst.HorzStride must not be 0. */
634 if (desc->ndst != 0 && !dst_is_null(devinfo, inst)) {
635 ERROR_IF(brw_inst_dst_hstride(devinfo, inst) == BRW_HORIZONTAL_STRIDE_0,
636 "Destination Horizontal Stride must not be 0");
637 }
638
639 return error_msg;
640 }
641
642 /**
643 * Creates an \p access_mask for an \p exec_size, \p element_size, and a region
644 *
645 * An \p access_mask is a 32-element array of uint64_t, where each uint64_t is
646 * a bitmask of bytes accessed by the region.
647 *
648 * For instance the access mask of the source gX.1<4,2,2>F in an exec_size = 4
649 * instruction would be
650 *
651 * access_mask[0] = 0x00000000000000F0
652 * access_mask[1] = 0x000000000000F000
653 * access_mask[2] = 0x0000000000F00000
654 * access_mask[3] = 0x00000000F0000000
655 * access_mask[4-31] = 0
656 *
657 * because the first execution channel accesses bytes 7-4 and the second
658 * execution channel accesses bytes 15-12, etc.
659 */
660 static void
661 align1_access_mask(uint64_t access_mask[static 32],
662 unsigned exec_size, unsigned element_size, unsigned subreg,
663 unsigned vstride, unsigned width, unsigned hstride)
664 {
665 const uint64_t mask = (1ULL << element_size) - 1;
666 unsigned rowbase = subreg;
667 unsigned element = 0;
668
669 for (int y = 0; y < exec_size / width; y++) {
670 unsigned offset = rowbase;
671
672 for (int x = 0; x < width; x++) {
673 access_mask[element++] = mask << offset;
674 offset += hstride * element_size;
675 }
676
677 rowbase += vstride * element_size;
678 }
679
680 assert(element == 0 || element == exec_size);
681 }
682
683 /**
684 * Returns the number of registers accessed according to the \p access_mask
685 */
686 static int
687 registers_read(const uint64_t access_mask[static 32])
688 {
689 int regs_read = 0;
690
691 for (unsigned i = 0; i < 32; i++) {
692 if (access_mask[i] > 0xFFFFFFFF) {
693 return 2;
694 } else if (access_mask[i]) {
695 regs_read = 1;
696 }
697 }
698
699 return regs_read;
700 }
701
702 /**
703 * Checks restrictions listed in "Region Alignment Rules" in the "Register
704 * Region Restrictions" section.
705 */
706 static struct string
707 region_alignment_rules(const struct gen_device_info *devinfo,
708 const brw_inst *inst)
709 {
710 const struct opcode_desc *desc =
711 brw_opcode_desc(devinfo, brw_inst_opcode(devinfo, inst));
712 unsigned num_sources = num_sources_from_inst(devinfo, inst);
713 unsigned exec_size = 1 << brw_inst_exec_size(devinfo, inst);
714 uint64_t dst_access_mask[32], src0_access_mask[32], src1_access_mask[32];
715 struct string error_msg = { .str = NULL, .len = 0 };
716
717 if (num_sources == 3)
718 return (struct string){};
719
720 if (brw_inst_access_mode(devinfo, inst) == BRW_ALIGN_16)
721 return (struct string){};
722
723 if (inst_is_send(devinfo, inst))
724 return (struct string){};
725
726 memset(dst_access_mask, 0, sizeof(dst_access_mask));
727 memset(src0_access_mask, 0, sizeof(src0_access_mask));
728 memset(src1_access_mask, 0, sizeof(src1_access_mask));
729
730 for (unsigned i = 0; i < num_sources; i++) {
731 unsigned vstride, width, hstride, element_size, subreg;
732 enum brw_reg_type type;
733
734 /* In Direct Addressing mode, a source cannot span more than 2 adjacent
735 * GRF registers.
736 */
737
738 #define DO_SRC(n) \
739 if (brw_inst_src ## n ## _address_mode(devinfo, inst) != \
740 BRW_ADDRESS_DIRECT) \
741 continue; \
742 \
743 if (brw_inst_src ## n ## _reg_file(devinfo, inst) == \
744 BRW_IMMEDIATE_VALUE) \
745 continue; \
746 \
747 vstride = brw_inst_src ## n ## _vstride(devinfo, inst) ? \
748 (1 << (brw_inst_src ## n ## _vstride(devinfo, inst) - 1)) : 0; \
749 width = 1 << brw_inst_src ## n ## _width(devinfo, inst); \
750 hstride = brw_inst_src ## n ## _hstride(devinfo, inst) ? \
751 (1 << (brw_inst_src ## n ## _hstride(devinfo, inst) - 1)) : 0; \
752 type = brw_inst_src ## n ## _type(devinfo, inst); \
753 element_size = brw_reg_type_to_size(type); \
754 subreg = brw_inst_src ## n ## _da1_subreg_nr(devinfo, inst); \
755 align1_access_mask(src ## n ## _access_mask, \
756 exec_size, element_size, subreg, \
757 vstride, width, hstride)
758
759 if (i == 0) {
760 DO_SRC(0);
761 } else if (i == 1) {
762 DO_SRC(1);
763 }
764 #undef DO_SRC
765
766 unsigned num_vstride = exec_size / width;
767 unsigned num_hstride = width;
768 unsigned vstride_elements = (num_vstride - 1) * vstride;
769 unsigned hstride_elements = (num_hstride - 1) * hstride;
770 unsigned offset = (vstride_elements + hstride_elements) * element_size +
771 subreg;
772 ERROR_IF(offset >= 64,
773 "A source cannot span more than 2 adjacent GRF registers");
774 }
775
776 if (desc->ndst == 0 || dst_is_null(devinfo, inst))
777 return error_msg;
778
779 unsigned stride = 1 << (brw_inst_dst_hstride(devinfo, inst) - 1);
780 enum brw_reg_type dst_type = brw_inst_dst_type(devinfo, inst);
781 unsigned element_size = brw_reg_type_to_size(dst_type);
782 unsigned subreg = brw_inst_dst_da1_subreg_nr(devinfo, inst);
783 unsigned offset = ((exec_size - 1) * stride * element_size) + subreg;
784 ERROR_IF(offset >= 64,
785 "A destination cannot span more than 2 adjacent GRF registers");
786
787 if (error_msg.str)
788 return error_msg;
789
790 /* On IVB/BYT, region parameters and execution size for DF are in terms of
791 * 32-bit elements, so they are doubled. For evaluating the validity of an
792 * instruction, we halve them.
793 */
794 if (devinfo->gen == 7 && !devinfo->is_haswell &&
795 element_size == 8)
796 element_size = 4;
797
798 align1_access_mask(dst_access_mask, exec_size, element_size, subreg,
799 exec_size == 1 ? 0 : exec_size * stride,
800 exec_size == 1 ? 1 : exec_size,
801 exec_size == 1 ? 0 : stride);
802
803 unsigned dst_regs = registers_read(dst_access_mask);
804 unsigned src0_regs = registers_read(src0_access_mask);
805 unsigned src1_regs = registers_read(src1_access_mask);
806
807 /* The SNB, IVB, HSW, BDW, and CHV PRMs say:
808 *
809 * When an instruction has a source region spanning two registers and a
810 * destination region contained in one register, the number of elements
811 * must be the same between two sources and one of the following must be
812 * true:
813 *
814 * 1. The destination region is entirely contained in the lower OWord
815 * of a register.
816 * 2. The destination region is entirely contained in the upper OWord
817 * of a register.
818 * 3. The destination elements are evenly split between the two OWords
819 * of a register.
820 */
821 if (devinfo->gen <= 8) {
822 if (dst_regs == 1 && (src0_regs == 2 || src1_regs == 2)) {
823 unsigned upper_oword_writes = 0, lower_oword_writes = 0;
824
825 for (unsigned i = 0; i < exec_size; i++) {
826 if (dst_access_mask[i] > 0x0000FFFF) {
827 upper_oword_writes++;
828 } else {
829 assert(dst_access_mask[i] != 0);
830 lower_oword_writes++;
831 }
832 }
833
834 ERROR_IF(lower_oword_writes != 0 &&
835 upper_oword_writes != 0 &&
836 upper_oword_writes != lower_oword_writes,
837 "Writes must be to only one OWord or "
838 "evenly split between OWords");
839 }
840 }
841
842 /* The IVB and HSW PRMs say:
843 *
844 * When an instruction has a source region that spans two registers and
845 * the destination spans two registers, the destination elements must be
846 * evenly split between the two registers [...]
847 *
848 * The SNB PRM contains similar wording (but written in a much more
849 * confusing manner).
850 *
851 * The BDW PRM says:
852 *
853 * When destination spans two registers, the source may be one or two
854 * registers. The destination elements must be evenly split between the
855 * two registers.
856 *
857 * The SKL PRM says:
858 *
859 * When destination of MATH instruction spans two registers, the
860 * destination elements must be evenly split between the two registers.
861 *
862 * It is not known whether this restriction applies to KBL other Gens after
863 * SKL.
864 */
865 if (devinfo->gen <= 8 ||
866 brw_inst_opcode(devinfo, inst) == BRW_OPCODE_MATH) {
867
868 /* Nothing explicitly states that on Gen < 8 elements must be evenly
869 * split between two destination registers in the two exceptional
870 * source-region-spans-one-register cases, but since Broadwell requires
871 * evenly split writes regardless of source region, we assume that it was
872 * an oversight and require it.
873 */
874 if (dst_regs == 2) {
875 unsigned upper_reg_writes = 0, lower_reg_writes = 0;
876
877 for (unsigned i = 0; i < exec_size; i++) {
878 if (dst_access_mask[i] > 0xFFFFFFFF) {
879 upper_reg_writes++;
880 } else {
881 assert(dst_access_mask[i] != 0);
882 lower_reg_writes++;
883 }
884 }
885
886 ERROR_IF(upper_reg_writes != lower_reg_writes,
887 "Writes must be evenly split between the two "
888 "destination registers");
889 }
890 }
891
892 /* The IVB and HSW PRMs say:
893 *
894 * When an instruction has a source region that spans two registers and
895 * the destination spans two registers, the destination elements must be
896 * evenly split between the two registers and each destination register
897 * must be entirely derived from one source register.
898 *
899 * Note: In such cases, the regioning parameters must ensure that the
900 * offset from the two source registers is the same.
901 *
902 * The SNB PRM contains similar wording (but written in a much more
903 * confusing manner).
904 *
905 * There are effectively three rules stated here:
906 *
907 * For an instruction with a source and a destination spanning two
908 * registers,
909 *
910 * (1) destination elements must be evenly split between the two
911 * registers
912 * (2) all destination elements in a register must be derived
913 * from one source register
914 * (3) the offset (i.e. the starting location in each of the two
915 * registers spanned by a region) must be the same in the two
916 * registers spanned by a region
917 *
918 * It is impossible to violate rule (1) without violating (2) or (3), so we
919 * do not attempt to validate it.
920 */
921 if (devinfo->gen <= 7 && dst_regs == 2) {
922 for (unsigned i = 0; i < num_sources; i++) {
923 #define DO_SRC(n) \
924 if (src ## n ## _regs <= 1) \
925 continue; \
926 \
927 for (unsigned i = 0; i < exec_size; i++) { \
928 if ((dst_access_mask[i] > 0xFFFFFFFF) != \
929 (src ## n ## _access_mask[i] > 0xFFFFFFFF)) { \
930 ERROR("Each destination register must be entirely derived " \
931 "from one source register"); \
932 break; \
933 } \
934 } \
935 \
936 unsigned offset_0 = \
937 brw_inst_src ## n ## _da1_subreg_nr(devinfo, inst); \
938 unsigned offset_1 = offset_0; \
939 \
940 for (unsigned i = 0; i < exec_size; i++) { \
941 if (src ## n ## _access_mask[i] > 0xFFFFFFFF) { \
942 offset_1 = __builtin_ctzll(src ## n ## _access_mask[i]) - 32; \
943 break; \
944 } \
945 } \
946 \
947 ERROR_IF(offset_0 != offset_1, \
948 "The offset from the two source registers " \
949 "must be the same")
950
951 if (i == 0) {
952 DO_SRC(0);
953 } else if (i == 1) {
954 DO_SRC(1);
955 }
956 #undef DO_SRC
957 }
958 }
959
960 /* The IVB and HSW PRMs say:
961 *
962 * When destination spans two registers, the source MUST span two
963 * registers. The exception to the above rule:
964 * 1. When source is scalar, the source registers are not
965 * incremented.
966 * 2. When source is packed integer Word and destination is packed
967 * integer DWord, the source register is not incremented by the
968 * source sub register is incremented.
969 *
970 * The SNB PRM does not contain this rule, but the internal documentation
971 * indicates that it applies to SNB as well. We assume that the rule applies
972 * to Gen <= 5 although their PRMs do not state it.
973 *
974 * While the documentation explicitly says in exception (2) that the
975 * destination must be an integer DWord, the hardware allows at least a
976 * float destination type as well. We emit such instructions from
977 *
978 * fs_visitor::emit_interpolation_setup_gen6
979 * fs_visitor::emit_fragcoord_interpolation
980 *
981 * and have for years with no ill effects.
982 *
983 * Additionally the simulator source code indicates that the real condition
984 * is that the size of the destination type is 4 bytes.
985 */
986 if (devinfo->gen <= 7 && dst_regs == 2) {
987 enum brw_reg_type dst_type = brw_inst_dst_type(devinfo, inst);
988 bool dst_is_packed_dword =
989 is_packed(exec_size * stride, exec_size, stride) &&
990 brw_reg_type_to_size(dst_type) == 4;
991
992 for (unsigned i = 0; i < num_sources; i++) {
993 #define DO_SRC(n) \
994 unsigned vstride, width, hstride; \
995 vstride = brw_inst_src ## n ## _vstride(devinfo, inst) ? \
996 (1 << (brw_inst_src ## n ## _vstride(devinfo, inst) - 1)) : 0; \
997 width = 1 << brw_inst_src ## n ## _width(devinfo, inst); \
998 hstride = brw_inst_src ## n ## _hstride(devinfo, inst) ? \
999 (1 << (brw_inst_src ## n ## _hstride(devinfo, inst) - 1)) : 0; \
1000 bool src ## n ## _is_packed_word = \
1001 is_packed(vstride, width, hstride) && \
1002 (brw_inst_src ## n ## _type(devinfo, inst) == BRW_REGISTER_TYPE_W || \
1003 brw_inst_src ## n ## _type(devinfo, inst) == BRW_REGISTER_TYPE_UW); \
1004 \
1005 ERROR_IF(src ## n ## _regs == 1 && \
1006 !src ## n ## _has_scalar_region(devinfo, inst) && \
1007 !(dst_is_packed_dword && src ## n ## _is_packed_word), \
1008 "When the destination spans two registers, the source must " \
1009 "span two registers\n" ERROR_INDENT "(exceptions for scalar " \
1010 "source and packed-word to packed-dword expansion)")
1011
1012 if (i == 0) {
1013 DO_SRC(0);
1014 } else if (i == 1) {
1015 DO_SRC(1);
1016 }
1017 #undef DO_SRC
1018 }
1019 }
1020
1021 return error_msg;
1022 }
1023
1024 static struct string
1025 vector_immediate_restrictions(const struct gen_device_info *devinfo,
1026 const brw_inst *inst)
1027 {
1028 unsigned num_sources = num_sources_from_inst(devinfo, inst);
1029 struct string error_msg = { .str = NULL, .len = 0 };
1030
1031 if (num_sources == 3 || num_sources == 0)
1032 return (struct string){};
1033
1034 unsigned file = num_sources == 1 ?
1035 brw_inst_src0_reg_file(devinfo, inst) :
1036 brw_inst_src1_reg_file(devinfo, inst);
1037 if (file != BRW_IMMEDIATE_VALUE)
1038 return (struct string){};
1039
1040 enum brw_reg_type dst_type = brw_inst_dst_type(devinfo, inst);
1041 unsigned dst_type_size = brw_reg_type_to_size(dst_type);
1042 unsigned dst_subreg = brw_inst_access_mode(devinfo, inst) == BRW_ALIGN_1 ?
1043 brw_inst_dst_da1_subreg_nr(devinfo, inst) : 0;
1044 unsigned dst_stride = 1 << (brw_inst_dst_hstride(devinfo, inst) - 1);
1045 enum brw_reg_type type = num_sources == 1 ?
1046 brw_inst_src0_type(devinfo, inst) :
1047 brw_inst_src1_type(devinfo, inst);
1048
1049 /* The PRMs say:
1050 *
1051 * When an immediate vector is used in an instruction, the destination
1052 * must be 128-bit aligned with destination horizontal stride equivalent
1053 * to a word for an immediate integer vector (v) and equivalent to a
1054 * DWord for an immediate float vector (vf).
1055 *
1056 * The text has not been updated for the addition of the immediate unsigned
1057 * integer vector type (uv) on SNB, but presumably the same restriction
1058 * applies.
1059 */
1060 switch (type) {
1061 case BRW_REGISTER_TYPE_V:
1062 case BRW_REGISTER_TYPE_UV:
1063 case BRW_REGISTER_TYPE_VF:
1064 ERROR_IF(dst_subreg % (128 / 8) != 0,
1065 "Destination must be 128-bit aligned in order to use immediate "
1066 "vector types");
1067
1068 if (type == BRW_REGISTER_TYPE_VF) {
1069 ERROR_IF(dst_type_size * dst_stride != 4,
1070 "Destination must have stride equivalent to dword in order "
1071 "to use the VF type");
1072 } else {
1073 ERROR_IF(dst_type_size * dst_stride != 2,
1074 "Destination must have stride equivalent to word in order "
1075 "to use the V or UV type");
1076 }
1077 break;
1078 default:
1079 break;
1080 }
1081
1082 return error_msg;
1083 }
1084
1085 bool
1086 brw_validate_instructions(const struct gen_device_info *devinfo,
1087 void *assembly, int start_offset, int end_offset,
1088 struct annotation_info *annotation)
1089 {
1090 bool valid = true;
1091
1092 for (int src_offset = start_offset; src_offset < end_offset;) {
1093 struct string error_msg = { .str = NULL, .len = 0 };
1094 const brw_inst *inst = assembly + src_offset;
1095 bool is_compact = brw_inst_cmpt_control(devinfo, inst);
1096 brw_inst uncompacted;
1097
1098 if (is_compact) {
1099 brw_compact_inst *compacted = (void *)inst;
1100 brw_uncompact_instruction(devinfo, &uncompacted, compacted);
1101 inst = &uncompacted;
1102 }
1103
1104 if (is_unsupported_inst(devinfo, inst)) {
1105 ERROR("Instruction not supported on this Gen");
1106 } else {
1107 CHECK(sources_not_null);
1108 CHECK(send_restrictions);
1109 CHECK(general_restrictions_based_on_operand_types);
1110 CHECK(general_restrictions_on_region_parameters);
1111 CHECK(region_alignment_rules);
1112 CHECK(vector_immediate_restrictions);
1113 }
1114
1115 if (error_msg.str && annotation) {
1116 annotation_insert_error(annotation, src_offset, error_msg.str);
1117 }
1118 valid = valid && error_msg.len == 0;
1119 free(error_msg.str);
1120
1121 if (is_compact) {
1122 src_offset += sizeof(brw_compact_inst);
1123 } else {
1124 src_offset += sizeof(brw_inst);
1125 }
1126 }
1127
1128 return valid;
1129 }