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