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