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