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