i965/fs: Add byte scattered read message and fs support
[mesa.git] / src / intel / compiler / brw_eu_emit.c
1 /*
2 Copyright (C) Intel Corp. 2006. All Rights Reserved.
3 Intel funded Tungsten Graphics to
4 develop this 3D driver.
5
6 Permission is hereby granted, free of charge, to any person obtaining
7 a copy of this software and associated documentation files (the
8 "Software"), to deal in the Software without restriction, including
9 without limitation the rights to use, copy, modify, merge, publish,
10 distribute, sublicense, and/or sell copies of the Software, and to
11 permit persons to whom the Software is furnished to do so, subject to
12 the following conditions:
13
14 The above copyright notice and this permission notice (including the
15 next paragraph) shall be included in all copies or substantial
16 portions of the Software.
17
18 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
19 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
21 IN NO EVENT SHALL THE COPYRIGHT OWNER(S) AND/OR ITS SUPPLIERS BE
22 LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
23 OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
24 WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25
26 **********************************************************************/
27 /*
28 * Authors:
29 * Keith Whitwell <keithw@vmware.com>
30 */
31
32
33 #include "brw_eu_defines.h"
34 #include "brw_eu.h"
35
36 #include "util/ralloc.h"
37
38 /**
39 * Prior to Sandybridge, the SEND instruction accepted non-MRF source
40 * registers, implicitly moving the operand to a message register.
41 *
42 * On Sandybridge, this is no longer the case. This function performs the
43 * explicit move; it should be called before emitting a SEND instruction.
44 */
45 void
46 gen6_resolve_implied_move(struct brw_codegen *p,
47 struct brw_reg *src,
48 unsigned msg_reg_nr)
49 {
50 const struct gen_device_info *devinfo = p->devinfo;
51 if (devinfo->gen < 6)
52 return;
53
54 if (src->file == BRW_MESSAGE_REGISTER_FILE)
55 return;
56
57 if (src->file != BRW_ARCHITECTURE_REGISTER_FILE || src->nr != BRW_ARF_NULL) {
58 brw_push_insn_state(p);
59 brw_set_default_exec_size(p, BRW_EXECUTE_8);
60 brw_set_default_mask_control(p, BRW_MASK_DISABLE);
61 brw_set_default_compression_control(p, BRW_COMPRESSION_NONE);
62 brw_MOV(p, retype(brw_message_reg(msg_reg_nr), BRW_REGISTER_TYPE_UD),
63 retype(*src, BRW_REGISTER_TYPE_UD));
64 brw_pop_insn_state(p);
65 }
66 *src = brw_message_reg(msg_reg_nr);
67 }
68
69 static void
70 gen7_convert_mrf_to_grf(struct brw_codegen *p, struct brw_reg *reg)
71 {
72 /* From the Ivybridge PRM, Volume 4 Part 3, page 218 ("send"):
73 * "The send with EOT should use register space R112-R127 for <src>. This is
74 * to enable loading of a new thread into the same slot while the message
75 * with EOT for current thread is pending dispatch."
76 *
77 * Since we're pretending to have 16 MRFs anyway, we may as well use the
78 * registers required for messages with EOT.
79 */
80 const struct gen_device_info *devinfo = p->devinfo;
81 if (devinfo->gen >= 7 && reg->file == BRW_MESSAGE_REGISTER_FILE) {
82 reg->file = BRW_GENERAL_REGISTER_FILE;
83 reg->nr += GEN7_MRF_HACK_START;
84 }
85 }
86
87 void
88 brw_set_dest(struct brw_codegen *p, brw_inst *inst, struct brw_reg dest)
89 {
90 const struct gen_device_info *devinfo = p->devinfo;
91
92 if (dest.file == BRW_MESSAGE_REGISTER_FILE)
93 assert((dest.nr & ~BRW_MRF_COMPR4) < BRW_MAX_MRF(devinfo->gen));
94 else if (dest.file != BRW_ARCHITECTURE_REGISTER_FILE)
95 assert(dest.nr < 128);
96
97 gen7_convert_mrf_to_grf(p, &dest);
98
99 brw_inst_set_dst_file_type(devinfo, inst, dest.file, dest.type);
100 brw_inst_set_dst_address_mode(devinfo, inst, dest.address_mode);
101
102 if (dest.address_mode == BRW_ADDRESS_DIRECT) {
103 brw_inst_set_dst_da_reg_nr(devinfo, inst, dest.nr);
104
105 if (brw_inst_access_mode(devinfo, inst) == BRW_ALIGN_1) {
106 brw_inst_set_dst_da1_subreg_nr(devinfo, inst, dest.subnr);
107 if (dest.hstride == BRW_HORIZONTAL_STRIDE_0)
108 dest.hstride = BRW_HORIZONTAL_STRIDE_1;
109 brw_inst_set_dst_hstride(devinfo, inst, dest.hstride);
110 } else {
111 brw_inst_set_dst_da16_subreg_nr(devinfo, inst, dest.subnr / 16);
112 brw_inst_set_da16_writemask(devinfo, inst, dest.writemask);
113 if (dest.file == BRW_GENERAL_REGISTER_FILE ||
114 dest.file == BRW_MESSAGE_REGISTER_FILE) {
115 assert(dest.writemask != 0);
116 }
117 /* From the Ivybridge PRM, Vol 4, Part 3, Section 5.2.4.1:
118 * Although Dst.HorzStride is a don't care for Align16, HW needs
119 * this to be programmed as "01".
120 */
121 brw_inst_set_dst_hstride(devinfo, inst, 1);
122 }
123 } else {
124 brw_inst_set_dst_ia_subreg_nr(devinfo, inst, dest.subnr);
125
126 /* These are different sizes in align1 vs align16:
127 */
128 if (brw_inst_access_mode(devinfo, inst) == BRW_ALIGN_1) {
129 brw_inst_set_dst_ia1_addr_imm(devinfo, inst,
130 dest.indirect_offset);
131 if (dest.hstride == BRW_HORIZONTAL_STRIDE_0)
132 dest.hstride = BRW_HORIZONTAL_STRIDE_1;
133 brw_inst_set_dst_hstride(devinfo, inst, dest.hstride);
134 } else {
135 brw_inst_set_dst_ia16_addr_imm(devinfo, inst,
136 dest.indirect_offset);
137 /* even ignored in da16, still need to set as '01' */
138 brw_inst_set_dst_hstride(devinfo, inst, 1);
139 }
140 }
141
142 /* Generators should set a default exec_size of either 8 (SIMD4x2 or SIMD8)
143 * or 16 (SIMD16), as that's normally correct. However, when dealing with
144 * small registers, it can be useful for us to automatically reduce it to
145 * match the register size.
146 */
147 if (p->automatic_exec_sizes) {
148 /*
149 * In platforms that support fp64 we can emit instructions with a width
150 * of 4 that need two SIMD8 registers and an exec_size of 8 or 16. In
151 * these cases we need to make sure that these instructions have their
152 * exec sizes set properly when they are emitted and we can't rely on
153 * this code to fix it.
154 */
155 bool fix_exec_size;
156 if (devinfo->gen >= 6)
157 fix_exec_size = dest.width < BRW_EXECUTE_4;
158 else
159 fix_exec_size = dest.width < BRW_EXECUTE_8;
160
161 if (fix_exec_size)
162 brw_inst_set_exec_size(devinfo, inst, dest.width);
163 }
164 }
165
166 void
167 brw_set_src0(struct brw_codegen *p, brw_inst *inst, struct brw_reg reg)
168 {
169 const struct gen_device_info *devinfo = p->devinfo;
170
171 if (reg.file == BRW_MESSAGE_REGISTER_FILE)
172 assert((reg.nr & ~BRW_MRF_COMPR4) < BRW_MAX_MRF(devinfo->gen));
173 else if (reg.file != BRW_ARCHITECTURE_REGISTER_FILE)
174 assert(reg.nr < 128);
175
176 gen7_convert_mrf_to_grf(p, &reg);
177
178 if (devinfo->gen >= 6 && (brw_inst_opcode(devinfo, inst) == BRW_OPCODE_SEND ||
179 brw_inst_opcode(devinfo, inst) == BRW_OPCODE_SENDC)) {
180 /* Any source modifiers or regions will be ignored, since this just
181 * identifies the MRF/GRF to start reading the message contents from.
182 * Check for some likely failures.
183 */
184 assert(!reg.negate);
185 assert(!reg.abs);
186 assert(reg.address_mode == BRW_ADDRESS_DIRECT);
187 }
188
189 brw_inst_set_src0_file_type(devinfo, inst, reg.file, reg.type);
190 brw_inst_set_src0_abs(devinfo, inst, reg.abs);
191 brw_inst_set_src0_negate(devinfo, inst, reg.negate);
192 brw_inst_set_src0_address_mode(devinfo, inst, reg.address_mode);
193
194 if (reg.file == BRW_IMMEDIATE_VALUE) {
195 if (reg.type == BRW_REGISTER_TYPE_DF ||
196 brw_inst_opcode(devinfo, inst) == BRW_OPCODE_DIM)
197 brw_inst_set_imm_df(devinfo, inst, reg.df);
198 else if (reg.type == BRW_REGISTER_TYPE_UQ ||
199 reg.type == BRW_REGISTER_TYPE_Q)
200 brw_inst_set_imm_uq(devinfo, inst, reg.u64);
201 else
202 brw_inst_set_imm_ud(devinfo, inst, reg.ud);
203
204 if (type_sz(reg.type) < 8) {
205 brw_inst_set_src1_reg_file(devinfo, inst,
206 BRW_ARCHITECTURE_REGISTER_FILE);
207 brw_inst_set_src1_reg_hw_type(devinfo, inst,
208 brw_inst_src0_reg_hw_type(devinfo, inst));
209 }
210 } else {
211 if (reg.address_mode == BRW_ADDRESS_DIRECT) {
212 brw_inst_set_src0_da_reg_nr(devinfo, inst, reg.nr);
213 if (brw_inst_access_mode(devinfo, inst) == BRW_ALIGN_1) {
214 brw_inst_set_src0_da1_subreg_nr(devinfo, inst, reg.subnr);
215 } else {
216 brw_inst_set_src0_da16_subreg_nr(devinfo, inst, reg.subnr / 16);
217 }
218 } else {
219 brw_inst_set_src0_ia_subreg_nr(devinfo, inst, reg.subnr);
220
221 if (brw_inst_access_mode(devinfo, inst) == BRW_ALIGN_1) {
222 brw_inst_set_src0_ia1_addr_imm(devinfo, inst, reg.indirect_offset);
223 } else {
224 brw_inst_set_src0_ia16_addr_imm(devinfo, inst, reg.indirect_offset);
225 }
226 }
227
228 if (brw_inst_access_mode(devinfo, inst) == BRW_ALIGN_1) {
229 if (reg.width == BRW_WIDTH_1 &&
230 brw_inst_exec_size(devinfo, inst) == BRW_EXECUTE_1) {
231 brw_inst_set_src0_hstride(devinfo, inst, BRW_HORIZONTAL_STRIDE_0);
232 brw_inst_set_src0_width(devinfo, inst, BRW_WIDTH_1);
233 brw_inst_set_src0_vstride(devinfo, inst, BRW_VERTICAL_STRIDE_0);
234 } else {
235 brw_inst_set_src0_hstride(devinfo, inst, reg.hstride);
236 brw_inst_set_src0_width(devinfo, inst, reg.width);
237 brw_inst_set_src0_vstride(devinfo, inst, reg.vstride);
238 }
239 } else {
240 brw_inst_set_src0_da16_swiz_x(devinfo, inst,
241 BRW_GET_SWZ(reg.swizzle, BRW_CHANNEL_X));
242 brw_inst_set_src0_da16_swiz_y(devinfo, inst,
243 BRW_GET_SWZ(reg.swizzle, BRW_CHANNEL_Y));
244 brw_inst_set_src0_da16_swiz_z(devinfo, inst,
245 BRW_GET_SWZ(reg.swizzle, BRW_CHANNEL_Z));
246 brw_inst_set_src0_da16_swiz_w(devinfo, inst,
247 BRW_GET_SWZ(reg.swizzle, BRW_CHANNEL_W));
248
249 if (reg.vstride == BRW_VERTICAL_STRIDE_8) {
250 /* This is an oddity of the fact we're using the same
251 * descriptions for registers in align_16 as align_1:
252 */
253 brw_inst_set_src0_vstride(devinfo, inst, BRW_VERTICAL_STRIDE_4);
254 } else if (devinfo->gen == 7 && !devinfo->is_haswell &&
255 reg.type == BRW_REGISTER_TYPE_DF &&
256 reg.vstride == BRW_VERTICAL_STRIDE_2) {
257 /* From SNB PRM:
258 *
259 * "For Align16 access mode, only encodings of 0000 and 0011
260 * are allowed. Other codes are reserved."
261 *
262 * Presumably the DevSNB behavior applies to IVB as well.
263 */
264 brw_inst_set_src0_vstride(devinfo, inst, BRW_VERTICAL_STRIDE_4);
265 } else {
266 brw_inst_set_src0_vstride(devinfo, inst, reg.vstride);
267 }
268 }
269 }
270 }
271
272
273 void
274 brw_set_src1(struct brw_codegen *p, brw_inst *inst, struct brw_reg reg)
275 {
276 const struct gen_device_info *devinfo = p->devinfo;
277
278 if (reg.file != BRW_ARCHITECTURE_REGISTER_FILE)
279 assert(reg.nr < 128);
280
281 /* From the IVB PRM Vol. 4, Pt. 3, Section 3.3.3.5:
282 *
283 * "Accumulator registers may be accessed explicitly as src0
284 * operands only."
285 */
286 assert(reg.file != BRW_ARCHITECTURE_REGISTER_FILE ||
287 reg.nr != BRW_ARF_ACCUMULATOR);
288
289 gen7_convert_mrf_to_grf(p, &reg);
290 assert(reg.file != BRW_MESSAGE_REGISTER_FILE);
291
292 brw_inst_set_src1_file_type(devinfo, inst, reg.file, reg.type);
293 brw_inst_set_src1_abs(devinfo, inst, reg.abs);
294 brw_inst_set_src1_negate(devinfo, inst, reg.negate);
295
296 /* Only src1 can be immediate in two-argument instructions.
297 */
298 assert(brw_inst_src0_reg_file(devinfo, inst) != BRW_IMMEDIATE_VALUE);
299
300 if (reg.file == BRW_IMMEDIATE_VALUE) {
301 /* two-argument instructions can only use 32-bit immediates */
302 assert(type_sz(reg.type) < 8);
303 brw_inst_set_imm_ud(devinfo, inst, reg.ud);
304 } else {
305 /* This is a hardware restriction, which may or may not be lifted
306 * in the future:
307 */
308 assert (reg.address_mode == BRW_ADDRESS_DIRECT);
309 /* assert (reg.file == BRW_GENERAL_REGISTER_FILE); */
310
311 brw_inst_set_src1_da_reg_nr(devinfo, inst, reg.nr);
312 if (brw_inst_access_mode(devinfo, inst) == BRW_ALIGN_1) {
313 brw_inst_set_src1_da1_subreg_nr(devinfo, inst, reg.subnr);
314 } else {
315 brw_inst_set_src1_da16_subreg_nr(devinfo, inst, reg.subnr / 16);
316 }
317
318 if (brw_inst_access_mode(devinfo, inst) == BRW_ALIGN_1) {
319 if (reg.width == BRW_WIDTH_1 &&
320 brw_inst_exec_size(devinfo, inst) == BRW_EXECUTE_1) {
321 brw_inst_set_src1_hstride(devinfo, inst, BRW_HORIZONTAL_STRIDE_0);
322 brw_inst_set_src1_width(devinfo, inst, BRW_WIDTH_1);
323 brw_inst_set_src1_vstride(devinfo, inst, BRW_VERTICAL_STRIDE_0);
324 } else {
325 brw_inst_set_src1_hstride(devinfo, inst, reg.hstride);
326 brw_inst_set_src1_width(devinfo, inst, reg.width);
327 brw_inst_set_src1_vstride(devinfo, inst, reg.vstride);
328 }
329 } else {
330 brw_inst_set_src1_da16_swiz_x(devinfo, inst,
331 BRW_GET_SWZ(reg.swizzle, BRW_CHANNEL_X));
332 brw_inst_set_src1_da16_swiz_y(devinfo, inst,
333 BRW_GET_SWZ(reg.swizzle, BRW_CHANNEL_Y));
334 brw_inst_set_src1_da16_swiz_z(devinfo, inst,
335 BRW_GET_SWZ(reg.swizzle, BRW_CHANNEL_Z));
336 brw_inst_set_src1_da16_swiz_w(devinfo, inst,
337 BRW_GET_SWZ(reg.swizzle, BRW_CHANNEL_W));
338
339 if (reg.vstride == BRW_VERTICAL_STRIDE_8) {
340 /* This is an oddity of the fact we're using the same
341 * descriptions for registers in align_16 as align_1:
342 */
343 brw_inst_set_src1_vstride(devinfo, inst, BRW_VERTICAL_STRIDE_4);
344 } else if (devinfo->gen == 7 && !devinfo->is_haswell &&
345 reg.type == BRW_REGISTER_TYPE_DF &&
346 reg.vstride == BRW_VERTICAL_STRIDE_2) {
347 /* From SNB PRM:
348 *
349 * "For Align16 access mode, only encodings of 0000 and 0011
350 * are allowed. Other codes are reserved."
351 *
352 * Presumably the DevSNB behavior applies to IVB as well.
353 */
354 brw_inst_set_src1_vstride(devinfo, inst, BRW_VERTICAL_STRIDE_4);
355 } else {
356 brw_inst_set_src1_vstride(devinfo, inst, reg.vstride);
357 }
358 }
359 }
360 }
361
362 /**
363 * Set the Message Descriptor and Extended Message Descriptor fields
364 * for SEND messages.
365 *
366 * \note This zeroes out the Function Control bits, so it must be called
367 * \b before filling out any message-specific data. Callers can
368 * choose not to fill in irrelevant bits; they will be zero.
369 */
370 void
371 brw_set_message_descriptor(struct brw_codegen *p,
372 brw_inst *inst,
373 enum brw_message_target sfid,
374 unsigned msg_length,
375 unsigned response_length,
376 bool header_present,
377 bool end_of_thread)
378 {
379 const struct gen_device_info *devinfo = p->devinfo;
380
381 brw_set_src1(p, inst, brw_imm_d(0));
382
383 /* For indirect sends, `inst` will not be the SEND/SENDC instruction
384 * itself; instead, it will be a MOV/OR into the address register.
385 *
386 * In this case, we avoid setting the extended message descriptor bits,
387 * since they go on the later SEND/SENDC instead and if set here would
388 * instead clobber the conditionalmod bits.
389 */
390 unsigned opcode = brw_inst_opcode(devinfo, inst);
391 if (opcode == BRW_OPCODE_SEND || opcode == BRW_OPCODE_SENDC) {
392 brw_inst_set_sfid(devinfo, inst, sfid);
393 }
394
395 brw_inst_set_mlen(devinfo, inst, msg_length);
396 brw_inst_set_rlen(devinfo, inst, response_length);
397 brw_inst_set_eot(devinfo, inst, end_of_thread);
398
399 if (devinfo->gen >= 5) {
400 brw_inst_set_header_present(devinfo, inst, header_present);
401 }
402 }
403
404 static void brw_set_math_message( struct brw_codegen *p,
405 brw_inst *inst,
406 unsigned function,
407 unsigned integer_type,
408 bool low_precision,
409 unsigned dataType )
410 {
411 const struct gen_device_info *devinfo = p->devinfo;
412 unsigned msg_length;
413 unsigned response_length;
414
415 /* Infer message length from the function */
416 switch (function) {
417 case BRW_MATH_FUNCTION_POW:
418 case BRW_MATH_FUNCTION_INT_DIV_QUOTIENT:
419 case BRW_MATH_FUNCTION_INT_DIV_REMAINDER:
420 case BRW_MATH_FUNCTION_INT_DIV_QUOTIENT_AND_REMAINDER:
421 msg_length = 2;
422 break;
423 default:
424 msg_length = 1;
425 break;
426 }
427
428 /* Infer response length from the function */
429 switch (function) {
430 case BRW_MATH_FUNCTION_SINCOS:
431 case BRW_MATH_FUNCTION_INT_DIV_QUOTIENT_AND_REMAINDER:
432 response_length = 2;
433 break;
434 default:
435 response_length = 1;
436 break;
437 }
438
439
440 brw_set_message_descriptor(p, inst, BRW_SFID_MATH,
441 msg_length, response_length, false, false);
442 brw_inst_set_math_msg_function(devinfo, inst, function);
443 brw_inst_set_math_msg_signed_int(devinfo, inst, integer_type);
444 brw_inst_set_math_msg_precision(devinfo, inst, low_precision);
445 brw_inst_set_math_msg_saturate(devinfo, inst, brw_inst_saturate(devinfo, inst));
446 brw_inst_set_math_msg_data_type(devinfo, inst, dataType);
447 brw_inst_set_saturate(devinfo, inst, 0);
448 }
449
450
451 static void brw_set_ff_sync_message(struct brw_codegen *p,
452 brw_inst *insn,
453 bool allocate,
454 unsigned response_length,
455 bool end_of_thread)
456 {
457 const struct gen_device_info *devinfo = p->devinfo;
458
459 brw_set_message_descriptor(p, insn, BRW_SFID_URB,
460 1, response_length, true, end_of_thread);
461 brw_inst_set_urb_opcode(devinfo, insn, 1); /* FF_SYNC */
462 brw_inst_set_urb_allocate(devinfo, insn, allocate);
463 /* The following fields are not used by FF_SYNC: */
464 brw_inst_set_urb_global_offset(devinfo, insn, 0);
465 brw_inst_set_urb_swizzle_control(devinfo, insn, 0);
466 brw_inst_set_urb_used(devinfo, insn, 0);
467 brw_inst_set_urb_complete(devinfo, insn, 0);
468 }
469
470 static void brw_set_urb_message( struct brw_codegen *p,
471 brw_inst *insn,
472 enum brw_urb_write_flags flags,
473 unsigned msg_length,
474 unsigned response_length,
475 unsigned offset,
476 unsigned swizzle_control )
477 {
478 const struct gen_device_info *devinfo = p->devinfo;
479
480 assert(devinfo->gen < 7 || swizzle_control != BRW_URB_SWIZZLE_TRANSPOSE);
481 assert(devinfo->gen < 7 || !(flags & BRW_URB_WRITE_ALLOCATE));
482 assert(devinfo->gen >= 7 || !(flags & BRW_URB_WRITE_PER_SLOT_OFFSET));
483
484 brw_set_message_descriptor(p, insn, BRW_SFID_URB,
485 msg_length, response_length, true,
486 flags & BRW_URB_WRITE_EOT);
487
488 if (flags & BRW_URB_WRITE_OWORD) {
489 assert(msg_length == 2); /* header + one OWORD of data */
490 brw_inst_set_urb_opcode(devinfo, insn, BRW_URB_OPCODE_WRITE_OWORD);
491 } else {
492 brw_inst_set_urb_opcode(devinfo, insn, BRW_URB_OPCODE_WRITE_HWORD);
493 }
494
495 brw_inst_set_urb_global_offset(devinfo, insn, offset);
496 brw_inst_set_urb_swizzle_control(devinfo, insn, swizzle_control);
497
498 if (devinfo->gen < 8) {
499 brw_inst_set_urb_complete(devinfo, insn, !!(flags & BRW_URB_WRITE_COMPLETE));
500 }
501
502 if (devinfo->gen < 7) {
503 brw_inst_set_urb_allocate(devinfo, insn, !!(flags & BRW_URB_WRITE_ALLOCATE));
504 brw_inst_set_urb_used(devinfo, insn, !(flags & BRW_URB_WRITE_UNUSED));
505 } else {
506 brw_inst_set_urb_per_slot_offset(devinfo, insn,
507 !!(flags & BRW_URB_WRITE_PER_SLOT_OFFSET));
508 }
509 }
510
511 void
512 brw_set_dp_write_message(struct brw_codegen *p,
513 brw_inst *insn,
514 unsigned binding_table_index,
515 unsigned msg_control,
516 unsigned msg_type,
517 unsigned target_cache,
518 unsigned msg_length,
519 bool header_present,
520 unsigned last_render_target,
521 unsigned response_length,
522 unsigned end_of_thread,
523 unsigned send_commit_msg)
524 {
525 const struct gen_device_info *devinfo = p->devinfo;
526 const unsigned sfid = (devinfo->gen >= 6 ? target_cache :
527 BRW_SFID_DATAPORT_WRITE);
528
529 brw_set_message_descriptor(p, insn, sfid, msg_length, response_length,
530 header_present, end_of_thread);
531
532 brw_inst_set_binding_table_index(devinfo, insn, binding_table_index);
533 brw_inst_set_dp_write_msg_type(devinfo, insn, msg_type);
534 brw_inst_set_dp_write_msg_control(devinfo, insn, msg_control);
535 brw_inst_set_rt_last(devinfo, insn, last_render_target);
536 if (devinfo->gen < 7) {
537 brw_inst_set_dp_write_commit(devinfo, insn, send_commit_msg);
538 }
539 }
540
541 void
542 brw_set_dp_read_message(struct brw_codegen *p,
543 brw_inst *insn,
544 unsigned binding_table_index,
545 unsigned msg_control,
546 unsigned msg_type,
547 unsigned target_cache,
548 unsigned msg_length,
549 bool header_present,
550 unsigned response_length)
551 {
552 const struct gen_device_info *devinfo = p->devinfo;
553 const unsigned sfid = (devinfo->gen >= 6 ? target_cache :
554 BRW_SFID_DATAPORT_READ);
555
556 brw_set_message_descriptor(p, insn, sfid, msg_length, response_length,
557 header_present, false);
558
559 brw_inst_set_binding_table_index(devinfo, insn, binding_table_index);
560 brw_inst_set_dp_read_msg_type(devinfo, insn, msg_type);
561 brw_inst_set_dp_read_msg_control(devinfo, insn, msg_control);
562 if (devinfo->gen < 6)
563 brw_inst_set_dp_read_target_cache(devinfo, insn, target_cache);
564 }
565
566 void
567 brw_set_sampler_message(struct brw_codegen *p,
568 brw_inst *inst,
569 unsigned binding_table_index,
570 unsigned sampler,
571 unsigned msg_type,
572 unsigned response_length,
573 unsigned msg_length,
574 unsigned header_present,
575 unsigned simd_mode,
576 unsigned return_format)
577 {
578 const struct gen_device_info *devinfo = p->devinfo;
579
580 brw_set_message_descriptor(p, inst, BRW_SFID_SAMPLER, msg_length,
581 response_length, header_present, false);
582
583 brw_inst_set_binding_table_index(devinfo, inst, binding_table_index);
584 brw_inst_set_sampler(devinfo, inst, sampler);
585 brw_inst_set_sampler_msg_type(devinfo, inst, msg_type);
586 if (devinfo->gen >= 5) {
587 brw_inst_set_sampler_simd_mode(devinfo, inst, simd_mode);
588 } else if (devinfo->gen == 4 && !devinfo->is_g4x) {
589 brw_inst_set_sampler_return_format(devinfo, inst, return_format);
590 }
591 }
592
593 static void
594 gen7_set_dp_scratch_message(struct brw_codegen *p,
595 brw_inst *inst,
596 bool write,
597 bool dword,
598 bool invalidate_after_read,
599 unsigned num_regs,
600 unsigned addr_offset,
601 unsigned mlen,
602 unsigned rlen,
603 bool header_present)
604 {
605 const struct gen_device_info *devinfo = p->devinfo;
606 assert(num_regs == 1 || num_regs == 2 || num_regs == 4 ||
607 (devinfo->gen >= 8 && num_regs == 8));
608 const unsigned block_size = (devinfo->gen >= 8 ? _mesa_logbase2(num_regs) :
609 num_regs - 1);
610
611 brw_set_message_descriptor(p, inst, GEN7_SFID_DATAPORT_DATA_CACHE,
612 mlen, rlen, header_present, false);
613 brw_inst_set_dp_category(devinfo, inst, 1); /* Scratch Block Read/Write msgs */
614 brw_inst_set_scratch_read_write(devinfo, inst, write);
615 brw_inst_set_scratch_type(devinfo, inst, dword);
616 brw_inst_set_scratch_invalidate_after_read(devinfo, inst, invalidate_after_read);
617 brw_inst_set_scratch_block_size(devinfo, inst, block_size);
618 brw_inst_set_scratch_addr_offset(devinfo, inst, addr_offset);
619 }
620
621 #define next_insn brw_next_insn
622 brw_inst *
623 brw_next_insn(struct brw_codegen *p, unsigned opcode)
624 {
625 const struct gen_device_info *devinfo = p->devinfo;
626 brw_inst *insn;
627
628 if (p->nr_insn + 1 > p->store_size) {
629 p->store_size <<= 1;
630 p->store = reralloc(p->mem_ctx, p->store, brw_inst, p->store_size);
631 }
632
633 p->next_insn_offset += 16;
634 insn = &p->store[p->nr_insn++];
635 memcpy(insn, p->current, sizeof(*insn));
636
637 brw_inst_set_opcode(devinfo, insn, opcode);
638 return insn;
639 }
640
641 static brw_inst *
642 brw_alu1(struct brw_codegen *p, unsigned opcode,
643 struct brw_reg dest, struct brw_reg src)
644 {
645 brw_inst *insn = next_insn(p, opcode);
646 brw_set_dest(p, insn, dest);
647 brw_set_src0(p, insn, src);
648 return insn;
649 }
650
651 static brw_inst *
652 brw_alu2(struct brw_codegen *p, unsigned opcode,
653 struct brw_reg dest, struct brw_reg src0, struct brw_reg src1)
654 {
655 /* 64-bit immediates are only supported on 1-src instructions */
656 assert(src0.file != BRW_IMMEDIATE_VALUE || type_sz(src0.type) <= 4);
657 assert(src1.file != BRW_IMMEDIATE_VALUE || type_sz(src1.type) <= 4);
658
659 brw_inst *insn = next_insn(p, opcode);
660 brw_set_dest(p, insn, dest);
661 brw_set_src0(p, insn, src0);
662 brw_set_src1(p, insn, src1);
663 return insn;
664 }
665
666 static int
667 get_3src_subreg_nr(struct brw_reg reg)
668 {
669 /* Normally, SubRegNum is in bytes (0..31). However, 3-src instructions
670 * use 32-bit units (components 0..7). Since they only support F/D/UD
671 * types, this doesn't lose any flexibility, but uses fewer bits.
672 */
673 return reg.subnr / 4;
674 }
675
676 static brw_inst *
677 brw_alu3(struct brw_codegen *p, unsigned opcode, struct brw_reg dest,
678 struct brw_reg src0, struct brw_reg src1, struct brw_reg src2)
679 {
680 const struct gen_device_info *devinfo = p->devinfo;
681 brw_inst *inst = next_insn(p, opcode);
682
683 gen7_convert_mrf_to_grf(p, &dest);
684
685 assert(dest.nr < 128);
686 assert(src0.nr < 128);
687 assert(src1.nr < 128);
688 assert(src2.nr < 128);
689 assert(dest.address_mode == BRW_ADDRESS_DIRECT);
690 assert(src0.address_mode == BRW_ADDRESS_DIRECT);
691 assert(src1.address_mode == BRW_ADDRESS_DIRECT);
692 assert(src2.address_mode == BRW_ADDRESS_DIRECT);
693
694 if (brw_inst_access_mode(devinfo, inst) == BRW_ALIGN_1) {
695 assert(dest.file == BRW_GENERAL_REGISTER_FILE ||
696 dest.file == BRW_ARCHITECTURE_REGISTER_FILE);
697
698 if (dest.file == BRW_ARCHITECTURE_REGISTER_FILE) {
699 brw_inst_set_3src_a1_dst_reg_file(devinfo, inst,
700 BRW_ALIGN1_3SRC_ACCUMULATOR);
701 brw_inst_set_3src_dst_reg_nr(devinfo, inst, BRW_ARF_ACCUMULATOR);
702 } else {
703 brw_inst_set_3src_a1_dst_reg_file(devinfo, inst,
704 BRW_ALIGN1_3SRC_GENERAL_REGISTER_FILE);
705 brw_inst_set_3src_dst_reg_nr(devinfo, inst, dest.nr);
706 }
707 brw_inst_set_3src_a1_dst_subreg_nr(devinfo, inst, dest.subnr / 8);
708
709 brw_inst_set_3src_a1_dst_hstride(devinfo, inst, BRW_ALIGN1_3SRC_DST_HORIZONTAL_STRIDE_1);
710
711 if (brw_reg_type_is_floating_point(dest.type)) {
712 brw_inst_set_3src_a1_exec_type(devinfo, inst,
713 BRW_ALIGN1_3SRC_EXEC_TYPE_FLOAT);
714 } else {
715 brw_inst_set_3src_a1_exec_type(devinfo, inst,
716 BRW_ALIGN1_3SRC_EXEC_TYPE_INT);
717 }
718
719 brw_inst_set_3src_a1_dst_type(devinfo, inst, dest.type);
720 brw_inst_set_3src_a1_src0_type(devinfo, inst, src0.type);
721 brw_inst_set_3src_a1_src1_type(devinfo, inst, src1.type);
722 brw_inst_set_3src_a1_src2_type(devinfo, inst, src2.type);
723
724 assert((src0.vstride == BRW_VERTICAL_STRIDE_0 &&
725 src0.hstride == BRW_HORIZONTAL_STRIDE_0) ||
726 (src0.vstride == BRW_VERTICAL_STRIDE_8 &&
727 src0.hstride == BRW_HORIZONTAL_STRIDE_1));
728 assert((src1.vstride == BRW_VERTICAL_STRIDE_0 &&
729 src1.hstride == BRW_HORIZONTAL_STRIDE_0) ||
730 (src1.vstride == BRW_VERTICAL_STRIDE_8 &&
731 src1.hstride == BRW_HORIZONTAL_STRIDE_1));
732 assert((src2.vstride == BRW_VERTICAL_STRIDE_0 &&
733 src2.hstride == BRW_HORIZONTAL_STRIDE_0) ||
734 (src2.vstride == BRW_VERTICAL_STRIDE_8 &&
735 src2.hstride == BRW_HORIZONTAL_STRIDE_1));
736
737 brw_inst_set_3src_a1_src0_vstride(devinfo, inst,
738 src0.vstride == BRW_VERTICAL_STRIDE_0 ?
739 BRW_ALIGN1_3SRC_VERTICAL_STRIDE_0 :
740 BRW_ALIGN1_3SRC_VERTICAL_STRIDE_8);
741 brw_inst_set_3src_a1_src1_vstride(devinfo, inst,
742 src1.vstride == BRW_VERTICAL_STRIDE_0 ?
743 BRW_ALIGN1_3SRC_VERTICAL_STRIDE_0 :
744 BRW_ALIGN1_3SRC_VERTICAL_STRIDE_8);
745 /* no vstride on src2 */
746
747 brw_inst_set_3src_a1_src0_hstride(devinfo, inst,
748 src0.hstride == BRW_HORIZONTAL_STRIDE_0 ?
749 BRW_ALIGN1_3SRC_SRC_HORIZONTAL_STRIDE_0 :
750 BRW_ALIGN1_3SRC_SRC_HORIZONTAL_STRIDE_1);
751 brw_inst_set_3src_a1_src1_hstride(devinfo, inst,
752 src1.hstride == BRW_HORIZONTAL_STRIDE_0 ?
753 BRW_ALIGN1_3SRC_SRC_HORIZONTAL_STRIDE_0 :
754 BRW_ALIGN1_3SRC_SRC_HORIZONTAL_STRIDE_1);
755 brw_inst_set_3src_a1_src2_hstride(devinfo, inst,
756 src2.hstride == BRW_HORIZONTAL_STRIDE_0 ?
757 BRW_ALIGN1_3SRC_SRC_HORIZONTAL_STRIDE_0 :
758 BRW_ALIGN1_3SRC_SRC_HORIZONTAL_STRIDE_1);
759
760 brw_inst_set_3src_a1_src0_subreg_nr(devinfo, inst, src0.subnr);
761 brw_inst_set_3src_src0_reg_nr(devinfo, inst, src0.nr);
762 brw_inst_set_3src_src0_abs(devinfo, inst, src0.abs);
763 brw_inst_set_3src_src0_negate(devinfo, inst, src0.negate);
764
765 brw_inst_set_3src_a1_src1_subreg_nr(devinfo, inst, src1.subnr);
766 if (src1.file == BRW_ARCHITECTURE_REGISTER_FILE) {
767 brw_inst_set_3src_src1_reg_nr(devinfo, inst, BRW_ARF_ACCUMULATOR);
768 } else {
769 brw_inst_set_3src_src1_reg_nr(devinfo, inst, src1.nr);
770 }
771 brw_inst_set_3src_src1_abs(devinfo, inst, src1.abs);
772 brw_inst_set_3src_src1_negate(devinfo, inst, src1.negate);
773
774 brw_inst_set_3src_a1_src2_subreg_nr(devinfo, inst, src2.subnr);
775 brw_inst_set_3src_src2_reg_nr(devinfo, inst, src2.nr);
776 brw_inst_set_3src_src2_abs(devinfo, inst, src2.abs);
777 brw_inst_set_3src_src2_negate(devinfo, inst, src2.negate);
778
779 assert(src0.file == BRW_GENERAL_REGISTER_FILE ||
780 src0.file == BRW_IMMEDIATE_VALUE);
781 assert(src1.file == BRW_GENERAL_REGISTER_FILE ||
782 src1.file == BRW_ARCHITECTURE_REGISTER_FILE);
783 assert(src2.file == BRW_GENERAL_REGISTER_FILE ||
784 src2.file == BRW_IMMEDIATE_VALUE);
785
786 brw_inst_set_3src_a1_src0_reg_file(devinfo, inst,
787 src0.file == BRW_GENERAL_REGISTER_FILE ?
788 BRW_ALIGN1_3SRC_GENERAL_REGISTER_FILE :
789 BRW_ALIGN1_3SRC_IMMEDIATE_VALUE);
790 brw_inst_set_3src_a1_src1_reg_file(devinfo, inst,
791 src1.file == BRW_GENERAL_REGISTER_FILE ?
792 BRW_ALIGN1_3SRC_GENERAL_REGISTER_FILE :
793 BRW_ALIGN1_3SRC_ACCUMULATOR);
794 brw_inst_set_3src_a1_src2_reg_file(devinfo, inst,
795 src2.file == BRW_GENERAL_REGISTER_FILE ?
796 BRW_ALIGN1_3SRC_GENERAL_REGISTER_FILE :
797 BRW_ALIGN1_3SRC_IMMEDIATE_VALUE);
798 } else {
799 assert(dest.file == BRW_GENERAL_REGISTER_FILE ||
800 dest.file == BRW_MESSAGE_REGISTER_FILE);
801 assert(dest.type == BRW_REGISTER_TYPE_F ||
802 dest.type == BRW_REGISTER_TYPE_DF ||
803 dest.type == BRW_REGISTER_TYPE_D ||
804 dest.type == BRW_REGISTER_TYPE_UD);
805 if (devinfo->gen == 6) {
806 brw_inst_set_3src_a16_dst_reg_file(devinfo, inst,
807 dest.file == BRW_MESSAGE_REGISTER_FILE);
808 }
809 brw_inst_set_3src_dst_reg_nr(devinfo, inst, dest.nr);
810 brw_inst_set_3src_a16_dst_subreg_nr(devinfo, inst, dest.subnr / 16);
811 brw_inst_set_3src_a16_dst_writemask(devinfo, inst, dest.writemask);
812
813 assert(src0.file == BRW_GENERAL_REGISTER_FILE);
814 brw_inst_set_3src_a16_src0_swizzle(devinfo, inst, src0.swizzle);
815 brw_inst_set_3src_a16_src0_subreg_nr(devinfo, inst, get_3src_subreg_nr(src0));
816 brw_inst_set_3src_src0_reg_nr(devinfo, inst, src0.nr);
817 brw_inst_set_3src_src0_abs(devinfo, inst, src0.abs);
818 brw_inst_set_3src_src0_negate(devinfo, inst, src0.negate);
819 brw_inst_set_3src_a16_src0_rep_ctrl(devinfo, inst,
820 src0.vstride == BRW_VERTICAL_STRIDE_0);
821
822 assert(src1.file == BRW_GENERAL_REGISTER_FILE);
823 brw_inst_set_3src_a16_src1_swizzle(devinfo, inst, src1.swizzle);
824 brw_inst_set_3src_a16_src1_subreg_nr(devinfo, inst, get_3src_subreg_nr(src1));
825 brw_inst_set_3src_src1_reg_nr(devinfo, inst, src1.nr);
826 brw_inst_set_3src_src1_abs(devinfo, inst, src1.abs);
827 brw_inst_set_3src_src1_negate(devinfo, inst, src1.negate);
828 brw_inst_set_3src_a16_src1_rep_ctrl(devinfo, inst,
829 src1.vstride == BRW_VERTICAL_STRIDE_0);
830
831 assert(src2.file == BRW_GENERAL_REGISTER_FILE);
832 brw_inst_set_3src_a16_src2_swizzle(devinfo, inst, src2.swizzle);
833 brw_inst_set_3src_a16_src2_subreg_nr(devinfo, inst, get_3src_subreg_nr(src2));
834 brw_inst_set_3src_src2_reg_nr(devinfo, inst, src2.nr);
835 brw_inst_set_3src_src2_abs(devinfo, inst, src2.abs);
836 brw_inst_set_3src_src2_negate(devinfo, inst, src2.negate);
837 brw_inst_set_3src_a16_src2_rep_ctrl(devinfo, inst,
838 src2.vstride == BRW_VERTICAL_STRIDE_0);
839
840 if (devinfo->gen >= 7) {
841 /* Set both the source and destination types based on dest.type,
842 * ignoring the source register types. The MAD and LRP emitters ensure
843 * that all four types are float. The BFE and BFI2 emitters, however,
844 * may send us mixed D and UD types and want us to ignore that and use
845 * the destination type.
846 */
847 brw_inst_set_3src_a16_src_type(devinfo, inst, dest.type);
848 brw_inst_set_3src_a16_dst_type(devinfo, inst, dest.type);
849 }
850 }
851
852 return inst;
853 }
854
855
856 /***********************************************************************
857 * Convenience routines.
858 */
859 #define ALU1(OP) \
860 brw_inst *brw_##OP(struct brw_codegen *p, \
861 struct brw_reg dest, \
862 struct brw_reg src0) \
863 { \
864 return brw_alu1(p, BRW_OPCODE_##OP, dest, src0); \
865 }
866
867 #define ALU2(OP) \
868 brw_inst *brw_##OP(struct brw_codegen *p, \
869 struct brw_reg dest, \
870 struct brw_reg src0, \
871 struct brw_reg src1) \
872 { \
873 return brw_alu2(p, BRW_OPCODE_##OP, dest, src0, src1); \
874 }
875
876 #define ALU3(OP) \
877 brw_inst *brw_##OP(struct brw_codegen *p, \
878 struct brw_reg dest, \
879 struct brw_reg src0, \
880 struct brw_reg src1, \
881 struct brw_reg src2) \
882 { \
883 return brw_alu3(p, BRW_OPCODE_##OP, dest, src0, src1, src2); \
884 }
885
886 #define ALU3F(OP) \
887 brw_inst *brw_##OP(struct brw_codegen *p, \
888 struct brw_reg dest, \
889 struct brw_reg src0, \
890 struct brw_reg src1, \
891 struct brw_reg src2) \
892 { \
893 assert(dest.type == BRW_REGISTER_TYPE_F || \
894 dest.type == BRW_REGISTER_TYPE_DF); \
895 if (dest.type == BRW_REGISTER_TYPE_F) { \
896 assert(src0.type == BRW_REGISTER_TYPE_F); \
897 assert(src1.type == BRW_REGISTER_TYPE_F); \
898 assert(src2.type == BRW_REGISTER_TYPE_F); \
899 } else if (dest.type == BRW_REGISTER_TYPE_DF) { \
900 assert(src0.type == BRW_REGISTER_TYPE_DF); \
901 assert(src1.type == BRW_REGISTER_TYPE_DF); \
902 assert(src2.type == BRW_REGISTER_TYPE_DF); \
903 } \
904 return brw_alu3(p, BRW_OPCODE_##OP, dest, src0, src1, src2); \
905 }
906
907 /* Rounding operations (other than RNDD) require two instructions - the first
908 * stores a rounded value (possibly the wrong way) in the dest register, but
909 * also sets a per-channel "increment bit" in the flag register. A predicated
910 * add of 1.0 fixes dest to contain the desired result.
911 *
912 * Sandybridge and later appear to round correctly without an ADD.
913 */
914 #define ROUND(OP) \
915 void brw_##OP(struct brw_codegen *p, \
916 struct brw_reg dest, \
917 struct brw_reg src) \
918 { \
919 const struct gen_device_info *devinfo = p->devinfo; \
920 brw_inst *rnd, *add; \
921 rnd = next_insn(p, BRW_OPCODE_##OP); \
922 brw_set_dest(p, rnd, dest); \
923 brw_set_src0(p, rnd, src); \
924 \
925 if (devinfo->gen < 6) { \
926 /* turn on round-increments */ \
927 brw_inst_set_cond_modifier(devinfo, rnd, BRW_CONDITIONAL_R); \
928 add = brw_ADD(p, dest, dest, brw_imm_f(1.0f)); \
929 brw_inst_set_pred_control(devinfo, add, BRW_PREDICATE_NORMAL); \
930 } \
931 }
932
933
934 ALU2(SEL)
935 ALU1(NOT)
936 ALU2(AND)
937 ALU2(OR)
938 ALU2(XOR)
939 ALU2(SHR)
940 ALU2(SHL)
941 ALU1(DIM)
942 ALU2(ASR)
943 ALU1(FRC)
944 ALU1(RNDD)
945 ALU2(MAC)
946 ALU2(MACH)
947 ALU1(LZD)
948 ALU2(DP4)
949 ALU2(DPH)
950 ALU2(DP3)
951 ALU2(DP2)
952 ALU3F(MAD)
953 ALU3F(LRP)
954 ALU1(BFREV)
955 ALU3(BFE)
956 ALU2(BFI1)
957 ALU3(BFI2)
958 ALU1(FBH)
959 ALU1(FBL)
960 ALU1(CBIT)
961 ALU2(ADDC)
962 ALU2(SUBB)
963
964 ROUND(RNDZ)
965 ROUND(RNDE)
966
967 brw_inst *
968 brw_MOV(struct brw_codegen *p, struct brw_reg dest, struct brw_reg src0)
969 {
970 const struct gen_device_info *devinfo = p->devinfo;
971
972 /* When converting F->DF on IVB/BYT, every odd source channel is ignored.
973 * To avoid the problems that causes, we use a <1,2,0> source region to read
974 * each element twice.
975 */
976 if (devinfo->gen == 7 && !devinfo->is_haswell &&
977 brw_inst_access_mode(devinfo, p->current) == BRW_ALIGN_1 &&
978 dest.type == BRW_REGISTER_TYPE_DF &&
979 (src0.type == BRW_REGISTER_TYPE_F ||
980 src0.type == BRW_REGISTER_TYPE_D ||
981 src0.type == BRW_REGISTER_TYPE_UD) &&
982 !has_scalar_region(src0)) {
983 assert(src0.vstride == BRW_VERTICAL_STRIDE_4 &&
984 src0.width == BRW_WIDTH_4 &&
985 src0.hstride == BRW_HORIZONTAL_STRIDE_1);
986
987 src0.vstride = BRW_VERTICAL_STRIDE_1;
988 src0.width = BRW_WIDTH_2;
989 src0.hstride = BRW_HORIZONTAL_STRIDE_0;
990 }
991
992 return brw_alu1(p, BRW_OPCODE_MOV, dest, src0);
993 }
994
995 brw_inst *
996 brw_ADD(struct brw_codegen *p, struct brw_reg dest,
997 struct brw_reg src0, struct brw_reg src1)
998 {
999 /* 6.2.2: add */
1000 if (src0.type == BRW_REGISTER_TYPE_F ||
1001 (src0.file == BRW_IMMEDIATE_VALUE &&
1002 src0.type == BRW_REGISTER_TYPE_VF)) {
1003 assert(src1.type != BRW_REGISTER_TYPE_UD);
1004 assert(src1.type != BRW_REGISTER_TYPE_D);
1005 }
1006
1007 if (src1.type == BRW_REGISTER_TYPE_F ||
1008 (src1.file == BRW_IMMEDIATE_VALUE &&
1009 src1.type == BRW_REGISTER_TYPE_VF)) {
1010 assert(src0.type != BRW_REGISTER_TYPE_UD);
1011 assert(src0.type != BRW_REGISTER_TYPE_D);
1012 }
1013
1014 return brw_alu2(p, BRW_OPCODE_ADD, dest, src0, src1);
1015 }
1016
1017 brw_inst *
1018 brw_AVG(struct brw_codegen *p, struct brw_reg dest,
1019 struct brw_reg src0, struct brw_reg src1)
1020 {
1021 assert(dest.type == src0.type);
1022 assert(src0.type == src1.type);
1023 switch (src0.type) {
1024 case BRW_REGISTER_TYPE_B:
1025 case BRW_REGISTER_TYPE_UB:
1026 case BRW_REGISTER_TYPE_W:
1027 case BRW_REGISTER_TYPE_UW:
1028 case BRW_REGISTER_TYPE_D:
1029 case BRW_REGISTER_TYPE_UD:
1030 break;
1031 default:
1032 unreachable("Bad type for brw_AVG");
1033 }
1034
1035 return brw_alu2(p, BRW_OPCODE_AVG, dest, src0, src1);
1036 }
1037
1038 brw_inst *
1039 brw_MUL(struct brw_codegen *p, struct brw_reg dest,
1040 struct brw_reg src0, struct brw_reg src1)
1041 {
1042 /* 6.32.38: mul */
1043 if (src0.type == BRW_REGISTER_TYPE_D ||
1044 src0.type == BRW_REGISTER_TYPE_UD ||
1045 src1.type == BRW_REGISTER_TYPE_D ||
1046 src1.type == BRW_REGISTER_TYPE_UD) {
1047 assert(dest.type != BRW_REGISTER_TYPE_F);
1048 }
1049
1050 if (src0.type == BRW_REGISTER_TYPE_F ||
1051 (src0.file == BRW_IMMEDIATE_VALUE &&
1052 src0.type == BRW_REGISTER_TYPE_VF)) {
1053 assert(src1.type != BRW_REGISTER_TYPE_UD);
1054 assert(src1.type != BRW_REGISTER_TYPE_D);
1055 }
1056
1057 if (src1.type == BRW_REGISTER_TYPE_F ||
1058 (src1.file == BRW_IMMEDIATE_VALUE &&
1059 src1.type == BRW_REGISTER_TYPE_VF)) {
1060 assert(src0.type != BRW_REGISTER_TYPE_UD);
1061 assert(src0.type != BRW_REGISTER_TYPE_D);
1062 }
1063
1064 assert(src0.file != BRW_ARCHITECTURE_REGISTER_FILE ||
1065 src0.nr != BRW_ARF_ACCUMULATOR);
1066 assert(src1.file != BRW_ARCHITECTURE_REGISTER_FILE ||
1067 src1.nr != BRW_ARF_ACCUMULATOR);
1068
1069 return brw_alu2(p, BRW_OPCODE_MUL, dest, src0, src1);
1070 }
1071
1072 brw_inst *
1073 brw_LINE(struct brw_codegen *p, struct brw_reg dest,
1074 struct brw_reg src0, struct brw_reg src1)
1075 {
1076 src0.vstride = BRW_VERTICAL_STRIDE_0;
1077 src0.width = BRW_WIDTH_1;
1078 src0.hstride = BRW_HORIZONTAL_STRIDE_0;
1079 return brw_alu2(p, BRW_OPCODE_LINE, dest, src0, src1);
1080 }
1081
1082 brw_inst *
1083 brw_PLN(struct brw_codegen *p, struct brw_reg dest,
1084 struct brw_reg src0, struct brw_reg src1)
1085 {
1086 src0.vstride = BRW_VERTICAL_STRIDE_0;
1087 src0.width = BRW_WIDTH_1;
1088 src0.hstride = BRW_HORIZONTAL_STRIDE_0;
1089 src1.vstride = BRW_VERTICAL_STRIDE_8;
1090 src1.width = BRW_WIDTH_8;
1091 src1.hstride = BRW_HORIZONTAL_STRIDE_1;
1092 return brw_alu2(p, BRW_OPCODE_PLN, dest, src0, src1);
1093 }
1094
1095 brw_inst *
1096 brw_F32TO16(struct brw_codegen *p, struct brw_reg dst, struct brw_reg src)
1097 {
1098 const struct gen_device_info *devinfo = p->devinfo;
1099 const bool align16 = brw_inst_access_mode(devinfo, p->current) == BRW_ALIGN_16;
1100 /* The F32TO16 instruction doesn't support 32-bit destination types in
1101 * Align1 mode, and neither does the Gen8 implementation in terms of a
1102 * converting MOV. Gen7 does zero out the high 16 bits in Align16 mode as
1103 * an undocumented feature.
1104 */
1105 const bool needs_zero_fill = (dst.type == BRW_REGISTER_TYPE_UD &&
1106 (!align16 || devinfo->gen >= 8));
1107 brw_inst *inst;
1108
1109 if (align16) {
1110 assert(dst.type == BRW_REGISTER_TYPE_UD);
1111 } else {
1112 assert(dst.type == BRW_REGISTER_TYPE_UD ||
1113 dst.type == BRW_REGISTER_TYPE_W ||
1114 dst.type == BRW_REGISTER_TYPE_UW ||
1115 dst.type == BRW_REGISTER_TYPE_HF);
1116 }
1117
1118 brw_push_insn_state(p);
1119
1120 if (needs_zero_fill) {
1121 brw_set_default_access_mode(p, BRW_ALIGN_1);
1122 dst = spread(retype(dst, BRW_REGISTER_TYPE_W), 2);
1123 }
1124
1125 if (devinfo->gen >= 8) {
1126 inst = brw_MOV(p, retype(dst, BRW_REGISTER_TYPE_HF), src);
1127 } else {
1128 assert(devinfo->gen == 7);
1129 inst = brw_alu1(p, BRW_OPCODE_F32TO16, dst, src);
1130 }
1131
1132 if (needs_zero_fill) {
1133 brw_inst_set_no_dd_clear(devinfo, inst, true);
1134 inst = brw_MOV(p, suboffset(dst, 1), brw_imm_w(0));
1135 brw_inst_set_no_dd_check(devinfo, inst, true);
1136 }
1137
1138 brw_pop_insn_state(p);
1139 return inst;
1140 }
1141
1142 brw_inst *
1143 brw_F16TO32(struct brw_codegen *p, struct brw_reg dst, struct brw_reg src)
1144 {
1145 const struct gen_device_info *devinfo = p->devinfo;
1146 bool align16 = brw_inst_access_mode(devinfo, p->current) == BRW_ALIGN_16;
1147
1148 if (align16) {
1149 assert(src.type == BRW_REGISTER_TYPE_UD);
1150 } else {
1151 /* From the Ivybridge PRM, Vol4, Part3, Section 6.26 f16to32:
1152 *
1153 * Because this instruction does not have a 16-bit floating-point
1154 * type, the source data type must be Word (W). The destination type
1155 * must be F (Float).
1156 */
1157 if (src.type == BRW_REGISTER_TYPE_UD)
1158 src = spread(retype(src, BRW_REGISTER_TYPE_W), 2);
1159
1160 assert(src.type == BRW_REGISTER_TYPE_W ||
1161 src.type == BRW_REGISTER_TYPE_UW ||
1162 src.type == BRW_REGISTER_TYPE_HF);
1163 }
1164
1165 if (devinfo->gen >= 8) {
1166 return brw_MOV(p, dst, retype(src, BRW_REGISTER_TYPE_HF));
1167 } else {
1168 assert(devinfo->gen == 7);
1169 return brw_alu1(p, BRW_OPCODE_F16TO32, dst, src);
1170 }
1171 }
1172
1173
1174 void brw_NOP(struct brw_codegen *p)
1175 {
1176 brw_inst *insn = next_insn(p, BRW_OPCODE_NOP);
1177 memset(insn, 0, sizeof(*insn));
1178 brw_inst_set_opcode(p->devinfo, insn, BRW_OPCODE_NOP);
1179 }
1180
1181
1182
1183
1184
1185 /***********************************************************************
1186 * Comparisons, if/else/endif
1187 */
1188
1189 brw_inst *
1190 brw_JMPI(struct brw_codegen *p, struct brw_reg index,
1191 unsigned predicate_control)
1192 {
1193 const struct gen_device_info *devinfo = p->devinfo;
1194 struct brw_reg ip = brw_ip_reg();
1195 brw_inst *inst = brw_alu2(p, BRW_OPCODE_JMPI, ip, ip, index);
1196
1197 brw_inst_set_exec_size(devinfo, inst, BRW_EXECUTE_1);
1198 brw_inst_set_qtr_control(devinfo, inst, BRW_COMPRESSION_NONE);
1199 brw_inst_set_mask_control(devinfo, inst, BRW_MASK_DISABLE);
1200 brw_inst_set_pred_control(devinfo, inst, predicate_control);
1201
1202 return inst;
1203 }
1204
1205 static void
1206 push_if_stack(struct brw_codegen *p, brw_inst *inst)
1207 {
1208 p->if_stack[p->if_stack_depth] = inst - p->store;
1209
1210 p->if_stack_depth++;
1211 if (p->if_stack_array_size <= p->if_stack_depth) {
1212 p->if_stack_array_size *= 2;
1213 p->if_stack = reralloc(p->mem_ctx, p->if_stack, int,
1214 p->if_stack_array_size);
1215 }
1216 }
1217
1218 static brw_inst *
1219 pop_if_stack(struct brw_codegen *p)
1220 {
1221 p->if_stack_depth--;
1222 return &p->store[p->if_stack[p->if_stack_depth]];
1223 }
1224
1225 static void
1226 push_loop_stack(struct brw_codegen *p, brw_inst *inst)
1227 {
1228 if (p->loop_stack_array_size <= (p->loop_stack_depth + 1)) {
1229 p->loop_stack_array_size *= 2;
1230 p->loop_stack = reralloc(p->mem_ctx, p->loop_stack, int,
1231 p->loop_stack_array_size);
1232 p->if_depth_in_loop = reralloc(p->mem_ctx, p->if_depth_in_loop, int,
1233 p->loop_stack_array_size);
1234 }
1235
1236 p->loop_stack[p->loop_stack_depth] = inst - p->store;
1237 p->loop_stack_depth++;
1238 p->if_depth_in_loop[p->loop_stack_depth] = 0;
1239 }
1240
1241 static brw_inst *
1242 get_inner_do_insn(struct brw_codegen *p)
1243 {
1244 return &p->store[p->loop_stack[p->loop_stack_depth - 1]];
1245 }
1246
1247 /* EU takes the value from the flag register and pushes it onto some
1248 * sort of a stack (presumably merging with any flag value already on
1249 * the stack). Within an if block, the flags at the top of the stack
1250 * control execution on each channel of the unit, eg. on each of the
1251 * 16 pixel values in our wm programs.
1252 *
1253 * When the matching 'else' instruction is reached (presumably by
1254 * countdown of the instruction count patched in by our ELSE/ENDIF
1255 * functions), the relevant flags are inverted.
1256 *
1257 * When the matching 'endif' instruction is reached, the flags are
1258 * popped off. If the stack is now empty, normal execution resumes.
1259 */
1260 brw_inst *
1261 brw_IF(struct brw_codegen *p, unsigned execute_size)
1262 {
1263 const struct gen_device_info *devinfo = p->devinfo;
1264 brw_inst *insn;
1265
1266 insn = next_insn(p, BRW_OPCODE_IF);
1267
1268 /* Override the defaults for this instruction:
1269 */
1270 if (devinfo->gen < 6) {
1271 brw_set_dest(p, insn, brw_ip_reg());
1272 brw_set_src0(p, insn, brw_ip_reg());
1273 brw_set_src1(p, insn, brw_imm_d(0x0));
1274 } else if (devinfo->gen == 6) {
1275 brw_set_dest(p, insn, brw_imm_w(0));
1276 brw_inst_set_gen6_jump_count(devinfo, insn, 0);
1277 brw_set_src0(p, insn, vec1(retype(brw_null_reg(), BRW_REGISTER_TYPE_D)));
1278 brw_set_src1(p, insn, vec1(retype(brw_null_reg(), BRW_REGISTER_TYPE_D)));
1279 } else if (devinfo->gen == 7) {
1280 brw_set_dest(p, insn, vec1(retype(brw_null_reg(), BRW_REGISTER_TYPE_D)));
1281 brw_set_src0(p, insn, vec1(retype(brw_null_reg(), BRW_REGISTER_TYPE_D)));
1282 brw_set_src1(p, insn, brw_imm_w(0));
1283 brw_inst_set_jip(devinfo, insn, 0);
1284 brw_inst_set_uip(devinfo, insn, 0);
1285 } else {
1286 brw_set_dest(p, insn, vec1(retype(brw_null_reg(), BRW_REGISTER_TYPE_D)));
1287 brw_set_src0(p, insn, brw_imm_d(0));
1288 brw_inst_set_jip(devinfo, insn, 0);
1289 brw_inst_set_uip(devinfo, insn, 0);
1290 }
1291
1292 brw_inst_set_exec_size(devinfo, insn, execute_size);
1293 brw_inst_set_qtr_control(devinfo, insn, BRW_COMPRESSION_NONE);
1294 brw_inst_set_pred_control(devinfo, insn, BRW_PREDICATE_NORMAL);
1295 brw_inst_set_mask_control(devinfo, insn, BRW_MASK_ENABLE);
1296 if (!p->single_program_flow && devinfo->gen < 6)
1297 brw_inst_set_thread_control(devinfo, insn, BRW_THREAD_SWITCH);
1298
1299 push_if_stack(p, insn);
1300 p->if_depth_in_loop[p->loop_stack_depth]++;
1301 return insn;
1302 }
1303
1304 /* This function is only used for gen6-style IF instructions with an
1305 * embedded comparison (conditional modifier). It is not used on gen7.
1306 */
1307 brw_inst *
1308 gen6_IF(struct brw_codegen *p, enum brw_conditional_mod conditional,
1309 struct brw_reg src0, struct brw_reg src1)
1310 {
1311 const struct gen_device_info *devinfo = p->devinfo;
1312 brw_inst *insn;
1313
1314 insn = next_insn(p, BRW_OPCODE_IF);
1315
1316 brw_set_dest(p, insn, brw_imm_w(0));
1317 brw_inst_set_exec_size(devinfo, insn,
1318 brw_inst_exec_size(devinfo, p->current));
1319 brw_inst_set_gen6_jump_count(devinfo, insn, 0);
1320 brw_set_src0(p, insn, src0);
1321 brw_set_src1(p, insn, src1);
1322
1323 assert(brw_inst_qtr_control(devinfo, insn) == BRW_COMPRESSION_NONE);
1324 assert(brw_inst_pred_control(devinfo, insn) == BRW_PREDICATE_NONE);
1325 brw_inst_set_cond_modifier(devinfo, insn, conditional);
1326
1327 push_if_stack(p, insn);
1328 return insn;
1329 }
1330
1331 /**
1332 * In single-program-flow (SPF) mode, convert IF and ELSE into ADDs.
1333 */
1334 static void
1335 convert_IF_ELSE_to_ADD(struct brw_codegen *p,
1336 brw_inst *if_inst, brw_inst *else_inst)
1337 {
1338 const struct gen_device_info *devinfo = p->devinfo;
1339
1340 /* The next instruction (where the ENDIF would be, if it existed) */
1341 brw_inst *next_inst = &p->store[p->nr_insn];
1342
1343 assert(p->single_program_flow);
1344 assert(if_inst != NULL && brw_inst_opcode(devinfo, if_inst) == BRW_OPCODE_IF);
1345 assert(else_inst == NULL || brw_inst_opcode(devinfo, else_inst) == BRW_OPCODE_ELSE);
1346 assert(brw_inst_exec_size(devinfo, if_inst) == BRW_EXECUTE_1);
1347
1348 /* Convert IF to an ADD instruction that moves the instruction pointer
1349 * to the first instruction of the ELSE block. If there is no ELSE
1350 * block, point to where ENDIF would be. Reverse the predicate.
1351 *
1352 * There's no need to execute an ENDIF since we don't need to do any
1353 * stack operations, and if we're currently executing, we just want to
1354 * continue normally.
1355 */
1356 brw_inst_set_opcode(devinfo, if_inst, BRW_OPCODE_ADD);
1357 brw_inst_set_pred_inv(devinfo, if_inst, true);
1358
1359 if (else_inst != NULL) {
1360 /* Convert ELSE to an ADD instruction that points where the ENDIF
1361 * would be.
1362 */
1363 brw_inst_set_opcode(devinfo, else_inst, BRW_OPCODE_ADD);
1364
1365 brw_inst_set_imm_ud(devinfo, if_inst, (else_inst - if_inst + 1) * 16);
1366 brw_inst_set_imm_ud(devinfo, else_inst, (next_inst - else_inst) * 16);
1367 } else {
1368 brw_inst_set_imm_ud(devinfo, if_inst, (next_inst - if_inst) * 16);
1369 }
1370 }
1371
1372 /**
1373 * Patch IF and ELSE instructions with appropriate jump targets.
1374 */
1375 static void
1376 patch_IF_ELSE(struct brw_codegen *p,
1377 brw_inst *if_inst, brw_inst *else_inst, brw_inst *endif_inst)
1378 {
1379 const struct gen_device_info *devinfo = p->devinfo;
1380
1381 /* We shouldn't be patching IF and ELSE instructions in single program flow
1382 * mode when gen < 6, because in single program flow mode on those
1383 * platforms, we convert flow control instructions to conditional ADDs that
1384 * operate on IP (see brw_ENDIF).
1385 *
1386 * However, on Gen6, writing to IP doesn't work in single program flow mode
1387 * (see the SandyBridge PRM, Volume 4 part 2, p79: "When SPF is ON, IP may
1388 * not be updated by non-flow control instructions."). And on later
1389 * platforms, there is no significant benefit to converting control flow
1390 * instructions to conditional ADDs. So we do patch IF and ELSE
1391 * instructions in single program flow mode on those platforms.
1392 */
1393 if (devinfo->gen < 6)
1394 assert(!p->single_program_flow);
1395
1396 assert(if_inst != NULL && brw_inst_opcode(devinfo, if_inst) == BRW_OPCODE_IF);
1397 assert(endif_inst != NULL);
1398 assert(else_inst == NULL || brw_inst_opcode(devinfo, else_inst) == BRW_OPCODE_ELSE);
1399
1400 unsigned br = brw_jump_scale(devinfo);
1401
1402 assert(brw_inst_opcode(devinfo, endif_inst) == BRW_OPCODE_ENDIF);
1403 brw_inst_set_exec_size(devinfo, endif_inst, brw_inst_exec_size(devinfo, if_inst));
1404
1405 if (else_inst == NULL) {
1406 /* Patch IF -> ENDIF */
1407 if (devinfo->gen < 6) {
1408 /* Turn it into an IFF, which means no mask stack operations for
1409 * all-false and jumping past the ENDIF.
1410 */
1411 brw_inst_set_opcode(devinfo, if_inst, BRW_OPCODE_IFF);
1412 brw_inst_set_gen4_jump_count(devinfo, if_inst,
1413 br * (endif_inst - if_inst + 1));
1414 brw_inst_set_gen4_pop_count(devinfo, if_inst, 0);
1415 } else if (devinfo->gen == 6) {
1416 /* As of gen6, there is no IFF and IF must point to the ENDIF. */
1417 brw_inst_set_gen6_jump_count(devinfo, if_inst, br*(endif_inst - if_inst));
1418 } else {
1419 brw_inst_set_uip(devinfo, if_inst, br * (endif_inst - if_inst));
1420 brw_inst_set_jip(devinfo, if_inst, br * (endif_inst - if_inst));
1421 }
1422 } else {
1423 brw_inst_set_exec_size(devinfo, else_inst, brw_inst_exec_size(devinfo, if_inst));
1424
1425 /* Patch IF -> ELSE */
1426 if (devinfo->gen < 6) {
1427 brw_inst_set_gen4_jump_count(devinfo, if_inst,
1428 br * (else_inst - if_inst));
1429 brw_inst_set_gen4_pop_count(devinfo, if_inst, 0);
1430 } else if (devinfo->gen == 6) {
1431 brw_inst_set_gen6_jump_count(devinfo, if_inst,
1432 br * (else_inst - if_inst + 1));
1433 }
1434
1435 /* Patch ELSE -> ENDIF */
1436 if (devinfo->gen < 6) {
1437 /* BRW_OPCODE_ELSE pre-gen6 should point just past the
1438 * matching ENDIF.
1439 */
1440 brw_inst_set_gen4_jump_count(devinfo, else_inst,
1441 br * (endif_inst - else_inst + 1));
1442 brw_inst_set_gen4_pop_count(devinfo, else_inst, 1);
1443 } else if (devinfo->gen == 6) {
1444 /* BRW_OPCODE_ELSE on gen6 should point to the matching ENDIF. */
1445 brw_inst_set_gen6_jump_count(devinfo, else_inst,
1446 br * (endif_inst - else_inst));
1447 } else {
1448 /* The IF instruction's JIP should point just past the ELSE */
1449 brw_inst_set_jip(devinfo, if_inst, br * (else_inst - if_inst + 1));
1450 /* The IF instruction's UIP and ELSE's JIP should point to ENDIF */
1451 brw_inst_set_uip(devinfo, if_inst, br * (endif_inst - if_inst));
1452 brw_inst_set_jip(devinfo, else_inst, br * (endif_inst - else_inst));
1453 if (devinfo->gen >= 8) {
1454 /* Since we don't set branch_ctrl, the ELSE's JIP and UIP both
1455 * should point to ENDIF.
1456 */
1457 brw_inst_set_uip(devinfo, else_inst, br * (endif_inst - else_inst));
1458 }
1459 }
1460 }
1461 }
1462
1463 void
1464 brw_ELSE(struct brw_codegen *p)
1465 {
1466 const struct gen_device_info *devinfo = p->devinfo;
1467 brw_inst *insn;
1468
1469 insn = next_insn(p, BRW_OPCODE_ELSE);
1470
1471 if (devinfo->gen < 6) {
1472 brw_set_dest(p, insn, brw_ip_reg());
1473 brw_set_src0(p, insn, brw_ip_reg());
1474 brw_set_src1(p, insn, brw_imm_d(0x0));
1475 } else if (devinfo->gen == 6) {
1476 brw_set_dest(p, insn, brw_imm_w(0));
1477 brw_inst_set_gen6_jump_count(devinfo, insn, 0);
1478 brw_set_src0(p, insn, retype(brw_null_reg(), BRW_REGISTER_TYPE_D));
1479 brw_set_src1(p, insn, retype(brw_null_reg(), BRW_REGISTER_TYPE_D));
1480 } else if (devinfo->gen == 7) {
1481 brw_set_dest(p, insn, retype(brw_null_reg(), BRW_REGISTER_TYPE_D));
1482 brw_set_src0(p, insn, retype(brw_null_reg(), BRW_REGISTER_TYPE_D));
1483 brw_set_src1(p, insn, brw_imm_w(0));
1484 brw_inst_set_jip(devinfo, insn, 0);
1485 brw_inst_set_uip(devinfo, insn, 0);
1486 } else {
1487 brw_set_dest(p, insn, retype(brw_null_reg(), BRW_REGISTER_TYPE_D));
1488 brw_set_src0(p, insn, brw_imm_d(0));
1489 brw_inst_set_jip(devinfo, insn, 0);
1490 brw_inst_set_uip(devinfo, insn, 0);
1491 }
1492
1493 brw_inst_set_qtr_control(devinfo, insn, BRW_COMPRESSION_NONE);
1494 brw_inst_set_mask_control(devinfo, insn, BRW_MASK_ENABLE);
1495 if (!p->single_program_flow && devinfo->gen < 6)
1496 brw_inst_set_thread_control(devinfo, insn, BRW_THREAD_SWITCH);
1497
1498 push_if_stack(p, insn);
1499 }
1500
1501 void
1502 brw_ENDIF(struct brw_codegen *p)
1503 {
1504 const struct gen_device_info *devinfo = p->devinfo;
1505 brw_inst *insn = NULL;
1506 brw_inst *else_inst = NULL;
1507 brw_inst *if_inst = NULL;
1508 brw_inst *tmp;
1509 bool emit_endif = true;
1510
1511 /* In single program flow mode, we can express IF and ELSE instructions
1512 * equivalently as ADD instructions that operate on IP. On platforms prior
1513 * to Gen6, flow control instructions cause an implied thread switch, so
1514 * this is a significant savings.
1515 *
1516 * However, on Gen6, writing to IP doesn't work in single program flow mode
1517 * (see the SandyBridge PRM, Volume 4 part 2, p79: "When SPF is ON, IP may
1518 * not be updated by non-flow control instructions."). And on later
1519 * platforms, there is no significant benefit to converting control flow
1520 * instructions to conditional ADDs. So we only do this trick on Gen4 and
1521 * Gen5.
1522 */
1523 if (devinfo->gen < 6 && p->single_program_flow)
1524 emit_endif = false;
1525
1526 /*
1527 * A single next_insn() may change the base address of instruction store
1528 * memory(p->store), so call it first before referencing the instruction
1529 * store pointer from an index
1530 */
1531 if (emit_endif)
1532 insn = next_insn(p, BRW_OPCODE_ENDIF);
1533
1534 /* Pop the IF and (optional) ELSE instructions from the stack */
1535 p->if_depth_in_loop[p->loop_stack_depth]--;
1536 tmp = pop_if_stack(p);
1537 if (brw_inst_opcode(devinfo, tmp) == BRW_OPCODE_ELSE) {
1538 else_inst = tmp;
1539 tmp = pop_if_stack(p);
1540 }
1541 if_inst = tmp;
1542
1543 if (!emit_endif) {
1544 /* ENDIF is useless; don't bother emitting it. */
1545 convert_IF_ELSE_to_ADD(p, if_inst, else_inst);
1546 return;
1547 }
1548
1549 if (devinfo->gen < 6) {
1550 brw_set_dest(p, insn, retype(brw_null_reg(), BRW_REGISTER_TYPE_D));
1551 brw_set_src0(p, insn, retype(brw_null_reg(), BRW_REGISTER_TYPE_D));
1552 brw_set_src1(p, insn, brw_imm_d(0x0));
1553 } else if (devinfo->gen == 6) {
1554 brw_set_dest(p, insn, brw_imm_w(0));
1555 brw_set_src0(p, insn, retype(brw_null_reg(), BRW_REGISTER_TYPE_D));
1556 brw_set_src1(p, insn, retype(brw_null_reg(), BRW_REGISTER_TYPE_D));
1557 } else if (devinfo->gen == 7) {
1558 brw_set_dest(p, insn, retype(brw_null_reg(), BRW_REGISTER_TYPE_D));
1559 brw_set_src0(p, insn, retype(brw_null_reg(), BRW_REGISTER_TYPE_D));
1560 brw_set_src1(p, insn, brw_imm_w(0));
1561 } else {
1562 brw_set_src0(p, insn, brw_imm_d(0));
1563 }
1564
1565 brw_inst_set_qtr_control(devinfo, insn, BRW_COMPRESSION_NONE);
1566 brw_inst_set_mask_control(devinfo, insn, BRW_MASK_ENABLE);
1567 if (devinfo->gen < 6)
1568 brw_inst_set_thread_control(devinfo, insn, BRW_THREAD_SWITCH);
1569
1570 /* Also pop item off the stack in the endif instruction: */
1571 if (devinfo->gen < 6) {
1572 brw_inst_set_gen4_jump_count(devinfo, insn, 0);
1573 brw_inst_set_gen4_pop_count(devinfo, insn, 1);
1574 } else if (devinfo->gen == 6) {
1575 brw_inst_set_gen6_jump_count(devinfo, insn, 2);
1576 } else {
1577 brw_inst_set_jip(devinfo, insn, 2);
1578 }
1579 patch_IF_ELSE(p, if_inst, else_inst, insn);
1580 }
1581
1582 brw_inst *
1583 brw_BREAK(struct brw_codegen *p)
1584 {
1585 const struct gen_device_info *devinfo = p->devinfo;
1586 brw_inst *insn;
1587
1588 insn = next_insn(p, BRW_OPCODE_BREAK);
1589 if (devinfo->gen >= 8) {
1590 brw_set_dest(p, insn, retype(brw_null_reg(), BRW_REGISTER_TYPE_D));
1591 brw_set_src0(p, insn, brw_imm_d(0x0));
1592 } else if (devinfo->gen >= 6) {
1593 brw_set_dest(p, insn, retype(brw_null_reg(), BRW_REGISTER_TYPE_D));
1594 brw_set_src0(p, insn, retype(brw_null_reg(), BRW_REGISTER_TYPE_D));
1595 brw_set_src1(p, insn, brw_imm_d(0x0));
1596 } else {
1597 brw_set_dest(p, insn, brw_ip_reg());
1598 brw_set_src0(p, insn, brw_ip_reg());
1599 brw_set_src1(p, insn, brw_imm_d(0x0));
1600 brw_inst_set_gen4_pop_count(devinfo, insn,
1601 p->if_depth_in_loop[p->loop_stack_depth]);
1602 }
1603 brw_inst_set_qtr_control(devinfo, insn, BRW_COMPRESSION_NONE);
1604 brw_inst_set_exec_size(devinfo, insn,
1605 brw_inst_exec_size(devinfo, p->current));
1606
1607 return insn;
1608 }
1609
1610 brw_inst *
1611 brw_CONT(struct brw_codegen *p)
1612 {
1613 const struct gen_device_info *devinfo = p->devinfo;
1614 brw_inst *insn;
1615
1616 insn = next_insn(p, BRW_OPCODE_CONTINUE);
1617 brw_set_dest(p, insn, brw_ip_reg());
1618 if (devinfo->gen >= 8) {
1619 brw_set_src0(p, insn, brw_imm_d(0x0));
1620 } else {
1621 brw_set_src0(p, insn, brw_ip_reg());
1622 brw_set_src1(p, insn, brw_imm_d(0x0));
1623 }
1624
1625 if (devinfo->gen < 6) {
1626 brw_inst_set_gen4_pop_count(devinfo, insn,
1627 p->if_depth_in_loop[p->loop_stack_depth]);
1628 }
1629 brw_inst_set_qtr_control(devinfo, insn, BRW_COMPRESSION_NONE);
1630 brw_inst_set_exec_size(devinfo, insn,
1631 brw_inst_exec_size(devinfo, p->current));
1632 return insn;
1633 }
1634
1635 brw_inst *
1636 gen6_HALT(struct brw_codegen *p)
1637 {
1638 const struct gen_device_info *devinfo = p->devinfo;
1639 brw_inst *insn;
1640
1641 insn = next_insn(p, BRW_OPCODE_HALT);
1642 brw_set_dest(p, insn, retype(brw_null_reg(), BRW_REGISTER_TYPE_D));
1643 if (devinfo->gen >= 8) {
1644 brw_set_src0(p, insn, brw_imm_d(0x0));
1645 } else {
1646 brw_set_src0(p, insn, retype(brw_null_reg(), BRW_REGISTER_TYPE_D));
1647 brw_set_src1(p, insn, brw_imm_d(0x0)); /* UIP and JIP, updated later. */
1648 }
1649
1650 brw_inst_set_qtr_control(devinfo, insn, BRW_COMPRESSION_NONE);
1651 brw_inst_set_exec_size(devinfo, insn,
1652 brw_inst_exec_size(devinfo, p->current));
1653 return insn;
1654 }
1655
1656 /* DO/WHILE loop:
1657 *
1658 * The DO/WHILE is just an unterminated loop -- break or continue are
1659 * used for control within the loop. We have a few ways they can be
1660 * done.
1661 *
1662 * For uniform control flow, the WHILE is just a jump, so ADD ip, ip,
1663 * jip and no DO instruction.
1664 *
1665 * For non-uniform control flow pre-gen6, there's a DO instruction to
1666 * push the mask, and a WHILE to jump back, and BREAK to get out and
1667 * pop the mask.
1668 *
1669 * For gen6, there's no more mask stack, so no need for DO. WHILE
1670 * just points back to the first instruction of the loop.
1671 */
1672 brw_inst *
1673 brw_DO(struct brw_codegen *p, unsigned execute_size)
1674 {
1675 const struct gen_device_info *devinfo = p->devinfo;
1676
1677 if (devinfo->gen >= 6 || p->single_program_flow) {
1678 push_loop_stack(p, &p->store[p->nr_insn]);
1679 return &p->store[p->nr_insn];
1680 } else {
1681 brw_inst *insn = next_insn(p, BRW_OPCODE_DO);
1682
1683 push_loop_stack(p, insn);
1684
1685 /* Override the defaults for this instruction:
1686 */
1687 brw_set_dest(p, insn, brw_null_reg());
1688 brw_set_src0(p, insn, brw_null_reg());
1689 brw_set_src1(p, insn, brw_null_reg());
1690
1691 brw_inst_set_qtr_control(devinfo, insn, BRW_COMPRESSION_NONE);
1692 brw_inst_set_exec_size(devinfo, insn, execute_size);
1693 brw_inst_set_pred_control(devinfo, insn, BRW_PREDICATE_NONE);
1694
1695 return insn;
1696 }
1697 }
1698
1699 /**
1700 * For pre-gen6, we patch BREAK/CONT instructions to point at the WHILE
1701 * instruction here.
1702 *
1703 * For gen6+, see brw_set_uip_jip(), which doesn't care so much about the loop
1704 * nesting, since it can always just point to the end of the block/current loop.
1705 */
1706 static void
1707 brw_patch_break_cont(struct brw_codegen *p, brw_inst *while_inst)
1708 {
1709 const struct gen_device_info *devinfo = p->devinfo;
1710 brw_inst *do_inst = get_inner_do_insn(p);
1711 brw_inst *inst;
1712 unsigned br = brw_jump_scale(devinfo);
1713
1714 assert(devinfo->gen < 6);
1715
1716 for (inst = while_inst - 1; inst != do_inst; inst--) {
1717 /* If the jump count is != 0, that means that this instruction has already
1718 * been patched because it's part of a loop inside of the one we're
1719 * patching.
1720 */
1721 if (brw_inst_opcode(devinfo, inst) == BRW_OPCODE_BREAK &&
1722 brw_inst_gen4_jump_count(devinfo, inst) == 0) {
1723 brw_inst_set_gen4_jump_count(devinfo, inst, br*((while_inst - inst) + 1));
1724 } else if (brw_inst_opcode(devinfo, inst) == BRW_OPCODE_CONTINUE &&
1725 brw_inst_gen4_jump_count(devinfo, inst) == 0) {
1726 brw_inst_set_gen4_jump_count(devinfo, inst, br * (while_inst - inst));
1727 }
1728 }
1729 }
1730
1731 brw_inst *
1732 brw_WHILE(struct brw_codegen *p)
1733 {
1734 const struct gen_device_info *devinfo = p->devinfo;
1735 brw_inst *insn, *do_insn;
1736 unsigned br = brw_jump_scale(devinfo);
1737
1738 if (devinfo->gen >= 6) {
1739 insn = next_insn(p, BRW_OPCODE_WHILE);
1740 do_insn = get_inner_do_insn(p);
1741
1742 if (devinfo->gen >= 8) {
1743 brw_set_dest(p, insn, retype(brw_null_reg(), BRW_REGISTER_TYPE_D));
1744 brw_set_src0(p, insn, brw_imm_d(0));
1745 brw_inst_set_jip(devinfo, insn, br * (do_insn - insn));
1746 } else if (devinfo->gen == 7) {
1747 brw_set_dest(p, insn, retype(brw_null_reg(), BRW_REGISTER_TYPE_D));
1748 brw_set_src0(p, insn, retype(brw_null_reg(), BRW_REGISTER_TYPE_D));
1749 brw_set_src1(p, insn, brw_imm_w(0));
1750 brw_inst_set_jip(devinfo, insn, br * (do_insn - insn));
1751 } else {
1752 brw_set_dest(p, insn, brw_imm_w(0));
1753 brw_inst_set_gen6_jump_count(devinfo, insn, br * (do_insn - insn));
1754 brw_set_src0(p, insn, retype(brw_null_reg(), BRW_REGISTER_TYPE_D));
1755 brw_set_src1(p, insn, retype(brw_null_reg(), BRW_REGISTER_TYPE_D));
1756 }
1757
1758 brw_inst_set_exec_size(devinfo, insn,
1759 brw_inst_exec_size(devinfo, p->current));
1760
1761 } else {
1762 if (p->single_program_flow) {
1763 insn = next_insn(p, BRW_OPCODE_ADD);
1764 do_insn = get_inner_do_insn(p);
1765
1766 brw_set_dest(p, insn, brw_ip_reg());
1767 brw_set_src0(p, insn, brw_ip_reg());
1768 brw_set_src1(p, insn, brw_imm_d((do_insn - insn) * 16));
1769 brw_inst_set_exec_size(devinfo, insn, BRW_EXECUTE_1);
1770 } else {
1771 insn = next_insn(p, BRW_OPCODE_WHILE);
1772 do_insn = get_inner_do_insn(p);
1773
1774 assert(brw_inst_opcode(devinfo, do_insn) == BRW_OPCODE_DO);
1775
1776 brw_set_dest(p, insn, brw_ip_reg());
1777 brw_set_src0(p, insn, brw_ip_reg());
1778 brw_set_src1(p, insn, brw_imm_d(0));
1779
1780 brw_inst_set_exec_size(devinfo, insn, brw_inst_exec_size(devinfo, do_insn));
1781 brw_inst_set_gen4_jump_count(devinfo, insn, br * (do_insn - insn + 1));
1782 brw_inst_set_gen4_pop_count(devinfo, insn, 0);
1783
1784 brw_patch_break_cont(p, insn);
1785 }
1786 }
1787 brw_inst_set_qtr_control(devinfo, insn, BRW_COMPRESSION_NONE);
1788
1789 p->loop_stack_depth--;
1790
1791 return insn;
1792 }
1793
1794 /* FORWARD JUMPS:
1795 */
1796 void brw_land_fwd_jump(struct brw_codegen *p, int jmp_insn_idx)
1797 {
1798 const struct gen_device_info *devinfo = p->devinfo;
1799 brw_inst *jmp_insn = &p->store[jmp_insn_idx];
1800 unsigned jmpi = 1;
1801
1802 if (devinfo->gen >= 5)
1803 jmpi = 2;
1804
1805 assert(brw_inst_opcode(devinfo, jmp_insn) == BRW_OPCODE_JMPI);
1806 assert(brw_inst_src1_reg_file(devinfo, jmp_insn) == BRW_IMMEDIATE_VALUE);
1807
1808 brw_inst_set_gen4_jump_count(devinfo, jmp_insn,
1809 jmpi * (p->nr_insn - jmp_insn_idx - 1));
1810 }
1811
1812 /* To integrate with the above, it makes sense that the comparison
1813 * instruction should populate the flag register. It might be simpler
1814 * just to use the flag reg for most WM tasks?
1815 */
1816 void brw_CMP(struct brw_codegen *p,
1817 struct brw_reg dest,
1818 unsigned conditional,
1819 struct brw_reg src0,
1820 struct brw_reg src1)
1821 {
1822 const struct gen_device_info *devinfo = p->devinfo;
1823 brw_inst *insn = next_insn(p, BRW_OPCODE_CMP);
1824
1825 brw_inst_set_cond_modifier(devinfo, insn, conditional);
1826 brw_set_dest(p, insn, dest);
1827 brw_set_src0(p, insn, src0);
1828 brw_set_src1(p, insn, src1);
1829
1830 /* Item WaCMPInstNullDstForcesThreadSwitch in the Haswell Bspec workarounds
1831 * page says:
1832 * "Any CMP instruction with a null destination must use a {switch}."
1833 *
1834 * It also applies to other Gen7 platforms (IVB, BYT) even though it isn't
1835 * mentioned on their work-arounds pages.
1836 */
1837 if (devinfo->gen == 7) {
1838 if (dest.file == BRW_ARCHITECTURE_REGISTER_FILE &&
1839 dest.nr == BRW_ARF_NULL) {
1840 brw_inst_set_thread_control(devinfo, insn, BRW_THREAD_SWITCH);
1841 }
1842 }
1843 }
1844
1845 /***********************************************************************
1846 * Helpers for the various SEND message types:
1847 */
1848
1849 /** Extended math function, float[8].
1850 */
1851 void gen4_math(struct brw_codegen *p,
1852 struct brw_reg dest,
1853 unsigned function,
1854 unsigned msg_reg_nr,
1855 struct brw_reg src,
1856 unsigned precision )
1857 {
1858 const struct gen_device_info *devinfo = p->devinfo;
1859 brw_inst *insn = next_insn(p, BRW_OPCODE_SEND);
1860 unsigned data_type;
1861 if (has_scalar_region(src)) {
1862 data_type = BRW_MATH_DATA_SCALAR;
1863 } else {
1864 data_type = BRW_MATH_DATA_VECTOR;
1865 }
1866
1867 assert(devinfo->gen < 6);
1868
1869 /* Example code doesn't set predicate_control for send
1870 * instructions.
1871 */
1872 brw_inst_set_pred_control(devinfo, insn, 0);
1873 brw_inst_set_base_mrf(devinfo, insn, msg_reg_nr);
1874
1875 brw_set_dest(p, insn, dest);
1876 brw_set_src0(p, insn, src);
1877 brw_set_math_message(p,
1878 insn,
1879 function,
1880 src.type == BRW_REGISTER_TYPE_D,
1881 precision,
1882 data_type);
1883 }
1884
1885 void gen6_math(struct brw_codegen *p,
1886 struct brw_reg dest,
1887 unsigned function,
1888 struct brw_reg src0,
1889 struct brw_reg src1)
1890 {
1891 const struct gen_device_info *devinfo = p->devinfo;
1892 brw_inst *insn = next_insn(p, BRW_OPCODE_MATH);
1893
1894 assert(devinfo->gen >= 6);
1895
1896 assert(dest.file == BRW_GENERAL_REGISTER_FILE ||
1897 (devinfo->gen >= 7 && dest.file == BRW_MESSAGE_REGISTER_FILE));
1898
1899 assert(dest.hstride == BRW_HORIZONTAL_STRIDE_1);
1900 if (devinfo->gen == 6) {
1901 assert(src0.hstride == BRW_HORIZONTAL_STRIDE_1);
1902 assert(src1.hstride == BRW_HORIZONTAL_STRIDE_1);
1903 }
1904
1905 if (function == BRW_MATH_FUNCTION_INT_DIV_QUOTIENT ||
1906 function == BRW_MATH_FUNCTION_INT_DIV_REMAINDER ||
1907 function == BRW_MATH_FUNCTION_INT_DIV_QUOTIENT_AND_REMAINDER) {
1908 assert(src0.type != BRW_REGISTER_TYPE_F);
1909 assert(src1.type != BRW_REGISTER_TYPE_F);
1910 assert(src1.file == BRW_GENERAL_REGISTER_FILE ||
1911 (devinfo->gen >= 8 && src1.file == BRW_IMMEDIATE_VALUE));
1912 } else {
1913 assert(src0.type == BRW_REGISTER_TYPE_F);
1914 assert(src1.type == BRW_REGISTER_TYPE_F);
1915 }
1916
1917 /* Source modifiers are ignored for extended math instructions on Gen6. */
1918 if (devinfo->gen == 6) {
1919 assert(!src0.negate);
1920 assert(!src0.abs);
1921 assert(!src1.negate);
1922 assert(!src1.abs);
1923 }
1924
1925 brw_inst_set_math_function(devinfo, insn, function);
1926
1927 brw_set_dest(p, insn, dest);
1928 brw_set_src0(p, insn, src0);
1929 brw_set_src1(p, insn, src1);
1930 }
1931
1932 /**
1933 * Return the right surface index to access the thread scratch space using
1934 * stateless dataport messages.
1935 */
1936 unsigned
1937 brw_scratch_surface_idx(const struct brw_codegen *p)
1938 {
1939 /* The scratch space is thread-local so IA coherency is unnecessary. */
1940 if (p->devinfo->gen >= 8)
1941 return GEN8_BTI_STATELESS_NON_COHERENT;
1942 else
1943 return BRW_BTI_STATELESS;
1944 }
1945
1946 /**
1947 * Write a block of OWORDs (half a GRF each) from the scratch buffer,
1948 * using a constant offset per channel.
1949 *
1950 * The offset must be aligned to oword size (16 bytes). Used for
1951 * register spilling.
1952 */
1953 void brw_oword_block_write_scratch(struct brw_codegen *p,
1954 struct brw_reg mrf,
1955 int num_regs,
1956 unsigned offset)
1957 {
1958 const struct gen_device_info *devinfo = p->devinfo;
1959 const unsigned target_cache =
1960 (devinfo->gen >= 7 ? GEN7_SFID_DATAPORT_DATA_CACHE :
1961 devinfo->gen >= 6 ? GEN6_SFID_DATAPORT_RENDER_CACHE :
1962 BRW_DATAPORT_READ_TARGET_RENDER_CACHE);
1963 uint32_t msg_type;
1964
1965 if (devinfo->gen >= 6)
1966 offset /= 16;
1967
1968 mrf = retype(mrf, BRW_REGISTER_TYPE_UD);
1969
1970 const unsigned mlen = 1 + num_regs;
1971
1972 /* Set up the message header. This is g0, with g0.2 filled with
1973 * the offset. We don't want to leave our offset around in g0 or
1974 * it'll screw up texture samples, so set it up inside the message
1975 * reg.
1976 */
1977 {
1978 brw_push_insn_state(p);
1979 brw_set_default_exec_size(p, BRW_EXECUTE_8);
1980 brw_set_default_mask_control(p, BRW_MASK_DISABLE);
1981 brw_set_default_compression_control(p, BRW_COMPRESSION_NONE);
1982
1983 brw_MOV(p, mrf, retype(brw_vec8_grf(0, 0), BRW_REGISTER_TYPE_UD));
1984
1985 /* set message header global offset field (reg 0, element 2) */
1986 brw_set_default_exec_size(p, BRW_EXECUTE_1);
1987 brw_MOV(p,
1988 retype(brw_vec1_reg(BRW_MESSAGE_REGISTER_FILE,
1989 mrf.nr,
1990 2), BRW_REGISTER_TYPE_UD),
1991 brw_imm_ud(offset));
1992
1993 brw_pop_insn_state(p);
1994 }
1995
1996 {
1997 struct brw_reg dest;
1998 brw_inst *insn = next_insn(p, BRW_OPCODE_SEND);
1999 int send_commit_msg;
2000 struct brw_reg src_header = retype(brw_vec8_grf(0, 0),
2001 BRW_REGISTER_TYPE_UW);
2002
2003 brw_inst_set_compression(devinfo, insn, false);
2004
2005 if (brw_inst_exec_size(devinfo, insn) >= 16)
2006 src_header = vec16(src_header);
2007
2008 assert(brw_inst_pred_control(devinfo, insn) == BRW_PREDICATE_NONE);
2009 if (devinfo->gen < 6)
2010 brw_inst_set_base_mrf(devinfo, insn, mrf.nr);
2011
2012 /* Until gen6, writes followed by reads from the same location
2013 * are not guaranteed to be ordered unless write_commit is set.
2014 * If set, then a no-op write is issued to the destination
2015 * register to set a dependency, and a read from the destination
2016 * can be used to ensure the ordering.
2017 *
2018 * For gen6, only writes between different threads need ordering
2019 * protection. Our use of DP writes is all about register
2020 * spilling within a thread.
2021 */
2022 if (devinfo->gen >= 6) {
2023 dest = retype(vec16(brw_null_reg()), BRW_REGISTER_TYPE_UW);
2024 send_commit_msg = 0;
2025 } else {
2026 dest = src_header;
2027 send_commit_msg = 1;
2028 }
2029
2030 brw_set_dest(p, insn, dest);
2031 if (devinfo->gen >= 6) {
2032 brw_set_src0(p, insn, mrf);
2033 } else {
2034 brw_set_src0(p, insn, brw_null_reg());
2035 }
2036
2037 if (devinfo->gen >= 6)
2038 msg_type = GEN6_DATAPORT_WRITE_MESSAGE_OWORD_BLOCK_WRITE;
2039 else
2040 msg_type = BRW_DATAPORT_WRITE_MESSAGE_OWORD_BLOCK_WRITE;
2041
2042 brw_set_dp_write_message(p,
2043 insn,
2044 brw_scratch_surface_idx(p),
2045 BRW_DATAPORT_OWORD_BLOCK_DWORDS(num_regs * 8),
2046 msg_type,
2047 target_cache,
2048 mlen,
2049 true, /* header_present */
2050 0, /* not a render target */
2051 send_commit_msg, /* response_length */
2052 0, /* eot */
2053 send_commit_msg);
2054 }
2055 }
2056
2057
2058 /**
2059 * Read a block of owords (half a GRF each) from the scratch buffer
2060 * using a constant index per channel.
2061 *
2062 * Offset must be aligned to oword size (16 bytes). Used for register
2063 * spilling.
2064 */
2065 void
2066 brw_oword_block_read_scratch(struct brw_codegen *p,
2067 struct brw_reg dest,
2068 struct brw_reg mrf,
2069 int num_regs,
2070 unsigned offset)
2071 {
2072 const struct gen_device_info *devinfo = p->devinfo;
2073
2074 if (devinfo->gen >= 6)
2075 offset /= 16;
2076
2077 if (p->devinfo->gen >= 7) {
2078 /* On gen 7 and above, we no longer have message registers and we can
2079 * send from any register we want. By using the destination register
2080 * for the message, we guarantee that the implied message write won't
2081 * accidentally overwrite anything. This has been a problem because
2082 * the MRF registers and source for the final FB write are both fixed
2083 * and may overlap.
2084 */
2085 mrf = retype(dest, BRW_REGISTER_TYPE_UD);
2086 } else {
2087 mrf = retype(mrf, BRW_REGISTER_TYPE_UD);
2088 }
2089 dest = retype(dest, BRW_REGISTER_TYPE_UW);
2090
2091 const unsigned rlen = num_regs;
2092 const unsigned target_cache =
2093 (devinfo->gen >= 7 ? GEN7_SFID_DATAPORT_DATA_CACHE :
2094 devinfo->gen >= 6 ? GEN6_SFID_DATAPORT_RENDER_CACHE :
2095 BRW_DATAPORT_READ_TARGET_RENDER_CACHE);
2096
2097 {
2098 brw_push_insn_state(p);
2099 brw_set_default_exec_size(p, BRW_EXECUTE_8);
2100 brw_set_default_compression_control(p, BRW_COMPRESSION_NONE);
2101 brw_set_default_mask_control(p, BRW_MASK_DISABLE);
2102
2103 brw_MOV(p, mrf, retype(brw_vec8_grf(0, 0), BRW_REGISTER_TYPE_UD));
2104
2105 /* set message header global offset field (reg 0, element 2) */
2106 brw_set_default_exec_size(p, BRW_EXECUTE_1);
2107 brw_MOV(p, get_element_ud(mrf, 2), brw_imm_ud(offset));
2108
2109 brw_pop_insn_state(p);
2110 }
2111
2112 {
2113 brw_inst *insn = next_insn(p, BRW_OPCODE_SEND);
2114
2115 assert(brw_inst_pred_control(devinfo, insn) == 0);
2116 brw_inst_set_compression(devinfo, insn, false);
2117
2118 brw_set_dest(p, insn, dest); /* UW? */
2119 if (devinfo->gen >= 6) {
2120 brw_set_src0(p, insn, mrf);
2121 } else {
2122 brw_set_src0(p, insn, brw_null_reg());
2123 brw_inst_set_base_mrf(devinfo, insn, mrf.nr);
2124 }
2125
2126 brw_set_dp_read_message(p,
2127 insn,
2128 brw_scratch_surface_idx(p),
2129 BRW_DATAPORT_OWORD_BLOCK_DWORDS(num_regs * 8),
2130 BRW_DATAPORT_READ_MESSAGE_OWORD_BLOCK_READ, /* msg_type */
2131 target_cache,
2132 1, /* msg_length */
2133 true, /* header_present */
2134 rlen);
2135 }
2136 }
2137
2138 void
2139 gen7_block_read_scratch(struct brw_codegen *p,
2140 struct brw_reg dest,
2141 int num_regs,
2142 unsigned offset)
2143 {
2144 brw_inst *insn = next_insn(p, BRW_OPCODE_SEND);
2145 assert(brw_inst_pred_control(p->devinfo, insn) == BRW_PREDICATE_NONE);
2146
2147 brw_set_dest(p, insn, retype(dest, BRW_REGISTER_TYPE_UW));
2148
2149 /* The HW requires that the header is present; this is to get the g0.5
2150 * scratch offset.
2151 */
2152 brw_set_src0(p, insn, brw_vec8_grf(0, 0));
2153
2154 /* According to the docs, offset is "A 12-bit HWord offset into the memory
2155 * Immediate Memory buffer as specified by binding table 0xFF." An HWORD
2156 * is 32 bytes, which happens to be the size of a register.
2157 */
2158 offset /= REG_SIZE;
2159 assert(offset < (1 << 12));
2160
2161 gen7_set_dp_scratch_message(p, insn,
2162 false, /* scratch read */
2163 false, /* OWords */
2164 false, /* invalidate after read */
2165 num_regs,
2166 offset,
2167 1, /* mlen: just g0 */
2168 num_regs, /* rlen */
2169 true); /* header present */
2170 }
2171
2172 /**
2173 * Read float[4] vectors from the data port constant cache.
2174 * Location (in buffer) should be a multiple of 16.
2175 * Used for fetching shader constants.
2176 */
2177 void brw_oword_block_read(struct brw_codegen *p,
2178 struct brw_reg dest,
2179 struct brw_reg mrf,
2180 uint32_t offset,
2181 uint32_t bind_table_index)
2182 {
2183 const struct gen_device_info *devinfo = p->devinfo;
2184 const unsigned target_cache =
2185 (devinfo->gen >= 6 ? GEN6_SFID_DATAPORT_CONSTANT_CACHE :
2186 BRW_DATAPORT_READ_TARGET_DATA_CACHE);
2187 const unsigned exec_size = 1 << brw_inst_exec_size(devinfo, p->current);
2188
2189 /* On newer hardware, offset is in units of owords. */
2190 if (devinfo->gen >= 6)
2191 offset /= 16;
2192
2193 mrf = retype(mrf, BRW_REGISTER_TYPE_UD);
2194
2195 brw_push_insn_state(p);
2196 brw_set_default_predicate_control(p, BRW_PREDICATE_NONE);
2197 brw_set_default_compression_control(p, BRW_COMPRESSION_NONE);
2198 brw_set_default_mask_control(p, BRW_MASK_DISABLE);
2199
2200 brw_push_insn_state(p);
2201 brw_set_default_exec_size(p, BRW_EXECUTE_8);
2202 brw_MOV(p, mrf, retype(brw_vec8_grf(0, 0), BRW_REGISTER_TYPE_UD));
2203
2204 /* set message header global offset field (reg 0, element 2) */
2205 brw_set_default_exec_size(p, BRW_EXECUTE_1);
2206 brw_MOV(p,
2207 retype(brw_vec1_reg(BRW_MESSAGE_REGISTER_FILE,
2208 mrf.nr,
2209 2), BRW_REGISTER_TYPE_UD),
2210 brw_imm_ud(offset));
2211 brw_pop_insn_state(p);
2212
2213 brw_inst *insn = next_insn(p, BRW_OPCODE_SEND);
2214
2215 /* cast dest to a uword[8] vector */
2216 dest = retype(vec8(dest), BRW_REGISTER_TYPE_UW);
2217
2218 brw_set_dest(p, insn, dest);
2219 if (devinfo->gen >= 6) {
2220 brw_set_src0(p, insn, mrf);
2221 } else {
2222 brw_set_src0(p, insn, brw_null_reg());
2223 brw_inst_set_base_mrf(devinfo, insn, mrf.nr);
2224 }
2225
2226 brw_set_dp_read_message(p, insn, bind_table_index,
2227 BRW_DATAPORT_OWORD_BLOCK_DWORDS(exec_size),
2228 BRW_DATAPORT_READ_MESSAGE_OWORD_BLOCK_READ,
2229 target_cache,
2230 1, /* msg_length */
2231 true, /* header_present */
2232 DIV_ROUND_UP(exec_size, 8)); /* response_length */
2233
2234 brw_pop_insn_state(p);
2235 }
2236
2237
2238 void brw_fb_WRITE(struct brw_codegen *p,
2239 struct brw_reg payload,
2240 struct brw_reg implied_header,
2241 unsigned msg_control,
2242 unsigned binding_table_index,
2243 unsigned msg_length,
2244 unsigned response_length,
2245 bool eot,
2246 bool last_render_target,
2247 bool header_present)
2248 {
2249 const struct gen_device_info *devinfo = p->devinfo;
2250 const unsigned target_cache =
2251 (devinfo->gen >= 6 ? GEN6_SFID_DATAPORT_RENDER_CACHE :
2252 BRW_DATAPORT_READ_TARGET_RENDER_CACHE);
2253 brw_inst *insn;
2254 unsigned msg_type;
2255 struct brw_reg dest, src0;
2256
2257 if (brw_inst_exec_size(devinfo, p->current) >= BRW_EXECUTE_16)
2258 dest = retype(vec16(brw_null_reg()), BRW_REGISTER_TYPE_UW);
2259 else
2260 dest = retype(vec8(brw_null_reg()), BRW_REGISTER_TYPE_UW);
2261
2262 if (devinfo->gen >= 6) {
2263 insn = next_insn(p, BRW_OPCODE_SENDC);
2264 } else {
2265 insn = next_insn(p, BRW_OPCODE_SEND);
2266 }
2267 brw_inst_set_compression(devinfo, insn, false);
2268
2269 if (devinfo->gen >= 6) {
2270 /* headerless version, just submit color payload */
2271 src0 = payload;
2272
2273 msg_type = GEN6_DATAPORT_WRITE_MESSAGE_RENDER_TARGET_WRITE;
2274 } else {
2275 assert(payload.file == BRW_MESSAGE_REGISTER_FILE);
2276 brw_inst_set_base_mrf(devinfo, insn, payload.nr);
2277 src0 = implied_header;
2278
2279 msg_type = BRW_DATAPORT_WRITE_MESSAGE_RENDER_TARGET_WRITE;
2280 }
2281
2282 brw_set_dest(p, insn, dest);
2283 brw_set_src0(p, insn, src0);
2284 brw_set_dp_write_message(p,
2285 insn,
2286 binding_table_index,
2287 msg_control,
2288 msg_type,
2289 target_cache,
2290 msg_length,
2291 header_present,
2292 last_render_target,
2293 response_length,
2294 eot,
2295 0 /* send_commit_msg */);
2296 }
2297
2298 brw_inst *
2299 gen9_fb_READ(struct brw_codegen *p,
2300 struct brw_reg dst,
2301 struct brw_reg payload,
2302 unsigned binding_table_index,
2303 unsigned msg_length,
2304 unsigned response_length,
2305 bool per_sample)
2306 {
2307 const struct gen_device_info *devinfo = p->devinfo;
2308 assert(devinfo->gen >= 9);
2309 const unsigned msg_subtype =
2310 brw_inst_exec_size(devinfo, p->current) == BRW_EXECUTE_16 ? 0 : 1;
2311 brw_inst *insn = next_insn(p, BRW_OPCODE_SENDC);
2312
2313 brw_set_dest(p, insn, dst);
2314 brw_set_src0(p, insn, payload);
2315 brw_set_dp_read_message(p, insn, binding_table_index,
2316 per_sample << 5 | msg_subtype,
2317 GEN9_DATAPORT_RC_RENDER_TARGET_READ,
2318 GEN6_SFID_DATAPORT_RENDER_CACHE,
2319 msg_length, true /* header_present */,
2320 response_length);
2321 brw_inst_set_rt_slot_group(devinfo, insn,
2322 brw_inst_qtr_control(devinfo, p->current) / 2);
2323
2324 return insn;
2325 }
2326
2327 /**
2328 * Texture sample instruction.
2329 * Note: the msg_type plus msg_length values determine exactly what kind
2330 * of sampling operation is performed. See volume 4, page 161 of docs.
2331 */
2332 void brw_SAMPLE(struct brw_codegen *p,
2333 struct brw_reg dest,
2334 unsigned msg_reg_nr,
2335 struct brw_reg src0,
2336 unsigned binding_table_index,
2337 unsigned sampler,
2338 unsigned msg_type,
2339 unsigned response_length,
2340 unsigned msg_length,
2341 unsigned header_present,
2342 unsigned simd_mode,
2343 unsigned return_format)
2344 {
2345 const struct gen_device_info *devinfo = p->devinfo;
2346 brw_inst *insn;
2347
2348 if (msg_reg_nr != -1)
2349 gen6_resolve_implied_move(p, &src0, msg_reg_nr);
2350
2351 insn = next_insn(p, BRW_OPCODE_SEND);
2352 brw_inst_set_pred_control(devinfo, insn, BRW_PREDICATE_NONE); /* XXX */
2353
2354 /* From the 965 PRM (volume 4, part 1, section 14.2.41):
2355 *
2356 * "Instruction compression is not allowed for this instruction (that
2357 * is, send). The hardware behavior is undefined if this instruction is
2358 * set as compressed. However, compress control can be set to "SecHalf"
2359 * to affect the EMask generation."
2360 *
2361 * No similar wording is found in later PRMs, but there are examples
2362 * utilizing send with SecHalf. More importantly, SIMD8 sampler messages
2363 * are allowed in SIMD16 mode and they could not work without SecHalf. For
2364 * these reasons, we allow BRW_COMPRESSION_2NDHALF here.
2365 */
2366 brw_inst_set_compression(devinfo, insn, false);
2367
2368 if (devinfo->gen < 6)
2369 brw_inst_set_base_mrf(devinfo, insn, msg_reg_nr);
2370
2371 brw_set_dest(p, insn, dest);
2372 brw_set_src0(p, insn, src0);
2373 brw_set_sampler_message(p, insn,
2374 binding_table_index,
2375 sampler,
2376 msg_type,
2377 response_length,
2378 msg_length,
2379 header_present,
2380 simd_mode,
2381 return_format);
2382 }
2383
2384 /* Adjust the message header's sampler state pointer to
2385 * select the correct group of 16 samplers.
2386 */
2387 void brw_adjust_sampler_state_pointer(struct brw_codegen *p,
2388 struct brw_reg header,
2389 struct brw_reg sampler_index)
2390 {
2391 /* The "Sampler Index" field can only store values between 0 and 15.
2392 * However, we can add an offset to the "Sampler State Pointer"
2393 * field, effectively selecting a different set of 16 samplers.
2394 *
2395 * The "Sampler State Pointer" needs to be aligned to a 32-byte
2396 * offset, and each sampler state is only 16-bytes, so we can't
2397 * exclusively use the offset - we have to use both.
2398 */
2399
2400 const struct gen_device_info *devinfo = p->devinfo;
2401
2402 if (sampler_index.file == BRW_IMMEDIATE_VALUE) {
2403 const int sampler_state_size = 16; /* 16 bytes */
2404 uint32_t sampler = sampler_index.ud;
2405
2406 if (sampler >= 16) {
2407 assert(devinfo->is_haswell || devinfo->gen >= 8);
2408 brw_ADD(p,
2409 get_element_ud(header, 3),
2410 get_element_ud(brw_vec8_grf(0, 0), 3),
2411 brw_imm_ud(16 * (sampler / 16) * sampler_state_size));
2412 }
2413 } else {
2414 /* Non-const sampler array indexing case */
2415 if (devinfo->gen < 8 && !devinfo->is_haswell) {
2416 return;
2417 }
2418
2419 struct brw_reg temp = get_element_ud(header, 3);
2420
2421 brw_AND(p, temp, get_element_ud(sampler_index, 0), brw_imm_ud(0x0f0));
2422 brw_SHL(p, temp, temp, brw_imm_ud(4));
2423 brw_ADD(p,
2424 get_element_ud(header, 3),
2425 get_element_ud(brw_vec8_grf(0, 0), 3),
2426 temp);
2427 }
2428 }
2429
2430 /* All these variables are pretty confusing - we might be better off
2431 * using bitmasks and macros for this, in the old style. Or perhaps
2432 * just having the caller instantiate the fields in dword3 itself.
2433 */
2434 void brw_urb_WRITE(struct brw_codegen *p,
2435 struct brw_reg dest,
2436 unsigned msg_reg_nr,
2437 struct brw_reg src0,
2438 enum brw_urb_write_flags flags,
2439 unsigned msg_length,
2440 unsigned response_length,
2441 unsigned offset,
2442 unsigned swizzle)
2443 {
2444 const struct gen_device_info *devinfo = p->devinfo;
2445 brw_inst *insn;
2446
2447 gen6_resolve_implied_move(p, &src0, msg_reg_nr);
2448
2449 if (devinfo->gen >= 7 && !(flags & BRW_URB_WRITE_USE_CHANNEL_MASKS)) {
2450 /* Enable Channel Masks in the URB_WRITE_HWORD message header */
2451 brw_push_insn_state(p);
2452 brw_set_default_access_mode(p, BRW_ALIGN_1);
2453 brw_set_default_mask_control(p, BRW_MASK_DISABLE);
2454 brw_set_default_exec_size(p, BRW_EXECUTE_1);
2455 brw_OR(p, retype(brw_vec1_reg(BRW_MESSAGE_REGISTER_FILE, msg_reg_nr, 5),
2456 BRW_REGISTER_TYPE_UD),
2457 retype(brw_vec1_grf(0, 5), BRW_REGISTER_TYPE_UD),
2458 brw_imm_ud(0xff00));
2459 brw_pop_insn_state(p);
2460 }
2461
2462 insn = next_insn(p, BRW_OPCODE_SEND);
2463
2464 assert(msg_length < BRW_MAX_MRF(devinfo->gen));
2465
2466 brw_set_dest(p, insn, dest);
2467 brw_set_src0(p, insn, src0);
2468 brw_set_src1(p, insn, brw_imm_d(0));
2469
2470 if (devinfo->gen < 6)
2471 brw_inst_set_base_mrf(devinfo, insn, msg_reg_nr);
2472
2473 brw_set_urb_message(p,
2474 insn,
2475 flags,
2476 msg_length,
2477 response_length,
2478 offset,
2479 swizzle);
2480 }
2481
2482 struct brw_inst *
2483 brw_send_indirect_message(struct brw_codegen *p,
2484 unsigned sfid,
2485 struct brw_reg dst,
2486 struct brw_reg payload,
2487 struct brw_reg desc)
2488 {
2489 const struct gen_device_info *devinfo = p->devinfo;
2490 struct brw_inst *send;
2491 int setup;
2492
2493 dst = retype(dst, BRW_REGISTER_TYPE_UW);
2494
2495 assert(desc.type == BRW_REGISTER_TYPE_UD);
2496
2497 /* We hold on to the setup instruction (the SEND in the direct case, the OR
2498 * in the indirect case) by its index in the instruction store. The
2499 * pointer returned by next_insn() may become invalid if emitting the SEND
2500 * in the indirect case reallocs the store.
2501 */
2502
2503 if (desc.file == BRW_IMMEDIATE_VALUE) {
2504 setup = p->nr_insn;
2505 send = next_insn(p, BRW_OPCODE_SEND);
2506 brw_set_src1(p, send, desc);
2507
2508 } else {
2509 struct brw_reg addr = retype(brw_address_reg(0), BRW_REGISTER_TYPE_UD);
2510
2511 brw_push_insn_state(p);
2512 brw_set_default_access_mode(p, BRW_ALIGN_1);
2513 brw_set_default_mask_control(p, BRW_MASK_DISABLE);
2514 brw_set_default_exec_size(p, BRW_EXECUTE_1);
2515 brw_set_default_predicate_control(p, BRW_PREDICATE_NONE);
2516
2517 /* Load the indirect descriptor to an address register using OR so the
2518 * caller can specify additional descriptor bits with the usual
2519 * brw_set_*_message() helper functions.
2520 */
2521 setup = p->nr_insn;
2522 brw_OR(p, addr, desc, brw_imm_ud(0));
2523
2524 brw_pop_insn_state(p);
2525
2526 send = next_insn(p, BRW_OPCODE_SEND);
2527 brw_set_src1(p, send, addr);
2528 }
2529
2530 if (dst.width < BRW_EXECUTE_8)
2531 brw_inst_set_exec_size(devinfo, send, dst.width);
2532
2533 brw_set_dest(p, send, dst);
2534 brw_set_src0(p, send, retype(payload, BRW_REGISTER_TYPE_UD));
2535 brw_inst_set_sfid(devinfo, send, sfid);
2536
2537 return &p->store[setup];
2538 }
2539
2540 static struct brw_inst *
2541 brw_send_indirect_surface_message(struct brw_codegen *p,
2542 unsigned sfid,
2543 struct brw_reg dst,
2544 struct brw_reg payload,
2545 struct brw_reg surface,
2546 unsigned message_len,
2547 unsigned response_len,
2548 bool header_present)
2549 {
2550 const struct gen_device_info *devinfo = p->devinfo;
2551 struct brw_inst *insn;
2552
2553 if (surface.file != BRW_IMMEDIATE_VALUE) {
2554 struct brw_reg addr = retype(brw_address_reg(0), BRW_REGISTER_TYPE_UD);
2555
2556 brw_push_insn_state(p);
2557 brw_set_default_access_mode(p, BRW_ALIGN_1);
2558 brw_set_default_mask_control(p, BRW_MASK_DISABLE);
2559 brw_set_default_exec_size(p, BRW_EXECUTE_1);
2560 brw_set_default_predicate_control(p, BRW_PREDICATE_NONE);
2561
2562 /* Mask out invalid bits from the surface index to avoid hangs e.g. when
2563 * some surface array is accessed out of bounds.
2564 */
2565 insn = brw_AND(p, addr,
2566 suboffset(vec1(retype(surface, BRW_REGISTER_TYPE_UD)),
2567 BRW_GET_SWZ(surface.swizzle, 0)),
2568 brw_imm_ud(0xff));
2569
2570 brw_pop_insn_state(p);
2571
2572 surface = addr;
2573 }
2574
2575 insn = brw_send_indirect_message(p, sfid, dst, payload, surface);
2576 brw_inst_set_mlen(devinfo, insn, message_len);
2577 brw_inst_set_rlen(devinfo, insn, response_len);
2578 brw_inst_set_header_present(devinfo, insn, header_present);
2579
2580 return insn;
2581 }
2582
2583 static bool
2584 while_jumps_before_offset(const struct gen_device_info *devinfo,
2585 brw_inst *insn, int while_offset, int start_offset)
2586 {
2587 int scale = 16 / brw_jump_scale(devinfo);
2588 int jip = devinfo->gen == 6 ? brw_inst_gen6_jump_count(devinfo, insn)
2589 : brw_inst_jip(devinfo, insn);
2590 assert(jip < 0);
2591 return while_offset + jip * scale <= start_offset;
2592 }
2593
2594
2595 static int
2596 brw_find_next_block_end(struct brw_codegen *p, int start_offset)
2597 {
2598 int offset;
2599 void *store = p->store;
2600 const struct gen_device_info *devinfo = p->devinfo;
2601
2602 int depth = 0;
2603
2604 for (offset = next_offset(devinfo, store, start_offset);
2605 offset < p->next_insn_offset;
2606 offset = next_offset(devinfo, store, offset)) {
2607 brw_inst *insn = store + offset;
2608
2609 switch (brw_inst_opcode(devinfo, insn)) {
2610 case BRW_OPCODE_IF:
2611 depth++;
2612 break;
2613 case BRW_OPCODE_ENDIF:
2614 if (depth == 0)
2615 return offset;
2616 depth--;
2617 break;
2618 case BRW_OPCODE_WHILE:
2619 /* If the while doesn't jump before our instruction, it's the end
2620 * of a sibling do...while loop. Ignore it.
2621 */
2622 if (!while_jumps_before_offset(devinfo, insn, offset, start_offset))
2623 continue;
2624 /* fallthrough */
2625 case BRW_OPCODE_ELSE:
2626 case BRW_OPCODE_HALT:
2627 if (depth == 0)
2628 return offset;
2629 }
2630 }
2631
2632 return 0;
2633 }
2634
2635 /* There is no DO instruction on gen6, so to find the end of the loop
2636 * we have to see if the loop is jumping back before our start
2637 * instruction.
2638 */
2639 static int
2640 brw_find_loop_end(struct brw_codegen *p, int start_offset)
2641 {
2642 const struct gen_device_info *devinfo = p->devinfo;
2643 int offset;
2644 void *store = p->store;
2645
2646 assert(devinfo->gen >= 6);
2647
2648 /* Always start after the instruction (such as a WHILE) we're trying to fix
2649 * up.
2650 */
2651 for (offset = next_offset(devinfo, store, start_offset);
2652 offset < p->next_insn_offset;
2653 offset = next_offset(devinfo, store, offset)) {
2654 brw_inst *insn = store + offset;
2655
2656 if (brw_inst_opcode(devinfo, insn) == BRW_OPCODE_WHILE) {
2657 if (while_jumps_before_offset(devinfo, insn, offset, start_offset))
2658 return offset;
2659 }
2660 }
2661 assert(!"not reached");
2662 return start_offset;
2663 }
2664
2665 /* After program generation, go back and update the UIP and JIP of
2666 * BREAK, CONT, and HALT instructions to their correct locations.
2667 */
2668 void
2669 brw_set_uip_jip(struct brw_codegen *p, int start_offset)
2670 {
2671 const struct gen_device_info *devinfo = p->devinfo;
2672 int offset;
2673 int br = brw_jump_scale(devinfo);
2674 int scale = 16 / br;
2675 void *store = p->store;
2676
2677 if (devinfo->gen < 6)
2678 return;
2679
2680 for (offset = start_offset; offset < p->next_insn_offset; offset += 16) {
2681 brw_inst *insn = store + offset;
2682 assert(brw_inst_cmpt_control(devinfo, insn) == 0);
2683
2684 int block_end_offset = brw_find_next_block_end(p, offset);
2685 switch (brw_inst_opcode(devinfo, insn)) {
2686 case BRW_OPCODE_BREAK:
2687 assert(block_end_offset != 0);
2688 brw_inst_set_jip(devinfo, insn, (block_end_offset - offset) / scale);
2689 /* Gen7 UIP points to WHILE; Gen6 points just after it */
2690 brw_inst_set_uip(devinfo, insn,
2691 (brw_find_loop_end(p, offset) - offset +
2692 (devinfo->gen == 6 ? 16 : 0)) / scale);
2693 break;
2694 case BRW_OPCODE_CONTINUE:
2695 assert(block_end_offset != 0);
2696 brw_inst_set_jip(devinfo, insn, (block_end_offset - offset) / scale);
2697 brw_inst_set_uip(devinfo, insn,
2698 (brw_find_loop_end(p, offset) - offset) / scale);
2699
2700 assert(brw_inst_uip(devinfo, insn) != 0);
2701 assert(brw_inst_jip(devinfo, insn) != 0);
2702 break;
2703
2704 case BRW_OPCODE_ENDIF: {
2705 int32_t jump = (block_end_offset == 0) ?
2706 1 * br : (block_end_offset - offset) / scale;
2707 if (devinfo->gen >= 7)
2708 brw_inst_set_jip(devinfo, insn, jump);
2709 else
2710 brw_inst_set_gen6_jump_count(devinfo, insn, jump);
2711 break;
2712 }
2713
2714 case BRW_OPCODE_HALT:
2715 /* From the Sandy Bridge PRM (volume 4, part 2, section 8.3.19):
2716 *
2717 * "In case of the halt instruction not inside any conditional
2718 * code block, the value of <JIP> and <UIP> should be the
2719 * same. In case of the halt instruction inside conditional code
2720 * block, the <UIP> should be the end of the program, and the
2721 * <JIP> should be end of the most inner conditional code block."
2722 *
2723 * The uip will have already been set by whoever set up the
2724 * instruction.
2725 */
2726 if (block_end_offset == 0) {
2727 brw_inst_set_jip(devinfo, insn, brw_inst_uip(devinfo, insn));
2728 } else {
2729 brw_inst_set_jip(devinfo, insn, (block_end_offset - offset) / scale);
2730 }
2731 assert(brw_inst_uip(devinfo, insn) != 0);
2732 assert(brw_inst_jip(devinfo, insn) != 0);
2733 break;
2734 }
2735 }
2736 }
2737
2738 void brw_ff_sync(struct brw_codegen *p,
2739 struct brw_reg dest,
2740 unsigned msg_reg_nr,
2741 struct brw_reg src0,
2742 bool allocate,
2743 unsigned response_length,
2744 bool eot)
2745 {
2746 const struct gen_device_info *devinfo = p->devinfo;
2747 brw_inst *insn;
2748
2749 gen6_resolve_implied_move(p, &src0, msg_reg_nr);
2750
2751 insn = next_insn(p, BRW_OPCODE_SEND);
2752 brw_set_dest(p, insn, dest);
2753 brw_set_src0(p, insn, src0);
2754 brw_set_src1(p, insn, brw_imm_d(0));
2755
2756 if (devinfo->gen < 6)
2757 brw_inst_set_base_mrf(devinfo, insn, msg_reg_nr);
2758
2759 brw_set_ff_sync_message(p,
2760 insn,
2761 allocate,
2762 response_length,
2763 eot);
2764 }
2765
2766 /**
2767 * Emit the SEND instruction necessary to generate stream output data on Gen6
2768 * (for transform feedback).
2769 *
2770 * If send_commit_msg is true, this is the last piece of stream output data
2771 * from this thread, so send the data as a committed write. According to the
2772 * Sandy Bridge PRM (volume 2 part 1, section 4.5.1):
2773 *
2774 * "Prior to End of Thread with a URB_WRITE, the kernel must ensure all
2775 * writes are complete by sending the final write as a committed write."
2776 */
2777 void
2778 brw_svb_write(struct brw_codegen *p,
2779 struct brw_reg dest,
2780 unsigned msg_reg_nr,
2781 struct brw_reg src0,
2782 unsigned binding_table_index,
2783 bool send_commit_msg)
2784 {
2785 const struct gen_device_info *devinfo = p->devinfo;
2786 const unsigned target_cache =
2787 (devinfo->gen >= 7 ? GEN7_SFID_DATAPORT_DATA_CACHE :
2788 devinfo->gen >= 6 ? GEN6_SFID_DATAPORT_RENDER_CACHE :
2789 BRW_DATAPORT_READ_TARGET_RENDER_CACHE);
2790 brw_inst *insn;
2791
2792 gen6_resolve_implied_move(p, &src0, msg_reg_nr);
2793
2794 insn = next_insn(p, BRW_OPCODE_SEND);
2795 brw_set_dest(p, insn, dest);
2796 brw_set_src0(p, insn, src0);
2797 brw_set_src1(p, insn, brw_imm_d(0));
2798 brw_set_dp_write_message(p, insn,
2799 binding_table_index,
2800 0, /* msg_control: ignored */
2801 GEN6_DATAPORT_WRITE_MESSAGE_STREAMED_VB_WRITE,
2802 target_cache,
2803 1, /* msg_length */
2804 true, /* header_present */
2805 0, /* last_render_target: ignored */
2806 send_commit_msg, /* response_length */
2807 0, /* end_of_thread */
2808 send_commit_msg); /* send_commit_msg */
2809 }
2810
2811 static unsigned
2812 brw_surface_payload_size(struct brw_codegen *p,
2813 unsigned num_channels,
2814 bool has_simd4x2,
2815 bool has_simd16)
2816 {
2817 if (has_simd4x2 &&
2818 brw_inst_access_mode(p->devinfo, p->current) == BRW_ALIGN_16)
2819 return 1;
2820 else if (has_simd16 &&
2821 brw_inst_exec_size(p->devinfo, p->current) == BRW_EXECUTE_16)
2822 return 2 * num_channels;
2823 else
2824 return num_channels;
2825 }
2826
2827 static void
2828 brw_set_dp_untyped_atomic_message(struct brw_codegen *p,
2829 brw_inst *insn,
2830 unsigned atomic_op,
2831 bool response_expected)
2832 {
2833 const struct gen_device_info *devinfo = p->devinfo;
2834 unsigned msg_control =
2835 atomic_op | /* Atomic Operation Type: BRW_AOP_* */
2836 (response_expected ? 1 << 5 : 0); /* Return data expected */
2837
2838 if (devinfo->gen >= 8 || devinfo->is_haswell) {
2839 if (brw_inst_access_mode(devinfo, p->current) == BRW_ALIGN_1) {
2840 if (brw_inst_exec_size(devinfo, p->current) != BRW_EXECUTE_16)
2841 msg_control |= 1 << 4; /* SIMD8 mode */
2842
2843 brw_inst_set_dp_msg_type(devinfo, insn,
2844 HSW_DATAPORT_DC_PORT1_UNTYPED_ATOMIC_OP);
2845 } else {
2846 brw_inst_set_dp_msg_type(devinfo, insn,
2847 HSW_DATAPORT_DC_PORT1_UNTYPED_ATOMIC_OP_SIMD4X2);
2848 }
2849 } else {
2850 brw_inst_set_dp_msg_type(devinfo, insn,
2851 GEN7_DATAPORT_DC_UNTYPED_ATOMIC_OP);
2852
2853 if (brw_inst_exec_size(devinfo, p->current) != BRW_EXECUTE_16)
2854 msg_control |= 1 << 4; /* SIMD8 mode */
2855 }
2856
2857 brw_inst_set_dp_msg_control(devinfo, insn, msg_control);
2858 }
2859
2860 void
2861 brw_untyped_atomic(struct brw_codegen *p,
2862 struct brw_reg dst,
2863 struct brw_reg payload,
2864 struct brw_reg surface,
2865 unsigned atomic_op,
2866 unsigned msg_length,
2867 bool response_expected)
2868 {
2869 const struct gen_device_info *devinfo = p->devinfo;
2870 const unsigned sfid = (devinfo->gen >= 8 || devinfo->is_haswell ?
2871 HSW_SFID_DATAPORT_DATA_CACHE_1 :
2872 GEN7_SFID_DATAPORT_DATA_CACHE);
2873 const bool align1 = brw_inst_access_mode(devinfo, p->current) == BRW_ALIGN_1;
2874 /* Mask out unused components -- This is especially important in Align16
2875 * mode on generations that don't have native support for SIMD4x2 atomics,
2876 * because unused but enabled components will cause the dataport to perform
2877 * additional atomic operations on the addresses that happen to be in the
2878 * uninitialized Y, Z and W coordinates of the payload.
2879 */
2880 const unsigned mask = align1 ? WRITEMASK_XYZW : WRITEMASK_X;
2881 struct brw_inst *insn = brw_send_indirect_surface_message(
2882 p, sfid, brw_writemask(dst, mask), payload, surface, msg_length,
2883 brw_surface_payload_size(p, response_expected,
2884 devinfo->gen >= 8 || devinfo->is_haswell, true),
2885 align1);
2886
2887 brw_set_dp_untyped_atomic_message(
2888 p, insn, atomic_op, response_expected);
2889 }
2890
2891 static void
2892 brw_set_dp_untyped_surface_read_message(struct brw_codegen *p,
2893 struct brw_inst *insn,
2894 unsigned num_channels)
2895 {
2896 const struct gen_device_info *devinfo = p->devinfo;
2897 /* Set mask of 32-bit channels to drop. */
2898 unsigned msg_control = 0xf & (0xf << num_channels);
2899
2900 if (brw_inst_access_mode(devinfo, p->current) == BRW_ALIGN_1) {
2901 if (brw_inst_exec_size(devinfo, p->current) == BRW_EXECUTE_16)
2902 msg_control |= 1 << 4; /* SIMD16 mode */
2903 else
2904 msg_control |= 2 << 4; /* SIMD8 mode */
2905 }
2906
2907 brw_inst_set_dp_msg_type(devinfo, insn,
2908 (devinfo->gen >= 8 || devinfo->is_haswell ?
2909 HSW_DATAPORT_DC_PORT1_UNTYPED_SURFACE_READ :
2910 GEN7_DATAPORT_DC_UNTYPED_SURFACE_READ));
2911 brw_inst_set_dp_msg_control(devinfo, insn, msg_control);
2912 }
2913
2914 void
2915 brw_untyped_surface_read(struct brw_codegen *p,
2916 struct brw_reg dst,
2917 struct brw_reg payload,
2918 struct brw_reg surface,
2919 unsigned msg_length,
2920 unsigned num_channels)
2921 {
2922 const struct gen_device_info *devinfo = p->devinfo;
2923 const unsigned sfid = (devinfo->gen >= 8 || devinfo->is_haswell ?
2924 HSW_SFID_DATAPORT_DATA_CACHE_1 :
2925 GEN7_SFID_DATAPORT_DATA_CACHE);
2926 struct brw_inst *insn = brw_send_indirect_surface_message(
2927 p, sfid, dst, payload, surface, msg_length,
2928 brw_surface_payload_size(p, num_channels, true, true),
2929 false);
2930
2931 brw_set_dp_untyped_surface_read_message(
2932 p, insn, num_channels);
2933 }
2934
2935 static void
2936 brw_set_dp_untyped_surface_write_message(struct brw_codegen *p,
2937 struct brw_inst *insn,
2938 unsigned num_channels)
2939 {
2940 const struct gen_device_info *devinfo = p->devinfo;
2941 /* Set mask of 32-bit channels to drop. */
2942 unsigned msg_control = 0xf & (0xf << num_channels);
2943
2944 if (brw_inst_access_mode(devinfo, p->current) == BRW_ALIGN_1) {
2945 if (brw_inst_exec_size(devinfo, p->current) == BRW_EXECUTE_16)
2946 msg_control |= 1 << 4; /* SIMD16 mode */
2947 else
2948 msg_control |= 2 << 4; /* SIMD8 mode */
2949 } else {
2950 if (devinfo->gen >= 8 || devinfo->is_haswell)
2951 msg_control |= 0 << 4; /* SIMD4x2 mode */
2952 else
2953 msg_control |= 2 << 4; /* SIMD8 mode */
2954 }
2955
2956 brw_inst_set_dp_msg_type(devinfo, insn,
2957 devinfo->gen >= 8 || devinfo->is_haswell ?
2958 HSW_DATAPORT_DC_PORT1_UNTYPED_SURFACE_WRITE :
2959 GEN7_DATAPORT_DC_UNTYPED_SURFACE_WRITE);
2960 brw_inst_set_dp_msg_control(devinfo, insn, msg_control);
2961 }
2962
2963 void
2964 brw_untyped_surface_write(struct brw_codegen *p,
2965 struct brw_reg payload,
2966 struct brw_reg surface,
2967 unsigned msg_length,
2968 unsigned num_channels)
2969 {
2970 const struct gen_device_info *devinfo = p->devinfo;
2971 const unsigned sfid = (devinfo->gen >= 8 || devinfo->is_haswell ?
2972 HSW_SFID_DATAPORT_DATA_CACHE_1 :
2973 GEN7_SFID_DATAPORT_DATA_CACHE);
2974 const bool align1 = brw_inst_access_mode(devinfo, p->current) == BRW_ALIGN_1;
2975 /* Mask out unused components -- See comment in brw_untyped_atomic(). */
2976 const unsigned mask = devinfo->gen == 7 && !devinfo->is_haswell && !align1 ?
2977 WRITEMASK_X : WRITEMASK_XYZW;
2978 struct brw_inst *insn = brw_send_indirect_surface_message(
2979 p, sfid, brw_writemask(brw_null_reg(), mask),
2980 payload, surface, msg_length, 0, align1);
2981
2982 brw_set_dp_untyped_surface_write_message(
2983 p, insn, num_channels);
2984 }
2985
2986 static unsigned
2987 brw_byte_scattered_data_element_from_bit_size(unsigned bit_size)
2988 {
2989 switch (bit_size) {
2990 case 8:
2991 return GEN7_BYTE_SCATTERED_DATA_ELEMENT_BYTE;
2992 case 16:
2993 return GEN7_BYTE_SCATTERED_DATA_ELEMENT_WORD;
2994 case 32:
2995 return GEN7_BYTE_SCATTERED_DATA_ELEMENT_DWORD;
2996 default:
2997 unreachable("Unsupported bit_size for byte scattered messages");
2998 }
2999 }
3000
3001
3002 void
3003 brw_byte_scattered_read(struct brw_codegen *p,
3004 struct brw_reg dst,
3005 struct brw_reg payload,
3006 struct brw_reg surface,
3007 unsigned msg_length,
3008 unsigned bit_size)
3009 {
3010 const struct gen_device_info *devinfo = p->devinfo;
3011 assert(devinfo->gen > 7 || devinfo->is_haswell);
3012 assert(brw_inst_access_mode(devinfo, p->current) == BRW_ALIGN_1);
3013 const unsigned sfid = GEN7_SFID_DATAPORT_DATA_CACHE;
3014
3015 struct brw_inst *insn = brw_send_indirect_surface_message(
3016 p, sfid, dst, payload, surface, msg_length,
3017 brw_surface_payload_size(p, 1, true, true),
3018 false);
3019
3020 unsigned msg_control =
3021 brw_byte_scattered_data_element_from_bit_size(bit_size) << 2;
3022
3023 if (brw_inst_exec_size(devinfo, p->current) == BRW_EXECUTE_16)
3024 msg_control |= 1; /* SIMD16 mode */
3025 else
3026 msg_control |= 0; /* SIMD8 mode */
3027
3028 brw_inst_set_dp_msg_type(devinfo, insn,
3029 HSW_DATAPORT_DC_PORT0_BYTE_SCATTERED_READ);
3030 brw_inst_set_dp_msg_control(devinfo, insn, msg_control);
3031 }
3032
3033 void
3034 brw_byte_scattered_write(struct brw_codegen *p,
3035 struct brw_reg payload,
3036 struct brw_reg surface,
3037 unsigned msg_length,
3038 unsigned bit_size)
3039 {
3040 const struct gen_device_info *devinfo = p->devinfo;
3041 assert(devinfo->gen > 7 || devinfo->is_haswell);
3042 assert(brw_inst_access_mode(devinfo, p->current) == BRW_ALIGN_1);
3043 const unsigned sfid = GEN7_SFID_DATAPORT_DATA_CACHE;
3044
3045 struct brw_inst *insn = brw_send_indirect_surface_message(
3046 p, sfid, brw_writemask(brw_null_reg(), WRITEMASK_XYZW),
3047 payload, surface, msg_length, 0, true);
3048
3049 unsigned msg_control =
3050 brw_byte_scattered_data_element_from_bit_size(bit_size) << 2;
3051
3052 if (brw_inst_exec_size(devinfo, p->current) == BRW_EXECUTE_16)
3053 msg_control |= 1;
3054 else
3055 msg_control |= 0;
3056
3057 brw_inst_set_dp_msg_type(devinfo, insn,
3058 HSW_DATAPORT_DC_PORT0_BYTE_SCATTERED_WRITE);
3059 brw_inst_set_dp_msg_control(devinfo, insn, msg_control);
3060 }
3061
3062 static void
3063 brw_set_dp_typed_atomic_message(struct brw_codegen *p,
3064 struct brw_inst *insn,
3065 unsigned atomic_op,
3066 bool response_expected)
3067 {
3068 const struct gen_device_info *devinfo = p->devinfo;
3069 unsigned msg_control =
3070 atomic_op | /* Atomic Operation Type: BRW_AOP_* */
3071 (response_expected ? 1 << 5 : 0); /* Return data expected */
3072
3073 if (devinfo->gen >= 8 || devinfo->is_haswell) {
3074 if (brw_inst_access_mode(devinfo, p->current) == BRW_ALIGN_1) {
3075 if (brw_inst_qtr_control(devinfo, p->current) % 2 == 1)
3076 msg_control |= 1 << 4; /* Use high 8 slots of the sample mask */
3077
3078 brw_inst_set_dp_msg_type(devinfo, insn,
3079 HSW_DATAPORT_DC_PORT1_TYPED_ATOMIC_OP);
3080 } else {
3081 brw_inst_set_dp_msg_type(devinfo, insn,
3082 HSW_DATAPORT_DC_PORT1_TYPED_ATOMIC_OP_SIMD4X2);
3083 }
3084
3085 } else {
3086 brw_inst_set_dp_msg_type(devinfo, insn,
3087 GEN7_DATAPORT_RC_TYPED_ATOMIC_OP);
3088
3089 if (brw_inst_qtr_control(devinfo, p->current) % 2 == 1)
3090 msg_control |= 1 << 4; /* Use high 8 slots of the sample mask */
3091 }
3092
3093 brw_inst_set_dp_msg_control(devinfo, insn, msg_control);
3094 }
3095
3096 void
3097 brw_typed_atomic(struct brw_codegen *p,
3098 struct brw_reg dst,
3099 struct brw_reg payload,
3100 struct brw_reg surface,
3101 unsigned atomic_op,
3102 unsigned msg_length,
3103 bool response_expected) {
3104 const struct gen_device_info *devinfo = p->devinfo;
3105 const unsigned sfid = (devinfo->gen >= 8 || devinfo->is_haswell ?
3106 HSW_SFID_DATAPORT_DATA_CACHE_1 :
3107 GEN6_SFID_DATAPORT_RENDER_CACHE);
3108 const bool align1 = (brw_inst_access_mode(devinfo, p->current) == BRW_ALIGN_1);
3109 /* Mask out unused components -- See comment in brw_untyped_atomic(). */
3110 const unsigned mask = align1 ? WRITEMASK_XYZW : WRITEMASK_X;
3111 struct brw_inst *insn = brw_send_indirect_surface_message(
3112 p, sfid, brw_writemask(dst, mask), payload, surface, msg_length,
3113 brw_surface_payload_size(p, response_expected,
3114 devinfo->gen >= 8 || devinfo->is_haswell, false),
3115 true);
3116
3117 brw_set_dp_typed_atomic_message(
3118 p, insn, atomic_op, response_expected);
3119 }
3120
3121 static void
3122 brw_set_dp_typed_surface_read_message(struct brw_codegen *p,
3123 struct brw_inst *insn,
3124 unsigned num_channels)
3125 {
3126 const struct gen_device_info *devinfo = p->devinfo;
3127 /* Set mask of unused channels. */
3128 unsigned msg_control = 0xf & (0xf << num_channels);
3129
3130 if (devinfo->gen >= 8 || devinfo->is_haswell) {
3131 if (brw_inst_access_mode(devinfo, p->current) == BRW_ALIGN_1) {
3132 if (brw_inst_qtr_control(devinfo, p->current) % 2 == 1)
3133 msg_control |= 2 << 4; /* Use high 8 slots of the sample mask */
3134 else
3135 msg_control |= 1 << 4; /* Use low 8 slots of the sample mask */
3136 }
3137
3138 brw_inst_set_dp_msg_type(devinfo, insn,
3139 HSW_DATAPORT_DC_PORT1_TYPED_SURFACE_READ);
3140 } else {
3141 if (brw_inst_access_mode(devinfo, p->current) == BRW_ALIGN_1) {
3142 if (brw_inst_qtr_control(devinfo, p->current) % 2 == 1)
3143 msg_control |= 1 << 5; /* Use high 8 slots of the sample mask */
3144 }
3145
3146 brw_inst_set_dp_msg_type(devinfo, insn,
3147 GEN7_DATAPORT_RC_TYPED_SURFACE_READ);
3148 }
3149
3150 brw_inst_set_dp_msg_control(devinfo, insn, msg_control);
3151 }
3152
3153 void
3154 brw_typed_surface_read(struct brw_codegen *p,
3155 struct brw_reg dst,
3156 struct brw_reg payload,
3157 struct brw_reg surface,
3158 unsigned msg_length,
3159 unsigned num_channels)
3160 {
3161 const struct gen_device_info *devinfo = p->devinfo;
3162 const unsigned sfid = (devinfo->gen >= 8 || devinfo->is_haswell ?
3163 HSW_SFID_DATAPORT_DATA_CACHE_1 :
3164 GEN6_SFID_DATAPORT_RENDER_CACHE);
3165 struct brw_inst *insn = brw_send_indirect_surface_message(
3166 p, sfid, dst, payload, surface, msg_length,
3167 brw_surface_payload_size(p, num_channels,
3168 devinfo->gen >= 8 || devinfo->is_haswell, false),
3169 true);
3170
3171 brw_set_dp_typed_surface_read_message(
3172 p, insn, num_channels);
3173 }
3174
3175 static void
3176 brw_set_dp_typed_surface_write_message(struct brw_codegen *p,
3177 struct brw_inst *insn,
3178 unsigned num_channels)
3179 {
3180 const struct gen_device_info *devinfo = p->devinfo;
3181 /* Set mask of unused channels. */
3182 unsigned msg_control = 0xf & (0xf << num_channels);
3183
3184 if (devinfo->gen >= 8 || devinfo->is_haswell) {
3185 if (brw_inst_access_mode(devinfo, p->current) == BRW_ALIGN_1) {
3186 if (brw_inst_qtr_control(devinfo, p->current) % 2 == 1)
3187 msg_control |= 2 << 4; /* Use high 8 slots of the sample mask */
3188 else
3189 msg_control |= 1 << 4; /* Use low 8 slots of the sample mask */
3190 }
3191
3192 brw_inst_set_dp_msg_type(devinfo, insn,
3193 HSW_DATAPORT_DC_PORT1_TYPED_SURFACE_WRITE);
3194
3195 } else {
3196 if (brw_inst_access_mode(devinfo, p->current) == BRW_ALIGN_1) {
3197 if (brw_inst_qtr_control(devinfo, p->current) % 2 == 1)
3198 msg_control |= 1 << 5; /* Use high 8 slots of the sample mask */
3199 }
3200
3201 brw_inst_set_dp_msg_type(devinfo, insn,
3202 GEN7_DATAPORT_RC_TYPED_SURFACE_WRITE);
3203 }
3204
3205 brw_inst_set_dp_msg_control(devinfo, insn, msg_control);
3206 }
3207
3208 void
3209 brw_typed_surface_write(struct brw_codegen *p,
3210 struct brw_reg payload,
3211 struct brw_reg surface,
3212 unsigned msg_length,
3213 unsigned num_channels)
3214 {
3215 const struct gen_device_info *devinfo = p->devinfo;
3216 const unsigned sfid = (devinfo->gen >= 8 || devinfo->is_haswell ?
3217 HSW_SFID_DATAPORT_DATA_CACHE_1 :
3218 GEN6_SFID_DATAPORT_RENDER_CACHE);
3219 const bool align1 = (brw_inst_access_mode(devinfo, p->current) == BRW_ALIGN_1);
3220 /* Mask out unused components -- See comment in brw_untyped_atomic(). */
3221 const unsigned mask = (devinfo->gen == 7 && !devinfo->is_haswell && !align1 ?
3222 WRITEMASK_X : WRITEMASK_XYZW);
3223 struct brw_inst *insn = brw_send_indirect_surface_message(
3224 p, sfid, brw_writemask(brw_null_reg(), mask),
3225 payload, surface, msg_length, 0, true);
3226
3227 brw_set_dp_typed_surface_write_message(
3228 p, insn, num_channels);
3229 }
3230
3231 static void
3232 brw_set_memory_fence_message(struct brw_codegen *p,
3233 struct brw_inst *insn,
3234 enum brw_message_target sfid,
3235 bool commit_enable)
3236 {
3237 const struct gen_device_info *devinfo = p->devinfo;
3238
3239 brw_set_message_descriptor(p, insn, sfid,
3240 1 /* message length */,
3241 (commit_enable ? 1 : 0) /* response length */,
3242 true /* header present */,
3243 false);
3244
3245 switch (sfid) {
3246 case GEN6_SFID_DATAPORT_RENDER_CACHE:
3247 brw_inst_set_dp_msg_type(devinfo, insn, GEN7_DATAPORT_RC_MEMORY_FENCE);
3248 break;
3249 case GEN7_SFID_DATAPORT_DATA_CACHE:
3250 brw_inst_set_dp_msg_type(devinfo, insn, GEN7_DATAPORT_DC_MEMORY_FENCE);
3251 break;
3252 default:
3253 unreachable("Not reached");
3254 }
3255
3256 if (commit_enable)
3257 brw_inst_set_dp_msg_control(devinfo, insn, 1 << 5);
3258 }
3259
3260 void
3261 brw_memory_fence(struct brw_codegen *p,
3262 struct brw_reg dst)
3263 {
3264 const struct gen_device_info *devinfo = p->devinfo;
3265 const bool commit_enable = devinfo->gen == 7 && !devinfo->is_haswell;
3266 struct brw_inst *insn;
3267
3268 brw_push_insn_state(p);
3269 brw_set_default_mask_control(p, BRW_MASK_DISABLE);
3270 brw_set_default_exec_size(p, BRW_EXECUTE_1);
3271 dst = vec1(dst);
3272
3273 /* Set dst as destination for dependency tracking, the MEMORY_FENCE
3274 * message doesn't write anything back.
3275 */
3276 insn = next_insn(p, BRW_OPCODE_SEND);
3277 dst = retype(dst, BRW_REGISTER_TYPE_UW);
3278 brw_set_dest(p, insn, dst);
3279 brw_set_src0(p, insn, dst);
3280 brw_set_memory_fence_message(p, insn, GEN7_SFID_DATAPORT_DATA_CACHE,
3281 commit_enable);
3282
3283 if (devinfo->gen == 7 && !devinfo->is_haswell) {
3284 /* IVB does typed surface access through the render cache, so we need to
3285 * flush it too. Use a different register so both flushes can be
3286 * pipelined by the hardware.
3287 */
3288 insn = next_insn(p, BRW_OPCODE_SEND);
3289 brw_set_dest(p, insn, offset(dst, 1));
3290 brw_set_src0(p, insn, offset(dst, 1));
3291 brw_set_memory_fence_message(p, insn, GEN6_SFID_DATAPORT_RENDER_CACHE,
3292 commit_enable);
3293
3294 /* Now write the response of the second message into the response of the
3295 * first to trigger a pipeline stall -- This way future render and data
3296 * cache messages will be properly ordered with respect to past data and
3297 * render cache messages.
3298 */
3299 brw_MOV(p, dst, offset(dst, 1));
3300 }
3301
3302 brw_pop_insn_state(p);
3303 }
3304
3305 void
3306 brw_pixel_interpolator_query(struct brw_codegen *p,
3307 struct brw_reg dest,
3308 struct brw_reg mrf,
3309 bool noperspective,
3310 unsigned mode,
3311 struct brw_reg data,
3312 unsigned msg_length,
3313 unsigned response_length)
3314 {
3315 const struct gen_device_info *devinfo = p->devinfo;
3316 struct brw_inst *insn;
3317 const uint16_t exec_size = brw_inst_exec_size(devinfo, p->current);
3318
3319 /* brw_send_indirect_message will automatically use a direct send message
3320 * if data is actually immediate.
3321 */
3322 insn = brw_send_indirect_message(p,
3323 GEN7_SFID_PIXEL_INTERPOLATOR,
3324 dest,
3325 mrf,
3326 vec1(data));
3327 brw_inst_set_mlen(devinfo, insn, msg_length);
3328 brw_inst_set_rlen(devinfo, insn, response_length);
3329
3330 brw_inst_set_pi_simd_mode(devinfo, insn, exec_size == BRW_EXECUTE_16);
3331 brw_inst_set_pi_slot_group(devinfo, insn, 0); /* zero unless 32/64px dispatch */
3332 brw_inst_set_pi_nopersp(devinfo, insn, noperspective);
3333 brw_inst_set_pi_message_type(devinfo, insn, mode);
3334 }
3335
3336 void
3337 brw_find_live_channel(struct brw_codegen *p, struct brw_reg dst,
3338 struct brw_reg mask)
3339 {
3340 const struct gen_device_info *devinfo = p->devinfo;
3341 const unsigned exec_size = 1 << brw_inst_exec_size(devinfo, p->current);
3342 const unsigned qtr_control = brw_inst_qtr_control(devinfo, p->current);
3343 brw_inst *inst;
3344
3345 assert(devinfo->gen >= 7);
3346 assert(mask.type == BRW_REGISTER_TYPE_UD);
3347
3348 brw_push_insn_state(p);
3349
3350 if (brw_inst_access_mode(devinfo, p->current) == BRW_ALIGN_1) {
3351 brw_set_default_mask_control(p, BRW_MASK_DISABLE);
3352
3353 if (devinfo->gen >= 8) {
3354 /* Getting the first active channel index is easy on Gen8: Just find
3355 * the first bit set in the execution mask. The register exists on
3356 * HSW already but it reads back as all ones when the current
3357 * instruction has execution masking disabled, so it's kind of
3358 * useless.
3359 */
3360 struct brw_reg exec_mask =
3361 retype(brw_mask_reg(0), BRW_REGISTER_TYPE_UD);
3362
3363 brw_set_default_exec_size(p, BRW_EXECUTE_1);
3364 if (mask.file != BRW_IMMEDIATE_VALUE || mask.ud != 0xffffffff) {
3365 /* Unfortunately, ce0 does not take into account the thread
3366 * dispatch mask, which may be a problem in cases where it's not
3367 * tightly packed (i.e. it doesn't have the form '2^n - 1' for
3368 * some n). Combine ce0 with the given dispatch (or vector) mask
3369 * to mask off those channels which were never dispatched by the
3370 * hardware.
3371 */
3372 brw_SHR(p, vec1(dst), mask, brw_imm_ud(qtr_control * 8));
3373 brw_AND(p, vec1(dst), exec_mask, vec1(dst));
3374 exec_mask = vec1(dst);
3375 }
3376
3377 /* Quarter control has the effect of magically shifting the value of
3378 * ce0 so you'll get the first active channel relative to the
3379 * specified quarter control as result.
3380 */
3381 inst = brw_FBL(p, vec1(dst), exec_mask);
3382 } else {
3383 const struct brw_reg flag = brw_flag_reg(1, 0);
3384
3385 brw_set_default_exec_size(p, BRW_EXECUTE_1);
3386 brw_MOV(p, retype(flag, BRW_REGISTER_TYPE_UD), brw_imm_ud(0));
3387
3388 /* Run enough instructions returning zero with execution masking and
3389 * a conditional modifier enabled in order to get the full execution
3390 * mask in f1.0. We could use a single 32-wide move here if it
3391 * weren't because of the hardware bug that causes channel enables to
3392 * be applied incorrectly to the second half of 32-wide instructions
3393 * on Gen7.
3394 */
3395 const unsigned lower_size = MIN2(16, exec_size);
3396 for (unsigned i = 0; i < exec_size / lower_size; i++) {
3397 inst = brw_MOV(p, retype(brw_null_reg(), BRW_REGISTER_TYPE_UW),
3398 brw_imm_uw(0));
3399 brw_inst_set_mask_control(devinfo, inst, BRW_MASK_ENABLE);
3400 brw_inst_set_group(devinfo, inst, lower_size * i + 8 * qtr_control);
3401 brw_inst_set_cond_modifier(devinfo, inst, BRW_CONDITIONAL_Z);
3402 brw_inst_set_flag_reg_nr(devinfo, inst, 1);
3403 brw_inst_set_exec_size(devinfo, inst, cvt(lower_size) - 1);
3404 }
3405
3406 /* Find the first bit set in the exec_size-wide portion of the flag
3407 * register that was updated by the last sequence of MOV
3408 * instructions.
3409 */
3410 const enum brw_reg_type type = brw_int_type(exec_size / 8, false);
3411 brw_set_default_exec_size(p, BRW_EXECUTE_1);
3412 brw_FBL(p, vec1(dst), byte_offset(retype(flag, type), qtr_control));
3413 }
3414 } else {
3415 brw_set_default_mask_control(p, BRW_MASK_DISABLE);
3416
3417 if (devinfo->gen >= 8 &&
3418 mask.file == BRW_IMMEDIATE_VALUE && mask.ud == 0xffffffff) {
3419 /* In SIMD4x2 mode the first active channel index is just the
3420 * negation of the first bit of the mask register. Note that ce0
3421 * doesn't take into account the dispatch mask, so the Gen7 path
3422 * should be used instead unless you have the guarantee that the
3423 * dispatch mask is tightly packed (i.e. it has the form '2^n - 1'
3424 * for some n).
3425 */
3426 inst = brw_AND(p, brw_writemask(dst, WRITEMASK_X),
3427 negate(retype(brw_mask_reg(0), BRW_REGISTER_TYPE_UD)),
3428 brw_imm_ud(1));
3429
3430 } else {
3431 /* Overwrite the destination without and with execution masking to
3432 * find out which of the channels is active.
3433 */
3434 brw_push_insn_state(p);
3435 brw_set_default_exec_size(p, BRW_EXECUTE_4);
3436 brw_MOV(p, brw_writemask(vec4(dst), WRITEMASK_X),
3437 brw_imm_ud(1));
3438
3439 inst = brw_MOV(p, brw_writemask(vec4(dst), WRITEMASK_X),
3440 brw_imm_ud(0));
3441 brw_pop_insn_state(p);
3442 brw_inst_set_mask_control(devinfo, inst, BRW_MASK_ENABLE);
3443 }
3444 }
3445
3446 brw_pop_insn_state(p);
3447 }
3448
3449 void
3450 brw_broadcast(struct brw_codegen *p,
3451 struct brw_reg dst,
3452 struct brw_reg src,
3453 struct brw_reg idx)
3454 {
3455 const struct gen_device_info *devinfo = p->devinfo;
3456 const bool align1 = brw_inst_access_mode(devinfo, p->current) == BRW_ALIGN_1;
3457 brw_inst *inst;
3458
3459 brw_push_insn_state(p);
3460 brw_set_default_mask_control(p, BRW_MASK_DISABLE);
3461 brw_set_default_exec_size(p, align1 ? BRW_EXECUTE_1 : BRW_EXECUTE_4);
3462
3463 assert(src.file == BRW_GENERAL_REGISTER_FILE &&
3464 src.address_mode == BRW_ADDRESS_DIRECT);
3465 assert(!src.abs && !src.negate);
3466 assert(src.type == dst.type);
3467
3468 if ((src.vstride == 0 && (src.hstride == 0 || !align1)) ||
3469 idx.file == BRW_IMMEDIATE_VALUE) {
3470 /* Trivial, the source is already uniform or the index is a constant.
3471 * We will typically not get here if the optimizer is doing its job, but
3472 * asserting would be mean.
3473 */
3474 const unsigned i = idx.file == BRW_IMMEDIATE_VALUE ? idx.ud : 0;
3475 brw_MOV(p, dst,
3476 (align1 ? stride(suboffset(src, i), 0, 1, 0) :
3477 stride(suboffset(src, 4 * i), 0, 4, 1)));
3478 } else {
3479 /* From the Haswell PRM section "Register Region Restrictions":
3480 *
3481 * "The lower bits of the AddressImmediate must not overflow to
3482 * change the register address. The lower 5 bits of Address
3483 * Immediate when added to lower 5 bits of address register gives
3484 * the sub-register offset. The upper bits of Address Immediate
3485 * when added to upper bits of address register gives the register
3486 * address. Any overflow from sub-register offset is dropped."
3487 *
3488 * Fortunately, for broadcast, we never have a sub-register offset so
3489 * this isn't an issue.
3490 */
3491 assert(src.subnr == 0);
3492
3493 if (align1) {
3494 const struct brw_reg addr =
3495 retype(brw_address_reg(0), BRW_REGISTER_TYPE_UD);
3496 unsigned offset = src.nr * REG_SIZE + src.subnr;
3497 /* Limit in bytes of the signed indirect addressing immediate. */
3498 const unsigned limit = 512;
3499
3500 brw_push_insn_state(p);
3501 brw_set_default_mask_control(p, BRW_MASK_DISABLE);
3502 brw_set_default_predicate_control(p, BRW_PREDICATE_NONE);
3503
3504 /* Take into account the component size and horizontal stride. */
3505 assert(src.vstride == src.hstride + src.width);
3506 brw_SHL(p, addr, vec1(idx),
3507 brw_imm_ud(_mesa_logbase2(type_sz(src.type)) +
3508 src.hstride - 1));
3509
3510 /* We can only address up to limit bytes using the indirect
3511 * addressing immediate, account for the difference if the source
3512 * register is above this limit.
3513 */
3514 if (offset >= limit) {
3515 brw_ADD(p, addr, addr, brw_imm_ud(offset - offset % limit));
3516 offset = offset % limit;
3517 }
3518
3519 brw_pop_insn_state(p);
3520
3521 /* Use indirect addressing to fetch the specified component. */
3522 if (type_sz(src.type) > 4 &&
3523 (devinfo->is_cherryview || gen_device_info_is_9lp(devinfo))) {
3524 /* From the Cherryview PRM Vol 7. "Register Region Restrictions":
3525 *
3526 * "When source or destination datatype is 64b or operation is
3527 * integer DWord multiply, indirect addressing must not be
3528 * used."
3529 *
3530 * To work around both of this issue, we do two integer MOVs
3531 * insead of one 64-bit MOV. Because no double value should ever
3532 * cross a register boundary, it's safe to use the immediate
3533 * offset in the indirect here to handle adding 4 bytes to the
3534 * offset and avoid the extra ADD to the register file.
3535 */
3536 brw_MOV(p, subscript(dst, BRW_REGISTER_TYPE_D, 0),
3537 retype(brw_vec1_indirect(addr.subnr, offset),
3538 BRW_REGISTER_TYPE_D));
3539 brw_MOV(p, subscript(dst, BRW_REGISTER_TYPE_D, 1),
3540 retype(brw_vec1_indirect(addr.subnr, offset + 4),
3541 BRW_REGISTER_TYPE_D));
3542 } else {
3543 brw_MOV(p, dst,
3544 retype(brw_vec1_indirect(addr.subnr, offset), src.type));
3545 }
3546 } else {
3547 /* In SIMD4x2 mode the index can be either zero or one, replicate it
3548 * to all bits of a flag register,
3549 */
3550 inst = brw_MOV(p,
3551 brw_null_reg(),
3552 stride(brw_swizzle(idx, BRW_SWIZZLE_XXXX), 4, 4, 1));
3553 brw_inst_set_pred_control(devinfo, inst, BRW_PREDICATE_NONE);
3554 brw_inst_set_cond_modifier(devinfo, inst, BRW_CONDITIONAL_NZ);
3555 brw_inst_set_flag_reg_nr(devinfo, inst, 1);
3556
3557 /* and use predicated SEL to pick the right channel. */
3558 inst = brw_SEL(p, dst,
3559 stride(suboffset(src, 4), 4, 4, 1),
3560 stride(src, 4, 4, 1));
3561 brw_inst_set_pred_control(devinfo, inst, BRW_PREDICATE_NORMAL);
3562 brw_inst_set_flag_reg_nr(devinfo, inst, 1);
3563 }
3564 }
3565
3566 brw_pop_insn_state(p);
3567 }
3568
3569 /**
3570 * This instruction is generated as a single-channel align1 instruction by
3571 * both the VS and FS stages when using INTEL_DEBUG=shader_time.
3572 *
3573 * We can't use the typed atomic op in the FS because that has the execution
3574 * mask ANDed with the pixel mask, but we just want to write the one dword for
3575 * all the pixels.
3576 *
3577 * We don't use the SIMD4x2 atomic ops in the VS because want to just write
3578 * one u32. So we use the same untyped atomic write message as the pixel
3579 * shader.
3580 *
3581 * The untyped atomic operation requires a BUFFER surface type with RAW
3582 * format, and is only accessible through the legacy DATA_CACHE dataport
3583 * messages.
3584 */
3585 void brw_shader_time_add(struct brw_codegen *p,
3586 struct brw_reg payload,
3587 uint32_t surf_index)
3588 {
3589 const unsigned sfid = (p->devinfo->gen >= 8 || p->devinfo->is_haswell ?
3590 HSW_SFID_DATAPORT_DATA_CACHE_1 :
3591 GEN7_SFID_DATAPORT_DATA_CACHE);
3592 assert(p->devinfo->gen >= 7);
3593
3594 brw_push_insn_state(p);
3595 brw_set_default_access_mode(p, BRW_ALIGN_1);
3596 brw_set_default_mask_control(p, BRW_MASK_DISABLE);
3597 brw_set_default_compression_control(p, BRW_COMPRESSION_NONE);
3598 brw_inst *send = brw_next_insn(p, BRW_OPCODE_SEND);
3599
3600 /* We use brw_vec1_reg and unmasked because we want to increment the given
3601 * offset only once.
3602 */
3603 brw_set_dest(p, send, brw_vec1_reg(BRW_ARCHITECTURE_REGISTER_FILE,
3604 BRW_ARF_NULL, 0));
3605 brw_set_src0(p, send, brw_vec1_reg(payload.file,
3606 payload.nr, 0));
3607 brw_set_src1(p, send, brw_imm_ud(0));
3608 brw_set_message_descriptor(p, send, sfid, 2, 0, false, false);
3609 brw_inst_set_binding_table_index(p->devinfo, send, surf_index);
3610 brw_set_dp_untyped_atomic_message(p, send, BRW_AOP_ADD, false);
3611
3612 brw_pop_insn_state(p);
3613 }
3614
3615
3616 /**
3617 * Emit the SEND message for a barrier
3618 */
3619 void
3620 brw_barrier(struct brw_codegen *p, struct brw_reg src)
3621 {
3622 const struct gen_device_info *devinfo = p->devinfo;
3623 struct brw_inst *inst;
3624
3625 assert(devinfo->gen >= 7);
3626
3627 brw_push_insn_state(p);
3628 brw_set_default_access_mode(p, BRW_ALIGN_1);
3629 inst = next_insn(p, BRW_OPCODE_SEND);
3630 brw_set_dest(p, inst, retype(brw_null_reg(), BRW_REGISTER_TYPE_UW));
3631 brw_set_src0(p, inst, src);
3632 brw_set_src1(p, inst, brw_null_reg());
3633
3634 brw_set_message_descriptor(p, inst, BRW_SFID_MESSAGE_GATEWAY,
3635 1 /* msg_length */,
3636 0 /* response_length */,
3637 false /* header_present */,
3638 false /* end_of_thread */);
3639
3640 brw_inst_set_gateway_notify(devinfo, inst, 1);
3641 brw_inst_set_gateway_subfuncid(devinfo, inst,
3642 BRW_MESSAGE_GATEWAY_SFID_BARRIER_MSG);
3643
3644 brw_inst_set_mask_control(devinfo, inst, BRW_MASK_DISABLE);
3645 brw_pop_insn_state(p);
3646 }
3647
3648
3649 /**
3650 * Emit the wait instruction for a barrier
3651 */
3652 void
3653 brw_WAIT(struct brw_codegen *p)
3654 {
3655 const struct gen_device_info *devinfo = p->devinfo;
3656 struct brw_inst *insn;
3657
3658 struct brw_reg src = brw_notification_reg();
3659
3660 insn = next_insn(p, BRW_OPCODE_WAIT);
3661 brw_set_dest(p, insn, src);
3662 brw_set_src0(p, insn, src);
3663 brw_set_src1(p, insn, brw_null_reg());
3664
3665 brw_inst_set_exec_size(devinfo, insn, BRW_EXECUTE_1);
3666 brw_inst_set_mask_control(devinfo, insn, BRW_MASK_DISABLE);
3667 }
3668
3669 /**
3670 * Changes the floating point rounding mode updating the control register
3671 * field defined at cr0.0[5-6] bits. This function supports the changes to
3672 * RTNE (00), RU (01), RD (10) and RTZ (11) rounding using bitwise operations.
3673 * Only RTNE and RTZ rounding are enabled at nir.
3674 */
3675 void
3676 brw_rounding_mode(struct brw_codegen *p,
3677 enum brw_rnd_mode mode)
3678 {
3679 const unsigned bits = mode << BRW_CR0_RND_MODE_SHIFT;
3680
3681 if (bits != BRW_CR0_RND_MODE_MASK) {
3682 brw_inst *inst = brw_AND(p, brw_cr0_reg(0), brw_cr0_reg(0),
3683 brw_imm_ud(~BRW_CR0_RND_MODE_MASK));
3684
3685 /* From the Skylake PRM, Volume 7, page 760:
3686 * "Implementation Restriction on Register Access: When the control
3687 * register is used as an explicit source and/or destination, hardware
3688 * does not ensure execution pipeline coherency. Software must set the
3689 * thread control field to ‘switch’ for an instruction that uses
3690 * control register as an explicit operand."
3691 */
3692 brw_inst_set_thread_control(p->devinfo, inst, BRW_THREAD_SWITCH);
3693 }
3694
3695 if (bits) {
3696 brw_inst *inst = brw_OR(p, brw_cr0_reg(0), brw_cr0_reg(0),
3697 brw_imm_ud(bits));
3698 brw_inst_set_thread_control(p->devinfo, inst, BRW_THREAD_SWITCH);
3699 }
3700 }