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