intel/eu: Split brw_inst ex_desc accessors for SEND(C) vs. SENDS(C).
[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 dest = get_nir_dest(instr->dest);
631
632 if (nir_src_is_const(instr->src[0])) {
633 /* The block index is a constant, so just emit the binding table entry
634 * as an immediate.
635 */
636 const unsigned index = prog_data->base.binding_table.ubo_start +
637 nir_src_as_uint(instr->src[0]);
638 surf_index = brw_imm_ud(index);
639 } else {
640 /* The block index is not a constant. Evaluate the index expression
641 * per-channel and add the base UBO index; we have to select a value
642 * from any live channel.
643 */
644 surf_index = src_reg(this, glsl_type::uint_type);
645 emit(ADD(dst_reg(surf_index), get_nir_src(instr->src[0], nir_type_int32,
646 instr->num_components),
647 brw_imm_ud(prog_data->base.binding_table.ubo_start)));
648 surf_index = emit_uniformize(surf_index);
649 }
650
651 src_reg offset_reg;
652 if (nir_src_is_const(instr->src[1])) {
653 unsigned load_offset = nir_src_as_uint(instr->src[1]);
654 offset_reg = brw_imm_ud(load_offset & ~15);
655 } else {
656 offset_reg = src_reg(this, glsl_type::uint_type);
657 emit(MOV(dst_reg(offset_reg),
658 get_nir_src(instr->src[1], nir_type_uint32, 1)));
659 }
660
661 src_reg packed_consts;
662 if (nir_dest_bit_size(instr->dest) == 32) {
663 packed_consts = src_reg(this, glsl_type::vec4_type);
664 emit_pull_constant_load_reg(dst_reg(packed_consts),
665 surf_index,
666 offset_reg,
667 NULL, NULL /* before_block/inst */);
668 } else {
669 src_reg temp = src_reg(this, glsl_type::dvec4_type);
670 src_reg temp_float = retype(temp, BRW_REGISTER_TYPE_F);
671
672 emit_pull_constant_load_reg(dst_reg(temp_float),
673 surf_index, offset_reg, NULL, NULL);
674 if (offset_reg.file == IMM)
675 offset_reg.ud += 16;
676 else
677 emit(ADD(dst_reg(offset_reg), offset_reg, brw_imm_ud(16u)));
678 emit_pull_constant_load_reg(dst_reg(byte_offset(temp_float, REG_SIZE)),
679 surf_index, offset_reg, NULL, NULL);
680
681 packed_consts = src_reg(this, glsl_type::dvec4_type);
682 shuffle_64bit_data(dst_reg(packed_consts), temp, false);
683 }
684
685 packed_consts.swizzle = brw_swizzle_for_size(instr->num_components);
686 if (nir_src_is_const(instr->src[1])) {
687 unsigned load_offset = nir_src_as_uint(instr->src[1]);
688 unsigned type_size = type_sz(dest.type);
689 packed_consts.swizzle +=
690 BRW_SWIZZLE4(load_offset % 16 / type_size,
691 load_offset % 16 / type_size,
692 load_offset % 16 / type_size,
693 load_offset % 16 / type_size);
694 }
695
696 emit(MOV(dest, retype(packed_consts, dest.type)));
697
698 break;
699 }
700
701 case nir_intrinsic_memory_barrier: {
702 const vec4_builder bld =
703 vec4_builder(this).at_end().annotate(current_annotation, base_ir);
704 const dst_reg tmp = bld.vgrf(BRW_REGISTER_TYPE_UD, 2);
705 bld.emit(SHADER_OPCODE_MEMORY_FENCE, tmp, brw_vec8_grf(0, 0))
706 ->size_written = 2 * REG_SIZE;
707 break;
708 }
709
710 case nir_intrinsic_shader_clock: {
711 /* We cannot do anything if there is an event, so ignore it for now */
712 const src_reg shader_clock = get_timestamp();
713 const enum brw_reg_type type = brw_type_for_base_type(glsl_type::uvec2_type);
714
715 dest = get_nir_dest(instr->dest, type);
716 emit(MOV(dest, shader_clock));
717 break;
718 }
719
720 default:
721 unreachable("Unknown intrinsic");
722 }
723 }
724
725 void
726 vec4_visitor::nir_emit_ssbo_atomic(int op, nir_intrinsic_instr *instr)
727 {
728 dst_reg dest;
729 if (nir_intrinsic_infos[instr->intrinsic].has_dest)
730 dest = get_nir_dest(instr->dest);
731
732 src_reg surface = get_nir_ssbo_intrinsic_index(instr);
733 src_reg offset = get_nir_src(instr->src[1], 1);
734 src_reg data1;
735 if (op != BRW_AOP_INC && op != BRW_AOP_DEC && op != BRW_AOP_PREDEC)
736 data1 = get_nir_src(instr->src[2], 1);
737 src_reg data2;
738 if (op == BRW_AOP_CMPWR)
739 data2 = get_nir_src(instr->src[3], 1);
740
741 /* Emit the actual atomic operation operation */
742 const vec4_builder bld =
743 vec4_builder(this).at_end().annotate(current_annotation, base_ir);
744
745 src_reg atomic_result = emit_untyped_atomic(bld, surface, offset,
746 data1, data2,
747 1 /* dims */, 1 /* rsize */,
748 op,
749 BRW_PREDICATE_NONE);
750 dest.type = atomic_result.type;
751 bld.MOV(dest, atomic_result);
752 }
753
754 static unsigned
755 brw_swizzle_for_nir_swizzle(uint8_t swizzle[4])
756 {
757 return BRW_SWIZZLE4(swizzle[0], swizzle[1], swizzle[2], swizzle[3]);
758 }
759
760 bool
761 vec4_visitor::optimize_predicate(nir_alu_instr *instr,
762 enum brw_predicate *predicate)
763 {
764 if (!instr->src[0].src.is_ssa ||
765 instr->src[0].src.ssa->parent_instr->type != nir_instr_type_alu)
766 return false;
767
768 nir_alu_instr *cmp_instr =
769 nir_instr_as_alu(instr->src[0].src.ssa->parent_instr);
770
771 switch (cmp_instr->op) {
772 case nir_op_b32any_fnequal2:
773 case nir_op_b32any_inequal2:
774 case nir_op_b32any_fnequal3:
775 case nir_op_b32any_inequal3:
776 case nir_op_b32any_fnequal4:
777 case nir_op_b32any_inequal4:
778 *predicate = BRW_PREDICATE_ALIGN16_ANY4H;
779 break;
780 case nir_op_b32all_fequal2:
781 case nir_op_b32all_iequal2:
782 case nir_op_b32all_fequal3:
783 case nir_op_b32all_iequal3:
784 case nir_op_b32all_fequal4:
785 case nir_op_b32all_iequal4:
786 *predicate = BRW_PREDICATE_ALIGN16_ALL4H;
787 break;
788 default:
789 return false;
790 }
791
792 unsigned size_swizzle =
793 brw_swizzle_for_size(nir_op_infos[cmp_instr->op].input_sizes[0]);
794
795 src_reg op[2];
796 assert(nir_op_infos[cmp_instr->op].num_inputs == 2);
797 for (unsigned i = 0; i < 2; i++) {
798 nir_alu_type type = nir_op_infos[cmp_instr->op].input_types[i];
799 unsigned bit_size = nir_src_bit_size(cmp_instr->src[i].src);
800 type = (nir_alu_type) (((unsigned) type) | bit_size);
801 op[i] = get_nir_src(cmp_instr->src[i].src, type, 4);
802 unsigned base_swizzle =
803 brw_swizzle_for_nir_swizzle(cmp_instr->src[i].swizzle);
804 op[i].swizzle = brw_compose_swizzle(size_swizzle, base_swizzle);
805 op[i].abs = cmp_instr->src[i].abs;
806 op[i].negate = cmp_instr->src[i].negate;
807 }
808
809 emit(CMP(dst_null_d(), op[0], op[1],
810 brw_cmod_for_nir_comparison(cmp_instr->op)));
811
812 return true;
813 }
814
815 static void
816 emit_find_msb_using_lzd(const vec4_builder &bld,
817 const dst_reg &dst,
818 const src_reg &src,
819 bool is_signed)
820 {
821 vec4_instruction *inst;
822 src_reg temp = src;
823
824 if (is_signed) {
825 /* LZD of an absolute value source almost always does the right
826 * thing. There are two problem values:
827 *
828 * * 0x80000000. Since abs(0x80000000) == 0x80000000, LZD returns
829 * 0. However, findMSB(int(0x80000000)) == 30.
830 *
831 * * 0xffffffff. Since abs(0xffffffff) == 1, LZD returns
832 * 31. Section 8.8 (Integer Functions) of the GLSL 4.50 spec says:
833 *
834 * For a value of zero or negative one, -1 will be returned.
835 *
836 * * Negative powers of two. LZD(abs(-(1<<x))) returns x, but
837 * findMSB(-(1<<x)) should return x-1.
838 *
839 * For all negative number cases, including 0x80000000 and
840 * 0xffffffff, the correct value is obtained from LZD if instead of
841 * negating the (already negative) value the logical-not is used. A
842 * conditonal logical-not can be achieved in two instructions.
843 */
844 temp = src_reg(bld.vgrf(BRW_REGISTER_TYPE_D));
845
846 bld.ASR(dst_reg(temp), src, brw_imm_d(31));
847 bld.XOR(dst_reg(temp), temp, src);
848 }
849
850 bld.LZD(retype(dst, BRW_REGISTER_TYPE_UD),
851 retype(temp, BRW_REGISTER_TYPE_UD));
852
853 /* LZD counts from the MSB side, while GLSL's findMSB() wants the count
854 * from the LSB side. Subtract the result from 31 to convert the MSB count
855 * into an LSB count. If no bits are set, LZD will return 32. 31-32 = -1,
856 * which is exactly what findMSB() is supposed to return.
857 */
858 inst = bld.ADD(dst, retype(src_reg(dst), BRW_REGISTER_TYPE_D),
859 brw_imm_d(31));
860 inst->src[0].negate = true;
861 }
862
863 void
864 vec4_visitor::emit_conversion_from_double(dst_reg dst, src_reg src,
865 bool saturate)
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 vec4_instruction *inst = emit(MOV(dst, brw_imm_f(src.df)));
874 inst->saturate = saturate;
875 return;
876 }
877
878 enum opcode op;
879 switch (dst.type) {
880 case BRW_REGISTER_TYPE_D:
881 op = VEC4_OPCODE_DOUBLE_TO_D32;
882 break;
883 case BRW_REGISTER_TYPE_UD:
884 op = VEC4_OPCODE_DOUBLE_TO_U32;
885 break;
886 case BRW_REGISTER_TYPE_F:
887 op = VEC4_OPCODE_DOUBLE_TO_F32;
888 break;
889 default:
890 unreachable("Unknown conversion");
891 }
892
893 dst_reg temp = dst_reg(this, glsl_type::dvec4_type);
894 emit(MOV(temp, src));
895 dst_reg temp2 = dst_reg(this, glsl_type::dvec4_type);
896 emit(op, temp2, src_reg(temp));
897
898 emit(VEC4_OPCODE_PICK_LOW_32BIT, retype(temp2, dst.type), src_reg(temp2));
899 vec4_instruction *inst = emit(MOV(dst, src_reg(retype(temp2, dst.type))));
900 inst->saturate = saturate;
901 }
902
903 void
904 vec4_visitor::emit_conversion_to_double(dst_reg dst, src_reg src,
905 bool saturate)
906 {
907 dst_reg tmp_dst = dst_reg(src_reg(this, glsl_type::dvec4_type));
908 src_reg tmp_src = retype(src_reg(this, glsl_type::vec4_type), src.type);
909 emit(MOV(dst_reg(tmp_src), src));
910 emit(VEC4_OPCODE_TO_DOUBLE, tmp_dst, tmp_src);
911 vec4_instruction *inst = emit(MOV(dst, src_reg(tmp_dst)));
912 inst->saturate = saturate;
913 }
914
915 /**
916 * Try to use an immediate value for a source
917 *
918 * In cases of flow control, constant propagation is sometimes unable to
919 * determine that a register contains a constant value. To work around this,
920 * try to emit a literal as one of the sources. If \c try_src0_also is set,
921 * \c op[0] will also be tried for an immediate value.
922 *
923 * If \c op[0] is modified, the operands will be exchanged so that \c op[1]
924 * will always be the immediate value.
925 *
926 * \return The index of the source that was modified, 0 or 1, if successful.
927 * Otherwise, -1.
928 *
929 * \param op - Operands to the instruction
930 * \param try_src0_also - True if \c op[0] should also be a candidate for
931 * getting an immediate value. This should only be set
932 * for commutative operations.
933 */
934 static int
935 try_immediate_source(const nir_alu_instr *instr, src_reg *op,
936 bool try_src0_also,
937 ASSERTED const gen_device_info *devinfo)
938 {
939 unsigned idx;
940
941 /* MOV should be the only single-source instruction passed to this
942 * function. Any other unary instruction with a constant source should
943 * have been constant-folded away!
944 */
945 assert(nir_op_infos[instr->op].num_inputs > 1 ||
946 instr->op == nir_op_mov);
947
948 if (instr->op != nir_op_mov &&
949 nir_src_bit_size(instr->src[1].src) == 32 &&
950 nir_src_is_const(instr->src[1].src)) {
951 idx = 1;
952 } else if (try_src0_also &&
953 nir_src_bit_size(instr->src[0].src) == 32 &&
954 nir_src_is_const(instr->src[0].src)) {
955 idx = 0;
956 } else {
957 return -1;
958 }
959
960 const enum brw_reg_type old_type = op[idx].type;
961
962 switch (old_type) {
963 case BRW_REGISTER_TYPE_D:
964 case BRW_REGISTER_TYPE_UD: {
965 int first_comp = -1;
966 int d = 0;
967
968 for (unsigned i = 0; i < NIR_MAX_VEC_COMPONENTS; i++) {
969 if (nir_alu_instr_channel_used(instr, idx, i)) {
970 if (first_comp < 0) {
971 first_comp = i;
972 d = nir_src_comp_as_int(instr->src[idx].src,
973 instr->src[idx].swizzle[i]);
974 } else if (d != nir_src_comp_as_int(instr->src[idx].src,
975 instr->src[idx].swizzle[i])) {
976 return -1;
977 }
978 }
979 }
980
981 assert(first_comp >= 0);
982
983 if (op[idx].abs)
984 d = MAX2(-d, d);
985
986 if (op[idx].negate) {
987 /* On Gen8+ a negation source modifier on a logical operation means
988 * something different. Nothing should generate this, so assert that
989 * it does not occur.
990 */
991 assert(devinfo->gen < 8 || (instr->op != nir_op_iand &&
992 instr->op != nir_op_ior &&
993 instr->op != nir_op_ixor));
994 d = -d;
995 }
996
997 op[idx] = retype(src_reg(brw_imm_d(d)), old_type);
998 break;
999 }
1000
1001 case BRW_REGISTER_TYPE_F: {
1002 int first_comp = -1;
1003 float f[4] = { 0.0f, 0.0f, 0.0f, 0.0f };
1004 bool is_scalar = true;
1005
1006 for (unsigned i = 0; i < NIR_MAX_VEC_COMPONENTS; i++) {
1007 if (nir_alu_instr_channel_used(instr, idx, i)) {
1008 f[i] = nir_src_comp_as_float(instr->src[idx].src,
1009 instr->src[idx].swizzle[i]);
1010 if (first_comp < 0) {
1011 first_comp = i;
1012 } else if (f[first_comp] != f[i]) {
1013 is_scalar = false;
1014 }
1015 }
1016 }
1017
1018 if (is_scalar) {
1019 if (op[idx].abs)
1020 f[first_comp] = fabs(f[first_comp]);
1021
1022 if (op[idx].negate)
1023 f[first_comp] = -f[first_comp];
1024
1025 op[idx] = src_reg(brw_imm_f(f[first_comp]));
1026 assert(op[idx].type == old_type);
1027 } else {
1028 uint8_t vf_values[4] = { 0, 0, 0, 0 };
1029
1030 for (unsigned i = 0; i < NIR_MAX_VEC_COMPONENTS; i++) {
1031 if (op[idx].abs)
1032 f[i] = fabs(f[i]);
1033
1034 if (op[idx].negate)
1035 f[i] = -f[i];
1036
1037 const int vf = brw_float_to_vf(f[i]);
1038 if (vf == -1)
1039 return -1;
1040
1041 vf_values[i] = vf;
1042 }
1043
1044 op[idx] = src_reg(brw_imm_vf4(vf_values[0], vf_values[1],
1045 vf_values[2], vf_values[3]));
1046 }
1047 break;
1048 }
1049
1050 default:
1051 unreachable("Non-32bit type.");
1052 }
1053
1054 /* If the instruction has more than one source, the instruction format only
1055 * allows source 1 to be an immediate value. If the immediate value was
1056 * source 0, then the sources must be exchanged.
1057 */
1058 if (idx == 0 && instr->op != nir_op_mov) {
1059 src_reg tmp = op[0];
1060 op[0] = op[1];
1061 op[1] = tmp;
1062 }
1063
1064 return idx;
1065 }
1066
1067 void
1068 vec4_visitor::fix_float_operands(src_reg op[3], nir_alu_instr *instr)
1069 {
1070 bool fixed[3] = { false, false, false };
1071
1072 for (unsigned i = 0; i < 2; i++) {
1073 if (!nir_src_is_const(instr->src[i].src))
1074 continue;
1075
1076 for (unsigned j = i + 1; j < 3; j++) {
1077 if (fixed[j])
1078 continue;
1079
1080 if (!nir_src_is_const(instr->src[j].src))
1081 continue;
1082
1083 if (nir_alu_srcs_equal(instr, instr, i, j)) {
1084 if (!fixed[i])
1085 op[i] = fix_3src_operand(op[i]);
1086
1087 op[j] = op[i];
1088
1089 fixed[i] = true;
1090 fixed[j] = true;
1091 } else if (nir_alu_srcs_negative_equal(instr, instr, i, j)) {
1092 if (!fixed[i])
1093 op[i] = fix_3src_operand(op[i]);
1094
1095 op[j] = op[i];
1096 op[j].negate = !op[j].negate;
1097
1098 fixed[i] = true;
1099 fixed[j] = true;
1100 }
1101 }
1102 }
1103
1104 for (unsigned i = 0; i < 3; i++) {
1105 if (!fixed[i])
1106 op[i] = fix_3src_operand(op[i]);
1107 }
1108 }
1109
1110 void
1111 vec4_visitor::nir_emit_alu(nir_alu_instr *instr)
1112 {
1113 vec4_instruction *inst;
1114
1115 nir_alu_type dst_type = (nir_alu_type) (nir_op_infos[instr->op].output_type |
1116 nir_dest_bit_size(instr->dest.dest));
1117 dst_reg dst = get_nir_dest(instr->dest.dest, dst_type);
1118 dst.writemask = instr->dest.write_mask;
1119
1120 src_reg op[4];
1121 for (unsigned i = 0; i < nir_op_infos[instr->op].num_inputs; i++) {
1122 nir_alu_type src_type = (nir_alu_type)
1123 (nir_op_infos[instr->op].input_types[i] |
1124 nir_src_bit_size(instr->src[i].src));
1125 op[i] = get_nir_src(instr->src[i].src, src_type, 4);
1126 op[i].swizzle = brw_swizzle_for_nir_swizzle(instr->src[i].swizzle);
1127 op[i].abs = instr->src[i].abs;
1128 op[i].negate = instr->src[i].negate;
1129 }
1130
1131 switch (instr->op) {
1132 case nir_op_mov:
1133 try_immediate_source(instr, &op[0], true, devinfo);
1134 inst = emit(MOV(dst, op[0]));
1135 inst->saturate = instr->dest.saturate;
1136 break;
1137
1138 case nir_op_vec2:
1139 case nir_op_vec3:
1140 case nir_op_vec4:
1141 unreachable("not reached: should be handled by lower_vec_to_movs()");
1142
1143 case nir_op_i2f32:
1144 case nir_op_u2f32:
1145 inst = emit(MOV(dst, op[0]));
1146 inst->saturate = instr->dest.saturate;
1147 break;
1148
1149 case nir_op_f2f32:
1150 case nir_op_f2i32:
1151 case nir_op_f2u32:
1152 if (nir_src_bit_size(instr->src[0].src) == 64)
1153 emit_conversion_from_double(dst, op[0], instr->dest.saturate);
1154 else
1155 inst = emit(MOV(dst, op[0]));
1156 break;
1157
1158 case nir_op_f2f64:
1159 case nir_op_i2f64:
1160 case nir_op_u2f64:
1161 emit_conversion_to_double(dst, op[0], instr->dest.saturate);
1162 break;
1163
1164 case nir_op_fsat:
1165 inst = emit(MOV(dst, op[0]));
1166 inst->saturate = true;
1167 break;
1168
1169 case nir_op_fneg:
1170 case nir_op_ineg:
1171 op[0].negate = true;
1172 inst = emit(MOV(dst, op[0]));
1173 if (instr->op == nir_op_fneg)
1174 inst->saturate = instr->dest.saturate;
1175 break;
1176
1177 case nir_op_fabs:
1178 case nir_op_iabs:
1179 op[0].negate = false;
1180 op[0].abs = true;
1181 inst = emit(MOV(dst, op[0]));
1182 if (instr->op == nir_op_fabs)
1183 inst->saturate = instr->dest.saturate;
1184 break;
1185
1186 case nir_op_iadd:
1187 assert(nir_dest_bit_size(instr->dest.dest) < 64);
1188 /* fall through */
1189 case nir_op_fadd:
1190 try_immediate_source(instr, op, true, devinfo);
1191 inst = emit(ADD(dst, op[0], op[1]));
1192 inst->saturate = instr->dest.saturate;
1193 break;
1194
1195 case nir_op_uadd_sat:
1196 assert(nir_dest_bit_size(instr->dest.dest) < 64);
1197 inst = emit(ADD(dst, op[0], op[1]));
1198 inst->saturate = true;
1199 break;
1200
1201 case nir_op_fmul:
1202 try_immediate_source(instr, op, true, devinfo);
1203 inst = emit(MUL(dst, op[0], op[1]));
1204 inst->saturate = instr->dest.saturate;
1205 break;
1206
1207 case nir_op_imul: {
1208 assert(nir_dest_bit_size(instr->dest.dest) < 64);
1209 if (devinfo->gen < 8) {
1210 /* For integer multiplication, the MUL uses the low 16 bits of one of
1211 * the operands (src0 through SNB, src1 on IVB and later). The MACH
1212 * accumulates in the contribution of the upper 16 bits of that
1213 * operand. If we can determine that one of the args is in the low
1214 * 16 bits, though, we can just emit a single MUL.
1215 */
1216 if (nir_src_is_const(instr->src[0].src) &&
1217 nir_alu_instr_src_read_mask(instr, 0) == 1 &&
1218 nir_src_comp_as_uint(instr->src[0].src, 0) < (1 << 16)) {
1219 if (devinfo->gen < 7)
1220 emit(MUL(dst, op[0], op[1]));
1221 else
1222 emit(MUL(dst, op[1], op[0]));
1223 } else if (nir_src_is_const(instr->src[1].src) &&
1224 nir_alu_instr_src_read_mask(instr, 1) == 1 &&
1225 nir_src_comp_as_uint(instr->src[1].src, 0) < (1 << 16)) {
1226 if (devinfo->gen < 7)
1227 emit(MUL(dst, op[1], op[0]));
1228 else
1229 emit(MUL(dst, op[0], op[1]));
1230 } else {
1231 struct brw_reg acc = retype(brw_acc_reg(8), dst.type);
1232
1233 emit(MUL(acc, op[0], op[1]));
1234 emit(MACH(dst_null_d(), op[0], op[1]));
1235 emit(MOV(dst, src_reg(acc)));
1236 }
1237 } else {
1238 emit(MUL(dst, op[0], op[1]));
1239 }
1240 break;
1241 }
1242
1243 case nir_op_imul_high:
1244 case nir_op_umul_high: {
1245 assert(nir_dest_bit_size(instr->dest.dest) < 64);
1246 struct brw_reg acc = retype(brw_acc_reg(8), dst.type);
1247
1248 if (devinfo->gen >= 8)
1249 emit(MUL(acc, op[0], retype(op[1], BRW_REGISTER_TYPE_UW)));
1250 else
1251 emit(MUL(acc, op[0], op[1]));
1252
1253 emit(MACH(dst, op[0], op[1]));
1254 break;
1255 }
1256
1257 case nir_op_frcp:
1258 inst = emit_math(SHADER_OPCODE_RCP, dst, op[0]);
1259 inst->saturate = instr->dest.saturate;
1260 break;
1261
1262 case nir_op_fexp2:
1263 inst = emit_math(SHADER_OPCODE_EXP2, dst, op[0]);
1264 inst->saturate = instr->dest.saturate;
1265 break;
1266
1267 case nir_op_flog2:
1268 inst = emit_math(SHADER_OPCODE_LOG2, dst, op[0]);
1269 inst->saturate = instr->dest.saturate;
1270 break;
1271
1272 case nir_op_fsin:
1273 inst = emit_math(SHADER_OPCODE_SIN, dst, op[0]);
1274 inst->saturate = instr->dest.saturate;
1275 break;
1276
1277 case nir_op_fcos:
1278 inst = emit_math(SHADER_OPCODE_COS, dst, op[0]);
1279 inst->saturate = instr->dest.saturate;
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 inst->saturate = instr->dest.saturate;
1335 break;
1336
1337 case nir_op_frsq:
1338 inst = emit_math(SHADER_OPCODE_RSQ, dst, op[0]);
1339 inst->saturate = instr->dest.saturate;
1340 break;
1341
1342 case nir_op_fpow:
1343 inst = emit_math(SHADER_OPCODE_POW, dst, op[0], op[1]);
1344 inst->saturate = instr->dest.saturate;
1345 break;
1346
1347 case nir_op_uadd_carry: {
1348 assert(nir_dest_bit_size(instr->dest.dest) < 64);
1349 struct brw_reg acc = retype(brw_acc_reg(8), BRW_REGISTER_TYPE_UD);
1350
1351 emit(ADDC(dst_null_ud(), op[0], op[1]));
1352 emit(MOV(dst, src_reg(acc)));
1353 break;
1354 }
1355
1356 case nir_op_usub_borrow: {
1357 assert(nir_dest_bit_size(instr->dest.dest) < 64);
1358 struct brw_reg acc = retype(brw_acc_reg(8), BRW_REGISTER_TYPE_UD);
1359
1360 emit(SUBB(dst_null_ud(), op[0], op[1]));
1361 emit(MOV(dst, src_reg(acc)));
1362 break;
1363 }
1364
1365 case nir_op_ftrunc:
1366 inst = emit(RNDZ(dst, op[0]));
1367 inst->saturate = instr->dest.saturate;
1368 break;
1369
1370 case nir_op_fceil: {
1371 src_reg tmp = src_reg(this, glsl_type::float_type);
1372 tmp.swizzle =
1373 brw_swizzle_for_size(instr->src[0].src.is_ssa ?
1374 instr->src[0].src.ssa->num_components :
1375 instr->src[0].src.reg.reg->num_components);
1376
1377 op[0].negate = !op[0].negate;
1378 emit(RNDD(dst_reg(tmp), op[0]));
1379 tmp.negate = true;
1380 inst = emit(MOV(dst, tmp));
1381 inst->saturate = instr->dest.saturate;
1382 break;
1383 }
1384
1385 case nir_op_ffloor:
1386 inst = emit(RNDD(dst, op[0]));
1387 inst->saturate = instr->dest.saturate;
1388 break;
1389
1390 case nir_op_ffract:
1391 inst = emit(FRC(dst, op[0]));
1392 inst->saturate = instr->dest.saturate;
1393 break;
1394
1395 case nir_op_fround_even:
1396 inst = emit(RNDE(dst, op[0]));
1397 inst->saturate = instr->dest.saturate;
1398 break;
1399
1400 case nir_op_fquantize2f16: {
1401 /* See also vec4_visitor::emit_pack_half_2x16() */
1402 src_reg tmp16 = src_reg(this, glsl_type::uvec4_type);
1403 src_reg tmp32 = src_reg(this, glsl_type::vec4_type);
1404 src_reg zero = src_reg(this, glsl_type::vec4_type);
1405
1406 /* Check for denormal */
1407 src_reg abs_src0 = op[0];
1408 abs_src0.abs = true;
1409 emit(CMP(dst_null_f(), abs_src0, brw_imm_f(ldexpf(1.0, -14)),
1410 BRW_CONDITIONAL_L));
1411 /* Get the appropriately signed zero */
1412 emit(AND(retype(dst_reg(zero), BRW_REGISTER_TYPE_UD),
1413 retype(op[0], BRW_REGISTER_TYPE_UD),
1414 brw_imm_ud(0x80000000)));
1415 /* Do the actual F32 -> F16 -> F32 conversion */
1416 emit(F32TO16(dst_reg(tmp16), op[0]));
1417 emit(F16TO32(dst_reg(tmp32), tmp16));
1418 /* Select that or zero based on normal status */
1419 inst = emit(BRW_OPCODE_SEL, dst, zero, tmp32);
1420 inst->predicate = BRW_PREDICATE_NORMAL;
1421 inst->saturate = instr->dest.saturate;
1422 break;
1423 }
1424
1425 case nir_op_imin:
1426 case nir_op_umin:
1427 assert(nir_dest_bit_size(instr->dest.dest) < 64);
1428 /* fall through */
1429 case nir_op_fmin:
1430 try_immediate_source(instr, op, true, devinfo);
1431 inst = emit_minmax(BRW_CONDITIONAL_L, dst, op[0], op[1]);
1432 inst->saturate = instr->dest.saturate;
1433 break;
1434
1435 case nir_op_imax:
1436 case nir_op_umax:
1437 assert(nir_dest_bit_size(instr->dest.dest) < 64);
1438 /* fall through */
1439 case nir_op_fmax:
1440 try_immediate_source(instr, op, true, devinfo);
1441 inst = emit_minmax(BRW_CONDITIONAL_GE, dst, op[0], op[1]);
1442 inst->saturate = instr->dest.saturate;
1443 break;
1444
1445 case nir_op_fddx:
1446 case nir_op_fddx_coarse:
1447 case nir_op_fddx_fine:
1448 case nir_op_fddy:
1449 case nir_op_fddy_coarse:
1450 case nir_op_fddy_fine:
1451 unreachable("derivatives are not valid in vertex shaders");
1452
1453 case nir_op_ilt32:
1454 case nir_op_ult32:
1455 case nir_op_ige32:
1456 case nir_op_uge32:
1457 case nir_op_ieq32:
1458 case nir_op_ine32:
1459 assert(nir_dest_bit_size(instr->dest.dest) < 64);
1460 /* Fallthrough */
1461 case nir_op_flt32:
1462 case nir_op_fge32:
1463 case nir_op_feq32:
1464 case nir_op_fne32: {
1465 enum brw_conditional_mod conditional_mod =
1466 brw_cmod_for_nir_comparison(instr->op);
1467
1468 if (nir_src_bit_size(instr->src[0].src) < 64) {
1469 /* If the order of the sources is changed due to an immediate value,
1470 * then the condition must also be changed.
1471 */
1472 if (try_immediate_source(instr, op, true, devinfo) == 0)
1473 conditional_mod = brw_swap_cmod(conditional_mod);
1474
1475 emit(CMP(dst, op[0], op[1], conditional_mod));
1476 } else {
1477 /* Produce a 32-bit boolean result from the DF comparison by selecting
1478 * only the low 32-bit in each DF produced. Do this in a temporary
1479 * so we can then move from there to the result using align16 again
1480 * to honor the original writemask.
1481 */
1482 dst_reg temp = dst_reg(this, glsl_type::dvec4_type);
1483 emit(CMP(temp, op[0], op[1], conditional_mod));
1484 dst_reg result = dst_reg(this, glsl_type::bvec4_type);
1485 emit(VEC4_OPCODE_PICK_LOW_32BIT, result, src_reg(temp));
1486 emit(MOV(dst, src_reg(result)));
1487 }
1488 break;
1489 }
1490
1491 case nir_op_b32all_iequal2:
1492 case nir_op_b32all_iequal3:
1493 case nir_op_b32all_iequal4:
1494 assert(nir_dest_bit_size(instr->dest.dest) < 64);
1495 /* Fallthrough */
1496 case nir_op_b32all_fequal2:
1497 case nir_op_b32all_fequal3:
1498 case nir_op_b32all_fequal4: {
1499 unsigned swiz =
1500 brw_swizzle_for_size(nir_op_infos[instr->op].input_sizes[0]);
1501
1502 emit(CMP(dst_null_d(), swizzle(op[0], swiz), swizzle(op[1], swiz),
1503 brw_cmod_for_nir_comparison(instr->op)));
1504 emit(MOV(dst, brw_imm_d(0)));
1505 inst = emit(MOV(dst, brw_imm_d(~0)));
1506 inst->predicate = BRW_PREDICATE_ALIGN16_ALL4H;
1507 break;
1508 }
1509
1510 case nir_op_b32any_inequal2:
1511 case nir_op_b32any_inequal3:
1512 case nir_op_b32any_inequal4:
1513 assert(nir_dest_bit_size(instr->dest.dest) < 64);
1514 /* Fallthrough */
1515 case nir_op_b32any_fnequal2:
1516 case nir_op_b32any_fnequal3:
1517 case nir_op_b32any_fnequal4: {
1518 unsigned swiz =
1519 brw_swizzle_for_size(nir_op_infos[instr->op].input_sizes[0]);
1520
1521 emit(CMP(dst_null_d(), swizzle(op[0], swiz), swizzle(op[1], swiz),
1522 brw_cmod_for_nir_comparison(instr->op)));
1523
1524 emit(MOV(dst, brw_imm_d(0)));
1525 inst = emit(MOV(dst, brw_imm_d(~0)));
1526 inst->predicate = BRW_PREDICATE_ALIGN16_ANY4H;
1527 break;
1528 }
1529
1530 case nir_op_inot:
1531 assert(nir_dest_bit_size(instr->dest.dest) < 64);
1532 if (devinfo->gen >= 8) {
1533 op[0] = resolve_source_modifiers(op[0]);
1534 }
1535 emit(NOT(dst, op[0]));
1536 break;
1537
1538 case nir_op_ixor:
1539 assert(nir_dest_bit_size(instr->dest.dest) < 64);
1540 if (devinfo->gen >= 8) {
1541 op[0] = resolve_source_modifiers(op[0]);
1542 op[1] = resolve_source_modifiers(op[1]);
1543 }
1544 try_immediate_source(instr, op, true, devinfo);
1545 emit(XOR(dst, op[0], op[1]));
1546 break;
1547
1548 case nir_op_ior:
1549 assert(nir_dest_bit_size(instr->dest.dest) < 64);
1550 if (devinfo->gen >= 8) {
1551 op[0] = resolve_source_modifiers(op[0]);
1552 op[1] = resolve_source_modifiers(op[1]);
1553 }
1554 try_immediate_source(instr, op, true, devinfo);
1555 emit(OR(dst, op[0], op[1]));
1556 break;
1557
1558 case nir_op_iand:
1559 assert(nir_dest_bit_size(instr->dest.dest) < 64);
1560 if (devinfo->gen >= 8) {
1561 op[0] = resolve_source_modifiers(op[0]);
1562 op[1] = resolve_source_modifiers(op[1]);
1563 }
1564 try_immediate_source(instr, op, true, devinfo);
1565 emit(AND(dst, op[0], op[1]));
1566 break;
1567
1568 case nir_op_b2i32:
1569 case nir_op_b2f32:
1570 case nir_op_b2f64:
1571 if (nir_dest_bit_size(instr->dest.dest) > 32) {
1572 assert(dst.type == BRW_REGISTER_TYPE_DF);
1573 emit_conversion_to_double(dst, negate(op[0]), false);
1574 } else {
1575 emit(MOV(dst, negate(op[0])));
1576 }
1577 break;
1578
1579 case nir_op_f2b32:
1580 if (nir_src_bit_size(instr->src[0].src) == 64) {
1581 /* We use a MOV with conditional_mod to check if the provided value is
1582 * 0.0. We want this to flush denormalized numbers to zero, so we set a
1583 * source modifier on the source operand to trigger this, as source
1584 * modifiers don't affect the result of the testing against 0.0.
1585 */
1586 src_reg value = op[0];
1587 value.abs = true;
1588 vec4_instruction *inst = emit(MOV(dst_null_df(), value));
1589 inst->conditional_mod = BRW_CONDITIONAL_NZ;
1590
1591 src_reg one = src_reg(this, glsl_type::ivec4_type);
1592 emit(MOV(dst_reg(one), brw_imm_d(~0)));
1593 inst = emit(BRW_OPCODE_SEL, dst, one, brw_imm_d(0));
1594 inst->predicate = BRW_PREDICATE_NORMAL;
1595 } else {
1596 emit(CMP(dst, op[0], brw_imm_f(0.0f), BRW_CONDITIONAL_NZ));
1597 }
1598 break;
1599
1600 case nir_op_i2b32:
1601 emit(CMP(dst, op[0], brw_imm_d(0), BRW_CONDITIONAL_NZ));
1602 break;
1603
1604 case nir_op_fnoise1_1:
1605 case nir_op_fnoise1_2:
1606 case nir_op_fnoise1_3:
1607 case nir_op_fnoise1_4:
1608 case nir_op_fnoise2_1:
1609 case nir_op_fnoise2_2:
1610 case nir_op_fnoise2_3:
1611 case nir_op_fnoise2_4:
1612 case nir_op_fnoise3_1:
1613 case nir_op_fnoise3_2:
1614 case nir_op_fnoise3_3:
1615 case nir_op_fnoise3_4:
1616 case nir_op_fnoise4_1:
1617 case nir_op_fnoise4_2:
1618 case nir_op_fnoise4_3:
1619 case nir_op_fnoise4_4:
1620 unreachable("not reached: should be handled by lower_noise");
1621
1622 case nir_op_unpack_half_2x16_split_x:
1623 case nir_op_unpack_half_2x16_split_y:
1624 case nir_op_pack_half_2x16_split:
1625 unreachable("not reached: should not occur in vertex shader");
1626
1627 case nir_op_unpack_snorm_2x16:
1628 case nir_op_unpack_unorm_2x16:
1629 case nir_op_pack_snorm_2x16:
1630 case nir_op_pack_unorm_2x16:
1631 unreachable("not reached: should be handled by lower_packing_builtins");
1632
1633 case nir_op_pack_uvec4_to_uint:
1634 unreachable("not reached");
1635
1636 case nir_op_pack_uvec2_to_uint: {
1637 dst_reg tmp1 = dst_reg(this, glsl_type::uint_type);
1638 tmp1.writemask = WRITEMASK_X;
1639 op[0].swizzle = BRW_SWIZZLE_YYYY;
1640 emit(SHL(tmp1, op[0], src_reg(brw_imm_ud(16u))));
1641
1642 dst_reg tmp2 = dst_reg(this, glsl_type::uint_type);
1643 tmp2.writemask = WRITEMASK_X;
1644 op[0].swizzle = BRW_SWIZZLE_XXXX;
1645 emit(AND(tmp2, op[0], src_reg(brw_imm_ud(0xffffu))));
1646
1647 emit(OR(dst, src_reg(tmp1), src_reg(tmp2)));
1648 break;
1649 }
1650
1651 case nir_op_pack_64_2x32_split: {
1652 dst_reg result = dst_reg(this, glsl_type::dvec4_type);
1653 dst_reg tmp = dst_reg(this, glsl_type::uvec4_type);
1654 emit(MOV(tmp, retype(op[0], BRW_REGISTER_TYPE_UD)));
1655 emit(VEC4_OPCODE_SET_LOW_32BIT, result, src_reg(tmp));
1656 emit(MOV(tmp, retype(op[1], BRW_REGISTER_TYPE_UD)));
1657 emit(VEC4_OPCODE_SET_HIGH_32BIT, result, src_reg(tmp));
1658 emit(MOV(dst, src_reg(result)));
1659 break;
1660 }
1661
1662 case nir_op_unpack_64_2x32_split_x:
1663 case nir_op_unpack_64_2x32_split_y: {
1664 enum opcode oper = (instr->op == nir_op_unpack_64_2x32_split_x) ?
1665 VEC4_OPCODE_PICK_LOW_32BIT : VEC4_OPCODE_PICK_HIGH_32BIT;
1666 dst_reg tmp = dst_reg(this, glsl_type::dvec4_type);
1667 emit(MOV(tmp, op[0]));
1668 dst_reg tmp2 = dst_reg(this, glsl_type::uvec4_type);
1669 emit(oper, tmp2, src_reg(tmp));
1670 emit(MOV(dst, src_reg(tmp2)));
1671 break;
1672 }
1673
1674 case nir_op_unpack_half_2x16:
1675 /* As NIR does not guarantee that we have a correct swizzle outside the
1676 * boundaries of a vector, and the implementation of emit_unpack_half_2x16
1677 * uses the source operand in an operation with WRITEMASK_Y while our
1678 * source operand has only size 1, it accessed incorrect data producing
1679 * regressions in Piglit. We repeat the swizzle of the first component on the
1680 * rest of components to avoid regressions. In the vec4_visitor IR code path
1681 * this is not needed because the operand has already the correct swizzle.
1682 */
1683 op[0].swizzle = brw_compose_swizzle(BRW_SWIZZLE_XXXX, op[0].swizzle);
1684 emit_unpack_half_2x16(dst, op[0]);
1685 break;
1686
1687 case nir_op_pack_half_2x16:
1688 emit_pack_half_2x16(dst, op[0]);
1689 break;
1690
1691 case nir_op_unpack_unorm_4x8:
1692 assert(nir_dest_bit_size(instr->dest.dest) < 64);
1693 emit_unpack_unorm_4x8(dst, op[0]);
1694 break;
1695
1696 case nir_op_pack_unorm_4x8:
1697 assert(nir_dest_bit_size(instr->dest.dest) < 64);
1698 emit_pack_unorm_4x8(dst, op[0]);
1699 break;
1700
1701 case nir_op_unpack_snorm_4x8:
1702 assert(nir_dest_bit_size(instr->dest.dest) < 64);
1703 emit_unpack_snorm_4x8(dst, op[0]);
1704 break;
1705
1706 case nir_op_pack_snorm_4x8:
1707 assert(nir_dest_bit_size(instr->dest.dest) < 64);
1708 emit_pack_snorm_4x8(dst, op[0]);
1709 break;
1710
1711 case nir_op_bitfield_reverse:
1712 assert(nir_dest_bit_size(instr->dest.dest) < 64);
1713 emit(BFREV(dst, op[0]));
1714 break;
1715
1716 case nir_op_bit_count:
1717 assert(nir_dest_bit_size(instr->dest.dest) < 64);
1718 emit(CBIT(dst, op[0]));
1719 break;
1720
1721 case nir_op_ufind_msb:
1722 assert(nir_dest_bit_size(instr->dest.dest) < 64);
1723 emit_find_msb_using_lzd(vec4_builder(this).at_end(), dst, op[0], false);
1724 break;
1725
1726 case nir_op_ifind_msb: {
1727 assert(nir_dest_bit_size(instr->dest.dest) < 64);
1728 vec4_builder bld = vec4_builder(this).at_end();
1729 src_reg src(dst);
1730
1731 if (devinfo->gen < 7) {
1732 emit_find_msb_using_lzd(bld, dst, op[0], true);
1733 } else {
1734 emit(FBH(retype(dst, BRW_REGISTER_TYPE_UD), op[0]));
1735
1736 /* FBH counts from the MSB side, while GLSL's findMSB() wants the
1737 * count from the LSB side. If FBH didn't return an error
1738 * (0xFFFFFFFF), then subtract the result from 31 to convert the MSB
1739 * count into an LSB count.
1740 */
1741 bld.CMP(dst_null_d(), src, brw_imm_d(-1), BRW_CONDITIONAL_NZ);
1742
1743 inst = bld.ADD(dst, src, brw_imm_d(31));
1744 inst->predicate = BRW_PREDICATE_NORMAL;
1745 inst->src[0].negate = true;
1746 }
1747 break;
1748 }
1749
1750 case nir_op_find_lsb: {
1751 assert(nir_dest_bit_size(instr->dest.dest) < 64);
1752 vec4_builder bld = vec4_builder(this).at_end();
1753
1754 if (devinfo->gen < 7) {
1755 dst_reg temp = bld.vgrf(BRW_REGISTER_TYPE_D);
1756
1757 /* (x & -x) generates a value that consists of only the LSB of x.
1758 * For all powers of 2, findMSB(y) == findLSB(y).
1759 */
1760 src_reg src = src_reg(retype(op[0], BRW_REGISTER_TYPE_D));
1761 src_reg negated_src = src;
1762
1763 /* One must be negated, and the other must be non-negated. It
1764 * doesn't matter which is which.
1765 */
1766 negated_src.negate = true;
1767 src.negate = false;
1768
1769 bld.AND(temp, src, negated_src);
1770 emit_find_msb_using_lzd(bld, dst, src_reg(temp), false);
1771 } else {
1772 bld.FBL(dst, op[0]);
1773 }
1774 break;
1775 }
1776
1777 case nir_op_ubitfield_extract:
1778 case nir_op_ibitfield_extract:
1779 unreachable("should have been lowered");
1780 case nir_op_ubfe:
1781 case nir_op_ibfe:
1782 assert(nir_dest_bit_size(instr->dest.dest) < 64);
1783 op[0] = fix_3src_operand(op[0]);
1784 op[1] = fix_3src_operand(op[1]);
1785 op[2] = fix_3src_operand(op[2]);
1786
1787 emit(BFE(dst, op[2], op[1], op[0]));
1788 break;
1789
1790 case nir_op_bfm:
1791 assert(nir_dest_bit_size(instr->dest.dest) < 64);
1792 emit(BFI1(dst, op[0], op[1]));
1793 break;
1794
1795 case nir_op_bfi:
1796 assert(nir_dest_bit_size(instr->dest.dest) < 64);
1797 op[0] = fix_3src_operand(op[0]);
1798 op[1] = fix_3src_operand(op[1]);
1799 op[2] = fix_3src_operand(op[2]);
1800
1801 emit(BFI2(dst, op[0], op[1], op[2]));
1802 break;
1803
1804 case nir_op_bitfield_insert:
1805 unreachable("not reached: should have been lowered");
1806
1807 case nir_op_fsign:
1808 assert(!instr->dest.saturate);
1809 if (op[0].abs) {
1810 /* Straightforward since the source can be assumed to be either
1811 * strictly >= 0 or strictly <= 0 depending on the setting of the
1812 * negate flag.
1813 */
1814 inst = emit(MOV(dst, op[0]));
1815 inst->conditional_mod = BRW_CONDITIONAL_NZ;
1816
1817 inst = (op[0].negate)
1818 ? emit(MOV(dst, brw_imm_f(-1.0f)))
1819 : emit(MOV(dst, brw_imm_f(1.0f)));
1820 inst->predicate = BRW_PREDICATE_NORMAL;
1821 } else if (type_sz(op[0].type) < 8) {
1822 /* AND(val, 0x80000000) gives the sign bit.
1823 *
1824 * Predicated OR ORs 1.0 (0x3f800000) with the sign bit if val is not
1825 * zero.
1826 */
1827 emit(CMP(dst_null_f(), op[0], brw_imm_f(0.0f), BRW_CONDITIONAL_NZ));
1828
1829 op[0].type = BRW_REGISTER_TYPE_UD;
1830 dst.type = BRW_REGISTER_TYPE_UD;
1831 emit(AND(dst, op[0], brw_imm_ud(0x80000000u)));
1832
1833 inst = emit(OR(dst, src_reg(dst), brw_imm_ud(0x3f800000u)));
1834 inst->predicate = BRW_PREDICATE_NORMAL;
1835 dst.type = BRW_REGISTER_TYPE_F;
1836 } else {
1837 /* For doubles we do the same but we need to consider:
1838 *
1839 * - We use a MOV with conditional_mod instead of a CMP so that we can
1840 * skip loading a 0.0 immediate. We use a source modifier on the
1841 * source of the MOV so that we flush denormalized values to 0.
1842 * Since we want to compare against 0, this won't alter the result.
1843 * - We need to extract the high 32-bit of each DF where the sign
1844 * is stored.
1845 * - We need to produce a DF result.
1846 */
1847
1848 /* Check for zero */
1849 src_reg value = op[0];
1850 value.abs = true;
1851 inst = emit(MOV(dst_null_df(), value));
1852 inst->conditional_mod = BRW_CONDITIONAL_NZ;
1853
1854 /* AND each high 32-bit channel with 0x80000000u */
1855 dst_reg tmp = dst_reg(this, glsl_type::uvec4_type);
1856 emit(VEC4_OPCODE_PICK_HIGH_32BIT, tmp, op[0]);
1857 emit(AND(tmp, src_reg(tmp), brw_imm_ud(0x80000000u)));
1858
1859 /* Add 1.0 to each channel, predicated to skip the cases where the
1860 * channel's value was 0
1861 */
1862 inst = emit(OR(tmp, src_reg(tmp), brw_imm_ud(0x3f800000u)));
1863 inst->predicate = BRW_PREDICATE_NORMAL;
1864
1865 /* Now convert the result from float to double */
1866 emit_conversion_to_double(dst, retype(src_reg(tmp),
1867 BRW_REGISTER_TYPE_F),
1868 false);
1869 }
1870 break;
1871
1872 case nir_op_ishl:
1873 assert(nir_dest_bit_size(instr->dest.dest) < 64);
1874 try_immediate_source(instr, op, false, devinfo);
1875 emit(SHL(dst, op[0], op[1]));
1876 break;
1877
1878 case nir_op_ishr:
1879 assert(nir_dest_bit_size(instr->dest.dest) < 64);
1880 try_immediate_source(instr, op, false, devinfo);
1881 emit(ASR(dst, op[0], op[1]));
1882 break;
1883
1884 case nir_op_ushr:
1885 assert(nir_dest_bit_size(instr->dest.dest) < 64);
1886 try_immediate_source(instr, op, false, devinfo);
1887 emit(SHR(dst, op[0], op[1]));
1888 break;
1889
1890 case nir_op_ffma:
1891 if (type_sz(dst.type) == 8) {
1892 dst_reg mul_dst = dst_reg(this, glsl_type::dvec4_type);
1893 emit(MUL(mul_dst, op[1], op[0]));
1894 inst = emit(ADD(dst, src_reg(mul_dst), op[2]));
1895 inst->saturate = instr->dest.saturate;
1896 } else {
1897 fix_float_operands(op, instr);
1898 inst = emit(MAD(dst, op[2], op[1], op[0]));
1899 inst->saturate = instr->dest.saturate;
1900 }
1901 break;
1902
1903 case nir_op_flrp:
1904 fix_float_operands(op, instr);
1905 inst = emit(LRP(dst, op[2], op[1], op[0]));
1906 inst->saturate = instr->dest.saturate;
1907 break;
1908
1909 case nir_op_b32csel:
1910 enum brw_predicate predicate;
1911 if (!optimize_predicate(instr, &predicate)) {
1912 emit(CMP(dst_null_d(), op[0], brw_imm_d(0), BRW_CONDITIONAL_NZ));
1913 switch (dst.writemask) {
1914 case WRITEMASK_X:
1915 predicate = BRW_PREDICATE_ALIGN16_REPLICATE_X;
1916 break;
1917 case WRITEMASK_Y:
1918 predicate = BRW_PREDICATE_ALIGN16_REPLICATE_Y;
1919 break;
1920 case WRITEMASK_Z:
1921 predicate = BRW_PREDICATE_ALIGN16_REPLICATE_Z;
1922 break;
1923 case WRITEMASK_W:
1924 predicate = BRW_PREDICATE_ALIGN16_REPLICATE_W;
1925 break;
1926 default:
1927 predicate = BRW_PREDICATE_NORMAL;
1928 break;
1929 }
1930 }
1931 inst = emit(BRW_OPCODE_SEL, dst, op[1], op[2]);
1932 inst->predicate = predicate;
1933 break;
1934
1935 case nir_op_fdot_replicated2:
1936 try_immediate_source(instr, op, true, devinfo);
1937 inst = emit(BRW_OPCODE_DP2, dst, op[0], op[1]);
1938 inst->saturate = instr->dest.saturate;
1939 break;
1940
1941 case nir_op_fdot_replicated3:
1942 try_immediate_source(instr, op, true, devinfo);
1943 inst = emit(BRW_OPCODE_DP3, dst, op[0], op[1]);
1944 inst->saturate = instr->dest.saturate;
1945 break;
1946
1947 case nir_op_fdot_replicated4:
1948 try_immediate_source(instr, op, true, devinfo);
1949 inst = emit(BRW_OPCODE_DP4, dst, op[0], op[1]);
1950 inst->saturate = instr->dest.saturate;
1951 break;
1952
1953 case nir_op_fdph_replicated:
1954 try_immediate_source(instr, op, true, devinfo);
1955 inst = emit(BRW_OPCODE_DPH, dst, op[0], op[1]);
1956 inst->saturate = instr->dest.saturate;
1957 break;
1958
1959 case nir_op_fdiv:
1960 unreachable("not reached: should be lowered by DIV_TO_MUL_RCP in the compiler");
1961
1962 case nir_op_fmod:
1963 unreachable("not reached: should be lowered by MOD_TO_FLOOR in the compiler");
1964
1965 case nir_op_fsub:
1966 case nir_op_isub:
1967 unreachable("not reached: should be handled by ir_sub_to_add_neg");
1968
1969 default:
1970 unreachable("Unimplemented ALU operation");
1971 }
1972
1973 /* If we need to do a boolean resolve, replace the result with -(x & 1)
1974 * to sign extend the low bit to 0/~0
1975 */
1976 if (devinfo->gen <= 5 &&
1977 (instr->instr.pass_flags & BRW_NIR_BOOLEAN_MASK) ==
1978 BRW_NIR_BOOLEAN_NEEDS_RESOLVE) {
1979 dst_reg masked = dst_reg(this, glsl_type::int_type);
1980 masked.writemask = dst.writemask;
1981 emit(AND(masked, src_reg(dst), brw_imm_d(1)));
1982 src_reg masked_neg = src_reg(masked);
1983 masked_neg.negate = true;
1984 emit(MOV(retype(dst, BRW_REGISTER_TYPE_D), masked_neg));
1985 }
1986 }
1987
1988 void
1989 vec4_visitor::nir_emit_jump(nir_jump_instr *instr)
1990 {
1991 switch (instr->type) {
1992 case nir_jump_break:
1993 emit(BRW_OPCODE_BREAK);
1994 break;
1995
1996 case nir_jump_continue:
1997 emit(BRW_OPCODE_CONTINUE);
1998 break;
1999
2000 case nir_jump_return:
2001 /* fall through */
2002 default:
2003 unreachable("unknown jump");
2004 }
2005 }
2006
2007 static enum ir_texture_opcode
2008 ir_texture_opcode_for_nir_texop(nir_texop texop)
2009 {
2010 enum ir_texture_opcode op;
2011
2012 switch (texop) {
2013 case nir_texop_lod: op = ir_lod; break;
2014 case nir_texop_query_levels: op = ir_query_levels; break;
2015 case nir_texop_texture_samples: op = ir_texture_samples; break;
2016 case nir_texop_tex: op = ir_tex; break;
2017 case nir_texop_tg4: op = ir_tg4; break;
2018 case nir_texop_txb: op = ir_txb; break;
2019 case nir_texop_txd: op = ir_txd; break;
2020 case nir_texop_txf: op = ir_txf; break;
2021 case nir_texop_txf_ms: op = ir_txf_ms; break;
2022 case nir_texop_txl: op = ir_txl; break;
2023 case nir_texop_txs: op = ir_txs; break;
2024 case nir_texop_samples_identical: op = ir_samples_identical; break;
2025 default:
2026 unreachable("unknown texture opcode");
2027 }
2028
2029 return op;
2030 }
2031
2032 static const glsl_type *
2033 glsl_type_for_nir_alu_type(nir_alu_type alu_type,
2034 unsigned components)
2035 {
2036 return glsl_type::get_instance(brw_glsl_base_type_for_nir_type(alu_type),
2037 components, 1);
2038 }
2039
2040 void
2041 vec4_visitor::nir_emit_texture(nir_tex_instr *instr)
2042 {
2043 unsigned texture = instr->texture_index;
2044 unsigned sampler = instr->sampler_index;
2045 src_reg texture_reg = brw_imm_ud(texture);
2046 src_reg sampler_reg = brw_imm_ud(sampler);
2047 src_reg coordinate;
2048 const glsl_type *coord_type = NULL;
2049 src_reg shadow_comparator;
2050 src_reg offset_value;
2051 src_reg lod, lod2;
2052 src_reg sample_index;
2053 src_reg mcs;
2054
2055 const glsl_type *dest_type =
2056 glsl_type_for_nir_alu_type(instr->dest_type,
2057 nir_tex_instr_dest_size(instr));
2058 dst_reg dest = get_nir_dest(instr->dest, instr->dest_type);
2059
2060 /* The hardware requires a LOD for buffer textures */
2061 if (instr->sampler_dim == GLSL_SAMPLER_DIM_BUF)
2062 lod = brw_imm_d(0);
2063
2064 /* Load the texture operation sources */
2065 uint32_t constant_offset = 0;
2066 for (unsigned i = 0; i < instr->num_srcs; i++) {
2067 switch (instr->src[i].src_type) {
2068 case nir_tex_src_comparator:
2069 shadow_comparator = get_nir_src(instr->src[i].src,
2070 BRW_REGISTER_TYPE_F, 1);
2071 break;
2072
2073 case nir_tex_src_coord: {
2074 unsigned src_size = nir_tex_instr_src_size(instr, i);
2075
2076 switch (instr->op) {
2077 case nir_texop_txf:
2078 case nir_texop_txf_ms:
2079 case nir_texop_samples_identical:
2080 coordinate = get_nir_src(instr->src[i].src, BRW_REGISTER_TYPE_D,
2081 src_size);
2082 coord_type = glsl_type::ivec(src_size);
2083 break;
2084
2085 default:
2086 coordinate = get_nir_src(instr->src[i].src, BRW_REGISTER_TYPE_F,
2087 src_size);
2088 coord_type = glsl_type::vec(src_size);
2089 break;
2090 }
2091 break;
2092 }
2093
2094 case nir_tex_src_ddx:
2095 lod = get_nir_src(instr->src[i].src, BRW_REGISTER_TYPE_F,
2096 nir_tex_instr_src_size(instr, i));
2097 break;
2098
2099 case nir_tex_src_ddy:
2100 lod2 = get_nir_src(instr->src[i].src, BRW_REGISTER_TYPE_F,
2101 nir_tex_instr_src_size(instr, i));
2102 break;
2103
2104 case nir_tex_src_lod:
2105 switch (instr->op) {
2106 case nir_texop_txs:
2107 case nir_texop_txf:
2108 lod = get_nir_src(instr->src[i].src, BRW_REGISTER_TYPE_D, 1);
2109 break;
2110
2111 default:
2112 lod = get_nir_src(instr->src[i].src, BRW_REGISTER_TYPE_F, 1);
2113 break;
2114 }
2115 break;
2116
2117 case nir_tex_src_ms_index: {
2118 sample_index = get_nir_src(instr->src[i].src, BRW_REGISTER_TYPE_D, 1);
2119 break;
2120 }
2121
2122 case nir_tex_src_offset:
2123 if (!brw_texture_offset(instr, i, &constant_offset)) {
2124 offset_value =
2125 get_nir_src(instr->src[i].src, BRW_REGISTER_TYPE_D, 2);
2126 }
2127 break;
2128
2129 case nir_tex_src_texture_offset: {
2130 /* Emit code to evaluate the actual indexing expression */
2131 src_reg src = get_nir_src(instr->src[i].src, 1);
2132 src_reg temp(this, glsl_type::uint_type);
2133 emit(ADD(dst_reg(temp), src, brw_imm_ud(texture)));
2134 texture_reg = emit_uniformize(temp);
2135 break;
2136 }
2137
2138 case nir_tex_src_sampler_offset: {
2139 /* Emit code to evaluate the actual indexing expression */
2140 src_reg src = get_nir_src(instr->src[i].src, 1);
2141 src_reg temp(this, glsl_type::uint_type);
2142 emit(ADD(dst_reg(temp), src, brw_imm_ud(sampler)));
2143 sampler_reg = emit_uniformize(temp);
2144 break;
2145 }
2146
2147 case nir_tex_src_projector:
2148 unreachable("Should be lowered by do_lower_texture_projection");
2149
2150 case nir_tex_src_bias:
2151 unreachable("LOD bias is not valid for vertex shaders.\n");
2152
2153 default:
2154 unreachable("unknown texture source");
2155 }
2156 }
2157
2158 if (instr->op == nir_texop_txf_ms ||
2159 instr->op == nir_texop_samples_identical) {
2160 assert(coord_type != NULL);
2161 if (devinfo->gen >= 7 &&
2162 key_tex->compressed_multisample_layout_mask & (1 << texture)) {
2163 mcs = emit_mcs_fetch(coord_type, coordinate, texture_reg);
2164 } else {
2165 mcs = brw_imm_ud(0u);
2166 }
2167 }
2168
2169 /* Stuff the channel select bits in the top of the texture offset */
2170 if (instr->op == nir_texop_tg4) {
2171 if (instr->component == 1 &&
2172 (key_tex->gather_channel_quirk_mask & (1 << texture))) {
2173 /* gather4 sampler is broken for green channel on RG32F --
2174 * we must ask for blue instead.
2175 */
2176 constant_offset |= 2 << 16;
2177 } else {
2178 constant_offset |= instr->component << 16;
2179 }
2180 }
2181
2182 ir_texture_opcode op = ir_texture_opcode_for_nir_texop(instr->op);
2183
2184 emit_texture(op, dest, dest_type, coordinate, instr->coord_components,
2185 shadow_comparator,
2186 lod, lod2, sample_index,
2187 constant_offset, offset_value, mcs,
2188 texture, texture_reg, sampler_reg);
2189 }
2190
2191 void
2192 vec4_visitor::nir_emit_undef(nir_ssa_undef_instr *instr)
2193 {
2194 nir_ssa_values[instr->def.index] =
2195 dst_reg(VGRF, alloc.allocate(DIV_ROUND_UP(instr->def.bit_size, 32)));
2196 }
2197
2198 /* SIMD4x2 64bit data is stored in register space like this:
2199 *
2200 * r0.0:DF x0 y0 z0 w0
2201 * r1.0:DF x1 y1 z1 w1
2202 *
2203 * When we need to write data such as this to memory using 32-bit write
2204 * messages we need to shuffle it in this fashion:
2205 *
2206 * r0.0:DF x0 y0 x1 y1 (to be written at base offset)
2207 * r0.0:DF z0 w0 z1 w1 (to be written at base offset + 16)
2208 *
2209 * We need to do the inverse operation when we read using 32-bit messages,
2210 * which we can do by applying the same exact shuffling on the 64-bit data
2211 * read, only that because the data for each vertex is positioned differently
2212 * we need to apply different channel enables.
2213 *
2214 * This function takes 64bit data and shuffles it as explained above.
2215 *
2216 * The @for_write parameter is used to specify if the shuffling is being done
2217 * for proper SIMD4x2 64-bit data that needs to be shuffled prior to a 32-bit
2218 * write message (for_write = true), or instead we are doing the inverse
2219 * operation and we have just read 64-bit data using a 32-bit messages that we
2220 * need to shuffle to create valid SIMD4x2 64-bit data (for_write = false).
2221 *
2222 * If @block and @ref are non-NULL, then the shuffling is done after @ref,
2223 * otherwise the instructions are emitted normally at the end. The function
2224 * returns the last instruction inserted.
2225 *
2226 * Notice that @src and @dst cannot be the same register.
2227 */
2228 vec4_instruction *
2229 vec4_visitor::shuffle_64bit_data(dst_reg dst, src_reg src, bool for_write,
2230 bblock_t *block, vec4_instruction *ref)
2231 {
2232 assert(type_sz(src.type) == 8);
2233 assert(type_sz(dst.type) == 8);
2234 assert(!regions_overlap(dst, 2 * REG_SIZE, src, 2 * REG_SIZE));
2235 assert(!ref == !block);
2236
2237 const vec4_builder bld = !ref ? vec4_builder(this).at_end() :
2238 vec4_builder(this).at(block, ref->next);
2239
2240 /* Resolve swizzle in src */
2241 vec4_instruction *inst;
2242 if (src.swizzle != BRW_SWIZZLE_XYZW) {
2243 dst_reg data = dst_reg(this, glsl_type::dvec4_type);
2244 inst = bld.MOV(data, src);
2245 src = src_reg(data);
2246 }
2247
2248 /* dst+0.XY = src+0.XY */
2249 inst = bld.group(4, 0).MOV(writemask(dst, WRITEMASK_XY), src);
2250
2251 /* dst+0.ZW = src+1.XY */
2252 inst = bld.group(4, for_write ? 1 : 0)
2253 .MOV(writemask(dst, WRITEMASK_ZW),
2254 swizzle(byte_offset(src, REG_SIZE), BRW_SWIZZLE_XYXY));
2255
2256 /* dst+1.XY = src+0.ZW */
2257 inst = bld.group(4, for_write ? 0 : 1)
2258 .MOV(writemask(byte_offset(dst, REG_SIZE), WRITEMASK_XY),
2259 swizzle(src, BRW_SWIZZLE_ZWZW));
2260
2261 /* dst+1.ZW = src+1.ZW */
2262 inst = bld.group(4, 1)
2263 .MOV(writemask(byte_offset(dst, REG_SIZE), WRITEMASK_ZW),
2264 byte_offset(src, REG_SIZE));
2265
2266 return inst;
2267 }
2268
2269 }