2 * Copyright © 2014 Broadcom
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
26 #include "vc4_context.h"
29 #include "util/ralloc.h"
32 vc4_dump_program(struct vc4_compile
*c
)
34 fprintf(stderr
, "%s prog %d/%d QPU:\n",
35 qir_get_stage_name(c
->stage
),
36 c
->program_id
, c
->variant_id
);
38 for (int i
= 0; i
< c
->qpu_inst_count
; i
++) {
39 fprintf(stderr
, "0x%016"PRIx64
" ", c
->qpu_insts
[i
]);
40 vc4_qpu_disasm(&c
->qpu_insts
[i
], 1);
41 fprintf(stderr
, "\n");
43 fprintf(stderr
, "\n");
47 queue(struct vc4_compile
*c
, uint64_t inst
)
49 struct queued_qpu_inst
*q
= rzalloc(c
, struct queued_qpu_inst
);
51 list_addtail(&q
->link
, &c
->qpu_inst_list
);
55 last_inst(struct vc4_compile
*c
)
57 struct queued_qpu_inst
*q
=
58 (struct queued_qpu_inst
*)c
->qpu_inst_list
.prev
;
63 set_last_cond_add(struct vc4_compile
*c
, uint32_t cond
)
65 *last_inst(c
) = qpu_set_cond_add(*last_inst(c
), cond
);
69 set_last_cond_mul(struct vc4_compile
*c
, uint32_t cond
)
71 *last_inst(c
) = qpu_set_cond_mul(*last_inst(c
), cond
);
75 * Some special registers can be read from either file, which lets us resolve
76 * raddr conflicts without extra MOVs.
79 swap_file(struct qpu_reg
*src
)
84 if (src
->mux
== QPU_MUX_SMALL_IMM
) {
87 if (src
->mux
== QPU_MUX_A
)
100 * This is used to resolve the fact that we might register-allocate two
101 * different operands of an instruction to the same physical register file
102 * even though instructions have only one field for the register file source
105 * In that case, we need to move one to a temporary that can be used in the
106 * instruction, instead. We reserve ra31/rb31 for this purpose.
109 fixup_raddr_conflict(struct vc4_compile
*c
,
111 struct qpu_reg
*src0
, struct qpu_reg
*src1
,
112 struct qinst
*inst
, uint64_t *unpack
)
114 uint32_t mux0
= src0
->mux
== QPU_MUX_SMALL_IMM
? QPU_MUX_B
: src0
->mux
;
115 uint32_t mux1
= src1
->mux
== QPU_MUX_SMALL_IMM
? QPU_MUX_B
: src1
->mux
;
117 if (mux0
<= QPU_MUX_R5
||
119 (src0
->addr
== src1
->addr
&&
120 src0
->mux
== src1
->mux
)) {
124 if (swap_file(src0
) || swap_file(src1
))
127 if (mux0
== QPU_MUX_A
) {
128 /* Make sure we use the same type of MOV as the instruction,
129 * in case of unpacks.
131 if (qir_is_float_input(inst
))
132 queue(c
, qpu_a_FMAX(qpu_rb(31), *src0
, *src0
));
134 queue(c
, qpu_a_MOV(qpu_rb(31), *src0
));
136 /* If we had an unpack on this A-file source, we need to put
137 * it into this MOV, not into the later move from regfile B.
139 if (inst
->src
[0].pack
) {
140 *last_inst(c
) |= *unpack
;
145 queue(c
, qpu_a_MOV(qpu_ra(31), *src0
));
151 set_last_dst_pack(struct vc4_compile
*c
, struct qinst
*inst
)
153 bool had_pm
= *last_inst(c
) & QPU_PM
;
154 bool had_ws
= *last_inst(c
) & QPU_WS
;
155 uint32_t unpack
= QPU_GET_FIELD(*last_inst(c
), QPU_UNPACK
);
160 *last_inst(c
) |= QPU_SET_FIELD(inst
->dst
.pack
, QPU_PACK
);
162 if (qir_is_mul(inst
)) {
163 assert(!unpack
|| had_pm
);
164 *last_inst(c
) |= QPU_PM
;
166 assert(!unpack
|| !had_pm
);
167 assert(!had_ws
); /* dst must be a-file to pack. */
172 handle_r4_qpu_write(struct vc4_compile
*c
, struct qinst
*qinst
,
175 if (dst
.mux
!= QPU_MUX_R4
)
176 queue(c
, qpu_a_MOV(dst
, qpu_r4()));
178 queue(c
, qpu_a_MOV(qpu_ra(QPU_W_NOP
), qpu_r4()));
182 vc4_generate_code(struct vc4_context
*vc4
, struct vc4_compile
*c
)
184 struct qpu_reg
*temp_registers
= vc4_register_allocate(vc4
, c
);
185 uint32_t inputs_remaining
= c
->num_inputs
;
186 uint32_t vpm_read_fifo_count
= 0;
187 uint32_t vpm_read_offset
= 0;
188 int last_vpm_read_index
= -1;
190 list_inithead(&c
->qpu_inst_list
);
195 /* There's a 4-entry FIFO for VPMVCD reads, each of which can
196 * load up to 16 dwords (4 vec4s) per vertex.
198 while (inputs_remaining
) {
199 uint32_t num_entries
= MIN2(inputs_remaining
, 16);
200 queue(c
, qpu_load_imm_ui(qpu_vrsetup(),
203 ((num_entries
& 0xf) << 20)));
204 inputs_remaining
-= num_entries
;
205 vpm_read_offset
+= num_entries
;
206 vpm_read_fifo_count
++;
208 assert(vpm_read_fifo_count
<= 4);
210 queue(c
, qpu_load_imm_ui(qpu_vwsetup(), 0x00001a00));
216 list_for_each_entry(struct qinst
, qinst
, &c
->instructions
, link
) {
218 fprintf(stderr
, "translating qinst to qpu: ");
219 qir_dump_inst(qinst
);
220 fprintf(stderr
, "\n");
223 static const struct {
226 #define A(name) [QOP_##name] = {QPU_A_##name}
227 #define M(name) [QOP_##name] = {QPU_M_##name}
256 /* If we replicate src[0] out to src[1], this works
257 * out the same as a MOV.
259 [QOP_MOV
] = { QPU_A_OR
},
260 [QOP_FMOV
] = { QPU_A_FMAX
},
261 [QOP_MMOV
] = { QPU_M_V8MIN
},
265 struct qpu_reg src
[4];
266 for (int i
= 0; i
< qir_get_op_nsrc(qinst
->op
); i
++) {
267 int index
= qinst
->src
[i
].index
;
268 switch (qinst
->src
[i
].file
) {
273 src
[i
] = temp_registers
[index
];
274 if (qinst
->src
[i
].pack
) {
276 unpack
== qinst
->src
[i
].pack
);
277 unpack
= QPU_SET_FIELD(qinst
->src
[i
].pack
,
279 if (src
[i
].mux
== QPU_MUX_R4
)
289 case QFILE_SMALL_IMM
:
290 src
[i
].mux
= QPU_MUX_SMALL_IMM
;
291 src
[i
].addr
= qpu_encode_small_immediate(qinst
->src
[i
].index
);
292 /* This should only have returned a valid
293 * small immediate field, not ~0 for failure.
295 assert(src
[i
].addr
<= 47);
298 assert((int)qinst
->src
[i
].index
>=
299 last_vpm_read_index
);
300 (void)last_vpm_read_index
;
301 last_vpm_read_index
= qinst
->src
[i
].index
;
302 src
[i
] = qpu_ra(QPU_R_VPM
);
306 src
[i
] = qpu_ra(QPU_R_XY_PIXEL_COORD
);
309 src
[i
] = qpu_rb(QPU_R_XY_PIXEL_COORD
);
311 case QFILE_FRAG_REV_FLAG
:
312 src
[i
] = qpu_rb(QPU_R_MS_REV_FLAGS
);
315 case QFILE_TLB_COLOR_WRITE
:
316 case QFILE_TLB_COLOR_WRITE_MS
:
317 case QFILE_TLB_Z_WRITE
:
318 case QFILE_TLB_STENCIL_SETUP
:
319 unreachable("bad qir src file");
324 switch (qinst
->dst
.file
) {
326 dst
= qpu_ra(QPU_W_NOP
);
329 dst
= temp_registers
[qinst
->dst
.index
];
332 dst
= qpu_ra(QPU_W_VPM
);
335 case QFILE_TLB_COLOR_WRITE
:
339 case QFILE_TLB_COLOR_WRITE_MS
:
343 case QFILE_TLB_Z_WRITE
:
344 dst
= qpu_ra(QPU_W_TLB_Z
);
347 case QFILE_TLB_STENCIL_SETUP
:
348 dst
= qpu_ra(QPU_W_TLB_STENCIL_SETUP
);
353 case QFILE_SMALL_IMM
:
356 case QFILE_FRAG_REV_FLAG
:
357 assert(!"not reached");
361 bool handled_qinst_cond
= false;
370 queue(c
, qpu_a_MOV(qpu_rb(QPU_W_SFU_RECIP
),
374 queue(c
, qpu_a_MOV(qpu_rb(QPU_W_SFU_RECIPSQRT
),
378 queue(c
, qpu_a_MOV(qpu_rb(QPU_W_SFU_EXP
),
382 queue(c
, qpu_a_MOV(qpu_rb(QPU_W_SFU_LOG
),
389 handle_r4_qpu_write(c
, qinst
, dst
);
394 src
[1] = qpu_ra(QPU_R_MS_REV_FLAGS
);
395 fixup_raddr_conflict(c
, dst
, &src
[0], &src
[1],
397 queue(c
, qpu_a_AND(qpu_ra(QPU_W_MS_FLAGS
),
398 src
[0], src
[1]) | unpack
);
403 /* QOP_FRAG_Z/W don't emit instructions, just allocate
404 * the register to the Z/W payload.
408 case QOP_TLB_COLOR_READ
:
410 *last_inst(c
) = qpu_set_sig(*last_inst(c
),
412 handle_r4_qpu_write(c
, qinst
, dst
);
416 queue(c
, qpu_a_FADD(dst
, src
[0], qpu_r5()) | unpack
);
423 queue(c
, qpu_a_MOV(qpu_rb(QPU_W_TMU0_S
+
424 (qinst
->op
- QOP_TEX_S
)),
429 fixup_raddr_conflict(c
, dst
, &src
[0], &src
[1],
431 queue(c
, qpu_a_ADD(qpu_rb(QPU_W_TMU0_S
),
432 src
[0], src
[1]) | unpack
);
437 *last_inst(c
) = qpu_set_sig(*last_inst(c
),
439 handle_r4_qpu_write(c
, qinst
, dst
);
443 assert(qinst
->op
< ARRAY_SIZE(translate
));
444 assert(translate
[qinst
->op
].op
!= 0); /* NOPs */
446 /* Skip emitting the MOV if it's a no-op. */
447 if (qir_is_raw_mov(qinst
) &&
448 dst
.mux
== src
[0].mux
&& dst
.addr
== src
[0].addr
) {
452 /* If we have only one source, put it in the second
453 * argument slot as well so that we don't take up
454 * another raddr just to get unused data.
456 if (qir_get_op_nsrc(qinst
->op
) == 1)
459 fixup_raddr_conflict(c
, dst
, &src
[0], &src
[1],
462 if (qir_is_mul(qinst
)) {
463 queue(c
, qpu_m_alu2(translate
[qinst
->op
].op
,
465 src
[0], src
[1]) | unpack
);
466 set_last_cond_mul(c
, qinst
->cond
);
468 queue(c
, qpu_a_alu2(translate
[qinst
->op
].op
,
470 src
[0], src
[1]) | unpack
);
471 set_last_cond_add(c
, qinst
->cond
);
473 handled_qinst_cond
= true;
474 set_last_dst_pack(c
, qinst
);
479 assert(qinst
->cond
== QPU_COND_ALWAYS
||
483 *last_inst(c
) |= QPU_SF
;
486 uint32_t cycles
= qpu_schedule_instructions(c
);
487 uint32_t inst_count_at_schedule_time
= c
->qpu_inst_count
;
489 /* thread end can't have VPM write or read */
490 if (QPU_GET_FIELD(c
->qpu_insts
[c
->qpu_inst_count
- 1],
491 QPU_WADDR_ADD
) == QPU_W_VPM
||
492 QPU_GET_FIELD(c
->qpu_insts
[c
->qpu_inst_count
- 1],
493 QPU_WADDR_MUL
) == QPU_W_VPM
||
494 QPU_GET_FIELD(c
->qpu_insts
[c
->qpu_inst_count
- 1],
495 QPU_RADDR_A
) == QPU_R_VPM
||
496 QPU_GET_FIELD(c
->qpu_insts
[c
->qpu_inst_count
- 1],
497 QPU_RADDR_B
) == QPU_R_VPM
) {
498 qpu_serialize_one_inst(c
, qpu_NOP());
501 /* thread end can't have uniform read */
502 if (QPU_GET_FIELD(c
->qpu_insts
[c
->qpu_inst_count
- 1],
503 QPU_RADDR_A
) == QPU_R_UNIF
||
504 QPU_GET_FIELD(c
->qpu_insts
[c
->qpu_inst_count
- 1],
505 QPU_RADDR_B
) == QPU_R_UNIF
) {
506 qpu_serialize_one_inst(c
, qpu_NOP());
509 /* thread end can't have TLB operations */
510 if (qpu_inst_is_tlb(c
->qpu_insts
[c
->qpu_inst_count
- 1]))
511 qpu_serialize_one_inst(c
, qpu_NOP());
513 c
->qpu_insts
[c
->qpu_inst_count
- 1] =
514 qpu_set_sig(c
->qpu_insts
[c
->qpu_inst_count
- 1],
516 qpu_serialize_one_inst(c
, qpu_NOP());
517 qpu_serialize_one_inst(c
, qpu_NOP());
524 c
->qpu_insts
[c
->qpu_inst_count
- 1] =
525 qpu_set_sig(c
->qpu_insts
[c
->qpu_inst_count
- 1],
526 QPU_SIG_SCOREBOARD_UNLOCK
);
530 cycles
+= c
->qpu_inst_count
- inst_count_at_schedule_time
;
532 if (vc4_debug
& VC4_DEBUG_SHADERDB
) {
533 fprintf(stderr
, "SHADER-DB: %s prog %d/%d: %d estimated cycles\n",
534 qir_get_stage_name(c
->stage
),
535 c
->program_id
, c
->variant_id
,
539 if (vc4_debug
& VC4_DEBUG_QPU
)
542 vc4_qpu_validate(c
->qpu_insts
, c
->qpu_inst_count
);
544 free(temp_registers
);