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