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