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
29 #include "brw_vec4_live_variables.h"
30 #include "brw_dead_control_flow.h"
33 #include "main/macros.h"
34 #include "main/shaderobj.h"
35 #include "program/prog_print.h"
36 #include "program/prog_parameter.h"
39 #define MAX_INSTRUCTION (1 << 30)
48 memset(this, 0, sizeof(*this));
50 this->file
= BAD_FILE
;
53 src_reg::src_reg(register_file file
, int reg
, const glsl_type
*type
)
59 if (type
&& (type
->is_scalar() || type
->is_vector() || type
->is_matrix()))
60 this->swizzle
= brw_swizzle_for_size(type
->vector_elements
);
62 this->swizzle
= BRW_SWIZZLE_XYZW
;
65 /** Generic unset register constructor. */
71 src_reg::src_reg(float f
)
76 this->type
= BRW_REGISTER_TYPE_F
;
77 this->fixed_hw_reg
.dw1
.f
= f
;
80 src_reg::src_reg(uint32_t u
)
85 this->type
= BRW_REGISTER_TYPE_UD
;
86 this->fixed_hw_reg
.dw1
.ud
= u
;
89 src_reg::src_reg(int32_t i
)
94 this->type
= BRW_REGISTER_TYPE_D
;
95 this->fixed_hw_reg
.dw1
.d
= i
;
98 src_reg::src_reg(uint8_t vf
[4])
103 this->type
= BRW_REGISTER_TYPE_VF
;
104 memcpy(&this->fixed_hw_reg
.dw1
.ud
, vf
, sizeof(unsigned));
107 src_reg::src_reg(uint8_t vf0
, uint8_t vf1
, uint8_t vf2
, uint8_t vf3
)
112 this->type
= BRW_REGISTER_TYPE_VF
;
113 this->fixed_hw_reg
.dw1
.ud
= (vf0
<< 0) |
119 src_reg::src_reg(struct brw_reg reg
)
124 this->fixed_hw_reg
= reg
;
125 this->type
= reg
.type
;
128 src_reg::src_reg(const dst_reg
®
)
132 this->file
= reg
.file
;
134 this->reg_offset
= reg
.reg_offset
;
135 this->type
= reg
.type
;
136 this->reladdr
= reg
.reladdr
;
137 this->fixed_hw_reg
= reg
.fixed_hw_reg
;
138 this->swizzle
= brw_swizzle_for_mask(reg
.writemask
);
144 memset(this, 0, sizeof(*this));
145 this->file
= BAD_FILE
;
146 this->writemask
= WRITEMASK_XYZW
;
154 dst_reg::dst_reg(register_file file
, int reg
)
162 dst_reg::dst_reg(register_file file
, int reg
, const glsl_type
*type
,
169 this->type
= brw_type_for_base_type(type
);
170 this->writemask
= writemask
;
173 dst_reg::dst_reg(struct brw_reg reg
)
178 this->fixed_hw_reg
= reg
;
179 this->type
= reg
.type
;
182 dst_reg::dst_reg(const src_reg
®
)
186 this->file
= reg
.file
;
188 this->reg_offset
= reg
.reg_offset
;
189 this->type
= reg
.type
;
190 this->writemask
= brw_mask_for_swizzle(reg
.swizzle
);
191 this->reladdr
= reg
.reladdr
;
192 this->fixed_hw_reg
= reg
.fixed_hw_reg
;
196 dst_reg::equals(const dst_reg
&r
) const
198 return (file
== r
.file
&&
200 reg_offset
== r
.reg_offset
&&
202 negate
== r
.negate
&&
204 writemask
== r
.writemask
&&
205 (reladdr
== r
.reladdr
||
206 (reladdr
&& r
.reladdr
&& reladdr
->equals(*r
.reladdr
))) &&
207 memcmp(&fixed_hw_reg
, &r
.fixed_hw_reg
,
208 sizeof(fixed_hw_reg
)) == 0);
212 vec4_instruction::is_send_from_grf()
215 case SHADER_OPCODE_SHADER_TIME_ADD
:
216 case VS_OPCODE_PULL_CONSTANT_LOAD_GEN7
:
224 vec4_instruction::regs_read(unsigned arg
) const
226 if (src
[arg
].file
== BAD_FILE
)
230 case SHADER_OPCODE_SHADER_TIME_ADD
:
231 return arg
== 0 ? mlen
: 1;
233 case VS_OPCODE_PULL_CONSTANT_LOAD_GEN7
:
234 return arg
== 1 ? mlen
: 1;
242 vec4_instruction::can_do_source_mods(const struct brw_device_info
*devinfo
)
244 if (devinfo
->gen
== 6 && is_math())
247 if (is_send_from_grf())
250 if (!backend_instruction::can_do_source_mods())
257 * Returns how many MRFs an opcode will write over.
259 * Note that this is not the 0 or 1 implied writes in an actual gen
260 * instruction -- the generate_* functions generate additional MOVs
264 vec4_visitor::implied_mrf_writes(vec4_instruction
*inst
)
266 if (inst
->mlen
== 0 || inst
->is_send_from_grf())
269 switch (inst
->opcode
) {
270 case SHADER_OPCODE_RCP
:
271 case SHADER_OPCODE_RSQ
:
272 case SHADER_OPCODE_SQRT
:
273 case SHADER_OPCODE_EXP2
:
274 case SHADER_OPCODE_LOG2
:
275 case SHADER_OPCODE_SIN
:
276 case SHADER_OPCODE_COS
:
278 case SHADER_OPCODE_INT_QUOTIENT
:
279 case SHADER_OPCODE_INT_REMAINDER
:
280 case SHADER_OPCODE_POW
:
282 case VS_OPCODE_URB_WRITE
:
284 case VS_OPCODE_PULL_CONSTANT_LOAD
:
286 case SHADER_OPCODE_GEN4_SCRATCH_READ
:
288 case SHADER_OPCODE_GEN4_SCRATCH_WRITE
:
290 case GS_OPCODE_URB_WRITE
:
291 case GS_OPCODE_URB_WRITE_ALLOCATE
:
292 case GS_OPCODE_THREAD_END
:
294 case GS_OPCODE_FF_SYNC
:
296 case SHADER_OPCODE_SHADER_TIME_ADD
:
298 case SHADER_OPCODE_TEX
:
299 case SHADER_OPCODE_TXL
:
300 case SHADER_OPCODE_TXD
:
301 case SHADER_OPCODE_TXF
:
302 case SHADER_OPCODE_TXF_CMS
:
303 case SHADER_OPCODE_TXF_MCS
:
304 case SHADER_OPCODE_TXS
:
305 case SHADER_OPCODE_TG4
:
306 case SHADER_OPCODE_TG4_OFFSET
:
307 return inst
->header_present
? 1 : 0;
308 case SHADER_OPCODE_UNTYPED_ATOMIC
:
309 case SHADER_OPCODE_UNTYPED_SURFACE_READ
:
312 unreachable("not reached");
317 src_reg::equals(const src_reg
&r
) const
319 return (file
== r
.file
&&
321 reg_offset
== r
.reg_offset
&&
323 negate
== r
.negate
&&
325 swizzle
== r
.swizzle
&&
326 !reladdr
&& !r
.reladdr
&&
327 memcmp(&fixed_hw_reg
, &r
.fixed_hw_reg
,
328 sizeof(fixed_hw_reg
)) == 0);
332 vec4_visitor::opt_vector_float()
334 bool progress
= false;
336 int last_reg
= -1, last_reg_offset
= -1;
337 enum register_file last_reg_file
= BAD_FILE
;
339 int remaining_channels
= 0;
342 vec4_instruction
*imm_inst
[4];
344 foreach_block_and_inst_safe(block
, vec4_instruction
, inst
, cfg
) {
345 if (last_reg
!= inst
->dst
.reg
||
346 last_reg_offset
!= inst
->dst
.reg_offset
||
347 last_reg_file
!= inst
->dst
.file
) {
348 last_reg
= inst
->dst
.reg
;
349 last_reg_offset
= inst
->dst
.reg_offset
;
350 last_reg_file
= inst
->dst
.file
;
351 remaining_channels
= WRITEMASK_XYZW
;
356 if (inst
->opcode
!= BRW_OPCODE_MOV
||
357 inst
->dst
.writemask
== WRITEMASK_XYZW
||
358 inst
->src
[0].file
!= IMM
)
361 int vf
= brw_float_to_vf(inst
->src
[0].fixed_hw_reg
.dw1
.f
);
365 if ((inst
->dst
.writemask
& WRITEMASK_X
) != 0)
367 if ((inst
->dst
.writemask
& WRITEMASK_Y
) != 0)
369 if ((inst
->dst
.writemask
& WRITEMASK_Z
) != 0)
371 if ((inst
->dst
.writemask
& WRITEMASK_W
) != 0)
374 imm_inst
[inst_count
++] = inst
;
376 remaining_channels
&= ~inst
->dst
.writemask
;
377 if (remaining_channels
== 0) {
378 vec4_instruction
*mov
= MOV(inst
->dst
, imm
);
379 mov
->dst
.type
= BRW_REGISTER_TYPE_F
;
380 mov
->dst
.writemask
= WRITEMASK_XYZW
;
381 inst
->insert_after(block
, mov
);
384 for (int i
= 0; i
< inst_count
; i
++) {
385 imm_inst
[i
]->remove(block
);
392 invalidate_live_intervals();
397 /* Replaces unused channels of a swizzle with channels that are used.
399 * For instance, this pass transforms
401 * mov vgrf4.yz, vgrf5.wxzy
405 * mov vgrf4.yz, vgrf5.xxzx
407 * This eliminates false uses of some channels, letting dead code elimination
408 * remove the instructions that wrote them.
411 vec4_visitor::opt_reduce_swizzle()
413 bool progress
= false;
415 foreach_block_and_inst_safe(block
, vec4_instruction
, inst
, cfg
) {
416 if (inst
->dst
.file
== BAD_FILE
|| inst
->dst
.file
== HW_REG
||
417 inst
->is_send_from_grf())
422 /* Determine which channels of the sources are read. */
423 switch (inst
->opcode
) {
424 case VEC4_OPCODE_PACK_BYTES
:
426 case BRW_OPCODE_DPH
: /* FINISHME: DPH reads only three channels of src0,
427 * but all four of src1.
429 swizzle
= brw_swizzle_for_size(4);
432 swizzle
= brw_swizzle_for_size(3);
435 swizzle
= brw_swizzle_for_size(2);
438 swizzle
= brw_swizzle_for_mask(inst
->dst
.writemask
);
442 /* Update sources' swizzles. */
443 for (int i
= 0; i
< 3; i
++) {
444 if (inst
->src
[i
].file
!= GRF
&&
445 inst
->src
[i
].file
!= ATTR
&&
446 inst
->src
[i
].file
!= UNIFORM
)
449 const unsigned new_swizzle
=
450 brw_compose_swizzle(swizzle
, inst
->src
[i
].swizzle
);
451 if (inst
->src
[i
].swizzle
!= new_swizzle
) {
452 inst
->src
[i
].swizzle
= new_swizzle
;
459 invalidate_live_intervals();
465 vec4_visitor::split_uniform_registers()
467 /* Prior to this, uniforms have been in an array sized according to
468 * the number of vector uniforms present, sparsely filled (so an
469 * aggregate results in reg indices being skipped over). Now we're
470 * going to cut those aggregates up so each .reg index is one
471 * vector. The goal is to make elimination of unused uniform
472 * components easier later.
474 foreach_block_and_inst(block
, vec4_instruction
, inst
, cfg
) {
475 for (int i
= 0 ; i
< 3; i
++) {
476 if (inst
->src
[i
].file
!= UNIFORM
)
479 assert(!inst
->src
[i
].reladdr
);
481 inst
->src
[i
].reg
+= inst
->src
[i
].reg_offset
;
482 inst
->src
[i
].reg_offset
= 0;
486 /* Update that everything is now vector-sized. */
487 for (int i
= 0; i
< this->uniforms
; i
++) {
488 this->uniform_size
[i
] = 1;
493 vec4_visitor::pack_uniform_registers()
495 bool uniform_used
[this->uniforms
];
496 int new_loc
[this->uniforms
];
497 int new_chan
[this->uniforms
];
499 memset(uniform_used
, 0, sizeof(uniform_used
));
500 memset(new_loc
, 0, sizeof(new_loc
));
501 memset(new_chan
, 0, sizeof(new_chan
));
503 /* Find which uniform vectors are actually used by the program. We
504 * expect unused vector elements when we've moved array access out
505 * to pull constants, and from some GLSL code generators like wine.
507 foreach_block_and_inst(block
, vec4_instruction
, inst
, cfg
) {
508 for (int i
= 0 ; i
< 3; i
++) {
509 if (inst
->src
[i
].file
!= UNIFORM
)
512 uniform_used
[inst
->src
[i
].reg
] = true;
516 int new_uniform_count
= 0;
518 /* Now, figure out a packing of the live uniform vectors into our
521 for (int src
= 0; src
< uniforms
; src
++) {
522 assert(src
< uniform_array_size
);
523 int size
= this->uniform_vector_size
[src
];
525 if (!uniform_used
[src
]) {
526 this->uniform_vector_size
[src
] = 0;
531 /* Find the lowest place we can slot this uniform in. */
532 for (dst
= 0; dst
< src
; dst
++) {
533 if (this->uniform_vector_size
[dst
] + size
<= 4)
542 new_chan
[src
] = this->uniform_vector_size
[dst
];
544 /* Move the references to the data */
545 for (int j
= 0; j
< size
; j
++) {
546 stage_prog_data
->param
[dst
* 4 + new_chan
[src
] + j
] =
547 stage_prog_data
->param
[src
* 4 + j
];
550 this->uniform_vector_size
[dst
] += size
;
551 this->uniform_vector_size
[src
] = 0;
554 new_uniform_count
= MAX2(new_uniform_count
, dst
+ 1);
557 this->uniforms
= new_uniform_count
;
559 /* Now, update the instructions for our repacked uniforms. */
560 foreach_block_and_inst(block
, vec4_instruction
, inst
, cfg
) {
561 for (int i
= 0 ; i
< 3; i
++) {
562 int src
= inst
->src
[i
].reg
;
564 if (inst
->src
[i
].file
!= UNIFORM
)
567 inst
->src
[i
].reg
= new_loc
[src
];
568 inst
->src
[i
].swizzle
+= BRW_SWIZZLE4(new_chan
[src
], new_chan
[src
],
569 new_chan
[src
], new_chan
[src
]);
575 * Does algebraic optimizations (0 * a = 0, 1 * a = a, a + 0 = a).
577 * While GLSL IR also performs this optimization, we end up with it in
578 * our instruction stream for a couple of reasons. One is that we
579 * sometimes generate silly instructions, for example in array access
580 * where we'll generate "ADD offset, index, base" even if base is 0.
581 * The other is that GLSL IR's constant propagation doesn't track the
582 * components of aggregates, so some VS patterns (initialize matrix to
583 * 0, accumulate in vertex blending factors) end up breaking down to
584 * instructions involving 0.
587 vec4_visitor::opt_algebraic()
589 bool progress
= false;
591 foreach_block_and_inst(block
, vec4_instruction
, inst
, cfg
) {
592 switch (inst
->opcode
) {
594 if (inst
->src
[0].file
!= IMM
)
597 if (inst
->saturate
) {
598 if (inst
->dst
.type
!= inst
->src
[0].type
)
599 assert(!"unimplemented: saturate mixed types");
601 if (brw_saturate_immediate(inst
->dst
.type
,
602 &inst
->src
[0].fixed_hw_reg
)) {
603 inst
->saturate
= false;
609 case VEC4_OPCODE_UNPACK_UNIFORM
:
610 if (inst
->src
[0].file
!= UNIFORM
) {
611 inst
->opcode
= BRW_OPCODE_MOV
;
617 if (inst
->src
[1].is_zero()) {
618 inst
->opcode
= BRW_OPCODE_MOV
;
619 inst
->src
[1] = src_reg();
625 if (inst
->src
[1].is_zero()) {
626 inst
->opcode
= BRW_OPCODE_MOV
;
627 switch (inst
->src
[0].type
) {
628 case BRW_REGISTER_TYPE_F
:
629 inst
->src
[0] = src_reg(0.0f
);
631 case BRW_REGISTER_TYPE_D
:
632 inst
->src
[0] = src_reg(0);
634 case BRW_REGISTER_TYPE_UD
:
635 inst
->src
[0] = src_reg(0u);
638 unreachable("not reached");
640 inst
->src
[1] = src_reg();
642 } else if (inst
->src
[1].is_one()) {
643 inst
->opcode
= BRW_OPCODE_MOV
;
644 inst
->src
[1] = src_reg();
646 } else if (inst
->src
[1].is_negative_one()) {
647 inst
->opcode
= BRW_OPCODE_MOV
;
648 inst
->src
[0].negate
= !inst
->src
[0].negate
;
649 inst
->src
[1] = src_reg();
654 if (inst
->conditional_mod
== BRW_CONDITIONAL_GE
&&
656 inst
->src
[0].negate
&&
657 inst
->src
[1].is_zero()) {
658 inst
->src
[0].abs
= false;
659 inst
->src
[0].negate
= false;
660 inst
->conditional_mod
= BRW_CONDITIONAL_Z
;
665 case SHADER_OPCODE_RCP
: {
666 vec4_instruction
*prev
= (vec4_instruction
*)inst
->prev
;
667 if (prev
->opcode
== SHADER_OPCODE_SQRT
) {
668 if (inst
->src
[0].equals(src_reg(prev
->dst
))) {
669 inst
->opcode
= SHADER_OPCODE_RSQ
;
670 inst
->src
[0] = prev
->src
[0];
682 invalidate_live_intervals();
688 * Only a limited number of hardware registers may be used for push
689 * constants, so this turns access to the overflowed constants into
693 vec4_visitor::move_push_constants_to_pull_constants()
695 int pull_constant_loc
[this->uniforms
];
697 /* Only allow 32 registers (256 uniform components) as push constants,
698 * which is the limit on gen6.
700 * If changing this value, note the limitation about total_regs in
703 int max_uniform_components
= 32 * 8;
704 if (this->uniforms
* 4 <= max_uniform_components
)
707 /* Make some sort of choice as to which uniforms get sent to pull
708 * constants. We could potentially do something clever here like
709 * look for the most infrequently used uniform vec4s, but leave
712 for (int i
= 0; i
< this->uniforms
* 4; i
+= 4) {
713 pull_constant_loc
[i
/ 4] = -1;
715 if (i
>= max_uniform_components
) {
716 const gl_constant_value
**values
= &stage_prog_data
->param
[i
];
718 /* Try to find an existing copy of this uniform in the pull
719 * constants if it was part of an array access already.
721 for (unsigned int j
= 0; j
< stage_prog_data
->nr_pull_params
; j
+= 4) {
724 for (matches
= 0; matches
< 4; matches
++) {
725 if (stage_prog_data
->pull_param
[j
+ matches
] != values
[matches
])
730 pull_constant_loc
[i
/ 4] = j
/ 4;
735 if (pull_constant_loc
[i
/ 4] == -1) {
736 assert(stage_prog_data
->nr_pull_params
% 4 == 0);
737 pull_constant_loc
[i
/ 4] = stage_prog_data
->nr_pull_params
/ 4;
739 for (int j
= 0; j
< 4; j
++) {
740 stage_prog_data
->pull_param
[stage_prog_data
->nr_pull_params
++] =
747 /* Now actually rewrite usage of the things we've moved to pull
750 foreach_block_and_inst_safe(block
, vec4_instruction
, inst
, cfg
) {
751 for (int i
= 0 ; i
< 3; i
++) {
752 if (inst
->src
[i
].file
!= UNIFORM
||
753 pull_constant_loc
[inst
->src
[i
].reg
] == -1)
756 int uniform
= inst
->src
[i
].reg
;
758 dst_reg temp
= dst_reg(this, glsl_type::vec4_type
);
760 emit_pull_constant_load(block
, inst
, temp
, inst
->src
[i
],
761 pull_constant_loc
[uniform
]);
763 inst
->src
[i
].file
= temp
.file
;
764 inst
->src
[i
].reg
= temp
.reg
;
765 inst
->src
[i
].reg_offset
= temp
.reg_offset
;
766 inst
->src
[i
].reladdr
= NULL
;
770 /* Repack push constants to remove the now-unused ones. */
771 pack_uniform_registers();
774 /* Conditions for which we want to avoid setting the dependency control bits */
776 vec4_visitor::is_dep_ctrl_unsafe(const vec4_instruction
*inst
)
778 #define IS_DWORD(reg) \
779 (reg.type == BRW_REGISTER_TYPE_UD || \
780 reg.type == BRW_REGISTER_TYPE_D)
782 /* "When source or destination datatype is 64b or operation is integer DWord
783 * multiply, DepCtrl must not be used."
784 * May apply to future SoCs as well.
786 if (devinfo
->is_cherryview
) {
787 if (inst
->opcode
== BRW_OPCODE_MUL
&&
788 IS_DWORD(inst
->src
[0]) &&
789 IS_DWORD(inst
->src
[1]))
794 if (devinfo
->gen
>= 8) {
795 if (inst
->opcode
== BRW_OPCODE_F32TO16
)
801 * In the presence of send messages, totally interrupt dependency
802 * control. They're long enough that the chance of dependency
803 * control around them just doesn't matter.
806 * From the Ivy Bridge PRM, volume 4 part 3.7, page 80:
807 * When a sequence of NoDDChk and NoDDClr are used, the last instruction that
808 * completes the scoreboard clear must have a non-zero execution mask. This
809 * means, if any kind of predication can change the execution mask or channel
810 * enable of the last instruction, the optimization must be avoided. This is
811 * to avoid instructions being shot down the pipeline when no writes are
815 * Dependency control does not work well over math instructions.
816 * NB: Discovered empirically
818 return (inst
->mlen
|| inst
->predicate
|| inst
->is_math());
822 * Sets the dependency control fields on instructions after register
823 * allocation and before the generator is run.
825 * When you have a sequence of instructions like:
827 * DP4 temp.x vertex uniform[0]
828 * DP4 temp.y vertex uniform[0]
829 * DP4 temp.z vertex uniform[0]
830 * DP4 temp.w vertex uniform[0]
832 * The hardware doesn't know that it can actually run the later instructions
833 * while the previous ones are in flight, producing stalls. However, we have
834 * manual fields we can set in the instructions that let it do so.
837 vec4_visitor::opt_set_dependency_control()
839 vec4_instruction
*last_grf_write
[BRW_MAX_GRF
];
840 uint8_t grf_channels_written
[BRW_MAX_GRF
];
841 vec4_instruction
*last_mrf_write
[BRW_MAX_GRF
];
842 uint8_t mrf_channels_written
[BRW_MAX_GRF
];
844 assert(prog_data
->total_grf
||
845 !"Must be called after register allocation");
847 foreach_block (block
, cfg
) {
848 memset(last_grf_write
, 0, sizeof(last_grf_write
));
849 memset(last_mrf_write
, 0, sizeof(last_mrf_write
));
851 foreach_inst_in_block (vec4_instruction
, inst
, block
) {
852 /* If we read from a register that we were doing dependency control
853 * on, don't do dependency control across the read.
855 for (int i
= 0; i
< 3; i
++) {
856 int reg
= inst
->src
[i
].reg
+ inst
->src
[i
].reg_offset
;
857 if (inst
->src
[i
].file
== GRF
) {
858 last_grf_write
[reg
] = NULL
;
859 } else if (inst
->src
[i
].file
== HW_REG
) {
860 memset(last_grf_write
, 0, sizeof(last_grf_write
));
863 assert(inst
->src
[i
].file
!= MRF
);
866 if (is_dep_ctrl_unsafe(inst
)) {
867 memset(last_grf_write
, 0, sizeof(last_grf_write
));
868 memset(last_mrf_write
, 0, sizeof(last_mrf_write
));
872 /* Now, see if we can do dependency control for this instruction
873 * against a previous one writing to its destination.
875 int reg
= inst
->dst
.reg
+ inst
->dst
.reg_offset
;
876 if (inst
->dst
.file
== GRF
) {
877 if (last_grf_write
[reg
] &&
878 !(inst
->dst
.writemask
& grf_channels_written
[reg
])) {
879 last_grf_write
[reg
]->no_dd_clear
= true;
880 inst
->no_dd_check
= true;
882 grf_channels_written
[reg
] = 0;
885 last_grf_write
[reg
] = inst
;
886 grf_channels_written
[reg
] |= inst
->dst
.writemask
;
887 } else if (inst
->dst
.file
== MRF
) {
888 if (last_mrf_write
[reg
] &&
889 !(inst
->dst
.writemask
& mrf_channels_written
[reg
])) {
890 last_mrf_write
[reg
]->no_dd_clear
= true;
891 inst
->no_dd_check
= true;
893 mrf_channels_written
[reg
] = 0;
896 last_mrf_write
[reg
] = inst
;
897 mrf_channels_written
[reg
] |= inst
->dst
.writemask
;
898 } else if (inst
->dst
.reg
== HW_REG
) {
899 if (inst
->dst
.fixed_hw_reg
.file
== BRW_GENERAL_REGISTER_FILE
)
900 memset(last_grf_write
, 0, sizeof(last_grf_write
));
901 if (inst
->dst
.fixed_hw_reg
.file
== BRW_MESSAGE_REGISTER_FILE
)
902 memset(last_mrf_write
, 0, sizeof(last_mrf_write
));
909 vec4_instruction::can_reswizzle(int dst_writemask
,
913 /* If this instruction sets anything not referenced by swizzle, then we'd
914 * totally break it when we reswizzle.
916 if (dst
.writemask
& ~swizzle_mask
)
926 * For any channels in the swizzle's source that were populated by this
927 * instruction, rewrite the instruction to put the appropriate result directly
930 * e.g. for swizzle=yywx, MUL a.xy b c -> MUL a.yy_x b.yy z.yy_x
933 vec4_instruction::reswizzle(int dst_writemask
, int swizzle
)
935 /* Destination write mask doesn't correspond to source swizzle for the dot
936 * product and pack_bytes instructions.
938 if (opcode
!= BRW_OPCODE_DP4
&& opcode
!= BRW_OPCODE_DPH
&&
939 opcode
!= BRW_OPCODE_DP3
&& opcode
!= BRW_OPCODE_DP2
&&
940 opcode
!= VEC4_OPCODE_PACK_BYTES
) {
941 for (int i
= 0; i
< 3; i
++) {
942 if (src
[i
].file
== BAD_FILE
|| src
[i
].file
== IMM
)
945 src
[i
].swizzle
= brw_compose_swizzle(swizzle
, src
[i
].swizzle
);
949 /* Apply the specified swizzle and writemask to the original mask of
950 * written components.
952 dst
.writemask
= dst_writemask
&
953 brw_apply_swizzle_to_mask(swizzle
, dst
.writemask
);
957 * Tries to reduce extra MOV instructions by taking temporary GRFs that get
958 * just written and then MOVed into another reg and making the original write
959 * of the GRF write directly to the final destination instead.
962 vec4_visitor::opt_register_coalesce()
964 bool progress
= false;
967 calculate_live_intervals();
969 foreach_block_and_inst_safe (block
, vec4_instruction
, inst
, cfg
) {
973 if (inst
->opcode
!= BRW_OPCODE_MOV
||
974 (inst
->dst
.file
!= GRF
&& inst
->dst
.file
!= MRF
) ||
976 inst
->src
[0].file
!= GRF
||
977 inst
->dst
.type
!= inst
->src
[0].type
||
978 inst
->src
[0].abs
|| inst
->src
[0].negate
|| inst
->src
[0].reladdr
)
981 bool to_mrf
= (inst
->dst
.file
== MRF
);
983 /* Can't coalesce this GRF if someone else was going to
986 if (var_range_end(var_from_reg(alloc
, inst
->src
[0]), 4) > ip
)
989 /* We need to check interference with the final destination between this
990 * instruction and the earliest instruction involved in writing the GRF
991 * we're eliminating. To do that, keep track of which of our source
992 * channels we've seen initialized.
994 const unsigned chans_needed
=
995 brw_apply_inv_swizzle_to_mask(inst
->src
[0].swizzle
,
996 inst
->dst
.writemask
);
997 unsigned chans_remaining
= chans_needed
;
999 /* Now walk up the instruction stream trying to see if we can rewrite
1000 * everything writing to the temporary to write into the destination
1003 vec4_instruction
*_scan_inst
= (vec4_instruction
*)inst
->prev
;
1004 foreach_inst_in_block_reverse_starting_from(vec4_instruction
, scan_inst
,
1006 _scan_inst
= scan_inst
;
1008 if (inst
->src
[0].in_range(scan_inst
->dst
, scan_inst
->regs_written
)) {
1009 /* Found something writing to the reg we want to coalesce away. */
1011 /* SEND instructions can't have MRF as a destination. */
1012 if (scan_inst
->mlen
)
1015 if (devinfo
->gen
== 6) {
1016 /* gen6 math instructions must have the destination be
1017 * GRF, so no compute-to-MRF for them.
1019 if (scan_inst
->is_math()) {
1025 /* If we can't handle the swizzle, bail. */
1026 if (!scan_inst
->can_reswizzle(inst
->dst
.writemask
,
1027 inst
->src
[0].swizzle
,
1032 /* This doesn't handle coalescing of multiple registers. */
1033 if (scan_inst
->regs_written
> 1)
1036 /* Mark which channels we found unconditional writes for. */
1037 if (!scan_inst
->predicate
)
1038 chans_remaining
&= ~scan_inst
->dst
.writemask
;
1040 if (chans_remaining
== 0)
1044 /* You can't read from an MRF, so if someone else reads our MRF's
1045 * source GRF that we wanted to rewrite, that stops us. If it's a
1046 * GRF we're trying to coalesce to, we don't actually handle
1047 * rewriting sources so bail in that case as well.
1049 bool interfered
= false;
1050 for (int i
= 0; i
< 3; i
++) {
1051 if (inst
->src
[0].in_range(scan_inst
->src
[i
],
1052 scan_inst
->regs_read(i
)))
1058 /* If somebody else writes our destination here, we can't coalesce
1061 if (inst
->dst
.in_range(scan_inst
->dst
, scan_inst
->regs_written
))
1064 /* Check for reads of the register we're trying to coalesce into. We
1065 * can't go rewriting instructions above that to put some other value
1066 * in the register instead.
1068 if (to_mrf
&& scan_inst
->mlen
> 0) {
1069 if (inst
->dst
.reg
>= scan_inst
->base_mrf
&&
1070 inst
->dst
.reg
< scan_inst
->base_mrf
+ scan_inst
->mlen
) {
1074 for (int i
= 0; i
< 3; i
++) {
1075 if (inst
->dst
.in_range(scan_inst
->src
[i
],
1076 scan_inst
->regs_read(i
)))
1084 if (chans_remaining
== 0) {
1085 /* If we've made it here, we have an MOV we want to coalesce out, and
1086 * a scan_inst pointing to the earliest instruction involved in
1087 * computing the value. Now go rewrite the instruction stream
1090 vec4_instruction
*scan_inst
= _scan_inst
;
1091 while (scan_inst
!= inst
) {
1092 if (scan_inst
->dst
.file
== GRF
&&
1093 scan_inst
->dst
.reg
== inst
->src
[0].reg
&&
1094 scan_inst
->dst
.reg_offset
== inst
->src
[0].reg_offset
) {
1095 scan_inst
->reswizzle(inst
->dst
.writemask
,
1096 inst
->src
[0].swizzle
);
1097 scan_inst
->dst
.file
= inst
->dst
.file
;
1098 scan_inst
->dst
.reg
= inst
->dst
.reg
;
1099 scan_inst
->dst
.reg_offset
= inst
->dst
.reg_offset
;
1100 scan_inst
->saturate
|= inst
->saturate
;
1102 scan_inst
= (vec4_instruction
*)scan_inst
->next
;
1104 inst
->remove(block
);
1110 invalidate_live_intervals();
1116 * Splits virtual GRFs requesting more than one contiguous physical register.
1118 * We initially create large virtual GRFs for temporary structures, arrays,
1119 * and matrices, so that the dereference visitor functions can add reg_offsets
1120 * to work their way down to the actual member being accessed. But when it
1121 * comes to optimization, we'd like to treat each register as individual
1122 * storage if possible.
1124 * So far, the only thing that might prevent splitting is a send message from
1128 vec4_visitor::split_virtual_grfs()
1130 int num_vars
= this->alloc
.count
;
1131 int new_virtual_grf
[num_vars
];
1132 bool split_grf
[num_vars
];
1134 memset(new_virtual_grf
, 0, sizeof(new_virtual_grf
));
1136 /* Try to split anything > 0 sized. */
1137 for (int i
= 0; i
< num_vars
; i
++) {
1138 split_grf
[i
] = this->alloc
.sizes
[i
] != 1;
1141 /* Check that the instructions are compatible with the registers we're trying
1144 foreach_block_and_inst(block
, vec4_instruction
, inst
, cfg
) {
1145 if (inst
->dst
.file
== GRF
&& inst
->regs_written
> 1)
1146 split_grf
[inst
->dst
.reg
] = false;
1148 for (int i
= 0; i
< 3; i
++) {
1149 if (inst
->src
[i
].file
== GRF
&& inst
->regs_read(i
) > 1)
1150 split_grf
[inst
->src
[i
].reg
] = false;
1154 /* Allocate new space for split regs. Note that the virtual
1155 * numbers will be contiguous.
1157 for (int i
= 0; i
< num_vars
; i
++) {
1161 new_virtual_grf
[i
] = alloc
.allocate(1);
1162 for (unsigned j
= 2; j
< this->alloc
.sizes
[i
]; j
++) {
1163 unsigned reg
= alloc
.allocate(1);
1164 assert(reg
== new_virtual_grf
[i
] + j
- 1);
1167 this->alloc
.sizes
[i
] = 1;
1170 foreach_block_and_inst(block
, vec4_instruction
, inst
, cfg
) {
1171 if (inst
->dst
.file
== GRF
&& split_grf
[inst
->dst
.reg
] &&
1172 inst
->dst
.reg_offset
!= 0) {
1173 inst
->dst
.reg
= (new_virtual_grf
[inst
->dst
.reg
] +
1174 inst
->dst
.reg_offset
- 1);
1175 inst
->dst
.reg_offset
= 0;
1177 for (int i
= 0; i
< 3; i
++) {
1178 if (inst
->src
[i
].file
== GRF
&& split_grf
[inst
->src
[i
].reg
] &&
1179 inst
->src
[i
].reg_offset
!= 0) {
1180 inst
->src
[i
].reg
= (new_virtual_grf
[inst
->src
[i
].reg
] +
1181 inst
->src
[i
].reg_offset
- 1);
1182 inst
->src
[i
].reg_offset
= 0;
1186 invalidate_live_intervals();
1190 vec4_visitor::dump_instruction(backend_instruction
*be_inst
)
1192 dump_instruction(be_inst
, stderr
);
1196 vec4_visitor::dump_instruction(backend_instruction
*be_inst
, FILE *file
)
1198 vec4_instruction
*inst
= (vec4_instruction
*)be_inst
;
1200 if (inst
->predicate
) {
1201 fprintf(file
, "(%cf0.%d) ",
1202 inst
->predicate_inverse
? '-' : '+',
1206 fprintf(file
, "%s", brw_instruction_name(inst
->opcode
));
1208 fprintf(file
, ".sat");
1209 if (inst
->conditional_mod
) {
1210 fprintf(file
, "%s", conditional_modifier
[inst
->conditional_mod
]);
1211 if (!inst
->predicate
&&
1212 (devinfo
->gen
< 5 || (inst
->opcode
!= BRW_OPCODE_SEL
&&
1213 inst
->opcode
!= BRW_OPCODE_IF
&&
1214 inst
->opcode
!= BRW_OPCODE_WHILE
))) {
1215 fprintf(file
, ".f0.%d", inst
->flag_subreg
);
1220 switch (inst
->dst
.file
) {
1222 fprintf(file
, "vgrf%d.%d", inst
->dst
.reg
, inst
->dst
.reg_offset
);
1225 fprintf(file
, "m%d", inst
->dst
.reg
);
1228 if (inst
->dst
.fixed_hw_reg
.file
== BRW_ARCHITECTURE_REGISTER_FILE
) {
1229 switch (inst
->dst
.fixed_hw_reg
.nr
) {
1231 fprintf(file
, "null");
1233 case BRW_ARF_ADDRESS
:
1234 fprintf(file
, "a0.%d", inst
->dst
.fixed_hw_reg
.subnr
);
1236 case BRW_ARF_ACCUMULATOR
:
1237 fprintf(file
, "acc%d", inst
->dst
.fixed_hw_reg
.subnr
);
1240 fprintf(file
, "f%d.%d", inst
->dst
.fixed_hw_reg
.nr
& 0xf,
1241 inst
->dst
.fixed_hw_reg
.subnr
);
1244 fprintf(file
, "arf%d.%d", inst
->dst
.fixed_hw_reg
.nr
& 0xf,
1245 inst
->dst
.fixed_hw_reg
.subnr
);
1249 fprintf(file
, "hw_reg%d", inst
->dst
.fixed_hw_reg
.nr
);
1251 if (inst
->dst
.fixed_hw_reg
.subnr
)
1252 fprintf(file
, "+%d", inst
->dst
.fixed_hw_reg
.subnr
);
1255 fprintf(file
, "(null)");
1258 fprintf(file
, "???");
1261 if (inst
->dst
.writemask
!= WRITEMASK_XYZW
) {
1263 if (inst
->dst
.writemask
& 1)
1265 if (inst
->dst
.writemask
& 2)
1267 if (inst
->dst
.writemask
& 4)
1269 if (inst
->dst
.writemask
& 8)
1272 fprintf(file
, ":%s", brw_reg_type_letters(inst
->dst
.type
));
1274 if (inst
->src
[0].file
!= BAD_FILE
)
1275 fprintf(file
, ", ");
1277 for (int i
= 0; i
< 3 && inst
->src
[i
].file
!= BAD_FILE
; i
++) {
1278 if (inst
->src
[i
].negate
)
1280 if (inst
->src
[i
].abs
)
1282 switch (inst
->src
[i
].file
) {
1284 fprintf(file
, "vgrf%d", inst
->src
[i
].reg
);
1287 fprintf(file
, "attr%d", inst
->src
[i
].reg
);
1290 fprintf(file
, "u%d", inst
->src
[i
].reg
);
1293 switch (inst
->src
[i
].type
) {
1294 case BRW_REGISTER_TYPE_F
:
1295 fprintf(file
, "%fF", inst
->src
[i
].fixed_hw_reg
.dw1
.f
);
1297 case BRW_REGISTER_TYPE_D
:
1298 fprintf(file
, "%dD", inst
->src
[i
].fixed_hw_reg
.dw1
.d
);
1300 case BRW_REGISTER_TYPE_UD
:
1301 fprintf(file
, "%uU", inst
->src
[i
].fixed_hw_reg
.dw1
.ud
);
1303 case BRW_REGISTER_TYPE_VF
:
1304 fprintf(file
, "[%-gF, %-gF, %-gF, %-gF]",
1305 brw_vf_to_float((inst
->src
[i
].fixed_hw_reg
.dw1
.ud
>> 0) & 0xff),
1306 brw_vf_to_float((inst
->src
[i
].fixed_hw_reg
.dw1
.ud
>> 8) & 0xff),
1307 brw_vf_to_float((inst
->src
[i
].fixed_hw_reg
.dw1
.ud
>> 16) & 0xff),
1308 brw_vf_to_float((inst
->src
[i
].fixed_hw_reg
.dw1
.ud
>> 24) & 0xff));
1311 fprintf(file
, "???");
1316 if (inst
->src
[i
].fixed_hw_reg
.negate
)
1318 if (inst
->src
[i
].fixed_hw_reg
.abs
)
1320 if (inst
->src
[i
].fixed_hw_reg
.file
== BRW_ARCHITECTURE_REGISTER_FILE
) {
1321 switch (inst
->src
[i
].fixed_hw_reg
.nr
) {
1323 fprintf(file
, "null");
1325 case BRW_ARF_ADDRESS
:
1326 fprintf(file
, "a0.%d", inst
->src
[i
].fixed_hw_reg
.subnr
);
1328 case BRW_ARF_ACCUMULATOR
:
1329 fprintf(file
, "acc%d", inst
->src
[i
].fixed_hw_reg
.subnr
);
1332 fprintf(file
, "f%d.%d", inst
->src
[i
].fixed_hw_reg
.nr
& 0xf,
1333 inst
->src
[i
].fixed_hw_reg
.subnr
);
1336 fprintf(file
, "arf%d.%d", inst
->src
[i
].fixed_hw_reg
.nr
& 0xf,
1337 inst
->src
[i
].fixed_hw_reg
.subnr
);
1341 fprintf(file
, "hw_reg%d", inst
->src
[i
].fixed_hw_reg
.nr
);
1343 if (inst
->src
[i
].fixed_hw_reg
.subnr
)
1344 fprintf(file
, "+%d", inst
->src
[i
].fixed_hw_reg
.subnr
);
1345 if (inst
->src
[i
].fixed_hw_reg
.abs
)
1349 fprintf(file
, "(null)");
1352 fprintf(file
, "???");
1356 /* Don't print .0; and only VGRFs have reg_offsets and sizes */
1357 if (inst
->src
[i
].reg_offset
!= 0 &&
1358 inst
->src
[i
].file
== GRF
&&
1359 alloc
.sizes
[inst
->src
[i
].reg
] != 1)
1360 fprintf(file
, ".%d", inst
->src
[i
].reg_offset
);
1362 if (inst
->src
[i
].file
!= IMM
) {
1363 static const char *chans
[4] = {"x", "y", "z", "w"};
1365 for (int c
= 0; c
< 4; c
++) {
1366 fprintf(file
, "%s", chans
[BRW_GET_SWZ(inst
->src
[i
].swizzle
, c
)]);
1370 if (inst
->src
[i
].abs
)
1373 if (inst
->src
[i
].file
!= IMM
) {
1374 fprintf(file
, ":%s", brw_reg_type_letters(inst
->src
[i
].type
));
1377 if (i
< 2 && inst
->src
[i
+ 1].file
!= BAD_FILE
)
1378 fprintf(file
, ", ");
1381 fprintf(file
, "\n");
1385 static inline struct brw_reg
1386 attribute_to_hw_reg(int attr
, bool interleaved
)
1389 return stride(brw_vec4_grf(attr
/ 2, (attr
% 2) * 4), 0, 4, 1);
1391 return brw_vec8_grf(attr
, 0);
1396 * Replace each register of type ATTR in this->instructions with a reference
1397 * to a fixed HW register.
1399 * If interleaved is true, then each attribute takes up half a register, with
1400 * register N containing attribute 2*N in its first half and attribute 2*N+1
1401 * in its second half (this corresponds to the payload setup used by geometry
1402 * shaders in "single" or "dual instanced" dispatch mode). If interleaved is
1403 * false, then each attribute takes up a whole register, with register N
1404 * containing attribute N (this corresponds to the payload setup used by
1405 * vertex shaders, and by geometry shaders in "dual object" dispatch mode).
1408 vec4_visitor::lower_attributes_to_hw_regs(const int *attribute_map
,
1411 foreach_block_and_inst(block
, vec4_instruction
, inst
, cfg
) {
1412 /* We have to support ATTR as a destination for GL_FIXED fixup. */
1413 if (inst
->dst
.file
== ATTR
) {
1414 int grf
= attribute_map
[inst
->dst
.reg
+ inst
->dst
.reg_offset
];
1416 /* All attributes used in the shader need to have been assigned a
1417 * hardware register by the caller
1421 struct brw_reg reg
= attribute_to_hw_reg(grf
, interleaved
);
1422 reg
.type
= inst
->dst
.type
;
1423 reg
.dw1
.bits
.writemask
= inst
->dst
.writemask
;
1425 inst
->dst
.file
= HW_REG
;
1426 inst
->dst
.fixed_hw_reg
= reg
;
1429 for (int i
= 0; i
< 3; i
++) {
1430 if (inst
->src
[i
].file
!= ATTR
)
1433 int grf
= attribute_map
[inst
->src
[i
].reg
+ inst
->src
[i
].reg_offset
];
1435 /* All attributes used in the shader need to have been assigned a
1436 * hardware register by the caller
1440 struct brw_reg reg
= attribute_to_hw_reg(grf
, interleaved
);
1441 reg
.dw1
.bits
.swizzle
= inst
->src
[i
].swizzle
;
1442 reg
.type
= inst
->src
[i
].type
;
1443 if (inst
->src
[i
].abs
)
1445 if (inst
->src
[i
].negate
)
1448 inst
->src
[i
].file
= HW_REG
;
1449 inst
->src
[i
].fixed_hw_reg
= reg
;
1455 vec4_vs_visitor::setup_attributes(int payload_reg
)
1458 int attribute_map
[VERT_ATTRIB_MAX
+ 1];
1459 memset(attribute_map
, 0, sizeof(attribute_map
));
1462 for (int i
= 0; i
< VERT_ATTRIB_MAX
; i
++) {
1463 if (vs_prog_data
->inputs_read
& BITFIELD64_BIT(i
)) {
1464 attribute_map
[i
] = payload_reg
+ nr_attributes
;
1469 /* VertexID is stored by the VF as the last vertex element, but we
1470 * don't represent it with a flag in inputs_read, so we call it
1473 if (vs_prog_data
->uses_vertexid
|| vs_prog_data
->uses_instanceid
) {
1474 attribute_map
[VERT_ATTRIB_MAX
] = payload_reg
+ nr_attributes
;
1478 lower_attributes_to_hw_regs(attribute_map
, false /* interleaved */);
1480 /* The BSpec says we always have to read at least one thing from
1481 * the VF, and it appears that the hardware wedges otherwise.
1483 if (nr_attributes
== 0)
1486 prog_data
->urb_read_length
= (nr_attributes
+ 1) / 2;
1488 unsigned vue_entries
=
1489 MAX2(nr_attributes
, prog_data
->vue_map
.num_slots
);
1491 if (devinfo
->gen
== 6)
1492 prog_data
->urb_entry_size
= ALIGN(vue_entries
, 8) / 8;
1494 prog_data
->urb_entry_size
= ALIGN(vue_entries
, 4) / 4;
1496 return payload_reg
+ nr_attributes
;
1500 vec4_visitor::setup_uniforms(int reg
)
1502 prog_data
->base
.dispatch_grf_start_reg
= reg
;
1504 /* The pre-gen6 VS requires that some push constants get loaded no
1505 * matter what, or the GPU would hang.
1507 if (devinfo
->gen
< 6 && this->uniforms
== 0) {
1508 assert(this->uniforms
< this->uniform_array_size
);
1509 this->uniform_vector_size
[this->uniforms
] = 1;
1511 stage_prog_data
->param
=
1512 reralloc(NULL
, stage_prog_data
->param
, const gl_constant_value
*, 4);
1513 for (unsigned int i
= 0; i
< 4; i
++) {
1514 unsigned int slot
= this->uniforms
* 4 + i
;
1515 static gl_constant_value zero
= { 0.0 };
1516 stage_prog_data
->param
[slot
] = &zero
;
1522 reg
+= ALIGN(uniforms
, 2) / 2;
1525 stage_prog_data
->nr_params
= this->uniforms
* 4;
1527 prog_data
->base
.curb_read_length
=
1528 reg
- prog_data
->base
.dispatch_grf_start_reg
;
1534 vec4_vs_visitor::setup_payload(void)
1538 /* The payload always contains important data in g0, which contains
1539 * the URB handles that are passed on to the URB write at the end
1540 * of the thread. So, we always start push constants at g1.
1544 reg
= setup_uniforms(reg
);
1546 reg
= setup_attributes(reg
);
1548 this->first_non_payload_grf
= reg
;
1552 vec4_visitor::assign_binding_table_offsets()
1554 assign_common_binding_table_offsets(0);
1558 vec4_visitor::get_timestamp()
1560 assert(devinfo
->gen
>= 7);
1562 src_reg ts
= src_reg(brw_reg(BRW_ARCHITECTURE_REGISTER_FILE
,
1567 BRW_REGISTER_TYPE_UD
,
1568 BRW_VERTICAL_STRIDE_0
,
1570 BRW_HORIZONTAL_STRIDE_4
,
1574 dst_reg dst
= dst_reg(this, glsl_type::uvec4_type
);
1576 vec4_instruction
*mov
= emit(MOV(dst
, ts
));
1577 /* We want to read the 3 fields we care about (mostly field 0, but also 2)
1578 * even if it's not enabled in the dispatch.
1580 mov
->force_writemask_all
= true;
1582 return src_reg(dst
);
1586 vec4_visitor::emit_shader_time_begin()
1588 current_annotation
= "shader time start";
1589 shader_start_time
= get_timestamp();
1593 vec4_visitor::emit_shader_time_end()
1595 current_annotation
= "shader time end";
1596 src_reg shader_end_time
= get_timestamp();
1599 /* Check that there weren't any timestamp reset events (assuming these
1600 * were the only two timestamp reads that happened).
1602 src_reg reset_end
= shader_end_time
;
1603 reset_end
.swizzle
= BRW_SWIZZLE_ZZZZ
;
1604 vec4_instruction
*test
= emit(AND(dst_null_d(), reset_end
, src_reg(1u)));
1605 test
->conditional_mod
= BRW_CONDITIONAL_Z
;
1607 emit(IF(BRW_PREDICATE_NORMAL
));
1609 /* Take the current timestamp and get the delta. */
1610 shader_start_time
.negate
= true;
1611 dst_reg diff
= dst_reg(this, glsl_type::uint_type
);
1612 emit(ADD(diff
, shader_start_time
, shader_end_time
));
1614 /* If there were no instructions between the two timestamp gets, the diff
1615 * is 2 cycles. Remove that overhead, so I can forget about that when
1616 * trying to determine the time taken for single instructions.
1618 emit(ADD(diff
, src_reg(diff
), src_reg(-2u)));
1620 emit_shader_time_write(st_base
, src_reg(diff
));
1621 emit_shader_time_write(st_written
, src_reg(1u));
1622 emit(BRW_OPCODE_ELSE
);
1623 emit_shader_time_write(st_reset
, src_reg(1u));
1624 emit(BRW_OPCODE_ENDIF
);
1628 vec4_visitor::emit_shader_time_write(enum shader_time_shader_type type
,
1631 int shader_time_index
=
1632 brw_get_shader_time_index(brw
, shader_prog
, prog
, type
);
1635 dst_reg(this, glsl_type::get_array_instance(glsl_type::vec4_type
, 2));
1637 dst_reg offset
= dst
;
1641 offset
.type
= BRW_REGISTER_TYPE_UD
;
1642 emit(MOV(offset
, src_reg(shader_time_index
* SHADER_TIME_STRIDE
)));
1644 time
.type
= BRW_REGISTER_TYPE_UD
;
1645 emit(MOV(time
, src_reg(value
)));
1647 vec4_instruction
*inst
=
1648 emit(SHADER_OPCODE_SHADER_TIME_ADD
, dst_reg(), src_reg(dst
));
1655 sanity_param_count
= prog
->Parameters
->NumParameters
;
1657 if (INTEL_DEBUG
& DEBUG_SHADER_TIME
)
1658 emit_shader_time_begin();
1660 assign_binding_table_offsets();
1664 /* Generate VS IR for main(). (the visitor only descends into
1665 * functions called "main").
1668 visit_instructions(shader
->base
.ir
);
1670 emit_program_code();
1674 if (key
->userclip_active
&& !prog
->UsesClipDistanceOut
)
1675 setup_uniform_clipplane_values();
1681 /* Before any optimization, push array accesses out to scratch
1682 * space where we need them to be. This pass may allocate new
1683 * virtual GRFs, so we want to do it early. It also makes sure
1684 * that we have reladdr computations available for CSE, since we'll
1685 * often do repeated subexpressions for those.
1688 move_grf_array_access_to_scratch();
1689 move_uniform_array_access_to_pull_constants();
1691 /* The ARB_vertex_program frontend emits pull constant loads directly
1692 * rather than using reladdr, so we don't need to walk through all the
1693 * instructions looking for things to move. There isn't anything.
1695 * We do still need to split things to vec4 size.
1697 split_uniform_registers();
1699 pack_uniform_registers();
1700 move_push_constants_to_pull_constants();
1701 split_virtual_grfs();
1703 const char *stage_name
= stage
== MESA_SHADER_GEOMETRY
? "gs" : "vs";
1705 #define OPT(pass, args...) ({ \
1707 bool this_progress = pass(args); \
1709 if (unlikely(INTEL_DEBUG & DEBUG_OPTIMIZER) && this_progress) { \
1710 char filename[64]; \
1711 snprintf(filename, 64, "%s-%04d-%02d-%02d-" #pass, \
1712 stage_name, shader_prog ? shader_prog->Name : 0, iteration, pass_num); \
1714 backend_visitor::dump_instructions(filename); \
1717 progress = progress || this_progress; \
1722 if (unlikely(INTEL_DEBUG
& DEBUG_OPTIMIZER
)) {
1724 snprintf(filename
, 64, "%s-%04d-00-start",
1725 stage_name
, shader_prog
? shader_prog
->Name
: 0);
1727 backend_visitor::dump_instructions(filename
);
1738 OPT(opt_reduce_swizzle
);
1739 OPT(dead_code_eliminate
);
1740 OPT(dead_control_flow_eliminate
, this);
1741 OPT(opt_copy_propagation
);
1744 OPT(opt_register_coalesce
);
1749 if (OPT(opt_vector_float
)) {
1751 OPT(opt_copy_propagation
, false);
1752 OPT(opt_copy_propagation
, true);
1753 OPT(dead_code_eliminate
);
1762 /* Debug of register spilling: Go spill everything. */
1763 const int grf_count
= alloc
.count
;
1764 float spill_costs
[alloc
.count
];
1765 bool no_spill
[alloc
.count
];
1766 evaluate_spill_costs(spill_costs
, no_spill
);
1767 for (int i
= 0; i
< grf_count
; i
++) {
1774 while (!reg_allocate()) {
1779 opt_schedule_instructions();
1781 opt_set_dependency_control();
1783 /* If any state parameters were appended, then ParameterValues could have
1784 * been realloced, in which case the driver uniform storage set up by
1785 * _mesa_associate_uniform_storage() would point to freed memory. Make
1786 * sure that didn't happen.
1788 assert(sanity_param_count
== prog
->Parameters
->NumParameters
);
1793 } /* namespace brw */
1798 * Compile a vertex shader.
1800 * Returns the final assembly and the program's size.
1803 brw_vs_emit(struct brw_context
*brw
,
1804 struct gl_shader_program
*prog
,
1805 struct brw_vs_compile
*c
,
1806 struct brw_vs_prog_data
*prog_data
,
1808 unsigned *final_assembly_size
)
1810 bool start_busy
= false;
1811 double start_time
= 0;
1812 const unsigned *assembly
= NULL
;
1814 brw
->ctx
.Const
.ShaderCompilerOptions
[MESA_SHADER_VERTEX
].NirOptions
!= NULL
;
1816 if (unlikely(brw
->perf_debug
)) {
1817 start_busy
= (brw
->batch
.last_bo
&&
1818 drm_intel_bo_busy(brw
->batch
.last_bo
));
1819 start_time
= get_time();
1822 struct brw_shader
*shader
= NULL
;
1824 shader
= (brw_shader
*) prog
->_LinkedShaders
[MESA_SHADER_VERTEX
];
1826 if (unlikely(INTEL_DEBUG
& DEBUG_VS
))
1827 brw_dump_ir("vertex", prog
, &shader
->base
, &c
->vp
->program
.Base
);
1829 if (use_nir
&& !c
->vp
->program
.Base
.nir
) {
1830 /* Normally we generate NIR in LinkShader() or ProgramStringNotify(), but
1831 * Mesa's fixed-function vertex program handling doesn't notify the driver
1832 * at all. Just do it here, at the last minute, even though it's lame.
1834 assert(c
->vp
->program
.Base
.Id
== 0 && prog
== NULL
);
1835 c
->vp
->program
.Base
.nir
=
1836 brw_create_nir(brw
, NULL
, &c
->vp
->program
.Base
, MESA_SHADER_VERTEX
);
1839 if (brw
->scalar_vs
&& (prog
|| use_nir
)) {
1840 fs_visitor
v(brw
, mem_ctx
, &c
->key
, prog_data
, prog
, &c
->vp
->program
, 8);
1843 prog
->LinkStatus
= false;
1844 ralloc_strcat(&prog
->InfoLog
, v
.fail_msg
);
1847 _mesa_problem(NULL
, "Failed to compile vertex shader: %s\n",
1853 fs_generator
g(brw
, mem_ctx
, (void *) &c
->key
, &prog_data
->base
.base
,
1854 &c
->vp
->program
.Base
, v
.promoted_constants
,
1855 v
.runtime_check_aads_emit
, "VS");
1856 if (INTEL_DEBUG
& DEBUG_VS
) {
1859 name
= ralloc_asprintf(mem_ctx
, "%s vertex shader %d",
1860 prog
->Label
? prog
->Label
: "unnamed",
1863 name
= ralloc_asprintf(mem_ctx
, "vertex program %d",
1864 c
->vp
->program
.Base
.Id
);
1866 g
.enable_debug(name
);
1868 g
.generate_code(v
.cfg
, 8);
1869 assembly
= g
.get_assembly(final_assembly_size
);
1872 prog_data
->base
.simd8
= true;
1873 c
->base
.last_scratch
= v
.last_scratch
;
1877 vec4_vs_visitor
v(brw
, c
, prog_data
, prog
, mem_ctx
);
1880 prog
->LinkStatus
= false;
1881 ralloc_strcat(&prog
->InfoLog
, v
.fail_msg
);
1884 _mesa_problem(NULL
, "Failed to compile vertex shader: %s\n",
1890 vec4_generator
g(brw
, prog
, &c
->vp
->program
.Base
, &prog_data
->base
,
1891 mem_ctx
, INTEL_DEBUG
& DEBUG_VS
, "vertex", "VS");
1892 assembly
= g
.generate_assembly(v
.cfg
, final_assembly_size
);
1895 if (unlikely(brw
->perf_debug
) && shader
) {
1896 if (shader
->compiled_once
) {
1897 brw_vs_debug_recompile(brw
, prog
, &c
->key
);
1899 if (start_busy
&& !drm_intel_bo_busy(brw
->batch
.last_bo
)) {
1900 perf_debug("VS compile took %.03f ms and stalled the GPU\n",
1901 (get_time() - start_time
) * 1000);
1903 shader
->compiled_once
= true;
1911 brw_vue_setup_prog_key_for_precompile(struct gl_context
*ctx
,
1912 struct brw_vue_prog_key
*key
,
1913 GLuint id
, struct gl_program
*prog
)
1915 struct brw_context
*brw
= brw_context(ctx
);
1916 key
->program_string_id
= id
;
1918 const bool has_shader_channel_select
= brw
->is_haswell
|| brw
->gen
>= 8;
1919 unsigned sampler_count
= _mesa_fls(prog
->SamplersUsed
);
1920 for (unsigned i
= 0; i
< sampler_count
; i
++) {
1921 if (!has_shader_channel_select
&& (prog
->ShadowSamplers
& (1 << i
))) {
1922 /* Assume DEPTH_TEXTURE_MODE is the default: X, X, X, 1 */
1923 key
->tex
.swizzles
[i
] =
1924 MAKE_SWIZZLE4(SWIZZLE_X
, SWIZZLE_X
, SWIZZLE_X
, SWIZZLE_ONE
);
1926 /* Color sampler: assume no swizzling. */
1927 key
->tex
.swizzles
[i
] = SWIZZLE_XYZW
;