Added few more stubs so that control reaches to DestroyDevice().
[mesa.git] / src / intel / compiler / brw_fs_generator.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_generator.cpp
25 *
26 * This file supports generating code from the FS LIR to the actual
27 * native instructions.
28 */
29
30 #include "brw_eu.h"
31 #include "brw_fs.h"
32 #include "brw_cfg.h"
33 #include "util/mesa-sha1.h"
34
35 static enum brw_reg_file
36 brw_file_from_reg(fs_reg *reg)
37 {
38 switch (reg->file) {
39 case ARF:
40 return BRW_ARCHITECTURE_REGISTER_FILE;
41 case FIXED_GRF:
42 case VGRF:
43 return BRW_GENERAL_REGISTER_FILE;
44 case MRF:
45 return BRW_MESSAGE_REGISTER_FILE;
46 case IMM:
47 return BRW_IMMEDIATE_VALUE;
48 case BAD_FILE:
49 case ATTR:
50 case UNIFORM:
51 unreachable("not reached");
52 }
53 return BRW_ARCHITECTURE_REGISTER_FILE;
54 }
55
56 static struct brw_reg
57 brw_reg_from_fs_reg(const struct gen_device_info *devinfo, fs_inst *inst,
58 fs_reg *reg, bool compressed)
59 {
60 struct brw_reg brw_reg;
61
62 switch (reg->file) {
63 case MRF:
64 assert((reg->nr & ~BRW_MRF_COMPR4) < BRW_MAX_MRF(devinfo->gen));
65 /* Fallthrough */
66 case VGRF:
67 if (reg->stride == 0) {
68 brw_reg = brw_vec1_reg(brw_file_from_reg(reg), reg->nr, 0);
69 } else {
70 /* From the Haswell PRM:
71 *
72 * "VertStride must be used to cross GRF register boundaries. This
73 * rule implies that elements within a 'Width' cannot cross GRF
74 * boundaries."
75 *
76 * The maximum width value that could satisfy this restriction is:
77 */
78 const unsigned reg_width = REG_SIZE / (reg->stride * type_sz(reg->type));
79
80 /* Because the hardware can only split source regions at a whole
81 * multiple of width during decompression (i.e. vertically), clamp
82 * the value obtained above to the physical execution size of a
83 * single decompressed chunk of the instruction:
84 */
85 const unsigned phys_width = compressed ? inst->exec_size / 2 :
86 inst->exec_size;
87
88 const unsigned max_hw_width = 16;
89
90 /* XXX - The equation above is strictly speaking not correct on
91 * hardware that supports unbalanced GRF writes -- On Gen9+
92 * each decompressed chunk of the instruction may have a
93 * different execution size when the number of components
94 * written to each destination GRF is not the same.
95 */
96 if (reg->stride > 4) {
97 assert(reg != &inst->dst);
98 assert(reg->stride * type_sz(reg->type) <= REG_SIZE);
99 brw_reg = brw_vecn_reg(1, brw_file_from_reg(reg), reg->nr, 0);
100 brw_reg = stride(brw_reg, reg->stride, 1, 0);
101 } else {
102 const unsigned width = MIN3(reg_width, phys_width, max_hw_width);
103 brw_reg = brw_vecn_reg(width, brw_file_from_reg(reg), reg->nr, 0);
104 brw_reg = stride(brw_reg, width * reg->stride, width, reg->stride);
105 }
106
107 if (devinfo->gen == 7 && !devinfo->is_haswell) {
108 /* From the IvyBridge PRM (EU Changes by Processor Generation, page 13):
109 * "Each DF (Double Float) operand uses an element size of 4 rather
110 * than 8 and all regioning parameters are twice what the values
111 * would be based on the true element size: ExecSize, Width,
112 * HorzStride, and VertStride. Each DF operand uses a pair of
113 * channels and all masking and swizzing should be adjusted
114 * appropriately."
115 *
116 * From the IvyBridge PRM (Special Requirements for Handling Double
117 * Precision Data Types, page 71):
118 * "In Align1 mode, all regioning parameters like stride, execution
119 * size, and width must use the syntax of a pair of packed
120 * floats. The offsets for these data types must be 64-bit
121 * aligned. The execution size and regioning parameters are in terms
122 * of floats."
123 *
124 * Summarized: when handling DF-typed arguments, ExecSize,
125 * VertStride, and Width must be doubled.
126 *
127 * It applies to BayTrail too.
128 */
129 if (type_sz(reg->type) == 8) {
130 brw_reg.width++;
131 if (brw_reg.vstride > 0)
132 brw_reg.vstride++;
133 assert(brw_reg.hstride == BRW_HORIZONTAL_STRIDE_1);
134 }
135
136 /* When converting from DF->F, we set the destination stride to 2
137 * because each d2f conversion implicitly writes 2 floats, being
138 * the first one the converted value. IVB/BYT actually writes two
139 * F components per SIMD channel, and every other component is
140 * filled with garbage.
141 */
142 if (reg == &inst->dst && get_exec_type_size(inst) == 8 &&
143 type_sz(inst->dst.type) < 8) {
144 assert(brw_reg.hstride > BRW_HORIZONTAL_STRIDE_1);
145 brw_reg.hstride--;
146 }
147 }
148 }
149
150 brw_reg = retype(brw_reg, reg->type);
151 brw_reg = byte_offset(brw_reg, reg->offset);
152 brw_reg.abs = reg->abs;
153 brw_reg.negate = reg->negate;
154 break;
155 case ARF:
156 case FIXED_GRF:
157 case IMM:
158 assert(reg->offset == 0);
159 brw_reg = reg->as_brw_reg();
160 break;
161 case BAD_FILE:
162 /* Probably unused. */
163 brw_reg = brw_null_reg();
164 break;
165 case ATTR:
166 case UNIFORM:
167 unreachable("not reached");
168 }
169
170 /* On HSW+, scalar DF sources can be accessed using the normal <0,1,0>
171 * region, but on IVB and BYT DF regions must be programmed in terms of
172 * floats. A <0,2,1> region accomplishes this.
173 */
174 if (devinfo->gen == 7 && !devinfo->is_haswell &&
175 type_sz(reg->type) == 8 &&
176 brw_reg.vstride == BRW_VERTICAL_STRIDE_0 &&
177 brw_reg.width == BRW_WIDTH_1 &&
178 brw_reg.hstride == BRW_HORIZONTAL_STRIDE_0) {
179 brw_reg.width = BRW_WIDTH_2;
180 brw_reg.hstride = BRW_HORIZONTAL_STRIDE_1;
181 }
182
183 return brw_reg;
184 }
185
186 fs_generator::fs_generator(const struct brw_compiler *compiler, void *log_data,
187 void *mem_ctx,
188 struct brw_stage_prog_data *prog_data,
189 bool runtime_check_aads_emit,
190 gl_shader_stage stage)
191
192 : compiler(compiler), log_data(log_data),
193 devinfo(compiler->devinfo),
194 prog_data(prog_data),
195 runtime_check_aads_emit(runtime_check_aads_emit), debug_flag(false),
196 stage(stage), mem_ctx(mem_ctx)
197 {
198 p = rzalloc(mem_ctx, struct brw_codegen);
199 brw_init_codegen(devinfo, p, mem_ctx);
200
201 /* In the FS code generator, we are very careful to ensure that we always
202 * set the right execution size so we don't need the EU code to "help" us
203 * by trying to infer it. Sometimes, it infers the wrong thing.
204 */
205 p->automatic_exec_sizes = false;
206 }
207
208 fs_generator::~fs_generator()
209 {
210 }
211
212 class ip_record : public exec_node {
213 public:
214 DECLARE_RALLOC_CXX_OPERATORS(ip_record)
215
216 ip_record(int ip)
217 {
218 this->ip = ip;
219 }
220
221 int ip;
222 };
223
224 bool
225 fs_generator::patch_discard_jumps_to_fb_writes()
226 {
227 if (this->discard_halt_patches.is_empty())
228 return false;
229
230 int scale = brw_jump_scale(p->devinfo);
231
232 if (devinfo->gen >= 6) {
233 /* There is a somewhat strange undocumented requirement of using
234 * HALT, according to the simulator. If some channel has HALTed to
235 * a particular UIP, then by the end of the program, every channel
236 * must have HALTed to that UIP. Furthermore, the tracking is a
237 * stack, so you can't do the final halt of a UIP after starting
238 * halting to a new UIP.
239 *
240 * Symptoms of not emitting this instruction on actual hardware
241 * included GPU hangs and sparkly rendering on the piglit discard
242 * tests.
243 */
244 brw_inst *last_halt = brw_HALT(p);
245 brw_inst_set_uip(p->devinfo, last_halt, 1 * scale);
246 brw_inst_set_jip(p->devinfo, last_halt, 1 * scale);
247 }
248
249 int ip = p->nr_insn;
250
251 foreach_in_list(ip_record, patch_ip, &discard_halt_patches) {
252 brw_inst *patch = &p->store[patch_ip->ip];
253
254 assert(brw_inst_opcode(p->devinfo, patch) == BRW_OPCODE_HALT);
255 if (devinfo->gen >= 6) {
256 /* HALT takes a half-instruction distance from the pre-incremented IP. */
257 brw_inst_set_uip(p->devinfo, patch, (ip - patch_ip->ip) * scale);
258 } else {
259 brw_set_src1(p, patch, brw_imm_d((ip - patch_ip->ip) * scale));
260 }
261 }
262
263 this->discard_halt_patches.make_empty();
264
265 if (devinfo->gen < 6) {
266 /* From the g965 PRM:
267 *
268 * "As DMask is not automatically reloaded into AMask upon completion
269 * of this instruction, software has to manually restore AMask upon
270 * completion."
271 *
272 * DMask lives in the bottom 16 bits of sr0.1.
273 */
274 brw_inst *reset = brw_MOV(p, brw_mask_reg(BRW_AMASK),
275 retype(brw_sr0_reg(1), BRW_REGISTER_TYPE_UW));
276 brw_inst_set_exec_size(devinfo, reset, BRW_EXECUTE_1);
277 brw_inst_set_mask_control(devinfo, reset, BRW_MASK_DISABLE);
278 brw_inst_set_qtr_control(devinfo, reset, BRW_COMPRESSION_NONE);
279 brw_inst_set_thread_control(devinfo, reset, BRW_THREAD_SWITCH);
280 }
281
282 if (devinfo->gen == 4 && !devinfo->is_g4x) {
283 /* From the g965 PRM:
284 *
285 * "[DevBW, DevCL] Erratum: The subfields in mask stack register are
286 * reset to zero during graphics reset, however, they are not
287 * initialized at thread dispatch. These subfields will retain the
288 * values from the previous thread. Software should make sure the
289 * mask stack is empty (reset to zero) before terminating the thread.
290 * In case that this is not practical, software may have to reset the
291 * mask stack at the beginning of each kernel, which will impact the
292 * performance."
293 *
294 * Luckily we can rely on:
295 *
296 * "[DevBW, DevCL] This register access restriction is not
297 * applicable, hardware does ensure execution pipeline coherency,
298 * when a mask stack register is used as an explicit source and/or
299 * destination."
300 */
301 brw_push_insn_state(p);
302 brw_set_default_mask_control(p, BRW_MASK_DISABLE);
303 brw_set_default_compression_control(p, BRW_COMPRESSION_NONE);
304
305 brw_set_default_exec_size(p, BRW_EXECUTE_2);
306 brw_MOV(p, vec2(brw_mask_stack_depth_reg(0)), brw_imm_uw(0));
307
308 brw_set_default_exec_size(p, BRW_EXECUTE_16);
309 /* Reset the if stack. */
310 brw_MOV(p, retype(brw_mask_stack_reg(0), BRW_REGISTER_TYPE_UW),
311 brw_imm_uw(0));
312
313 brw_pop_insn_state(p);
314 }
315
316 return true;
317 }
318
319 void
320 fs_generator::generate_send(fs_inst *inst,
321 struct brw_reg dst,
322 struct brw_reg desc,
323 struct brw_reg ex_desc,
324 struct brw_reg payload,
325 struct brw_reg payload2)
326 {
327 const bool dst_is_null = dst.file == BRW_ARCHITECTURE_REGISTER_FILE &&
328 dst.nr == BRW_ARF_NULL;
329 const unsigned rlen = dst_is_null ? 0 : inst->size_written / REG_SIZE;
330
331 uint32_t desc_imm = inst->desc |
332 brw_message_desc(devinfo, inst->mlen, rlen, inst->header_size);
333
334 uint32_t ex_desc_imm = brw_message_ex_desc(devinfo, inst->ex_mlen);
335
336 if (ex_desc.file != BRW_IMMEDIATE_VALUE || ex_desc.ud || ex_desc_imm) {
337 /* If we have any sort of extended descriptor, then we need SENDS. This
338 * also covers the dual-payload case because ex_mlen goes in ex_desc.
339 */
340 brw_send_indirect_split_message(p, inst->sfid, dst, payload, payload2,
341 desc, desc_imm, ex_desc, ex_desc_imm,
342 inst->eot);
343 if (inst->check_tdr)
344 brw_inst_set_opcode(p->devinfo, brw_last_inst,
345 devinfo->gen >= 12 ? BRW_OPCODE_SENDC : BRW_OPCODE_SENDSC);
346 } else {
347 brw_send_indirect_message(p, inst->sfid, dst, payload, desc, desc_imm,
348 inst->eot);
349 if (inst->check_tdr)
350 brw_inst_set_opcode(p->devinfo, brw_last_inst, BRW_OPCODE_SENDC);
351 }
352 }
353
354 void
355 fs_generator::fire_fb_write(fs_inst *inst,
356 struct brw_reg payload,
357 struct brw_reg implied_header,
358 GLuint nr)
359 {
360 struct brw_wm_prog_data *prog_data = brw_wm_prog_data(this->prog_data);
361
362 if (devinfo->gen < 6) {
363 brw_push_insn_state(p);
364 brw_set_default_exec_size(p, BRW_EXECUTE_8);
365 brw_set_default_mask_control(p, BRW_MASK_DISABLE);
366 brw_set_default_predicate_control(p, BRW_PREDICATE_NONE);
367 brw_set_default_compression_control(p, BRW_COMPRESSION_NONE);
368 brw_MOV(p, offset(retype(payload, BRW_REGISTER_TYPE_UD), 1),
369 offset(retype(implied_header, BRW_REGISTER_TYPE_UD), 1));
370 brw_pop_insn_state(p);
371 }
372
373 uint32_t msg_control = brw_fb_write_msg_control(inst, prog_data);
374
375 /* We assume render targets start at 0, because headerless FB write
376 * messages set "Render Target Index" to 0. Using a different binding
377 * table index would make it impossible to use headerless messages.
378 */
379 const uint32_t surf_index = inst->target;
380
381 brw_inst *insn = brw_fb_WRITE(p,
382 payload,
383 retype(implied_header, BRW_REGISTER_TYPE_UW),
384 msg_control,
385 surf_index,
386 nr,
387 0,
388 inst->eot,
389 inst->last_rt,
390 inst->header_size != 0);
391
392 if (devinfo->gen >= 6)
393 brw_inst_set_rt_slot_group(devinfo, insn, inst->group / 16);
394 }
395
396 void
397 fs_generator::generate_fb_write(fs_inst *inst, struct brw_reg payload)
398 {
399 if (devinfo->gen < 8 && !devinfo->is_haswell) {
400 brw_set_default_predicate_control(p, BRW_PREDICATE_NONE);
401 brw_set_default_flag_reg(p, 0, 0);
402 }
403
404 const struct brw_reg implied_header =
405 devinfo->gen < 6 ? payload : brw_null_reg();
406
407 if (inst->base_mrf >= 0)
408 payload = brw_message_reg(inst->base_mrf);
409
410 if (!runtime_check_aads_emit) {
411 fire_fb_write(inst, payload, implied_header, inst->mlen);
412 } else {
413 /* This can only happen in gen < 6 */
414 assert(devinfo->gen < 6);
415
416 struct brw_reg v1_null_ud = vec1(retype(brw_null_reg(), BRW_REGISTER_TYPE_UD));
417
418 /* Check runtime bit to detect if we have to send AA data or not */
419 brw_push_insn_state(p);
420 brw_set_default_compression_control(p, BRW_COMPRESSION_NONE);
421 brw_set_default_exec_size(p, BRW_EXECUTE_1);
422 brw_AND(p,
423 v1_null_ud,
424 retype(brw_vec1_grf(1, 6), BRW_REGISTER_TYPE_UD),
425 brw_imm_ud(1<<26));
426 brw_inst_set_cond_modifier(p->devinfo, brw_last_inst, BRW_CONDITIONAL_NZ);
427
428 int jmp = brw_JMPI(p, brw_imm_ud(0), BRW_PREDICATE_NORMAL) - p->store;
429 brw_pop_insn_state(p);
430 {
431 /* Don't send AA data */
432 fire_fb_write(inst, offset(payload, 1), implied_header, inst->mlen-1);
433 }
434 brw_land_fwd_jump(p, jmp);
435 fire_fb_write(inst, payload, implied_header, inst->mlen);
436 }
437 }
438
439 void
440 fs_generator::generate_fb_read(fs_inst *inst, struct brw_reg dst,
441 struct brw_reg payload)
442 {
443 assert(inst->size_written % REG_SIZE == 0);
444 struct brw_wm_prog_data *prog_data = brw_wm_prog_data(this->prog_data);
445 /* We assume that render targets start at binding table index 0. */
446 const unsigned surf_index = inst->target;
447
448 gen9_fb_READ(p, dst, payload, surf_index,
449 inst->header_size, inst->size_written / REG_SIZE,
450 prog_data->persample_dispatch);
451 }
452
453 void
454 fs_generator::generate_mov_indirect(fs_inst *inst,
455 struct brw_reg dst,
456 struct brw_reg reg,
457 struct brw_reg indirect_byte_offset)
458 {
459 assert(indirect_byte_offset.type == BRW_REGISTER_TYPE_UD);
460 assert(indirect_byte_offset.file == BRW_GENERAL_REGISTER_FILE);
461 assert(!reg.abs && !reg.negate);
462 assert(reg.type == dst.type);
463
464 unsigned imm_byte_offset = reg.nr * REG_SIZE + reg.subnr;
465
466 if (indirect_byte_offset.file == BRW_IMMEDIATE_VALUE) {
467 imm_byte_offset += indirect_byte_offset.ud;
468
469 reg.nr = imm_byte_offset / REG_SIZE;
470 reg.subnr = imm_byte_offset % REG_SIZE;
471 if (type_sz(reg.type) > 4 && !devinfo->has_64bit_float) {
472 brw_MOV(p, subscript(dst, BRW_REGISTER_TYPE_D, 0),
473 subscript(reg, BRW_REGISTER_TYPE_D, 0));
474 brw_set_default_swsb(p, tgl_swsb_null());
475 brw_MOV(p, subscript(dst, BRW_REGISTER_TYPE_D, 1),
476 subscript(reg, BRW_REGISTER_TYPE_D, 1));
477 } else {
478 brw_MOV(p, dst, reg);
479 }
480 } else {
481 /* Prior to Broadwell, there are only 8 address registers. */
482 assert(inst->exec_size <= 8 || devinfo->gen >= 8);
483
484 /* We use VxH indirect addressing, clobbering a0.0 through a0.7. */
485 struct brw_reg addr = vec8(brw_address_reg(0));
486
487 /* Whether we can use destination dependency control without running the
488 * risk of a hang if an instruction gets shot down.
489 */
490 const bool use_dep_ctrl = !inst->predicate &&
491 inst->exec_size == dispatch_width;
492 brw_inst *insn;
493
494 /* The destination stride of an instruction (in bytes) must be greater
495 * than or equal to the size of the rest of the instruction. Since the
496 * address register is of type UW, we can't use a D-type instruction.
497 * In order to get around this, re retype to UW and use a stride.
498 */
499 indirect_byte_offset =
500 retype(spread(indirect_byte_offset, 2), BRW_REGISTER_TYPE_UW);
501
502 /* There are a number of reasons why we don't use the base offset here.
503 * One reason is that the field is only 9 bits which means we can only
504 * use it to access the first 16 GRFs. Also, from the Haswell PRM
505 * section "Register Region Restrictions":
506 *
507 * "The lower bits of the AddressImmediate must not overflow to
508 * change the register address. The lower 5 bits of Address
509 * Immediate when added to lower 5 bits of address register gives
510 * the sub-register offset. The upper bits of Address Immediate
511 * when added to upper bits of address register gives the register
512 * address. Any overflow from sub-register offset is dropped."
513 *
514 * Since the indirect may cause us to cross a register boundary, this
515 * makes the base offset almost useless. We could try and do something
516 * clever where we use a actual base offset if base_offset % 32 == 0 but
517 * that would mean we were generating different code depending on the
518 * base offset. Instead, for the sake of consistency, we'll just do the
519 * add ourselves. This restriction is only listed in the Haswell PRM
520 * but empirical testing indicates that it applies on all older
521 * generations and is lifted on Broadwell.
522 *
523 * In the end, while base_offset is nice to look at in the generated
524 * code, using it saves us 0 instructions and would require quite a bit
525 * of case-by-case work. It's just not worth it.
526 *
527 * Due to a hardware bug some platforms (particularly Gen11+) seem to
528 * require the address components of all channels to be valid whether or
529 * not they're active, which causes issues if we use VxH addressing
530 * under non-uniform control-flow. We can easily work around that by
531 * initializing the whole address register with a pipelined NoMask MOV
532 * instruction.
533 */
534 if (devinfo->gen >= 7) {
535 insn = brw_MOV(p, addr, brw_imm_uw(imm_byte_offset));
536 brw_inst_set_mask_control(devinfo, insn, BRW_MASK_DISABLE);
537 brw_inst_set_pred_control(devinfo, insn, BRW_PREDICATE_NONE);
538 if (devinfo->gen >= 12)
539 brw_set_default_swsb(p, tgl_swsb_null());
540 else
541 brw_inst_set_no_dd_clear(devinfo, insn, use_dep_ctrl);
542 }
543
544 insn = brw_ADD(p, addr, indirect_byte_offset, brw_imm_uw(imm_byte_offset));
545 if (devinfo->gen >= 12)
546 brw_set_default_swsb(p, tgl_swsb_regdist(1));
547 else if (devinfo->gen >= 7)
548 brw_inst_set_no_dd_check(devinfo, insn, use_dep_ctrl);
549
550 if (type_sz(reg.type) > 4 &&
551 ((devinfo->gen == 7 && !devinfo->is_haswell) ||
552 devinfo->is_cherryview || gen_device_info_is_9lp(devinfo) ||
553 !devinfo->has_64bit_float)) {
554 /* IVB has an issue (which we found empirically) where it reads two
555 * address register components per channel for indirectly addressed
556 * 64-bit sources.
557 *
558 * From the Cherryview PRM Vol 7. "Register Region Restrictions":
559 *
560 * "When source or destination datatype is 64b or operation is
561 * integer DWord multiply, indirect addressing must not be used."
562 *
563 * To work around both of these, we do two integer MOVs insead of one
564 * 64-bit MOV. Because no double value should ever cross a register
565 * boundary, it's safe to use the immediate offset in the indirect
566 * here to handle adding 4 bytes to the offset and avoid the extra
567 * ADD to the register file.
568 */
569 brw_MOV(p, subscript(dst, BRW_REGISTER_TYPE_D, 0),
570 retype(brw_VxH_indirect(0, 0), BRW_REGISTER_TYPE_D));
571 brw_set_default_swsb(p, tgl_swsb_null());
572 brw_MOV(p, subscript(dst, BRW_REGISTER_TYPE_D, 1),
573 retype(brw_VxH_indirect(0, 4), BRW_REGISTER_TYPE_D));
574 } else {
575 struct brw_reg ind_src = brw_VxH_indirect(0, 0);
576
577 brw_inst *mov = brw_MOV(p, dst, retype(ind_src, reg.type));
578
579 if (devinfo->gen == 6 && dst.file == BRW_MESSAGE_REGISTER_FILE &&
580 !inst->get_next()->is_tail_sentinel() &&
581 ((fs_inst *)inst->get_next())->mlen > 0) {
582 /* From the Sandybridge PRM:
583 *
584 * "[Errata: DevSNB(SNB)] If MRF register is updated by any
585 * instruction that “indexed/indirect” source AND is followed
586 * by a send, the instruction requires a “Switch”. This is to
587 * avoid race condition where send may dispatch before MRF is
588 * updated."
589 */
590 brw_inst_set_thread_control(devinfo, mov, BRW_THREAD_SWITCH);
591 }
592 }
593 }
594 }
595
596 void
597 fs_generator::generate_shuffle(fs_inst *inst,
598 struct brw_reg dst,
599 struct brw_reg src,
600 struct brw_reg idx)
601 {
602 /* Ivy bridge has some strange behavior that makes this a real pain to
603 * implement for 64-bit values so we just don't bother.
604 */
605 assert(devinfo->gen >= 8 || devinfo->is_haswell || type_sz(src.type) <= 4);
606
607 /* Because we're using the address register, we're limited to 8-wide
608 * execution on gen7. On gen8, we're limited to 16-wide by the address
609 * register file and 8-wide for 64-bit types. We could try and make this
610 * instruction splittable higher up in the compiler but that gets weird
611 * because it reads all of the channels regardless of execution size. It's
612 * easier just to split it here.
613 */
614 const unsigned lower_width =
615 (devinfo->gen <= 7 || type_sz(src.type) > 4) ?
616 8 : MIN2(16, inst->exec_size);
617
618 brw_set_default_exec_size(p, cvt(lower_width) - 1);
619 for (unsigned group = 0; group < inst->exec_size; group += lower_width) {
620 brw_set_default_group(p, group);
621
622 if ((src.vstride == 0 && src.hstride == 0) ||
623 idx.file == BRW_IMMEDIATE_VALUE) {
624 /* Trivial, the source is already uniform or the index is a constant.
625 * We will typically not get here if the optimizer is doing its job,
626 * but asserting would be mean.
627 */
628 const unsigned i = idx.file == BRW_IMMEDIATE_VALUE ? idx.ud : 0;
629 brw_MOV(p, suboffset(dst, group), stride(suboffset(src, i), 0, 1, 0));
630 } else {
631 /* We use VxH indirect addressing, clobbering a0.0 through a0.7. */
632 struct brw_reg addr = vec8(brw_address_reg(0));
633
634 struct brw_reg group_idx = suboffset(idx, group);
635
636 if (lower_width == 8 && group_idx.width == BRW_WIDTH_16) {
637 /* Things get grumpy if the register is too wide. */
638 group_idx.width--;
639 group_idx.vstride--;
640 }
641
642 assert(type_sz(group_idx.type) <= 4);
643 if (type_sz(group_idx.type) == 4) {
644 /* The destination stride of an instruction (in bytes) must be
645 * greater than or equal to the size of the rest of the
646 * instruction. Since the address register is of type UW, we
647 * can't use a D-type instruction. In order to get around this,
648 * re retype to UW and use a stride.
649 */
650 group_idx = retype(spread(group_idx, 2), BRW_REGISTER_TYPE_W);
651 }
652
653 /* Take into account the component size and horizontal stride. */
654 assert(src.vstride == src.hstride + src.width);
655 brw_SHL(p, addr, group_idx,
656 brw_imm_uw(util_logbase2(type_sz(src.type)) +
657 src.hstride - 1));
658
659 /* Add on the register start offset */
660 brw_set_default_swsb(p, tgl_swsb_regdist(1));
661 brw_ADD(p, addr, addr, brw_imm_uw(src.nr * REG_SIZE + src.subnr));
662
663 if (type_sz(src.type) > 4 &&
664 ((devinfo->gen == 7 && !devinfo->is_haswell) ||
665 devinfo->is_cherryview || gen_device_info_is_9lp(devinfo))) {
666 /* IVB has an issue (which we found empirically) where it reads
667 * two address register components per channel for indirectly
668 * addressed 64-bit sources.
669 *
670 * From the Cherryview PRM Vol 7. "Register Region Restrictions":
671 *
672 * "When source or destination datatype is 64b or operation is
673 * integer DWord multiply, indirect addressing must not be
674 * used."
675 *
676 * To work around both of these, we do two integer MOVs insead of
677 * one 64-bit MOV. Because no double value should ever cross a
678 * register boundary, it's safe to use the immediate offset in the
679 * indirect here to handle adding 4 bytes to the offset and avoid
680 * the extra ADD to the register file.
681 */
682 struct brw_reg gdst = suboffset(dst, group);
683 struct brw_reg dst_d = retype(spread(gdst, 2),
684 BRW_REGISTER_TYPE_D);
685 assert(dst.hstride == 1);
686 brw_MOV(p, dst_d,
687 retype(brw_VxH_indirect(0, 0), BRW_REGISTER_TYPE_D));
688 brw_set_default_swsb(p, tgl_swsb_null());
689 brw_MOV(p, byte_offset(dst_d, 4),
690 retype(brw_VxH_indirect(0, 4), BRW_REGISTER_TYPE_D));
691 } else {
692 brw_MOV(p, suboffset(dst, group * dst.hstride),
693 retype(brw_VxH_indirect(0, 0), src.type));
694 }
695 }
696
697 brw_set_default_swsb(p, tgl_swsb_null());
698 }
699 }
700
701 void
702 fs_generator::generate_quad_swizzle(const fs_inst *inst,
703 struct brw_reg dst, struct brw_reg src,
704 unsigned swiz)
705 {
706 /* Requires a quad. */
707 assert(inst->exec_size >= 4);
708
709 if (src.file == BRW_IMMEDIATE_VALUE ||
710 has_scalar_region(src)) {
711 /* The value is uniform across all channels */
712 brw_MOV(p, dst, src);
713
714 } else if (devinfo->gen < 11 && type_sz(src.type) == 4) {
715 /* This only works on 8-wide 32-bit values */
716 assert(inst->exec_size == 8);
717 assert(src.hstride == BRW_HORIZONTAL_STRIDE_1);
718 assert(src.vstride == src.width + 1);
719 brw_set_default_access_mode(p, BRW_ALIGN_16);
720 struct brw_reg swiz_src = stride(src, 4, 4, 1);
721 swiz_src.swizzle = swiz;
722 brw_MOV(p, dst, swiz_src);
723
724 } else {
725 assert(src.hstride == BRW_HORIZONTAL_STRIDE_1);
726 assert(src.vstride == src.width + 1);
727 const struct brw_reg src_0 = suboffset(src, BRW_GET_SWZ(swiz, 0));
728
729 switch (swiz) {
730 case BRW_SWIZZLE_XXXX:
731 case BRW_SWIZZLE_YYYY:
732 case BRW_SWIZZLE_ZZZZ:
733 case BRW_SWIZZLE_WWWW:
734 brw_MOV(p, dst, stride(src_0, 4, 4, 0));
735 break;
736
737 case BRW_SWIZZLE_XXZZ:
738 case BRW_SWIZZLE_YYWW:
739 brw_MOV(p, dst, stride(src_0, 2, 2, 0));
740 break;
741
742 case BRW_SWIZZLE_XYXY:
743 case BRW_SWIZZLE_ZWZW:
744 assert(inst->exec_size == 4);
745 brw_MOV(p, dst, stride(src_0, 0, 2, 1));
746 break;
747
748 default:
749 assert(inst->force_writemask_all);
750 brw_set_default_exec_size(p, cvt(inst->exec_size / 4) - 1);
751
752 for (unsigned c = 0; c < 4; c++) {
753 brw_inst *insn = brw_MOV(
754 p, stride(suboffset(dst, c),
755 4 * inst->dst.stride, 1, 4 * inst->dst.stride),
756 stride(suboffset(src, BRW_GET_SWZ(swiz, c)), 4, 1, 0));
757
758 if (devinfo->gen < 12) {
759 brw_inst_set_no_dd_clear(devinfo, insn, c < 3);
760 brw_inst_set_no_dd_check(devinfo, insn, c > 0);
761 }
762
763 brw_set_default_swsb(p, tgl_swsb_null());
764 }
765
766 break;
767 }
768 }
769 }
770
771 void
772 fs_generator::generate_urb_read(fs_inst *inst,
773 struct brw_reg dst,
774 struct brw_reg header)
775 {
776 assert(inst->size_written % REG_SIZE == 0);
777 assert(header.file == BRW_GENERAL_REGISTER_FILE);
778 assert(header.type == BRW_REGISTER_TYPE_UD);
779
780 brw_inst *send = brw_next_insn(p, BRW_OPCODE_SEND);
781 brw_set_dest(p, send, retype(dst, BRW_REGISTER_TYPE_UD));
782 brw_set_src0(p, send, header);
783 if (devinfo->gen < 12)
784 brw_set_src1(p, send, brw_imm_ud(0u));
785
786 brw_inst_set_sfid(p->devinfo, send, BRW_SFID_URB);
787 brw_inst_set_urb_opcode(p->devinfo, send, GEN8_URB_OPCODE_SIMD8_READ);
788
789 if (inst->opcode == SHADER_OPCODE_URB_READ_SIMD8_PER_SLOT)
790 brw_inst_set_urb_per_slot_offset(p->devinfo, send, true);
791
792 brw_inst_set_mlen(p->devinfo, send, inst->mlen);
793 brw_inst_set_rlen(p->devinfo, send, inst->size_written / REG_SIZE);
794 brw_inst_set_header_present(p->devinfo, send, true);
795 brw_inst_set_urb_global_offset(p->devinfo, send, inst->offset);
796 }
797
798 void
799 fs_generator::generate_urb_write(fs_inst *inst, struct brw_reg payload)
800 {
801 brw_inst *insn;
802
803 /* WaClearTDRRegBeforeEOTForNonPS.
804 *
805 * WA: Clear tdr register before send EOT in all non-PS shader kernels
806 *
807 * mov(8) tdr0:ud 0x0:ud {NoMask}"
808 */
809 if (inst->eot && p->devinfo->gen == 10) {
810 brw_push_insn_state(p);
811 brw_set_default_mask_control(p, BRW_MASK_DISABLE);
812 brw_MOV(p, brw_tdr_reg(), brw_imm_uw(0));
813 brw_pop_insn_state(p);
814 }
815
816 insn = brw_next_insn(p, BRW_OPCODE_SEND);
817
818 brw_set_dest(p, insn, brw_null_reg());
819 brw_set_src0(p, insn, payload);
820 if (devinfo->gen < 12)
821 brw_set_src1(p, insn, brw_imm_ud(0u));
822
823 brw_inst_set_sfid(p->devinfo, insn, BRW_SFID_URB);
824 brw_inst_set_urb_opcode(p->devinfo, insn, GEN8_URB_OPCODE_SIMD8_WRITE);
825
826 if (inst->opcode == SHADER_OPCODE_URB_WRITE_SIMD8_PER_SLOT ||
827 inst->opcode == SHADER_OPCODE_URB_WRITE_SIMD8_MASKED_PER_SLOT)
828 brw_inst_set_urb_per_slot_offset(p->devinfo, insn, true);
829
830 if (inst->opcode == SHADER_OPCODE_URB_WRITE_SIMD8_MASKED ||
831 inst->opcode == SHADER_OPCODE_URB_WRITE_SIMD8_MASKED_PER_SLOT)
832 brw_inst_set_urb_channel_mask_present(p->devinfo, insn, true);
833
834 brw_inst_set_mlen(p->devinfo, insn, inst->mlen);
835 brw_inst_set_rlen(p->devinfo, insn, 0);
836 brw_inst_set_eot(p->devinfo, insn, inst->eot);
837 brw_inst_set_header_present(p->devinfo, insn, true);
838 brw_inst_set_urb_global_offset(p->devinfo, insn, inst->offset);
839 }
840
841 void
842 fs_generator::generate_cs_terminate(fs_inst *inst, struct brw_reg payload)
843 {
844 struct brw_inst *insn;
845
846 insn = brw_next_insn(p, BRW_OPCODE_SEND);
847
848 brw_set_dest(p, insn, retype(brw_null_reg(), BRW_REGISTER_TYPE_UW));
849 brw_set_src0(p, insn, retype(payload, BRW_REGISTER_TYPE_UW));
850 if (devinfo->gen < 12)
851 brw_set_src1(p, insn, brw_imm_ud(0u));
852
853 /* Terminate a compute shader by sending a message to the thread spawner.
854 */
855 brw_inst_set_sfid(devinfo, insn, BRW_SFID_THREAD_SPAWNER);
856 brw_inst_set_mlen(devinfo, insn, 1);
857 brw_inst_set_rlen(devinfo, insn, 0);
858 brw_inst_set_eot(devinfo, insn, inst->eot);
859 brw_inst_set_header_present(devinfo, insn, false);
860
861 brw_inst_set_ts_opcode(devinfo, insn, 0); /* Dereference resource */
862
863 if (devinfo->gen < 11) {
864 brw_inst_set_ts_request_type(devinfo, insn, 0); /* Root thread */
865
866 /* Note that even though the thread has a URB resource associated with it,
867 * we set the "do not dereference URB" bit, because the URB resource is
868 * managed by the fixed-function unit, so it will free it automatically.
869 */
870 brw_inst_set_ts_resource_select(devinfo, insn, 1); /* Do not dereference URB */
871 }
872
873 brw_inst_set_mask_control(devinfo, insn, BRW_MASK_DISABLE);
874 }
875
876 void
877 fs_generator::generate_barrier(fs_inst *, struct brw_reg src)
878 {
879 brw_barrier(p, src);
880 if (devinfo->gen >= 12) {
881 brw_set_default_swsb(p, tgl_swsb_null());
882 brw_SYNC(p, TGL_SYNC_BAR);
883 } else {
884 brw_WAIT(p);
885 }
886 }
887
888 bool
889 fs_generator::generate_linterp(fs_inst *inst,
890 struct brw_reg dst, struct brw_reg *src)
891 {
892 /* PLN reads:
893 * / in SIMD16 \
894 * -----------------------------------
895 * | src1+0 | src1+1 | src1+2 | src1+3 |
896 * |-----------------------------------|
897 * |(x0, x1)|(y0, y1)|(x2, x3)|(y2, y3)|
898 * -----------------------------------
899 *
900 * but for the LINE/MAC pair, the LINE reads Xs and the MAC reads Ys:
901 *
902 * -----------------------------------
903 * | src1+0 | src1+1 | src1+2 | src1+3 |
904 * |-----------------------------------|
905 * |(x0, x1)|(y0, y1)| | | in SIMD8
906 * |-----------------------------------|
907 * |(x0, x1)|(x2, x3)|(y0, y1)|(y2, y3)| in SIMD16
908 * -----------------------------------
909 *
910 * See also: emit_interpolation_setup_gen4().
911 */
912 struct brw_reg delta_x = src[0];
913 struct brw_reg delta_y = offset(src[0], inst->exec_size / 8);
914 struct brw_reg interp = src[1];
915 brw_inst *i[2];
916
917 /* nir_lower_interpolation() will do the lowering to MAD instructions for
918 * us on gen11+
919 */
920 assert(devinfo->gen < 11);
921
922 if (devinfo->has_pln) {
923 if (devinfo->gen <= 6 && (delta_x.nr & 1) != 0) {
924 /* From the Sandy Bridge PRM Vol. 4, Pt. 2, Section 8.3.53, "Plane":
925 *
926 * "[DevSNB]:<src1> must be even register aligned.
927 *
928 * This restriction is lifted on Ivy Bridge.
929 *
930 * This means that we need to split PLN into LINE+MAC on-the-fly.
931 * Unfortunately, the inputs are laid out for PLN and not LINE+MAC so
932 * we have to split into SIMD8 pieces. For gen4 (!has_pln), the
933 * coordinate registers are laid out differently so we leave it as a
934 * SIMD16 instruction.
935 */
936 assert(inst->exec_size == 8 || inst->exec_size == 16);
937 assert(inst->group % 16 == 0);
938
939 brw_push_insn_state(p);
940 brw_set_default_exec_size(p, BRW_EXECUTE_8);
941
942 /* Thanks to two accumulators, we can emit all the LINEs and then all
943 * the MACs. This improves parallelism a bit.
944 */
945 for (unsigned g = 0; g < inst->exec_size / 8; g++) {
946 brw_inst *line = brw_LINE(p, brw_null_reg(), interp,
947 offset(delta_x, g * 2));
948 brw_inst_set_group(devinfo, line, inst->group + g * 8);
949
950 /* LINE writes the accumulator automatically on gen4-5. On Sandy
951 * Bridge and later, we have to explicitly enable it.
952 */
953 if (devinfo->gen >= 6)
954 brw_inst_set_acc_wr_control(p->devinfo, line, true);
955
956 /* brw_set_default_saturate() is called before emitting
957 * instructions, so the saturate bit is set in each instruction,
958 * so we need to unset it on the LINE instructions.
959 */
960 brw_inst_set_saturate(p->devinfo, line, false);
961 }
962
963 for (unsigned g = 0; g < inst->exec_size / 8; g++) {
964 brw_inst *mac = brw_MAC(p, offset(dst, g), suboffset(interp, 1),
965 offset(delta_x, g * 2 + 1));
966 brw_inst_set_group(devinfo, mac, inst->group + g * 8);
967 brw_inst_set_cond_modifier(p->devinfo, mac, inst->conditional_mod);
968 }
969
970 brw_pop_insn_state(p);
971
972 return true;
973 } else {
974 brw_PLN(p, dst, interp, delta_x);
975
976 return false;
977 }
978 } else {
979 i[0] = brw_LINE(p, brw_null_reg(), interp, delta_x);
980 i[1] = brw_MAC(p, dst, suboffset(interp, 1), delta_y);
981
982 brw_inst_set_cond_modifier(p->devinfo, i[1], inst->conditional_mod);
983
984 /* brw_set_default_saturate() is called before emitting instructions, so
985 * the saturate bit is set in each instruction, so we need to unset it on
986 * the first instruction.
987 */
988 brw_inst_set_saturate(p->devinfo, i[0], false);
989
990 return true;
991 }
992 }
993
994 void
995 fs_generator::generate_get_buffer_size(fs_inst *inst,
996 struct brw_reg dst,
997 struct brw_reg src,
998 struct brw_reg surf_index)
999 {
1000 assert(devinfo->gen >= 7);
1001 assert(surf_index.file == BRW_IMMEDIATE_VALUE);
1002
1003 uint32_t simd_mode;
1004 int rlen = 4;
1005
1006 switch (inst->exec_size) {
1007 case 8:
1008 simd_mode = BRW_SAMPLER_SIMD_MODE_SIMD8;
1009 break;
1010 case 16:
1011 simd_mode = BRW_SAMPLER_SIMD_MODE_SIMD16;
1012 break;
1013 default:
1014 unreachable("Invalid width for texture instruction");
1015 }
1016
1017 if (simd_mode == BRW_SAMPLER_SIMD_MODE_SIMD16) {
1018 rlen = 8;
1019 dst = vec16(dst);
1020 }
1021
1022 brw_SAMPLE(p,
1023 retype(dst, BRW_REGISTER_TYPE_UW),
1024 inst->base_mrf,
1025 src,
1026 surf_index.ud,
1027 0,
1028 GEN5_SAMPLER_MESSAGE_SAMPLE_RESINFO,
1029 rlen, /* response length */
1030 inst->mlen,
1031 inst->header_size > 0,
1032 simd_mode,
1033 BRW_SAMPLER_RETURN_FORMAT_SINT32);
1034 }
1035
1036 void
1037 fs_generator::generate_tex(fs_inst *inst, struct brw_reg dst,
1038 struct brw_reg surface_index,
1039 struct brw_reg sampler_index)
1040 {
1041 assert(devinfo->gen < 7);
1042 assert(inst->size_written % REG_SIZE == 0);
1043 int msg_type = -1;
1044 uint32_t simd_mode;
1045 uint32_t return_format;
1046
1047 /* Sampler EOT message of less than the dispatch width would kill the
1048 * thread prematurely.
1049 */
1050 assert(!inst->eot || inst->exec_size == dispatch_width);
1051
1052 switch (dst.type) {
1053 case BRW_REGISTER_TYPE_D:
1054 return_format = BRW_SAMPLER_RETURN_FORMAT_SINT32;
1055 break;
1056 case BRW_REGISTER_TYPE_UD:
1057 return_format = BRW_SAMPLER_RETURN_FORMAT_UINT32;
1058 break;
1059 default:
1060 return_format = BRW_SAMPLER_RETURN_FORMAT_FLOAT32;
1061 break;
1062 }
1063
1064 /* Stomp the resinfo output type to UINT32. On gens 4-5, the output type
1065 * is set as part of the message descriptor. On gen4, the PRM seems to
1066 * allow UINT32 and FLOAT32 (i965 PRM, Vol. 4 Section 4.8.1.1), but on
1067 * later gens UINT32 is required. Once you hit Sandy Bridge, the bit is
1068 * gone from the message descriptor entirely and you just get UINT32 all
1069 * the time regasrdless. Since we can really only do non-UINT32 on gen4,
1070 * just stomp it to UINT32 all the time.
1071 */
1072 if (inst->opcode == SHADER_OPCODE_TXS)
1073 return_format = BRW_SAMPLER_RETURN_FORMAT_UINT32;
1074
1075 switch (inst->exec_size) {
1076 case 8:
1077 simd_mode = BRW_SAMPLER_SIMD_MODE_SIMD8;
1078 break;
1079 case 16:
1080 simd_mode = BRW_SAMPLER_SIMD_MODE_SIMD16;
1081 break;
1082 default:
1083 unreachable("Invalid width for texture instruction");
1084 }
1085
1086 if (devinfo->gen >= 5) {
1087 switch (inst->opcode) {
1088 case SHADER_OPCODE_TEX:
1089 if (inst->shadow_compare) {
1090 msg_type = GEN5_SAMPLER_MESSAGE_SAMPLE_COMPARE;
1091 } else {
1092 msg_type = GEN5_SAMPLER_MESSAGE_SAMPLE;
1093 }
1094 break;
1095 case FS_OPCODE_TXB:
1096 if (inst->shadow_compare) {
1097 msg_type = GEN5_SAMPLER_MESSAGE_SAMPLE_BIAS_COMPARE;
1098 } else {
1099 msg_type = GEN5_SAMPLER_MESSAGE_SAMPLE_BIAS;
1100 }
1101 break;
1102 case SHADER_OPCODE_TXL:
1103 if (inst->shadow_compare) {
1104 msg_type = GEN5_SAMPLER_MESSAGE_SAMPLE_LOD_COMPARE;
1105 } else {
1106 msg_type = GEN5_SAMPLER_MESSAGE_SAMPLE_LOD;
1107 }
1108 break;
1109 case SHADER_OPCODE_TXS:
1110 msg_type = GEN5_SAMPLER_MESSAGE_SAMPLE_RESINFO;
1111 break;
1112 case SHADER_OPCODE_TXD:
1113 assert(!inst->shadow_compare);
1114 msg_type = GEN5_SAMPLER_MESSAGE_SAMPLE_DERIVS;
1115 break;
1116 case SHADER_OPCODE_TXF:
1117 msg_type = GEN5_SAMPLER_MESSAGE_SAMPLE_LD;
1118 break;
1119 case SHADER_OPCODE_TXF_CMS:
1120 msg_type = GEN5_SAMPLER_MESSAGE_SAMPLE_LD;
1121 break;
1122 case SHADER_OPCODE_LOD:
1123 msg_type = GEN5_SAMPLER_MESSAGE_LOD;
1124 break;
1125 case SHADER_OPCODE_TG4:
1126 assert(devinfo->gen == 6);
1127 assert(!inst->shadow_compare);
1128 msg_type = GEN7_SAMPLER_MESSAGE_SAMPLE_GATHER4;
1129 break;
1130 case SHADER_OPCODE_SAMPLEINFO:
1131 msg_type = GEN6_SAMPLER_MESSAGE_SAMPLE_SAMPLEINFO;
1132 break;
1133 default:
1134 unreachable("not reached");
1135 }
1136 } else {
1137 switch (inst->opcode) {
1138 case SHADER_OPCODE_TEX:
1139 /* Note that G45 and older determines shadow compare and dispatch width
1140 * from message length for most messages.
1141 */
1142 if (inst->exec_size == 8) {
1143 msg_type = BRW_SAMPLER_MESSAGE_SIMD8_SAMPLE;
1144 if (inst->shadow_compare) {
1145 assert(inst->mlen == 6);
1146 } else {
1147 assert(inst->mlen <= 4);
1148 }
1149 } else {
1150 if (inst->shadow_compare) {
1151 msg_type = BRW_SAMPLER_MESSAGE_SIMD16_SAMPLE_COMPARE;
1152 assert(inst->mlen == 9);
1153 } else {
1154 msg_type = BRW_SAMPLER_MESSAGE_SIMD16_SAMPLE;
1155 assert(inst->mlen <= 7 && inst->mlen % 2 == 1);
1156 }
1157 }
1158 break;
1159 case FS_OPCODE_TXB:
1160 if (inst->shadow_compare) {
1161 assert(inst->exec_size == 8);
1162 assert(inst->mlen == 6);
1163 msg_type = BRW_SAMPLER_MESSAGE_SIMD8_SAMPLE_BIAS_COMPARE;
1164 } else {
1165 assert(inst->mlen == 9);
1166 msg_type = BRW_SAMPLER_MESSAGE_SIMD16_SAMPLE_BIAS;
1167 simd_mode = BRW_SAMPLER_SIMD_MODE_SIMD16;
1168 }
1169 break;
1170 case SHADER_OPCODE_TXL:
1171 if (inst->shadow_compare) {
1172 assert(inst->exec_size == 8);
1173 assert(inst->mlen == 6);
1174 msg_type = BRW_SAMPLER_MESSAGE_SIMD8_SAMPLE_LOD_COMPARE;
1175 } else {
1176 assert(inst->mlen == 9);
1177 msg_type = BRW_SAMPLER_MESSAGE_SIMD16_SAMPLE_LOD;
1178 simd_mode = BRW_SAMPLER_SIMD_MODE_SIMD16;
1179 }
1180 break;
1181 case SHADER_OPCODE_TXD:
1182 /* There is no sample_d_c message; comparisons are done manually */
1183 assert(inst->exec_size == 8);
1184 assert(inst->mlen == 7 || inst->mlen == 10);
1185 msg_type = BRW_SAMPLER_MESSAGE_SIMD8_SAMPLE_GRADIENTS;
1186 break;
1187 case SHADER_OPCODE_TXF:
1188 assert(inst->mlen <= 9 && inst->mlen % 2 == 1);
1189 msg_type = BRW_SAMPLER_MESSAGE_SIMD16_LD;
1190 simd_mode = BRW_SAMPLER_SIMD_MODE_SIMD16;
1191 break;
1192 case SHADER_OPCODE_TXS:
1193 assert(inst->mlen == 3);
1194 msg_type = BRW_SAMPLER_MESSAGE_SIMD16_RESINFO;
1195 simd_mode = BRW_SAMPLER_SIMD_MODE_SIMD16;
1196 break;
1197 default:
1198 unreachable("not reached");
1199 }
1200 }
1201 assert(msg_type != -1);
1202
1203 if (simd_mode == BRW_SAMPLER_SIMD_MODE_SIMD16) {
1204 dst = vec16(dst);
1205 }
1206
1207 assert(sampler_index.type == BRW_REGISTER_TYPE_UD);
1208
1209 /* Load the message header if present. If there's a texture offset,
1210 * we need to set it up explicitly and load the offset bitfield.
1211 * Otherwise, we can use an implied move from g0 to the first message reg.
1212 */
1213 struct brw_reg src = brw_null_reg();
1214 if (inst->header_size != 0) {
1215 if (devinfo->gen < 6 && !inst->offset) {
1216 /* Set up an implied move from g0 to the MRF. */
1217 src = retype(brw_vec8_grf(0, 0), BRW_REGISTER_TYPE_UW);
1218 } else {
1219 const tgl_swsb swsb = brw_get_default_swsb(p);
1220 assert(inst->base_mrf != -1);
1221 struct brw_reg header_reg = brw_message_reg(inst->base_mrf);
1222
1223 brw_push_insn_state(p);
1224 brw_set_default_swsb(p, tgl_swsb_src_dep(swsb));
1225 brw_set_default_exec_size(p, BRW_EXECUTE_8);
1226 brw_set_default_mask_control(p, BRW_MASK_DISABLE);
1227 brw_set_default_compression_control(p, BRW_COMPRESSION_NONE);
1228 /* Explicitly set up the message header by copying g0 to the MRF. */
1229 brw_MOV(p, header_reg, brw_vec8_grf(0, 0));
1230 brw_set_default_swsb(p, tgl_swsb_regdist(1));
1231
1232 brw_set_default_exec_size(p, BRW_EXECUTE_1);
1233 if (inst->offset) {
1234 /* Set the offset bits in DWord 2. */
1235 brw_MOV(p, get_element_ud(header_reg, 2),
1236 brw_imm_ud(inst->offset));
1237 }
1238
1239 brw_pop_insn_state(p);
1240 brw_set_default_swsb(p, tgl_swsb_dst_dep(swsb, 1));
1241 }
1242 }
1243
1244 uint32_t base_binding_table_index;
1245 switch (inst->opcode) {
1246 case SHADER_OPCODE_TG4:
1247 base_binding_table_index = prog_data->binding_table.gather_texture_start;
1248 break;
1249 default:
1250 base_binding_table_index = prog_data->binding_table.texture_start;
1251 break;
1252 }
1253
1254 assert(surface_index.file == BRW_IMMEDIATE_VALUE);
1255 assert(sampler_index.file == BRW_IMMEDIATE_VALUE);
1256
1257 brw_SAMPLE(p,
1258 retype(dst, BRW_REGISTER_TYPE_UW),
1259 inst->base_mrf,
1260 src,
1261 surface_index.ud + base_binding_table_index,
1262 sampler_index.ud % 16,
1263 msg_type,
1264 inst->size_written / REG_SIZE,
1265 inst->mlen,
1266 inst->header_size != 0,
1267 simd_mode,
1268 return_format);
1269 }
1270
1271
1272 /* For OPCODE_DDX and OPCODE_DDY, per channel of output we've got input
1273 * looking like:
1274 *
1275 * arg0: ss0.tl ss0.tr ss0.bl ss0.br ss1.tl ss1.tr ss1.bl ss1.br
1276 *
1277 * Ideally, we want to produce:
1278 *
1279 * DDX DDY
1280 * dst: (ss0.tr - ss0.tl) (ss0.tl - ss0.bl)
1281 * (ss0.tr - ss0.tl) (ss0.tr - ss0.br)
1282 * (ss0.br - ss0.bl) (ss0.tl - ss0.bl)
1283 * (ss0.br - ss0.bl) (ss0.tr - ss0.br)
1284 * (ss1.tr - ss1.tl) (ss1.tl - ss1.bl)
1285 * (ss1.tr - ss1.tl) (ss1.tr - ss1.br)
1286 * (ss1.br - ss1.bl) (ss1.tl - ss1.bl)
1287 * (ss1.br - ss1.bl) (ss1.tr - ss1.br)
1288 *
1289 * and add another set of two more subspans if in 16-pixel dispatch mode.
1290 *
1291 * For DDX, it ends up being easy: width = 2, horiz=0 gets us the same result
1292 * for each pair, and vertstride = 2 jumps us 2 elements after processing a
1293 * pair. But the ideal approximation may impose a huge performance cost on
1294 * sample_d. On at least Haswell, sample_d instruction does some
1295 * optimizations if the same LOD is used for all pixels in the subspan.
1296 *
1297 * For DDY, we need to use ALIGN16 mode since it's capable of doing the
1298 * appropriate swizzling.
1299 */
1300 void
1301 fs_generator::generate_ddx(const fs_inst *inst,
1302 struct brw_reg dst, struct brw_reg src)
1303 {
1304 unsigned vstride, width;
1305
1306 if (devinfo->gen >= 8) {
1307 if (inst->opcode == FS_OPCODE_DDX_FINE) {
1308 /* produce accurate derivatives */
1309 vstride = BRW_VERTICAL_STRIDE_2;
1310 width = BRW_WIDTH_2;
1311 } else {
1312 /* replicate the derivative at the top-left pixel to other pixels */
1313 vstride = BRW_VERTICAL_STRIDE_4;
1314 width = BRW_WIDTH_4;
1315 }
1316
1317 struct brw_reg src0 = byte_offset(src, type_sz(src.type));;
1318 struct brw_reg src1 = src;
1319
1320 src0.vstride = vstride;
1321 src0.width = width;
1322 src0.hstride = BRW_HORIZONTAL_STRIDE_0;
1323 src1.vstride = vstride;
1324 src1.width = width;
1325 src1.hstride = BRW_HORIZONTAL_STRIDE_0;
1326
1327 brw_ADD(p, dst, src0, negate(src1));
1328 } else {
1329 /* On Haswell and earlier, the region used above appears to not work
1330 * correctly for compressed instructions. At least on Haswell and
1331 * Iron Lake, compressed ALIGN16 instructions do work. Since we
1332 * would have to split to SIMD8 no matter which method we choose, we
1333 * may as well use ALIGN16 on all platforms gen7 and earlier.
1334 */
1335 struct brw_reg src0 = stride(src, 4, 4, 1);
1336 struct brw_reg src1 = stride(src, 4, 4, 1);
1337 if (inst->opcode == FS_OPCODE_DDX_FINE) {
1338 src0.swizzle = BRW_SWIZZLE_XXZZ;
1339 src1.swizzle = BRW_SWIZZLE_YYWW;
1340 } else {
1341 src0.swizzle = BRW_SWIZZLE_XXXX;
1342 src1.swizzle = BRW_SWIZZLE_YYYY;
1343 }
1344
1345 brw_push_insn_state(p);
1346 brw_set_default_access_mode(p, BRW_ALIGN_16);
1347 brw_ADD(p, dst, negate(src0), src1);
1348 brw_pop_insn_state(p);
1349 }
1350 }
1351
1352 /* The negate_value boolean is used to negate the derivative computation for
1353 * FBOs, since they place the origin at the upper left instead of the lower
1354 * left.
1355 */
1356 void
1357 fs_generator::generate_ddy(const fs_inst *inst,
1358 struct brw_reg dst, struct brw_reg src)
1359 {
1360 const uint32_t type_size = type_sz(src.type);
1361
1362 if (inst->opcode == FS_OPCODE_DDY_FINE) {
1363 /* produce accurate derivatives.
1364 *
1365 * From the Broadwell PRM, Volume 7 (3D-Media-GPGPU)
1366 * "Register Region Restrictions", Section "1. Special Restrictions":
1367 *
1368 * "In Align16 mode, the channel selects and channel enables apply to
1369 * a pair of half-floats, because these parameters are defined for
1370 * DWord elements ONLY. This is applicable when both source and
1371 * destination are half-floats."
1372 *
1373 * So for half-float operations we use the Gen11+ Align1 path. CHV
1374 * inherits its FP16 hardware from SKL, so it is not affected.
1375 */
1376 if (devinfo->gen >= 11 ||
1377 (devinfo->is_broadwell && src.type == BRW_REGISTER_TYPE_HF)) {
1378 src = stride(src, 0, 2, 1);
1379
1380 brw_push_insn_state(p);
1381 brw_set_default_exec_size(p, BRW_EXECUTE_4);
1382 for (uint32_t g = 0; g < inst->exec_size; g += 4) {
1383 brw_set_default_group(p, inst->group + g);
1384 brw_ADD(p, byte_offset(dst, g * type_size),
1385 negate(byte_offset(src, g * type_size)),
1386 byte_offset(src, (g + 2) * type_size));
1387 brw_set_default_swsb(p, tgl_swsb_null());
1388 }
1389 brw_pop_insn_state(p);
1390 } else {
1391 struct brw_reg src0 = stride(src, 4, 4, 1);
1392 struct brw_reg src1 = stride(src, 4, 4, 1);
1393 src0.swizzle = BRW_SWIZZLE_XYXY;
1394 src1.swizzle = BRW_SWIZZLE_ZWZW;
1395
1396 brw_push_insn_state(p);
1397 brw_set_default_access_mode(p, BRW_ALIGN_16);
1398 brw_ADD(p, dst, negate(src0), src1);
1399 brw_pop_insn_state(p);
1400 }
1401 } else {
1402 /* replicate the derivative at the top-left pixel to other pixels */
1403 if (devinfo->gen >= 8) {
1404 struct brw_reg src0 = byte_offset(stride(src, 4, 4, 0), 0 * type_size);
1405 struct brw_reg src1 = byte_offset(stride(src, 4, 4, 0), 2 * type_size);
1406
1407 brw_ADD(p, dst, negate(src0), src1);
1408 } else {
1409 /* On Haswell and earlier, the region used above appears to not work
1410 * correctly for compressed instructions. At least on Haswell and
1411 * Iron Lake, compressed ALIGN16 instructions do work. Since we
1412 * would have to split to SIMD8 no matter which method we choose, we
1413 * may as well use ALIGN16 on all platforms gen7 and earlier.
1414 */
1415 struct brw_reg src0 = stride(src, 4, 4, 1);
1416 struct brw_reg src1 = stride(src, 4, 4, 1);
1417 src0.swizzle = BRW_SWIZZLE_XXXX;
1418 src1.swizzle = BRW_SWIZZLE_ZZZZ;
1419
1420 brw_push_insn_state(p);
1421 brw_set_default_access_mode(p, BRW_ALIGN_16);
1422 brw_ADD(p, dst, negate(src0), src1);
1423 brw_pop_insn_state(p);
1424 }
1425 }
1426 }
1427
1428 void
1429 fs_generator::generate_discard_jump(fs_inst *)
1430 {
1431 /* This HALT will be patched up at FB write time to point UIP at the end of
1432 * the program, and at brw_uip_jip() JIP will be set to the end of the
1433 * current block (or the program).
1434 */
1435 this->discard_halt_patches.push_tail(new(mem_ctx) ip_record(p->nr_insn));
1436 brw_HALT(p);
1437 }
1438
1439 void
1440 fs_generator::generate_scratch_write(fs_inst *inst, struct brw_reg src)
1441 {
1442 /* The 32-wide messages only respect the first 16-wide half of the channel
1443 * enable signals which are replicated identically for the second group of
1444 * 16 channels, so we cannot use them unless the write is marked
1445 * force_writemask_all.
1446 */
1447 const unsigned lower_size = inst->force_writemask_all ? inst->exec_size :
1448 MIN2(16, inst->exec_size);
1449 const unsigned block_size = 4 * lower_size / REG_SIZE;
1450 const tgl_swsb swsb = brw_get_default_swsb(p);
1451 assert(inst->mlen != 0);
1452
1453 brw_push_insn_state(p);
1454 brw_set_default_exec_size(p, cvt(lower_size) - 1);
1455 brw_set_default_compression(p, lower_size > 8);
1456
1457 for (unsigned i = 0; i < inst->exec_size / lower_size; i++) {
1458 brw_set_default_group(p, inst->group + lower_size * i);
1459
1460 if (i > 0) {
1461 assert(swsb.mode & TGL_SBID_SET);
1462 brw_set_default_swsb(p, tgl_swsb_sbid(TGL_SBID_SRC, swsb.sbid));
1463 } else {
1464 brw_set_default_swsb(p, tgl_swsb_src_dep(swsb));
1465 }
1466
1467 brw_MOV(p, brw_uvec_mrf(lower_size, inst->base_mrf + 1, 0),
1468 retype(offset(src, block_size * i), BRW_REGISTER_TYPE_UD));
1469
1470 brw_set_default_swsb(p, tgl_swsb_dst_dep(swsb, 1));
1471 brw_oword_block_write_scratch(p, brw_message_reg(inst->base_mrf),
1472 block_size,
1473 inst->offset + block_size * REG_SIZE * i);
1474 }
1475
1476 brw_pop_insn_state(p);
1477 }
1478
1479 void
1480 fs_generator::generate_scratch_read(fs_inst *inst, struct brw_reg dst)
1481 {
1482 assert(inst->exec_size <= 16 || inst->force_writemask_all);
1483 assert(inst->mlen != 0);
1484
1485 brw_oword_block_read_scratch(p, dst, brw_message_reg(inst->base_mrf),
1486 inst->exec_size / 8, inst->offset);
1487 }
1488
1489 void
1490 fs_generator::generate_scratch_read_gen7(fs_inst *inst, struct brw_reg dst)
1491 {
1492 assert(inst->exec_size <= 16 || inst->force_writemask_all);
1493
1494 gen7_block_read_scratch(p, dst, inst->exec_size / 8, inst->offset);
1495 }
1496
1497 void
1498 fs_generator::generate_uniform_pull_constant_load(fs_inst *inst,
1499 struct brw_reg dst,
1500 struct brw_reg index,
1501 struct brw_reg offset)
1502 {
1503 assert(type_sz(dst.type) == 4);
1504 assert(inst->mlen != 0);
1505
1506 assert(index.file == BRW_IMMEDIATE_VALUE &&
1507 index.type == BRW_REGISTER_TYPE_UD);
1508 uint32_t surf_index = index.ud;
1509
1510 assert(offset.file == BRW_IMMEDIATE_VALUE &&
1511 offset.type == BRW_REGISTER_TYPE_UD);
1512 uint32_t read_offset = offset.ud;
1513
1514 brw_oword_block_read(p, dst, brw_message_reg(inst->base_mrf),
1515 read_offset, surf_index);
1516 }
1517
1518 void
1519 fs_generator::generate_uniform_pull_constant_load_gen7(fs_inst *inst,
1520 struct brw_reg dst,
1521 struct brw_reg index,
1522 struct brw_reg payload)
1523 {
1524 assert(index.type == BRW_REGISTER_TYPE_UD);
1525 assert(payload.file == BRW_GENERAL_REGISTER_FILE);
1526 assert(type_sz(dst.type) == 4);
1527
1528 if (index.file == BRW_IMMEDIATE_VALUE) {
1529 const uint32_t surf_index = index.ud;
1530
1531 brw_push_insn_state(p);
1532 brw_set_default_mask_control(p, BRW_MASK_DISABLE);
1533 brw_inst *send = brw_next_insn(p, BRW_OPCODE_SEND);
1534 brw_pop_insn_state(p);
1535
1536 brw_inst_set_sfid(devinfo, send, GEN6_SFID_DATAPORT_CONSTANT_CACHE);
1537 brw_set_dest(p, send, retype(dst, BRW_REGISTER_TYPE_UD));
1538 brw_set_src0(p, send, retype(payload, BRW_REGISTER_TYPE_UD));
1539 brw_set_desc(p, send,
1540 brw_message_desc(devinfo, 1, DIV_ROUND_UP(inst->size_written,
1541 REG_SIZE), true) |
1542 brw_dp_read_desc(devinfo, surf_index,
1543 BRW_DATAPORT_OWORD_BLOCK_DWORDS(inst->exec_size),
1544 GEN7_DATAPORT_DC_OWORD_BLOCK_READ,
1545 BRW_DATAPORT_READ_TARGET_DATA_CACHE));
1546
1547 } else {
1548 const tgl_swsb swsb = brw_get_default_swsb(p);
1549 struct brw_reg addr = vec1(retype(brw_address_reg(0), BRW_REGISTER_TYPE_UD));
1550
1551 brw_push_insn_state(p);
1552 brw_set_default_mask_control(p, BRW_MASK_DISABLE);
1553
1554 /* a0.0 = surf_index & 0xff */
1555 brw_set_default_swsb(p, tgl_swsb_src_dep(swsb));
1556 brw_inst *insn_and = brw_next_insn(p, BRW_OPCODE_AND);
1557 brw_inst_set_exec_size(p->devinfo, insn_and, BRW_EXECUTE_1);
1558 brw_set_dest(p, insn_and, addr);
1559 brw_set_src0(p, insn_and, vec1(retype(index, BRW_REGISTER_TYPE_UD)));
1560 brw_set_src1(p, insn_and, brw_imm_ud(0x0ff));
1561
1562 /* dst = send(payload, a0.0 | <descriptor>) */
1563 brw_set_default_swsb(p, tgl_swsb_dst_dep(swsb, 1));
1564 brw_send_indirect_message(
1565 p, GEN6_SFID_DATAPORT_CONSTANT_CACHE,
1566 retype(dst, BRW_REGISTER_TYPE_UD),
1567 retype(payload, BRW_REGISTER_TYPE_UD), addr,
1568 brw_message_desc(devinfo, 1,
1569 DIV_ROUND_UP(inst->size_written, REG_SIZE), true) |
1570 brw_dp_read_desc(devinfo, 0 /* surface */,
1571 BRW_DATAPORT_OWORD_BLOCK_DWORDS(inst->exec_size),
1572 GEN7_DATAPORT_DC_OWORD_BLOCK_READ,
1573 BRW_DATAPORT_READ_TARGET_DATA_CACHE),
1574 false /* EOT */);
1575
1576 brw_pop_insn_state(p);
1577 }
1578 }
1579
1580 void
1581 fs_generator::generate_varying_pull_constant_load_gen4(fs_inst *inst,
1582 struct brw_reg dst,
1583 struct brw_reg index)
1584 {
1585 assert(devinfo->gen < 7); /* Should use the gen7 variant. */
1586 assert(inst->header_size != 0);
1587 assert(inst->mlen);
1588
1589 assert(index.file == BRW_IMMEDIATE_VALUE &&
1590 index.type == BRW_REGISTER_TYPE_UD);
1591 uint32_t surf_index = index.ud;
1592
1593 uint32_t simd_mode, rlen, msg_type;
1594 if (inst->exec_size == 16) {
1595 simd_mode = BRW_SAMPLER_SIMD_MODE_SIMD16;
1596 rlen = 8;
1597 } else {
1598 assert(inst->exec_size == 8);
1599 simd_mode = BRW_SAMPLER_SIMD_MODE_SIMD8;
1600 rlen = 4;
1601 }
1602
1603 if (devinfo->gen >= 5)
1604 msg_type = GEN5_SAMPLER_MESSAGE_SAMPLE_LD;
1605 else {
1606 /* We always use the SIMD16 message so that we only have to load U, and
1607 * not V or R.
1608 */
1609 msg_type = BRW_SAMPLER_MESSAGE_SIMD16_LD;
1610 assert(inst->mlen == 3);
1611 assert(inst->size_written == 8 * REG_SIZE);
1612 rlen = 8;
1613 simd_mode = BRW_SAMPLER_SIMD_MODE_SIMD16;
1614 }
1615
1616 struct brw_reg header = brw_vec8_grf(0, 0);
1617 gen6_resolve_implied_move(p, &header, inst->base_mrf);
1618
1619 brw_inst *send = brw_next_insn(p, BRW_OPCODE_SEND);
1620 brw_inst_set_compression(devinfo, send, false);
1621 brw_inst_set_sfid(devinfo, send, BRW_SFID_SAMPLER);
1622 brw_set_dest(p, send, retype(dst, BRW_REGISTER_TYPE_UW));
1623 brw_set_src0(p, send, header);
1624 if (devinfo->gen < 6)
1625 brw_inst_set_base_mrf(p->devinfo, send, inst->base_mrf);
1626
1627 /* Our surface is set up as floats, regardless of what actual data is
1628 * stored in it.
1629 */
1630 uint32_t return_format = BRW_SAMPLER_RETURN_FORMAT_FLOAT32;
1631 brw_set_desc(p, send,
1632 brw_message_desc(devinfo, inst->mlen, rlen, inst->header_size) |
1633 brw_sampler_desc(devinfo, surf_index,
1634 0, /* sampler (unused) */
1635 msg_type, simd_mode, return_format));
1636 }
1637
1638 void
1639 fs_generator::generate_pixel_interpolator_query(fs_inst *inst,
1640 struct brw_reg dst,
1641 struct brw_reg src,
1642 struct brw_reg msg_data,
1643 unsigned msg_type)
1644 {
1645 const bool has_payload = inst->src[0].file != BAD_FILE;
1646 assert(msg_data.type == BRW_REGISTER_TYPE_UD);
1647 assert(inst->size_written % REG_SIZE == 0);
1648
1649 brw_pixel_interpolator_query(p,
1650 retype(dst, BRW_REGISTER_TYPE_UW),
1651 /* If we don't have a payload, what we send doesn't matter */
1652 has_payload ? src : brw_vec8_grf(0, 0),
1653 inst->pi_noperspective,
1654 msg_type,
1655 msg_data,
1656 has_payload ? 2 * inst->exec_size / 8 : 1,
1657 inst->size_written / REG_SIZE);
1658 }
1659
1660 /* Sets vstride=1, width=4, hstride=0 of register src1 during
1661 * the ADD instruction.
1662 */
1663 void
1664 fs_generator::generate_set_sample_id(fs_inst *inst,
1665 struct brw_reg dst,
1666 struct brw_reg src0,
1667 struct brw_reg src1)
1668 {
1669 assert(dst.type == BRW_REGISTER_TYPE_D ||
1670 dst.type == BRW_REGISTER_TYPE_UD);
1671 assert(src0.type == BRW_REGISTER_TYPE_D ||
1672 src0.type == BRW_REGISTER_TYPE_UD);
1673
1674 const struct brw_reg reg = stride(src1, 1, 4, 0);
1675 const unsigned lower_size = MIN2(inst->exec_size,
1676 devinfo->gen >= 8 ? 16 : 8);
1677
1678 for (unsigned i = 0; i < inst->exec_size / lower_size; i++) {
1679 brw_inst *insn = brw_ADD(p, offset(dst, i * lower_size / 8),
1680 offset(src0, (src0.vstride == 0 ? 0 : (1 << (src0.vstride - 1)) *
1681 (i * lower_size / (1 << src0.width))) *
1682 type_sz(src0.type) / REG_SIZE),
1683 suboffset(reg, i * lower_size / 4));
1684 brw_inst_set_exec_size(devinfo, insn, cvt(lower_size) - 1);
1685 brw_inst_set_group(devinfo, insn, inst->group + lower_size * i);
1686 brw_inst_set_compression(devinfo, insn, lower_size > 8);
1687 brw_set_default_swsb(p, tgl_swsb_null());
1688 }
1689 }
1690
1691 void
1692 fs_generator::generate_pack_half_2x16_split(fs_inst *,
1693 struct brw_reg dst,
1694 struct brw_reg x,
1695 struct brw_reg y)
1696 {
1697 assert(devinfo->gen >= 7);
1698 assert(dst.type == BRW_REGISTER_TYPE_UD);
1699 assert(x.type == BRW_REGISTER_TYPE_F);
1700 assert(y.type == BRW_REGISTER_TYPE_F);
1701
1702 /* From the Ivybridge PRM, Vol4, Part3, Section 6.27 f32to16:
1703 *
1704 * Because this instruction does not have a 16-bit floating-point type,
1705 * the destination data type must be Word (W).
1706 *
1707 * The destination must be DWord-aligned and specify a horizontal stride
1708 * (HorzStride) of 2. The 16-bit result is stored in the lower word of
1709 * each destination channel and the upper word is not modified.
1710 */
1711 struct brw_reg dst_w = spread(retype(dst, BRW_REGISTER_TYPE_W), 2);
1712
1713 /* Give each 32-bit channel of dst the form below, where "." means
1714 * unchanged.
1715 * 0x....hhhh
1716 */
1717 brw_F32TO16(p, dst_w, y);
1718
1719 /* Now the form:
1720 * 0xhhhh0000
1721 */
1722 brw_set_default_swsb(p, tgl_swsb_regdist(1));
1723 brw_SHL(p, dst, dst, brw_imm_ud(16u));
1724
1725 /* And, finally the form of packHalf2x16's output:
1726 * 0xhhhhllll
1727 */
1728 brw_F32TO16(p, dst_w, x);
1729 }
1730
1731 void
1732 fs_generator::generate_shader_time_add(fs_inst *,
1733 struct brw_reg payload,
1734 struct brw_reg offset,
1735 struct brw_reg value)
1736 {
1737 const tgl_swsb swsb = brw_get_default_swsb(p);
1738
1739 assert(devinfo->gen >= 7);
1740 brw_push_insn_state(p);
1741 brw_set_default_mask_control(p, true);
1742 brw_set_default_swsb(p, tgl_swsb_src_dep(swsb));
1743
1744 assert(payload.file == BRW_GENERAL_REGISTER_FILE);
1745 struct brw_reg payload_offset = retype(brw_vec1_grf(payload.nr, 0),
1746 offset.type);
1747 struct brw_reg payload_value = retype(brw_vec1_grf(payload.nr + 1, 0),
1748 value.type);
1749
1750 assert(offset.file == BRW_IMMEDIATE_VALUE);
1751 if (value.file == BRW_GENERAL_REGISTER_FILE) {
1752 value.width = BRW_WIDTH_1;
1753 value.hstride = BRW_HORIZONTAL_STRIDE_0;
1754 value.vstride = BRW_VERTICAL_STRIDE_0;
1755 } else {
1756 assert(value.file == BRW_IMMEDIATE_VALUE);
1757 }
1758
1759 /* Trying to deal with setup of the params from the IR is crazy in the FS8
1760 * case, and we don't really care about squeezing every bit of performance
1761 * out of this path, so we just emit the MOVs from here.
1762 */
1763 brw_MOV(p, payload_offset, offset);
1764 brw_set_default_swsb(p, tgl_swsb_null());
1765 brw_MOV(p, payload_value, value);
1766 brw_set_default_swsb(p, tgl_swsb_dst_dep(swsb, 1));
1767 brw_shader_time_add(p, payload,
1768 prog_data->binding_table.shader_time_start);
1769 brw_pop_insn_state(p);
1770 }
1771
1772 void
1773 fs_generator::enable_debug(const char *shader_name)
1774 {
1775 debug_flag = true;
1776 this->shader_name = shader_name;
1777 }
1778
1779 int
1780 fs_generator::generate_code(const cfg_t *cfg, int dispatch_width,
1781 struct shader_stats shader_stats,
1782 const brw::performance &perf,
1783 struct brw_compile_stats *stats)
1784 {
1785 /* align to 64 byte boundary. */
1786 brw_realign(p, 64);
1787
1788 this->dispatch_width = dispatch_width;
1789
1790 int start_offset = p->next_insn_offset;
1791
1792 /* `send_count` explicitly does not include spills or fills, as we'd
1793 * like to use it as a metric for intentional memory access or other
1794 * shared function use. Otherwise, subtle changes to scheduling or
1795 * register allocation could cause it to fluctuate wildly - and that
1796 * effect is already counted in spill/fill counts.
1797 */
1798 int spill_count = 0, fill_count = 0;
1799 int loop_count = 0, send_count = 0, nop_count = 0;
1800 bool is_accum_used = false;
1801
1802 struct disasm_info *disasm_info = disasm_initialize(devinfo, cfg);
1803
1804 foreach_block_and_inst (block, fs_inst, inst, cfg) {
1805 if (inst->opcode == SHADER_OPCODE_UNDEF)
1806 continue;
1807
1808 struct brw_reg src[4], dst;
1809 unsigned int last_insn_offset = p->next_insn_offset;
1810 bool multiple_instructions_emitted = false;
1811
1812 /* From the Broadwell PRM, Volume 7, "3D-Media-GPGPU", in the
1813 * "Register Region Restrictions" section: for BDW, SKL:
1814 *
1815 * "A POW/FDIV operation must not be followed by an instruction
1816 * that requires two destination registers."
1817 *
1818 * The documentation is often lacking annotations for Atom parts,
1819 * and empirically this affects CHV as well.
1820 */
1821 if (devinfo->gen >= 8 &&
1822 devinfo->gen <= 9 &&
1823 p->nr_insn > 1 &&
1824 brw_inst_opcode(devinfo, brw_last_inst) == BRW_OPCODE_MATH &&
1825 brw_inst_math_function(devinfo, brw_last_inst) == BRW_MATH_FUNCTION_POW &&
1826 inst->dst.component_size(inst->exec_size) > REG_SIZE) {
1827 brw_NOP(p);
1828 last_insn_offset = p->next_insn_offset;
1829
1830 /* In order to avoid spurious instruction count differences when the
1831 * instruction schedule changes, keep track of the number of inserted
1832 * NOPs.
1833 */
1834 nop_count++;
1835 }
1836
1837 /* GEN:BUG:14010017096:
1838 *
1839 * Clear accumulator register before end of thread.
1840 */
1841 if (inst->eot && is_accum_used && devinfo->gen >= 12) {
1842 brw_set_default_exec_size(p, BRW_EXECUTE_16);
1843 brw_set_default_mask_control(p, BRW_MASK_DISABLE);
1844 brw_set_default_predicate_control(p, BRW_PREDICATE_NONE);
1845 brw_MOV(p, brw_acc_reg(8), brw_imm_f(0.0f));
1846 last_insn_offset = p->next_insn_offset;
1847 }
1848
1849 if (!is_accum_used && !inst->eot) {
1850 is_accum_used = inst->writes_accumulator_implicitly(devinfo) ||
1851 inst->dst.is_accumulator();
1852 }
1853
1854 if (unlikely(debug_flag))
1855 disasm_annotate(disasm_info, inst, p->next_insn_offset);
1856
1857 /* If the instruction writes to more than one register, it needs to be
1858 * explicitly marked as compressed on Gen <= 5. On Gen >= 6 the
1859 * hardware figures out by itself what the right compression mode is,
1860 * but we still need to know whether the instruction is compressed to
1861 * set up the source register regions appropriately.
1862 *
1863 * XXX - This is wrong for instructions that write a single register but
1864 * read more than one which should strictly speaking be treated as
1865 * compressed. For instructions that don't write any registers it
1866 * relies on the destination being a null register of the correct
1867 * type and regioning so the instruction is considered compressed
1868 * or not accordingly.
1869 */
1870 const bool compressed =
1871 inst->dst.component_size(inst->exec_size) > REG_SIZE;
1872 brw_set_default_compression(p, compressed);
1873 brw_set_default_group(p, inst->group);
1874
1875 for (unsigned int i = 0; i < inst->sources; i++) {
1876 src[i] = brw_reg_from_fs_reg(devinfo, inst,
1877 &inst->src[i], compressed);
1878 /* The accumulator result appears to get used for the
1879 * conditional modifier generation. When negating a UD
1880 * value, there is a 33rd bit generated for the sign in the
1881 * accumulator value, so now you can't check, for example,
1882 * equality with a 32-bit value. See piglit fs-op-neg-uvec4.
1883 */
1884 assert(!inst->conditional_mod ||
1885 inst->src[i].type != BRW_REGISTER_TYPE_UD ||
1886 !inst->src[i].negate);
1887 }
1888 dst = brw_reg_from_fs_reg(devinfo, inst,
1889 &inst->dst, compressed);
1890
1891 brw_set_default_access_mode(p, BRW_ALIGN_1);
1892 brw_set_default_predicate_control(p, inst->predicate);
1893 brw_set_default_predicate_inverse(p, inst->predicate_inverse);
1894 /* On gen7 and above, hardware automatically adds the group onto the
1895 * flag subregister number. On Sandy Bridge and older, we have to do it
1896 * ourselves.
1897 */
1898 const unsigned flag_subreg = inst->flag_subreg +
1899 (devinfo->gen >= 7 ? 0 : inst->group / 16);
1900 brw_set_default_flag_reg(p, flag_subreg / 2, flag_subreg % 2);
1901 brw_set_default_saturate(p, inst->saturate);
1902 brw_set_default_mask_control(p, inst->force_writemask_all);
1903 brw_set_default_acc_write_control(p, inst->writes_accumulator);
1904 brw_set_default_swsb(p, inst->sched);
1905
1906 unsigned exec_size = inst->exec_size;
1907 if (devinfo->gen == 7 && !devinfo->is_haswell &&
1908 (get_exec_type_size(inst) == 8 || type_sz(inst->dst.type) == 8)) {
1909 exec_size *= 2;
1910 }
1911
1912 brw_set_default_exec_size(p, cvt(exec_size) - 1);
1913
1914 assert(inst->force_writemask_all || inst->exec_size >= 4);
1915 assert(inst->force_writemask_all || inst->group % inst->exec_size == 0);
1916 assert(inst->base_mrf + inst->mlen <= BRW_MAX_MRF(devinfo->gen));
1917 assert(inst->mlen <= BRW_MAX_MSG_LENGTH);
1918
1919 switch (inst->opcode) {
1920 case BRW_OPCODE_SYNC:
1921 assert(src[0].file == BRW_IMMEDIATE_VALUE);
1922 brw_SYNC(p, tgl_sync_function(src[0].ud));
1923 break;
1924 case BRW_OPCODE_MOV:
1925 brw_MOV(p, dst, src[0]);
1926 break;
1927 case BRW_OPCODE_ADD:
1928 brw_ADD(p, dst, src[0], src[1]);
1929 break;
1930 case BRW_OPCODE_MUL:
1931 brw_MUL(p, dst, src[0], src[1]);
1932 break;
1933 case BRW_OPCODE_AVG:
1934 brw_AVG(p, dst, src[0], src[1]);
1935 break;
1936 case BRW_OPCODE_MACH:
1937 brw_MACH(p, dst, src[0], src[1]);
1938 break;
1939
1940 case BRW_OPCODE_LINE:
1941 brw_LINE(p, dst, src[0], src[1]);
1942 break;
1943
1944 case BRW_OPCODE_MAD:
1945 assert(devinfo->gen >= 6);
1946 if (devinfo->gen < 10)
1947 brw_set_default_access_mode(p, BRW_ALIGN_16);
1948 brw_MAD(p, dst, src[0], src[1], src[2]);
1949 break;
1950
1951 case BRW_OPCODE_LRP:
1952 assert(devinfo->gen >= 6 && devinfo->gen <= 10);
1953 if (devinfo->gen < 10)
1954 brw_set_default_access_mode(p, BRW_ALIGN_16);
1955 brw_LRP(p, dst, src[0], src[1], src[2]);
1956 break;
1957
1958 case BRW_OPCODE_FRC:
1959 brw_FRC(p, dst, src[0]);
1960 break;
1961 case BRW_OPCODE_RNDD:
1962 brw_RNDD(p, dst, src[0]);
1963 break;
1964 case BRW_OPCODE_RNDE:
1965 brw_RNDE(p, dst, src[0]);
1966 break;
1967 case BRW_OPCODE_RNDZ:
1968 brw_RNDZ(p, dst, src[0]);
1969 break;
1970
1971 case BRW_OPCODE_AND:
1972 brw_AND(p, dst, src[0], src[1]);
1973 break;
1974 case BRW_OPCODE_OR:
1975 brw_OR(p, dst, src[0], src[1]);
1976 break;
1977 case BRW_OPCODE_XOR:
1978 brw_XOR(p, dst, src[0], src[1]);
1979 break;
1980 case BRW_OPCODE_NOT:
1981 brw_NOT(p, dst, src[0]);
1982 break;
1983 case BRW_OPCODE_ASR:
1984 brw_ASR(p, dst, src[0], src[1]);
1985 break;
1986 case BRW_OPCODE_SHR:
1987 brw_SHR(p, dst, src[0], src[1]);
1988 break;
1989 case BRW_OPCODE_SHL:
1990 brw_SHL(p, dst, src[0], src[1]);
1991 break;
1992 case BRW_OPCODE_ROL:
1993 assert(devinfo->gen >= 11);
1994 assert(src[0].type == dst.type);
1995 brw_ROL(p, dst, src[0], src[1]);
1996 break;
1997 case BRW_OPCODE_ROR:
1998 assert(devinfo->gen >= 11);
1999 assert(src[0].type == dst.type);
2000 brw_ROR(p, dst, src[0], src[1]);
2001 break;
2002 case BRW_OPCODE_F32TO16:
2003 assert(devinfo->gen >= 7);
2004 brw_F32TO16(p, dst, src[0]);
2005 break;
2006 case BRW_OPCODE_F16TO32:
2007 assert(devinfo->gen >= 7);
2008 brw_F16TO32(p, dst, src[0]);
2009 break;
2010 case BRW_OPCODE_CMP:
2011 if (inst->exec_size >= 16 && devinfo->gen == 7 && !devinfo->is_haswell &&
2012 dst.file == BRW_ARCHITECTURE_REGISTER_FILE) {
2013 /* For unknown reasons the WaCMPInstFlagDepClearedEarly workaround
2014 * implemented in the compiler is not sufficient. Overriding the
2015 * type when the destination is the null register is necessary but
2016 * not sufficient by itself.
2017 */
2018 assert(dst.nr == BRW_ARF_NULL);
2019 dst.type = BRW_REGISTER_TYPE_D;
2020 }
2021 brw_CMP(p, dst, inst->conditional_mod, src[0], src[1]);
2022 break;
2023 case BRW_OPCODE_SEL:
2024 brw_SEL(p, dst, src[0], src[1]);
2025 break;
2026 case BRW_OPCODE_CSEL:
2027 assert(devinfo->gen >= 8);
2028 if (devinfo->gen < 10)
2029 brw_set_default_access_mode(p, BRW_ALIGN_16);
2030 brw_CSEL(p, dst, src[0], src[1], src[2]);
2031 break;
2032 case BRW_OPCODE_BFREV:
2033 assert(devinfo->gen >= 7);
2034 brw_BFREV(p, retype(dst, BRW_REGISTER_TYPE_UD),
2035 retype(src[0], BRW_REGISTER_TYPE_UD));
2036 break;
2037 case BRW_OPCODE_FBH:
2038 assert(devinfo->gen >= 7);
2039 brw_FBH(p, retype(dst, src[0].type), src[0]);
2040 break;
2041 case BRW_OPCODE_FBL:
2042 assert(devinfo->gen >= 7);
2043 brw_FBL(p, retype(dst, BRW_REGISTER_TYPE_UD),
2044 retype(src[0], BRW_REGISTER_TYPE_UD));
2045 break;
2046 case BRW_OPCODE_LZD:
2047 brw_LZD(p, dst, src[0]);
2048 break;
2049 case BRW_OPCODE_CBIT:
2050 assert(devinfo->gen >= 7);
2051 brw_CBIT(p, retype(dst, BRW_REGISTER_TYPE_UD),
2052 retype(src[0], BRW_REGISTER_TYPE_UD));
2053 break;
2054 case BRW_OPCODE_ADDC:
2055 assert(devinfo->gen >= 7);
2056 brw_ADDC(p, dst, src[0], src[1]);
2057 break;
2058 case BRW_OPCODE_SUBB:
2059 assert(devinfo->gen >= 7);
2060 brw_SUBB(p, dst, src[0], src[1]);
2061 break;
2062 case BRW_OPCODE_MAC:
2063 brw_MAC(p, dst, src[0], src[1]);
2064 break;
2065
2066 case BRW_OPCODE_BFE:
2067 assert(devinfo->gen >= 7);
2068 if (devinfo->gen < 10)
2069 brw_set_default_access_mode(p, BRW_ALIGN_16);
2070 brw_BFE(p, dst, src[0], src[1], src[2]);
2071 break;
2072
2073 case BRW_OPCODE_BFI1:
2074 assert(devinfo->gen >= 7);
2075 brw_BFI1(p, dst, src[0], src[1]);
2076 break;
2077 case BRW_OPCODE_BFI2:
2078 assert(devinfo->gen >= 7);
2079 if (devinfo->gen < 10)
2080 brw_set_default_access_mode(p, BRW_ALIGN_16);
2081 brw_BFI2(p, dst, src[0], src[1], src[2]);
2082 break;
2083
2084 case BRW_OPCODE_IF:
2085 if (inst->src[0].file != BAD_FILE) {
2086 /* The instruction has an embedded compare (only allowed on gen6) */
2087 assert(devinfo->gen == 6);
2088 gen6_IF(p, inst->conditional_mod, src[0], src[1]);
2089 } else {
2090 brw_IF(p, brw_get_default_exec_size(p));
2091 }
2092 break;
2093
2094 case BRW_OPCODE_ELSE:
2095 brw_ELSE(p);
2096 break;
2097 case BRW_OPCODE_ENDIF:
2098 brw_ENDIF(p);
2099 break;
2100
2101 case BRW_OPCODE_DO:
2102 brw_DO(p, brw_get_default_exec_size(p));
2103 break;
2104
2105 case BRW_OPCODE_BREAK:
2106 brw_BREAK(p);
2107 break;
2108 case BRW_OPCODE_CONTINUE:
2109 brw_CONT(p);
2110 break;
2111
2112 case BRW_OPCODE_WHILE:
2113 brw_WHILE(p);
2114 loop_count++;
2115 break;
2116
2117 case SHADER_OPCODE_RCP:
2118 case SHADER_OPCODE_RSQ:
2119 case SHADER_OPCODE_SQRT:
2120 case SHADER_OPCODE_EXP2:
2121 case SHADER_OPCODE_LOG2:
2122 case SHADER_OPCODE_SIN:
2123 case SHADER_OPCODE_COS:
2124 assert(inst->conditional_mod == BRW_CONDITIONAL_NONE);
2125 if (devinfo->gen >= 6) {
2126 assert(inst->mlen == 0);
2127 assert(devinfo->gen >= 7 || inst->exec_size == 8);
2128 gen6_math(p, dst, brw_math_function(inst->opcode),
2129 src[0], brw_null_reg());
2130 } else {
2131 assert(inst->mlen >= 1);
2132 assert(devinfo->gen == 5 || devinfo->is_g4x || inst->exec_size == 8);
2133 gen4_math(p, dst,
2134 brw_math_function(inst->opcode),
2135 inst->base_mrf, src[0],
2136 BRW_MATH_PRECISION_FULL);
2137 send_count++;
2138 }
2139 break;
2140 case SHADER_OPCODE_INT_QUOTIENT:
2141 case SHADER_OPCODE_INT_REMAINDER:
2142 case SHADER_OPCODE_POW:
2143 assert(inst->conditional_mod == BRW_CONDITIONAL_NONE);
2144 if (devinfo->gen >= 6) {
2145 assert(inst->mlen == 0);
2146 assert((devinfo->gen >= 7 && inst->opcode == SHADER_OPCODE_POW) ||
2147 inst->exec_size == 8);
2148 gen6_math(p, dst, brw_math_function(inst->opcode), src[0], src[1]);
2149 } else {
2150 assert(inst->mlen >= 1);
2151 assert(inst->exec_size == 8);
2152 gen4_math(p, dst, brw_math_function(inst->opcode),
2153 inst->base_mrf, src[0],
2154 BRW_MATH_PRECISION_FULL);
2155 send_count++;
2156 }
2157 break;
2158 case FS_OPCODE_LINTERP:
2159 multiple_instructions_emitted = generate_linterp(inst, dst, src);
2160 break;
2161 case FS_OPCODE_PIXEL_X:
2162 assert(src[0].type == BRW_REGISTER_TYPE_UW);
2163 src[0].subnr = 0 * type_sz(src[0].type);
2164 brw_MOV(p, dst, stride(src[0], 8, 4, 1));
2165 break;
2166 case FS_OPCODE_PIXEL_Y:
2167 assert(src[0].type == BRW_REGISTER_TYPE_UW);
2168 src[0].subnr = 4 * type_sz(src[0].type);
2169 brw_MOV(p, dst, stride(src[0], 8, 4, 1));
2170 break;
2171
2172 case SHADER_OPCODE_SEND:
2173 generate_send(inst, dst, src[0], src[1], src[2],
2174 inst->ex_mlen > 0 ? src[3] : brw_null_reg());
2175 if ((inst->desc & 0xff) == BRW_BTI_STATELESS ||
2176 (inst->desc & 0xff) == GEN8_BTI_STATELESS_NON_COHERENT) {
2177 if (inst->size_written)
2178 fill_count++;
2179 else
2180 spill_count++;
2181 } else {
2182 send_count++;
2183 }
2184 break;
2185
2186 case SHADER_OPCODE_GET_BUFFER_SIZE:
2187 generate_get_buffer_size(inst, dst, src[0], src[1]);
2188 send_count++;
2189 break;
2190 case SHADER_OPCODE_TEX:
2191 case FS_OPCODE_TXB:
2192 case SHADER_OPCODE_TXD:
2193 case SHADER_OPCODE_TXF:
2194 case SHADER_OPCODE_TXF_CMS:
2195 case SHADER_OPCODE_TXL:
2196 case SHADER_OPCODE_TXS:
2197 case SHADER_OPCODE_LOD:
2198 case SHADER_OPCODE_TG4:
2199 case SHADER_OPCODE_SAMPLEINFO:
2200 assert(inst->src[0].file == BAD_FILE);
2201 generate_tex(inst, dst, src[1], src[2]);
2202 send_count++;
2203 break;
2204
2205 case FS_OPCODE_DDX_COARSE:
2206 case FS_OPCODE_DDX_FINE:
2207 generate_ddx(inst, dst, src[0]);
2208 break;
2209 case FS_OPCODE_DDY_COARSE:
2210 case FS_OPCODE_DDY_FINE:
2211 generate_ddy(inst, dst, src[0]);
2212 break;
2213
2214 case SHADER_OPCODE_GEN4_SCRATCH_WRITE:
2215 generate_scratch_write(inst, src[0]);
2216 spill_count++;
2217 break;
2218
2219 case SHADER_OPCODE_GEN4_SCRATCH_READ:
2220 generate_scratch_read(inst, dst);
2221 fill_count++;
2222 break;
2223
2224 case SHADER_OPCODE_GEN7_SCRATCH_READ:
2225 generate_scratch_read_gen7(inst, dst);
2226 fill_count++;
2227 break;
2228
2229 case SHADER_OPCODE_MOV_INDIRECT:
2230 generate_mov_indirect(inst, dst, src[0], src[1]);
2231 break;
2232
2233 case SHADER_OPCODE_MOV_RELOC_IMM:
2234 assert(src[0].file == BRW_IMMEDIATE_VALUE);
2235 brw_MOV_reloc_imm(p, dst, dst.type, src[0].ud);
2236 break;
2237
2238 case SHADER_OPCODE_URB_READ_SIMD8:
2239 case SHADER_OPCODE_URB_READ_SIMD8_PER_SLOT:
2240 generate_urb_read(inst, dst, src[0]);
2241 send_count++;
2242 break;
2243
2244 case SHADER_OPCODE_URB_WRITE_SIMD8:
2245 case SHADER_OPCODE_URB_WRITE_SIMD8_PER_SLOT:
2246 case SHADER_OPCODE_URB_WRITE_SIMD8_MASKED:
2247 case SHADER_OPCODE_URB_WRITE_SIMD8_MASKED_PER_SLOT:
2248 generate_urb_write(inst, src[0]);
2249 send_count++;
2250 break;
2251
2252 case FS_OPCODE_UNIFORM_PULL_CONSTANT_LOAD:
2253 assert(inst->force_writemask_all);
2254 generate_uniform_pull_constant_load(inst, dst, src[0], src[1]);
2255 send_count++;
2256 break;
2257
2258 case FS_OPCODE_UNIFORM_PULL_CONSTANT_LOAD_GEN7:
2259 assert(inst->force_writemask_all);
2260 generate_uniform_pull_constant_load_gen7(inst, dst, src[0], src[1]);
2261 send_count++;
2262 break;
2263
2264 case FS_OPCODE_VARYING_PULL_CONSTANT_LOAD_GEN4:
2265 generate_varying_pull_constant_load_gen4(inst, dst, src[0]);
2266 send_count++;
2267 break;
2268
2269 case FS_OPCODE_REP_FB_WRITE:
2270 case FS_OPCODE_FB_WRITE:
2271 generate_fb_write(inst, src[0]);
2272 send_count++;
2273 break;
2274
2275 case FS_OPCODE_FB_READ:
2276 generate_fb_read(inst, dst, src[0]);
2277 send_count++;
2278 break;
2279
2280 case FS_OPCODE_DISCARD_JUMP:
2281 generate_discard_jump(inst);
2282 break;
2283
2284 case SHADER_OPCODE_SHADER_TIME_ADD:
2285 generate_shader_time_add(inst, src[0], src[1], src[2]);
2286 break;
2287
2288 case SHADER_OPCODE_INTERLOCK:
2289 case SHADER_OPCODE_MEMORY_FENCE: {
2290 assert(src[1].file == BRW_IMMEDIATE_VALUE);
2291 assert(src[2].file == BRW_IMMEDIATE_VALUE);
2292
2293 const enum opcode send_op = inst->opcode == SHADER_OPCODE_INTERLOCK ?
2294 BRW_OPCODE_SENDC : BRW_OPCODE_SEND;
2295
2296 brw_memory_fence(p, dst, src[0], send_op,
2297 brw_message_target(inst->sfid),
2298 /* commit_enable */ src[1].ud,
2299 /* bti */ src[2].ud);
2300 send_count++;
2301 break;
2302 }
2303
2304 case FS_OPCODE_SCHEDULING_FENCE:
2305 if (inst->sources == 0 && inst->sched.regdist == 0 &&
2306 inst->sched.mode == TGL_SBID_NULL) {
2307 if (unlikely(debug_flag))
2308 disasm_info->use_tail = true;
2309 break;
2310 }
2311
2312 if (devinfo->gen >= 12) {
2313 /* Use the available SWSB information to stall. A single SYNC is
2314 * sufficient since if there were multiple dependencies, the
2315 * scoreboard algorithm already injected other SYNCs before this
2316 * instruction.
2317 */
2318 brw_SYNC(p, TGL_SYNC_NOP);
2319 } else {
2320 for (unsigned i = 0; i < inst->sources; i++) {
2321 /* Emit a MOV to force a stall until the instruction producing the
2322 * registers finishes.
2323 */
2324 brw_MOV(p, retype(brw_null_reg(), BRW_REGISTER_TYPE_UW),
2325 retype(src[i], BRW_REGISTER_TYPE_UW));
2326 }
2327
2328 if (inst->sources > 1)
2329 multiple_instructions_emitted = true;
2330 }
2331
2332 break;
2333
2334 case SHADER_OPCODE_FIND_LIVE_CHANNEL: {
2335 const struct brw_reg mask =
2336 brw_stage_has_packed_dispatch(devinfo, stage,
2337 prog_data) ? brw_imm_ud(~0u) :
2338 stage == MESA_SHADER_FRAGMENT ? brw_vmask_reg() :
2339 brw_dmask_reg();
2340 brw_find_live_channel(p, dst, mask);
2341 break;
2342 }
2343 case FS_OPCODE_LOAD_LIVE_CHANNELS: {
2344 assert(devinfo->gen >= 8);
2345 assert(inst->force_writemask_all && inst->group == 0);
2346 assert(inst->dst.file == BAD_FILE);
2347 brw_set_default_exec_size(p, BRW_EXECUTE_1);
2348 brw_MOV(p, retype(brw_flag_subreg(inst->flag_subreg),
2349 BRW_REGISTER_TYPE_UD),
2350 retype(brw_mask_reg(0), BRW_REGISTER_TYPE_UD));
2351 break;
2352 }
2353 case SHADER_OPCODE_BROADCAST:
2354 assert(inst->force_writemask_all);
2355 brw_broadcast(p, dst, src[0], src[1]);
2356 break;
2357
2358 case SHADER_OPCODE_SHUFFLE:
2359 generate_shuffle(inst, dst, src[0], src[1]);
2360 break;
2361
2362 case SHADER_OPCODE_SEL_EXEC:
2363 assert(inst->force_writemask_all);
2364 brw_set_default_mask_control(p, BRW_MASK_DISABLE);
2365 brw_MOV(p, dst, src[1]);
2366 brw_set_default_mask_control(p, BRW_MASK_ENABLE);
2367 brw_set_default_swsb(p, tgl_swsb_null());
2368 brw_MOV(p, dst, src[0]);
2369 break;
2370
2371 case SHADER_OPCODE_QUAD_SWIZZLE:
2372 assert(src[1].file == BRW_IMMEDIATE_VALUE);
2373 assert(src[1].type == BRW_REGISTER_TYPE_UD);
2374 generate_quad_swizzle(inst, dst, src[0], src[1].ud);
2375 break;
2376
2377 case SHADER_OPCODE_CLUSTER_BROADCAST: {
2378 assert(!src[0].negate && !src[0].abs);
2379 assert(src[1].file == BRW_IMMEDIATE_VALUE);
2380 assert(src[1].type == BRW_REGISTER_TYPE_UD);
2381 assert(src[2].file == BRW_IMMEDIATE_VALUE);
2382 assert(src[2].type == BRW_REGISTER_TYPE_UD);
2383 const unsigned component = src[1].ud;
2384 const unsigned cluster_size = src[2].ud;
2385 unsigned vstride = cluster_size;
2386 unsigned width = cluster_size;
2387
2388 /* The maximum exec_size is 32, but the maximum width is only 16. */
2389 if (inst->exec_size == width) {
2390 vstride = 0;
2391 width = 1;
2392 }
2393
2394 struct brw_reg strided = stride(suboffset(src[0], component),
2395 vstride, width, 0);
2396 if (type_sz(src[0].type) > 4 &&
2397 (devinfo->is_cherryview || gen_device_info_is_9lp(devinfo))) {
2398 /* IVB has an issue (which we found empirically) where it reads
2399 * two address register components per channel for indirectly
2400 * addressed 64-bit sources.
2401 *
2402 * From the Cherryview PRM Vol 7. "Register Region Restrictions":
2403 *
2404 * "When source or destination datatype is 64b or operation is
2405 * integer DWord multiply, indirect addressing must not be
2406 * used."
2407 *
2408 * To work around both of these, we do two integer MOVs insead of
2409 * one 64-bit MOV. Because no double value should ever cross a
2410 * register boundary, it's safe to use the immediate offset in the
2411 * indirect here to handle adding 4 bytes to the offset and avoid
2412 * the extra ADD to the register file.
2413 */
2414 assert(src[0].type == dst.type);
2415 brw_MOV(p, subscript(dst, BRW_REGISTER_TYPE_D, 0),
2416 subscript(strided, BRW_REGISTER_TYPE_D, 0));
2417 brw_set_default_swsb(p, tgl_swsb_null());
2418 brw_MOV(p, subscript(dst, BRW_REGISTER_TYPE_D, 1),
2419 subscript(strided, BRW_REGISTER_TYPE_D, 1));
2420 } else {
2421 brw_MOV(p, dst, strided);
2422 }
2423 break;
2424 }
2425
2426 case FS_OPCODE_SET_SAMPLE_ID:
2427 generate_set_sample_id(inst, dst, src[0], src[1]);
2428 break;
2429
2430 case FS_OPCODE_PACK_HALF_2x16_SPLIT:
2431 generate_pack_half_2x16_split(inst, dst, src[0], src[1]);
2432 break;
2433
2434 case FS_OPCODE_PLACEHOLDER_HALT:
2435 /* This is the place where the final HALT needs to be inserted if
2436 * we've emitted any discards. If not, this will emit no code.
2437 */
2438 if (!patch_discard_jumps_to_fb_writes()) {
2439 if (unlikely(debug_flag)) {
2440 disasm_info->use_tail = true;
2441 }
2442 }
2443 break;
2444
2445 case FS_OPCODE_INTERPOLATE_AT_SAMPLE:
2446 generate_pixel_interpolator_query(inst, dst, src[0], src[1],
2447 GEN7_PIXEL_INTERPOLATOR_LOC_SAMPLE);
2448 send_count++;
2449 break;
2450
2451 case FS_OPCODE_INTERPOLATE_AT_SHARED_OFFSET:
2452 generate_pixel_interpolator_query(inst, dst, src[0], src[1],
2453 GEN7_PIXEL_INTERPOLATOR_LOC_SHARED_OFFSET);
2454 send_count++;
2455 break;
2456
2457 case FS_OPCODE_INTERPOLATE_AT_PER_SLOT_OFFSET:
2458 generate_pixel_interpolator_query(inst, dst, src[0], src[1],
2459 GEN7_PIXEL_INTERPOLATOR_LOC_PER_SLOT_OFFSET);
2460 send_count++;
2461 break;
2462
2463 case CS_OPCODE_CS_TERMINATE:
2464 generate_cs_terminate(inst, src[0]);
2465 send_count++;
2466 break;
2467
2468 case SHADER_OPCODE_BARRIER:
2469 generate_barrier(inst, src[0]);
2470 send_count++;
2471 break;
2472
2473 case BRW_OPCODE_DIM:
2474 assert(devinfo->is_haswell);
2475 assert(src[0].type == BRW_REGISTER_TYPE_DF);
2476 assert(dst.type == BRW_REGISTER_TYPE_DF);
2477 brw_DIM(p, dst, retype(src[0], BRW_REGISTER_TYPE_F));
2478 break;
2479
2480 case SHADER_OPCODE_RND_MODE: {
2481 assert(src[0].file == BRW_IMMEDIATE_VALUE);
2482 /*
2483 * Changes the floating point rounding mode updating the control
2484 * register field defined at cr0.0[5-6] bits.
2485 */
2486 enum brw_rnd_mode mode =
2487 (enum brw_rnd_mode) (src[0].d << BRW_CR0_RND_MODE_SHIFT);
2488 brw_float_controls_mode(p, mode, BRW_CR0_RND_MODE_MASK);
2489 }
2490 break;
2491
2492 case SHADER_OPCODE_FLOAT_CONTROL_MODE:
2493 assert(src[0].file == BRW_IMMEDIATE_VALUE);
2494 assert(src[1].file == BRW_IMMEDIATE_VALUE);
2495 brw_float_controls_mode(p, src[0].d, src[1].d);
2496 break;
2497
2498 default:
2499 unreachable("Unsupported opcode");
2500
2501 case SHADER_OPCODE_LOAD_PAYLOAD:
2502 unreachable("Should be lowered by lower_load_payload()");
2503 }
2504
2505 if (multiple_instructions_emitted)
2506 continue;
2507
2508 if (inst->no_dd_clear || inst->no_dd_check || inst->conditional_mod) {
2509 assert(p->next_insn_offset == last_insn_offset + 16 ||
2510 !"conditional_mod, no_dd_check, or no_dd_clear set for IR "
2511 "emitting more than 1 instruction");
2512
2513 brw_inst *last = &p->store[last_insn_offset / 16];
2514
2515 if (inst->conditional_mod)
2516 brw_inst_set_cond_modifier(p->devinfo, last, inst->conditional_mod);
2517 if (devinfo->gen < 12) {
2518 brw_inst_set_no_dd_clear(p->devinfo, last, inst->no_dd_clear);
2519 brw_inst_set_no_dd_check(p->devinfo, last, inst->no_dd_check);
2520 }
2521 }
2522 }
2523
2524 brw_set_uip_jip(p, start_offset);
2525
2526 /* end of program sentinel */
2527 disasm_new_inst_group(disasm_info, p->next_insn_offset);
2528
2529 #ifndef NDEBUG
2530 bool validated =
2531 #else
2532 if (unlikely(debug_flag))
2533 #endif
2534 brw_validate_instructions(devinfo, p->store,
2535 start_offset,
2536 p->next_insn_offset,
2537 disasm_info);
2538
2539 int before_size = p->next_insn_offset - start_offset;
2540 brw_compact_instructions(p, start_offset, disasm_info);
2541 int after_size = p->next_insn_offset - start_offset;
2542
2543 if (unlikely(debug_flag)) {
2544 unsigned char sha1[21];
2545 char sha1buf[41];
2546
2547 _mesa_sha1_compute(p->store + start_offset / sizeof(brw_inst),
2548 after_size, sha1);
2549 _mesa_sha1_format(sha1buf, sha1);
2550
2551 fprintf(stderr, "Native code for %s (sha1 %s)\n"
2552 "SIMD%d shader: %d instructions. %d loops. %u cycles. "
2553 "%d:%d spills:fills, %u sends, "
2554 "scheduled with mode %s. "
2555 "Promoted %u constants. "
2556 "Compacted %d to %d bytes (%.0f%%)\n",
2557 shader_name, sha1buf,
2558 dispatch_width, before_size / 16,
2559 loop_count, perf.latency,
2560 spill_count, fill_count, send_count,
2561 shader_stats.scheduler_mode,
2562 shader_stats.promoted_constants,
2563 before_size, after_size,
2564 100.0f * (before_size - after_size) / before_size);
2565
2566 /* overriding the shader makes disasm_info invalid */
2567 if (!brw_try_override_assembly(p, start_offset, sha1buf)) {
2568 dump_assembly(p->store, start_offset, p->next_insn_offset,
2569 disasm_info, perf.block_latency);
2570 } else {
2571 fprintf(stderr, "Successfully overrode shader with sha1 %s\n\n", sha1buf);
2572 }
2573 }
2574 ralloc_free(disasm_info);
2575 #ifndef NDEBUG
2576 if (!validated && !debug_flag) {
2577 fprintf(stderr,
2578 "Validation failed. Rerun with INTEL_DEBUG=shaders to get more information.\n");
2579 }
2580 #endif
2581 assert(validated);
2582
2583 compiler->shader_debug_log(log_data,
2584 "%s SIMD%d shader: %d inst, %d loops, %u cycles, "
2585 "%d:%d spills:fills, %u sends, "
2586 "scheduled with mode %s, "
2587 "Promoted %u constants, "
2588 "compacted %d to %d bytes.",
2589 _mesa_shader_stage_to_abbrev(stage),
2590 dispatch_width, before_size / 16 - nop_count,
2591 loop_count, perf.latency,
2592 spill_count, fill_count, send_count,
2593 shader_stats.scheduler_mode,
2594 shader_stats.promoted_constants,
2595 before_size, after_size);
2596 if (stats) {
2597 stats->dispatch_width = dispatch_width;
2598 stats->instructions = before_size / 16 - nop_count;
2599 stats->sends = send_count;
2600 stats->loops = loop_count;
2601 stats->cycles = perf.latency;
2602 stats->spills = spill_count;
2603 stats->fills = fill_count;
2604 }
2605
2606 return start_offset;
2607 }
2608
2609 void
2610 fs_generator::add_const_data(void *data, unsigned size)
2611 {
2612 assert(prog_data->const_data_size == 0);
2613 if (size > 0) {
2614 prog_data->const_data_size = size;
2615 prog_data->const_data_offset = brw_append_data(p, data, size, 32);
2616 }
2617 }
2618
2619 const unsigned *
2620 fs_generator::get_assembly()
2621 {
2622 prog_data->relocs = brw_get_shader_relocs(p, &prog_data->num_relocs);
2623
2624 return brw_get_program(p, &prog_data->program_size);
2625 }