nir: Make boolean conversions sized just like the others
[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.u32[i] == instr->value.u32[j]) ||
357 (instr->def.bit_size == 64 &&
358 instr->value.f64[i] == instr->value.f64[j])) {
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.f64[i])));
366 } else {
367 emit(MOV(reg, brw_imm_d(instr->value.i32[i])));
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 brw_mark_surface_used(&prog_data->base, index);
391 } else {
392 surf_index = src_reg(this, glsl_type::uint_type);
393 emit(ADD(dst_reg(surf_index), get_nir_src(instr->src[src], 1),
394 brw_imm_ud(prog_data->base.binding_table.ssbo_start)));
395 surf_index = emit_uniformize(surf_index);
396
397 brw_mark_surface_used(&prog_data->base,
398 prog_data->base.binding_table.ssbo_start +
399 nir->info.num_ssbos - 1);
400 }
401
402 return surf_index;
403 }
404
405 void
406 vec4_visitor::nir_emit_intrinsic(nir_intrinsic_instr *instr)
407 {
408 dst_reg dest;
409 src_reg src;
410
411 switch (instr->intrinsic) {
412
413 case nir_intrinsic_load_input: {
414 /* We set EmitNoIndirectInput for VS */
415 unsigned load_offset = nir_src_as_uint(instr->src[0]);
416
417 dest = get_nir_dest(instr->dest);
418 dest.writemask = brw_writemask_for_size(instr->num_components);
419
420 src = src_reg(ATTR, instr->const_index[0] + load_offset,
421 glsl_type::uvec4_type);
422 src = retype(src, dest.type);
423
424 bool is_64bit = nir_dest_bit_size(instr->dest) == 64;
425 if (is_64bit) {
426 dst_reg tmp = dst_reg(this, glsl_type::dvec4_type);
427 src.swizzle = BRW_SWIZZLE_XYZW;
428 shuffle_64bit_data(tmp, src, false);
429 emit(MOV(dest, src_reg(tmp)));
430 } else {
431 /* Swizzle source based on component layout qualifier */
432 src.swizzle = BRW_SWZ_COMP_INPUT(nir_intrinsic_component(instr));
433 emit(MOV(dest, src));
434 }
435 break;
436 }
437
438 case nir_intrinsic_store_output: {
439 unsigned store_offset = nir_src_as_uint(instr->src[1]);
440 int varying = instr->const_index[0] + store_offset;
441
442 bool is_64bit = nir_src_bit_size(instr->src[0]) == 64;
443 if (is_64bit) {
444 src_reg data;
445 src = get_nir_src(instr->src[0], BRW_REGISTER_TYPE_DF,
446 instr->num_components);
447 data = src_reg(this, glsl_type::dvec4_type);
448 shuffle_64bit_data(dst_reg(data), src, true);
449 src = retype(data, BRW_REGISTER_TYPE_F);
450 } else {
451 src = get_nir_src(instr->src[0], BRW_REGISTER_TYPE_F,
452 instr->num_components);
453 }
454
455 unsigned c = nir_intrinsic_component(instr);
456 output_reg[varying][c] = dst_reg(src);
457 output_num_components[varying][c] = instr->num_components;
458
459 unsigned num_components = instr->num_components;
460 if (is_64bit)
461 num_components *= 2;
462
463 output_reg[varying][c] = dst_reg(src);
464 output_num_components[varying][c] = MIN2(4, num_components);
465
466 if (is_64bit && num_components > 4) {
467 assert(num_components <= 8);
468 output_reg[varying + 1][c] = byte_offset(dst_reg(src), REG_SIZE);
469 output_num_components[varying + 1][c] = num_components - 4;
470 }
471 break;
472 }
473
474 case nir_intrinsic_get_buffer_size: {
475 unsigned ssbo_index = nir_src_is_const(instr->src[0]) ?
476 nir_src_as_uint(instr->src[0]) : 0;
477
478 const unsigned index =
479 prog_data->base.binding_table.ssbo_start + ssbo_index;
480 dst_reg result_dst = get_nir_dest(instr->dest);
481 vec4_instruction *inst = new(mem_ctx)
482 vec4_instruction(SHADER_OPCODE_GET_BUFFER_SIZE, result_dst);
483
484 inst->base_mrf = 2;
485 inst->mlen = 1; /* always at least one */
486 inst->src[1] = brw_imm_ud(index);
487
488 /* MRF for the first parameter */
489 src_reg lod = brw_imm_d(0);
490 int param_base = inst->base_mrf;
491 int writemask = WRITEMASK_X;
492 emit(MOV(dst_reg(MRF, param_base, glsl_type::int_type, writemask), lod));
493
494 emit(inst);
495
496 brw_mark_surface_used(&prog_data->base, index);
497 break;
498 }
499
500 case nir_intrinsic_store_ssbo: {
501 assert(devinfo->gen >= 7);
502
503 /* brw_nir_lower_mem_access_bit_sizes takes care of this */
504 assert(nir_src_bit_size(instr->src[0]) == 32);
505 assert(nir_intrinsic_write_mask(instr) ==
506 (1u << instr->num_components) - 1);
507
508 src_reg surf_index = get_nir_ssbo_intrinsic_index(instr);
509 src_reg offset_reg = retype(get_nir_src_imm(instr->src[2]),
510 BRW_REGISTER_TYPE_UD);
511
512 /* Value */
513 src_reg val_reg = get_nir_src(instr->src[0], BRW_REGISTER_TYPE_F, 4);
514
515 /* IvyBridge does not have a native SIMD4x2 untyped write message so untyped
516 * writes will use SIMD8 mode. In order to hide this and keep symmetry across
517 * typed and untyped messages and across hardware platforms, the
518 * current implementation of the untyped messages will transparently convert
519 * the SIMD4x2 payload into an equivalent SIMD8 payload by transposing it
520 * and enabling only channel X on the SEND instruction.
521 *
522 * The above, works well for full vector writes, but not for partial writes
523 * where we want to write some channels and not others, like when we have
524 * code such as v.xyw = vec3(1,2,4). Because the untyped write messages are
525 * quite restrictive with regards to the channel enables we can configure in
526 * the message descriptor (not all combinations are allowed) we cannot simply
527 * implement these scenarios with a single message while keeping the
528 * aforementioned symmetry in the implementation. For now we de decided that
529 * it is better to keep the symmetry to reduce complexity, so in situations
530 * such as the one described we end up emitting two untyped write messages
531 * (one for xy and another for w).
532 *
533 * The code below packs consecutive channels into a single write message,
534 * detects gaps in the vector write and if needed, sends a second message
535 * with the remaining channels. If in the future we decide that we want to
536 * emit a single message at the expense of losing the symmetry in the
537 * implementation we can:
538 *
539 * 1) For IvyBridge: Only use the red channel of the untyped write SIMD8
540 * message payload. In this mode we can write up to 8 offsets and dwords
541 * to the red channel only (for the two vec4s in the SIMD4x2 execution)
542 * and select which of the 8 channels carry data to write by setting the
543 * appropriate writemask in the dst register of the SEND instruction.
544 * It would require to write a new generator opcode specifically for
545 * IvyBridge since we would need to prepare a SIMD8 payload that could
546 * use any channel, not just X.
547 *
548 * 2) For Haswell+: Simply send a single write message but set the writemask
549 * on the dst of the SEND instruction to select the channels we want to
550 * write. It would require to modify the current messages to receive
551 * and honor the writemask provided.
552 */
553 const vec4_builder bld = vec4_builder(this).at_end()
554 .annotate(current_annotation, base_ir);
555
556 emit_untyped_write(bld, surf_index, offset_reg, val_reg,
557 1 /* dims */, instr->num_components /* size */,
558 BRW_PREDICATE_NONE);
559 break;
560 }
561
562 case nir_intrinsic_load_ssbo: {
563 assert(devinfo->gen >= 7);
564
565 /* brw_nir_lower_mem_access_bit_sizes takes care of this */
566 assert(nir_dest_bit_size(instr->dest) == 32);
567
568 src_reg surf_index = get_nir_ssbo_intrinsic_index(instr);
569 src_reg offset_reg = retype(get_nir_src_imm(instr->src[1]),
570 BRW_REGISTER_TYPE_UD);
571
572 /* Read the vector */
573 const vec4_builder bld = vec4_builder(this).at_end()
574 .annotate(current_annotation, base_ir);
575
576 src_reg read_result = emit_untyped_read(bld, surf_index, offset_reg,
577 1 /* dims */, 4 /* size*/,
578 BRW_PREDICATE_NONE);
579 dst_reg dest = get_nir_dest(instr->dest);
580 read_result.type = dest.type;
581 read_result.swizzle = brw_swizzle_for_size(instr->num_components);
582 emit(MOV(dest, read_result));
583 break;
584 }
585
586 case nir_intrinsic_ssbo_atomic_add: {
587 int op = BRW_AOP_ADD;
588
589 if (nir_src_is_const(instr->src[2])) {
590 int add_val = nir_src_as_int(instr->src[2]);
591 if (add_val == 1)
592 op = BRW_AOP_INC;
593 else if (add_val == -1)
594 op = BRW_AOP_DEC;
595 }
596
597 nir_emit_ssbo_atomic(op, instr);
598 break;
599 }
600 case nir_intrinsic_ssbo_atomic_imin:
601 nir_emit_ssbo_atomic(BRW_AOP_IMIN, instr);
602 break;
603 case nir_intrinsic_ssbo_atomic_umin:
604 nir_emit_ssbo_atomic(BRW_AOP_UMIN, instr);
605 break;
606 case nir_intrinsic_ssbo_atomic_imax:
607 nir_emit_ssbo_atomic(BRW_AOP_IMAX, instr);
608 break;
609 case nir_intrinsic_ssbo_atomic_umax:
610 nir_emit_ssbo_atomic(BRW_AOP_UMAX, instr);
611 break;
612 case nir_intrinsic_ssbo_atomic_and:
613 nir_emit_ssbo_atomic(BRW_AOP_AND, instr);
614 break;
615 case nir_intrinsic_ssbo_atomic_or:
616 nir_emit_ssbo_atomic(BRW_AOP_OR, instr);
617 break;
618 case nir_intrinsic_ssbo_atomic_xor:
619 nir_emit_ssbo_atomic(BRW_AOP_XOR, instr);
620 break;
621 case nir_intrinsic_ssbo_atomic_exchange:
622 nir_emit_ssbo_atomic(BRW_AOP_MOV, instr);
623 break;
624 case nir_intrinsic_ssbo_atomic_comp_swap:
625 nir_emit_ssbo_atomic(BRW_AOP_CMPWR, instr);
626 break;
627
628 case nir_intrinsic_load_vertex_id:
629 unreachable("should be lowered by lower_vertex_id()");
630
631 case nir_intrinsic_load_vertex_id_zero_base:
632 case nir_intrinsic_load_base_vertex:
633 case nir_intrinsic_load_instance_id:
634 case nir_intrinsic_load_base_instance:
635 case nir_intrinsic_load_draw_id:
636 case nir_intrinsic_load_invocation_id:
637 unreachable("should be lowered by brw_nir_lower_vs_inputs()");
638
639 case nir_intrinsic_load_uniform: {
640 /* Offsets are in bytes but they should always be multiples of 4 */
641 assert(nir_intrinsic_base(instr) % 4 == 0);
642
643 dest = get_nir_dest(instr->dest);
644
645 src = src_reg(dst_reg(UNIFORM, nir_intrinsic_base(instr) / 16));
646 src.type = dest.type;
647
648 /* Uniforms don't actually have to be vec4 aligned. In the case that
649 * it isn't, we have to use a swizzle to shift things around. They
650 * do still have the std140 alignment requirement that vec2's have to
651 * be vec2-aligned and vec3's and vec4's have to be vec4-aligned.
652 *
653 * The swizzle also works in the indirect case as the generator adds
654 * the swizzle to the offset for us.
655 */
656 const int type_size = type_sz(src.type);
657 unsigned shift = (nir_intrinsic_base(instr) % 16) / type_size;
658 assert(shift + instr->num_components <= 4);
659
660 if (nir_src_is_const(instr->src[0])) {
661 const unsigned load_offset = nir_src_as_uint(instr->src[0]);
662 /* Offsets are in bytes but they should always be multiples of 4 */
663 assert(load_offset % 4 == 0);
664
665 src.swizzle = brw_swizzle_for_size(instr->num_components);
666 dest.writemask = brw_writemask_for_size(instr->num_components);
667 unsigned offset = load_offset + shift * type_size;
668 src.offset = ROUND_DOWN_TO(offset, 16);
669 shift = (offset % 16) / type_size;
670 assert(shift + instr->num_components <= 4);
671 src.swizzle += BRW_SWIZZLE4(shift, shift, shift, shift);
672
673 emit(MOV(dest, src));
674 } else {
675 /* Uniform arrays are vec4 aligned, because of std140 alignment
676 * rules.
677 */
678 assert(shift == 0);
679
680 src_reg indirect = get_nir_src(instr->src[0], BRW_REGISTER_TYPE_UD, 1);
681
682 /* MOV_INDIRECT is going to stomp the whole thing anyway */
683 dest.writemask = WRITEMASK_XYZW;
684
685 emit(SHADER_OPCODE_MOV_INDIRECT, dest, src,
686 indirect, brw_imm_ud(instr->const_index[1]));
687 }
688 break;
689 }
690
691 case nir_intrinsic_load_ubo: {
692 src_reg surf_index;
693
694 dest = get_nir_dest(instr->dest);
695
696 if (nir_src_is_const(instr->src[0])) {
697 /* The block index is a constant, so just emit the binding table entry
698 * as an immediate.
699 */
700 const unsigned index = prog_data->base.binding_table.ubo_start +
701 nir_src_as_uint(instr->src[0]);
702 surf_index = brw_imm_ud(index);
703 brw_mark_surface_used(&prog_data->base, index);
704 } else {
705 /* The block index is not a constant. Evaluate the index expression
706 * per-channel and add the base UBO index; we have to select a value
707 * from any live channel.
708 */
709 surf_index = src_reg(this, glsl_type::uint_type);
710 emit(ADD(dst_reg(surf_index), get_nir_src(instr->src[0], nir_type_int32,
711 instr->num_components),
712 brw_imm_ud(prog_data->base.binding_table.ubo_start)));
713 surf_index = emit_uniformize(surf_index);
714
715 /* Assume this may touch any UBO. It would be nice to provide
716 * a tighter bound, but the array information is already lowered away.
717 */
718 brw_mark_surface_used(&prog_data->base,
719 prog_data->base.binding_table.ubo_start +
720 nir->info.num_ubos - 1);
721 }
722
723 src_reg offset_reg;
724 if (nir_src_is_const(instr->src[1])) {
725 unsigned load_offset = nir_src_as_uint(instr->src[1]);
726 offset_reg = brw_imm_ud(load_offset & ~15);
727 } else {
728 offset_reg = src_reg(this, glsl_type::uint_type);
729 emit(MOV(dst_reg(offset_reg),
730 get_nir_src(instr->src[1], nir_type_uint32, 1)));
731 }
732
733 src_reg packed_consts;
734 if (nir_dest_bit_size(instr->dest) == 32) {
735 packed_consts = src_reg(this, glsl_type::vec4_type);
736 emit_pull_constant_load_reg(dst_reg(packed_consts),
737 surf_index,
738 offset_reg,
739 NULL, NULL /* before_block/inst */);
740 } else {
741 src_reg temp = src_reg(this, glsl_type::dvec4_type);
742 src_reg temp_float = retype(temp, BRW_REGISTER_TYPE_F);
743
744 emit_pull_constant_load_reg(dst_reg(temp_float),
745 surf_index, offset_reg, NULL, NULL);
746 if (offset_reg.file == IMM)
747 offset_reg.ud += 16;
748 else
749 emit(ADD(dst_reg(offset_reg), offset_reg, brw_imm_ud(16u)));
750 emit_pull_constant_load_reg(dst_reg(byte_offset(temp_float, REG_SIZE)),
751 surf_index, offset_reg, NULL, NULL);
752
753 packed_consts = src_reg(this, glsl_type::dvec4_type);
754 shuffle_64bit_data(dst_reg(packed_consts), temp, false);
755 }
756
757 packed_consts.swizzle = brw_swizzle_for_size(instr->num_components);
758 if (nir_src_is_const(instr->src[1])) {
759 unsigned load_offset = nir_src_as_uint(instr->src[1]);
760 unsigned type_size = type_sz(dest.type);
761 packed_consts.swizzle +=
762 BRW_SWIZZLE4(load_offset % 16 / type_size,
763 load_offset % 16 / type_size,
764 load_offset % 16 / type_size,
765 load_offset % 16 / type_size);
766 }
767
768 emit(MOV(dest, retype(packed_consts, dest.type)));
769
770 break;
771 }
772
773 case nir_intrinsic_memory_barrier: {
774 const vec4_builder bld =
775 vec4_builder(this).at_end().annotate(current_annotation, base_ir);
776 const dst_reg tmp = bld.vgrf(BRW_REGISTER_TYPE_UD, 2);
777 bld.emit(SHADER_OPCODE_MEMORY_FENCE, tmp)
778 ->size_written = 2 * REG_SIZE;
779 break;
780 }
781
782 case nir_intrinsic_shader_clock: {
783 /* We cannot do anything if there is an event, so ignore it for now */
784 const src_reg shader_clock = get_timestamp();
785 const enum brw_reg_type type = brw_type_for_base_type(glsl_type::uvec2_type);
786
787 dest = get_nir_dest(instr->dest, type);
788 emit(MOV(dest, shader_clock));
789 break;
790 }
791
792 default:
793 unreachable("Unknown intrinsic");
794 }
795 }
796
797 void
798 vec4_visitor::nir_emit_ssbo_atomic(int op, nir_intrinsic_instr *instr)
799 {
800 dst_reg dest;
801 if (nir_intrinsic_infos[instr->intrinsic].has_dest)
802 dest = get_nir_dest(instr->dest);
803
804 src_reg surface = get_nir_ssbo_intrinsic_index(instr);
805 src_reg offset = get_nir_src(instr->src[1], 1);
806 src_reg data1;
807 if (op != BRW_AOP_INC && op != BRW_AOP_DEC && op != BRW_AOP_PREDEC)
808 data1 = get_nir_src(instr->src[2], 1);
809 src_reg data2;
810 if (op == BRW_AOP_CMPWR)
811 data2 = get_nir_src(instr->src[3], 1);
812
813 /* Emit the actual atomic operation operation */
814 const vec4_builder bld =
815 vec4_builder(this).at_end().annotate(current_annotation, base_ir);
816
817 src_reg atomic_result = emit_untyped_atomic(bld, surface, offset,
818 data1, data2,
819 1 /* dims */, 1 /* rsize */,
820 op,
821 BRW_PREDICATE_NONE);
822 dest.type = atomic_result.type;
823 bld.MOV(dest, atomic_result);
824 }
825
826 static unsigned
827 brw_swizzle_for_nir_swizzle(uint8_t swizzle[4])
828 {
829 return BRW_SWIZZLE4(swizzle[0], swizzle[1], swizzle[2], swizzle[3]);
830 }
831
832 static enum brw_conditional_mod
833 brw_conditional_for_nir_comparison(nir_op op)
834 {
835 switch (op) {
836 case nir_op_flt:
837 case nir_op_ilt:
838 case nir_op_ult:
839 return BRW_CONDITIONAL_L;
840
841 case nir_op_fge:
842 case nir_op_ige:
843 case nir_op_uge:
844 return BRW_CONDITIONAL_GE;
845
846 case nir_op_feq:
847 case nir_op_ieq:
848 case nir_op_ball_fequal2:
849 case nir_op_ball_iequal2:
850 case nir_op_ball_fequal3:
851 case nir_op_ball_iequal3:
852 case nir_op_ball_fequal4:
853 case nir_op_ball_iequal4:
854 return BRW_CONDITIONAL_Z;
855
856 case nir_op_fne:
857 case nir_op_ine:
858 case nir_op_bany_fnequal2:
859 case nir_op_bany_inequal2:
860 case nir_op_bany_fnequal3:
861 case nir_op_bany_inequal3:
862 case nir_op_bany_fnequal4:
863 case nir_op_bany_inequal4:
864 return BRW_CONDITIONAL_NZ;
865
866 default:
867 unreachable("not reached: bad operation for comparison");
868 }
869 }
870
871 bool
872 vec4_visitor::optimize_predicate(nir_alu_instr *instr,
873 enum brw_predicate *predicate)
874 {
875 if (!instr->src[0].src.is_ssa ||
876 instr->src[0].src.ssa->parent_instr->type != nir_instr_type_alu)
877 return false;
878
879 nir_alu_instr *cmp_instr =
880 nir_instr_as_alu(instr->src[0].src.ssa->parent_instr);
881
882 switch (cmp_instr->op) {
883 case nir_op_bany_fnequal2:
884 case nir_op_bany_inequal2:
885 case nir_op_bany_fnequal3:
886 case nir_op_bany_inequal3:
887 case nir_op_bany_fnequal4:
888 case nir_op_bany_inequal4:
889 *predicate = BRW_PREDICATE_ALIGN16_ANY4H;
890 break;
891 case nir_op_ball_fequal2:
892 case nir_op_ball_iequal2:
893 case nir_op_ball_fequal3:
894 case nir_op_ball_iequal3:
895 case nir_op_ball_fequal4:
896 case nir_op_ball_iequal4:
897 *predicate = BRW_PREDICATE_ALIGN16_ALL4H;
898 break;
899 default:
900 return false;
901 }
902
903 unsigned size_swizzle =
904 brw_swizzle_for_size(nir_op_infos[cmp_instr->op].input_sizes[0]);
905
906 src_reg op[2];
907 assert(nir_op_infos[cmp_instr->op].num_inputs == 2);
908 for (unsigned i = 0; i < 2; i++) {
909 nir_alu_type type = nir_op_infos[cmp_instr->op].input_types[i];
910 unsigned bit_size = nir_src_bit_size(cmp_instr->src[i].src);
911 type = (nir_alu_type) (((unsigned) type) | bit_size);
912 op[i] = get_nir_src(cmp_instr->src[i].src, type, 4);
913 unsigned base_swizzle =
914 brw_swizzle_for_nir_swizzle(cmp_instr->src[i].swizzle);
915 op[i].swizzle = brw_compose_swizzle(size_swizzle, base_swizzle);
916 op[i].abs = cmp_instr->src[i].abs;
917 op[i].negate = cmp_instr->src[i].negate;
918 }
919
920 emit(CMP(dst_null_d(), op[0], op[1],
921 brw_conditional_for_nir_comparison(cmp_instr->op)));
922
923 return true;
924 }
925
926 static void
927 emit_find_msb_using_lzd(const vec4_builder &bld,
928 const dst_reg &dst,
929 const src_reg &src,
930 bool is_signed)
931 {
932 vec4_instruction *inst;
933 src_reg temp = src;
934
935 if (is_signed) {
936 /* LZD of an absolute value source almost always does the right
937 * thing. There are two problem values:
938 *
939 * * 0x80000000. Since abs(0x80000000) == 0x80000000, LZD returns
940 * 0. However, findMSB(int(0x80000000)) == 30.
941 *
942 * * 0xffffffff. Since abs(0xffffffff) == 1, LZD returns
943 * 31. Section 8.8 (Integer Functions) of the GLSL 4.50 spec says:
944 *
945 * For a value of zero or negative one, -1 will be returned.
946 *
947 * * Negative powers of two. LZD(abs(-(1<<x))) returns x, but
948 * findMSB(-(1<<x)) should return x-1.
949 *
950 * For all negative number cases, including 0x80000000 and
951 * 0xffffffff, the correct value is obtained from LZD if instead of
952 * negating the (already negative) value the logical-not is used. A
953 * conditonal logical-not can be achieved in two instructions.
954 */
955 temp = src_reg(bld.vgrf(BRW_REGISTER_TYPE_D));
956
957 bld.ASR(dst_reg(temp), src, brw_imm_d(31));
958 bld.XOR(dst_reg(temp), temp, src);
959 }
960
961 bld.LZD(retype(dst, BRW_REGISTER_TYPE_UD),
962 retype(temp, BRW_REGISTER_TYPE_UD));
963
964 /* LZD counts from the MSB side, while GLSL's findMSB() wants the count
965 * from the LSB side. Subtract the result from 31 to convert the MSB count
966 * into an LSB count. If no bits are set, LZD will return 32. 31-32 = -1,
967 * which is exactly what findMSB() is supposed to return.
968 */
969 inst = bld.ADD(dst, retype(src_reg(dst), BRW_REGISTER_TYPE_D),
970 brw_imm_d(31));
971 inst->src[0].negate = true;
972 }
973
974 void
975 vec4_visitor::emit_conversion_from_double(dst_reg dst, src_reg src,
976 bool saturate)
977 {
978 /* BDW PRM vol 15 - workarounds:
979 * DF->f format conversion for Align16 has wrong emask calculation when
980 * source is immediate.
981 */
982 if (devinfo->gen == 8 && dst.type == BRW_REGISTER_TYPE_F &&
983 src.file == BRW_IMMEDIATE_VALUE) {
984 vec4_instruction *inst = emit(MOV(dst, brw_imm_f(src.df)));
985 inst->saturate = saturate;
986 return;
987 }
988
989 enum opcode op;
990 switch (dst.type) {
991 case BRW_REGISTER_TYPE_D:
992 op = VEC4_OPCODE_DOUBLE_TO_D32;
993 break;
994 case BRW_REGISTER_TYPE_UD:
995 op = VEC4_OPCODE_DOUBLE_TO_U32;
996 break;
997 case BRW_REGISTER_TYPE_F:
998 op = VEC4_OPCODE_DOUBLE_TO_F32;
999 break;
1000 default:
1001 unreachable("Unknown conversion");
1002 }
1003
1004 dst_reg temp = dst_reg(this, glsl_type::dvec4_type);
1005 emit(MOV(temp, src));
1006 dst_reg temp2 = dst_reg(this, glsl_type::dvec4_type);
1007 emit(op, temp2, src_reg(temp));
1008
1009 emit(VEC4_OPCODE_PICK_LOW_32BIT, retype(temp2, dst.type), src_reg(temp2));
1010 vec4_instruction *inst = emit(MOV(dst, src_reg(retype(temp2, dst.type))));
1011 inst->saturate = saturate;
1012 }
1013
1014 void
1015 vec4_visitor::emit_conversion_to_double(dst_reg dst, src_reg src,
1016 bool saturate)
1017 {
1018 dst_reg tmp_dst = dst_reg(src_reg(this, glsl_type::dvec4_type));
1019 src_reg tmp_src = retype(src_reg(this, glsl_type::vec4_type), src.type);
1020 emit(MOV(dst_reg(tmp_src), src));
1021 emit(VEC4_OPCODE_TO_DOUBLE, tmp_dst, tmp_src);
1022 vec4_instruction *inst = emit(MOV(dst, src_reg(tmp_dst)));
1023 inst->saturate = saturate;
1024 }
1025
1026 void
1027 vec4_visitor::nir_emit_alu(nir_alu_instr *instr)
1028 {
1029 vec4_instruction *inst;
1030
1031 nir_alu_type dst_type = (nir_alu_type) (nir_op_infos[instr->op].output_type |
1032 nir_dest_bit_size(instr->dest.dest));
1033 dst_reg dst = get_nir_dest(instr->dest.dest, dst_type);
1034 dst.writemask = instr->dest.write_mask;
1035
1036 src_reg op[4];
1037 for (unsigned i = 0; i < nir_op_infos[instr->op].num_inputs; i++) {
1038 nir_alu_type src_type = (nir_alu_type)
1039 (nir_op_infos[instr->op].input_types[i] |
1040 nir_src_bit_size(instr->src[i].src));
1041 op[i] = get_nir_src(instr->src[i].src, src_type, 4);
1042 op[i].swizzle = brw_swizzle_for_nir_swizzle(instr->src[i].swizzle);
1043 op[i].abs = instr->src[i].abs;
1044 op[i].negate = instr->src[i].negate;
1045 }
1046
1047 switch (instr->op) {
1048 case nir_op_imov:
1049 case nir_op_fmov:
1050 inst = emit(MOV(dst, op[0]));
1051 inst->saturate = instr->dest.saturate;
1052 break;
1053
1054 case nir_op_vec2:
1055 case nir_op_vec3:
1056 case nir_op_vec4:
1057 unreachable("not reached: should be handled by lower_vec_to_movs()");
1058
1059 case nir_op_i2f32:
1060 case nir_op_u2f32:
1061 inst = emit(MOV(dst, op[0]));
1062 inst->saturate = instr->dest.saturate;
1063 break;
1064
1065 case nir_op_f2f32:
1066 case nir_op_f2i32:
1067 case nir_op_f2u32:
1068 if (nir_src_bit_size(instr->src[0].src) == 64)
1069 emit_conversion_from_double(dst, op[0], instr->dest.saturate);
1070 else
1071 inst = emit(MOV(dst, op[0]));
1072 break;
1073
1074 case nir_op_f2f64:
1075 case nir_op_i2f64:
1076 case nir_op_u2f64:
1077 emit_conversion_to_double(dst, op[0], instr->dest.saturate);
1078 break;
1079
1080 case nir_op_iadd:
1081 assert(nir_dest_bit_size(instr->dest.dest) < 64);
1082 /* fall through */
1083 case nir_op_fadd:
1084 inst = emit(ADD(dst, op[0], op[1]));
1085 inst->saturate = instr->dest.saturate;
1086 break;
1087
1088 case nir_op_fmul:
1089 inst = emit(MUL(dst, op[0], op[1]));
1090 inst->saturate = instr->dest.saturate;
1091 break;
1092
1093 case nir_op_imul: {
1094 assert(nir_dest_bit_size(instr->dest.dest) < 64);
1095 if (devinfo->gen < 8) {
1096 /* For integer multiplication, the MUL uses the low 16 bits of one of
1097 * the operands (src0 through SNB, src1 on IVB and later). The MACH
1098 * accumulates in the contribution of the upper 16 bits of that
1099 * operand. If we can determine that one of the args is in the low
1100 * 16 bits, though, we can just emit a single MUL.
1101 */
1102 if (nir_src_is_const(instr->src[0].src) &&
1103 nir_alu_instr_src_read_mask(instr, 0) == 1 &&
1104 nir_src_comp_as_uint(instr->src[0].src, 0) < (1 << 16)) {
1105 if (devinfo->gen < 7)
1106 emit(MUL(dst, op[0], op[1]));
1107 else
1108 emit(MUL(dst, op[1], op[0]));
1109 } else if (nir_src_is_const(instr->src[1].src) &&
1110 nir_alu_instr_src_read_mask(instr, 1) == 1 &&
1111 nir_src_comp_as_uint(instr->src[1].src, 0) < (1 << 16)) {
1112 if (devinfo->gen < 7)
1113 emit(MUL(dst, op[1], op[0]));
1114 else
1115 emit(MUL(dst, op[0], op[1]));
1116 } else {
1117 struct brw_reg acc = retype(brw_acc_reg(8), dst.type);
1118
1119 emit(MUL(acc, op[0], op[1]));
1120 emit(MACH(dst_null_d(), op[0], op[1]));
1121 emit(MOV(dst, src_reg(acc)));
1122 }
1123 } else {
1124 emit(MUL(dst, op[0], op[1]));
1125 }
1126 break;
1127 }
1128
1129 case nir_op_imul_high:
1130 case nir_op_umul_high: {
1131 assert(nir_dest_bit_size(instr->dest.dest) < 64);
1132 struct brw_reg acc = retype(brw_acc_reg(8), dst.type);
1133
1134 if (devinfo->gen >= 8)
1135 emit(MUL(acc, op[0], retype(op[1], BRW_REGISTER_TYPE_UW)));
1136 else
1137 emit(MUL(acc, op[0], op[1]));
1138
1139 emit(MACH(dst, op[0], op[1]));
1140 break;
1141 }
1142
1143 case nir_op_frcp:
1144 inst = emit_math(SHADER_OPCODE_RCP, dst, op[0]);
1145 inst->saturate = instr->dest.saturate;
1146 break;
1147
1148 case nir_op_fexp2:
1149 inst = emit_math(SHADER_OPCODE_EXP2, dst, op[0]);
1150 inst->saturate = instr->dest.saturate;
1151 break;
1152
1153 case nir_op_flog2:
1154 inst = emit_math(SHADER_OPCODE_LOG2, dst, op[0]);
1155 inst->saturate = instr->dest.saturate;
1156 break;
1157
1158 case nir_op_fsin:
1159 inst = emit_math(SHADER_OPCODE_SIN, dst, op[0]);
1160 inst->saturate = instr->dest.saturate;
1161 break;
1162
1163 case nir_op_fcos:
1164 inst = emit_math(SHADER_OPCODE_COS, dst, op[0]);
1165 inst->saturate = instr->dest.saturate;
1166 break;
1167
1168 case nir_op_idiv:
1169 case nir_op_udiv:
1170 assert(nir_dest_bit_size(instr->dest.dest) < 64);
1171 emit_math(SHADER_OPCODE_INT_QUOTIENT, dst, op[0], op[1]);
1172 break;
1173
1174 case nir_op_umod:
1175 case nir_op_irem:
1176 /* According to the sign table for INT DIV in the Ivy Bridge PRM, it
1177 * appears that our hardware just does the right thing for signed
1178 * remainder.
1179 */
1180 assert(nir_dest_bit_size(instr->dest.dest) < 64);
1181 emit_math(SHADER_OPCODE_INT_REMAINDER, dst, op[0], op[1]);
1182 break;
1183
1184 case nir_op_imod: {
1185 /* Get a regular C-style remainder. If a % b == 0, set the predicate. */
1186 inst = emit_math(SHADER_OPCODE_INT_REMAINDER, dst, op[0], op[1]);
1187
1188 /* Math instructions don't support conditional mod */
1189 inst = emit(MOV(dst_null_d(), src_reg(dst)));
1190 inst->conditional_mod = BRW_CONDITIONAL_NZ;
1191
1192 /* Now, we need to determine if signs of the sources are different.
1193 * When we XOR the sources, the top bit is 0 if they are the same and 1
1194 * if they are different. We can then use a conditional modifier to
1195 * turn that into a predicate. This leads us to an XOR.l instruction.
1196 *
1197 * Technically, according to the PRM, you're not allowed to use .l on a
1198 * XOR instruction. However, emperical experiments and Curro's reading
1199 * of the simulator source both indicate that it's safe.
1200 */
1201 src_reg tmp = src_reg(this, glsl_type::ivec4_type);
1202 inst = emit(XOR(dst_reg(tmp), op[0], op[1]));
1203 inst->predicate = BRW_PREDICATE_NORMAL;
1204 inst->conditional_mod = BRW_CONDITIONAL_L;
1205
1206 /* If the result of the initial remainder operation is non-zero and the
1207 * two sources have different signs, add in a copy of op[1] to get the
1208 * final integer modulus value.
1209 */
1210 inst = emit(ADD(dst, src_reg(dst), op[1]));
1211 inst->predicate = BRW_PREDICATE_NORMAL;
1212 break;
1213 }
1214
1215 case nir_op_ldexp:
1216 unreachable("not reached: should be handled by ldexp_to_arith()");
1217
1218 case nir_op_fsqrt:
1219 inst = emit_math(SHADER_OPCODE_SQRT, dst, op[0]);
1220 inst->saturate = instr->dest.saturate;
1221 break;
1222
1223 case nir_op_frsq:
1224 inst = emit_math(SHADER_OPCODE_RSQ, dst, op[0]);
1225 inst->saturate = instr->dest.saturate;
1226 break;
1227
1228 case nir_op_fpow:
1229 inst = emit_math(SHADER_OPCODE_POW, dst, op[0], op[1]);
1230 inst->saturate = instr->dest.saturate;
1231 break;
1232
1233 case nir_op_uadd_carry: {
1234 assert(nir_dest_bit_size(instr->dest.dest) < 64);
1235 struct brw_reg acc = retype(brw_acc_reg(8), BRW_REGISTER_TYPE_UD);
1236
1237 emit(ADDC(dst_null_ud(), op[0], op[1]));
1238 emit(MOV(dst, src_reg(acc)));
1239 break;
1240 }
1241
1242 case nir_op_usub_borrow: {
1243 assert(nir_dest_bit_size(instr->dest.dest) < 64);
1244 struct brw_reg acc = retype(brw_acc_reg(8), BRW_REGISTER_TYPE_UD);
1245
1246 emit(SUBB(dst_null_ud(), op[0], op[1]));
1247 emit(MOV(dst, src_reg(acc)));
1248 break;
1249 }
1250
1251 case nir_op_ftrunc:
1252 inst = emit(RNDZ(dst, op[0]));
1253 inst->saturate = instr->dest.saturate;
1254 break;
1255
1256 case nir_op_fceil: {
1257 src_reg tmp = src_reg(this, glsl_type::float_type);
1258 tmp.swizzle =
1259 brw_swizzle_for_size(instr->src[0].src.is_ssa ?
1260 instr->src[0].src.ssa->num_components :
1261 instr->src[0].src.reg.reg->num_components);
1262
1263 op[0].negate = !op[0].negate;
1264 emit(RNDD(dst_reg(tmp), op[0]));
1265 tmp.negate = true;
1266 inst = emit(MOV(dst, tmp));
1267 inst->saturate = instr->dest.saturate;
1268 break;
1269 }
1270
1271 case nir_op_ffloor:
1272 inst = emit(RNDD(dst, op[0]));
1273 inst->saturate = instr->dest.saturate;
1274 break;
1275
1276 case nir_op_ffract:
1277 inst = emit(FRC(dst, op[0]));
1278 inst->saturate = instr->dest.saturate;
1279 break;
1280
1281 case nir_op_fround_even:
1282 inst = emit(RNDE(dst, op[0]));
1283 inst->saturate = instr->dest.saturate;
1284 break;
1285
1286 case nir_op_fquantize2f16: {
1287 /* See also vec4_visitor::emit_pack_half_2x16() */
1288 src_reg tmp16 = src_reg(this, glsl_type::uvec4_type);
1289 src_reg tmp32 = src_reg(this, glsl_type::vec4_type);
1290 src_reg zero = src_reg(this, glsl_type::vec4_type);
1291
1292 /* Check for denormal */
1293 src_reg abs_src0 = op[0];
1294 abs_src0.abs = true;
1295 emit(CMP(dst_null_f(), abs_src0, brw_imm_f(ldexpf(1.0, -14)),
1296 BRW_CONDITIONAL_L));
1297 /* Get the appropriately signed zero */
1298 emit(AND(retype(dst_reg(zero), BRW_REGISTER_TYPE_UD),
1299 retype(op[0], BRW_REGISTER_TYPE_UD),
1300 brw_imm_ud(0x80000000)));
1301 /* Do the actual F32 -> F16 -> F32 conversion */
1302 emit(F32TO16(dst_reg(tmp16), op[0]));
1303 emit(F16TO32(dst_reg(tmp32), tmp16));
1304 /* Select that or zero based on normal status */
1305 inst = emit(BRW_OPCODE_SEL, dst, zero, tmp32);
1306 inst->predicate = BRW_PREDICATE_NORMAL;
1307 inst->saturate = instr->dest.saturate;
1308 break;
1309 }
1310
1311 case nir_op_imin:
1312 case nir_op_umin:
1313 assert(nir_dest_bit_size(instr->dest.dest) < 64);
1314 /* fall through */
1315 case nir_op_fmin:
1316 inst = emit_minmax(BRW_CONDITIONAL_L, dst, op[0], op[1]);
1317 inst->saturate = instr->dest.saturate;
1318 break;
1319
1320 case nir_op_imax:
1321 case nir_op_umax:
1322 assert(nir_dest_bit_size(instr->dest.dest) < 64);
1323 /* fall through */
1324 case nir_op_fmax:
1325 inst = emit_minmax(BRW_CONDITIONAL_GE, dst, op[0], op[1]);
1326 inst->saturate = instr->dest.saturate;
1327 break;
1328
1329 case nir_op_fddx:
1330 case nir_op_fddx_coarse:
1331 case nir_op_fddx_fine:
1332 case nir_op_fddy:
1333 case nir_op_fddy_coarse:
1334 case nir_op_fddy_fine:
1335 unreachable("derivatives are not valid in vertex shaders");
1336
1337 case nir_op_ilt:
1338 case nir_op_ult:
1339 case nir_op_ige:
1340 case nir_op_uge:
1341 case nir_op_ieq:
1342 case nir_op_ine:
1343 assert(nir_dest_bit_size(instr->dest.dest) < 64);
1344 /* Fallthrough */
1345 case nir_op_flt:
1346 case nir_op_fge:
1347 case nir_op_feq:
1348 case nir_op_fne: {
1349 enum brw_conditional_mod conditional_mod =
1350 brw_conditional_for_nir_comparison(instr->op);
1351
1352 if (nir_src_bit_size(instr->src[0].src) < 64) {
1353 emit(CMP(dst, op[0], op[1], conditional_mod));
1354 } else {
1355 /* Produce a 32-bit boolean result from the DF comparison by selecting
1356 * only the low 32-bit in each DF produced. Do this in a temporary
1357 * so we can then move from there to the result using align16 again
1358 * to honor the original writemask.
1359 */
1360 dst_reg temp = dst_reg(this, glsl_type::dvec4_type);
1361 emit(CMP(temp, op[0], op[1], conditional_mod));
1362 dst_reg result = dst_reg(this, glsl_type::bvec4_type);
1363 emit(VEC4_OPCODE_PICK_LOW_32BIT, result, src_reg(temp));
1364 emit(MOV(dst, src_reg(result)));
1365 }
1366 break;
1367 }
1368
1369 case nir_op_ball_iequal2:
1370 case nir_op_ball_iequal3:
1371 case nir_op_ball_iequal4:
1372 assert(nir_dest_bit_size(instr->dest.dest) < 64);
1373 /* Fallthrough */
1374 case nir_op_ball_fequal2:
1375 case nir_op_ball_fequal3:
1376 case nir_op_ball_fequal4: {
1377 unsigned swiz =
1378 brw_swizzle_for_size(nir_op_infos[instr->op].input_sizes[0]);
1379
1380 emit(CMP(dst_null_d(), swizzle(op[0], swiz), swizzle(op[1], swiz),
1381 brw_conditional_for_nir_comparison(instr->op)));
1382 emit(MOV(dst, brw_imm_d(0)));
1383 inst = emit(MOV(dst, brw_imm_d(~0)));
1384 inst->predicate = BRW_PREDICATE_ALIGN16_ALL4H;
1385 break;
1386 }
1387
1388 case nir_op_bany_inequal2:
1389 case nir_op_bany_inequal3:
1390 case nir_op_bany_inequal4:
1391 assert(nir_dest_bit_size(instr->dest.dest) < 64);
1392 /* Fallthrough */
1393 case nir_op_bany_fnequal2:
1394 case nir_op_bany_fnequal3:
1395 case nir_op_bany_fnequal4: {
1396 unsigned swiz =
1397 brw_swizzle_for_size(nir_op_infos[instr->op].input_sizes[0]);
1398
1399 emit(CMP(dst_null_d(), swizzle(op[0], swiz), swizzle(op[1], swiz),
1400 brw_conditional_for_nir_comparison(instr->op)));
1401
1402 emit(MOV(dst, brw_imm_d(0)));
1403 inst = emit(MOV(dst, brw_imm_d(~0)));
1404 inst->predicate = BRW_PREDICATE_ALIGN16_ANY4H;
1405 break;
1406 }
1407
1408 case nir_op_inot:
1409 assert(nir_dest_bit_size(instr->dest.dest) < 64);
1410 if (devinfo->gen >= 8) {
1411 op[0] = resolve_source_modifiers(op[0]);
1412 }
1413 emit(NOT(dst, op[0]));
1414 break;
1415
1416 case nir_op_ixor:
1417 assert(nir_dest_bit_size(instr->dest.dest) < 64);
1418 if (devinfo->gen >= 8) {
1419 op[0] = resolve_source_modifiers(op[0]);
1420 op[1] = resolve_source_modifiers(op[1]);
1421 }
1422 emit(XOR(dst, op[0], op[1]));
1423 break;
1424
1425 case nir_op_ior:
1426 assert(nir_dest_bit_size(instr->dest.dest) < 64);
1427 if (devinfo->gen >= 8) {
1428 op[0] = resolve_source_modifiers(op[0]);
1429 op[1] = resolve_source_modifiers(op[1]);
1430 }
1431 emit(OR(dst, op[0], op[1]));
1432 break;
1433
1434 case nir_op_iand:
1435 assert(nir_dest_bit_size(instr->dest.dest) < 64);
1436 if (devinfo->gen >= 8) {
1437 op[0] = resolve_source_modifiers(op[0]);
1438 op[1] = resolve_source_modifiers(op[1]);
1439 }
1440 emit(AND(dst, op[0], op[1]));
1441 break;
1442
1443 case nir_op_b2i32:
1444 case nir_op_b2f32:
1445 case nir_op_b2f64:
1446 if (nir_dest_bit_size(instr->dest.dest) > 32) {
1447 assert(dst.type == BRW_REGISTER_TYPE_DF);
1448 emit_conversion_to_double(dst, negate(op[0]), false);
1449 } else {
1450 emit(MOV(dst, negate(op[0])));
1451 }
1452 break;
1453
1454 case nir_op_f2b32:
1455 if (nir_src_bit_size(instr->src[0].src) == 64) {
1456 /* We use a MOV with conditional_mod to check if the provided value is
1457 * 0.0. We want this to flush denormalized numbers to zero, so we set a
1458 * source modifier on the source operand to trigger this, as source
1459 * modifiers don't affect the result of the testing against 0.0.
1460 */
1461 src_reg value = op[0];
1462 value.abs = true;
1463 vec4_instruction *inst = emit(MOV(dst_null_df(), value));
1464 inst->conditional_mod = BRW_CONDITIONAL_NZ;
1465
1466 src_reg one = src_reg(this, glsl_type::ivec4_type);
1467 emit(MOV(dst_reg(one), brw_imm_d(~0)));
1468 inst = emit(BRW_OPCODE_SEL, dst, one, brw_imm_d(0));
1469 inst->predicate = BRW_PREDICATE_NORMAL;
1470 } else {
1471 emit(CMP(dst, op[0], brw_imm_f(0.0f), BRW_CONDITIONAL_NZ));
1472 }
1473 break;
1474
1475 case nir_op_i2b32:
1476 emit(CMP(dst, op[0], brw_imm_d(0), BRW_CONDITIONAL_NZ));
1477 break;
1478
1479 case nir_op_fnoise1_1:
1480 case nir_op_fnoise1_2:
1481 case nir_op_fnoise1_3:
1482 case nir_op_fnoise1_4:
1483 case nir_op_fnoise2_1:
1484 case nir_op_fnoise2_2:
1485 case nir_op_fnoise2_3:
1486 case nir_op_fnoise2_4:
1487 case nir_op_fnoise3_1:
1488 case nir_op_fnoise3_2:
1489 case nir_op_fnoise3_3:
1490 case nir_op_fnoise3_4:
1491 case nir_op_fnoise4_1:
1492 case nir_op_fnoise4_2:
1493 case nir_op_fnoise4_3:
1494 case nir_op_fnoise4_4:
1495 unreachable("not reached: should be handled by lower_noise");
1496
1497 case nir_op_unpack_half_2x16_split_x:
1498 case nir_op_unpack_half_2x16_split_y:
1499 case nir_op_pack_half_2x16_split:
1500 unreachable("not reached: should not occur in vertex shader");
1501
1502 case nir_op_unpack_snorm_2x16:
1503 case nir_op_unpack_unorm_2x16:
1504 case nir_op_pack_snorm_2x16:
1505 case nir_op_pack_unorm_2x16:
1506 unreachable("not reached: should be handled by lower_packing_builtins");
1507
1508 case nir_op_pack_uvec4_to_uint:
1509 unreachable("not reached");
1510
1511 case nir_op_pack_uvec2_to_uint: {
1512 dst_reg tmp1 = dst_reg(this, glsl_type::uint_type);
1513 tmp1.writemask = WRITEMASK_X;
1514 op[0].swizzle = BRW_SWIZZLE_YYYY;
1515 emit(SHL(tmp1, op[0], src_reg(brw_imm_ud(16u))));
1516
1517 dst_reg tmp2 = dst_reg(this, glsl_type::uint_type);
1518 tmp2.writemask = WRITEMASK_X;
1519 op[0].swizzle = BRW_SWIZZLE_XXXX;
1520 emit(AND(tmp2, op[0], src_reg(brw_imm_ud(0xffffu))));
1521
1522 emit(OR(dst, src_reg(tmp1), src_reg(tmp2)));
1523 break;
1524 }
1525
1526 case nir_op_pack_64_2x32_split: {
1527 dst_reg result = dst_reg(this, glsl_type::dvec4_type);
1528 dst_reg tmp = dst_reg(this, glsl_type::uvec4_type);
1529 emit(MOV(tmp, retype(op[0], BRW_REGISTER_TYPE_UD)));
1530 emit(VEC4_OPCODE_SET_LOW_32BIT, result, src_reg(tmp));
1531 emit(MOV(tmp, retype(op[1], BRW_REGISTER_TYPE_UD)));
1532 emit(VEC4_OPCODE_SET_HIGH_32BIT, result, src_reg(tmp));
1533 emit(MOV(dst, src_reg(result)));
1534 break;
1535 }
1536
1537 case nir_op_unpack_64_2x32_split_x:
1538 case nir_op_unpack_64_2x32_split_y: {
1539 enum opcode oper = (instr->op == nir_op_unpack_64_2x32_split_x) ?
1540 VEC4_OPCODE_PICK_LOW_32BIT : VEC4_OPCODE_PICK_HIGH_32BIT;
1541 dst_reg tmp = dst_reg(this, glsl_type::dvec4_type);
1542 emit(MOV(tmp, op[0]));
1543 dst_reg tmp2 = dst_reg(this, glsl_type::uvec4_type);
1544 emit(oper, tmp2, src_reg(tmp));
1545 emit(MOV(dst, src_reg(tmp2)));
1546 break;
1547 }
1548
1549 case nir_op_unpack_half_2x16:
1550 /* As NIR does not guarantee that we have a correct swizzle outside the
1551 * boundaries of a vector, and the implementation of emit_unpack_half_2x16
1552 * uses the source operand in an operation with WRITEMASK_Y while our
1553 * source operand has only size 1, it accessed incorrect data producing
1554 * regressions in Piglit. We repeat the swizzle of the first component on the
1555 * rest of components to avoid regressions. In the vec4_visitor IR code path
1556 * this is not needed because the operand has already the correct swizzle.
1557 */
1558 op[0].swizzle = brw_compose_swizzle(BRW_SWIZZLE_XXXX, op[0].swizzle);
1559 emit_unpack_half_2x16(dst, op[0]);
1560 break;
1561
1562 case nir_op_pack_half_2x16:
1563 emit_pack_half_2x16(dst, op[0]);
1564 break;
1565
1566 case nir_op_unpack_unorm_4x8:
1567 assert(nir_dest_bit_size(instr->dest.dest) < 64);
1568 emit_unpack_unorm_4x8(dst, op[0]);
1569 break;
1570
1571 case nir_op_pack_unorm_4x8:
1572 assert(nir_dest_bit_size(instr->dest.dest) < 64);
1573 emit_pack_unorm_4x8(dst, op[0]);
1574 break;
1575
1576 case nir_op_unpack_snorm_4x8:
1577 assert(nir_dest_bit_size(instr->dest.dest) < 64);
1578 emit_unpack_snorm_4x8(dst, op[0]);
1579 break;
1580
1581 case nir_op_pack_snorm_4x8:
1582 assert(nir_dest_bit_size(instr->dest.dest) < 64);
1583 emit_pack_snorm_4x8(dst, op[0]);
1584 break;
1585
1586 case nir_op_bitfield_reverse:
1587 assert(nir_dest_bit_size(instr->dest.dest) < 64);
1588 emit(BFREV(dst, op[0]));
1589 break;
1590
1591 case nir_op_bit_count:
1592 assert(nir_dest_bit_size(instr->dest.dest) < 64);
1593 emit(CBIT(dst, op[0]));
1594 break;
1595
1596 case nir_op_ufind_msb:
1597 assert(nir_dest_bit_size(instr->dest.dest) < 64);
1598 emit_find_msb_using_lzd(vec4_builder(this).at_end(), dst, op[0], false);
1599 break;
1600
1601 case nir_op_ifind_msb: {
1602 assert(nir_dest_bit_size(instr->dest.dest) < 64);
1603 vec4_builder bld = vec4_builder(this).at_end();
1604 src_reg src(dst);
1605
1606 if (devinfo->gen < 7) {
1607 emit_find_msb_using_lzd(bld, dst, op[0], true);
1608 } else {
1609 emit(FBH(retype(dst, BRW_REGISTER_TYPE_UD), op[0]));
1610
1611 /* FBH counts from the MSB side, while GLSL's findMSB() wants the
1612 * count from the LSB side. If FBH didn't return an error
1613 * (0xFFFFFFFF), then subtract the result from 31 to convert the MSB
1614 * count into an LSB count.
1615 */
1616 bld.CMP(dst_null_d(), src, brw_imm_d(-1), BRW_CONDITIONAL_NZ);
1617
1618 inst = bld.ADD(dst, src, brw_imm_d(31));
1619 inst->predicate = BRW_PREDICATE_NORMAL;
1620 inst->src[0].negate = true;
1621 }
1622 break;
1623 }
1624
1625 case nir_op_find_lsb: {
1626 assert(nir_dest_bit_size(instr->dest.dest) < 64);
1627 vec4_builder bld = vec4_builder(this).at_end();
1628
1629 if (devinfo->gen < 7) {
1630 dst_reg temp = bld.vgrf(BRW_REGISTER_TYPE_D);
1631
1632 /* (x & -x) generates a value that consists of only the LSB of x.
1633 * For all powers of 2, findMSB(y) == findLSB(y).
1634 */
1635 src_reg src = src_reg(retype(op[0], BRW_REGISTER_TYPE_D));
1636 src_reg negated_src = src;
1637
1638 /* One must be negated, and the other must be non-negated. It
1639 * doesn't matter which is which.
1640 */
1641 negated_src.negate = true;
1642 src.negate = false;
1643
1644 bld.AND(temp, src, negated_src);
1645 emit_find_msb_using_lzd(bld, dst, src_reg(temp), false);
1646 } else {
1647 bld.FBL(dst, op[0]);
1648 }
1649 break;
1650 }
1651
1652 case nir_op_ubitfield_extract:
1653 case nir_op_ibitfield_extract:
1654 unreachable("should have been lowered");
1655 case nir_op_ubfe:
1656 case nir_op_ibfe:
1657 assert(nir_dest_bit_size(instr->dest.dest) < 64);
1658 op[0] = fix_3src_operand(op[0]);
1659 op[1] = fix_3src_operand(op[1]);
1660 op[2] = fix_3src_operand(op[2]);
1661
1662 emit(BFE(dst, op[2], op[1], op[0]));
1663 break;
1664
1665 case nir_op_bfm:
1666 assert(nir_dest_bit_size(instr->dest.dest) < 64);
1667 emit(BFI1(dst, op[0], op[1]));
1668 break;
1669
1670 case nir_op_bfi:
1671 assert(nir_dest_bit_size(instr->dest.dest) < 64);
1672 op[0] = fix_3src_operand(op[0]);
1673 op[1] = fix_3src_operand(op[1]);
1674 op[2] = fix_3src_operand(op[2]);
1675
1676 emit(BFI2(dst, op[0], op[1], op[2]));
1677 break;
1678
1679 case nir_op_bitfield_insert:
1680 unreachable("not reached: should have been lowered");
1681
1682 case nir_op_fsign:
1683 assert(!instr->dest.saturate);
1684 if (op[0].abs) {
1685 /* Straightforward since the source can be assumed to be either
1686 * strictly >= 0 or strictly <= 0 depending on the setting of the
1687 * negate flag.
1688 */
1689 inst = emit(MOV(dst, op[0]));
1690 inst->conditional_mod = BRW_CONDITIONAL_NZ;
1691
1692 inst = (op[0].negate)
1693 ? emit(MOV(dst, brw_imm_f(-1.0f)))
1694 : emit(MOV(dst, brw_imm_f(1.0f)));
1695 inst->predicate = BRW_PREDICATE_NORMAL;
1696 } else if (type_sz(op[0].type) < 8) {
1697 /* AND(val, 0x80000000) gives the sign bit.
1698 *
1699 * Predicated OR ORs 1.0 (0x3f800000) with the sign bit if val is not
1700 * zero.
1701 */
1702 emit(CMP(dst_null_f(), op[0], brw_imm_f(0.0f), BRW_CONDITIONAL_NZ));
1703
1704 op[0].type = BRW_REGISTER_TYPE_UD;
1705 dst.type = BRW_REGISTER_TYPE_UD;
1706 emit(AND(dst, op[0], brw_imm_ud(0x80000000u)));
1707
1708 inst = emit(OR(dst, src_reg(dst), brw_imm_ud(0x3f800000u)));
1709 inst->predicate = BRW_PREDICATE_NORMAL;
1710 dst.type = BRW_REGISTER_TYPE_F;
1711 } else {
1712 /* For doubles we do the same but we need to consider:
1713 *
1714 * - We use a MOV with conditional_mod instead of a CMP so that we can
1715 * skip loading a 0.0 immediate. We use a source modifier on the
1716 * source of the MOV so that we flush denormalized values to 0.
1717 * Since we want to compare against 0, this won't alter the result.
1718 * - We need to extract the high 32-bit of each DF where the sign
1719 * is stored.
1720 * - We need to produce a DF result.
1721 */
1722
1723 /* Check for zero */
1724 src_reg value = op[0];
1725 value.abs = true;
1726 inst = emit(MOV(dst_null_df(), value));
1727 inst->conditional_mod = BRW_CONDITIONAL_NZ;
1728
1729 /* AND each high 32-bit channel with 0x80000000u */
1730 dst_reg tmp = dst_reg(this, glsl_type::uvec4_type);
1731 emit(VEC4_OPCODE_PICK_HIGH_32BIT, tmp, op[0]);
1732 emit(AND(tmp, src_reg(tmp), brw_imm_ud(0x80000000u)));
1733
1734 /* Add 1.0 to each channel, predicated to skip the cases where the
1735 * channel's value was 0
1736 */
1737 inst = emit(OR(tmp, src_reg(tmp), brw_imm_ud(0x3f800000u)));
1738 inst->predicate = BRW_PREDICATE_NORMAL;
1739
1740 /* Now convert the result from float to double */
1741 emit_conversion_to_double(dst, retype(src_reg(tmp),
1742 BRW_REGISTER_TYPE_F),
1743 false);
1744 }
1745 break;
1746
1747 case nir_op_isign:
1748 /* ASR(val, 31) -> negative val generates 0xffffffff (signed -1).
1749 * -> non-negative val generates 0x00000000.
1750 * Predicated OR sets 1 if val is positive.
1751 */
1752 assert(nir_dest_bit_size(instr->dest.dest) < 64);
1753 emit(CMP(dst_null_d(), op[0], brw_imm_d(0), BRW_CONDITIONAL_G));
1754 emit(ASR(dst, op[0], brw_imm_d(31)));
1755 inst = emit(OR(dst, src_reg(dst), brw_imm_d(1)));
1756 inst->predicate = BRW_PREDICATE_NORMAL;
1757 break;
1758
1759 case nir_op_ishl:
1760 assert(nir_dest_bit_size(instr->dest.dest) < 64);
1761 emit(SHL(dst, op[0], op[1]));
1762 break;
1763
1764 case nir_op_ishr:
1765 assert(nir_dest_bit_size(instr->dest.dest) < 64);
1766 emit(ASR(dst, op[0], op[1]));
1767 break;
1768
1769 case nir_op_ushr:
1770 assert(nir_dest_bit_size(instr->dest.dest) < 64);
1771 emit(SHR(dst, op[0], op[1]));
1772 break;
1773
1774 case nir_op_ffma:
1775 if (type_sz(dst.type) == 8) {
1776 dst_reg mul_dst = dst_reg(this, glsl_type::dvec4_type);
1777 emit(MUL(mul_dst, op[1], op[0]));
1778 inst = emit(ADD(dst, src_reg(mul_dst), op[2]));
1779 inst->saturate = instr->dest.saturate;
1780 } else {
1781 op[0] = fix_3src_operand(op[0]);
1782 op[1] = fix_3src_operand(op[1]);
1783 op[2] = fix_3src_operand(op[2]);
1784
1785 inst = emit(MAD(dst, op[2], op[1], op[0]));
1786 inst->saturate = instr->dest.saturate;
1787 }
1788 break;
1789
1790 case nir_op_flrp:
1791 inst = emit_lrp(dst, op[0], op[1], op[2]);
1792 inst->saturate = instr->dest.saturate;
1793 break;
1794
1795 case nir_op_bcsel:
1796 enum brw_predicate predicate;
1797 if (!optimize_predicate(instr, &predicate)) {
1798 emit(CMP(dst_null_d(), op[0], brw_imm_d(0), BRW_CONDITIONAL_NZ));
1799 switch (dst.writemask) {
1800 case WRITEMASK_X:
1801 predicate = BRW_PREDICATE_ALIGN16_REPLICATE_X;
1802 break;
1803 case WRITEMASK_Y:
1804 predicate = BRW_PREDICATE_ALIGN16_REPLICATE_Y;
1805 break;
1806 case WRITEMASK_Z:
1807 predicate = BRW_PREDICATE_ALIGN16_REPLICATE_Z;
1808 break;
1809 case WRITEMASK_W:
1810 predicate = BRW_PREDICATE_ALIGN16_REPLICATE_W;
1811 break;
1812 default:
1813 predicate = BRW_PREDICATE_NORMAL;
1814 break;
1815 }
1816 }
1817 inst = emit(BRW_OPCODE_SEL, dst, op[1], op[2]);
1818 inst->predicate = predicate;
1819 break;
1820
1821 case nir_op_fdot_replicated2:
1822 inst = emit(BRW_OPCODE_DP2, dst, op[0], op[1]);
1823 inst->saturate = instr->dest.saturate;
1824 break;
1825
1826 case nir_op_fdot_replicated3:
1827 inst = emit(BRW_OPCODE_DP3, dst, op[0], op[1]);
1828 inst->saturate = instr->dest.saturate;
1829 break;
1830
1831 case nir_op_fdot_replicated4:
1832 inst = emit(BRW_OPCODE_DP4, dst, op[0], op[1]);
1833 inst->saturate = instr->dest.saturate;
1834 break;
1835
1836 case nir_op_fdph_replicated:
1837 inst = emit(BRW_OPCODE_DPH, dst, op[0], op[1]);
1838 inst->saturate = instr->dest.saturate;
1839 break;
1840
1841 case nir_op_iabs:
1842 case nir_op_ineg:
1843 assert(nir_dest_bit_size(instr->dest.dest) < 64);
1844 /* fall through */
1845 case nir_op_fabs:
1846 case nir_op_fneg:
1847 case nir_op_fsat:
1848 unreachable("not reached: should be lowered by lower_source mods");
1849
1850 case nir_op_fdiv:
1851 unreachable("not reached: should be lowered by DIV_TO_MUL_RCP in the compiler");
1852
1853 case nir_op_fmod:
1854 unreachable("not reached: should be lowered by MOD_TO_FLOOR in the compiler");
1855
1856 case nir_op_fsub:
1857 case nir_op_isub:
1858 unreachable("not reached: should be handled by ir_sub_to_add_neg");
1859
1860 default:
1861 unreachable("Unimplemented ALU operation");
1862 }
1863
1864 /* If we need to do a boolean resolve, replace the result with -(x & 1)
1865 * to sign extend the low bit to 0/~0
1866 */
1867 if (devinfo->gen <= 5 &&
1868 (instr->instr.pass_flags & BRW_NIR_BOOLEAN_MASK) ==
1869 BRW_NIR_BOOLEAN_NEEDS_RESOLVE) {
1870 dst_reg masked = dst_reg(this, glsl_type::int_type);
1871 masked.writemask = dst.writemask;
1872 emit(AND(masked, src_reg(dst), brw_imm_d(1)));
1873 src_reg masked_neg = src_reg(masked);
1874 masked_neg.negate = true;
1875 emit(MOV(retype(dst, BRW_REGISTER_TYPE_D), masked_neg));
1876 }
1877 }
1878
1879 void
1880 vec4_visitor::nir_emit_jump(nir_jump_instr *instr)
1881 {
1882 switch (instr->type) {
1883 case nir_jump_break:
1884 emit(BRW_OPCODE_BREAK);
1885 break;
1886
1887 case nir_jump_continue:
1888 emit(BRW_OPCODE_CONTINUE);
1889 break;
1890
1891 case nir_jump_return:
1892 /* fall through */
1893 default:
1894 unreachable("unknown jump");
1895 }
1896 }
1897
1898 static enum ir_texture_opcode
1899 ir_texture_opcode_for_nir_texop(nir_texop texop)
1900 {
1901 enum ir_texture_opcode op;
1902
1903 switch (texop) {
1904 case nir_texop_lod: op = ir_lod; break;
1905 case nir_texop_query_levels: op = ir_query_levels; break;
1906 case nir_texop_texture_samples: op = ir_texture_samples; break;
1907 case nir_texop_tex: op = ir_tex; break;
1908 case nir_texop_tg4: op = ir_tg4; break;
1909 case nir_texop_txb: op = ir_txb; break;
1910 case nir_texop_txd: op = ir_txd; break;
1911 case nir_texop_txf: op = ir_txf; break;
1912 case nir_texop_txf_ms: op = ir_txf_ms; break;
1913 case nir_texop_txl: op = ir_txl; break;
1914 case nir_texop_txs: op = ir_txs; break;
1915 case nir_texop_samples_identical: op = ir_samples_identical; break;
1916 default:
1917 unreachable("unknown texture opcode");
1918 }
1919
1920 return op;
1921 }
1922
1923 static const glsl_type *
1924 glsl_type_for_nir_alu_type(nir_alu_type alu_type,
1925 unsigned components)
1926 {
1927 return glsl_type::get_instance(brw_glsl_base_type_for_nir_type(alu_type),
1928 components, 1);
1929 }
1930
1931 void
1932 vec4_visitor::nir_emit_texture(nir_tex_instr *instr)
1933 {
1934 unsigned texture = instr->texture_index;
1935 unsigned sampler = instr->sampler_index;
1936 src_reg texture_reg = brw_imm_ud(texture);
1937 src_reg sampler_reg = brw_imm_ud(sampler);
1938 src_reg coordinate;
1939 const glsl_type *coord_type = NULL;
1940 src_reg shadow_comparator;
1941 src_reg offset_value;
1942 src_reg lod, lod2;
1943 src_reg sample_index;
1944 src_reg mcs;
1945
1946 const glsl_type *dest_type =
1947 glsl_type_for_nir_alu_type(instr->dest_type,
1948 nir_tex_instr_dest_size(instr));
1949 dst_reg dest = get_nir_dest(instr->dest, instr->dest_type);
1950
1951 /* The hardware requires a LOD for buffer textures */
1952 if (instr->sampler_dim == GLSL_SAMPLER_DIM_BUF)
1953 lod = brw_imm_d(0);
1954
1955 /* Load the texture operation sources */
1956 uint32_t constant_offset = 0;
1957 for (unsigned i = 0; i < instr->num_srcs; i++) {
1958 switch (instr->src[i].src_type) {
1959 case nir_tex_src_comparator:
1960 shadow_comparator = get_nir_src(instr->src[i].src,
1961 BRW_REGISTER_TYPE_F, 1);
1962 break;
1963
1964 case nir_tex_src_coord: {
1965 unsigned src_size = nir_tex_instr_src_size(instr, i);
1966
1967 switch (instr->op) {
1968 case nir_texop_txf:
1969 case nir_texop_txf_ms:
1970 case nir_texop_samples_identical:
1971 coordinate = get_nir_src(instr->src[i].src, BRW_REGISTER_TYPE_D,
1972 src_size);
1973 coord_type = glsl_type::ivec(src_size);
1974 break;
1975
1976 default:
1977 coordinate = get_nir_src(instr->src[i].src, BRW_REGISTER_TYPE_F,
1978 src_size);
1979 coord_type = glsl_type::vec(src_size);
1980 break;
1981 }
1982 break;
1983 }
1984
1985 case nir_tex_src_ddx:
1986 lod = get_nir_src(instr->src[i].src, BRW_REGISTER_TYPE_F,
1987 nir_tex_instr_src_size(instr, i));
1988 break;
1989
1990 case nir_tex_src_ddy:
1991 lod2 = get_nir_src(instr->src[i].src, BRW_REGISTER_TYPE_F,
1992 nir_tex_instr_src_size(instr, i));
1993 break;
1994
1995 case nir_tex_src_lod:
1996 switch (instr->op) {
1997 case nir_texop_txs:
1998 case nir_texop_txf:
1999 lod = get_nir_src(instr->src[i].src, BRW_REGISTER_TYPE_D, 1);
2000 break;
2001
2002 default:
2003 lod = get_nir_src(instr->src[i].src, BRW_REGISTER_TYPE_F, 1);
2004 break;
2005 }
2006 break;
2007
2008 case nir_tex_src_ms_index: {
2009 sample_index = get_nir_src(instr->src[i].src, BRW_REGISTER_TYPE_D, 1);
2010 break;
2011 }
2012
2013 case nir_tex_src_offset: {
2014 nir_const_value *const_offset =
2015 nir_src_as_const_value(instr->src[i].src);
2016 assert(nir_src_bit_size(instr->src[i].src) == 32);
2017 if (!const_offset ||
2018 !brw_texture_offset(const_offset->i32,
2019 nir_tex_instr_src_size(instr, i),
2020 &constant_offset)) {
2021 offset_value =
2022 get_nir_src(instr->src[i].src, BRW_REGISTER_TYPE_D, 2);
2023 }
2024 break;
2025 }
2026
2027 case nir_tex_src_texture_offset: {
2028 /* The highest texture which may be used by this operation is
2029 * the last element of the array. Mark it here, because the generator
2030 * doesn't have enough information to determine the bound.
2031 */
2032 uint32_t array_size = instr->texture_array_size;
2033 uint32_t max_used = texture + array_size - 1;
2034 if (instr->op == nir_texop_tg4) {
2035 max_used += prog_data->base.binding_table.gather_texture_start;
2036 } else {
2037 max_used += prog_data->base.binding_table.texture_start;
2038 }
2039
2040 brw_mark_surface_used(&prog_data->base, max_used);
2041
2042 /* Emit code to evaluate the actual indexing expression */
2043 src_reg src = get_nir_src(instr->src[i].src, 1);
2044 src_reg temp(this, glsl_type::uint_type);
2045 emit(ADD(dst_reg(temp), src, brw_imm_ud(texture)));
2046 texture_reg = emit_uniformize(temp);
2047 break;
2048 }
2049
2050 case nir_tex_src_sampler_offset: {
2051 /* Emit code to evaluate the actual indexing expression */
2052 src_reg src = get_nir_src(instr->src[i].src, 1);
2053 src_reg temp(this, glsl_type::uint_type);
2054 emit(ADD(dst_reg(temp), src, brw_imm_ud(sampler)));
2055 sampler_reg = emit_uniformize(temp);
2056 break;
2057 }
2058
2059 case nir_tex_src_projector:
2060 unreachable("Should be lowered by do_lower_texture_projection");
2061
2062 case nir_tex_src_bias:
2063 unreachable("LOD bias is not valid for vertex shaders.\n");
2064
2065 default:
2066 unreachable("unknown texture source");
2067 }
2068 }
2069
2070 if (instr->op == nir_texop_txf_ms ||
2071 instr->op == nir_texop_samples_identical) {
2072 assert(coord_type != NULL);
2073 if (devinfo->gen >= 7 &&
2074 key_tex->compressed_multisample_layout_mask & (1 << texture)) {
2075 mcs = emit_mcs_fetch(coord_type, coordinate, texture_reg);
2076 } else {
2077 mcs = brw_imm_ud(0u);
2078 }
2079 }
2080
2081 /* Stuff the channel select bits in the top of the texture offset */
2082 if (instr->op == nir_texop_tg4) {
2083 if (instr->component == 1 &&
2084 (key_tex->gather_channel_quirk_mask & (1 << texture))) {
2085 /* gather4 sampler is broken for green channel on RG32F --
2086 * we must ask for blue instead.
2087 */
2088 constant_offset |= 2 << 16;
2089 } else {
2090 constant_offset |= instr->component << 16;
2091 }
2092 }
2093
2094 ir_texture_opcode op = ir_texture_opcode_for_nir_texop(instr->op);
2095
2096 emit_texture(op, dest, dest_type, coordinate, instr->coord_components,
2097 shadow_comparator,
2098 lod, lod2, sample_index,
2099 constant_offset, offset_value, mcs,
2100 texture, texture_reg, sampler_reg);
2101 }
2102
2103 void
2104 vec4_visitor::nir_emit_undef(nir_ssa_undef_instr *instr)
2105 {
2106 nir_ssa_values[instr->def.index] =
2107 dst_reg(VGRF, alloc.allocate(DIV_ROUND_UP(instr->def.bit_size, 32)));
2108 }
2109
2110 /* SIMD4x2 64bit data is stored in register space like this:
2111 *
2112 * r0.0:DF x0 y0 z0 w0
2113 * r1.0:DF x1 y1 z1 w1
2114 *
2115 * When we need to write data such as this to memory using 32-bit write
2116 * messages we need to shuffle it in this fashion:
2117 *
2118 * r0.0:DF x0 y0 x1 y1 (to be written at base offset)
2119 * r0.0:DF z0 w0 z1 w1 (to be written at base offset + 16)
2120 *
2121 * We need to do the inverse operation when we read using 32-bit messages,
2122 * which we can do by applying the same exact shuffling on the 64-bit data
2123 * read, only that because the data for each vertex is positioned differently
2124 * we need to apply different channel enables.
2125 *
2126 * This function takes 64bit data and shuffles it as explained above.
2127 *
2128 * The @for_write parameter is used to specify if the shuffling is being done
2129 * for proper SIMD4x2 64-bit data that needs to be shuffled prior to a 32-bit
2130 * write message (for_write = true), or instead we are doing the inverse
2131 * operation and we have just read 64-bit data using a 32-bit messages that we
2132 * need to shuffle to create valid SIMD4x2 64-bit data (for_write = false).
2133 *
2134 * If @block and @ref are non-NULL, then the shuffling is done after @ref,
2135 * otherwise the instructions are emitted normally at the end. The function
2136 * returns the last instruction inserted.
2137 *
2138 * Notice that @src and @dst cannot be the same register.
2139 */
2140 vec4_instruction *
2141 vec4_visitor::shuffle_64bit_data(dst_reg dst, src_reg src, bool for_write,
2142 bblock_t *block, vec4_instruction *ref)
2143 {
2144 assert(type_sz(src.type) == 8);
2145 assert(type_sz(dst.type) == 8);
2146 assert(!regions_overlap(dst, 2 * REG_SIZE, src, 2 * REG_SIZE));
2147 assert(!ref == !block);
2148
2149 const vec4_builder bld = !ref ? vec4_builder(this).at_end() :
2150 vec4_builder(this).at(block, ref->next);
2151
2152 /* Resolve swizzle in src */
2153 vec4_instruction *inst;
2154 if (src.swizzle != BRW_SWIZZLE_XYZW) {
2155 dst_reg data = dst_reg(this, glsl_type::dvec4_type);
2156 inst = bld.MOV(data, src);
2157 src = src_reg(data);
2158 }
2159
2160 /* dst+0.XY = src+0.XY */
2161 inst = bld.group(4, 0).MOV(writemask(dst, WRITEMASK_XY), src);
2162
2163 /* dst+0.ZW = src+1.XY */
2164 inst = bld.group(4, for_write ? 1 : 0)
2165 .MOV(writemask(dst, WRITEMASK_ZW),
2166 swizzle(byte_offset(src, REG_SIZE), BRW_SWIZZLE_XYXY));
2167
2168 /* dst+1.XY = src+0.ZW */
2169 inst = bld.group(4, for_write ? 0 : 1)
2170 .MOV(writemask(byte_offset(dst, REG_SIZE), WRITEMASK_XY),
2171 swizzle(src, BRW_SWIZZLE_ZWZW));
2172
2173 /* dst+1.ZW = src+1.ZW */
2174 inst = bld.group(4, 1)
2175 .MOV(writemask(byte_offset(dst, REG_SIZE), WRITEMASK_ZW),
2176 byte_offset(src, REG_SIZE));
2177
2178 return inst;
2179 }
2180
2181 }