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