e9c990d430866149e283a4ebdab103c5cc6a9196
[mesa.git] / src / mesa / drivers / dri / i965 / 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 <sys/types.h>
32
33 #include "util/hash_table.h"
34 #include "main/macros.h"
35 #include "main/shaderobj.h"
36 #include "main/fbobject.h"
37 #include "program/prog_parameter.h"
38 #include "program/prog_print.h"
39 #include "util/register_allocate.h"
40 #include "program/hash_table.h"
41 #include "brw_context.h"
42 #include "brw_eu.h"
43 #include "brw_wm.h"
44 #include "brw_fs.h"
45 #include "brw_cs.h"
46 #include "brw_vec4_gs_visitor.h"
47 #include "brw_cfg.h"
48 #include "brw_dead_control_flow.h"
49 #include "main/uniforms.h"
50 #include "brw_fs_live_variables.h"
51 #include "glsl/nir/glsl_types.h"
52 #include "program/sampler.h"
53
54 using namespace brw;
55
56 void
57 fs_inst::init(enum opcode opcode, uint8_t exec_size, const fs_reg &dst,
58 const fs_reg *src, unsigned sources)
59 {
60 memset(this, 0, sizeof(*this));
61
62 this->src = new fs_reg[MAX2(sources, 3)];
63 for (unsigned i = 0; i < sources; i++)
64 this->src[i] = src[i];
65
66 this->opcode = opcode;
67 this->dst = dst;
68 this->sources = sources;
69 this->exec_size = exec_size;
70
71 assert(dst.file != IMM && dst.file != UNIFORM);
72
73 assert(this->exec_size != 0);
74
75 this->conditional_mod = BRW_CONDITIONAL_NONE;
76
77 /* This will be the case for almost all instructions. */
78 switch (dst.file) {
79 case VGRF:
80 case ARF:
81 case FIXED_GRF:
82 case MRF:
83 case ATTR:
84 this->regs_written = DIV_ROUND_UP(dst.component_size(exec_size),
85 REG_SIZE);
86 break;
87 case BAD_FILE:
88 this->regs_written = 0;
89 break;
90 case IMM:
91 case UNIFORM:
92 unreachable("Invalid destination register file");
93 }
94
95 this->writes_accumulator = false;
96 }
97
98 fs_inst::fs_inst()
99 {
100 init(BRW_OPCODE_NOP, 8, dst, NULL, 0);
101 }
102
103 fs_inst::fs_inst(enum opcode opcode, uint8_t exec_size)
104 {
105 init(opcode, exec_size, reg_undef, NULL, 0);
106 }
107
108 fs_inst::fs_inst(enum opcode opcode, uint8_t exec_size, const fs_reg &dst)
109 {
110 init(opcode, exec_size, dst, NULL, 0);
111 }
112
113 fs_inst::fs_inst(enum opcode opcode, uint8_t exec_size, const fs_reg &dst,
114 const fs_reg &src0)
115 {
116 const fs_reg src[1] = { src0 };
117 init(opcode, exec_size, dst, src, 1);
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)
122 {
123 const fs_reg src[2] = { src0, src1 };
124 init(opcode, exec_size, dst, src, 2);
125 }
126
127 fs_inst::fs_inst(enum opcode opcode, uint8_t exec_size, const fs_reg &dst,
128 const fs_reg &src0, const fs_reg &src1, const fs_reg &src2)
129 {
130 const fs_reg src[3] = { src0, src1, src2 };
131 init(opcode, exec_size, dst, src, 3);
132 }
133
134 fs_inst::fs_inst(enum opcode opcode, uint8_t exec_width, const fs_reg &dst,
135 const fs_reg src[], unsigned sources)
136 {
137 init(opcode, exec_width, dst, src, sources);
138 }
139
140 fs_inst::fs_inst(const fs_inst &that)
141 {
142 memcpy(this, &that, sizeof(that));
143
144 this->src = new fs_reg[MAX2(that.sources, 3)];
145
146 for (unsigned i = 0; i < that.sources; i++)
147 this->src[i] = that.src[i];
148 }
149
150 fs_inst::~fs_inst()
151 {
152 delete[] this->src;
153 }
154
155 void
156 fs_inst::resize_sources(uint8_t num_sources)
157 {
158 if (this->sources != num_sources) {
159 fs_reg *src = new fs_reg[MAX2(num_sources, 3)];
160
161 for (unsigned i = 0; i < MIN2(this->sources, num_sources); ++i)
162 src[i] = this->src[i];
163
164 delete[] this->src;
165 this->src = src;
166 this->sources = num_sources;
167 }
168 }
169
170 void
171 fs_visitor::VARYING_PULL_CONSTANT_LOAD(const fs_builder &bld,
172 const fs_reg &dst,
173 const fs_reg &surf_index,
174 const fs_reg &varying_offset,
175 uint32_t const_offset)
176 {
177 /* We have our constant surface use a pitch of 4 bytes, so our index can
178 * be any component of a vector, and then we load 4 contiguous
179 * components starting from that.
180 *
181 * We break down the const_offset to a portion added to the variable
182 * offset and a portion done using reg_offset, which means that if you
183 * have GLSL using something like "uniform vec4 a[20]; gl_FragColor =
184 * a[i]", we'll temporarily generate 4 vec4 loads from offset i * 4, and
185 * CSE can later notice that those loads are all the same and eliminate
186 * the redundant ones.
187 */
188 fs_reg vec4_offset = vgrf(glsl_type::int_type);
189 bld.ADD(vec4_offset, varying_offset, brw_imm_ud(const_offset & ~3));
190
191 int scale = 1;
192 if (devinfo->gen == 4 && bld.dispatch_width() == 8) {
193 /* Pre-gen5, we can either use a SIMD8 message that requires (header,
194 * u, v, r) as parameters, or we can just use the SIMD16 message
195 * consisting of (header, u). We choose the second, at the cost of a
196 * longer return length.
197 */
198 scale = 2;
199 }
200
201 enum opcode op;
202 if (devinfo->gen >= 7)
203 op = FS_OPCODE_VARYING_PULL_CONSTANT_LOAD_GEN7;
204 else
205 op = FS_OPCODE_VARYING_PULL_CONSTANT_LOAD;
206
207 int regs_written = 4 * (bld.dispatch_width() / 8) * scale;
208 fs_reg vec4_result = fs_reg(VGRF, alloc.allocate(regs_written), dst.type);
209 fs_inst *inst = bld.emit(op, vec4_result, surf_index, vec4_offset);
210 inst->regs_written = regs_written;
211
212 if (devinfo->gen < 7) {
213 inst->base_mrf = FIRST_PULL_LOAD_MRF(devinfo->gen);
214 inst->header_size = 1;
215 if (devinfo->gen == 4)
216 inst->mlen = 3;
217 else
218 inst->mlen = 1 + bld.dispatch_width() / 8;
219 }
220
221 bld.MOV(dst, offset(vec4_result, bld, (const_offset & 3) * scale));
222 }
223
224 /**
225 * A helper for MOV generation for fixing up broken hardware SEND dependency
226 * handling.
227 */
228 void
229 fs_visitor::DEP_RESOLVE_MOV(const fs_builder &bld, int grf)
230 {
231 /* The caller always wants uncompressed to emit the minimal extra
232 * dependencies, and to avoid having to deal with aligning its regs to 2.
233 */
234 const fs_builder ubld = bld.annotate("send dependency resolve")
235 .half(0);
236
237 ubld.MOV(ubld.null_reg_f(), fs_reg(VGRF, grf, BRW_REGISTER_TYPE_F));
238 }
239
240 bool
241 fs_inst::equals(fs_inst *inst) const
242 {
243 return (opcode == inst->opcode &&
244 dst.equals(inst->dst) &&
245 src[0].equals(inst->src[0]) &&
246 src[1].equals(inst->src[1]) &&
247 src[2].equals(inst->src[2]) &&
248 saturate == inst->saturate &&
249 predicate == inst->predicate &&
250 conditional_mod == inst->conditional_mod &&
251 mlen == inst->mlen &&
252 base_mrf == inst->base_mrf &&
253 target == inst->target &&
254 eot == inst->eot &&
255 header_size == inst->header_size &&
256 shadow_compare == inst->shadow_compare &&
257 exec_size == inst->exec_size &&
258 offset == inst->offset);
259 }
260
261 bool
262 fs_inst::overwrites_reg(const fs_reg &reg) const
263 {
264 return reg.in_range(dst, regs_written);
265 }
266
267 bool
268 fs_inst::is_send_from_grf() const
269 {
270 switch (opcode) {
271 case FS_OPCODE_VARYING_PULL_CONSTANT_LOAD_GEN7:
272 case SHADER_OPCODE_SHADER_TIME_ADD:
273 case FS_OPCODE_INTERPOLATE_AT_CENTROID:
274 case FS_OPCODE_INTERPOLATE_AT_SAMPLE:
275 case FS_OPCODE_INTERPOLATE_AT_SHARED_OFFSET:
276 case FS_OPCODE_INTERPOLATE_AT_PER_SLOT_OFFSET:
277 case SHADER_OPCODE_UNTYPED_ATOMIC:
278 case SHADER_OPCODE_UNTYPED_SURFACE_READ:
279 case SHADER_OPCODE_UNTYPED_SURFACE_WRITE:
280 case SHADER_OPCODE_TYPED_ATOMIC:
281 case SHADER_OPCODE_TYPED_SURFACE_READ:
282 case SHADER_OPCODE_TYPED_SURFACE_WRITE:
283 case SHADER_OPCODE_URB_WRITE_SIMD8:
284 case SHADER_OPCODE_URB_WRITE_SIMD8_PER_SLOT:
285 case SHADER_OPCODE_URB_WRITE_SIMD8_MASKED:
286 case SHADER_OPCODE_URB_WRITE_SIMD8_MASKED_PER_SLOT:
287 case SHADER_OPCODE_URB_READ_SIMD8:
288 case SHADER_OPCODE_URB_READ_SIMD8_PER_SLOT:
289 return true;
290 case FS_OPCODE_UNIFORM_PULL_CONSTANT_LOAD:
291 return src[1].file == VGRF;
292 case FS_OPCODE_FB_WRITE:
293 return src[0].file == VGRF;
294 default:
295 if (is_tex())
296 return src[0].file == VGRF;
297
298 return false;
299 }
300 }
301
302 bool
303 fs_inst::is_copy_payload(const brw::simple_allocator &grf_alloc) const
304 {
305 if (this->opcode != SHADER_OPCODE_LOAD_PAYLOAD)
306 return false;
307
308 fs_reg reg = this->src[0];
309 if (reg.file != VGRF || reg.reg_offset != 0 || reg.stride == 0)
310 return false;
311
312 if (grf_alloc.sizes[reg.nr] != this->regs_written)
313 return false;
314
315 for (int i = 0; i < this->sources; i++) {
316 reg.type = this->src[i].type;
317 if (!this->src[i].equals(reg))
318 return false;
319
320 if (i < this->header_size) {
321 reg.reg_offset += 1;
322 } else {
323 reg.reg_offset += this->exec_size / 8;
324 }
325 }
326
327 return true;
328 }
329
330 bool
331 fs_inst::can_do_source_mods(const struct brw_device_info *devinfo)
332 {
333 if (devinfo->gen == 6 && is_math())
334 return false;
335
336 if (is_send_from_grf())
337 return false;
338
339 if (!backend_instruction::can_do_source_mods())
340 return false;
341
342 return true;
343 }
344
345 bool
346 fs_inst::can_change_types() const
347 {
348 return dst.type == src[0].type &&
349 !src[0].abs && !src[0].negate && !saturate &&
350 (opcode == BRW_OPCODE_MOV ||
351 (opcode == BRW_OPCODE_SEL &&
352 dst.type == src[1].type &&
353 predicate != BRW_PREDICATE_NONE &&
354 !src[1].abs && !src[1].negate));
355 }
356
357 bool
358 fs_inst::has_side_effects() const
359 {
360 return this->eot || backend_instruction::has_side_effects();
361 }
362
363 void
364 fs_reg::init()
365 {
366 memset(this, 0, sizeof(*this));
367 stride = 1;
368 }
369
370 /** Generic unset register constructor. */
371 fs_reg::fs_reg()
372 {
373 init();
374 this->file = BAD_FILE;
375 }
376
377 fs_reg::fs_reg(struct brw_reg reg) :
378 backend_reg(reg)
379 {
380 this->reg_offset = 0;
381 this->subreg_offset = 0;
382 this->reladdr = NULL;
383 this->stride = 1;
384 if (this->file == IMM &&
385 (this->type != BRW_REGISTER_TYPE_V &&
386 this->type != BRW_REGISTER_TYPE_UV &&
387 this->type != BRW_REGISTER_TYPE_VF)) {
388 this->stride = 0;
389 }
390 }
391
392 bool
393 fs_reg::equals(const fs_reg &r) const
394 {
395 return (memcmp((brw_reg *)this, (brw_reg *)&r, sizeof(brw_reg)) == 0 &&
396 reg_offset == r.reg_offset &&
397 subreg_offset == r.subreg_offset &&
398 !reladdr && !r.reladdr &&
399 stride == r.stride);
400 }
401
402 fs_reg &
403 fs_reg::set_smear(unsigned subreg)
404 {
405 assert(file != ARF && file != FIXED_GRF && file != IMM);
406 subreg_offset = subreg * type_sz(type);
407 stride = 0;
408 return *this;
409 }
410
411 bool
412 fs_reg::is_contiguous() const
413 {
414 return stride == 1;
415 }
416
417 unsigned
418 fs_reg::component_size(unsigned width) const
419 {
420 const unsigned stride = ((file != ARF && file != FIXED_GRF) ? this->stride :
421 hstride == 0 ? 0 :
422 1 << (hstride - 1));
423 return MAX2(width * stride, 1) * type_sz(type);
424 }
425
426 extern "C" int
427 type_size_scalar(const struct glsl_type *type)
428 {
429 unsigned int size, i;
430
431 switch (type->base_type) {
432 case GLSL_TYPE_UINT:
433 case GLSL_TYPE_INT:
434 case GLSL_TYPE_FLOAT:
435 case GLSL_TYPE_BOOL:
436 return type->components();
437 case GLSL_TYPE_ARRAY:
438 return type_size_scalar(type->fields.array) * type->length;
439 case GLSL_TYPE_STRUCT:
440 size = 0;
441 for (i = 0; i < type->length; i++) {
442 size += type_size_scalar(type->fields.structure[i].type);
443 }
444 return size;
445 case GLSL_TYPE_SAMPLER:
446 /* Samplers take up no register space, since they're baked in at
447 * link time.
448 */
449 return 0;
450 case GLSL_TYPE_ATOMIC_UINT:
451 return 0;
452 case GLSL_TYPE_SUBROUTINE:
453 return 1;
454 case GLSL_TYPE_IMAGE:
455 return BRW_IMAGE_PARAM_SIZE;
456 case GLSL_TYPE_VOID:
457 case GLSL_TYPE_ERROR:
458 case GLSL_TYPE_INTERFACE:
459 case GLSL_TYPE_DOUBLE:
460 unreachable("not reached");
461 }
462
463 return 0;
464 }
465
466 /**
467 * Returns the number of scalar components needed to store type, assuming
468 * that vectors are padded out to vec4.
469 *
470 * This has the packing rules of type_size_vec4(), but counts components
471 * similar to type_size_scalar().
472 */
473 extern "C" int
474 type_size_vec4_times_4(const struct glsl_type *type)
475 {
476 return 4 * type_size_vec4(type);
477 }
478
479 /**
480 * Create a MOV to read the timestamp register.
481 *
482 * The caller is responsible for emitting the MOV. The return value is
483 * the destination of the MOV, with extra parameters set.
484 */
485 fs_reg
486 fs_visitor::get_timestamp(const fs_builder &bld)
487 {
488 assert(devinfo->gen >= 7);
489
490 fs_reg ts = fs_reg(retype(brw_vec4_reg(BRW_ARCHITECTURE_REGISTER_FILE,
491 BRW_ARF_TIMESTAMP,
492 0),
493 BRW_REGISTER_TYPE_UD));
494
495 fs_reg dst = fs_reg(VGRF, alloc.allocate(1), BRW_REGISTER_TYPE_UD);
496
497 /* We want to read the 3 fields we care about even if it's not enabled in
498 * the dispatch.
499 */
500 bld.group(4, 0).exec_all().MOV(dst, ts);
501
502 return dst;
503 }
504
505 void
506 fs_visitor::emit_shader_time_begin()
507 {
508 shader_start_time = get_timestamp(bld.annotate("shader time start"));
509
510 /* We want only the low 32 bits of the timestamp. Since it's running
511 * at the GPU clock rate of ~1.2ghz, it will roll over every ~3 seconds,
512 * which is plenty of time for our purposes. It is identical across the
513 * EUs, but since it's tracking GPU core speed it will increment at a
514 * varying rate as render P-states change.
515 */
516 shader_start_time.set_smear(0);
517 }
518
519 void
520 fs_visitor::emit_shader_time_end()
521 {
522 /* Insert our code just before the final SEND with EOT. */
523 exec_node *end = this->instructions.get_tail();
524 assert(end && ((fs_inst *) end)->eot);
525 const fs_builder ibld = bld.annotate("shader time end")
526 .exec_all().at(NULL, end);
527
528 fs_reg shader_end_time = get_timestamp(ibld);
529
530 /* We only use the low 32 bits of the timestamp - see
531 * emit_shader_time_begin()).
532 *
533 * We could also check if render P-states have changed (or anything
534 * else that might disrupt timing) by setting smear to 2 and checking if
535 * that field is != 0.
536 */
537 shader_end_time.set_smear(0);
538
539 /* Check that there weren't any timestamp reset events (assuming these
540 * were the only two timestamp reads that happened).
541 */
542 fs_reg reset = shader_end_time;
543 reset.set_smear(2);
544 set_condmod(BRW_CONDITIONAL_Z,
545 ibld.AND(ibld.null_reg_ud(), reset, brw_imm_ud(1u)));
546 ibld.IF(BRW_PREDICATE_NORMAL);
547
548 fs_reg start = shader_start_time;
549 start.negate = true;
550 fs_reg diff = fs_reg(VGRF, alloc.allocate(1), BRW_REGISTER_TYPE_UD);
551 diff.set_smear(0);
552
553 const fs_builder cbld = ibld.group(1, 0);
554 cbld.group(1, 0).ADD(diff, start, shader_end_time);
555
556 /* If there were no instructions between the two timestamp gets, the diff
557 * is 2 cycles. Remove that overhead, so I can forget about that when
558 * trying to determine the time taken for single instructions.
559 */
560 cbld.ADD(diff, diff, brw_imm_ud(-2u));
561 SHADER_TIME_ADD(cbld, 0, diff);
562 SHADER_TIME_ADD(cbld, 1, brw_imm_ud(1u));
563 ibld.emit(BRW_OPCODE_ELSE);
564 SHADER_TIME_ADD(cbld, 2, brw_imm_ud(1u));
565 ibld.emit(BRW_OPCODE_ENDIF);
566 }
567
568 void
569 fs_visitor::SHADER_TIME_ADD(const fs_builder &bld,
570 int shader_time_subindex,
571 fs_reg value)
572 {
573 int index = shader_time_index * 3 + shader_time_subindex;
574 struct brw_reg offset = brw_imm_d(index * SHADER_TIME_STRIDE);
575
576 fs_reg payload;
577 if (dispatch_width == 8)
578 payload = vgrf(glsl_type::uvec2_type);
579 else
580 payload = vgrf(glsl_type::uint_type);
581
582 bld.emit(SHADER_OPCODE_SHADER_TIME_ADD, fs_reg(), payload, offset, value);
583 }
584
585 void
586 fs_visitor::vfail(const char *format, va_list va)
587 {
588 char *msg;
589
590 if (failed)
591 return;
592
593 failed = true;
594
595 msg = ralloc_vasprintf(mem_ctx, format, va);
596 msg = ralloc_asprintf(mem_ctx, "%s compile failed: %s\n", stage_abbrev, msg);
597
598 this->fail_msg = msg;
599
600 if (debug_enabled) {
601 fprintf(stderr, "%s", msg);
602 }
603 }
604
605 void
606 fs_visitor::fail(const char *format, ...)
607 {
608 va_list va;
609
610 va_start(va, format);
611 vfail(format, va);
612 va_end(va);
613 }
614
615 /**
616 * Mark this program as impossible to compile in SIMD16 mode.
617 *
618 * During the SIMD8 compile (which happens first), we can detect and flag
619 * things that are unsupported in SIMD16 mode, so the compiler can skip
620 * the SIMD16 compile altogether.
621 *
622 * During a SIMD16 compile (if one happens anyway), this just calls fail().
623 */
624 void
625 fs_visitor::no16(const char *msg)
626 {
627 if (dispatch_width == 16) {
628 fail("%s", msg);
629 } else {
630 simd16_unsupported = true;
631
632 compiler->shader_perf_log(log_data,
633 "SIMD16 shader failed to compile: %s", msg);
634 }
635 }
636
637 /**
638 * Returns true if the instruction has a flag that means it won't
639 * update an entire destination register.
640 *
641 * For example, dead code elimination and live variable analysis want to know
642 * when a write to a variable screens off any preceding values that were in
643 * it.
644 */
645 bool
646 fs_inst::is_partial_write() const
647 {
648 return ((this->predicate && this->opcode != BRW_OPCODE_SEL) ||
649 (this->exec_size * type_sz(this->dst.type)) < 32 ||
650 !this->dst.is_contiguous());
651 }
652
653 unsigned
654 fs_inst::components_read(unsigned i) const
655 {
656 switch (opcode) {
657 case FS_OPCODE_LINTERP:
658 if (i == 0)
659 return 2;
660 else
661 return 1;
662
663 case FS_OPCODE_PIXEL_X:
664 case FS_OPCODE_PIXEL_Y:
665 assert(i == 0);
666 return 2;
667
668 case FS_OPCODE_FB_WRITE_LOGICAL:
669 assert(src[FB_WRITE_LOGICAL_SRC_COMPONENTS].file == IMM);
670 /* First/second FB write color. */
671 if (i < 2)
672 return src[FB_WRITE_LOGICAL_SRC_COMPONENTS].ud;
673 else
674 return 1;
675
676 case SHADER_OPCODE_TEX_LOGICAL:
677 case SHADER_OPCODE_TXD_LOGICAL:
678 case SHADER_OPCODE_TXF_LOGICAL:
679 case SHADER_OPCODE_TXL_LOGICAL:
680 case SHADER_OPCODE_TXS_LOGICAL:
681 case FS_OPCODE_TXB_LOGICAL:
682 case SHADER_OPCODE_TXF_CMS_LOGICAL:
683 case SHADER_OPCODE_TXF_CMS_W_LOGICAL:
684 case SHADER_OPCODE_TXF_UMS_LOGICAL:
685 case SHADER_OPCODE_TXF_MCS_LOGICAL:
686 case SHADER_OPCODE_LOD_LOGICAL:
687 case SHADER_OPCODE_TG4_LOGICAL:
688 case SHADER_OPCODE_TG4_OFFSET_LOGICAL:
689 assert(src[8].file == IMM && src[9].file == IMM);
690 /* Texture coordinates. */
691 if (i == 0)
692 return src[8].ud;
693 /* Texture derivatives. */
694 else if ((i == 2 || i == 3) && opcode == SHADER_OPCODE_TXD_LOGICAL)
695 return src[9].ud;
696 /* Texture offset. */
697 else if (i == 7)
698 return 2;
699 /* MCS */
700 else if (i == 5 && opcode == SHADER_OPCODE_TXF_CMS_W_LOGICAL)
701 return 2;
702 else
703 return 1;
704
705 case SHADER_OPCODE_UNTYPED_SURFACE_READ_LOGICAL:
706 case SHADER_OPCODE_TYPED_SURFACE_READ_LOGICAL:
707 assert(src[3].file == IMM);
708 /* Surface coordinates. */
709 if (i == 0)
710 return src[3].ud;
711 /* Surface operation source (ignored for reads). */
712 else if (i == 1)
713 return 0;
714 else
715 return 1;
716
717 case SHADER_OPCODE_UNTYPED_SURFACE_WRITE_LOGICAL:
718 case SHADER_OPCODE_TYPED_SURFACE_WRITE_LOGICAL:
719 assert(src[3].file == IMM &&
720 src[4].file == IMM);
721 /* Surface coordinates. */
722 if (i == 0)
723 return src[3].ud;
724 /* Surface operation source. */
725 else if (i == 1)
726 return src[4].ud;
727 else
728 return 1;
729
730 case SHADER_OPCODE_UNTYPED_ATOMIC_LOGICAL:
731 case SHADER_OPCODE_TYPED_ATOMIC_LOGICAL: {
732 assert(src[3].file == IMM &&
733 src[4].file == IMM);
734 const unsigned op = src[4].ud;
735 /* Surface coordinates. */
736 if (i == 0)
737 return src[3].ud;
738 /* Surface operation source. */
739 else if (i == 1 && op == BRW_AOP_CMPWR)
740 return 2;
741 else if (i == 1 && (op == BRW_AOP_INC || op == BRW_AOP_DEC ||
742 op == BRW_AOP_PREDEC))
743 return 0;
744 else
745 return 1;
746 }
747
748 default:
749 return 1;
750 }
751 }
752
753 int
754 fs_inst::regs_read(int arg) const
755 {
756 switch (opcode) {
757 case FS_OPCODE_FB_WRITE:
758 case SHADER_OPCODE_URB_WRITE_SIMD8:
759 case SHADER_OPCODE_URB_WRITE_SIMD8_PER_SLOT:
760 case SHADER_OPCODE_URB_WRITE_SIMD8_MASKED:
761 case SHADER_OPCODE_URB_WRITE_SIMD8_MASKED_PER_SLOT:
762 case SHADER_OPCODE_URB_READ_SIMD8:
763 case SHADER_OPCODE_URB_READ_SIMD8_PER_SLOT:
764 case SHADER_OPCODE_UNTYPED_ATOMIC:
765 case SHADER_OPCODE_UNTYPED_SURFACE_READ:
766 case SHADER_OPCODE_UNTYPED_SURFACE_WRITE:
767 case SHADER_OPCODE_TYPED_ATOMIC:
768 case SHADER_OPCODE_TYPED_SURFACE_READ:
769 case SHADER_OPCODE_TYPED_SURFACE_WRITE:
770 case FS_OPCODE_INTERPOLATE_AT_PER_SLOT_OFFSET:
771 if (arg == 0)
772 return mlen;
773 break;
774
775 case FS_OPCODE_UNIFORM_PULL_CONSTANT_LOAD_GEN7:
776 /* The payload is actually stored in src1 */
777 if (arg == 1)
778 return mlen;
779 break;
780
781 case FS_OPCODE_LINTERP:
782 if (arg == 1)
783 return 1;
784 break;
785
786 case SHADER_OPCODE_LOAD_PAYLOAD:
787 if (arg < this->header_size)
788 return 1;
789 break;
790
791 case CS_OPCODE_CS_TERMINATE:
792 case SHADER_OPCODE_BARRIER:
793 return 1;
794
795 case SHADER_OPCODE_MOV_INDIRECT:
796 if (arg == 0) {
797 assert(src[2].file == IMM);
798 unsigned region_length = src[2].ud;
799
800 if (src[0].file == FIXED_GRF) {
801 /* If the start of the region is not register aligned, then
802 * there's some portion of the register that's technically
803 * unread at the beginning.
804 *
805 * However, the register allocator works in terms of whole
806 * registers, and does not use subnr. It assumes that the
807 * read starts at the beginning of the register, and extends
808 * regs_read() whole registers beyond that.
809 *
810 * To compensate, we extend the region length to include this
811 * unread portion at the beginning.
812 */
813 if (src[0].subnr)
814 region_length += src[0].subnr * type_sz(src[0].type);
815
816 return DIV_ROUND_UP(region_length, REG_SIZE);
817 } else {
818 assert(!"Invalid register file");
819 }
820 }
821 break;
822
823 default:
824 if (is_tex() && arg == 0 && src[0].file == VGRF)
825 return mlen;
826 break;
827 }
828
829 switch (src[arg].file) {
830 case BAD_FILE:
831 return 0;
832 case UNIFORM:
833 case IMM:
834 return 1;
835 case ARF:
836 case FIXED_GRF:
837 case VGRF:
838 case ATTR:
839 return DIV_ROUND_UP(components_read(arg) *
840 src[arg].component_size(exec_size),
841 REG_SIZE);
842 case MRF:
843 unreachable("MRF registers are not allowed as sources");
844 }
845 return 0;
846 }
847
848 bool
849 fs_inst::reads_flag() const
850 {
851 return predicate;
852 }
853
854 bool
855 fs_inst::writes_flag() const
856 {
857 return (conditional_mod && (opcode != BRW_OPCODE_SEL &&
858 opcode != BRW_OPCODE_IF &&
859 opcode != BRW_OPCODE_WHILE)) ||
860 opcode == FS_OPCODE_MOV_DISPATCH_TO_FLAGS;
861 }
862
863 /**
864 * Returns how many MRFs an FS opcode will write over.
865 *
866 * Note that this is not the 0 or 1 implied writes in an actual gen
867 * instruction -- the FS opcodes often generate MOVs in addition.
868 */
869 int
870 fs_visitor::implied_mrf_writes(fs_inst *inst)
871 {
872 if (inst->mlen == 0)
873 return 0;
874
875 if (inst->base_mrf == -1)
876 return 0;
877
878 switch (inst->opcode) {
879 case SHADER_OPCODE_RCP:
880 case SHADER_OPCODE_RSQ:
881 case SHADER_OPCODE_SQRT:
882 case SHADER_OPCODE_EXP2:
883 case SHADER_OPCODE_LOG2:
884 case SHADER_OPCODE_SIN:
885 case SHADER_OPCODE_COS:
886 return 1 * dispatch_width / 8;
887 case SHADER_OPCODE_POW:
888 case SHADER_OPCODE_INT_QUOTIENT:
889 case SHADER_OPCODE_INT_REMAINDER:
890 return 2 * dispatch_width / 8;
891 case SHADER_OPCODE_TEX:
892 case FS_OPCODE_TXB:
893 case SHADER_OPCODE_TXD:
894 case SHADER_OPCODE_TXF:
895 case SHADER_OPCODE_TXF_CMS:
896 case SHADER_OPCODE_TXF_CMS_W:
897 case SHADER_OPCODE_TXF_MCS:
898 case SHADER_OPCODE_TG4:
899 case SHADER_OPCODE_TG4_OFFSET:
900 case SHADER_OPCODE_TXL:
901 case SHADER_OPCODE_TXS:
902 case SHADER_OPCODE_LOD:
903 case SHADER_OPCODE_SAMPLEINFO:
904 return 1;
905 case FS_OPCODE_FB_WRITE:
906 return 2;
907 case FS_OPCODE_GET_BUFFER_SIZE:
908 case FS_OPCODE_UNIFORM_PULL_CONSTANT_LOAD:
909 case SHADER_OPCODE_GEN4_SCRATCH_READ:
910 return 1;
911 case FS_OPCODE_VARYING_PULL_CONSTANT_LOAD:
912 return inst->mlen;
913 case SHADER_OPCODE_GEN4_SCRATCH_WRITE:
914 return inst->mlen;
915 case SHADER_OPCODE_UNTYPED_ATOMIC:
916 case SHADER_OPCODE_UNTYPED_SURFACE_READ:
917 case SHADER_OPCODE_UNTYPED_SURFACE_WRITE:
918 case SHADER_OPCODE_TYPED_ATOMIC:
919 case SHADER_OPCODE_TYPED_SURFACE_READ:
920 case SHADER_OPCODE_TYPED_SURFACE_WRITE:
921 case SHADER_OPCODE_URB_WRITE_SIMD8:
922 case SHADER_OPCODE_URB_WRITE_SIMD8_PER_SLOT:
923 case SHADER_OPCODE_URB_WRITE_SIMD8_MASKED:
924 case SHADER_OPCODE_URB_WRITE_SIMD8_MASKED_PER_SLOT:
925 case FS_OPCODE_INTERPOLATE_AT_CENTROID:
926 case FS_OPCODE_INTERPOLATE_AT_SAMPLE:
927 case FS_OPCODE_INTERPOLATE_AT_SHARED_OFFSET:
928 case FS_OPCODE_INTERPOLATE_AT_PER_SLOT_OFFSET:
929 return 0;
930 default:
931 unreachable("not reached");
932 }
933 }
934
935 fs_reg
936 fs_visitor::vgrf(const glsl_type *const type)
937 {
938 int reg_width = dispatch_width / 8;
939 return fs_reg(VGRF, alloc.allocate(type_size_scalar(type) * reg_width),
940 brw_type_for_base_type(type));
941 }
942
943 fs_reg::fs_reg(enum brw_reg_file file, int nr)
944 {
945 init();
946 this->file = file;
947 this->nr = nr;
948 this->type = BRW_REGISTER_TYPE_F;
949 this->stride = (file == UNIFORM ? 0 : 1);
950 }
951
952 fs_reg::fs_reg(enum brw_reg_file file, int nr, enum brw_reg_type type)
953 {
954 init();
955 this->file = file;
956 this->nr = nr;
957 this->type = type;
958 this->stride = (file == UNIFORM ? 0 : 1);
959 }
960
961 /* For SIMD16, we need to follow from the uniform setup of SIMD8 dispatch.
962 * This brings in those uniform definitions
963 */
964 void
965 fs_visitor::import_uniforms(fs_visitor *v)
966 {
967 this->push_constant_loc = v->push_constant_loc;
968 this->pull_constant_loc = v->pull_constant_loc;
969 this->uniforms = v->uniforms;
970 this->param_size = v->param_size;
971 }
972
973 fs_reg *
974 fs_visitor::emit_fragcoord_interpolation(bool pixel_center_integer,
975 bool origin_upper_left)
976 {
977 assert(stage == MESA_SHADER_FRAGMENT);
978 brw_wm_prog_key *key = (brw_wm_prog_key*) this->key;
979 fs_reg *reg = new(this->mem_ctx) fs_reg(vgrf(glsl_type::vec4_type));
980 fs_reg wpos = *reg;
981 bool flip = !origin_upper_left ^ key->render_to_fbo;
982
983 /* gl_FragCoord.x */
984 if (pixel_center_integer) {
985 bld.MOV(wpos, this->pixel_x);
986 } else {
987 bld.ADD(wpos, this->pixel_x, brw_imm_f(0.5f));
988 }
989 wpos = offset(wpos, bld, 1);
990
991 /* gl_FragCoord.y */
992 if (!flip && pixel_center_integer) {
993 bld.MOV(wpos, this->pixel_y);
994 } else {
995 fs_reg pixel_y = this->pixel_y;
996 float offset = (pixel_center_integer ? 0.0f : 0.5f);
997
998 if (flip) {
999 pixel_y.negate = true;
1000 offset += key->drawable_height - 1.0f;
1001 }
1002
1003 bld.ADD(wpos, pixel_y, brw_imm_f(offset));
1004 }
1005 wpos = offset(wpos, bld, 1);
1006
1007 /* gl_FragCoord.z */
1008 if (devinfo->gen >= 6) {
1009 bld.MOV(wpos, fs_reg(brw_vec8_grf(payload.source_depth_reg, 0)));
1010 } else {
1011 bld.emit(FS_OPCODE_LINTERP, wpos,
1012 this->delta_xy[BRW_WM_PERSPECTIVE_PIXEL_BARYCENTRIC],
1013 interp_reg(VARYING_SLOT_POS, 2));
1014 }
1015 wpos = offset(wpos, bld, 1);
1016
1017 /* gl_FragCoord.w: Already set up in emit_interpolation */
1018 bld.MOV(wpos, this->wpos_w);
1019
1020 return reg;
1021 }
1022
1023 fs_inst *
1024 fs_visitor::emit_linterp(const fs_reg &attr, const fs_reg &interp,
1025 glsl_interp_qualifier interpolation_mode,
1026 bool is_centroid, bool is_sample)
1027 {
1028 brw_wm_barycentric_interp_mode barycoord_mode;
1029 if (devinfo->gen >= 6) {
1030 if (is_centroid) {
1031 if (interpolation_mode == INTERP_QUALIFIER_SMOOTH)
1032 barycoord_mode = BRW_WM_PERSPECTIVE_CENTROID_BARYCENTRIC;
1033 else
1034 barycoord_mode = BRW_WM_NONPERSPECTIVE_CENTROID_BARYCENTRIC;
1035 } else if (is_sample) {
1036 if (interpolation_mode == INTERP_QUALIFIER_SMOOTH)
1037 barycoord_mode = BRW_WM_PERSPECTIVE_SAMPLE_BARYCENTRIC;
1038 else
1039 barycoord_mode = BRW_WM_NONPERSPECTIVE_SAMPLE_BARYCENTRIC;
1040 } else {
1041 if (interpolation_mode == INTERP_QUALIFIER_SMOOTH)
1042 barycoord_mode = BRW_WM_PERSPECTIVE_PIXEL_BARYCENTRIC;
1043 else
1044 barycoord_mode = BRW_WM_NONPERSPECTIVE_PIXEL_BARYCENTRIC;
1045 }
1046 } else {
1047 /* On Ironlake and below, there is only one interpolation mode.
1048 * Centroid interpolation doesn't mean anything on this hardware --
1049 * there is no multisampling.
1050 */
1051 barycoord_mode = BRW_WM_PERSPECTIVE_PIXEL_BARYCENTRIC;
1052 }
1053 return bld.emit(FS_OPCODE_LINTERP, attr,
1054 this->delta_xy[barycoord_mode], interp);
1055 }
1056
1057 void
1058 fs_visitor::emit_general_interpolation(fs_reg attr, const char *name,
1059 const glsl_type *type,
1060 glsl_interp_qualifier interpolation_mode,
1061 int location, bool mod_centroid,
1062 bool mod_sample)
1063 {
1064 attr.type = brw_type_for_base_type(type->get_scalar_type());
1065
1066 assert(stage == MESA_SHADER_FRAGMENT);
1067 brw_wm_prog_data *prog_data = (brw_wm_prog_data*) this->prog_data;
1068 brw_wm_prog_key *key = (brw_wm_prog_key*) this->key;
1069
1070 unsigned int array_elements;
1071
1072 if (type->is_array()) {
1073 array_elements = type->arrays_of_arrays_size();
1074 if (array_elements == 0) {
1075 fail("dereferenced array '%s' has length 0\n", name);
1076 }
1077 type = type->without_array();
1078 } else {
1079 array_elements = 1;
1080 }
1081
1082 if (interpolation_mode == INTERP_QUALIFIER_NONE) {
1083 bool is_gl_Color =
1084 location == VARYING_SLOT_COL0 || location == VARYING_SLOT_COL1;
1085 if (key->flat_shade && is_gl_Color) {
1086 interpolation_mode = INTERP_QUALIFIER_FLAT;
1087 } else {
1088 interpolation_mode = INTERP_QUALIFIER_SMOOTH;
1089 }
1090 }
1091
1092 for (unsigned int i = 0; i < array_elements; i++) {
1093 for (unsigned int j = 0; j < type->matrix_columns; j++) {
1094 if (prog_data->urb_setup[location] == -1) {
1095 /* If there's no incoming setup data for this slot, don't
1096 * emit interpolation for it.
1097 */
1098 attr = offset(attr, bld, type->vector_elements);
1099 location++;
1100 continue;
1101 }
1102
1103 if (interpolation_mode == INTERP_QUALIFIER_FLAT) {
1104 /* Constant interpolation (flat shading) case. The SF has
1105 * handed us defined values in only the constant offset
1106 * field of the setup reg.
1107 */
1108 for (unsigned int k = 0; k < type->vector_elements; k++) {
1109 struct brw_reg interp = interp_reg(location, k);
1110 interp = suboffset(interp, 3);
1111 interp.type = attr.type;
1112 bld.emit(FS_OPCODE_CINTERP, attr, fs_reg(interp));
1113 attr = offset(attr, bld, 1);
1114 }
1115 } else {
1116 /* Smooth/noperspective interpolation case. */
1117 for (unsigned int k = 0; k < type->vector_elements; k++) {
1118 struct brw_reg interp = interp_reg(location, k);
1119 if (devinfo->needs_unlit_centroid_workaround && mod_centroid) {
1120 /* Get the pixel/sample mask into f0 so that we know
1121 * which pixels are lit. Then, for each channel that is
1122 * unlit, replace the centroid data with non-centroid
1123 * data.
1124 */
1125 bld.emit(FS_OPCODE_MOV_DISPATCH_TO_FLAGS);
1126
1127 fs_inst *inst;
1128 inst = emit_linterp(attr, fs_reg(interp), interpolation_mode,
1129 false, false);
1130 inst->predicate = BRW_PREDICATE_NORMAL;
1131 inst->predicate_inverse = true;
1132 if (devinfo->has_pln)
1133 inst->no_dd_clear = true;
1134
1135 inst = emit_linterp(attr, fs_reg(interp), interpolation_mode,
1136 mod_centroid && !key->persample_shading,
1137 mod_sample || key->persample_shading);
1138 inst->predicate = BRW_PREDICATE_NORMAL;
1139 inst->predicate_inverse = false;
1140 if (devinfo->has_pln)
1141 inst->no_dd_check = true;
1142
1143 } else {
1144 emit_linterp(attr, fs_reg(interp), interpolation_mode,
1145 mod_centroid && !key->persample_shading,
1146 mod_sample || key->persample_shading);
1147 }
1148 if (devinfo->gen < 6 && interpolation_mode == INTERP_QUALIFIER_SMOOTH) {
1149 bld.MUL(attr, attr, this->pixel_w);
1150 }
1151 attr = offset(attr, bld, 1);
1152 }
1153
1154 }
1155 location++;
1156 }
1157 }
1158 }
1159
1160 fs_reg *
1161 fs_visitor::emit_frontfacing_interpolation()
1162 {
1163 fs_reg *reg = new(this->mem_ctx) fs_reg(vgrf(glsl_type::bool_type));
1164
1165 if (devinfo->gen >= 6) {
1166 /* Bit 15 of g0.0 is 0 if the polygon is front facing. We want to create
1167 * a boolean result from this (~0/true or 0/false).
1168 *
1169 * We can use the fact that bit 15 is the MSB of g0.0:W to accomplish
1170 * this task in only one instruction:
1171 * - a negation source modifier will flip the bit; and
1172 * - a W -> D type conversion will sign extend the bit into the high
1173 * word of the destination.
1174 *
1175 * An ASR 15 fills the low word of the destination.
1176 */
1177 fs_reg g0 = fs_reg(retype(brw_vec1_grf(0, 0), BRW_REGISTER_TYPE_W));
1178 g0.negate = true;
1179
1180 bld.ASR(*reg, g0, brw_imm_d(15));
1181 } else {
1182 /* Bit 31 of g1.6 is 0 if the polygon is front facing. We want to create
1183 * a boolean result from this (1/true or 0/false).
1184 *
1185 * Like in the above case, since the bit is the MSB of g1.6:UD we can use
1186 * the negation source modifier to flip it. Unfortunately the SHR
1187 * instruction only operates on UD (or D with an abs source modifier)
1188 * sources without negation.
1189 *
1190 * Instead, use ASR (which will give ~0/true or 0/false).
1191 */
1192 fs_reg g1_6 = fs_reg(retype(brw_vec1_grf(1, 6), BRW_REGISTER_TYPE_D));
1193 g1_6.negate = true;
1194
1195 bld.ASR(*reg, g1_6, brw_imm_d(31));
1196 }
1197
1198 return reg;
1199 }
1200
1201 void
1202 fs_visitor::compute_sample_position(fs_reg dst, fs_reg int_sample_pos)
1203 {
1204 assert(stage == MESA_SHADER_FRAGMENT);
1205 brw_wm_prog_key *key = (brw_wm_prog_key*) this->key;
1206 assert(dst.type == BRW_REGISTER_TYPE_F);
1207
1208 if (key->compute_pos_offset) {
1209 /* Convert int_sample_pos to floating point */
1210 bld.MOV(dst, int_sample_pos);
1211 /* Scale to the range [0, 1] */
1212 bld.MUL(dst, dst, brw_imm_f(1 / 16.0f));
1213 }
1214 else {
1215 /* From ARB_sample_shading specification:
1216 * "When rendering to a non-multisample buffer, or if multisample
1217 * rasterization is disabled, gl_SamplePosition will always be
1218 * (0.5, 0.5).
1219 */
1220 bld.MOV(dst, brw_imm_f(0.5f));
1221 }
1222 }
1223
1224 fs_reg *
1225 fs_visitor::emit_samplepos_setup()
1226 {
1227 assert(devinfo->gen >= 6);
1228
1229 const fs_builder abld = bld.annotate("compute sample position");
1230 fs_reg *reg = new(this->mem_ctx) fs_reg(vgrf(glsl_type::vec2_type));
1231 fs_reg pos = *reg;
1232 fs_reg int_sample_x = vgrf(glsl_type::int_type);
1233 fs_reg int_sample_y = vgrf(glsl_type::int_type);
1234
1235 /* WM will be run in MSDISPMODE_PERSAMPLE. So, only one of SIMD8 or SIMD16
1236 * mode will be enabled.
1237 *
1238 * From the Ivy Bridge PRM, volume 2 part 1, page 344:
1239 * R31.1:0 Position Offset X/Y for Slot[3:0]
1240 * R31.3:2 Position Offset X/Y for Slot[7:4]
1241 * .....
1242 *
1243 * The X, Y sample positions come in as bytes in thread payload. So, read
1244 * the positions using vstride=16, width=8, hstride=2.
1245 */
1246 struct brw_reg sample_pos_reg =
1247 stride(retype(brw_vec1_grf(payload.sample_pos_reg, 0),
1248 BRW_REGISTER_TYPE_B), 16, 8, 2);
1249
1250 if (dispatch_width == 8) {
1251 abld.MOV(int_sample_x, fs_reg(sample_pos_reg));
1252 } else {
1253 abld.half(0).MOV(half(int_sample_x, 0), fs_reg(sample_pos_reg));
1254 abld.half(1).MOV(half(int_sample_x, 1),
1255 fs_reg(suboffset(sample_pos_reg, 16)));
1256 }
1257 /* Compute gl_SamplePosition.x */
1258 compute_sample_position(pos, int_sample_x);
1259 pos = offset(pos, abld, 1);
1260 if (dispatch_width == 8) {
1261 abld.MOV(int_sample_y, fs_reg(suboffset(sample_pos_reg, 1)));
1262 } else {
1263 abld.half(0).MOV(half(int_sample_y, 0),
1264 fs_reg(suboffset(sample_pos_reg, 1)));
1265 abld.half(1).MOV(half(int_sample_y, 1),
1266 fs_reg(suboffset(sample_pos_reg, 17)));
1267 }
1268 /* Compute gl_SamplePosition.y */
1269 compute_sample_position(pos, int_sample_y);
1270 return reg;
1271 }
1272
1273 fs_reg *
1274 fs_visitor::emit_sampleid_setup()
1275 {
1276 assert(stage == MESA_SHADER_FRAGMENT);
1277 brw_wm_prog_key *key = (brw_wm_prog_key*) this->key;
1278 assert(devinfo->gen >= 6);
1279
1280 const fs_builder abld = bld.annotate("compute sample id");
1281 fs_reg *reg = new(this->mem_ctx) fs_reg(vgrf(glsl_type::int_type));
1282
1283 if (key->compute_sample_id) {
1284 fs_reg t1(VGRF, alloc.allocate(1), BRW_REGISTER_TYPE_D);
1285 t1.set_smear(0);
1286 fs_reg t2(VGRF, alloc.allocate(1), BRW_REGISTER_TYPE_W);
1287
1288 /* The PS will be run in MSDISPMODE_PERSAMPLE. For example with
1289 * 8x multisampling, subspan 0 will represent sample N (where N
1290 * is 0, 2, 4 or 6), subspan 1 will represent sample 1, 3, 5 or
1291 * 7. We can find the value of N by looking at R0.0 bits 7:6
1292 * ("Starting Sample Pair Index (SSPI)") and multiplying by two
1293 * (since samples are always delivered in pairs). That is, we
1294 * compute 2*((R0.0 & 0xc0) >> 6) == (R0.0 & 0xc0) >> 5. Then
1295 * we need to add N to the sequence (0, 0, 0, 0, 1, 1, 1, 1) in
1296 * case of SIMD8 and sequence (0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2,
1297 * 2, 3, 3, 3, 3) in case of SIMD16. We compute this sequence by
1298 * populating a temporary variable with the sequence (0, 1, 2, 3),
1299 * and then reading from it using vstride=1, width=4, hstride=0.
1300 * These computations hold good for 4x multisampling as well.
1301 *
1302 * For 2x MSAA and SIMD16, we want to use the sequence (0, 1, 0, 1):
1303 * the first four slots are sample 0 of subspan 0; the next four
1304 * are sample 1 of subspan 0; the third group is sample 0 of
1305 * subspan 1, and finally sample 1 of subspan 1.
1306 */
1307
1308 /* SKL+ has an extra bit for the Starting Sample Pair Index to
1309 * accomodate 16x MSAA.
1310 */
1311 unsigned sspi_mask = devinfo->gen >= 9 ? 0x1c0 : 0xc0;
1312
1313 abld.exec_all().group(1, 0)
1314 .AND(t1, fs_reg(retype(brw_vec1_grf(0, 0), BRW_REGISTER_TYPE_D)),
1315 brw_imm_ud(sspi_mask));
1316 abld.exec_all().group(1, 0).SHR(t1, t1, brw_imm_d(5));
1317
1318 /* This works for both SIMD8 and SIMD16 */
1319 abld.exec_all().group(4, 0)
1320 .MOV(t2, brw_imm_v(key->persample_2x ? 0x1010 : 0x3210));
1321
1322 /* This special instruction takes care of setting vstride=1,
1323 * width=4, hstride=0 of t2 during an ADD instruction.
1324 */
1325 abld.emit(FS_OPCODE_SET_SAMPLE_ID, *reg, t1, t2);
1326 } else {
1327 /* As per GL_ARB_sample_shading specification:
1328 * "When rendering to a non-multisample buffer, or if multisample
1329 * rasterization is disabled, gl_SampleID will always be zero."
1330 */
1331 abld.MOV(*reg, brw_imm_d(0));
1332 }
1333
1334 return reg;
1335 }
1336
1337 fs_reg
1338 fs_visitor::resolve_source_modifiers(const fs_reg &src)
1339 {
1340 if (!src.abs && !src.negate)
1341 return src;
1342
1343 fs_reg temp = bld.vgrf(src.type);
1344 bld.MOV(temp, src);
1345
1346 return temp;
1347 }
1348
1349 void
1350 fs_visitor::emit_discard_jump()
1351 {
1352 assert(((brw_wm_prog_data*) this->prog_data)->uses_kill);
1353
1354 /* For performance, after a discard, jump to the end of the
1355 * shader if all relevant channels have been discarded.
1356 */
1357 fs_inst *discard_jump = bld.emit(FS_OPCODE_DISCARD_JUMP);
1358 discard_jump->flag_subreg = 1;
1359
1360 discard_jump->predicate = (dispatch_width == 8)
1361 ? BRW_PREDICATE_ALIGN1_ANY8H
1362 : BRW_PREDICATE_ALIGN1_ANY16H;
1363 discard_jump->predicate_inverse = true;
1364 }
1365
1366 void
1367 fs_visitor::emit_gs_thread_end()
1368 {
1369 assert(stage == MESA_SHADER_GEOMETRY);
1370
1371 struct brw_gs_prog_data *gs_prog_data =
1372 (struct brw_gs_prog_data *) prog_data;
1373
1374 if (gs_compile->control_data_header_size_bits > 0) {
1375 emit_gs_control_data_bits(this->final_gs_vertex_count);
1376 }
1377
1378 const fs_builder abld = bld.annotate("thread end");
1379 fs_inst *inst;
1380
1381 if (gs_prog_data->static_vertex_count != -1) {
1382 foreach_in_list_reverse(fs_inst, prev, &this->instructions) {
1383 if (prev->opcode == SHADER_OPCODE_URB_WRITE_SIMD8 ||
1384 prev->opcode == SHADER_OPCODE_URB_WRITE_SIMD8_MASKED ||
1385 prev->opcode == SHADER_OPCODE_URB_WRITE_SIMD8_PER_SLOT ||
1386 prev->opcode == SHADER_OPCODE_URB_WRITE_SIMD8_MASKED_PER_SLOT) {
1387 prev->eot = true;
1388
1389 /* Delete now dead instructions. */
1390 foreach_in_list_reverse_safe(exec_node, dead, &this->instructions) {
1391 if (dead == prev)
1392 break;
1393 dead->remove();
1394 }
1395 return;
1396 } else if (prev->is_control_flow() || prev->has_side_effects()) {
1397 break;
1398 }
1399 }
1400 fs_reg hdr = abld.vgrf(BRW_REGISTER_TYPE_UD, 1);
1401 abld.MOV(hdr, fs_reg(retype(brw_vec8_grf(1, 0), BRW_REGISTER_TYPE_UD)));
1402 inst = abld.emit(SHADER_OPCODE_URB_WRITE_SIMD8, reg_undef, hdr);
1403 inst->mlen = 1;
1404 } else {
1405 fs_reg payload = abld.vgrf(BRW_REGISTER_TYPE_UD, 2);
1406 fs_reg *sources = ralloc_array(mem_ctx, fs_reg, 2);
1407 sources[0] = fs_reg(retype(brw_vec8_grf(1, 0), BRW_REGISTER_TYPE_UD));
1408 sources[1] = this->final_gs_vertex_count;
1409 abld.LOAD_PAYLOAD(payload, sources, 2, 2);
1410 inst = abld.emit(SHADER_OPCODE_URB_WRITE_SIMD8, reg_undef, payload);
1411 inst->mlen = 2;
1412 }
1413 inst->eot = true;
1414 inst->offset = 0;
1415 }
1416
1417 void
1418 fs_visitor::assign_curb_setup()
1419 {
1420 if (dispatch_width == 8) {
1421 prog_data->dispatch_grf_start_reg = payload.num_regs;
1422 } else {
1423 if (stage == MESA_SHADER_FRAGMENT) {
1424 brw_wm_prog_data *prog_data = (brw_wm_prog_data*) this->prog_data;
1425 prog_data->dispatch_grf_start_reg_16 = payload.num_regs;
1426 } else if (stage == MESA_SHADER_COMPUTE) {
1427 brw_cs_prog_data *prog_data = (brw_cs_prog_data*) this->prog_data;
1428 prog_data->dispatch_grf_start_reg_16 = payload.num_regs;
1429 } else {
1430 unreachable("Unsupported shader type!");
1431 }
1432 }
1433
1434 prog_data->curb_read_length = ALIGN(stage_prog_data->nr_params, 8) / 8;
1435
1436 /* Map the offsets in the UNIFORM file to fixed HW regs. */
1437 foreach_block_and_inst(block, fs_inst, inst, cfg) {
1438 for (unsigned int i = 0; i < inst->sources; i++) {
1439 if (inst->src[i].file == UNIFORM) {
1440 int uniform_nr = inst->src[i].nr + inst->src[i].reg_offset;
1441 int constant_nr;
1442 if (uniform_nr >= 0 && uniform_nr < (int) uniforms) {
1443 constant_nr = push_constant_loc[uniform_nr];
1444 } else {
1445 /* Section 5.11 of the OpenGL 4.1 spec says:
1446 * "Out-of-bounds reads return undefined values, which include
1447 * values from other variables of the active program or zero."
1448 * Just return the first push constant.
1449 */
1450 constant_nr = 0;
1451 }
1452
1453 struct brw_reg brw_reg = brw_vec1_grf(payload.num_regs +
1454 constant_nr / 8,
1455 constant_nr % 8);
1456 brw_reg.abs = inst->src[i].abs;
1457 brw_reg.negate = inst->src[i].negate;
1458
1459 assert(inst->src[i].stride == 0);
1460 inst->src[i] = byte_offset(
1461 retype(brw_reg, inst->src[i].type),
1462 inst->src[i].subreg_offset);
1463 }
1464 }
1465 }
1466
1467 /* This may be updated in assign_urb_setup or assign_vs_urb_setup. */
1468 this->first_non_payload_grf = payload.num_regs + prog_data->curb_read_length;
1469 }
1470
1471 void
1472 fs_visitor::calculate_urb_setup()
1473 {
1474 assert(stage == MESA_SHADER_FRAGMENT);
1475 brw_wm_prog_data *prog_data = (brw_wm_prog_data*) this->prog_data;
1476 brw_wm_prog_key *key = (brw_wm_prog_key*) this->key;
1477
1478 memset(prog_data->urb_setup, -1,
1479 sizeof(prog_data->urb_setup[0]) * VARYING_SLOT_MAX);
1480
1481 int urb_next = 0;
1482 /* Figure out where each of the incoming setup attributes lands. */
1483 if (devinfo->gen >= 6) {
1484 if (_mesa_bitcount_64(nir->info.inputs_read &
1485 BRW_FS_VARYING_INPUT_MASK) <= 16) {
1486 /* The SF/SBE pipeline stage can do arbitrary rearrangement of the
1487 * first 16 varying inputs, so we can put them wherever we want.
1488 * Just put them in order.
1489 *
1490 * This is useful because it means that (a) inputs not used by the
1491 * fragment shader won't take up valuable register space, and (b) we
1492 * won't have to recompile the fragment shader if it gets paired with
1493 * a different vertex (or geometry) shader.
1494 */
1495 for (unsigned int i = 0; i < VARYING_SLOT_MAX; i++) {
1496 if (nir->info.inputs_read & BRW_FS_VARYING_INPUT_MASK &
1497 BITFIELD64_BIT(i)) {
1498 prog_data->urb_setup[i] = urb_next++;
1499 }
1500 }
1501 } else {
1502 bool include_vue_header =
1503 nir->info.inputs_read & (VARYING_BIT_LAYER | VARYING_BIT_VIEWPORT);
1504
1505 /* We have enough input varyings that the SF/SBE pipeline stage can't
1506 * arbitrarily rearrange them to suit our whim; we have to put them
1507 * in an order that matches the output of the previous pipeline stage
1508 * (geometry or vertex shader).
1509 */
1510 struct brw_vue_map prev_stage_vue_map;
1511 brw_compute_vue_map(devinfo, &prev_stage_vue_map,
1512 key->input_slots_valid,
1513 nir->info.separate_shader);
1514 int first_slot =
1515 include_vue_header ? 0 : 2 * BRW_SF_URB_ENTRY_READ_OFFSET;
1516
1517 assert(prev_stage_vue_map.num_slots <= first_slot + 32);
1518 for (int slot = first_slot; slot < prev_stage_vue_map.num_slots;
1519 slot++) {
1520 int varying = prev_stage_vue_map.slot_to_varying[slot];
1521 if (varying != BRW_VARYING_SLOT_PAD &&
1522 (nir->info.inputs_read & BRW_FS_VARYING_INPUT_MASK &
1523 BITFIELD64_BIT(varying))) {
1524 prog_data->urb_setup[varying] = slot - first_slot;
1525 }
1526 }
1527 urb_next = prev_stage_vue_map.num_slots - first_slot;
1528 }
1529 } else {
1530 /* FINISHME: The sf doesn't map VS->FS inputs for us very well. */
1531 for (unsigned int i = 0; i < VARYING_SLOT_MAX; i++) {
1532 /* Point size is packed into the header, not as a general attribute */
1533 if (i == VARYING_SLOT_PSIZ)
1534 continue;
1535
1536 if (key->input_slots_valid & BITFIELD64_BIT(i)) {
1537 /* The back color slot is skipped when the front color is
1538 * also written to. In addition, some slots can be
1539 * written in the vertex shader and not read in the
1540 * fragment shader. So the register number must always be
1541 * incremented, mapped or not.
1542 */
1543 if (_mesa_varying_slot_in_fs((gl_varying_slot) i))
1544 prog_data->urb_setup[i] = urb_next;
1545 urb_next++;
1546 }
1547 }
1548
1549 /*
1550 * It's a FS only attribute, and we did interpolation for this attribute
1551 * in SF thread. So, count it here, too.
1552 *
1553 * See compile_sf_prog() for more info.
1554 */
1555 if (nir->info.inputs_read & BITFIELD64_BIT(VARYING_SLOT_PNTC))
1556 prog_data->urb_setup[VARYING_SLOT_PNTC] = urb_next++;
1557 }
1558
1559 prog_data->num_varying_inputs = urb_next;
1560 }
1561
1562 void
1563 fs_visitor::assign_urb_setup()
1564 {
1565 assert(stage == MESA_SHADER_FRAGMENT);
1566 brw_wm_prog_data *prog_data = (brw_wm_prog_data*) this->prog_data;
1567
1568 int urb_start = payload.num_regs + prog_data->base.curb_read_length;
1569
1570 /* Offset all the urb_setup[] index by the actual position of the
1571 * setup regs, now that the location of the constants has been chosen.
1572 */
1573 foreach_block_and_inst(block, fs_inst, inst, cfg) {
1574 if (inst->opcode == FS_OPCODE_LINTERP) {
1575 assert(inst->src[1].file == FIXED_GRF);
1576 inst->src[1].nr += urb_start;
1577 }
1578
1579 if (inst->opcode == FS_OPCODE_CINTERP) {
1580 assert(inst->src[0].file == FIXED_GRF);
1581 inst->src[0].nr += urb_start;
1582 }
1583 }
1584
1585 /* Each attribute is 4 setup channels, each of which is half a reg. */
1586 this->first_non_payload_grf += prog_data->num_varying_inputs * 2;
1587 }
1588
1589 void
1590 fs_visitor::convert_attr_sources_to_hw_regs(fs_inst *inst)
1591 {
1592 for (int i = 0; i < inst->sources; i++) {
1593 if (inst->src[i].file == ATTR) {
1594 int grf = payload.num_regs +
1595 prog_data->curb_read_length +
1596 inst->src[i].nr +
1597 inst->src[i].reg_offset;
1598
1599 unsigned width = inst->src[i].stride == 0 ? 1 : inst->exec_size;
1600 struct brw_reg reg =
1601 stride(byte_offset(retype(brw_vec8_grf(grf, 0), inst->src[i].type),
1602 inst->src[i].subreg_offset),
1603 inst->exec_size * inst->src[i].stride,
1604 width, inst->src[i].stride);
1605 reg.abs = inst->src[i].abs;
1606 reg.negate = inst->src[i].negate;
1607
1608 inst->src[i] = reg;
1609 }
1610 }
1611 }
1612
1613 void
1614 fs_visitor::assign_vs_urb_setup()
1615 {
1616 brw_vs_prog_data *vs_prog_data = (brw_vs_prog_data *) prog_data;
1617
1618 assert(stage == MESA_SHADER_VERTEX);
1619 int count = _mesa_bitcount_64(vs_prog_data->inputs_read);
1620 if (vs_prog_data->uses_vertexid || vs_prog_data->uses_instanceid)
1621 count++;
1622
1623 /* Each attribute is 4 regs. */
1624 this->first_non_payload_grf += 4 * vs_prog_data->nr_attributes;
1625
1626 assert(vs_prog_data->base.urb_read_length <= 15);
1627
1628 /* Rewrite all ATTR file references to the hw grf that they land in. */
1629 foreach_block_and_inst(block, fs_inst, inst, cfg) {
1630 convert_attr_sources_to_hw_regs(inst);
1631 }
1632 }
1633
1634 void
1635 fs_visitor::assign_gs_urb_setup()
1636 {
1637 assert(stage == MESA_SHADER_GEOMETRY);
1638
1639 brw_vue_prog_data *vue_prog_data = (brw_vue_prog_data *) prog_data;
1640
1641 first_non_payload_grf +=
1642 8 * vue_prog_data->urb_read_length * nir->info.gs.vertices_in;
1643
1644 foreach_block_and_inst(block, fs_inst, inst, cfg) {
1645 /* Rewrite all ATTR file references to GRFs. */
1646 convert_attr_sources_to_hw_regs(inst);
1647 }
1648 }
1649
1650
1651 /**
1652 * Split large virtual GRFs into separate components if we can.
1653 *
1654 * This is mostly duplicated with what brw_fs_vector_splitting does,
1655 * but that's really conservative because it's afraid of doing
1656 * splitting that doesn't result in real progress after the rest of
1657 * the optimization phases, which would cause infinite looping in
1658 * optimization. We can do it once here, safely. This also has the
1659 * opportunity to split interpolated values, or maybe even uniforms,
1660 * which we don't have at the IR level.
1661 *
1662 * We want to split, because virtual GRFs are what we register
1663 * allocate and spill (due to contiguousness requirements for some
1664 * instructions), and they're what we naturally generate in the
1665 * codegen process, but most virtual GRFs don't actually need to be
1666 * contiguous sets of GRFs. If we split, we'll end up with reduced
1667 * live intervals and better dead code elimination and coalescing.
1668 */
1669 void
1670 fs_visitor::split_virtual_grfs()
1671 {
1672 int num_vars = this->alloc.count;
1673
1674 /* Count the total number of registers */
1675 int reg_count = 0;
1676 int vgrf_to_reg[num_vars];
1677 for (int i = 0; i < num_vars; i++) {
1678 vgrf_to_reg[i] = reg_count;
1679 reg_count += alloc.sizes[i];
1680 }
1681
1682 /* An array of "split points". For each register slot, this indicates
1683 * if this slot can be separated from the previous slot. Every time an
1684 * instruction uses multiple elements of a register (as a source or
1685 * destination), we mark the used slots as inseparable. Then we go
1686 * through and split the registers into the smallest pieces we can.
1687 */
1688 bool split_points[reg_count];
1689 memset(split_points, 0, sizeof(split_points));
1690
1691 /* Mark all used registers as fully splittable */
1692 foreach_block_and_inst(block, fs_inst, inst, cfg) {
1693 if (inst->dst.file == VGRF) {
1694 int reg = vgrf_to_reg[inst->dst.nr];
1695 for (unsigned j = 1; j < this->alloc.sizes[inst->dst.nr]; j++)
1696 split_points[reg + j] = true;
1697 }
1698
1699 for (int i = 0; i < inst->sources; i++) {
1700 if (inst->src[i].file == VGRF) {
1701 int reg = vgrf_to_reg[inst->src[i].nr];
1702 for (unsigned j = 1; j < this->alloc.sizes[inst->src[i].nr]; j++)
1703 split_points[reg + j] = true;
1704 }
1705 }
1706 }
1707
1708 foreach_block_and_inst(block, fs_inst, inst, cfg) {
1709 if (inst->dst.file == VGRF) {
1710 int reg = vgrf_to_reg[inst->dst.nr] + inst->dst.reg_offset;
1711 for (int j = 1; j < inst->regs_written; j++)
1712 split_points[reg + j] = false;
1713 }
1714 for (int i = 0; i < inst->sources; i++) {
1715 if (inst->src[i].file == VGRF) {
1716 int reg = vgrf_to_reg[inst->src[i].nr] + inst->src[i].reg_offset;
1717 for (int j = 1; j < inst->regs_read(i); j++)
1718 split_points[reg + j] = false;
1719 }
1720 }
1721 }
1722
1723 int new_virtual_grf[reg_count];
1724 int new_reg_offset[reg_count];
1725
1726 int reg = 0;
1727 for (int i = 0; i < num_vars; i++) {
1728 /* The first one should always be 0 as a quick sanity check. */
1729 assert(split_points[reg] == false);
1730
1731 /* j = 0 case */
1732 new_reg_offset[reg] = 0;
1733 reg++;
1734 int offset = 1;
1735
1736 /* j > 0 case */
1737 for (unsigned j = 1; j < alloc.sizes[i]; j++) {
1738 /* If this is a split point, reset the offset to 0 and allocate a
1739 * new virtual GRF for the previous offset many registers
1740 */
1741 if (split_points[reg]) {
1742 assert(offset <= MAX_VGRF_SIZE);
1743 int grf = alloc.allocate(offset);
1744 for (int k = reg - offset; k < reg; k++)
1745 new_virtual_grf[k] = grf;
1746 offset = 0;
1747 }
1748 new_reg_offset[reg] = offset;
1749 offset++;
1750 reg++;
1751 }
1752
1753 /* The last one gets the original register number */
1754 assert(offset <= MAX_VGRF_SIZE);
1755 alloc.sizes[i] = offset;
1756 for (int k = reg - offset; k < reg; k++)
1757 new_virtual_grf[k] = i;
1758 }
1759 assert(reg == reg_count);
1760
1761 foreach_block_and_inst(block, fs_inst, inst, cfg) {
1762 if (inst->dst.file == VGRF) {
1763 reg = vgrf_to_reg[inst->dst.nr] + inst->dst.reg_offset;
1764 inst->dst.nr = new_virtual_grf[reg];
1765 inst->dst.reg_offset = new_reg_offset[reg];
1766 assert((unsigned)new_reg_offset[reg] < alloc.sizes[new_virtual_grf[reg]]);
1767 }
1768 for (int i = 0; i < inst->sources; i++) {
1769 if (inst->src[i].file == VGRF) {
1770 reg = vgrf_to_reg[inst->src[i].nr] + inst->src[i].reg_offset;
1771 inst->src[i].nr = new_virtual_grf[reg];
1772 inst->src[i].reg_offset = new_reg_offset[reg];
1773 assert((unsigned)new_reg_offset[reg] < alloc.sizes[new_virtual_grf[reg]]);
1774 }
1775 }
1776 }
1777 invalidate_live_intervals();
1778 }
1779
1780 /**
1781 * Remove unused virtual GRFs and compact the virtual_grf_* arrays.
1782 *
1783 * During code generation, we create tons of temporary variables, many of
1784 * which get immediately killed and are never used again. Yet, in later
1785 * optimization and analysis passes, such as compute_live_intervals, we need
1786 * to loop over all the virtual GRFs. Compacting them can save a lot of
1787 * overhead.
1788 */
1789 bool
1790 fs_visitor::compact_virtual_grfs()
1791 {
1792 bool progress = false;
1793 int remap_table[this->alloc.count];
1794 memset(remap_table, -1, sizeof(remap_table));
1795
1796 /* Mark which virtual GRFs are used. */
1797 foreach_block_and_inst(block, const fs_inst, inst, cfg) {
1798 if (inst->dst.file == VGRF)
1799 remap_table[inst->dst.nr] = 0;
1800
1801 for (int i = 0; i < inst->sources; i++) {
1802 if (inst->src[i].file == VGRF)
1803 remap_table[inst->src[i].nr] = 0;
1804 }
1805 }
1806
1807 /* Compact the GRF arrays. */
1808 int new_index = 0;
1809 for (unsigned i = 0; i < this->alloc.count; i++) {
1810 if (remap_table[i] == -1) {
1811 /* We just found an unused register. This means that we are
1812 * actually going to compact something.
1813 */
1814 progress = true;
1815 } else {
1816 remap_table[i] = new_index;
1817 alloc.sizes[new_index] = alloc.sizes[i];
1818 invalidate_live_intervals();
1819 ++new_index;
1820 }
1821 }
1822
1823 this->alloc.count = new_index;
1824
1825 /* Patch all the instructions to use the newly renumbered registers */
1826 foreach_block_and_inst(block, fs_inst, inst, cfg) {
1827 if (inst->dst.file == VGRF)
1828 inst->dst.nr = remap_table[inst->dst.nr];
1829
1830 for (int i = 0; i < inst->sources; i++) {
1831 if (inst->src[i].file == VGRF)
1832 inst->src[i].nr = remap_table[inst->src[i].nr];
1833 }
1834 }
1835
1836 /* Patch all the references to delta_xy, since they're used in register
1837 * allocation. If they're unused, switch them to BAD_FILE so we don't
1838 * think some random VGRF is delta_xy.
1839 */
1840 for (unsigned i = 0; i < ARRAY_SIZE(delta_xy); i++) {
1841 if (delta_xy[i].file == VGRF) {
1842 if (remap_table[delta_xy[i].nr] != -1) {
1843 delta_xy[i].nr = remap_table[delta_xy[i].nr];
1844 } else {
1845 delta_xy[i].file = BAD_FILE;
1846 }
1847 }
1848 }
1849
1850 return progress;
1851 }
1852
1853 /**
1854 * Assign UNIFORM file registers to either push constants or pull constants.
1855 *
1856 * We allow a fragment shader to have more than the specified minimum
1857 * maximum number of fragment shader uniform components (64). If
1858 * there are too many of these, they'd fill up all of register space.
1859 * So, this will push some of them out to the pull constant buffer and
1860 * update the program to load them. We also use pull constants for all
1861 * indirect constant loads because we don't support indirect accesses in
1862 * registers yet.
1863 */
1864 void
1865 fs_visitor::assign_constant_locations()
1866 {
1867 /* Only the first compile (SIMD8 mode) gets to decide on locations. */
1868 if (dispatch_width != 8)
1869 return;
1870
1871 unsigned int num_pull_constants = 0;
1872
1873 pull_constant_loc = ralloc_array(mem_ctx, int, uniforms);
1874 memset(pull_constant_loc, -1, sizeof(pull_constant_loc[0]) * uniforms);
1875
1876 bool is_live[uniforms];
1877 memset(is_live, 0, sizeof(is_live));
1878
1879 /* First, we walk through the instructions and do two things:
1880 *
1881 * 1) Figure out which uniforms are live.
1882 *
1883 * 2) Find all indirect access of uniform arrays and flag them as needing
1884 * to go into the pull constant buffer.
1885 *
1886 * Note that we don't move constant-indexed accesses to arrays. No
1887 * testing has been done of the performance impact of this choice.
1888 */
1889 foreach_block_and_inst_safe(block, fs_inst, inst, cfg) {
1890 for (int i = 0 ; i < inst->sources; i++) {
1891 if (inst->src[i].file != UNIFORM)
1892 continue;
1893
1894 if (inst->src[i].reladdr) {
1895 int uniform = inst->src[i].nr;
1896
1897 /* If this array isn't already present in the pull constant buffer,
1898 * add it.
1899 */
1900 if (pull_constant_loc[uniform] == -1) {
1901 assert(param_size[uniform]);
1902 for (int j = 0; j < param_size[uniform]; j++)
1903 pull_constant_loc[uniform + j] = num_pull_constants++;
1904 }
1905 } else {
1906 /* Mark the the one accessed uniform as live */
1907 int constant_nr = inst->src[i].nr + inst->src[i].reg_offset;
1908 if (constant_nr >= 0 && constant_nr < (int) uniforms)
1909 is_live[constant_nr] = true;
1910 }
1911 }
1912 }
1913
1914 /* Only allow 16 registers (128 uniform components) as push constants.
1915 *
1916 * Just demote the end of the list. We could probably do better
1917 * here, demoting things that are rarely used in the program first.
1918 *
1919 * If changing this value, note the limitation about total_regs in
1920 * brw_curbe.c.
1921 */
1922 unsigned int max_push_components = 16 * 8;
1923 unsigned int num_push_constants = 0;
1924
1925 push_constant_loc = ralloc_array(mem_ctx, int, uniforms);
1926
1927 for (unsigned int i = 0; i < uniforms; i++) {
1928 if (!is_live[i] || pull_constant_loc[i] != -1) {
1929 /* This UNIFORM register is either dead, or has already been demoted
1930 * to a pull const. Mark it as no longer living in the param[] array.
1931 */
1932 push_constant_loc[i] = -1;
1933 continue;
1934 }
1935
1936 if (num_push_constants < max_push_components) {
1937 /* Retain as a push constant. Record the location in the params[]
1938 * array.
1939 */
1940 push_constant_loc[i] = num_push_constants++;
1941 } else {
1942 /* Demote to a pull constant. */
1943 push_constant_loc[i] = -1;
1944 pull_constant_loc[i] = num_pull_constants++;
1945 }
1946 }
1947
1948 stage_prog_data->nr_params = num_push_constants;
1949 stage_prog_data->nr_pull_params = num_pull_constants;
1950
1951 /* Up until now, the param[] array has been indexed by reg + reg_offset
1952 * of UNIFORM registers. Move pull constants into pull_param[] and
1953 * condense param[] to only contain the uniforms we chose to push.
1954 *
1955 * NOTE: Because we are condensing the params[] array, we know that
1956 * push_constant_loc[i] <= i and we can do it in one smooth loop without
1957 * having to make a copy.
1958 */
1959 for (unsigned int i = 0; i < uniforms; i++) {
1960 const gl_constant_value *value = stage_prog_data->param[i];
1961
1962 if (pull_constant_loc[i] != -1) {
1963 stage_prog_data->pull_param[pull_constant_loc[i]] = value;
1964 } else if (push_constant_loc[i] != -1) {
1965 stage_prog_data->param[push_constant_loc[i]] = value;
1966 }
1967 }
1968 }
1969
1970 /**
1971 * Replace UNIFORM register file access with either UNIFORM_PULL_CONSTANT_LOAD
1972 * or VARYING_PULL_CONSTANT_LOAD instructions which load values into VGRFs.
1973 */
1974 void
1975 fs_visitor::demote_pull_constants()
1976 {
1977 foreach_block_and_inst (block, fs_inst, inst, cfg) {
1978 for (int i = 0; i < inst->sources; i++) {
1979 if (inst->src[i].file != UNIFORM)
1980 continue;
1981
1982 int pull_index;
1983 unsigned location = inst->src[i].nr + inst->src[i].reg_offset;
1984 if (location >= uniforms) /* Out of bounds access */
1985 pull_index = -1;
1986 else
1987 pull_index = pull_constant_loc[location];
1988
1989 if (pull_index == -1)
1990 continue;
1991
1992 /* Set up the annotation tracking for new generated instructions. */
1993 const fs_builder ibld(this, block, inst);
1994 const unsigned index = stage_prog_data->binding_table.pull_constants_start;
1995 fs_reg dst = vgrf(glsl_type::float_type);
1996
1997 assert(inst->src[i].stride == 0);
1998
1999 /* Generate a pull load into dst. */
2000 if (inst->src[i].reladdr) {
2001 VARYING_PULL_CONSTANT_LOAD(ibld, dst,
2002 brw_imm_ud(index),
2003 *inst->src[i].reladdr,
2004 pull_index);
2005 inst->src[i].reladdr = NULL;
2006 inst->src[i].stride = 1;
2007 } else {
2008 const fs_builder ubld = ibld.exec_all().group(8, 0);
2009 struct brw_reg offset = brw_imm_ud((unsigned)(pull_index * 4) & ~15);
2010 ubld.emit(FS_OPCODE_UNIFORM_PULL_CONSTANT_LOAD,
2011 dst, brw_imm_ud(index), offset);
2012 inst->src[i].set_smear(pull_index & 3);
2013 }
2014 brw_mark_surface_used(prog_data, index);
2015
2016 /* Rewrite the instruction to use the temporary VGRF. */
2017 inst->src[i].file = VGRF;
2018 inst->src[i].nr = dst.nr;
2019 inst->src[i].reg_offset = 0;
2020 }
2021 }
2022 invalidate_live_intervals();
2023 }
2024
2025 bool
2026 fs_visitor::opt_algebraic()
2027 {
2028 bool progress = false;
2029
2030 foreach_block_and_inst(block, fs_inst, inst, cfg) {
2031 switch (inst->opcode) {
2032 case BRW_OPCODE_MOV:
2033 if (inst->src[0].file != IMM)
2034 break;
2035
2036 if (inst->saturate) {
2037 if (inst->dst.type != inst->src[0].type)
2038 assert(!"unimplemented: saturate mixed types");
2039
2040 if (brw_saturate_immediate(inst->dst.type, &inst->src[0])) {
2041 inst->saturate = false;
2042 progress = true;
2043 }
2044 }
2045 break;
2046
2047 case BRW_OPCODE_MUL:
2048 if (inst->src[1].file != IMM)
2049 continue;
2050
2051 /* a * 1.0 = a */
2052 if (inst->src[1].is_one()) {
2053 inst->opcode = BRW_OPCODE_MOV;
2054 inst->src[1] = reg_undef;
2055 progress = true;
2056 break;
2057 }
2058
2059 /* a * -1.0 = -a */
2060 if (inst->src[1].is_negative_one()) {
2061 inst->opcode = BRW_OPCODE_MOV;
2062 inst->src[0].negate = !inst->src[0].negate;
2063 inst->src[1] = reg_undef;
2064 progress = true;
2065 break;
2066 }
2067
2068 /* a * 0.0 = 0.0 */
2069 if (inst->src[1].is_zero()) {
2070 inst->opcode = BRW_OPCODE_MOV;
2071 inst->src[0] = inst->src[1];
2072 inst->src[1] = reg_undef;
2073 progress = true;
2074 break;
2075 }
2076
2077 if (inst->src[0].file == IMM) {
2078 assert(inst->src[0].type == BRW_REGISTER_TYPE_F);
2079 inst->opcode = BRW_OPCODE_MOV;
2080 inst->src[0].f *= inst->src[1].f;
2081 inst->src[1] = reg_undef;
2082 progress = true;
2083 break;
2084 }
2085 break;
2086 case BRW_OPCODE_ADD:
2087 if (inst->src[1].file != IMM)
2088 continue;
2089
2090 /* a + 0.0 = a */
2091 if (inst->src[1].is_zero()) {
2092 inst->opcode = BRW_OPCODE_MOV;
2093 inst->src[1] = reg_undef;
2094 progress = true;
2095 break;
2096 }
2097
2098 if (inst->src[0].file == IMM) {
2099 assert(inst->src[0].type == BRW_REGISTER_TYPE_F);
2100 inst->opcode = BRW_OPCODE_MOV;
2101 inst->src[0].f += inst->src[1].f;
2102 inst->src[1] = reg_undef;
2103 progress = true;
2104 break;
2105 }
2106 break;
2107 case BRW_OPCODE_OR:
2108 if (inst->src[0].equals(inst->src[1])) {
2109 inst->opcode = BRW_OPCODE_MOV;
2110 inst->src[1] = reg_undef;
2111 progress = true;
2112 break;
2113 }
2114 break;
2115 case BRW_OPCODE_LRP:
2116 if (inst->src[1].equals(inst->src[2])) {
2117 inst->opcode = BRW_OPCODE_MOV;
2118 inst->src[0] = inst->src[1];
2119 inst->src[1] = reg_undef;
2120 inst->src[2] = reg_undef;
2121 progress = true;
2122 break;
2123 }
2124 break;
2125 case BRW_OPCODE_CMP:
2126 if (inst->conditional_mod == BRW_CONDITIONAL_GE &&
2127 inst->src[0].abs &&
2128 inst->src[0].negate &&
2129 inst->src[1].is_zero()) {
2130 inst->src[0].abs = false;
2131 inst->src[0].negate = false;
2132 inst->conditional_mod = BRW_CONDITIONAL_Z;
2133 progress = true;
2134 break;
2135 }
2136 break;
2137 case BRW_OPCODE_SEL:
2138 if (inst->src[0].equals(inst->src[1])) {
2139 inst->opcode = BRW_OPCODE_MOV;
2140 inst->src[1] = reg_undef;
2141 inst->predicate = BRW_PREDICATE_NONE;
2142 inst->predicate_inverse = false;
2143 progress = true;
2144 } else if (inst->saturate && inst->src[1].file == IMM) {
2145 switch (inst->conditional_mod) {
2146 case BRW_CONDITIONAL_LE:
2147 case BRW_CONDITIONAL_L:
2148 switch (inst->src[1].type) {
2149 case BRW_REGISTER_TYPE_F:
2150 if (inst->src[1].f >= 1.0f) {
2151 inst->opcode = BRW_OPCODE_MOV;
2152 inst->src[1] = reg_undef;
2153 inst->conditional_mod = BRW_CONDITIONAL_NONE;
2154 progress = true;
2155 }
2156 break;
2157 default:
2158 break;
2159 }
2160 break;
2161 case BRW_CONDITIONAL_GE:
2162 case BRW_CONDITIONAL_G:
2163 switch (inst->src[1].type) {
2164 case BRW_REGISTER_TYPE_F:
2165 if (inst->src[1].f <= 0.0f) {
2166 inst->opcode = BRW_OPCODE_MOV;
2167 inst->src[1] = reg_undef;
2168 inst->conditional_mod = BRW_CONDITIONAL_NONE;
2169 progress = true;
2170 }
2171 break;
2172 default:
2173 break;
2174 }
2175 default:
2176 break;
2177 }
2178 }
2179 break;
2180 case BRW_OPCODE_MAD:
2181 if (inst->src[1].is_zero() || inst->src[2].is_zero()) {
2182 inst->opcode = BRW_OPCODE_MOV;
2183 inst->src[1] = reg_undef;
2184 inst->src[2] = reg_undef;
2185 progress = true;
2186 } else if (inst->src[0].is_zero()) {
2187 inst->opcode = BRW_OPCODE_MUL;
2188 inst->src[0] = inst->src[2];
2189 inst->src[2] = reg_undef;
2190 progress = true;
2191 } else if (inst->src[1].is_one()) {
2192 inst->opcode = BRW_OPCODE_ADD;
2193 inst->src[1] = inst->src[2];
2194 inst->src[2] = reg_undef;
2195 progress = true;
2196 } else if (inst->src[2].is_one()) {
2197 inst->opcode = BRW_OPCODE_ADD;
2198 inst->src[2] = reg_undef;
2199 progress = true;
2200 } else if (inst->src[1].file == IMM && inst->src[2].file == IMM) {
2201 inst->opcode = BRW_OPCODE_ADD;
2202 inst->src[1].f *= inst->src[2].f;
2203 inst->src[2] = reg_undef;
2204 progress = true;
2205 }
2206 break;
2207 case SHADER_OPCODE_RCP: {
2208 fs_inst *prev = (fs_inst *)inst->prev;
2209 if (prev->opcode == SHADER_OPCODE_SQRT) {
2210 if (inst->src[0].equals(prev->dst)) {
2211 inst->opcode = SHADER_OPCODE_RSQ;
2212 inst->src[0] = prev->src[0];
2213 progress = true;
2214 }
2215 }
2216 break;
2217 }
2218 case SHADER_OPCODE_BROADCAST:
2219 if (is_uniform(inst->src[0])) {
2220 inst->opcode = BRW_OPCODE_MOV;
2221 inst->sources = 1;
2222 inst->force_writemask_all = true;
2223 progress = true;
2224 } else if (inst->src[1].file == IMM) {
2225 inst->opcode = BRW_OPCODE_MOV;
2226 inst->src[0] = component(inst->src[0],
2227 inst->src[1].ud);
2228 inst->sources = 1;
2229 inst->force_writemask_all = true;
2230 progress = true;
2231 }
2232 break;
2233
2234 default:
2235 break;
2236 }
2237
2238 /* Swap if src[0] is immediate. */
2239 if (progress && inst->is_commutative()) {
2240 if (inst->src[0].file == IMM) {
2241 fs_reg tmp = inst->src[1];
2242 inst->src[1] = inst->src[0];
2243 inst->src[0] = tmp;
2244 }
2245 }
2246 }
2247 return progress;
2248 }
2249
2250 /**
2251 * Optimize sample messages that have constant zero values for the trailing
2252 * texture coordinates. We can just reduce the message length for these
2253 * instructions instead of reserving a register for it. Trailing parameters
2254 * that aren't sent default to zero anyway. This will cause the dead code
2255 * eliminator to remove the MOV instruction that would otherwise be emitted to
2256 * set up the zero value.
2257 */
2258 bool
2259 fs_visitor::opt_zero_samples()
2260 {
2261 /* Gen4 infers the texturing opcode based on the message length so we can't
2262 * change it.
2263 */
2264 if (devinfo->gen < 5)
2265 return false;
2266
2267 bool progress = false;
2268
2269 foreach_block_and_inst(block, fs_inst, inst, cfg) {
2270 if (!inst->is_tex())
2271 continue;
2272
2273 fs_inst *load_payload = (fs_inst *) inst->prev;
2274
2275 if (load_payload->is_head_sentinel() ||
2276 load_payload->opcode != SHADER_OPCODE_LOAD_PAYLOAD)
2277 continue;
2278
2279 /* We don't want to remove the message header or the first parameter.
2280 * Removing the first parameter is not allowed, see the Haswell PRM
2281 * volume 7, page 149:
2282 *
2283 * "Parameter 0 is required except for the sampleinfo message, which
2284 * has no parameter 0"
2285 */
2286 while (inst->mlen > inst->header_size + inst->exec_size / 8 &&
2287 load_payload->src[(inst->mlen - inst->header_size) /
2288 (inst->exec_size / 8) +
2289 inst->header_size - 1].is_zero()) {
2290 inst->mlen -= inst->exec_size / 8;
2291 progress = true;
2292 }
2293 }
2294
2295 if (progress)
2296 invalidate_live_intervals();
2297
2298 return progress;
2299 }
2300
2301 /**
2302 * Optimize sample messages which are followed by the final RT write.
2303 *
2304 * CHV, and GEN9+ can mark a texturing SEND instruction with EOT to have its
2305 * results sent directly to the framebuffer, bypassing the EU. Recognize the
2306 * final texturing results copied to the framebuffer write payload and modify
2307 * them to write to the framebuffer directly.
2308 */
2309 bool
2310 fs_visitor::opt_sampler_eot()
2311 {
2312 brw_wm_prog_key *key = (brw_wm_prog_key*) this->key;
2313
2314 if (stage != MESA_SHADER_FRAGMENT)
2315 return false;
2316
2317 if (devinfo->gen < 9 && !devinfo->is_cherryview)
2318 return false;
2319
2320 /* FINISHME: It should be possible to implement this optimization when there
2321 * are multiple drawbuffers.
2322 */
2323 if (key->nr_color_regions != 1)
2324 return false;
2325
2326 /* Look for a texturing instruction immediately before the final FB_WRITE. */
2327 bblock_t *block = cfg->blocks[cfg->num_blocks - 1];
2328 fs_inst *fb_write = (fs_inst *)block->end();
2329 assert(fb_write->eot);
2330 assert(fb_write->opcode == FS_OPCODE_FB_WRITE);
2331
2332 fs_inst *tex_inst = (fs_inst *) fb_write->prev;
2333
2334 /* There wasn't one; nothing to do. */
2335 if (unlikely(tex_inst->is_head_sentinel()) || !tex_inst->is_tex())
2336 return false;
2337
2338 /* 3D Sampler » Messages » Message Format
2339 *
2340 * “Response Length of zero is allowed on all SIMD8* and SIMD16* sampler
2341 * messages except sample+killpix, resinfo, sampleinfo, LOD, and gather4*”
2342 */
2343 if (tex_inst->opcode == SHADER_OPCODE_TXS ||
2344 tex_inst->opcode == SHADER_OPCODE_SAMPLEINFO ||
2345 tex_inst->opcode == SHADER_OPCODE_LOD ||
2346 tex_inst->opcode == SHADER_OPCODE_TG4 ||
2347 tex_inst->opcode == SHADER_OPCODE_TG4_OFFSET)
2348 return false;
2349
2350 /* If there's no header present, we need to munge the LOAD_PAYLOAD as well.
2351 * It's very likely to be the previous instruction.
2352 */
2353 fs_inst *load_payload = (fs_inst *) tex_inst->prev;
2354 if (load_payload->is_head_sentinel() ||
2355 load_payload->opcode != SHADER_OPCODE_LOAD_PAYLOAD)
2356 return false;
2357
2358 assert(!tex_inst->eot); /* We can't get here twice */
2359 assert((tex_inst->offset & (0xff << 24)) == 0);
2360
2361 const fs_builder ibld(this, block, tex_inst);
2362
2363 tex_inst->offset |= fb_write->target << 24;
2364 tex_inst->eot = true;
2365 tex_inst->dst = ibld.null_reg_ud();
2366 fb_write->remove(cfg->blocks[cfg->num_blocks - 1]);
2367
2368 /* If a header is present, marking the eot is sufficient. Otherwise, we need
2369 * to create a new LOAD_PAYLOAD command with the same sources and a space
2370 * saved for the header. Using a new destination register not only makes sure
2371 * we have enough space, but it will make sure the dead code eliminator kills
2372 * the instruction that this will replace.
2373 */
2374 if (tex_inst->header_size != 0)
2375 return true;
2376
2377 fs_reg send_header = ibld.vgrf(BRW_REGISTER_TYPE_F,
2378 load_payload->sources + 1);
2379 fs_reg *new_sources =
2380 ralloc_array(mem_ctx, fs_reg, load_payload->sources + 1);
2381
2382 new_sources[0] = fs_reg();
2383 for (int i = 0; i < load_payload->sources; i++)
2384 new_sources[i+1] = load_payload->src[i];
2385
2386 /* The LOAD_PAYLOAD helper seems like the obvious choice here. However, it
2387 * requires a lot of information about the sources to appropriately figure
2388 * out the number of registers needed to be used. Given this stage in our
2389 * optimization, we may not have the appropriate GRFs required by
2390 * LOAD_PAYLOAD at this point (copy propagation). Therefore, we need to
2391 * manually emit the instruction.
2392 */
2393 fs_inst *new_load_payload = new(mem_ctx) fs_inst(SHADER_OPCODE_LOAD_PAYLOAD,
2394 load_payload->exec_size,
2395 send_header,
2396 new_sources,
2397 load_payload->sources + 1);
2398
2399 new_load_payload->regs_written = load_payload->regs_written + 1;
2400 new_load_payload->header_size = 1;
2401 tex_inst->mlen++;
2402 tex_inst->header_size = 1;
2403 tex_inst->insert_before(cfg->blocks[cfg->num_blocks - 1], new_load_payload);
2404 tex_inst->src[0] = send_header;
2405
2406 return true;
2407 }
2408
2409 bool
2410 fs_visitor::opt_register_renaming()
2411 {
2412 bool progress = false;
2413 int depth = 0;
2414
2415 int remap[alloc.count];
2416 memset(remap, -1, sizeof(int) * alloc.count);
2417
2418 foreach_block_and_inst(block, fs_inst, inst, cfg) {
2419 if (inst->opcode == BRW_OPCODE_IF || inst->opcode == BRW_OPCODE_DO) {
2420 depth++;
2421 } else if (inst->opcode == BRW_OPCODE_ENDIF ||
2422 inst->opcode == BRW_OPCODE_WHILE) {
2423 depth--;
2424 }
2425
2426 /* Rewrite instruction sources. */
2427 for (int i = 0; i < inst->sources; i++) {
2428 if (inst->src[i].file == VGRF &&
2429 remap[inst->src[i].nr] != -1 &&
2430 remap[inst->src[i].nr] != inst->src[i].nr) {
2431 inst->src[i].nr = remap[inst->src[i].nr];
2432 progress = true;
2433 }
2434 }
2435
2436 const int dst = inst->dst.nr;
2437
2438 if (depth == 0 &&
2439 inst->dst.file == VGRF &&
2440 alloc.sizes[inst->dst.nr] == inst->exec_size / 8 &&
2441 !inst->is_partial_write()) {
2442 if (remap[dst] == -1) {
2443 remap[dst] = dst;
2444 } else {
2445 remap[dst] = alloc.allocate(inst->exec_size / 8);
2446 inst->dst.nr = remap[dst];
2447 progress = true;
2448 }
2449 } else if (inst->dst.file == VGRF &&
2450 remap[dst] != -1 &&
2451 remap[dst] != dst) {
2452 inst->dst.nr = remap[dst];
2453 progress = true;
2454 }
2455 }
2456
2457 if (progress) {
2458 invalidate_live_intervals();
2459
2460 for (unsigned i = 0; i < ARRAY_SIZE(delta_xy); i++) {
2461 if (delta_xy[i].file == VGRF && remap[delta_xy[i].nr] != -1) {
2462 delta_xy[i].nr = remap[delta_xy[i].nr];
2463 }
2464 }
2465 }
2466
2467 return progress;
2468 }
2469
2470 /**
2471 * Remove redundant or useless discard jumps.
2472 *
2473 * For example, we can eliminate jumps in the following sequence:
2474 *
2475 * discard-jump (redundant with the next jump)
2476 * discard-jump (useless; jumps to the next instruction)
2477 * placeholder-halt
2478 */
2479 bool
2480 fs_visitor::opt_redundant_discard_jumps()
2481 {
2482 bool progress = false;
2483
2484 bblock_t *last_bblock = cfg->blocks[cfg->num_blocks - 1];
2485
2486 fs_inst *placeholder_halt = NULL;
2487 foreach_inst_in_block_reverse(fs_inst, inst, last_bblock) {
2488 if (inst->opcode == FS_OPCODE_PLACEHOLDER_HALT) {
2489 placeholder_halt = inst;
2490 break;
2491 }
2492 }
2493
2494 if (!placeholder_halt)
2495 return false;
2496
2497 /* Delete any HALTs immediately before the placeholder halt. */
2498 for (fs_inst *prev = (fs_inst *) placeholder_halt->prev;
2499 !prev->is_head_sentinel() && prev->opcode == FS_OPCODE_DISCARD_JUMP;
2500 prev = (fs_inst *) placeholder_halt->prev) {
2501 prev->remove(last_bblock);
2502 progress = true;
2503 }
2504
2505 if (progress)
2506 invalidate_live_intervals();
2507
2508 return progress;
2509 }
2510
2511 bool
2512 fs_visitor::compute_to_mrf()
2513 {
2514 bool progress = false;
2515 int next_ip = 0;
2516
2517 /* No MRFs on Gen >= 7. */
2518 if (devinfo->gen >= 7)
2519 return false;
2520
2521 calculate_live_intervals();
2522
2523 foreach_block_and_inst_safe(block, fs_inst, inst, cfg) {
2524 int ip = next_ip;
2525 next_ip++;
2526
2527 if (inst->opcode != BRW_OPCODE_MOV ||
2528 inst->is_partial_write() ||
2529 inst->dst.file != MRF || inst->src[0].file != VGRF ||
2530 inst->dst.type != inst->src[0].type ||
2531 inst->src[0].abs || inst->src[0].negate ||
2532 !inst->src[0].is_contiguous() ||
2533 inst->src[0].subreg_offset)
2534 continue;
2535
2536 /* Work out which hardware MRF registers are written by this
2537 * instruction.
2538 */
2539 int mrf_low = inst->dst.nr & ~BRW_MRF_COMPR4;
2540 int mrf_high;
2541 if (inst->dst.nr & BRW_MRF_COMPR4) {
2542 mrf_high = mrf_low + 4;
2543 } else if (inst->exec_size == 16) {
2544 mrf_high = mrf_low + 1;
2545 } else {
2546 mrf_high = mrf_low;
2547 }
2548
2549 /* Can't compute-to-MRF this GRF if someone else was going to
2550 * read it later.
2551 */
2552 if (this->virtual_grf_end[inst->src[0].nr] > ip)
2553 continue;
2554
2555 /* Found a move of a GRF to a MRF. Let's see if we can go
2556 * rewrite the thing that made this GRF to write into the MRF.
2557 */
2558 foreach_inst_in_block_reverse_starting_from(fs_inst, scan_inst, inst) {
2559 if (scan_inst->dst.file == VGRF &&
2560 scan_inst->dst.nr == inst->src[0].nr) {
2561 /* Found the last thing to write our reg we want to turn
2562 * into a compute-to-MRF.
2563 */
2564
2565 /* If this one instruction didn't populate all the
2566 * channels, bail. We might be able to rewrite everything
2567 * that writes that reg, but it would require smarter
2568 * tracking to delay the rewriting until complete success.
2569 */
2570 if (scan_inst->is_partial_write())
2571 break;
2572
2573 /* Things returning more than one register would need us to
2574 * understand coalescing out more than one MOV at a time.
2575 */
2576 if (scan_inst->regs_written > scan_inst->exec_size / 8)
2577 break;
2578
2579 /* SEND instructions can't have MRF as a destination. */
2580 if (scan_inst->mlen)
2581 break;
2582
2583 if (devinfo->gen == 6) {
2584 /* gen6 math instructions must have the destination be
2585 * GRF, so no compute-to-MRF for them.
2586 */
2587 if (scan_inst->is_math()) {
2588 break;
2589 }
2590 }
2591
2592 if (scan_inst->dst.reg_offset == inst->src[0].reg_offset) {
2593 /* Found the creator of our MRF's source value. */
2594 scan_inst->dst.file = MRF;
2595 scan_inst->dst.nr = inst->dst.nr;
2596 scan_inst->saturate |= inst->saturate;
2597 inst->remove(block);
2598 progress = true;
2599 }
2600 break;
2601 }
2602
2603 /* We don't handle control flow here. Most computation of
2604 * values that end up in MRFs are shortly before the MRF
2605 * write anyway.
2606 */
2607 if (block->start() == scan_inst)
2608 break;
2609
2610 /* You can't read from an MRF, so if someone else reads our
2611 * MRF's source GRF that we wanted to rewrite, that stops us.
2612 */
2613 bool interfered = false;
2614 for (int i = 0; i < scan_inst->sources; i++) {
2615 if (scan_inst->src[i].file == VGRF &&
2616 scan_inst->src[i].nr == inst->src[0].nr &&
2617 scan_inst->src[i].reg_offset == inst->src[0].reg_offset) {
2618 interfered = true;
2619 }
2620 }
2621 if (interfered)
2622 break;
2623
2624 if (scan_inst->dst.file == MRF) {
2625 /* If somebody else writes our MRF here, we can't
2626 * compute-to-MRF before that.
2627 */
2628 int scan_mrf_low = scan_inst->dst.nr & ~BRW_MRF_COMPR4;
2629 int scan_mrf_high;
2630
2631 if (scan_inst->dst.nr & BRW_MRF_COMPR4) {
2632 scan_mrf_high = scan_mrf_low + 4;
2633 } else if (scan_inst->exec_size == 16) {
2634 scan_mrf_high = scan_mrf_low + 1;
2635 } else {
2636 scan_mrf_high = scan_mrf_low;
2637 }
2638
2639 if (mrf_low == scan_mrf_low ||
2640 mrf_low == scan_mrf_high ||
2641 mrf_high == scan_mrf_low ||
2642 mrf_high == scan_mrf_high) {
2643 break;
2644 }
2645 }
2646
2647 if (scan_inst->mlen > 0 && scan_inst->base_mrf != -1) {
2648 /* Found a SEND instruction, which means that there are
2649 * live values in MRFs from base_mrf to base_mrf +
2650 * scan_inst->mlen - 1. Don't go pushing our MRF write up
2651 * above it.
2652 */
2653 if (mrf_low >= scan_inst->base_mrf &&
2654 mrf_low < scan_inst->base_mrf + scan_inst->mlen) {
2655 break;
2656 }
2657 if (mrf_high >= scan_inst->base_mrf &&
2658 mrf_high < scan_inst->base_mrf + scan_inst->mlen) {
2659 break;
2660 }
2661 }
2662 }
2663 }
2664
2665 if (progress)
2666 invalidate_live_intervals();
2667
2668 return progress;
2669 }
2670
2671 /**
2672 * Eliminate FIND_LIVE_CHANNEL instructions occurring outside any control
2673 * flow. We could probably do better here with some form of divergence
2674 * analysis.
2675 */
2676 bool
2677 fs_visitor::eliminate_find_live_channel()
2678 {
2679 bool progress = false;
2680 unsigned depth = 0;
2681
2682 foreach_block_and_inst_safe(block, fs_inst, inst, cfg) {
2683 switch (inst->opcode) {
2684 case BRW_OPCODE_IF:
2685 case BRW_OPCODE_DO:
2686 depth++;
2687 break;
2688
2689 case BRW_OPCODE_ENDIF:
2690 case BRW_OPCODE_WHILE:
2691 depth--;
2692 break;
2693
2694 case FS_OPCODE_DISCARD_JUMP:
2695 /* This can potentially make control flow non-uniform until the end
2696 * of the program.
2697 */
2698 return progress;
2699
2700 case SHADER_OPCODE_FIND_LIVE_CHANNEL:
2701 if (depth == 0) {
2702 inst->opcode = BRW_OPCODE_MOV;
2703 inst->src[0] = brw_imm_ud(0u);
2704 inst->sources = 1;
2705 inst->force_writemask_all = true;
2706 progress = true;
2707 }
2708 break;
2709
2710 default:
2711 break;
2712 }
2713 }
2714
2715 return progress;
2716 }
2717
2718 /**
2719 * Once we've generated code, try to convert normal FS_OPCODE_FB_WRITE
2720 * instructions to FS_OPCODE_REP_FB_WRITE.
2721 */
2722 void
2723 fs_visitor::emit_repclear_shader()
2724 {
2725 brw_wm_prog_key *key = (brw_wm_prog_key*) this->key;
2726 int base_mrf = 1;
2727 int color_mrf = base_mrf + 2;
2728
2729 fs_inst *mov = bld.exec_all().group(4, 0)
2730 .MOV(brw_message_reg(color_mrf),
2731 fs_reg(UNIFORM, 0, BRW_REGISTER_TYPE_F));
2732
2733 fs_inst *write;
2734 if (key->nr_color_regions == 1) {
2735 write = bld.emit(FS_OPCODE_REP_FB_WRITE);
2736 write->saturate = key->clamp_fragment_color;
2737 write->base_mrf = color_mrf;
2738 write->target = 0;
2739 write->header_size = 0;
2740 write->mlen = 1;
2741 } else {
2742 assume(key->nr_color_regions > 0);
2743 for (int i = 0; i < key->nr_color_regions; ++i) {
2744 write = bld.emit(FS_OPCODE_REP_FB_WRITE);
2745 write->saturate = key->clamp_fragment_color;
2746 write->base_mrf = base_mrf;
2747 write->target = i;
2748 write->header_size = 2;
2749 write->mlen = 3;
2750 }
2751 }
2752 write->eot = true;
2753
2754 calculate_cfg();
2755
2756 assign_constant_locations();
2757 assign_curb_setup();
2758
2759 /* Now that we have the uniform assigned, go ahead and force it to a vec4. */
2760 assert(mov->src[0].file == FIXED_GRF);
2761 mov->src[0] = brw_vec4_grf(mov->src[0].nr, 0);
2762 }
2763
2764 /**
2765 * Walks through basic blocks, looking for repeated MRF writes and
2766 * removing the later ones.
2767 */
2768 bool
2769 fs_visitor::remove_duplicate_mrf_writes()
2770 {
2771 fs_inst *last_mrf_move[BRW_MAX_MRF(devinfo->gen)];
2772 bool progress = false;
2773
2774 /* Need to update the MRF tracking for compressed instructions. */
2775 if (dispatch_width == 16)
2776 return false;
2777
2778 memset(last_mrf_move, 0, sizeof(last_mrf_move));
2779
2780 foreach_block_and_inst_safe (block, fs_inst, inst, cfg) {
2781 if (inst->is_control_flow()) {
2782 memset(last_mrf_move, 0, sizeof(last_mrf_move));
2783 }
2784
2785 if (inst->opcode == BRW_OPCODE_MOV &&
2786 inst->dst.file == MRF) {
2787 fs_inst *prev_inst = last_mrf_move[inst->dst.nr];
2788 if (prev_inst && inst->equals(prev_inst)) {
2789 inst->remove(block);
2790 progress = true;
2791 continue;
2792 }
2793 }
2794
2795 /* Clear out the last-write records for MRFs that were overwritten. */
2796 if (inst->dst.file == MRF) {
2797 last_mrf_move[inst->dst.nr] = NULL;
2798 }
2799
2800 if (inst->mlen > 0 && inst->base_mrf != -1) {
2801 /* Found a SEND instruction, which will include two or fewer
2802 * implied MRF writes. We could do better here.
2803 */
2804 for (int i = 0; i < implied_mrf_writes(inst); i++) {
2805 last_mrf_move[inst->base_mrf + i] = NULL;
2806 }
2807 }
2808
2809 /* Clear out any MRF move records whose sources got overwritten. */
2810 if (inst->dst.file == VGRF) {
2811 for (unsigned int i = 0; i < ARRAY_SIZE(last_mrf_move); i++) {
2812 if (last_mrf_move[i] &&
2813 last_mrf_move[i]->src[0].nr == inst->dst.nr) {
2814 last_mrf_move[i] = NULL;
2815 }
2816 }
2817 }
2818
2819 if (inst->opcode == BRW_OPCODE_MOV &&
2820 inst->dst.file == MRF &&
2821 inst->src[0].file == VGRF &&
2822 !inst->is_partial_write()) {
2823 last_mrf_move[inst->dst.nr] = inst;
2824 }
2825 }
2826
2827 if (progress)
2828 invalidate_live_intervals();
2829
2830 return progress;
2831 }
2832
2833 static void
2834 clear_deps_for_inst_src(fs_inst *inst, bool *deps, int first_grf, int grf_len)
2835 {
2836 /* Clear the flag for registers that actually got read (as expected). */
2837 for (int i = 0; i < inst->sources; i++) {
2838 int grf;
2839 if (inst->src[i].file == VGRF || inst->src[i].file == FIXED_GRF) {
2840 grf = inst->src[i].nr;
2841 } else {
2842 continue;
2843 }
2844
2845 if (grf >= first_grf &&
2846 grf < first_grf + grf_len) {
2847 deps[grf - first_grf] = false;
2848 if (inst->exec_size == 16)
2849 deps[grf - first_grf + 1] = false;
2850 }
2851 }
2852 }
2853
2854 /**
2855 * Implements this workaround for the original 965:
2856 *
2857 * "[DevBW, DevCL] Implementation Restrictions: As the hardware does not
2858 * check for post destination dependencies on this instruction, software
2859 * must ensure that there is no destination hazard for the case of ‘write
2860 * followed by a posted write’ shown in the following example.
2861 *
2862 * 1. mov r3 0
2863 * 2. send r3.xy <rest of send instruction>
2864 * 3. mov r2 r3
2865 *
2866 * Due to no post-destination dependency check on the ‘send’, the above
2867 * code sequence could have two instructions (1 and 2) in flight at the
2868 * same time that both consider ‘r3’ as the target of their final writes.
2869 */
2870 void
2871 fs_visitor::insert_gen4_pre_send_dependency_workarounds(bblock_t *block,
2872 fs_inst *inst)
2873 {
2874 int write_len = inst->regs_written;
2875 int first_write_grf = inst->dst.nr;
2876 bool needs_dep[BRW_MAX_MRF(devinfo->gen)];
2877 assert(write_len < (int)sizeof(needs_dep) - 1);
2878
2879 memset(needs_dep, false, sizeof(needs_dep));
2880 memset(needs_dep, true, write_len);
2881
2882 clear_deps_for_inst_src(inst, needs_dep, first_write_grf, write_len);
2883
2884 /* Walk backwards looking for writes to registers we're writing which
2885 * aren't read since being written. If we hit the start of the program,
2886 * we assume that there are no outstanding dependencies on entry to the
2887 * program.
2888 */
2889 foreach_inst_in_block_reverse_starting_from(fs_inst, scan_inst, inst) {
2890 /* If we hit control flow, assume that there *are* outstanding
2891 * dependencies, and force their cleanup before our instruction.
2892 */
2893 if (block->start() == scan_inst) {
2894 for (int i = 0; i < write_len; i++) {
2895 if (needs_dep[i])
2896 DEP_RESOLVE_MOV(fs_builder(this, block, inst),
2897 first_write_grf + i);
2898 }
2899 return;
2900 }
2901
2902 /* We insert our reads as late as possible on the assumption that any
2903 * instruction but a MOV that might have left us an outstanding
2904 * dependency has more latency than a MOV.
2905 */
2906 if (scan_inst->dst.file == VGRF) {
2907 for (int i = 0; i < scan_inst->regs_written; i++) {
2908 int reg = scan_inst->dst.nr + i;
2909
2910 if (reg >= first_write_grf &&
2911 reg < first_write_grf + write_len &&
2912 needs_dep[reg - first_write_grf]) {
2913 DEP_RESOLVE_MOV(fs_builder(this, block, inst), reg);
2914 needs_dep[reg - first_write_grf] = false;
2915 if (scan_inst->exec_size == 16)
2916 needs_dep[reg - first_write_grf + 1] = false;
2917 }
2918 }
2919 }
2920
2921 /* Clear the flag for registers that actually got read (as expected). */
2922 clear_deps_for_inst_src(scan_inst, needs_dep, first_write_grf, write_len);
2923
2924 /* Continue the loop only if we haven't resolved all the dependencies */
2925 int i;
2926 for (i = 0; i < write_len; i++) {
2927 if (needs_dep[i])
2928 break;
2929 }
2930 if (i == write_len)
2931 return;
2932 }
2933 }
2934
2935 /**
2936 * Implements this workaround for the original 965:
2937 *
2938 * "[DevBW, DevCL] Errata: A destination register from a send can not be
2939 * used as a destination register until after it has been sourced by an
2940 * instruction with a different destination register.
2941 */
2942 void
2943 fs_visitor::insert_gen4_post_send_dependency_workarounds(bblock_t *block, fs_inst *inst)
2944 {
2945 int write_len = inst->regs_written;
2946 int first_write_grf = inst->dst.nr;
2947 bool needs_dep[BRW_MAX_MRF(devinfo->gen)];
2948 assert(write_len < (int)sizeof(needs_dep) - 1);
2949
2950 memset(needs_dep, false, sizeof(needs_dep));
2951 memset(needs_dep, true, write_len);
2952 /* Walk forwards looking for writes to registers we're writing which aren't
2953 * read before being written.
2954 */
2955 foreach_inst_in_block_starting_from(fs_inst, scan_inst, inst) {
2956 /* If we hit control flow, force resolve all remaining dependencies. */
2957 if (block->end() == scan_inst) {
2958 for (int i = 0; i < write_len; i++) {
2959 if (needs_dep[i])
2960 DEP_RESOLVE_MOV(fs_builder(this, block, scan_inst),
2961 first_write_grf + i);
2962 }
2963 return;
2964 }
2965
2966 /* Clear the flag for registers that actually got read (as expected). */
2967 clear_deps_for_inst_src(scan_inst, needs_dep, first_write_grf, write_len);
2968
2969 /* We insert our reads as late as possible since they're reading the
2970 * result of a SEND, which has massive latency.
2971 */
2972 if (scan_inst->dst.file == VGRF &&
2973 scan_inst->dst.nr >= first_write_grf &&
2974 scan_inst->dst.nr < first_write_grf + write_len &&
2975 needs_dep[scan_inst->dst.nr - first_write_grf]) {
2976 DEP_RESOLVE_MOV(fs_builder(this, block, scan_inst),
2977 scan_inst->dst.nr);
2978 needs_dep[scan_inst->dst.nr - first_write_grf] = false;
2979 }
2980
2981 /* Continue the loop only if we haven't resolved all the dependencies */
2982 int i;
2983 for (i = 0; i < write_len; i++) {
2984 if (needs_dep[i])
2985 break;
2986 }
2987 if (i == write_len)
2988 return;
2989 }
2990 }
2991
2992 void
2993 fs_visitor::insert_gen4_send_dependency_workarounds()
2994 {
2995 if (devinfo->gen != 4 || devinfo->is_g4x)
2996 return;
2997
2998 bool progress = false;
2999
3000 /* Note that we're done with register allocation, so GRF fs_regs always
3001 * have a .reg_offset of 0.
3002 */
3003
3004 foreach_block_and_inst(block, fs_inst, inst, cfg) {
3005 if (inst->mlen != 0 && inst->dst.file == VGRF) {
3006 insert_gen4_pre_send_dependency_workarounds(block, inst);
3007 insert_gen4_post_send_dependency_workarounds(block, inst);
3008 progress = true;
3009 }
3010 }
3011
3012 if (progress)
3013 invalidate_live_intervals();
3014 }
3015
3016 /**
3017 * Turns the generic expression-style uniform pull constant load instruction
3018 * into a hardware-specific series of instructions for loading a pull
3019 * constant.
3020 *
3021 * The expression style allows the CSE pass before this to optimize out
3022 * repeated loads from the same offset, and gives the pre-register-allocation
3023 * scheduling full flexibility, while the conversion to native instructions
3024 * allows the post-register-allocation scheduler the best information
3025 * possible.
3026 *
3027 * Note that execution masking for setting up pull constant loads is special:
3028 * the channels that need to be written are unrelated to the current execution
3029 * mask, since a later instruction will use one of the result channels as a
3030 * source operand for all 8 or 16 of its channels.
3031 */
3032 void
3033 fs_visitor::lower_uniform_pull_constant_loads()
3034 {
3035 foreach_block_and_inst (block, fs_inst, inst, cfg) {
3036 if (inst->opcode != FS_OPCODE_UNIFORM_PULL_CONSTANT_LOAD)
3037 continue;
3038
3039 if (devinfo->gen >= 7) {
3040 /* The offset arg before was a vec4-aligned byte offset. We need to
3041 * turn it into a dword offset.
3042 */
3043 fs_reg const_offset_reg = inst->src[1];
3044 assert(const_offset_reg.file == IMM &&
3045 const_offset_reg.type == BRW_REGISTER_TYPE_UD);
3046 const_offset_reg.ud /= 4;
3047
3048 fs_reg payload, offset;
3049 if (devinfo->gen >= 9) {
3050 /* We have to use a message header on Skylake to get SIMD4x2
3051 * mode. Reserve space for the register.
3052 */
3053 offset = payload = fs_reg(VGRF, alloc.allocate(2));
3054 offset.reg_offset++;
3055 inst->mlen = 2;
3056 } else {
3057 offset = payload = fs_reg(VGRF, alloc.allocate(1));
3058 inst->mlen = 1;
3059 }
3060
3061 /* This is actually going to be a MOV, but since only the first dword
3062 * is accessed, we have a special opcode to do just that one. Note
3063 * that this needs to be an operation that will be considered a def
3064 * by live variable analysis, or register allocation will explode.
3065 */
3066 fs_inst *setup = new(mem_ctx) fs_inst(FS_OPCODE_SET_SIMD4X2_OFFSET,
3067 8, offset, const_offset_reg);
3068 setup->force_writemask_all = true;
3069
3070 setup->ir = inst->ir;
3071 setup->annotation = inst->annotation;
3072 inst->insert_before(block, setup);
3073
3074 /* Similarly, this will only populate the first 4 channels of the
3075 * result register (since we only use smear values from 0-3), but we
3076 * don't tell the optimizer.
3077 */
3078 inst->opcode = FS_OPCODE_UNIFORM_PULL_CONSTANT_LOAD_GEN7;
3079 inst->src[1] = payload;
3080 inst->base_mrf = -1;
3081
3082 invalidate_live_intervals();
3083 } else {
3084 /* Before register allocation, we didn't tell the scheduler about the
3085 * MRF we use. We know it's safe to use this MRF because nothing
3086 * else does except for register spill/unspill, which generates and
3087 * uses its MRF within a single IR instruction.
3088 */
3089 inst->base_mrf = FIRST_PULL_LOAD_MRF(devinfo->gen) + 1;
3090 inst->mlen = 1;
3091 }
3092 }
3093 }
3094
3095 bool
3096 fs_visitor::lower_load_payload()
3097 {
3098 bool progress = false;
3099
3100 foreach_block_and_inst_safe (block, fs_inst, inst, cfg) {
3101 if (inst->opcode != SHADER_OPCODE_LOAD_PAYLOAD)
3102 continue;
3103
3104 assert(inst->dst.file == MRF || inst->dst.file == VGRF);
3105 assert(inst->saturate == false);
3106 fs_reg dst = inst->dst;
3107
3108 /* Get rid of COMPR4. We'll add it back in if we need it */
3109 if (dst.file == MRF)
3110 dst.nr = dst.nr & ~BRW_MRF_COMPR4;
3111
3112 const fs_builder ibld(this, block, inst);
3113 const fs_builder hbld = ibld.exec_all().group(8, 0);
3114
3115 for (uint8_t i = 0; i < inst->header_size; i++) {
3116 if (inst->src[i].file != BAD_FILE) {
3117 fs_reg mov_dst = retype(dst, BRW_REGISTER_TYPE_UD);
3118 fs_reg mov_src = retype(inst->src[i], BRW_REGISTER_TYPE_UD);
3119 hbld.MOV(mov_dst, mov_src);
3120 }
3121 dst = offset(dst, hbld, 1);
3122 }
3123
3124 if (inst->dst.file == MRF && (inst->dst.nr & BRW_MRF_COMPR4) &&
3125 inst->exec_size > 8) {
3126 /* In this case, the payload portion of the LOAD_PAYLOAD isn't
3127 * a straightforward copy. Instead, the result of the
3128 * LOAD_PAYLOAD is treated as interleaved and the first four
3129 * non-header sources are unpacked as:
3130 *
3131 * m + 0: r0
3132 * m + 1: g0
3133 * m + 2: b0
3134 * m + 3: a0
3135 * m + 4: r1
3136 * m + 5: g1
3137 * m + 6: b1
3138 * m + 7: a1
3139 *
3140 * This is used for gen <= 5 fb writes.
3141 */
3142 assert(inst->exec_size == 16);
3143 assert(inst->header_size + 4 <= inst->sources);
3144 for (uint8_t i = inst->header_size; i < inst->header_size + 4; i++) {
3145 if (inst->src[i].file != BAD_FILE) {
3146 if (devinfo->has_compr4) {
3147 fs_reg compr4_dst = retype(dst, inst->src[i].type);
3148 compr4_dst.nr |= BRW_MRF_COMPR4;
3149 ibld.MOV(compr4_dst, inst->src[i]);
3150 } else {
3151 /* Platform doesn't have COMPR4. We have to fake it */
3152 fs_reg mov_dst = retype(dst, inst->src[i].type);
3153 ibld.half(0).MOV(mov_dst, half(inst->src[i], 0));
3154 mov_dst.nr += 4;
3155 ibld.half(1).MOV(mov_dst, half(inst->src[i], 1));
3156 }
3157 }
3158
3159 dst.nr++;
3160 }
3161
3162 /* The loop above only ever incremented us through the first set
3163 * of 4 registers. However, thanks to the magic of COMPR4, we
3164 * actually wrote to the first 8 registers, so we need to take
3165 * that into account now.
3166 */
3167 dst.nr += 4;
3168
3169 /* The COMPR4 code took care of the first 4 sources. We'll let
3170 * the regular path handle any remaining sources. Yes, we are
3171 * modifying the instruction but we're about to delete it so
3172 * this really doesn't hurt anything.
3173 */
3174 inst->header_size += 4;
3175 }
3176
3177 for (uint8_t i = inst->header_size; i < inst->sources; i++) {
3178 if (inst->src[i].file != BAD_FILE)
3179 ibld.MOV(retype(dst, inst->src[i].type), inst->src[i]);
3180 dst = offset(dst, ibld, 1);
3181 }
3182
3183 inst->remove(block);
3184 progress = true;
3185 }
3186
3187 if (progress)
3188 invalidate_live_intervals();
3189
3190 return progress;
3191 }
3192
3193 bool
3194 fs_visitor::lower_integer_multiplication()
3195 {
3196 bool progress = false;
3197
3198 foreach_block_and_inst_safe(block, fs_inst, inst, cfg) {
3199 const fs_builder ibld(this, block, inst);
3200
3201 if (inst->opcode == BRW_OPCODE_MUL) {
3202 if (inst->dst.is_accumulator() ||
3203 (inst->dst.type != BRW_REGISTER_TYPE_D &&
3204 inst->dst.type != BRW_REGISTER_TYPE_UD))
3205 continue;
3206
3207 /* Gen8's MUL instruction can do a 32-bit x 32-bit -> 32-bit
3208 * operation directly, but CHV/BXT cannot.
3209 */
3210 if (devinfo->gen >= 8 &&
3211 !devinfo->is_cherryview && !devinfo->is_broxton)
3212 continue;
3213
3214 if (inst->src[1].file == IMM &&
3215 inst->src[1].ud < (1 << 16)) {
3216 /* The MUL instruction isn't commutative. On Gen <= 6, only the low
3217 * 16-bits of src0 are read, and on Gen >= 7 only the low 16-bits of
3218 * src1 are used.
3219 *
3220 * If multiplying by an immediate value that fits in 16-bits, do a
3221 * single MUL instruction with that value in the proper location.
3222 */
3223 if (devinfo->gen < 7) {
3224 fs_reg imm(VGRF, alloc.allocate(dispatch_width / 8),
3225 inst->dst.type);
3226 ibld.MOV(imm, inst->src[1]);
3227 ibld.MUL(inst->dst, imm, inst->src[0]);
3228 } else {
3229 ibld.MUL(inst->dst, inst->src[0], inst->src[1]);
3230 }
3231 } else {
3232 /* Gen < 8 (and some Gen8+ low-power parts like Cherryview) cannot
3233 * do 32-bit integer multiplication in one instruction, but instead
3234 * must do a sequence (which actually calculates a 64-bit result):
3235 *
3236 * mul(8) acc0<1>D g3<8,8,1>D g4<8,8,1>D
3237 * mach(8) null g3<8,8,1>D g4<8,8,1>D
3238 * mov(8) g2<1>D acc0<8,8,1>D
3239 *
3240 * But on Gen > 6, the ability to use second accumulator register
3241 * (acc1) for non-float data types was removed, preventing a simple
3242 * implementation in SIMD16. A 16-channel result can be calculated by
3243 * executing the three instructions twice in SIMD8, once with quarter
3244 * control of 1Q for the first eight channels and again with 2Q for
3245 * the second eight channels.
3246 *
3247 * Which accumulator register is implicitly accessed (by AccWrEnable
3248 * for instance) is determined by the quarter control. Unfortunately
3249 * Ivybridge (and presumably Baytrail) has a hardware bug in which an
3250 * implicit accumulator access by an instruction with 2Q will access
3251 * acc1 regardless of whether the data type is usable in acc1.
3252 *
3253 * Specifically, the 2Q mach(8) writes acc1 which does not exist for
3254 * integer data types.
3255 *
3256 * Since we only want the low 32-bits of the result, we can do two
3257 * 32-bit x 16-bit multiplies (like the mul and mach are doing), and
3258 * adjust the high result and add them (like the mach is doing):
3259 *
3260 * mul(8) g7<1>D g3<8,8,1>D g4.0<8,8,1>UW
3261 * mul(8) g8<1>D g3<8,8,1>D g4.1<8,8,1>UW
3262 * shl(8) g9<1>D g8<8,8,1>D 16D
3263 * add(8) g2<1>D g7<8,8,1>D g8<8,8,1>D
3264 *
3265 * We avoid the shl instruction by realizing that we only want to add
3266 * the low 16-bits of the "high" result to the high 16-bits of the
3267 * "low" result and using proper regioning on the add:
3268 *
3269 * mul(8) g7<1>D g3<8,8,1>D g4.0<16,8,2>UW
3270 * mul(8) g8<1>D g3<8,8,1>D g4.1<16,8,2>UW
3271 * add(8) g7.1<2>UW g7.1<16,8,2>UW g8<16,8,2>UW
3272 *
3273 * Since it does not use the (single) accumulator register, we can
3274 * schedule multi-component multiplications much better.
3275 */
3276
3277 fs_reg orig_dst = inst->dst;
3278 if (orig_dst.is_null() || orig_dst.file == MRF) {
3279 inst->dst = fs_reg(VGRF, alloc.allocate(dispatch_width / 8),
3280 inst->dst.type);
3281 }
3282 fs_reg low = inst->dst;
3283 fs_reg high(VGRF, alloc.allocate(dispatch_width / 8),
3284 inst->dst.type);
3285
3286 if (devinfo->gen >= 7) {
3287 fs_reg src1_0_w = inst->src[1];
3288 fs_reg src1_1_w = inst->src[1];
3289
3290 if (inst->src[1].file == IMM) {
3291 src1_0_w.ud &= 0xffff;
3292 src1_1_w.ud >>= 16;
3293 } else {
3294 src1_0_w.type = BRW_REGISTER_TYPE_UW;
3295 if (src1_0_w.stride != 0) {
3296 assert(src1_0_w.stride == 1);
3297 src1_0_w.stride = 2;
3298 }
3299
3300 src1_1_w.type = BRW_REGISTER_TYPE_UW;
3301 if (src1_1_w.stride != 0) {
3302 assert(src1_1_w.stride == 1);
3303 src1_1_w.stride = 2;
3304 }
3305 src1_1_w.subreg_offset += type_sz(BRW_REGISTER_TYPE_UW);
3306 }
3307 ibld.MUL(low, inst->src[0], src1_0_w);
3308 ibld.MUL(high, inst->src[0], src1_1_w);
3309 } else {
3310 fs_reg src0_0_w = inst->src[0];
3311 fs_reg src0_1_w = inst->src[0];
3312
3313 src0_0_w.type = BRW_REGISTER_TYPE_UW;
3314 if (src0_0_w.stride != 0) {
3315 assert(src0_0_w.stride == 1);
3316 src0_0_w.stride = 2;
3317 }
3318
3319 src0_1_w.type = BRW_REGISTER_TYPE_UW;
3320 if (src0_1_w.stride != 0) {
3321 assert(src0_1_w.stride == 1);
3322 src0_1_w.stride = 2;
3323 }
3324 src0_1_w.subreg_offset += type_sz(BRW_REGISTER_TYPE_UW);
3325
3326 ibld.MUL(low, src0_0_w, inst->src[1]);
3327 ibld.MUL(high, src0_1_w, inst->src[1]);
3328 }
3329
3330 fs_reg dst = inst->dst;
3331 dst.type = BRW_REGISTER_TYPE_UW;
3332 dst.subreg_offset = 2;
3333 dst.stride = 2;
3334
3335 high.type = BRW_REGISTER_TYPE_UW;
3336 high.stride = 2;
3337
3338 low.type = BRW_REGISTER_TYPE_UW;
3339 low.subreg_offset = 2;
3340 low.stride = 2;
3341
3342 ibld.ADD(dst, low, high);
3343
3344 if (inst->conditional_mod || orig_dst.file == MRF) {
3345 set_condmod(inst->conditional_mod,
3346 ibld.MOV(orig_dst, inst->dst));
3347 }
3348 }
3349
3350 } else if (inst->opcode == SHADER_OPCODE_MULH) {
3351 /* Should have been lowered to 8-wide. */
3352 assert(inst->exec_size <= 8);
3353 const fs_reg acc = retype(brw_acc_reg(inst->exec_size),
3354 inst->dst.type);
3355 fs_inst *mul = ibld.MUL(acc, inst->src[0], inst->src[1]);
3356 fs_inst *mach = ibld.MACH(inst->dst, inst->src[0], inst->src[1]);
3357
3358 if (devinfo->gen >= 8) {
3359 /* Until Gen8, integer multiplies read 32-bits from one source,
3360 * and 16-bits from the other, and relying on the MACH instruction
3361 * to generate the high bits of the result.
3362 *
3363 * On Gen8, the multiply instruction does a full 32x32-bit
3364 * multiply, but in order to do a 64-bit multiply we can simulate
3365 * the previous behavior and then use a MACH instruction.
3366 *
3367 * FINISHME: Don't use source modifiers on src1.
3368 */
3369 assert(mul->src[1].type == BRW_REGISTER_TYPE_D ||
3370 mul->src[1].type == BRW_REGISTER_TYPE_UD);
3371 mul->src[1].type = (type_is_signed(mul->src[1].type) ?
3372 BRW_REGISTER_TYPE_W : BRW_REGISTER_TYPE_UW);
3373 mul->src[1].stride *= 2;
3374
3375 } else if (devinfo->gen == 7 && !devinfo->is_haswell &&
3376 inst->force_sechalf) {
3377 /* Among other things the quarter control bits influence which
3378 * accumulator register is used by the hardware for instructions
3379 * that access the accumulator implicitly (e.g. MACH). A
3380 * second-half instruction would normally map to acc1, which
3381 * doesn't exist on Gen7 and up (the hardware does emulate it for
3382 * floating-point instructions *only* by taking advantage of the
3383 * extra precision of acc0 not normally used for floating point
3384 * arithmetic).
3385 *
3386 * HSW and up are careful enough not to try to access an
3387 * accumulator register that doesn't exist, but on earlier Gen7
3388 * hardware we need to make sure that the quarter control bits are
3389 * zero to avoid non-deterministic behaviour and emit an extra MOV
3390 * to get the result masked correctly according to the current
3391 * channel enables.
3392 */
3393 mach->force_sechalf = false;
3394 mach->force_writemask_all = true;
3395 mach->dst = ibld.vgrf(inst->dst.type);
3396 ibld.MOV(inst->dst, mach->dst);
3397 }
3398 } else {
3399 continue;
3400 }
3401
3402 inst->remove(block);
3403 progress = true;
3404 }
3405
3406 if (progress)
3407 invalidate_live_intervals();
3408
3409 return progress;
3410 }
3411
3412 static void
3413 setup_color_payload(const fs_builder &bld, const brw_wm_prog_key *key,
3414 fs_reg *dst, fs_reg color, unsigned components)
3415 {
3416 if (key->clamp_fragment_color) {
3417 fs_reg tmp = bld.vgrf(BRW_REGISTER_TYPE_F, 4);
3418 assert(color.type == BRW_REGISTER_TYPE_F);
3419
3420 for (unsigned i = 0; i < components; i++)
3421 set_saturate(true,
3422 bld.MOV(offset(tmp, bld, i), offset(color, bld, i)));
3423
3424 color = tmp;
3425 }
3426
3427 for (unsigned i = 0; i < components; i++)
3428 dst[i] = offset(color, bld, i);
3429 }
3430
3431 static void
3432 lower_fb_write_logical_send(const fs_builder &bld, fs_inst *inst,
3433 const brw_wm_prog_data *prog_data,
3434 const brw_wm_prog_key *key,
3435 const fs_visitor::thread_payload &payload)
3436 {
3437 assert(inst->src[FB_WRITE_LOGICAL_SRC_COMPONENTS].file == IMM);
3438 const brw_device_info *devinfo = bld.shader->devinfo;
3439 const fs_reg &color0 = inst->src[FB_WRITE_LOGICAL_SRC_COLOR0];
3440 const fs_reg &color1 = inst->src[FB_WRITE_LOGICAL_SRC_COLOR1];
3441 const fs_reg &src0_alpha = inst->src[FB_WRITE_LOGICAL_SRC_SRC0_ALPHA];
3442 const fs_reg &src_depth = inst->src[FB_WRITE_LOGICAL_SRC_SRC_DEPTH];
3443 const fs_reg &dst_depth = inst->src[FB_WRITE_LOGICAL_SRC_DST_DEPTH];
3444 const fs_reg &src_stencil = inst->src[FB_WRITE_LOGICAL_SRC_SRC_STENCIL];
3445 fs_reg sample_mask = inst->src[FB_WRITE_LOGICAL_SRC_OMASK];
3446 const unsigned components =
3447 inst->src[FB_WRITE_LOGICAL_SRC_COMPONENTS].ud;
3448
3449 /* We can potentially have a message length of up to 15, so we have to set
3450 * base_mrf to either 0 or 1 in order to fit in m0..m15.
3451 */
3452 fs_reg sources[15];
3453 int header_size = 2, payload_header_size;
3454 unsigned length = 0;
3455
3456 /* From the Sandy Bridge PRM, volume 4, page 198:
3457 *
3458 * "Dispatched Pixel Enables. One bit per pixel indicating
3459 * which pixels were originally enabled when the thread was
3460 * dispatched. This field is only required for the end-of-
3461 * thread message and on all dual-source messages."
3462 */
3463 if (devinfo->gen >= 6 &&
3464 (devinfo->is_haswell || devinfo->gen >= 8 || !prog_data->uses_kill) &&
3465 color1.file == BAD_FILE &&
3466 key->nr_color_regions == 1) {
3467 header_size = 0;
3468 }
3469
3470 if (header_size != 0) {
3471 assert(header_size == 2);
3472 /* Allocate 2 registers for a header */
3473 length += 2;
3474 }
3475
3476 if (payload.aa_dest_stencil_reg) {
3477 sources[length] = fs_reg(VGRF, bld.shader->alloc.allocate(1));
3478 bld.group(8, 0).exec_all().annotate("FB write stencil/AA alpha")
3479 .MOV(sources[length],
3480 fs_reg(brw_vec8_grf(payload.aa_dest_stencil_reg, 0)));
3481 length++;
3482 }
3483
3484 if (prog_data->uses_omask) {
3485 sources[length] = fs_reg(VGRF, bld.shader->alloc.allocate(1),
3486 BRW_REGISTER_TYPE_UD);
3487
3488 /* Hand over gl_SampleMask. Only the lower 16 bits of each channel are
3489 * relevant. Since it's unsigned single words one vgrf is always
3490 * 16-wide, but only the lower or higher 8 channels will be used by the
3491 * hardware when doing a SIMD8 write depending on whether we have
3492 * selected the subspans for the first or second half respectively.
3493 */
3494 assert(sample_mask.file != BAD_FILE && type_sz(sample_mask.type) == 4);
3495 sample_mask.type = BRW_REGISTER_TYPE_UW;
3496 sample_mask.stride *= 2;
3497
3498 bld.exec_all().annotate("FB write oMask")
3499 .MOV(half(retype(sources[length], BRW_REGISTER_TYPE_UW),
3500 inst->force_sechalf),
3501 sample_mask);
3502 length++;
3503 }
3504
3505 payload_header_size = length;
3506
3507 if (src0_alpha.file != BAD_FILE) {
3508 /* FIXME: This is being passed at the wrong location in the payload and
3509 * doesn't work when gl_SampleMask and MRTs are used simultaneously.
3510 * It's supposed to be immediately before oMask but there seems to be no
3511 * reasonable way to pass them in the correct order because LOAD_PAYLOAD
3512 * requires header sources to form a contiguous segment at the beginning
3513 * of the message and src0_alpha has per-channel semantics.
3514 */
3515 setup_color_payload(bld, key, &sources[length], src0_alpha, 1);
3516 length++;
3517 }
3518
3519 setup_color_payload(bld, key, &sources[length], color0, components);
3520 length += 4;
3521
3522 if (color1.file != BAD_FILE) {
3523 setup_color_payload(bld, key, &sources[length], color1, components);
3524 length += 4;
3525 }
3526
3527 if (src_depth.file != BAD_FILE) {
3528 sources[length] = src_depth;
3529 length++;
3530 }
3531
3532 if (dst_depth.file != BAD_FILE) {
3533 sources[length] = dst_depth;
3534 length++;
3535 }
3536
3537 if (src_stencil.file != BAD_FILE) {
3538 assert(devinfo->gen >= 9);
3539 assert(bld.dispatch_width() != 16);
3540
3541 /* XXX: src_stencil is only available on gen9+. dst_depth is never
3542 * available on gen9+. As such it's impossible to have both enabled at the
3543 * same time and therefore length cannot overrun the array.
3544 */
3545 assert(length < 15);
3546
3547 sources[length] = bld.vgrf(BRW_REGISTER_TYPE_UD);
3548 bld.exec_all().annotate("FB write OS")
3549 .emit(FS_OPCODE_PACK_STENCIL_REF, sources[length],
3550 retype(src_stencil, BRW_REGISTER_TYPE_UB));
3551 length++;
3552 }
3553
3554 fs_inst *load;
3555 if (devinfo->gen >= 7) {
3556 /* Send from the GRF */
3557 fs_reg payload = fs_reg(VGRF, -1, BRW_REGISTER_TYPE_F);
3558 load = bld.LOAD_PAYLOAD(payload, sources, length, payload_header_size);
3559 payload.nr = bld.shader->alloc.allocate(load->regs_written);
3560 load->dst = payload;
3561
3562 inst->src[0] = payload;
3563 inst->resize_sources(1);
3564 inst->base_mrf = -1;
3565 } else {
3566 /* Send from the MRF */
3567 load = bld.LOAD_PAYLOAD(fs_reg(MRF, 1, BRW_REGISTER_TYPE_F),
3568 sources, length, payload_header_size);
3569
3570 /* On pre-SNB, we have to interlace the color values. LOAD_PAYLOAD
3571 * will do this for us if we just give it a COMPR4 destination.
3572 */
3573 if (devinfo->gen < 6 && bld.dispatch_width() == 16)
3574 load->dst.nr |= BRW_MRF_COMPR4;
3575
3576 inst->resize_sources(0);
3577 inst->base_mrf = 1;
3578 }
3579
3580 inst->opcode = FS_OPCODE_FB_WRITE;
3581 inst->mlen = load->regs_written;
3582 inst->header_size = header_size;
3583 }
3584
3585 static void
3586 lower_sampler_logical_send_gen4(const fs_builder &bld, fs_inst *inst, opcode op,
3587 const fs_reg &coordinate,
3588 const fs_reg &shadow_c,
3589 const fs_reg &lod, const fs_reg &lod2,
3590 const fs_reg &sampler,
3591 unsigned coord_components,
3592 unsigned grad_components)
3593 {
3594 const bool has_lod = (op == SHADER_OPCODE_TXL || op == FS_OPCODE_TXB ||
3595 op == SHADER_OPCODE_TXF || op == SHADER_OPCODE_TXS);
3596 fs_reg msg_begin(MRF, 1, BRW_REGISTER_TYPE_F);
3597 fs_reg msg_end = msg_begin;
3598
3599 /* g0 header. */
3600 msg_end = offset(msg_end, bld.group(8, 0), 1);
3601
3602 for (unsigned i = 0; i < coord_components; i++)
3603 bld.MOV(retype(offset(msg_end, bld, i), coordinate.type),
3604 offset(coordinate, bld, i));
3605
3606 msg_end = offset(msg_end, bld, coord_components);
3607
3608 /* Messages other than SAMPLE and RESINFO in SIMD16 and TXD in SIMD8
3609 * require all three components to be present and zero if they are unused.
3610 */
3611 if (coord_components > 0 &&
3612 (has_lod || shadow_c.file != BAD_FILE ||
3613 (op == SHADER_OPCODE_TEX && bld.dispatch_width() == 8))) {
3614 for (unsigned i = coord_components; i < 3; i++)
3615 bld.MOV(offset(msg_end, bld, i), brw_imm_f(0.0f));
3616
3617 msg_end = offset(msg_end, bld, 3 - coord_components);
3618 }
3619
3620 if (op == SHADER_OPCODE_TXD) {
3621 /* TXD unsupported in SIMD16 mode. */
3622 assert(bld.dispatch_width() == 8);
3623
3624 /* the slots for u and v are always present, but r is optional */
3625 if (coord_components < 2)
3626 msg_end = offset(msg_end, bld, 2 - coord_components);
3627
3628 /* P = u, v, r
3629 * dPdx = dudx, dvdx, drdx
3630 * dPdy = dudy, dvdy, drdy
3631 *
3632 * 1-arg: Does not exist.
3633 *
3634 * 2-arg: dudx dvdx dudy dvdy
3635 * dPdx.x dPdx.y dPdy.x dPdy.y
3636 * m4 m5 m6 m7
3637 *
3638 * 3-arg: dudx dvdx drdx dudy dvdy drdy
3639 * dPdx.x dPdx.y dPdx.z dPdy.x dPdy.y dPdy.z
3640 * m5 m6 m7 m8 m9 m10
3641 */
3642 for (unsigned i = 0; i < grad_components; i++)
3643 bld.MOV(offset(msg_end, bld, i), offset(lod, bld, i));
3644
3645 msg_end = offset(msg_end, bld, MAX2(grad_components, 2));
3646
3647 for (unsigned i = 0; i < grad_components; i++)
3648 bld.MOV(offset(msg_end, bld, i), offset(lod2, bld, i));
3649
3650 msg_end = offset(msg_end, bld, MAX2(grad_components, 2));
3651 }
3652
3653 if (has_lod) {
3654 /* Bias/LOD with shadow comparitor is unsupported in SIMD16 -- *Without*
3655 * shadow comparitor (including RESINFO) it's unsupported in SIMD8 mode.
3656 */
3657 assert(shadow_c.file != BAD_FILE ? bld.dispatch_width() == 8 :
3658 bld.dispatch_width() == 16);
3659
3660 const brw_reg_type type =
3661 (op == SHADER_OPCODE_TXF || op == SHADER_OPCODE_TXS ?
3662 BRW_REGISTER_TYPE_UD : BRW_REGISTER_TYPE_F);
3663 bld.MOV(retype(msg_end, type), lod);
3664 msg_end = offset(msg_end, bld, 1);
3665 }
3666
3667 if (shadow_c.file != BAD_FILE) {
3668 if (op == SHADER_OPCODE_TEX && bld.dispatch_width() == 8) {
3669 /* There's no plain shadow compare message, so we use shadow
3670 * compare with a bias of 0.0.
3671 */
3672 bld.MOV(msg_end, brw_imm_f(0.0f));
3673 msg_end = offset(msg_end, bld, 1);
3674 }
3675
3676 bld.MOV(msg_end, shadow_c);
3677 msg_end = offset(msg_end, bld, 1);
3678 }
3679
3680 inst->opcode = op;
3681 inst->src[0] = reg_undef;
3682 inst->src[1] = sampler;
3683 inst->resize_sources(2);
3684 inst->base_mrf = msg_begin.nr;
3685 inst->mlen = msg_end.nr - msg_begin.nr;
3686 inst->header_size = 1;
3687 }
3688
3689 static void
3690 lower_sampler_logical_send_gen5(const fs_builder &bld, fs_inst *inst, opcode op,
3691 fs_reg coordinate,
3692 const fs_reg &shadow_c,
3693 fs_reg lod, fs_reg lod2,
3694 const fs_reg &sample_index,
3695 const fs_reg &sampler,
3696 const fs_reg &offset_value,
3697 unsigned coord_components,
3698 unsigned grad_components)
3699 {
3700 fs_reg message(MRF, 2, BRW_REGISTER_TYPE_F);
3701 fs_reg msg_coords = message;
3702 unsigned header_size = 0;
3703
3704 if (offset_value.file != BAD_FILE) {
3705 /* The offsets set up by the visitor are in the m1 header, so we can't
3706 * go headerless.
3707 */
3708 header_size = 1;
3709 message.nr--;
3710 }
3711
3712 for (unsigned i = 0; i < coord_components; i++) {
3713 bld.MOV(retype(offset(msg_coords, bld, i), coordinate.type), coordinate);
3714 coordinate = offset(coordinate, bld, 1);
3715 }
3716 fs_reg msg_end = offset(msg_coords, bld, coord_components);
3717 fs_reg msg_lod = offset(msg_coords, bld, 4);
3718
3719 if (shadow_c.file != BAD_FILE) {
3720 fs_reg msg_shadow = msg_lod;
3721 bld.MOV(msg_shadow, shadow_c);
3722 msg_lod = offset(msg_shadow, bld, 1);
3723 msg_end = msg_lod;
3724 }
3725
3726 switch (op) {
3727 case SHADER_OPCODE_TXL:
3728 case FS_OPCODE_TXB:
3729 bld.MOV(msg_lod, lod);
3730 msg_end = offset(msg_lod, bld, 1);
3731 break;
3732 case SHADER_OPCODE_TXD:
3733 /**
3734 * P = u, v, r
3735 * dPdx = dudx, dvdx, drdx
3736 * dPdy = dudy, dvdy, drdy
3737 *
3738 * Load up these values:
3739 * - dudx dudy dvdx dvdy drdx drdy
3740 * - dPdx.x dPdy.x dPdx.y dPdy.y dPdx.z dPdy.z
3741 */
3742 msg_end = msg_lod;
3743 for (unsigned i = 0; i < grad_components; i++) {
3744 bld.MOV(msg_end, lod);
3745 lod = offset(lod, bld, 1);
3746 msg_end = offset(msg_end, bld, 1);
3747
3748 bld.MOV(msg_end, lod2);
3749 lod2 = offset(lod2, bld, 1);
3750 msg_end = offset(msg_end, bld, 1);
3751 }
3752 break;
3753 case SHADER_OPCODE_TXS:
3754 msg_lod = retype(msg_end, BRW_REGISTER_TYPE_UD);
3755 bld.MOV(msg_lod, lod);
3756 msg_end = offset(msg_lod, bld, 1);
3757 break;
3758 case SHADER_OPCODE_TXF:
3759 msg_lod = offset(msg_coords, bld, 3);
3760 bld.MOV(retype(msg_lod, BRW_REGISTER_TYPE_UD), lod);
3761 msg_end = offset(msg_lod, bld, 1);
3762 break;
3763 case SHADER_OPCODE_TXF_CMS:
3764 msg_lod = offset(msg_coords, bld, 3);
3765 /* lod */
3766 bld.MOV(retype(msg_lod, BRW_REGISTER_TYPE_UD), brw_imm_ud(0u));
3767 /* sample index */
3768 bld.MOV(retype(offset(msg_lod, bld, 1), BRW_REGISTER_TYPE_UD), sample_index);
3769 msg_end = offset(msg_lod, bld, 2);
3770 break;
3771 default:
3772 break;
3773 }
3774
3775 inst->opcode = op;
3776 inst->src[0] = reg_undef;
3777 inst->src[1] = sampler;
3778 inst->resize_sources(2);
3779 inst->base_mrf = message.nr;
3780 inst->mlen = msg_end.nr - message.nr;
3781 inst->header_size = header_size;
3782
3783 /* Message length > MAX_SAMPLER_MESSAGE_SIZE disallowed by hardware. */
3784 assert(inst->mlen <= MAX_SAMPLER_MESSAGE_SIZE);
3785 }
3786
3787 static bool
3788 is_high_sampler(const struct brw_device_info *devinfo, const fs_reg &sampler)
3789 {
3790 if (devinfo->gen < 8 && !devinfo->is_haswell)
3791 return false;
3792
3793 return sampler.file != IMM || sampler.ud >= 16;
3794 }
3795
3796 static void
3797 lower_sampler_logical_send_gen7(const fs_builder &bld, fs_inst *inst, opcode op,
3798 fs_reg coordinate,
3799 const fs_reg &shadow_c,
3800 fs_reg lod, fs_reg lod2,
3801 const fs_reg &sample_index,
3802 const fs_reg &mcs, const fs_reg &sampler,
3803 fs_reg offset_value,
3804 unsigned coord_components,
3805 unsigned grad_components)
3806 {
3807 const brw_device_info *devinfo = bld.shader->devinfo;
3808 int reg_width = bld.dispatch_width() / 8;
3809 unsigned header_size = 0, length = 0;
3810 fs_reg sources[MAX_SAMPLER_MESSAGE_SIZE];
3811 for (unsigned i = 0; i < ARRAY_SIZE(sources); i++)
3812 sources[i] = bld.vgrf(BRW_REGISTER_TYPE_F);
3813
3814 if (op == SHADER_OPCODE_TG4 || op == SHADER_OPCODE_TG4_OFFSET ||
3815 offset_value.file != BAD_FILE ||
3816 is_high_sampler(devinfo, sampler)) {
3817 /* For general texture offsets (no txf workaround), we need a header to
3818 * put them in. Note that we're only reserving space for it in the
3819 * message payload as it will be initialized implicitly by the
3820 * generator.
3821 *
3822 * TG4 needs to place its channel select in the header, for interaction
3823 * with ARB_texture_swizzle. The sampler index is only 4-bits, so for
3824 * larger sampler numbers we need to offset the Sampler State Pointer in
3825 * the header.
3826 */
3827 header_size = 1;
3828 sources[0] = fs_reg();
3829 length++;
3830 }
3831
3832 if (shadow_c.file != BAD_FILE) {
3833 bld.MOV(sources[length], shadow_c);
3834 length++;
3835 }
3836
3837 bool coordinate_done = false;
3838
3839 /* The sampler can only meaningfully compute LOD for fragment shader
3840 * messages. For all other stages, we change the opcode to TXL and
3841 * hardcode the LOD to 0.
3842 */
3843 if (bld.shader->stage != MESA_SHADER_FRAGMENT &&
3844 op == SHADER_OPCODE_TEX) {
3845 op = SHADER_OPCODE_TXL;
3846 lod = brw_imm_f(0.0f);
3847 }
3848
3849 /* Set up the LOD info */
3850 switch (op) {
3851 case FS_OPCODE_TXB:
3852 case SHADER_OPCODE_TXL:
3853 bld.MOV(sources[length], lod);
3854 length++;
3855 break;
3856 case SHADER_OPCODE_TXD:
3857 /* TXD should have been lowered in SIMD16 mode. */
3858 assert(bld.dispatch_width() == 8);
3859
3860 /* Load dPdx and the coordinate together:
3861 * [hdr], [ref], x, dPdx.x, dPdy.x, y, dPdx.y, dPdy.y, z, dPdx.z, dPdy.z
3862 */
3863 for (unsigned i = 0; i < coord_components; i++) {
3864 bld.MOV(sources[length], coordinate);
3865 coordinate = offset(coordinate, bld, 1);
3866 length++;
3867
3868 /* For cube map array, the coordinate is (u,v,r,ai) but there are
3869 * only derivatives for (u, v, r).
3870 */
3871 if (i < grad_components) {
3872 bld.MOV(sources[length], lod);
3873 lod = offset(lod, bld, 1);
3874 length++;
3875
3876 bld.MOV(sources[length], lod2);
3877 lod2 = offset(lod2, bld, 1);
3878 length++;
3879 }
3880 }
3881
3882 coordinate_done = true;
3883 break;
3884 case SHADER_OPCODE_TXS:
3885 bld.MOV(retype(sources[length], BRW_REGISTER_TYPE_UD), lod);
3886 length++;
3887 break;
3888 case SHADER_OPCODE_TXF:
3889 /* Unfortunately, the parameters for LD are intermixed: u, lod, v, r.
3890 * On Gen9 they are u, v, lod, r
3891 */
3892 bld.MOV(retype(sources[length], BRW_REGISTER_TYPE_D), coordinate);
3893 coordinate = offset(coordinate, bld, 1);
3894 length++;
3895
3896 if (devinfo->gen >= 9) {
3897 if (coord_components >= 2) {
3898 bld.MOV(retype(sources[length], BRW_REGISTER_TYPE_D), coordinate);
3899 coordinate = offset(coordinate, bld, 1);
3900 }
3901 length++;
3902 }
3903
3904 bld.MOV(retype(sources[length], BRW_REGISTER_TYPE_D), lod);
3905 length++;
3906
3907 for (unsigned i = devinfo->gen >= 9 ? 2 : 1; i < coord_components; i++) {
3908 bld.MOV(retype(sources[length], BRW_REGISTER_TYPE_D), coordinate);
3909 coordinate = offset(coordinate, bld, 1);
3910 length++;
3911 }
3912
3913 coordinate_done = true;
3914 break;
3915 case SHADER_OPCODE_TXF_CMS:
3916 case SHADER_OPCODE_TXF_CMS_W:
3917 case SHADER_OPCODE_TXF_UMS:
3918 case SHADER_OPCODE_TXF_MCS:
3919 if (op == SHADER_OPCODE_TXF_UMS ||
3920 op == SHADER_OPCODE_TXF_CMS ||
3921 op == SHADER_OPCODE_TXF_CMS_W) {
3922 bld.MOV(retype(sources[length], BRW_REGISTER_TYPE_UD), sample_index);
3923 length++;
3924 }
3925
3926 if (op == SHADER_OPCODE_TXF_CMS || op == SHADER_OPCODE_TXF_CMS_W) {
3927 /* Data from the multisample control surface. */
3928 bld.MOV(retype(sources[length], BRW_REGISTER_TYPE_UD), mcs);
3929 length++;
3930
3931 /* On Gen9+ we'll use ld2dms_w instead which has two registers for
3932 * the MCS data.
3933 */
3934 if (op == SHADER_OPCODE_TXF_CMS_W) {
3935 bld.MOV(retype(sources[length], BRW_REGISTER_TYPE_UD),
3936 mcs.file == IMM ?
3937 mcs :
3938 offset(mcs, bld, 1));
3939 length++;
3940 }
3941 }
3942
3943 /* There is no offsetting for this message; just copy in the integer
3944 * texture coordinates.
3945 */
3946 for (unsigned i = 0; i < coord_components; i++) {
3947 bld.MOV(retype(sources[length], BRW_REGISTER_TYPE_D), coordinate);
3948 coordinate = offset(coordinate, bld, 1);
3949 length++;
3950 }
3951
3952 coordinate_done = true;
3953 break;
3954 case SHADER_OPCODE_TG4_OFFSET:
3955 /* gather4_po_c should have been lowered in SIMD16 mode. */
3956 assert(bld.dispatch_width() == 8 || shadow_c.file == BAD_FILE);
3957
3958 /* More crazy intermixing */
3959 for (unsigned i = 0; i < 2; i++) { /* u, v */
3960 bld.MOV(sources[length], coordinate);
3961 coordinate = offset(coordinate, bld, 1);
3962 length++;
3963 }
3964
3965 for (unsigned i = 0; i < 2; i++) { /* offu, offv */
3966 bld.MOV(retype(sources[length], BRW_REGISTER_TYPE_D), offset_value);
3967 offset_value = offset(offset_value, bld, 1);
3968 length++;
3969 }
3970
3971 if (coord_components == 3) { /* r if present */
3972 bld.MOV(sources[length], coordinate);
3973 coordinate = offset(coordinate, bld, 1);
3974 length++;
3975 }
3976
3977 coordinate_done = true;
3978 break;
3979 default:
3980 break;
3981 }
3982
3983 /* Set up the coordinate (except for cases where it was done above) */
3984 if (!coordinate_done) {
3985 for (unsigned i = 0; i < coord_components; i++) {
3986 bld.MOV(sources[length], coordinate);
3987 coordinate = offset(coordinate, bld, 1);
3988 length++;
3989 }
3990 }
3991
3992 int mlen;
3993 if (reg_width == 2)
3994 mlen = length * reg_width - header_size;
3995 else
3996 mlen = length * reg_width;
3997
3998 const fs_reg src_payload = fs_reg(VGRF, bld.shader->alloc.allocate(mlen),
3999 BRW_REGISTER_TYPE_F);
4000 bld.LOAD_PAYLOAD(src_payload, sources, length, header_size);
4001
4002 /* Generate the SEND. */
4003 inst->opcode = op;
4004 inst->src[0] = src_payload;
4005 inst->src[1] = sampler;
4006 inst->resize_sources(2);
4007 inst->base_mrf = -1;
4008 inst->mlen = mlen;
4009 inst->header_size = header_size;
4010
4011 /* Message length > MAX_SAMPLER_MESSAGE_SIZE disallowed by hardware. */
4012 assert(inst->mlen <= MAX_SAMPLER_MESSAGE_SIZE);
4013 }
4014
4015 static void
4016 lower_sampler_logical_send(const fs_builder &bld, fs_inst *inst, opcode op)
4017 {
4018 const brw_device_info *devinfo = bld.shader->devinfo;
4019 const fs_reg &coordinate = inst->src[0];
4020 const fs_reg &shadow_c = inst->src[1];
4021 const fs_reg &lod = inst->src[2];
4022 const fs_reg &lod2 = inst->src[3];
4023 const fs_reg &sample_index = inst->src[4];
4024 const fs_reg &mcs = inst->src[5];
4025 const fs_reg &sampler = inst->src[6];
4026 const fs_reg &offset_value = inst->src[7];
4027 assert(inst->src[8].file == IMM && inst->src[9].file == IMM);
4028 const unsigned coord_components = inst->src[8].ud;
4029 const unsigned grad_components = inst->src[9].ud;
4030
4031 if (devinfo->gen >= 7) {
4032 lower_sampler_logical_send_gen7(bld, inst, op, coordinate,
4033 shadow_c, lod, lod2, sample_index,
4034 mcs, sampler, offset_value,
4035 coord_components, grad_components);
4036 } else if (devinfo->gen >= 5) {
4037 lower_sampler_logical_send_gen5(bld, inst, op, coordinate,
4038 shadow_c, lod, lod2, sample_index,
4039 sampler, offset_value,
4040 coord_components, grad_components);
4041 } else {
4042 lower_sampler_logical_send_gen4(bld, inst, op, coordinate,
4043 shadow_c, lod, lod2, sampler,
4044 coord_components, grad_components);
4045 }
4046 }
4047
4048 /**
4049 * Initialize the header present in some typed and untyped surface
4050 * messages.
4051 */
4052 static fs_reg
4053 emit_surface_header(const fs_builder &bld, const fs_reg &sample_mask)
4054 {
4055 fs_builder ubld = bld.exec_all().group(8, 0);
4056 const fs_reg dst = ubld.vgrf(BRW_REGISTER_TYPE_UD);
4057 ubld.MOV(dst, brw_imm_d(0));
4058 ubld.MOV(component(dst, 7), sample_mask);
4059 return dst;
4060 }
4061
4062 static void
4063 lower_surface_logical_send(const fs_builder &bld, fs_inst *inst, opcode op,
4064 const fs_reg &sample_mask)
4065 {
4066 /* Get the logical send arguments. */
4067 const fs_reg &addr = inst->src[0];
4068 const fs_reg &src = inst->src[1];
4069 const fs_reg &surface = inst->src[2];
4070 const UNUSED fs_reg &dims = inst->src[3];
4071 const fs_reg &arg = inst->src[4];
4072
4073 /* Calculate the total number of components of the payload. */
4074 const unsigned addr_sz = inst->components_read(0);
4075 const unsigned src_sz = inst->components_read(1);
4076 const unsigned header_sz = (sample_mask.file == BAD_FILE ? 0 : 1);
4077 const unsigned sz = header_sz + addr_sz + src_sz;
4078
4079 /* Allocate space for the payload. */
4080 fs_reg *const components = new fs_reg[sz];
4081 const fs_reg payload = bld.vgrf(BRW_REGISTER_TYPE_UD, sz);
4082 unsigned n = 0;
4083
4084 /* Construct the payload. */
4085 if (header_sz)
4086 components[n++] = emit_surface_header(bld, sample_mask);
4087
4088 for (unsigned i = 0; i < addr_sz; i++)
4089 components[n++] = offset(addr, bld, i);
4090
4091 for (unsigned i = 0; i < src_sz; i++)
4092 components[n++] = offset(src, bld, i);
4093
4094 bld.LOAD_PAYLOAD(payload, components, sz, header_sz);
4095
4096 /* Update the original instruction. */
4097 inst->opcode = op;
4098 inst->mlen = header_sz + (addr_sz + src_sz) * inst->exec_size / 8;
4099 inst->header_size = header_sz;
4100
4101 inst->src[0] = payload;
4102 inst->src[1] = surface;
4103 inst->src[2] = arg;
4104 inst->resize_sources(3);
4105
4106 delete[] components;
4107 }
4108
4109 bool
4110 fs_visitor::lower_logical_sends()
4111 {
4112 bool progress = false;
4113
4114 foreach_block_and_inst_safe(block, fs_inst, inst, cfg) {
4115 const fs_builder ibld(this, block, inst);
4116
4117 switch (inst->opcode) {
4118 case FS_OPCODE_FB_WRITE_LOGICAL:
4119 assert(stage == MESA_SHADER_FRAGMENT);
4120 lower_fb_write_logical_send(ibld, inst,
4121 (const brw_wm_prog_data *)prog_data,
4122 (const brw_wm_prog_key *)key,
4123 payload);
4124 break;
4125
4126 case SHADER_OPCODE_TEX_LOGICAL:
4127 lower_sampler_logical_send(ibld, inst, SHADER_OPCODE_TEX);
4128 break;
4129
4130 case SHADER_OPCODE_TXD_LOGICAL:
4131 lower_sampler_logical_send(ibld, inst, SHADER_OPCODE_TXD);
4132 break;
4133
4134 case SHADER_OPCODE_TXF_LOGICAL:
4135 lower_sampler_logical_send(ibld, inst, SHADER_OPCODE_TXF);
4136 break;
4137
4138 case SHADER_OPCODE_TXL_LOGICAL:
4139 lower_sampler_logical_send(ibld, inst, SHADER_OPCODE_TXL);
4140 break;
4141
4142 case SHADER_OPCODE_TXS_LOGICAL:
4143 lower_sampler_logical_send(ibld, inst, SHADER_OPCODE_TXS);
4144 break;
4145
4146 case FS_OPCODE_TXB_LOGICAL:
4147 lower_sampler_logical_send(ibld, inst, FS_OPCODE_TXB);
4148 break;
4149
4150 case SHADER_OPCODE_TXF_CMS_LOGICAL:
4151 lower_sampler_logical_send(ibld, inst, SHADER_OPCODE_TXF_CMS);
4152 break;
4153
4154 case SHADER_OPCODE_TXF_CMS_W_LOGICAL:
4155 lower_sampler_logical_send(ibld, inst, SHADER_OPCODE_TXF_CMS_W);
4156 break;
4157
4158 case SHADER_OPCODE_TXF_UMS_LOGICAL:
4159 lower_sampler_logical_send(ibld, inst, SHADER_OPCODE_TXF_UMS);
4160 break;
4161
4162 case SHADER_OPCODE_TXF_MCS_LOGICAL:
4163 lower_sampler_logical_send(ibld, inst, SHADER_OPCODE_TXF_MCS);
4164 break;
4165
4166 case SHADER_OPCODE_LOD_LOGICAL:
4167 lower_sampler_logical_send(ibld, inst, SHADER_OPCODE_LOD);
4168 break;
4169
4170 case SHADER_OPCODE_TG4_LOGICAL:
4171 lower_sampler_logical_send(ibld, inst, SHADER_OPCODE_TG4);
4172 break;
4173
4174 case SHADER_OPCODE_TG4_OFFSET_LOGICAL:
4175 lower_sampler_logical_send(ibld, inst, SHADER_OPCODE_TG4_OFFSET);
4176 break;
4177
4178 case SHADER_OPCODE_UNTYPED_SURFACE_READ_LOGICAL:
4179 lower_surface_logical_send(ibld, inst,
4180 SHADER_OPCODE_UNTYPED_SURFACE_READ,
4181 fs_reg());
4182 break;
4183
4184 case SHADER_OPCODE_UNTYPED_SURFACE_WRITE_LOGICAL:
4185 lower_surface_logical_send(ibld, inst,
4186 SHADER_OPCODE_UNTYPED_SURFACE_WRITE,
4187 ibld.sample_mask_reg());
4188 break;
4189
4190 case SHADER_OPCODE_UNTYPED_ATOMIC_LOGICAL:
4191 lower_surface_logical_send(ibld, inst,
4192 SHADER_OPCODE_UNTYPED_ATOMIC,
4193 ibld.sample_mask_reg());
4194 break;
4195
4196 case SHADER_OPCODE_TYPED_SURFACE_READ_LOGICAL:
4197 lower_surface_logical_send(ibld, inst,
4198 SHADER_OPCODE_TYPED_SURFACE_READ,
4199 brw_imm_d(0xffff));
4200 break;
4201
4202 case SHADER_OPCODE_TYPED_SURFACE_WRITE_LOGICAL:
4203 lower_surface_logical_send(ibld, inst,
4204 SHADER_OPCODE_TYPED_SURFACE_WRITE,
4205 ibld.sample_mask_reg());
4206 break;
4207
4208 case SHADER_OPCODE_TYPED_ATOMIC_LOGICAL:
4209 lower_surface_logical_send(ibld, inst,
4210 SHADER_OPCODE_TYPED_ATOMIC,
4211 ibld.sample_mask_reg());
4212 break;
4213
4214 default:
4215 continue;
4216 }
4217
4218 progress = true;
4219 }
4220
4221 if (progress)
4222 invalidate_live_intervals();
4223
4224 return progress;
4225 }
4226
4227 /**
4228 * Get the closest native SIMD width supported by the hardware for instruction
4229 * \p inst. The instruction will be left untouched by
4230 * fs_visitor::lower_simd_width() if the returned value is equal to the
4231 * original execution size.
4232 */
4233 static unsigned
4234 get_lowered_simd_width(const struct brw_device_info *devinfo,
4235 const fs_inst *inst)
4236 {
4237 switch (inst->opcode) {
4238 case BRW_OPCODE_MOV:
4239 case BRW_OPCODE_SEL:
4240 case BRW_OPCODE_NOT:
4241 case BRW_OPCODE_AND:
4242 case BRW_OPCODE_OR:
4243 case BRW_OPCODE_XOR:
4244 case BRW_OPCODE_SHR:
4245 case BRW_OPCODE_SHL:
4246 case BRW_OPCODE_ASR:
4247 case BRW_OPCODE_CMP:
4248 case BRW_OPCODE_CMPN:
4249 case BRW_OPCODE_CSEL:
4250 case BRW_OPCODE_F32TO16:
4251 case BRW_OPCODE_F16TO32:
4252 case BRW_OPCODE_BFREV:
4253 case BRW_OPCODE_BFE:
4254 case BRW_OPCODE_BFI1:
4255 case BRW_OPCODE_BFI2:
4256 case BRW_OPCODE_ADD:
4257 case BRW_OPCODE_MUL:
4258 case BRW_OPCODE_AVG:
4259 case BRW_OPCODE_FRC:
4260 case BRW_OPCODE_RNDU:
4261 case BRW_OPCODE_RNDD:
4262 case BRW_OPCODE_RNDE:
4263 case BRW_OPCODE_RNDZ:
4264 case BRW_OPCODE_LZD:
4265 case BRW_OPCODE_FBH:
4266 case BRW_OPCODE_FBL:
4267 case BRW_OPCODE_CBIT:
4268 case BRW_OPCODE_SAD2:
4269 case BRW_OPCODE_MAD:
4270 case BRW_OPCODE_LRP:
4271 case SHADER_OPCODE_RCP:
4272 case SHADER_OPCODE_RSQ:
4273 case SHADER_OPCODE_SQRT:
4274 case SHADER_OPCODE_EXP2:
4275 case SHADER_OPCODE_LOG2:
4276 case SHADER_OPCODE_POW:
4277 case SHADER_OPCODE_INT_QUOTIENT:
4278 case SHADER_OPCODE_INT_REMAINDER:
4279 case SHADER_OPCODE_SIN:
4280 case SHADER_OPCODE_COS: {
4281 /* According to the PRMs:
4282 * "A. In Direct Addressing mode, a source cannot span more than 2
4283 * adjacent GRF registers.
4284 * B. A destination cannot span more than 2 adjacent GRF registers."
4285 *
4286 * Look for the source or destination with the largest register region
4287 * which is the one that is going to limit the overal execution size of
4288 * the instruction due to this rule.
4289 */
4290 unsigned reg_count = inst->regs_written;
4291
4292 for (unsigned i = 0; i < inst->sources; i++)
4293 reg_count = MAX2(reg_count, (unsigned)inst->regs_read(i));
4294
4295 /* Calculate the maximum execution size of the instruction based on the
4296 * factor by which it goes over the hardware limit of 2 GRFs.
4297 */
4298 return inst->exec_size / DIV_ROUND_UP(reg_count, 2);
4299 }
4300 case SHADER_OPCODE_MULH:
4301 /* MULH is lowered to the MUL/MACH sequence using the accumulator, which
4302 * is 8-wide on Gen7+.
4303 */
4304 return (devinfo->gen >= 7 ? 8 : inst->exec_size);
4305
4306 case FS_OPCODE_FB_WRITE_LOGICAL:
4307 /* Gen6 doesn't support SIMD16 depth writes but we cannot handle them
4308 * here.
4309 */
4310 assert(devinfo->gen != 6 ||
4311 inst->src[FB_WRITE_LOGICAL_SRC_SRC_DEPTH].file == BAD_FILE ||
4312 inst->exec_size == 8);
4313 /* Dual-source FB writes are unsupported in SIMD16 mode. */
4314 return (inst->src[FB_WRITE_LOGICAL_SRC_COLOR1].file != BAD_FILE ?
4315 8 : inst->exec_size);
4316
4317 case SHADER_OPCODE_TXD_LOGICAL:
4318 /* TXD is unsupported in SIMD16 mode. */
4319 return 8;
4320
4321 case SHADER_OPCODE_TG4_OFFSET_LOGICAL: {
4322 /* gather4_po_c is unsupported in SIMD16 mode. */
4323 const fs_reg &shadow_c = inst->src[1];
4324 return (shadow_c.file != BAD_FILE ? 8 : inst->exec_size);
4325 }
4326 case SHADER_OPCODE_TXL_LOGICAL:
4327 case FS_OPCODE_TXB_LOGICAL: {
4328 /* Gen4 doesn't have SIMD8 non-shadow-compare bias/LOD instructions, and
4329 * Gen4-6 can't support TXL and TXB with shadow comparison in SIMD16
4330 * mode because the message exceeds the maximum length of 11.
4331 */
4332 const fs_reg &shadow_c = inst->src[1];
4333 if (devinfo->gen == 4 && shadow_c.file == BAD_FILE)
4334 return 16;
4335 else if (devinfo->gen < 7 && shadow_c.file != BAD_FILE)
4336 return 8;
4337 else
4338 return inst->exec_size;
4339 }
4340 case SHADER_OPCODE_TXF_LOGICAL:
4341 case SHADER_OPCODE_TXS_LOGICAL:
4342 /* Gen4 doesn't have SIMD8 variants for the RESINFO and LD-with-LOD
4343 * messages. Use SIMD16 instead.
4344 */
4345 if (devinfo->gen == 4)
4346 return 16;
4347 else
4348 return inst->exec_size;
4349
4350 case SHADER_OPCODE_TXF_CMS_W_LOGICAL: {
4351 /* This opcode can take up to 6 arguments which means that in some
4352 * circumstances it can end up with a message that is too long in SIMD16
4353 * mode.
4354 */
4355 const unsigned coord_components = inst->src[8].ud;
4356 /* First three arguments are the sample index and the two arguments for
4357 * the MCS data.
4358 */
4359 if ((coord_components + 3) * 2 > MAX_SAMPLER_MESSAGE_SIZE)
4360 return 8;
4361 else
4362 return inst->exec_size;
4363 }
4364
4365 case SHADER_OPCODE_TYPED_ATOMIC_LOGICAL:
4366 case SHADER_OPCODE_TYPED_SURFACE_READ_LOGICAL:
4367 case SHADER_OPCODE_TYPED_SURFACE_WRITE_LOGICAL:
4368 return 8;
4369
4370 default:
4371 return inst->exec_size;
4372 }
4373 }
4374
4375 /**
4376 * The \p rows array of registers represents a \p num_rows by \p num_columns
4377 * matrix in row-major order, write it in column-major order into the register
4378 * passed as destination. \p stride gives the separation between matrix
4379 * elements in the input in fs_builder::dispatch_width() units.
4380 */
4381 static void
4382 emit_transpose(const fs_builder &bld,
4383 const fs_reg &dst, const fs_reg *rows,
4384 unsigned num_rows, unsigned num_columns, unsigned stride)
4385 {
4386 fs_reg *const components = new fs_reg[num_rows * num_columns];
4387
4388 for (unsigned i = 0; i < num_columns; ++i) {
4389 for (unsigned j = 0; j < num_rows; ++j)
4390 components[num_rows * i + j] = offset(rows[j], bld, stride * i);
4391 }
4392
4393 bld.LOAD_PAYLOAD(dst, components, num_rows * num_columns, 0);
4394
4395 delete[] components;
4396 }
4397
4398 bool
4399 fs_visitor::lower_simd_width()
4400 {
4401 bool progress = false;
4402
4403 foreach_block_and_inst_safe(block, fs_inst, inst, cfg) {
4404 const unsigned lower_width = get_lowered_simd_width(devinfo, inst);
4405
4406 if (lower_width != inst->exec_size) {
4407 /* Builder matching the original instruction. We may also need to
4408 * emit an instruction of width larger than the original, set the
4409 * execution size of the builder to the highest of both for now so
4410 * we're sure that both cases can be handled.
4411 */
4412 const fs_builder ibld = bld.at(block, inst)
4413 .exec_all(inst->force_writemask_all)
4414 .group(MAX2(inst->exec_size, lower_width),
4415 inst->force_sechalf);
4416
4417 /* Split the copies in chunks of the execution width of either the
4418 * original or the lowered instruction, whichever is lower.
4419 */
4420 const unsigned copy_width = MIN2(lower_width, inst->exec_size);
4421 const unsigned n = inst->exec_size / copy_width;
4422 const unsigned dst_size = inst->regs_written * REG_SIZE /
4423 inst->dst.component_size(inst->exec_size);
4424 fs_reg dsts[4];
4425
4426 assert(n > 0 && n <= ARRAY_SIZE(dsts) &&
4427 !inst->writes_accumulator && !inst->mlen);
4428
4429 for (unsigned i = 0; i < n; i++) {
4430 /* Emit a copy of the original instruction with the lowered width.
4431 * If the EOT flag was set throw it away except for the last
4432 * instruction to avoid killing the thread prematurely.
4433 */
4434 fs_inst split_inst = *inst;
4435 split_inst.exec_size = lower_width;
4436 split_inst.eot = inst->eot && i == n - 1;
4437
4438 /* Select the correct channel enables for the i-th group, then
4439 * transform the sources and destination and emit the lowered
4440 * instruction.
4441 */
4442 const fs_builder lbld = ibld.group(lower_width, i);
4443
4444 for (unsigned j = 0; j < inst->sources; j++) {
4445 if (inst->src[j].file != BAD_FILE &&
4446 !is_uniform(inst->src[j])) {
4447 /* Get the i-th copy_width-wide chunk of the source. */
4448 const fs_reg src = horiz_offset(inst->src[j], copy_width * i);
4449 const unsigned src_size = inst->components_read(j);
4450
4451 /* Use a trivial transposition to copy one every n
4452 * copy_width-wide components of the register into a
4453 * temporary passed as source to the lowered instruction.
4454 */
4455 split_inst.src[j] = lbld.vgrf(inst->src[j].type, src_size);
4456 emit_transpose(lbld.group(copy_width, 0),
4457 split_inst.src[j], &src, 1, src_size, n);
4458 }
4459 }
4460
4461 if (inst->regs_written) {
4462 /* Allocate enough space to hold the result of the lowered
4463 * instruction and fix up the number of registers written.
4464 */
4465 split_inst.dst = dsts[i] =
4466 lbld.vgrf(inst->dst.type, dst_size);
4467 split_inst.regs_written =
4468 DIV_ROUND_UP(inst->regs_written * lower_width,
4469 inst->exec_size);
4470 }
4471
4472 lbld.emit(split_inst);
4473 }
4474
4475 if (inst->regs_written) {
4476 /* Distance between useful channels in the temporaries, skipping
4477 * garbage if the lowered instruction is wider than the original.
4478 */
4479 const unsigned m = lower_width / copy_width;
4480
4481 /* Interleave the components of the result from the lowered
4482 * instructions. We need to set exec_all() when copying more than
4483 * one half per component, because LOAD_PAYLOAD (in terms of which
4484 * emit_transpose is implemented) can only use the same channel
4485 * enable signals for all of its non-header sources.
4486 */
4487 emit_transpose(ibld.exec_all(inst->exec_size > copy_width)
4488 .group(copy_width, 0),
4489 inst->dst, dsts, n, dst_size, m);
4490 }
4491
4492 inst->remove(block);
4493 progress = true;
4494 }
4495 }
4496
4497 if (progress)
4498 invalidate_live_intervals();
4499
4500 return progress;
4501 }
4502
4503 void
4504 fs_visitor::dump_instructions()
4505 {
4506 dump_instructions(NULL);
4507 }
4508
4509 void
4510 fs_visitor::dump_instructions(const char *name)
4511 {
4512 FILE *file = stderr;
4513 if (name && geteuid() != 0) {
4514 file = fopen(name, "w");
4515 if (!file)
4516 file = stderr;
4517 }
4518
4519 if (cfg) {
4520 calculate_register_pressure();
4521 int ip = 0, max_pressure = 0;
4522 foreach_block_and_inst(block, backend_instruction, inst, cfg) {
4523 max_pressure = MAX2(max_pressure, regs_live_at_ip[ip]);
4524 fprintf(file, "{%3d} %4d: ", regs_live_at_ip[ip], ip);
4525 dump_instruction(inst, file);
4526 ip++;
4527 }
4528 fprintf(file, "Maximum %3d registers live at once.\n", max_pressure);
4529 } else {
4530 int ip = 0;
4531 foreach_in_list(backend_instruction, inst, &instructions) {
4532 fprintf(file, "%4d: ", ip++);
4533 dump_instruction(inst, file);
4534 }
4535 }
4536
4537 if (file != stderr) {
4538 fclose(file);
4539 }
4540 }
4541
4542 void
4543 fs_visitor::dump_instruction(backend_instruction *be_inst)
4544 {
4545 dump_instruction(be_inst, stderr);
4546 }
4547
4548 void
4549 fs_visitor::dump_instruction(backend_instruction *be_inst, FILE *file)
4550 {
4551 fs_inst *inst = (fs_inst *)be_inst;
4552
4553 if (inst->predicate) {
4554 fprintf(file, "(%cf0.%d) ",
4555 inst->predicate_inverse ? '-' : '+',
4556 inst->flag_subreg);
4557 }
4558
4559 fprintf(file, "%s", brw_instruction_name(inst->opcode));
4560 if (inst->saturate)
4561 fprintf(file, ".sat");
4562 if (inst->conditional_mod) {
4563 fprintf(file, "%s", conditional_modifier[inst->conditional_mod]);
4564 if (!inst->predicate &&
4565 (devinfo->gen < 5 || (inst->opcode != BRW_OPCODE_SEL &&
4566 inst->opcode != BRW_OPCODE_IF &&
4567 inst->opcode != BRW_OPCODE_WHILE))) {
4568 fprintf(file, ".f0.%d", inst->flag_subreg);
4569 }
4570 }
4571 fprintf(file, "(%d) ", inst->exec_size);
4572
4573 if (inst->mlen) {
4574 fprintf(file, "(mlen: %d) ", inst->mlen);
4575 }
4576
4577 switch (inst->dst.file) {
4578 case VGRF:
4579 fprintf(file, "vgrf%d", inst->dst.nr);
4580 if (alloc.sizes[inst->dst.nr] != inst->regs_written ||
4581 inst->dst.subreg_offset)
4582 fprintf(file, "+%d.%d",
4583 inst->dst.reg_offset, inst->dst.subreg_offset);
4584 break;
4585 case FIXED_GRF:
4586 fprintf(file, "g%d", inst->dst.nr);
4587 break;
4588 case MRF:
4589 fprintf(file, "m%d", inst->dst.nr);
4590 break;
4591 case BAD_FILE:
4592 fprintf(file, "(null)");
4593 break;
4594 case UNIFORM:
4595 fprintf(file, "***u%d***", inst->dst.nr + inst->dst.reg_offset);
4596 break;
4597 case ATTR:
4598 fprintf(file, "***attr%d***", inst->dst.nr + inst->dst.reg_offset);
4599 break;
4600 case ARF:
4601 switch (inst->dst.nr) {
4602 case BRW_ARF_NULL:
4603 fprintf(file, "null");
4604 break;
4605 case BRW_ARF_ADDRESS:
4606 fprintf(file, "a0.%d", inst->dst.subnr);
4607 break;
4608 case BRW_ARF_ACCUMULATOR:
4609 fprintf(file, "acc%d", inst->dst.subnr);
4610 break;
4611 case BRW_ARF_FLAG:
4612 fprintf(file, "f%d.%d", inst->dst.nr & 0xf, inst->dst.subnr);
4613 break;
4614 default:
4615 fprintf(file, "arf%d.%d", inst->dst.nr & 0xf, inst->dst.subnr);
4616 break;
4617 }
4618 if (inst->dst.subnr)
4619 fprintf(file, "+%d", inst->dst.subnr);
4620 break;
4621 case IMM:
4622 unreachable("not reached");
4623 }
4624 fprintf(file, ":%s, ", brw_reg_type_letters(inst->dst.type));
4625
4626 for (int i = 0; i < inst->sources; i++) {
4627 if (inst->src[i].negate)
4628 fprintf(file, "-");
4629 if (inst->src[i].abs)
4630 fprintf(file, "|");
4631 switch (inst->src[i].file) {
4632 case VGRF:
4633 fprintf(file, "vgrf%d", inst->src[i].nr);
4634 if (alloc.sizes[inst->src[i].nr] != (unsigned)inst->regs_read(i) ||
4635 inst->src[i].subreg_offset)
4636 fprintf(file, "+%d.%d", inst->src[i].reg_offset,
4637 inst->src[i].subreg_offset);
4638 break;
4639 case FIXED_GRF:
4640 fprintf(file, "g%d", inst->src[i].nr);
4641 break;
4642 case MRF:
4643 fprintf(file, "***m%d***", inst->src[i].nr);
4644 break;
4645 case ATTR:
4646 fprintf(file, "attr%d+%d", inst->src[i].nr, inst->src[i].reg_offset);
4647 break;
4648 case UNIFORM:
4649 fprintf(file, "u%d", inst->src[i].nr + inst->src[i].reg_offset);
4650 if (inst->src[i].reladdr) {
4651 fprintf(file, "+reladdr");
4652 } else if (inst->src[i].subreg_offset) {
4653 fprintf(file, "+%d.%d", inst->src[i].reg_offset,
4654 inst->src[i].subreg_offset);
4655 }
4656 break;
4657 case BAD_FILE:
4658 fprintf(file, "(null)");
4659 break;
4660 case IMM:
4661 switch (inst->src[i].type) {
4662 case BRW_REGISTER_TYPE_F:
4663 fprintf(file, "%ff", inst->src[i].f);
4664 break;
4665 case BRW_REGISTER_TYPE_W:
4666 case BRW_REGISTER_TYPE_D:
4667 fprintf(file, "%dd", inst->src[i].d);
4668 break;
4669 case BRW_REGISTER_TYPE_UW:
4670 case BRW_REGISTER_TYPE_UD:
4671 fprintf(file, "%uu", inst->src[i].ud);
4672 break;
4673 case BRW_REGISTER_TYPE_VF:
4674 fprintf(file, "[%-gF, %-gF, %-gF, %-gF]",
4675 brw_vf_to_float((inst->src[i].ud >> 0) & 0xff),
4676 brw_vf_to_float((inst->src[i].ud >> 8) & 0xff),
4677 brw_vf_to_float((inst->src[i].ud >> 16) & 0xff),
4678 brw_vf_to_float((inst->src[i].ud >> 24) & 0xff));
4679 break;
4680 default:
4681 fprintf(file, "???");
4682 break;
4683 }
4684 break;
4685 case ARF:
4686 switch (inst->src[i].nr) {
4687 case BRW_ARF_NULL:
4688 fprintf(file, "null");
4689 break;
4690 case BRW_ARF_ADDRESS:
4691 fprintf(file, "a0.%d", inst->src[i].subnr);
4692 break;
4693 case BRW_ARF_ACCUMULATOR:
4694 fprintf(file, "acc%d", inst->src[i].subnr);
4695 break;
4696 case BRW_ARF_FLAG:
4697 fprintf(file, "f%d.%d", inst->src[i].nr & 0xf, inst->src[i].subnr);
4698 break;
4699 default:
4700 fprintf(file, "arf%d.%d", inst->src[i].nr & 0xf, inst->src[i].subnr);
4701 break;
4702 }
4703 if (inst->src[i].subnr)
4704 fprintf(file, "+%d", inst->src[i].subnr);
4705 break;
4706 }
4707 if (inst->src[i].abs)
4708 fprintf(file, "|");
4709
4710 if (inst->src[i].file != IMM) {
4711 fprintf(file, ":%s", brw_reg_type_letters(inst->src[i].type));
4712 }
4713
4714 if (i < inst->sources - 1 && inst->src[i + 1].file != BAD_FILE)
4715 fprintf(file, ", ");
4716 }
4717
4718 fprintf(file, " ");
4719
4720 if (inst->force_writemask_all)
4721 fprintf(file, "NoMask ");
4722
4723 if (dispatch_width == 16 && inst->exec_size == 8) {
4724 if (inst->force_sechalf)
4725 fprintf(file, "2ndhalf ");
4726 else
4727 fprintf(file, "1sthalf ");
4728 }
4729
4730 fprintf(file, "\n");
4731 }
4732
4733 /**
4734 * Possibly returns an instruction that set up @param reg.
4735 *
4736 * Sometimes we want to take the result of some expression/variable
4737 * dereference tree and rewrite the instruction generating the result
4738 * of the tree. When processing the tree, we know that the
4739 * instructions generated are all writing temporaries that are dead
4740 * outside of this tree. So, if we have some instructions that write
4741 * a temporary, we're free to point that temp write somewhere else.
4742 *
4743 * Note that this doesn't guarantee that the instruction generated
4744 * only reg -- it might be the size=4 destination of a texture instruction.
4745 */
4746 fs_inst *
4747 fs_visitor::get_instruction_generating_reg(fs_inst *start,
4748 fs_inst *end,
4749 const fs_reg &reg)
4750 {
4751 if (end == start ||
4752 end->is_partial_write() ||
4753 reg.reladdr ||
4754 !reg.equals(end->dst)) {
4755 return NULL;
4756 } else {
4757 return end;
4758 }
4759 }
4760
4761 void
4762 fs_visitor::setup_payload_gen6()
4763 {
4764 bool uses_depth =
4765 (nir->info.inputs_read & (1 << VARYING_SLOT_POS)) != 0;
4766 unsigned barycentric_interp_modes =
4767 (stage == MESA_SHADER_FRAGMENT) ?
4768 ((brw_wm_prog_data*) this->prog_data)->barycentric_interp_modes : 0;
4769
4770 assert(devinfo->gen >= 6);
4771
4772 /* R0-1: masks, pixel X/Y coordinates. */
4773 payload.num_regs = 2;
4774 /* R2: only for 32-pixel dispatch.*/
4775
4776 /* R3-26: barycentric interpolation coordinates. These appear in the
4777 * same order that they appear in the brw_wm_barycentric_interp_mode
4778 * enum. Each set of coordinates occupies 2 registers if dispatch width
4779 * == 8 and 4 registers if dispatch width == 16. Coordinates only
4780 * appear if they were enabled using the "Barycentric Interpolation
4781 * Mode" bits in WM_STATE.
4782 */
4783 for (int i = 0; i < BRW_WM_BARYCENTRIC_INTERP_MODE_COUNT; ++i) {
4784 if (barycentric_interp_modes & (1 << i)) {
4785 payload.barycentric_coord_reg[i] = payload.num_regs;
4786 payload.num_regs += 2;
4787 if (dispatch_width == 16) {
4788 payload.num_regs += 2;
4789 }
4790 }
4791 }
4792
4793 /* R27: interpolated depth if uses source depth */
4794 if (uses_depth) {
4795 payload.source_depth_reg = payload.num_regs;
4796 payload.num_regs++;
4797 if (dispatch_width == 16) {
4798 /* R28: interpolated depth if not SIMD8. */
4799 payload.num_regs++;
4800 }
4801 }
4802 /* R29: interpolated W set if GEN6_WM_USES_SOURCE_W. */
4803 if (uses_depth) {
4804 payload.source_w_reg = payload.num_regs;
4805 payload.num_regs++;
4806 if (dispatch_width == 16) {
4807 /* R30: interpolated W if not SIMD8. */
4808 payload.num_regs++;
4809 }
4810 }
4811
4812 if (stage == MESA_SHADER_FRAGMENT) {
4813 brw_wm_prog_data *prog_data = (brw_wm_prog_data*) this->prog_data;
4814 brw_wm_prog_key *key = (brw_wm_prog_key*) this->key;
4815 prog_data->uses_pos_offset = key->compute_pos_offset;
4816 /* R31: MSAA position offsets. */
4817 if (prog_data->uses_pos_offset) {
4818 payload.sample_pos_reg = payload.num_regs;
4819 payload.num_regs++;
4820 }
4821 }
4822
4823 /* R32: MSAA input coverage mask */
4824 if (nir->info.system_values_read & SYSTEM_BIT_SAMPLE_MASK_IN) {
4825 assert(devinfo->gen >= 7);
4826 payload.sample_mask_in_reg = payload.num_regs;
4827 payload.num_regs++;
4828 if (dispatch_width == 16) {
4829 /* R33: input coverage mask if not SIMD8. */
4830 payload.num_regs++;
4831 }
4832 }
4833
4834 /* R34-: bary for 32-pixel. */
4835 /* R58-59: interp W for 32-pixel. */
4836
4837 if (nir->info.outputs_written & BITFIELD64_BIT(FRAG_RESULT_DEPTH)) {
4838 source_depth_to_render_target = true;
4839 }
4840 }
4841
4842 void
4843 fs_visitor::setup_vs_payload()
4844 {
4845 /* R0: thread header, R1: urb handles */
4846 payload.num_regs = 2;
4847 }
4848
4849 /**
4850 * We are building the local ID push constant data using the simplest possible
4851 * method. We simply push the local IDs directly as they should appear in the
4852 * registers for the uvec3 gl_LocalInvocationID variable.
4853 *
4854 * Therefore, for SIMD8, we use 3 full registers, and for SIMD16 we use 6
4855 * registers worth of push constant space.
4856 *
4857 * Note: Any updates to brw_cs_prog_local_id_payload_dwords,
4858 * fill_local_id_payload or fs_visitor::emit_cs_local_invocation_id_setup need
4859 * to coordinated.
4860 *
4861 * FINISHME: There are a few easy optimizations to consider.
4862 *
4863 * 1. If gl_WorkGroupSize x, y or z is 1, we can just use zero, and there is
4864 * no need for using push constant space for that dimension.
4865 *
4866 * 2. Since GL_MAX_COMPUTE_WORK_GROUP_SIZE is currently 1024 or less, we can
4867 * easily use 16-bit words rather than 32-bit dwords in the push constant
4868 * data.
4869 *
4870 * 3. If gl_WorkGroupSize x, y or z is small, then we can use bytes for
4871 * conveying the data, and thereby reduce push constant usage.
4872 *
4873 */
4874 void
4875 fs_visitor::setup_gs_payload()
4876 {
4877 assert(stage == MESA_SHADER_GEOMETRY);
4878
4879 struct brw_gs_prog_data *gs_prog_data =
4880 (struct brw_gs_prog_data *) prog_data;
4881 struct brw_vue_prog_data *vue_prog_data =
4882 (struct brw_vue_prog_data *) prog_data;
4883
4884 /* R0: thread header, R1: output URB handles */
4885 payload.num_regs = 2;
4886
4887 if (gs_prog_data->include_primitive_id) {
4888 /* R2: Primitive ID 0..7 */
4889 payload.num_regs++;
4890 }
4891
4892 /* Use a maximum of 32 registers for push-model inputs. */
4893 const unsigned max_push_components = 32;
4894
4895 /* If pushing our inputs would take too many registers, reduce the URB read
4896 * length (which is in HWords, or 8 registers), and resort to pulling.
4897 *
4898 * Note that the GS reads <URB Read Length> HWords for every vertex - so we
4899 * have to multiply by VerticesIn to obtain the total storage requirement.
4900 */
4901 if (8 * vue_prog_data->urb_read_length * nir->info.gs.vertices_in >
4902 max_push_components) {
4903 gs_prog_data->base.include_vue_handles = true;
4904
4905 /* R3..RN: ICP Handles for each incoming vertex (when using pull model) */
4906 payload.num_regs += nir->info.gs.vertices_in;
4907
4908 vue_prog_data->urb_read_length =
4909 ROUND_DOWN_TO(max_push_components / nir->info.gs.vertices_in, 8) / 8;
4910 }
4911 }
4912
4913 void
4914 fs_visitor::setup_cs_payload()
4915 {
4916 assert(devinfo->gen >= 7);
4917 brw_cs_prog_data *prog_data = (brw_cs_prog_data*) this->prog_data;
4918
4919 payload.num_regs = 1;
4920
4921 if (nir->info.system_values_read & SYSTEM_BIT_LOCAL_INVOCATION_ID) {
4922 prog_data->local_invocation_id_regs = dispatch_width * 3 / 8;
4923 payload.local_invocation_id_reg = payload.num_regs;
4924 payload.num_regs += prog_data->local_invocation_id_regs;
4925 }
4926 }
4927
4928 void
4929 fs_visitor::calculate_register_pressure()
4930 {
4931 invalidate_live_intervals();
4932 calculate_live_intervals();
4933
4934 unsigned num_instructions = 0;
4935 foreach_block(block, cfg)
4936 num_instructions += block->instructions.length();
4937
4938 regs_live_at_ip = rzalloc_array(mem_ctx, int, num_instructions);
4939
4940 for (unsigned reg = 0; reg < alloc.count; reg++) {
4941 for (int ip = virtual_grf_start[reg]; ip <= virtual_grf_end[reg]; ip++)
4942 regs_live_at_ip[ip] += alloc.sizes[reg];
4943 }
4944 }
4945
4946 void
4947 fs_visitor::optimize()
4948 {
4949 /* Start by validating the shader we currently have. */
4950 validate();
4951
4952 /* bld is the common builder object pointing at the end of the program we
4953 * used to translate it into i965 IR. For the optimization and lowering
4954 * passes coming next, any code added after the end of the program without
4955 * having explicitly called fs_builder::at() clearly points at a mistake.
4956 * Ideally optimization passes wouldn't be part of the visitor so they
4957 * wouldn't have access to bld at all, but they do, so just in case some
4958 * pass forgets to ask for a location explicitly set it to NULL here to
4959 * make it trip. The dispatch width is initialized to a bogus value to
4960 * make sure that optimizations set the execution controls explicitly to
4961 * match the code they are manipulating instead of relying on the defaults.
4962 */
4963 bld = fs_builder(this, 64);
4964
4965 assign_constant_locations();
4966 demote_pull_constants();
4967
4968 validate();
4969
4970 split_virtual_grfs();
4971 validate();
4972
4973 #define OPT(pass, args...) ({ \
4974 pass_num++; \
4975 bool this_progress = pass(args); \
4976 \
4977 if (unlikely(INTEL_DEBUG & DEBUG_OPTIMIZER) && this_progress) { \
4978 char filename[64]; \
4979 snprintf(filename, 64, "%s%d-%s-%02d-%02d-" #pass, \
4980 stage_abbrev, dispatch_width, nir->info.name, iteration, pass_num); \
4981 \
4982 backend_shader::dump_instructions(filename); \
4983 } \
4984 \
4985 validate(); \
4986 \
4987 progress = progress || this_progress; \
4988 this_progress; \
4989 })
4990
4991 if (unlikely(INTEL_DEBUG & DEBUG_OPTIMIZER)) {
4992 char filename[64];
4993 snprintf(filename, 64, "%s%d-%s-00-start",
4994 stage_abbrev, dispatch_width, nir->info.name);
4995
4996 backend_shader::dump_instructions(filename);
4997 }
4998
4999 bool progress = false;
5000 int iteration = 0;
5001 int pass_num = 0;
5002
5003 OPT(lower_simd_width);
5004 OPT(lower_logical_sends);
5005
5006 do {
5007 progress = false;
5008 pass_num = 0;
5009 iteration++;
5010
5011 OPT(remove_duplicate_mrf_writes);
5012
5013 OPT(opt_algebraic);
5014 OPT(opt_cse);
5015 OPT(opt_copy_propagate);
5016 OPT(opt_predicated_break, this);
5017 OPT(opt_cmod_propagation);
5018 OPT(dead_code_eliminate);
5019 OPT(opt_peephole_sel);
5020 OPT(dead_control_flow_eliminate, this);
5021 OPT(opt_register_renaming);
5022 OPT(opt_redundant_discard_jumps);
5023 OPT(opt_saturate_propagation);
5024 OPT(opt_zero_samples);
5025 OPT(register_coalesce);
5026 OPT(compute_to_mrf);
5027 OPT(eliminate_find_live_channel);
5028
5029 OPT(compact_virtual_grfs);
5030 } while (progress);
5031
5032 pass_num = 0;
5033
5034 OPT(opt_sampler_eot);
5035
5036 if (OPT(lower_load_payload)) {
5037 split_virtual_grfs();
5038 OPT(register_coalesce);
5039 OPT(compute_to_mrf);
5040 OPT(dead_code_eliminate);
5041 }
5042
5043 OPT(opt_combine_constants);
5044 OPT(lower_integer_multiplication);
5045
5046 lower_uniform_pull_constant_loads();
5047
5048 validate();
5049 }
5050
5051 /**
5052 * Three source instruction must have a GRF/MRF destination register.
5053 * ARF NULL is not allowed. Fix that up by allocating a temporary GRF.
5054 */
5055 void
5056 fs_visitor::fixup_3src_null_dest()
5057 {
5058 foreach_block_and_inst_safe (block, fs_inst, inst, cfg) {
5059 if (inst->is_3src() && inst->dst.is_null()) {
5060 inst->dst = fs_reg(VGRF, alloc.allocate(dispatch_width / 8),
5061 inst->dst.type);
5062 }
5063 }
5064 }
5065
5066 void
5067 fs_visitor::allocate_registers()
5068 {
5069 bool allocated_without_spills;
5070
5071 static const enum instruction_scheduler_mode pre_modes[] = {
5072 SCHEDULE_PRE,
5073 SCHEDULE_PRE_NON_LIFO,
5074 SCHEDULE_PRE_LIFO,
5075 };
5076
5077 /* Try each scheduling heuristic to see if it can successfully register
5078 * allocate without spilling. They should be ordered by decreasing
5079 * performance but increasing likelihood of allocating.
5080 */
5081 for (unsigned i = 0; i < ARRAY_SIZE(pre_modes); i++) {
5082 schedule_instructions(pre_modes[i]);
5083
5084 if (0) {
5085 assign_regs_trivial();
5086 allocated_without_spills = true;
5087 } else {
5088 allocated_without_spills = assign_regs(false);
5089 }
5090 if (allocated_without_spills)
5091 break;
5092 }
5093
5094 if (!allocated_without_spills) {
5095 /* We assume that any spilling is worse than just dropping back to
5096 * SIMD8. There's probably actually some intermediate point where
5097 * SIMD16 with a couple of spills is still better.
5098 */
5099 if (dispatch_width == 16) {
5100 fail("Failure to register allocate. Reduce number of "
5101 "live scalar values to avoid this.");
5102 } else {
5103 compiler->shader_perf_log(log_data,
5104 "%s shader triggered register spilling. "
5105 "Try reducing the number of live scalar "
5106 "values to improve performance.\n",
5107 stage_name);
5108 }
5109
5110 /* Since we're out of heuristics, just go spill registers until we
5111 * get an allocation.
5112 */
5113 while (!assign_regs(true)) {
5114 if (failed)
5115 break;
5116 }
5117 }
5118
5119 /* This must come after all optimization and register allocation, since
5120 * it inserts dead code that happens to have side effects, and it does
5121 * so based on the actual physical registers in use.
5122 */
5123 insert_gen4_send_dependency_workarounds();
5124
5125 if (failed)
5126 return;
5127
5128 schedule_instructions(SCHEDULE_POST);
5129
5130 if (last_scratch > 0)
5131 prog_data->total_scratch = brw_get_scratch_size(last_scratch);
5132 }
5133
5134 bool
5135 fs_visitor::run_vs(gl_clip_plane *clip_planes)
5136 {
5137 assert(stage == MESA_SHADER_VERTEX);
5138
5139 setup_vs_payload();
5140
5141 if (shader_time_index >= 0)
5142 emit_shader_time_begin();
5143
5144 emit_nir_code();
5145
5146 if (failed)
5147 return false;
5148
5149 compute_clip_distance(clip_planes);
5150
5151 emit_urb_writes();
5152
5153 if (shader_time_index >= 0)
5154 emit_shader_time_end();
5155
5156 calculate_cfg();
5157
5158 optimize();
5159
5160 assign_curb_setup();
5161 assign_vs_urb_setup();
5162
5163 fixup_3src_null_dest();
5164 allocate_registers();
5165
5166 return !failed;
5167 }
5168
5169 bool
5170 fs_visitor::run_gs()
5171 {
5172 assert(stage == MESA_SHADER_GEOMETRY);
5173
5174 setup_gs_payload();
5175
5176 this->final_gs_vertex_count = vgrf(glsl_type::uint_type);
5177
5178 if (gs_compile->control_data_header_size_bits > 0) {
5179 /* Create a VGRF to store accumulated control data bits. */
5180 this->control_data_bits = vgrf(glsl_type::uint_type);
5181
5182 /* If we're outputting more than 32 control data bits, then EmitVertex()
5183 * will set control_data_bits to 0 after emitting the first vertex.
5184 * Otherwise, we need to initialize it to 0 here.
5185 */
5186 if (gs_compile->control_data_header_size_bits <= 32) {
5187 const fs_builder abld = bld.annotate("initialize control data bits");
5188 abld.MOV(this->control_data_bits, brw_imm_ud(0u));
5189 }
5190 }
5191
5192 if (shader_time_index >= 0)
5193 emit_shader_time_begin();
5194
5195 emit_nir_code();
5196
5197 emit_gs_thread_end();
5198
5199 if (shader_time_index >= 0)
5200 emit_shader_time_end();
5201
5202 if (failed)
5203 return false;
5204
5205 calculate_cfg();
5206
5207 optimize();
5208
5209 assign_curb_setup();
5210 assign_gs_urb_setup();
5211
5212 fixup_3src_null_dest();
5213 allocate_registers();
5214
5215 return !failed;
5216 }
5217
5218 bool
5219 fs_visitor::run_fs(bool do_rep_send)
5220 {
5221 brw_wm_prog_data *wm_prog_data = (brw_wm_prog_data *) this->prog_data;
5222 brw_wm_prog_key *wm_key = (brw_wm_prog_key *) this->key;
5223
5224 assert(stage == MESA_SHADER_FRAGMENT);
5225
5226 if (devinfo->gen >= 6)
5227 setup_payload_gen6();
5228 else
5229 setup_payload_gen4();
5230
5231 if (0) {
5232 emit_dummy_fs();
5233 } else if (do_rep_send) {
5234 assert(dispatch_width == 16);
5235 emit_repclear_shader();
5236 } else {
5237 if (shader_time_index >= 0)
5238 emit_shader_time_begin();
5239
5240 calculate_urb_setup();
5241 if (nir->info.inputs_read > 0) {
5242 if (devinfo->gen < 6)
5243 emit_interpolation_setup_gen4();
5244 else
5245 emit_interpolation_setup_gen6();
5246 }
5247
5248 /* We handle discards by keeping track of the still-live pixels in f0.1.
5249 * Initialize it with the dispatched pixels.
5250 */
5251 if (wm_prog_data->uses_kill) {
5252 fs_inst *discard_init = bld.emit(FS_OPCODE_MOV_DISPATCH_TO_FLAGS);
5253 discard_init->flag_subreg = 1;
5254 }
5255
5256 /* Generate FS IR for main(). (the visitor only descends into
5257 * functions called "main").
5258 */
5259 emit_nir_code();
5260
5261 if (failed)
5262 return false;
5263
5264 if (wm_prog_data->uses_kill)
5265 bld.emit(FS_OPCODE_PLACEHOLDER_HALT);
5266
5267 if (wm_key->alpha_test_func)
5268 emit_alpha_test();
5269
5270 emit_fb_writes();
5271
5272 if (shader_time_index >= 0)
5273 emit_shader_time_end();
5274
5275 calculate_cfg();
5276
5277 optimize();
5278
5279 assign_curb_setup();
5280 assign_urb_setup();
5281
5282 fixup_3src_null_dest();
5283 allocate_registers();
5284
5285 if (failed)
5286 return false;
5287 }
5288
5289 if (dispatch_width == 8)
5290 wm_prog_data->reg_blocks = brw_register_blocks(grf_used);
5291 else
5292 wm_prog_data->reg_blocks_16 = brw_register_blocks(grf_used);
5293
5294 return !failed;
5295 }
5296
5297 bool
5298 fs_visitor::run_cs()
5299 {
5300 assert(stage == MESA_SHADER_COMPUTE);
5301
5302 setup_cs_payload();
5303
5304 if (shader_time_index >= 0)
5305 emit_shader_time_begin();
5306
5307 emit_nir_code();
5308
5309 if (failed)
5310 return false;
5311
5312 emit_cs_terminate();
5313
5314 if (shader_time_index >= 0)
5315 emit_shader_time_end();
5316
5317 calculate_cfg();
5318
5319 optimize();
5320
5321 assign_curb_setup();
5322
5323 fixup_3src_null_dest();
5324 allocate_registers();
5325
5326 if (failed)
5327 return false;
5328
5329 return !failed;
5330 }
5331
5332 /**
5333 * Return a bitfield where bit n is set if barycentric interpolation mode n
5334 * (see enum brw_wm_barycentric_interp_mode) is needed by the fragment shader.
5335 */
5336 static unsigned
5337 brw_compute_barycentric_interp_modes(const struct brw_device_info *devinfo,
5338 bool shade_model_flat,
5339 bool persample_shading,
5340 const nir_shader *shader)
5341 {
5342 unsigned barycentric_interp_modes = 0;
5343
5344 nir_foreach_variable(var, &shader->inputs) {
5345 enum glsl_interp_qualifier interp_qualifier =
5346 (enum glsl_interp_qualifier)var->data.interpolation;
5347 bool is_centroid = var->data.centroid && !persample_shading;
5348 bool is_sample = var->data.sample || persample_shading;
5349 bool is_gl_Color = (var->data.location == VARYING_SLOT_COL0) ||
5350 (var->data.location == VARYING_SLOT_COL1);
5351
5352 /* Ignore WPOS and FACE, because they don't require interpolation. */
5353 if (var->data.location == VARYING_SLOT_POS ||
5354 var->data.location == VARYING_SLOT_FACE)
5355 continue;
5356
5357 /* Determine the set (or sets) of barycentric coordinates needed to
5358 * interpolate this variable. Note that when
5359 * brw->needs_unlit_centroid_workaround is set, centroid interpolation
5360 * uses PIXEL interpolation for unlit pixels and CENTROID interpolation
5361 * for lit pixels, so we need both sets of barycentric coordinates.
5362 */
5363 if (interp_qualifier == INTERP_QUALIFIER_NOPERSPECTIVE) {
5364 if (is_centroid) {
5365 barycentric_interp_modes |=
5366 1 << BRW_WM_NONPERSPECTIVE_CENTROID_BARYCENTRIC;
5367 } else if (is_sample) {
5368 barycentric_interp_modes |=
5369 1 << BRW_WM_NONPERSPECTIVE_SAMPLE_BARYCENTRIC;
5370 }
5371 if ((!is_centroid && !is_sample) ||
5372 devinfo->needs_unlit_centroid_workaround) {
5373 barycentric_interp_modes |=
5374 1 << BRW_WM_NONPERSPECTIVE_PIXEL_BARYCENTRIC;
5375 }
5376 } else if (interp_qualifier == INTERP_QUALIFIER_SMOOTH ||
5377 (!(shade_model_flat && is_gl_Color) &&
5378 interp_qualifier == INTERP_QUALIFIER_NONE)) {
5379 if (is_centroid) {
5380 barycentric_interp_modes |=
5381 1 << BRW_WM_PERSPECTIVE_CENTROID_BARYCENTRIC;
5382 } else if (is_sample) {
5383 barycentric_interp_modes |=
5384 1 << BRW_WM_PERSPECTIVE_SAMPLE_BARYCENTRIC;
5385 }
5386 if ((!is_centroid && !is_sample) ||
5387 devinfo->needs_unlit_centroid_workaround) {
5388 barycentric_interp_modes |=
5389 1 << BRW_WM_PERSPECTIVE_PIXEL_BARYCENTRIC;
5390 }
5391 }
5392 }
5393
5394 return barycentric_interp_modes;
5395 }
5396
5397 static uint8_t
5398 computed_depth_mode(const nir_shader *shader)
5399 {
5400 if (shader->info.outputs_written & BITFIELD64_BIT(FRAG_RESULT_DEPTH)) {
5401 switch (shader->info.fs.depth_layout) {
5402 case FRAG_DEPTH_LAYOUT_NONE:
5403 case FRAG_DEPTH_LAYOUT_ANY:
5404 return BRW_PSCDEPTH_ON;
5405 case FRAG_DEPTH_LAYOUT_GREATER:
5406 return BRW_PSCDEPTH_ON_GE;
5407 case FRAG_DEPTH_LAYOUT_LESS:
5408 return BRW_PSCDEPTH_ON_LE;
5409 case FRAG_DEPTH_LAYOUT_UNCHANGED:
5410 return BRW_PSCDEPTH_OFF;
5411 }
5412 }
5413 return BRW_PSCDEPTH_OFF;
5414 }
5415
5416 const unsigned *
5417 brw_compile_fs(const struct brw_compiler *compiler, void *log_data,
5418 void *mem_ctx,
5419 const struct brw_wm_prog_key *key,
5420 struct brw_wm_prog_data *prog_data,
5421 const nir_shader *shader,
5422 struct gl_program *prog,
5423 int shader_time_index8, int shader_time_index16,
5424 bool use_rep_send,
5425 unsigned *final_assembly_size,
5426 char **error_str)
5427 {
5428 /* key->alpha_test_func means simulating alpha testing via discards,
5429 * so the shader definitely kills pixels.
5430 */
5431 prog_data->uses_kill = shader->info.fs.uses_discard || key->alpha_test_func;
5432 prog_data->uses_omask =
5433 shader->info.outputs_written & BITFIELD64_BIT(FRAG_RESULT_SAMPLE_MASK);
5434 prog_data->computed_depth_mode = computed_depth_mode(shader);
5435 prog_data->computed_stencil =
5436 shader->info.outputs_written & BITFIELD64_BIT(FRAG_RESULT_STENCIL);
5437
5438 prog_data->early_fragment_tests = shader->info.fs.early_fragment_tests;
5439
5440 prog_data->barycentric_interp_modes =
5441 brw_compute_barycentric_interp_modes(compiler->devinfo,
5442 key->flat_shade,
5443 key->persample_shading,
5444 shader);
5445
5446 fs_visitor v(compiler, log_data, mem_ctx, key,
5447 &prog_data->base, prog, shader, 8,
5448 shader_time_index8);
5449 if (!v.run_fs(false /* do_rep_send */)) {
5450 if (error_str)
5451 *error_str = ralloc_strdup(mem_ctx, v.fail_msg);
5452
5453 return NULL;
5454 }
5455
5456 cfg_t *simd16_cfg = NULL;
5457 fs_visitor v2(compiler, log_data, mem_ctx, key,
5458 &prog_data->base, prog, shader, 16,
5459 shader_time_index16);
5460 if (likely(!(INTEL_DEBUG & DEBUG_NO16) || use_rep_send)) {
5461 if (!v.simd16_unsupported) {
5462 /* Try a SIMD16 compile */
5463 v2.import_uniforms(&v);
5464 if (!v2.run_fs(use_rep_send)) {
5465 compiler->shader_perf_log(log_data,
5466 "SIMD16 shader failed to compile: %s",
5467 v2.fail_msg);
5468 } else {
5469 simd16_cfg = v2.cfg;
5470 }
5471 }
5472 }
5473
5474 cfg_t *simd8_cfg;
5475 int no_simd8 = (INTEL_DEBUG & DEBUG_NO8) || use_rep_send;
5476 if ((no_simd8 || compiler->devinfo->gen < 5) && simd16_cfg) {
5477 simd8_cfg = NULL;
5478 prog_data->no_8 = true;
5479 } else {
5480 simd8_cfg = v.cfg;
5481 prog_data->no_8 = false;
5482 }
5483
5484 fs_generator g(compiler, log_data, mem_ctx, (void *) key, &prog_data->base,
5485 v.promoted_constants, v.runtime_check_aads_emit, "FS");
5486
5487 if (unlikely(INTEL_DEBUG & DEBUG_WM)) {
5488 g.enable_debug(ralloc_asprintf(mem_ctx, "%s fragment shader %s",
5489 shader->info.label ? shader->info.label :
5490 "unnamed",
5491 shader->info.name));
5492 }
5493
5494 if (simd8_cfg)
5495 g.generate_code(simd8_cfg, 8);
5496 if (simd16_cfg)
5497 prog_data->prog_offset_16 = g.generate_code(simd16_cfg, 16);
5498
5499 return g.get_assembly(final_assembly_size);
5500 }
5501
5502 void
5503 brw_cs_fill_local_id_payload(const struct brw_cs_prog_data *prog_data,
5504 void *buffer, uint32_t threads, uint32_t stride)
5505 {
5506 if (prog_data->local_invocation_id_regs == 0)
5507 return;
5508
5509 /* 'stride' should be an integer number of registers, that is, a multiple
5510 * of 32 bytes.
5511 */
5512 assert(stride % 32 == 0);
5513
5514 unsigned x = 0, y = 0, z = 0;
5515 for (unsigned t = 0; t < threads; t++) {
5516 uint32_t *param = (uint32_t *) buffer + stride * t / 4;
5517
5518 for (unsigned i = 0; i < prog_data->simd_size; i++) {
5519 param[0 * prog_data->simd_size + i] = x;
5520 param[1 * prog_data->simd_size + i] = y;
5521 param[2 * prog_data->simd_size + i] = z;
5522
5523 x++;
5524 if (x == prog_data->local_size[0]) {
5525 x = 0;
5526 y++;
5527 if (y == prog_data->local_size[1]) {
5528 y = 0;
5529 z++;
5530 if (z == prog_data->local_size[2])
5531 z = 0;
5532 }
5533 }
5534 }
5535 }
5536 }
5537
5538 fs_reg *
5539 fs_visitor::emit_cs_local_invocation_id_setup()
5540 {
5541 assert(stage == MESA_SHADER_COMPUTE);
5542
5543 fs_reg *reg = new(this->mem_ctx) fs_reg(vgrf(glsl_type::uvec3_type));
5544
5545 struct brw_reg src =
5546 brw_vec8_grf(payload.local_invocation_id_reg, 0);
5547 src = retype(src, BRW_REGISTER_TYPE_UD);
5548 bld.MOV(*reg, src);
5549 src.nr += dispatch_width / 8;
5550 bld.MOV(offset(*reg, bld, 1), src);
5551 src.nr += dispatch_width / 8;
5552 bld.MOV(offset(*reg, bld, 2), src);
5553
5554 return reg;
5555 }
5556
5557 fs_reg *
5558 fs_visitor::emit_cs_work_group_id_setup()
5559 {
5560 assert(stage == MESA_SHADER_COMPUTE);
5561
5562 fs_reg *reg = new(this->mem_ctx) fs_reg(vgrf(glsl_type::uvec3_type));
5563
5564 struct brw_reg r0_1(retype(brw_vec1_grf(0, 1), BRW_REGISTER_TYPE_UD));
5565 struct brw_reg r0_6(retype(brw_vec1_grf(0, 6), BRW_REGISTER_TYPE_UD));
5566 struct brw_reg r0_7(retype(brw_vec1_grf(0, 7), BRW_REGISTER_TYPE_UD));
5567
5568 bld.MOV(*reg, r0_1);
5569 bld.MOV(offset(*reg, bld, 1), r0_6);
5570 bld.MOV(offset(*reg, bld, 2), r0_7);
5571
5572 return reg;
5573 }
5574
5575 const unsigned *
5576 brw_compile_cs(const struct brw_compiler *compiler, void *log_data,
5577 void *mem_ctx,
5578 const struct brw_cs_prog_key *key,
5579 struct brw_cs_prog_data *prog_data,
5580 const nir_shader *shader,
5581 int shader_time_index,
5582 unsigned *final_assembly_size,
5583 char **error_str)
5584 {
5585 prog_data->local_size[0] = shader->info.cs.local_size[0];
5586 prog_data->local_size[1] = shader->info.cs.local_size[1];
5587 prog_data->local_size[2] = shader->info.cs.local_size[2];
5588 unsigned local_workgroup_size =
5589 shader->info.cs.local_size[0] * shader->info.cs.local_size[1] *
5590 shader->info.cs.local_size[2];
5591
5592 unsigned max_cs_threads = compiler->devinfo->max_cs_threads;
5593
5594 cfg_t *cfg = NULL;
5595 const char *fail_msg = NULL;
5596
5597 /* Now the main event: Visit the shader IR and generate our CS IR for it.
5598 */
5599 fs_visitor v8(compiler, log_data, mem_ctx, key, &prog_data->base,
5600 NULL, /* Never used in core profile */
5601 shader, 8, shader_time_index);
5602 if (!v8.run_cs()) {
5603 fail_msg = v8.fail_msg;
5604 } else if (local_workgroup_size <= 8 * max_cs_threads) {
5605 cfg = v8.cfg;
5606 prog_data->simd_size = 8;
5607 }
5608
5609 fs_visitor v16(compiler, log_data, mem_ctx, key, &prog_data->base,
5610 NULL, /* Never used in core profile */
5611 shader, 16, shader_time_index);
5612 if (likely(!(INTEL_DEBUG & DEBUG_NO16)) &&
5613 !fail_msg && !v8.simd16_unsupported &&
5614 local_workgroup_size <= 16 * max_cs_threads) {
5615 /* Try a SIMD16 compile */
5616 v16.import_uniforms(&v8);
5617 if (!v16.run_cs()) {
5618 compiler->shader_perf_log(log_data,
5619 "SIMD16 shader failed to compile: %s",
5620 v16.fail_msg);
5621 if (!cfg) {
5622 fail_msg =
5623 "Couldn't generate SIMD16 program and not "
5624 "enough threads for SIMD8";
5625 }
5626 } else {
5627 cfg = v16.cfg;
5628 prog_data->simd_size = 16;
5629 }
5630 }
5631
5632 if (unlikely(cfg == NULL)) {
5633 assert(fail_msg);
5634 if (error_str)
5635 *error_str = ralloc_strdup(mem_ctx, fail_msg);
5636
5637 return NULL;
5638 }
5639
5640 fs_generator g(compiler, log_data, mem_ctx, (void*) key, &prog_data->base,
5641 v8.promoted_constants, v8.runtime_check_aads_emit, "CS");
5642 if (INTEL_DEBUG & DEBUG_CS) {
5643 char *name = ralloc_asprintf(mem_ctx, "%s compute shader %s",
5644 shader->info.label ? shader->info.label :
5645 "unnamed",
5646 shader->info.name);
5647 g.enable_debug(name);
5648 }
5649
5650 g.generate_code(cfg, prog_data->simd_size);
5651
5652 return g.get_assembly(final_assembly_size);
5653 }