2 * Copyright © 2011 Intel Corporation
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
28 #include "brw_dead_control_flow.h"
31 #include "main/macros.h"
32 #include "main/shaderobj.h"
33 #include "program/prog_print.h"
34 #include "program/prog_parameter.h"
37 #define MAX_INSTRUCTION (1 << 30)
44 * Common helper for constructing swizzles. When only a subset of
45 * channels of a vec4 are used, we don't want to reference the other
46 * channels, as that will tell optimization passes that those other
50 swizzle_for_size(int size
)
52 static const unsigned size_swizzles
[4] = {
53 BRW_SWIZZLE4(SWIZZLE_X
, SWIZZLE_X
, SWIZZLE_X
, SWIZZLE_X
),
54 BRW_SWIZZLE4(SWIZZLE_X
, SWIZZLE_Y
, SWIZZLE_Y
, SWIZZLE_Y
),
55 BRW_SWIZZLE4(SWIZZLE_X
, SWIZZLE_Y
, SWIZZLE_Z
, SWIZZLE_Z
),
56 BRW_SWIZZLE4(SWIZZLE_X
, SWIZZLE_Y
, SWIZZLE_Z
, SWIZZLE_W
),
59 assert((size
>= 1) && (size
<= 4));
60 return size_swizzles
[size
- 1];
66 memset(this, 0, sizeof(*this));
68 this->file
= BAD_FILE
;
71 src_reg::src_reg(register_file file
, int reg
, const glsl_type
*type
)
77 if (type
&& (type
->is_scalar() || type
->is_vector() || type
->is_matrix()))
78 this->swizzle
= swizzle_for_size(type
->vector_elements
);
80 this->swizzle
= BRW_SWIZZLE_XYZW
;
83 /** Generic unset register constructor. */
89 src_reg::src_reg(float f
)
94 this->type
= BRW_REGISTER_TYPE_F
;
95 this->fixed_hw_reg
.dw1
.f
= f
;
98 src_reg::src_reg(uint32_t u
)
103 this->type
= BRW_REGISTER_TYPE_UD
;
104 this->fixed_hw_reg
.dw1
.ud
= u
;
107 src_reg::src_reg(int32_t i
)
112 this->type
= BRW_REGISTER_TYPE_D
;
113 this->fixed_hw_reg
.dw1
.d
= i
;
116 src_reg::src_reg(uint8_t vf
[4])
121 this->type
= BRW_REGISTER_TYPE_VF
;
122 memcpy(&this->fixed_hw_reg
.dw1
.ud
, vf
, sizeof(unsigned));
125 src_reg::src_reg(uint8_t vf0
, uint8_t vf1
, uint8_t vf2
, uint8_t vf3
)
130 this->type
= BRW_REGISTER_TYPE_VF
;
131 this->fixed_hw_reg
.dw1
.ud
= (vf0
<< 0) |
137 src_reg::src_reg(struct brw_reg reg
)
142 this->fixed_hw_reg
= reg
;
143 this->type
= reg
.type
;
146 src_reg::src_reg(dst_reg reg
)
150 this->file
= reg
.file
;
152 this->reg_offset
= reg
.reg_offset
;
153 this->type
= reg
.type
;
154 this->reladdr
= reg
.reladdr
;
155 this->fixed_hw_reg
= reg
.fixed_hw_reg
;
161 for (int i
= 0; i
< 4; i
++) {
162 if (!(reg
.writemask
& (1 << i
)))
165 swizzles
[next_chan
++] = last
= i
;
168 for (; next_chan
< 4; next_chan
++) {
169 swizzles
[next_chan
] = last
;
172 this->swizzle
= BRW_SWIZZLE4(swizzles
[0], swizzles
[1],
173 swizzles
[2], swizzles
[3]);
179 memset(this, 0, sizeof(*this));
180 this->file
= BAD_FILE
;
181 this->writemask
= WRITEMASK_XYZW
;
189 dst_reg::dst_reg(register_file file
, int reg
)
197 dst_reg::dst_reg(register_file file
, int reg
, const glsl_type
*type
,
204 this->type
= brw_type_for_base_type(type
);
205 this->writemask
= writemask
;
208 dst_reg::dst_reg(struct brw_reg reg
)
213 this->fixed_hw_reg
= reg
;
214 this->type
= reg
.type
;
217 dst_reg::dst_reg(src_reg reg
)
221 this->file
= reg
.file
;
223 this->reg_offset
= reg
.reg_offset
;
224 this->type
= reg
.type
;
225 /* How should we do writemasking when converting from a src_reg? It seems
226 * pretty obvious that for src.xxxx the caller wants to write to src.x, but
227 * what about for src.wx? Just special-case src.xxxx for now.
229 if (reg
.swizzle
== BRW_SWIZZLE_XXXX
)
230 this->writemask
= WRITEMASK_X
;
232 this->writemask
= WRITEMASK_XYZW
;
233 this->reladdr
= reg
.reladdr
;
234 this->fixed_hw_reg
= reg
.fixed_hw_reg
;
238 dst_reg::equals(const dst_reg
&r
) const
240 return (file
== r
.file
&&
242 reg_offset
== r
.reg_offset
&&
244 negate
== r
.negate
&&
246 writemask
== r
.writemask
&&
247 (reladdr
== r
.reladdr
||
248 (reladdr
&& r
.reladdr
&& reladdr
->equals(*r
.reladdr
))) &&
249 memcmp(&fixed_hw_reg
, &r
.fixed_hw_reg
,
250 sizeof(fixed_hw_reg
)) == 0);
254 vec4_instruction::is_send_from_grf()
257 case SHADER_OPCODE_SHADER_TIME_ADD
:
258 case VS_OPCODE_PULL_CONSTANT_LOAD_GEN7
:
266 vec4_instruction::regs_read(unsigned arg
) const
268 if (src
[arg
].file
== BAD_FILE
)
272 case SHADER_OPCODE_SHADER_TIME_ADD
:
273 return arg
== 0 ? mlen
: 1;
275 case VS_OPCODE_PULL_CONSTANT_LOAD_GEN7
:
276 return arg
== 1 ? mlen
: 1;
284 vec4_instruction::can_do_source_mods(struct brw_context
*brw
)
286 if (brw
->gen
== 6 && is_math())
289 if (is_send_from_grf())
292 if (!backend_instruction::can_do_source_mods())
299 * Returns how many MRFs an opcode will write over.
301 * Note that this is not the 0 or 1 implied writes in an actual gen
302 * instruction -- the generate_* functions generate additional MOVs
306 vec4_visitor::implied_mrf_writes(vec4_instruction
*inst
)
308 if (inst
->mlen
== 0 || inst
->is_send_from_grf())
311 switch (inst
->opcode
) {
312 case SHADER_OPCODE_RCP
:
313 case SHADER_OPCODE_RSQ
:
314 case SHADER_OPCODE_SQRT
:
315 case SHADER_OPCODE_EXP2
:
316 case SHADER_OPCODE_LOG2
:
317 case SHADER_OPCODE_SIN
:
318 case SHADER_OPCODE_COS
:
320 case SHADER_OPCODE_INT_QUOTIENT
:
321 case SHADER_OPCODE_INT_REMAINDER
:
322 case SHADER_OPCODE_POW
:
324 case VS_OPCODE_URB_WRITE
:
326 case VS_OPCODE_PULL_CONSTANT_LOAD
:
328 case SHADER_OPCODE_GEN4_SCRATCH_READ
:
330 case SHADER_OPCODE_GEN4_SCRATCH_WRITE
:
332 case GS_OPCODE_URB_WRITE
:
333 case GS_OPCODE_URB_WRITE_ALLOCATE
:
334 case GS_OPCODE_THREAD_END
:
336 case GS_OPCODE_FF_SYNC
:
338 case SHADER_OPCODE_SHADER_TIME_ADD
:
340 case SHADER_OPCODE_TEX
:
341 case SHADER_OPCODE_TXL
:
342 case SHADER_OPCODE_TXD
:
343 case SHADER_OPCODE_TXF
:
344 case SHADER_OPCODE_TXF_CMS
:
345 case SHADER_OPCODE_TXF_MCS
:
346 case SHADER_OPCODE_TXS
:
347 case SHADER_OPCODE_TG4
:
348 case SHADER_OPCODE_TG4_OFFSET
:
349 return inst
->header_present
? 1 : 0;
350 case SHADER_OPCODE_UNTYPED_ATOMIC
:
351 case SHADER_OPCODE_UNTYPED_SURFACE_READ
:
354 unreachable("not reached");
359 src_reg::equals(const src_reg
&r
) const
361 return (file
== r
.file
&&
363 reg_offset
== r
.reg_offset
&&
365 negate
== r
.negate
&&
367 swizzle
== r
.swizzle
&&
368 !reladdr
&& !r
.reladdr
&&
369 memcmp(&fixed_hw_reg
, &r
.fixed_hw_reg
,
370 sizeof(fixed_hw_reg
)) == 0);
374 vec4_visitor::opt_vector_float()
376 bool progress
= false;
378 int last_reg
= -1, last_reg_offset
= -1;
379 enum register_file last_reg_file
= BAD_FILE
;
381 int remaining_channels
;
384 vec4_instruction
*imm_inst
[4];
386 foreach_block_and_inst_safe(block
, vec4_instruction
, inst
, cfg
) {
387 if (last_reg
!= inst
->dst
.reg
||
388 last_reg_offset
!= inst
->dst
.reg_offset
||
389 last_reg_file
!= inst
->dst
.file
) {
390 last_reg
= inst
->dst
.reg
;
391 last_reg_offset
= inst
->dst
.reg_offset
;
392 last_reg_file
= inst
->dst
.file
;
393 remaining_channels
= WRITEMASK_XYZW
;
398 if (inst
->opcode
!= BRW_OPCODE_MOV
||
399 inst
->dst
.writemask
== WRITEMASK_XYZW
||
400 inst
->src
[0].file
!= IMM
)
403 int vf
= brw_float_to_vf(inst
->src
[0].fixed_hw_reg
.dw1
.f
);
407 if ((inst
->dst
.writemask
& WRITEMASK_X
) != 0)
409 if ((inst
->dst
.writemask
& WRITEMASK_Y
) != 0)
411 if ((inst
->dst
.writemask
& WRITEMASK_Z
) != 0)
413 if ((inst
->dst
.writemask
& WRITEMASK_W
) != 0)
416 imm_inst
[inst_count
++] = inst
;
418 remaining_channels
&= ~inst
->dst
.writemask
;
419 if (remaining_channels
== 0) {
420 vec4_instruction
*mov
= MOV(inst
->dst
, imm
);
421 mov
->dst
.type
= BRW_REGISTER_TYPE_F
;
422 mov
->dst
.writemask
= WRITEMASK_XYZW
;
423 inst
->insert_after(block
, mov
);
426 for (int i
= 0; i
< inst_count
; i
++) {
427 imm_inst
[i
]->remove(block
);
434 invalidate_live_intervals();
439 /* Replaces unused channels of a swizzle with channels that are used.
441 * For instance, this pass transforms
443 * mov vgrf4.yz, vgrf5.wxzy
447 * mov vgrf4.yz, vgrf5.xxzx
449 * This eliminates false uses of some channels, letting dead code elimination
450 * remove the instructions that wrote them.
453 vec4_visitor::opt_reduce_swizzle()
455 bool progress
= false;
457 foreach_block_and_inst_safe(block
, vec4_instruction
, inst
, cfg
) {
458 if (inst
->dst
.file
== BAD_FILE
|| inst
->dst
.file
== HW_REG
)
463 /* Determine which channels of the sources are read. */
464 switch (inst
->opcode
) {
465 case VEC4_OPCODE_PACK_BYTES
:
472 case BRW_OPCODE_DPH
: /* FINISHME: DPH reads only three channels of src0,
473 * but all four of src1.
493 swizzle
[0] = inst
->dst
.writemask
& WRITEMASK_X
? 0 : -1;
494 swizzle
[1] = inst
->dst
.writemask
& WRITEMASK_Y
? 1 : -1;
495 swizzle
[2] = inst
->dst
.writemask
& WRITEMASK_Z
? 2 : -1;
496 swizzle
[3] = inst
->dst
.writemask
& WRITEMASK_W
? 3 : -1;
500 /* Resolve unread channels (-1) by assigning them the swizzle of the
501 * first channel that is used.
503 int first_used_channel
= 0;
504 for (int i
= 0; i
< 4; i
++) {
505 if (swizzle
[i
] != -1) {
506 first_used_channel
= swizzle
[i
];
510 for (int i
= 0; i
< 4; i
++) {
511 if (swizzle
[i
] == -1) {
512 swizzle
[i
] = first_used_channel
;
516 /* Update sources' swizzles. */
517 for (int i
= 0; i
< 3; i
++) {
518 if (inst
->src
[i
].file
!= GRF
&&
519 inst
->src
[i
].file
!= ATTR
&&
520 inst
->src
[i
].file
!= UNIFORM
)
524 for (int j
= 0; j
< 4; j
++) {
525 swiz
[j
] = BRW_GET_SWZ(inst
->src
[i
].swizzle
, swizzle
[j
]);
528 unsigned new_swizzle
= BRW_SWIZZLE4(swiz
[0], swiz
[1], swiz
[2], swiz
[3]);
529 if (inst
->src
[i
].swizzle
!= new_swizzle
) {
530 inst
->src
[i
].swizzle
= new_swizzle
;
537 invalidate_live_intervals();
543 vec4_visitor::split_uniform_registers()
545 /* Prior to this, uniforms have been in an array sized according to
546 * the number of vector uniforms present, sparsely filled (so an
547 * aggregate results in reg indices being skipped over). Now we're
548 * going to cut those aggregates up so each .reg index is one
549 * vector. The goal is to make elimination of unused uniform
550 * components easier later.
552 foreach_block_and_inst(block
, vec4_instruction
, inst
, cfg
) {
553 for (int i
= 0 ; i
< 3; i
++) {
554 if (inst
->src
[i
].file
!= UNIFORM
)
557 assert(!inst
->src
[i
].reladdr
);
559 inst
->src
[i
].reg
+= inst
->src
[i
].reg_offset
;
560 inst
->src
[i
].reg_offset
= 0;
564 /* Update that everything is now vector-sized. */
565 for (int i
= 0; i
< this->uniforms
; i
++) {
566 this->uniform_size
[i
] = 1;
571 vec4_visitor::pack_uniform_registers()
573 bool uniform_used
[this->uniforms
];
574 int new_loc
[this->uniforms
];
575 int new_chan
[this->uniforms
];
577 memset(uniform_used
, 0, sizeof(uniform_used
));
578 memset(new_loc
, 0, sizeof(new_loc
));
579 memset(new_chan
, 0, sizeof(new_chan
));
581 /* Find which uniform vectors are actually used by the program. We
582 * expect unused vector elements when we've moved array access out
583 * to pull constants, and from some GLSL code generators like wine.
585 foreach_block_and_inst(block
, vec4_instruction
, inst
, cfg
) {
586 for (int i
= 0 ; i
< 3; i
++) {
587 if (inst
->src
[i
].file
!= UNIFORM
)
590 uniform_used
[inst
->src
[i
].reg
] = true;
594 int new_uniform_count
= 0;
596 /* Now, figure out a packing of the live uniform vectors into our
599 for (int src
= 0; src
< uniforms
; src
++) {
600 assert(src
< uniform_array_size
);
601 int size
= this->uniform_vector_size
[src
];
603 if (!uniform_used
[src
]) {
604 this->uniform_vector_size
[src
] = 0;
609 /* Find the lowest place we can slot this uniform in. */
610 for (dst
= 0; dst
< src
; dst
++) {
611 if (this->uniform_vector_size
[dst
] + size
<= 4)
620 new_chan
[src
] = this->uniform_vector_size
[dst
];
622 /* Move the references to the data */
623 for (int j
= 0; j
< size
; j
++) {
624 stage_prog_data
->param
[dst
* 4 + new_chan
[src
] + j
] =
625 stage_prog_data
->param
[src
* 4 + j
];
628 this->uniform_vector_size
[dst
] += size
;
629 this->uniform_vector_size
[src
] = 0;
632 new_uniform_count
= MAX2(new_uniform_count
, dst
+ 1);
635 this->uniforms
= new_uniform_count
;
637 /* Now, update the instructions for our repacked uniforms. */
638 foreach_block_and_inst(block
, vec4_instruction
, inst
, cfg
) {
639 for (int i
= 0 ; i
< 3; i
++) {
640 int src
= inst
->src
[i
].reg
;
642 if (inst
->src
[i
].file
!= UNIFORM
)
645 inst
->src
[i
].reg
= new_loc
[src
];
647 int sx
= BRW_GET_SWZ(inst
->src
[i
].swizzle
, 0) + new_chan
[src
];
648 int sy
= BRW_GET_SWZ(inst
->src
[i
].swizzle
, 1) + new_chan
[src
];
649 int sz
= BRW_GET_SWZ(inst
->src
[i
].swizzle
, 2) + new_chan
[src
];
650 int sw
= BRW_GET_SWZ(inst
->src
[i
].swizzle
, 3) + new_chan
[src
];
651 inst
->src
[i
].swizzle
= BRW_SWIZZLE4(sx
, sy
, sz
, sw
);
657 * Does algebraic optimizations (0 * a = 0, 1 * a = a, a + 0 = a).
659 * While GLSL IR also performs this optimization, we end up with it in
660 * our instruction stream for a couple of reasons. One is that we
661 * sometimes generate silly instructions, for example in array access
662 * where we'll generate "ADD offset, index, base" even if base is 0.
663 * The other is that GLSL IR's constant propagation doesn't track the
664 * components of aggregates, so some VS patterns (initialize matrix to
665 * 0, accumulate in vertex blending factors) end up breaking down to
666 * instructions involving 0.
669 vec4_visitor::opt_algebraic()
671 bool progress
= false;
673 foreach_block_and_inst(block
, vec4_instruction
, inst
, cfg
) {
674 switch (inst
->opcode
) {
676 if (inst
->src
[0].file
!= IMM
)
679 if (inst
->saturate
) {
680 if (inst
->dst
.type
!= inst
->src
[0].type
)
681 assert(!"unimplemented: saturate mixed types");
683 if (brw_saturate_immediate(inst
->dst
.type
,
684 &inst
->src
[0].fixed_hw_reg
)) {
685 inst
->saturate
= false;
691 case VEC4_OPCODE_UNPACK_UNIFORM
:
692 if (inst
->src
[0].file
!= UNIFORM
) {
693 inst
->opcode
= BRW_OPCODE_MOV
;
699 if (inst
->src
[1].is_zero()) {
700 inst
->opcode
= BRW_OPCODE_MOV
;
701 inst
->src
[1] = src_reg();
707 if (inst
->src
[1].is_zero()) {
708 inst
->opcode
= BRW_OPCODE_MOV
;
709 switch (inst
->src
[0].type
) {
710 case BRW_REGISTER_TYPE_F
:
711 inst
->src
[0] = src_reg(0.0f
);
713 case BRW_REGISTER_TYPE_D
:
714 inst
->src
[0] = src_reg(0);
716 case BRW_REGISTER_TYPE_UD
:
717 inst
->src
[0] = src_reg(0u);
720 unreachable("not reached");
722 inst
->src
[1] = src_reg();
724 } else if (inst
->src
[1].is_one()) {
725 inst
->opcode
= BRW_OPCODE_MOV
;
726 inst
->src
[1] = src_reg();
731 if (inst
->conditional_mod
== BRW_CONDITIONAL_GE
&&
733 inst
->src
[0].negate
&&
734 inst
->src
[1].is_zero()) {
735 inst
->src
[0].abs
= false;
736 inst
->src
[0].negate
= false;
737 inst
->conditional_mod
= BRW_CONDITIONAL_Z
;
742 case SHADER_OPCODE_RCP
: {
743 vec4_instruction
*prev
= (vec4_instruction
*)inst
->prev
;
744 if (prev
->opcode
== SHADER_OPCODE_SQRT
) {
745 if (inst
->src
[0].equals(src_reg(prev
->dst
))) {
746 inst
->opcode
= SHADER_OPCODE_RSQ
;
747 inst
->src
[0] = prev
->src
[0];
759 invalidate_live_intervals();
765 * Only a limited number of hardware registers may be used for push
766 * constants, so this turns access to the overflowed constants into
770 vec4_visitor::move_push_constants_to_pull_constants()
772 int pull_constant_loc
[this->uniforms
];
774 /* Only allow 32 registers (256 uniform components) as push constants,
775 * which is the limit on gen6.
777 * If changing this value, note the limitation about total_regs in
780 int max_uniform_components
= 32 * 8;
781 if (this->uniforms
* 4 <= max_uniform_components
)
784 /* Make some sort of choice as to which uniforms get sent to pull
785 * constants. We could potentially do something clever here like
786 * look for the most infrequently used uniform vec4s, but leave
789 for (int i
= 0; i
< this->uniforms
* 4; i
+= 4) {
790 pull_constant_loc
[i
/ 4] = -1;
792 if (i
>= max_uniform_components
) {
793 const gl_constant_value
**values
= &stage_prog_data
->param
[i
];
795 /* Try to find an existing copy of this uniform in the pull
796 * constants if it was part of an array access already.
798 for (unsigned int j
= 0; j
< stage_prog_data
->nr_pull_params
; j
+= 4) {
801 for (matches
= 0; matches
< 4; matches
++) {
802 if (stage_prog_data
->pull_param
[j
+ matches
] != values
[matches
])
807 pull_constant_loc
[i
/ 4] = j
/ 4;
812 if (pull_constant_loc
[i
/ 4] == -1) {
813 assert(stage_prog_data
->nr_pull_params
% 4 == 0);
814 pull_constant_loc
[i
/ 4] = stage_prog_data
->nr_pull_params
/ 4;
816 for (int j
= 0; j
< 4; j
++) {
817 stage_prog_data
->pull_param
[stage_prog_data
->nr_pull_params
++] =
824 /* Now actually rewrite usage of the things we've moved to pull
827 foreach_block_and_inst_safe(block
, vec4_instruction
, inst
, cfg
) {
828 for (int i
= 0 ; i
< 3; i
++) {
829 if (inst
->src
[i
].file
!= UNIFORM
||
830 pull_constant_loc
[inst
->src
[i
].reg
] == -1)
833 int uniform
= inst
->src
[i
].reg
;
835 dst_reg temp
= dst_reg(this, glsl_type::vec4_type
);
837 emit_pull_constant_load(block
, inst
, temp
, inst
->src
[i
],
838 pull_constant_loc
[uniform
]);
840 inst
->src
[i
].file
= temp
.file
;
841 inst
->src
[i
].reg
= temp
.reg
;
842 inst
->src
[i
].reg_offset
= temp
.reg_offset
;
843 inst
->src
[i
].reladdr
= NULL
;
847 /* Repack push constants to remove the now-unused ones. */
848 pack_uniform_registers();
851 /* Conditions for which we want to avoid setting the dependency control bits */
853 vec4_visitor::is_dep_ctrl_unsafe(const vec4_instruction
*inst
)
855 #define IS_DWORD(reg) \
856 (reg.type == BRW_REGISTER_TYPE_UD || \
857 reg.type == BRW_REGISTER_TYPE_D)
859 /* "When source or destination datatype is 64b or operation is integer DWord
860 * multiply, DepCtrl must not be used."
861 * May apply to future SoCs as well.
863 if (brw
->is_cherryview
) {
864 if (inst
->opcode
== BRW_OPCODE_MUL
&&
865 IS_DWORD(inst
->src
[0]) &&
866 IS_DWORD(inst
->src
[1]))
872 if (inst
->opcode
== BRW_OPCODE_F32TO16
)
878 * In the presence of send messages, totally interrupt dependency
879 * control. They're long enough that the chance of dependency
880 * control around them just doesn't matter.
883 * From the Ivy Bridge PRM, volume 4 part 3.7, page 80:
884 * When a sequence of NoDDChk and NoDDClr are used, the last instruction that
885 * completes the scoreboard clear must have a non-zero execution mask. This
886 * means, if any kind of predication can change the execution mask or channel
887 * enable of the last instruction, the optimization must be avoided. This is
888 * to avoid instructions being shot down the pipeline when no writes are
892 * Dependency control does not work well over math instructions.
893 * NB: Discovered empirically
895 return (inst
->mlen
|| inst
->predicate
|| inst
->is_math());
899 * Sets the dependency control fields on instructions after register
900 * allocation and before the generator is run.
902 * When you have a sequence of instructions like:
904 * DP4 temp.x vertex uniform[0]
905 * DP4 temp.y vertex uniform[0]
906 * DP4 temp.z vertex uniform[0]
907 * DP4 temp.w vertex uniform[0]
909 * The hardware doesn't know that it can actually run the later instructions
910 * while the previous ones are in flight, producing stalls. However, we have
911 * manual fields we can set in the instructions that let it do so.
914 vec4_visitor::opt_set_dependency_control()
916 vec4_instruction
*last_grf_write
[BRW_MAX_GRF
];
917 uint8_t grf_channels_written
[BRW_MAX_GRF
];
918 vec4_instruction
*last_mrf_write
[BRW_MAX_GRF
];
919 uint8_t mrf_channels_written
[BRW_MAX_GRF
];
921 assert(prog_data
->total_grf
||
922 !"Must be called after register allocation");
924 foreach_block (block
, cfg
) {
925 memset(last_grf_write
, 0, sizeof(last_grf_write
));
926 memset(last_mrf_write
, 0, sizeof(last_mrf_write
));
928 foreach_inst_in_block (vec4_instruction
, inst
, block
) {
929 /* If we read from a register that we were doing dependency control
930 * on, don't do dependency control across the read.
932 for (int i
= 0; i
< 3; i
++) {
933 int reg
= inst
->src
[i
].reg
+ inst
->src
[i
].reg_offset
;
934 if (inst
->src
[i
].file
== GRF
) {
935 last_grf_write
[reg
] = NULL
;
936 } else if (inst
->src
[i
].file
== HW_REG
) {
937 memset(last_grf_write
, 0, sizeof(last_grf_write
));
940 assert(inst
->src
[i
].file
!= MRF
);
943 if (is_dep_ctrl_unsafe(inst
)) {
944 memset(last_grf_write
, 0, sizeof(last_grf_write
));
945 memset(last_mrf_write
, 0, sizeof(last_mrf_write
));
949 /* Now, see if we can do dependency control for this instruction
950 * against a previous one writing to its destination.
952 int reg
= inst
->dst
.reg
+ inst
->dst
.reg_offset
;
953 if (inst
->dst
.file
== GRF
) {
954 if (last_grf_write
[reg
] &&
955 !(inst
->dst
.writemask
& grf_channels_written
[reg
])) {
956 last_grf_write
[reg
]->no_dd_clear
= true;
957 inst
->no_dd_check
= true;
959 grf_channels_written
[reg
] = 0;
962 last_grf_write
[reg
] = inst
;
963 grf_channels_written
[reg
] |= inst
->dst
.writemask
;
964 } else if (inst
->dst
.file
== MRF
) {
965 if (last_mrf_write
[reg
] &&
966 !(inst
->dst
.writemask
& mrf_channels_written
[reg
])) {
967 last_mrf_write
[reg
]->no_dd_clear
= true;
968 inst
->no_dd_check
= true;
970 mrf_channels_written
[reg
] = 0;
973 last_mrf_write
[reg
] = inst
;
974 mrf_channels_written
[reg
] |= inst
->dst
.writemask
;
975 } else if (inst
->dst
.reg
== HW_REG
) {
976 if (inst
->dst
.fixed_hw_reg
.file
== BRW_GENERAL_REGISTER_FILE
)
977 memset(last_grf_write
, 0, sizeof(last_grf_write
));
978 if (inst
->dst
.fixed_hw_reg
.file
== BRW_MESSAGE_REGISTER_FILE
)
979 memset(last_mrf_write
, 0, sizeof(last_mrf_write
));
986 vec4_instruction::can_reswizzle(int dst_writemask
,
990 /* If this instruction sets anything not referenced by swizzle, then we'd
991 * totally break it when we reswizzle.
993 if (dst
.writemask
& ~swizzle_mask
)
1003 * For any channels in the swizzle's source that were populated by this
1004 * instruction, rewrite the instruction to put the appropriate result directly
1005 * in those channels.
1007 * e.g. for swizzle=yywx, MUL a.xy b c -> MUL a.yy_x b.yy z.yy_x
1010 vec4_instruction::reswizzle(int dst_writemask
, int swizzle
)
1012 int new_writemask
= 0;
1013 int new_swizzle
[4] = { 0 };
1015 /* Dot product instructions write a single result into all channels. */
1016 if (opcode
!= BRW_OPCODE_DP4
&& opcode
!= BRW_OPCODE_DPH
&&
1017 opcode
!= BRW_OPCODE_DP3
&& opcode
!= BRW_OPCODE_DP2
) {
1018 for (int i
= 0; i
< 3; i
++) {
1019 if (src
[i
].file
== BAD_FILE
|| src
[i
].file
== IMM
)
1022 /* Destination write mask doesn't correspond to source swizzle for the
1023 * pack_bytes instruction.
1025 if (opcode
== VEC4_OPCODE_PACK_BYTES
)
1028 for (int c
= 0; c
< 4; c
++) {
1029 new_swizzle
[c
] = BRW_GET_SWZ(src
[i
].swizzle
, BRW_GET_SWZ(swizzle
, c
));
1032 src
[i
].swizzle
= BRW_SWIZZLE4(new_swizzle
[0], new_swizzle
[1],
1033 new_swizzle
[2], new_swizzle
[3]);
1037 for (int c
= 0; c
< 4; c
++) {
1038 int bit
= 1 << BRW_GET_SWZ(swizzle
, c
);
1039 /* Skip components of the swizzle not used by the dst. */
1040 if (!(dst_writemask
& (1 << c
)))
1042 /* If we were populating this component, then populate the
1043 * corresponding channel of the new dst.
1045 if (dst
.writemask
& bit
)
1046 new_writemask
|= (1 << c
);
1048 dst
.writemask
= new_writemask
;
1052 * Tries to reduce extra MOV instructions by taking temporary GRFs that get
1053 * just written and then MOVed into another reg and making the original write
1054 * of the GRF write directly to the final destination instead.
1057 vec4_visitor::opt_register_coalesce()
1059 bool progress
= false;
1062 calculate_live_intervals();
1064 foreach_block_and_inst_safe (block
, vec4_instruction
, inst
, cfg
) {
1068 if (inst
->opcode
!= BRW_OPCODE_MOV
||
1069 (inst
->dst
.file
!= GRF
&& inst
->dst
.file
!= MRF
) ||
1071 inst
->src
[0].file
!= GRF
||
1072 inst
->dst
.type
!= inst
->src
[0].type
||
1073 inst
->src
[0].abs
|| inst
->src
[0].negate
|| inst
->src
[0].reladdr
)
1076 bool to_mrf
= (inst
->dst
.file
== MRF
);
1078 /* Can't coalesce this GRF if someone else was going to
1081 if (this->virtual_grf_end
[inst
->src
[0].reg
* 4 + 0] > ip
||
1082 this->virtual_grf_end
[inst
->src
[0].reg
* 4 + 1] > ip
||
1083 this->virtual_grf_end
[inst
->src
[0].reg
* 4 + 2] > ip
||
1084 this->virtual_grf_end
[inst
->src
[0].reg
* 4 + 3] > ip
)
1087 /* We need to check interference with the final destination between this
1088 * instruction and the earliest instruction involved in writing the GRF
1089 * we're eliminating. To do that, keep track of which of our source
1090 * channels we've seen initialized.
1092 bool chans_needed
[4] = {false, false, false, false};
1093 int chans_remaining
= 0;
1094 int swizzle_mask
= 0;
1095 for (int i
= 0; i
< 4; i
++) {
1096 int chan
= BRW_GET_SWZ(inst
->src
[0].swizzle
, i
);
1098 if (!(inst
->dst
.writemask
& (1 << i
)))
1101 swizzle_mask
|= (1 << chan
);
1103 if (!chans_needed
[chan
]) {
1104 chans_needed
[chan
] = true;
1109 /* Now walk up the instruction stream trying to see if we can rewrite
1110 * everything writing to the temporary to write into the destination
1113 vec4_instruction
*_scan_inst
= (vec4_instruction
*)inst
->prev
;
1114 foreach_inst_in_block_reverse_starting_from(vec4_instruction
, scan_inst
,
1116 _scan_inst
= scan_inst
;
1118 if (scan_inst
->dst
.file
== GRF
&&
1119 scan_inst
->dst
.reg
== inst
->src
[0].reg
&&
1120 scan_inst
->dst
.reg_offset
== inst
->src
[0].reg_offset
) {
1121 /* Found something writing to the reg we want to coalesce away. */
1123 /* SEND instructions can't have MRF as a destination. */
1124 if (scan_inst
->mlen
)
1127 if (brw
->gen
== 6) {
1128 /* gen6 math instructions must have the destination be
1129 * GRF, so no compute-to-MRF for them.
1131 if (scan_inst
->is_math()) {
1137 /* If we can't handle the swizzle, bail. */
1138 if (!scan_inst
->can_reswizzle(inst
->dst
.writemask
,
1139 inst
->src
[0].swizzle
,
1144 /* Mark which channels we found unconditional writes for. */
1145 if (!scan_inst
->predicate
) {
1146 for (int i
= 0; i
< 4; i
++) {
1147 if (scan_inst
->dst
.writemask
& (1 << i
) &&
1149 chans_needed
[i
] = false;
1155 if (chans_remaining
== 0)
1159 /* You can't read from an MRF, so if someone else reads our MRF's
1160 * source GRF that we wanted to rewrite, that stops us. If it's a
1161 * GRF we're trying to coalesce to, we don't actually handle
1162 * rewriting sources so bail in that case as well.
1164 bool interfered
= false;
1165 for (int i
= 0; i
< 3; i
++) {
1166 if (scan_inst
->src
[i
].file
== GRF
&&
1167 scan_inst
->src
[i
].reg
== inst
->src
[0].reg
&&
1168 scan_inst
->src
[i
].reg_offset
== inst
->src
[0].reg_offset
) {
1175 /* If somebody else writes our destination here, we can't coalesce
1178 if (scan_inst
->dst
.file
== inst
->dst
.file
&&
1179 scan_inst
->dst
.reg
== inst
->dst
.reg
) {
1183 /* Check for reads of the register we're trying to coalesce into. We
1184 * can't go rewriting instructions above that to put some other value
1185 * in the register instead.
1187 if (to_mrf
&& scan_inst
->mlen
> 0) {
1188 if (inst
->dst
.reg
>= scan_inst
->base_mrf
&&
1189 inst
->dst
.reg
< scan_inst
->base_mrf
+ scan_inst
->mlen
) {
1193 for (int i
= 0; i
< 3; i
++) {
1194 if (scan_inst
->src
[i
].file
== inst
->dst
.file
&&
1195 scan_inst
->src
[i
].reg
== inst
->dst
.reg
&&
1196 scan_inst
->src
[i
].reg_offset
== inst
->src
[0].reg_offset
) {
1205 if (chans_remaining
== 0) {
1206 /* If we've made it here, we have an MOV we want to coalesce out, and
1207 * a scan_inst pointing to the earliest instruction involved in
1208 * computing the value. Now go rewrite the instruction stream
1211 vec4_instruction
*scan_inst
= _scan_inst
;
1212 while (scan_inst
!= inst
) {
1213 if (scan_inst
->dst
.file
== GRF
&&
1214 scan_inst
->dst
.reg
== inst
->src
[0].reg
&&
1215 scan_inst
->dst
.reg_offset
== inst
->src
[0].reg_offset
) {
1216 scan_inst
->reswizzle(inst
->dst
.writemask
,
1217 inst
->src
[0].swizzle
);
1218 scan_inst
->dst
.file
= inst
->dst
.file
;
1219 scan_inst
->dst
.reg
= inst
->dst
.reg
;
1220 scan_inst
->dst
.reg_offset
= inst
->dst
.reg_offset
;
1221 scan_inst
->saturate
|= inst
->saturate
;
1223 scan_inst
= (vec4_instruction
*)scan_inst
->next
;
1225 inst
->remove(block
);
1231 invalidate_live_intervals();
1237 * Splits virtual GRFs requesting more than one contiguous physical register.
1239 * We initially create large virtual GRFs for temporary structures, arrays,
1240 * and matrices, so that the dereference visitor functions can add reg_offsets
1241 * to work their way down to the actual member being accessed. But when it
1242 * comes to optimization, we'd like to treat each register as individual
1243 * storage if possible.
1245 * So far, the only thing that might prevent splitting is a send message from
1249 vec4_visitor::split_virtual_grfs()
1251 int num_vars
= this->alloc
.count
;
1252 int new_virtual_grf
[num_vars
];
1253 bool split_grf
[num_vars
];
1255 memset(new_virtual_grf
, 0, sizeof(new_virtual_grf
));
1257 /* Try to split anything > 0 sized. */
1258 for (int i
= 0; i
< num_vars
; i
++) {
1259 split_grf
[i
] = this->alloc
.sizes
[i
] != 1;
1262 /* Check that the instructions are compatible with the registers we're trying
1265 foreach_block_and_inst(block
, vec4_instruction
, inst
, cfg
) {
1266 /* If there's a SEND message loading from a GRF on gen7+, it needs to be
1269 if (inst
->is_send_from_grf()) {
1270 for (int i
= 0; i
< 3; i
++) {
1271 if (inst
->src
[i
].file
== GRF
) {
1272 split_grf
[inst
->src
[i
].reg
] = false;
1278 /* Allocate new space for split regs. Note that the virtual
1279 * numbers will be contiguous.
1281 for (int i
= 0; i
< num_vars
; i
++) {
1285 new_virtual_grf
[i
] = alloc
.allocate(1);
1286 for (unsigned j
= 2; j
< this->alloc
.sizes
[i
]; j
++) {
1287 unsigned reg
= alloc
.allocate(1);
1288 assert(reg
== new_virtual_grf
[i
] + j
- 1);
1291 this->alloc
.sizes
[i
] = 1;
1294 foreach_block_and_inst(block
, vec4_instruction
, inst
, cfg
) {
1295 if (inst
->dst
.file
== GRF
&& split_grf
[inst
->dst
.reg
] &&
1296 inst
->dst
.reg_offset
!= 0) {
1297 inst
->dst
.reg
= (new_virtual_grf
[inst
->dst
.reg
] +
1298 inst
->dst
.reg_offset
- 1);
1299 inst
->dst
.reg_offset
= 0;
1301 for (int i
= 0; i
< 3; i
++) {
1302 if (inst
->src
[i
].file
== GRF
&& split_grf
[inst
->src
[i
].reg
] &&
1303 inst
->src
[i
].reg_offset
!= 0) {
1304 inst
->src
[i
].reg
= (new_virtual_grf
[inst
->src
[i
].reg
] +
1305 inst
->src
[i
].reg_offset
- 1);
1306 inst
->src
[i
].reg_offset
= 0;
1310 invalidate_live_intervals();
1314 vec4_visitor::dump_instruction(backend_instruction
*be_inst
)
1316 dump_instruction(be_inst
, stderr
);
1320 vec4_visitor::dump_instruction(backend_instruction
*be_inst
, FILE *file
)
1322 vec4_instruction
*inst
= (vec4_instruction
*)be_inst
;
1324 if (inst
->predicate
) {
1325 fprintf(file
, "(%cf0.%d) ",
1326 inst
->predicate_inverse
? '-' : '+',
1330 fprintf(file
, "%s", brw_instruction_name(inst
->opcode
));
1331 if (inst
->conditional_mod
) {
1332 fprintf(file
, "%s", conditional_modifier
[inst
->conditional_mod
]);
1333 if (!inst
->predicate
&&
1334 (brw
->gen
< 5 || (inst
->opcode
!= BRW_OPCODE_SEL
&&
1335 inst
->opcode
!= BRW_OPCODE_IF
&&
1336 inst
->opcode
!= BRW_OPCODE_WHILE
))) {
1337 fprintf(file
, ".f0.%d", inst
->flag_subreg
);
1342 switch (inst
->dst
.file
) {
1344 fprintf(file
, "vgrf%d.%d", inst
->dst
.reg
, inst
->dst
.reg_offset
);
1347 fprintf(file
, "m%d", inst
->dst
.reg
);
1350 if (inst
->dst
.fixed_hw_reg
.file
== BRW_ARCHITECTURE_REGISTER_FILE
) {
1351 switch (inst
->dst
.fixed_hw_reg
.nr
) {
1353 fprintf(file
, "null");
1355 case BRW_ARF_ADDRESS
:
1356 fprintf(file
, "a0.%d", inst
->dst
.fixed_hw_reg
.subnr
);
1358 case BRW_ARF_ACCUMULATOR
:
1359 fprintf(file
, "acc%d", inst
->dst
.fixed_hw_reg
.subnr
);
1362 fprintf(file
, "f%d.%d", inst
->dst
.fixed_hw_reg
.nr
& 0xf,
1363 inst
->dst
.fixed_hw_reg
.subnr
);
1366 fprintf(file
, "arf%d.%d", inst
->dst
.fixed_hw_reg
.nr
& 0xf,
1367 inst
->dst
.fixed_hw_reg
.subnr
);
1371 fprintf(file
, "hw_reg%d", inst
->dst
.fixed_hw_reg
.nr
);
1373 if (inst
->dst
.fixed_hw_reg
.subnr
)
1374 fprintf(file
, "+%d", inst
->dst
.fixed_hw_reg
.subnr
);
1377 fprintf(file
, "(null)");
1380 fprintf(file
, "???");
1383 if (inst
->dst
.writemask
!= WRITEMASK_XYZW
) {
1385 if (inst
->dst
.writemask
& 1)
1387 if (inst
->dst
.writemask
& 2)
1389 if (inst
->dst
.writemask
& 4)
1391 if (inst
->dst
.writemask
& 8)
1394 fprintf(file
, ":%s", brw_reg_type_letters(inst
->dst
.type
));
1396 if (inst
->src
[0].file
!= BAD_FILE
)
1397 fprintf(file
, ", ");
1399 for (int i
= 0; i
< 3 && inst
->src
[i
].file
!= BAD_FILE
; i
++) {
1400 if (inst
->src
[i
].negate
)
1402 if (inst
->src
[i
].abs
)
1404 switch (inst
->src
[i
].file
) {
1406 fprintf(file
, "vgrf%d", inst
->src
[i
].reg
);
1409 fprintf(file
, "attr%d", inst
->src
[i
].reg
);
1412 fprintf(file
, "u%d", inst
->src
[i
].reg
);
1415 switch (inst
->src
[i
].type
) {
1416 case BRW_REGISTER_TYPE_F
:
1417 fprintf(file
, "%fF", inst
->src
[i
].fixed_hw_reg
.dw1
.f
);
1419 case BRW_REGISTER_TYPE_D
:
1420 fprintf(file
, "%dD", inst
->src
[i
].fixed_hw_reg
.dw1
.d
);
1422 case BRW_REGISTER_TYPE_UD
:
1423 fprintf(file
, "%uU", inst
->src
[i
].fixed_hw_reg
.dw1
.ud
);
1425 case BRW_REGISTER_TYPE_VF
:
1426 fprintf(file
, "[%-gF, %-gF, %-gF, %-gF]",
1427 brw_vf_to_float((inst
->src
[i
].fixed_hw_reg
.dw1
.ud
>> 0) & 0xff),
1428 brw_vf_to_float((inst
->src
[i
].fixed_hw_reg
.dw1
.ud
>> 8) & 0xff),
1429 brw_vf_to_float((inst
->src
[i
].fixed_hw_reg
.dw1
.ud
>> 16) & 0xff),
1430 brw_vf_to_float((inst
->src
[i
].fixed_hw_reg
.dw1
.ud
>> 24) & 0xff));
1433 fprintf(file
, "???");
1438 if (inst
->src
[i
].fixed_hw_reg
.negate
)
1440 if (inst
->src
[i
].fixed_hw_reg
.abs
)
1442 if (inst
->src
[i
].fixed_hw_reg
.file
== BRW_ARCHITECTURE_REGISTER_FILE
) {
1443 switch (inst
->src
[i
].fixed_hw_reg
.nr
) {
1445 fprintf(file
, "null");
1447 case BRW_ARF_ADDRESS
:
1448 fprintf(file
, "a0.%d", inst
->src
[i
].fixed_hw_reg
.subnr
);
1450 case BRW_ARF_ACCUMULATOR
:
1451 fprintf(file
, "acc%d", inst
->src
[i
].fixed_hw_reg
.subnr
);
1454 fprintf(file
, "f%d.%d", inst
->src
[i
].fixed_hw_reg
.nr
& 0xf,
1455 inst
->src
[i
].fixed_hw_reg
.subnr
);
1458 fprintf(file
, "arf%d.%d", inst
->src
[i
].fixed_hw_reg
.nr
& 0xf,
1459 inst
->src
[i
].fixed_hw_reg
.subnr
);
1463 fprintf(file
, "hw_reg%d", inst
->src
[i
].fixed_hw_reg
.nr
);
1465 if (inst
->src
[i
].fixed_hw_reg
.subnr
)
1466 fprintf(file
, "+%d", inst
->src
[i
].fixed_hw_reg
.subnr
);
1467 if (inst
->src
[i
].fixed_hw_reg
.abs
)
1471 fprintf(file
, "(null)");
1474 fprintf(file
, "???");
1478 /* Don't print .0; and only VGRFs have reg_offsets and sizes */
1479 if (inst
->src
[i
].reg_offset
!= 0 &&
1480 inst
->src
[i
].file
== GRF
&&
1481 alloc
.sizes
[inst
->src
[i
].reg
] != 1)
1482 fprintf(file
, ".%d", inst
->src
[i
].reg_offset
);
1484 if (inst
->src
[i
].file
!= IMM
) {
1485 static const char *chans
[4] = {"x", "y", "z", "w"};
1487 for (int c
= 0; c
< 4; c
++) {
1488 fprintf(file
, "%s", chans
[BRW_GET_SWZ(inst
->src
[i
].swizzle
, c
)]);
1492 if (inst
->src
[i
].abs
)
1495 if (inst
->src
[i
].file
!= IMM
) {
1496 fprintf(file
, ":%s", brw_reg_type_letters(inst
->src
[i
].type
));
1499 if (i
< 2 && inst
->src
[i
+ 1].file
!= BAD_FILE
)
1500 fprintf(file
, ", ");
1503 fprintf(file
, "\n");
1507 static inline struct brw_reg
1508 attribute_to_hw_reg(int attr
, bool interleaved
)
1511 return stride(brw_vec4_grf(attr
/ 2, (attr
% 2) * 4), 0, 4, 1);
1513 return brw_vec8_grf(attr
, 0);
1518 * Replace each register of type ATTR in this->instructions with a reference
1519 * to a fixed HW register.
1521 * If interleaved is true, then each attribute takes up half a register, with
1522 * register N containing attribute 2*N in its first half and attribute 2*N+1
1523 * in its second half (this corresponds to the payload setup used by geometry
1524 * shaders in "single" or "dual instanced" dispatch mode). If interleaved is
1525 * false, then each attribute takes up a whole register, with register N
1526 * containing attribute N (this corresponds to the payload setup used by
1527 * vertex shaders, and by geometry shaders in "dual object" dispatch mode).
1530 vec4_visitor::lower_attributes_to_hw_regs(const int *attribute_map
,
1533 foreach_block_and_inst(block
, vec4_instruction
, inst
, cfg
) {
1534 /* We have to support ATTR as a destination for GL_FIXED fixup. */
1535 if (inst
->dst
.file
== ATTR
) {
1536 int grf
= attribute_map
[inst
->dst
.reg
+ inst
->dst
.reg_offset
];
1538 /* All attributes used in the shader need to have been assigned a
1539 * hardware register by the caller
1543 struct brw_reg reg
= attribute_to_hw_reg(grf
, interleaved
);
1544 reg
.type
= inst
->dst
.type
;
1545 reg
.dw1
.bits
.writemask
= inst
->dst
.writemask
;
1547 inst
->dst
.file
= HW_REG
;
1548 inst
->dst
.fixed_hw_reg
= reg
;
1551 for (int i
= 0; i
< 3; i
++) {
1552 if (inst
->src
[i
].file
!= ATTR
)
1555 int grf
= attribute_map
[inst
->src
[i
].reg
+ inst
->src
[i
].reg_offset
];
1557 /* All attributes used in the shader need to have been assigned a
1558 * hardware register by the caller
1562 struct brw_reg reg
= attribute_to_hw_reg(grf
, interleaved
);
1563 reg
.dw1
.bits
.swizzle
= inst
->src
[i
].swizzle
;
1564 reg
.type
= inst
->src
[i
].type
;
1565 if (inst
->src
[i
].abs
)
1567 if (inst
->src
[i
].negate
)
1570 inst
->src
[i
].file
= HW_REG
;
1571 inst
->src
[i
].fixed_hw_reg
= reg
;
1577 vec4_vs_visitor::setup_attributes(int payload_reg
)
1580 int attribute_map
[VERT_ATTRIB_MAX
+ 1];
1581 memset(attribute_map
, 0, sizeof(attribute_map
));
1584 for (int i
= 0; i
< VERT_ATTRIB_MAX
; i
++) {
1585 if (vs_prog_data
->inputs_read
& BITFIELD64_BIT(i
)) {
1586 attribute_map
[i
] = payload_reg
+ nr_attributes
;
1591 /* VertexID is stored by the VF as the last vertex element, but we
1592 * don't represent it with a flag in inputs_read, so we call it
1595 if (vs_prog_data
->uses_vertexid
|| vs_prog_data
->uses_instanceid
) {
1596 attribute_map
[VERT_ATTRIB_MAX
] = payload_reg
+ nr_attributes
;
1600 lower_attributes_to_hw_regs(attribute_map
, false /* interleaved */);
1602 /* The BSpec says we always have to read at least one thing from
1603 * the VF, and it appears that the hardware wedges otherwise.
1605 if (nr_attributes
== 0)
1608 prog_data
->urb_read_length
= (nr_attributes
+ 1) / 2;
1610 unsigned vue_entries
=
1611 MAX2(nr_attributes
, prog_data
->vue_map
.num_slots
);
1614 prog_data
->urb_entry_size
= ALIGN(vue_entries
, 8) / 8;
1616 prog_data
->urb_entry_size
= ALIGN(vue_entries
, 4) / 4;
1618 return payload_reg
+ nr_attributes
;
1622 vec4_visitor::setup_uniforms(int reg
)
1624 prog_data
->base
.dispatch_grf_start_reg
= reg
;
1626 /* The pre-gen6 VS requires that some push constants get loaded no
1627 * matter what, or the GPU would hang.
1629 if (brw
->gen
< 6 && this->uniforms
== 0) {
1630 assert(this->uniforms
< this->uniform_array_size
);
1631 this->uniform_vector_size
[this->uniforms
] = 1;
1633 stage_prog_data
->param
=
1634 reralloc(NULL
, stage_prog_data
->param
, const gl_constant_value
*, 4);
1635 for (unsigned int i
= 0; i
< 4; i
++) {
1636 unsigned int slot
= this->uniforms
* 4 + i
;
1637 static gl_constant_value zero
= { 0.0 };
1638 stage_prog_data
->param
[slot
] = &zero
;
1644 reg
+= ALIGN(uniforms
, 2) / 2;
1647 stage_prog_data
->nr_params
= this->uniforms
* 4;
1649 prog_data
->base
.curb_read_length
=
1650 reg
- prog_data
->base
.dispatch_grf_start_reg
;
1656 vec4_vs_visitor::setup_payload(void)
1660 /* The payload always contains important data in g0, which contains
1661 * the URB handles that are passed on to the URB write at the end
1662 * of the thread. So, we always start push constants at g1.
1666 reg
= setup_uniforms(reg
);
1668 reg
= setup_attributes(reg
);
1670 this->first_non_payload_grf
= reg
;
1674 vec4_visitor::assign_binding_table_offsets()
1676 assign_common_binding_table_offsets(0);
1680 vec4_visitor::get_timestamp()
1682 assert(brw
->gen
>= 7);
1684 src_reg ts
= src_reg(brw_reg(BRW_ARCHITECTURE_REGISTER_FILE
,
1689 BRW_REGISTER_TYPE_UD
,
1690 BRW_VERTICAL_STRIDE_0
,
1692 BRW_HORIZONTAL_STRIDE_4
,
1696 dst_reg dst
= dst_reg(this, glsl_type::uvec4_type
);
1698 vec4_instruction
*mov
= emit(MOV(dst
, ts
));
1699 /* We want to read the 3 fields we care about (mostly field 0, but also 2)
1700 * even if it's not enabled in the dispatch.
1702 mov
->force_writemask_all
= true;
1704 return src_reg(dst
);
1708 vec4_visitor::emit_shader_time_begin()
1710 current_annotation
= "shader time start";
1711 shader_start_time
= get_timestamp();
1715 vec4_visitor::emit_shader_time_end()
1717 current_annotation
= "shader time end";
1718 src_reg shader_end_time
= get_timestamp();
1721 /* Check that there weren't any timestamp reset events (assuming these
1722 * were the only two timestamp reads that happened).
1724 src_reg reset_end
= shader_end_time
;
1725 reset_end
.swizzle
= BRW_SWIZZLE_ZZZZ
;
1726 vec4_instruction
*test
= emit(AND(dst_null_d(), reset_end
, src_reg(1u)));
1727 test
->conditional_mod
= BRW_CONDITIONAL_Z
;
1729 emit(IF(BRW_PREDICATE_NORMAL
));
1731 /* Take the current timestamp and get the delta. */
1732 shader_start_time
.negate
= true;
1733 dst_reg diff
= dst_reg(this, glsl_type::uint_type
);
1734 emit(ADD(diff
, shader_start_time
, shader_end_time
));
1736 /* If there were no instructions between the two timestamp gets, the diff
1737 * is 2 cycles. Remove that overhead, so I can forget about that when
1738 * trying to determine the time taken for single instructions.
1740 emit(ADD(diff
, src_reg(diff
), src_reg(-2u)));
1742 emit_shader_time_write(st_base
, src_reg(diff
));
1743 emit_shader_time_write(st_written
, src_reg(1u));
1744 emit(BRW_OPCODE_ELSE
);
1745 emit_shader_time_write(st_reset
, src_reg(1u));
1746 emit(BRW_OPCODE_ENDIF
);
1750 vec4_visitor::emit_shader_time_write(enum shader_time_shader_type type
,
1753 int shader_time_index
=
1754 brw_get_shader_time_index(brw
, shader_prog
, prog
, type
);
1757 dst_reg(this, glsl_type::get_array_instance(glsl_type::vec4_type
, 2));
1759 dst_reg offset
= dst
;
1763 offset
.type
= BRW_REGISTER_TYPE_UD
;
1764 emit(MOV(offset
, src_reg(shader_time_index
* SHADER_TIME_STRIDE
)));
1766 time
.type
= BRW_REGISTER_TYPE_UD
;
1767 emit(MOV(time
, src_reg(value
)));
1769 vec4_instruction
*inst
=
1770 emit(SHADER_OPCODE_SHADER_TIME_ADD
, dst_reg(), src_reg(dst
));
1777 sanity_param_count
= prog
->Parameters
->NumParameters
;
1779 if (INTEL_DEBUG
& DEBUG_SHADER_TIME
)
1780 emit_shader_time_begin();
1782 assign_binding_table_offsets();
1786 /* Generate VS IR for main(). (the visitor only descends into
1787 * functions called "main").
1790 visit_instructions(shader
->base
.ir
);
1792 emit_program_code();
1796 if (key
->userclip_active
&& !prog
->UsesClipDistanceOut
)
1797 setup_uniform_clipplane_values();
1803 /* Before any optimization, push array accesses out to scratch
1804 * space where we need them to be. This pass may allocate new
1805 * virtual GRFs, so we want to do it early. It also makes sure
1806 * that we have reladdr computations available for CSE, since we'll
1807 * often do repeated subexpressions for those.
1810 move_grf_array_access_to_scratch();
1811 move_uniform_array_access_to_pull_constants();
1813 /* The ARB_vertex_program frontend emits pull constant loads directly
1814 * rather than using reladdr, so we don't need to walk through all the
1815 * instructions looking for things to move. There isn't anything.
1817 * We do still need to split things to vec4 size.
1819 split_uniform_registers();
1821 pack_uniform_registers();
1822 move_push_constants_to_pull_constants();
1823 split_virtual_grfs();
1825 const char *stage_name
= stage
== MESA_SHADER_GEOMETRY
? "gs" : "vs";
1827 #define OPT(pass, args...) ({ \
1829 bool this_progress = pass(args); \
1831 if (unlikely(INTEL_DEBUG & DEBUG_OPTIMIZER) && this_progress) { \
1832 char filename[64]; \
1833 snprintf(filename, 64, "%s-%04d-%02d-%02d-" #pass, \
1834 stage_name, shader_prog ? shader_prog->Name : 0, iteration, pass_num); \
1836 backend_visitor::dump_instructions(filename); \
1839 progress = progress || this_progress; \
1844 if (unlikely(INTEL_DEBUG
& DEBUG_OPTIMIZER
)) {
1846 snprintf(filename
, 64, "%s-%04d-00-start",
1847 stage_name
, shader_prog
? shader_prog
->Name
: 0);
1849 backend_visitor::dump_instructions(filename
);
1860 OPT(opt_reduce_swizzle
);
1861 OPT(dead_code_eliminate
);
1862 OPT(dead_control_flow_eliminate
, this);
1863 OPT(opt_copy_propagation
);
1866 OPT(opt_register_coalesce
);
1871 if (OPT(opt_vector_float
)) {
1873 OPT(opt_copy_propagation
, false);
1874 OPT(opt_copy_propagation
, true);
1875 OPT(dead_code_eliminate
);
1884 /* Debug of register spilling: Go spill everything. */
1885 const int grf_count
= alloc
.count
;
1886 float spill_costs
[alloc
.count
];
1887 bool no_spill
[alloc
.count
];
1888 evaluate_spill_costs(spill_costs
, no_spill
);
1889 for (int i
= 0; i
< grf_count
; i
++) {
1896 while (!reg_allocate()) {
1901 opt_schedule_instructions();
1903 opt_set_dependency_control();
1905 /* If any state parameters were appended, then ParameterValues could have
1906 * been realloced, in which case the driver uniform storage set up by
1907 * _mesa_associate_uniform_storage() would point to freed memory. Make
1908 * sure that didn't happen.
1910 assert(sanity_param_count
== prog
->Parameters
->NumParameters
);
1915 } /* namespace brw */
1920 * Compile a vertex shader.
1922 * Returns the final assembly and the program's size.
1925 brw_vs_emit(struct brw_context
*brw
,
1926 struct gl_shader_program
*prog
,
1927 struct brw_vs_compile
*c
,
1928 struct brw_vs_prog_data
*prog_data
,
1930 unsigned *final_assembly_size
)
1932 bool start_busy
= false;
1933 double start_time
= 0;
1934 const unsigned *assembly
= NULL
;
1936 if (unlikely(brw
->perf_debug
)) {
1937 start_busy
= (brw
->batch
.last_bo
&&
1938 drm_intel_bo_busy(brw
->batch
.last_bo
));
1939 start_time
= get_time();
1942 struct brw_shader
*shader
= NULL
;
1944 shader
= (brw_shader
*) prog
->_LinkedShaders
[MESA_SHADER_VERTEX
];
1946 if (unlikely(INTEL_DEBUG
& DEBUG_VS
))
1947 brw_dump_ir("vertex", prog
, &shader
->base
, &c
->vp
->program
.Base
);
1949 if (prog
&& brw
->gen
>= 8 && brw
->scalar_vs
) {
1950 fs_visitor
v(brw
, mem_ctx
, &c
->key
, prog_data
, prog
, &c
->vp
->program
, 8);
1953 prog
->LinkStatus
= false;
1954 ralloc_strcat(&prog
->InfoLog
, v
.fail_msg
);
1957 _mesa_problem(NULL
, "Failed to compile vertex shader: %s\n",
1963 fs_generator
g(brw
, mem_ctx
, (void *) &c
->key
, &prog_data
->base
.base
,
1964 &c
->vp
->program
.Base
, v
.runtime_check_aads_emit
, "VS");
1965 if (INTEL_DEBUG
& DEBUG_VS
) {
1966 char *name
= ralloc_asprintf(mem_ctx
, "%s vertex shader %d",
1967 prog
->Label
? prog
->Label
: "unnamed",
1969 g
.enable_debug(name
);
1971 g
.generate_code(v
.cfg
, 8);
1972 assembly
= g
.get_assembly(final_assembly_size
);
1975 prog_data
->base
.simd8
= true;
1976 c
->base
.last_scratch
= v
.last_scratch
;
1980 vec4_vs_visitor
v(brw
, c
, prog_data
, prog
, mem_ctx
);
1983 prog
->LinkStatus
= false;
1984 ralloc_strcat(&prog
->InfoLog
, v
.fail_msg
);
1987 _mesa_problem(NULL
, "Failed to compile vertex shader: %s\n",
1993 vec4_generator
g(brw
, prog
, &c
->vp
->program
.Base
, &prog_data
->base
,
1994 mem_ctx
, INTEL_DEBUG
& DEBUG_VS
, "vertex", "VS");
1995 assembly
= g
.generate_assembly(v
.cfg
, final_assembly_size
);
1998 if (unlikely(brw
->perf_debug
) && shader
) {
1999 if (shader
->compiled_once
) {
2000 brw_vs_debug_recompile(brw
, prog
, &c
->key
);
2002 if (start_busy
&& !drm_intel_bo_busy(brw
->batch
.last_bo
)) {
2003 perf_debug("VS compile took %.03f ms and stalled the GPU\n",
2004 (get_time() - start_time
) * 1000);
2006 shader
->compiled_once
= true;
2014 brw_vue_setup_prog_key_for_precompile(struct gl_context
*ctx
,
2015 struct brw_vue_prog_key
*key
,
2016 GLuint id
, struct gl_program
*prog
)
2018 struct brw_context
*brw
= brw_context(ctx
);
2019 key
->program_string_id
= id
;
2021 const bool has_shader_channel_select
= brw
->is_haswell
|| brw
->gen
>= 8;
2022 unsigned sampler_count
= _mesa_fls(prog
->SamplersUsed
);
2023 for (unsigned i
= 0; i
< sampler_count
; i
++) {
2024 if (!has_shader_channel_select
&& (prog
->ShadowSamplers
& (1 << i
))) {
2025 /* Assume DEPTH_TEXTURE_MODE is the default: X, X, X, 1 */
2026 key
->tex
.swizzles
[i
] =
2027 MAKE_SWIZZLE4(SWIZZLE_X
, SWIZZLE_X
, SWIZZLE_X
, SWIZZLE_ONE
);
2029 /* Color sampler: assume no swizzling. */
2030 key
->tex
.swizzles
[i
] = SWIZZLE_XYZW
;