intel/fs: Fix unused texture coordinate zeroing on Gen4-5
[mesa.git] / src / intel / compiler / brw_fs.cpp
1 /*
2 * Copyright © 2010 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_fs.cpp
25 *
26 * This file drives the GLSL IR -> LIR translation, contains the
27 * optimizations on the LIR, and drives the generation of native code
28 * from the LIR.
29 */
30
31 #include "main/macros.h"
32 #include "brw_eu.h"
33 #include "brw_fs.h"
34 #include "brw_fs_live_variables.h"
35 #include "brw_nir.h"
36 #include "brw_vec4_gs_visitor.h"
37 #include "brw_cfg.h"
38 #include "brw_dead_control_flow.h"
39 #include "dev/gen_debug.h"
40 #include "compiler/glsl_types.h"
41 #include "compiler/nir/nir_builder.h"
42 #include "program/prog_parameter.h"
43 #include "util/u_math.h"
44
45 using namespace brw;
46
47 static unsigned get_lowered_simd_width(const struct gen_device_info *devinfo,
48 const fs_inst *inst);
49
50 void
51 fs_inst::init(enum opcode opcode, uint8_t exec_size, const fs_reg &dst,
52 const fs_reg *src, unsigned sources)
53 {
54 memset((void*)this, 0, sizeof(*this));
55
56 this->src = new fs_reg[MAX2(sources, 3)];
57 for (unsigned i = 0; i < sources; i++)
58 this->src[i] = src[i];
59
60 this->opcode = opcode;
61 this->dst = dst;
62 this->sources = sources;
63 this->exec_size = exec_size;
64 this->base_mrf = -1;
65
66 assert(dst.file != IMM && dst.file != UNIFORM);
67
68 assert(this->exec_size != 0);
69
70 this->conditional_mod = BRW_CONDITIONAL_NONE;
71
72 /* This will be the case for almost all instructions. */
73 switch (dst.file) {
74 case VGRF:
75 case ARF:
76 case FIXED_GRF:
77 case MRF:
78 case ATTR:
79 this->size_written = dst.component_size(exec_size);
80 break;
81 case BAD_FILE:
82 this->size_written = 0;
83 break;
84 case IMM:
85 case UNIFORM:
86 unreachable("Invalid destination register file");
87 }
88
89 this->writes_accumulator = false;
90 }
91
92 fs_inst::fs_inst()
93 {
94 init(BRW_OPCODE_NOP, 8, dst, NULL, 0);
95 }
96
97 fs_inst::fs_inst(enum opcode opcode, uint8_t exec_size)
98 {
99 init(opcode, exec_size, reg_undef, NULL, 0);
100 }
101
102 fs_inst::fs_inst(enum opcode opcode, uint8_t exec_size, const fs_reg &dst)
103 {
104 init(opcode, exec_size, dst, NULL, 0);
105 }
106
107 fs_inst::fs_inst(enum opcode opcode, uint8_t exec_size, const fs_reg &dst,
108 const fs_reg &src0)
109 {
110 const fs_reg src[1] = { src0 };
111 init(opcode, exec_size, dst, src, 1);
112 }
113
114 fs_inst::fs_inst(enum opcode opcode, uint8_t exec_size, const fs_reg &dst,
115 const fs_reg &src0, const fs_reg &src1)
116 {
117 const fs_reg src[2] = { src0, src1 };
118 init(opcode, exec_size, dst, src, 2);
119 }
120
121 fs_inst::fs_inst(enum opcode opcode, uint8_t exec_size, const fs_reg &dst,
122 const fs_reg &src0, const fs_reg &src1, const fs_reg &src2)
123 {
124 const fs_reg src[3] = { src0, src1, src2 };
125 init(opcode, exec_size, dst, src, 3);
126 }
127
128 fs_inst::fs_inst(enum opcode opcode, uint8_t exec_width, const fs_reg &dst,
129 const fs_reg src[], unsigned sources)
130 {
131 init(opcode, exec_width, dst, src, sources);
132 }
133
134 fs_inst::fs_inst(const fs_inst &that)
135 {
136 memcpy((void*)this, &that, sizeof(that));
137
138 this->src = new fs_reg[MAX2(that.sources, 3)];
139
140 for (unsigned i = 0; i < that.sources; i++)
141 this->src[i] = that.src[i];
142 }
143
144 fs_inst::~fs_inst()
145 {
146 delete[] this->src;
147 }
148
149 void
150 fs_inst::resize_sources(uint8_t num_sources)
151 {
152 if (this->sources != num_sources) {
153 fs_reg *src = new fs_reg[MAX2(num_sources, 3)];
154
155 for (unsigned i = 0; i < MIN2(this->sources, num_sources); ++i)
156 src[i] = this->src[i];
157
158 delete[] this->src;
159 this->src = src;
160 this->sources = num_sources;
161 }
162 }
163
164 void
165 fs_visitor::VARYING_PULL_CONSTANT_LOAD(const fs_builder &bld,
166 const fs_reg &dst,
167 const fs_reg &surf_index,
168 const fs_reg &varying_offset,
169 uint32_t const_offset)
170 {
171 /* We have our constant surface use a pitch of 4 bytes, so our index can
172 * be any component of a vector, and then we load 4 contiguous
173 * components starting from that.
174 *
175 * We break down the const_offset to a portion added to the variable offset
176 * and a portion done using fs_reg::offset, which means that if you have
177 * GLSL using something like "uniform vec4 a[20]; gl_FragColor = a[i]",
178 * we'll temporarily generate 4 vec4 loads from offset i * 4, and CSE can
179 * later notice that those loads are all the same and eliminate the
180 * redundant ones.
181 */
182 fs_reg vec4_offset = vgrf(glsl_type::uint_type);
183 bld.ADD(vec4_offset, varying_offset, brw_imm_ud(const_offset & ~0xf));
184
185 /* The pull load message will load a vec4 (16 bytes). If we are loading
186 * a double this means we are only loading 2 elements worth of data.
187 * We also want to use a 32-bit data type for the dst of the load operation
188 * so other parts of the driver don't get confused about the size of the
189 * result.
190 */
191 fs_reg vec4_result = bld.vgrf(BRW_REGISTER_TYPE_F, 4);
192 fs_inst *inst = bld.emit(FS_OPCODE_VARYING_PULL_CONSTANT_LOAD_LOGICAL,
193 vec4_result, surf_index, vec4_offset);
194 inst->size_written = 4 * vec4_result.component_size(inst->exec_size);
195
196 shuffle_from_32bit_read(bld, dst, vec4_result,
197 (const_offset & 0xf) / type_sz(dst.type), 1);
198 }
199
200 /**
201 * A helper for MOV generation for fixing up broken hardware SEND dependency
202 * handling.
203 */
204 void
205 fs_visitor::DEP_RESOLVE_MOV(const fs_builder &bld, int grf)
206 {
207 /* The caller always wants uncompressed to emit the minimal extra
208 * dependencies, and to avoid having to deal with aligning its regs to 2.
209 */
210 const fs_builder ubld = bld.annotate("send dependency resolve")
211 .quarter(0);
212
213 ubld.MOV(ubld.null_reg_f(), fs_reg(VGRF, grf, BRW_REGISTER_TYPE_F));
214 }
215
216 bool
217 fs_inst::is_send_from_grf() const
218 {
219 switch (opcode) {
220 case SHADER_OPCODE_SEND:
221 case SHADER_OPCODE_SHADER_TIME_ADD:
222 case FS_OPCODE_INTERPOLATE_AT_SAMPLE:
223 case FS_OPCODE_INTERPOLATE_AT_SHARED_OFFSET:
224 case FS_OPCODE_INTERPOLATE_AT_PER_SLOT_OFFSET:
225 case SHADER_OPCODE_URB_WRITE_SIMD8:
226 case SHADER_OPCODE_URB_WRITE_SIMD8_PER_SLOT:
227 case SHADER_OPCODE_URB_WRITE_SIMD8_MASKED:
228 case SHADER_OPCODE_URB_WRITE_SIMD8_MASKED_PER_SLOT:
229 case SHADER_OPCODE_URB_READ_SIMD8:
230 case SHADER_OPCODE_URB_READ_SIMD8_PER_SLOT:
231 case SHADER_OPCODE_INTERLOCK:
232 case SHADER_OPCODE_MEMORY_FENCE:
233 case SHADER_OPCODE_BARRIER:
234 return true;
235 case FS_OPCODE_UNIFORM_PULL_CONSTANT_LOAD:
236 return src[1].file == VGRF;
237 case FS_OPCODE_FB_WRITE:
238 case FS_OPCODE_FB_READ:
239 return src[0].file == VGRF;
240 default:
241 if (is_tex())
242 return src[0].file == VGRF;
243
244 return false;
245 }
246 }
247
248 bool
249 fs_inst::is_control_source(unsigned arg) const
250 {
251 switch (opcode) {
252 case FS_OPCODE_UNIFORM_PULL_CONSTANT_LOAD:
253 case FS_OPCODE_UNIFORM_PULL_CONSTANT_LOAD_GEN7:
254 case FS_OPCODE_VARYING_PULL_CONSTANT_LOAD_GEN4:
255 return arg == 0;
256
257 case SHADER_OPCODE_BROADCAST:
258 case SHADER_OPCODE_SHUFFLE:
259 case SHADER_OPCODE_QUAD_SWIZZLE:
260 case FS_OPCODE_INTERPOLATE_AT_SAMPLE:
261 case FS_OPCODE_INTERPOLATE_AT_SHARED_OFFSET:
262 case FS_OPCODE_INTERPOLATE_AT_PER_SLOT_OFFSET:
263 case SHADER_OPCODE_GET_BUFFER_SIZE:
264 return arg == 1;
265
266 case SHADER_OPCODE_MOV_INDIRECT:
267 case SHADER_OPCODE_CLUSTER_BROADCAST:
268 case SHADER_OPCODE_TEX:
269 case FS_OPCODE_TXB:
270 case SHADER_OPCODE_TXD:
271 case SHADER_OPCODE_TXF:
272 case SHADER_OPCODE_TXF_LZ:
273 case SHADER_OPCODE_TXF_CMS:
274 case SHADER_OPCODE_TXF_CMS_W:
275 case SHADER_OPCODE_TXF_UMS:
276 case SHADER_OPCODE_TXF_MCS:
277 case SHADER_OPCODE_TXL:
278 case SHADER_OPCODE_TXL_LZ:
279 case SHADER_OPCODE_TXS:
280 case SHADER_OPCODE_LOD:
281 case SHADER_OPCODE_TG4:
282 case SHADER_OPCODE_TG4_OFFSET:
283 case SHADER_OPCODE_SAMPLEINFO:
284 return arg == 1 || arg == 2;
285
286 case SHADER_OPCODE_SEND:
287 return arg == 0 || arg == 1;
288
289 default:
290 return false;
291 }
292 }
293
294 bool
295 fs_inst::is_payload(unsigned arg) const
296 {
297 switch (opcode) {
298 case FS_OPCODE_FB_WRITE:
299 case FS_OPCODE_FB_READ:
300 case SHADER_OPCODE_URB_WRITE_SIMD8:
301 case SHADER_OPCODE_URB_WRITE_SIMD8_PER_SLOT:
302 case SHADER_OPCODE_URB_WRITE_SIMD8_MASKED:
303 case SHADER_OPCODE_URB_WRITE_SIMD8_MASKED_PER_SLOT:
304 case SHADER_OPCODE_URB_READ_SIMD8:
305 case SHADER_OPCODE_URB_READ_SIMD8_PER_SLOT:
306 case VEC4_OPCODE_UNTYPED_ATOMIC:
307 case VEC4_OPCODE_UNTYPED_SURFACE_READ:
308 case VEC4_OPCODE_UNTYPED_SURFACE_WRITE:
309 case FS_OPCODE_INTERPOLATE_AT_PER_SLOT_OFFSET:
310 case SHADER_OPCODE_SHADER_TIME_ADD:
311 case FS_OPCODE_INTERPOLATE_AT_SAMPLE:
312 case FS_OPCODE_INTERPOLATE_AT_SHARED_OFFSET:
313 case SHADER_OPCODE_INTERLOCK:
314 case SHADER_OPCODE_MEMORY_FENCE:
315 case SHADER_OPCODE_BARRIER:
316 return arg == 0;
317
318 case FS_OPCODE_UNIFORM_PULL_CONSTANT_LOAD_GEN7:
319 return arg == 1;
320
321 case SHADER_OPCODE_SEND:
322 return arg == 2 || arg == 3;
323
324 default:
325 if (is_tex())
326 return arg == 0;
327 else
328 return false;
329 }
330 }
331
332 /**
333 * Returns true if this instruction's sources and destinations cannot
334 * safely be the same register.
335 *
336 * In most cases, a register can be written over safely by the same
337 * instruction that is its last use. For a single instruction, the
338 * sources are dereferenced before writing of the destination starts
339 * (naturally).
340 *
341 * However, there are a few cases where this can be problematic:
342 *
343 * - Virtual opcodes that translate to multiple instructions in the
344 * code generator: if src == dst and one instruction writes the
345 * destination before a later instruction reads the source, then
346 * src will have been clobbered.
347 *
348 * - SIMD16 compressed instructions with certain regioning (see below).
349 *
350 * The register allocator uses this information to set up conflicts between
351 * GRF sources and the destination.
352 */
353 bool
354 fs_inst::has_source_and_destination_hazard() const
355 {
356 switch (opcode) {
357 case FS_OPCODE_PACK_HALF_2x16_SPLIT:
358 /* Multiple partial writes to the destination */
359 return true;
360 case SHADER_OPCODE_SHUFFLE:
361 /* This instruction returns an arbitrary channel from the source and
362 * gets split into smaller instructions in the generator. It's possible
363 * that one of the instructions will read from a channel corresponding
364 * to an earlier instruction.
365 */
366 case SHADER_OPCODE_SEL_EXEC:
367 /* This is implemented as
368 *
369 * mov(16) g4<1>D 0D { align1 WE_all 1H };
370 * mov(16) g4<1>D g5<8,8,1>D { align1 1H }
371 *
372 * Because the source is only read in the second instruction, the first
373 * may stomp all over it.
374 */
375 return true;
376 case SHADER_OPCODE_QUAD_SWIZZLE:
377 switch (src[1].ud) {
378 case BRW_SWIZZLE_XXXX:
379 case BRW_SWIZZLE_YYYY:
380 case BRW_SWIZZLE_ZZZZ:
381 case BRW_SWIZZLE_WWWW:
382 case BRW_SWIZZLE_XXZZ:
383 case BRW_SWIZZLE_YYWW:
384 case BRW_SWIZZLE_XYXY:
385 case BRW_SWIZZLE_ZWZW:
386 /* These can be implemented as a single Align1 region on all
387 * platforms, so there's never a hazard between source and
388 * destination. C.f. fs_generator::generate_quad_swizzle().
389 */
390 return false;
391 default:
392 return !is_uniform(src[0]);
393 }
394 default:
395 /* The SIMD16 compressed instruction
396 *
397 * add(16) g4<1>F g4<8,8,1>F g6<8,8,1>F
398 *
399 * is actually decoded in hardware as:
400 *
401 * add(8) g4<1>F g4<8,8,1>F g6<8,8,1>F
402 * add(8) g5<1>F g5<8,8,1>F g7<8,8,1>F
403 *
404 * Which is safe. However, if we have uniform accesses
405 * happening, we get into trouble:
406 *
407 * add(8) g4<1>F g4<0,1,0>F g6<8,8,1>F
408 * add(8) g5<1>F g4<0,1,0>F g7<8,8,1>F
409 *
410 * Now our destination for the first instruction overwrote the
411 * second instruction's src0, and we get garbage for those 8
412 * pixels. There's a similar issue for the pre-gen6
413 * pixel_x/pixel_y, which are registers of 16-bit values and thus
414 * would get stomped by the first decode as well.
415 */
416 if (exec_size == 16) {
417 for (int i = 0; i < sources; i++) {
418 if (src[i].file == VGRF && (src[i].stride == 0 ||
419 src[i].type == BRW_REGISTER_TYPE_UW ||
420 src[i].type == BRW_REGISTER_TYPE_W ||
421 src[i].type == BRW_REGISTER_TYPE_UB ||
422 src[i].type == BRW_REGISTER_TYPE_B)) {
423 return true;
424 }
425 }
426 }
427 return false;
428 }
429 }
430
431 bool
432 fs_inst::can_do_source_mods(const struct gen_device_info *devinfo) const
433 {
434 if (devinfo->gen == 6 && is_math())
435 return false;
436
437 if (is_send_from_grf())
438 return false;
439
440 /* From GEN:BUG:1604601757:
441 *
442 * "When multiplying a DW and any lower precision integer, source modifier
443 * is not supported."
444 */
445 if (devinfo->gen >= 12 && (opcode == BRW_OPCODE_MUL ||
446 opcode == BRW_OPCODE_MAD)) {
447 const brw_reg_type exec_type = get_exec_type(this);
448 const unsigned min_type_sz = opcode == BRW_OPCODE_MAD ?
449 MIN2(type_sz(src[1].type), type_sz(src[2].type)) :
450 MIN2(type_sz(src[0].type), type_sz(src[1].type));
451
452 if (brw_reg_type_is_integer(exec_type) &&
453 type_sz(exec_type) >= 4 &&
454 type_sz(exec_type) != min_type_sz)
455 return false;
456 }
457
458 if (!backend_instruction::can_do_source_mods())
459 return false;
460
461 return true;
462 }
463
464 bool
465 fs_inst::can_do_cmod()
466 {
467 if (!backend_instruction::can_do_cmod())
468 return false;
469
470 /* The accumulator result appears to get used for the conditional modifier
471 * generation. When negating a UD value, there is a 33rd bit generated for
472 * the sign in the accumulator value, so now you can't check, for example,
473 * equality with a 32-bit value. See piglit fs-op-neg-uvec4.
474 */
475 for (unsigned i = 0; i < sources; i++) {
476 if (type_is_unsigned_int(src[i].type) && src[i].negate)
477 return false;
478 }
479
480 return true;
481 }
482
483 bool
484 fs_inst::can_change_types() const
485 {
486 return dst.type == src[0].type &&
487 !src[0].abs && !src[0].negate && !saturate &&
488 (opcode == BRW_OPCODE_MOV ||
489 (opcode == BRW_OPCODE_SEL &&
490 dst.type == src[1].type &&
491 predicate != BRW_PREDICATE_NONE &&
492 !src[1].abs && !src[1].negate));
493 }
494
495 void
496 fs_reg::init()
497 {
498 memset((void*)this, 0, sizeof(*this));
499 type = BRW_REGISTER_TYPE_UD;
500 stride = 1;
501 }
502
503 /** Generic unset register constructor. */
504 fs_reg::fs_reg()
505 {
506 init();
507 this->file = BAD_FILE;
508 }
509
510 fs_reg::fs_reg(struct ::brw_reg reg) :
511 backend_reg(reg)
512 {
513 this->offset = 0;
514 this->stride = 1;
515 if (this->file == IMM &&
516 (this->type != BRW_REGISTER_TYPE_V &&
517 this->type != BRW_REGISTER_TYPE_UV &&
518 this->type != BRW_REGISTER_TYPE_VF)) {
519 this->stride = 0;
520 }
521 }
522
523 bool
524 fs_reg::equals(const fs_reg &r) const
525 {
526 return (this->backend_reg::equals(r) &&
527 stride == r.stride);
528 }
529
530 bool
531 fs_reg::negative_equals(const fs_reg &r) const
532 {
533 return (this->backend_reg::negative_equals(r) &&
534 stride == r.stride);
535 }
536
537 bool
538 fs_reg::is_contiguous() const
539 {
540 switch (file) {
541 case ARF:
542 case FIXED_GRF:
543 return hstride == BRW_HORIZONTAL_STRIDE_1 &&
544 vstride == width + hstride;
545 case MRF:
546 case VGRF:
547 case ATTR:
548 return stride == 1;
549 case UNIFORM:
550 case IMM:
551 case BAD_FILE:
552 return true;
553 }
554
555 unreachable("Invalid register file");
556 }
557
558 unsigned
559 fs_reg::component_size(unsigned width) const
560 {
561 const unsigned stride = ((file != ARF && file != FIXED_GRF) ? this->stride :
562 hstride == 0 ? 0 :
563 1 << (hstride - 1));
564 return MAX2(width * stride, 1) * type_sz(type);
565 }
566
567 /**
568 * Create a MOV to read the timestamp register.
569 */
570 fs_reg
571 fs_visitor::get_timestamp(const fs_builder &bld)
572 {
573 assert(devinfo->gen >= 7);
574
575 fs_reg ts = fs_reg(retype(brw_vec4_reg(BRW_ARCHITECTURE_REGISTER_FILE,
576 BRW_ARF_TIMESTAMP,
577 0),
578 BRW_REGISTER_TYPE_UD));
579
580 fs_reg dst = fs_reg(VGRF, alloc.allocate(1), BRW_REGISTER_TYPE_UD);
581
582 /* We want to read the 3 fields we care about even if it's not enabled in
583 * the dispatch.
584 */
585 bld.group(4, 0).exec_all().MOV(dst, ts);
586
587 return dst;
588 }
589
590 void
591 fs_visitor::emit_shader_time_begin()
592 {
593 /* We want only the low 32 bits of the timestamp. Since it's running
594 * at the GPU clock rate of ~1.2ghz, it will roll over every ~3 seconds,
595 * which is plenty of time for our purposes. It is identical across the
596 * EUs, but since it's tracking GPU core speed it will increment at a
597 * varying rate as render P-states change.
598 */
599 shader_start_time = component(
600 get_timestamp(bld.annotate("shader time start")), 0);
601 }
602
603 void
604 fs_visitor::emit_shader_time_end()
605 {
606 /* Insert our code just before the final SEND with EOT. */
607 exec_node *end = this->instructions.get_tail();
608 assert(end && ((fs_inst *) end)->eot);
609 const fs_builder ibld = bld.annotate("shader time end")
610 .exec_all().at(NULL, end);
611 const fs_reg timestamp = get_timestamp(ibld);
612
613 /* We only use the low 32 bits of the timestamp - see
614 * emit_shader_time_begin()).
615 *
616 * We could also check if render P-states have changed (or anything
617 * else that might disrupt timing) by setting smear to 2 and checking if
618 * that field is != 0.
619 */
620 const fs_reg shader_end_time = component(timestamp, 0);
621
622 /* Check that there weren't any timestamp reset events (assuming these
623 * were the only two timestamp reads that happened).
624 */
625 const fs_reg reset = component(timestamp, 2);
626 set_condmod(BRW_CONDITIONAL_Z,
627 ibld.AND(ibld.null_reg_ud(), reset, brw_imm_ud(1u)));
628 ibld.IF(BRW_PREDICATE_NORMAL);
629
630 fs_reg start = shader_start_time;
631 start.negate = true;
632 const fs_reg diff = component(fs_reg(VGRF, alloc.allocate(1),
633 BRW_REGISTER_TYPE_UD),
634 0);
635 const fs_builder cbld = ibld.group(1, 0);
636 cbld.group(1, 0).ADD(diff, start, shader_end_time);
637
638 /* If there were no instructions between the two timestamp gets, the diff
639 * is 2 cycles. Remove that overhead, so I can forget about that when
640 * trying to determine the time taken for single instructions.
641 */
642 cbld.ADD(diff, diff, brw_imm_ud(-2u));
643 SHADER_TIME_ADD(cbld, 0, diff);
644 SHADER_TIME_ADD(cbld, 1, brw_imm_ud(1u));
645 ibld.emit(BRW_OPCODE_ELSE);
646 SHADER_TIME_ADD(cbld, 2, brw_imm_ud(1u));
647 ibld.emit(BRW_OPCODE_ENDIF);
648 }
649
650 void
651 fs_visitor::SHADER_TIME_ADD(const fs_builder &bld,
652 int shader_time_subindex,
653 fs_reg value)
654 {
655 int index = shader_time_index * 3 + shader_time_subindex;
656 struct brw_reg offset = brw_imm_d(index * BRW_SHADER_TIME_STRIDE);
657
658 fs_reg payload;
659 if (dispatch_width == 8)
660 payload = vgrf(glsl_type::uvec2_type);
661 else
662 payload = vgrf(glsl_type::uint_type);
663
664 bld.emit(SHADER_OPCODE_SHADER_TIME_ADD, fs_reg(), payload, offset, value);
665 }
666
667 void
668 fs_visitor::vfail(const char *format, va_list va)
669 {
670 char *msg;
671
672 if (failed)
673 return;
674
675 failed = true;
676
677 msg = ralloc_vasprintf(mem_ctx, format, va);
678 msg = ralloc_asprintf(mem_ctx, "%s compile failed: %s\n", stage_abbrev, msg);
679
680 this->fail_msg = msg;
681
682 if (debug_enabled) {
683 fprintf(stderr, "%s", msg);
684 }
685 }
686
687 void
688 fs_visitor::fail(const char *format, ...)
689 {
690 va_list va;
691
692 va_start(va, format);
693 vfail(format, va);
694 va_end(va);
695 }
696
697 /**
698 * Mark this program as impossible to compile with dispatch width greater
699 * than n.
700 *
701 * During the SIMD8 compile (which happens first), we can detect and flag
702 * things that are unsupported in SIMD16+ mode, so the compiler can skip the
703 * SIMD16+ compile altogether.
704 *
705 * During a compile of dispatch width greater than n (if one happens anyway),
706 * this just calls fail().
707 */
708 void
709 fs_visitor::limit_dispatch_width(unsigned n, const char *msg)
710 {
711 if (dispatch_width > n) {
712 fail("%s", msg);
713 } else {
714 max_dispatch_width = n;
715 compiler->shader_perf_log(log_data,
716 "Shader dispatch width limited to SIMD%d: %s",
717 n, msg);
718 }
719 }
720
721 /**
722 * Returns true if the instruction has a flag that means it won't
723 * update an entire destination register.
724 *
725 * For example, dead code elimination and live variable analysis want to know
726 * when a write to a variable screens off any preceding values that were in
727 * it.
728 */
729 bool
730 fs_inst::is_partial_write() const
731 {
732 return ((this->predicate && this->opcode != BRW_OPCODE_SEL) ||
733 (this->exec_size * type_sz(this->dst.type)) < 32 ||
734 !this->dst.is_contiguous() ||
735 this->dst.offset % REG_SIZE != 0);
736 }
737
738 unsigned
739 fs_inst::components_read(unsigned i) const
740 {
741 /* Return zero if the source is not present. */
742 if (src[i].file == BAD_FILE)
743 return 0;
744
745 switch (opcode) {
746 case FS_OPCODE_LINTERP:
747 if (i == 0)
748 return 2;
749 else
750 return 1;
751
752 case FS_OPCODE_PIXEL_X:
753 case FS_OPCODE_PIXEL_Y:
754 assert(i == 0);
755 return 2;
756
757 case FS_OPCODE_FB_WRITE_LOGICAL:
758 assert(src[FB_WRITE_LOGICAL_SRC_COMPONENTS].file == IMM);
759 /* First/second FB write color. */
760 if (i < 2)
761 return src[FB_WRITE_LOGICAL_SRC_COMPONENTS].ud;
762 else
763 return 1;
764
765 case SHADER_OPCODE_TEX_LOGICAL:
766 case SHADER_OPCODE_TXD_LOGICAL:
767 case SHADER_OPCODE_TXF_LOGICAL:
768 case SHADER_OPCODE_TXL_LOGICAL:
769 case SHADER_OPCODE_TXS_LOGICAL:
770 case SHADER_OPCODE_IMAGE_SIZE_LOGICAL:
771 case FS_OPCODE_TXB_LOGICAL:
772 case SHADER_OPCODE_TXF_CMS_LOGICAL:
773 case SHADER_OPCODE_TXF_CMS_W_LOGICAL:
774 case SHADER_OPCODE_TXF_UMS_LOGICAL:
775 case SHADER_OPCODE_TXF_MCS_LOGICAL:
776 case SHADER_OPCODE_LOD_LOGICAL:
777 case SHADER_OPCODE_TG4_LOGICAL:
778 case SHADER_OPCODE_TG4_OFFSET_LOGICAL:
779 case SHADER_OPCODE_SAMPLEINFO_LOGICAL:
780 assert(src[TEX_LOGICAL_SRC_COORD_COMPONENTS].file == IMM &&
781 src[TEX_LOGICAL_SRC_GRAD_COMPONENTS].file == IMM);
782 /* Texture coordinates. */
783 if (i == TEX_LOGICAL_SRC_COORDINATE)
784 return src[TEX_LOGICAL_SRC_COORD_COMPONENTS].ud;
785 /* Texture derivatives. */
786 else if ((i == TEX_LOGICAL_SRC_LOD || i == TEX_LOGICAL_SRC_LOD2) &&
787 opcode == SHADER_OPCODE_TXD_LOGICAL)
788 return src[TEX_LOGICAL_SRC_GRAD_COMPONENTS].ud;
789 /* Texture offset. */
790 else if (i == TEX_LOGICAL_SRC_TG4_OFFSET)
791 return 2;
792 /* MCS */
793 else if (i == TEX_LOGICAL_SRC_MCS && opcode == SHADER_OPCODE_TXF_CMS_W_LOGICAL)
794 return 2;
795 else
796 return 1;
797
798 case SHADER_OPCODE_UNTYPED_SURFACE_READ_LOGICAL:
799 case SHADER_OPCODE_TYPED_SURFACE_READ_LOGICAL:
800 assert(src[SURFACE_LOGICAL_SRC_IMM_DIMS].file == IMM);
801 /* Surface coordinates. */
802 if (i == SURFACE_LOGICAL_SRC_ADDRESS)
803 return src[SURFACE_LOGICAL_SRC_IMM_DIMS].ud;
804 /* Surface operation source (ignored for reads). */
805 else if (i == SURFACE_LOGICAL_SRC_DATA)
806 return 0;
807 else
808 return 1;
809
810 case SHADER_OPCODE_UNTYPED_SURFACE_WRITE_LOGICAL:
811 case SHADER_OPCODE_TYPED_SURFACE_WRITE_LOGICAL:
812 assert(src[SURFACE_LOGICAL_SRC_IMM_DIMS].file == IMM &&
813 src[SURFACE_LOGICAL_SRC_IMM_ARG].file == IMM);
814 /* Surface coordinates. */
815 if (i == SURFACE_LOGICAL_SRC_ADDRESS)
816 return src[SURFACE_LOGICAL_SRC_IMM_DIMS].ud;
817 /* Surface operation source. */
818 else if (i == SURFACE_LOGICAL_SRC_DATA)
819 return src[SURFACE_LOGICAL_SRC_IMM_ARG].ud;
820 else
821 return 1;
822
823 case SHADER_OPCODE_A64_UNTYPED_READ_LOGICAL:
824 assert(src[2].file == IMM);
825 return 1;
826
827 case SHADER_OPCODE_A64_UNTYPED_WRITE_LOGICAL:
828 assert(src[2].file == IMM);
829 return i == 1 ? src[2].ud : 1;
830
831 case SHADER_OPCODE_A64_UNTYPED_ATOMIC_LOGICAL:
832 case SHADER_OPCODE_A64_UNTYPED_ATOMIC_INT64_LOGICAL:
833 assert(src[2].file == IMM);
834 if (i == 1) {
835 /* Data source */
836 const unsigned op = src[2].ud;
837 switch (op) {
838 case BRW_AOP_INC:
839 case BRW_AOP_DEC:
840 case BRW_AOP_PREDEC:
841 return 0;
842 case BRW_AOP_CMPWR:
843 return 2;
844 default:
845 return 1;
846 }
847 } else {
848 return 1;
849 }
850
851 case SHADER_OPCODE_A64_UNTYPED_ATOMIC_FLOAT_LOGICAL:
852 assert(src[2].file == IMM);
853 if (i == 1) {
854 /* Data source */
855 const unsigned op = src[2].ud;
856 return op == BRW_AOP_FCMPWR ? 2 : 1;
857 } else {
858 return 1;
859 }
860
861 case SHADER_OPCODE_BYTE_SCATTERED_READ_LOGICAL:
862 case SHADER_OPCODE_DWORD_SCATTERED_READ_LOGICAL:
863 /* Scattered logical opcodes use the following params:
864 * src[0] Surface coordinates
865 * src[1] Surface operation source (ignored for reads)
866 * src[2] Surface
867 * src[3] IMM with always 1 dimension.
868 * src[4] IMM with arg bitsize for scattered read/write 8, 16, 32
869 */
870 assert(src[SURFACE_LOGICAL_SRC_IMM_DIMS].file == IMM &&
871 src[SURFACE_LOGICAL_SRC_IMM_ARG].file == IMM);
872 return i == SURFACE_LOGICAL_SRC_DATA ? 0 : 1;
873
874 case SHADER_OPCODE_BYTE_SCATTERED_WRITE_LOGICAL:
875 case SHADER_OPCODE_DWORD_SCATTERED_WRITE_LOGICAL:
876 assert(src[SURFACE_LOGICAL_SRC_IMM_DIMS].file == IMM &&
877 src[SURFACE_LOGICAL_SRC_IMM_ARG].file == IMM);
878 return 1;
879
880 case SHADER_OPCODE_UNTYPED_ATOMIC_LOGICAL:
881 case SHADER_OPCODE_TYPED_ATOMIC_LOGICAL: {
882 assert(src[SURFACE_LOGICAL_SRC_IMM_DIMS].file == IMM &&
883 src[SURFACE_LOGICAL_SRC_IMM_ARG].file == IMM);
884 const unsigned op = src[SURFACE_LOGICAL_SRC_IMM_ARG].ud;
885 /* Surface coordinates. */
886 if (i == SURFACE_LOGICAL_SRC_ADDRESS)
887 return src[SURFACE_LOGICAL_SRC_IMM_DIMS].ud;
888 /* Surface operation source. */
889 else if (i == SURFACE_LOGICAL_SRC_DATA && op == BRW_AOP_CMPWR)
890 return 2;
891 else if (i == SURFACE_LOGICAL_SRC_DATA &&
892 (op == BRW_AOP_INC || op == BRW_AOP_DEC || op == BRW_AOP_PREDEC))
893 return 0;
894 else
895 return 1;
896 }
897 case FS_OPCODE_INTERPOLATE_AT_PER_SLOT_OFFSET:
898 return (i == 0 ? 2 : 1);
899
900 case SHADER_OPCODE_UNTYPED_ATOMIC_FLOAT_LOGICAL: {
901 assert(src[SURFACE_LOGICAL_SRC_IMM_DIMS].file == IMM &&
902 src[SURFACE_LOGICAL_SRC_IMM_ARG].file == IMM);
903 const unsigned op = src[SURFACE_LOGICAL_SRC_IMM_ARG].ud;
904 /* Surface coordinates. */
905 if (i == SURFACE_LOGICAL_SRC_ADDRESS)
906 return src[SURFACE_LOGICAL_SRC_IMM_DIMS].ud;
907 /* Surface operation source. */
908 else if (i == SURFACE_LOGICAL_SRC_DATA && op == BRW_AOP_FCMPWR)
909 return 2;
910 else
911 return 1;
912 }
913
914 default:
915 return 1;
916 }
917 }
918
919 unsigned
920 fs_inst::size_read(int arg) const
921 {
922 switch (opcode) {
923 case SHADER_OPCODE_SEND:
924 if (arg == 2) {
925 return mlen * REG_SIZE;
926 } else if (arg == 3) {
927 return ex_mlen * REG_SIZE;
928 }
929 break;
930
931 case FS_OPCODE_FB_WRITE:
932 case FS_OPCODE_REP_FB_WRITE:
933 if (arg == 0) {
934 if (base_mrf >= 0)
935 return src[0].file == BAD_FILE ? 0 : 2 * REG_SIZE;
936 else
937 return mlen * REG_SIZE;
938 }
939 break;
940
941 case FS_OPCODE_FB_READ:
942 case SHADER_OPCODE_URB_WRITE_SIMD8:
943 case SHADER_OPCODE_URB_WRITE_SIMD8_PER_SLOT:
944 case SHADER_OPCODE_URB_WRITE_SIMD8_MASKED:
945 case SHADER_OPCODE_URB_WRITE_SIMD8_MASKED_PER_SLOT:
946 case SHADER_OPCODE_URB_READ_SIMD8:
947 case SHADER_OPCODE_URB_READ_SIMD8_PER_SLOT:
948 case FS_OPCODE_INTERPOLATE_AT_SAMPLE:
949 case FS_OPCODE_INTERPOLATE_AT_SHARED_OFFSET:
950 if (arg == 0)
951 return mlen * REG_SIZE;
952 break;
953
954 case FS_OPCODE_SET_SAMPLE_ID:
955 if (arg == 1)
956 return 1;
957 break;
958
959 case FS_OPCODE_UNIFORM_PULL_CONSTANT_LOAD_GEN7:
960 /* The payload is actually stored in src1 */
961 if (arg == 1)
962 return mlen * REG_SIZE;
963 break;
964
965 case FS_OPCODE_LINTERP:
966 if (arg == 1)
967 return 16;
968 break;
969
970 case SHADER_OPCODE_LOAD_PAYLOAD:
971 if (arg < this->header_size)
972 return REG_SIZE;
973 break;
974
975 case CS_OPCODE_CS_TERMINATE:
976 case SHADER_OPCODE_BARRIER:
977 return REG_SIZE;
978
979 case SHADER_OPCODE_MOV_INDIRECT:
980 if (arg == 0) {
981 assert(src[2].file == IMM);
982 return src[2].ud;
983 }
984 break;
985
986 default:
987 if (is_tex() && arg == 0 && src[0].file == VGRF)
988 return mlen * REG_SIZE;
989 break;
990 }
991
992 switch (src[arg].file) {
993 case UNIFORM:
994 case IMM:
995 return components_read(arg) * type_sz(src[arg].type);
996 case BAD_FILE:
997 case ARF:
998 case FIXED_GRF:
999 case VGRF:
1000 case ATTR:
1001 return components_read(arg) * src[arg].component_size(exec_size);
1002 case MRF:
1003 unreachable("MRF registers are not allowed as sources");
1004 }
1005 return 0;
1006 }
1007
1008 namespace {
1009 unsigned
1010 predicate_width(brw_predicate predicate)
1011 {
1012 switch (predicate) {
1013 case BRW_PREDICATE_NONE: return 1;
1014 case BRW_PREDICATE_NORMAL: return 1;
1015 case BRW_PREDICATE_ALIGN1_ANY2H: return 2;
1016 case BRW_PREDICATE_ALIGN1_ALL2H: return 2;
1017 case BRW_PREDICATE_ALIGN1_ANY4H: return 4;
1018 case BRW_PREDICATE_ALIGN1_ALL4H: return 4;
1019 case BRW_PREDICATE_ALIGN1_ANY8H: return 8;
1020 case BRW_PREDICATE_ALIGN1_ALL8H: return 8;
1021 case BRW_PREDICATE_ALIGN1_ANY16H: return 16;
1022 case BRW_PREDICATE_ALIGN1_ALL16H: return 16;
1023 case BRW_PREDICATE_ALIGN1_ANY32H: return 32;
1024 case BRW_PREDICATE_ALIGN1_ALL32H: return 32;
1025 default: unreachable("Unsupported predicate");
1026 }
1027 }
1028
1029 /* Return the subset of flag registers that an instruction could
1030 * potentially read or write based on the execution controls and flag
1031 * subregister number of the instruction.
1032 */
1033 unsigned
1034 flag_mask(const fs_inst *inst, unsigned width)
1035 {
1036 assert(util_is_power_of_two_nonzero(width));
1037 const unsigned start = (inst->flag_subreg * 16 + inst->group) &
1038 ~(width - 1);
1039 const unsigned end = start + ALIGN(inst->exec_size, width);
1040 return ((1 << DIV_ROUND_UP(end, 8)) - 1) & ~((1 << (start / 8)) - 1);
1041 }
1042
1043 unsigned
1044 bit_mask(unsigned n)
1045 {
1046 return (n >= CHAR_BIT * sizeof(bit_mask(n)) ? ~0u : (1u << n) - 1);
1047 }
1048
1049 unsigned
1050 flag_mask(const fs_reg &r, unsigned sz)
1051 {
1052 if (r.file == ARF) {
1053 const unsigned start = (r.nr - BRW_ARF_FLAG) * 4 + r.subnr;
1054 const unsigned end = start + sz;
1055 return bit_mask(end) & ~bit_mask(start);
1056 } else {
1057 return 0;
1058 }
1059 }
1060 }
1061
1062 unsigned
1063 fs_inst::flags_read(const gen_device_info *devinfo) const
1064 {
1065 if (predicate == BRW_PREDICATE_ALIGN1_ANYV ||
1066 predicate == BRW_PREDICATE_ALIGN1_ALLV) {
1067 /* The vertical predication modes combine corresponding bits from
1068 * f0.0 and f1.0 on Gen7+, and f0.0 and f0.1 on older hardware.
1069 */
1070 const unsigned shift = devinfo->gen >= 7 ? 4 : 2;
1071 return flag_mask(this, 1) << shift | flag_mask(this, 1);
1072 } else if (predicate) {
1073 return flag_mask(this, predicate_width(predicate));
1074 } else {
1075 unsigned mask = 0;
1076 for (int i = 0; i < sources; i++) {
1077 mask |= flag_mask(src[i], size_read(i));
1078 }
1079 return mask;
1080 }
1081 }
1082
1083 unsigned
1084 fs_inst::flags_written() const
1085 {
1086 if ((conditional_mod && (opcode != BRW_OPCODE_SEL &&
1087 opcode != BRW_OPCODE_CSEL &&
1088 opcode != BRW_OPCODE_IF &&
1089 opcode != BRW_OPCODE_WHILE)) ||
1090 opcode == FS_OPCODE_FB_WRITE) {
1091 return flag_mask(this, 1);
1092 } else if (opcode == SHADER_OPCODE_FIND_LIVE_CHANNEL ||
1093 opcode == FS_OPCODE_LOAD_LIVE_CHANNELS) {
1094 return flag_mask(this, 32);
1095 } else {
1096 return flag_mask(dst, size_written);
1097 }
1098 }
1099
1100 /**
1101 * Returns how many MRFs an FS opcode will write over.
1102 *
1103 * Note that this is not the 0 or 1 implied writes in an actual gen
1104 * instruction -- the FS opcodes often generate MOVs in addition.
1105 */
1106 unsigned
1107 fs_inst::implied_mrf_writes() const
1108 {
1109 if (mlen == 0)
1110 return 0;
1111
1112 if (base_mrf == -1)
1113 return 0;
1114
1115 switch (opcode) {
1116 case SHADER_OPCODE_RCP:
1117 case SHADER_OPCODE_RSQ:
1118 case SHADER_OPCODE_SQRT:
1119 case SHADER_OPCODE_EXP2:
1120 case SHADER_OPCODE_LOG2:
1121 case SHADER_OPCODE_SIN:
1122 case SHADER_OPCODE_COS:
1123 return 1 * exec_size / 8;
1124 case SHADER_OPCODE_POW:
1125 case SHADER_OPCODE_INT_QUOTIENT:
1126 case SHADER_OPCODE_INT_REMAINDER:
1127 return 2 * exec_size / 8;
1128 case SHADER_OPCODE_TEX:
1129 case FS_OPCODE_TXB:
1130 case SHADER_OPCODE_TXD:
1131 case SHADER_OPCODE_TXF:
1132 case SHADER_OPCODE_TXF_CMS:
1133 case SHADER_OPCODE_TXF_MCS:
1134 case SHADER_OPCODE_TG4:
1135 case SHADER_OPCODE_TG4_OFFSET:
1136 case SHADER_OPCODE_TXL:
1137 case SHADER_OPCODE_TXS:
1138 case SHADER_OPCODE_LOD:
1139 case SHADER_OPCODE_SAMPLEINFO:
1140 return 1;
1141 case FS_OPCODE_FB_WRITE:
1142 case FS_OPCODE_REP_FB_WRITE:
1143 return src[0].file == BAD_FILE ? 0 : 2;
1144 case FS_OPCODE_UNIFORM_PULL_CONSTANT_LOAD:
1145 case SHADER_OPCODE_GEN4_SCRATCH_READ:
1146 return 1;
1147 case FS_OPCODE_VARYING_PULL_CONSTANT_LOAD_GEN4:
1148 return mlen;
1149 case SHADER_OPCODE_GEN4_SCRATCH_WRITE:
1150 return mlen;
1151 default:
1152 unreachable("not reached");
1153 }
1154 }
1155
1156 fs_reg
1157 fs_visitor::vgrf(const glsl_type *const type)
1158 {
1159 int reg_width = dispatch_width / 8;
1160 return fs_reg(VGRF,
1161 alloc.allocate(glsl_count_dword_slots(type, false) * reg_width),
1162 brw_type_for_base_type(type));
1163 }
1164
1165 fs_reg::fs_reg(enum brw_reg_file file, int nr)
1166 {
1167 init();
1168 this->file = file;
1169 this->nr = nr;
1170 this->type = BRW_REGISTER_TYPE_F;
1171 this->stride = (file == UNIFORM ? 0 : 1);
1172 }
1173
1174 fs_reg::fs_reg(enum brw_reg_file file, int nr, enum brw_reg_type type)
1175 {
1176 init();
1177 this->file = file;
1178 this->nr = nr;
1179 this->type = type;
1180 this->stride = (file == UNIFORM ? 0 : 1);
1181 }
1182
1183 /* For SIMD16, we need to follow from the uniform setup of SIMD8 dispatch.
1184 * This brings in those uniform definitions
1185 */
1186 void
1187 fs_visitor::import_uniforms(fs_visitor *v)
1188 {
1189 this->push_constant_loc = v->push_constant_loc;
1190 this->pull_constant_loc = v->pull_constant_loc;
1191 this->uniforms = v->uniforms;
1192 this->subgroup_id = v->subgroup_id;
1193 for (unsigned i = 0; i < ARRAY_SIZE(this->group_size); i++)
1194 this->group_size[i] = v->group_size[i];
1195 }
1196
1197 void
1198 fs_visitor::emit_fragcoord_interpolation(fs_reg wpos)
1199 {
1200 assert(stage == MESA_SHADER_FRAGMENT);
1201
1202 /* gl_FragCoord.x */
1203 bld.MOV(wpos, this->pixel_x);
1204 wpos = offset(wpos, bld, 1);
1205
1206 /* gl_FragCoord.y */
1207 bld.MOV(wpos, this->pixel_y);
1208 wpos = offset(wpos, bld, 1);
1209
1210 /* gl_FragCoord.z */
1211 if (devinfo->gen >= 6) {
1212 bld.MOV(wpos, fetch_payload_reg(bld, payload.source_depth_reg));
1213 } else {
1214 bld.emit(FS_OPCODE_LINTERP, wpos,
1215 this->delta_xy[BRW_BARYCENTRIC_PERSPECTIVE_PIXEL],
1216 component(interp_reg(VARYING_SLOT_POS, 2), 0));
1217 }
1218 wpos = offset(wpos, bld, 1);
1219
1220 /* gl_FragCoord.w: Already set up in emit_interpolation */
1221 bld.MOV(wpos, this->wpos_w);
1222 }
1223
1224 enum brw_barycentric_mode
1225 brw_barycentric_mode(enum glsl_interp_mode mode, nir_intrinsic_op op)
1226 {
1227 /* Barycentric modes don't make sense for flat inputs. */
1228 assert(mode != INTERP_MODE_FLAT);
1229
1230 unsigned bary;
1231 switch (op) {
1232 case nir_intrinsic_load_barycentric_pixel:
1233 case nir_intrinsic_load_barycentric_at_offset:
1234 bary = BRW_BARYCENTRIC_PERSPECTIVE_PIXEL;
1235 break;
1236 case nir_intrinsic_load_barycentric_centroid:
1237 bary = BRW_BARYCENTRIC_PERSPECTIVE_CENTROID;
1238 break;
1239 case nir_intrinsic_load_barycentric_sample:
1240 case nir_intrinsic_load_barycentric_at_sample:
1241 bary = BRW_BARYCENTRIC_PERSPECTIVE_SAMPLE;
1242 break;
1243 default:
1244 unreachable("invalid intrinsic");
1245 }
1246
1247 if (mode == INTERP_MODE_NOPERSPECTIVE)
1248 bary += 3;
1249
1250 return (enum brw_barycentric_mode) bary;
1251 }
1252
1253 /**
1254 * Turn one of the two CENTROID barycentric modes into PIXEL mode.
1255 */
1256 static enum brw_barycentric_mode
1257 centroid_to_pixel(enum brw_barycentric_mode bary)
1258 {
1259 assert(bary == BRW_BARYCENTRIC_PERSPECTIVE_CENTROID ||
1260 bary == BRW_BARYCENTRIC_NONPERSPECTIVE_CENTROID);
1261 return (enum brw_barycentric_mode) ((unsigned) bary - 1);
1262 }
1263
1264 fs_reg *
1265 fs_visitor::emit_frontfacing_interpolation()
1266 {
1267 fs_reg *reg = new(this->mem_ctx) fs_reg(vgrf(glsl_type::bool_type));
1268
1269 if (devinfo->gen >= 12) {
1270 fs_reg g1 = fs_reg(retype(brw_vec1_grf(1, 1), BRW_REGISTER_TYPE_W));
1271
1272 fs_reg tmp = bld.vgrf(BRW_REGISTER_TYPE_W);
1273 bld.ASR(tmp, g1, brw_imm_d(15));
1274 bld.NOT(*reg, tmp);
1275 } else if (devinfo->gen >= 6) {
1276 /* Bit 15 of g0.0 is 0 if the polygon is front facing. We want to create
1277 * a boolean result from this (~0/true or 0/false).
1278 *
1279 * We can use the fact that bit 15 is the MSB of g0.0:W to accomplish
1280 * this task in only one instruction:
1281 * - a negation source modifier will flip the bit; and
1282 * - a W -> D type conversion will sign extend the bit into the high
1283 * word of the destination.
1284 *
1285 * An ASR 15 fills the low word of the destination.
1286 */
1287 fs_reg g0 = fs_reg(retype(brw_vec1_grf(0, 0), BRW_REGISTER_TYPE_W));
1288 g0.negate = true;
1289
1290 bld.ASR(*reg, g0, brw_imm_d(15));
1291 } else {
1292 /* Bit 31 of g1.6 is 0 if the polygon is front facing. We want to create
1293 * a boolean result from this (1/true or 0/false).
1294 *
1295 * Like in the above case, since the bit is the MSB of g1.6:UD we can use
1296 * the negation source modifier to flip it. Unfortunately the SHR
1297 * instruction only operates on UD (or D with an abs source modifier)
1298 * sources without negation.
1299 *
1300 * Instead, use ASR (which will give ~0/true or 0/false).
1301 */
1302 fs_reg g1_6 = fs_reg(retype(brw_vec1_grf(1, 6), BRW_REGISTER_TYPE_D));
1303 g1_6.negate = true;
1304
1305 bld.ASR(*reg, g1_6, brw_imm_d(31));
1306 }
1307
1308 return reg;
1309 }
1310
1311 void
1312 fs_visitor::compute_sample_position(fs_reg dst, fs_reg int_sample_pos)
1313 {
1314 assert(stage == MESA_SHADER_FRAGMENT);
1315 struct brw_wm_prog_data *wm_prog_data = brw_wm_prog_data(this->prog_data);
1316 assert(dst.type == BRW_REGISTER_TYPE_F);
1317
1318 if (wm_prog_data->persample_dispatch) {
1319 /* Convert int_sample_pos to floating point */
1320 bld.MOV(dst, int_sample_pos);
1321 /* Scale to the range [0, 1] */
1322 bld.MUL(dst, dst, brw_imm_f(1 / 16.0f));
1323 }
1324 else {
1325 /* From ARB_sample_shading specification:
1326 * "When rendering to a non-multisample buffer, or if multisample
1327 * rasterization is disabled, gl_SamplePosition will always be
1328 * (0.5, 0.5).
1329 */
1330 bld.MOV(dst, brw_imm_f(0.5f));
1331 }
1332 }
1333
1334 fs_reg *
1335 fs_visitor::emit_samplepos_setup()
1336 {
1337 assert(devinfo->gen >= 6);
1338
1339 const fs_builder abld = bld.annotate("compute sample position");
1340 fs_reg *reg = new(this->mem_ctx) fs_reg(vgrf(glsl_type::vec2_type));
1341 fs_reg pos = *reg;
1342 fs_reg int_sample_x = vgrf(glsl_type::int_type);
1343 fs_reg int_sample_y = vgrf(glsl_type::int_type);
1344
1345 /* WM will be run in MSDISPMODE_PERSAMPLE. So, only one of SIMD8 or SIMD16
1346 * mode will be enabled.
1347 *
1348 * From the Ivy Bridge PRM, volume 2 part 1, page 344:
1349 * R31.1:0 Position Offset X/Y for Slot[3:0]
1350 * R31.3:2 Position Offset X/Y for Slot[7:4]
1351 * .....
1352 *
1353 * The X, Y sample positions come in as bytes in thread payload. So, read
1354 * the positions using vstride=16, width=8, hstride=2.
1355 */
1356 const fs_reg sample_pos_reg =
1357 fetch_payload_reg(abld, payload.sample_pos_reg, BRW_REGISTER_TYPE_W);
1358
1359 /* Compute gl_SamplePosition.x */
1360 abld.MOV(int_sample_x, subscript(sample_pos_reg, BRW_REGISTER_TYPE_B, 0));
1361 compute_sample_position(offset(pos, abld, 0), int_sample_x);
1362
1363 /* Compute gl_SamplePosition.y */
1364 abld.MOV(int_sample_y, subscript(sample_pos_reg, BRW_REGISTER_TYPE_B, 1));
1365 compute_sample_position(offset(pos, abld, 1), int_sample_y);
1366 return reg;
1367 }
1368
1369 fs_reg *
1370 fs_visitor::emit_sampleid_setup()
1371 {
1372 assert(stage == MESA_SHADER_FRAGMENT);
1373 brw_wm_prog_key *key = (brw_wm_prog_key*) this->key;
1374 assert(devinfo->gen >= 6);
1375
1376 const fs_builder abld = bld.annotate("compute sample id");
1377 fs_reg *reg = new(this->mem_ctx) fs_reg(vgrf(glsl_type::uint_type));
1378
1379 if (!key->multisample_fbo) {
1380 /* As per GL_ARB_sample_shading specification:
1381 * "When rendering to a non-multisample buffer, or if multisample
1382 * rasterization is disabled, gl_SampleID will always be zero."
1383 */
1384 abld.MOV(*reg, brw_imm_d(0));
1385 } else if (devinfo->gen >= 8) {
1386 /* Sample ID comes in as 4-bit numbers in g1.0:
1387 *
1388 * 15:12 Slot 3 SampleID (only used in SIMD16)
1389 * 11:8 Slot 2 SampleID (only used in SIMD16)
1390 * 7:4 Slot 1 SampleID
1391 * 3:0 Slot 0 SampleID
1392 *
1393 * Each slot corresponds to four channels, so we want to replicate each
1394 * half-byte value to 4 channels in a row:
1395 *
1396 * dst+0: .7 .6 .5 .4 .3 .2 .1 .0
1397 * 7:4 7:4 7:4 7:4 3:0 3:0 3:0 3:0
1398 *
1399 * dst+1: .7 .6 .5 .4 .3 .2 .1 .0 (if SIMD16)
1400 * 15:12 15:12 15:12 15:12 11:8 11:8 11:8 11:8
1401 *
1402 * First, we read g1.0 with a <1,8,0>UB region, causing the first 8
1403 * channels to read the first byte (7:0), and the second group of 8
1404 * channels to read the second byte (15:8). Then, we shift right by
1405 * a vector immediate of <4, 4, 4, 4, 0, 0, 0, 0>, moving the slot 1 / 3
1406 * values into place. Finally, we AND with 0xf to keep the low nibble.
1407 *
1408 * shr(16) tmp<1>W g1.0<1,8,0>B 0x44440000:V
1409 * and(16) dst<1>D tmp<8,8,1>W 0xf:W
1410 *
1411 * TODO: These payload bits exist on Gen7 too, but they appear to always
1412 * be zero, so this code fails to work. We should find out why.
1413 */
1414 const fs_reg tmp = abld.vgrf(BRW_REGISTER_TYPE_UW);
1415
1416 for (unsigned i = 0; i < DIV_ROUND_UP(dispatch_width, 16); i++) {
1417 const fs_builder hbld = abld.group(MIN2(16, dispatch_width), i);
1418 hbld.SHR(offset(tmp, hbld, i),
1419 stride(retype(brw_vec1_grf(1 + i, 0), BRW_REGISTER_TYPE_UB),
1420 1, 8, 0),
1421 brw_imm_v(0x44440000));
1422 }
1423
1424 abld.AND(*reg, tmp, brw_imm_w(0xf));
1425 } else {
1426 const fs_reg t1 = component(abld.vgrf(BRW_REGISTER_TYPE_UD), 0);
1427 const fs_reg t2 = abld.vgrf(BRW_REGISTER_TYPE_UW);
1428
1429 /* The PS will be run in MSDISPMODE_PERSAMPLE. For example with
1430 * 8x multisampling, subspan 0 will represent sample N (where N
1431 * is 0, 2, 4 or 6), subspan 1 will represent sample 1, 3, 5 or
1432 * 7. We can find the value of N by looking at R0.0 bits 7:6
1433 * ("Starting Sample Pair Index (SSPI)") and multiplying by two
1434 * (since samples are always delivered in pairs). That is, we
1435 * compute 2*((R0.0 & 0xc0) >> 6) == (R0.0 & 0xc0) >> 5. Then
1436 * we need to add N to the sequence (0, 0, 0, 0, 1, 1, 1, 1) in
1437 * case of SIMD8 and sequence (0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2,
1438 * 2, 3, 3, 3, 3) in case of SIMD16. We compute this sequence by
1439 * populating a temporary variable with the sequence (0, 1, 2, 3),
1440 * and then reading from it using vstride=1, width=4, hstride=0.
1441 * These computations hold good for 4x multisampling as well.
1442 *
1443 * For 2x MSAA and SIMD16, we want to use the sequence (0, 1, 0, 1):
1444 * the first four slots are sample 0 of subspan 0; the next four
1445 * are sample 1 of subspan 0; the third group is sample 0 of
1446 * subspan 1, and finally sample 1 of subspan 1.
1447 */
1448
1449 /* SKL+ has an extra bit for the Starting Sample Pair Index to
1450 * accomodate 16x MSAA.
1451 */
1452 abld.exec_all().group(1, 0)
1453 .AND(t1, fs_reg(retype(brw_vec1_grf(0, 0), BRW_REGISTER_TYPE_UD)),
1454 brw_imm_ud(0xc0));
1455 abld.exec_all().group(1, 0).SHR(t1, t1, brw_imm_d(5));
1456
1457 /* This works for SIMD8-SIMD16. It also works for SIMD32 but only if we
1458 * can assume 4x MSAA. Disallow it on IVB+
1459 *
1460 * FINISHME: One day, we could come up with a way to do this that
1461 * actually works on gen7.
1462 */
1463 if (devinfo->gen >= 7)
1464 limit_dispatch_width(16, "gl_SampleId is unsupported in SIMD32 on gen7");
1465 abld.exec_all().group(8, 0).MOV(t2, brw_imm_v(0x32103210));
1466
1467 /* This special instruction takes care of setting vstride=1,
1468 * width=4, hstride=0 of t2 during an ADD instruction.
1469 */
1470 abld.emit(FS_OPCODE_SET_SAMPLE_ID, *reg, t1, t2);
1471 }
1472
1473 return reg;
1474 }
1475
1476 fs_reg *
1477 fs_visitor::emit_samplemaskin_setup()
1478 {
1479 assert(stage == MESA_SHADER_FRAGMENT);
1480 struct brw_wm_prog_data *wm_prog_data = brw_wm_prog_data(this->prog_data);
1481 assert(devinfo->gen >= 6);
1482
1483 fs_reg *reg = new(this->mem_ctx) fs_reg(vgrf(glsl_type::int_type));
1484
1485 fs_reg coverage_mask =
1486 fetch_payload_reg(bld, payload.sample_mask_in_reg, BRW_REGISTER_TYPE_D);
1487
1488 if (wm_prog_data->persample_dispatch) {
1489 /* gl_SampleMaskIn[] comes from two sources: the input coverage mask,
1490 * and a mask representing which sample is being processed by the
1491 * current shader invocation.
1492 *
1493 * From the OES_sample_variables specification:
1494 * "When per-sample shading is active due to the use of a fragment input
1495 * qualified by "sample" or due to the use of the gl_SampleID or
1496 * gl_SamplePosition variables, only the bit for the current sample is
1497 * set in gl_SampleMaskIn."
1498 */
1499 const fs_builder abld = bld.annotate("compute gl_SampleMaskIn");
1500
1501 if (nir_system_values[SYSTEM_VALUE_SAMPLE_ID].file == BAD_FILE)
1502 nir_system_values[SYSTEM_VALUE_SAMPLE_ID] = *emit_sampleid_setup();
1503
1504 fs_reg one = vgrf(glsl_type::int_type);
1505 fs_reg enabled_mask = vgrf(glsl_type::int_type);
1506 abld.MOV(one, brw_imm_d(1));
1507 abld.SHL(enabled_mask, one, nir_system_values[SYSTEM_VALUE_SAMPLE_ID]);
1508 abld.AND(*reg, enabled_mask, coverage_mask);
1509 } else {
1510 /* In per-pixel mode, the coverage mask is sufficient. */
1511 *reg = coverage_mask;
1512 }
1513 return reg;
1514 }
1515
1516 fs_reg
1517 fs_visitor::resolve_source_modifiers(const fs_reg &src)
1518 {
1519 if (!src.abs && !src.negate)
1520 return src;
1521
1522 fs_reg temp = bld.vgrf(src.type);
1523 bld.MOV(temp, src);
1524
1525 return temp;
1526 }
1527
1528 void
1529 fs_visitor::emit_discard_jump()
1530 {
1531 assert(brw_wm_prog_data(this->prog_data)->uses_kill);
1532
1533 /* For performance, after a discard, jump to the end of the
1534 * shader if all relevant channels have been discarded.
1535 */
1536 fs_inst *discard_jump = bld.emit(FS_OPCODE_DISCARD_JUMP);
1537 discard_jump->flag_subreg = sample_mask_flag_subreg(this);
1538
1539 discard_jump->predicate = BRW_PREDICATE_ALIGN1_ANY4H;
1540 discard_jump->predicate_inverse = true;
1541 }
1542
1543 void
1544 fs_visitor::emit_gs_thread_end()
1545 {
1546 assert(stage == MESA_SHADER_GEOMETRY);
1547
1548 struct brw_gs_prog_data *gs_prog_data = brw_gs_prog_data(prog_data);
1549
1550 if (gs_compile->control_data_header_size_bits > 0) {
1551 emit_gs_control_data_bits(this->final_gs_vertex_count);
1552 }
1553
1554 const fs_builder abld = bld.annotate("thread end");
1555 fs_inst *inst;
1556
1557 if (gs_prog_data->static_vertex_count != -1) {
1558 foreach_in_list_reverse(fs_inst, prev, &this->instructions) {
1559 if (prev->opcode == SHADER_OPCODE_URB_WRITE_SIMD8 ||
1560 prev->opcode == SHADER_OPCODE_URB_WRITE_SIMD8_MASKED ||
1561 prev->opcode == SHADER_OPCODE_URB_WRITE_SIMD8_PER_SLOT ||
1562 prev->opcode == SHADER_OPCODE_URB_WRITE_SIMD8_MASKED_PER_SLOT) {
1563 prev->eot = true;
1564
1565 /* Delete now dead instructions. */
1566 foreach_in_list_reverse_safe(exec_node, dead, &this->instructions) {
1567 if (dead == prev)
1568 break;
1569 dead->remove();
1570 }
1571 return;
1572 } else if (prev->is_control_flow() || prev->has_side_effects()) {
1573 break;
1574 }
1575 }
1576 fs_reg hdr = abld.vgrf(BRW_REGISTER_TYPE_UD, 1);
1577 abld.MOV(hdr, fs_reg(retype(brw_vec8_grf(1, 0), BRW_REGISTER_TYPE_UD)));
1578 inst = abld.emit(SHADER_OPCODE_URB_WRITE_SIMD8, reg_undef, hdr);
1579 inst->mlen = 1;
1580 } else {
1581 fs_reg payload = abld.vgrf(BRW_REGISTER_TYPE_UD, 2);
1582 fs_reg *sources = ralloc_array(mem_ctx, fs_reg, 2);
1583 sources[0] = fs_reg(retype(brw_vec8_grf(1, 0), BRW_REGISTER_TYPE_UD));
1584 sources[1] = this->final_gs_vertex_count;
1585 abld.LOAD_PAYLOAD(payload, sources, 2, 2);
1586 inst = abld.emit(SHADER_OPCODE_URB_WRITE_SIMD8, reg_undef, payload);
1587 inst->mlen = 2;
1588 }
1589 inst->eot = true;
1590 inst->offset = 0;
1591 }
1592
1593 void
1594 fs_visitor::assign_curb_setup()
1595 {
1596 unsigned uniform_push_length = DIV_ROUND_UP(stage_prog_data->nr_params, 8);
1597
1598 unsigned ubo_push_length = 0;
1599 unsigned ubo_push_start[4];
1600 for (int i = 0; i < 4; i++) {
1601 ubo_push_start[i] = 8 * (ubo_push_length + uniform_push_length);
1602 ubo_push_length += stage_prog_data->ubo_ranges[i].length;
1603 }
1604
1605 prog_data->curb_read_length = uniform_push_length + ubo_push_length;
1606
1607 uint64_t used = 0;
1608
1609 /* Map the offsets in the UNIFORM file to fixed HW regs. */
1610 foreach_block_and_inst(block, fs_inst, inst, cfg) {
1611 for (unsigned int i = 0; i < inst->sources; i++) {
1612 if (inst->src[i].file == UNIFORM) {
1613 int uniform_nr = inst->src[i].nr + inst->src[i].offset / 4;
1614 int constant_nr;
1615 if (inst->src[i].nr >= UBO_START) {
1616 /* constant_nr is in 32-bit units, the rest are in bytes */
1617 constant_nr = ubo_push_start[inst->src[i].nr - UBO_START] +
1618 inst->src[i].offset / 4;
1619 } else if (uniform_nr >= 0 && uniform_nr < (int) uniforms) {
1620 constant_nr = push_constant_loc[uniform_nr];
1621 } else {
1622 /* Section 5.11 of the OpenGL 4.1 spec says:
1623 * "Out-of-bounds reads return undefined values, which include
1624 * values from other variables of the active program or zero."
1625 * Just return the first push constant.
1626 */
1627 constant_nr = 0;
1628 }
1629
1630 assert(constant_nr / 8 < 64);
1631 used |= BITFIELD64_BIT(constant_nr / 8);
1632
1633 struct brw_reg brw_reg = brw_vec1_grf(payload.num_regs +
1634 constant_nr / 8,
1635 constant_nr % 8);
1636 brw_reg.abs = inst->src[i].abs;
1637 brw_reg.negate = inst->src[i].negate;
1638
1639 assert(inst->src[i].stride == 0);
1640 inst->src[i] = byte_offset(
1641 retype(brw_reg, inst->src[i].type),
1642 inst->src[i].offset % 4);
1643 }
1644 }
1645 }
1646
1647 uint64_t want_zero = used & stage_prog_data->zero_push_reg;
1648 if (want_zero) {
1649 assert(!compiler->compact_params);
1650 fs_builder ubld = bld.exec_all().group(8, 0).at(
1651 cfg->first_block(), cfg->first_block()->start());
1652
1653 /* push_reg_mask_param is in 32-bit units */
1654 unsigned mask_param = stage_prog_data->push_reg_mask_param;
1655 struct brw_reg mask = brw_vec1_grf(payload.num_regs + mask_param / 8,
1656 mask_param % 8);
1657
1658 fs_reg b32;
1659 for (unsigned i = 0; i < 64; i++) {
1660 if (i % 16 == 0 && (want_zero & BITFIELD64_RANGE(i, 16))) {
1661 fs_reg shifted = ubld.vgrf(BRW_REGISTER_TYPE_W, 2);
1662 ubld.SHL(horiz_offset(shifted, 8),
1663 byte_offset(retype(mask, BRW_REGISTER_TYPE_W), i / 8),
1664 brw_imm_v(0x01234567));
1665 ubld.SHL(shifted, horiz_offset(shifted, 8), brw_imm_w(8));
1666
1667 fs_builder ubld16 = ubld.group(16, 0);
1668 b32 = ubld16.vgrf(BRW_REGISTER_TYPE_D);
1669 ubld16.group(16, 0).ASR(b32, shifted, brw_imm_w(15));
1670 }
1671
1672 if (want_zero & BITFIELD64_BIT(i)) {
1673 assert(i < prog_data->curb_read_length);
1674 struct brw_reg push_reg =
1675 retype(brw_vec8_grf(payload.num_regs + i, 0),
1676 BRW_REGISTER_TYPE_D);
1677
1678 ubld.AND(push_reg, push_reg, component(b32, i % 16));
1679 }
1680 }
1681
1682 invalidate_analysis(DEPENDENCY_INSTRUCTIONS);
1683 }
1684
1685 /* This may be updated in assign_urb_setup or assign_vs_urb_setup. */
1686 this->first_non_payload_grf = payload.num_regs + prog_data->curb_read_length;
1687 }
1688
1689 /*
1690 * Build up an array of indices into the urb_setup array that
1691 * references the active entries of the urb_setup array.
1692 * Used to accelerate walking the active entries of the urb_setup array
1693 * on each upload.
1694 */
1695 void
1696 brw_compute_urb_setup_index(struct brw_wm_prog_data *wm_prog_data)
1697 {
1698 /* Make sure uint8_t is sufficient */
1699 STATIC_ASSERT(VARYING_SLOT_MAX <= 0xff);
1700 uint8_t index = 0;
1701 for (uint8_t attr = 0; attr < VARYING_SLOT_MAX; attr++) {
1702 if (wm_prog_data->urb_setup[attr] >= 0) {
1703 wm_prog_data->urb_setup_attribs[index++] = attr;
1704 }
1705 }
1706 wm_prog_data->urb_setup_attribs_count = index;
1707 }
1708
1709 static void
1710 calculate_urb_setup(const struct gen_device_info *devinfo,
1711 const struct brw_wm_prog_key *key,
1712 struct brw_wm_prog_data *prog_data,
1713 const nir_shader *nir)
1714 {
1715 memset(prog_data->urb_setup, -1,
1716 sizeof(prog_data->urb_setup[0]) * VARYING_SLOT_MAX);
1717
1718 int urb_next = 0;
1719 /* Figure out where each of the incoming setup attributes lands. */
1720 if (devinfo->gen >= 6) {
1721 if (util_bitcount64(nir->info.inputs_read &
1722 BRW_FS_VARYING_INPUT_MASK) <= 16) {
1723 /* The SF/SBE pipeline stage can do arbitrary rearrangement of the
1724 * first 16 varying inputs, so we can put them wherever we want.
1725 * Just put them in order.
1726 *
1727 * This is useful because it means that (a) inputs not used by the
1728 * fragment shader won't take up valuable register space, and (b) we
1729 * won't have to recompile the fragment shader if it gets paired with
1730 * a different vertex (or geometry) shader.
1731 */
1732 for (unsigned int i = 0; i < VARYING_SLOT_MAX; i++) {
1733 if (nir->info.inputs_read & BRW_FS_VARYING_INPUT_MASK &
1734 BITFIELD64_BIT(i)) {
1735 prog_data->urb_setup[i] = urb_next++;
1736 }
1737 }
1738 } else {
1739 /* We have enough input varyings that the SF/SBE pipeline stage can't
1740 * arbitrarily rearrange them to suit our whim; we have to put them
1741 * in an order that matches the output of the previous pipeline stage
1742 * (geometry or vertex shader).
1743 */
1744 struct brw_vue_map prev_stage_vue_map;
1745 brw_compute_vue_map(devinfo, &prev_stage_vue_map,
1746 key->input_slots_valid,
1747 nir->info.separate_shader, 1);
1748
1749 int first_slot =
1750 brw_compute_first_urb_slot_required(nir->info.inputs_read,
1751 &prev_stage_vue_map);
1752
1753 assert(prev_stage_vue_map.num_slots <= first_slot + 32);
1754 for (int slot = first_slot; slot < prev_stage_vue_map.num_slots;
1755 slot++) {
1756 int varying = prev_stage_vue_map.slot_to_varying[slot];
1757 if (varying != BRW_VARYING_SLOT_PAD &&
1758 (nir->info.inputs_read & BRW_FS_VARYING_INPUT_MASK &
1759 BITFIELD64_BIT(varying))) {
1760 prog_data->urb_setup[varying] = slot - first_slot;
1761 }
1762 }
1763 urb_next = prev_stage_vue_map.num_slots - first_slot;
1764 }
1765 } else {
1766 /* FINISHME: The sf doesn't map VS->FS inputs for us very well. */
1767 for (unsigned int i = 0; i < VARYING_SLOT_MAX; i++) {
1768 /* Point size is packed into the header, not as a general attribute */
1769 if (i == VARYING_SLOT_PSIZ)
1770 continue;
1771
1772 if (key->input_slots_valid & BITFIELD64_BIT(i)) {
1773 /* The back color slot is skipped when the front color is
1774 * also written to. In addition, some slots can be
1775 * written in the vertex shader and not read in the
1776 * fragment shader. So the register number must always be
1777 * incremented, mapped or not.
1778 */
1779 if (_mesa_varying_slot_in_fs((gl_varying_slot) i))
1780 prog_data->urb_setup[i] = urb_next;
1781 urb_next++;
1782 }
1783 }
1784
1785 /*
1786 * It's a FS only attribute, and we did interpolation for this attribute
1787 * in SF thread. So, count it here, too.
1788 *
1789 * See compile_sf_prog() for more info.
1790 */
1791 if (nir->info.inputs_read & BITFIELD64_BIT(VARYING_SLOT_PNTC))
1792 prog_data->urb_setup[VARYING_SLOT_PNTC] = urb_next++;
1793 }
1794
1795 prog_data->num_varying_inputs = urb_next;
1796 prog_data->inputs = nir->info.inputs_read;
1797
1798 brw_compute_urb_setup_index(prog_data);
1799 }
1800
1801 void
1802 fs_visitor::assign_urb_setup()
1803 {
1804 assert(stage == MESA_SHADER_FRAGMENT);
1805 struct brw_wm_prog_data *prog_data = brw_wm_prog_data(this->prog_data);
1806
1807 int urb_start = payload.num_regs + prog_data->base.curb_read_length;
1808
1809 /* Offset all the urb_setup[] index by the actual position of the
1810 * setup regs, now that the location of the constants has been chosen.
1811 */
1812 foreach_block_and_inst(block, fs_inst, inst, cfg) {
1813 for (int i = 0; i < inst->sources; i++) {
1814 if (inst->src[i].file == ATTR) {
1815 /* ATTR regs in the FS are in units of logical scalar inputs each
1816 * of which consumes half of a GRF register.
1817 */
1818 assert(inst->src[i].offset < REG_SIZE / 2);
1819 const unsigned grf = urb_start + inst->src[i].nr / 2;
1820 const unsigned offset = (inst->src[i].nr % 2) * (REG_SIZE / 2) +
1821 inst->src[i].offset;
1822 const unsigned width = inst->src[i].stride == 0 ?
1823 1 : MIN2(inst->exec_size, 8);
1824 struct brw_reg reg = stride(
1825 byte_offset(retype(brw_vec8_grf(grf, 0), inst->src[i].type),
1826 offset),
1827 width * inst->src[i].stride,
1828 width, inst->src[i].stride);
1829 reg.abs = inst->src[i].abs;
1830 reg.negate = inst->src[i].negate;
1831 inst->src[i] = reg;
1832 }
1833 }
1834 }
1835
1836 /* Each attribute is 4 setup channels, each of which is half a reg. */
1837 this->first_non_payload_grf += prog_data->num_varying_inputs * 2;
1838 }
1839
1840 void
1841 fs_visitor::convert_attr_sources_to_hw_regs(fs_inst *inst)
1842 {
1843 for (int i = 0; i < inst->sources; i++) {
1844 if (inst->src[i].file == ATTR) {
1845 int grf = payload.num_regs +
1846 prog_data->curb_read_length +
1847 inst->src[i].nr +
1848 inst->src[i].offset / REG_SIZE;
1849
1850 /* As explained at brw_reg_from_fs_reg, From the Haswell PRM:
1851 *
1852 * VertStride must be used to cross GRF register boundaries. This
1853 * rule implies that elements within a 'Width' cannot cross GRF
1854 * boundaries.
1855 *
1856 * So, for registers that are large enough, we have to split the exec
1857 * size in two and trust the compression state to sort it out.
1858 */
1859 unsigned total_size = inst->exec_size *
1860 inst->src[i].stride *
1861 type_sz(inst->src[i].type);
1862
1863 assert(total_size <= 2 * REG_SIZE);
1864 const unsigned exec_size =
1865 (total_size <= REG_SIZE) ? inst->exec_size : inst->exec_size / 2;
1866
1867 unsigned width = inst->src[i].stride == 0 ? 1 : exec_size;
1868 struct brw_reg reg =
1869 stride(byte_offset(retype(brw_vec8_grf(grf, 0), inst->src[i].type),
1870 inst->src[i].offset % REG_SIZE),
1871 exec_size * inst->src[i].stride,
1872 width, inst->src[i].stride);
1873 reg.abs = inst->src[i].abs;
1874 reg.negate = inst->src[i].negate;
1875
1876 inst->src[i] = reg;
1877 }
1878 }
1879 }
1880
1881 void
1882 fs_visitor::assign_vs_urb_setup()
1883 {
1884 struct brw_vs_prog_data *vs_prog_data = brw_vs_prog_data(prog_data);
1885
1886 assert(stage == MESA_SHADER_VERTEX);
1887
1888 /* Each attribute is 4 regs. */
1889 this->first_non_payload_grf += 4 * vs_prog_data->nr_attribute_slots;
1890
1891 assert(vs_prog_data->base.urb_read_length <= 15);
1892
1893 /* Rewrite all ATTR file references to the hw grf that they land in. */
1894 foreach_block_and_inst(block, fs_inst, inst, cfg) {
1895 convert_attr_sources_to_hw_regs(inst);
1896 }
1897 }
1898
1899 void
1900 fs_visitor::assign_tcs_urb_setup()
1901 {
1902 assert(stage == MESA_SHADER_TESS_CTRL);
1903
1904 /* Rewrite all ATTR file references to HW_REGs. */
1905 foreach_block_and_inst(block, fs_inst, inst, cfg) {
1906 convert_attr_sources_to_hw_regs(inst);
1907 }
1908 }
1909
1910 void
1911 fs_visitor::assign_tes_urb_setup()
1912 {
1913 assert(stage == MESA_SHADER_TESS_EVAL);
1914
1915 struct brw_vue_prog_data *vue_prog_data = brw_vue_prog_data(prog_data);
1916
1917 first_non_payload_grf += 8 * vue_prog_data->urb_read_length;
1918
1919 /* Rewrite all ATTR file references to HW_REGs. */
1920 foreach_block_and_inst(block, fs_inst, inst, cfg) {
1921 convert_attr_sources_to_hw_regs(inst);
1922 }
1923 }
1924
1925 void
1926 fs_visitor::assign_gs_urb_setup()
1927 {
1928 assert(stage == MESA_SHADER_GEOMETRY);
1929
1930 struct brw_vue_prog_data *vue_prog_data = brw_vue_prog_data(prog_data);
1931
1932 first_non_payload_grf +=
1933 8 * vue_prog_data->urb_read_length * nir->info.gs.vertices_in;
1934
1935 foreach_block_and_inst(block, fs_inst, inst, cfg) {
1936 /* Rewrite all ATTR file references to GRFs. */
1937 convert_attr_sources_to_hw_regs(inst);
1938 }
1939 }
1940
1941
1942 /**
1943 * Split large virtual GRFs into separate components if we can.
1944 *
1945 * This is mostly duplicated with what brw_fs_vector_splitting does,
1946 * but that's really conservative because it's afraid of doing
1947 * splitting that doesn't result in real progress after the rest of
1948 * the optimization phases, which would cause infinite looping in
1949 * optimization. We can do it once here, safely. This also has the
1950 * opportunity to split interpolated values, or maybe even uniforms,
1951 * which we don't have at the IR level.
1952 *
1953 * We want to split, because virtual GRFs are what we register
1954 * allocate and spill (due to contiguousness requirements for some
1955 * instructions), and they're what we naturally generate in the
1956 * codegen process, but most virtual GRFs don't actually need to be
1957 * contiguous sets of GRFs. If we split, we'll end up with reduced
1958 * live intervals and better dead code elimination and coalescing.
1959 */
1960 void
1961 fs_visitor::split_virtual_grfs()
1962 {
1963 /* Compact the register file so we eliminate dead vgrfs. This
1964 * only defines split points for live registers, so if we have
1965 * too large dead registers they will hit assertions later.
1966 */
1967 compact_virtual_grfs();
1968
1969 int num_vars = this->alloc.count;
1970
1971 /* Count the total number of registers */
1972 int reg_count = 0;
1973 int vgrf_to_reg[num_vars];
1974 for (int i = 0; i < num_vars; i++) {
1975 vgrf_to_reg[i] = reg_count;
1976 reg_count += alloc.sizes[i];
1977 }
1978
1979 /* An array of "split points". For each register slot, this indicates
1980 * if this slot can be separated from the previous slot. Every time an
1981 * instruction uses multiple elements of a register (as a source or
1982 * destination), we mark the used slots as inseparable. Then we go
1983 * through and split the registers into the smallest pieces we can.
1984 */
1985 bool *split_points = new bool[reg_count];
1986 memset(split_points, 0, reg_count * sizeof(*split_points));
1987
1988 /* Mark all used registers as fully splittable */
1989 foreach_block_and_inst(block, fs_inst, inst, cfg) {
1990 if (inst->dst.file == VGRF) {
1991 int reg = vgrf_to_reg[inst->dst.nr];
1992 for (unsigned j = 1; j < this->alloc.sizes[inst->dst.nr]; j++)
1993 split_points[reg + j] = true;
1994 }
1995
1996 for (int i = 0; i < inst->sources; i++) {
1997 if (inst->src[i].file == VGRF) {
1998 int reg = vgrf_to_reg[inst->src[i].nr];
1999 for (unsigned j = 1; j < this->alloc.sizes[inst->src[i].nr]; j++)
2000 split_points[reg + j] = true;
2001 }
2002 }
2003 }
2004
2005 foreach_block_and_inst(block, fs_inst, inst, cfg) {
2006 /* We fix up undef instructions later */
2007 if (inst->opcode == SHADER_OPCODE_UNDEF) {
2008 /* UNDEF instructions are currently only used to undef entire
2009 * registers. We need this invariant later when we split them.
2010 */
2011 assert(inst->dst.file == VGRF);
2012 assert(inst->dst.offset == 0);
2013 assert(inst->size_written == alloc.sizes[inst->dst.nr] * REG_SIZE);
2014 continue;
2015 }
2016
2017 if (inst->dst.file == VGRF) {
2018 int reg = vgrf_to_reg[inst->dst.nr] + inst->dst.offset / REG_SIZE;
2019 for (unsigned j = 1; j < regs_written(inst); j++)
2020 split_points[reg + j] = false;
2021 }
2022 for (int i = 0; i < inst->sources; i++) {
2023 if (inst->src[i].file == VGRF) {
2024 int reg = vgrf_to_reg[inst->src[i].nr] + inst->src[i].offset / REG_SIZE;
2025 for (unsigned j = 1; j < regs_read(inst, i); j++)
2026 split_points[reg + j] = false;
2027 }
2028 }
2029 }
2030
2031 int *new_virtual_grf = new int[reg_count];
2032 int *new_reg_offset = new int[reg_count];
2033
2034 int reg = 0;
2035 for (int i = 0; i < num_vars; i++) {
2036 /* The first one should always be 0 as a quick sanity check. */
2037 assert(split_points[reg] == false);
2038
2039 /* j = 0 case */
2040 new_reg_offset[reg] = 0;
2041 reg++;
2042 int offset = 1;
2043
2044 /* j > 0 case */
2045 for (unsigned j = 1; j < alloc.sizes[i]; j++) {
2046 /* If this is a split point, reset the offset to 0 and allocate a
2047 * new virtual GRF for the previous offset many registers
2048 */
2049 if (split_points[reg]) {
2050 assert(offset <= MAX_VGRF_SIZE);
2051 int grf = alloc.allocate(offset);
2052 for (int k = reg - offset; k < reg; k++)
2053 new_virtual_grf[k] = grf;
2054 offset = 0;
2055 }
2056 new_reg_offset[reg] = offset;
2057 offset++;
2058 reg++;
2059 }
2060
2061 /* The last one gets the original register number */
2062 assert(offset <= MAX_VGRF_SIZE);
2063 alloc.sizes[i] = offset;
2064 for (int k = reg - offset; k < reg; k++)
2065 new_virtual_grf[k] = i;
2066 }
2067 assert(reg == reg_count);
2068
2069 foreach_block_and_inst_safe(block, fs_inst, inst, cfg) {
2070 if (inst->opcode == SHADER_OPCODE_UNDEF) {
2071 const fs_builder ibld(this, block, inst);
2072 assert(inst->size_written % REG_SIZE == 0);
2073 unsigned reg_offset = 0;
2074 while (reg_offset < inst->size_written / REG_SIZE) {
2075 reg = vgrf_to_reg[inst->dst.nr] + reg_offset;
2076 ibld.UNDEF(fs_reg(VGRF, new_virtual_grf[reg], inst->dst.type));
2077 reg_offset += alloc.sizes[new_virtual_grf[reg]];
2078 }
2079 inst->remove(block);
2080 continue;
2081 }
2082
2083 if (inst->dst.file == VGRF) {
2084 reg = vgrf_to_reg[inst->dst.nr] + inst->dst.offset / REG_SIZE;
2085 inst->dst.nr = new_virtual_grf[reg];
2086 inst->dst.offset = new_reg_offset[reg] * REG_SIZE +
2087 inst->dst.offset % REG_SIZE;
2088 assert((unsigned)new_reg_offset[reg] < alloc.sizes[new_virtual_grf[reg]]);
2089 }
2090 for (int i = 0; i < inst->sources; i++) {
2091 if (inst->src[i].file == VGRF) {
2092 reg = vgrf_to_reg[inst->src[i].nr] + inst->src[i].offset / REG_SIZE;
2093 inst->src[i].nr = new_virtual_grf[reg];
2094 inst->src[i].offset = new_reg_offset[reg] * REG_SIZE +
2095 inst->src[i].offset % REG_SIZE;
2096 assert((unsigned)new_reg_offset[reg] < alloc.sizes[new_virtual_grf[reg]]);
2097 }
2098 }
2099 }
2100 invalidate_analysis(DEPENDENCY_INSTRUCTION_DETAIL | DEPENDENCY_VARIABLES);
2101
2102 delete[] split_points;
2103 delete[] new_virtual_grf;
2104 delete[] new_reg_offset;
2105 }
2106
2107 /**
2108 * Remove unused virtual GRFs and compact the vgrf_* arrays.
2109 *
2110 * During code generation, we create tons of temporary variables, many of
2111 * which get immediately killed and are never used again. Yet, in later
2112 * optimization and analysis passes, such as compute_live_intervals, we need
2113 * to loop over all the virtual GRFs. Compacting them can save a lot of
2114 * overhead.
2115 */
2116 bool
2117 fs_visitor::compact_virtual_grfs()
2118 {
2119 bool progress = false;
2120 int *remap_table = new int[this->alloc.count];
2121 memset(remap_table, -1, this->alloc.count * sizeof(int));
2122
2123 /* Mark which virtual GRFs are used. */
2124 foreach_block_and_inst(block, const fs_inst, inst, cfg) {
2125 if (inst->dst.file == VGRF)
2126 remap_table[inst->dst.nr] = 0;
2127
2128 for (int i = 0; i < inst->sources; i++) {
2129 if (inst->src[i].file == VGRF)
2130 remap_table[inst->src[i].nr] = 0;
2131 }
2132 }
2133
2134 /* Compact the GRF arrays. */
2135 int new_index = 0;
2136 for (unsigned i = 0; i < this->alloc.count; i++) {
2137 if (remap_table[i] == -1) {
2138 /* We just found an unused register. This means that we are
2139 * actually going to compact something.
2140 */
2141 progress = true;
2142 } else {
2143 remap_table[i] = new_index;
2144 alloc.sizes[new_index] = alloc.sizes[i];
2145 invalidate_analysis(DEPENDENCY_INSTRUCTION_DETAIL | DEPENDENCY_VARIABLES);
2146 ++new_index;
2147 }
2148 }
2149
2150 this->alloc.count = new_index;
2151
2152 /* Patch all the instructions to use the newly renumbered registers */
2153 foreach_block_and_inst(block, fs_inst, inst, cfg) {
2154 if (inst->dst.file == VGRF)
2155 inst->dst.nr = remap_table[inst->dst.nr];
2156
2157 for (int i = 0; i < inst->sources; i++) {
2158 if (inst->src[i].file == VGRF)
2159 inst->src[i].nr = remap_table[inst->src[i].nr];
2160 }
2161 }
2162
2163 /* Patch all the references to delta_xy, since they're used in register
2164 * allocation. If they're unused, switch them to BAD_FILE so we don't
2165 * think some random VGRF is delta_xy.
2166 */
2167 for (unsigned i = 0; i < ARRAY_SIZE(delta_xy); i++) {
2168 if (delta_xy[i].file == VGRF) {
2169 if (remap_table[delta_xy[i].nr] != -1) {
2170 delta_xy[i].nr = remap_table[delta_xy[i].nr];
2171 } else {
2172 delta_xy[i].file = BAD_FILE;
2173 }
2174 }
2175 }
2176
2177 delete[] remap_table;
2178
2179 return progress;
2180 }
2181
2182 static int
2183 get_subgroup_id_param_index(const brw_stage_prog_data *prog_data)
2184 {
2185 if (prog_data->nr_params == 0)
2186 return -1;
2187
2188 /* The local thread id is always the last parameter in the list */
2189 uint32_t last_param = prog_data->param[prog_data->nr_params - 1];
2190 if (last_param == BRW_PARAM_BUILTIN_SUBGROUP_ID)
2191 return prog_data->nr_params - 1;
2192
2193 return -1;
2194 }
2195
2196 /**
2197 * Struct for handling complex alignments.
2198 *
2199 * A complex alignment is stored as multiplier and an offset. A value is
2200 * considered to be aligned if it is {offset} larger than a multiple of {mul}.
2201 * For instance, with an alignment of {8, 2}, cplx_align_apply would do the
2202 * following:
2203 *
2204 * N | cplx_align_apply({8, 2}, N)
2205 * ----+-----------------------------
2206 * 4 | 6
2207 * 6 | 6
2208 * 8 | 14
2209 * 10 | 14
2210 * 12 | 14
2211 * 14 | 14
2212 * 16 | 22
2213 */
2214 struct cplx_align {
2215 unsigned mul:4;
2216 unsigned offset:4;
2217 };
2218
2219 #define CPLX_ALIGN_MAX_MUL 8
2220
2221 static void
2222 cplx_align_assert_sane(struct cplx_align a)
2223 {
2224 assert(a.mul > 0 && util_is_power_of_two_nonzero(a.mul));
2225 assert(a.offset < a.mul);
2226 }
2227
2228 /**
2229 * Combines two alignments to produce a least multiple of sorts.
2230 *
2231 * The returned alignment is the smallest (in terms of multiplier) such that
2232 * anything aligned to both a and b will be aligned to the new alignment.
2233 * This function will assert-fail if a and b are not compatible, i.e. if the
2234 * offset parameters are such that no common alignment is possible.
2235 */
2236 static struct cplx_align
2237 cplx_align_combine(struct cplx_align a, struct cplx_align b)
2238 {
2239 cplx_align_assert_sane(a);
2240 cplx_align_assert_sane(b);
2241
2242 /* Assert that the alignments agree. */
2243 assert((a.offset & (b.mul - 1)) == (b.offset & (a.mul - 1)));
2244
2245 return a.mul > b.mul ? a : b;
2246 }
2247
2248 /**
2249 * Apply a complex alignment
2250 *
2251 * This function will return the smallest number greater than or equal to
2252 * offset that is aligned to align.
2253 */
2254 static unsigned
2255 cplx_align_apply(struct cplx_align align, unsigned offset)
2256 {
2257 return ALIGN(offset - align.offset, align.mul) + align.offset;
2258 }
2259
2260 #define UNIFORM_SLOT_SIZE 4
2261
2262 struct uniform_slot_info {
2263 /** True if the given uniform slot is live */
2264 unsigned is_live:1;
2265
2266 /** True if this slot and the next slot must remain contiguous */
2267 unsigned contiguous:1;
2268
2269 struct cplx_align align;
2270 };
2271
2272 static void
2273 mark_uniform_slots_read(struct uniform_slot_info *slots,
2274 unsigned num_slots, unsigned alignment)
2275 {
2276 assert(alignment > 0 && util_is_power_of_two_nonzero(alignment));
2277 assert(alignment <= CPLX_ALIGN_MAX_MUL);
2278
2279 /* We can't align a slot to anything less than the slot size */
2280 alignment = MAX2(alignment, UNIFORM_SLOT_SIZE);
2281
2282 struct cplx_align align = {alignment, 0};
2283 cplx_align_assert_sane(align);
2284
2285 for (unsigned i = 0; i < num_slots; i++) {
2286 slots[i].is_live = true;
2287 if (i < num_slots - 1)
2288 slots[i].contiguous = true;
2289
2290 align.offset = (i * UNIFORM_SLOT_SIZE) & (align.mul - 1);
2291 if (slots[i].align.mul == 0) {
2292 slots[i].align = align;
2293 } else {
2294 slots[i].align = cplx_align_combine(slots[i].align, align);
2295 }
2296 }
2297 }
2298
2299 /**
2300 * Assign UNIFORM file registers to either push constants or pull constants.
2301 *
2302 * We allow a fragment shader to have more than the specified minimum
2303 * maximum number of fragment shader uniform components (64). If
2304 * there are too many of these, they'd fill up all of register space.
2305 * So, this will push some of them out to the pull constant buffer and
2306 * update the program to load them.
2307 */
2308 void
2309 fs_visitor::assign_constant_locations()
2310 {
2311 /* Only the first compile gets to decide on locations. */
2312 if (push_constant_loc) {
2313 assert(pull_constant_loc);
2314 return;
2315 }
2316
2317 if (compiler->compact_params) {
2318 struct uniform_slot_info slots[uniforms + 1];
2319 memset(slots, 0, sizeof(slots));
2320
2321 foreach_block_and_inst_safe(block, fs_inst, inst, cfg) {
2322 for (int i = 0 ; i < inst->sources; i++) {
2323 if (inst->src[i].file != UNIFORM)
2324 continue;
2325
2326 /* NIR tightly packs things so the uniform number might not be
2327 * aligned (if we have a double right after a float, for
2328 * instance). This is fine because the process of re-arranging
2329 * them will ensure that things are properly aligned. The offset
2330 * into that uniform, however, must be aligned.
2331 *
2332 * In Vulkan, we have explicit offsets but everything is crammed
2333 * into a single "variable" so inst->src[i].nr will always be 0.
2334 * Everything will be properly aligned relative to that one base.
2335 */
2336 assert(inst->src[i].offset % type_sz(inst->src[i].type) == 0);
2337
2338 unsigned u = inst->src[i].nr +
2339 inst->src[i].offset / UNIFORM_SLOT_SIZE;
2340
2341 if (u >= uniforms)
2342 continue;
2343
2344 unsigned slots_read;
2345 if (inst->opcode == SHADER_OPCODE_MOV_INDIRECT && i == 0) {
2346 slots_read = DIV_ROUND_UP(inst->src[2].ud, UNIFORM_SLOT_SIZE);
2347 } else {
2348 unsigned bytes_read = inst->components_read(i) *
2349 type_sz(inst->src[i].type);
2350 slots_read = DIV_ROUND_UP(bytes_read, UNIFORM_SLOT_SIZE);
2351 }
2352
2353 assert(u + slots_read <= uniforms);
2354 mark_uniform_slots_read(&slots[u], slots_read,
2355 type_sz(inst->src[i].type));
2356 }
2357 }
2358
2359 int subgroup_id_index = get_subgroup_id_param_index(stage_prog_data);
2360
2361 /* Only allow 16 registers (128 uniform components) as push constants.
2362 *
2363 * Just demote the end of the list. We could probably do better
2364 * here, demoting things that are rarely used in the program first.
2365 *
2366 * If changing this value, note the limitation about total_regs in
2367 * brw_curbe.c.
2368 */
2369 unsigned int max_push_components = 16 * 8;
2370 if (subgroup_id_index >= 0)
2371 max_push_components--; /* Save a slot for the thread ID */
2372
2373 /* We push small arrays, but no bigger than 16 floats. This is big
2374 * enough for a vec4 but hopefully not large enough to push out other
2375 * stuff. We should probably use a better heuristic at some point.
2376 */
2377 const unsigned int max_chunk_size = 16;
2378
2379 unsigned int num_push_constants = 0;
2380 unsigned int num_pull_constants = 0;
2381
2382 push_constant_loc = ralloc_array(mem_ctx, int, uniforms);
2383 pull_constant_loc = ralloc_array(mem_ctx, int, uniforms);
2384
2385 /* Default to -1 meaning no location */
2386 memset(push_constant_loc, -1, uniforms * sizeof(*push_constant_loc));
2387 memset(pull_constant_loc, -1, uniforms * sizeof(*pull_constant_loc));
2388
2389 int chunk_start = -1;
2390 struct cplx_align align;
2391 for (unsigned u = 0; u < uniforms; u++) {
2392 if (!slots[u].is_live) {
2393 assert(chunk_start == -1);
2394 continue;
2395 }
2396
2397 /* Skip subgroup_id_index to put it in the last push register. */
2398 if (subgroup_id_index == (int)u)
2399 continue;
2400
2401 if (chunk_start == -1) {
2402 chunk_start = u;
2403 align = slots[u].align;
2404 } else {
2405 /* Offset into the chunk */
2406 unsigned chunk_offset = (u - chunk_start) * UNIFORM_SLOT_SIZE;
2407
2408 /* Shift the slot alignment down by the chunk offset so it is
2409 * comparable with the base chunk alignment.
2410 */
2411 struct cplx_align slot_align = slots[u].align;
2412 slot_align.offset =
2413 (slot_align.offset - chunk_offset) & (align.mul - 1);
2414
2415 align = cplx_align_combine(align, slot_align);
2416 }
2417
2418 /* Sanity check the alignment */
2419 cplx_align_assert_sane(align);
2420
2421 if (slots[u].contiguous)
2422 continue;
2423
2424 /* Adjust the alignment to be in terms of slots, not bytes */
2425 assert((align.mul & (UNIFORM_SLOT_SIZE - 1)) == 0);
2426 assert((align.offset & (UNIFORM_SLOT_SIZE - 1)) == 0);
2427 align.mul /= UNIFORM_SLOT_SIZE;
2428 align.offset /= UNIFORM_SLOT_SIZE;
2429
2430 unsigned push_start_align = cplx_align_apply(align, num_push_constants);
2431 unsigned chunk_size = u - chunk_start + 1;
2432 if ((!compiler->supports_pull_constants && u < UBO_START) ||
2433 (chunk_size < max_chunk_size &&
2434 push_start_align + chunk_size <= max_push_components)) {
2435 /* Align up the number of push constants */
2436 num_push_constants = push_start_align;
2437 for (unsigned i = 0; i < chunk_size; i++)
2438 push_constant_loc[chunk_start + i] = num_push_constants++;
2439 } else {
2440 /* We need to pull this one */
2441 num_pull_constants = cplx_align_apply(align, num_pull_constants);
2442 for (unsigned i = 0; i < chunk_size; i++)
2443 pull_constant_loc[chunk_start + i] = num_pull_constants++;
2444 }
2445
2446 /* Reset the chunk and start again */
2447 chunk_start = -1;
2448 }
2449
2450 /* Add the CS local thread ID uniform at the end of the push constants */
2451 if (subgroup_id_index >= 0)
2452 push_constant_loc[subgroup_id_index] = num_push_constants++;
2453
2454 /* As the uniforms are going to be reordered, stash the old array and
2455 * create two new arrays for push/pull params.
2456 */
2457 uint32_t *param = stage_prog_data->param;
2458 stage_prog_data->nr_params = num_push_constants;
2459 if (num_push_constants) {
2460 stage_prog_data->param = rzalloc_array(mem_ctx, uint32_t,
2461 num_push_constants);
2462 } else {
2463 stage_prog_data->param = NULL;
2464 }
2465 assert(stage_prog_data->nr_pull_params == 0);
2466 assert(stage_prog_data->pull_param == NULL);
2467 if (num_pull_constants > 0) {
2468 stage_prog_data->nr_pull_params = num_pull_constants;
2469 stage_prog_data->pull_param = rzalloc_array(mem_ctx, uint32_t,
2470 num_pull_constants);
2471 }
2472
2473 /* Up until now, the param[] array has been indexed by reg + offset
2474 * of UNIFORM registers. Move pull constants into pull_param[] and
2475 * condense param[] to only contain the uniforms we chose to push.
2476 *
2477 * NOTE: Because we are condensing the params[] array, we know that
2478 * push_constant_loc[i] <= i and we can do it in one smooth loop without
2479 * having to make a copy.
2480 */
2481 for (unsigned int i = 0; i < uniforms; i++) {
2482 uint32_t value = param[i];
2483 if (pull_constant_loc[i] != -1) {
2484 stage_prog_data->pull_param[pull_constant_loc[i]] = value;
2485 } else if (push_constant_loc[i] != -1) {
2486 stage_prog_data->param[push_constant_loc[i]] = value;
2487 }
2488 }
2489 ralloc_free(param);
2490 } else {
2491 /* If we don't want to compact anything, just set up dummy push/pull
2492 * arrays. All the rest of the compiler cares about are these arrays.
2493 */
2494 push_constant_loc = ralloc_array(mem_ctx, int, uniforms);
2495 pull_constant_loc = ralloc_array(mem_ctx, int, uniforms);
2496
2497 for (unsigned u = 0; u < uniforms; u++)
2498 push_constant_loc[u] = u;
2499
2500 memset(pull_constant_loc, -1, uniforms * sizeof(*pull_constant_loc));
2501 }
2502
2503 /* Now that we know how many regular uniforms we'll push, reduce the
2504 * UBO push ranges so we don't exceed the 3DSTATE_CONSTANT limits.
2505 */
2506 unsigned push_length = DIV_ROUND_UP(stage_prog_data->nr_params, 8);
2507 for (int i = 0; i < 4; i++) {
2508 struct brw_ubo_range *range = &prog_data->ubo_ranges[i];
2509
2510 if (push_length + range->length > 64)
2511 range->length = 64 - push_length;
2512
2513 push_length += range->length;
2514 }
2515 assert(push_length <= 64);
2516 }
2517
2518 bool
2519 fs_visitor::get_pull_locs(const fs_reg &src,
2520 unsigned *out_surf_index,
2521 unsigned *out_pull_index)
2522 {
2523 assert(src.file == UNIFORM);
2524
2525 if (src.nr >= UBO_START) {
2526 const struct brw_ubo_range *range =
2527 &prog_data->ubo_ranges[src.nr - UBO_START];
2528
2529 /* If this access is in our (reduced) range, use the push data. */
2530 if (src.offset / 32 < range->length)
2531 return false;
2532
2533 *out_surf_index = prog_data->binding_table.ubo_start + range->block;
2534 *out_pull_index = (32 * range->start + src.offset) / 4;
2535
2536 prog_data->has_ubo_pull = true;
2537 return true;
2538 }
2539
2540 const unsigned location = src.nr + src.offset / 4;
2541
2542 if (location < uniforms && pull_constant_loc[location] != -1) {
2543 /* A regular uniform push constant */
2544 *out_surf_index = stage_prog_data->binding_table.pull_constants_start;
2545 *out_pull_index = pull_constant_loc[location];
2546
2547 prog_data->has_ubo_pull = true;
2548 return true;
2549 }
2550
2551 return false;
2552 }
2553
2554 /**
2555 * Replace UNIFORM register file access with either UNIFORM_PULL_CONSTANT_LOAD
2556 * or VARYING_PULL_CONSTANT_LOAD instructions which load values into VGRFs.
2557 */
2558 void
2559 fs_visitor::lower_constant_loads()
2560 {
2561 unsigned index, pull_index;
2562
2563 foreach_block_and_inst_safe (block, fs_inst, inst, cfg) {
2564 /* Set up the annotation tracking for new generated instructions. */
2565 const fs_builder ibld(this, block, inst);
2566
2567 for (int i = 0; i < inst->sources; i++) {
2568 if (inst->src[i].file != UNIFORM)
2569 continue;
2570
2571 /* We'll handle this case later */
2572 if (inst->opcode == SHADER_OPCODE_MOV_INDIRECT && i == 0)
2573 continue;
2574
2575 if (!get_pull_locs(inst->src[i], &index, &pull_index))
2576 continue;
2577
2578 assert(inst->src[i].stride == 0);
2579
2580 const unsigned block_sz = 64; /* Fetch one cacheline at a time. */
2581 const fs_builder ubld = ibld.exec_all().group(block_sz / 4, 0);
2582 const fs_reg dst = ubld.vgrf(BRW_REGISTER_TYPE_UD);
2583 const unsigned base = pull_index * 4;
2584
2585 ubld.emit(FS_OPCODE_UNIFORM_PULL_CONSTANT_LOAD,
2586 dst, brw_imm_ud(index), brw_imm_ud(base & ~(block_sz - 1)));
2587
2588 /* Rewrite the instruction to use the temporary VGRF. */
2589 inst->src[i].file = VGRF;
2590 inst->src[i].nr = dst.nr;
2591 inst->src[i].offset = (base & (block_sz - 1)) +
2592 inst->src[i].offset % 4;
2593 }
2594
2595 if (inst->opcode == SHADER_OPCODE_MOV_INDIRECT &&
2596 inst->src[0].file == UNIFORM) {
2597
2598 if (!get_pull_locs(inst->src[0], &index, &pull_index))
2599 continue;
2600
2601 VARYING_PULL_CONSTANT_LOAD(ibld, inst->dst,
2602 brw_imm_ud(index),
2603 inst->src[1],
2604 pull_index * 4);
2605 inst->remove(block);
2606 }
2607 }
2608 invalidate_analysis(DEPENDENCY_INSTRUCTIONS);
2609 }
2610
2611 bool
2612 fs_visitor::opt_algebraic()
2613 {
2614 bool progress = false;
2615
2616 foreach_block_and_inst_safe(block, fs_inst, inst, cfg) {
2617 switch (inst->opcode) {
2618 case BRW_OPCODE_MOV:
2619 if (!devinfo->has_64bit_float &&
2620 !devinfo->has_64bit_int &&
2621 (inst->dst.type == BRW_REGISTER_TYPE_DF ||
2622 inst->dst.type == BRW_REGISTER_TYPE_UQ ||
2623 inst->dst.type == BRW_REGISTER_TYPE_Q)) {
2624 assert(inst->dst.type == inst->src[0].type);
2625 assert(!inst->saturate);
2626 assert(!inst->src[0].abs);
2627 assert(!inst->src[0].negate);
2628 const brw::fs_builder ibld(this, block, inst);
2629
2630 if (inst->src[0].file == IMM) {
2631 ibld.MOV(subscript(inst->dst, BRW_REGISTER_TYPE_UD, 1),
2632 brw_imm_ud(inst->src[0].u64 >> 32));
2633 ibld.MOV(subscript(inst->dst, BRW_REGISTER_TYPE_UD, 0),
2634 brw_imm_ud(inst->src[0].u64));
2635 } else {
2636 ibld.MOV(subscript(inst->dst, BRW_REGISTER_TYPE_UD, 1),
2637 subscript(inst->src[0], BRW_REGISTER_TYPE_UD, 1));
2638 ibld.MOV(subscript(inst->dst, BRW_REGISTER_TYPE_UD, 0),
2639 subscript(inst->src[0], BRW_REGISTER_TYPE_UD, 0));
2640 }
2641
2642 inst->remove(block);
2643 progress = true;
2644 }
2645
2646 if ((inst->conditional_mod == BRW_CONDITIONAL_Z ||
2647 inst->conditional_mod == BRW_CONDITIONAL_NZ) &&
2648 inst->dst.is_null() &&
2649 (inst->src[0].abs || inst->src[0].negate)) {
2650 inst->src[0].abs = false;
2651 inst->src[0].negate = false;
2652 progress = true;
2653 break;
2654 }
2655
2656 if (inst->src[0].file != IMM)
2657 break;
2658
2659 if (inst->saturate) {
2660 /* Full mixed-type saturates don't happen. However, we can end up
2661 * with things like:
2662 *
2663 * mov.sat(8) g21<1>DF -1F
2664 *
2665 * Other mixed-size-but-same-base-type cases may also be possible.
2666 */
2667 if (inst->dst.type != inst->src[0].type &&
2668 inst->dst.type != BRW_REGISTER_TYPE_DF &&
2669 inst->src[0].type != BRW_REGISTER_TYPE_F)
2670 assert(!"unimplemented: saturate mixed types");
2671
2672 if (brw_saturate_immediate(inst->src[0].type,
2673 &inst->src[0].as_brw_reg())) {
2674 inst->saturate = false;
2675 progress = true;
2676 }
2677 }
2678 break;
2679
2680 case BRW_OPCODE_MUL:
2681 if (inst->src[1].file != IMM)
2682 continue;
2683
2684 /* a * 1.0 = a */
2685 if (inst->src[1].is_one()) {
2686 inst->opcode = BRW_OPCODE_MOV;
2687 inst->src[1] = reg_undef;
2688 progress = true;
2689 break;
2690 }
2691
2692 /* a * -1.0 = -a */
2693 if (inst->src[1].is_negative_one()) {
2694 inst->opcode = BRW_OPCODE_MOV;
2695 inst->src[0].negate = !inst->src[0].negate;
2696 inst->src[1] = reg_undef;
2697 progress = true;
2698 break;
2699 }
2700
2701 if (inst->src[0].file == IMM) {
2702 assert(inst->src[0].type == BRW_REGISTER_TYPE_F);
2703 inst->opcode = BRW_OPCODE_MOV;
2704 inst->src[0].f *= inst->src[1].f;
2705 inst->src[1] = reg_undef;
2706 progress = true;
2707 break;
2708 }
2709 break;
2710 case BRW_OPCODE_ADD:
2711 if (inst->src[1].file != IMM)
2712 continue;
2713
2714 if (inst->src[0].file == IMM) {
2715 assert(inst->src[0].type == BRW_REGISTER_TYPE_F);
2716 inst->opcode = BRW_OPCODE_MOV;
2717 inst->src[0].f += inst->src[1].f;
2718 inst->src[1] = reg_undef;
2719 progress = true;
2720 break;
2721 }
2722 break;
2723 case BRW_OPCODE_OR:
2724 if (inst->src[0].equals(inst->src[1]) ||
2725 inst->src[1].is_zero()) {
2726 /* On Gen8+, the OR instruction can have a source modifier that
2727 * performs logical not on the operand. Cases of 'OR r0, ~r1, 0'
2728 * or 'OR r0, ~r1, ~r1' should become a NOT instead of a MOV.
2729 */
2730 if (inst->src[0].negate) {
2731 inst->opcode = BRW_OPCODE_NOT;
2732 inst->src[0].negate = false;
2733 } else {
2734 inst->opcode = BRW_OPCODE_MOV;
2735 }
2736 inst->src[1] = reg_undef;
2737 progress = true;
2738 break;
2739 }
2740 break;
2741 case BRW_OPCODE_CMP:
2742 if ((inst->conditional_mod == BRW_CONDITIONAL_Z ||
2743 inst->conditional_mod == BRW_CONDITIONAL_NZ) &&
2744 inst->src[1].is_zero() &&
2745 (inst->src[0].abs || inst->src[0].negate)) {
2746 inst->src[0].abs = false;
2747 inst->src[0].negate = false;
2748 progress = true;
2749 break;
2750 }
2751 break;
2752 case BRW_OPCODE_SEL:
2753 if (!devinfo->has_64bit_float &&
2754 !devinfo->has_64bit_int &&
2755 (inst->dst.type == BRW_REGISTER_TYPE_DF ||
2756 inst->dst.type == BRW_REGISTER_TYPE_UQ ||
2757 inst->dst.type == BRW_REGISTER_TYPE_Q)) {
2758 assert(inst->dst.type == inst->src[0].type);
2759 assert(!inst->saturate);
2760 assert(!inst->src[0].abs && !inst->src[0].negate);
2761 assert(!inst->src[1].abs && !inst->src[1].negate);
2762 const brw::fs_builder ibld(this, block, inst);
2763
2764 set_predicate(inst->predicate,
2765 ibld.SEL(subscript(inst->dst, BRW_REGISTER_TYPE_UD, 0),
2766 subscript(inst->src[0], BRW_REGISTER_TYPE_UD, 0),
2767 subscript(inst->src[1], BRW_REGISTER_TYPE_UD, 0)));
2768 set_predicate(inst->predicate,
2769 ibld.SEL(subscript(inst->dst, BRW_REGISTER_TYPE_UD, 1),
2770 subscript(inst->src[0], BRW_REGISTER_TYPE_UD, 1),
2771 subscript(inst->src[1], BRW_REGISTER_TYPE_UD, 1)));
2772
2773 inst->remove(block);
2774 progress = true;
2775 }
2776 if (inst->src[0].equals(inst->src[1])) {
2777 inst->opcode = BRW_OPCODE_MOV;
2778 inst->src[1] = reg_undef;
2779 inst->predicate = BRW_PREDICATE_NONE;
2780 inst->predicate_inverse = false;
2781 progress = true;
2782 } else if (inst->saturate && inst->src[1].file == IMM) {
2783 switch (inst->conditional_mod) {
2784 case BRW_CONDITIONAL_LE:
2785 case BRW_CONDITIONAL_L:
2786 switch (inst->src[1].type) {
2787 case BRW_REGISTER_TYPE_F:
2788 if (inst->src[1].f >= 1.0f) {
2789 inst->opcode = BRW_OPCODE_MOV;
2790 inst->src[1] = reg_undef;
2791 inst->conditional_mod = BRW_CONDITIONAL_NONE;
2792 progress = true;
2793 }
2794 break;
2795 default:
2796 break;
2797 }
2798 break;
2799 case BRW_CONDITIONAL_GE:
2800 case BRW_CONDITIONAL_G:
2801 switch (inst->src[1].type) {
2802 case BRW_REGISTER_TYPE_F:
2803 if (inst->src[1].f <= 0.0f) {
2804 inst->opcode = BRW_OPCODE_MOV;
2805 inst->src[1] = reg_undef;
2806 inst->conditional_mod = BRW_CONDITIONAL_NONE;
2807 progress = true;
2808 }
2809 break;
2810 default:
2811 break;
2812 }
2813 default:
2814 break;
2815 }
2816 }
2817 break;
2818 case BRW_OPCODE_MAD:
2819 if (inst->src[0].type != BRW_REGISTER_TYPE_F ||
2820 inst->src[1].type != BRW_REGISTER_TYPE_F ||
2821 inst->src[2].type != BRW_REGISTER_TYPE_F)
2822 break;
2823 if (inst->src[1].is_one()) {
2824 inst->opcode = BRW_OPCODE_ADD;
2825 inst->src[1] = inst->src[2];
2826 inst->src[2] = reg_undef;
2827 progress = true;
2828 } else if (inst->src[2].is_one()) {
2829 inst->opcode = BRW_OPCODE_ADD;
2830 inst->src[2] = reg_undef;
2831 progress = true;
2832 }
2833 break;
2834 case SHADER_OPCODE_BROADCAST:
2835 if (is_uniform(inst->src[0])) {
2836 inst->opcode = BRW_OPCODE_MOV;
2837 inst->sources = 1;
2838 inst->force_writemask_all = true;
2839 progress = true;
2840 } else if (inst->src[1].file == IMM) {
2841 inst->opcode = BRW_OPCODE_MOV;
2842 /* It's possible that the selected component will be too large and
2843 * overflow the register. This can happen if someone does a
2844 * readInvocation() from GLSL or SPIR-V and provides an OOB
2845 * invocationIndex. If this happens and we some how manage
2846 * to constant fold it in and get here, then component() may cause
2847 * us to start reading outside of the VGRF which will lead to an
2848 * assert later. Instead, just let it wrap around if it goes over
2849 * exec_size.
2850 */
2851 const unsigned comp = inst->src[1].ud & (inst->exec_size - 1);
2852 inst->src[0] = component(inst->src[0], comp);
2853 inst->sources = 1;
2854 inst->force_writemask_all = true;
2855 progress = true;
2856 }
2857 break;
2858
2859 case SHADER_OPCODE_SHUFFLE:
2860 if (is_uniform(inst->src[0])) {
2861 inst->opcode = BRW_OPCODE_MOV;
2862 inst->sources = 1;
2863 progress = true;
2864 } else if (inst->src[1].file == IMM) {
2865 inst->opcode = BRW_OPCODE_MOV;
2866 inst->src[0] = component(inst->src[0],
2867 inst->src[1].ud);
2868 inst->sources = 1;
2869 progress = true;
2870 }
2871 break;
2872
2873 default:
2874 break;
2875 }
2876
2877 /* Swap if src[0] is immediate. */
2878 if (progress && inst->is_commutative()) {
2879 if (inst->src[0].file == IMM) {
2880 fs_reg tmp = inst->src[1];
2881 inst->src[1] = inst->src[0];
2882 inst->src[0] = tmp;
2883 }
2884 }
2885 }
2886
2887 if (progress)
2888 invalidate_analysis(DEPENDENCY_INSTRUCTION_DATA_FLOW |
2889 DEPENDENCY_INSTRUCTION_DETAIL);
2890
2891 return progress;
2892 }
2893
2894 /**
2895 * Optimize sample messages that have constant zero values for the trailing
2896 * texture coordinates. We can just reduce the message length for these
2897 * instructions instead of reserving a register for it. Trailing parameters
2898 * that aren't sent default to zero anyway. This will cause the dead code
2899 * eliminator to remove the MOV instruction that would otherwise be emitted to
2900 * set up the zero value.
2901 */
2902 bool
2903 fs_visitor::opt_zero_samples()
2904 {
2905 /* Gen4 infers the texturing opcode based on the message length so we can't
2906 * change it.
2907 */
2908 if (devinfo->gen < 5)
2909 return false;
2910
2911 bool progress = false;
2912
2913 foreach_block_and_inst(block, fs_inst, inst, cfg) {
2914 if (!inst->is_tex())
2915 continue;
2916
2917 fs_inst *load_payload = (fs_inst *) inst->prev;
2918
2919 if (load_payload->is_head_sentinel() ||
2920 load_payload->opcode != SHADER_OPCODE_LOAD_PAYLOAD)
2921 continue;
2922
2923 /* We don't want to remove the message header or the first parameter.
2924 * Removing the first parameter is not allowed, see the Haswell PRM
2925 * volume 7, page 149:
2926 *
2927 * "Parameter 0 is required except for the sampleinfo message, which
2928 * has no parameter 0"
2929 */
2930 while (inst->mlen > inst->header_size + inst->exec_size / 8 &&
2931 load_payload->src[(inst->mlen - inst->header_size) /
2932 (inst->exec_size / 8) +
2933 inst->header_size - 1].is_zero()) {
2934 inst->mlen -= inst->exec_size / 8;
2935 progress = true;
2936 }
2937 }
2938
2939 if (progress)
2940 invalidate_analysis(DEPENDENCY_INSTRUCTION_DETAIL);
2941
2942 return progress;
2943 }
2944
2945 /**
2946 * Optimize sample messages which are followed by the final RT write.
2947 *
2948 * CHV, and GEN9+ can mark a texturing SEND instruction with EOT to have its
2949 * results sent directly to the framebuffer, bypassing the EU. Recognize the
2950 * final texturing results copied to the framebuffer write payload and modify
2951 * them to write to the framebuffer directly.
2952 */
2953 bool
2954 fs_visitor::opt_sampler_eot()
2955 {
2956 brw_wm_prog_key *key = (brw_wm_prog_key*) this->key;
2957
2958 if (stage != MESA_SHADER_FRAGMENT || dispatch_width > 16)
2959 return false;
2960
2961 if (devinfo->gen != 9 && !devinfo->is_cherryview)
2962 return false;
2963
2964 /* FINISHME: It should be possible to implement this optimization when there
2965 * are multiple drawbuffers.
2966 */
2967 if (key->nr_color_regions != 1)
2968 return false;
2969
2970 /* Requires emitting a bunch of saturating MOV instructions during logical
2971 * send lowering to clamp the color payload, which the sampler unit isn't
2972 * going to do for us.
2973 */
2974 if (key->clamp_fragment_color)
2975 return false;
2976
2977 /* Look for a texturing instruction immediately before the final FB_WRITE. */
2978 bblock_t *block = cfg->blocks[cfg->num_blocks - 1];
2979 fs_inst *fb_write = (fs_inst *)block->end();
2980 assert(fb_write->eot);
2981 assert(fb_write->opcode == FS_OPCODE_FB_WRITE_LOGICAL);
2982
2983 /* There wasn't one; nothing to do. */
2984 if (unlikely(fb_write->prev->is_head_sentinel()))
2985 return false;
2986
2987 fs_inst *tex_inst = (fs_inst *) fb_write->prev;
2988
2989 /* 3D Sampler » Messages » Message Format
2990 *
2991 * “Response Length of zero is allowed on all SIMD8* and SIMD16* sampler
2992 * messages except sample+killpix, resinfo, sampleinfo, LOD, and gather4*”
2993 */
2994 if (tex_inst->opcode != SHADER_OPCODE_TEX_LOGICAL &&
2995 tex_inst->opcode != SHADER_OPCODE_TXD_LOGICAL &&
2996 tex_inst->opcode != SHADER_OPCODE_TXF_LOGICAL &&
2997 tex_inst->opcode != SHADER_OPCODE_TXL_LOGICAL &&
2998 tex_inst->opcode != FS_OPCODE_TXB_LOGICAL &&
2999 tex_inst->opcode != SHADER_OPCODE_TXF_CMS_LOGICAL &&
3000 tex_inst->opcode != SHADER_OPCODE_TXF_CMS_W_LOGICAL &&
3001 tex_inst->opcode != SHADER_OPCODE_TXF_UMS_LOGICAL)
3002 return false;
3003
3004 /* XXX - This shouldn't be necessary. */
3005 if (tex_inst->prev->is_head_sentinel())
3006 return false;
3007
3008 /* Check that the FB write sources are fully initialized by the single
3009 * texturing instruction.
3010 */
3011 for (unsigned i = 0; i < FB_WRITE_LOGICAL_NUM_SRCS; i++) {
3012 if (i == FB_WRITE_LOGICAL_SRC_COLOR0) {
3013 if (!fb_write->src[i].equals(tex_inst->dst) ||
3014 fb_write->size_read(i) != tex_inst->size_written)
3015 return false;
3016 } else if (i != FB_WRITE_LOGICAL_SRC_COMPONENTS) {
3017 if (fb_write->src[i].file != BAD_FILE)
3018 return false;
3019 }
3020 }
3021
3022 assert(!tex_inst->eot); /* We can't get here twice */
3023 assert((tex_inst->offset & (0xff << 24)) == 0);
3024
3025 const fs_builder ibld(this, block, tex_inst);
3026
3027 tex_inst->offset |= fb_write->target << 24;
3028 tex_inst->eot = true;
3029 tex_inst->dst = ibld.null_reg_ud();
3030 tex_inst->size_written = 0;
3031 fb_write->remove(cfg->blocks[cfg->num_blocks - 1]);
3032
3033 /* Marking EOT is sufficient, lower_logical_sends() will notice the EOT
3034 * flag and submit a header together with the sampler message as required
3035 * by the hardware.
3036 */
3037 invalidate_analysis(DEPENDENCY_INSTRUCTIONS | DEPENDENCY_VARIABLES);
3038 return true;
3039 }
3040
3041 bool
3042 fs_visitor::opt_register_renaming()
3043 {
3044 bool progress = false;
3045 int depth = 0;
3046
3047 unsigned remap[alloc.count];
3048 memset(remap, ~0u, sizeof(unsigned) * alloc.count);
3049
3050 foreach_block_and_inst(block, fs_inst, inst, cfg) {
3051 if (inst->opcode == BRW_OPCODE_IF || inst->opcode == BRW_OPCODE_DO) {
3052 depth++;
3053 } else if (inst->opcode == BRW_OPCODE_ENDIF ||
3054 inst->opcode == BRW_OPCODE_WHILE) {
3055 depth--;
3056 }
3057
3058 /* Rewrite instruction sources. */
3059 for (int i = 0; i < inst->sources; i++) {
3060 if (inst->src[i].file == VGRF &&
3061 remap[inst->src[i].nr] != ~0u &&
3062 remap[inst->src[i].nr] != inst->src[i].nr) {
3063 inst->src[i].nr = remap[inst->src[i].nr];
3064 progress = true;
3065 }
3066 }
3067
3068 const unsigned dst = inst->dst.nr;
3069
3070 if (depth == 0 &&
3071 inst->dst.file == VGRF &&
3072 alloc.sizes[inst->dst.nr] * REG_SIZE == inst->size_written &&
3073 !inst->is_partial_write()) {
3074 if (remap[dst] == ~0u) {
3075 remap[dst] = dst;
3076 } else {
3077 remap[dst] = alloc.allocate(regs_written(inst));
3078 inst->dst.nr = remap[dst];
3079 progress = true;
3080 }
3081 } else if (inst->dst.file == VGRF &&
3082 remap[dst] != ~0u &&
3083 remap[dst] != dst) {
3084 inst->dst.nr = remap[dst];
3085 progress = true;
3086 }
3087 }
3088
3089 if (progress) {
3090 invalidate_analysis(DEPENDENCY_INSTRUCTION_DETAIL |
3091 DEPENDENCY_VARIABLES);
3092
3093 for (unsigned i = 0; i < ARRAY_SIZE(delta_xy); i++) {
3094 if (delta_xy[i].file == VGRF && remap[delta_xy[i].nr] != ~0u) {
3095 delta_xy[i].nr = remap[delta_xy[i].nr];
3096 }
3097 }
3098 }
3099
3100 return progress;
3101 }
3102
3103 /**
3104 * Remove redundant or useless discard jumps.
3105 *
3106 * For example, we can eliminate jumps in the following sequence:
3107 *
3108 * discard-jump (redundant with the next jump)
3109 * discard-jump (useless; jumps to the next instruction)
3110 * placeholder-halt
3111 */
3112 bool
3113 fs_visitor::opt_redundant_discard_jumps()
3114 {
3115 bool progress = false;
3116
3117 bblock_t *last_bblock = cfg->blocks[cfg->num_blocks - 1];
3118
3119 fs_inst *placeholder_halt = NULL;
3120 foreach_inst_in_block_reverse(fs_inst, inst, last_bblock) {
3121 if (inst->opcode == FS_OPCODE_PLACEHOLDER_HALT) {
3122 placeholder_halt = inst;
3123 break;
3124 }
3125 }
3126
3127 if (!placeholder_halt)
3128 return false;
3129
3130 /* Delete any HALTs immediately before the placeholder halt. */
3131 for (fs_inst *prev = (fs_inst *) placeholder_halt->prev;
3132 !prev->is_head_sentinel() && prev->opcode == FS_OPCODE_DISCARD_JUMP;
3133 prev = (fs_inst *) placeholder_halt->prev) {
3134 prev->remove(last_bblock);
3135 progress = true;
3136 }
3137
3138 if (progress)
3139 invalidate_analysis(DEPENDENCY_INSTRUCTIONS);
3140
3141 return progress;
3142 }
3143
3144 /**
3145 * Compute a bitmask with GRF granularity with a bit set for each GRF starting
3146 * from \p r.offset which overlaps the region starting at \p s.offset and
3147 * spanning \p ds bytes.
3148 */
3149 static inline unsigned
3150 mask_relative_to(const fs_reg &r, const fs_reg &s, unsigned ds)
3151 {
3152 const int rel_offset = reg_offset(s) - reg_offset(r);
3153 const int shift = rel_offset / REG_SIZE;
3154 const unsigned n = DIV_ROUND_UP(rel_offset % REG_SIZE + ds, REG_SIZE);
3155 assert(reg_space(r) == reg_space(s) &&
3156 shift >= 0 && shift < int(8 * sizeof(unsigned)));
3157 return ((1 << n) - 1) << shift;
3158 }
3159
3160 bool
3161 fs_visitor::compute_to_mrf()
3162 {
3163 bool progress = false;
3164 int next_ip = 0;
3165
3166 /* No MRFs on Gen >= 7. */
3167 if (devinfo->gen >= 7)
3168 return false;
3169
3170 const fs_live_variables &live = live_analysis.require();
3171
3172 foreach_block_and_inst_safe(block, fs_inst, inst, cfg) {
3173 int ip = next_ip;
3174 next_ip++;
3175
3176 if (inst->opcode != BRW_OPCODE_MOV ||
3177 inst->is_partial_write() ||
3178 inst->dst.file != MRF || inst->src[0].file != VGRF ||
3179 inst->dst.type != inst->src[0].type ||
3180 inst->src[0].abs || inst->src[0].negate ||
3181 !inst->src[0].is_contiguous() ||
3182 inst->src[0].offset % REG_SIZE != 0)
3183 continue;
3184
3185 /* Can't compute-to-MRF this GRF if someone else was going to
3186 * read it later.
3187 */
3188 if (live.vgrf_end[inst->src[0].nr] > ip)
3189 continue;
3190
3191 /* Found a move of a GRF to a MRF. Let's see if we can go rewrite the
3192 * things that computed the value of all GRFs of the source region. The
3193 * regs_left bitset keeps track of the registers we haven't yet found a
3194 * generating instruction for.
3195 */
3196 unsigned regs_left = (1 << regs_read(inst, 0)) - 1;
3197
3198 foreach_inst_in_block_reverse_starting_from(fs_inst, scan_inst, inst) {
3199 if (regions_overlap(scan_inst->dst, scan_inst->size_written,
3200 inst->src[0], inst->size_read(0))) {
3201 /* Found the last thing to write our reg we want to turn
3202 * into a compute-to-MRF.
3203 */
3204
3205 /* If this one instruction didn't populate all the
3206 * channels, bail. We might be able to rewrite everything
3207 * that writes that reg, but it would require smarter
3208 * tracking.
3209 */
3210 if (scan_inst->is_partial_write())
3211 break;
3212
3213 /* Handling things not fully contained in the source of the copy
3214 * would need us to understand coalescing out more than one MOV at
3215 * a time.
3216 */
3217 if (!region_contained_in(scan_inst->dst, scan_inst->size_written,
3218 inst->src[0], inst->size_read(0)))
3219 break;
3220
3221 /* SEND instructions can't have MRF as a destination. */
3222 if (scan_inst->mlen)
3223 break;
3224
3225 if (devinfo->gen == 6) {
3226 /* gen6 math instructions must have the destination be
3227 * GRF, so no compute-to-MRF for them.
3228 */
3229 if (scan_inst->is_math()) {
3230 break;
3231 }
3232 }
3233
3234 /* Clear the bits for any registers this instruction overwrites. */
3235 regs_left &= ~mask_relative_to(
3236 inst->src[0], scan_inst->dst, scan_inst->size_written);
3237 if (!regs_left)
3238 break;
3239 }
3240
3241 /* We don't handle control flow here. Most computation of
3242 * values that end up in MRFs are shortly before the MRF
3243 * write anyway.
3244 */
3245 if (block->start() == scan_inst)
3246 break;
3247
3248 /* You can't read from an MRF, so if someone else reads our
3249 * MRF's source GRF that we wanted to rewrite, that stops us.
3250 */
3251 bool interfered = false;
3252 for (int i = 0; i < scan_inst->sources; i++) {
3253 if (regions_overlap(scan_inst->src[i], scan_inst->size_read(i),
3254 inst->src[0], inst->size_read(0))) {
3255 interfered = true;
3256 }
3257 }
3258 if (interfered)
3259 break;
3260
3261 if (regions_overlap(scan_inst->dst, scan_inst->size_written,
3262 inst->dst, inst->size_written)) {
3263 /* If somebody else writes our MRF here, we can't
3264 * compute-to-MRF before that.
3265 */
3266 break;
3267 }
3268
3269 if (scan_inst->mlen > 0 && scan_inst->base_mrf != -1 &&
3270 regions_overlap(fs_reg(MRF, scan_inst->base_mrf), scan_inst->mlen * REG_SIZE,
3271 inst->dst, inst->size_written)) {
3272 /* Found a SEND instruction, which means that there are
3273 * live values in MRFs from base_mrf to base_mrf +
3274 * scan_inst->mlen - 1. Don't go pushing our MRF write up
3275 * above it.
3276 */
3277 break;
3278 }
3279 }
3280
3281 if (regs_left)
3282 continue;
3283
3284 /* Found all generating instructions of our MRF's source value, so it
3285 * should be safe to rewrite them to point to the MRF directly.
3286 */
3287 regs_left = (1 << regs_read(inst, 0)) - 1;
3288
3289 foreach_inst_in_block_reverse_starting_from(fs_inst, scan_inst, inst) {
3290 if (regions_overlap(scan_inst->dst, scan_inst->size_written,
3291 inst->src[0], inst->size_read(0))) {
3292 /* Clear the bits for any registers this instruction overwrites. */
3293 regs_left &= ~mask_relative_to(
3294 inst->src[0], scan_inst->dst, scan_inst->size_written);
3295
3296 const unsigned rel_offset = reg_offset(scan_inst->dst) -
3297 reg_offset(inst->src[0]);
3298
3299 if (inst->dst.nr & BRW_MRF_COMPR4) {
3300 /* Apply the same address transformation done by the hardware
3301 * for COMPR4 MRF writes.
3302 */
3303 assert(rel_offset < 2 * REG_SIZE);
3304 scan_inst->dst.nr = inst->dst.nr + rel_offset / REG_SIZE * 4;
3305
3306 /* Clear the COMPR4 bit if the generating instruction is not
3307 * compressed.
3308 */
3309 if (scan_inst->size_written < 2 * REG_SIZE)
3310 scan_inst->dst.nr &= ~BRW_MRF_COMPR4;
3311
3312 } else {
3313 /* Calculate the MRF number the result of this instruction is
3314 * ultimately written to.
3315 */
3316 scan_inst->dst.nr = inst->dst.nr + rel_offset / REG_SIZE;
3317 }
3318
3319 scan_inst->dst.file = MRF;
3320 scan_inst->dst.offset = inst->dst.offset + rel_offset % REG_SIZE;
3321 scan_inst->saturate |= inst->saturate;
3322 if (!regs_left)
3323 break;
3324 }
3325 }
3326
3327 assert(!regs_left);
3328 inst->remove(block);
3329 progress = true;
3330 }
3331
3332 if (progress)
3333 invalidate_analysis(DEPENDENCY_INSTRUCTIONS);
3334
3335 return progress;
3336 }
3337
3338 /**
3339 * Eliminate FIND_LIVE_CHANNEL instructions occurring outside any control
3340 * flow. We could probably do better here with some form of divergence
3341 * analysis.
3342 */
3343 bool
3344 fs_visitor::eliminate_find_live_channel()
3345 {
3346 bool progress = false;
3347 unsigned depth = 0;
3348
3349 if (!brw_stage_has_packed_dispatch(devinfo, stage, stage_prog_data)) {
3350 /* The optimization below assumes that channel zero is live on thread
3351 * dispatch, which may not be the case if the fixed function dispatches
3352 * threads sparsely.
3353 */
3354 return false;
3355 }
3356
3357 foreach_block_and_inst_safe(block, fs_inst, inst, cfg) {
3358 switch (inst->opcode) {
3359 case BRW_OPCODE_IF:
3360 case BRW_OPCODE_DO:
3361 depth++;
3362 break;
3363
3364 case BRW_OPCODE_ENDIF:
3365 case BRW_OPCODE_WHILE:
3366 depth--;
3367 break;
3368
3369 case FS_OPCODE_DISCARD_JUMP:
3370 /* This can potentially make control flow non-uniform until the end
3371 * of the program.
3372 */
3373 return progress;
3374
3375 case SHADER_OPCODE_FIND_LIVE_CHANNEL:
3376 if (depth == 0) {
3377 inst->opcode = BRW_OPCODE_MOV;
3378 inst->src[0] = brw_imm_ud(0u);
3379 inst->sources = 1;
3380 inst->force_writemask_all = true;
3381 progress = true;
3382 }
3383 break;
3384
3385 default:
3386 break;
3387 }
3388 }
3389
3390 if (progress)
3391 invalidate_analysis(DEPENDENCY_INSTRUCTION_DETAIL);
3392
3393 return progress;
3394 }
3395
3396 /**
3397 * Once we've generated code, try to convert normal FS_OPCODE_FB_WRITE
3398 * instructions to FS_OPCODE_REP_FB_WRITE.
3399 */
3400 void
3401 fs_visitor::emit_repclear_shader()
3402 {
3403 brw_wm_prog_key *key = (brw_wm_prog_key*) this->key;
3404 int base_mrf = 0;
3405 int color_mrf = base_mrf + 2;
3406 fs_inst *mov;
3407
3408 if (uniforms > 0) {
3409 mov = bld.exec_all().group(4, 0)
3410 .MOV(brw_message_reg(color_mrf),
3411 fs_reg(UNIFORM, 0, BRW_REGISTER_TYPE_F));
3412 } else {
3413 struct brw_reg reg =
3414 brw_reg(BRW_GENERAL_REGISTER_FILE, 2, 3, 0, 0, BRW_REGISTER_TYPE_F,
3415 BRW_VERTICAL_STRIDE_8, BRW_WIDTH_2, BRW_HORIZONTAL_STRIDE_4,
3416 BRW_SWIZZLE_XYZW, WRITEMASK_XYZW);
3417
3418 mov = bld.exec_all().group(4, 0)
3419 .MOV(vec4(brw_message_reg(color_mrf)), fs_reg(reg));
3420 }
3421
3422 fs_inst *write = NULL;
3423 if (key->nr_color_regions == 1) {
3424 write = bld.emit(FS_OPCODE_REP_FB_WRITE);
3425 write->saturate = key->clamp_fragment_color;
3426 write->base_mrf = color_mrf;
3427 write->target = 0;
3428 write->header_size = 0;
3429 write->mlen = 1;
3430 } else {
3431 assume(key->nr_color_regions > 0);
3432
3433 struct brw_reg header =
3434 retype(brw_message_reg(base_mrf), BRW_REGISTER_TYPE_UD);
3435 bld.exec_all().group(16, 0)
3436 .MOV(header, retype(brw_vec8_grf(0, 0), BRW_REGISTER_TYPE_UD));
3437
3438 for (int i = 0; i < key->nr_color_regions; ++i) {
3439 if (i > 0) {
3440 bld.exec_all().group(1, 0)
3441 .MOV(component(header, 2), brw_imm_ud(i));
3442 }
3443
3444 write = bld.emit(FS_OPCODE_REP_FB_WRITE);
3445 write->saturate = key->clamp_fragment_color;
3446 write->base_mrf = base_mrf;
3447 write->target = i;
3448 write->header_size = 2;
3449 write->mlen = 3;
3450 }
3451 }
3452 write->eot = true;
3453 write->last_rt = true;
3454
3455 calculate_cfg();
3456
3457 assign_constant_locations();
3458 assign_curb_setup();
3459
3460 /* Now that we have the uniform assigned, go ahead and force it to a vec4. */
3461 if (uniforms > 0) {
3462 assert(mov->src[0].file == FIXED_GRF);
3463 mov->src[0] = brw_vec4_grf(mov->src[0].nr, 0);
3464 }
3465
3466 lower_scoreboard();
3467 }
3468
3469 /**
3470 * Walks through basic blocks, looking for repeated MRF writes and
3471 * removing the later ones.
3472 */
3473 bool
3474 fs_visitor::remove_duplicate_mrf_writes()
3475 {
3476 fs_inst *last_mrf_move[BRW_MAX_MRF(devinfo->gen)];
3477 bool progress = false;
3478
3479 /* Need to update the MRF tracking for compressed instructions. */
3480 if (dispatch_width >= 16)
3481 return false;
3482
3483 memset(last_mrf_move, 0, sizeof(last_mrf_move));
3484
3485 foreach_block_and_inst_safe (block, fs_inst, inst, cfg) {
3486 if (inst->is_control_flow()) {
3487 memset(last_mrf_move, 0, sizeof(last_mrf_move));
3488 }
3489
3490 if (inst->opcode == BRW_OPCODE_MOV &&
3491 inst->dst.file == MRF) {
3492 fs_inst *prev_inst = last_mrf_move[inst->dst.nr];
3493 if (prev_inst && prev_inst->opcode == BRW_OPCODE_MOV &&
3494 inst->dst.equals(prev_inst->dst) &&
3495 inst->src[0].equals(prev_inst->src[0]) &&
3496 inst->saturate == prev_inst->saturate &&
3497 inst->predicate == prev_inst->predicate &&
3498 inst->conditional_mod == prev_inst->conditional_mod &&
3499 inst->exec_size == prev_inst->exec_size) {
3500 inst->remove(block);
3501 progress = true;
3502 continue;
3503 }
3504 }
3505
3506 /* Clear out the last-write records for MRFs that were overwritten. */
3507 if (inst->dst.file == MRF) {
3508 last_mrf_move[inst->dst.nr] = NULL;
3509 }
3510
3511 if (inst->mlen > 0 && inst->base_mrf != -1) {
3512 /* Found a SEND instruction, which will include two or fewer
3513 * implied MRF writes. We could do better here.
3514 */
3515 for (unsigned i = 0; i < inst->implied_mrf_writes(); i++) {
3516 last_mrf_move[inst->base_mrf + i] = NULL;
3517 }
3518 }
3519
3520 /* Clear out any MRF move records whose sources got overwritten. */
3521 for (unsigned i = 0; i < ARRAY_SIZE(last_mrf_move); i++) {
3522 if (last_mrf_move[i] &&
3523 regions_overlap(inst->dst, inst->size_written,
3524 last_mrf_move[i]->src[0],
3525 last_mrf_move[i]->size_read(0))) {
3526 last_mrf_move[i] = NULL;
3527 }
3528 }
3529
3530 if (inst->opcode == BRW_OPCODE_MOV &&
3531 inst->dst.file == MRF &&
3532 inst->src[0].file != ARF &&
3533 !inst->is_partial_write()) {
3534 last_mrf_move[inst->dst.nr] = inst;
3535 }
3536 }
3537
3538 if (progress)
3539 invalidate_analysis(DEPENDENCY_INSTRUCTIONS);
3540
3541 return progress;
3542 }
3543
3544 /**
3545 * Rounding modes for conversion instructions are included for each
3546 * conversion, but right now it is a state. So once it is set,
3547 * we don't need to call it again for subsequent calls.
3548 *
3549 * This is useful for vector/matrices conversions, as setting the
3550 * mode once is enough for the full vector/matrix
3551 */
3552 bool
3553 fs_visitor::remove_extra_rounding_modes()
3554 {
3555 bool progress = false;
3556 unsigned execution_mode = this->nir->info.float_controls_execution_mode;
3557
3558 brw_rnd_mode base_mode = BRW_RND_MODE_UNSPECIFIED;
3559 if ((FLOAT_CONTROLS_ROUNDING_MODE_RTE_FP16 |
3560 FLOAT_CONTROLS_ROUNDING_MODE_RTE_FP32 |
3561 FLOAT_CONTROLS_ROUNDING_MODE_RTE_FP64) &
3562 execution_mode)
3563 base_mode = BRW_RND_MODE_RTNE;
3564 if ((FLOAT_CONTROLS_ROUNDING_MODE_RTZ_FP16 |
3565 FLOAT_CONTROLS_ROUNDING_MODE_RTZ_FP32 |
3566 FLOAT_CONTROLS_ROUNDING_MODE_RTZ_FP64) &
3567 execution_mode)
3568 base_mode = BRW_RND_MODE_RTZ;
3569
3570 foreach_block (block, cfg) {
3571 brw_rnd_mode prev_mode = base_mode;
3572
3573 foreach_inst_in_block_safe (fs_inst, inst, block) {
3574 if (inst->opcode == SHADER_OPCODE_RND_MODE) {
3575 assert(inst->src[0].file == BRW_IMMEDIATE_VALUE);
3576 const brw_rnd_mode mode = (brw_rnd_mode) inst->src[0].d;
3577 if (mode == prev_mode) {
3578 inst->remove(block);
3579 progress = true;
3580 } else {
3581 prev_mode = mode;
3582 }
3583 }
3584 }
3585 }
3586
3587 if (progress)
3588 invalidate_analysis(DEPENDENCY_INSTRUCTIONS);
3589
3590 return progress;
3591 }
3592
3593 static void
3594 clear_deps_for_inst_src(fs_inst *inst, bool *deps, int first_grf, int grf_len)
3595 {
3596 /* Clear the flag for registers that actually got read (as expected). */
3597 for (int i = 0; i < inst->sources; i++) {
3598 int grf;
3599 if (inst->src[i].file == VGRF || inst->src[i].file == FIXED_GRF) {
3600 grf = inst->src[i].nr;
3601 } else {
3602 continue;
3603 }
3604
3605 if (grf >= first_grf &&
3606 grf < first_grf + grf_len) {
3607 deps[grf - first_grf] = false;
3608 if (inst->exec_size == 16)
3609 deps[grf - first_grf + 1] = false;
3610 }
3611 }
3612 }
3613
3614 /**
3615 * Implements this workaround for the original 965:
3616 *
3617 * "[DevBW, DevCL] Implementation Restrictions: As the hardware does not
3618 * check for post destination dependencies on this instruction, software
3619 * must ensure that there is no destination hazard for the case of ‘write
3620 * followed by a posted write’ shown in the following example.
3621 *
3622 * 1. mov r3 0
3623 * 2. send r3.xy <rest of send instruction>
3624 * 3. mov r2 r3
3625 *
3626 * Due to no post-destination dependency check on the ‘send’, the above
3627 * code sequence could have two instructions (1 and 2) in flight at the
3628 * same time that both consider ‘r3’ as the target of their final writes.
3629 */
3630 void
3631 fs_visitor::insert_gen4_pre_send_dependency_workarounds(bblock_t *block,
3632 fs_inst *inst)
3633 {
3634 int write_len = regs_written(inst);
3635 int first_write_grf = inst->dst.nr;
3636 bool needs_dep[BRW_MAX_MRF(devinfo->gen)];
3637 assert(write_len < (int)sizeof(needs_dep) - 1);
3638
3639 memset(needs_dep, false, sizeof(needs_dep));
3640 memset(needs_dep, true, write_len);
3641
3642 clear_deps_for_inst_src(inst, needs_dep, first_write_grf, write_len);
3643
3644 /* Walk backwards looking for writes to registers we're writing which
3645 * aren't read since being written. If we hit the start of the program,
3646 * we assume that there are no outstanding dependencies on entry to the
3647 * program.
3648 */
3649 foreach_inst_in_block_reverse_starting_from(fs_inst, scan_inst, inst) {
3650 /* If we hit control flow, assume that there *are* outstanding
3651 * dependencies, and force their cleanup before our instruction.
3652 */
3653 if (block->start() == scan_inst && block->num != 0) {
3654 for (int i = 0; i < write_len; i++) {
3655 if (needs_dep[i])
3656 DEP_RESOLVE_MOV(fs_builder(this, block, inst),
3657 first_write_grf + i);
3658 }
3659 return;
3660 }
3661
3662 /* We insert our reads as late as possible on the assumption that any
3663 * instruction but a MOV that might have left us an outstanding
3664 * dependency has more latency than a MOV.
3665 */
3666 if (scan_inst->dst.file == VGRF) {
3667 for (unsigned i = 0; i < regs_written(scan_inst); i++) {
3668 int reg = scan_inst->dst.nr + i;
3669
3670 if (reg >= first_write_grf &&
3671 reg < first_write_grf + write_len &&
3672 needs_dep[reg - first_write_grf]) {
3673 DEP_RESOLVE_MOV(fs_builder(this, block, inst), reg);
3674 needs_dep[reg - first_write_grf] = false;
3675 if (scan_inst->exec_size == 16)
3676 needs_dep[reg - first_write_grf + 1] = false;
3677 }
3678 }
3679 }
3680
3681 /* Clear the flag for registers that actually got read (as expected). */
3682 clear_deps_for_inst_src(scan_inst, needs_dep, first_write_grf, write_len);
3683
3684 /* Continue the loop only if we haven't resolved all the dependencies */
3685 int i;
3686 for (i = 0; i < write_len; i++) {
3687 if (needs_dep[i])
3688 break;
3689 }
3690 if (i == write_len)
3691 return;
3692 }
3693 }
3694
3695 /**
3696 * Implements this workaround for the original 965:
3697 *
3698 * "[DevBW, DevCL] Errata: A destination register from a send can not be
3699 * used as a destination register until after it has been sourced by an
3700 * instruction with a different destination register.
3701 */
3702 void
3703 fs_visitor::insert_gen4_post_send_dependency_workarounds(bblock_t *block, fs_inst *inst)
3704 {
3705 int write_len = regs_written(inst);
3706 unsigned first_write_grf = inst->dst.nr;
3707 bool needs_dep[BRW_MAX_MRF(devinfo->gen)];
3708 assert(write_len < (int)sizeof(needs_dep) - 1);
3709
3710 memset(needs_dep, false, sizeof(needs_dep));
3711 memset(needs_dep, true, write_len);
3712 /* Walk forwards looking for writes to registers we're writing which aren't
3713 * read before being written.
3714 */
3715 foreach_inst_in_block_starting_from(fs_inst, scan_inst, inst) {
3716 /* If we hit control flow, force resolve all remaining dependencies. */
3717 if (block->end() == scan_inst && block->num != cfg->num_blocks - 1) {
3718 for (int i = 0; i < write_len; i++) {
3719 if (needs_dep[i])
3720 DEP_RESOLVE_MOV(fs_builder(this, block, scan_inst),
3721 first_write_grf + i);
3722 }
3723 return;
3724 }
3725
3726 /* Clear the flag for registers that actually got read (as expected). */
3727 clear_deps_for_inst_src(scan_inst, needs_dep, first_write_grf, write_len);
3728
3729 /* We insert our reads as late as possible since they're reading the
3730 * result of a SEND, which has massive latency.
3731 */
3732 if (scan_inst->dst.file == VGRF &&
3733 scan_inst->dst.nr >= first_write_grf &&
3734 scan_inst->dst.nr < first_write_grf + write_len &&
3735 needs_dep[scan_inst->dst.nr - first_write_grf]) {
3736 DEP_RESOLVE_MOV(fs_builder(this, block, scan_inst),
3737 scan_inst->dst.nr);
3738 needs_dep[scan_inst->dst.nr - first_write_grf] = false;
3739 }
3740
3741 /* Continue the loop only if we haven't resolved all the dependencies */
3742 int i;
3743 for (i = 0; i < write_len; i++) {
3744 if (needs_dep[i])
3745 break;
3746 }
3747 if (i == write_len)
3748 return;
3749 }
3750 }
3751
3752 void
3753 fs_visitor::insert_gen4_send_dependency_workarounds()
3754 {
3755 if (devinfo->gen != 4 || devinfo->is_g4x)
3756 return;
3757
3758 bool progress = false;
3759
3760 foreach_block_and_inst(block, fs_inst, inst, cfg) {
3761 if (inst->mlen != 0 && inst->dst.file == VGRF) {
3762 insert_gen4_pre_send_dependency_workarounds(block, inst);
3763 insert_gen4_post_send_dependency_workarounds(block, inst);
3764 progress = true;
3765 }
3766 }
3767
3768 if (progress)
3769 invalidate_analysis(DEPENDENCY_INSTRUCTIONS);
3770 }
3771
3772 /**
3773 * Turns the generic expression-style uniform pull constant load instruction
3774 * into a hardware-specific series of instructions for loading a pull
3775 * constant.
3776 *
3777 * The expression style allows the CSE pass before this to optimize out
3778 * repeated loads from the same offset, and gives the pre-register-allocation
3779 * scheduling full flexibility, while the conversion to native instructions
3780 * allows the post-register-allocation scheduler the best information
3781 * possible.
3782 *
3783 * Note that execution masking for setting up pull constant loads is special:
3784 * the channels that need to be written are unrelated to the current execution
3785 * mask, since a later instruction will use one of the result channels as a
3786 * source operand for all 8 or 16 of its channels.
3787 */
3788 void
3789 fs_visitor::lower_uniform_pull_constant_loads()
3790 {
3791 foreach_block_and_inst (block, fs_inst, inst, cfg) {
3792 if (inst->opcode != FS_OPCODE_UNIFORM_PULL_CONSTANT_LOAD)
3793 continue;
3794
3795 if (devinfo->gen >= 7) {
3796 const fs_builder ubld = fs_builder(this, block, inst).exec_all();
3797 const fs_reg payload = ubld.group(8, 0).vgrf(BRW_REGISTER_TYPE_UD);
3798
3799 ubld.group(8, 0).MOV(payload,
3800 retype(brw_vec8_grf(0, 0), BRW_REGISTER_TYPE_UD));
3801 ubld.group(1, 0).MOV(component(payload, 2),
3802 brw_imm_ud(inst->src[1].ud / 16));
3803
3804 inst->opcode = FS_OPCODE_UNIFORM_PULL_CONSTANT_LOAD_GEN7;
3805 inst->src[1] = payload;
3806 inst->header_size = 1;
3807 inst->mlen = 1;
3808
3809 invalidate_analysis(DEPENDENCY_INSTRUCTIONS | DEPENDENCY_VARIABLES);
3810 } else {
3811 /* Before register allocation, we didn't tell the scheduler about the
3812 * MRF we use. We know it's safe to use this MRF because nothing
3813 * else does except for register spill/unspill, which generates and
3814 * uses its MRF within a single IR instruction.
3815 */
3816 inst->base_mrf = FIRST_PULL_LOAD_MRF(devinfo->gen) + 1;
3817 inst->mlen = 1;
3818 }
3819 }
3820 }
3821
3822 bool
3823 fs_visitor::lower_load_payload()
3824 {
3825 bool progress = false;
3826
3827 foreach_block_and_inst_safe (block, fs_inst, inst, cfg) {
3828 if (inst->opcode != SHADER_OPCODE_LOAD_PAYLOAD)
3829 continue;
3830
3831 assert(inst->dst.file == MRF || inst->dst.file == VGRF);
3832 assert(inst->saturate == false);
3833 fs_reg dst = inst->dst;
3834
3835 /* Get rid of COMPR4. We'll add it back in if we need it */
3836 if (dst.file == MRF)
3837 dst.nr = dst.nr & ~BRW_MRF_COMPR4;
3838
3839 const fs_builder ibld(this, block, inst);
3840 const fs_builder ubld = ibld.exec_all();
3841
3842 for (uint8_t i = 0; i < inst->header_size;) {
3843 /* Number of header GRFs to initialize at once with a single MOV
3844 * instruction.
3845 */
3846 const unsigned n =
3847 (i + 1 < inst->header_size && inst->src[i].stride == 1 &&
3848 inst->src[i + 1].equals(byte_offset(inst->src[i], REG_SIZE))) ?
3849 2 : 1;
3850
3851 if (inst->src[i].file != BAD_FILE)
3852 ubld.group(8 * n, 0).MOV(retype(dst, BRW_REGISTER_TYPE_UD),
3853 retype(inst->src[i], BRW_REGISTER_TYPE_UD));
3854
3855 dst = byte_offset(dst, n * REG_SIZE);
3856 i += n;
3857 }
3858
3859 if (inst->dst.file == MRF && (inst->dst.nr & BRW_MRF_COMPR4) &&
3860 inst->exec_size > 8) {
3861 /* In this case, the payload portion of the LOAD_PAYLOAD isn't
3862 * a straightforward copy. Instead, the result of the
3863 * LOAD_PAYLOAD is treated as interleaved and the first four
3864 * non-header sources are unpacked as:
3865 *
3866 * m + 0: r0
3867 * m + 1: g0
3868 * m + 2: b0
3869 * m + 3: a0
3870 * m + 4: r1
3871 * m + 5: g1
3872 * m + 6: b1
3873 * m + 7: a1
3874 *
3875 * This is used for gen <= 5 fb writes.
3876 */
3877 assert(inst->exec_size == 16);
3878 assert(inst->header_size + 4 <= inst->sources);
3879 for (uint8_t i = inst->header_size; i < inst->header_size + 4; i++) {
3880 if (inst->src[i].file != BAD_FILE) {
3881 if (devinfo->has_compr4) {
3882 fs_reg compr4_dst = retype(dst, inst->src[i].type);
3883 compr4_dst.nr |= BRW_MRF_COMPR4;
3884 ibld.MOV(compr4_dst, inst->src[i]);
3885 } else {
3886 /* Platform doesn't have COMPR4. We have to fake it */
3887 fs_reg mov_dst = retype(dst, inst->src[i].type);
3888 ibld.quarter(0).MOV(mov_dst, quarter(inst->src[i], 0));
3889 mov_dst.nr += 4;
3890 ibld.quarter(1).MOV(mov_dst, quarter(inst->src[i], 1));
3891 }
3892 }
3893
3894 dst.nr++;
3895 }
3896
3897 /* The loop above only ever incremented us through the first set
3898 * of 4 registers. However, thanks to the magic of COMPR4, we
3899 * actually wrote to the first 8 registers, so we need to take
3900 * that into account now.
3901 */
3902 dst.nr += 4;
3903
3904 /* The COMPR4 code took care of the first 4 sources. We'll let
3905 * the regular path handle any remaining sources. Yes, we are
3906 * modifying the instruction but we're about to delete it so
3907 * this really doesn't hurt anything.
3908 */
3909 inst->header_size += 4;
3910 }
3911
3912 for (uint8_t i = inst->header_size; i < inst->sources; i++) {
3913 if (inst->src[i].file != BAD_FILE) {
3914 dst.type = inst->src[i].type;
3915 ibld.MOV(dst, inst->src[i]);
3916 } else {
3917 dst.type = BRW_REGISTER_TYPE_UD;
3918 }
3919 dst = offset(dst, ibld, 1);
3920 }
3921
3922 inst->remove(block);
3923 progress = true;
3924 }
3925
3926 if (progress)
3927 invalidate_analysis(DEPENDENCY_INSTRUCTIONS);
3928
3929 return progress;
3930 }
3931
3932 void
3933 fs_visitor::lower_mul_dword_inst(fs_inst *inst, bblock_t *block)
3934 {
3935 const fs_builder ibld(this, block, inst);
3936
3937 const bool ud = (inst->src[1].type == BRW_REGISTER_TYPE_UD);
3938 if (inst->src[1].file == IMM &&
3939 (( ud && inst->src[1].ud <= UINT16_MAX) ||
3940 (!ud && inst->src[1].d <= INT16_MAX && inst->src[1].d >= INT16_MIN))) {
3941 /* The MUL instruction isn't commutative. On Gen <= 6, only the low
3942 * 16-bits of src0 are read, and on Gen >= 7 only the low 16-bits of
3943 * src1 are used.
3944 *
3945 * If multiplying by an immediate value that fits in 16-bits, do a
3946 * single MUL instruction with that value in the proper location.
3947 */
3948 if (devinfo->gen < 7) {
3949 fs_reg imm(VGRF, alloc.allocate(dispatch_width / 8), inst->dst.type);
3950 ibld.MOV(imm, inst->src[1]);
3951 ibld.MUL(inst->dst, imm, inst->src[0]);
3952 } else {
3953 ibld.MUL(inst->dst, inst->src[0],
3954 ud ? brw_imm_uw(inst->src[1].ud)
3955 : brw_imm_w(inst->src[1].d));
3956 }
3957 } else {
3958 /* Gen < 8 (and some Gen8+ low-power parts like Cherryview) cannot
3959 * do 32-bit integer multiplication in one instruction, but instead
3960 * must do a sequence (which actually calculates a 64-bit result):
3961 *
3962 * mul(8) acc0<1>D g3<8,8,1>D g4<8,8,1>D
3963 * mach(8) null g3<8,8,1>D g4<8,8,1>D
3964 * mov(8) g2<1>D acc0<8,8,1>D
3965 *
3966 * But on Gen > 6, the ability to use second accumulator register
3967 * (acc1) for non-float data types was removed, preventing a simple
3968 * implementation in SIMD16. A 16-channel result can be calculated by
3969 * executing the three instructions twice in SIMD8, once with quarter
3970 * control of 1Q for the first eight channels and again with 2Q for
3971 * the second eight channels.
3972 *
3973 * Which accumulator register is implicitly accessed (by AccWrEnable
3974 * for instance) is determined by the quarter control. Unfortunately
3975 * Ivybridge (and presumably Baytrail) has a hardware bug in which an
3976 * implicit accumulator access by an instruction with 2Q will access
3977 * acc1 regardless of whether the data type is usable in acc1.
3978 *
3979 * Specifically, the 2Q mach(8) writes acc1 which does not exist for
3980 * integer data types.
3981 *
3982 * Since we only want the low 32-bits of the result, we can do two
3983 * 32-bit x 16-bit multiplies (like the mul and mach are doing), and
3984 * adjust the high result and add them (like the mach is doing):
3985 *
3986 * mul(8) g7<1>D g3<8,8,1>D g4.0<8,8,1>UW
3987 * mul(8) g8<1>D g3<8,8,1>D g4.1<8,8,1>UW
3988 * shl(8) g9<1>D g8<8,8,1>D 16D
3989 * add(8) g2<1>D g7<8,8,1>D g8<8,8,1>D
3990 *
3991 * We avoid the shl instruction by realizing that we only want to add
3992 * the low 16-bits of the "high" result to the high 16-bits of the
3993 * "low" result and using proper regioning on the add:
3994 *
3995 * mul(8) g7<1>D g3<8,8,1>D g4.0<16,8,2>UW
3996 * mul(8) g8<1>D g3<8,8,1>D g4.1<16,8,2>UW
3997 * add(8) g7.1<2>UW g7.1<16,8,2>UW g8<16,8,2>UW
3998 *
3999 * Since it does not use the (single) accumulator register, we can
4000 * schedule multi-component multiplications much better.
4001 */
4002
4003 bool needs_mov = false;
4004 fs_reg orig_dst = inst->dst;
4005
4006 /* Get a new VGRF for the "low" 32x16-bit multiplication result if
4007 * reusing the original destination is impossible due to hardware
4008 * restrictions, source/destination overlap, or it being the null
4009 * register.
4010 */
4011 fs_reg low = inst->dst;
4012 if (orig_dst.is_null() || orig_dst.file == MRF ||
4013 regions_overlap(inst->dst, inst->size_written,
4014 inst->src[0], inst->size_read(0)) ||
4015 regions_overlap(inst->dst, inst->size_written,
4016 inst->src[1], inst->size_read(1)) ||
4017 inst->dst.stride >= 4) {
4018 needs_mov = true;
4019 low = fs_reg(VGRF, alloc.allocate(regs_written(inst)),
4020 inst->dst.type);
4021 }
4022
4023 /* Get a new VGRF but keep the same stride as inst->dst */
4024 fs_reg high(VGRF, alloc.allocate(regs_written(inst)), inst->dst.type);
4025 high.stride = inst->dst.stride;
4026 high.offset = inst->dst.offset % REG_SIZE;
4027
4028 if (devinfo->gen >= 7) {
4029 if (inst->src[1].abs)
4030 lower_src_modifiers(this, block, inst, 1);
4031
4032 if (inst->src[1].file == IMM) {
4033 ibld.MUL(low, inst->src[0],
4034 brw_imm_uw(inst->src[1].ud & 0xffff));
4035 ibld.MUL(high, inst->src[0],
4036 brw_imm_uw(inst->src[1].ud >> 16));
4037 } else {
4038 ibld.MUL(low, inst->src[0],
4039 subscript(inst->src[1], BRW_REGISTER_TYPE_UW, 0));
4040 ibld.MUL(high, inst->src[0],
4041 subscript(inst->src[1], BRW_REGISTER_TYPE_UW, 1));
4042 }
4043 } else {
4044 if (inst->src[0].abs)
4045 lower_src_modifiers(this, block, inst, 0);
4046
4047 ibld.MUL(low, subscript(inst->src[0], BRW_REGISTER_TYPE_UW, 0),
4048 inst->src[1]);
4049 ibld.MUL(high, subscript(inst->src[0], BRW_REGISTER_TYPE_UW, 1),
4050 inst->src[1]);
4051 }
4052
4053 ibld.ADD(subscript(low, BRW_REGISTER_TYPE_UW, 1),
4054 subscript(low, BRW_REGISTER_TYPE_UW, 1),
4055 subscript(high, BRW_REGISTER_TYPE_UW, 0));
4056
4057 if (needs_mov || inst->conditional_mod)
4058 set_condmod(inst->conditional_mod, ibld.MOV(orig_dst, low));
4059 }
4060 }
4061
4062 void
4063 fs_visitor::lower_mul_qword_inst(fs_inst *inst, bblock_t *block)
4064 {
4065 const fs_builder ibld(this, block, inst);
4066
4067 /* Considering two 64-bit integers ab and cd where each letter ab
4068 * corresponds to 32 bits, we get a 128-bit result WXYZ. We * cd
4069 * only need to provide the YZ part of the result. -------
4070 * BD
4071 * Only BD needs to be 64 bits. For AD and BC we only care + AD
4072 * about the lower 32 bits (since they are part of the upper + BC
4073 * 32 bits of our result). AC is not needed since it starts + AC
4074 * on the 65th bit of the result. -------
4075 * WXYZ
4076 */
4077 unsigned int q_regs = regs_written(inst);
4078 unsigned int d_regs = (q_regs + 1) / 2;
4079
4080 fs_reg bd(VGRF, alloc.allocate(q_regs), BRW_REGISTER_TYPE_UQ);
4081 fs_reg ad(VGRF, alloc.allocate(d_regs), BRW_REGISTER_TYPE_UD);
4082 fs_reg bc(VGRF, alloc.allocate(d_regs), BRW_REGISTER_TYPE_UD);
4083
4084 /* Here we need the full 64 bit result for 32b * 32b. */
4085 if (devinfo->has_integer_dword_mul) {
4086 ibld.MUL(bd, subscript(inst->src[0], BRW_REGISTER_TYPE_UD, 0),
4087 subscript(inst->src[1], BRW_REGISTER_TYPE_UD, 0));
4088 } else {
4089 fs_reg bd_high(VGRF, alloc.allocate(d_regs), BRW_REGISTER_TYPE_UD);
4090 fs_reg bd_low(VGRF, alloc.allocate(d_regs), BRW_REGISTER_TYPE_UD);
4091 fs_reg acc = retype(brw_acc_reg(inst->exec_size), BRW_REGISTER_TYPE_UD);
4092
4093 fs_inst *mul = ibld.MUL(acc,
4094 subscript(inst->src[0], BRW_REGISTER_TYPE_UD, 0),
4095 subscript(inst->src[1], BRW_REGISTER_TYPE_UW, 0));
4096 mul->writes_accumulator = true;
4097
4098 ibld.MACH(bd_high, subscript(inst->src[0], BRW_REGISTER_TYPE_UD, 0),
4099 subscript(inst->src[1], BRW_REGISTER_TYPE_UD, 0));
4100 ibld.MOV(bd_low, acc);
4101
4102 ibld.MOV(subscript(bd, BRW_REGISTER_TYPE_UD, 0), bd_low);
4103 ibld.MOV(subscript(bd, BRW_REGISTER_TYPE_UD, 1), bd_high);
4104 }
4105
4106 ibld.MUL(ad, subscript(inst->src[0], BRW_REGISTER_TYPE_UD, 1),
4107 subscript(inst->src[1], BRW_REGISTER_TYPE_UD, 0));
4108 ibld.MUL(bc, subscript(inst->src[0], BRW_REGISTER_TYPE_UD, 0),
4109 subscript(inst->src[1], BRW_REGISTER_TYPE_UD, 1));
4110
4111 ibld.ADD(ad, ad, bc);
4112 ibld.ADD(subscript(bd, BRW_REGISTER_TYPE_UD, 1),
4113 subscript(bd, BRW_REGISTER_TYPE_UD, 1), ad);
4114
4115 ibld.MOV(inst->dst, bd);
4116 }
4117
4118 void
4119 fs_visitor::lower_mulh_inst(fs_inst *inst, bblock_t *block)
4120 {
4121 const fs_builder ibld(this, block, inst);
4122
4123 /* According to the BDW+ BSpec page for the "Multiply Accumulate
4124 * High" instruction:
4125 *
4126 * "An added preliminary mov is required for source modification on
4127 * src1:
4128 * mov (8) r3.0<1>:d -r3<8;8,1>:d
4129 * mul (8) acc0:d r2.0<8;8,1>:d r3.0<16;8,2>:uw
4130 * mach (8) r5.0<1>:d r2.0<8;8,1>:d r3.0<8;8,1>:d"
4131 */
4132 if (devinfo->gen >= 8 && (inst->src[1].negate || inst->src[1].abs))
4133 lower_src_modifiers(this, block, inst, 1);
4134
4135 /* Should have been lowered to 8-wide. */
4136 assert(inst->exec_size <= get_lowered_simd_width(devinfo, inst));
4137 const fs_reg acc = retype(brw_acc_reg(inst->exec_size), inst->dst.type);
4138 fs_inst *mul = ibld.MUL(acc, inst->src[0], inst->src[1]);
4139 fs_inst *mach = ibld.MACH(inst->dst, inst->src[0], inst->src[1]);
4140
4141 if (devinfo->gen >= 8) {
4142 /* Until Gen8, integer multiplies read 32-bits from one source,
4143 * and 16-bits from the other, and relying on the MACH instruction
4144 * to generate the high bits of the result.
4145 *
4146 * On Gen8, the multiply instruction does a full 32x32-bit
4147 * multiply, but in order to do a 64-bit multiply we can simulate
4148 * the previous behavior and then use a MACH instruction.
4149 */
4150 assert(mul->src[1].type == BRW_REGISTER_TYPE_D ||
4151 mul->src[1].type == BRW_REGISTER_TYPE_UD);
4152 mul->src[1].type = BRW_REGISTER_TYPE_UW;
4153 mul->src[1].stride *= 2;
4154
4155 if (mul->src[1].file == IMM) {
4156 mul->src[1] = brw_imm_uw(mul->src[1].ud);
4157 }
4158 } else if (devinfo->gen == 7 && !devinfo->is_haswell &&
4159 inst->group > 0) {
4160 /* Among other things the quarter control bits influence which
4161 * accumulator register is used by the hardware for instructions
4162 * that access the accumulator implicitly (e.g. MACH). A
4163 * second-half instruction would normally map to acc1, which
4164 * doesn't exist on Gen7 and up (the hardware does emulate it for
4165 * floating-point instructions *only* by taking advantage of the
4166 * extra precision of acc0 not normally used for floating point
4167 * arithmetic).
4168 *
4169 * HSW and up are careful enough not to try to access an
4170 * accumulator register that doesn't exist, but on earlier Gen7
4171 * hardware we need to make sure that the quarter control bits are
4172 * zero to avoid non-deterministic behaviour and emit an extra MOV
4173 * to get the result masked correctly according to the current
4174 * channel enables.
4175 */
4176 mach->group = 0;
4177 mach->force_writemask_all = true;
4178 mach->dst = ibld.vgrf(inst->dst.type);
4179 ibld.MOV(inst->dst, mach->dst);
4180 }
4181 }
4182
4183 bool
4184 fs_visitor::lower_integer_multiplication()
4185 {
4186 bool progress = false;
4187
4188 foreach_block_and_inst_safe(block, fs_inst, inst, cfg) {
4189 if (inst->opcode == BRW_OPCODE_MUL) {
4190 /* If the instruction is already in a form that does not need lowering,
4191 * return early.
4192 */
4193 if (devinfo->gen >= 7) {
4194 if (type_sz(inst->src[1].type) < 4 && type_sz(inst->src[0].type) <= 4)
4195 continue;
4196 } else {
4197 if (type_sz(inst->src[0].type) < 4 && type_sz(inst->src[1].type) <= 4)
4198 continue;
4199 }
4200
4201 if ((inst->dst.type == BRW_REGISTER_TYPE_Q ||
4202 inst->dst.type == BRW_REGISTER_TYPE_UQ) &&
4203 (inst->src[0].type == BRW_REGISTER_TYPE_Q ||
4204 inst->src[0].type == BRW_REGISTER_TYPE_UQ) &&
4205 (inst->src[1].type == BRW_REGISTER_TYPE_Q ||
4206 inst->src[1].type == BRW_REGISTER_TYPE_UQ)) {
4207 lower_mul_qword_inst(inst, block);
4208 inst->remove(block);
4209 progress = true;
4210 } else if (!inst->dst.is_accumulator() &&
4211 (inst->dst.type == BRW_REGISTER_TYPE_D ||
4212 inst->dst.type == BRW_REGISTER_TYPE_UD) &&
4213 !devinfo->has_integer_dword_mul) {
4214 lower_mul_dword_inst(inst, block);
4215 inst->remove(block);
4216 progress = true;
4217 }
4218 } else if (inst->opcode == SHADER_OPCODE_MULH) {
4219 lower_mulh_inst(inst, block);
4220 inst->remove(block);
4221 progress = true;
4222 }
4223
4224 }
4225
4226 if (progress)
4227 invalidate_analysis(DEPENDENCY_INSTRUCTIONS | DEPENDENCY_VARIABLES);
4228
4229 return progress;
4230 }
4231
4232 bool
4233 fs_visitor::lower_minmax()
4234 {
4235 assert(devinfo->gen < 6);
4236
4237 bool progress = false;
4238
4239 foreach_block_and_inst_safe(block, fs_inst, inst, cfg) {
4240 const fs_builder ibld(this, block, inst);
4241
4242 if (inst->opcode == BRW_OPCODE_SEL &&
4243 inst->predicate == BRW_PREDICATE_NONE) {
4244 /* FIXME: Using CMP doesn't preserve the NaN propagation semantics of
4245 * the original SEL.L/GE instruction
4246 */
4247 ibld.CMP(ibld.null_reg_d(), inst->src[0], inst->src[1],
4248 inst->conditional_mod);
4249 inst->predicate = BRW_PREDICATE_NORMAL;
4250 inst->conditional_mod = BRW_CONDITIONAL_NONE;
4251
4252 progress = true;
4253 }
4254 }
4255
4256 if (progress)
4257 invalidate_analysis(DEPENDENCY_INSTRUCTIONS);
4258
4259 return progress;
4260 }
4261
4262 bool
4263 fs_visitor::lower_sub_sat()
4264 {
4265 bool progress = false;
4266
4267 foreach_block_and_inst_safe(block, fs_inst, inst, cfg) {
4268 const fs_builder ibld(this, block, inst);
4269
4270 if (inst->opcode == SHADER_OPCODE_USUB_SAT ||
4271 inst->opcode == SHADER_OPCODE_ISUB_SAT) {
4272 /* The fundamental problem is the hardware performs source negation
4273 * at the bit width of the source. If the source is 0x80000000D, the
4274 * negation is 0x80000000D. As a result, subtractSaturate(0,
4275 * 0x80000000) will produce 0x80000000 instead of 0x7fffffff. There
4276 * are at least three ways to resolve this:
4277 *
4278 * 1. Use the accumulator for the negated source. The accumulator is
4279 * 33 bits, so our source 0x80000000 is sign-extended to
4280 * 0x1800000000. The negation of which is 0x080000000. This
4281 * doesn't help for 64-bit integers (which are already bigger than
4282 * 33 bits). There are also only 8 accumulators, so SIMD16 or
4283 * SIMD32 instructions would have to be split into multiple SIMD8
4284 * instructions.
4285 *
4286 * 2. Use slightly different math. For any n-bit value x, we know (x
4287 * >> 1) != -(x >> 1). We can use this fact to only do
4288 * subtractions involving (x >> 1). subtractSaturate(a, b) ==
4289 * subtractSaturate(subtractSaturate(a, (b >> 1)), b - (b >> 1)).
4290 *
4291 * 3. For unsigned sources, it is sufficient to replace the
4292 * subtractSaturate with (a > b) ? a - b : 0.
4293 *
4294 * It may also be possible to use the SUBB instruction. This
4295 * implicitly writes the accumulator, so it could only be used in the
4296 * same situations as #1 above. It is further limited by only
4297 * allowing UD sources.
4298 */
4299 if (inst->exec_size == 8 && inst->src[0].type != BRW_REGISTER_TYPE_Q &&
4300 inst->src[0].type != BRW_REGISTER_TYPE_UQ) {
4301 fs_reg acc(ARF, BRW_ARF_ACCUMULATOR, inst->src[1].type);
4302
4303 ibld.MOV(acc, inst->src[1]);
4304 fs_inst *add = ibld.ADD(inst->dst, acc, inst->src[0]);
4305 add->saturate = true;
4306 add->src[0].negate = true;
4307 } else if (inst->opcode == SHADER_OPCODE_ISUB_SAT) {
4308 /* tmp = src1 >> 1;
4309 * dst = add.sat(add.sat(src0, -tmp), -(src1 - tmp));
4310 */
4311 fs_reg tmp1 = ibld.vgrf(inst->src[0].type);
4312 fs_reg tmp2 = ibld.vgrf(inst->src[0].type);
4313 fs_reg tmp3 = ibld.vgrf(inst->src[0].type);
4314 fs_inst *add;
4315
4316 ibld.SHR(tmp1, inst->src[1], brw_imm_d(1));
4317
4318 add = ibld.ADD(tmp2, inst->src[1], tmp1);
4319 add->src[1].negate = true;
4320
4321 add = ibld.ADD(tmp3, inst->src[0], tmp1);
4322 add->src[1].negate = true;
4323 add->saturate = true;
4324
4325 add = ibld.ADD(inst->dst, tmp3, tmp2);
4326 add->src[1].negate = true;
4327 add->saturate = true;
4328 } else {
4329 /* a > b ? a - b : 0 */
4330 ibld.CMP(ibld.null_reg_d(), inst->src[0], inst->src[1],
4331 BRW_CONDITIONAL_G);
4332
4333 fs_inst *add = ibld.ADD(inst->dst, inst->src[0], inst->src[1]);
4334 add->src[1].negate = !add->src[1].negate;
4335
4336 ibld.SEL(inst->dst, inst->dst, brw_imm_ud(0))
4337 ->predicate = BRW_PREDICATE_NORMAL;
4338 }
4339
4340 inst->remove(block);
4341 progress = true;
4342 }
4343 }
4344
4345 if (progress)
4346 invalidate_analysis(DEPENDENCY_INSTRUCTIONS | DEPENDENCY_VARIABLES);
4347
4348 return progress;
4349 }
4350
4351 /**
4352 * Get the mask of SIMD channels enabled during dispatch and not yet disabled
4353 * by discard. Due to the layout of the sample mask in the fragment shader
4354 * thread payload, \p bld is required to have a dispatch_width() not greater
4355 * than 16 for fragment shaders.
4356 */
4357 static fs_reg
4358 sample_mask_reg(const fs_builder &bld)
4359 {
4360 const fs_visitor *v = static_cast<const fs_visitor *>(bld.shader);
4361
4362 if (v->stage != MESA_SHADER_FRAGMENT) {
4363 return brw_imm_ud(0xffffffff);
4364 } else if (brw_wm_prog_data(v->stage_prog_data)->uses_kill) {
4365 assert(bld.dispatch_width() <= 16);
4366 return brw_flag_subreg(sample_mask_flag_subreg(v) + bld.group() / 16);
4367 } else {
4368 assert(v->devinfo->gen >= 6 && bld.dispatch_width() <= 16);
4369 return retype(brw_vec1_grf((bld.group() >= 16 ? 2 : 1), 7),
4370 BRW_REGISTER_TYPE_UW);
4371 }
4372 }
4373
4374 static void
4375 setup_color_payload(const fs_builder &bld, const brw_wm_prog_key *key,
4376 fs_reg *dst, fs_reg color, unsigned components)
4377 {
4378 if (key->clamp_fragment_color) {
4379 fs_reg tmp = bld.vgrf(BRW_REGISTER_TYPE_F, 4);
4380 assert(color.type == BRW_REGISTER_TYPE_F);
4381
4382 for (unsigned i = 0; i < components; i++)
4383 set_saturate(true,
4384 bld.MOV(offset(tmp, bld, i), offset(color, bld, i)));
4385
4386 color = tmp;
4387 }
4388
4389 for (unsigned i = 0; i < components; i++)
4390 dst[i] = offset(color, bld, i);
4391 }
4392
4393 uint32_t
4394 brw_fb_write_msg_control(const fs_inst *inst,
4395 const struct brw_wm_prog_data *prog_data)
4396 {
4397 uint32_t mctl;
4398
4399 if (inst->opcode == FS_OPCODE_REP_FB_WRITE) {
4400 assert(inst->group == 0 && inst->exec_size == 16);
4401 mctl = BRW_DATAPORT_RENDER_TARGET_WRITE_SIMD16_SINGLE_SOURCE_REPLICATED;
4402 } else if (prog_data->dual_src_blend) {
4403 assert(inst->exec_size == 8);
4404
4405 if (inst->group % 16 == 0)
4406 mctl = BRW_DATAPORT_RENDER_TARGET_WRITE_SIMD8_DUAL_SOURCE_SUBSPAN01;
4407 else if (inst->group % 16 == 8)
4408 mctl = BRW_DATAPORT_RENDER_TARGET_WRITE_SIMD8_DUAL_SOURCE_SUBSPAN23;
4409 else
4410 unreachable("Invalid dual-source FB write instruction group");
4411 } else {
4412 assert(inst->group == 0 || (inst->group == 16 && inst->exec_size == 16));
4413
4414 if (inst->exec_size == 16)
4415 mctl = BRW_DATAPORT_RENDER_TARGET_WRITE_SIMD16_SINGLE_SOURCE;
4416 else if (inst->exec_size == 8)
4417 mctl = BRW_DATAPORT_RENDER_TARGET_WRITE_SIMD8_SINGLE_SOURCE_SUBSPAN01;
4418 else
4419 unreachable("Invalid FB write execution size");
4420 }
4421
4422 return mctl;
4423 }
4424
4425 static void
4426 lower_fb_write_logical_send(const fs_builder &bld, fs_inst *inst,
4427 const struct brw_wm_prog_data *prog_data,
4428 const brw_wm_prog_key *key,
4429 const fs_visitor::thread_payload &payload)
4430 {
4431 assert(inst->src[FB_WRITE_LOGICAL_SRC_COMPONENTS].file == IMM);
4432 const gen_device_info *devinfo = bld.shader->devinfo;
4433 const fs_reg &color0 = inst->src[FB_WRITE_LOGICAL_SRC_COLOR0];
4434 const fs_reg &color1 = inst->src[FB_WRITE_LOGICAL_SRC_COLOR1];
4435 const fs_reg &src0_alpha = inst->src[FB_WRITE_LOGICAL_SRC_SRC0_ALPHA];
4436 const fs_reg &src_depth = inst->src[FB_WRITE_LOGICAL_SRC_SRC_DEPTH];
4437 const fs_reg &dst_depth = inst->src[FB_WRITE_LOGICAL_SRC_DST_DEPTH];
4438 const fs_reg &src_stencil = inst->src[FB_WRITE_LOGICAL_SRC_SRC_STENCIL];
4439 fs_reg sample_mask = inst->src[FB_WRITE_LOGICAL_SRC_OMASK];
4440 const unsigned components =
4441 inst->src[FB_WRITE_LOGICAL_SRC_COMPONENTS].ud;
4442
4443 assert(inst->target != 0 || src0_alpha.file == BAD_FILE);
4444
4445 /* We can potentially have a message length of up to 15, so we have to set
4446 * base_mrf to either 0 or 1 in order to fit in m0..m15.
4447 */
4448 fs_reg sources[15];
4449 int header_size = 2, payload_header_size;
4450 unsigned length = 0;
4451
4452 if (devinfo->gen < 6) {
4453 /* TODO: Support SIMD32 on gen4-5 */
4454 assert(bld.group() < 16);
4455
4456 /* For gen4-5, we always have a header consisting of g0 and g1. We have
4457 * an implied MOV from g0,g1 to the start of the message. The MOV from
4458 * g0 is handled by the hardware and the MOV from g1 is provided by the
4459 * generator. This is required because, on gen4-5, the generator may
4460 * generate two write messages with different message lengths in order
4461 * to handle AA data properly.
4462 *
4463 * Also, since the pixel mask goes in the g0 portion of the message and
4464 * since render target writes are the last thing in the shader, we write
4465 * the pixel mask directly into g0 and it will get copied as part of the
4466 * implied write.
4467 */
4468 if (prog_data->uses_kill) {
4469 bld.exec_all().group(1, 0)
4470 .MOV(retype(brw_vec1_grf(0, 0), BRW_REGISTER_TYPE_UW),
4471 sample_mask_reg(bld));
4472 }
4473
4474 assert(length == 0);
4475 length = 2;
4476 } else if ((devinfo->gen <= 7 && !devinfo->is_haswell &&
4477 prog_data->uses_kill) ||
4478 (devinfo->gen < 11 &&
4479 (color1.file != BAD_FILE || key->nr_color_regions > 1))) {
4480 /* From the Sandy Bridge PRM, volume 4, page 198:
4481 *
4482 * "Dispatched Pixel Enables. One bit per pixel indicating
4483 * which pixels were originally enabled when the thread was
4484 * dispatched. This field is only required for the end-of-
4485 * thread message and on all dual-source messages."
4486 */
4487 const fs_builder ubld = bld.exec_all().group(8, 0);
4488
4489 fs_reg header = ubld.vgrf(BRW_REGISTER_TYPE_UD, 2);
4490 if (bld.group() < 16) {
4491 /* The header starts off as g0 and g1 for the first half */
4492 ubld.group(16, 0).MOV(header, retype(brw_vec8_grf(0, 0),
4493 BRW_REGISTER_TYPE_UD));
4494 } else {
4495 /* The header starts off as g0 and g2 for the second half */
4496 assert(bld.group() < 32);
4497 const fs_reg header_sources[2] = {
4498 retype(brw_vec8_grf(0, 0), BRW_REGISTER_TYPE_UD),
4499 retype(brw_vec8_grf(2, 0), BRW_REGISTER_TYPE_UD),
4500 };
4501 ubld.LOAD_PAYLOAD(header, header_sources, 2, 0);
4502
4503 /* Gen12 will require additional fix-ups if we ever hit this path. */
4504 assert(devinfo->gen < 12);
4505 }
4506
4507 uint32_t g00_bits = 0;
4508
4509 /* Set "Source0 Alpha Present to RenderTarget" bit in message
4510 * header.
4511 */
4512 if (src0_alpha.file != BAD_FILE)
4513 g00_bits |= 1 << 11;
4514
4515 /* Set computes stencil to render target */
4516 if (prog_data->computed_stencil)
4517 g00_bits |= 1 << 14;
4518
4519 if (g00_bits) {
4520 /* OR extra bits into g0.0 */
4521 ubld.group(1, 0).OR(component(header, 0),
4522 retype(brw_vec1_grf(0, 0),
4523 BRW_REGISTER_TYPE_UD),
4524 brw_imm_ud(g00_bits));
4525 }
4526
4527 /* Set the render target index for choosing BLEND_STATE. */
4528 if (inst->target > 0) {
4529 ubld.group(1, 0).MOV(component(header, 2), brw_imm_ud(inst->target));
4530 }
4531
4532 if (prog_data->uses_kill) {
4533 ubld.group(1, 0).MOV(retype(component(header, 15),
4534 BRW_REGISTER_TYPE_UW),
4535 sample_mask_reg(bld));
4536 }
4537
4538 assert(length == 0);
4539 sources[0] = header;
4540 sources[1] = horiz_offset(header, 8);
4541 length = 2;
4542 }
4543 assert(length == 0 || length == 2);
4544 header_size = length;
4545
4546 if (payload.aa_dest_stencil_reg[0]) {
4547 assert(inst->group < 16);
4548 sources[length] = fs_reg(VGRF, bld.shader->alloc.allocate(1));
4549 bld.group(8, 0).exec_all().annotate("FB write stencil/AA alpha")
4550 .MOV(sources[length],
4551 fs_reg(brw_vec8_grf(payload.aa_dest_stencil_reg[0], 0)));
4552 length++;
4553 }
4554
4555 if (src0_alpha.file != BAD_FILE) {
4556 for (unsigned i = 0; i < bld.dispatch_width() / 8; i++) {
4557 const fs_builder &ubld = bld.exec_all().group(8, i)
4558 .annotate("FB write src0 alpha");
4559 const fs_reg tmp = ubld.vgrf(BRW_REGISTER_TYPE_F);
4560 ubld.MOV(tmp, horiz_offset(src0_alpha, i * 8));
4561 setup_color_payload(ubld, key, &sources[length], tmp, 1);
4562 length++;
4563 }
4564 }
4565
4566 if (sample_mask.file != BAD_FILE) {
4567 sources[length] = fs_reg(VGRF, bld.shader->alloc.allocate(1),
4568 BRW_REGISTER_TYPE_UD);
4569
4570 /* Hand over gl_SampleMask. Only the lower 16 bits of each channel are
4571 * relevant. Since it's unsigned single words one vgrf is always
4572 * 16-wide, but only the lower or higher 8 channels will be used by the
4573 * hardware when doing a SIMD8 write depending on whether we have
4574 * selected the subspans for the first or second half respectively.
4575 */
4576 assert(sample_mask.file != BAD_FILE && type_sz(sample_mask.type) == 4);
4577 sample_mask.type = BRW_REGISTER_TYPE_UW;
4578 sample_mask.stride *= 2;
4579
4580 bld.exec_all().annotate("FB write oMask")
4581 .MOV(horiz_offset(retype(sources[length], BRW_REGISTER_TYPE_UW),
4582 inst->group % 16),
4583 sample_mask);
4584 length++;
4585 }
4586
4587 payload_header_size = length;
4588
4589 setup_color_payload(bld, key, &sources[length], color0, components);
4590 length += 4;
4591
4592 if (color1.file != BAD_FILE) {
4593 setup_color_payload(bld, key, &sources[length], color1, components);
4594 length += 4;
4595 }
4596
4597 if (src_depth.file != BAD_FILE) {
4598 sources[length] = src_depth;
4599 length++;
4600 }
4601
4602 if (dst_depth.file != BAD_FILE) {
4603 sources[length] = dst_depth;
4604 length++;
4605 }
4606
4607 if (src_stencil.file != BAD_FILE) {
4608 assert(devinfo->gen >= 9);
4609 assert(bld.dispatch_width() == 8);
4610
4611 /* XXX: src_stencil is only available on gen9+. dst_depth is never
4612 * available on gen9+. As such it's impossible to have both enabled at the
4613 * same time and therefore length cannot overrun the array.
4614 */
4615 assert(length < 15);
4616
4617 sources[length] = bld.vgrf(BRW_REGISTER_TYPE_UD);
4618 bld.exec_all().annotate("FB write OS")
4619 .MOV(retype(sources[length], BRW_REGISTER_TYPE_UB),
4620 subscript(src_stencil, BRW_REGISTER_TYPE_UB, 0));
4621 length++;
4622 }
4623
4624 fs_inst *load;
4625 if (devinfo->gen >= 7) {
4626 /* Send from the GRF */
4627 fs_reg payload = fs_reg(VGRF, -1, BRW_REGISTER_TYPE_F);
4628 load = bld.LOAD_PAYLOAD(payload, sources, length, payload_header_size);
4629 payload.nr = bld.shader->alloc.allocate(regs_written(load));
4630 load->dst = payload;
4631
4632 uint32_t msg_ctl = brw_fb_write_msg_control(inst, prog_data);
4633 uint32_t ex_desc = 0;
4634
4635 inst->desc =
4636 (inst->group / 16) << 11 | /* rt slot group */
4637 brw_dp_write_desc(devinfo, inst->target, msg_ctl,
4638 GEN6_DATAPORT_WRITE_MESSAGE_RENDER_TARGET_WRITE,
4639 inst->last_rt, false);
4640
4641 if (devinfo->gen >= 11) {
4642 /* Set the "Render Target Index" and "Src0 Alpha Present" fields
4643 * in the extended message descriptor, in lieu of using a header.
4644 */
4645 ex_desc = inst->target << 12 | (src0_alpha.file != BAD_FILE) << 15;
4646
4647 if (key->nr_color_regions == 0)
4648 ex_desc |= 1 << 20; /* Null Render Target */
4649 }
4650
4651 inst->opcode = SHADER_OPCODE_SEND;
4652 inst->resize_sources(3);
4653 inst->sfid = GEN6_SFID_DATAPORT_RENDER_CACHE;
4654 inst->src[0] = brw_imm_ud(inst->desc);
4655 inst->src[1] = brw_imm_ud(ex_desc);
4656 inst->src[2] = payload;
4657 inst->mlen = regs_written(load);
4658 inst->ex_mlen = 0;
4659 inst->header_size = header_size;
4660 inst->check_tdr = true;
4661 inst->send_has_side_effects = true;
4662 } else {
4663 /* Send from the MRF */
4664 load = bld.LOAD_PAYLOAD(fs_reg(MRF, 1, BRW_REGISTER_TYPE_F),
4665 sources, length, payload_header_size);
4666
4667 /* On pre-SNB, we have to interlace the color values. LOAD_PAYLOAD
4668 * will do this for us if we just give it a COMPR4 destination.
4669 */
4670 if (devinfo->gen < 6 && bld.dispatch_width() == 16)
4671 load->dst.nr |= BRW_MRF_COMPR4;
4672
4673 if (devinfo->gen < 6) {
4674 /* Set up src[0] for the implied MOV from grf0-1 */
4675 inst->resize_sources(1);
4676 inst->src[0] = brw_vec8_grf(0, 0);
4677 } else {
4678 inst->resize_sources(0);
4679 }
4680 inst->base_mrf = 1;
4681 inst->opcode = FS_OPCODE_FB_WRITE;
4682 inst->mlen = regs_written(load);
4683 inst->header_size = header_size;
4684 }
4685 }
4686
4687 static void
4688 lower_fb_read_logical_send(const fs_builder &bld, fs_inst *inst)
4689 {
4690 const gen_device_info *devinfo = bld.shader->devinfo;
4691 const fs_builder &ubld = bld.exec_all().group(8, 0);
4692 const unsigned length = 2;
4693 const fs_reg header = ubld.vgrf(BRW_REGISTER_TYPE_UD, length);
4694
4695 if (bld.group() < 16) {
4696 ubld.group(16, 0).MOV(header, retype(brw_vec8_grf(0, 0),
4697 BRW_REGISTER_TYPE_UD));
4698 } else {
4699 assert(bld.group() < 32);
4700 const fs_reg header_sources[] = {
4701 retype(brw_vec8_grf(0, 0), BRW_REGISTER_TYPE_UD),
4702 retype(brw_vec8_grf(2, 0), BRW_REGISTER_TYPE_UD)
4703 };
4704 ubld.LOAD_PAYLOAD(header, header_sources, ARRAY_SIZE(header_sources), 0);
4705
4706 if (devinfo->gen >= 12) {
4707 /* On Gen12 the Viewport and Render Target Array Index fields (AKA
4708 * Poly 0 Info) are provided in r1.1 instead of r0.0, and the render
4709 * target message header format was updated accordingly -- However
4710 * the updated format only works for the lower 16 channels in a
4711 * SIMD32 thread, since the higher 16 channels want the subspan data
4712 * from r2 instead of r1, so we need to copy over the contents of
4713 * r1.1 in order to fix things up.
4714 */
4715 ubld.group(1, 0).MOV(component(header, 9),
4716 retype(brw_vec1_grf(1, 1), BRW_REGISTER_TYPE_UD));
4717 }
4718 }
4719
4720 inst->resize_sources(1);
4721 inst->src[0] = header;
4722 inst->opcode = FS_OPCODE_FB_READ;
4723 inst->mlen = length;
4724 inst->header_size = length;
4725 }
4726
4727 static void
4728 lower_sampler_logical_send_gen4(const fs_builder &bld, fs_inst *inst, opcode op,
4729 const fs_reg &coordinate,
4730 const fs_reg &shadow_c,
4731 const fs_reg &lod, const fs_reg &lod2,
4732 const fs_reg &surface,
4733 const fs_reg &sampler,
4734 unsigned coord_components,
4735 unsigned grad_components)
4736 {
4737 const bool has_lod = (op == SHADER_OPCODE_TXL || op == FS_OPCODE_TXB ||
4738 op == SHADER_OPCODE_TXF || op == SHADER_OPCODE_TXS);
4739 fs_reg msg_begin(MRF, 1, BRW_REGISTER_TYPE_F);
4740 fs_reg msg_end = msg_begin;
4741
4742 /* g0 header. */
4743 msg_end = offset(msg_end, bld.group(8, 0), 1);
4744
4745 for (unsigned i = 0; i < coord_components; i++)
4746 bld.MOV(retype(offset(msg_end, bld, i), coordinate.type),
4747 offset(coordinate, bld, i));
4748
4749 msg_end = offset(msg_end, bld, coord_components);
4750
4751 /* Messages other than SAMPLE and RESINFO in SIMD16 and TXD in SIMD8
4752 * require all three components to be present and zero if they are unused.
4753 */
4754 if (coord_components > 0 &&
4755 (has_lod || shadow_c.file != BAD_FILE ||
4756 (op == SHADER_OPCODE_TEX && bld.dispatch_width() == 8))) {
4757 assert(coord_components <= 3);
4758 for (unsigned i = 0; i < 3 - coord_components; i++)
4759 bld.MOV(offset(msg_end, bld, i), brw_imm_f(0.0f));
4760
4761 msg_end = offset(msg_end, bld, 3 - coord_components);
4762 }
4763
4764 if (op == SHADER_OPCODE_TXD) {
4765 /* TXD unsupported in SIMD16 mode. */
4766 assert(bld.dispatch_width() == 8);
4767
4768 /* the slots for u and v are always present, but r is optional */
4769 if (coord_components < 2)
4770 msg_end = offset(msg_end, bld, 2 - coord_components);
4771
4772 /* P = u, v, r
4773 * dPdx = dudx, dvdx, drdx
4774 * dPdy = dudy, dvdy, drdy
4775 *
4776 * 1-arg: Does not exist.
4777 *
4778 * 2-arg: dudx dvdx dudy dvdy
4779 * dPdx.x dPdx.y dPdy.x dPdy.y
4780 * m4 m5 m6 m7
4781 *
4782 * 3-arg: dudx dvdx drdx dudy dvdy drdy
4783 * dPdx.x dPdx.y dPdx.z dPdy.x dPdy.y dPdy.z
4784 * m5 m6 m7 m8 m9 m10
4785 */
4786 for (unsigned i = 0; i < grad_components; i++)
4787 bld.MOV(offset(msg_end, bld, i), offset(lod, bld, i));
4788
4789 msg_end = offset(msg_end, bld, MAX2(grad_components, 2));
4790
4791 for (unsigned i = 0; i < grad_components; i++)
4792 bld.MOV(offset(msg_end, bld, i), offset(lod2, bld, i));
4793
4794 msg_end = offset(msg_end, bld, MAX2(grad_components, 2));
4795 }
4796
4797 if (has_lod) {
4798 /* Bias/LOD with shadow comparator is unsupported in SIMD16 -- *Without*
4799 * shadow comparator (including RESINFO) it's unsupported in SIMD8 mode.
4800 */
4801 assert(shadow_c.file != BAD_FILE ? bld.dispatch_width() == 8 :
4802 bld.dispatch_width() == 16);
4803
4804 const brw_reg_type type =
4805 (op == SHADER_OPCODE_TXF || op == SHADER_OPCODE_TXS ?
4806 BRW_REGISTER_TYPE_UD : BRW_REGISTER_TYPE_F);
4807 bld.MOV(retype(msg_end, type), lod);
4808 msg_end = offset(msg_end, bld, 1);
4809 }
4810
4811 if (shadow_c.file != BAD_FILE) {
4812 if (op == SHADER_OPCODE_TEX && bld.dispatch_width() == 8) {
4813 /* There's no plain shadow compare message, so we use shadow
4814 * compare with a bias of 0.0.
4815 */
4816 bld.MOV(msg_end, brw_imm_f(0.0f));
4817 msg_end = offset(msg_end, bld, 1);
4818 }
4819
4820 bld.MOV(msg_end, shadow_c);
4821 msg_end = offset(msg_end, bld, 1);
4822 }
4823
4824 inst->opcode = op;
4825 inst->src[0] = reg_undef;
4826 inst->src[1] = surface;
4827 inst->src[2] = sampler;
4828 inst->resize_sources(3);
4829 inst->base_mrf = msg_begin.nr;
4830 inst->mlen = msg_end.nr - msg_begin.nr;
4831 inst->header_size = 1;
4832 }
4833
4834 static void
4835 lower_sampler_logical_send_gen5(const fs_builder &bld, fs_inst *inst, opcode op,
4836 const fs_reg &coordinate,
4837 const fs_reg &shadow_c,
4838 const fs_reg &lod, const fs_reg &lod2,
4839 const fs_reg &sample_index,
4840 const fs_reg &surface,
4841 const fs_reg &sampler,
4842 unsigned coord_components,
4843 unsigned grad_components)
4844 {
4845 fs_reg message(MRF, 2, BRW_REGISTER_TYPE_F);
4846 fs_reg msg_coords = message;
4847 unsigned header_size = 0;
4848
4849 if (inst->offset != 0) {
4850 /* The offsets set up by the visitor are in the m1 header, so we can't
4851 * go headerless.
4852 */
4853 header_size = 1;
4854 message.nr--;
4855 }
4856
4857 for (unsigned i = 0; i < coord_components; i++)
4858 bld.MOV(retype(offset(msg_coords, bld, i), coordinate.type),
4859 offset(coordinate, bld, i));
4860
4861 fs_reg msg_end = offset(msg_coords, bld, coord_components);
4862 fs_reg msg_lod = offset(msg_coords, bld, 4);
4863
4864 if (shadow_c.file != BAD_FILE) {
4865 fs_reg msg_shadow = msg_lod;
4866 bld.MOV(msg_shadow, shadow_c);
4867 msg_lod = offset(msg_shadow, bld, 1);
4868 msg_end = msg_lod;
4869 }
4870
4871 switch (op) {
4872 case SHADER_OPCODE_TXL:
4873 case FS_OPCODE_TXB:
4874 bld.MOV(msg_lod, lod);
4875 msg_end = offset(msg_lod, bld, 1);
4876 break;
4877 case SHADER_OPCODE_TXD:
4878 /**
4879 * P = u, v, r
4880 * dPdx = dudx, dvdx, drdx
4881 * dPdy = dudy, dvdy, drdy
4882 *
4883 * Load up these values:
4884 * - dudx dudy dvdx dvdy drdx drdy
4885 * - dPdx.x dPdy.x dPdx.y dPdy.y dPdx.z dPdy.z
4886 */
4887 msg_end = msg_lod;
4888 for (unsigned i = 0; i < grad_components; i++) {
4889 bld.MOV(msg_end, offset(lod, bld, i));
4890 msg_end = offset(msg_end, bld, 1);
4891
4892 bld.MOV(msg_end, offset(lod2, bld, i));
4893 msg_end = offset(msg_end, bld, 1);
4894 }
4895 break;
4896 case SHADER_OPCODE_TXS:
4897 msg_lod = retype(msg_end, BRW_REGISTER_TYPE_UD);
4898 bld.MOV(msg_lod, lod);
4899 msg_end = offset(msg_lod, bld, 1);
4900 break;
4901 case SHADER_OPCODE_TXF:
4902 msg_lod = offset(msg_coords, bld, 3);
4903 bld.MOV(retype(msg_lod, BRW_REGISTER_TYPE_UD), lod);
4904 msg_end = offset(msg_lod, bld, 1);
4905 break;
4906 case SHADER_OPCODE_TXF_CMS:
4907 msg_lod = offset(msg_coords, bld, 3);
4908 /* lod */
4909 bld.MOV(retype(msg_lod, BRW_REGISTER_TYPE_UD), brw_imm_ud(0u));
4910 /* sample index */
4911 bld.MOV(retype(offset(msg_lod, bld, 1), BRW_REGISTER_TYPE_UD), sample_index);
4912 msg_end = offset(msg_lod, bld, 2);
4913 break;
4914 default:
4915 break;
4916 }
4917
4918 inst->opcode = op;
4919 inst->src[0] = reg_undef;
4920 inst->src[1] = surface;
4921 inst->src[2] = sampler;
4922 inst->resize_sources(3);
4923 inst->base_mrf = message.nr;
4924 inst->mlen = msg_end.nr - message.nr;
4925 inst->header_size = header_size;
4926
4927 /* Message length > MAX_SAMPLER_MESSAGE_SIZE disallowed by hardware. */
4928 assert(inst->mlen <= MAX_SAMPLER_MESSAGE_SIZE);
4929 }
4930
4931 static bool
4932 is_high_sampler(const struct gen_device_info *devinfo, const fs_reg &sampler)
4933 {
4934 if (devinfo->gen < 8 && !devinfo->is_haswell)
4935 return false;
4936
4937 return sampler.file != IMM || sampler.ud >= 16;
4938 }
4939
4940 static unsigned
4941 sampler_msg_type(const gen_device_info *devinfo,
4942 opcode opcode, bool shadow_compare)
4943 {
4944 assert(devinfo->gen >= 5);
4945 switch (opcode) {
4946 case SHADER_OPCODE_TEX:
4947 return shadow_compare ? GEN5_SAMPLER_MESSAGE_SAMPLE_COMPARE :
4948 GEN5_SAMPLER_MESSAGE_SAMPLE;
4949 case FS_OPCODE_TXB:
4950 return shadow_compare ? GEN5_SAMPLER_MESSAGE_SAMPLE_BIAS_COMPARE :
4951 GEN5_SAMPLER_MESSAGE_SAMPLE_BIAS;
4952 case SHADER_OPCODE_TXL:
4953 return shadow_compare ? GEN5_SAMPLER_MESSAGE_SAMPLE_LOD_COMPARE :
4954 GEN5_SAMPLER_MESSAGE_SAMPLE_LOD;
4955 case SHADER_OPCODE_TXL_LZ:
4956 return shadow_compare ? GEN9_SAMPLER_MESSAGE_SAMPLE_C_LZ :
4957 GEN9_SAMPLER_MESSAGE_SAMPLE_LZ;
4958 case SHADER_OPCODE_TXS:
4959 case SHADER_OPCODE_IMAGE_SIZE_LOGICAL:
4960 return GEN5_SAMPLER_MESSAGE_SAMPLE_RESINFO;
4961 case SHADER_OPCODE_TXD:
4962 assert(!shadow_compare || devinfo->gen >= 8 || devinfo->is_haswell);
4963 return shadow_compare ? HSW_SAMPLER_MESSAGE_SAMPLE_DERIV_COMPARE :
4964 GEN5_SAMPLER_MESSAGE_SAMPLE_DERIVS;
4965 case SHADER_OPCODE_TXF:
4966 return GEN5_SAMPLER_MESSAGE_SAMPLE_LD;
4967 case SHADER_OPCODE_TXF_LZ:
4968 assert(devinfo->gen >= 9);
4969 return GEN9_SAMPLER_MESSAGE_SAMPLE_LD_LZ;
4970 case SHADER_OPCODE_TXF_CMS_W:
4971 assert(devinfo->gen >= 9);
4972 return GEN9_SAMPLER_MESSAGE_SAMPLE_LD2DMS_W;
4973 case SHADER_OPCODE_TXF_CMS:
4974 return devinfo->gen >= 7 ? GEN7_SAMPLER_MESSAGE_SAMPLE_LD2DMS :
4975 GEN5_SAMPLER_MESSAGE_SAMPLE_LD;
4976 case SHADER_OPCODE_TXF_UMS:
4977 assert(devinfo->gen >= 7);
4978 return GEN7_SAMPLER_MESSAGE_SAMPLE_LD2DSS;
4979 case SHADER_OPCODE_TXF_MCS:
4980 assert(devinfo->gen >= 7);
4981 return GEN7_SAMPLER_MESSAGE_SAMPLE_LD_MCS;
4982 case SHADER_OPCODE_LOD:
4983 return GEN5_SAMPLER_MESSAGE_LOD;
4984 case SHADER_OPCODE_TG4:
4985 assert(devinfo->gen >= 7);
4986 return shadow_compare ? GEN7_SAMPLER_MESSAGE_SAMPLE_GATHER4_C :
4987 GEN7_SAMPLER_MESSAGE_SAMPLE_GATHER4;
4988 break;
4989 case SHADER_OPCODE_TG4_OFFSET:
4990 assert(devinfo->gen >= 7);
4991 return shadow_compare ? GEN7_SAMPLER_MESSAGE_SAMPLE_GATHER4_PO_C :
4992 GEN7_SAMPLER_MESSAGE_SAMPLE_GATHER4_PO;
4993 case SHADER_OPCODE_SAMPLEINFO:
4994 return GEN6_SAMPLER_MESSAGE_SAMPLE_SAMPLEINFO;
4995 default:
4996 unreachable("not reached");
4997 }
4998 }
4999
5000 static void
5001 lower_sampler_logical_send_gen7(const fs_builder &bld, fs_inst *inst, opcode op,
5002 const fs_reg &coordinate,
5003 const fs_reg &shadow_c,
5004 fs_reg lod, const fs_reg &lod2,
5005 const fs_reg &min_lod,
5006 const fs_reg &sample_index,
5007 const fs_reg &mcs,
5008 const fs_reg &surface,
5009 const fs_reg &sampler,
5010 const fs_reg &surface_handle,
5011 const fs_reg &sampler_handle,
5012 const fs_reg &tg4_offset,
5013 unsigned coord_components,
5014 unsigned grad_components)
5015 {
5016 const gen_device_info *devinfo = bld.shader->devinfo;
5017 const brw_stage_prog_data *prog_data = bld.shader->stage_prog_data;
5018 unsigned reg_width = bld.dispatch_width() / 8;
5019 unsigned header_size = 0, length = 0;
5020 fs_reg sources[MAX_SAMPLER_MESSAGE_SIZE];
5021 for (unsigned i = 0; i < ARRAY_SIZE(sources); i++)
5022 sources[i] = bld.vgrf(BRW_REGISTER_TYPE_F);
5023
5024 /* We must have exactly one of surface/sampler and surface/sampler_handle */
5025 assert((surface.file == BAD_FILE) != (surface_handle.file == BAD_FILE));
5026 assert((sampler.file == BAD_FILE) != (sampler_handle.file == BAD_FILE));
5027
5028 if (op == SHADER_OPCODE_TG4 || op == SHADER_OPCODE_TG4_OFFSET ||
5029 inst->offset != 0 || inst->eot ||
5030 op == SHADER_OPCODE_SAMPLEINFO ||
5031 sampler_handle.file != BAD_FILE ||
5032 is_high_sampler(devinfo, sampler)) {
5033 /* For general texture offsets (no txf workaround), we need a header to
5034 * put them in.
5035 *
5036 * TG4 needs to place its channel select in the header, for interaction
5037 * with ARB_texture_swizzle. The sampler index is only 4-bits, so for
5038 * larger sampler numbers we need to offset the Sampler State Pointer in
5039 * the header.
5040 */
5041 fs_reg header = retype(sources[0], BRW_REGISTER_TYPE_UD);
5042 header_size = 1;
5043 length++;
5044
5045 /* If we're requesting fewer than four channels worth of response,
5046 * and we have an explicit header, we need to set up the sampler
5047 * writemask. It's reversed from normal: 1 means "don't write".
5048 */
5049 if (!inst->eot && regs_written(inst) != 4 * reg_width) {
5050 assert(regs_written(inst) % reg_width == 0);
5051 unsigned mask = ~((1 << (regs_written(inst) / reg_width)) - 1) & 0xf;
5052 inst->offset |= mask << 12;
5053 }
5054
5055 /* Build the actual header */
5056 const fs_builder ubld = bld.exec_all().group(8, 0);
5057 const fs_builder ubld1 = ubld.group(1, 0);
5058 ubld.MOV(header, retype(brw_vec8_grf(0, 0), BRW_REGISTER_TYPE_UD));
5059 if (inst->offset) {
5060 ubld1.MOV(component(header, 2), brw_imm_ud(inst->offset));
5061 } else if (bld.shader->stage != MESA_SHADER_VERTEX &&
5062 bld.shader->stage != MESA_SHADER_FRAGMENT) {
5063 /* The vertex and fragment stages have g0.2 set to 0, so
5064 * header0.2 is 0 when g0 is copied. Other stages may not, so we
5065 * must set it to 0 to avoid setting undesirable bits in the
5066 * message.
5067 */
5068 ubld1.MOV(component(header, 2), brw_imm_ud(0));
5069 }
5070
5071 if (sampler_handle.file != BAD_FILE) {
5072 /* Bindless sampler handles aren't relative to the sampler state
5073 * pointer passed into the shader through SAMPLER_STATE_POINTERS_*.
5074 * Instead, it's an absolute pointer relative to dynamic state base
5075 * address.
5076 *
5077 * Sampler states are 16 bytes each and the pointer we give here has
5078 * to be 32-byte aligned. In order to avoid more indirect messages
5079 * than required, we assume that all bindless sampler states are
5080 * 32-byte aligned. This sacrifices a bit of general state base
5081 * address space but means we can do something more efficient in the
5082 * shader.
5083 */
5084 ubld1.MOV(component(header, 3), sampler_handle);
5085 } else if (is_high_sampler(devinfo, sampler)) {
5086 if (sampler.file == BRW_IMMEDIATE_VALUE) {
5087 assert(sampler.ud >= 16);
5088 const int sampler_state_size = 16; /* 16 bytes */
5089
5090 ubld1.ADD(component(header, 3),
5091 retype(brw_vec1_grf(0, 3), BRW_REGISTER_TYPE_UD),
5092 brw_imm_ud(16 * (sampler.ud / 16) * sampler_state_size));
5093 } else {
5094 fs_reg tmp = ubld1.vgrf(BRW_REGISTER_TYPE_UD);
5095 ubld1.AND(tmp, sampler, brw_imm_ud(0x0f0));
5096 ubld1.SHL(tmp, tmp, brw_imm_ud(4));
5097 ubld1.ADD(component(header, 3),
5098 retype(brw_vec1_grf(0, 3), BRW_REGISTER_TYPE_UD),
5099 tmp);
5100 }
5101 }
5102 }
5103
5104 if (shadow_c.file != BAD_FILE) {
5105 bld.MOV(sources[length], shadow_c);
5106 length++;
5107 }
5108
5109 bool coordinate_done = false;
5110
5111 /* Set up the LOD info */
5112 switch (op) {
5113 case FS_OPCODE_TXB:
5114 case SHADER_OPCODE_TXL:
5115 if (devinfo->gen >= 9 && op == SHADER_OPCODE_TXL && lod.is_zero()) {
5116 op = SHADER_OPCODE_TXL_LZ;
5117 break;
5118 }
5119 bld.MOV(sources[length], lod);
5120 length++;
5121 break;
5122 case SHADER_OPCODE_TXD:
5123 /* TXD should have been lowered in SIMD16 mode. */
5124 assert(bld.dispatch_width() == 8);
5125
5126 /* Load dPdx and the coordinate together:
5127 * [hdr], [ref], x, dPdx.x, dPdy.x, y, dPdx.y, dPdy.y, z, dPdx.z, dPdy.z
5128 */
5129 for (unsigned i = 0; i < coord_components; i++) {
5130 bld.MOV(sources[length++], offset(coordinate, bld, i));
5131
5132 /* For cube map array, the coordinate is (u,v,r,ai) but there are
5133 * only derivatives for (u, v, r).
5134 */
5135 if (i < grad_components) {
5136 bld.MOV(sources[length++], offset(lod, bld, i));
5137 bld.MOV(sources[length++], offset(lod2, bld, i));
5138 }
5139 }
5140
5141 coordinate_done = true;
5142 break;
5143 case SHADER_OPCODE_TXS:
5144 bld.MOV(retype(sources[length], BRW_REGISTER_TYPE_UD), lod);
5145 length++;
5146 break;
5147 case SHADER_OPCODE_IMAGE_SIZE_LOGICAL:
5148 /* We need an LOD; just use 0 */
5149 bld.MOV(retype(sources[length], BRW_REGISTER_TYPE_UD), brw_imm_ud(0));
5150 length++;
5151 break;
5152 case SHADER_OPCODE_TXF:
5153 /* Unfortunately, the parameters for LD are intermixed: u, lod, v, r.
5154 * On Gen9 they are u, v, lod, r
5155 */
5156 bld.MOV(retype(sources[length++], BRW_REGISTER_TYPE_D), coordinate);
5157
5158 if (devinfo->gen >= 9) {
5159 if (coord_components >= 2) {
5160 bld.MOV(retype(sources[length], BRW_REGISTER_TYPE_D),
5161 offset(coordinate, bld, 1));
5162 } else {
5163 sources[length] = brw_imm_d(0);
5164 }
5165 length++;
5166 }
5167
5168 if (devinfo->gen >= 9 && lod.is_zero()) {
5169 op = SHADER_OPCODE_TXF_LZ;
5170 } else {
5171 bld.MOV(retype(sources[length], BRW_REGISTER_TYPE_D), lod);
5172 length++;
5173 }
5174
5175 for (unsigned i = devinfo->gen >= 9 ? 2 : 1; i < coord_components; i++)
5176 bld.MOV(retype(sources[length++], BRW_REGISTER_TYPE_D),
5177 offset(coordinate, bld, i));
5178
5179 coordinate_done = true;
5180 break;
5181
5182 case SHADER_OPCODE_TXF_CMS:
5183 case SHADER_OPCODE_TXF_CMS_W:
5184 case SHADER_OPCODE_TXF_UMS:
5185 case SHADER_OPCODE_TXF_MCS:
5186 if (op == SHADER_OPCODE_TXF_UMS ||
5187 op == SHADER_OPCODE_TXF_CMS ||
5188 op == SHADER_OPCODE_TXF_CMS_W) {
5189 bld.MOV(retype(sources[length], BRW_REGISTER_TYPE_UD), sample_index);
5190 length++;
5191 }
5192
5193 if (op == SHADER_OPCODE_TXF_CMS || op == SHADER_OPCODE_TXF_CMS_W) {
5194 /* Data from the multisample control surface. */
5195 bld.MOV(retype(sources[length], BRW_REGISTER_TYPE_UD), mcs);
5196 length++;
5197
5198 /* On Gen9+ we'll use ld2dms_w instead which has two registers for
5199 * the MCS data.
5200 */
5201 if (op == SHADER_OPCODE_TXF_CMS_W) {
5202 bld.MOV(retype(sources[length], BRW_REGISTER_TYPE_UD),
5203 mcs.file == IMM ?
5204 mcs :
5205 offset(mcs, bld, 1));
5206 length++;
5207 }
5208 }
5209
5210 /* There is no offsetting for this message; just copy in the integer
5211 * texture coordinates.
5212 */
5213 for (unsigned i = 0; i < coord_components; i++)
5214 bld.MOV(retype(sources[length++], BRW_REGISTER_TYPE_D),
5215 offset(coordinate, bld, i));
5216
5217 coordinate_done = true;
5218 break;
5219 case SHADER_OPCODE_TG4_OFFSET:
5220 /* More crazy intermixing */
5221 for (unsigned i = 0; i < 2; i++) /* u, v */
5222 bld.MOV(sources[length++], offset(coordinate, bld, i));
5223
5224 for (unsigned i = 0; i < 2; i++) /* offu, offv */
5225 bld.MOV(retype(sources[length++], BRW_REGISTER_TYPE_D),
5226 offset(tg4_offset, bld, i));
5227
5228 if (coord_components == 3) /* r if present */
5229 bld.MOV(sources[length++], offset(coordinate, bld, 2));
5230
5231 coordinate_done = true;
5232 break;
5233 default:
5234 break;
5235 }
5236
5237 /* Set up the coordinate (except for cases where it was done above) */
5238 if (!coordinate_done) {
5239 for (unsigned i = 0; i < coord_components; i++)
5240 bld.MOV(sources[length++], offset(coordinate, bld, i));
5241 }
5242
5243 if (min_lod.file != BAD_FILE) {
5244 /* Account for all of the missing coordinate sources */
5245 length += 4 - coord_components;
5246 if (op == SHADER_OPCODE_TXD)
5247 length += (3 - grad_components) * 2;
5248
5249 bld.MOV(sources[length++], min_lod);
5250 }
5251
5252 unsigned mlen;
5253 if (reg_width == 2)
5254 mlen = length * reg_width - header_size;
5255 else
5256 mlen = length * reg_width;
5257
5258 const fs_reg src_payload = fs_reg(VGRF, bld.shader->alloc.allocate(mlen),
5259 BRW_REGISTER_TYPE_F);
5260 bld.LOAD_PAYLOAD(src_payload, sources, length, header_size);
5261
5262 /* Generate the SEND. */
5263 inst->opcode = SHADER_OPCODE_SEND;
5264 inst->mlen = mlen;
5265 inst->header_size = header_size;
5266
5267 const unsigned msg_type =
5268 sampler_msg_type(devinfo, op, inst->shadow_compare);
5269 const unsigned simd_mode =
5270 inst->exec_size <= 8 ? BRW_SAMPLER_SIMD_MODE_SIMD8 :
5271 BRW_SAMPLER_SIMD_MODE_SIMD16;
5272
5273 uint32_t base_binding_table_index;
5274 switch (op) {
5275 case SHADER_OPCODE_TG4:
5276 case SHADER_OPCODE_TG4_OFFSET:
5277 base_binding_table_index = prog_data->binding_table.gather_texture_start;
5278 break;
5279 case SHADER_OPCODE_IMAGE_SIZE_LOGICAL:
5280 base_binding_table_index = prog_data->binding_table.image_start;
5281 break;
5282 default:
5283 base_binding_table_index = prog_data->binding_table.texture_start;
5284 break;
5285 }
5286
5287 inst->sfid = BRW_SFID_SAMPLER;
5288 if (surface.file == IMM &&
5289 (sampler.file == IMM || sampler_handle.file != BAD_FILE)) {
5290 inst->desc = brw_sampler_desc(devinfo,
5291 surface.ud + base_binding_table_index,
5292 sampler.file == IMM ? sampler.ud % 16 : 0,
5293 msg_type,
5294 simd_mode,
5295 0 /* return_format unused on gen7+ */);
5296 inst->src[0] = brw_imm_ud(0);
5297 inst->src[1] = brw_imm_ud(0); /* ex_desc */
5298 } else if (surface_handle.file != BAD_FILE) {
5299 /* Bindless surface */
5300 assert(devinfo->gen >= 9);
5301 inst->desc = brw_sampler_desc(devinfo,
5302 GEN9_BTI_BINDLESS,
5303 sampler.file == IMM ? sampler.ud % 16 : 0,
5304 msg_type,
5305 simd_mode,
5306 0 /* return_format unused on gen7+ */);
5307
5308 /* For bindless samplers, the entire address is included in the message
5309 * header so we can leave the portion in the message descriptor 0.
5310 */
5311 if (sampler_handle.file != BAD_FILE || sampler.file == IMM) {
5312 inst->src[0] = brw_imm_ud(0);
5313 } else {
5314 const fs_builder ubld = bld.group(1, 0).exec_all();
5315 fs_reg desc = ubld.vgrf(BRW_REGISTER_TYPE_UD);
5316 ubld.SHL(desc, sampler, brw_imm_ud(8));
5317 inst->src[0] = desc;
5318 }
5319
5320 /* We assume that the driver provided the handle in the top 20 bits so
5321 * we can use the surface handle directly as the extended descriptor.
5322 */
5323 inst->src[1] = retype(surface_handle, BRW_REGISTER_TYPE_UD);
5324 } else {
5325 /* Immediate portion of the descriptor */
5326 inst->desc = brw_sampler_desc(devinfo,
5327 0, /* surface */
5328 0, /* sampler */
5329 msg_type,
5330 simd_mode,
5331 0 /* return_format unused on gen7+ */);
5332 const fs_builder ubld = bld.group(1, 0).exec_all();
5333 fs_reg desc = ubld.vgrf(BRW_REGISTER_TYPE_UD);
5334 if (surface.equals(sampler)) {
5335 /* This case is common in GL */
5336 ubld.MUL(desc, surface, brw_imm_ud(0x101));
5337 } else {
5338 if (sampler_handle.file != BAD_FILE) {
5339 ubld.MOV(desc, surface);
5340 } else if (sampler.file == IMM) {
5341 ubld.OR(desc, surface, brw_imm_ud(sampler.ud << 8));
5342 } else {
5343 ubld.SHL(desc, sampler, brw_imm_ud(8));
5344 ubld.OR(desc, desc, surface);
5345 }
5346 }
5347 if (base_binding_table_index)
5348 ubld.ADD(desc, desc, brw_imm_ud(base_binding_table_index));
5349 ubld.AND(desc, desc, brw_imm_ud(0xfff));
5350
5351 inst->src[0] = component(desc, 0);
5352 inst->src[1] = brw_imm_ud(0); /* ex_desc */
5353 }
5354
5355 inst->src[2] = src_payload;
5356 inst->resize_sources(3);
5357
5358 if (inst->eot) {
5359 /* EOT sampler messages don't make sense to split because it would
5360 * involve ending half of the thread early.
5361 */
5362 assert(inst->group == 0);
5363 /* We need to use SENDC for EOT sampler messages */
5364 inst->check_tdr = true;
5365 inst->send_has_side_effects = true;
5366 }
5367
5368 /* Message length > MAX_SAMPLER_MESSAGE_SIZE disallowed by hardware. */
5369 assert(inst->mlen <= MAX_SAMPLER_MESSAGE_SIZE);
5370 }
5371
5372 static void
5373 lower_sampler_logical_send(const fs_builder &bld, fs_inst *inst, opcode op)
5374 {
5375 const gen_device_info *devinfo = bld.shader->devinfo;
5376 const fs_reg &coordinate = inst->src[TEX_LOGICAL_SRC_COORDINATE];
5377 const fs_reg &shadow_c = inst->src[TEX_LOGICAL_SRC_SHADOW_C];
5378 const fs_reg &lod = inst->src[TEX_LOGICAL_SRC_LOD];
5379 const fs_reg &lod2 = inst->src[TEX_LOGICAL_SRC_LOD2];
5380 const fs_reg &min_lod = inst->src[TEX_LOGICAL_SRC_MIN_LOD];
5381 const fs_reg &sample_index = inst->src[TEX_LOGICAL_SRC_SAMPLE_INDEX];
5382 const fs_reg &mcs = inst->src[TEX_LOGICAL_SRC_MCS];
5383 const fs_reg &surface = inst->src[TEX_LOGICAL_SRC_SURFACE];
5384 const fs_reg &sampler = inst->src[TEX_LOGICAL_SRC_SAMPLER];
5385 const fs_reg &surface_handle = inst->src[TEX_LOGICAL_SRC_SURFACE_HANDLE];
5386 const fs_reg &sampler_handle = inst->src[TEX_LOGICAL_SRC_SAMPLER_HANDLE];
5387 const fs_reg &tg4_offset = inst->src[TEX_LOGICAL_SRC_TG4_OFFSET];
5388 assert(inst->src[TEX_LOGICAL_SRC_COORD_COMPONENTS].file == IMM);
5389 const unsigned coord_components = inst->src[TEX_LOGICAL_SRC_COORD_COMPONENTS].ud;
5390 assert(inst->src[TEX_LOGICAL_SRC_GRAD_COMPONENTS].file == IMM);
5391 const unsigned grad_components = inst->src[TEX_LOGICAL_SRC_GRAD_COMPONENTS].ud;
5392
5393 if (devinfo->gen >= 7) {
5394 lower_sampler_logical_send_gen7(bld, inst, op, coordinate,
5395 shadow_c, lod, lod2, min_lod,
5396 sample_index,
5397 mcs, surface, sampler,
5398 surface_handle, sampler_handle,
5399 tg4_offset,
5400 coord_components, grad_components);
5401 } else if (devinfo->gen >= 5) {
5402 lower_sampler_logical_send_gen5(bld, inst, op, coordinate,
5403 shadow_c, lod, lod2, sample_index,
5404 surface, sampler,
5405 coord_components, grad_components);
5406 } else {
5407 lower_sampler_logical_send_gen4(bld, inst, op, coordinate,
5408 shadow_c, lod, lod2,
5409 surface, sampler,
5410 coord_components, grad_components);
5411 }
5412 }
5413
5414 /**
5415 * Predicate the specified instruction on the sample mask.
5416 */
5417 static void
5418 emit_predicate_on_sample_mask(const fs_builder &bld, fs_inst *inst)
5419 {
5420 assert(bld.shader->stage == MESA_SHADER_FRAGMENT &&
5421 bld.group() == inst->group &&
5422 bld.dispatch_width() == inst->exec_size);
5423
5424 const fs_visitor *v = static_cast<const fs_visitor *>(bld.shader);
5425 const fs_reg sample_mask = sample_mask_reg(bld);
5426 const unsigned subreg = sample_mask_flag_subreg(v);
5427
5428 if (brw_wm_prog_data(v->stage_prog_data)->uses_kill) {
5429 assert(sample_mask.file == ARF &&
5430 sample_mask.nr == brw_flag_subreg(subreg).nr &&
5431 sample_mask.subnr == brw_flag_subreg(
5432 subreg + inst->group / 16).subnr);
5433 } else {
5434 bld.group(1, 0).exec_all()
5435 .MOV(brw_flag_subreg(subreg + inst->group / 16), sample_mask);
5436 }
5437
5438 if (inst->predicate) {
5439 assert(inst->predicate == BRW_PREDICATE_NORMAL);
5440 assert(!inst->predicate_inverse);
5441 assert(inst->flag_subreg == 0);
5442 /* Combine the sample mask with the existing predicate by using a
5443 * vertical predication mode.
5444 */
5445 inst->predicate = BRW_PREDICATE_ALIGN1_ALLV;
5446 } else {
5447 inst->flag_subreg = subreg;
5448 inst->predicate = BRW_PREDICATE_NORMAL;
5449 inst->predicate_inverse = false;
5450 }
5451 }
5452
5453 static void
5454 lower_surface_logical_send(const fs_builder &bld, fs_inst *inst)
5455 {
5456 const gen_device_info *devinfo = bld.shader->devinfo;
5457
5458 /* Get the logical send arguments. */
5459 const fs_reg &addr = inst->src[SURFACE_LOGICAL_SRC_ADDRESS];
5460 const fs_reg &src = inst->src[SURFACE_LOGICAL_SRC_DATA];
5461 const fs_reg &surface = inst->src[SURFACE_LOGICAL_SRC_SURFACE];
5462 const fs_reg &surface_handle = inst->src[SURFACE_LOGICAL_SRC_SURFACE_HANDLE];
5463 const UNUSED fs_reg &dims = inst->src[SURFACE_LOGICAL_SRC_IMM_DIMS];
5464 const fs_reg &arg = inst->src[SURFACE_LOGICAL_SRC_IMM_ARG];
5465 assert(arg.file == IMM);
5466
5467 /* We must have exactly one of surface and surface_handle */
5468 assert((surface.file == BAD_FILE) != (surface_handle.file == BAD_FILE));
5469
5470 /* Calculate the total number of components of the payload. */
5471 const unsigned addr_sz = inst->components_read(SURFACE_LOGICAL_SRC_ADDRESS);
5472 const unsigned src_sz = inst->components_read(SURFACE_LOGICAL_SRC_DATA);
5473
5474 const bool is_typed_access =
5475 inst->opcode == SHADER_OPCODE_TYPED_SURFACE_READ_LOGICAL ||
5476 inst->opcode == SHADER_OPCODE_TYPED_SURFACE_WRITE_LOGICAL ||
5477 inst->opcode == SHADER_OPCODE_TYPED_ATOMIC_LOGICAL;
5478
5479 const bool is_surface_access = is_typed_access ||
5480 inst->opcode == SHADER_OPCODE_UNTYPED_SURFACE_READ_LOGICAL ||
5481 inst->opcode == SHADER_OPCODE_UNTYPED_SURFACE_WRITE_LOGICAL ||
5482 inst->opcode == SHADER_OPCODE_UNTYPED_ATOMIC_LOGICAL;
5483
5484 const bool is_stateless =
5485 surface.file == IMM && (surface.ud == BRW_BTI_STATELESS ||
5486 surface.ud == GEN8_BTI_STATELESS_NON_COHERENT);
5487
5488 const bool has_side_effects = inst->has_side_effects();
5489 fs_reg sample_mask = has_side_effects ? sample_mask_reg(bld) :
5490 fs_reg(brw_imm_d(0xffff));
5491
5492 /* From the BDW PRM Volume 7, page 147:
5493 *
5494 * "For the Data Cache Data Port*, the header must be present for the
5495 * following message types: [...] Typed read/write/atomics"
5496 *
5497 * Earlier generations have a similar wording. Because of this restriction
5498 * we don't attempt to implement sample masks via predication for such
5499 * messages prior to Gen9, since we have to provide a header anyway. On
5500 * Gen11+ the header has been removed so we can only use predication.
5501 *
5502 * For all stateless A32 messages, we also need a header
5503 */
5504 fs_reg header;
5505 if ((devinfo->gen < 9 && is_typed_access) || is_stateless) {
5506 fs_builder ubld = bld.exec_all().group(8, 0);
5507 header = ubld.vgrf(BRW_REGISTER_TYPE_UD);
5508 ubld.MOV(header, brw_imm_d(0));
5509 if (is_stateless) {
5510 /* Both the typed and scattered byte/dword A32 messages take a buffer
5511 * base address in R0.5:[31:0] (See MH1_A32_PSM for typed messages or
5512 * MH_A32_GO for byte/dword scattered messages in the SKL PRM Vol. 2d
5513 * for more details.) This is conveniently where the HW places the
5514 * scratch surface base address.
5515 *
5516 * From the SKL PRM Vol. 7 "Per-Thread Scratch Space":
5517 *
5518 * "When a thread becomes 'active' it is allocated a portion of
5519 * scratch space, sized according to PerThreadScratchSpace. The
5520 * starting location of each thread’s scratch space allocation,
5521 * ScratchSpaceOffset, is passed in the thread payload in
5522 * R0.5[31:10] and is specified as a 1KB-granular offset from the
5523 * GeneralStateBaseAddress. The computation of ScratchSpaceOffset
5524 * includes the starting address of the stage’s scratch space
5525 * allocation, as programmed by ScratchSpaceBasePointer."
5526 *
5527 * The base address is passed in bits R0.5[31:10] and the bottom 10
5528 * bits of R0.5 are used for other things. Therefore, we have to
5529 * mask off the bottom 10 bits so that we don't get a garbage base
5530 * address.
5531 */
5532 ubld.group(1, 0).AND(component(header, 5),
5533 retype(brw_vec1_grf(0, 5), BRW_REGISTER_TYPE_UD),
5534 brw_imm_ud(0xfffffc00));
5535 }
5536 if (is_surface_access)
5537 ubld.group(1, 0).MOV(component(header, 7), sample_mask);
5538 }
5539 const unsigned header_sz = header.file != BAD_FILE ? 1 : 0;
5540
5541 fs_reg payload, payload2;
5542 unsigned mlen, ex_mlen = 0;
5543 if (devinfo->gen >= 9 &&
5544 (src.file == BAD_FILE || header.file == BAD_FILE)) {
5545 /* We have split sends on gen9 and above */
5546 if (header.file == BAD_FILE) {
5547 payload = bld.move_to_vgrf(addr, addr_sz);
5548 payload2 = bld.move_to_vgrf(src, src_sz);
5549 mlen = addr_sz * (inst->exec_size / 8);
5550 ex_mlen = src_sz * (inst->exec_size / 8);
5551 } else {
5552 assert(src.file == BAD_FILE);
5553 payload = header;
5554 payload2 = bld.move_to_vgrf(addr, addr_sz);
5555 mlen = header_sz;
5556 ex_mlen = addr_sz * (inst->exec_size / 8);
5557 }
5558 } else {
5559 /* Allocate space for the payload. */
5560 const unsigned sz = header_sz + addr_sz + src_sz;
5561 payload = bld.vgrf(BRW_REGISTER_TYPE_UD, sz);
5562 fs_reg *const components = new fs_reg[sz];
5563 unsigned n = 0;
5564
5565 /* Construct the payload. */
5566 if (header.file != BAD_FILE)
5567 components[n++] = header;
5568
5569 for (unsigned i = 0; i < addr_sz; i++)
5570 components[n++] = offset(addr, bld, i);
5571
5572 for (unsigned i = 0; i < src_sz; i++)
5573 components[n++] = offset(src, bld, i);
5574
5575 bld.LOAD_PAYLOAD(payload, components, sz, header_sz);
5576 mlen = header_sz + (addr_sz + src_sz) * inst->exec_size / 8;
5577
5578 delete[] components;
5579 }
5580
5581 /* Predicate the instruction on the sample mask if no header is
5582 * provided.
5583 */
5584 if ((header.file == BAD_FILE || !is_surface_access) &&
5585 sample_mask.file != BAD_FILE && sample_mask.file != IMM)
5586 emit_predicate_on_sample_mask(bld, inst);
5587
5588 uint32_t sfid;
5589 switch (inst->opcode) {
5590 case SHADER_OPCODE_BYTE_SCATTERED_WRITE_LOGICAL:
5591 case SHADER_OPCODE_BYTE_SCATTERED_READ_LOGICAL:
5592 /* Byte scattered opcodes go through the normal data cache */
5593 sfid = GEN7_SFID_DATAPORT_DATA_CACHE;
5594 break;
5595
5596 case SHADER_OPCODE_DWORD_SCATTERED_READ_LOGICAL:
5597 case SHADER_OPCODE_DWORD_SCATTERED_WRITE_LOGICAL:
5598 sfid = devinfo->gen >= 7 ? GEN7_SFID_DATAPORT_DATA_CACHE :
5599 devinfo->gen >= 6 ? GEN6_SFID_DATAPORT_RENDER_CACHE :
5600 BRW_DATAPORT_READ_TARGET_RENDER_CACHE;
5601 break;
5602
5603 case SHADER_OPCODE_UNTYPED_SURFACE_READ_LOGICAL:
5604 case SHADER_OPCODE_UNTYPED_SURFACE_WRITE_LOGICAL:
5605 case SHADER_OPCODE_UNTYPED_ATOMIC_LOGICAL:
5606 case SHADER_OPCODE_UNTYPED_ATOMIC_FLOAT_LOGICAL:
5607 /* Untyped Surface messages go through the data cache but the SFID value
5608 * changed on Haswell.
5609 */
5610 sfid = (devinfo->gen >= 8 || devinfo->is_haswell ?
5611 HSW_SFID_DATAPORT_DATA_CACHE_1 :
5612 GEN7_SFID_DATAPORT_DATA_CACHE);
5613 break;
5614
5615 case SHADER_OPCODE_TYPED_SURFACE_READ_LOGICAL:
5616 case SHADER_OPCODE_TYPED_SURFACE_WRITE_LOGICAL:
5617 case SHADER_OPCODE_TYPED_ATOMIC_LOGICAL:
5618 /* Typed surface messages go through the render cache on IVB and the
5619 * data cache on HSW+.
5620 */
5621 sfid = (devinfo->gen >= 8 || devinfo->is_haswell ?
5622 HSW_SFID_DATAPORT_DATA_CACHE_1 :
5623 GEN6_SFID_DATAPORT_RENDER_CACHE);
5624 break;
5625
5626 default:
5627 unreachable("Unsupported surface opcode");
5628 }
5629
5630 uint32_t desc;
5631 switch (inst->opcode) {
5632 case SHADER_OPCODE_UNTYPED_SURFACE_READ_LOGICAL:
5633 desc = brw_dp_untyped_surface_rw_desc(devinfo, inst->exec_size,
5634 arg.ud, /* num_channels */
5635 false /* write */);
5636 break;
5637
5638 case SHADER_OPCODE_UNTYPED_SURFACE_WRITE_LOGICAL:
5639 desc = brw_dp_untyped_surface_rw_desc(devinfo, inst->exec_size,
5640 arg.ud, /* num_channels */
5641 true /* write */);
5642 break;
5643
5644 case SHADER_OPCODE_BYTE_SCATTERED_READ_LOGICAL:
5645 desc = brw_dp_byte_scattered_rw_desc(devinfo, inst->exec_size,
5646 arg.ud, /* bit_size */
5647 false /* write */);
5648 break;
5649
5650 case SHADER_OPCODE_BYTE_SCATTERED_WRITE_LOGICAL:
5651 desc = brw_dp_byte_scattered_rw_desc(devinfo, inst->exec_size,
5652 arg.ud, /* bit_size */
5653 true /* write */);
5654 break;
5655
5656 case SHADER_OPCODE_DWORD_SCATTERED_READ_LOGICAL:
5657 assert(arg.ud == 32); /* bit_size */
5658 desc = brw_dp_dword_scattered_rw_desc(devinfo, inst->exec_size,
5659 false /* write */);
5660 break;
5661
5662 case SHADER_OPCODE_DWORD_SCATTERED_WRITE_LOGICAL:
5663 assert(arg.ud == 32); /* bit_size */
5664 desc = brw_dp_dword_scattered_rw_desc(devinfo, inst->exec_size,
5665 true /* write */);
5666 break;
5667
5668 case SHADER_OPCODE_UNTYPED_ATOMIC_LOGICAL:
5669 desc = brw_dp_untyped_atomic_desc(devinfo, inst->exec_size,
5670 arg.ud, /* atomic_op */
5671 !inst->dst.is_null());
5672 break;
5673
5674 case SHADER_OPCODE_UNTYPED_ATOMIC_FLOAT_LOGICAL:
5675 desc = brw_dp_untyped_atomic_float_desc(devinfo, inst->exec_size,
5676 arg.ud, /* atomic_op */
5677 !inst->dst.is_null());
5678 break;
5679
5680 case SHADER_OPCODE_TYPED_SURFACE_READ_LOGICAL:
5681 desc = brw_dp_typed_surface_rw_desc(devinfo, inst->exec_size, inst->group,
5682 arg.ud, /* num_channels */
5683 false /* write */);
5684 break;
5685
5686 case SHADER_OPCODE_TYPED_SURFACE_WRITE_LOGICAL:
5687 desc = brw_dp_typed_surface_rw_desc(devinfo, inst->exec_size, inst->group,
5688 arg.ud, /* num_channels */
5689 true /* write */);
5690 break;
5691
5692 case SHADER_OPCODE_TYPED_ATOMIC_LOGICAL:
5693 desc = brw_dp_typed_atomic_desc(devinfo, inst->exec_size, inst->group,
5694 arg.ud, /* atomic_op */
5695 !inst->dst.is_null());
5696 break;
5697
5698 default:
5699 unreachable("Unknown surface logical instruction");
5700 }
5701
5702 /* Update the original instruction. */
5703 inst->opcode = SHADER_OPCODE_SEND;
5704 inst->mlen = mlen;
5705 inst->ex_mlen = ex_mlen;
5706 inst->header_size = header_sz;
5707 inst->send_has_side_effects = has_side_effects;
5708 inst->send_is_volatile = !has_side_effects;
5709
5710 /* Set up SFID and descriptors */
5711 inst->sfid = sfid;
5712 inst->desc = desc;
5713 if (surface.file == IMM) {
5714 inst->desc |= surface.ud & 0xff;
5715 inst->src[0] = brw_imm_ud(0);
5716 inst->src[1] = brw_imm_ud(0); /* ex_desc */
5717 } else if (surface_handle.file != BAD_FILE) {
5718 /* Bindless surface */
5719 assert(devinfo->gen >= 9);
5720 inst->desc |= GEN9_BTI_BINDLESS;
5721 inst->src[0] = brw_imm_ud(0);
5722
5723 /* We assume that the driver provided the handle in the top 20 bits so
5724 * we can use the surface handle directly as the extended descriptor.
5725 */
5726 inst->src[1] = retype(surface_handle, BRW_REGISTER_TYPE_UD);
5727 } else {
5728 const fs_builder ubld = bld.exec_all().group(1, 0);
5729 fs_reg tmp = ubld.vgrf(BRW_REGISTER_TYPE_UD);
5730 ubld.AND(tmp, surface, brw_imm_ud(0xff));
5731 inst->src[0] = component(tmp, 0);
5732 inst->src[1] = brw_imm_ud(0); /* ex_desc */
5733 }
5734
5735 /* Finally, the payload */
5736 inst->src[2] = payload;
5737 inst->src[3] = payload2;
5738
5739 inst->resize_sources(4);
5740 }
5741
5742 static void
5743 lower_a64_logical_send(const fs_builder &bld, fs_inst *inst)
5744 {
5745 const gen_device_info *devinfo = bld.shader->devinfo;
5746
5747 const fs_reg &addr = inst->src[0];
5748 const fs_reg &src = inst->src[1];
5749 const unsigned src_comps = inst->components_read(1);
5750 assert(inst->src[2].file == IMM);
5751 const unsigned arg = inst->src[2].ud;
5752 const bool has_side_effects = inst->has_side_effects();
5753
5754 /* If the surface message has side effects and we're a fragment shader, we
5755 * have to predicate with the sample mask to avoid helper invocations.
5756 */
5757 if (has_side_effects && bld.shader->stage == MESA_SHADER_FRAGMENT)
5758 emit_predicate_on_sample_mask(bld, inst);
5759
5760 fs_reg payload, payload2;
5761 unsigned mlen, ex_mlen = 0;
5762 if (devinfo->gen >= 9) {
5763 /* On Skylake and above, we have SENDS */
5764 mlen = 2 * (inst->exec_size / 8);
5765 ex_mlen = src_comps * type_sz(src.type) * inst->exec_size / REG_SIZE;
5766 payload = retype(bld.move_to_vgrf(addr, 1), BRW_REGISTER_TYPE_UD);
5767 payload2 = retype(bld.move_to_vgrf(src, src_comps),
5768 BRW_REGISTER_TYPE_UD);
5769 } else {
5770 /* Add two because the address is 64-bit */
5771 const unsigned dwords = 2 + src_comps;
5772 mlen = dwords * (inst->exec_size / 8);
5773
5774 fs_reg sources[5];
5775
5776 sources[0] = addr;
5777
5778 for (unsigned i = 0; i < src_comps; i++)
5779 sources[1 + i] = offset(src, bld, i);
5780
5781 payload = bld.vgrf(BRW_REGISTER_TYPE_UD, dwords);
5782 bld.LOAD_PAYLOAD(payload, sources, 1 + src_comps, 0);
5783 }
5784
5785 uint32_t desc;
5786 switch (inst->opcode) {
5787 case SHADER_OPCODE_A64_UNTYPED_READ_LOGICAL:
5788 desc = brw_dp_a64_untyped_surface_rw_desc(devinfo, inst->exec_size,
5789 arg, /* num_channels */
5790 false /* write */);
5791 break;
5792
5793 case SHADER_OPCODE_A64_UNTYPED_WRITE_LOGICAL:
5794 desc = brw_dp_a64_untyped_surface_rw_desc(devinfo, inst->exec_size,
5795 arg, /* num_channels */
5796 true /* write */);
5797 break;
5798
5799 case SHADER_OPCODE_A64_BYTE_SCATTERED_READ_LOGICAL:
5800 desc = brw_dp_a64_byte_scattered_rw_desc(devinfo, inst->exec_size,
5801 arg, /* bit_size */
5802 false /* write */);
5803 break;
5804
5805 case SHADER_OPCODE_A64_BYTE_SCATTERED_WRITE_LOGICAL:
5806 desc = brw_dp_a64_byte_scattered_rw_desc(devinfo, inst->exec_size,
5807 arg, /* bit_size */
5808 true /* write */);
5809 break;
5810
5811 case SHADER_OPCODE_A64_UNTYPED_ATOMIC_LOGICAL:
5812 desc = brw_dp_a64_untyped_atomic_desc(devinfo, inst->exec_size, 32,
5813 arg, /* atomic_op */
5814 !inst->dst.is_null());
5815 break;
5816
5817 case SHADER_OPCODE_A64_UNTYPED_ATOMIC_INT64_LOGICAL:
5818 desc = brw_dp_a64_untyped_atomic_desc(devinfo, inst->exec_size, 64,
5819 arg, /* atomic_op */
5820 !inst->dst.is_null());
5821 break;
5822
5823
5824 case SHADER_OPCODE_A64_UNTYPED_ATOMIC_FLOAT_LOGICAL:
5825 desc = brw_dp_a64_untyped_atomic_float_desc(devinfo, inst->exec_size,
5826 arg, /* atomic_op */
5827 !inst->dst.is_null());
5828 break;
5829
5830 default:
5831 unreachable("Unknown A64 logical instruction");
5832 }
5833
5834 /* Update the original instruction. */
5835 inst->opcode = SHADER_OPCODE_SEND;
5836 inst->mlen = mlen;
5837 inst->ex_mlen = ex_mlen;
5838 inst->header_size = 0;
5839 inst->send_has_side_effects = has_side_effects;
5840 inst->send_is_volatile = !has_side_effects;
5841
5842 /* Set up SFID and descriptors */
5843 inst->sfid = HSW_SFID_DATAPORT_DATA_CACHE_1;
5844 inst->desc = desc;
5845 inst->resize_sources(4);
5846 inst->src[0] = brw_imm_ud(0); /* desc */
5847 inst->src[1] = brw_imm_ud(0); /* ex_desc */
5848 inst->src[2] = payload;
5849 inst->src[3] = payload2;
5850 }
5851
5852 static void
5853 lower_varying_pull_constant_logical_send(const fs_builder &bld, fs_inst *inst)
5854 {
5855 const gen_device_info *devinfo = bld.shader->devinfo;
5856
5857 if (devinfo->gen >= 7) {
5858 fs_reg index = inst->src[0];
5859 /* We are switching the instruction from an ALU-like instruction to a
5860 * send-from-grf instruction. Since sends can't handle strides or
5861 * source modifiers, we have to make a copy of the offset source.
5862 */
5863 fs_reg offset = bld.vgrf(BRW_REGISTER_TYPE_UD);
5864 bld.MOV(offset, inst->src[1]);
5865
5866 const unsigned simd_mode =
5867 inst->exec_size <= 8 ? BRW_SAMPLER_SIMD_MODE_SIMD8 :
5868 BRW_SAMPLER_SIMD_MODE_SIMD16;
5869
5870 inst->opcode = SHADER_OPCODE_SEND;
5871 inst->mlen = inst->exec_size / 8;
5872 inst->resize_sources(3);
5873
5874 inst->sfid = BRW_SFID_SAMPLER;
5875 inst->desc = brw_sampler_desc(devinfo, 0, 0,
5876 GEN5_SAMPLER_MESSAGE_SAMPLE_LD,
5877 simd_mode, 0);
5878 if (index.file == IMM) {
5879 inst->desc |= index.ud & 0xff;
5880 inst->src[0] = brw_imm_ud(0);
5881 } else {
5882 const fs_builder ubld = bld.exec_all().group(1, 0);
5883 fs_reg tmp = ubld.vgrf(BRW_REGISTER_TYPE_UD);
5884 ubld.AND(tmp, index, brw_imm_ud(0xff));
5885 inst->src[0] = component(tmp, 0);
5886 }
5887 inst->src[1] = brw_imm_ud(0); /* ex_desc */
5888 inst->src[2] = offset; /* payload */
5889 } else {
5890 const fs_reg payload(MRF, FIRST_PULL_LOAD_MRF(devinfo->gen),
5891 BRW_REGISTER_TYPE_UD);
5892
5893 bld.MOV(byte_offset(payload, REG_SIZE), inst->src[1]);
5894
5895 inst->opcode = FS_OPCODE_VARYING_PULL_CONSTANT_LOAD_GEN4;
5896 inst->resize_sources(1);
5897 inst->base_mrf = payload.nr;
5898 inst->header_size = 1;
5899 inst->mlen = 1 + inst->exec_size / 8;
5900 }
5901 }
5902
5903 static void
5904 lower_math_logical_send(const fs_builder &bld, fs_inst *inst)
5905 {
5906 assert(bld.shader->devinfo->gen < 6);
5907
5908 inst->base_mrf = 2;
5909 inst->mlen = inst->sources * inst->exec_size / 8;
5910
5911 if (inst->sources > 1) {
5912 /* From the Ironlake PRM, Volume 4, Part 1, Section 6.1.13
5913 * "Message Payload":
5914 *
5915 * "Operand0[7]. For the INT DIV functions, this operand is the
5916 * denominator."
5917 * ...
5918 * "Operand1[7]. For the INT DIV functions, this operand is the
5919 * numerator."
5920 */
5921 const bool is_int_div = inst->opcode != SHADER_OPCODE_POW;
5922 const fs_reg src0 = is_int_div ? inst->src[1] : inst->src[0];
5923 const fs_reg src1 = is_int_div ? inst->src[0] : inst->src[1];
5924
5925 inst->resize_sources(1);
5926 inst->src[0] = src0;
5927
5928 assert(inst->exec_size == 8);
5929 bld.MOV(fs_reg(MRF, inst->base_mrf + 1, src1.type), src1);
5930 }
5931 }
5932
5933 bool
5934 fs_visitor::lower_logical_sends()
5935 {
5936 bool progress = false;
5937
5938 foreach_block_and_inst_safe(block, fs_inst, inst, cfg) {
5939 const fs_builder ibld(this, block, inst);
5940
5941 switch (inst->opcode) {
5942 case FS_OPCODE_FB_WRITE_LOGICAL:
5943 assert(stage == MESA_SHADER_FRAGMENT);
5944 lower_fb_write_logical_send(ibld, inst,
5945 brw_wm_prog_data(prog_data),
5946 (const brw_wm_prog_key *)key,
5947 payload);
5948 break;
5949
5950 case FS_OPCODE_FB_READ_LOGICAL:
5951 lower_fb_read_logical_send(ibld, inst);
5952 break;
5953
5954 case SHADER_OPCODE_TEX_LOGICAL:
5955 lower_sampler_logical_send(ibld, inst, SHADER_OPCODE_TEX);
5956 break;
5957
5958 case SHADER_OPCODE_TXD_LOGICAL:
5959 lower_sampler_logical_send(ibld, inst, SHADER_OPCODE_TXD);
5960 break;
5961
5962 case SHADER_OPCODE_TXF_LOGICAL:
5963 lower_sampler_logical_send(ibld, inst, SHADER_OPCODE_TXF);
5964 break;
5965
5966 case SHADER_OPCODE_TXL_LOGICAL:
5967 lower_sampler_logical_send(ibld, inst, SHADER_OPCODE_TXL);
5968 break;
5969
5970 case SHADER_OPCODE_TXS_LOGICAL:
5971 lower_sampler_logical_send(ibld, inst, SHADER_OPCODE_TXS);
5972 break;
5973
5974 case SHADER_OPCODE_IMAGE_SIZE_LOGICAL:
5975 lower_sampler_logical_send(ibld, inst,
5976 SHADER_OPCODE_IMAGE_SIZE_LOGICAL);
5977 break;
5978
5979 case FS_OPCODE_TXB_LOGICAL:
5980 lower_sampler_logical_send(ibld, inst, FS_OPCODE_TXB);
5981 break;
5982
5983 case SHADER_OPCODE_TXF_CMS_LOGICAL:
5984 lower_sampler_logical_send(ibld, inst, SHADER_OPCODE_TXF_CMS);
5985 break;
5986
5987 case SHADER_OPCODE_TXF_CMS_W_LOGICAL:
5988 lower_sampler_logical_send(ibld, inst, SHADER_OPCODE_TXF_CMS_W);
5989 break;
5990
5991 case SHADER_OPCODE_TXF_UMS_LOGICAL:
5992 lower_sampler_logical_send(ibld, inst, SHADER_OPCODE_TXF_UMS);
5993 break;
5994
5995 case SHADER_OPCODE_TXF_MCS_LOGICAL:
5996 lower_sampler_logical_send(ibld, inst, SHADER_OPCODE_TXF_MCS);
5997 break;
5998
5999 case SHADER_OPCODE_LOD_LOGICAL:
6000 lower_sampler_logical_send(ibld, inst, SHADER_OPCODE_LOD);
6001 break;
6002
6003 case SHADER_OPCODE_TG4_LOGICAL:
6004 lower_sampler_logical_send(ibld, inst, SHADER_OPCODE_TG4);
6005 break;
6006
6007 case SHADER_OPCODE_TG4_OFFSET_LOGICAL:
6008 lower_sampler_logical_send(ibld, inst, SHADER_OPCODE_TG4_OFFSET);
6009 break;
6010
6011 case SHADER_OPCODE_SAMPLEINFO_LOGICAL:
6012 lower_sampler_logical_send(ibld, inst, SHADER_OPCODE_SAMPLEINFO);
6013 break;
6014
6015 case SHADER_OPCODE_UNTYPED_SURFACE_READ_LOGICAL:
6016 case SHADER_OPCODE_UNTYPED_SURFACE_WRITE_LOGICAL:
6017 case SHADER_OPCODE_BYTE_SCATTERED_READ_LOGICAL:
6018 case SHADER_OPCODE_BYTE_SCATTERED_WRITE_LOGICAL:
6019 case SHADER_OPCODE_DWORD_SCATTERED_READ_LOGICAL:
6020 case SHADER_OPCODE_DWORD_SCATTERED_WRITE_LOGICAL:
6021 case SHADER_OPCODE_UNTYPED_ATOMIC_LOGICAL:
6022 case SHADER_OPCODE_UNTYPED_ATOMIC_FLOAT_LOGICAL:
6023 case SHADER_OPCODE_TYPED_SURFACE_READ_LOGICAL:
6024 case SHADER_OPCODE_TYPED_SURFACE_WRITE_LOGICAL:
6025 case SHADER_OPCODE_TYPED_ATOMIC_LOGICAL:
6026 lower_surface_logical_send(ibld, inst);
6027 break;
6028
6029 case SHADER_OPCODE_A64_UNTYPED_WRITE_LOGICAL:
6030 case SHADER_OPCODE_A64_UNTYPED_READ_LOGICAL:
6031 case SHADER_OPCODE_A64_BYTE_SCATTERED_WRITE_LOGICAL:
6032 case SHADER_OPCODE_A64_BYTE_SCATTERED_READ_LOGICAL:
6033 case SHADER_OPCODE_A64_UNTYPED_ATOMIC_LOGICAL:
6034 case SHADER_OPCODE_A64_UNTYPED_ATOMIC_INT64_LOGICAL:
6035 case SHADER_OPCODE_A64_UNTYPED_ATOMIC_FLOAT_LOGICAL:
6036 lower_a64_logical_send(ibld, inst);
6037 break;
6038
6039 case FS_OPCODE_VARYING_PULL_CONSTANT_LOAD_LOGICAL:
6040 lower_varying_pull_constant_logical_send(ibld, inst);
6041 break;
6042
6043 case SHADER_OPCODE_RCP:
6044 case SHADER_OPCODE_RSQ:
6045 case SHADER_OPCODE_SQRT:
6046 case SHADER_OPCODE_EXP2:
6047 case SHADER_OPCODE_LOG2:
6048 case SHADER_OPCODE_SIN:
6049 case SHADER_OPCODE_COS:
6050 case SHADER_OPCODE_POW:
6051 case SHADER_OPCODE_INT_QUOTIENT:
6052 case SHADER_OPCODE_INT_REMAINDER:
6053 /* The math opcodes are overloaded for the send-like and
6054 * expression-like instructions which seems kind of icky. Gen6+ has
6055 * a native (but rather quirky) MATH instruction so we don't need to
6056 * do anything here. On Gen4-5 we'll have to lower the Gen6-like
6057 * logical instructions (which we can easily recognize because they
6058 * have mlen = 0) into send-like virtual instructions.
6059 */
6060 if (devinfo->gen < 6 && inst->mlen == 0) {
6061 lower_math_logical_send(ibld, inst);
6062 break;
6063
6064 } else {
6065 continue;
6066 }
6067
6068 default:
6069 continue;
6070 }
6071
6072 progress = true;
6073 }
6074
6075 if (progress)
6076 invalidate_analysis(DEPENDENCY_INSTRUCTIONS | DEPENDENCY_VARIABLES);
6077
6078 return progress;
6079 }
6080
6081 static bool
6082 is_mixed_float_with_fp32_dst(const fs_inst *inst)
6083 {
6084 /* This opcode sometimes uses :W type on the source even if the operand is
6085 * a :HF, because in gen7 there is no support for :HF, and thus it uses :W.
6086 */
6087 if (inst->opcode == BRW_OPCODE_F16TO32)
6088 return true;
6089
6090 if (inst->dst.type != BRW_REGISTER_TYPE_F)
6091 return false;
6092
6093 for (int i = 0; i < inst->sources; i++) {
6094 if (inst->src[i].type == BRW_REGISTER_TYPE_HF)
6095 return true;
6096 }
6097
6098 return false;
6099 }
6100
6101 static bool
6102 is_mixed_float_with_packed_fp16_dst(const fs_inst *inst)
6103 {
6104 /* This opcode sometimes uses :W type on the destination even if the
6105 * destination is a :HF, because in gen7 there is no support for :HF, and
6106 * thus it uses :W.
6107 */
6108 if (inst->opcode == BRW_OPCODE_F32TO16 &&
6109 inst->dst.stride == 1)
6110 return true;
6111
6112 if (inst->dst.type != BRW_REGISTER_TYPE_HF ||
6113 inst->dst.stride != 1)
6114 return false;
6115
6116 for (int i = 0; i < inst->sources; i++) {
6117 if (inst->src[i].type == BRW_REGISTER_TYPE_F)
6118 return true;
6119 }
6120
6121 return false;
6122 }
6123
6124 /**
6125 * Get the closest allowed SIMD width for instruction \p inst accounting for
6126 * some common regioning and execution control restrictions that apply to FPU
6127 * instructions. These restrictions don't necessarily have any relevance to
6128 * instructions not executed by the FPU pipeline like extended math, control
6129 * flow or send message instructions.
6130 *
6131 * For virtual opcodes it's really up to the instruction -- In some cases
6132 * (e.g. where a virtual instruction unrolls into a simple sequence of FPU
6133 * instructions) it may simplify virtual instruction lowering if we can
6134 * enforce FPU-like regioning restrictions already on the virtual instruction,
6135 * in other cases (e.g. virtual send-like instructions) this may be
6136 * excessively restrictive.
6137 */
6138 static unsigned
6139 get_fpu_lowered_simd_width(const struct gen_device_info *devinfo,
6140 const fs_inst *inst)
6141 {
6142 /* Maximum execution size representable in the instruction controls. */
6143 unsigned max_width = MIN2(32, inst->exec_size);
6144
6145 /* According to the PRMs:
6146 * "A. In Direct Addressing mode, a source cannot span more than 2
6147 * adjacent GRF registers.
6148 * B. A destination cannot span more than 2 adjacent GRF registers."
6149 *
6150 * Look for the source or destination with the largest register region
6151 * which is the one that is going to limit the overall execution size of
6152 * the instruction due to this rule.
6153 */
6154 unsigned reg_count = DIV_ROUND_UP(inst->size_written, REG_SIZE);
6155
6156 for (unsigned i = 0; i < inst->sources; i++)
6157 reg_count = MAX2(reg_count, DIV_ROUND_UP(inst->size_read(i), REG_SIZE));
6158
6159 /* Calculate the maximum execution size of the instruction based on the
6160 * factor by which it goes over the hardware limit of 2 GRFs.
6161 */
6162 if (reg_count > 2)
6163 max_width = MIN2(max_width, inst->exec_size / DIV_ROUND_UP(reg_count, 2));
6164
6165 /* According to the IVB PRMs:
6166 * "When destination spans two registers, the source MUST span two
6167 * registers. The exception to the above rule:
6168 *
6169 * - When source is scalar, the source registers are not incremented.
6170 * - When source is packed integer Word and destination is packed
6171 * integer DWord, the source register is not incremented but the
6172 * source sub register is incremented."
6173 *
6174 * The hardware specs from Gen4 to Gen7.5 mention similar regioning
6175 * restrictions. The code below intentionally doesn't check whether the
6176 * destination type is integer because empirically the hardware doesn't
6177 * seem to care what the actual type is as long as it's dword-aligned.
6178 */
6179 if (devinfo->gen < 8) {
6180 for (unsigned i = 0; i < inst->sources; i++) {
6181 /* IVB implements DF scalars as <0;2,1> regions. */
6182 const bool is_scalar_exception = is_uniform(inst->src[i]) &&
6183 (devinfo->is_haswell || type_sz(inst->src[i].type) != 8);
6184 const bool is_packed_word_exception =
6185 type_sz(inst->dst.type) == 4 && inst->dst.stride == 1 &&
6186 type_sz(inst->src[i].type) == 2 && inst->src[i].stride == 1;
6187
6188 /* We check size_read(i) against size_written instead of REG_SIZE
6189 * because we want to properly handle SIMD32. In SIMD32, you can end
6190 * up with writes to 4 registers and a source that reads 2 registers
6191 * and we may still need to lower all the way to SIMD8 in that case.
6192 */
6193 if (inst->size_written > REG_SIZE &&
6194 inst->size_read(i) != 0 &&
6195 inst->size_read(i) < inst->size_written &&
6196 !is_scalar_exception && !is_packed_word_exception) {
6197 const unsigned reg_count = DIV_ROUND_UP(inst->size_written, REG_SIZE);
6198 max_width = MIN2(max_width, inst->exec_size / reg_count);
6199 }
6200 }
6201 }
6202
6203 if (devinfo->gen < 6) {
6204 /* From the G45 PRM, Volume 4 Page 361:
6205 *
6206 * "Operand Alignment Rule: With the exceptions listed below, a
6207 * source/destination operand in general should be aligned to even
6208 * 256-bit physical register with a region size equal to two 256-bit
6209 * physical registers."
6210 *
6211 * Normally we enforce this by allocating virtual registers to the
6212 * even-aligned class. But we need to handle payload registers.
6213 */
6214 for (unsigned i = 0; i < inst->sources; i++) {
6215 if (inst->src[i].file == FIXED_GRF && (inst->src[i].nr & 1) &&
6216 inst->size_read(i) > REG_SIZE) {
6217 max_width = MIN2(max_width, 8);
6218 }
6219 }
6220 }
6221
6222 /* From the IVB PRMs:
6223 * "When an instruction is SIMD32, the low 16 bits of the execution mask
6224 * are applied for both halves of the SIMD32 instruction. If different
6225 * execution mask channels are required, split the instruction into two
6226 * SIMD16 instructions."
6227 *
6228 * There is similar text in the HSW PRMs. Gen4-6 don't even implement
6229 * 32-wide control flow support in hardware and will behave similarly.
6230 */
6231 if (devinfo->gen < 8 && !inst->force_writemask_all)
6232 max_width = MIN2(max_width, 16);
6233
6234 /* From the IVB PRMs (applies to HSW too):
6235 * "Instructions with condition modifiers must not use SIMD32."
6236 *
6237 * From the BDW PRMs (applies to later hardware too):
6238 * "Ternary instruction with condition modifiers must not use SIMD32."
6239 */
6240 if (inst->conditional_mod && (devinfo->gen < 8 || inst->is_3src(devinfo)))
6241 max_width = MIN2(max_width, 16);
6242
6243 /* From the IVB PRMs (applies to other devices that don't have the
6244 * gen_device_info::supports_simd16_3src flag set):
6245 * "In Align16 access mode, SIMD16 is not allowed for DW operations and
6246 * SIMD8 is not allowed for DF operations."
6247 */
6248 if (inst->is_3src(devinfo) && !devinfo->supports_simd16_3src)
6249 max_width = MIN2(max_width, inst->exec_size / reg_count);
6250
6251 /* Pre-Gen8 EUs are hardwired to use the QtrCtrl+1 (where QtrCtrl is
6252 * the 8-bit quarter of the execution mask signals specified in the
6253 * instruction control fields) for the second compressed half of any
6254 * single-precision instruction (for double-precision instructions
6255 * it's hardwired to use NibCtrl+1, at least on HSW), which means that
6256 * the EU will apply the wrong execution controls for the second
6257 * sequential GRF write if the number of channels per GRF is not exactly
6258 * eight in single-precision mode (or four in double-float mode).
6259 *
6260 * In this situation we calculate the maximum size of the split
6261 * instructions so they only ever write to a single register.
6262 */
6263 if (devinfo->gen < 8 && inst->size_written > REG_SIZE &&
6264 !inst->force_writemask_all) {
6265 const unsigned channels_per_grf = inst->exec_size /
6266 DIV_ROUND_UP(inst->size_written, REG_SIZE);
6267 const unsigned exec_type_size = get_exec_type_size(inst);
6268 assert(exec_type_size);
6269
6270 /* The hardware shifts exactly 8 channels per compressed half of the
6271 * instruction in single-precision mode and exactly 4 in double-precision.
6272 */
6273 if (channels_per_grf != (exec_type_size == 8 ? 4 : 8))
6274 max_width = MIN2(max_width, channels_per_grf);
6275
6276 /* Lower all non-force_writemask_all DF instructions to SIMD4 on IVB/BYT
6277 * because HW applies the same channel enable signals to both halves of
6278 * the compressed instruction which will be just wrong under
6279 * non-uniform control flow.
6280 */
6281 if (devinfo->gen == 7 && !devinfo->is_haswell &&
6282 (exec_type_size == 8 || type_sz(inst->dst.type) == 8))
6283 max_width = MIN2(max_width, 4);
6284 }
6285
6286 /* From the SKL PRM, Special Restrictions for Handling Mixed Mode
6287 * Float Operations:
6288 *
6289 * "No SIMD16 in mixed mode when destination is f32. Instruction
6290 * execution size must be no more than 8."
6291 *
6292 * FIXME: the simulator doesn't seem to complain if we don't do this and
6293 * empirical testing with existing CTS tests show that they pass just fine
6294 * without implementing this, however, since our interpretation of the PRM
6295 * is that conversion MOVs between HF and F are still mixed-float
6296 * instructions (and therefore subject to this restriction) we decided to
6297 * split them to be safe. Might be useful to do additional investigation to
6298 * lift the restriction if we can ensure that it is safe though, since these
6299 * conversions are common when half-float types are involved since many
6300 * instructions do not support HF types and conversions from/to F are
6301 * required.
6302 */
6303 if (is_mixed_float_with_fp32_dst(inst))
6304 max_width = MIN2(max_width, 8);
6305
6306 /* From the SKL PRM, Special Restrictions for Handling Mixed Mode
6307 * Float Operations:
6308 *
6309 * "No SIMD16 in mixed mode when destination is packed f16 for both
6310 * Align1 and Align16."
6311 */
6312 if (is_mixed_float_with_packed_fp16_dst(inst))
6313 max_width = MIN2(max_width, 8);
6314
6315 /* Only power-of-two execution sizes are representable in the instruction
6316 * control fields.
6317 */
6318 return 1 << util_logbase2(max_width);
6319 }
6320
6321 /**
6322 * Get the maximum allowed SIMD width for instruction \p inst accounting for
6323 * various payload size restrictions that apply to sampler message
6324 * instructions.
6325 *
6326 * This is only intended to provide a maximum theoretical bound for the
6327 * execution size of the message based on the number of argument components
6328 * alone, which in most cases will determine whether the SIMD8 or SIMD16
6329 * variant of the message can be used, though some messages may have
6330 * additional restrictions not accounted for here (e.g. pre-ILK hardware uses
6331 * the message length to determine the exact SIMD width and argument count,
6332 * which makes a number of sampler message combinations impossible to
6333 * represent).
6334 */
6335 static unsigned
6336 get_sampler_lowered_simd_width(const struct gen_device_info *devinfo,
6337 const fs_inst *inst)
6338 {
6339 /* If we have a min_lod parameter on anything other than a simple sample
6340 * message, it will push it over 5 arguments and we have to fall back to
6341 * SIMD8.
6342 */
6343 if (inst->opcode != SHADER_OPCODE_TEX &&
6344 inst->components_read(TEX_LOGICAL_SRC_MIN_LOD))
6345 return 8;
6346
6347 /* Calculate the number of coordinate components that have to be present
6348 * assuming that additional arguments follow the texel coordinates in the
6349 * message payload. On IVB+ there is no need for padding, on ILK-SNB we
6350 * need to pad to four or three components depending on the message,
6351 * pre-ILK we need to pad to at most three components.
6352 */
6353 const unsigned req_coord_components =
6354 (devinfo->gen >= 7 ||
6355 !inst->components_read(TEX_LOGICAL_SRC_COORDINATE)) ? 0 :
6356 (devinfo->gen >= 5 && inst->opcode != SHADER_OPCODE_TXF_LOGICAL &&
6357 inst->opcode != SHADER_OPCODE_TXF_CMS_LOGICAL) ? 4 :
6358 3;
6359
6360 /* On Gen9+ the LOD argument is for free if we're able to use the LZ
6361 * variant of the TXL or TXF message.
6362 */
6363 const bool implicit_lod = devinfo->gen >= 9 &&
6364 (inst->opcode == SHADER_OPCODE_TXL ||
6365 inst->opcode == SHADER_OPCODE_TXF) &&
6366 inst->src[TEX_LOGICAL_SRC_LOD].is_zero();
6367
6368 /* Calculate the total number of argument components that need to be passed
6369 * to the sampler unit.
6370 */
6371 const unsigned num_payload_components =
6372 MAX2(inst->components_read(TEX_LOGICAL_SRC_COORDINATE),
6373 req_coord_components) +
6374 inst->components_read(TEX_LOGICAL_SRC_SHADOW_C) +
6375 (implicit_lod ? 0 : inst->components_read(TEX_LOGICAL_SRC_LOD)) +
6376 inst->components_read(TEX_LOGICAL_SRC_LOD2) +
6377 inst->components_read(TEX_LOGICAL_SRC_SAMPLE_INDEX) +
6378 (inst->opcode == SHADER_OPCODE_TG4_OFFSET_LOGICAL ?
6379 inst->components_read(TEX_LOGICAL_SRC_TG4_OFFSET) : 0) +
6380 inst->components_read(TEX_LOGICAL_SRC_MCS);
6381
6382 /* SIMD16 messages with more than five arguments exceed the maximum message
6383 * size supported by the sampler, regardless of whether a header is
6384 * provided or not.
6385 */
6386 return MIN2(inst->exec_size,
6387 num_payload_components > MAX_SAMPLER_MESSAGE_SIZE / 2 ? 8 : 16);
6388 }
6389
6390 /**
6391 * Get the closest native SIMD width supported by the hardware for instruction
6392 * \p inst. The instruction will be left untouched by
6393 * fs_visitor::lower_simd_width() if the returned value is equal to the
6394 * original execution size.
6395 */
6396 static unsigned
6397 get_lowered_simd_width(const struct gen_device_info *devinfo,
6398 const fs_inst *inst)
6399 {
6400 switch (inst->opcode) {
6401 case BRW_OPCODE_MOV:
6402 case BRW_OPCODE_SEL:
6403 case BRW_OPCODE_NOT:
6404 case BRW_OPCODE_AND:
6405 case BRW_OPCODE_OR:
6406 case BRW_OPCODE_XOR:
6407 case BRW_OPCODE_SHR:
6408 case BRW_OPCODE_SHL:
6409 case BRW_OPCODE_ASR:
6410 case BRW_OPCODE_ROR:
6411 case BRW_OPCODE_ROL:
6412 case BRW_OPCODE_CMPN:
6413 case BRW_OPCODE_CSEL:
6414 case BRW_OPCODE_F32TO16:
6415 case BRW_OPCODE_F16TO32:
6416 case BRW_OPCODE_BFREV:
6417 case BRW_OPCODE_BFE:
6418 case BRW_OPCODE_ADD:
6419 case BRW_OPCODE_MUL:
6420 case BRW_OPCODE_AVG:
6421 case BRW_OPCODE_FRC:
6422 case BRW_OPCODE_RNDU:
6423 case BRW_OPCODE_RNDD:
6424 case BRW_OPCODE_RNDE:
6425 case BRW_OPCODE_RNDZ:
6426 case BRW_OPCODE_LZD:
6427 case BRW_OPCODE_FBH:
6428 case BRW_OPCODE_FBL:
6429 case BRW_OPCODE_CBIT:
6430 case BRW_OPCODE_SAD2:
6431 case BRW_OPCODE_MAD:
6432 case BRW_OPCODE_LRP:
6433 case FS_OPCODE_PACK:
6434 case SHADER_OPCODE_SEL_EXEC:
6435 case SHADER_OPCODE_CLUSTER_BROADCAST:
6436 return get_fpu_lowered_simd_width(devinfo, inst);
6437
6438 case BRW_OPCODE_CMP: {
6439 /* The Ivybridge/BayTrail WaCMPInstFlagDepClearedEarly workaround says that
6440 * when the destination is a GRF the dependency-clear bit on the flag
6441 * register is cleared early.
6442 *
6443 * Suggested workarounds are to disable coissuing CMP instructions
6444 * or to split CMP(16) instructions into two CMP(8) instructions.
6445 *
6446 * We choose to split into CMP(8) instructions since disabling
6447 * coissuing would affect CMP instructions not otherwise affected by
6448 * the errata.
6449 */
6450 const unsigned max_width = (devinfo->gen == 7 && !devinfo->is_haswell &&
6451 !inst->dst.is_null() ? 8 : ~0);
6452 return MIN2(max_width, get_fpu_lowered_simd_width(devinfo, inst));
6453 }
6454 case BRW_OPCODE_BFI1:
6455 case BRW_OPCODE_BFI2:
6456 /* The Haswell WaForceSIMD8ForBFIInstruction workaround says that we
6457 * should
6458 * "Force BFI instructions to be executed always in SIMD8."
6459 */
6460 return MIN2(devinfo->is_haswell ? 8 : ~0u,
6461 get_fpu_lowered_simd_width(devinfo, inst));
6462
6463 case BRW_OPCODE_IF:
6464 assert(inst->src[0].file == BAD_FILE || inst->exec_size <= 16);
6465 return inst->exec_size;
6466
6467 case SHADER_OPCODE_RCP:
6468 case SHADER_OPCODE_RSQ:
6469 case SHADER_OPCODE_SQRT:
6470 case SHADER_OPCODE_EXP2:
6471 case SHADER_OPCODE_LOG2:
6472 case SHADER_OPCODE_SIN:
6473 case SHADER_OPCODE_COS: {
6474 /* Unary extended math instructions are limited to SIMD8 on Gen4 and
6475 * Gen6. Extended Math Function is limited to SIMD8 with half-float.
6476 */
6477 if (devinfo->gen == 6 || (devinfo->gen == 4 && !devinfo->is_g4x))
6478 return MIN2(8, inst->exec_size);
6479 if (inst->dst.type == BRW_REGISTER_TYPE_HF)
6480 return MIN2(8, inst->exec_size);
6481 return MIN2(16, inst->exec_size);
6482 }
6483
6484 case SHADER_OPCODE_POW: {
6485 /* SIMD16 is only allowed on Gen7+. Extended Math Function is limited
6486 * to SIMD8 with half-float
6487 */
6488 if (devinfo->gen < 7)
6489 return MIN2(8, inst->exec_size);
6490 if (inst->dst.type == BRW_REGISTER_TYPE_HF)
6491 return MIN2(8, inst->exec_size);
6492 return MIN2(16, inst->exec_size);
6493 }
6494
6495 case SHADER_OPCODE_USUB_SAT:
6496 case SHADER_OPCODE_ISUB_SAT:
6497 return get_fpu_lowered_simd_width(devinfo, inst);
6498
6499 case SHADER_OPCODE_INT_QUOTIENT:
6500 case SHADER_OPCODE_INT_REMAINDER:
6501 /* Integer division is limited to SIMD8 on all generations. */
6502 return MIN2(8, inst->exec_size);
6503
6504 case FS_OPCODE_LINTERP:
6505 case SHADER_OPCODE_GET_BUFFER_SIZE:
6506 case FS_OPCODE_UNIFORM_PULL_CONSTANT_LOAD:
6507 case FS_OPCODE_PACK_HALF_2x16_SPLIT:
6508 case FS_OPCODE_INTERPOLATE_AT_SAMPLE:
6509 case FS_OPCODE_INTERPOLATE_AT_SHARED_OFFSET:
6510 case FS_OPCODE_INTERPOLATE_AT_PER_SLOT_OFFSET:
6511 return MIN2(16, inst->exec_size);
6512
6513 case FS_OPCODE_VARYING_PULL_CONSTANT_LOAD_LOGICAL:
6514 /* Pre-ILK hardware doesn't have a SIMD8 variant of the texel fetch
6515 * message used to implement varying pull constant loads, so expand it
6516 * to SIMD16. An alternative with longer message payload length but
6517 * shorter return payload would be to use the SIMD8 sampler message that
6518 * takes (header, u, v, r) as parameters instead of (header, u).
6519 */
6520 return (devinfo->gen == 4 ? 16 : MIN2(16, inst->exec_size));
6521
6522 case FS_OPCODE_DDX_COARSE:
6523 case FS_OPCODE_DDX_FINE:
6524 case FS_OPCODE_DDY_COARSE:
6525 case FS_OPCODE_DDY_FINE:
6526 /* The implementation of this virtual opcode may require emitting
6527 * compressed Align16 instructions, which are severely limited on some
6528 * generations.
6529 *
6530 * From the Ivy Bridge PRM, volume 4 part 3, section 3.3.9 (Register
6531 * Region Restrictions):
6532 *
6533 * "In Align16 access mode, SIMD16 is not allowed for DW operations
6534 * and SIMD8 is not allowed for DF operations."
6535 *
6536 * In this context, "DW operations" means "operations acting on 32-bit
6537 * values", so it includes operations on floats.
6538 *
6539 * Gen4 has a similar restriction. From the i965 PRM, section 11.5.3
6540 * (Instruction Compression -> Rules and Restrictions):
6541 *
6542 * "A compressed instruction must be in Align1 access mode. Align16
6543 * mode instructions cannot be compressed."
6544 *
6545 * Similar text exists in the g45 PRM.
6546 *
6547 * Empirically, compressed align16 instructions using odd register
6548 * numbers don't appear to work on Sandybridge either.
6549 */
6550 return (devinfo->gen == 4 || devinfo->gen == 6 ||
6551 (devinfo->gen == 7 && !devinfo->is_haswell) ?
6552 MIN2(8, inst->exec_size) : MIN2(16, inst->exec_size));
6553
6554 case SHADER_OPCODE_MULH:
6555 /* MULH is lowered to the MUL/MACH sequence using the accumulator, which
6556 * is 8-wide on Gen7+.
6557 */
6558 return (devinfo->gen >= 7 ? 8 :
6559 get_fpu_lowered_simd_width(devinfo, inst));
6560
6561 case FS_OPCODE_FB_WRITE_LOGICAL:
6562 /* Gen6 doesn't support SIMD16 depth writes but we cannot handle them
6563 * here.
6564 */
6565 assert(devinfo->gen != 6 ||
6566 inst->src[FB_WRITE_LOGICAL_SRC_SRC_DEPTH].file == BAD_FILE ||
6567 inst->exec_size == 8);
6568 /* Dual-source FB writes are unsupported in SIMD16 mode. */
6569 return (inst->src[FB_WRITE_LOGICAL_SRC_COLOR1].file != BAD_FILE ?
6570 8 : MIN2(16, inst->exec_size));
6571
6572 case FS_OPCODE_FB_READ_LOGICAL:
6573 return MIN2(16, inst->exec_size);
6574
6575 case SHADER_OPCODE_TEX_LOGICAL:
6576 case SHADER_OPCODE_TXF_CMS_LOGICAL:
6577 case SHADER_OPCODE_TXF_UMS_LOGICAL:
6578 case SHADER_OPCODE_TXF_MCS_LOGICAL:
6579 case SHADER_OPCODE_LOD_LOGICAL:
6580 case SHADER_OPCODE_TG4_LOGICAL:
6581 case SHADER_OPCODE_SAMPLEINFO_LOGICAL:
6582 case SHADER_OPCODE_TXF_CMS_W_LOGICAL:
6583 case SHADER_OPCODE_TG4_OFFSET_LOGICAL:
6584 return get_sampler_lowered_simd_width(devinfo, inst);
6585
6586 case SHADER_OPCODE_TXD_LOGICAL:
6587 /* TXD is unsupported in SIMD16 mode. */
6588 return 8;
6589
6590 case SHADER_OPCODE_TXL_LOGICAL:
6591 case FS_OPCODE_TXB_LOGICAL:
6592 /* Only one execution size is representable pre-ILK depending on whether
6593 * the shadow reference argument is present.
6594 */
6595 if (devinfo->gen == 4)
6596 return inst->src[TEX_LOGICAL_SRC_SHADOW_C].file == BAD_FILE ? 16 : 8;
6597 else
6598 return get_sampler_lowered_simd_width(devinfo, inst);
6599
6600 case SHADER_OPCODE_TXF_LOGICAL:
6601 case SHADER_OPCODE_TXS_LOGICAL:
6602 /* Gen4 doesn't have SIMD8 variants for the RESINFO and LD-with-LOD
6603 * messages. Use SIMD16 instead.
6604 */
6605 if (devinfo->gen == 4)
6606 return 16;
6607 else
6608 return get_sampler_lowered_simd_width(devinfo, inst);
6609
6610 case SHADER_OPCODE_TYPED_ATOMIC_LOGICAL:
6611 case SHADER_OPCODE_TYPED_SURFACE_READ_LOGICAL:
6612 case SHADER_OPCODE_TYPED_SURFACE_WRITE_LOGICAL:
6613 return 8;
6614
6615 case SHADER_OPCODE_UNTYPED_ATOMIC_LOGICAL:
6616 case SHADER_OPCODE_UNTYPED_ATOMIC_FLOAT_LOGICAL:
6617 case SHADER_OPCODE_UNTYPED_SURFACE_READ_LOGICAL:
6618 case SHADER_OPCODE_UNTYPED_SURFACE_WRITE_LOGICAL:
6619 case SHADER_OPCODE_BYTE_SCATTERED_WRITE_LOGICAL:
6620 case SHADER_OPCODE_BYTE_SCATTERED_READ_LOGICAL:
6621 case SHADER_OPCODE_DWORD_SCATTERED_WRITE_LOGICAL:
6622 case SHADER_OPCODE_DWORD_SCATTERED_READ_LOGICAL:
6623 return MIN2(16, inst->exec_size);
6624
6625 case SHADER_OPCODE_A64_UNTYPED_WRITE_LOGICAL:
6626 case SHADER_OPCODE_A64_UNTYPED_READ_LOGICAL:
6627 case SHADER_OPCODE_A64_BYTE_SCATTERED_WRITE_LOGICAL:
6628 case SHADER_OPCODE_A64_BYTE_SCATTERED_READ_LOGICAL:
6629 return devinfo->gen <= 8 ? 8 : MIN2(16, inst->exec_size);
6630
6631 case SHADER_OPCODE_A64_UNTYPED_ATOMIC_LOGICAL:
6632 case SHADER_OPCODE_A64_UNTYPED_ATOMIC_INT64_LOGICAL:
6633 case SHADER_OPCODE_A64_UNTYPED_ATOMIC_FLOAT_LOGICAL:
6634 return 8;
6635
6636 case SHADER_OPCODE_URB_READ_SIMD8:
6637 case SHADER_OPCODE_URB_READ_SIMD8_PER_SLOT:
6638 case SHADER_OPCODE_URB_WRITE_SIMD8:
6639 case SHADER_OPCODE_URB_WRITE_SIMD8_PER_SLOT:
6640 case SHADER_OPCODE_URB_WRITE_SIMD8_MASKED:
6641 case SHADER_OPCODE_URB_WRITE_SIMD8_MASKED_PER_SLOT:
6642 return MIN2(8, inst->exec_size);
6643
6644 case SHADER_OPCODE_QUAD_SWIZZLE: {
6645 const unsigned swiz = inst->src[1].ud;
6646 return (is_uniform(inst->src[0]) ?
6647 get_fpu_lowered_simd_width(devinfo, inst) :
6648 devinfo->gen < 11 && type_sz(inst->src[0].type) == 4 ? 8 :
6649 swiz == BRW_SWIZZLE_XYXY || swiz == BRW_SWIZZLE_ZWZW ? 4 :
6650 get_fpu_lowered_simd_width(devinfo, inst));
6651 }
6652 case SHADER_OPCODE_MOV_INDIRECT: {
6653 /* From IVB and HSW PRMs:
6654 *
6655 * "2.When the destination requires two registers and the sources are
6656 * indirect, the sources must use 1x1 regioning mode.
6657 *
6658 * In case of DF instructions in HSW/IVB, the exec_size is limited by
6659 * the EU decompression logic not handling VxH indirect addressing
6660 * correctly.
6661 */
6662 const unsigned max_size = (devinfo->gen >= 8 ? 2 : 1) * REG_SIZE;
6663 /* Prior to Broadwell, we only have 8 address subregisters. */
6664 return MIN3(devinfo->gen >= 8 ? 16 : 8,
6665 max_size / (inst->dst.stride * type_sz(inst->dst.type)),
6666 inst->exec_size);
6667 }
6668
6669 case SHADER_OPCODE_LOAD_PAYLOAD: {
6670 const unsigned reg_count =
6671 DIV_ROUND_UP(inst->dst.component_size(inst->exec_size), REG_SIZE);
6672
6673 if (reg_count > 2) {
6674 /* Only LOAD_PAYLOAD instructions with per-channel destination region
6675 * can be easily lowered (which excludes headers and heterogeneous
6676 * types).
6677 */
6678 assert(!inst->header_size);
6679 for (unsigned i = 0; i < inst->sources; i++)
6680 assert(type_sz(inst->dst.type) == type_sz(inst->src[i].type) ||
6681 inst->src[i].file == BAD_FILE);
6682
6683 return inst->exec_size / DIV_ROUND_UP(reg_count, 2);
6684 } else {
6685 return inst->exec_size;
6686 }
6687 }
6688 default:
6689 return inst->exec_size;
6690 }
6691 }
6692
6693 /**
6694 * Return true if splitting out the group of channels of instruction \p inst
6695 * given by lbld.group() requires allocating a temporary for the i-th source
6696 * of the lowered instruction.
6697 */
6698 static inline bool
6699 needs_src_copy(const fs_builder &lbld, const fs_inst *inst, unsigned i)
6700 {
6701 return !(is_periodic(inst->src[i], lbld.dispatch_width()) ||
6702 (inst->components_read(i) == 1 &&
6703 lbld.dispatch_width() <= inst->exec_size)) ||
6704 (inst->flags_written() &
6705 flag_mask(inst->src[i], type_sz(inst->src[i].type)));
6706 }
6707
6708 /**
6709 * Extract the data that would be consumed by the channel group given by
6710 * lbld.group() from the i-th source region of instruction \p inst and return
6711 * it as result in packed form.
6712 */
6713 static fs_reg
6714 emit_unzip(const fs_builder &lbld, fs_inst *inst, unsigned i)
6715 {
6716 assert(lbld.group() >= inst->group);
6717
6718 /* Specified channel group from the source region. */
6719 const fs_reg src = horiz_offset(inst->src[i], lbld.group() - inst->group);
6720
6721 if (needs_src_copy(lbld, inst, i)) {
6722 /* Builder of the right width to perform the copy avoiding uninitialized
6723 * data if the lowered execution size is greater than the original
6724 * execution size of the instruction.
6725 */
6726 const fs_builder cbld = lbld.group(MIN2(lbld.dispatch_width(),
6727 inst->exec_size), 0);
6728 const fs_reg tmp = lbld.vgrf(inst->src[i].type, inst->components_read(i));
6729
6730 for (unsigned k = 0; k < inst->components_read(i); ++k)
6731 cbld.MOV(offset(tmp, lbld, k), offset(src, inst->exec_size, k));
6732
6733 return tmp;
6734
6735 } else if (is_periodic(inst->src[i], lbld.dispatch_width())) {
6736 /* The source is invariant for all dispatch_width-wide groups of the
6737 * original region.
6738 */
6739 return inst->src[i];
6740
6741 } else {
6742 /* We can just point the lowered instruction at the right channel group
6743 * from the original region.
6744 */
6745 return src;
6746 }
6747 }
6748
6749 /**
6750 * Return true if splitting out the group of channels of instruction \p inst
6751 * given by lbld.group() requires allocating a temporary for the destination
6752 * of the lowered instruction and copying the data back to the original
6753 * destination region.
6754 */
6755 static inline bool
6756 needs_dst_copy(const fs_builder &lbld, const fs_inst *inst)
6757 {
6758 /* If the instruction writes more than one component we'll have to shuffle
6759 * the results of multiple lowered instructions in order to make sure that
6760 * they end up arranged correctly in the original destination region.
6761 */
6762 if (inst->size_written > inst->dst.component_size(inst->exec_size))
6763 return true;
6764
6765 /* If the lowered execution size is larger than the original the result of
6766 * the instruction won't fit in the original destination, so we'll have to
6767 * allocate a temporary in any case.
6768 */
6769 if (lbld.dispatch_width() > inst->exec_size)
6770 return true;
6771
6772 for (unsigned i = 0; i < inst->sources; i++) {
6773 /* If we already made a copy of the source for other reasons there won't
6774 * be any overlap with the destination.
6775 */
6776 if (needs_src_copy(lbld, inst, i))
6777 continue;
6778
6779 /* In order to keep the logic simple we emit a copy whenever the
6780 * destination region doesn't exactly match an overlapping source, which
6781 * may point at the source and destination not being aligned group by
6782 * group which could cause one of the lowered instructions to overwrite
6783 * the data read from the same source by other lowered instructions.
6784 */
6785 if (regions_overlap(inst->dst, inst->size_written,
6786 inst->src[i], inst->size_read(i)) &&
6787 !inst->dst.equals(inst->src[i]))
6788 return true;
6789 }
6790
6791 return false;
6792 }
6793
6794 /**
6795 * Insert data from a packed temporary into the channel group given by
6796 * lbld.group() of the destination region of instruction \p inst and return
6797 * the temporary as result. Any copy instructions that are required for
6798 * unzipping the previous value (in the case of partial writes) will be
6799 * inserted using \p lbld_before and any copy instructions required for
6800 * zipping up the destination of \p inst will be inserted using \p lbld_after.
6801 */
6802 static fs_reg
6803 emit_zip(const fs_builder &lbld_before, const fs_builder &lbld_after,
6804 fs_inst *inst)
6805 {
6806 assert(lbld_before.dispatch_width() == lbld_after.dispatch_width());
6807 assert(lbld_before.group() == lbld_after.group());
6808 assert(lbld_after.group() >= inst->group);
6809
6810 /* Specified channel group from the destination region. */
6811 const fs_reg dst = horiz_offset(inst->dst, lbld_after.group() - inst->group);
6812 const unsigned dst_size = inst->size_written /
6813 inst->dst.component_size(inst->exec_size);
6814
6815 if (needs_dst_copy(lbld_after, inst)) {
6816 const fs_reg tmp = lbld_after.vgrf(inst->dst.type, dst_size);
6817
6818 if (inst->predicate) {
6819 /* Handle predication by copying the original contents of
6820 * the destination into the temporary before emitting the
6821 * lowered instruction.
6822 */
6823 const fs_builder gbld_before =
6824 lbld_before.group(MIN2(lbld_before.dispatch_width(),
6825 inst->exec_size), 0);
6826 for (unsigned k = 0; k < dst_size; ++k) {
6827 gbld_before.MOV(offset(tmp, lbld_before, k),
6828 offset(dst, inst->exec_size, k));
6829 }
6830 }
6831
6832 const fs_builder gbld_after =
6833 lbld_after.group(MIN2(lbld_after.dispatch_width(),
6834 inst->exec_size), 0);
6835 for (unsigned k = 0; k < dst_size; ++k) {
6836 /* Use a builder of the right width to perform the copy avoiding
6837 * uninitialized data if the lowered execution size is greater than
6838 * the original execution size of the instruction.
6839 */
6840 gbld_after.MOV(offset(dst, inst->exec_size, k),
6841 offset(tmp, lbld_after, k));
6842 }
6843
6844 return tmp;
6845
6846 } else {
6847 /* No need to allocate a temporary for the lowered instruction, just
6848 * take the right group of channels from the original region.
6849 */
6850 return dst;
6851 }
6852 }
6853
6854 bool
6855 fs_visitor::lower_simd_width()
6856 {
6857 bool progress = false;
6858
6859 foreach_block_and_inst_safe(block, fs_inst, inst, cfg) {
6860 const unsigned lower_width = get_lowered_simd_width(devinfo, inst);
6861
6862 if (lower_width != inst->exec_size) {
6863 /* Builder matching the original instruction. We may also need to
6864 * emit an instruction of width larger than the original, set the
6865 * execution size of the builder to the highest of both for now so
6866 * we're sure that both cases can be handled.
6867 */
6868 const unsigned max_width = MAX2(inst->exec_size, lower_width);
6869 const fs_builder ibld = bld.at(block, inst)
6870 .exec_all(inst->force_writemask_all)
6871 .group(max_width, inst->group / max_width);
6872
6873 /* Split the copies in chunks of the execution width of either the
6874 * original or the lowered instruction, whichever is lower.
6875 */
6876 const unsigned n = DIV_ROUND_UP(inst->exec_size, lower_width);
6877 const unsigned dst_size = inst->size_written /
6878 inst->dst.component_size(inst->exec_size);
6879
6880 assert(!inst->writes_accumulator && !inst->mlen);
6881
6882 /* Inserting the zip, unzip, and duplicated instructions in all of
6883 * the right spots is somewhat tricky. All of the unzip and any
6884 * instructions from the zip which unzip the destination prior to
6885 * writing need to happen before all of the per-group instructions
6886 * and the zip instructions need to happen after. In order to sort
6887 * this all out, we insert the unzip instructions before \p inst,
6888 * insert the per-group instructions after \p inst (i.e. before
6889 * inst->next), and insert the zip instructions before the
6890 * instruction after \p inst. Since we are inserting instructions
6891 * after \p inst, inst->next is a moving target and we need to save
6892 * it off here so that we insert the zip instructions in the right
6893 * place.
6894 *
6895 * Since we're inserting split instructions after after_inst, the
6896 * instructions will end up in the reverse order that we insert them.
6897 * However, certain render target writes require that the low group
6898 * instructions come before the high group. From the Ivy Bridge PRM
6899 * Vol. 4, Pt. 1, Section 3.9.11:
6900 *
6901 * "If multiple SIMD8 Dual Source messages are delivered by the
6902 * pixel shader thread, each SIMD8_DUALSRC_LO message must be
6903 * issued before the SIMD8_DUALSRC_HI message with the same Slot
6904 * Group Select setting."
6905 *
6906 * And, from Section 3.9.11.1 of the same PRM:
6907 *
6908 * "When SIMD32 or SIMD16 PS threads send render target writes
6909 * with multiple SIMD8 and SIMD16 messages, the following must
6910 * hold:
6911 *
6912 * All the slots (as described above) must have a corresponding
6913 * render target write irrespective of the slot's validity. A slot
6914 * is considered valid when at least one sample is enabled. For
6915 * example, a SIMD16 PS thread must send two SIMD8 render target
6916 * writes to cover all the slots.
6917 *
6918 * PS thread must send SIMD render target write messages with
6919 * increasing slot numbers. For example, SIMD16 thread has
6920 * Slot[15:0] and if two SIMD8 render target writes are used, the
6921 * first SIMD8 render target write must send Slot[7:0] and the
6922 * next one must send Slot[15:8]."
6923 *
6924 * In order to make low group instructions come before high group
6925 * instructions (this is required for some render target writes), we
6926 * split from the highest group to lowest.
6927 */
6928 exec_node *const after_inst = inst->next;
6929 for (int i = n - 1; i >= 0; i--) {
6930 /* Emit a copy of the original instruction with the lowered width.
6931 * If the EOT flag was set throw it away except for the last
6932 * instruction to avoid killing the thread prematurely.
6933 */
6934 fs_inst split_inst = *inst;
6935 split_inst.exec_size = lower_width;
6936 split_inst.eot = inst->eot && i == int(n - 1);
6937
6938 /* Select the correct channel enables for the i-th group, then
6939 * transform the sources and destination and emit the lowered
6940 * instruction.
6941 */
6942 const fs_builder lbld = ibld.group(lower_width, i);
6943
6944 for (unsigned j = 0; j < inst->sources; j++)
6945 split_inst.src[j] = emit_unzip(lbld.at(block, inst), inst, j);
6946
6947 split_inst.dst = emit_zip(lbld.at(block, inst),
6948 lbld.at(block, after_inst), inst);
6949 split_inst.size_written =
6950 split_inst.dst.component_size(lower_width) * dst_size;
6951
6952 lbld.at(block, inst->next).emit(split_inst);
6953 }
6954
6955 inst->remove(block);
6956 progress = true;
6957 }
6958 }
6959
6960 if (progress)
6961 invalidate_analysis(DEPENDENCY_INSTRUCTIONS | DEPENDENCY_VARIABLES);
6962
6963 return progress;
6964 }
6965
6966 /**
6967 * Transform barycentric vectors into the interleaved form expected by the PLN
6968 * instruction and returned by the Gen7+ PI shared function.
6969 *
6970 * For channels 0-15 in SIMD16 mode they are expected to be laid out as
6971 * follows in the register file:
6972 *
6973 * rN+0: X[0-7]
6974 * rN+1: Y[0-7]
6975 * rN+2: X[8-15]
6976 * rN+3: Y[8-15]
6977 *
6978 * There is no need to handle SIMD32 here -- This is expected to be run after
6979 * SIMD lowering, since SIMD lowering relies on vectors having the standard
6980 * component layout.
6981 */
6982 bool
6983 fs_visitor::lower_barycentrics()
6984 {
6985 const bool has_interleaved_layout = devinfo->has_pln || devinfo->gen >= 7;
6986 bool progress = false;
6987
6988 if (stage != MESA_SHADER_FRAGMENT || !has_interleaved_layout)
6989 return false;
6990
6991 foreach_block_and_inst_safe(block, fs_inst, inst, cfg) {
6992 if (inst->exec_size < 16)
6993 continue;
6994
6995 const fs_builder ibld(this, block, inst);
6996 const fs_builder ubld = ibld.exec_all().group(8, 0);
6997
6998 switch (inst->opcode) {
6999 case FS_OPCODE_LINTERP : {
7000 assert(inst->exec_size == 16);
7001 const fs_reg tmp = ibld.vgrf(inst->src[0].type, 2);
7002 fs_reg srcs[4];
7003
7004 for (unsigned i = 0; i < ARRAY_SIZE(srcs); i++)
7005 srcs[i] = horiz_offset(offset(inst->src[0], ibld, i % 2),
7006 8 * (i / 2));
7007
7008 ubld.LOAD_PAYLOAD(tmp, srcs, ARRAY_SIZE(srcs), ARRAY_SIZE(srcs));
7009
7010 inst->src[0] = tmp;
7011 progress = true;
7012 break;
7013 }
7014 case FS_OPCODE_INTERPOLATE_AT_SAMPLE:
7015 case FS_OPCODE_INTERPOLATE_AT_SHARED_OFFSET:
7016 case FS_OPCODE_INTERPOLATE_AT_PER_SLOT_OFFSET: {
7017 assert(inst->exec_size == 16);
7018 const fs_reg tmp = ibld.vgrf(inst->dst.type, 2);
7019
7020 for (unsigned i = 0; i < 2; i++) {
7021 for (unsigned g = 0; g < inst->exec_size / 8; g++) {
7022 fs_inst *mov = ibld.at(block, inst->next).group(8, g)
7023 .MOV(horiz_offset(offset(inst->dst, ibld, i),
7024 8 * g),
7025 offset(tmp, ubld, 2 * g + i));
7026 mov->predicate = inst->predicate;
7027 mov->predicate_inverse = inst->predicate_inverse;
7028 mov->flag_subreg = inst->flag_subreg;
7029 }
7030 }
7031
7032 inst->dst = tmp;
7033 progress = true;
7034 break;
7035 }
7036 default:
7037 break;
7038 }
7039 }
7040
7041 if (progress)
7042 invalidate_analysis(DEPENDENCY_INSTRUCTIONS | DEPENDENCY_VARIABLES);
7043
7044 return progress;
7045 }
7046
7047 void
7048 fs_visitor::dump_instructions() const
7049 {
7050 dump_instructions(NULL);
7051 }
7052
7053 void
7054 fs_visitor::dump_instructions(const char *name) const
7055 {
7056 FILE *file = stderr;
7057 if (name && geteuid() != 0) {
7058 file = fopen(name, "w");
7059 if (!file)
7060 file = stderr;
7061 }
7062
7063 if (cfg) {
7064 const register_pressure &rp = regpressure_analysis.require();
7065 unsigned ip = 0, max_pressure = 0;
7066 foreach_block_and_inst(block, backend_instruction, inst, cfg) {
7067 max_pressure = MAX2(max_pressure, rp.regs_live_at_ip[ip]);
7068 fprintf(file, "{%3d} %4d: ", rp.regs_live_at_ip[ip], ip);
7069 dump_instruction(inst, file);
7070 ip++;
7071 }
7072 fprintf(file, "Maximum %3d registers live at once.\n", max_pressure);
7073 } else {
7074 int ip = 0;
7075 foreach_in_list(backend_instruction, inst, &instructions) {
7076 fprintf(file, "%4d: ", ip++);
7077 dump_instruction(inst, file);
7078 }
7079 }
7080
7081 if (file != stderr) {
7082 fclose(file);
7083 }
7084 }
7085
7086 void
7087 fs_visitor::dump_instruction(const backend_instruction *be_inst) const
7088 {
7089 dump_instruction(be_inst, stderr);
7090 }
7091
7092 void
7093 fs_visitor::dump_instruction(const backend_instruction *be_inst, FILE *file) const
7094 {
7095 const fs_inst *inst = (const fs_inst *)be_inst;
7096
7097 if (inst->predicate) {
7098 fprintf(file, "(%cf%d.%d) ",
7099 inst->predicate_inverse ? '-' : '+',
7100 inst->flag_subreg / 2,
7101 inst->flag_subreg % 2);
7102 }
7103
7104 fprintf(file, "%s", brw_instruction_name(devinfo, inst->opcode));
7105 if (inst->saturate)
7106 fprintf(file, ".sat");
7107 if (inst->conditional_mod) {
7108 fprintf(file, "%s", conditional_modifier[inst->conditional_mod]);
7109 if (!inst->predicate &&
7110 (devinfo->gen < 5 || (inst->opcode != BRW_OPCODE_SEL &&
7111 inst->opcode != BRW_OPCODE_CSEL &&
7112 inst->opcode != BRW_OPCODE_IF &&
7113 inst->opcode != BRW_OPCODE_WHILE))) {
7114 fprintf(file, ".f%d.%d", inst->flag_subreg / 2,
7115 inst->flag_subreg % 2);
7116 }
7117 }
7118 fprintf(file, "(%d) ", inst->exec_size);
7119
7120 if (inst->mlen) {
7121 fprintf(file, "(mlen: %d) ", inst->mlen);
7122 }
7123
7124 if (inst->ex_mlen) {
7125 fprintf(file, "(ex_mlen: %d) ", inst->ex_mlen);
7126 }
7127
7128 if (inst->eot) {
7129 fprintf(file, "(EOT) ");
7130 }
7131
7132 switch (inst->dst.file) {
7133 case VGRF:
7134 fprintf(file, "vgrf%d", inst->dst.nr);
7135 break;
7136 case FIXED_GRF:
7137 fprintf(file, "g%d", inst->dst.nr);
7138 break;
7139 case MRF:
7140 fprintf(file, "m%d", inst->dst.nr);
7141 break;
7142 case BAD_FILE:
7143 fprintf(file, "(null)");
7144 break;
7145 case UNIFORM:
7146 fprintf(file, "***u%d***", inst->dst.nr);
7147 break;
7148 case ATTR:
7149 fprintf(file, "***attr%d***", inst->dst.nr);
7150 break;
7151 case ARF:
7152 switch (inst->dst.nr) {
7153 case BRW_ARF_NULL:
7154 fprintf(file, "null");
7155 break;
7156 case BRW_ARF_ADDRESS:
7157 fprintf(file, "a0.%d", inst->dst.subnr);
7158 break;
7159 case BRW_ARF_ACCUMULATOR:
7160 fprintf(file, "acc%d", inst->dst.subnr);
7161 break;
7162 case BRW_ARF_FLAG:
7163 fprintf(file, "f%d.%d", inst->dst.nr & 0xf, inst->dst.subnr);
7164 break;
7165 default:
7166 fprintf(file, "arf%d.%d", inst->dst.nr & 0xf, inst->dst.subnr);
7167 break;
7168 }
7169 break;
7170 case IMM:
7171 unreachable("not reached");
7172 }
7173
7174 if (inst->dst.offset ||
7175 (inst->dst.file == VGRF &&
7176 alloc.sizes[inst->dst.nr] * REG_SIZE != inst->size_written)) {
7177 const unsigned reg_size = (inst->dst.file == UNIFORM ? 4 : REG_SIZE);
7178 fprintf(file, "+%d.%d", inst->dst.offset / reg_size,
7179 inst->dst.offset % reg_size);
7180 }
7181
7182 if (inst->dst.stride != 1)
7183 fprintf(file, "<%u>", inst->dst.stride);
7184 fprintf(file, ":%s, ", brw_reg_type_to_letters(inst->dst.type));
7185
7186 for (int i = 0; i < inst->sources; i++) {
7187 if (inst->src[i].negate)
7188 fprintf(file, "-");
7189 if (inst->src[i].abs)
7190 fprintf(file, "|");
7191 switch (inst->src[i].file) {
7192 case VGRF:
7193 fprintf(file, "vgrf%d", inst->src[i].nr);
7194 break;
7195 case FIXED_GRF:
7196 fprintf(file, "g%d", inst->src[i].nr);
7197 break;
7198 case MRF:
7199 fprintf(file, "***m%d***", inst->src[i].nr);
7200 break;
7201 case ATTR:
7202 fprintf(file, "attr%d", inst->src[i].nr);
7203 break;
7204 case UNIFORM:
7205 fprintf(file, "u%d", inst->src[i].nr);
7206 break;
7207 case BAD_FILE:
7208 fprintf(file, "(null)");
7209 break;
7210 case IMM:
7211 switch (inst->src[i].type) {
7212 case BRW_REGISTER_TYPE_F:
7213 fprintf(file, "%-gf", inst->src[i].f);
7214 break;
7215 case BRW_REGISTER_TYPE_DF:
7216 fprintf(file, "%fdf", inst->src[i].df);
7217 break;
7218 case BRW_REGISTER_TYPE_W:
7219 case BRW_REGISTER_TYPE_D:
7220 fprintf(file, "%dd", inst->src[i].d);
7221 break;
7222 case BRW_REGISTER_TYPE_UW:
7223 case BRW_REGISTER_TYPE_UD:
7224 fprintf(file, "%uu", inst->src[i].ud);
7225 break;
7226 case BRW_REGISTER_TYPE_Q:
7227 fprintf(file, "%" PRId64 "q", inst->src[i].d64);
7228 break;
7229 case BRW_REGISTER_TYPE_UQ:
7230 fprintf(file, "%" PRIu64 "uq", inst->src[i].u64);
7231 break;
7232 case BRW_REGISTER_TYPE_VF:
7233 fprintf(file, "[%-gF, %-gF, %-gF, %-gF]",
7234 brw_vf_to_float((inst->src[i].ud >> 0) & 0xff),
7235 brw_vf_to_float((inst->src[i].ud >> 8) & 0xff),
7236 brw_vf_to_float((inst->src[i].ud >> 16) & 0xff),
7237 brw_vf_to_float((inst->src[i].ud >> 24) & 0xff));
7238 break;
7239 case BRW_REGISTER_TYPE_V:
7240 case BRW_REGISTER_TYPE_UV:
7241 fprintf(file, "%08x%s", inst->src[i].ud,
7242 inst->src[i].type == BRW_REGISTER_TYPE_V ? "V" : "UV");
7243 break;
7244 default:
7245 fprintf(file, "???");
7246 break;
7247 }
7248 break;
7249 case ARF:
7250 switch (inst->src[i].nr) {
7251 case BRW_ARF_NULL:
7252 fprintf(file, "null");
7253 break;
7254 case BRW_ARF_ADDRESS:
7255 fprintf(file, "a0.%d", inst->src[i].subnr);
7256 break;
7257 case BRW_ARF_ACCUMULATOR:
7258 fprintf(file, "acc%d", inst->src[i].subnr);
7259 break;
7260 case BRW_ARF_FLAG:
7261 fprintf(file, "f%d.%d", inst->src[i].nr & 0xf, inst->src[i].subnr);
7262 break;
7263 default:
7264 fprintf(file, "arf%d.%d", inst->src[i].nr & 0xf, inst->src[i].subnr);
7265 break;
7266 }
7267 break;
7268 }
7269
7270 if (inst->src[i].offset ||
7271 (inst->src[i].file == VGRF &&
7272 alloc.sizes[inst->src[i].nr] * REG_SIZE != inst->size_read(i))) {
7273 const unsigned reg_size = (inst->src[i].file == UNIFORM ? 4 : REG_SIZE);
7274 fprintf(file, "+%d.%d", inst->src[i].offset / reg_size,
7275 inst->src[i].offset % reg_size);
7276 }
7277
7278 if (inst->src[i].abs)
7279 fprintf(file, "|");
7280
7281 if (inst->src[i].file != IMM) {
7282 unsigned stride;
7283 if (inst->src[i].file == ARF || inst->src[i].file == FIXED_GRF) {
7284 unsigned hstride = inst->src[i].hstride;
7285 stride = (hstride == 0 ? 0 : (1 << (hstride - 1)));
7286 } else {
7287 stride = inst->src[i].stride;
7288 }
7289 if (stride != 1)
7290 fprintf(file, "<%u>", stride);
7291
7292 fprintf(file, ":%s", brw_reg_type_to_letters(inst->src[i].type));
7293 }
7294
7295 if (i < inst->sources - 1 && inst->src[i + 1].file != BAD_FILE)
7296 fprintf(file, ", ");
7297 }
7298
7299 fprintf(file, " ");
7300
7301 if (inst->force_writemask_all)
7302 fprintf(file, "NoMask ");
7303
7304 if (inst->exec_size != dispatch_width)
7305 fprintf(file, "group%d ", inst->group);
7306
7307 fprintf(file, "\n");
7308 }
7309
7310 void
7311 fs_visitor::setup_fs_payload_gen6()
7312 {
7313 assert(stage == MESA_SHADER_FRAGMENT);
7314 struct brw_wm_prog_data *prog_data = brw_wm_prog_data(this->prog_data);
7315 const unsigned payload_width = MIN2(16, dispatch_width);
7316 assert(dispatch_width % payload_width == 0);
7317 assert(devinfo->gen >= 6);
7318
7319 prog_data->uses_src_depth = prog_data->uses_src_w =
7320 (nir->info.system_values_read & (1ull << SYSTEM_VALUE_FRAG_COORD)) != 0;
7321
7322 prog_data->uses_sample_mask =
7323 (nir->info.system_values_read & SYSTEM_BIT_SAMPLE_MASK_IN) != 0;
7324
7325 /* From the Ivy Bridge PRM documentation for 3DSTATE_PS:
7326 *
7327 * "MSDISPMODE_PERSAMPLE is required in order to select
7328 * POSOFFSET_SAMPLE"
7329 *
7330 * So we can only really get sample positions if we are doing real
7331 * per-sample dispatch. If we need gl_SamplePosition and we don't have
7332 * persample dispatch, we hard-code it to 0.5.
7333 */
7334 prog_data->uses_pos_offset = prog_data->persample_dispatch &&
7335 (nir->info.system_values_read & SYSTEM_BIT_SAMPLE_POS);
7336
7337 /* R0: PS thread payload header. */
7338 payload.num_regs++;
7339
7340 for (unsigned j = 0; j < dispatch_width / payload_width; j++) {
7341 /* R1: masks, pixel X/Y coordinates. */
7342 payload.subspan_coord_reg[j] = payload.num_regs++;
7343 }
7344
7345 for (unsigned j = 0; j < dispatch_width / payload_width; j++) {
7346 /* R3-26: barycentric interpolation coordinates. These appear in the
7347 * same order that they appear in the brw_barycentric_mode enum. Each
7348 * set of coordinates occupies 2 registers if dispatch width == 8 and 4
7349 * registers if dispatch width == 16. Coordinates only appear if they
7350 * were enabled using the "Barycentric Interpolation Mode" bits in
7351 * WM_STATE.
7352 */
7353 for (int i = 0; i < BRW_BARYCENTRIC_MODE_COUNT; ++i) {
7354 if (prog_data->barycentric_interp_modes & (1 << i)) {
7355 payload.barycentric_coord_reg[i][j] = payload.num_regs;
7356 payload.num_regs += payload_width / 4;
7357 }
7358 }
7359
7360 /* R27-28: interpolated depth if uses source depth */
7361 if (prog_data->uses_src_depth) {
7362 payload.source_depth_reg[j] = payload.num_regs;
7363 payload.num_regs += payload_width / 8;
7364 }
7365
7366 /* R29-30: interpolated W set if GEN6_WM_USES_SOURCE_W. */
7367 if (prog_data->uses_src_w) {
7368 payload.source_w_reg[j] = payload.num_regs;
7369 payload.num_regs += payload_width / 8;
7370 }
7371
7372 /* R31: MSAA position offsets. */
7373 if (prog_data->uses_pos_offset) {
7374 payload.sample_pos_reg[j] = payload.num_regs;
7375 payload.num_regs++;
7376 }
7377
7378 /* R32-33: MSAA input coverage mask */
7379 if (prog_data->uses_sample_mask) {
7380 assert(devinfo->gen >= 7);
7381 payload.sample_mask_in_reg[j] = payload.num_regs;
7382 payload.num_regs += payload_width / 8;
7383 }
7384 }
7385
7386 if (nir->info.outputs_written & BITFIELD64_BIT(FRAG_RESULT_DEPTH)) {
7387 source_depth_to_render_target = true;
7388 }
7389 }
7390
7391 void
7392 fs_visitor::setup_vs_payload()
7393 {
7394 /* R0: thread header, R1: urb handles */
7395 payload.num_regs = 2;
7396 }
7397
7398 void
7399 fs_visitor::setup_gs_payload()
7400 {
7401 assert(stage == MESA_SHADER_GEOMETRY);
7402
7403 struct brw_gs_prog_data *gs_prog_data = brw_gs_prog_data(prog_data);
7404 struct brw_vue_prog_data *vue_prog_data = brw_vue_prog_data(prog_data);
7405
7406 /* R0: thread header, R1: output URB handles */
7407 payload.num_regs = 2;
7408
7409 if (gs_prog_data->include_primitive_id) {
7410 /* R2: Primitive ID 0..7 */
7411 payload.num_regs++;
7412 }
7413
7414 /* Always enable VUE handles so we can safely use pull model if needed.
7415 *
7416 * The push model for a GS uses a ton of register space even for trivial
7417 * scenarios with just a few inputs, so just make things easier and a bit
7418 * safer by always having pull model available.
7419 */
7420 gs_prog_data->base.include_vue_handles = true;
7421
7422 /* R3..RN: ICP Handles for each incoming vertex (when using pull model) */
7423 payload.num_regs += nir->info.gs.vertices_in;
7424
7425 /* Use a maximum of 24 registers for push-model inputs. */
7426 const unsigned max_push_components = 24;
7427
7428 /* If pushing our inputs would take too many registers, reduce the URB read
7429 * length (which is in HWords, or 8 registers), and resort to pulling.
7430 *
7431 * Note that the GS reads <URB Read Length> HWords for every vertex - so we
7432 * have to multiply by VerticesIn to obtain the total storage requirement.
7433 */
7434 if (8 * vue_prog_data->urb_read_length * nir->info.gs.vertices_in >
7435 max_push_components) {
7436 vue_prog_data->urb_read_length =
7437 ROUND_DOWN_TO(max_push_components / nir->info.gs.vertices_in, 8) / 8;
7438 }
7439 }
7440
7441 void
7442 fs_visitor::setup_cs_payload()
7443 {
7444 assert(devinfo->gen >= 7);
7445 payload.num_regs = 1;
7446 }
7447
7448 brw::register_pressure::register_pressure(const fs_visitor *v)
7449 {
7450 const fs_live_variables &live = v->live_analysis.require();
7451 const unsigned num_instructions = v->cfg->num_blocks ?
7452 v->cfg->blocks[v->cfg->num_blocks - 1]->end_ip + 1 : 0;
7453
7454 regs_live_at_ip = new unsigned[num_instructions]();
7455
7456 for (unsigned reg = 0; reg < v->alloc.count; reg++) {
7457 for (int ip = live.vgrf_start[reg]; ip <= live.vgrf_end[reg]; ip++)
7458 regs_live_at_ip[ip] += v->alloc.sizes[reg];
7459 }
7460 }
7461
7462 brw::register_pressure::~register_pressure()
7463 {
7464 delete[] regs_live_at_ip;
7465 }
7466
7467 void
7468 fs_visitor::invalidate_analysis(brw::analysis_dependency_class c)
7469 {
7470 backend_shader::invalidate_analysis(c);
7471 live_analysis.invalidate(c);
7472 regpressure_analysis.invalidate(c);
7473 }
7474
7475 void
7476 fs_visitor::optimize()
7477 {
7478 /* Start by validating the shader we currently have. */
7479 validate();
7480
7481 /* bld is the common builder object pointing at the end of the program we
7482 * used to translate it into i965 IR. For the optimization and lowering
7483 * passes coming next, any code added after the end of the program without
7484 * having explicitly called fs_builder::at() clearly points at a mistake.
7485 * Ideally optimization passes wouldn't be part of the visitor so they
7486 * wouldn't have access to bld at all, but they do, so just in case some
7487 * pass forgets to ask for a location explicitly set it to NULL here to
7488 * make it trip. The dispatch width is initialized to a bogus value to
7489 * make sure that optimizations set the execution controls explicitly to
7490 * match the code they are manipulating instead of relying on the defaults.
7491 */
7492 bld = fs_builder(this, 64);
7493
7494 assign_constant_locations();
7495 lower_constant_loads();
7496
7497 validate();
7498
7499 split_virtual_grfs();
7500 validate();
7501
7502 #define OPT(pass, args...) ({ \
7503 pass_num++; \
7504 bool this_progress = pass(args); \
7505 \
7506 if (unlikely(INTEL_DEBUG & DEBUG_OPTIMIZER) && this_progress) { \
7507 char filename[64]; \
7508 snprintf(filename, 64, "%s%d-%s-%02d-%02d-" #pass, \
7509 stage_abbrev, dispatch_width, nir->info.name, iteration, pass_num); \
7510 \
7511 backend_shader::dump_instructions(filename); \
7512 } \
7513 \
7514 validate(); \
7515 \
7516 progress = progress || this_progress; \
7517 this_progress; \
7518 })
7519
7520 if (unlikely(INTEL_DEBUG & DEBUG_OPTIMIZER)) {
7521 char filename[64];
7522 snprintf(filename, 64, "%s%d-%s-00-00-start",
7523 stage_abbrev, dispatch_width, nir->info.name);
7524
7525 backend_shader::dump_instructions(filename);
7526 }
7527
7528 bool progress = false;
7529 int iteration = 0;
7530 int pass_num = 0;
7531
7532 /* Before anything else, eliminate dead code. The results of some NIR
7533 * instructions may effectively be calculated twice. Once when the
7534 * instruction is encountered, and again when the user of that result is
7535 * encountered. Wipe those away before algebraic optimizations and
7536 * especially copy propagation can mix things up.
7537 */
7538 OPT(dead_code_eliminate);
7539
7540 OPT(remove_extra_rounding_modes);
7541
7542 do {
7543 progress = false;
7544 pass_num = 0;
7545 iteration++;
7546
7547 OPT(remove_duplicate_mrf_writes);
7548
7549 OPT(opt_algebraic);
7550 OPT(opt_cse);
7551 OPT(opt_copy_propagation);
7552 OPT(opt_predicated_break, this);
7553 OPT(opt_cmod_propagation);
7554 OPT(dead_code_eliminate);
7555 OPT(opt_peephole_sel);
7556 OPT(dead_control_flow_eliminate, this);
7557 OPT(opt_register_renaming);
7558 OPT(opt_saturate_propagation);
7559 OPT(register_coalesce);
7560 OPT(compute_to_mrf);
7561 OPT(eliminate_find_live_channel);
7562
7563 OPT(compact_virtual_grfs);
7564 } while (progress);
7565
7566 progress = false;
7567 pass_num = 0;
7568
7569 if (OPT(lower_pack)) {
7570 OPT(register_coalesce);
7571 OPT(dead_code_eliminate);
7572 }
7573
7574 OPT(lower_simd_width);
7575 OPT(lower_barycentrics);
7576
7577 /* After SIMD lowering just in case we had to unroll the EOT send. */
7578 OPT(opt_sampler_eot);
7579
7580 OPT(lower_logical_sends);
7581
7582 /* After logical SEND lowering. */
7583 OPT(fixup_nomask_control_flow);
7584
7585 if (progress) {
7586 OPT(opt_copy_propagation);
7587 /* Only run after logical send lowering because it's easier to implement
7588 * in terms of physical sends.
7589 */
7590 if (OPT(opt_zero_samples))
7591 OPT(opt_copy_propagation);
7592 /* Run after logical send lowering to give it a chance to CSE the
7593 * LOAD_PAYLOAD instructions created to construct the payloads of
7594 * e.g. texturing messages in cases where it wasn't possible to CSE the
7595 * whole logical instruction.
7596 */
7597 OPT(opt_cse);
7598 OPT(register_coalesce);
7599 OPT(compute_to_mrf);
7600 OPT(dead_code_eliminate);
7601 OPT(remove_duplicate_mrf_writes);
7602 OPT(opt_peephole_sel);
7603 }
7604
7605 OPT(opt_redundant_discard_jumps);
7606
7607 if (OPT(lower_load_payload)) {
7608 split_virtual_grfs();
7609
7610 /* Lower 64 bit MOVs generated by payload lowering. */
7611 if (!devinfo->has_64bit_float && !devinfo->has_64bit_int)
7612 OPT(opt_algebraic);
7613
7614 OPT(register_coalesce);
7615 OPT(lower_simd_width);
7616 OPT(compute_to_mrf);
7617 OPT(dead_code_eliminate);
7618 }
7619
7620 OPT(opt_combine_constants);
7621 OPT(lower_integer_multiplication);
7622 OPT(lower_sub_sat);
7623
7624 if (devinfo->gen <= 5 && OPT(lower_minmax)) {
7625 OPT(opt_cmod_propagation);
7626 OPT(opt_cse);
7627 OPT(opt_copy_propagation);
7628 OPT(dead_code_eliminate);
7629 }
7630
7631 if (OPT(lower_regioning)) {
7632 OPT(opt_copy_propagation);
7633 OPT(dead_code_eliminate);
7634 OPT(lower_simd_width);
7635 }
7636
7637 OPT(fixup_sends_duplicate_payload);
7638
7639 lower_uniform_pull_constant_loads();
7640
7641 validate();
7642 }
7643
7644 /**
7645 * From the Skylake PRM Vol. 2a docs for sends:
7646 *
7647 * "It is required that the second block of GRFs does not overlap with the
7648 * first block."
7649 *
7650 * There are plenty of cases where we may accidentally violate this due to
7651 * having, for instance, both sources be the constant 0. This little pass
7652 * just adds a new vgrf for the second payload and copies it over.
7653 */
7654 bool
7655 fs_visitor::fixup_sends_duplicate_payload()
7656 {
7657 bool progress = false;
7658
7659 foreach_block_and_inst_safe (block, fs_inst, inst, cfg) {
7660 if (inst->opcode == SHADER_OPCODE_SEND && inst->ex_mlen > 0 &&
7661 regions_overlap(inst->src[2], inst->mlen * REG_SIZE,
7662 inst->src[3], inst->ex_mlen * REG_SIZE)) {
7663 fs_reg tmp = fs_reg(VGRF, alloc.allocate(inst->ex_mlen),
7664 BRW_REGISTER_TYPE_UD);
7665 /* Sadly, we've lost all notion of channels and bit sizes at this
7666 * point. Just WE_all it.
7667 */
7668 const fs_builder ibld = bld.at(block, inst).exec_all().group(16, 0);
7669 fs_reg copy_src = retype(inst->src[3], BRW_REGISTER_TYPE_UD);
7670 fs_reg copy_dst = tmp;
7671 for (unsigned i = 0; i < inst->ex_mlen; i += 2) {
7672 if (inst->ex_mlen == i + 1) {
7673 /* Only one register left; do SIMD8 */
7674 ibld.group(8, 0).MOV(copy_dst, copy_src);
7675 } else {
7676 ibld.MOV(copy_dst, copy_src);
7677 }
7678 copy_src = offset(copy_src, ibld, 1);
7679 copy_dst = offset(copy_dst, ibld, 1);
7680 }
7681 inst->src[3] = tmp;
7682 progress = true;
7683 }
7684 }
7685
7686 if (progress)
7687 invalidate_analysis(DEPENDENCY_INSTRUCTIONS | DEPENDENCY_VARIABLES);
7688
7689 return progress;
7690 }
7691
7692 /**
7693 * Three source instruction must have a GRF/MRF destination register.
7694 * ARF NULL is not allowed. Fix that up by allocating a temporary GRF.
7695 */
7696 void
7697 fs_visitor::fixup_3src_null_dest()
7698 {
7699 bool progress = false;
7700
7701 foreach_block_and_inst_safe (block, fs_inst, inst, cfg) {
7702 if (inst->is_3src(devinfo) && inst->dst.is_null()) {
7703 inst->dst = fs_reg(VGRF, alloc.allocate(dispatch_width / 8),
7704 inst->dst.type);
7705 progress = true;
7706 }
7707 }
7708
7709 if (progress)
7710 invalidate_analysis(DEPENDENCY_INSTRUCTION_DETAIL |
7711 DEPENDENCY_VARIABLES);
7712 }
7713
7714 /**
7715 * Find the first instruction in the program that might start a region of
7716 * divergent control flow due to a HALT jump. There is no
7717 * find_halt_control_flow_region_end(), the region of divergence extends until
7718 * the only FS_OPCODE_PLACEHOLDER_HALT in the program.
7719 */
7720 static const fs_inst *
7721 find_halt_control_flow_region_start(const fs_visitor *v)
7722 {
7723 if (brw_wm_prog_data(v->prog_data)->uses_kill) {
7724 foreach_block_and_inst(block, fs_inst, inst, v->cfg) {
7725 if (inst->opcode == FS_OPCODE_DISCARD_JUMP ||
7726 inst->opcode == FS_OPCODE_PLACEHOLDER_HALT)
7727 return inst;
7728 }
7729 }
7730
7731 return NULL;
7732 }
7733
7734 /**
7735 * Work around the Gen12 hardware bug filed as GEN:BUG:1407528679. EU fusion
7736 * can cause a BB to be executed with all channels disabled, which will lead
7737 * to the execution of any NoMask instructions in it, even though any
7738 * execution-masked instructions will be correctly shot down. This may break
7739 * assumptions of some NoMask SEND messages whose descriptor depends on data
7740 * generated by live invocations of the shader.
7741 *
7742 * This avoids the problem by predicating certain instructions on an ANY
7743 * horizontal predicate that makes sure that their execution is omitted when
7744 * all channels of the program are disabled.
7745 */
7746 bool
7747 fs_visitor::fixup_nomask_control_flow()
7748 {
7749 if (devinfo->gen != 12)
7750 return false;
7751
7752 const brw_predicate pred = dispatch_width > 16 ? BRW_PREDICATE_ALIGN1_ANY32H :
7753 dispatch_width > 8 ? BRW_PREDICATE_ALIGN1_ANY16H :
7754 BRW_PREDICATE_ALIGN1_ANY8H;
7755 const fs_inst *halt_start = find_halt_control_flow_region_start(this);
7756 unsigned depth = 0;
7757 bool progress = false;
7758
7759 const fs_live_variables &live_vars = live_analysis.require();
7760
7761 /* Scan the program backwards in order to be able to easily determine
7762 * whether the flag register is live at any point.
7763 */
7764 foreach_block_reverse_safe(block, cfg) {
7765 BITSET_WORD flag_liveout = live_vars.block_data[block->num]
7766 .flag_liveout[0];
7767 STATIC_ASSERT(ARRAY_SIZE(live_vars.block_data[0].flag_liveout) == 1);
7768
7769 foreach_inst_in_block_reverse_safe(fs_inst, inst, block) {
7770 if (!inst->predicate && inst->exec_size >= 8)
7771 flag_liveout &= ~inst->flags_written();
7772
7773 switch (inst->opcode) {
7774 case BRW_OPCODE_DO:
7775 case BRW_OPCODE_IF:
7776 /* Note that this doesn't handle FS_OPCODE_DISCARD_JUMP since only
7777 * the first one in the program closes the region of divergent
7778 * control flow due to any HALT instructions -- Instead this is
7779 * handled with the halt_start check below.
7780 */
7781 depth--;
7782 break;
7783
7784 case BRW_OPCODE_WHILE:
7785 case BRW_OPCODE_ENDIF:
7786 case FS_OPCODE_PLACEHOLDER_HALT:
7787 depth++;
7788 break;
7789
7790 default:
7791 /* Note that the vast majority of NoMask SEND instructions in the
7792 * program are harmless while executed in a block with all
7793 * channels disabled, since any instructions with side effects we
7794 * could hit here should be execution-masked.
7795 *
7796 * The main concern is NoMask SEND instructions where the message
7797 * descriptor or header depends on data generated by live
7798 * invocations of the shader (RESINFO and
7799 * FS_OPCODE_UNIFORM_PULL_CONSTANT_LOAD with a dynamically
7800 * computed surface index seem to be the only examples right now
7801 * where this could easily lead to GPU hangs). Unfortunately we
7802 * have no straightforward way to detect that currently, so just
7803 * predicate any NoMask SEND instructions we find under control
7804 * flow.
7805 *
7806 * If this proves to have a measurable performance impact it can
7807 * be easily extended with a whitelist of messages we know we can
7808 * safely omit the predication for.
7809 */
7810 if (depth && inst->force_writemask_all &&
7811 is_send(inst) && !inst->predicate) {
7812 /* We need to load the execution mask into the flag register by
7813 * using a builder with channel group matching the whole shader
7814 * (rather than the default which is derived from the original
7815 * instruction), in order to avoid getting a right-shifted
7816 * value.
7817 */
7818 const fs_builder ubld = fs_builder(this, block, inst)
7819 .exec_all().group(dispatch_width, 0);
7820 const fs_reg flag = retype(brw_flag_reg(0, 0),
7821 BRW_REGISTER_TYPE_UD);
7822
7823 /* Due to the lack of flag register allocation we need to save
7824 * and restore the flag register if it's live.
7825 */
7826 const bool save_flag = flag_liveout &
7827 flag_mask(flag, dispatch_width / 8);
7828 const fs_reg tmp = ubld.group(1, 0).vgrf(flag.type);
7829
7830 if (save_flag)
7831 ubld.group(1, 0).MOV(tmp, flag);
7832
7833 ubld.emit(FS_OPCODE_LOAD_LIVE_CHANNELS);
7834
7835 set_predicate(pred, inst);
7836 inst->flag_subreg = 0;
7837
7838 if (save_flag)
7839 ubld.group(1, 0).at(block, inst->next).MOV(flag, tmp);
7840
7841 progress = true;
7842 }
7843 break;
7844 }
7845
7846 if (inst == halt_start)
7847 depth--;
7848
7849 flag_liveout |= inst->flags_read(devinfo);
7850 }
7851 }
7852
7853 if (progress)
7854 invalidate_analysis(DEPENDENCY_INSTRUCTIONS | DEPENDENCY_VARIABLES);
7855
7856 return progress;
7857 }
7858
7859 void
7860 fs_visitor::allocate_registers(bool allow_spilling)
7861 {
7862 bool allocated;
7863
7864 static const enum instruction_scheduler_mode pre_modes[] = {
7865 SCHEDULE_PRE,
7866 SCHEDULE_PRE_NON_LIFO,
7867 SCHEDULE_PRE_LIFO,
7868 };
7869
7870 static const char *scheduler_mode_name[] = {
7871 "top-down",
7872 "non-lifo",
7873 "lifo"
7874 };
7875
7876 bool spill_all = allow_spilling && (INTEL_DEBUG & DEBUG_SPILL_FS);
7877
7878 /* Try each scheduling heuristic to see if it can successfully register
7879 * allocate without spilling. They should be ordered by decreasing
7880 * performance but increasing likelihood of allocating.
7881 */
7882 for (unsigned i = 0; i < ARRAY_SIZE(pre_modes); i++) {
7883 schedule_instructions(pre_modes[i]);
7884 this->shader_stats.scheduler_mode = scheduler_mode_name[i];
7885
7886 if (0) {
7887 assign_regs_trivial();
7888 allocated = true;
7889 break;
7890 }
7891
7892 /* Scheduling may create additional opportunities for CMOD propagation,
7893 * so let's do it again. If CMOD propagation made any progress,
7894 * elminate dead code one more time.
7895 */
7896 bool progress = false;
7897 const int iteration = 99;
7898 int pass_num = 0;
7899
7900 if (OPT(opt_cmod_propagation)) {
7901 /* dead_code_eliminate "undoes" the fixing done by
7902 * fixup_3src_null_dest, so we have to do it again if
7903 * dead_code_eliminiate makes any progress.
7904 */
7905 if (OPT(dead_code_eliminate))
7906 fixup_3src_null_dest();
7907 }
7908
7909 bool can_spill = allow_spilling &&
7910 (i == ARRAY_SIZE(pre_modes) - 1);
7911
7912 /* We should only spill registers on the last scheduling. */
7913 assert(!spilled_any_registers);
7914
7915 allocated = assign_regs(can_spill, spill_all);
7916 if (allocated)
7917 break;
7918 }
7919
7920 if (!allocated) {
7921 fail("Failure to register allocate. Reduce number of "
7922 "live scalar values to avoid this.");
7923 } else if (spilled_any_registers) {
7924 compiler->shader_perf_log(log_data,
7925 "%s shader triggered register spilling. "
7926 "Try reducing the number of live scalar "
7927 "values to improve performance.\n",
7928 stage_name);
7929 }
7930
7931 /* This must come after all optimization and register allocation, since
7932 * it inserts dead code that happens to have side effects, and it does
7933 * so based on the actual physical registers in use.
7934 */
7935 insert_gen4_send_dependency_workarounds();
7936
7937 if (failed)
7938 return;
7939
7940 opt_bank_conflicts();
7941
7942 schedule_instructions(SCHEDULE_POST);
7943
7944 if (last_scratch > 0) {
7945 ASSERTED unsigned max_scratch_size = 2 * 1024 * 1024;
7946
7947 prog_data->total_scratch = brw_get_scratch_size(last_scratch);
7948
7949 if (stage == MESA_SHADER_COMPUTE) {
7950 if (devinfo->is_haswell) {
7951 /* According to the MEDIA_VFE_STATE's "Per Thread Scratch Space"
7952 * field documentation, Haswell supports a minimum of 2kB of
7953 * scratch space for compute shaders, unlike every other stage
7954 * and platform.
7955 */
7956 prog_data->total_scratch = MAX2(prog_data->total_scratch, 2048);
7957 } else if (devinfo->gen <= 7) {
7958 /* According to the MEDIA_VFE_STATE's "Per Thread Scratch Space"
7959 * field documentation, platforms prior to Haswell measure scratch
7960 * size linearly with a range of [1kB, 12kB] and 1kB granularity.
7961 */
7962 prog_data->total_scratch = ALIGN(last_scratch, 1024);
7963 max_scratch_size = 12 * 1024;
7964 }
7965 }
7966
7967 /* We currently only support up to 2MB of scratch space. If we
7968 * need to support more eventually, the documentation suggests
7969 * that we could allocate a larger buffer, and partition it out
7970 * ourselves. We'd just have to undo the hardware's address
7971 * calculation by subtracting (FFTID * Per Thread Scratch Space)
7972 * and then add FFTID * (Larger Per Thread Scratch Space).
7973 *
7974 * See 3D-Media-GPGPU Engine > Media GPGPU Pipeline >
7975 * Thread Group Tracking > Local Memory/Scratch Space.
7976 */
7977 assert(prog_data->total_scratch < max_scratch_size);
7978 }
7979
7980 lower_scoreboard();
7981 }
7982
7983 bool
7984 fs_visitor::run_vs()
7985 {
7986 assert(stage == MESA_SHADER_VERTEX);
7987
7988 setup_vs_payload();
7989
7990 if (shader_time_index >= 0)
7991 emit_shader_time_begin();
7992
7993 emit_nir_code();
7994
7995 if (failed)
7996 return false;
7997
7998 emit_urb_writes();
7999
8000 if (shader_time_index >= 0)
8001 emit_shader_time_end();
8002
8003 calculate_cfg();
8004
8005 optimize();
8006
8007 assign_curb_setup();
8008 assign_vs_urb_setup();
8009
8010 fixup_3src_null_dest();
8011 allocate_registers(true /* allow_spilling */);
8012
8013 return !failed;
8014 }
8015
8016 void
8017 fs_visitor::set_tcs_invocation_id()
8018 {
8019 struct brw_tcs_prog_data *tcs_prog_data = brw_tcs_prog_data(prog_data);
8020 struct brw_vue_prog_data *vue_prog_data = &tcs_prog_data->base;
8021
8022 const unsigned instance_id_mask =
8023 devinfo->gen >= 11 ? INTEL_MASK(22, 16) : INTEL_MASK(23, 17);
8024 const unsigned instance_id_shift =
8025 devinfo->gen >= 11 ? 16 : 17;
8026
8027 /* Get instance number from g0.2 bits 22:16 or 23:17 */
8028 fs_reg t = bld.vgrf(BRW_REGISTER_TYPE_UD);
8029 bld.AND(t, fs_reg(retype(brw_vec1_grf(0, 2), BRW_REGISTER_TYPE_UD)),
8030 brw_imm_ud(instance_id_mask));
8031
8032 invocation_id = bld.vgrf(BRW_REGISTER_TYPE_UD);
8033
8034 if (vue_prog_data->dispatch_mode == DISPATCH_MODE_TCS_8_PATCH) {
8035 /* gl_InvocationID is just the thread number */
8036 bld.SHR(invocation_id, t, brw_imm_ud(instance_id_shift));
8037 return;
8038 }
8039
8040 assert(vue_prog_data->dispatch_mode == DISPATCH_MODE_TCS_SINGLE_PATCH);
8041
8042 fs_reg channels_uw = bld.vgrf(BRW_REGISTER_TYPE_UW);
8043 fs_reg channels_ud = bld.vgrf(BRW_REGISTER_TYPE_UD);
8044 bld.MOV(channels_uw, fs_reg(brw_imm_uv(0x76543210)));
8045 bld.MOV(channels_ud, channels_uw);
8046
8047 if (tcs_prog_data->instances == 1) {
8048 invocation_id = channels_ud;
8049 } else {
8050 fs_reg instance_times_8 = bld.vgrf(BRW_REGISTER_TYPE_UD);
8051 bld.SHR(instance_times_8, t, brw_imm_ud(instance_id_shift - 3));
8052 bld.ADD(invocation_id, instance_times_8, channels_ud);
8053 }
8054 }
8055
8056 bool
8057 fs_visitor::run_tcs()
8058 {
8059 assert(stage == MESA_SHADER_TESS_CTRL);
8060
8061 struct brw_vue_prog_data *vue_prog_data = brw_vue_prog_data(prog_data);
8062 struct brw_tcs_prog_data *tcs_prog_data = brw_tcs_prog_data(prog_data);
8063 struct brw_tcs_prog_key *tcs_key = (struct brw_tcs_prog_key *) key;
8064
8065 assert(vue_prog_data->dispatch_mode == DISPATCH_MODE_TCS_SINGLE_PATCH ||
8066 vue_prog_data->dispatch_mode == DISPATCH_MODE_TCS_8_PATCH);
8067
8068 if (vue_prog_data->dispatch_mode == DISPATCH_MODE_TCS_SINGLE_PATCH) {
8069 /* r1-r4 contain the ICP handles. */
8070 payload.num_regs = 5;
8071 } else {
8072 assert(vue_prog_data->dispatch_mode == DISPATCH_MODE_TCS_8_PATCH);
8073 assert(tcs_key->input_vertices > 0);
8074 /* r1 contains output handles, r2 may contain primitive ID, then the
8075 * ICP handles occupy the next 1-32 registers.
8076 */
8077 payload.num_regs = 2 + tcs_prog_data->include_primitive_id +
8078 tcs_key->input_vertices;
8079 }
8080
8081 if (shader_time_index >= 0)
8082 emit_shader_time_begin();
8083
8084 /* Initialize gl_InvocationID */
8085 set_tcs_invocation_id();
8086
8087 const bool fix_dispatch_mask =
8088 vue_prog_data->dispatch_mode == DISPATCH_MODE_TCS_SINGLE_PATCH &&
8089 (nir->info.tess.tcs_vertices_out % 8) != 0;
8090
8091 /* Fix the disptach mask */
8092 if (fix_dispatch_mask) {
8093 bld.CMP(bld.null_reg_ud(), invocation_id,
8094 brw_imm_ud(nir->info.tess.tcs_vertices_out), BRW_CONDITIONAL_L);
8095 bld.IF(BRW_PREDICATE_NORMAL);
8096 }
8097
8098 emit_nir_code();
8099
8100 if (fix_dispatch_mask) {
8101 bld.emit(BRW_OPCODE_ENDIF);
8102 }
8103
8104 /* Emit EOT write; set TR DS Cache bit */
8105 fs_reg srcs[3] = {
8106 fs_reg(get_tcs_output_urb_handle()),
8107 fs_reg(brw_imm_ud(WRITEMASK_X << 16)),
8108 fs_reg(brw_imm_ud(0)),
8109 };
8110 fs_reg payload = bld.vgrf(BRW_REGISTER_TYPE_UD, 3);
8111 bld.LOAD_PAYLOAD(payload, srcs, 3, 2);
8112
8113 fs_inst *inst = bld.emit(SHADER_OPCODE_URB_WRITE_SIMD8_MASKED,
8114 bld.null_reg_ud(), payload);
8115 inst->mlen = 3;
8116 inst->eot = true;
8117
8118 if (shader_time_index >= 0)
8119 emit_shader_time_end();
8120
8121 if (failed)
8122 return false;
8123
8124 calculate_cfg();
8125
8126 optimize();
8127
8128 assign_curb_setup();
8129 assign_tcs_urb_setup();
8130
8131 fixup_3src_null_dest();
8132 allocate_registers(true /* allow_spilling */);
8133
8134 return !failed;
8135 }
8136
8137 bool
8138 fs_visitor::run_tes()
8139 {
8140 assert(stage == MESA_SHADER_TESS_EVAL);
8141
8142 /* R0: thread header, R1-3: gl_TessCoord.xyz, R4: URB handles */
8143 payload.num_regs = 5;
8144
8145 if (shader_time_index >= 0)
8146 emit_shader_time_begin();
8147
8148 emit_nir_code();
8149
8150 if (failed)
8151 return false;
8152
8153 emit_urb_writes();
8154
8155 if (shader_time_index >= 0)
8156 emit_shader_time_end();
8157
8158 calculate_cfg();
8159
8160 optimize();
8161
8162 assign_curb_setup();
8163 assign_tes_urb_setup();
8164
8165 fixup_3src_null_dest();
8166 allocate_registers(true /* allow_spilling */);
8167
8168 return !failed;
8169 }
8170
8171 bool
8172 fs_visitor::run_gs()
8173 {
8174 assert(stage == MESA_SHADER_GEOMETRY);
8175
8176 setup_gs_payload();
8177
8178 this->final_gs_vertex_count = vgrf(glsl_type::uint_type);
8179
8180 if (gs_compile->control_data_header_size_bits > 0) {
8181 /* Create a VGRF to store accumulated control data bits. */
8182 this->control_data_bits = vgrf(glsl_type::uint_type);
8183
8184 /* If we're outputting more than 32 control data bits, then EmitVertex()
8185 * will set control_data_bits to 0 after emitting the first vertex.
8186 * Otherwise, we need to initialize it to 0 here.
8187 */
8188 if (gs_compile->control_data_header_size_bits <= 32) {
8189 const fs_builder abld = bld.annotate("initialize control data bits");
8190 abld.MOV(this->control_data_bits, brw_imm_ud(0u));
8191 }
8192 }
8193
8194 if (shader_time_index >= 0)
8195 emit_shader_time_begin();
8196
8197 emit_nir_code();
8198
8199 emit_gs_thread_end();
8200
8201 if (shader_time_index >= 0)
8202 emit_shader_time_end();
8203
8204 if (failed)
8205 return false;
8206
8207 calculate_cfg();
8208
8209 optimize();
8210
8211 assign_curb_setup();
8212 assign_gs_urb_setup();
8213
8214 fixup_3src_null_dest();
8215 allocate_registers(true /* allow_spilling */);
8216
8217 return !failed;
8218 }
8219
8220 /* From the SKL PRM, Volume 16, Workarounds:
8221 *
8222 * 0877 3D Pixel Shader Hang possible when pixel shader dispatched with
8223 * only header phases (R0-R2)
8224 *
8225 * WA: Enable a non-header phase (e.g. push constant) when dispatch would
8226 * have been header only.
8227 *
8228 * Instead of enabling push constants one can alternatively enable one of the
8229 * inputs. Here one simply chooses "layer" which shouldn't impose much
8230 * overhead.
8231 */
8232 static void
8233 gen9_ps_header_only_workaround(struct brw_wm_prog_data *wm_prog_data)
8234 {
8235 if (wm_prog_data->num_varying_inputs)
8236 return;
8237
8238 if (wm_prog_data->base.curb_read_length)
8239 return;
8240
8241 wm_prog_data->urb_setup[VARYING_SLOT_LAYER] = 0;
8242 wm_prog_data->num_varying_inputs = 1;
8243
8244 brw_compute_urb_setup_index(wm_prog_data);
8245 }
8246
8247 bool
8248 fs_visitor::run_fs(bool allow_spilling, bool do_rep_send)
8249 {
8250 struct brw_wm_prog_data *wm_prog_data = brw_wm_prog_data(this->prog_data);
8251 brw_wm_prog_key *wm_key = (brw_wm_prog_key *) this->key;
8252
8253 assert(stage == MESA_SHADER_FRAGMENT);
8254
8255 if (devinfo->gen >= 6)
8256 setup_fs_payload_gen6();
8257 else
8258 setup_fs_payload_gen4();
8259
8260 if (0) {
8261 emit_dummy_fs();
8262 } else if (do_rep_send) {
8263 assert(dispatch_width == 16);
8264 emit_repclear_shader();
8265 } else {
8266 if (shader_time_index >= 0)
8267 emit_shader_time_begin();
8268
8269 if (nir->info.inputs_read > 0 ||
8270 (nir->info.system_values_read & (1ull << SYSTEM_VALUE_FRAG_COORD)) ||
8271 (nir->info.outputs_read > 0 && !wm_key->coherent_fb_fetch)) {
8272 if (devinfo->gen < 6)
8273 emit_interpolation_setup_gen4();
8274 else
8275 emit_interpolation_setup_gen6();
8276 }
8277
8278 /* We handle discards by keeping track of the still-live pixels in f0.1.
8279 * Initialize it with the dispatched pixels.
8280 */
8281 if (wm_prog_data->uses_kill) {
8282 const unsigned lower_width = MIN2(dispatch_width, 16);
8283 for (unsigned i = 0; i < dispatch_width / lower_width; i++) {
8284 const fs_reg dispatch_mask =
8285 devinfo->gen >= 6 ? brw_vec1_grf((i ? 2 : 1), 7) :
8286 brw_vec1_grf(0, 0);
8287 bld.exec_all().group(1, 0)
8288 .MOV(sample_mask_reg(bld.group(lower_width, i)),
8289 retype(dispatch_mask, BRW_REGISTER_TYPE_UW));
8290 }
8291 }
8292
8293 if (nir->info.writes_memory)
8294 wm_prog_data->has_side_effects = true;
8295
8296 emit_nir_code();
8297
8298 if (failed)
8299 return false;
8300
8301 if (wm_prog_data->uses_kill)
8302 bld.emit(FS_OPCODE_PLACEHOLDER_HALT);
8303
8304 if (wm_key->alpha_test_func)
8305 emit_alpha_test();
8306
8307 emit_fb_writes();
8308
8309 if (shader_time_index >= 0)
8310 emit_shader_time_end();
8311
8312 calculate_cfg();
8313
8314 optimize();
8315
8316 assign_curb_setup();
8317
8318 if (devinfo->gen >= 9)
8319 gen9_ps_header_only_workaround(wm_prog_data);
8320
8321 assign_urb_setup();
8322
8323 fixup_3src_null_dest();
8324
8325 allocate_registers(allow_spilling);
8326
8327 if (failed)
8328 return false;
8329 }
8330
8331 return !failed;
8332 }
8333
8334 bool
8335 fs_visitor::run_cs(bool allow_spilling)
8336 {
8337 assert(stage == MESA_SHADER_COMPUTE);
8338
8339 setup_cs_payload();
8340
8341 if (shader_time_index >= 0)
8342 emit_shader_time_begin();
8343
8344 if (devinfo->is_haswell && prog_data->total_shared > 0) {
8345 /* Move SLM index from g0.0[27:24] to sr0.1[11:8] */
8346 const fs_builder abld = bld.exec_all().group(1, 0);
8347 abld.MOV(retype(brw_sr0_reg(1), BRW_REGISTER_TYPE_UW),
8348 suboffset(retype(brw_vec1_grf(0, 0), BRW_REGISTER_TYPE_UW), 1));
8349 }
8350
8351 emit_nir_code();
8352
8353 if (failed)
8354 return false;
8355
8356 emit_cs_terminate();
8357
8358 if (shader_time_index >= 0)
8359 emit_shader_time_end();
8360
8361 calculate_cfg();
8362
8363 optimize();
8364
8365 assign_curb_setup();
8366
8367 fixup_3src_null_dest();
8368 allocate_registers(allow_spilling);
8369
8370 if (failed)
8371 return false;
8372
8373 return !failed;
8374 }
8375
8376 static bool
8377 is_used_in_not_interp_frag_coord(nir_ssa_def *def)
8378 {
8379 nir_foreach_use(src, def) {
8380 if (src->parent_instr->type != nir_instr_type_intrinsic)
8381 return true;
8382
8383 nir_intrinsic_instr *intrin = nir_instr_as_intrinsic(src->parent_instr);
8384 if (intrin->intrinsic != nir_intrinsic_load_frag_coord)
8385 return true;
8386 }
8387
8388 nir_foreach_if_use(src, def)
8389 return true;
8390
8391 return false;
8392 }
8393
8394 /**
8395 * Return a bitfield where bit n is set if barycentric interpolation mode n
8396 * (see enum brw_barycentric_mode) is needed by the fragment shader.
8397 *
8398 * We examine the load_barycentric intrinsics rather than looking at input
8399 * variables so that we catch interpolateAtCentroid() messages too, which
8400 * also need the BRW_BARYCENTRIC_[NON]PERSPECTIVE_CENTROID mode set up.
8401 */
8402 static unsigned
8403 brw_compute_barycentric_interp_modes(const struct gen_device_info *devinfo,
8404 const nir_shader *shader)
8405 {
8406 unsigned barycentric_interp_modes = 0;
8407
8408 nir_foreach_function(f, shader) {
8409 if (!f->impl)
8410 continue;
8411
8412 nir_foreach_block(block, f->impl) {
8413 nir_foreach_instr(instr, block) {
8414 if (instr->type != nir_instr_type_intrinsic)
8415 continue;
8416
8417 nir_intrinsic_instr *intrin = nir_instr_as_intrinsic(instr);
8418 switch (intrin->intrinsic) {
8419 case nir_intrinsic_load_barycentric_pixel:
8420 case nir_intrinsic_load_barycentric_centroid:
8421 case nir_intrinsic_load_barycentric_sample:
8422 break;
8423 default:
8424 continue;
8425 }
8426
8427 /* Ignore WPOS; it doesn't require interpolation. */
8428 assert(intrin->dest.is_ssa);
8429 if (!is_used_in_not_interp_frag_coord(&intrin->dest.ssa))
8430 continue;
8431
8432 enum glsl_interp_mode interp = (enum glsl_interp_mode)
8433 nir_intrinsic_interp_mode(intrin);
8434 nir_intrinsic_op bary_op = intrin->intrinsic;
8435 enum brw_barycentric_mode bary =
8436 brw_barycentric_mode(interp, bary_op);
8437
8438 barycentric_interp_modes |= 1 << bary;
8439
8440 if (devinfo->needs_unlit_centroid_workaround &&
8441 bary_op == nir_intrinsic_load_barycentric_centroid)
8442 barycentric_interp_modes |= 1 << centroid_to_pixel(bary);
8443 }
8444 }
8445 }
8446
8447 return barycentric_interp_modes;
8448 }
8449
8450 static void
8451 brw_compute_flat_inputs(struct brw_wm_prog_data *prog_data,
8452 const nir_shader *shader)
8453 {
8454 prog_data->flat_inputs = 0;
8455
8456 nir_foreach_variable(var, &shader->inputs) {
8457 unsigned slots = glsl_count_attribute_slots(var->type, false);
8458 for (unsigned s = 0; s < slots; s++) {
8459 int input_index = prog_data->urb_setup[var->data.location + s];
8460
8461 if (input_index < 0)
8462 continue;
8463
8464 /* flat shading */
8465 if (var->data.interpolation == INTERP_MODE_FLAT)
8466 prog_data->flat_inputs |= 1 << input_index;
8467 }
8468 }
8469 }
8470
8471 static uint8_t
8472 computed_depth_mode(const nir_shader *shader)
8473 {
8474 if (shader->info.outputs_written & BITFIELD64_BIT(FRAG_RESULT_DEPTH)) {
8475 switch (shader->info.fs.depth_layout) {
8476 case FRAG_DEPTH_LAYOUT_NONE:
8477 case FRAG_DEPTH_LAYOUT_ANY:
8478 return BRW_PSCDEPTH_ON;
8479 case FRAG_DEPTH_LAYOUT_GREATER:
8480 return BRW_PSCDEPTH_ON_GE;
8481 case FRAG_DEPTH_LAYOUT_LESS:
8482 return BRW_PSCDEPTH_ON_LE;
8483 case FRAG_DEPTH_LAYOUT_UNCHANGED:
8484 return BRW_PSCDEPTH_OFF;
8485 }
8486 }
8487 return BRW_PSCDEPTH_OFF;
8488 }
8489
8490 /**
8491 * Move load_interpolated_input with simple (payload-based) barycentric modes
8492 * to the top of the program so we don't emit multiple PLNs for the same input.
8493 *
8494 * This works around CSE not being able to handle non-dominating cases
8495 * such as:
8496 *
8497 * if (...) {
8498 * interpolate input
8499 * } else {
8500 * interpolate the same exact input
8501 * }
8502 *
8503 * This should be replaced by global value numbering someday.
8504 */
8505 static bool
8506 move_interpolation_to_top(nir_shader *nir)
8507 {
8508 bool progress = false;
8509
8510 nir_foreach_function(f, nir) {
8511 if (!f->impl)
8512 continue;
8513
8514 nir_block *top = nir_start_block(f->impl);
8515 exec_node *cursor_node = NULL;
8516
8517 nir_foreach_block(block, f->impl) {
8518 if (block == top)
8519 continue;
8520
8521 nir_foreach_instr_safe(instr, block) {
8522 if (instr->type != nir_instr_type_intrinsic)
8523 continue;
8524
8525 nir_intrinsic_instr *intrin = nir_instr_as_intrinsic(instr);
8526 if (intrin->intrinsic != nir_intrinsic_load_interpolated_input)
8527 continue;
8528 nir_intrinsic_instr *bary_intrinsic =
8529 nir_instr_as_intrinsic(intrin->src[0].ssa->parent_instr);
8530 nir_intrinsic_op op = bary_intrinsic->intrinsic;
8531
8532 /* Leave interpolateAtSample/Offset() where they are. */
8533 if (op == nir_intrinsic_load_barycentric_at_sample ||
8534 op == nir_intrinsic_load_barycentric_at_offset)
8535 continue;
8536
8537 nir_instr *move[3] = {
8538 &bary_intrinsic->instr,
8539 intrin->src[1].ssa->parent_instr,
8540 instr
8541 };
8542
8543 for (unsigned i = 0; i < ARRAY_SIZE(move); i++) {
8544 if (move[i]->block != top) {
8545 move[i]->block = top;
8546 exec_node_remove(&move[i]->node);
8547 if (cursor_node) {
8548 exec_node_insert_after(cursor_node, &move[i]->node);
8549 } else {
8550 exec_list_push_head(&top->instr_list, &move[i]->node);
8551 }
8552 cursor_node = &move[i]->node;
8553 progress = true;
8554 }
8555 }
8556 }
8557 }
8558 nir_metadata_preserve(f->impl, (nir_metadata)
8559 ((unsigned) nir_metadata_block_index |
8560 (unsigned) nir_metadata_dominance));
8561 }
8562
8563 return progress;
8564 }
8565
8566 /**
8567 * Demote per-sample barycentric intrinsics to centroid.
8568 *
8569 * Useful when rendering to a non-multisampled buffer.
8570 */
8571 static bool
8572 demote_sample_qualifiers(nir_shader *nir)
8573 {
8574 bool progress = true;
8575
8576 nir_foreach_function(f, nir) {
8577 if (!f->impl)
8578 continue;
8579
8580 nir_builder b;
8581 nir_builder_init(&b, f->impl);
8582
8583 nir_foreach_block(block, f->impl) {
8584 nir_foreach_instr_safe(instr, block) {
8585 if (instr->type != nir_instr_type_intrinsic)
8586 continue;
8587
8588 nir_intrinsic_instr *intrin = nir_instr_as_intrinsic(instr);
8589 if (intrin->intrinsic != nir_intrinsic_load_barycentric_sample &&
8590 intrin->intrinsic != nir_intrinsic_load_barycentric_at_sample)
8591 continue;
8592
8593 b.cursor = nir_before_instr(instr);
8594 nir_ssa_def *centroid =
8595 nir_load_barycentric(&b, nir_intrinsic_load_barycentric_centroid,
8596 nir_intrinsic_interp_mode(intrin));
8597 nir_ssa_def_rewrite_uses(&intrin->dest.ssa,
8598 nir_src_for_ssa(centroid));
8599 nir_instr_remove(instr);
8600 progress = true;
8601 }
8602 }
8603
8604 nir_metadata_preserve(f->impl, (nir_metadata)
8605 ((unsigned) nir_metadata_block_index |
8606 (unsigned) nir_metadata_dominance));
8607 }
8608
8609 return progress;
8610 }
8611
8612 /**
8613 * Pre-gen6, the register file of the EUs was shared between threads,
8614 * and each thread used some subset allocated on a 16-register block
8615 * granularity. The unit states wanted these block counts.
8616 */
8617 static inline int
8618 brw_register_blocks(int reg_count)
8619 {
8620 return ALIGN(reg_count, 16) / 16 - 1;
8621 }
8622
8623 const unsigned *
8624 brw_compile_fs(const struct brw_compiler *compiler, void *log_data,
8625 void *mem_ctx,
8626 const struct brw_wm_prog_key *key,
8627 struct brw_wm_prog_data *prog_data,
8628 nir_shader *shader,
8629 int shader_time_index8, int shader_time_index16,
8630 int shader_time_index32, bool allow_spilling,
8631 bool use_rep_send, struct brw_vue_map *vue_map,
8632 struct brw_compile_stats *stats,
8633 char **error_str)
8634 {
8635 const struct gen_device_info *devinfo = compiler->devinfo;
8636 const unsigned max_subgroup_size = compiler->devinfo->gen >= 6 ? 32 : 16;
8637
8638 brw_nir_apply_key(shader, compiler, &key->base, max_subgroup_size, true);
8639 brw_nir_lower_fs_inputs(shader, devinfo, key);
8640 brw_nir_lower_fs_outputs(shader);
8641
8642 if (devinfo->gen < 6)
8643 brw_setup_vue_interpolation(vue_map, shader, prog_data);
8644
8645 /* From the SKL PRM, Volume 7, "Alpha Coverage":
8646 * "If Pixel Shader outputs oMask, AlphaToCoverage is disabled in
8647 * hardware, regardless of the state setting for this feature."
8648 */
8649 if (devinfo->gen > 6 && key->alpha_to_coverage) {
8650 /* Run constant fold optimization in order to get the correct source
8651 * offset to determine render target 0 store instruction in
8652 * emit_alpha_to_coverage pass.
8653 */
8654 NIR_PASS_V(shader, nir_opt_constant_folding);
8655 NIR_PASS_V(shader, brw_nir_lower_alpha_to_coverage);
8656 }
8657
8658 if (!key->multisample_fbo)
8659 NIR_PASS_V(shader, demote_sample_qualifiers);
8660 NIR_PASS_V(shader, move_interpolation_to_top);
8661 brw_postprocess_nir(shader, compiler, true);
8662
8663 /* key->alpha_test_func means simulating alpha testing via discards,
8664 * so the shader definitely kills pixels.
8665 */
8666 prog_data->uses_kill = shader->info.fs.uses_discard ||
8667 key->alpha_test_func;
8668 prog_data->uses_omask = key->multisample_fbo &&
8669 shader->info.outputs_written & BITFIELD64_BIT(FRAG_RESULT_SAMPLE_MASK);
8670 prog_data->computed_depth_mode = computed_depth_mode(shader);
8671 prog_data->computed_stencil =
8672 shader->info.outputs_written & BITFIELD64_BIT(FRAG_RESULT_STENCIL);
8673
8674 prog_data->persample_dispatch =
8675 key->multisample_fbo &&
8676 (key->persample_interp ||
8677 (shader->info.system_values_read & (SYSTEM_BIT_SAMPLE_ID |
8678 SYSTEM_BIT_SAMPLE_POS)) ||
8679 shader->info.fs.uses_sample_qualifier ||
8680 shader->info.outputs_read);
8681
8682 prog_data->has_render_target_reads = shader->info.outputs_read != 0ull;
8683
8684 prog_data->early_fragment_tests = shader->info.fs.early_fragment_tests;
8685 prog_data->post_depth_coverage = shader->info.fs.post_depth_coverage;
8686 prog_data->inner_coverage = shader->info.fs.inner_coverage;
8687
8688 prog_data->barycentric_interp_modes =
8689 brw_compute_barycentric_interp_modes(compiler->devinfo, shader);
8690
8691 calculate_urb_setup(devinfo, key, prog_data, shader);
8692 brw_compute_flat_inputs(prog_data, shader);
8693
8694 fs_visitor *v8 = NULL, *v16 = NULL, *v32 = NULL;
8695 cfg_t *simd8_cfg = NULL, *simd16_cfg = NULL, *simd32_cfg = NULL;
8696 float throughput = 0;
8697 bool has_spilled = false;
8698
8699 v8 = new fs_visitor(compiler, log_data, mem_ctx, &key->base,
8700 &prog_data->base, shader, 8, shader_time_index8);
8701 if (!v8->run_fs(allow_spilling, false /* do_rep_send */)) {
8702 if (error_str)
8703 *error_str = ralloc_strdup(mem_ctx, v8->fail_msg);
8704
8705 delete v8;
8706 return NULL;
8707 } else if (likely(!(INTEL_DEBUG & DEBUG_NO8))) {
8708 simd8_cfg = v8->cfg;
8709 prog_data->base.dispatch_grf_start_reg = v8->payload.num_regs;
8710 prog_data->reg_blocks_8 = brw_register_blocks(v8->grf_used);
8711 const performance &perf = v8->performance_analysis.require();
8712 throughput = MAX2(throughput, perf.throughput);
8713 has_spilled = v8->spilled_any_registers;
8714 allow_spilling = false;
8715 }
8716
8717 /* Limit dispatch width to simd8 with dual source blending on gen8.
8718 * See: https://gitlab.freedesktop.org/mesa/mesa/-/issues/1917
8719 */
8720 if (devinfo->gen == 8 && prog_data->dual_src_blend &&
8721 !(INTEL_DEBUG & DEBUG_NO8)) {
8722 assert(!use_rep_send);
8723 v8->limit_dispatch_width(8, "gen8 workaround: "
8724 "using SIMD8 when dual src blending.\n");
8725 }
8726
8727 if (!has_spilled &&
8728 v8->max_dispatch_width >= 16 &&
8729 likely(!(INTEL_DEBUG & DEBUG_NO16) || use_rep_send)) {
8730 /* Try a SIMD16 compile */
8731 v16 = new fs_visitor(compiler, log_data, mem_ctx, &key->base,
8732 &prog_data->base, shader, 16, shader_time_index16);
8733 v16->import_uniforms(v8);
8734 if (!v16->run_fs(allow_spilling, use_rep_send)) {
8735 compiler->shader_perf_log(log_data,
8736 "SIMD16 shader failed to compile: %s",
8737 v16->fail_msg);
8738 } else {
8739 simd16_cfg = v16->cfg;
8740 prog_data->dispatch_grf_start_reg_16 = v16->payload.num_regs;
8741 prog_data->reg_blocks_16 = brw_register_blocks(v16->grf_used);
8742 const performance &perf = v16->performance_analysis.require();
8743 throughput = MAX2(throughput, perf.throughput);
8744 has_spilled = v16->spilled_any_registers;
8745 allow_spilling = false;
8746 }
8747 }
8748
8749 /* Currently, the compiler only supports SIMD32 on SNB+ */
8750 if (!has_spilled &&
8751 v8->max_dispatch_width >= 32 && !use_rep_send &&
8752 devinfo->gen >= 6 && simd16_cfg &&
8753 !(INTEL_DEBUG & DEBUG_NO32)) {
8754 /* Try a SIMD32 compile */
8755 v32 = new fs_visitor(compiler, log_data, mem_ctx, &key->base,
8756 &prog_data->base, shader, 32, shader_time_index32);
8757 v32->import_uniforms(v8);
8758 if (!v32->run_fs(allow_spilling, false)) {
8759 compiler->shader_perf_log(log_data,
8760 "SIMD32 shader failed to compile: %s",
8761 v32->fail_msg);
8762 } else {
8763 const performance &perf = v32->performance_analysis.require();
8764
8765 if (!(INTEL_DEBUG & DEBUG_DO32) && throughput >= perf.throughput) {
8766 compiler->shader_perf_log(log_data, "SIMD32 shader inefficient\n");
8767 } else {
8768 simd32_cfg = v32->cfg;
8769 prog_data->dispatch_grf_start_reg_32 = v32->payload.num_regs;
8770 prog_data->reg_blocks_32 = brw_register_blocks(v32->grf_used);
8771 throughput = MAX2(throughput, perf.throughput);
8772 }
8773 }
8774 }
8775
8776 /* When the caller requests a repclear shader, they want SIMD16-only */
8777 if (use_rep_send)
8778 simd8_cfg = NULL;
8779
8780 /* Prior to Iron Lake, the PS had a single shader offset with a jump table
8781 * at the top to select the shader. We've never implemented that.
8782 * Instead, we just give them exactly one shader and we pick the widest one
8783 * available.
8784 */
8785 if (compiler->devinfo->gen < 5) {
8786 if (simd32_cfg || simd16_cfg)
8787 simd8_cfg = NULL;
8788 if (simd32_cfg)
8789 simd16_cfg = NULL;
8790 }
8791
8792 /* If computed depth is enabled SNB only allows SIMD8. */
8793 if (compiler->devinfo->gen == 6 &&
8794 prog_data->computed_depth_mode != BRW_PSCDEPTH_OFF)
8795 assert(simd16_cfg == NULL && simd32_cfg == NULL);
8796
8797 if (compiler->devinfo->gen <= 5 && !simd8_cfg) {
8798 /* Iron lake and earlier only have one Dispatch GRF start field. Make
8799 * the data available in the base prog data struct for convenience.
8800 */
8801 if (simd16_cfg) {
8802 prog_data->base.dispatch_grf_start_reg =
8803 prog_data->dispatch_grf_start_reg_16;
8804 } else if (simd32_cfg) {
8805 prog_data->base.dispatch_grf_start_reg =
8806 prog_data->dispatch_grf_start_reg_32;
8807 }
8808 }
8809
8810 if (prog_data->persample_dispatch) {
8811 /* Starting with SandyBridge (where we first get MSAA), the different
8812 * pixel dispatch combinations are grouped into classifications A
8813 * through F (SNB PRM Vol. 2 Part 1 Section 7.7.1). On most hardware
8814 * generations, the only configurations supporting persample dispatch
8815 * are those in which only one dispatch width is enabled.
8816 *
8817 * The Gen12 hardware spec has a similar dispatch grouping table, but
8818 * the following conflicting restriction applies (from the page on
8819 * "Structure_3DSTATE_PS_BODY"), so we need to keep the SIMD16 shader:
8820 *
8821 * "SIMD32 may only be enabled if SIMD16 or (dual)SIMD8 is also
8822 * enabled."
8823 */
8824 if (simd32_cfg || simd16_cfg)
8825 simd8_cfg = NULL;
8826 if (simd32_cfg && devinfo->gen < 12)
8827 simd16_cfg = NULL;
8828 }
8829
8830 fs_generator g(compiler, log_data, mem_ctx, &prog_data->base,
8831 v8->runtime_check_aads_emit, MESA_SHADER_FRAGMENT);
8832
8833 if (unlikely(INTEL_DEBUG & DEBUG_WM)) {
8834 g.enable_debug(ralloc_asprintf(mem_ctx, "%s fragment shader %s",
8835 shader->info.label ?
8836 shader->info.label : "unnamed",
8837 shader->info.name));
8838 }
8839
8840 if (simd8_cfg) {
8841 prog_data->dispatch_8 = true;
8842 g.generate_code(simd8_cfg, 8, v8->shader_stats,
8843 v8->performance_analysis.require(), stats);
8844 stats = stats ? stats + 1 : NULL;
8845 }
8846
8847 if (simd16_cfg) {
8848 prog_data->dispatch_16 = true;
8849 prog_data->prog_offset_16 = g.generate_code(
8850 simd16_cfg, 16, v16->shader_stats,
8851 v16->performance_analysis.require(), stats);
8852 stats = stats ? stats + 1 : NULL;
8853 }
8854
8855 if (simd32_cfg) {
8856 prog_data->dispatch_32 = true;
8857 prog_data->prog_offset_32 = g.generate_code(
8858 simd32_cfg, 32, v32->shader_stats,
8859 v32->performance_analysis.require(), stats);
8860 stats = stats ? stats + 1 : NULL;
8861 }
8862
8863 delete v8;
8864 delete v16;
8865 delete v32;
8866
8867 return g.get_assembly();
8868 }
8869
8870 fs_reg *
8871 fs_visitor::emit_cs_work_group_id_setup()
8872 {
8873 assert(stage == MESA_SHADER_COMPUTE);
8874
8875 fs_reg *reg = new(this->mem_ctx) fs_reg(vgrf(glsl_type::uvec3_type));
8876
8877 struct brw_reg r0_1(retype(brw_vec1_grf(0, 1), BRW_REGISTER_TYPE_UD));
8878 struct brw_reg r0_6(retype(brw_vec1_grf(0, 6), BRW_REGISTER_TYPE_UD));
8879 struct brw_reg r0_7(retype(brw_vec1_grf(0, 7), BRW_REGISTER_TYPE_UD));
8880
8881 bld.MOV(*reg, r0_1);
8882 bld.MOV(offset(*reg, bld, 1), r0_6);
8883 bld.MOV(offset(*reg, bld, 2), r0_7);
8884
8885 return reg;
8886 }
8887
8888 unsigned
8889 brw_cs_push_const_total_size(const struct brw_cs_prog_data *cs_prog_data,
8890 unsigned threads)
8891 {
8892 assert(cs_prog_data->push.per_thread.size % REG_SIZE == 0);
8893 assert(cs_prog_data->push.cross_thread.size % REG_SIZE == 0);
8894 return cs_prog_data->push.per_thread.size * threads +
8895 cs_prog_data->push.cross_thread.size;
8896 }
8897
8898 static void
8899 fill_push_const_block_info(struct brw_push_const_block *block, unsigned dwords)
8900 {
8901 block->dwords = dwords;
8902 block->regs = DIV_ROUND_UP(dwords, 8);
8903 block->size = block->regs * 32;
8904 }
8905
8906 static void
8907 cs_fill_push_const_info(const struct gen_device_info *devinfo,
8908 struct brw_cs_prog_data *cs_prog_data)
8909 {
8910 const struct brw_stage_prog_data *prog_data = &cs_prog_data->base;
8911 int subgroup_id_index = get_subgroup_id_param_index(prog_data);
8912 bool cross_thread_supported = devinfo->gen > 7 || devinfo->is_haswell;
8913
8914 /* The thread ID should be stored in the last param dword */
8915 assert(subgroup_id_index == -1 ||
8916 subgroup_id_index == (int)prog_data->nr_params - 1);
8917
8918 unsigned cross_thread_dwords, per_thread_dwords;
8919 if (!cross_thread_supported) {
8920 cross_thread_dwords = 0u;
8921 per_thread_dwords = prog_data->nr_params;
8922 } else if (subgroup_id_index >= 0) {
8923 /* Fill all but the last register with cross-thread payload */
8924 cross_thread_dwords = 8 * (subgroup_id_index / 8);
8925 per_thread_dwords = prog_data->nr_params - cross_thread_dwords;
8926 assert(per_thread_dwords > 0 && per_thread_dwords <= 8);
8927 } else {
8928 /* Fill all data using cross-thread payload */
8929 cross_thread_dwords = prog_data->nr_params;
8930 per_thread_dwords = 0u;
8931 }
8932
8933 fill_push_const_block_info(&cs_prog_data->push.cross_thread, cross_thread_dwords);
8934 fill_push_const_block_info(&cs_prog_data->push.per_thread, per_thread_dwords);
8935
8936 assert(cs_prog_data->push.cross_thread.dwords % 8 == 0 ||
8937 cs_prog_data->push.per_thread.size == 0);
8938 assert(cs_prog_data->push.cross_thread.dwords +
8939 cs_prog_data->push.per_thread.dwords ==
8940 prog_data->nr_params);
8941 }
8942
8943 static bool
8944 filter_simd(const nir_instr *instr, const void *_options)
8945 {
8946 if (instr->type != nir_instr_type_intrinsic)
8947 return false;
8948
8949 switch (nir_instr_as_intrinsic(instr)->intrinsic) {
8950 case nir_intrinsic_load_simd_width_intel:
8951 case nir_intrinsic_load_subgroup_id:
8952 return true;
8953
8954 default:
8955 return false;
8956 }
8957 }
8958
8959 static nir_ssa_def *
8960 lower_simd(nir_builder *b, nir_instr *instr, void *options)
8961 {
8962 uintptr_t simd_width = (uintptr_t)options;
8963
8964 switch (nir_instr_as_intrinsic(instr)->intrinsic) {
8965 case nir_intrinsic_load_simd_width_intel:
8966 return nir_imm_int(b, simd_width);
8967
8968 case nir_intrinsic_load_subgroup_id:
8969 /* If the whole workgroup fits in one thread, we can lower subgroup_id
8970 * to a constant zero.
8971 */
8972 if (!b->shader->info.cs.local_size_variable) {
8973 unsigned local_workgroup_size = b->shader->info.cs.local_size[0] *
8974 b->shader->info.cs.local_size[1] *
8975 b->shader->info.cs.local_size[2];
8976 if (local_workgroup_size <= simd_width)
8977 return nir_imm_int(b, 0);
8978 }
8979 return NULL;
8980
8981 default:
8982 return NULL;
8983 }
8984 }
8985
8986 static void
8987 brw_nir_lower_simd(nir_shader *nir, unsigned dispatch_width)
8988 {
8989 nir_shader_lower_instructions(nir, filter_simd, lower_simd,
8990 (void *)(uintptr_t)dispatch_width);
8991 }
8992
8993 static nir_shader *
8994 compile_cs_to_nir(const struct brw_compiler *compiler,
8995 void *mem_ctx,
8996 const struct brw_cs_prog_key *key,
8997 const nir_shader *src_shader,
8998 unsigned dispatch_width)
8999 {
9000 nir_shader *shader = nir_shader_clone(mem_ctx, src_shader);
9001 brw_nir_apply_key(shader, compiler, &key->base, dispatch_width, true);
9002
9003 NIR_PASS_V(shader, brw_nir_lower_simd, dispatch_width);
9004
9005 /* Clean up after the local index and ID calculations. */
9006 NIR_PASS_V(shader, nir_opt_constant_folding);
9007 NIR_PASS_V(shader, nir_opt_dce);
9008
9009 brw_postprocess_nir(shader, compiler, true);
9010
9011 return shader;
9012 }
9013
9014 const unsigned *
9015 brw_compile_cs(const struct brw_compiler *compiler, void *log_data,
9016 void *mem_ctx,
9017 const struct brw_cs_prog_key *key,
9018 struct brw_cs_prog_data *prog_data,
9019 const nir_shader *src_shader,
9020 int shader_time_index,
9021 struct brw_compile_stats *stats,
9022 char **error_str)
9023 {
9024 prog_data->base.total_shared = src_shader->info.cs.shared_size;
9025 prog_data->slm_size = src_shader->num_shared;
9026
9027 /* Generate code for all the possible SIMD variants. */
9028 bool generate_all;
9029
9030 unsigned min_dispatch_width;
9031 unsigned max_dispatch_width;
9032
9033 if (src_shader->info.cs.local_size_variable) {
9034 generate_all = true;
9035 min_dispatch_width = 8;
9036 max_dispatch_width = 32;
9037 } else {
9038 generate_all = false;
9039 prog_data->local_size[0] = src_shader->info.cs.local_size[0];
9040 prog_data->local_size[1] = src_shader->info.cs.local_size[1];
9041 prog_data->local_size[2] = src_shader->info.cs.local_size[2];
9042 unsigned local_workgroup_size = prog_data->local_size[0] *
9043 prog_data->local_size[1] *
9044 prog_data->local_size[2];
9045
9046 /* Limit max_threads to 64 for the GPGPU_WALKER command */
9047 const uint32_t max_threads = MIN2(64, compiler->devinfo->max_cs_threads);
9048 min_dispatch_width = util_next_power_of_two(
9049 MAX2(8, DIV_ROUND_UP(local_workgroup_size, max_threads)));
9050 assert(min_dispatch_width <= 32);
9051 max_dispatch_width = 32;
9052 }
9053
9054 if ((int)key->base.subgroup_size_type >= (int)BRW_SUBGROUP_SIZE_REQUIRE_8) {
9055 /* These enum values are expressly chosen to be equal to the subgroup
9056 * size that they require.
9057 */
9058 const unsigned required_dispatch_width =
9059 (unsigned)key->base.subgroup_size_type;
9060 assert(required_dispatch_width == 8 ||
9061 required_dispatch_width == 16 ||
9062 required_dispatch_width == 32);
9063 if (required_dispatch_width < min_dispatch_width ||
9064 required_dispatch_width > max_dispatch_width) {
9065 if (error_str) {
9066 *error_str = ralloc_strdup(mem_ctx,
9067 "Cannot satisfy explicit subgroup size");
9068 }
9069 return NULL;
9070 }
9071 min_dispatch_width = max_dispatch_width = required_dispatch_width;
9072 }
9073
9074 assert(min_dispatch_width <= max_dispatch_width);
9075
9076 fs_visitor *v8 = NULL, *v16 = NULL, *v32 = NULL;
9077 fs_visitor *v = NULL;
9078
9079 if (likely(!(INTEL_DEBUG & DEBUG_NO8)) &&
9080 min_dispatch_width <= 8 && max_dispatch_width >= 8) {
9081 nir_shader *nir8 = compile_cs_to_nir(compiler, mem_ctx, key,
9082 src_shader, 8);
9083 v8 = new fs_visitor(compiler, log_data, mem_ctx, &key->base,
9084 &prog_data->base,
9085 nir8, 8, shader_time_index);
9086 if (!v8->run_cs(true /* allow_spilling */)) {
9087 if (error_str)
9088 *error_str = ralloc_strdup(mem_ctx, v8->fail_msg);
9089 delete v8;
9090 return NULL;
9091 }
9092
9093 /* We should always be able to do SIMD32 for compute shaders */
9094 assert(v8->max_dispatch_width >= 32);
9095
9096 v = v8;
9097 prog_data->prog_mask |= 1 << 0;
9098 if (v8->spilled_any_registers)
9099 prog_data->prog_spilled |= 1 << 0;
9100 cs_fill_push_const_info(compiler->devinfo, prog_data);
9101 }
9102
9103 if (likely(!(INTEL_DEBUG & DEBUG_NO16)) &&
9104 (generate_all || !prog_data->prog_spilled) &&
9105 min_dispatch_width <= 16 && max_dispatch_width >= 16) {
9106 /* Try a SIMD16 compile */
9107 nir_shader *nir16 = compile_cs_to_nir(compiler, mem_ctx, key,
9108 src_shader, 16);
9109 v16 = new fs_visitor(compiler, log_data, mem_ctx, &key->base,
9110 &prog_data->base,
9111 nir16, 16, shader_time_index);
9112 if (v8)
9113 v16->import_uniforms(v8);
9114
9115 const bool allow_spilling = generate_all || v == NULL;
9116 if (!v16->run_cs(allow_spilling)) {
9117 compiler->shader_perf_log(log_data,
9118 "SIMD16 shader failed to compile: %s",
9119 v16->fail_msg);
9120 if (!v) {
9121 assert(v8 == NULL);
9122 if (error_str) {
9123 *error_str = ralloc_asprintf(
9124 mem_ctx, "Not enough threads for SIMD8 and "
9125 "couldn't generate SIMD16: %s", v16->fail_msg);
9126 }
9127 delete v16;
9128 return NULL;
9129 }
9130 } else {
9131 /* We should always be able to do SIMD32 for compute shaders */
9132 assert(v16->max_dispatch_width >= 32);
9133
9134 v = v16;
9135 prog_data->prog_mask |= 1 << 1;
9136 if (v16->spilled_any_registers)
9137 prog_data->prog_spilled |= 1 << 1;
9138 cs_fill_push_const_info(compiler->devinfo, prog_data);
9139 }
9140 }
9141
9142 /* The SIMD32 is only enabled for cases it is needed unless forced.
9143 *
9144 * TODO: Use performance_analysis and drop this boolean.
9145 */
9146 const bool needs_32 = min_dispatch_width > 16 ||
9147 (INTEL_DEBUG & DEBUG_DO32) ||
9148 generate_all;
9149
9150 if (likely(!(INTEL_DEBUG & DEBUG_NO32)) &&
9151 (generate_all || !prog_data->prog_spilled) &&
9152 needs_32 &&
9153 min_dispatch_width <= 32 && max_dispatch_width >= 32) {
9154 /* Try a SIMD32 compile */
9155 nir_shader *nir32 = compile_cs_to_nir(compiler, mem_ctx, key,
9156 src_shader, 32);
9157 v32 = new fs_visitor(compiler, log_data, mem_ctx, &key->base,
9158 &prog_data->base,
9159 nir32, 32, shader_time_index);
9160 if (v8)
9161 v32->import_uniforms(v8);
9162 else if (v16)
9163 v32->import_uniforms(v16);
9164
9165 const bool allow_spilling = generate_all || v == NULL;
9166 if (!v32->run_cs(allow_spilling)) {
9167 compiler->shader_perf_log(log_data,
9168 "SIMD32 shader failed to compile: %s",
9169 v32->fail_msg);
9170 if (!v) {
9171 assert(v8 == NULL);
9172 assert(v16 == NULL);
9173 if (error_str) {
9174 *error_str = ralloc_asprintf(
9175 mem_ctx, "Not enough threads for SIMD16 and "
9176 "couldn't generate SIMD32: %s", v32->fail_msg);
9177 }
9178 delete v32;
9179 return NULL;
9180 }
9181 } else {
9182 v = v32;
9183 prog_data->prog_mask |= 1 << 2;
9184 if (v32->spilled_any_registers)
9185 prog_data->prog_spilled |= 1 << 2;
9186 cs_fill_push_const_info(compiler->devinfo, prog_data);
9187 }
9188 }
9189
9190 if (unlikely(!v && (INTEL_DEBUG & (DEBUG_NO8 | DEBUG_NO16 | DEBUG_NO32)))) {
9191 if (error_str) {
9192 *error_str =
9193 ralloc_strdup(mem_ctx,
9194 "Cannot satisfy INTEL_DEBUG flags SIMD restrictions");
9195 }
9196 return NULL;
9197 }
9198
9199 assert(v);
9200
9201 const unsigned *ret = NULL;
9202
9203 fs_generator g(compiler, log_data, mem_ctx, &prog_data->base,
9204 v->runtime_check_aads_emit, MESA_SHADER_COMPUTE);
9205 if (INTEL_DEBUG & DEBUG_CS) {
9206 char *name = ralloc_asprintf(mem_ctx, "%s compute shader %s",
9207 src_shader->info.label ?
9208 src_shader->info.label : "unnamed",
9209 src_shader->info.name);
9210 g.enable_debug(name);
9211 }
9212
9213 if (generate_all) {
9214 if (prog_data->prog_mask & (1 << 0)) {
9215 assert(v8);
9216 prog_data->prog_offset[0] =
9217 g.generate_code(v8->cfg, 8, v8->shader_stats,
9218 v8->performance_analysis.require(), stats);
9219 stats = stats ? stats + 1 : NULL;
9220 }
9221
9222 if (prog_data->prog_mask & (1 << 1)) {
9223 assert(v16);
9224 prog_data->prog_offset[1] =
9225 g.generate_code(v16->cfg, 16, v16->shader_stats,
9226 v16->performance_analysis.require(), stats);
9227 stats = stats ? stats + 1 : NULL;
9228 }
9229
9230 if (prog_data->prog_mask & (1 << 2)) {
9231 assert(v32);
9232 prog_data->prog_offset[2] =
9233 g.generate_code(v32->cfg, 32, v32->shader_stats,
9234 v32->performance_analysis.require(), stats);
9235 stats = stats ? stats + 1 : NULL;
9236 }
9237 } else {
9238 /* Only one dispatch width will be valid, and will be at offset 0,
9239 * which is already the default value of prog_offset_* fields.
9240 */
9241 prog_data->prog_mask = 1 << (v->dispatch_width / 16);
9242 g.generate_code(v->cfg, v->dispatch_width, v->shader_stats,
9243 v->performance_analysis.require(), stats);
9244 }
9245
9246 ret = g.get_assembly();
9247
9248 delete v8;
9249 delete v16;
9250 delete v32;
9251
9252 return ret;
9253 }
9254
9255 unsigned
9256 brw_cs_simd_size_for_group_size(const struct gen_device_info *devinfo,
9257 const struct brw_cs_prog_data *cs_prog_data,
9258 unsigned group_size)
9259 {
9260 const unsigned mask = cs_prog_data->prog_mask;
9261 assert(mask != 0);
9262
9263 static const unsigned simd8 = 1 << 0;
9264 static const unsigned simd16 = 1 << 1;
9265 static const unsigned simd32 = 1 << 2;
9266
9267 if (unlikely(INTEL_DEBUG & DEBUG_DO32) && (mask & simd32))
9268 return 32;
9269
9270 /* Limit max_threads to 64 for the GPGPU_WALKER command */
9271 const uint32_t max_threads = MIN2(64, devinfo->max_cs_threads);
9272
9273 if ((mask & simd8) && group_size <= 8 * max_threads) {
9274 /* Prefer SIMD16 if can do without spilling. Matches logic in
9275 * brw_compile_cs.
9276 */
9277 if ((mask & simd16) && (~cs_prog_data->prog_spilled & simd16))
9278 return 16;
9279 return 8;
9280 }
9281
9282 if ((mask & simd16) && group_size <= 16 * max_threads)
9283 return 16;
9284
9285 assert(mask & simd32);
9286 assert(group_size <= 32 * max_threads);
9287 return 32;
9288 }
9289
9290 /**
9291 * Test the dispatch mask packing assumptions of
9292 * brw_stage_has_packed_dispatch(). Call this from e.g. the top of
9293 * fs_visitor::emit_nir_code() to cause a GPU hang if any shader invocation is
9294 * executed with an unexpected dispatch mask.
9295 */
9296 static UNUSED void
9297 brw_fs_test_dispatch_packing(const fs_builder &bld)
9298 {
9299 const gl_shader_stage stage = bld.shader->stage;
9300
9301 if (brw_stage_has_packed_dispatch(bld.shader->devinfo, stage,
9302 bld.shader->stage_prog_data)) {
9303 const fs_builder ubld = bld.exec_all().group(1, 0);
9304 const fs_reg tmp = component(bld.vgrf(BRW_REGISTER_TYPE_UD), 0);
9305 const fs_reg mask = (stage == MESA_SHADER_FRAGMENT ? brw_vmask_reg() :
9306 brw_dmask_reg());
9307
9308 ubld.ADD(tmp, mask, brw_imm_ud(1));
9309 ubld.AND(tmp, mask, tmp);
9310
9311 /* This will loop forever if the dispatch mask doesn't have the expected
9312 * form '2^n-1', in which case tmp will be non-zero.
9313 */
9314 bld.emit(BRW_OPCODE_DO);
9315 bld.CMP(bld.null_reg_ud(), tmp, brw_imm_ud(0), BRW_CONDITIONAL_NZ);
9316 set_predicate(BRW_PREDICATE_NORMAL, bld.emit(BRW_OPCODE_WHILE));
9317 }
9318 }
9319
9320 unsigned
9321 fs_visitor::workgroup_size() const
9322 {
9323 assert(stage == MESA_SHADER_COMPUTE);
9324 const struct brw_cs_prog_data *cs = brw_cs_prog_data(prog_data);
9325 return cs->local_size[0] * cs->local_size[1] * cs->local_size[2];
9326 }