2 * Copyright © 2019 Valve 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
31 enum chip_class chip_class
;
32 unsigned vcc_physical
;
35 /* just initialize these with something less than max NOPs */
36 int VALU_wrexec
= -10;
38 int VALU_wrsgpr
= -10;
41 int last_VMEM_since_scalar_write
= -1;
42 bool has_VOPC
= false;
44 NOP_ctx(Program
* program
) : chip_class(program
->chip_class
) {
45 vcc_physical
= program
->config
->num_sgprs
- 2;
49 bool VALU_writes_sgpr(aco_ptr
<Instruction
>& instr
)
51 if ((uint32_t) instr
->format
& (uint32_t) Format::VOPC
)
53 if (instr
->isVOP3() && instr
->definitions
.size() == 2)
55 if (instr
->opcode
== aco_opcode::v_readfirstlane_b32
|| instr
->opcode
== aco_opcode::v_readlane_b32
)
60 bool regs_intersect(PhysReg a_reg
, unsigned a_size
, PhysReg b_reg
, unsigned b_size
)
62 return a_reg
> b_reg
?
63 (a_reg
- b_reg
< b_size
) :
64 (b_reg
- a_reg
< a_size
);
67 unsigned handle_SMEM_clause(aco_ptr
<Instruction
>& instr
, int new_idx
,
68 std::vector
<aco_ptr
<Instruction
>>& new_instructions
)
70 //TODO: s_dcache_inv needs to be in it's own group on GFX10 (and previous versions?)
71 const bool is_store
= instr
->definitions
.empty();
72 for (int pred_idx
= new_idx
- 1; pred_idx
>= 0; pred_idx
--) {
73 aco_ptr
<Instruction
>& pred
= new_instructions
[pred_idx
];
74 if (pred
->format
!= Format::SMEM
)
77 /* Don't allow clauses with store instructions since the clause's
78 * instructions may use the same address. */
79 if (is_store
|| pred
->definitions
.empty())
82 Definition
& instr_def
= instr
->definitions
[0];
83 Definition
& pred_def
= pred
->definitions
[0];
85 /* ISA reference doesn't say anything about this, but best to be safe */
86 if (regs_intersect(instr_def
.physReg(), instr_def
.size(), pred_def
.physReg(), pred_def
.size()))
89 for (const Operand
& op
: pred
->operands
) {
90 if (op
.isConstant() || !op
.isFixed())
92 if (regs_intersect(instr_def
.physReg(), instr_def
.size(), op
.physReg(), op
.size()))
95 for (const Operand
& op
: instr
->operands
) {
96 if (op
.isConstant() || !op
.isFixed())
98 if (regs_intersect(pred_def
.physReg(), pred_def
.size(), op
.physReg(), op
.size()))
106 int handle_instruction(NOP_ctx
& ctx
, aco_ptr
<Instruction
>& instr
,
107 std::vector
<aco_ptr
<Instruction
>>& old_instructions
,
108 std::vector
<aco_ptr
<Instruction
>>& new_instructions
)
110 int new_idx
= new_instructions
.size();
112 // TODO: setreg / getreg / m0 writes
113 // TODO: try to schedule the NOP-causing instruction up to reduce the number of stall cycles
115 /* break off from prevous SMEM clause if needed */
116 if (instr
->format
== Format::SMEM
&& ctx
.chip_class
>= GFX8
) {
117 return handle_SMEM_clause(instr
, new_idx
, new_instructions
);
118 } else if (instr
->isVALU() || instr
->format
== Format::VINTRP
) {
121 if (instr
->isDPP()) {
122 /* VALU does not forward EXEC to DPP. */
123 if (ctx
.VALU_wrexec
+ 5 >= new_idx
)
124 NOPs
= 5 + ctx
.VALU_wrexec
- new_idx
+ 1;
126 /* VALU DPP reads VGPR written by VALU */
127 for (int pred_idx
= new_idx
- 1; pred_idx
>= 0 && pred_idx
>= new_idx
- 2; pred_idx
--) {
128 aco_ptr
<Instruction
>& pred
= new_instructions
[pred_idx
];
129 if ((pred
->isVALU() || pred
->format
== Format::VINTRP
) &&
130 !pred
->definitions
.empty() &&
131 pred
->definitions
[0].physReg() == instr
->operands
[0].physReg()) {
132 NOPs
= std::max(NOPs
, 2 + pred_idx
- new_idx
+ 1);
139 if (instr
->format
== Format::VINTRP
&& new_idx
> 0 && ctx
.chip_class
>= GFX9
) {
140 aco_ptr
<Instruction
>& pred
= new_instructions
.back();
141 if (pred
->isSALU() &&
142 !pred
->definitions
.empty() &&
143 pred
->definitions
[0].physReg() == m0
)
144 NOPs
= std::max(NOPs
, 1);
147 for (const Operand
& op
: instr
->operands
) {
148 /* VALU which uses VCCZ */
149 if (op
.physReg() == PhysReg
{251} &&
150 ctx
.VALU_wrvcc
+ 5 >= new_idx
)
151 NOPs
= std::max(NOPs
, 5 + ctx
.VALU_wrvcc
- new_idx
+ 1);
153 /* VALU which uses EXECZ */
154 if (op
.physReg() == PhysReg
{252} &&
155 ctx
.VALU_wrexec
+ 5 >= new_idx
)
156 NOPs
= std::max(NOPs
, 5 + ctx
.VALU_wrexec
- new_idx
+ 1);
158 /* VALU which reads VCC as a constant */
159 if (ctx
.VALU_wrvcc
+ 1 >= new_idx
) {
160 for (unsigned k
= 0; k
< op
.size(); k
++) {
161 unsigned reg
= op
.physReg() + k
;
162 if (reg
== ctx
.vcc_physical
|| reg
== ctx
.vcc_physical
+ 1)
163 NOPs
= std::max(NOPs
, 1);
168 switch (instr
->opcode
) {
169 case aco_opcode::v_readlane_b32
:
170 case aco_opcode::v_writelane_b32
: {
171 if (ctx
.VALU_wrsgpr
+ 4 < new_idx
)
173 PhysReg reg
= instr
->operands
[1].physReg();
174 for (int pred_idx
= new_idx
- 1; pred_idx
>= 0 && pred_idx
>= new_idx
- 4; pred_idx
--) {
175 aco_ptr
<Instruction
>& pred
= new_instructions
[pred_idx
];
176 if (!pred
->isVALU() || !VALU_writes_sgpr(pred
))
178 for (const Definition
& def
: pred
->definitions
) {
179 if (def
.physReg() == reg
)
180 NOPs
= std::max(NOPs
, 4 + pred_idx
- new_idx
+ 1);
185 case aco_opcode::v_div_fmas_f32
:
186 case aco_opcode::v_div_fmas_f64
: {
187 if (ctx
.VALU_wrvcc
+ 4 >= new_idx
)
188 NOPs
= std::max(NOPs
, 4 + ctx
.VALU_wrvcc
- new_idx
+ 1);
195 /* Write VGPRs holding writedata > 64 bit from MIMG/MUBUF instructions */
196 // FIXME: handle case if the last instruction of a block without branch is such store
197 // TODO: confirm that DS instructions cannot cause WAR hazards here
199 aco_ptr
<Instruction
>& pred
= new_instructions
.back();
200 if (pred
->isVMEM() &&
201 pred
->operands
.size() == 4 &&
202 pred
->operands
[3].size() > 2 &&
203 pred
->operands
[1].size() != 8 &&
204 (pred
->format
!= Format::MUBUF
|| pred
->operands
[2].physReg() >= 102)) {
205 /* Ops that use a 256-bit T# do not need a wait state.
206 * BUFFER_STORE_* operations that use an SGPR for "offset"
207 * do not require any wait states. */
208 PhysReg wrdata
= pred
->operands
[3].physReg();
209 unsigned size
= pred
->operands
[3].size();
210 assert(wrdata
>= 256);
211 for (const Definition
& def
: instr
->definitions
) {
212 if (regs_intersect(def
.physReg(), def
.size(), wrdata
, size
))
213 NOPs
= std::max(NOPs
, 1);
218 if (VALU_writes_sgpr(instr
)) {
219 for (const Definition
& def
: instr
->definitions
) {
220 if (def
.physReg() == vcc
)
221 ctx
.VALU_wrvcc
= NOPs
? new_idx
: new_idx
+ 1;
222 else if (def
.physReg() == exec
)
223 ctx
.VALU_wrexec
= NOPs
? new_idx
: new_idx
+ 1;
224 else if (def
.physReg() <= 102)
225 ctx
.VALU_wrsgpr
= NOPs
? new_idx
: new_idx
+ 1;
229 } else if (instr
->isVMEM() && ctx
.VALU_wrsgpr
+ 5 >= new_idx
) {
230 /* If the VALU writes the SGPR that is used by a VMEM, the user must add five wait states. */
231 for (int pred_idx
= new_idx
- 1; pred_idx
>= 0 && pred_idx
>= new_idx
- 5; pred_idx
--) {
232 aco_ptr
<Instruction
>& pred
= new_instructions
[pred_idx
];
233 if (!(pred
->isVALU() && VALU_writes_sgpr(pred
)))
236 for (const Definition
& def
: pred
->definitions
) {
237 if (def
.physReg() > 102)
240 if (instr
->operands
.size() > 1 &&
241 regs_intersect(instr
->operands
[1].physReg(), instr
->operands
[1].size(),
242 def
.physReg(), def
.size())) {
243 return 5 + pred_idx
- new_idx
+ 1;
246 if (instr
->operands
.size() > 2 &&
247 regs_intersect(instr
->operands
[2].physReg(), instr
->operands
[2].size(),
248 def
.physReg(), def
.size())) {
249 return 5 + pred_idx
- new_idx
+ 1;
258 std::pair
<int, int> handle_instruction_gfx10(NOP_ctx
& ctx
, aco_ptr
<Instruction
>& instr
,
259 std::vector
<aco_ptr
<Instruction
>>& old_instructions
,
260 std::vector
<aco_ptr
<Instruction
>>& new_instructions
)
262 int new_idx
= new_instructions
.size();
266 /* break off from prevous SMEM group ("clause" seems to mean something different in RDNA) if needed */
267 if (instr
->format
== Format::SMEM
)
268 sNOPs
= std::max(sNOPs
, handle_SMEM_clause(instr
, new_idx
, new_instructions
));
270 /* handle EXEC/M0/SGPR write following a VMEM instruction without a VALU or "waitcnt vmcnt(0)" in-between */
271 if (instr
->isSALU() || instr
->format
== Format::SMEM
) {
272 if (!instr
->definitions
.empty() && ctx
.last_VMEM_since_scalar_write
!= -1) {
273 ctx
.last_VMEM_since_scalar_write
= -1;
276 } else if (instr
->isVMEM() || instr
->isFlatOrGlobal()) {
277 ctx
.last_VMEM_since_scalar_write
= new_idx
;
278 } else if (instr
->opcode
== aco_opcode::s_waitcnt
) {
279 uint16_t imm
= static_cast<SOPP_instruction
*>(instr
.get())->imm
;
280 unsigned vmcnt
= (imm
& 0xF) | ((imm
& (0x3 << 14)) >> 10);
282 ctx
.last_VMEM_since_scalar_write
= -1;
283 } else if (instr
->isVALU()) {
284 ctx
.last_VMEM_since_scalar_write
= -1;
287 /* VcmpxPermlaneHazard
288 * Handle any permlane following a VOPC instruction, insert v_mov between them.
290 if (instr
->format
== Format::VOPC
) {
292 } else if (ctx
.has_VOPC
&&
293 (instr
->opcode
== aco_opcode::v_permlane16_b32
||
294 instr
->opcode
== aco_opcode::v_permlanex16_b32
)) {
295 ctx
.has_VOPC
= false;
297 /* v_nop would be discarded by SQ, so use v_mov with the first operand of the permlane */
298 aco_ptr
<VOP1_instruction
> v_mov
{create_instruction
<VOP1_instruction
>(aco_opcode::v_mov_b32
, Format::VOP1
, 1, 1)};
299 v_mov
->definitions
[0] = Definition(instr
->operands
[0].physReg(), v1
);
300 v_mov
->operands
[0] = Operand(instr
->operands
[0].physReg(), v1
);
301 new_instructions
.emplace_back(std::move(v_mov
));
302 } else if (instr
->isVALU() && instr
->opcode
!= aco_opcode::v_nop
) {
303 ctx
.has_VOPC
= false;
306 return std::make_pair(sNOPs
, vNOPs
);
310 void handle_block(NOP_ctx
& ctx
, Block
& block
)
312 std::vector
<aco_ptr
<Instruction
>> instructions
;
313 instructions
.reserve(block
.instructions
.size());
314 for (unsigned i
= 0; i
< block
.instructions
.size(); i
++) {
315 aco_ptr
<Instruction
>& instr
= block
.instructions
[i
];
316 unsigned NOPs
= handle_instruction(ctx
, instr
, block
.instructions
, instructions
);
318 // TODO: try to move the instruction down
320 aco_ptr
<SOPP_instruction
> nop
{create_instruction
<SOPP_instruction
>(aco_opcode::s_nop
, Format::SOPP
, 0, 0)};
323 instructions
.emplace_back(std::move(nop
));
326 instructions
.emplace_back(std::move(instr
));
329 ctx
.VALU_wrvcc
-= instructions
.size();
330 ctx
.VALU_wrexec
-= instructions
.size();
331 ctx
.VALU_wrsgpr
-= instructions
.size();
332 block
.instructions
= std::move(instructions
);
335 void handle_block_gfx10(NOP_ctx
& ctx
, Block
& block
)
337 std::vector
<aco_ptr
<Instruction
>> instructions
;
338 instructions
.reserve(block
.instructions
.size());
339 for (unsigned i
= 0; i
< block
.instructions
.size(); i
++) {
340 aco_ptr
<Instruction
>& instr
= block
.instructions
[i
];
341 std::pair
<int, int> NOPs
= handle_instruction_gfx10(ctx
, instr
, block
.instructions
, instructions
);
342 for (int i
= 0; i
< NOPs
.second
; i
++) {
343 // TODO: try to move the instruction down
345 aco_ptr
<VOP1_instruction
> nop
{create_instruction
<VOP1_instruction
>(aco_opcode::v_nop
, Format::VOP1
, 0, 0)};
346 instructions
.emplace_back(std::move(nop
));
349 // TODO: try to move the instruction down
351 aco_ptr
<SOPP_instruction
> nop
{create_instruction
<SOPP_instruction
>(aco_opcode::s_nop
, Format::SOPP
, 0, 0)};
352 nop
->imm
= NOPs
.first
- 1;
354 instructions
.emplace_back(std::move(nop
));
357 instructions
.emplace_back(std::move(instr
));
360 block
.instructions
= std::move(instructions
);
363 } /* end namespace */
366 void insert_NOPs(Program
* program
)
368 NOP_ctx
ctx(program
);
370 for (Block
& block
: program
->blocks
) {
371 if (block
.instructions
.empty())
374 if (ctx
.chip_class
>= GFX10
)
375 handle_block_gfx10(ctx
, block
);
377 handle_block(ctx
, block
);