1 /* Copyright © 2011 Intel Corporation
2  *
3  * Permission is hereby granted, free of charge, to any person obtaining a
4  * copy of this software and associated documentation files (the "Software"),
5  * to deal in the Software without restriction, including without limitation
6  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
7  * and/or sell copies of the Software, and to permit persons to whom the
8  * Software is furnished to do so, subject to the following conditions:
9  *
10  * The above copyright notice and this permission notice (including the next
11  * paragraph) shall be included in all copies or substantial portions of the
12  * Software.
13  *
14  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
17  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
19  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
20  * IN THE SOFTWARE.
21  */
22 
23 #include "brw_vec4.h"
24 #include "brw_cfg.h"
25 #include "brw_eu.h"
26 #include "dev/gen_debug.h"
27 #include "util/mesa-sha1.h"
28 
29 using namespace brw;
30 
31 static void
generate_math1_gen4(struct brw_codegen * p,vec4_instruction * inst,struct brw_reg dst,struct brw_reg src)32 generate_math1_gen4(struct brw_codegen *p,
33                     vec4_instruction *inst,
34                     struct brw_reg dst,
35                     struct brw_reg src)
36 {
37    gen4_math(p,
38 	     dst,
39 	     brw_math_function(inst->opcode),
40 	     inst->base_mrf,
41 	     src,
42 	     BRW_MATH_PRECISION_FULL);
43 }
44 
45 static void
check_gen6_math_src_arg(struct brw_reg src)46 check_gen6_math_src_arg(struct brw_reg src)
47 {
48    /* Source swizzles are ignored. */
49    assert(!src.abs);
50    assert(!src.negate);
51    assert(src.swizzle == BRW_SWIZZLE_XYZW);
52 }
53 
54 static void
generate_math_gen6(struct brw_codegen * p,vec4_instruction * inst,struct brw_reg dst,struct brw_reg src0,struct brw_reg src1)55 generate_math_gen6(struct brw_codegen *p,
56                    vec4_instruction *inst,
57                    struct brw_reg dst,
58                    struct brw_reg src0,
59                    struct brw_reg src1)
60 {
61    /* Can't do writemask because math can't be align16. */
62    assert(dst.writemask == WRITEMASK_XYZW);
63    /* Source swizzles are ignored. */
64    check_gen6_math_src_arg(src0);
65    if (src1.file == BRW_GENERAL_REGISTER_FILE)
66       check_gen6_math_src_arg(src1);
67 
68    brw_set_default_access_mode(p, BRW_ALIGN_1);
69    gen6_math(p, dst, brw_math_function(inst->opcode), src0, src1);
70    brw_set_default_access_mode(p, BRW_ALIGN_16);
71 }
72 
73 static void
generate_math2_gen4(struct brw_codegen * p,vec4_instruction * inst,struct brw_reg dst,struct brw_reg src0,struct brw_reg src1)74 generate_math2_gen4(struct brw_codegen *p,
75                     vec4_instruction *inst,
76                     struct brw_reg dst,
77                     struct brw_reg src0,
78                     struct brw_reg src1)
79 {
80    /* From the Ironlake PRM, Volume 4, Part 1, Section 6.1.13
81     * "Message Payload":
82     *
83     * "Operand0[7].  For the INT DIV functions, this operand is the
84     *  denominator."
85     *  ...
86     * "Operand1[7].  For the INT DIV functions, this operand is the
87     *  numerator."
88     */
89    bool is_int_div = inst->opcode != SHADER_OPCODE_POW;
90    struct brw_reg &op0 = is_int_div ? src1 : src0;
91    struct brw_reg &op1 = is_int_div ? src0 : src1;
92 
93    brw_push_insn_state(p);
94    brw_set_default_saturate(p, false);
95    brw_set_default_predicate_control(p, BRW_PREDICATE_NONE);
96    brw_MOV(p, retype(brw_message_reg(inst->base_mrf + 1), op1.type), op1);
97    brw_pop_insn_state(p);
98 
99    gen4_math(p,
100 	     dst,
101 	     brw_math_function(inst->opcode),
102 	     inst->base_mrf,
103 	     op0,
104 	     BRW_MATH_PRECISION_FULL);
105 }
106 
107 static void
generate_tex(struct brw_codegen * p,struct brw_vue_prog_data * prog_data,gl_shader_stage stage,vec4_instruction * inst,struct brw_reg dst,struct brw_reg src,struct brw_reg surface_index,struct brw_reg sampler_index)108 generate_tex(struct brw_codegen *p,
109              struct brw_vue_prog_data *prog_data,
110              gl_shader_stage stage,
111              vec4_instruction *inst,
112              struct brw_reg dst,
113              struct brw_reg src,
114              struct brw_reg surface_index,
115              struct brw_reg sampler_index)
116 {
117    const struct gen_device_info *devinfo = p->devinfo;
118    int msg_type = -1;
119 
120    if (devinfo->gen >= 5) {
121       switch (inst->opcode) {
122       case SHADER_OPCODE_TEX:
123       case SHADER_OPCODE_TXL:
124 	 if (inst->shadow_compare) {
125 	    msg_type = GEN5_SAMPLER_MESSAGE_SAMPLE_LOD_COMPARE;
126 	 } else {
127 	    msg_type = GEN5_SAMPLER_MESSAGE_SAMPLE_LOD;
128 	 }
129 	 break;
130       case SHADER_OPCODE_TXD:
131          if (inst->shadow_compare) {
132             /* Gen7.5+.  Otherwise, lowered by brw_lower_texture_gradients(). */
133             assert(devinfo->is_haswell);
134             msg_type = HSW_SAMPLER_MESSAGE_SAMPLE_DERIV_COMPARE;
135          } else {
136             msg_type = GEN5_SAMPLER_MESSAGE_SAMPLE_DERIVS;
137          }
138 	 break;
139       case SHADER_OPCODE_TXF:
140 	 msg_type = GEN5_SAMPLER_MESSAGE_SAMPLE_LD;
141 	 break;
142       case SHADER_OPCODE_TXF_CMS:
143          if (devinfo->gen >= 7)
144             msg_type = GEN7_SAMPLER_MESSAGE_SAMPLE_LD2DMS;
145          else
146             msg_type = GEN5_SAMPLER_MESSAGE_SAMPLE_LD;
147          break;
148       case SHADER_OPCODE_TXF_MCS:
149          assert(devinfo->gen >= 7);
150          msg_type = GEN7_SAMPLER_MESSAGE_SAMPLE_LD_MCS;
151          break;
152       case SHADER_OPCODE_TXS:
153 	 msg_type = GEN5_SAMPLER_MESSAGE_SAMPLE_RESINFO;
154 	 break;
155       case SHADER_OPCODE_TG4:
156          if (inst->shadow_compare) {
157             msg_type = GEN7_SAMPLER_MESSAGE_SAMPLE_GATHER4_C;
158          } else {
159             msg_type = GEN7_SAMPLER_MESSAGE_SAMPLE_GATHER4;
160          }
161          break;
162       case SHADER_OPCODE_TG4_OFFSET:
163          if (inst->shadow_compare) {
164             msg_type = GEN7_SAMPLER_MESSAGE_SAMPLE_GATHER4_PO_C;
165          } else {
166             msg_type = GEN7_SAMPLER_MESSAGE_SAMPLE_GATHER4_PO;
167          }
168          break;
169       case SHADER_OPCODE_SAMPLEINFO:
170          msg_type = GEN6_SAMPLER_MESSAGE_SAMPLE_SAMPLEINFO;
171          break;
172       default:
173 	 unreachable("should not get here: invalid vec4 texture opcode");
174       }
175    } else {
176       switch (inst->opcode) {
177       case SHADER_OPCODE_TEX:
178       case SHADER_OPCODE_TXL:
179 	 if (inst->shadow_compare) {
180 	    msg_type = BRW_SAMPLER_MESSAGE_SIMD4X2_SAMPLE_LOD_COMPARE;
181 	    assert(inst->mlen == 3);
182 	 } else {
183 	    msg_type = BRW_SAMPLER_MESSAGE_SIMD4X2_SAMPLE_LOD;
184 	    assert(inst->mlen == 2);
185 	 }
186 	 break;
187       case SHADER_OPCODE_TXD:
188 	 /* There is no sample_d_c message; comparisons are done manually. */
189 	 msg_type = BRW_SAMPLER_MESSAGE_SIMD4X2_SAMPLE_GRADIENTS;
190 	 assert(inst->mlen == 4);
191 	 break;
192       case SHADER_OPCODE_TXF:
193 	 msg_type = BRW_SAMPLER_MESSAGE_SIMD4X2_LD;
194 	 assert(inst->mlen == 2);
195 	 break;
196       case SHADER_OPCODE_TXS:
197 	 msg_type = BRW_SAMPLER_MESSAGE_SIMD4X2_RESINFO;
198 	 assert(inst->mlen == 2);
199 	 break;
200       default:
201 	 unreachable("should not get here: invalid vec4 texture opcode");
202       }
203    }
204 
205    assert(msg_type != -1);
206 
207    assert(sampler_index.type == BRW_REGISTER_TYPE_UD);
208 
209    /* Load the message header if present.  If there's a texture offset, we need
210     * to set it up explicitly and load the offset bitfield.  Otherwise, we can
211     * use an implied move from g0 to the first message register.
212     */
213    if (inst->header_size != 0) {
214       if (devinfo->gen < 6 && !inst->offset) {
215          /* Set up an implied move from g0 to the MRF. */
216          src = brw_vec8_grf(0, 0);
217       } else {
218          struct brw_reg header =
219             retype(brw_message_reg(inst->base_mrf), BRW_REGISTER_TYPE_UD);
220          uint32_t dw2 = 0;
221 
222          /* Explicitly set up the message header by copying g0 to the MRF. */
223          brw_push_insn_state(p);
224          brw_set_default_mask_control(p, BRW_MASK_DISABLE);
225          brw_MOV(p, header, retype(brw_vec8_grf(0, 0), BRW_REGISTER_TYPE_UD));
226 
227          brw_set_default_access_mode(p, BRW_ALIGN_1);
228 
229          if (inst->offset)
230             /* Set the texel offset bits in DWord 2. */
231             dw2 = inst->offset;
232 
233          /* The VS, DS, and FS stages have the g0.2 payload delivered as 0,
234           * so header0.2 is 0 when g0 is copied.  The HS and GS stages do
235           * not, so we must set to to 0 to avoid setting undesirable bits
236           * in the message header.
237           */
238          if (dw2 ||
239              stage == MESA_SHADER_TESS_CTRL ||
240              stage == MESA_SHADER_GEOMETRY) {
241             brw_MOV(p, get_element_ud(header, 2), brw_imm_ud(dw2));
242          }
243 
244          brw_adjust_sampler_state_pointer(p, header, sampler_index);
245          brw_pop_insn_state(p);
246       }
247    }
248 
249    uint32_t return_format;
250 
251    switch (dst.type) {
252    case BRW_REGISTER_TYPE_D:
253       return_format = BRW_SAMPLER_RETURN_FORMAT_SINT32;
254       break;
255    case BRW_REGISTER_TYPE_UD:
256       return_format = BRW_SAMPLER_RETURN_FORMAT_UINT32;
257       break;
258    default:
259       return_format = BRW_SAMPLER_RETURN_FORMAT_FLOAT32;
260       break;
261    }
262 
263    /* Stomp the resinfo output type to UINT32.  On gens 4-5, the output type
264     * is set as part of the message descriptor.  On gen4, the PRM seems to
265     * allow UINT32 and FLOAT32 (i965 PRM, Vol. 4 Section 4.8.1.1), but on
266     * later gens UINT32 is required.  Once you hit Sandy Bridge, the bit is
267     * gone from the message descriptor entirely and you just get UINT32 all
268     * the time regasrdless.  Since we can really only do non-UINT32 on gen4,
269     * just stomp it to UINT32 all the time.
270     */
271    if (inst->opcode == SHADER_OPCODE_TXS)
272       return_format = BRW_SAMPLER_RETURN_FORMAT_UINT32;
273 
274    uint32_t base_binding_table_index = (inst->opcode == SHADER_OPCODE_TG4 ||
275          inst->opcode == SHADER_OPCODE_TG4_OFFSET)
276          ? prog_data->base.binding_table.gather_texture_start
277          : prog_data->base.binding_table.texture_start;
278 
279    if (surface_index.file == BRW_IMMEDIATE_VALUE &&
280        sampler_index.file == BRW_IMMEDIATE_VALUE) {
281       uint32_t surface = surface_index.ud;
282       uint32_t sampler = sampler_index.ud;
283 
284       brw_SAMPLE(p,
285                  dst,
286                  inst->base_mrf,
287                  src,
288                  surface + base_binding_table_index,
289                  sampler % 16,
290                  msg_type,
291                  1, /* response length */
292                  inst->mlen,
293                  inst->header_size != 0,
294                  BRW_SAMPLER_SIMD_MODE_SIMD4X2,
295                  return_format);
296    } else {
297       /* Non-constant sampler index. */
298 
299       struct brw_reg addr = vec1(retype(brw_address_reg(0), BRW_REGISTER_TYPE_UD));
300       struct brw_reg surface_reg = vec1(retype(surface_index, BRW_REGISTER_TYPE_UD));
301       struct brw_reg sampler_reg = vec1(retype(sampler_index, BRW_REGISTER_TYPE_UD));
302 
303       brw_push_insn_state(p);
304       brw_set_default_mask_control(p, BRW_MASK_DISABLE);
305       brw_set_default_access_mode(p, BRW_ALIGN_1);
306 
307       if (brw_regs_equal(&surface_reg, &sampler_reg)) {
308          brw_MUL(p, addr, sampler_reg, brw_imm_uw(0x101));
309       } else {
310          if (sampler_reg.file == BRW_IMMEDIATE_VALUE) {
311             brw_OR(p, addr, surface_reg, brw_imm_ud(sampler_reg.ud << 8));
312          } else {
313             brw_SHL(p, addr, sampler_reg, brw_imm_ud(8));
314             brw_OR(p, addr, addr, surface_reg);
315          }
316       }
317       if (base_binding_table_index)
318          brw_ADD(p, addr, addr, brw_imm_ud(base_binding_table_index));
319       brw_AND(p, addr, addr, brw_imm_ud(0xfff));
320 
321       brw_pop_insn_state(p);
322 
323       if (inst->base_mrf != -1)
324          gen6_resolve_implied_move(p, &src, inst->base_mrf);
325 
326       /* dst = send(offset, a0.0 | <descriptor>) */
327       brw_send_indirect_message(
328          p, BRW_SFID_SAMPLER, dst, src, addr,
329          brw_message_desc(devinfo, inst->mlen, 1, inst->header_size) |
330          brw_sampler_desc(devinfo,
331                           0 /* surface */,
332                           0 /* sampler */,
333                           msg_type,
334                           BRW_SAMPLER_SIMD_MODE_SIMD4X2,
335                           return_format),
336          false /* EOT */);
337 
338       /* visitor knows more than we do about the surface limit required,
339        * so has already done marking.
340        */
341    }
342 }
343 
344 static void
generate_vs_urb_write(struct brw_codegen * p,vec4_instruction * inst)345 generate_vs_urb_write(struct brw_codegen *p, vec4_instruction *inst)
346 {
347    brw_urb_WRITE(p,
348 		 brw_null_reg(), /* dest */
349 		 inst->base_mrf, /* starting mrf reg nr */
350 		 brw_vec8_grf(0, 0), /* src */
351                  inst->urb_write_flags,
352 		 inst->mlen,
353 		 0,		/* response len */
354 		 inst->offset,	/* urb destination offset */
355 		 BRW_URB_SWIZZLE_INTERLEAVE);
356 }
357 
358 static void
generate_gs_urb_write(struct brw_codegen * p,vec4_instruction * inst)359 generate_gs_urb_write(struct brw_codegen *p, vec4_instruction *inst)
360 {
361    struct brw_reg src = brw_message_reg(inst->base_mrf);
362    brw_urb_WRITE(p,
363                  brw_null_reg(), /* dest */
364                  inst->base_mrf, /* starting mrf reg nr */
365                  src,
366                  inst->urb_write_flags,
367                  inst->mlen,
368                  0,             /* response len */
369                  inst->offset,  /* urb destination offset */
370                  BRW_URB_SWIZZLE_INTERLEAVE);
371 }
372 
373 static void
generate_gs_urb_write_allocate(struct brw_codegen * p,vec4_instruction * inst)374 generate_gs_urb_write_allocate(struct brw_codegen *p, vec4_instruction *inst)
375 {
376    struct brw_reg src = brw_message_reg(inst->base_mrf);
377 
378    /* We pass the temporary passed in src0 as the writeback register */
379    brw_urb_WRITE(p,
380                  inst->src[0].as_brw_reg(), /* dest */
381                  inst->base_mrf, /* starting mrf reg nr */
382                  src,
383                  BRW_URB_WRITE_ALLOCATE_COMPLETE,
384                  inst->mlen,
385                  1, /* response len */
386                  inst->offset,  /* urb destination offset */
387                  BRW_URB_SWIZZLE_INTERLEAVE);
388 
389    /* Now put allocated urb handle in dst.0 */
390    brw_push_insn_state(p);
391    brw_set_default_access_mode(p, BRW_ALIGN_1);
392    brw_set_default_mask_control(p, BRW_MASK_DISABLE);
393    brw_MOV(p, get_element_ud(inst->dst.as_brw_reg(), 0),
394            get_element_ud(inst->src[0].as_brw_reg(), 0));
395    brw_pop_insn_state(p);
396 }
397 
398 static void
generate_gs_thread_end(struct brw_codegen * p,vec4_instruction * inst)399 generate_gs_thread_end(struct brw_codegen *p, vec4_instruction *inst)
400 {
401    struct brw_reg src = brw_message_reg(inst->base_mrf);
402    brw_urb_WRITE(p,
403                  brw_null_reg(), /* dest */
404                  inst->base_mrf, /* starting mrf reg nr */
405                  src,
406                  BRW_URB_WRITE_EOT | inst->urb_write_flags,
407                  inst->mlen,
408                  0,              /* response len */
409                  0,              /* urb destination offset */
410                  BRW_URB_SWIZZLE_INTERLEAVE);
411 }
412 
413 static void
generate_gs_set_write_offset(struct brw_codegen * p,struct brw_reg dst,struct brw_reg src0,struct brw_reg src1)414 generate_gs_set_write_offset(struct brw_codegen *p,
415                              struct brw_reg dst,
416                              struct brw_reg src0,
417                              struct brw_reg src1)
418 {
419    /* From p22 of volume 4 part 2 of the Ivy Bridge PRM (2.4.3.1 Message
420     * Header: M0.3):
421     *
422     *     Slot 0 Offset. This field, after adding to the Global Offset field
423     *     in the message descriptor, specifies the offset (in 256-bit units)
424     *     from the start of the URB entry, as referenced by URB Handle 0, at
425     *     which the data will be accessed.
426     *
427     * Similar text describes DWORD M0.4, which is slot 1 offset.
428     *
429     * Therefore, we want to multiply DWORDs 0 and 4 of src0 (the x components
430     * of the register for geometry shader invocations 0 and 1) by the
431     * immediate value in src1, and store the result in DWORDs 3 and 4 of dst.
432     *
433     * We can do this with the following EU instruction:
434     *
435     *     mul(2) dst.3<1>UD src0<8;2,4>UD src1<...>UW   { Align1 WE_all }
436     */
437    brw_push_insn_state(p);
438    brw_set_default_access_mode(p, BRW_ALIGN_1);
439    brw_set_default_mask_control(p, BRW_MASK_DISABLE);
440    assert(p->devinfo->gen >= 7 &&
441           src1.file == BRW_IMMEDIATE_VALUE &&
442           src1.type == BRW_REGISTER_TYPE_UD &&
443           src1.ud <= USHRT_MAX);
444    if (src0.file == BRW_IMMEDIATE_VALUE) {
445       brw_MOV(p, suboffset(stride(dst, 2, 2, 1), 3),
446               brw_imm_ud(src0.ud * src1.ud));
447    } else {
448       if (src1.file == BRW_IMMEDIATE_VALUE) {
449          src1 = brw_imm_uw(src1.ud);
450       }
451       brw_MUL(p, suboffset(stride(dst, 2, 2, 1), 3), stride(src0, 8, 2, 4),
452               retype(src1, BRW_REGISTER_TYPE_UW));
453    }
454    brw_pop_insn_state(p);
455 }
456 
457 static void
generate_gs_set_vertex_count(struct brw_codegen * p,struct brw_reg dst,struct brw_reg src)458 generate_gs_set_vertex_count(struct brw_codegen *p,
459                              struct brw_reg dst,
460                              struct brw_reg src)
461 {
462    brw_push_insn_state(p);
463    brw_set_default_mask_control(p, BRW_MASK_DISABLE);
464 
465    /* If we think of the src and dst registers as composed of 8 DWORDs each,
466     * we want to pick up the contents of DWORDs 0 and 4 from src, truncate
467     * them to WORDs, and then pack them into DWORD 2 of dst.
468     *
469     * It's easier to get the EU to do this if we think of the src and dst
470     * registers as composed of 16 WORDS each; then, we want to pick up the
471     * contents of WORDs 0 and 8 from src, and pack them into WORDs 4 and 5
472     * of dst.
473     *
474     * We can do that by the following EU instruction:
475     *
476     *     mov (2) dst.4<1>:uw src<8;1,0>:uw   { Align1, Q1, NoMask }
477     */
478    brw_set_default_access_mode(p, BRW_ALIGN_1);
479    brw_MOV(p,
480            suboffset(stride(retype(dst, BRW_REGISTER_TYPE_UW), 2, 2, 1), 4),
481            stride(retype(src, BRW_REGISTER_TYPE_UW), 8, 1, 0));
482 
483    brw_pop_insn_state(p);
484 }
485 
486 static void
generate_gs_svb_write(struct brw_codegen * p,vec4_instruction * inst,struct brw_reg dst,struct brw_reg src0,struct brw_reg src1)487 generate_gs_svb_write(struct brw_codegen *p,
488                       vec4_instruction *inst,
489                       struct brw_reg dst,
490                       struct brw_reg src0,
491                       struct brw_reg src1)
492 {
493    int binding = inst->sol_binding;
494    bool final_write = inst->sol_final_write;
495 
496    brw_push_insn_state(p);
497    brw_set_default_exec_size(p, BRW_EXECUTE_4);
498    /* Copy Vertex data into M0.x */
499    brw_MOV(p, stride(dst, 4, 4, 1),
500            stride(retype(src0, BRW_REGISTER_TYPE_UD), 4, 4, 1));
501    brw_pop_insn_state(p);
502 
503    brw_push_insn_state(p);
504    /* Send SVB Write */
505    brw_svb_write(p,
506                  final_write ? src1 : brw_null_reg(), /* dest == src1 */
507                  1, /* msg_reg_nr */
508                  dst, /* src0 == previous dst */
509                  BRW_GEN6_SOL_BINDING_START + binding, /* binding_table_index */
510                  final_write); /* send_commit_msg */
511 
512    /* Finally, wait for the write commit to occur so that we can proceed to
513     * other things safely.
514     *
515     * From the Sandybridge PRM, Volume 4, Part 1, Section 3.3:
516     *
517     *   The write commit does not modify the destination register, but
518     *   merely clears the dependency associated with the destination
519     *   register. Thus, a simple “mov” instruction using the register as a
520     *   source is sufficient to wait for the write commit to occur.
521     */
522    if (final_write) {
523       brw_MOV(p, src1, src1);
524    }
525    brw_pop_insn_state(p);
526 }
527 
528 static void
generate_gs_svb_set_destination_index(struct brw_codegen * p,vec4_instruction * inst,struct brw_reg dst,struct brw_reg src)529 generate_gs_svb_set_destination_index(struct brw_codegen *p,
530                                       vec4_instruction *inst,
531                                       struct brw_reg dst,
532                                       struct brw_reg src)
533 {
534    int vertex = inst->sol_vertex;
535    brw_push_insn_state(p);
536    brw_set_default_access_mode(p, BRW_ALIGN_1);
537    brw_set_default_mask_control(p, BRW_MASK_DISABLE);
538    brw_MOV(p, get_element_ud(dst, 5), get_element_ud(src, vertex));
539    brw_pop_insn_state(p);
540 }
541 
542 static void
generate_gs_set_dword_2(struct brw_codegen * p,struct brw_reg dst,struct brw_reg src)543 generate_gs_set_dword_2(struct brw_codegen *p,
544                         struct brw_reg dst,
545                         struct brw_reg src)
546 {
547    brw_push_insn_state(p);
548    brw_set_default_access_mode(p, BRW_ALIGN_1);
549    brw_set_default_mask_control(p, BRW_MASK_DISABLE);
550    brw_MOV(p, suboffset(vec1(dst), 2), suboffset(vec1(src), 0));
551    brw_pop_insn_state(p);
552 }
553 
554 static void
generate_gs_prepare_channel_masks(struct brw_codegen * p,struct brw_reg dst)555 generate_gs_prepare_channel_masks(struct brw_codegen *p,
556                                   struct brw_reg dst)
557 {
558    /* We want to left shift just DWORD 4 (the x component belonging to the
559     * second geometry shader invocation) by 4 bits.  So generate the
560     * instruction:
561     *
562     *     shl(1) dst.4<1>UD dst.4<0,1,0>UD 4UD { align1 WE_all }
563     */
564    dst = suboffset(vec1(dst), 4);
565    brw_push_insn_state(p);
566    brw_set_default_access_mode(p, BRW_ALIGN_1);
567    brw_set_default_mask_control(p, BRW_MASK_DISABLE);
568    brw_SHL(p, dst, dst, brw_imm_ud(4));
569    brw_pop_insn_state(p);
570 }
571 
572 static void
generate_gs_set_channel_masks(struct brw_codegen * p,struct brw_reg dst,struct brw_reg src)573 generate_gs_set_channel_masks(struct brw_codegen *p,
574                               struct brw_reg dst,
575                               struct brw_reg src)
576 {
577    /* From p21 of volume 4 part 2 of the Ivy Bridge PRM (2.4.3.1 Message
578     * Header: M0.5):
579     *
580     *     15 Vertex 1 DATA [3] / Vertex 0 DATA[7] Channel Mask
581     *
582     *        When Swizzle Control = URB_INTERLEAVED this bit controls Vertex 1
583     *        DATA[3], when Swizzle Control = URB_NOSWIZZLE this bit controls
584     *        Vertex 0 DATA[7].  This bit is ANDed with the corresponding
585     *        channel enable to determine the final channel enable.  For the
586     *        URB_READ_OWORD & URB_READ_HWORD messages, when final channel
587     *        enable is 1 it indicates that Vertex 1 DATA [3] will be included
588     *        in the writeback message.  For the URB_WRITE_OWORD &
589     *        URB_WRITE_HWORD messages, when final channel enable is 1 it
590     *        indicates that Vertex 1 DATA [3] will be written to the surface.
591     *
592     *        0: Vertex 1 DATA [3] / Vertex 0 DATA[7] channel not included
593     *        1: Vertex DATA [3] / Vertex 0 DATA[7] channel included
594     *
595     *     14 Vertex 1 DATA [2] Channel Mask
596     *     13 Vertex 1 DATA [1] Channel Mask
597     *     12 Vertex 1 DATA [0] Channel Mask
598     *     11 Vertex 0 DATA [3] Channel Mask
599     *     10 Vertex 0 DATA [2] Channel Mask
600     *      9 Vertex 0 DATA [1] Channel Mask
601     *      8 Vertex 0 DATA [0] Channel Mask
602     *
603     * (This is from a section of the PRM that is agnostic to the particular
604     * type of shader being executed, so "Vertex 0" and "Vertex 1" refer to
605     * geometry shader invocations 0 and 1, respectively).  Since we have the
606     * enable flags for geometry shader invocation 0 in bits 3:0 of DWORD 0,
607     * and the enable flags for geometry shader invocation 1 in bits 7:0 of
608     * DWORD 4, we just need to OR them together and store the result in bits
609     * 15:8 of DWORD 5.
610     *
611     * It's easier to get the EU to do this if we think of the src and dst
612     * registers as composed of 32 bytes each; then, we want to pick up the
613     * contents of bytes 0 and 16 from src, OR them together, and store them in
614     * byte 21.
615     *
616     * We can do that by the following EU instruction:
617     *
618     *     or(1) dst.21<1>UB src<0,1,0>UB src.16<0,1,0>UB { align1 WE_all }
619     *
620     * Note: this relies on the source register having zeros in (a) bits 7:4 of
621     * DWORD 0 and (b) bits 3:0 of DWORD 4.  We can rely on (b) because the
622     * source register was prepared by GS_OPCODE_PREPARE_CHANNEL_MASKS (which
623     * shifts DWORD 4 left by 4 bits), and we can rely on (a) because prior to
624     * the execution of GS_OPCODE_PREPARE_CHANNEL_MASKS, DWORDs 0 and 4 need to
625     * contain valid channel mask values (which are in the range 0x0-0xf).
626     */
627    dst = retype(dst, BRW_REGISTER_TYPE_UB);
628    src = retype(src, BRW_REGISTER_TYPE_UB);
629    brw_push_insn_state(p);
630    brw_set_default_access_mode(p, BRW_ALIGN_1);
631    brw_set_default_mask_control(p, BRW_MASK_DISABLE);
632    brw_OR(p, suboffset(vec1(dst), 21), vec1(src), suboffset(vec1(src), 16));
633    brw_pop_insn_state(p);
634 }
635 
636 static void
generate_gs_get_instance_id(struct brw_codegen * p,struct brw_reg dst)637 generate_gs_get_instance_id(struct brw_codegen *p,
638                             struct brw_reg dst)
639 {
640    /* We want to right shift R0.0 & R0.1 by GEN7_GS_PAYLOAD_INSTANCE_ID_SHIFT
641     * and store into dst.0 & dst.4. So generate the instruction:
642     *
643     *     shr(8) dst<1> R0<1,4,0> GEN7_GS_PAYLOAD_INSTANCE_ID_SHIFT { align1 WE_normal 1Q }
644     */
645    brw_push_insn_state(p);
646    brw_set_default_access_mode(p, BRW_ALIGN_1);
647    dst = retype(dst, BRW_REGISTER_TYPE_UD);
648    struct brw_reg r0(retype(brw_vec8_grf(0, 0), BRW_REGISTER_TYPE_UD));
649    brw_SHR(p, dst, stride(r0, 1, 4, 0),
650            brw_imm_ud(GEN7_GS_PAYLOAD_INSTANCE_ID_SHIFT));
651    brw_pop_insn_state(p);
652 }
653 
654 static void
generate_gs_ff_sync_set_primitives(struct brw_codegen * p,struct brw_reg dst,struct brw_reg src0,struct brw_reg src1,struct brw_reg src2)655 generate_gs_ff_sync_set_primitives(struct brw_codegen *p,
656                                    struct brw_reg dst,
657                                    struct brw_reg src0,
658                                    struct brw_reg src1,
659                                    struct brw_reg src2)
660 {
661    brw_push_insn_state(p);
662    brw_set_default_access_mode(p, BRW_ALIGN_1);
663    /* Save src0 data in 16:31 bits of dst.0 */
664    brw_AND(p, suboffset(vec1(dst), 0), suboffset(vec1(src0), 0),
665            brw_imm_ud(0xffffu));
666    brw_SHL(p, suboffset(vec1(dst), 0), suboffset(vec1(dst), 0), brw_imm_ud(16));
667    /* Save src1 data in 0:15 bits of dst.0 */
668    brw_AND(p, suboffset(vec1(src2), 0), suboffset(vec1(src1), 0),
669            brw_imm_ud(0xffffu));
670    brw_OR(p, suboffset(vec1(dst), 0),
671           suboffset(vec1(dst), 0),
672           suboffset(vec1(src2), 0));
673    brw_pop_insn_state(p);
674 }
675 
676 static void
generate_gs_ff_sync(struct brw_codegen * p,vec4_instruction * inst,struct brw_reg dst,struct brw_reg src0,struct brw_reg src1)677 generate_gs_ff_sync(struct brw_codegen *p,
678                     vec4_instruction *inst,
679                     struct brw_reg dst,
680                     struct brw_reg src0,
681                     struct brw_reg src1)
682 {
683    /* This opcode uses an implied MRF register for:
684     *  - the header of the ff_sync message. And as such it is expected to be
685     *    initialized to r0 before calling here.
686     *  - the destination where we will write the allocated URB handle.
687     */
688    struct brw_reg header =
689       retype(brw_message_reg(inst->base_mrf), BRW_REGISTER_TYPE_UD);
690 
691    /* Overwrite dword 0 of the header (SO vertices to write) and
692     * dword 1 (number of primitives written).
693     */
694    brw_push_insn_state(p);
695    brw_set_default_mask_control(p, BRW_MASK_DISABLE);
696    brw_set_default_access_mode(p, BRW_ALIGN_1);
697    brw_MOV(p, get_element_ud(header, 0), get_element_ud(src1, 0));
698    brw_MOV(p, get_element_ud(header, 1), get_element_ud(src0, 0));
699    brw_pop_insn_state(p);
700 
701    /* Allocate URB handle in dst */
702    brw_ff_sync(p,
703                dst,
704                0,
705                header,
706                1, /* allocate */
707                1, /* response length */
708                0 /* eot */);
709 
710    /* Now put allocated urb handle in header.0 */
711    brw_push_insn_state(p);
712    brw_set_default_access_mode(p, BRW_ALIGN_1);
713    brw_set_default_mask_control(p, BRW_MASK_DISABLE);
714    brw_MOV(p, get_element_ud(header, 0), get_element_ud(dst, 0));
715 
716    /* src1 is not an immediate when we use transform feedback */
717    if (src1.file != BRW_IMMEDIATE_VALUE) {
718       brw_set_default_exec_size(p, BRW_EXECUTE_4);
719       brw_MOV(p, brw_vec4_grf(src1.nr, 0), brw_vec4_grf(dst.nr, 1));
720    }
721 
722    brw_pop_insn_state(p);
723 }
724 
725 static void
generate_gs_set_primitive_id(struct brw_codegen * p,struct brw_reg dst)726 generate_gs_set_primitive_id(struct brw_codegen *p, struct brw_reg dst)
727 {
728    /* In gen6, PrimitiveID is delivered in R0.1 of the payload */
729    struct brw_reg src = brw_vec8_grf(0, 0);
730    brw_push_insn_state(p);
731    brw_set_default_mask_control(p, BRW_MASK_DISABLE);
732    brw_set_default_access_mode(p, BRW_ALIGN_1);
733    brw_MOV(p, get_element_ud(dst, 0), get_element_ud(src, 1));
734    brw_pop_insn_state(p);
735 }
736 
737 static void
generate_tcs_get_instance_id(struct brw_codegen * p,struct brw_reg dst)738 generate_tcs_get_instance_id(struct brw_codegen *p, struct brw_reg dst)
739 {
740    const struct gen_device_info *devinfo = p->devinfo;
741    const bool ivb = devinfo->is_ivybridge || devinfo->is_baytrail;
742 
743    /* "Instance Count" comes as part of the payload in r0.2 bits 23:17.
744     *
745     * Since we operate in SIMD4x2 mode, we need run half as many threads
746     * as necessary.  So we assign (2i + 1, 2i) as the thread counts.  We
747     * shift right by one less to accomplish the multiplication by two.
748     */
749    dst = retype(dst, BRW_REGISTER_TYPE_UD);
750    struct brw_reg r0(retype(brw_vec8_grf(0, 0), BRW_REGISTER_TYPE_UD));
751 
752    brw_push_insn_state(p);
753    brw_set_default_access_mode(p, BRW_ALIGN_1);
754 
755    const int mask = ivb ? INTEL_MASK(22, 16) : INTEL_MASK(23, 17);
756    const int shift = ivb ? 16 : 17;
757 
758    brw_AND(p, get_element_ud(dst, 0), get_element_ud(r0, 2), brw_imm_ud(mask));
759    brw_SHR(p, get_element_ud(dst, 0), get_element_ud(dst, 0),
760            brw_imm_ud(shift - 1));
761    brw_ADD(p, get_element_ud(dst, 4), get_element_ud(dst, 0), brw_imm_ud(1));
762 
763    brw_pop_insn_state(p);
764 }
765 
766 static void
generate_tcs_urb_write(struct brw_codegen * p,vec4_instruction * inst,struct brw_reg urb_header)767 generate_tcs_urb_write(struct brw_codegen *p,
768                        vec4_instruction *inst,
769                        struct brw_reg urb_header)
770 {
771    const struct gen_device_info *devinfo = p->devinfo;
772 
773    brw_inst *send = brw_next_insn(p, BRW_OPCODE_SEND);
774    brw_set_dest(p, send, brw_null_reg());
775    brw_set_src0(p, send, urb_header);
776    brw_set_desc(p, send, brw_message_desc(devinfo, inst->mlen, 0, true));
777 
778    brw_inst_set_sfid(devinfo, send, BRW_SFID_URB);
779    brw_inst_set_urb_opcode(devinfo, send, BRW_URB_OPCODE_WRITE_OWORD);
780    brw_inst_set_urb_global_offset(devinfo, send, inst->offset);
781    if (inst->urb_write_flags & BRW_URB_WRITE_EOT) {
782       brw_inst_set_eot(devinfo, send, 1);
783    } else {
784       brw_inst_set_urb_per_slot_offset(devinfo, send, 1);
785       brw_inst_set_urb_swizzle_control(devinfo, send, BRW_URB_SWIZZLE_INTERLEAVE);
786    }
787 
788    /* what happens to swizzles? */
789 }
790 
791 
792 static void
generate_tcs_input_urb_offsets(struct brw_codegen * p,struct brw_reg dst,struct brw_reg vertex,struct brw_reg offset)793 generate_tcs_input_urb_offsets(struct brw_codegen *p,
794                                struct brw_reg dst,
795                                struct brw_reg vertex,
796                                struct brw_reg offset)
797 {
798    /* Generates an URB read/write message header for HS/DS operation.
799     * Inputs are a vertex index, and a byte offset from the beginning of
800     * the vertex. */
801 
802    /* If `vertex` is not an immediate, we clobber a0.0 */
803 
804    assert(vertex.file == BRW_IMMEDIATE_VALUE || vertex.file == BRW_GENERAL_REGISTER_FILE);
805    assert(vertex.type == BRW_REGISTER_TYPE_UD || vertex.type == BRW_REGISTER_TYPE_D);
806 
807    assert(dst.file == BRW_GENERAL_REGISTER_FILE);
808 
809    brw_push_insn_state(p);
810    brw_set_default_access_mode(p, BRW_ALIGN_1);
811    brw_set_default_mask_control(p, BRW_MASK_DISABLE);
812    brw_MOV(p, dst, brw_imm_ud(0));
813 
814    /* m0.5 bits 8-15 are channel enables */
815    brw_MOV(p, get_element_ud(dst, 5), brw_imm_ud(0xff00));
816 
817    /* m0.0-0.1: URB handles */
818    if (vertex.file == BRW_IMMEDIATE_VALUE) {
819       uint32_t vertex_index = vertex.ud;
820       struct brw_reg index_reg = brw_vec1_grf(
821             1 + (vertex_index >> 3), vertex_index & 7);
822 
823       brw_MOV(p, vec2(get_element_ud(dst, 0)),
824               retype(index_reg, BRW_REGISTER_TYPE_UD));
825    } else {
826       /* Use indirect addressing.  ICP Handles are DWords (single channels
827        * of a register) and start at g1.0.
828        *
829        * In order to start our region at g1.0, we add 8 to the vertex index,
830        * effectively skipping over the 8 channels in g0.0.  This gives us a
831        * DWord offset to the ICP Handle.
832        *
833        * Indirect addressing works in terms of bytes, so we then multiply
834        * the DWord offset by 4 (by shifting left by 2).
835        */
836       struct brw_reg addr = brw_address_reg(0);
837 
838       /* bottom half: m0.0 = g[1.0 + vertex.0]UD */
839       brw_ADD(p, addr, retype(get_element_ud(vertex, 0), BRW_REGISTER_TYPE_UW),
840               brw_imm_uw(0x8));
841       brw_SHL(p, addr, addr, brw_imm_uw(2));
842       brw_MOV(p, get_element_ud(dst, 0), deref_1ud(brw_indirect(0, 0), 0));
843 
844       /* top half: m0.1 = g[1.0 + vertex.4]UD */
845       brw_ADD(p, addr, retype(get_element_ud(vertex, 4), BRW_REGISTER_TYPE_UW),
846               brw_imm_uw(0x8));
847       brw_SHL(p, addr, addr, brw_imm_uw(2));
848       brw_MOV(p, get_element_ud(dst, 1), deref_1ud(brw_indirect(0, 0), 0));
849    }
850 
851    /* m0.3-0.4: 128bit-granular offsets into the URB from the handles */
852    if (offset.file != ARF)
853       brw_MOV(p, vec2(get_element_ud(dst, 3)), stride(offset, 4, 1, 0));
854 
855    brw_pop_insn_state(p);
856 }
857 
858 
859 static void
generate_tcs_output_urb_offsets(struct brw_codegen * p,struct brw_reg dst,struct brw_reg write_mask,struct brw_reg offset)860 generate_tcs_output_urb_offsets(struct brw_codegen *p,
861                                 struct brw_reg dst,
862                                 struct brw_reg write_mask,
863                                 struct brw_reg offset)
864 {
865    /* Generates an URB read/write message header for HS/DS operation, for the patch URB entry. */
866    assert(dst.file == BRW_GENERAL_REGISTER_FILE || dst.file == BRW_MESSAGE_REGISTER_FILE);
867 
868    assert(write_mask.file == BRW_IMMEDIATE_VALUE);
869    assert(write_mask.type == BRW_REGISTER_TYPE_UD);
870 
871    brw_push_insn_state(p);
872 
873    brw_set_default_access_mode(p, BRW_ALIGN_1);
874    brw_set_default_mask_control(p, BRW_MASK_DISABLE);
875    brw_MOV(p, dst, brw_imm_ud(0));
876 
877    unsigned mask = write_mask.ud;
878 
879    /* m0.5 bits 15:12 and 11:8 are channel enables */
880    brw_MOV(p, get_element_ud(dst, 5), brw_imm_ud((mask << 8) | (mask << 12)));
881 
882    /* HS patch URB handle is delivered in r0.0 */
883    struct brw_reg urb_handle = brw_vec1_grf(0, 0);
884 
885    /* m0.0-0.1: URB handles */
886    brw_MOV(p, vec2(get_element_ud(dst, 0)),
887            retype(urb_handle, BRW_REGISTER_TYPE_UD));
888 
889    /* m0.3-0.4: 128bit-granular offsets into the URB from the handles */
890    if (offset.file != ARF)
891       brw_MOV(p, vec2(get_element_ud(dst, 3)), stride(offset, 4, 1, 0));
892 
893    brw_pop_insn_state(p);
894 }
895 
896 static void
generate_tes_create_input_read_header(struct brw_codegen * p,struct brw_reg dst)897 generate_tes_create_input_read_header(struct brw_codegen *p,
898                                       struct brw_reg dst)
899 {
900    brw_push_insn_state(p);
901    brw_set_default_access_mode(p, BRW_ALIGN_1);
902    brw_set_default_mask_control(p, BRW_MASK_DISABLE);
903 
904    /* Initialize the register to 0 */
905    brw_MOV(p, dst, brw_imm_ud(0));
906 
907    /* Enable all the channels in m0.5 bits 15:8 */
908    brw_MOV(p, get_element_ud(dst, 5), brw_imm_ud(0xff00));
909 
910    /* Copy g1.3 (the patch URB handle) to m0.0 and m0.1.  For safety,
911     * mask out irrelevant "Reserved" bits, as they're not marked MBZ.
912     */
913    brw_AND(p, vec2(get_element_ud(dst, 0)),
914            retype(brw_vec1_grf(1, 3), BRW_REGISTER_TYPE_UD),
915            brw_imm_ud(0x1fff));
916    brw_pop_insn_state(p);
917 }
918 
919 static void
generate_tes_add_indirect_urb_offset(struct brw_codegen * p,struct brw_reg dst,struct brw_reg header,struct brw_reg offset)920 generate_tes_add_indirect_urb_offset(struct brw_codegen *p,
921                                      struct brw_reg dst,
922                                      struct brw_reg header,
923                                      struct brw_reg offset)
924 {
925    brw_push_insn_state(p);
926    brw_set_default_access_mode(p, BRW_ALIGN_1);
927    brw_set_default_mask_control(p, BRW_MASK_DISABLE);
928 
929    brw_MOV(p, dst, header);
930 
931    /* Uniforms will have a stride <0;4,1>, and we need to convert to <0;1,0>.
932     * Other values get <4;1,0>.
933     */
934    struct brw_reg restrided_offset;
935    if (offset.vstride == BRW_VERTICAL_STRIDE_0 &&
936        offset.width == BRW_WIDTH_4 &&
937        offset.hstride == BRW_HORIZONTAL_STRIDE_1) {
938       restrided_offset = stride(offset, 0, 1, 0);
939    } else {
940       restrided_offset = stride(offset, 4, 1, 0);
941    }
942 
943    /* m0.3-0.4: 128-bit-granular offsets into the URB from the handles */
944    brw_MOV(p, vec2(get_element_ud(dst, 3)), restrided_offset);
945 
946    brw_pop_insn_state(p);
947 }
948 
949 static void
generate_vec4_urb_read(struct brw_codegen * p,vec4_instruction * inst,struct brw_reg dst,struct brw_reg header)950 generate_vec4_urb_read(struct brw_codegen *p,
951                        vec4_instruction *inst,
952                        struct brw_reg dst,
953                        struct brw_reg header)
954 {
955    const struct gen_device_info *devinfo = p->devinfo;
956 
957    assert(header.file == BRW_GENERAL_REGISTER_FILE);
958    assert(header.type == BRW_REGISTER_TYPE_UD);
959 
960    brw_inst *send = brw_next_insn(p, BRW_OPCODE_SEND);
961    brw_set_dest(p, send, dst);
962    brw_set_src0(p, send, header);
963 
964    brw_set_desc(p, send, brw_message_desc(devinfo, 1, 1, true));
965 
966    brw_inst_set_sfid(devinfo, send, BRW_SFID_URB);
967    brw_inst_set_urb_opcode(devinfo, send, BRW_URB_OPCODE_READ_OWORD);
968    brw_inst_set_urb_swizzle_control(devinfo, send, BRW_URB_SWIZZLE_INTERLEAVE);
969    brw_inst_set_urb_per_slot_offset(devinfo, send, 1);
970 
971    brw_inst_set_urb_global_offset(devinfo, send, inst->offset);
972 }
973 
974 static void
generate_tcs_release_input(struct brw_codegen * p,struct brw_reg header,struct brw_reg vertex,struct brw_reg is_unpaired)975 generate_tcs_release_input(struct brw_codegen *p,
976                            struct brw_reg header,
977                            struct brw_reg vertex,
978                            struct brw_reg is_unpaired)
979 {
980    const struct gen_device_info *devinfo = p->devinfo;
981 
982    assert(vertex.file == BRW_IMMEDIATE_VALUE);
983    assert(vertex.type == BRW_REGISTER_TYPE_UD);
984 
985    /* m0.0-0.1: URB handles */
986    struct brw_reg urb_handles =
987       retype(brw_vec2_grf(1 + (vertex.ud >> 3), vertex.ud & 7),
988              BRW_REGISTER_TYPE_UD);
989 
990    brw_push_insn_state(p);
991    brw_set_default_access_mode(p, BRW_ALIGN_1);
992    brw_set_default_mask_control(p, BRW_MASK_DISABLE);
993    brw_MOV(p, header, brw_imm_ud(0));
994    brw_MOV(p, vec2(get_element_ud(header, 0)), urb_handles);
995    brw_pop_insn_state(p);
996 
997    brw_inst *send = brw_next_insn(p, BRW_OPCODE_SEND);
998    brw_set_dest(p, send, brw_null_reg());
999    brw_set_src0(p, send, header);
1000    brw_set_desc(p, send, brw_message_desc(devinfo, 1, 0, true));
1001 
1002    brw_inst_set_sfid(devinfo, send, BRW_SFID_URB);
1003    brw_inst_set_urb_opcode(devinfo, send, BRW_URB_OPCODE_READ_OWORD);
1004    brw_inst_set_urb_complete(devinfo, send, 1);
1005    brw_inst_set_urb_swizzle_control(devinfo, send, is_unpaired.ud ?
1006                                     BRW_URB_SWIZZLE_NONE :
1007                                     BRW_URB_SWIZZLE_INTERLEAVE);
1008 }
1009 
1010 static void
generate_tcs_thread_end(struct brw_codegen * p,vec4_instruction * inst)1011 generate_tcs_thread_end(struct brw_codegen *p, vec4_instruction *inst)
1012 {
1013    struct brw_reg header = brw_message_reg(inst->base_mrf);
1014 
1015    brw_push_insn_state(p);
1016    brw_set_default_access_mode(p, BRW_ALIGN_1);
1017    brw_set_default_mask_control(p, BRW_MASK_DISABLE);
1018    brw_MOV(p, header, brw_imm_ud(0));
1019    brw_MOV(p, get_element_ud(header, 5), brw_imm_ud(WRITEMASK_X << 8));
1020    brw_MOV(p, get_element_ud(header, 0),
1021            retype(brw_vec1_grf(0, 0), BRW_REGISTER_TYPE_UD));
1022    brw_MOV(p, brw_message_reg(inst->base_mrf + 1), brw_imm_ud(0u));
1023    brw_pop_insn_state(p);
1024 
1025    brw_urb_WRITE(p,
1026                  brw_null_reg(), /* dest */
1027                  inst->base_mrf, /* starting mrf reg nr */
1028                  header,
1029                  BRW_URB_WRITE_EOT | BRW_URB_WRITE_OWORD |
1030                  BRW_URB_WRITE_USE_CHANNEL_MASKS,
1031                  inst->mlen,
1032                  0,              /* response len */
1033                  0,              /* urb destination offset */
1034                  0);
1035 }
1036 
1037 static void
generate_tes_get_primitive_id(struct brw_codegen * p,struct brw_reg dst)1038 generate_tes_get_primitive_id(struct brw_codegen *p, struct brw_reg dst)
1039 {
1040    brw_push_insn_state(p);
1041    brw_set_default_access_mode(p, BRW_ALIGN_1);
1042    brw_MOV(p, dst, retype(brw_vec1_grf(1, 7), BRW_REGISTER_TYPE_D));
1043    brw_pop_insn_state(p);
1044 }
1045 
1046 static void
generate_tcs_get_primitive_id(struct brw_codegen * p,struct brw_reg dst)1047 generate_tcs_get_primitive_id(struct brw_codegen *p, struct brw_reg dst)
1048 {
1049    brw_push_insn_state(p);
1050    brw_set_default_access_mode(p, BRW_ALIGN_1);
1051    brw_MOV(p, dst, retype(brw_vec1_grf(0, 1), BRW_REGISTER_TYPE_UD));
1052    brw_pop_insn_state(p);
1053 }
1054 
1055 static void
generate_tcs_create_barrier_header(struct brw_codegen * p,struct brw_vue_prog_data * prog_data,struct brw_reg dst)1056 generate_tcs_create_barrier_header(struct brw_codegen *p,
1057                                    struct brw_vue_prog_data *prog_data,
1058                                    struct brw_reg dst)
1059 {
1060    const struct gen_device_info *devinfo = p->devinfo;
1061    const bool ivb = devinfo->is_ivybridge || devinfo->is_baytrail;
1062    struct brw_reg m0_2 = get_element_ud(dst, 2);
1063    unsigned instances = ((struct brw_tcs_prog_data *) prog_data)->instances;
1064 
1065    brw_push_insn_state(p);
1066    brw_set_default_access_mode(p, BRW_ALIGN_1);
1067    brw_set_default_mask_control(p, BRW_MASK_DISABLE);
1068 
1069    /* Zero the message header */
1070    brw_MOV(p, retype(dst, BRW_REGISTER_TYPE_UD), brw_imm_ud(0u));
1071 
1072    /* Copy "Barrier ID" from r0.2, bits 16:13 (Gen7.5+) or 15:12 (Gen7) */
1073    brw_AND(p, m0_2,
1074            retype(brw_vec1_grf(0, 2), BRW_REGISTER_TYPE_UD),
1075            brw_imm_ud(ivb ? INTEL_MASK(15, 12) : INTEL_MASK(16, 13)));
1076 
1077    /* Shift it up to bits 27:24. */
1078    brw_SHL(p, m0_2, get_element_ud(dst, 2), brw_imm_ud(ivb ? 12 : 11));
1079 
1080    /* Set the Barrier Count and the enable bit */
1081    brw_OR(p, m0_2, m0_2, brw_imm_ud(instances << 9 | (1 << 15)));
1082 
1083    brw_pop_insn_state(p);
1084 }
1085 
1086 static void
generate_oword_dual_block_offsets(struct brw_codegen * p,struct brw_reg m1,struct brw_reg index)1087 generate_oword_dual_block_offsets(struct brw_codegen *p,
1088                                   struct brw_reg m1,
1089                                   struct brw_reg index)
1090 {
1091    int second_vertex_offset;
1092 
1093    if (p->devinfo->gen >= 6)
1094       second_vertex_offset = 1;
1095    else
1096       second_vertex_offset = 16;
1097 
1098    m1 = retype(m1, BRW_REGISTER_TYPE_D);
1099 
1100    /* Set up M1 (message payload).  Only the block offsets in M1.0 and
1101     * M1.4 are used, and the rest are ignored.
1102     */
1103    struct brw_reg m1_0 = suboffset(vec1(m1), 0);
1104    struct brw_reg m1_4 = suboffset(vec1(m1), 4);
1105    struct brw_reg index_0 = suboffset(vec1(index), 0);
1106    struct brw_reg index_4 = suboffset(vec1(index), 4);
1107 
1108    brw_push_insn_state(p);
1109    brw_set_default_mask_control(p, BRW_MASK_DISABLE);
1110    brw_set_default_access_mode(p, BRW_ALIGN_1);
1111 
1112    brw_MOV(p, m1_0, index_0);
1113 
1114    if (index.file == BRW_IMMEDIATE_VALUE) {
1115       index_4.ud += second_vertex_offset;
1116       brw_MOV(p, m1_4, index_4);
1117    } else {
1118       brw_ADD(p, m1_4, index_4, brw_imm_d(second_vertex_offset));
1119    }
1120 
1121    brw_pop_insn_state(p);
1122 }
1123 
1124 static void
generate_unpack_flags(struct brw_codegen * p,struct brw_reg dst)1125 generate_unpack_flags(struct brw_codegen *p,
1126                       struct brw_reg dst)
1127 {
1128    brw_push_insn_state(p);
1129    brw_set_default_mask_control(p, BRW_MASK_DISABLE);
1130    brw_set_default_access_mode(p, BRW_ALIGN_1);
1131 
1132    struct brw_reg flags = brw_flag_reg(0, 0);
1133    struct brw_reg dst_0 = suboffset(vec1(dst), 0);
1134    struct brw_reg dst_4 = suboffset(vec1(dst), 4);
1135 
1136    brw_AND(p, dst_0, flags, brw_imm_ud(0x0f));
1137    brw_AND(p, dst_4, flags, brw_imm_ud(0xf0));
1138    brw_SHR(p, dst_4, dst_4, brw_imm_ud(4));
1139 
1140    brw_pop_insn_state(p);
1141 }
1142 
1143 static void
generate_scratch_read(struct brw_codegen * p,vec4_instruction * inst,struct brw_reg dst,struct brw_reg index)1144 generate_scratch_read(struct brw_codegen *p,
1145                       vec4_instruction *inst,
1146                       struct brw_reg dst,
1147                       struct brw_reg index)
1148 {
1149    const struct gen_device_info *devinfo = p->devinfo;
1150    struct brw_reg header = brw_vec8_grf(0, 0);
1151 
1152    gen6_resolve_implied_move(p, &header, inst->base_mrf);
1153 
1154    generate_oword_dual_block_offsets(p, brw_message_reg(inst->base_mrf + 1),
1155 				     index);
1156 
1157    uint32_t msg_type;
1158 
1159    if (devinfo->gen >= 6)
1160       msg_type = GEN6_DATAPORT_READ_MESSAGE_OWORD_DUAL_BLOCK_READ;
1161    else if (devinfo->gen == 5 || devinfo->is_g4x)
1162       msg_type = G45_DATAPORT_READ_MESSAGE_OWORD_DUAL_BLOCK_READ;
1163    else
1164       msg_type = BRW_DATAPORT_READ_MESSAGE_OWORD_DUAL_BLOCK_READ;
1165 
1166    const unsigned target_cache =
1167       devinfo->gen >= 7 ? GEN7_SFID_DATAPORT_DATA_CACHE :
1168       devinfo->gen >= 6 ? GEN6_SFID_DATAPORT_RENDER_CACHE :
1169       BRW_SFID_DATAPORT_READ;
1170 
1171    /* Each of the 8 channel enables is considered for whether each
1172     * dword is written.
1173     */
1174    brw_inst *send = brw_next_insn(p, BRW_OPCODE_SEND);
1175    brw_inst_set_sfid(devinfo, send, target_cache);
1176    brw_set_dest(p, send, dst);
1177    brw_set_src0(p, send, header);
1178    if (devinfo->gen < 6)
1179       brw_inst_set_cond_modifier(devinfo, send, inst->base_mrf);
1180    brw_set_desc(p, send,
1181                 brw_message_desc(devinfo, 2, 1, true) |
1182                 brw_dp_read_desc(devinfo,
1183                                  brw_scratch_surface_idx(p),
1184                                  BRW_DATAPORT_OWORD_DUAL_BLOCK_1OWORD,
1185                                  msg_type, BRW_DATAPORT_READ_TARGET_RENDER_CACHE));
1186 }
1187 
1188 static void
generate_scratch_write(struct brw_codegen * p,vec4_instruction * inst,struct brw_reg dst,struct brw_reg src,struct brw_reg index)1189 generate_scratch_write(struct brw_codegen *p,
1190                        vec4_instruction *inst,
1191                        struct brw_reg dst,
1192                        struct brw_reg src,
1193                        struct brw_reg index)
1194 {
1195    const struct gen_device_info *devinfo = p->devinfo;
1196    const unsigned target_cache =
1197       (devinfo->gen >= 7 ? GEN7_SFID_DATAPORT_DATA_CACHE :
1198        devinfo->gen >= 6 ? GEN6_SFID_DATAPORT_RENDER_CACHE :
1199        BRW_SFID_DATAPORT_WRITE);
1200    struct brw_reg header = brw_vec8_grf(0, 0);
1201    bool write_commit;
1202 
1203    /* If the instruction is predicated, we'll predicate the send, not
1204     * the header setup.
1205     */
1206    brw_set_default_predicate_control(p, BRW_PREDICATE_NONE);
1207 
1208    gen6_resolve_implied_move(p, &header, inst->base_mrf);
1209 
1210    generate_oword_dual_block_offsets(p, brw_message_reg(inst->base_mrf + 1),
1211 				     index);
1212 
1213    brw_MOV(p,
1214 	   retype(brw_message_reg(inst->base_mrf + 2), BRW_REGISTER_TYPE_D),
1215 	   retype(src, BRW_REGISTER_TYPE_D));
1216 
1217    uint32_t msg_type;
1218 
1219    if (devinfo->gen >= 7)
1220       msg_type = GEN7_DATAPORT_DC_OWORD_DUAL_BLOCK_WRITE;
1221    else if (devinfo->gen == 6)
1222       msg_type = GEN6_DATAPORT_WRITE_MESSAGE_OWORD_DUAL_BLOCK_WRITE;
1223    else
1224       msg_type = BRW_DATAPORT_WRITE_MESSAGE_OWORD_DUAL_BLOCK_WRITE;
1225 
1226    brw_set_default_predicate_control(p, inst->predicate);
1227 
1228    /* Pre-gen6, we have to specify write commits to ensure ordering
1229     * between reads and writes within a thread.  Afterwards, that's
1230     * guaranteed and write commits only matter for inter-thread
1231     * synchronization.
1232     */
1233    if (devinfo->gen >= 6) {
1234       write_commit = false;
1235    } else {
1236       /* The visitor set up our destination register to be g0.  This
1237        * means that when the next read comes along, we will end up
1238        * reading from g0 and causing a block on the write commit.  For
1239        * write-after-read, we are relying on the value of the previous
1240        * read being used (and thus blocking on completion) before our
1241        * write is executed.  This means we have to be careful in
1242        * instruction scheduling to not violate this assumption.
1243        */
1244       write_commit = true;
1245    }
1246 
1247    /* Each of the 8 channel enables is considered for whether each
1248     * dword is written.
1249     */
1250    brw_inst *send = brw_next_insn(p, BRW_OPCODE_SEND);
1251    brw_inst_set_sfid(p->devinfo, send, target_cache);
1252    brw_set_dest(p, send, dst);
1253    brw_set_src0(p, send, header);
1254    if (devinfo->gen < 6)
1255       brw_inst_set_cond_modifier(p->devinfo, send, inst->base_mrf);
1256    brw_set_desc(p, send,
1257                 brw_message_desc(devinfo, 3, write_commit, true) |
1258                 brw_dp_write_desc(devinfo,
1259                                   brw_scratch_surface_idx(p),
1260                                   BRW_DATAPORT_OWORD_DUAL_BLOCK_1OWORD,
1261                                   msg_type,
1262                                   false, /* not a render target write */
1263                                   write_commit));
1264 }
1265 
1266 static void
generate_pull_constant_load(struct brw_codegen * p,vec4_instruction * inst,struct brw_reg dst,struct brw_reg index,struct brw_reg offset)1267 generate_pull_constant_load(struct brw_codegen *p,
1268                             vec4_instruction *inst,
1269                             struct brw_reg dst,
1270                             struct brw_reg index,
1271                             struct brw_reg offset)
1272 {
1273    const struct gen_device_info *devinfo = p->devinfo;
1274    const unsigned target_cache =
1275       (devinfo->gen >= 6 ? GEN6_SFID_DATAPORT_SAMPLER_CACHE :
1276        BRW_SFID_DATAPORT_READ);
1277    assert(index.file == BRW_IMMEDIATE_VALUE &&
1278 	  index.type == BRW_REGISTER_TYPE_UD);
1279    uint32_t surf_index = index.ud;
1280 
1281    struct brw_reg header = brw_vec8_grf(0, 0);
1282 
1283    gen6_resolve_implied_move(p, &header, inst->base_mrf);
1284 
1285    if (devinfo->gen >= 6) {
1286       if (offset.file == BRW_IMMEDIATE_VALUE) {
1287          brw_MOV(p, retype(brw_message_reg(inst->base_mrf + 1),
1288                            BRW_REGISTER_TYPE_D),
1289                  brw_imm_d(offset.ud >> 4));
1290       } else {
1291          brw_SHR(p, retype(brw_message_reg(inst->base_mrf + 1),
1292                            BRW_REGISTER_TYPE_D),
1293                  offset, brw_imm_d(4));
1294       }
1295    } else {
1296       brw_MOV(p, retype(brw_message_reg(inst->base_mrf + 1),
1297                         BRW_REGISTER_TYPE_D),
1298               offset);
1299    }
1300 
1301    uint32_t msg_type;
1302 
1303    if (devinfo->gen >= 6)
1304       msg_type = GEN6_DATAPORT_READ_MESSAGE_OWORD_DUAL_BLOCK_READ;
1305    else if (devinfo->gen == 5 || devinfo->is_g4x)
1306       msg_type = G45_DATAPORT_READ_MESSAGE_OWORD_DUAL_BLOCK_READ;
1307    else
1308       msg_type = BRW_DATAPORT_READ_MESSAGE_OWORD_DUAL_BLOCK_READ;
1309 
1310    /* Each of the 8 channel enables is considered for whether each
1311     * dword is written.
1312     */
1313    brw_inst *send = brw_next_insn(p, BRW_OPCODE_SEND);
1314    brw_inst_set_sfid(devinfo, send, target_cache);
1315    brw_set_dest(p, send, dst);
1316    brw_set_src0(p, send, header);
1317    if (devinfo->gen < 6)
1318       brw_inst_set_cond_modifier(p->devinfo, send, inst->base_mrf);
1319    brw_set_desc(p, send,
1320                 brw_message_desc(devinfo, 2, 1, true) |
1321                 brw_dp_read_desc(devinfo, surf_index,
1322                                  BRW_DATAPORT_OWORD_DUAL_BLOCK_1OWORD,
1323                                  msg_type,
1324                                  BRW_DATAPORT_READ_TARGET_DATA_CACHE));
1325 }
1326 
1327 static void
generate_get_buffer_size(struct brw_codegen * p,vec4_instruction * inst,struct brw_reg dst,struct brw_reg src,struct brw_reg surf_index)1328 generate_get_buffer_size(struct brw_codegen *p,
1329                          vec4_instruction *inst,
1330                          struct brw_reg dst,
1331                          struct brw_reg src,
1332                          struct brw_reg surf_index)
1333 {
1334    assert(p->devinfo->gen >= 7);
1335    assert(surf_index.type == BRW_REGISTER_TYPE_UD &&
1336           surf_index.file == BRW_IMMEDIATE_VALUE);
1337 
1338    brw_SAMPLE(p,
1339               dst,
1340               inst->base_mrf,
1341               src,
1342               surf_index.ud,
1343               0,
1344               GEN5_SAMPLER_MESSAGE_SAMPLE_RESINFO,
1345               1, /* response length */
1346               inst->mlen,
1347               inst->header_size > 0,
1348               BRW_SAMPLER_SIMD_MODE_SIMD4X2,
1349               BRW_SAMPLER_RETURN_FORMAT_SINT32);
1350 }
1351 
1352 static void
generate_pull_constant_load_gen7(struct brw_codegen * p,vec4_instruction * inst,struct brw_reg dst,struct brw_reg surf_index,struct brw_reg offset)1353 generate_pull_constant_load_gen7(struct brw_codegen *p,
1354                                  vec4_instruction *inst,
1355                                  struct brw_reg dst,
1356                                  struct brw_reg surf_index,
1357                                  struct brw_reg offset)
1358 {
1359    const struct gen_device_info *devinfo = p->devinfo;
1360    assert(surf_index.type == BRW_REGISTER_TYPE_UD);
1361 
1362    if (surf_index.file == BRW_IMMEDIATE_VALUE) {
1363 
1364       brw_inst *insn = brw_next_insn(p, BRW_OPCODE_SEND);
1365       brw_inst_set_sfid(devinfo, insn, BRW_SFID_SAMPLER);
1366       brw_set_dest(p, insn, dst);
1367       brw_set_src0(p, insn, offset);
1368       brw_set_desc(p, insn,
1369                    brw_message_desc(devinfo, inst->mlen, 1, inst->header_size) |
1370                    brw_sampler_desc(devinfo, surf_index.ud,
1371                                     0, /* LD message ignores sampler unit */
1372                                     GEN5_SAMPLER_MESSAGE_SAMPLE_LD,
1373                                     BRW_SAMPLER_SIMD_MODE_SIMD4X2, 0));
1374    } else {
1375 
1376       struct brw_reg addr = vec1(retype(brw_address_reg(0), BRW_REGISTER_TYPE_UD));
1377 
1378       brw_push_insn_state(p);
1379       brw_set_default_mask_control(p, BRW_MASK_DISABLE);
1380       brw_set_default_access_mode(p, BRW_ALIGN_1);
1381 
1382       /* a0.0 = surf_index & 0xff */
1383       brw_inst *insn_and = brw_next_insn(p, BRW_OPCODE_AND);
1384       brw_inst_set_exec_size(devinfo, insn_and, BRW_EXECUTE_1);
1385       brw_set_dest(p, insn_and, addr);
1386       brw_set_src0(p, insn_and, vec1(retype(surf_index, BRW_REGISTER_TYPE_UD)));
1387       brw_set_src1(p, insn_and, brw_imm_ud(0x0ff));
1388 
1389       brw_pop_insn_state(p);
1390 
1391       /* dst = send(offset, a0.0 | <descriptor>) */
1392       brw_send_indirect_message(
1393          p, BRW_SFID_SAMPLER, dst, offset, addr,
1394          brw_message_desc(devinfo, inst->mlen, 1, inst->header_size) |
1395          brw_sampler_desc(devinfo,
1396                           0 /* surface */,
1397                           0 /* sampler */,
1398                           GEN5_SAMPLER_MESSAGE_SAMPLE_LD,
1399                           BRW_SAMPLER_SIMD_MODE_SIMD4X2,
1400                           0),
1401          false /* EOT */);
1402    }
1403 }
1404 
1405 static void
generate_mov_indirect(struct brw_codegen * p,vec4_instruction *,struct brw_reg dst,struct brw_reg reg,struct brw_reg indirect)1406 generate_mov_indirect(struct brw_codegen *p,
1407                       vec4_instruction *,
1408                       struct brw_reg dst, struct brw_reg reg,
1409                       struct brw_reg indirect)
1410 {
1411    assert(indirect.type == BRW_REGISTER_TYPE_UD);
1412    assert(p->devinfo->gen >= 6);
1413 
1414    unsigned imm_byte_offset = reg.nr * REG_SIZE + reg.subnr * (REG_SIZE / 2);
1415 
1416    /* This instruction acts in align1 mode */
1417    assert(dst.writemask == WRITEMASK_XYZW);
1418 
1419    if (indirect.file == BRW_IMMEDIATE_VALUE) {
1420       imm_byte_offset += indirect.ud;
1421 
1422       reg.nr = imm_byte_offset / REG_SIZE;
1423       reg.subnr = (imm_byte_offset / (REG_SIZE / 2)) % 2;
1424       unsigned shift = (imm_byte_offset / 4) % 4;
1425       reg.swizzle += BRW_SWIZZLE4(shift, shift, shift, shift);
1426 
1427       brw_MOV(p, dst, reg);
1428    } else {
1429       brw_push_insn_state(p);
1430       brw_set_default_access_mode(p, BRW_ALIGN_1);
1431       brw_set_default_mask_control(p, BRW_MASK_DISABLE);
1432 
1433       struct brw_reg addr = vec8(brw_address_reg(0));
1434 
1435       /* We need to move the indirect value into the address register.  In
1436        * order to make things make some sense, we want to respect at least the
1437        * X component of the swizzle.  In order to do that, we need to convert
1438        * the subnr (probably 0) to an align1 subnr and add in the swizzle.
1439        */
1440       assert(brw_is_single_value_swizzle(indirect.swizzle));
1441       indirect.subnr = (indirect.subnr * 4 + BRW_GET_SWZ(indirect.swizzle, 0));
1442 
1443       /* We then use a region of <8,4,0>:uw to pick off the first 2 bytes of
1444        * the indirect and splat it out to all four channels of the given half
1445        * of a0.
1446        */
1447       indirect.subnr *= 2;
1448       indirect = stride(retype(indirect, BRW_REGISTER_TYPE_UW), 8, 4, 0);
1449       brw_ADD(p, addr, indirect, brw_imm_uw(imm_byte_offset));
1450 
1451       /* Now we need to incorporate the swizzle from the source register */
1452       if (reg.swizzle != BRW_SWIZZLE_XXXX) {
1453          uint32_t uv_swiz = BRW_GET_SWZ(reg.swizzle, 0) << 2 |
1454                             BRW_GET_SWZ(reg.swizzle, 1) << 6 |
1455                             BRW_GET_SWZ(reg.swizzle, 2) << 10 |
1456                             BRW_GET_SWZ(reg.swizzle, 3) << 14;
1457          uv_swiz |= uv_swiz << 16;
1458 
1459          brw_ADD(p, addr, addr, brw_imm_uv(uv_swiz));
1460       }
1461 
1462       brw_MOV(p, dst, retype(brw_VxH_indirect(0, 0), reg.type));
1463 
1464       brw_pop_insn_state(p);
1465    }
1466 }
1467 
1468 static void
generate_code(struct brw_codegen * p,const struct brw_compiler * compiler,void * log_data,const nir_shader * nir,struct brw_vue_prog_data * prog_data,const struct cfg_t * cfg,const performance & perf,struct brw_compile_stats * stats)1469 generate_code(struct brw_codegen *p,
1470               const struct brw_compiler *compiler,
1471               void *log_data,
1472               const nir_shader *nir,
1473               struct brw_vue_prog_data *prog_data,
1474               const struct cfg_t *cfg,
1475               const performance &perf,
1476               struct brw_compile_stats *stats)
1477 {
1478    const struct gen_device_info *devinfo = p->devinfo;
1479    const char *stage_abbrev = _mesa_shader_stage_to_abbrev(nir->info.stage);
1480    bool debug_flag = INTEL_DEBUG &
1481       intel_debug_flag_for_shader_stage(nir->info.stage);
1482    struct disasm_info *disasm_info = disasm_initialize(devinfo, cfg);
1483 
1484    /* `send_count` explicitly does not include spills or fills, as we'd
1485     * like to use it as a metric for intentional memory access or other
1486     * shared function use.  Otherwise, subtle changes to scheduling or
1487     * register allocation could cause it to fluctuate wildly - and that
1488     * effect is already counted in spill/fill counts.
1489     */
1490    int spill_count = 0, fill_count = 0;
1491    int loop_count = 0, send_count = 0;
1492 
1493    foreach_block_and_inst (block, vec4_instruction, inst, cfg) {
1494       struct brw_reg src[3], dst;
1495 
1496       if (unlikely(debug_flag))
1497          disasm_annotate(disasm_info, inst, p->next_insn_offset);
1498 
1499       for (unsigned int i = 0; i < 3; i++) {
1500          src[i] = inst->src[i].as_brw_reg();
1501       }
1502       dst = inst->dst.as_brw_reg();
1503 
1504       brw_set_default_predicate_control(p, inst->predicate);
1505       brw_set_default_predicate_inverse(p, inst->predicate_inverse);
1506       brw_set_default_flag_reg(p, inst->flag_subreg / 2, inst->flag_subreg % 2);
1507       brw_set_default_saturate(p, inst->saturate);
1508       brw_set_default_mask_control(p, inst->force_writemask_all);
1509       brw_set_default_acc_write_control(p, inst->writes_accumulator);
1510 
1511       assert(inst->group % inst->exec_size == 0);
1512       assert(inst->group % 4 == 0);
1513 
1514       /* There are some instructions where the destination is 64-bit
1515        * but we retype it to a smaller type. In that case, we cannot
1516        * double the exec_size.
1517        */
1518       const bool is_df = (get_exec_type_size(inst) == 8 ||
1519                           inst->dst.type == BRW_REGISTER_TYPE_DF) &&
1520                          inst->opcode != VEC4_OPCODE_PICK_LOW_32BIT &&
1521                          inst->opcode != VEC4_OPCODE_PICK_HIGH_32BIT &&
1522                          inst->opcode != VEC4_OPCODE_SET_LOW_32BIT &&
1523                          inst->opcode != VEC4_OPCODE_SET_HIGH_32BIT;
1524 
1525       unsigned exec_size = inst->exec_size;
1526       if (devinfo->gen == 7 && !devinfo->is_haswell && is_df)
1527          exec_size *= 2;
1528 
1529       brw_set_default_exec_size(p, cvt(exec_size) - 1);
1530 
1531       if (!inst->force_writemask_all)
1532          brw_set_default_group(p, inst->group);
1533 
1534       assert(inst->base_mrf + inst->mlen <= BRW_MAX_MRF(devinfo->gen));
1535       assert(inst->mlen <= BRW_MAX_MSG_LENGTH);
1536 
1537       unsigned pre_emit_nr_insn = p->nr_insn;
1538 
1539       switch (inst->opcode) {
1540       case VEC4_OPCODE_UNPACK_UNIFORM:
1541       case BRW_OPCODE_MOV:
1542          brw_MOV(p, dst, src[0]);
1543          break;
1544       case BRW_OPCODE_ADD:
1545          brw_ADD(p, dst, src[0], src[1]);
1546          break;
1547       case BRW_OPCODE_MUL:
1548          brw_MUL(p, dst, src[0], src[1]);
1549          break;
1550       case BRW_OPCODE_MACH:
1551          brw_MACH(p, dst, src[0], src[1]);
1552          break;
1553 
1554       case BRW_OPCODE_MAD:
1555          assert(devinfo->gen >= 6);
1556          brw_MAD(p, dst, src[0], src[1], src[2]);
1557          break;
1558 
1559       case BRW_OPCODE_FRC:
1560          brw_FRC(p, dst, src[0]);
1561          break;
1562       case BRW_OPCODE_RNDD:
1563          brw_RNDD(p, dst, src[0]);
1564          break;
1565       case BRW_OPCODE_RNDE:
1566          brw_RNDE(p, dst, src[0]);
1567          break;
1568       case BRW_OPCODE_RNDZ:
1569          brw_RNDZ(p, dst, src[0]);
1570          break;
1571 
1572       case BRW_OPCODE_AND:
1573          brw_AND(p, dst, src[0], src[1]);
1574          break;
1575       case BRW_OPCODE_OR:
1576          brw_OR(p, dst, src[0], src[1]);
1577          break;
1578       case BRW_OPCODE_XOR:
1579          brw_XOR(p, dst, src[0], src[1]);
1580          break;
1581       case BRW_OPCODE_NOT:
1582          brw_NOT(p, dst, src[0]);
1583          break;
1584       case BRW_OPCODE_ASR:
1585          brw_ASR(p, dst, src[0], src[1]);
1586          break;
1587       case BRW_OPCODE_SHR:
1588          brw_SHR(p, dst, src[0], src[1]);
1589          break;
1590       case BRW_OPCODE_SHL:
1591          brw_SHL(p, dst, src[0], src[1]);
1592          break;
1593 
1594       case BRW_OPCODE_CMP:
1595          brw_CMP(p, dst, inst->conditional_mod, src[0], src[1]);
1596          break;
1597       case BRW_OPCODE_SEL:
1598          brw_SEL(p, dst, src[0], src[1]);
1599          break;
1600 
1601       case BRW_OPCODE_DPH:
1602          brw_DPH(p, dst, src[0], src[1]);
1603          break;
1604 
1605       case BRW_OPCODE_DP4:
1606          brw_DP4(p, dst, src[0], src[1]);
1607          break;
1608 
1609       case BRW_OPCODE_DP3:
1610          brw_DP3(p, dst, src[0], src[1]);
1611          break;
1612 
1613       case BRW_OPCODE_DP2:
1614          brw_DP2(p, dst, src[0], src[1]);
1615          break;
1616 
1617       case BRW_OPCODE_F32TO16:
1618          assert(devinfo->gen >= 7);
1619          brw_F32TO16(p, dst, src[0]);
1620          break;
1621 
1622       case BRW_OPCODE_F16TO32:
1623          assert(devinfo->gen >= 7);
1624          brw_F16TO32(p, dst, src[0]);
1625          break;
1626 
1627       case BRW_OPCODE_LRP:
1628          assert(devinfo->gen >= 6);
1629          brw_LRP(p, dst, src[0], src[1], src[2]);
1630          break;
1631 
1632       case BRW_OPCODE_BFREV:
1633          assert(devinfo->gen >= 7);
1634          brw_BFREV(p, retype(dst, BRW_REGISTER_TYPE_UD),
1635                    retype(src[0], BRW_REGISTER_TYPE_UD));
1636          break;
1637       case BRW_OPCODE_FBH:
1638          assert(devinfo->gen >= 7);
1639          brw_FBH(p, retype(dst, src[0].type), src[0]);
1640          break;
1641       case BRW_OPCODE_FBL:
1642          assert(devinfo->gen >= 7);
1643          brw_FBL(p, retype(dst, BRW_REGISTER_TYPE_UD),
1644                  retype(src[0], BRW_REGISTER_TYPE_UD));
1645          break;
1646       case BRW_OPCODE_LZD:
1647          brw_LZD(p, dst, src[0]);
1648          break;
1649       case BRW_OPCODE_CBIT:
1650          assert(devinfo->gen >= 7);
1651          brw_CBIT(p, retype(dst, BRW_REGISTER_TYPE_UD),
1652                   retype(src[0], BRW_REGISTER_TYPE_UD));
1653          break;
1654       case BRW_OPCODE_ADDC:
1655          assert(devinfo->gen >= 7);
1656          brw_ADDC(p, dst, src[0], src[1]);
1657          break;
1658       case BRW_OPCODE_SUBB:
1659          assert(devinfo->gen >= 7);
1660          brw_SUBB(p, dst, src[0], src[1]);
1661          break;
1662       case BRW_OPCODE_MAC:
1663          brw_MAC(p, dst, src[0], src[1]);
1664          break;
1665 
1666       case BRW_OPCODE_BFE:
1667          assert(devinfo->gen >= 7);
1668          brw_BFE(p, dst, src[0], src[1], src[2]);
1669          break;
1670 
1671       case BRW_OPCODE_BFI1:
1672          assert(devinfo->gen >= 7);
1673          brw_BFI1(p, dst, src[0], src[1]);
1674          break;
1675       case BRW_OPCODE_BFI2:
1676          assert(devinfo->gen >= 7);
1677          brw_BFI2(p, dst, src[0], src[1], src[2]);
1678          break;
1679 
1680       case BRW_OPCODE_IF:
1681          if (!inst->src[0].is_null()) {
1682             /* The instruction has an embedded compare (only allowed on gen6) */
1683             assert(devinfo->gen == 6);
1684             gen6_IF(p, inst->conditional_mod, src[0], src[1]);
1685          } else {
1686             brw_inst *if_inst = brw_IF(p, BRW_EXECUTE_8);
1687             brw_inst_set_pred_control(p->devinfo, if_inst, inst->predicate);
1688          }
1689          break;
1690 
1691       case BRW_OPCODE_ELSE:
1692          brw_ELSE(p);
1693          break;
1694       case BRW_OPCODE_ENDIF:
1695          brw_ENDIF(p);
1696          break;
1697 
1698       case BRW_OPCODE_DO:
1699          brw_DO(p, BRW_EXECUTE_8);
1700          break;
1701 
1702       case BRW_OPCODE_BREAK:
1703          brw_BREAK(p);
1704          brw_set_default_predicate_control(p, BRW_PREDICATE_NONE);
1705          break;
1706       case BRW_OPCODE_CONTINUE:
1707          brw_CONT(p);
1708          brw_set_default_predicate_control(p, BRW_PREDICATE_NONE);
1709          break;
1710 
1711       case BRW_OPCODE_WHILE:
1712          brw_WHILE(p);
1713          loop_count++;
1714          break;
1715 
1716       case SHADER_OPCODE_RCP:
1717       case SHADER_OPCODE_RSQ:
1718       case SHADER_OPCODE_SQRT:
1719       case SHADER_OPCODE_EXP2:
1720       case SHADER_OPCODE_LOG2:
1721       case SHADER_OPCODE_SIN:
1722       case SHADER_OPCODE_COS:
1723          assert(inst->conditional_mod == BRW_CONDITIONAL_NONE);
1724          if (devinfo->gen >= 7) {
1725             gen6_math(p, dst, brw_math_function(inst->opcode), src[0],
1726                       brw_null_reg());
1727          } else if (devinfo->gen == 6) {
1728             generate_math_gen6(p, inst, dst, src[0], brw_null_reg());
1729          } else {
1730             generate_math1_gen4(p, inst, dst, src[0]);
1731             send_count++;
1732          }
1733          break;
1734 
1735       case SHADER_OPCODE_POW:
1736       case SHADER_OPCODE_INT_QUOTIENT:
1737       case SHADER_OPCODE_INT_REMAINDER:
1738          assert(inst->conditional_mod == BRW_CONDITIONAL_NONE);
1739          if (devinfo->gen >= 7) {
1740             gen6_math(p, dst, brw_math_function(inst->opcode), src[0], src[1]);
1741          } else if (devinfo->gen == 6) {
1742             generate_math_gen6(p, inst, dst, src[0], src[1]);
1743          } else {
1744             generate_math2_gen4(p, inst, dst, src[0], src[1]);
1745             send_count++;
1746          }
1747          break;
1748 
1749       case SHADER_OPCODE_TEX:
1750       case SHADER_OPCODE_TXD:
1751       case SHADER_OPCODE_TXF:
1752       case SHADER_OPCODE_TXF_CMS:
1753       case SHADER_OPCODE_TXF_CMS_W:
1754       case SHADER_OPCODE_TXF_MCS:
1755       case SHADER_OPCODE_TXL:
1756       case SHADER_OPCODE_TXS:
1757       case SHADER_OPCODE_TG4:
1758       case SHADER_OPCODE_TG4_OFFSET:
1759       case SHADER_OPCODE_SAMPLEINFO:
1760          generate_tex(p, prog_data, nir->info.stage,
1761                       inst, dst, src[0], src[1], src[2]);
1762          send_count++;
1763          break;
1764 
1765       case SHADER_OPCODE_GET_BUFFER_SIZE:
1766          generate_get_buffer_size(p, inst, dst, src[0], src[1]);
1767          send_count++;
1768          break;
1769 
1770       case VS_OPCODE_URB_WRITE:
1771          generate_vs_urb_write(p, inst);
1772          send_count++;
1773          break;
1774 
1775       case SHADER_OPCODE_GEN4_SCRATCH_READ:
1776          generate_scratch_read(p, inst, dst, src[0]);
1777          fill_count++;
1778          break;
1779 
1780       case SHADER_OPCODE_GEN4_SCRATCH_WRITE:
1781          generate_scratch_write(p, inst, dst, src[0], src[1]);
1782          spill_count++;
1783          break;
1784 
1785       case VS_OPCODE_PULL_CONSTANT_LOAD:
1786          generate_pull_constant_load(p, inst, dst, src[0], src[1]);
1787          send_count++;
1788          break;
1789 
1790       case VS_OPCODE_PULL_CONSTANT_LOAD_GEN7:
1791          generate_pull_constant_load_gen7(p, inst, dst, src[0], src[1]);
1792          send_count++;
1793          break;
1794 
1795       case GS_OPCODE_URB_WRITE:
1796          generate_gs_urb_write(p, inst);
1797          send_count++;
1798          break;
1799 
1800       case GS_OPCODE_URB_WRITE_ALLOCATE:
1801          generate_gs_urb_write_allocate(p, inst);
1802          send_count++;
1803          break;
1804 
1805       case GS_OPCODE_SVB_WRITE:
1806          generate_gs_svb_write(p, inst, dst, src[0], src[1]);
1807          send_count++;
1808          break;
1809 
1810       case GS_OPCODE_SVB_SET_DST_INDEX:
1811          generate_gs_svb_set_destination_index(p, inst, dst, src[0]);
1812          break;
1813 
1814       case GS_OPCODE_THREAD_END:
1815          generate_gs_thread_end(p, inst);
1816          send_count++;
1817          break;
1818 
1819       case GS_OPCODE_SET_WRITE_OFFSET:
1820          generate_gs_set_write_offset(p, dst, src[0], src[1]);
1821          break;
1822 
1823       case GS_OPCODE_SET_VERTEX_COUNT:
1824          generate_gs_set_vertex_count(p, dst, src[0]);
1825          break;
1826 
1827       case GS_OPCODE_FF_SYNC:
1828          generate_gs_ff_sync(p, inst, dst, src[0], src[1]);
1829          send_count++;
1830          break;
1831 
1832       case GS_OPCODE_FF_SYNC_SET_PRIMITIVES:
1833          generate_gs_ff_sync_set_primitives(p, dst, src[0], src[1], src[2]);
1834          break;
1835 
1836       case GS_OPCODE_SET_PRIMITIVE_ID:
1837          generate_gs_set_primitive_id(p, dst);
1838          break;
1839 
1840       case GS_OPCODE_SET_DWORD_2:
1841          generate_gs_set_dword_2(p, dst, src[0]);
1842          break;
1843 
1844       case GS_OPCODE_PREPARE_CHANNEL_MASKS:
1845          generate_gs_prepare_channel_masks(p, dst);
1846          break;
1847 
1848       case GS_OPCODE_SET_CHANNEL_MASKS:
1849          generate_gs_set_channel_masks(p, dst, src[0]);
1850          break;
1851 
1852       case GS_OPCODE_GET_INSTANCE_ID:
1853          generate_gs_get_instance_id(p, dst);
1854          break;
1855 
1856       case SHADER_OPCODE_SHADER_TIME_ADD:
1857          brw_shader_time_add(p, src[0],
1858                              prog_data->base.binding_table.shader_time_start);
1859          send_count++;
1860          break;
1861 
1862       case VEC4_OPCODE_UNTYPED_ATOMIC:
1863          assert(src[2].file == BRW_IMMEDIATE_VALUE);
1864          brw_untyped_atomic(p, dst, src[0], src[1], src[2].ud, inst->mlen,
1865                             !inst->dst.is_null(), inst->header_size);
1866          send_count++;
1867          break;
1868 
1869       case VEC4_OPCODE_UNTYPED_SURFACE_READ:
1870          assert(!inst->header_size);
1871          assert(src[2].file == BRW_IMMEDIATE_VALUE);
1872          brw_untyped_surface_read(p, dst, src[0], src[1], inst->mlen,
1873                                   src[2].ud);
1874          send_count++;
1875          break;
1876 
1877       case VEC4_OPCODE_UNTYPED_SURFACE_WRITE:
1878          assert(src[2].file == BRW_IMMEDIATE_VALUE);
1879          brw_untyped_surface_write(p, src[0], src[1], inst->mlen,
1880                                    src[2].ud, inst->header_size);
1881          send_count++;
1882          break;
1883 
1884       case SHADER_OPCODE_MEMORY_FENCE:
1885          brw_memory_fence(p, dst, src[0], BRW_OPCODE_SEND,
1886                           brw_message_target(inst->sfid),
1887                           /* commit_enable */ false,
1888                           /* bti */ 0);
1889          send_count++;
1890          break;
1891 
1892       case SHADER_OPCODE_FIND_LIVE_CHANNEL: {
1893          const struct brw_reg mask =
1894             brw_stage_has_packed_dispatch(devinfo, nir->info.stage,
1895                                           &prog_data->base) ? brw_imm_ud(~0u) :
1896             brw_dmask_reg();
1897          brw_find_live_channel(p, dst, mask);
1898          break;
1899       }
1900 
1901       case SHADER_OPCODE_BROADCAST:
1902          assert(inst->force_writemask_all);
1903          brw_broadcast(p, dst, src[0], src[1]);
1904          break;
1905 
1906       case VS_OPCODE_UNPACK_FLAGS_SIMD4X2:
1907          generate_unpack_flags(p, dst);
1908          break;
1909 
1910       case VEC4_OPCODE_MOV_BYTES: {
1911          /* Moves the low byte from each channel, using an Align1 access mode
1912           * and a <4,1,0> source region.
1913           */
1914          assert(src[0].type == BRW_REGISTER_TYPE_UB ||
1915                 src[0].type == BRW_REGISTER_TYPE_B);
1916 
1917          brw_set_default_access_mode(p, BRW_ALIGN_1);
1918          src[0].vstride = BRW_VERTICAL_STRIDE_4;
1919          src[0].width = BRW_WIDTH_1;
1920          src[0].hstride = BRW_HORIZONTAL_STRIDE_0;
1921          brw_MOV(p, dst, src[0]);
1922          brw_set_default_access_mode(p, BRW_ALIGN_16);
1923          break;
1924       }
1925 
1926       case VEC4_OPCODE_DOUBLE_TO_F32:
1927       case VEC4_OPCODE_DOUBLE_TO_D32:
1928       case VEC4_OPCODE_DOUBLE_TO_U32: {
1929          assert(type_sz(src[0].type) == 8);
1930          assert(type_sz(dst.type) == 8);
1931 
1932          brw_reg_type dst_type;
1933 
1934          switch (inst->opcode) {
1935          case VEC4_OPCODE_DOUBLE_TO_F32:
1936             dst_type = BRW_REGISTER_TYPE_F;
1937             break;
1938          case VEC4_OPCODE_DOUBLE_TO_D32:
1939             dst_type = BRW_REGISTER_TYPE_D;
1940             break;
1941          case VEC4_OPCODE_DOUBLE_TO_U32:
1942             dst_type = BRW_REGISTER_TYPE_UD;
1943             break;
1944          default:
1945             unreachable("Not supported conversion");
1946          }
1947          dst = retype(dst, dst_type);
1948 
1949          brw_set_default_access_mode(p, BRW_ALIGN_1);
1950 
1951          /* When converting from DF->F, we set destination's stride as 2 as an
1952           * aligment requirement. But in IVB/BYT, each DF implicitly writes
1953           * two floats, being the first one the converted value. So we don't
1954           * need to explicitly set stride 2, but 1.
1955           */
1956          struct brw_reg spread_dst;
1957          if (devinfo->gen == 7 && !devinfo->is_haswell)
1958             spread_dst = stride(dst, 8, 4, 1);
1959          else
1960             spread_dst = stride(dst, 8, 4, 2);
1961 
1962          brw_MOV(p, spread_dst, src[0]);
1963 
1964          brw_set_default_access_mode(p, BRW_ALIGN_16);
1965          break;
1966       }
1967 
1968       case VEC4_OPCODE_TO_DOUBLE: {
1969          assert(type_sz(src[0].type) == 4);
1970          assert(type_sz(dst.type) == 8);
1971 
1972          brw_set_default_access_mode(p, BRW_ALIGN_1);
1973 
1974          brw_MOV(p, dst, src[0]);
1975 
1976          brw_set_default_access_mode(p, BRW_ALIGN_16);
1977          break;
1978       }
1979 
1980       case VEC4_OPCODE_PICK_LOW_32BIT:
1981       case VEC4_OPCODE_PICK_HIGH_32BIT: {
1982          /* Stores the low/high 32-bit of each 64-bit element in src[0] into
1983           * dst using ALIGN1 mode and a <8,4,2>:UD region on the source.
1984           */
1985          assert(type_sz(src[0].type) == 8);
1986          assert(type_sz(dst.type) == 4);
1987 
1988          brw_set_default_access_mode(p, BRW_ALIGN_1);
1989 
1990          dst = retype(dst, BRW_REGISTER_TYPE_UD);
1991          dst.hstride = BRW_HORIZONTAL_STRIDE_1;
1992 
1993          src[0] = retype(src[0], BRW_REGISTER_TYPE_UD);
1994          if (inst->opcode == VEC4_OPCODE_PICK_HIGH_32BIT)
1995             src[0] = suboffset(src[0], 1);
1996          src[0] = spread(src[0], 2);
1997          brw_MOV(p, dst, src[0]);
1998 
1999          brw_set_default_access_mode(p, BRW_ALIGN_16);
2000          break;
2001       }
2002 
2003       case VEC4_OPCODE_SET_LOW_32BIT:
2004       case VEC4_OPCODE_SET_HIGH_32BIT: {
2005          /* Reads consecutive 32-bit elements from src[0] and writes
2006           * them to the low/high 32-bit of each 64-bit element in dst.
2007           */
2008          assert(type_sz(src[0].type) == 4);
2009          assert(type_sz(dst.type) == 8);
2010 
2011          brw_set_default_access_mode(p, BRW_ALIGN_1);
2012 
2013          dst = retype(dst, BRW_REGISTER_TYPE_UD);
2014          if (inst->opcode == VEC4_OPCODE_SET_HIGH_32BIT)
2015             dst = suboffset(dst, 1);
2016          dst.hstride = BRW_HORIZONTAL_STRIDE_2;
2017 
2018          src[0] = retype(src[0], BRW_REGISTER_TYPE_UD);
2019          brw_MOV(p, dst, src[0]);
2020 
2021          brw_set_default_access_mode(p, BRW_ALIGN_16);
2022          break;
2023       }
2024 
2025       case VEC4_OPCODE_PACK_BYTES: {
2026          /* Is effectively:
2027           *
2028           *   mov(8) dst<16,4,1>:UB src<4,1,0>:UB
2029           *
2030           * but destinations' only regioning is horizontal stride, so instead we
2031           * have to use two instructions:
2032           *
2033           *   mov(4) dst<1>:UB     src<4,1,0>:UB
2034           *   mov(4) dst.16<1>:UB  src.16<4,1,0>:UB
2035           *
2036           * where they pack the four bytes from the low and high four DW.
2037           */
2038          assert(util_is_power_of_two_nonzero(dst.writemask));
2039          unsigned offset = __builtin_ctz(dst.writemask);
2040 
2041          dst.type = BRW_REGISTER_TYPE_UB;
2042 
2043          brw_set_default_access_mode(p, BRW_ALIGN_1);
2044 
2045          src[0].type = BRW_REGISTER_TYPE_UB;
2046          src[0].vstride = BRW_VERTICAL_STRIDE_4;
2047          src[0].width = BRW_WIDTH_1;
2048          src[0].hstride = BRW_HORIZONTAL_STRIDE_0;
2049          dst.subnr = offset * 4;
2050          struct brw_inst *insn = brw_MOV(p, dst, src[0]);
2051          brw_inst_set_exec_size(p->devinfo, insn, BRW_EXECUTE_4);
2052          brw_inst_set_no_dd_clear(p->devinfo, insn, true);
2053          brw_inst_set_no_dd_check(p->devinfo, insn, inst->no_dd_check);
2054 
2055          src[0].subnr = 16;
2056          dst.subnr = 16 + offset * 4;
2057          insn = brw_MOV(p, dst, src[0]);
2058          brw_inst_set_exec_size(p->devinfo, insn, BRW_EXECUTE_4);
2059          brw_inst_set_no_dd_clear(p->devinfo, insn, inst->no_dd_clear);
2060          brw_inst_set_no_dd_check(p->devinfo, insn, true);
2061 
2062          brw_set_default_access_mode(p, BRW_ALIGN_16);
2063          break;
2064       }
2065 
2066       case TCS_OPCODE_URB_WRITE:
2067          generate_tcs_urb_write(p, inst, src[0]);
2068          send_count++;
2069          break;
2070 
2071       case VEC4_OPCODE_URB_READ:
2072          generate_vec4_urb_read(p, inst, dst, src[0]);
2073          send_count++;
2074          break;
2075 
2076       case TCS_OPCODE_SET_INPUT_URB_OFFSETS:
2077          generate_tcs_input_urb_offsets(p, dst, src[0], src[1]);
2078          break;
2079 
2080       case TCS_OPCODE_SET_OUTPUT_URB_OFFSETS:
2081          generate_tcs_output_urb_offsets(p, dst, src[0], src[1]);
2082          break;
2083 
2084       case TCS_OPCODE_GET_INSTANCE_ID:
2085          generate_tcs_get_instance_id(p, dst);
2086          break;
2087 
2088       case TCS_OPCODE_GET_PRIMITIVE_ID:
2089          generate_tcs_get_primitive_id(p, dst);
2090          break;
2091 
2092       case TCS_OPCODE_CREATE_BARRIER_HEADER:
2093          generate_tcs_create_barrier_header(p, prog_data, dst);
2094          break;
2095 
2096       case TES_OPCODE_CREATE_INPUT_READ_HEADER:
2097          generate_tes_create_input_read_header(p, dst);
2098          break;
2099 
2100       case TES_OPCODE_ADD_INDIRECT_URB_OFFSET:
2101          generate_tes_add_indirect_urb_offset(p, dst, src[0], src[1]);
2102          break;
2103 
2104       case TES_OPCODE_GET_PRIMITIVE_ID:
2105          generate_tes_get_primitive_id(p, dst);
2106          break;
2107 
2108       case TCS_OPCODE_SRC0_010_IS_ZERO:
2109          /* If src_reg had stride like fs_reg, we wouldn't need this. */
2110          brw_MOV(p, brw_null_reg(), stride(src[0], 0, 1, 0));
2111          break;
2112 
2113       case TCS_OPCODE_RELEASE_INPUT:
2114          generate_tcs_release_input(p, dst, src[0], src[1]);
2115          send_count++;
2116          break;
2117 
2118       case TCS_OPCODE_THREAD_END:
2119          generate_tcs_thread_end(p, inst);
2120          send_count++;
2121          break;
2122 
2123       case SHADER_OPCODE_BARRIER:
2124          brw_barrier(p, src[0]);
2125          brw_WAIT(p);
2126          send_count++;
2127          break;
2128 
2129       case SHADER_OPCODE_MOV_INDIRECT:
2130          generate_mov_indirect(p, inst, dst, src[0], src[1]);
2131          break;
2132 
2133       case BRW_OPCODE_DIM:
2134          assert(devinfo->is_haswell);
2135          assert(src[0].type == BRW_REGISTER_TYPE_DF);
2136          assert(dst.type == BRW_REGISTER_TYPE_DF);
2137          brw_DIM(p, dst, retype(src[0], BRW_REGISTER_TYPE_F));
2138          break;
2139 
2140       default:
2141          unreachable("Unsupported opcode");
2142       }
2143 
2144       if (inst->opcode == VEC4_OPCODE_PACK_BYTES) {
2145          /* Handled dependency hints in the generator. */
2146 
2147          assert(!inst->conditional_mod);
2148       } else if (inst->no_dd_clear || inst->no_dd_check || inst->conditional_mod) {
2149          assert(p->nr_insn == pre_emit_nr_insn + 1 ||
2150                 !"conditional_mod, no_dd_check, or no_dd_clear set for IR "
2151                  "emitting more than 1 instruction");
2152 
2153          brw_inst *last = &p->store[pre_emit_nr_insn];
2154 
2155          if (inst->conditional_mod)
2156             brw_inst_set_cond_modifier(p->devinfo, last, inst->conditional_mod);
2157          brw_inst_set_no_dd_clear(p->devinfo, last, inst->no_dd_clear);
2158          brw_inst_set_no_dd_check(p->devinfo, last, inst->no_dd_check);
2159       }
2160    }
2161 
2162    brw_set_uip_jip(p, 0);
2163 
2164    /* end of program sentinel */
2165    disasm_new_inst_group(disasm_info, p->next_insn_offset);
2166 
2167 #ifndef NDEBUG
2168    bool validated =
2169 #else
2170    if (unlikely(debug_flag))
2171 #endif
2172       brw_validate_instructions(devinfo, p->store,
2173                                 0, p->next_insn_offset,
2174                                 disasm_info);
2175 
2176    int before_size = p->next_insn_offset;
2177    brw_compact_instructions(p, 0, disasm_info);
2178    int after_size = p->next_insn_offset;
2179 
2180    if (unlikely(debug_flag)) {
2181       unsigned char sha1[21];
2182       char sha1buf[41];
2183 
2184       _mesa_sha1_compute(p->store, p->next_insn_offset, sha1);
2185       _mesa_sha1_format(sha1buf, sha1);
2186 
2187       fprintf(stderr, "Native code for %s %s shader %s (sha1 %s):\n",
2188             nir->info.label ? nir->info.label : "unnamed",
2189             _mesa_shader_stage_to_string(nir->info.stage), nir->info.name,
2190             sha1buf);
2191 
2192       fprintf(stderr, "%s vec4 shader: %d instructions. %d loops. %u cycles. %d:%d "
2193                      "spills:fills, %u sends. Compacted %d to %d bytes (%.0f%%)\n",
2194             stage_abbrev, before_size / 16, loop_count, perf.latency,
2195             spill_count, fill_count, send_count, before_size, after_size,
2196             100.0f * (before_size - after_size) / before_size);
2197 
2198       /* overriding the shader makes disasm_info invalid */
2199       if (!brw_try_override_assembly(p, 0, sha1buf)) {
2200          dump_assembly(p->store, 0, p->next_insn_offset,
2201                        disasm_info, perf.block_latency);
2202       } else {
2203          fprintf(stderr, "Successfully overrode shader with sha1 %s\n\n", sha1buf);
2204       }
2205    }
2206    ralloc_free(disasm_info);
2207    assert(validated);
2208 
2209    compiler->shader_debug_log(log_data,
2210                               "%s vec4 shader: %d inst, %d loops, %u cycles, "
2211                               "%d:%d spills:fills, %u sends, "
2212                               "compacted %d to %d bytes.",
2213                               stage_abbrev, before_size / 16,
2214                               loop_count, perf.latency, spill_count,
2215                               fill_count, send_count, before_size, after_size);
2216    if (stats) {
2217       stats->dispatch_width = 0;
2218       stats->instructions = before_size / 16;
2219       stats->sends = send_count;
2220       stats->loops = loop_count;
2221       stats->cycles = perf.latency;
2222       stats->spills = spill_count;
2223       stats->fills = fill_count;
2224    }
2225 }
2226 
2227 extern "C" const unsigned *
brw_vec4_generate_assembly(const struct brw_compiler * compiler,void * log_data,void * mem_ctx,const nir_shader * nir,struct brw_vue_prog_data * prog_data,const struct cfg_t * cfg,const performance & perf,struct brw_compile_stats * stats)2228 brw_vec4_generate_assembly(const struct brw_compiler *compiler,
2229                            void *log_data,
2230                            void *mem_ctx,
2231                            const nir_shader *nir,
2232                            struct brw_vue_prog_data *prog_data,
2233                            const struct cfg_t *cfg,
2234                            const performance &perf,
2235                            struct brw_compile_stats *stats)
2236 {
2237    struct brw_codegen *p = rzalloc(mem_ctx, struct brw_codegen);
2238    brw_init_codegen(compiler->devinfo, p, mem_ctx);
2239    brw_set_default_access_mode(p, BRW_ALIGN_16);
2240 
2241    generate_code(p, compiler, log_data, nir, prog_data, cfg, perf, stats);
2242 
2243    assert(prog_data->base.const_data_size == 0);
2244    if (nir->constant_data_size > 0) {
2245       prog_data->base.const_data_size = nir->constant_data_size;
2246       prog_data->base.const_data_offset =
2247          brw_append_data(p, nir->constant_data, nir->constant_data_size, 32);
2248    }
2249 
2250    return brw_get_program(p, &prog_data->base.program_size);
2251 }
2252