intel/vec4: Try emitting non-scalar immediates
[mesa.git] / src / intel / compiler / brw_vec4_nir.cpp
1 /*
2 * Copyright © 2015 Intel Corporation
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21 * IN THE SOFTWARE.
22 */
23
24 #include "brw_nir.h"
25 #include "brw_vec4.h"
26 #include "brw_vec4_builder.h"
27 #include "brw_vec4_surface_builder.h"
28
29 using namespace brw;
30 using namespace brw::surface_access;
31
32 namespace brw {
33
34 void
35 vec4_visitor::emit_nir_code()
36 {
37 if (nir->num_uniforms > 0)
38 nir_setup_uniforms();
39
40 nir_emit_impl(nir_shader_get_entrypoint((nir_shader *)nir));
41 }
42
43 void
44 vec4_visitor::nir_setup_uniforms()
45 {
46 uniforms = nir->num_uniforms / 16;
47 }
48
49 void
50 vec4_visitor::nir_emit_impl(nir_function_impl *impl)
51 {
52 nir_locals = ralloc_array(mem_ctx, dst_reg, impl->reg_alloc);
53 for (unsigned i = 0; i < impl->reg_alloc; i++) {
54 nir_locals[i] = dst_reg();
55 }
56
57 foreach_list_typed(nir_register, reg, node, &impl->registers) {
58 unsigned array_elems =
59 reg->num_array_elems == 0 ? 1 : reg->num_array_elems;
60 const unsigned num_regs = array_elems * DIV_ROUND_UP(reg->bit_size, 32);
61 nir_locals[reg->index] = dst_reg(VGRF, alloc.allocate(num_regs));
62
63 if (reg->bit_size == 64)
64 nir_locals[reg->index].type = BRW_REGISTER_TYPE_DF;
65 }
66
67 nir_ssa_values = ralloc_array(mem_ctx, dst_reg, impl->ssa_alloc);
68
69 nir_emit_cf_list(&impl->body);
70 }
71
72 void
73 vec4_visitor::nir_emit_cf_list(exec_list *list)
74 {
75 exec_list_validate(list);
76 foreach_list_typed(nir_cf_node, node, node, list) {
77 switch (node->type) {
78 case nir_cf_node_if:
79 nir_emit_if(nir_cf_node_as_if(node));
80 break;
81
82 case nir_cf_node_loop:
83 nir_emit_loop(nir_cf_node_as_loop(node));
84 break;
85
86 case nir_cf_node_block:
87 nir_emit_block(nir_cf_node_as_block(node));
88 break;
89
90 default:
91 unreachable("Invalid CFG node block");
92 }
93 }
94 }
95
96 void
97 vec4_visitor::nir_emit_if(nir_if *if_stmt)
98 {
99 /* First, put the condition in f0 */
100 src_reg condition = get_nir_src(if_stmt->condition, BRW_REGISTER_TYPE_D, 1);
101 vec4_instruction *inst = emit(MOV(dst_null_d(), condition));
102 inst->conditional_mod = BRW_CONDITIONAL_NZ;
103
104 /* We can just predicate based on the X channel, as the condition only
105 * goes on its own line */
106 emit(IF(BRW_PREDICATE_ALIGN16_REPLICATE_X));
107
108 nir_emit_cf_list(&if_stmt->then_list);
109
110 /* note: if the else is empty, dead CF elimination will remove it */
111 emit(BRW_OPCODE_ELSE);
112
113 nir_emit_cf_list(&if_stmt->else_list);
114
115 emit(BRW_OPCODE_ENDIF);
116 }
117
118 void
119 vec4_visitor::nir_emit_loop(nir_loop *loop)
120 {
121 emit(BRW_OPCODE_DO);
122
123 nir_emit_cf_list(&loop->body);
124
125 emit(BRW_OPCODE_WHILE);
126 }
127
128 void
129 vec4_visitor::nir_emit_block(nir_block *block)
130 {
131 nir_foreach_instr(instr, block) {
132 nir_emit_instr(instr);
133 }
134 }
135
136 void
137 vec4_visitor::nir_emit_instr(nir_instr *instr)
138 {
139 base_ir = instr;
140
141 switch (instr->type) {
142 case nir_instr_type_load_const:
143 nir_emit_load_const(nir_instr_as_load_const(instr));
144 break;
145
146 case nir_instr_type_intrinsic:
147 nir_emit_intrinsic(nir_instr_as_intrinsic(instr));
148 break;
149
150 case nir_instr_type_alu:
151 nir_emit_alu(nir_instr_as_alu(instr));
152 break;
153
154 case nir_instr_type_jump:
155 nir_emit_jump(nir_instr_as_jump(instr));
156 break;
157
158 case nir_instr_type_tex:
159 nir_emit_texture(nir_instr_as_tex(instr));
160 break;
161
162 case nir_instr_type_ssa_undef:
163 nir_emit_undef(nir_instr_as_ssa_undef(instr));
164 break;
165
166 default:
167 unreachable("VS instruction not yet implemented by NIR->vec4");
168 }
169 }
170
171 static dst_reg
172 dst_reg_for_nir_reg(vec4_visitor *v, nir_register *nir_reg,
173 unsigned base_offset, nir_src *indirect)
174 {
175 dst_reg reg;
176
177 reg = v->nir_locals[nir_reg->index];
178 if (nir_reg->bit_size == 64)
179 reg.type = BRW_REGISTER_TYPE_DF;
180 reg = offset(reg, 8, base_offset);
181 if (indirect) {
182 reg.reladdr =
183 new(v->mem_ctx) src_reg(v->get_nir_src(*indirect,
184 BRW_REGISTER_TYPE_D,
185 1));
186 }
187 return reg;
188 }
189
190 dst_reg
191 vec4_visitor::get_nir_dest(const nir_dest &dest)
192 {
193 if (dest.is_ssa) {
194 dst_reg dst =
195 dst_reg(VGRF, alloc.allocate(DIV_ROUND_UP(dest.ssa.bit_size, 32)));
196 if (dest.ssa.bit_size == 64)
197 dst.type = BRW_REGISTER_TYPE_DF;
198 nir_ssa_values[dest.ssa.index] = dst;
199 return dst;
200 } else {
201 return dst_reg_for_nir_reg(this, dest.reg.reg, dest.reg.base_offset,
202 dest.reg.indirect);
203 }
204 }
205
206 dst_reg
207 vec4_visitor::get_nir_dest(const nir_dest &dest, enum brw_reg_type type)
208 {
209 return retype(get_nir_dest(dest), type);
210 }
211
212 dst_reg
213 vec4_visitor::get_nir_dest(const nir_dest &dest, nir_alu_type type)
214 {
215 return get_nir_dest(dest, brw_type_for_nir_type(devinfo, type));
216 }
217
218 src_reg
219 vec4_visitor::get_nir_src(const nir_src &src, enum brw_reg_type type,
220 unsigned num_components)
221 {
222 dst_reg reg;
223
224 if (src.is_ssa) {
225 assert(src.ssa != NULL);
226 reg = nir_ssa_values[src.ssa->index];
227 }
228 else {
229 reg = dst_reg_for_nir_reg(this, src.reg.reg, src.reg.base_offset,
230 src.reg.indirect);
231 }
232
233 reg = retype(reg, type);
234
235 src_reg reg_as_src = src_reg(reg);
236 reg_as_src.swizzle = brw_swizzle_for_size(num_components);
237 return reg_as_src;
238 }
239
240 src_reg
241 vec4_visitor::get_nir_src(const nir_src &src, nir_alu_type type,
242 unsigned num_components)
243 {
244 return get_nir_src(src, brw_type_for_nir_type(devinfo, type),
245 num_components);
246 }
247
248 src_reg
249 vec4_visitor::get_nir_src(const nir_src &src, unsigned num_components)
250 {
251 /* if type is not specified, default to signed int */
252 return get_nir_src(src, nir_type_int32, num_components);
253 }
254
255 src_reg
256 vec4_visitor::get_nir_src_imm(const nir_src &src)
257 {
258 assert(nir_src_num_components(src) == 1);
259 assert(nir_src_bit_size(src) == 32);
260 return nir_src_is_const(src) ? src_reg(brw_imm_d(nir_src_as_int(src))) :
261 get_nir_src(src, 1);
262 }
263
264 src_reg
265 vec4_visitor::get_indirect_offset(nir_intrinsic_instr *instr)
266 {
267 nir_src *offset_src = nir_get_io_offset_src(instr);
268
269 if (nir_src_is_const(*offset_src)) {
270 /* The only constant offset we should find is 0. brw_nir.c's
271 * add_const_offset_to_base() will fold other constant offsets
272 * into instr->const_index[0].
273 */
274 assert(nir_src_as_uint(*offset_src) == 0);
275 return src_reg();
276 }
277
278 return get_nir_src(*offset_src, BRW_REGISTER_TYPE_UD, 1);
279 }
280
281 static src_reg
282 setup_imm_df(const vec4_builder &bld, double v)
283 {
284 const gen_device_info *devinfo = bld.shader->devinfo;
285 assert(devinfo->gen >= 7);
286
287 if (devinfo->gen >= 8)
288 return brw_imm_df(v);
289
290 /* gen7.5 does not support DF immediates straighforward but the DIM
291 * instruction allows to set the 64-bit immediate value.
292 */
293 if (devinfo->is_haswell) {
294 const vec4_builder ubld = bld.exec_all();
295 const dst_reg dst = bld.vgrf(BRW_REGISTER_TYPE_DF);
296 ubld.DIM(dst, brw_imm_df(v));
297 return swizzle(src_reg(dst), BRW_SWIZZLE_XXXX);
298 }
299
300 /* gen7 does not support DF immediates */
301 union {
302 double d;
303 struct {
304 uint32_t i1;
305 uint32_t i2;
306 };
307 } di;
308
309 di.d = v;
310
311 /* Write the low 32-bit of the constant to the X:UD channel and the
312 * high 32-bit to the Y:UD channel to build the constant in a VGRF.
313 * We have to do this twice (offset 0 and offset 1), since a DF VGRF takes
314 * two SIMD8 registers in SIMD4x2 execution. Finally, return a swizzle
315 * XXXX so any access to the VGRF only reads the constant data in these
316 * channels.
317 */
318 const dst_reg tmp = bld.vgrf(BRW_REGISTER_TYPE_UD, 2);
319 for (unsigned n = 0; n < 2; n++) {
320 const vec4_builder ubld = bld.exec_all().group(4, n);
321 ubld.MOV(writemask(offset(tmp, 8, n), WRITEMASK_X), brw_imm_ud(di.i1));
322 ubld.MOV(writemask(offset(tmp, 8, n), WRITEMASK_Y), brw_imm_ud(di.i2));
323 }
324
325 return swizzle(src_reg(retype(tmp, BRW_REGISTER_TYPE_DF)), BRW_SWIZZLE_XXXX);
326 }
327
328 void
329 vec4_visitor::nir_emit_load_const(nir_load_const_instr *instr)
330 {
331 dst_reg reg;
332
333 if (instr->def.bit_size == 64) {
334 reg = dst_reg(VGRF, alloc.allocate(2));
335 reg.type = BRW_REGISTER_TYPE_DF;
336 } else {
337 reg = dst_reg(VGRF, alloc.allocate(1));
338 reg.type = BRW_REGISTER_TYPE_D;
339 }
340
341 const vec4_builder ibld = vec4_builder(this).at_end();
342 unsigned remaining = brw_writemask_for_size(instr->def.num_components);
343
344 /* @FIXME: consider emitting vector operations to save some MOVs in
345 * cases where the components are representable in 8 bits.
346 * For now, we emit a MOV for each distinct value.
347 */
348 for (unsigned i = 0; i < instr->def.num_components; i++) {
349 unsigned writemask = 1 << i;
350
351 if ((remaining & writemask) == 0)
352 continue;
353
354 for (unsigned j = i; j < instr->def.num_components; j++) {
355 if ((instr->def.bit_size == 32 &&
356 instr->value[i].u32 == instr->value[j].u32) ||
357 (instr->def.bit_size == 64 &&
358 instr->value[i].f64 == instr->value[j].f64)) {
359 writemask |= 1 << j;
360 }
361 }
362
363 reg.writemask = writemask;
364 if (instr->def.bit_size == 64) {
365 emit(MOV(reg, setup_imm_df(ibld, instr->value[i].f64)));
366 } else {
367 emit(MOV(reg, brw_imm_d(instr->value[i].i32)));
368 }
369
370 remaining &= ~writemask;
371 }
372
373 /* Set final writemask */
374 reg.writemask = brw_writemask_for_size(instr->def.num_components);
375
376 nir_ssa_values[instr->def.index] = reg;
377 }
378
379 src_reg
380 vec4_visitor::get_nir_ssbo_intrinsic_index(nir_intrinsic_instr *instr)
381 {
382 /* SSBO stores are weird in that their index is in src[1] */
383 const unsigned src = instr->intrinsic == nir_intrinsic_store_ssbo ? 1 : 0;
384
385 src_reg surf_index;
386 if (nir_src_is_const(instr->src[src])) {
387 unsigned index = prog_data->base.binding_table.ssbo_start +
388 nir_src_as_uint(instr->src[src]);
389 surf_index = brw_imm_ud(index);
390 } else {
391 surf_index = src_reg(this, glsl_type::uint_type);
392 emit(ADD(dst_reg(surf_index), get_nir_src(instr->src[src], 1),
393 brw_imm_ud(prog_data->base.binding_table.ssbo_start)));
394 surf_index = emit_uniformize(surf_index);
395 }
396
397 return surf_index;
398 }
399
400 void
401 vec4_visitor::nir_emit_intrinsic(nir_intrinsic_instr *instr)
402 {
403 dst_reg dest;
404 src_reg src;
405
406 switch (instr->intrinsic) {
407
408 case nir_intrinsic_load_input: {
409 /* We set EmitNoIndirectInput for VS */
410 unsigned load_offset = nir_src_as_uint(instr->src[0]);
411
412 dest = get_nir_dest(instr->dest);
413 dest.writemask = brw_writemask_for_size(instr->num_components);
414
415 src = src_reg(ATTR, instr->const_index[0] + load_offset,
416 glsl_type::uvec4_type);
417 src = retype(src, dest.type);
418
419 bool is_64bit = nir_dest_bit_size(instr->dest) == 64;
420 if (is_64bit) {
421 dst_reg tmp = dst_reg(this, glsl_type::dvec4_type);
422 src.swizzle = BRW_SWIZZLE_XYZW;
423 shuffle_64bit_data(tmp, src, false);
424 emit(MOV(dest, src_reg(tmp)));
425 } else {
426 /* Swizzle source based on component layout qualifier */
427 src.swizzle = BRW_SWZ_COMP_INPUT(nir_intrinsic_component(instr));
428 emit(MOV(dest, src));
429 }
430 break;
431 }
432
433 case nir_intrinsic_store_output: {
434 unsigned store_offset = nir_src_as_uint(instr->src[1]);
435 int varying = instr->const_index[0] + store_offset;
436
437 bool is_64bit = nir_src_bit_size(instr->src[0]) == 64;
438 if (is_64bit) {
439 src_reg data;
440 src = get_nir_src(instr->src[0], BRW_REGISTER_TYPE_DF,
441 instr->num_components);
442 data = src_reg(this, glsl_type::dvec4_type);
443 shuffle_64bit_data(dst_reg(data), src, true);
444 src = retype(data, BRW_REGISTER_TYPE_F);
445 } else {
446 src = get_nir_src(instr->src[0], BRW_REGISTER_TYPE_F,
447 instr->num_components);
448 }
449
450 unsigned c = nir_intrinsic_component(instr);
451 output_reg[varying][c] = dst_reg(src);
452 output_num_components[varying][c] = instr->num_components;
453
454 unsigned num_components = instr->num_components;
455 if (is_64bit)
456 num_components *= 2;
457
458 output_reg[varying][c] = dst_reg(src);
459 output_num_components[varying][c] = MIN2(4, num_components);
460
461 if (is_64bit && num_components > 4) {
462 assert(num_components <= 8);
463 output_reg[varying + 1][c] = byte_offset(dst_reg(src), REG_SIZE);
464 output_num_components[varying + 1][c] = num_components - 4;
465 }
466 break;
467 }
468
469 case nir_intrinsic_get_buffer_size: {
470 assert(nir_src_num_components(instr->src[0]) == 1);
471 unsigned ssbo_index = nir_src_is_const(instr->src[0]) ?
472 nir_src_as_uint(instr->src[0]) : 0;
473
474 const unsigned index =
475 prog_data->base.binding_table.ssbo_start + ssbo_index;
476 dst_reg result_dst = get_nir_dest(instr->dest);
477 vec4_instruction *inst = new(mem_ctx)
478 vec4_instruction(SHADER_OPCODE_GET_BUFFER_SIZE, result_dst);
479
480 inst->base_mrf = 2;
481 inst->mlen = 1; /* always at least one */
482 inst->src[1] = brw_imm_ud(index);
483
484 /* MRF for the first parameter */
485 src_reg lod = brw_imm_d(0);
486 int param_base = inst->base_mrf;
487 int writemask = WRITEMASK_X;
488 emit(MOV(dst_reg(MRF, param_base, glsl_type::int_type, writemask), lod));
489
490 emit(inst);
491 break;
492 }
493
494 case nir_intrinsic_store_ssbo: {
495 assert(devinfo->gen >= 7);
496
497 /* brw_nir_lower_mem_access_bit_sizes takes care of this */
498 assert(nir_src_bit_size(instr->src[0]) == 32);
499 assert(nir_intrinsic_write_mask(instr) ==
500 (1u << instr->num_components) - 1);
501
502 src_reg surf_index = get_nir_ssbo_intrinsic_index(instr);
503 src_reg offset_reg = retype(get_nir_src_imm(instr->src[2]),
504 BRW_REGISTER_TYPE_UD);
505
506 /* Value */
507 src_reg val_reg = get_nir_src(instr->src[0], BRW_REGISTER_TYPE_F, 4);
508
509 /* IvyBridge does not have a native SIMD4x2 untyped write message so untyped
510 * writes will use SIMD8 mode. In order to hide this and keep symmetry across
511 * typed and untyped messages and across hardware platforms, the
512 * current implementation of the untyped messages will transparently convert
513 * the SIMD4x2 payload into an equivalent SIMD8 payload by transposing it
514 * and enabling only channel X on the SEND instruction.
515 *
516 * The above, works well for full vector writes, but not for partial writes
517 * where we want to write some channels and not others, like when we have
518 * code such as v.xyw = vec3(1,2,4). Because the untyped write messages are
519 * quite restrictive with regards to the channel enables we can configure in
520 * the message descriptor (not all combinations are allowed) we cannot simply
521 * implement these scenarios with a single message while keeping the
522 * aforementioned symmetry in the implementation. For now we de decided that
523 * it is better to keep the symmetry to reduce complexity, so in situations
524 * such as the one described we end up emitting two untyped write messages
525 * (one for xy and another for w).
526 *
527 * The code below packs consecutive channels into a single write message,
528 * detects gaps in the vector write and if needed, sends a second message
529 * with the remaining channels. If in the future we decide that we want to
530 * emit a single message at the expense of losing the symmetry in the
531 * implementation we can:
532 *
533 * 1) For IvyBridge: Only use the red channel of the untyped write SIMD8
534 * message payload. In this mode we can write up to 8 offsets and dwords
535 * to the red channel only (for the two vec4s in the SIMD4x2 execution)
536 * and select which of the 8 channels carry data to write by setting the
537 * appropriate writemask in the dst register of the SEND instruction.
538 * It would require to write a new generator opcode specifically for
539 * IvyBridge since we would need to prepare a SIMD8 payload that could
540 * use any channel, not just X.
541 *
542 * 2) For Haswell+: Simply send a single write message but set the writemask
543 * on the dst of the SEND instruction to select the channels we want to
544 * write. It would require to modify the current messages to receive
545 * and honor the writemask provided.
546 */
547 const vec4_builder bld = vec4_builder(this).at_end()
548 .annotate(current_annotation, base_ir);
549
550 emit_untyped_write(bld, surf_index, offset_reg, val_reg,
551 1 /* dims */, instr->num_components /* size */,
552 BRW_PREDICATE_NONE);
553 break;
554 }
555
556 case nir_intrinsic_load_ssbo: {
557 assert(devinfo->gen >= 7);
558
559 /* brw_nir_lower_mem_access_bit_sizes takes care of this */
560 assert(nir_dest_bit_size(instr->dest) == 32);
561
562 src_reg surf_index = get_nir_ssbo_intrinsic_index(instr);
563 src_reg offset_reg = retype(get_nir_src_imm(instr->src[1]),
564 BRW_REGISTER_TYPE_UD);
565
566 /* Read the vector */
567 const vec4_builder bld = vec4_builder(this).at_end()
568 .annotate(current_annotation, base_ir);
569
570 src_reg read_result = emit_untyped_read(bld, surf_index, offset_reg,
571 1 /* dims */, 4 /* size*/,
572 BRW_PREDICATE_NONE);
573 dst_reg dest = get_nir_dest(instr->dest);
574 read_result.type = dest.type;
575 read_result.swizzle = brw_swizzle_for_size(instr->num_components);
576 emit(MOV(dest, read_result));
577 break;
578 }
579
580 case nir_intrinsic_ssbo_atomic_add: {
581 int op = BRW_AOP_ADD;
582
583 if (nir_src_is_const(instr->src[2])) {
584 int add_val = nir_src_as_int(instr->src[2]);
585 if (add_val == 1)
586 op = BRW_AOP_INC;
587 else if (add_val == -1)
588 op = BRW_AOP_DEC;
589 }
590
591 nir_emit_ssbo_atomic(op, instr);
592 break;
593 }
594 case nir_intrinsic_ssbo_atomic_imin:
595 nir_emit_ssbo_atomic(BRW_AOP_IMIN, instr);
596 break;
597 case nir_intrinsic_ssbo_atomic_umin:
598 nir_emit_ssbo_atomic(BRW_AOP_UMIN, instr);
599 break;
600 case nir_intrinsic_ssbo_atomic_imax:
601 nir_emit_ssbo_atomic(BRW_AOP_IMAX, instr);
602 break;
603 case nir_intrinsic_ssbo_atomic_umax:
604 nir_emit_ssbo_atomic(BRW_AOP_UMAX, instr);
605 break;
606 case nir_intrinsic_ssbo_atomic_and:
607 nir_emit_ssbo_atomic(BRW_AOP_AND, instr);
608 break;
609 case nir_intrinsic_ssbo_atomic_or:
610 nir_emit_ssbo_atomic(BRW_AOP_OR, instr);
611 break;
612 case nir_intrinsic_ssbo_atomic_xor:
613 nir_emit_ssbo_atomic(BRW_AOP_XOR, instr);
614 break;
615 case nir_intrinsic_ssbo_atomic_exchange:
616 nir_emit_ssbo_atomic(BRW_AOP_MOV, instr);
617 break;
618 case nir_intrinsic_ssbo_atomic_comp_swap:
619 nir_emit_ssbo_atomic(BRW_AOP_CMPWR, instr);
620 break;
621
622 case nir_intrinsic_load_vertex_id:
623 unreachable("should be lowered by lower_vertex_id()");
624
625 case nir_intrinsic_load_vertex_id_zero_base:
626 case nir_intrinsic_load_base_vertex:
627 case nir_intrinsic_load_instance_id:
628 case nir_intrinsic_load_base_instance:
629 case nir_intrinsic_load_draw_id:
630 case nir_intrinsic_load_invocation_id:
631 unreachable("should be lowered by brw_nir_lower_vs_inputs()");
632
633 case nir_intrinsic_load_uniform: {
634 /* Offsets are in bytes but they should always be multiples of 4 */
635 assert(nir_intrinsic_base(instr) % 4 == 0);
636
637 dest = get_nir_dest(instr->dest);
638
639 src = src_reg(dst_reg(UNIFORM, nir_intrinsic_base(instr) / 16));
640 src.type = dest.type;
641
642 /* Uniforms don't actually have to be vec4 aligned. In the case that
643 * it isn't, we have to use a swizzle to shift things around. They
644 * do still have the std140 alignment requirement that vec2's have to
645 * be vec2-aligned and vec3's and vec4's have to be vec4-aligned.
646 *
647 * The swizzle also works in the indirect case as the generator adds
648 * the swizzle to the offset for us.
649 */
650 const int type_size = type_sz(src.type);
651 unsigned shift = (nir_intrinsic_base(instr) % 16) / type_size;
652 assert(shift + instr->num_components <= 4);
653
654 if (nir_src_is_const(instr->src[0])) {
655 const unsigned load_offset = nir_src_as_uint(instr->src[0]);
656 /* Offsets are in bytes but they should always be multiples of 4 */
657 assert(load_offset % 4 == 0);
658
659 src.swizzle = brw_swizzle_for_size(instr->num_components);
660 dest.writemask = brw_writemask_for_size(instr->num_components);
661 unsigned offset = load_offset + shift * type_size;
662 src.offset = ROUND_DOWN_TO(offset, 16);
663 shift = (offset % 16) / type_size;
664 assert(shift + instr->num_components <= 4);
665 src.swizzle += BRW_SWIZZLE4(shift, shift, shift, shift);
666
667 emit(MOV(dest, src));
668 } else {
669 /* Uniform arrays are vec4 aligned, because of std140 alignment
670 * rules.
671 */
672 assert(shift == 0);
673
674 src_reg indirect = get_nir_src(instr->src[0], BRW_REGISTER_TYPE_UD, 1);
675
676 /* MOV_INDIRECT is going to stomp the whole thing anyway */
677 dest.writemask = WRITEMASK_XYZW;
678
679 emit(SHADER_OPCODE_MOV_INDIRECT, dest, src,
680 indirect, brw_imm_ud(instr->const_index[1]));
681 }
682 break;
683 }
684
685 case nir_intrinsic_load_ubo: {
686 src_reg surf_index;
687
688 dest = get_nir_dest(instr->dest);
689
690 if (nir_src_is_const(instr->src[0])) {
691 /* The block index is a constant, so just emit the binding table entry
692 * as an immediate.
693 */
694 const unsigned index = prog_data->base.binding_table.ubo_start +
695 nir_src_as_uint(instr->src[0]);
696 surf_index = brw_imm_ud(index);
697 } else {
698 /* The block index is not a constant. Evaluate the index expression
699 * per-channel and add the base UBO index; we have to select a value
700 * from any live channel.
701 */
702 surf_index = src_reg(this, glsl_type::uint_type);
703 emit(ADD(dst_reg(surf_index), get_nir_src(instr->src[0], nir_type_int32,
704 instr->num_components),
705 brw_imm_ud(prog_data->base.binding_table.ubo_start)));
706 surf_index = emit_uniformize(surf_index);
707 }
708
709 src_reg offset_reg;
710 if (nir_src_is_const(instr->src[1])) {
711 unsigned load_offset = nir_src_as_uint(instr->src[1]);
712 offset_reg = brw_imm_ud(load_offset & ~15);
713 } else {
714 offset_reg = src_reg(this, glsl_type::uint_type);
715 emit(MOV(dst_reg(offset_reg),
716 get_nir_src(instr->src[1], nir_type_uint32, 1)));
717 }
718
719 src_reg packed_consts;
720 if (nir_dest_bit_size(instr->dest) == 32) {
721 packed_consts = src_reg(this, glsl_type::vec4_type);
722 emit_pull_constant_load_reg(dst_reg(packed_consts),
723 surf_index,
724 offset_reg,
725 NULL, NULL /* before_block/inst */);
726 } else {
727 src_reg temp = src_reg(this, glsl_type::dvec4_type);
728 src_reg temp_float = retype(temp, BRW_REGISTER_TYPE_F);
729
730 emit_pull_constant_load_reg(dst_reg(temp_float),
731 surf_index, offset_reg, NULL, NULL);
732 if (offset_reg.file == IMM)
733 offset_reg.ud += 16;
734 else
735 emit(ADD(dst_reg(offset_reg), offset_reg, brw_imm_ud(16u)));
736 emit_pull_constant_load_reg(dst_reg(byte_offset(temp_float, REG_SIZE)),
737 surf_index, offset_reg, NULL, NULL);
738
739 packed_consts = src_reg(this, glsl_type::dvec4_type);
740 shuffle_64bit_data(dst_reg(packed_consts), temp, false);
741 }
742
743 packed_consts.swizzle = brw_swizzle_for_size(instr->num_components);
744 if (nir_src_is_const(instr->src[1])) {
745 unsigned load_offset = nir_src_as_uint(instr->src[1]);
746 unsigned type_size = type_sz(dest.type);
747 packed_consts.swizzle +=
748 BRW_SWIZZLE4(load_offset % 16 / type_size,
749 load_offset % 16 / type_size,
750 load_offset % 16 / type_size,
751 load_offset % 16 / type_size);
752 }
753
754 emit(MOV(dest, retype(packed_consts, dest.type)));
755
756 break;
757 }
758
759 case nir_intrinsic_memory_barrier: {
760 const vec4_builder bld =
761 vec4_builder(this).at_end().annotate(current_annotation, base_ir);
762 const dst_reg tmp = bld.vgrf(BRW_REGISTER_TYPE_UD, 2);
763 bld.emit(SHADER_OPCODE_MEMORY_FENCE, tmp, brw_vec8_grf(0, 0))
764 ->size_written = 2 * REG_SIZE;
765 break;
766 }
767
768 case nir_intrinsic_shader_clock: {
769 /* We cannot do anything if there is an event, so ignore it for now */
770 const src_reg shader_clock = get_timestamp();
771 const enum brw_reg_type type = brw_type_for_base_type(glsl_type::uvec2_type);
772
773 dest = get_nir_dest(instr->dest, type);
774 emit(MOV(dest, shader_clock));
775 break;
776 }
777
778 default:
779 unreachable("Unknown intrinsic");
780 }
781 }
782
783 void
784 vec4_visitor::nir_emit_ssbo_atomic(int op, nir_intrinsic_instr *instr)
785 {
786 dst_reg dest;
787 if (nir_intrinsic_infos[instr->intrinsic].has_dest)
788 dest = get_nir_dest(instr->dest);
789
790 src_reg surface = get_nir_ssbo_intrinsic_index(instr);
791 src_reg offset = get_nir_src(instr->src[1], 1);
792 src_reg data1;
793 if (op != BRW_AOP_INC && op != BRW_AOP_DEC && op != BRW_AOP_PREDEC)
794 data1 = get_nir_src(instr->src[2], 1);
795 src_reg data2;
796 if (op == BRW_AOP_CMPWR)
797 data2 = get_nir_src(instr->src[3], 1);
798
799 /* Emit the actual atomic operation operation */
800 const vec4_builder bld =
801 vec4_builder(this).at_end().annotate(current_annotation, base_ir);
802
803 src_reg atomic_result = emit_untyped_atomic(bld, surface, offset,
804 data1, data2,
805 1 /* dims */, 1 /* rsize */,
806 op,
807 BRW_PREDICATE_NONE);
808 dest.type = atomic_result.type;
809 bld.MOV(dest, atomic_result);
810 }
811
812 static unsigned
813 brw_swizzle_for_nir_swizzle(uint8_t swizzle[4])
814 {
815 return BRW_SWIZZLE4(swizzle[0], swizzle[1], swizzle[2], swizzle[3]);
816 }
817
818 static enum brw_conditional_mod
819 brw_conditional_for_nir_comparison(nir_op op)
820 {
821 switch (op) {
822 case nir_op_flt32:
823 case nir_op_ilt32:
824 case nir_op_ult32:
825 return BRW_CONDITIONAL_L;
826
827 case nir_op_fge32:
828 case nir_op_ige32:
829 case nir_op_uge32:
830 return BRW_CONDITIONAL_GE;
831
832 case nir_op_feq32:
833 case nir_op_ieq32:
834 case nir_op_b32all_fequal2:
835 case nir_op_b32all_iequal2:
836 case nir_op_b32all_fequal3:
837 case nir_op_b32all_iequal3:
838 case nir_op_b32all_fequal4:
839 case nir_op_b32all_iequal4:
840 return BRW_CONDITIONAL_Z;
841
842 case nir_op_fne32:
843 case nir_op_ine32:
844 case nir_op_b32any_fnequal2:
845 case nir_op_b32any_inequal2:
846 case nir_op_b32any_fnequal3:
847 case nir_op_b32any_inequal3:
848 case nir_op_b32any_fnequal4:
849 case nir_op_b32any_inequal4:
850 return BRW_CONDITIONAL_NZ;
851
852 default:
853 unreachable("not reached: bad operation for comparison");
854 }
855 }
856
857 bool
858 vec4_visitor::optimize_predicate(nir_alu_instr *instr,
859 enum brw_predicate *predicate)
860 {
861 if (!instr->src[0].src.is_ssa ||
862 instr->src[0].src.ssa->parent_instr->type != nir_instr_type_alu)
863 return false;
864
865 nir_alu_instr *cmp_instr =
866 nir_instr_as_alu(instr->src[0].src.ssa->parent_instr);
867
868 switch (cmp_instr->op) {
869 case nir_op_b32any_fnequal2:
870 case nir_op_b32any_inequal2:
871 case nir_op_b32any_fnequal3:
872 case nir_op_b32any_inequal3:
873 case nir_op_b32any_fnequal4:
874 case nir_op_b32any_inequal4:
875 *predicate = BRW_PREDICATE_ALIGN16_ANY4H;
876 break;
877 case nir_op_b32all_fequal2:
878 case nir_op_b32all_iequal2:
879 case nir_op_b32all_fequal3:
880 case nir_op_b32all_iequal3:
881 case nir_op_b32all_fequal4:
882 case nir_op_b32all_iequal4:
883 *predicate = BRW_PREDICATE_ALIGN16_ALL4H;
884 break;
885 default:
886 return false;
887 }
888
889 unsigned size_swizzle =
890 brw_swizzle_for_size(nir_op_infos[cmp_instr->op].input_sizes[0]);
891
892 src_reg op[2];
893 assert(nir_op_infos[cmp_instr->op].num_inputs == 2);
894 for (unsigned i = 0; i < 2; i++) {
895 nir_alu_type type = nir_op_infos[cmp_instr->op].input_types[i];
896 unsigned bit_size = nir_src_bit_size(cmp_instr->src[i].src);
897 type = (nir_alu_type) (((unsigned) type) | bit_size);
898 op[i] = get_nir_src(cmp_instr->src[i].src, type, 4);
899 unsigned base_swizzle =
900 brw_swizzle_for_nir_swizzle(cmp_instr->src[i].swizzle);
901 op[i].swizzle = brw_compose_swizzle(size_swizzle, base_swizzle);
902 op[i].abs = cmp_instr->src[i].abs;
903 op[i].negate = cmp_instr->src[i].negate;
904 }
905
906 emit(CMP(dst_null_d(), op[0], op[1],
907 brw_conditional_for_nir_comparison(cmp_instr->op)));
908
909 return true;
910 }
911
912 static void
913 emit_find_msb_using_lzd(const vec4_builder &bld,
914 const dst_reg &dst,
915 const src_reg &src,
916 bool is_signed)
917 {
918 vec4_instruction *inst;
919 src_reg temp = src;
920
921 if (is_signed) {
922 /* LZD of an absolute value source almost always does the right
923 * thing. There are two problem values:
924 *
925 * * 0x80000000. Since abs(0x80000000) == 0x80000000, LZD returns
926 * 0. However, findMSB(int(0x80000000)) == 30.
927 *
928 * * 0xffffffff. Since abs(0xffffffff) == 1, LZD returns
929 * 31. Section 8.8 (Integer Functions) of the GLSL 4.50 spec says:
930 *
931 * For a value of zero or negative one, -1 will be returned.
932 *
933 * * Negative powers of two. LZD(abs(-(1<<x))) returns x, but
934 * findMSB(-(1<<x)) should return x-1.
935 *
936 * For all negative number cases, including 0x80000000 and
937 * 0xffffffff, the correct value is obtained from LZD if instead of
938 * negating the (already negative) value the logical-not is used. A
939 * conditonal logical-not can be achieved in two instructions.
940 */
941 temp = src_reg(bld.vgrf(BRW_REGISTER_TYPE_D));
942
943 bld.ASR(dst_reg(temp), src, brw_imm_d(31));
944 bld.XOR(dst_reg(temp), temp, src);
945 }
946
947 bld.LZD(retype(dst, BRW_REGISTER_TYPE_UD),
948 retype(temp, BRW_REGISTER_TYPE_UD));
949
950 /* LZD counts from the MSB side, while GLSL's findMSB() wants the count
951 * from the LSB side. Subtract the result from 31 to convert the MSB count
952 * into an LSB count. If no bits are set, LZD will return 32. 31-32 = -1,
953 * which is exactly what findMSB() is supposed to return.
954 */
955 inst = bld.ADD(dst, retype(src_reg(dst), BRW_REGISTER_TYPE_D),
956 brw_imm_d(31));
957 inst->src[0].negate = true;
958 }
959
960 void
961 vec4_visitor::emit_conversion_from_double(dst_reg dst, src_reg src,
962 bool saturate)
963 {
964 /* BDW PRM vol 15 - workarounds:
965 * DF->f format conversion for Align16 has wrong emask calculation when
966 * source is immediate.
967 */
968 if (devinfo->gen == 8 && dst.type == BRW_REGISTER_TYPE_F &&
969 src.file == BRW_IMMEDIATE_VALUE) {
970 vec4_instruction *inst = emit(MOV(dst, brw_imm_f(src.df)));
971 inst->saturate = saturate;
972 return;
973 }
974
975 enum opcode op;
976 switch (dst.type) {
977 case BRW_REGISTER_TYPE_D:
978 op = VEC4_OPCODE_DOUBLE_TO_D32;
979 break;
980 case BRW_REGISTER_TYPE_UD:
981 op = VEC4_OPCODE_DOUBLE_TO_U32;
982 break;
983 case BRW_REGISTER_TYPE_F:
984 op = VEC4_OPCODE_DOUBLE_TO_F32;
985 break;
986 default:
987 unreachable("Unknown conversion");
988 }
989
990 dst_reg temp = dst_reg(this, glsl_type::dvec4_type);
991 emit(MOV(temp, src));
992 dst_reg temp2 = dst_reg(this, glsl_type::dvec4_type);
993 emit(op, temp2, src_reg(temp));
994
995 emit(VEC4_OPCODE_PICK_LOW_32BIT, retype(temp2, dst.type), src_reg(temp2));
996 vec4_instruction *inst = emit(MOV(dst, src_reg(retype(temp2, dst.type))));
997 inst->saturate = saturate;
998 }
999
1000 void
1001 vec4_visitor::emit_conversion_to_double(dst_reg dst, src_reg src,
1002 bool saturate)
1003 {
1004 dst_reg tmp_dst = dst_reg(src_reg(this, glsl_type::dvec4_type));
1005 src_reg tmp_src = retype(src_reg(this, glsl_type::vec4_type), src.type);
1006 emit(MOV(dst_reg(tmp_src), src));
1007 emit(VEC4_OPCODE_TO_DOUBLE, tmp_dst, tmp_src);
1008 vec4_instruction *inst = emit(MOV(dst, src_reg(tmp_dst)));
1009 inst->saturate = saturate;
1010 }
1011
1012 /**
1013 * Try to use an immediate value for source 1
1014 *
1015 * In cases of flow control, constant propagation is sometimes unable to
1016 * determine that a register contains a constant value. To work around this,
1017 * try to emit a literal as the second source here.
1018 */
1019 static void
1020 try_immediate_source(const nir_alu_instr *instr, src_reg *op,
1021 MAYBE_UNUSED const gen_device_info *devinfo)
1022 {
1023 if (nir_src_bit_size(instr->src[1].src) != 32 ||
1024 !nir_src_is_const(instr->src[1].src))
1025 return;
1026
1027 const enum brw_reg_type old_type = op->type;
1028
1029 switch (old_type) {
1030 case BRW_REGISTER_TYPE_D:
1031 case BRW_REGISTER_TYPE_UD: {
1032 int first_comp = -1;
1033 int d;
1034
1035 for (unsigned i = 0; i < NIR_MAX_VEC_COMPONENTS; i++) {
1036 if (nir_alu_instr_channel_used(instr, 1, i)) {
1037 if (first_comp < 0) {
1038 first_comp = i;
1039 d = nir_src_comp_as_int(instr->src[1].src,
1040 instr->src[1].swizzle[i]);
1041 } else if (d != nir_src_comp_as_int(instr->src[1].src,
1042 instr->src[1].swizzle[i])) {
1043 return;
1044 }
1045 }
1046 }
1047
1048 if (op->abs)
1049 d = MAX2(-d, d);
1050
1051 if (op->negate) {
1052 /* On Gen8+ a negation source modifier on a logical operation means
1053 * something different. Nothing should generate this, so assert that
1054 * it does not occur.
1055 */
1056 assert(devinfo->gen < 8 || (instr->op != nir_op_iand &&
1057 instr->op != nir_op_ior &&
1058 instr->op != nir_op_ixor));
1059 d = -d;
1060 }
1061
1062 *op = retype(src_reg(brw_imm_d(d)), old_type);
1063 break;
1064 }
1065
1066 case BRW_REGISTER_TYPE_F: {
1067 int first_comp = -1;
1068 float f;
1069
1070 for (unsigned i = 0; i < NIR_MAX_VEC_COMPONENTS; i++) {
1071 if (nir_alu_instr_channel_used(instr, 1, i)) {
1072 if (first_comp < 0) {
1073 first_comp = i;
1074 f = nir_src_comp_as_float(instr->src[1].src,
1075 instr->src[1].swizzle[i]);
1076 } else if (f != nir_src_comp_as_float(instr->src[1].src,
1077 instr->src[1].swizzle[i])) {
1078 return;
1079 }
1080 }
1081 }
1082
1083 if (op->abs)
1084 f = fabs(f);
1085
1086 if (op->negate)
1087 f = -f;
1088
1089 *op = src_reg(brw_imm_f(f));
1090 assert(op->type == old_type);
1091 break;
1092 }
1093
1094 default:
1095 unreachable("Non-32bit type.");
1096 }
1097 }
1098
1099 void
1100 vec4_visitor::nir_emit_alu(nir_alu_instr *instr)
1101 {
1102 vec4_instruction *inst;
1103
1104 nir_alu_type dst_type = (nir_alu_type) (nir_op_infos[instr->op].output_type |
1105 nir_dest_bit_size(instr->dest.dest));
1106 dst_reg dst = get_nir_dest(instr->dest.dest, dst_type);
1107 dst.writemask = instr->dest.write_mask;
1108
1109 src_reg op[4];
1110 for (unsigned i = 0; i < nir_op_infos[instr->op].num_inputs; i++) {
1111 nir_alu_type src_type = (nir_alu_type)
1112 (nir_op_infos[instr->op].input_types[i] |
1113 nir_src_bit_size(instr->src[i].src));
1114 op[i] = get_nir_src(instr->src[i].src, src_type, 4);
1115 op[i].swizzle = brw_swizzle_for_nir_swizzle(instr->src[i].swizzle);
1116 op[i].abs = instr->src[i].abs;
1117 op[i].negate = instr->src[i].negate;
1118 }
1119
1120 switch (instr->op) {
1121 case nir_op_mov:
1122 inst = emit(MOV(dst, op[0]));
1123 inst->saturate = instr->dest.saturate;
1124 break;
1125
1126 case nir_op_vec2:
1127 case nir_op_vec3:
1128 case nir_op_vec4:
1129 unreachable("not reached: should be handled by lower_vec_to_movs()");
1130
1131 case nir_op_i2f32:
1132 case nir_op_u2f32:
1133 inst = emit(MOV(dst, op[0]));
1134 inst->saturate = instr->dest.saturate;
1135 break;
1136
1137 case nir_op_f2f32:
1138 case nir_op_f2i32:
1139 case nir_op_f2u32:
1140 if (nir_src_bit_size(instr->src[0].src) == 64)
1141 emit_conversion_from_double(dst, op[0], instr->dest.saturate);
1142 else
1143 inst = emit(MOV(dst, op[0]));
1144 break;
1145
1146 case nir_op_f2f64:
1147 case nir_op_i2f64:
1148 case nir_op_u2f64:
1149 emit_conversion_to_double(dst, op[0], instr->dest.saturate);
1150 break;
1151
1152 case nir_op_fsat:
1153 inst = emit(MOV(dst, op[0]));
1154 inst->saturate = true;
1155 break;
1156
1157 case nir_op_fneg:
1158 case nir_op_ineg:
1159 op[0].negate = true;
1160 inst = emit(MOV(dst, op[0]));
1161 if (instr->op == nir_op_fneg)
1162 inst->saturate = instr->dest.saturate;
1163 break;
1164
1165 case nir_op_fabs:
1166 case nir_op_iabs:
1167 op[0].negate = false;
1168 op[0].abs = true;
1169 inst = emit(MOV(dst, op[0]));
1170 if (instr->op == nir_op_fabs)
1171 inst->saturate = instr->dest.saturate;
1172 break;
1173
1174 case nir_op_iadd:
1175 assert(nir_dest_bit_size(instr->dest.dest) < 64);
1176 /* fall through */
1177 case nir_op_fadd:
1178 try_immediate_source(instr, &op[1], devinfo);
1179 inst = emit(ADD(dst, op[0], op[1]));
1180 inst->saturate = instr->dest.saturate;
1181 break;
1182
1183 case nir_op_uadd_sat:
1184 assert(nir_dest_bit_size(instr->dest.dest) < 64);
1185 inst = emit(ADD(dst, op[0], op[1]));
1186 inst->saturate = true;
1187 break;
1188
1189 case nir_op_fmul:
1190 try_immediate_source(instr, &op[1], devinfo);
1191 inst = emit(MUL(dst, op[0], op[1]));
1192 inst->saturate = instr->dest.saturate;
1193 break;
1194
1195 case nir_op_imul: {
1196 assert(nir_dest_bit_size(instr->dest.dest) < 64);
1197 if (devinfo->gen < 8) {
1198 /* For integer multiplication, the MUL uses the low 16 bits of one of
1199 * the operands (src0 through SNB, src1 on IVB and later). The MACH
1200 * accumulates in the contribution of the upper 16 bits of that
1201 * operand. If we can determine that one of the args is in the low
1202 * 16 bits, though, we can just emit a single MUL.
1203 */
1204 if (nir_src_is_const(instr->src[0].src) &&
1205 nir_alu_instr_src_read_mask(instr, 0) == 1 &&
1206 nir_src_comp_as_uint(instr->src[0].src, 0) < (1 << 16)) {
1207 if (devinfo->gen < 7)
1208 emit(MUL(dst, op[0], op[1]));
1209 else
1210 emit(MUL(dst, op[1], op[0]));
1211 } else if (nir_src_is_const(instr->src[1].src) &&
1212 nir_alu_instr_src_read_mask(instr, 1) == 1 &&
1213 nir_src_comp_as_uint(instr->src[1].src, 0) < (1 << 16)) {
1214 if (devinfo->gen < 7)
1215 emit(MUL(dst, op[1], op[0]));
1216 else
1217 emit(MUL(dst, op[0], op[1]));
1218 } else {
1219 struct brw_reg acc = retype(brw_acc_reg(8), dst.type);
1220
1221 emit(MUL(acc, op[0], op[1]));
1222 emit(MACH(dst_null_d(), op[0], op[1]));
1223 emit(MOV(dst, src_reg(acc)));
1224 }
1225 } else {
1226 emit(MUL(dst, op[0], op[1]));
1227 }
1228 break;
1229 }
1230
1231 case nir_op_imul_high:
1232 case nir_op_umul_high: {
1233 assert(nir_dest_bit_size(instr->dest.dest) < 64);
1234 struct brw_reg acc = retype(brw_acc_reg(8), dst.type);
1235
1236 if (devinfo->gen >= 8)
1237 emit(MUL(acc, op[0], retype(op[1], BRW_REGISTER_TYPE_UW)));
1238 else
1239 emit(MUL(acc, op[0], op[1]));
1240
1241 emit(MACH(dst, op[0], op[1]));
1242 break;
1243 }
1244
1245 case nir_op_frcp:
1246 inst = emit_math(SHADER_OPCODE_RCP, dst, op[0]);
1247 inst->saturate = instr->dest.saturate;
1248 break;
1249
1250 case nir_op_fexp2:
1251 inst = emit_math(SHADER_OPCODE_EXP2, dst, op[0]);
1252 inst->saturate = instr->dest.saturate;
1253 break;
1254
1255 case nir_op_flog2:
1256 inst = emit_math(SHADER_OPCODE_LOG2, dst, op[0]);
1257 inst->saturate = instr->dest.saturate;
1258 break;
1259
1260 case nir_op_fsin:
1261 inst = emit_math(SHADER_OPCODE_SIN, dst, op[0]);
1262 inst->saturate = instr->dest.saturate;
1263 break;
1264
1265 case nir_op_fcos:
1266 inst = emit_math(SHADER_OPCODE_COS, dst, op[0]);
1267 inst->saturate = instr->dest.saturate;
1268 break;
1269
1270 case nir_op_idiv:
1271 case nir_op_udiv:
1272 assert(nir_dest_bit_size(instr->dest.dest) < 64);
1273 emit_math(SHADER_OPCODE_INT_QUOTIENT, dst, op[0], op[1]);
1274 break;
1275
1276 case nir_op_umod:
1277 case nir_op_irem:
1278 /* According to the sign table for INT DIV in the Ivy Bridge PRM, it
1279 * appears that our hardware just does the right thing for signed
1280 * remainder.
1281 */
1282 assert(nir_dest_bit_size(instr->dest.dest) < 64);
1283 emit_math(SHADER_OPCODE_INT_REMAINDER, dst, op[0], op[1]);
1284 break;
1285
1286 case nir_op_imod: {
1287 /* Get a regular C-style remainder. If a % b == 0, set the predicate. */
1288 inst = emit_math(SHADER_OPCODE_INT_REMAINDER, dst, op[0], op[1]);
1289
1290 /* Math instructions don't support conditional mod */
1291 inst = emit(MOV(dst_null_d(), src_reg(dst)));
1292 inst->conditional_mod = BRW_CONDITIONAL_NZ;
1293
1294 /* Now, we need to determine if signs of the sources are different.
1295 * When we XOR the sources, the top bit is 0 if they are the same and 1
1296 * if they are different. We can then use a conditional modifier to
1297 * turn that into a predicate. This leads us to an XOR.l instruction.
1298 *
1299 * Technically, according to the PRM, you're not allowed to use .l on a
1300 * XOR instruction. However, emperical experiments and Curro's reading
1301 * of the simulator source both indicate that it's safe.
1302 */
1303 src_reg tmp = src_reg(this, glsl_type::ivec4_type);
1304 inst = emit(XOR(dst_reg(tmp), op[0], op[1]));
1305 inst->predicate = BRW_PREDICATE_NORMAL;
1306 inst->conditional_mod = BRW_CONDITIONAL_L;
1307
1308 /* If the result of the initial remainder operation is non-zero and the
1309 * two sources have different signs, add in a copy of op[1] to get the
1310 * final integer modulus value.
1311 */
1312 inst = emit(ADD(dst, src_reg(dst), op[1]));
1313 inst->predicate = BRW_PREDICATE_NORMAL;
1314 break;
1315 }
1316
1317 case nir_op_ldexp:
1318 unreachable("not reached: should be handled by ldexp_to_arith()");
1319
1320 case nir_op_fsqrt:
1321 inst = emit_math(SHADER_OPCODE_SQRT, dst, op[0]);
1322 inst->saturate = instr->dest.saturate;
1323 break;
1324
1325 case nir_op_frsq:
1326 inst = emit_math(SHADER_OPCODE_RSQ, dst, op[0]);
1327 inst->saturate = instr->dest.saturate;
1328 break;
1329
1330 case nir_op_fpow:
1331 inst = emit_math(SHADER_OPCODE_POW, dst, op[0], op[1]);
1332 inst->saturate = instr->dest.saturate;
1333 break;
1334
1335 case nir_op_uadd_carry: {
1336 assert(nir_dest_bit_size(instr->dest.dest) < 64);
1337 struct brw_reg acc = retype(brw_acc_reg(8), BRW_REGISTER_TYPE_UD);
1338
1339 emit(ADDC(dst_null_ud(), op[0], op[1]));
1340 emit(MOV(dst, src_reg(acc)));
1341 break;
1342 }
1343
1344 case nir_op_usub_borrow: {
1345 assert(nir_dest_bit_size(instr->dest.dest) < 64);
1346 struct brw_reg acc = retype(brw_acc_reg(8), BRW_REGISTER_TYPE_UD);
1347
1348 emit(SUBB(dst_null_ud(), op[0], op[1]));
1349 emit(MOV(dst, src_reg(acc)));
1350 break;
1351 }
1352
1353 case nir_op_ftrunc:
1354 inst = emit(RNDZ(dst, op[0]));
1355 inst->saturate = instr->dest.saturate;
1356 break;
1357
1358 case nir_op_fceil: {
1359 src_reg tmp = src_reg(this, glsl_type::float_type);
1360 tmp.swizzle =
1361 brw_swizzle_for_size(instr->src[0].src.is_ssa ?
1362 instr->src[0].src.ssa->num_components :
1363 instr->src[0].src.reg.reg->num_components);
1364
1365 op[0].negate = !op[0].negate;
1366 emit(RNDD(dst_reg(tmp), op[0]));
1367 tmp.negate = true;
1368 inst = emit(MOV(dst, tmp));
1369 inst->saturate = instr->dest.saturate;
1370 break;
1371 }
1372
1373 case nir_op_ffloor:
1374 inst = emit(RNDD(dst, op[0]));
1375 inst->saturate = instr->dest.saturate;
1376 break;
1377
1378 case nir_op_ffract:
1379 inst = emit(FRC(dst, op[0]));
1380 inst->saturate = instr->dest.saturate;
1381 break;
1382
1383 case nir_op_fround_even:
1384 inst = emit(RNDE(dst, op[0]));
1385 inst->saturate = instr->dest.saturate;
1386 break;
1387
1388 case nir_op_fquantize2f16: {
1389 /* See also vec4_visitor::emit_pack_half_2x16() */
1390 src_reg tmp16 = src_reg(this, glsl_type::uvec4_type);
1391 src_reg tmp32 = src_reg(this, glsl_type::vec4_type);
1392 src_reg zero = src_reg(this, glsl_type::vec4_type);
1393
1394 /* Check for denormal */
1395 src_reg abs_src0 = op[0];
1396 abs_src0.abs = true;
1397 emit(CMP(dst_null_f(), abs_src0, brw_imm_f(ldexpf(1.0, -14)),
1398 BRW_CONDITIONAL_L));
1399 /* Get the appropriately signed zero */
1400 emit(AND(retype(dst_reg(zero), BRW_REGISTER_TYPE_UD),
1401 retype(op[0], BRW_REGISTER_TYPE_UD),
1402 brw_imm_ud(0x80000000)));
1403 /* Do the actual F32 -> F16 -> F32 conversion */
1404 emit(F32TO16(dst_reg(tmp16), op[0]));
1405 emit(F16TO32(dst_reg(tmp32), tmp16));
1406 /* Select that or zero based on normal status */
1407 inst = emit(BRW_OPCODE_SEL, dst, zero, tmp32);
1408 inst->predicate = BRW_PREDICATE_NORMAL;
1409 inst->saturate = instr->dest.saturate;
1410 break;
1411 }
1412
1413 case nir_op_imin:
1414 case nir_op_umin:
1415 assert(nir_dest_bit_size(instr->dest.dest) < 64);
1416 /* fall through */
1417 case nir_op_fmin:
1418 try_immediate_source(instr, &op[1], devinfo);
1419 inst = emit_minmax(BRW_CONDITIONAL_L, dst, op[0], op[1]);
1420 inst->saturate = instr->dest.saturate;
1421 break;
1422
1423 case nir_op_imax:
1424 case nir_op_umax:
1425 assert(nir_dest_bit_size(instr->dest.dest) < 64);
1426 /* fall through */
1427 case nir_op_fmax:
1428 try_immediate_source(instr, &op[1], devinfo);
1429 inst = emit_minmax(BRW_CONDITIONAL_GE, dst, op[0], op[1]);
1430 inst->saturate = instr->dest.saturate;
1431 break;
1432
1433 case nir_op_fddx:
1434 case nir_op_fddx_coarse:
1435 case nir_op_fddx_fine:
1436 case nir_op_fddy:
1437 case nir_op_fddy_coarse:
1438 case nir_op_fddy_fine:
1439 unreachable("derivatives are not valid in vertex shaders");
1440
1441 case nir_op_ilt32:
1442 case nir_op_ult32:
1443 case nir_op_ige32:
1444 case nir_op_uge32:
1445 case nir_op_ieq32:
1446 case nir_op_ine32:
1447 assert(nir_dest_bit_size(instr->dest.dest) < 64);
1448 /* Fallthrough */
1449 case nir_op_flt32:
1450 case nir_op_fge32:
1451 case nir_op_feq32:
1452 case nir_op_fne32: {
1453 enum brw_conditional_mod conditional_mod =
1454 brw_conditional_for_nir_comparison(instr->op);
1455
1456 if (nir_src_bit_size(instr->src[0].src) < 64) {
1457 try_immediate_source(instr, &op[1], devinfo);
1458 emit(CMP(dst, op[0], op[1], conditional_mod));
1459 } else {
1460 /* Produce a 32-bit boolean result from the DF comparison by selecting
1461 * only the low 32-bit in each DF produced. Do this in a temporary
1462 * so we can then move from there to the result using align16 again
1463 * to honor the original writemask.
1464 */
1465 dst_reg temp = dst_reg(this, glsl_type::dvec4_type);
1466 emit(CMP(temp, op[0], op[1], conditional_mod));
1467 dst_reg result = dst_reg(this, glsl_type::bvec4_type);
1468 emit(VEC4_OPCODE_PICK_LOW_32BIT, result, src_reg(temp));
1469 emit(MOV(dst, src_reg(result)));
1470 }
1471 break;
1472 }
1473
1474 case nir_op_b32all_iequal2:
1475 case nir_op_b32all_iequal3:
1476 case nir_op_b32all_iequal4:
1477 assert(nir_dest_bit_size(instr->dest.dest) < 64);
1478 /* Fallthrough */
1479 case nir_op_b32all_fequal2:
1480 case nir_op_b32all_fequal3:
1481 case nir_op_b32all_fequal4: {
1482 unsigned swiz =
1483 brw_swizzle_for_size(nir_op_infos[instr->op].input_sizes[0]);
1484
1485 emit(CMP(dst_null_d(), swizzle(op[0], swiz), swizzle(op[1], swiz),
1486 brw_conditional_for_nir_comparison(instr->op)));
1487 emit(MOV(dst, brw_imm_d(0)));
1488 inst = emit(MOV(dst, brw_imm_d(~0)));
1489 inst->predicate = BRW_PREDICATE_ALIGN16_ALL4H;
1490 break;
1491 }
1492
1493 case nir_op_b32any_inequal2:
1494 case nir_op_b32any_inequal3:
1495 case nir_op_b32any_inequal4:
1496 assert(nir_dest_bit_size(instr->dest.dest) < 64);
1497 /* Fallthrough */
1498 case nir_op_b32any_fnequal2:
1499 case nir_op_b32any_fnequal3:
1500 case nir_op_b32any_fnequal4: {
1501 unsigned swiz =
1502 brw_swizzle_for_size(nir_op_infos[instr->op].input_sizes[0]);
1503
1504 emit(CMP(dst_null_d(), swizzle(op[0], swiz), swizzle(op[1], swiz),
1505 brw_conditional_for_nir_comparison(instr->op)));
1506
1507 emit(MOV(dst, brw_imm_d(0)));
1508 inst = emit(MOV(dst, brw_imm_d(~0)));
1509 inst->predicate = BRW_PREDICATE_ALIGN16_ANY4H;
1510 break;
1511 }
1512
1513 case nir_op_inot:
1514 assert(nir_dest_bit_size(instr->dest.dest) < 64);
1515 if (devinfo->gen >= 8) {
1516 op[0] = resolve_source_modifiers(op[0]);
1517 }
1518 emit(NOT(dst, op[0]));
1519 break;
1520
1521 case nir_op_ixor:
1522 assert(nir_dest_bit_size(instr->dest.dest) < 64);
1523 if (devinfo->gen >= 8) {
1524 op[0] = resolve_source_modifiers(op[0]);
1525 op[1] = resolve_source_modifiers(op[1]);
1526 }
1527 try_immediate_source(instr, &op[1], devinfo);
1528 emit(XOR(dst, op[0], op[1]));
1529 break;
1530
1531 case nir_op_ior:
1532 assert(nir_dest_bit_size(instr->dest.dest) < 64);
1533 if (devinfo->gen >= 8) {
1534 op[0] = resolve_source_modifiers(op[0]);
1535 op[1] = resolve_source_modifiers(op[1]);
1536 }
1537 try_immediate_source(instr, &op[1], devinfo);
1538 emit(OR(dst, op[0], op[1]));
1539 break;
1540
1541 case nir_op_iand:
1542 assert(nir_dest_bit_size(instr->dest.dest) < 64);
1543 if (devinfo->gen >= 8) {
1544 op[0] = resolve_source_modifiers(op[0]);
1545 op[1] = resolve_source_modifiers(op[1]);
1546 }
1547 try_immediate_source(instr, &op[1], devinfo);
1548 emit(AND(dst, op[0], op[1]));
1549 break;
1550
1551 case nir_op_b2i32:
1552 case nir_op_b2f32:
1553 case nir_op_b2f64:
1554 if (nir_dest_bit_size(instr->dest.dest) > 32) {
1555 assert(dst.type == BRW_REGISTER_TYPE_DF);
1556 emit_conversion_to_double(dst, negate(op[0]), false);
1557 } else {
1558 emit(MOV(dst, negate(op[0])));
1559 }
1560 break;
1561
1562 case nir_op_f2b32:
1563 if (nir_src_bit_size(instr->src[0].src) == 64) {
1564 /* We use a MOV with conditional_mod to check if the provided value is
1565 * 0.0. We want this to flush denormalized numbers to zero, so we set a
1566 * source modifier on the source operand to trigger this, as source
1567 * modifiers don't affect the result of the testing against 0.0.
1568 */
1569 src_reg value = op[0];
1570 value.abs = true;
1571 vec4_instruction *inst = emit(MOV(dst_null_df(), value));
1572 inst->conditional_mod = BRW_CONDITIONAL_NZ;
1573
1574 src_reg one = src_reg(this, glsl_type::ivec4_type);
1575 emit(MOV(dst_reg(one), brw_imm_d(~0)));
1576 inst = emit(BRW_OPCODE_SEL, dst, one, brw_imm_d(0));
1577 inst->predicate = BRW_PREDICATE_NORMAL;
1578 } else {
1579 emit(CMP(dst, op[0], brw_imm_f(0.0f), BRW_CONDITIONAL_NZ));
1580 }
1581 break;
1582
1583 case nir_op_i2b32:
1584 emit(CMP(dst, op[0], brw_imm_d(0), BRW_CONDITIONAL_NZ));
1585 break;
1586
1587 case nir_op_fnoise1_1:
1588 case nir_op_fnoise1_2:
1589 case nir_op_fnoise1_3:
1590 case nir_op_fnoise1_4:
1591 case nir_op_fnoise2_1:
1592 case nir_op_fnoise2_2:
1593 case nir_op_fnoise2_3:
1594 case nir_op_fnoise2_4:
1595 case nir_op_fnoise3_1:
1596 case nir_op_fnoise3_2:
1597 case nir_op_fnoise3_3:
1598 case nir_op_fnoise3_4:
1599 case nir_op_fnoise4_1:
1600 case nir_op_fnoise4_2:
1601 case nir_op_fnoise4_3:
1602 case nir_op_fnoise4_4:
1603 unreachable("not reached: should be handled by lower_noise");
1604
1605 case nir_op_unpack_half_2x16_split_x:
1606 case nir_op_unpack_half_2x16_split_y:
1607 case nir_op_pack_half_2x16_split:
1608 unreachable("not reached: should not occur in vertex shader");
1609
1610 case nir_op_unpack_snorm_2x16:
1611 case nir_op_unpack_unorm_2x16:
1612 case nir_op_pack_snorm_2x16:
1613 case nir_op_pack_unorm_2x16:
1614 unreachable("not reached: should be handled by lower_packing_builtins");
1615
1616 case nir_op_pack_uvec4_to_uint:
1617 unreachable("not reached");
1618
1619 case nir_op_pack_uvec2_to_uint: {
1620 dst_reg tmp1 = dst_reg(this, glsl_type::uint_type);
1621 tmp1.writemask = WRITEMASK_X;
1622 op[0].swizzle = BRW_SWIZZLE_YYYY;
1623 emit(SHL(tmp1, op[0], src_reg(brw_imm_ud(16u))));
1624
1625 dst_reg tmp2 = dst_reg(this, glsl_type::uint_type);
1626 tmp2.writemask = WRITEMASK_X;
1627 op[0].swizzle = BRW_SWIZZLE_XXXX;
1628 emit(AND(tmp2, op[0], src_reg(brw_imm_ud(0xffffu))));
1629
1630 emit(OR(dst, src_reg(tmp1), src_reg(tmp2)));
1631 break;
1632 }
1633
1634 case nir_op_pack_64_2x32_split: {
1635 dst_reg result = dst_reg(this, glsl_type::dvec4_type);
1636 dst_reg tmp = dst_reg(this, glsl_type::uvec4_type);
1637 emit(MOV(tmp, retype(op[0], BRW_REGISTER_TYPE_UD)));
1638 emit(VEC4_OPCODE_SET_LOW_32BIT, result, src_reg(tmp));
1639 emit(MOV(tmp, retype(op[1], BRW_REGISTER_TYPE_UD)));
1640 emit(VEC4_OPCODE_SET_HIGH_32BIT, result, src_reg(tmp));
1641 emit(MOV(dst, src_reg(result)));
1642 break;
1643 }
1644
1645 case nir_op_unpack_64_2x32_split_x:
1646 case nir_op_unpack_64_2x32_split_y: {
1647 enum opcode oper = (instr->op == nir_op_unpack_64_2x32_split_x) ?
1648 VEC4_OPCODE_PICK_LOW_32BIT : VEC4_OPCODE_PICK_HIGH_32BIT;
1649 dst_reg tmp = dst_reg(this, glsl_type::dvec4_type);
1650 emit(MOV(tmp, op[0]));
1651 dst_reg tmp2 = dst_reg(this, glsl_type::uvec4_type);
1652 emit(oper, tmp2, src_reg(tmp));
1653 emit(MOV(dst, src_reg(tmp2)));
1654 break;
1655 }
1656
1657 case nir_op_unpack_half_2x16:
1658 /* As NIR does not guarantee that we have a correct swizzle outside the
1659 * boundaries of a vector, and the implementation of emit_unpack_half_2x16
1660 * uses the source operand in an operation with WRITEMASK_Y while our
1661 * source operand has only size 1, it accessed incorrect data producing
1662 * regressions in Piglit. We repeat the swizzle of the first component on the
1663 * rest of components to avoid regressions. In the vec4_visitor IR code path
1664 * this is not needed because the operand has already the correct swizzle.
1665 */
1666 op[0].swizzle = brw_compose_swizzle(BRW_SWIZZLE_XXXX, op[0].swizzle);
1667 emit_unpack_half_2x16(dst, op[0]);
1668 break;
1669
1670 case nir_op_pack_half_2x16:
1671 emit_pack_half_2x16(dst, op[0]);
1672 break;
1673
1674 case nir_op_unpack_unorm_4x8:
1675 assert(nir_dest_bit_size(instr->dest.dest) < 64);
1676 emit_unpack_unorm_4x8(dst, op[0]);
1677 break;
1678
1679 case nir_op_pack_unorm_4x8:
1680 assert(nir_dest_bit_size(instr->dest.dest) < 64);
1681 emit_pack_unorm_4x8(dst, op[0]);
1682 break;
1683
1684 case nir_op_unpack_snorm_4x8:
1685 assert(nir_dest_bit_size(instr->dest.dest) < 64);
1686 emit_unpack_snorm_4x8(dst, op[0]);
1687 break;
1688
1689 case nir_op_pack_snorm_4x8:
1690 assert(nir_dest_bit_size(instr->dest.dest) < 64);
1691 emit_pack_snorm_4x8(dst, op[0]);
1692 break;
1693
1694 case nir_op_bitfield_reverse:
1695 assert(nir_dest_bit_size(instr->dest.dest) < 64);
1696 emit(BFREV(dst, op[0]));
1697 break;
1698
1699 case nir_op_bit_count:
1700 assert(nir_dest_bit_size(instr->dest.dest) < 64);
1701 emit(CBIT(dst, op[0]));
1702 break;
1703
1704 case nir_op_ufind_msb:
1705 assert(nir_dest_bit_size(instr->dest.dest) < 64);
1706 emit_find_msb_using_lzd(vec4_builder(this).at_end(), dst, op[0], false);
1707 break;
1708
1709 case nir_op_ifind_msb: {
1710 assert(nir_dest_bit_size(instr->dest.dest) < 64);
1711 vec4_builder bld = vec4_builder(this).at_end();
1712 src_reg src(dst);
1713
1714 if (devinfo->gen < 7) {
1715 emit_find_msb_using_lzd(bld, dst, op[0], true);
1716 } else {
1717 emit(FBH(retype(dst, BRW_REGISTER_TYPE_UD), op[0]));
1718
1719 /* FBH counts from the MSB side, while GLSL's findMSB() wants the
1720 * count from the LSB side. If FBH didn't return an error
1721 * (0xFFFFFFFF), then subtract the result from 31 to convert the MSB
1722 * count into an LSB count.
1723 */
1724 bld.CMP(dst_null_d(), src, brw_imm_d(-1), BRW_CONDITIONAL_NZ);
1725
1726 inst = bld.ADD(dst, src, brw_imm_d(31));
1727 inst->predicate = BRW_PREDICATE_NORMAL;
1728 inst->src[0].negate = true;
1729 }
1730 break;
1731 }
1732
1733 case nir_op_find_lsb: {
1734 assert(nir_dest_bit_size(instr->dest.dest) < 64);
1735 vec4_builder bld = vec4_builder(this).at_end();
1736
1737 if (devinfo->gen < 7) {
1738 dst_reg temp = bld.vgrf(BRW_REGISTER_TYPE_D);
1739
1740 /* (x & -x) generates a value that consists of only the LSB of x.
1741 * For all powers of 2, findMSB(y) == findLSB(y).
1742 */
1743 src_reg src = src_reg(retype(op[0], BRW_REGISTER_TYPE_D));
1744 src_reg negated_src = src;
1745
1746 /* One must be negated, and the other must be non-negated. It
1747 * doesn't matter which is which.
1748 */
1749 negated_src.negate = true;
1750 src.negate = false;
1751
1752 bld.AND(temp, src, negated_src);
1753 emit_find_msb_using_lzd(bld, dst, src_reg(temp), false);
1754 } else {
1755 bld.FBL(dst, op[0]);
1756 }
1757 break;
1758 }
1759
1760 case nir_op_ubitfield_extract:
1761 case nir_op_ibitfield_extract:
1762 unreachable("should have been lowered");
1763 case nir_op_ubfe:
1764 case nir_op_ibfe:
1765 assert(nir_dest_bit_size(instr->dest.dest) < 64);
1766 op[0] = fix_3src_operand(op[0]);
1767 op[1] = fix_3src_operand(op[1]);
1768 op[2] = fix_3src_operand(op[2]);
1769
1770 emit(BFE(dst, op[2], op[1], op[0]));
1771 break;
1772
1773 case nir_op_bfm:
1774 assert(nir_dest_bit_size(instr->dest.dest) < 64);
1775 emit(BFI1(dst, op[0], op[1]));
1776 break;
1777
1778 case nir_op_bfi:
1779 assert(nir_dest_bit_size(instr->dest.dest) < 64);
1780 op[0] = fix_3src_operand(op[0]);
1781 op[1] = fix_3src_operand(op[1]);
1782 op[2] = fix_3src_operand(op[2]);
1783
1784 emit(BFI2(dst, op[0], op[1], op[2]));
1785 break;
1786
1787 case nir_op_bitfield_insert:
1788 unreachable("not reached: should have been lowered");
1789
1790 case nir_op_fsign:
1791 assert(!instr->dest.saturate);
1792 if (op[0].abs) {
1793 /* Straightforward since the source can be assumed to be either
1794 * strictly >= 0 or strictly <= 0 depending on the setting of the
1795 * negate flag.
1796 */
1797 inst = emit(MOV(dst, op[0]));
1798 inst->conditional_mod = BRW_CONDITIONAL_NZ;
1799
1800 inst = (op[0].negate)
1801 ? emit(MOV(dst, brw_imm_f(-1.0f)))
1802 : emit(MOV(dst, brw_imm_f(1.0f)));
1803 inst->predicate = BRW_PREDICATE_NORMAL;
1804 } else if (type_sz(op[0].type) < 8) {
1805 /* AND(val, 0x80000000) gives the sign bit.
1806 *
1807 * Predicated OR ORs 1.0 (0x3f800000) with the sign bit if val is not
1808 * zero.
1809 */
1810 emit(CMP(dst_null_f(), op[0], brw_imm_f(0.0f), BRW_CONDITIONAL_NZ));
1811
1812 op[0].type = BRW_REGISTER_TYPE_UD;
1813 dst.type = BRW_REGISTER_TYPE_UD;
1814 emit(AND(dst, op[0], brw_imm_ud(0x80000000u)));
1815
1816 inst = emit(OR(dst, src_reg(dst), brw_imm_ud(0x3f800000u)));
1817 inst->predicate = BRW_PREDICATE_NORMAL;
1818 dst.type = BRW_REGISTER_TYPE_F;
1819 } else {
1820 /* For doubles we do the same but we need to consider:
1821 *
1822 * - We use a MOV with conditional_mod instead of a CMP so that we can
1823 * skip loading a 0.0 immediate. We use a source modifier on the
1824 * source of the MOV so that we flush denormalized values to 0.
1825 * Since we want to compare against 0, this won't alter the result.
1826 * - We need to extract the high 32-bit of each DF where the sign
1827 * is stored.
1828 * - We need to produce a DF result.
1829 */
1830
1831 /* Check for zero */
1832 src_reg value = op[0];
1833 value.abs = true;
1834 inst = emit(MOV(dst_null_df(), value));
1835 inst->conditional_mod = BRW_CONDITIONAL_NZ;
1836
1837 /* AND each high 32-bit channel with 0x80000000u */
1838 dst_reg tmp = dst_reg(this, glsl_type::uvec4_type);
1839 emit(VEC4_OPCODE_PICK_HIGH_32BIT, tmp, op[0]);
1840 emit(AND(tmp, src_reg(tmp), brw_imm_ud(0x80000000u)));
1841
1842 /* Add 1.0 to each channel, predicated to skip the cases where the
1843 * channel's value was 0
1844 */
1845 inst = emit(OR(tmp, src_reg(tmp), brw_imm_ud(0x3f800000u)));
1846 inst->predicate = BRW_PREDICATE_NORMAL;
1847
1848 /* Now convert the result from float to double */
1849 emit_conversion_to_double(dst, retype(src_reg(tmp),
1850 BRW_REGISTER_TYPE_F),
1851 false);
1852 }
1853 break;
1854
1855 case nir_op_ishl:
1856 assert(nir_dest_bit_size(instr->dest.dest) < 64);
1857 try_immediate_source(instr, &op[1], devinfo);
1858 emit(SHL(dst, op[0], op[1]));
1859 break;
1860
1861 case nir_op_ishr:
1862 assert(nir_dest_bit_size(instr->dest.dest) < 64);
1863 try_immediate_source(instr, &op[1], devinfo);
1864 emit(ASR(dst, op[0], op[1]));
1865 break;
1866
1867 case nir_op_ushr:
1868 assert(nir_dest_bit_size(instr->dest.dest) < 64);
1869 try_immediate_source(instr, &op[1], devinfo);
1870 emit(SHR(dst, op[0], op[1]));
1871 break;
1872
1873 case nir_op_ffma:
1874 if (type_sz(dst.type) == 8) {
1875 dst_reg mul_dst = dst_reg(this, glsl_type::dvec4_type);
1876 emit(MUL(mul_dst, op[1], op[0]));
1877 inst = emit(ADD(dst, src_reg(mul_dst), op[2]));
1878 inst->saturate = instr->dest.saturate;
1879 } else {
1880 op[0] = fix_3src_operand(op[0]);
1881 op[1] = fix_3src_operand(op[1]);
1882 op[2] = fix_3src_operand(op[2]);
1883
1884 inst = emit(MAD(dst, op[2], op[1], op[0]));
1885 inst->saturate = instr->dest.saturate;
1886 }
1887 break;
1888
1889 case nir_op_flrp:
1890 inst = emit_lrp(dst, op[0], op[1], op[2]);
1891 inst->saturate = instr->dest.saturate;
1892 break;
1893
1894 case nir_op_b32csel:
1895 enum brw_predicate predicate;
1896 if (!optimize_predicate(instr, &predicate)) {
1897 emit(CMP(dst_null_d(), op[0], brw_imm_d(0), BRW_CONDITIONAL_NZ));
1898 switch (dst.writemask) {
1899 case WRITEMASK_X:
1900 predicate = BRW_PREDICATE_ALIGN16_REPLICATE_X;
1901 break;
1902 case WRITEMASK_Y:
1903 predicate = BRW_PREDICATE_ALIGN16_REPLICATE_Y;
1904 break;
1905 case WRITEMASK_Z:
1906 predicate = BRW_PREDICATE_ALIGN16_REPLICATE_Z;
1907 break;
1908 case WRITEMASK_W:
1909 predicate = BRW_PREDICATE_ALIGN16_REPLICATE_W;
1910 break;
1911 default:
1912 predicate = BRW_PREDICATE_NORMAL;
1913 break;
1914 }
1915 }
1916 inst = emit(BRW_OPCODE_SEL, dst, op[1], op[2]);
1917 inst->predicate = predicate;
1918 break;
1919
1920 case nir_op_fdot_replicated2:
1921 inst = emit(BRW_OPCODE_DP2, dst, op[0], op[1]);
1922 inst->saturate = instr->dest.saturate;
1923 break;
1924
1925 case nir_op_fdot_replicated3:
1926 inst = emit(BRW_OPCODE_DP3, dst, op[0], op[1]);
1927 inst->saturate = instr->dest.saturate;
1928 break;
1929
1930 case nir_op_fdot_replicated4:
1931 inst = emit(BRW_OPCODE_DP4, dst, op[0], op[1]);
1932 inst->saturate = instr->dest.saturate;
1933 break;
1934
1935 case nir_op_fdph_replicated:
1936 inst = emit(BRW_OPCODE_DPH, dst, op[0], op[1]);
1937 inst->saturate = instr->dest.saturate;
1938 break;
1939
1940 case nir_op_fdiv:
1941 unreachable("not reached: should be lowered by DIV_TO_MUL_RCP in the compiler");
1942
1943 case nir_op_fmod:
1944 unreachable("not reached: should be lowered by MOD_TO_FLOOR in the compiler");
1945
1946 case nir_op_fsub:
1947 case nir_op_isub:
1948 unreachable("not reached: should be handled by ir_sub_to_add_neg");
1949
1950 default:
1951 unreachable("Unimplemented ALU operation");
1952 }
1953
1954 /* If we need to do a boolean resolve, replace the result with -(x & 1)
1955 * to sign extend the low bit to 0/~0
1956 */
1957 if (devinfo->gen <= 5 &&
1958 (instr->instr.pass_flags & BRW_NIR_BOOLEAN_MASK) ==
1959 BRW_NIR_BOOLEAN_NEEDS_RESOLVE) {
1960 dst_reg masked = dst_reg(this, glsl_type::int_type);
1961 masked.writemask = dst.writemask;
1962 emit(AND(masked, src_reg(dst), brw_imm_d(1)));
1963 src_reg masked_neg = src_reg(masked);
1964 masked_neg.negate = true;
1965 emit(MOV(retype(dst, BRW_REGISTER_TYPE_D), masked_neg));
1966 }
1967 }
1968
1969 void
1970 vec4_visitor::nir_emit_jump(nir_jump_instr *instr)
1971 {
1972 switch (instr->type) {
1973 case nir_jump_break:
1974 emit(BRW_OPCODE_BREAK);
1975 break;
1976
1977 case nir_jump_continue:
1978 emit(BRW_OPCODE_CONTINUE);
1979 break;
1980
1981 case nir_jump_return:
1982 /* fall through */
1983 default:
1984 unreachable("unknown jump");
1985 }
1986 }
1987
1988 static enum ir_texture_opcode
1989 ir_texture_opcode_for_nir_texop(nir_texop texop)
1990 {
1991 enum ir_texture_opcode op;
1992
1993 switch (texop) {
1994 case nir_texop_lod: op = ir_lod; break;
1995 case nir_texop_query_levels: op = ir_query_levels; break;
1996 case nir_texop_texture_samples: op = ir_texture_samples; break;
1997 case nir_texop_tex: op = ir_tex; break;
1998 case nir_texop_tg4: op = ir_tg4; break;
1999 case nir_texop_txb: op = ir_txb; break;
2000 case nir_texop_txd: op = ir_txd; break;
2001 case nir_texop_txf: op = ir_txf; break;
2002 case nir_texop_txf_ms: op = ir_txf_ms; break;
2003 case nir_texop_txl: op = ir_txl; break;
2004 case nir_texop_txs: op = ir_txs; break;
2005 case nir_texop_samples_identical: op = ir_samples_identical; break;
2006 default:
2007 unreachable("unknown texture opcode");
2008 }
2009
2010 return op;
2011 }
2012
2013 static const glsl_type *
2014 glsl_type_for_nir_alu_type(nir_alu_type alu_type,
2015 unsigned components)
2016 {
2017 return glsl_type::get_instance(brw_glsl_base_type_for_nir_type(alu_type),
2018 components, 1);
2019 }
2020
2021 void
2022 vec4_visitor::nir_emit_texture(nir_tex_instr *instr)
2023 {
2024 unsigned texture = instr->texture_index;
2025 unsigned sampler = instr->sampler_index;
2026 src_reg texture_reg = brw_imm_ud(texture);
2027 src_reg sampler_reg = brw_imm_ud(sampler);
2028 src_reg coordinate;
2029 const glsl_type *coord_type = NULL;
2030 src_reg shadow_comparator;
2031 src_reg offset_value;
2032 src_reg lod, lod2;
2033 src_reg sample_index;
2034 src_reg mcs;
2035
2036 const glsl_type *dest_type =
2037 glsl_type_for_nir_alu_type(instr->dest_type,
2038 nir_tex_instr_dest_size(instr));
2039 dst_reg dest = get_nir_dest(instr->dest, instr->dest_type);
2040
2041 /* The hardware requires a LOD for buffer textures */
2042 if (instr->sampler_dim == GLSL_SAMPLER_DIM_BUF)
2043 lod = brw_imm_d(0);
2044
2045 /* Load the texture operation sources */
2046 uint32_t constant_offset = 0;
2047 for (unsigned i = 0; i < instr->num_srcs; i++) {
2048 switch (instr->src[i].src_type) {
2049 case nir_tex_src_comparator:
2050 shadow_comparator = get_nir_src(instr->src[i].src,
2051 BRW_REGISTER_TYPE_F, 1);
2052 break;
2053
2054 case nir_tex_src_coord: {
2055 unsigned src_size = nir_tex_instr_src_size(instr, i);
2056
2057 switch (instr->op) {
2058 case nir_texop_txf:
2059 case nir_texop_txf_ms:
2060 case nir_texop_samples_identical:
2061 coordinate = get_nir_src(instr->src[i].src, BRW_REGISTER_TYPE_D,
2062 src_size);
2063 coord_type = glsl_type::ivec(src_size);
2064 break;
2065
2066 default:
2067 coordinate = get_nir_src(instr->src[i].src, BRW_REGISTER_TYPE_F,
2068 src_size);
2069 coord_type = glsl_type::vec(src_size);
2070 break;
2071 }
2072 break;
2073 }
2074
2075 case nir_tex_src_ddx:
2076 lod = get_nir_src(instr->src[i].src, BRW_REGISTER_TYPE_F,
2077 nir_tex_instr_src_size(instr, i));
2078 break;
2079
2080 case nir_tex_src_ddy:
2081 lod2 = get_nir_src(instr->src[i].src, BRW_REGISTER_TYPE_F,
2082 nir_tex_instr_src_size(instr, i));
2083 break;
2084
2085 case nir_tex_src_lod:
2086 switch (instr->op) {
2087 case nir_texop_txs:
2088 case nir_texop_txf:
2089 lod = get_nir_src(instr->src[i].src, BRW_REGISTER_TYPE_D, 1);
2090 break;
2091
2092 default:
2093 lod = get_nir_src(instr->src[i].src, BRW_REGISTER_TYPE_F, 1);
2094 break;
2095 }
2096 break;
2097
2098 case nir_tex_src_ms_index: {
2099 sample_index = get_nir_src(instr->src[i].src, BRW_REGISTER_TYPE_D, 1);
2100 break;
2101 }
2102
2103 case nir_tex_src_offset:
2104 if (!brw_texture_offset(instr, i, &constant_offset)) {
2105 offset_value =
2106 get_nir_src(instr->src[i].src, BRW_REGISTER_TYPE_D, 2);
2107 }
2108 break;
2109
2110 case nir_tex_src_texture_offset: {
2111 /* Emit code to evaluate the actual indexing expression */
2112 src_reg src = get_nir_src(instr->src[i].src, 1);
2113 src_reg temp(this, glsl_type::uint_type);
2114 emit(ADD(dst_reg(temp), src, brw_imm_ud(texture)));
2115 texture_reg = emit_uniformize(temp);
2116 break;
2117 }
2118
2119 case nir_tex_src_sampler_offset: {
2120 /* Emit code to evaluate the actual indexing expression */
2121 src_reg src = get_nir_src(instr->src[i].src, 1);
2122 src_reg temp(this, glsl_type::uint_type);
2123 emit(ADD(dst_reg(temp), src, brw_imm_ud(sampler)));
2124 sampler_reg = emit_uniformize(temp);
2125 break;
2126 }
2127
2128 case nir_tex_src_projector:
2129 unreachable("Should be lowered by do_lower_texture_projection");
2130
2131 case nir_tex_src_bias:
2132 unreachable("LOD bias is not valid for vertex shaders.\n");
2133
2134 default:
2135 unreachable("unknown texture source");
2136 }
2137 }
2138
2139 if (instr->op == nir_texop_txf_ms ||
2140 instr->op == nir_texop_samples_identical) {
2141 assert(coord_type != NULL);
2142 if (devinfo->gen >= 7 &&
2143 key_tex->compressed_multisample_layout_mask & (1 << texture)) {
2144 mcs = emit_mcs_fetch(coord_type, coordinate, texture_reg);
2145 } else {
2146 mcs = brw_imm_ud(0u);
2147 }
2148 }
2149
2150 /* Stuff the channel select bits in the top of the texture offset */
2151 if (instr->op == nir_texop_tg4) {
2152 if (instr->component == 1 &&
2153 (key_tex->gather_channel_quirk_mask & (1 << texture))) {
2154 /* gather4 sampler is broken for green channel on RG32F --
2155 * we must ask for blue instead.
2156 */
2157 constant_offset |= 2 << 16;
2158 } else {
2159 constant_offset |= instr->component << 16;
2160 }
2161 }
2162
2163 ir_texture_opcode op = ir_texture_opcode_for_nir_texop(instr->op);
2164
2165 emit_texture(op, dest, dest_type, coordinate, instr->coord_components,
2166 shadow_comparator,
2167 lod, lod2, sample_index,
2168 constant_offset, offset_value, mcs,
2169 texture, texture_reg, sampler_reg);
2170 }
2171
2172 void
2173 vec4_visitor::nir_emit_undef(nir_ssa_undef_instr *instr)
2174 {
2175 nir_ssa_values[instr->def.index] =
2176 dst_reg(VGRF, alloc.allocate(DIV_ROUND_UP(instr->def.bit_size, 32)));
2177 }
2178
2179 /* SIMD4x2 64bit data is stored in register space like this:
2180 *
2181 * r0.0:DF x0 y0 z0 w0
2182 * r1.0:DF x1 y1 z1 w1
2183 *
2184 * When we need to write data such as this to memory using 32-bit write
2185 * messages we need to shuffle it in this fashion:
2186 *
2187 * r0.0:DF x0 y0 x1 y1 (to be written at base offset)
2188 * r0.0:DF z0 w0 z1 w1 (to be written at base offset + 16)
2189 *
2190 * We need to do the inverse operation when we read using 32-bit messages,
2191 * which we can do by applying the same exact shuffling on the 64-bit data
2192 * read, only that because the data for each vertex is positioned differently
2193 * we need to apply different channel enables.
2194 *
2195 * This function takes 64bit data and shuffles it as explained above.
2196 *
2197 * The @for_write parameter is used to specify if the shuffling is being done
2198 * for proper SIMD4x2 64-bit data that needs to be shuffled prior to a 32-bit
2199 * write message (for_write = true), or instead we are doing the inverse
2200 * operation and we have just read 64-bit data using a 32-bit messages that we
2201 * need to shuffle to create valid SIMD4x2 64-bit data (for_write = false).
2202 *
2203 * If @block and @ref are non-NULL, then the shuffling is done after @ref,
2204 * otherwise the instructions are emitted normally at the end. The function
2205 * returns the last instruction inserted.
2206 *
2207 * Notice that @src and @dst cannot be the same register.
2208 */
2209 vec4_instruction *
2210 vec4_visitor::shuffle_64bit_data(dst_reg dst, src_reg src, bool for_write,
2211 bblock_t *block, vec4_instruction *ref)
2212 {
2213 assert(type_sz(src.type) == 8);
2214 assert(type_sz(dst.type) == 8);
2215 assert(!regions_overlap(dst, 2 * REG_SIZE, src, 2 * REG_SIZE));
2216 assert(!ref == !block);
2217
2218 const vec4_builder bld = !ref ? vec4_builder(this).at_end() :
2219 vec4_builder(this).at(block, ref->next);
2220
2221 /* Resolve swizzle in src */
2222 vec4_instruction *inst;
2223 if (src.swizzle != BRW_SWIZZLE_XYZW) {
2224 dst_reg data = dst_reg(this, glsl_type::dvec4_type);
2225 inst = bld.MOV(data, src);
2226 src = src_reg(data);
2227 }
2228
2229 /* dst+0.XY = src+0.XY */
2230 inst = bld.group(4, 0).MOV(writemask(dst, WRITEMASK_XY), src);
2231
2232 /* dst+0.ZW = src+1.XY */
2233 inst = bld.group(4, for_write ? 1 : 0)
2234 .MOV(writemask(dst, WRITEMASK_ZW),
2235 swizzle(byte_offset(src, REG_SIZE), BRW_SWIZZLE_XYXY));
2236
2237 /* dst+1.XY = src+0.ZW */
2238 inst = bld.group(4, for_write ? 0 : 1)
2239 .MOV(writemask(byte_offset(dst, REG_SIZE), WRITEMASK_XY),
2240 swizzle(src, BRW_SWIZZLE_ZWZW));
2241
2242 /* dst+1.ZW = src+1.ZW */
2243 inst = bld.group(4, 1)
2244 .MOV(writemask(byte_offset(dst, REG_SIZE), WRITEMASK_ZW),
2245 byte_offset(src, REG_SIZE));
2246
2247 return inst;
2248 }
2249
2250 }