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