2 * Copyright © 2018 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
32 static void aco_log(Program
*program
, const char *prefix
,
33 const char *file
, unsigned line
,
34 const char *fmt
, va_list args
)
38 msg
= ralloc_strdup(NULL
, prefix
);
40 ralloc_asprintf_append(&msg
, " In file %s:%u\n", file
, line
);
41 ralloc_asprintf_append(&msg
, " ");
42 ralloc_vasprintf_append(&msg
, fmt
, args
);
44 /* TODO: log messages via callback if available too. */
46 fprintf(stderr
, "%s\n", msg
);
51 void _aco_perfwarn(Program
*program
, const char *file
, unsigned line
,
57 aco_log(program
, "ACO PERFWARN:\n", file
, line
, fmt
, args
);
61 void _aco_err(Program
*program
, const char *file
, unsigned line
,
67 aco_log(program
, "ACO ERROR:\n", file
, line
, fmt
, args
);
71 bool validate_ir(Program
* program
)
74 auto check
= [&program
, &is_valid
](bool check
, const char * msg
, aco::Instruction
* instr
) -> void {
78 FILE *memf
= open_memstream(&out
, &outsize
);
80 fprintf(memf
, "%s: ", msg
);
81 aco_print_instr(instr
, memf
);
84 aco_err(program
, out
);
91 auto check_block
= [&program
, &is_valid
](bool check
, const char * msg
, aco::Block
* block
) -> void {
93 aco_err(program
, "%s: BB%u", msg
, block
->index
);
98 for (Block
& block
: program
->blocks
) {
99 for (aco_ptr
<Instruction
>& instr
: block
.instructions
) {
101 /* check base format */
102 Format base_format
= instr
->format
;
103 base_format
= (Format
)((uint32_t)base_format
& ~(uint32_t)Format::SDWA
);
104 base_format
= (Format
)((uint32_t)base_format
& ~(uint32_t)Format::DPP
);
105 if ((uint32_t)base_format
& (uint32_t)Format::VOP1
)
106 base_format
= Format::VOP1
;
107 else if ((uint32_t)base_format
& (uint32_t)Format::VOP2
)
108 base_format
= Format::VOP2
;
109 else if ((uint32_t)base_format
& (uint32_t)Format::VOPC
)
110 base_format
= Format::VOPC
;
111 else if ((uint32_t)base_format
& (uint32_t)Format::VINTRP
) {
112 if (instr
->opcode
== aco_opcode::v_interp_p1ll_f16
||
113 instr
->opcode
== aco_opcode::v_interp_p1lv_f16
||
114 instr
->opcode
== aco_opcode::v_interp_p2_legacy_f16
||
115 instr
->opcode
== aco_opcode::v_interp_p2_f16
) {
116 /* v_interp_*_fp16 are considered VINTRP by the compiler but
117 * they are emitted as VOP3.
119 base_format
= Format::VOP3
;
121 base_format
= Format::VINTRP
;
124 check(base_format
== instr_info
.format
[(int)instr
->opcode
], "Wrong base format for instruction", instr
.get());
126 /* check VOP3 modifiers */
127 if (((uint32_t)instr
->format
& (uint32_t)Format::VOP3
) && instr
->format
!= Format::VOP3
) {
128 check(base_format
== Format::VOP2
||
129 base_format
== Format::VOP1
||
130 base_format
== Format::VOPC
||
131 base_format
== Format::VINTRP
,
132 "Format cannot have VOP3A/VOP3B applied", instr
.get());
136 if (instr
->isSDWA()) {
137 check(base_format
== Format::VOP2
||
138 base_format
== Format::VOP1
||
139 base_format
== Format::VOPC
,
140 "Format cannot have SDWA applied", instr
.get());
142 check(program
->chip_class
>= GFX8
, "SDWA is GFX8+ only", instr
.get());
144 SDWA_instruction
*sdwa
= static_cast<SDWA_instruction
*>(instr
.get());
145 check(sdwa
->omod
== 0 || program
->chip_class
>= GFX9
, "SDWA omod only supported on GFX9+", instr
.get());
146 if (base_format
== Format::VOPC
) {
147 check(sdwa
->clamp
== false || program
->chip_class
== GFX8
, "SDWA VOPC clamp only supported on GFX8", instr
.get());
148 check((instr
->definitions
[0].isFixed() && instr
->definitions
[0].physReg() == vcc
) ||
149 program
->chip_class
>= GFX9
,
150 "SDWA+VOPC definition must be fixed to vcc on GFX8", instr
.get());
153 if (instr
->operands
.size() >= 3) {
154 check(instr
->operands
[2].isFixed() && instr
->operands
[2].physReg() == vcc
,
155 "3rd operand must be fixed to vcc with SDWA", instr
.get());
157 if (instr
->definitions
.size() >= 2) {
158 check(instr
->definitions
[1].isFixed() && instr
->definitions
[1].physReg() == vcc
,
159 "2nd definition must be fixed to vcc with SDWA", instr
.get());
162 check(instr
->opcode
!= aco_opcode::v_madmk_f32
&&
163 instr
->opcode
!= aco_opcode::v_madak_f32
&&
164 instr
->opcode
!= aco_opcode::v_madmk_f16
&&
165 instr
->opcode
!= aco_opcode::v_madak_f16
&&
166 instr
->opcode
!= aco_opcode::v_readfirstlane_b32
&&
167 instr
->opcode
!= aco_opcode::v_clrexcp
&&
168 instr
->opcode
!= aco_opcode::v_swap_b32
,
169 "SDWA can't be used with this opcode", instr
.get());
170 if (program
->chip_class
!= GFX8
) {
171 check(instr
->opcode
!= aco_opcode::v_mac_f32
&&
172 instr
->opcode
!= aco_opcode::v_mac_f16
&&
173 instr
->opcode
!= aco_opcode::v_fmac_f32
&&
174 instr
->opcode
!= aco_opcode::v_fmac_f16
,
175 "SDWA can't be used with this opcode", instr
.get());
178 for (unsigned i
= 0; i
< MIN2(instr
->operands
.size(), 2); i
++) {
179 if (instr
->operands
[i
].hasRegClass() && instr
->operands
[i
].regClass().is_subdword())
180 check((sdwa
->sel
[i
] & sdwa_asuint
) == (sdwa_isra
| instr
->operands
[i
].bytes()), "Unexpected SDWA sel for sub-dword operand", instr
.get());
182 if (instr
->definitions
[0].regClass().is_subdword())
183 check((sdwa
->dst_sel
& sdwa_asuint
) == (sdwa_isra
| instr
->definitions
[0].bytes()), "Unexpected SDWA sel for sub-dword definition", instr
.get());
187 if (instr
->isVOP3()) {
188 VOP3A_instruction
*vop3
= static_cast<VOP3A_instruction
*>(instr
.get());
189 check(vop3
->opsel
== 0 || program
->chip_class
>= GFX9
, "Opsel is only supported on GFX9+", instr
.get());
191 for (unsigned i
= 0; i
< 3; i
++) {
192 if (i
>= instr
->operands
.size() ||
193 (instr
->operands
[i
].hasRegClass() && instr
->operands
[i
].regClass().is_subdword() && !instr
->operands
[i
].isFixed()))
194 check((vop3
->opsel
& (1 << i
)) == 0, "Unexpected opsel for operand", instr
.get());
196 if (instr
->definitions
[0].regClass().is_subdword() && !instr
->definitions
[0].isFixed())
197 check((vop3
->opsel
& (1 << 3)) == 0, "Unexpected opsel for sub-dword definition", instr
.get());
200 /* check for undefs */
201 for (unsigned i
= 0; i
< instr
->operands
.size(); i
++) {
202 if (instr
->operands
[i
].isUndefined()) {
203 bool flat
= instr
->format
== Format::FLAT
|| instr
->format
== Format::SCRATCH
|| instr
->format
== Format::GLOBAL
;
204 bool can_be_undef
= is_phi(instr
) || instr
->format
== Format::EXP
||
205 instr
->format
== Format::PSEUDO_REDUCTION
||
206 instr
->opcode
== aco_opcode::p_create_vector
||
207 (flat
&& i
== 1) || (instr
->format
== Format::MIMG
&& i
== 1) ||
208 ((instr
->format
== Format::MUBUF
|| instr
->format
== Format::MTBUF
) && i
== 1);
209 check(can_be_undef
, "Undefs can only be used in certain operands", instr
.get());
211 check(instr
->operands
[i
].isFixed() || instr
->operands
[i
].isTemp() || instr
->operands
[i
].isConstant(), "Uninitialized Operand", instr
.get());
215 /* check subdword definitions */
216 for (unsigned i
= 0; i
< instr
->definitions
.size(); i
++) {
217 if (instr
->definitions
[i
].regClass().is_subdword())
218 check(instr
->format
== Format::PSEUDO
|| instr
->definitions
[i
].bytes() <= 4, "Only Pseudo instructions can write subdword registers larger than 4 bytes", instr
.get());
221 if (instr
->isSALU() || instr
->isVALU()) {
224 for (unsigned i
= 0; i
< instr
->operands
.size(); i
++)
226 Operand op
= instr
->operands
[i
];
230 check(instr
->format
== Format::SOP1
||
231 instr
->format
== Format::SOP2
||
232 instr
->format
== Format::SOPC
||
233 instr
->format
== Format::VOP1
||
234 instr
->format
== Format::VOP2
||
235 instr
->format
== Format::VOPC
||
236 (instr
->isVOP3() && program
->chip_class
>= GFX10
),
237 "Literal applied on wrong instruction format", instr
.get());
239 check(literal
.isUndefined() || (literal
.size() == op
.size() && literal
.constantValue() == op
.constantValue()), "Only 1 Literal allowed", instr
.get());
241 check(!instr
->isVALU() || instr
->isVOP3() || i
== 0 || i
== 2, "Wrong source position for Literal argument", instr
.get());
244 /* check num sgprs for VALU */
245 if (instr
->isVALU()) {
246 bool is_shift64
= instr
->opcode
== aco_opcode::v_lshlrev_b64
||
247 instr
->opcode
== aco_opcode::v_lshrrev_b64
||
248 instr
->opcode
== aco_opcode::v_ashrrev_i64
;
249 unsigned const_bus_limit
= 1;
250 if (program
->chip_class
>= GFX10
&& !is_shift64
)
253 uint32_t scalar_mask
= instr
->isVOP3() ? 0x7 : 0x5;
255 scalar_mask
= program
->chip_class
>= GFX9
? 0x7 : 0x4;
257 if ((int) instr
->format
& (int) Format::VOPC
||
258 instr
->opcode
== aco_opcode::v_readfirstlane_b32
||
259 instr
->opcode
== aco_opcode::v_readlane_b32
||
260 instr
->opcode
== aco_opcode::v_readlane_b32_e64
) {
261 check(instr
->definitions
[0].getTemp().type() == RegType::sgpr
,
262 "Wrong Definition type for VALU instruction", instr
.get());
264 check(instr
->definitions
[0].getTemp().type() == RegType::vgpr
,
265 "Wrong Definition type for VALU instruction", instr
.get());
268 unsigned num_sgprs
= 0;
269 unsigned sgpr
[] = {0, 0};
270 for (unsigned i
= 0; i
< instr
->operands
.size(); i
++)
272 Operand op
= instr
->operands
[i
];
273 if (instr
->opcode
== aco_opcode::v_readfirstlane_b32
||
274 instr
->opcode
== aco_opcode::v_readlane_b32
||
275 instr
->opcode
== aco_opcode::v_readlane_b32_e64
) {
277 (op
.isTemp() && op
.regClass().type() == RegType::sgpr
) ||
279 "Must be a SGPR or a constant", instr
.get());
281 (op
.isTemp() && op
.regClass().type() == RegType::vgpr
&& op
.bytes() <= 4),
282 "Wrong Operand type for VALU instruction", instr
.get());
286 if (instr
->opcode
== aco_opcode::v_writelane_b32
||
287 instr
->opcode
== aco_opcode::v_writelane_b32_e64
) {
289 (op
.isTemp() && op
.regClass().type() == RegType::vgpr
&& op
.bytes() <= 4),
290 "Wrong Operand type for VALU instruction", instr
.get());
292 (op
.isTemp() && op
.regClass().type() == RegType::sgpr
) ||
294 "Must be a SGPR or a constant", instr
.get());
297 if (op
.isTemp() && instr
->operands
[i
].regClass().type() == RegType::sgpr
) {
298 check(scalar_mask
& (1 << i
), "Wrong source position for SGPR argument", instr
.get());
300 if (op
.tempId() != sgpr
[0] && op
.tempId() != sgpr
[1]) {
302 sgpr
[num_sgprs
++] = op
.tempId();
306 if (op
.isConstant() && !op
.isLiteral())
307 check(scalar_mask
& (1 << i
), "Wrong source position for constant argument", instr
.get());
309 check(num_sgprs
+ (literal
.isUndefined() ? 0 : 1) <= const_bus_limit
, "Too many SGPRs/literals", instr
.get());
312 if (instr
->format
== Format::SOP1
|| instr
->format
== Format::SOP2
) {
313 check(instr
->definitions
[0].getTemp().type() == RegType::sgpr
, "Wrong Definition type for SALU instruction", instr
.get());
314 for (const Operand
& op
: instr
->operands
) {
315 check(op
.isConstant() || op
.regClass().type() <= RegType::sgpr
,
316 "Wrong Operand type for SALU instruction", instr
.get());
321 switch (instr
->format
) {
322 case Format::PSEUDO
: {
323 bool is_subdword
= false;
324 bool has_const_sgpr
= false;
325 bool has_literal
= false;
326 for (Definition def
: instr
->definitions
)
327 is_subdword
|= def
.regClass().is_subdword();
328 for (unsigned i
= 0; i
< instr
->operands
.size(); i
++) {
329 if (instr
->opcode
== aco_opcode::p_extract_vector
&& i
== 1)
331 Operand op
= instr
->operands
[i
];
332 is_subdword
|= op
.hasRegClass() && op
.regClass().is_subdword();
333 has_const_sgpr
|= op
.isConstant() || (op
.hasRegClass() && op
.regClass().type() == RegType::sgpr
);
334 has_literal
|= op
.isLiteral();
337 check(!is_subdword
|| !has_const_sgpr
|| program
->chip_class
>= GFX9
,
338 "Sub-dword pseudo instructions can only take constants or SGPRs on GFX9+", instr
.get());
339 check(!is_subdword
|| !has_literal
, "Sub-dword pseudo instructions cannot take literals", instr
.get());
341 if (instr
->opcode
== aco_opcode::p_create_vector
) {
343 for (const Operand
& op
: instr
->operands
) {
346 check(size
== instr
->definitions
[0].bytes(), "Definition size does not match operand sizes", instr
.get());
347 if (instr
->definitions
[0].getTemp().type() == RegType::sgpr
) {
348 for (const Operand
& op
: instr
->operands
) {
349 check(op
.isConstant() || op
.regClass().type() == RegType::sgpr
,
350 "Wrong Operand type for scalar vector", instr
.get());
353 } else if (instr
->opcode
== aco_opcode::p_extract_vector
) {
354 check((instr
->operands
[0].isTemp()) && instr
->operands
[1].isConstant(), "Wrong Operand types", instr
.get());
355 check((instr
->operands
[1].constantValue() + 1) * instr
->definitions
[0].bytes() <= instr
->operands
[0].bytes(), "Index out of range", instr
.get());
356 check(instr
->definitions
[0].getTemp().type() == RegType::vgpr
|| instr
->operands
[0].regClass().type() == RegType::sgpr
,
357 "Cannot extract SGPR value from VGPR vector", instr
.get());
358 } else if (instr
->opcode
== aco_opcode::p_parallelcopy
) {
359 check(instr
->definitions
.size() == instr
->operands
.size(), "Number of Operands does not match number of Definitions", instr
.get());
360 for (unsigned i
= 0; i
< instr
->operands
.size(); i
++) {
361 if (instr
->operands
[i
].isTemp())
362 check((instr
->definitions
[i
].getTemp().type() == instr
->operands
[i
].regClass().type()) ||
363 (instr
->definitions
[i
].getTemp().type() == RegType::vgpr
&& instr
->operands
[i
].regClass().type() == RegType::sgpr
),
364 "Operand and Definition types do not match", instr
.get());
366 } else if (instr
->opcode
== aco_opcode::p_phi
) {
367 check(instr
->operands
.size() == block
.logical_preds
.size(), "Number of Operands does not match number of predecessors", instr
.get());
368 check(instr
->definitions
[0].getTemp().type() == RegType::vgpr
, "Logical Phi Definition must be vgpr", instr
.get());
369 } else if (instr
->opcode
== aco_opcode::p_linear_phi
) {
370 for (const Operand
& op
: instr
->operands
)
371 check(!op
.isTemp() || op
.getTemp().is_linear(), "Wrong Operand type", instr
.get());
372 check(instr
->operands
.size() == block
.linear_preds
.size(), "Number of Operands does not match number of predecessors", instr
.get());
377 if (instr
->operands
.size() >= 1)
378 check(instr
->operands
[0].isTemp() && instr
->operands
[0].regClass().type() == RegType::sgpr
, "SMEM operands must be sgpr", instr
.get());
379 if (instr
->operands
.size() >= 2)
380 check(instr
->operands
[1].isConstant() || (instr
->operands
[1].isTemp() && instr
->operands
[1].regClass().type() == RegType::sgpr
),
381 "SMEM offset must be constant or sgpr", instr
.get());
382 if (!instr
->definitions
.empty())
383 check(instr
->definitions
[0].getTemp().type() == RegType::sgpr
, "SMEM result must be sgpr", instr
.get());
387 case Format::MUBUF
: {
388 check(instr
->operands
.size() > 1, "VMEM instructions must have at least one operand", instr
.get());
389 check(instr
->operands
[1].hasRegClass() && instr
->operands
[1].regClass().type() == RegType::vgpr
,
390 "VADDR must be in vgpr for VMEM instructions", instr
.get());
391 check(instr
->operands
[0].isTemp() && instr
->operands
[0].regClass().type() == RegType::sgpr
, "VMEM resource constant must be sgpr", instr
.get());
392 check(instr
->operands
.size() < 4 || (instr
->operands
[3].isTemp() && instr
->operands
[3].regClass().type() == RegType::vgpr
), "VMEM write data must be vgpr", instr
.get());
396 check(instr
->operands
.size() == 3, "MIMG instructions must have exactly 3 operands", instr
.get());
397 check(instr
->operands
[0].hasRegClass() && (instr
->operands
[0].regClass() == s4
|| instr
->operands
[0].regClass() == s8
),
398 "MIMG operands[0] (resource constant) must be in 4 or 8 SGPRs", instr
.get());
399 if (instr
->operands
[1].hasRegClass() && instr
->operands
[1].regClass().type() == RegType::sgpr
)
400 check(instr
->operands
[1].regClass() == s4
, "MIMG operands[1] (sampler constant) must be 4 SGPRs", instr
.get());
401 else if (instr
->operands
[1].hasRegClass() && instr
->operands
[1].regClass().type() == RegType::vgpr
)
402 check((instr
->definitions
.empty() || instr
->definitions
[0].regClass() == instr
->operands
[1].regClass() ||
403 instr
->opcode
== aco_opcode::image_atomic_cmpswap
|| instr
->opcode
== aco_opcode::image_atomic_fcmpswap
),
404 "MIMG operands[1] (VDATA) must be the same as definitions[0] for atomics", instr
.get());
405 check(instr
->operands
[2].hasRegClass() && instr
->operands
[2].regClass().type() == RegType::vgpr
,
406 "MIMG operands[2] (VADDR) must be VGPR", instr
.get());
407 check(instr
->definitions
.empty() || (instr
->definitions
[0].isTemp() && instr
->definitions
[0].regClass().type() == RegType::vgpr
),
408 "MIMG definitions[0] (VDATA) must be VGPR", instr
.get());
412 for (const Operand
& op
: instr
->operands
) {
413 check((op
.isTemp() && op
.regClass().type() == RegType::vgpr
) || op
.physReg() == m0
,
414 "Only VGPRs are valid DS instruction operands", instr
.get());
416 if (!instr
->definitions
.empty())
417 check(instr
->definitions
[0].getTemp().type() == RegType::vgpr
, "DS instruction must return VGPR", instr
.get());
421 for (unsigned i
= 0; i
< 4; i
++)
422 check(instr
->operands
[i
].hasRegClass() && instr
->operands
[i
].regClass().type() == RegType::vgpr
,
423 "Only VGPRs are valid Export arguments", instr
.get());
427 check(instr
->operands
[1].isUndefined(), "Flat instructions don't support SADDR", instr
.get());
430 case Format::SCRATCH
: {
431 check(instr
->operands
[0].isTemp() && instr
->operands
[0].regClass().type() == RegType::vgpr
, "FLAT/GLOBAL/SCRATCH address must be vgpr", instr
.get());
432 check(instr
->operands
[1].hasRegClass() && instr
->operands
[1].regClass().type() == RegType::sgpr
,
433 "FLAT/GLOBAL/SCRATCH sgpr address must be undefined or sgpr", instr
.get());
434 if (!instr
->definitions
.empty())
435 check(instr
->definitions
[0].getTemp().type() == RegType::vgpr
, "FLAT/GLOBAL/SCRATCH result must be vgpr", instr
.get());
437 check(instr
->operands
[2].regClass().type() == RegType::vgpr
, "FLAT/GLOBAL/SCRATCH data must be vgpr", instr
.get());
447 for (unsigned i
= 0; i
< program
->blocks
.size(); i
++) {
448 Block
& block
= program
->blocks
[i
];
449 check_block(block
.index
== i
, "block.index must match actual index", &block
);
451 /* predecessors/successors should be sorted */
452 for (unsigned j
= 0; j
+ 1 < block
.linear_preds
.size(); j
++)
453 check_block(block
.linear_preds
[j
] < block
.linear_preds
[j
+ 1], "linear predecessors must be sorted", &block
);
454 for (unsigned j
= 0; j
+ 1 < block
.logical_preds
.size(); j
++)
455 check_block(block
.logical_preds
[j
] < block
.logical_preds
[j
+ 1], "logical predecessors must be sorted", &block
);
456 for (unsigned j
= 0; j
+ 1 < block
.linear_succs
.size(); j
++)
457 check_block(block
.linear_succs
[j
] < block
.linear_succs
[j
+ 1], "linear successors must be sorted", &block
);
458 for (unsigned j
= 0; j
+ 1 < block
.logical_succs
.size(); j
++)
459 check_block(block
.logical_succs
[j
] < block
.logical_succs
[j
+ 1], "logical successors must be sorted", &block
);
461 /* critical edges are not allowed */
462 if (block
.linear_preds
.size() > 1) {
463 for (unsigned pred
: block
.linear_preds
)
464 check_block(program
->blocks
[pred
].linear_succs
.size() == 1, "linear critical edges are not allowed", &program
->blocks
[pred
]);
465 for (unsigned pred
: block
.logical_preds
)
466 check_block(program
->blocks
[pred
].logical_succs
.size() == 1, "logical critical edges are not allowed", &program
->blocks
[pred
]);
477 Location() : block(NULL
), instr(NULL
) {}
480 Instruction
*instr
; //NULL if it's the block's live-in
489 bool ra_fail(Program
*program
, Location loc
, Location loc2
, const char *fmt
, ...) {
493 vsprintf(msg
, fmt
, args
);
498 FILE *memf
= open_memstream(&out
, &outsize
);
500 fprintf(memf
, "RA error found at instruction in BB%d:\n", loc
.block
->index
);
502 aco_print_instr(loc
.instr
, memf
);
503 fprintf(memf
, "\n%s", msg
);
505 fprintf(memf
, "%s", msg
);
508 fprintf(memf
, " in BB%d:\n", loc2
.block
->index
);
509 aco_print_instr(loc2
.instr
, memf
);
511 fprintf(memf
, "\n\n");
513 aco_err(program
, out
);
519 bool validate_subdword_operand(chip_class chip
, const aco_ptr
<Instruction
>& instr
, unsigned index
)
521 Operand op
= instr
->operands
[index
];
522 unsigned byte
= op
.physReg().byte();
524 if (instr
->opcode
== aco_opcode::p_as_uniform
)
526 if (instr
->format
== Format::PSEUDO
&& chip
>= GFX8
)
528 if (instr
->isSDWA() && (static_cast<SDWA_instruction
*>(instr
.get())->sel
[index
] & sdwa_asuint
) == (sdwa_isra
| op
.bytes()))
530 if (byte
== 2 && can_use_opsel(chip
, instr
->opcode
, index
, 1))
533 switch (instr
->opcode
) {
534 case aco_opcode::v_cvt_f32_ubyte1
:
538 case aco_opcode::v_cvt_f32_ubyte2
:
542 case aco_opcode::v_cvt_f32_ubyte3
:
546 case aco_opcode::ds_write_b8_d16_hi
:
547 case aco_opcode::ds_write_b16_d16_hi
:
548 if (byte
== 2 && index
== 1)
551 case aco_opcode::buffer_store_byte_d16_hi
:
552 case aco_opcode::buffer_store_short_d16_hi
:
553 if (byte
== 2 && index
== 3)
556 case aco_opcode::flat_store_byte_d16_hi
:
557 case aco_opcode::flat_store_short_d16_hi
:
558 case aco_opcode::scratch_store_byte_d16_hi
:
559 case aco_opcode::scratch_store_short_d16_hi
:
560 case aco_opcode::global_store_byte_d16_hi
:
561 case aco_opcode::global_store_short_d16_hi
:
562 if (byte
== 2 && index
== 2)
571 bool validate_subdword_definition(chip_class chip
, const aco_ptr
<Instruction
>& instr
)
573 Definition def
= instr
->definitions
[0];
574 unsigned byte
= def
.physReg().byte();
576 if (instr
->format
== Format::PSEUDO
&& chip
>= GFX8
)
578 if (instr
->isSDWA() && static_cast<SDWA_instruction
*>(instr
.get())->dst_sel
== (sdwa_isra
| def
.bytes()))
580 if (byte
== 2 && can_use_opsel(chip
, instr
->opcode
, -1, 1))
583 switch (instr
->opcode
) {
584 case aco_opcode::buffer_load_ubyte_d16_hi
:
585 case aco_opcode::buffer_load_short_d16_hi
:
586 case aco_opcode::flat_load_ubyte_d16_hi
:
587 case aco_opcode::flat_load_short_d16_hi
:
588 case aco_opcode::scratch_load_ubyte_d16_hi
:
589 case aco_opcode::scratch_load_short_d16_hi
:
590 case aco_opcode::global_load_ubyte_d16_hi
:
591 case aco_opcode::global_load_short_d16_hi
:
592 case aco_opcode::ds_read_u8_d16_hi
:
593 case aco_opcode::ds_read_u16_d16_hi
:
602 unsigned get_subdword_bytes_written(Program
*program
, const aco_ptr
<Instruction
>& instr
, unsigned index
)
604 chip_class chip
= program
->chip_class
;
605 Definition def
= instr
->definitions
[index
];
607 if (instr
->format
== Format::PSEUDO
)
608 return chip
>= GFX8
? def
.bytes() : def
.size() * 4u;
609 if (instr
->isSDWA() && static_cast<SDWA_instruction
*>(instr
.get())->dst_sel
== (sdwa_isra
| def
.bytes()))
612 switch (instr
->opcode
) {
613 case aco_opcode::buffer_load_ubyte_d16
:
614 case aco_opcode::buffer_load_short_d16
:
615 case aco_opcode::flat_load_ubyte_d16
:
616 case aco_opcode::flat_load_short_d16
:
617 case aco_opcode::scratch_load_ubyte_d16
:
618 case aco_opcode::scratch_load_short_d16
:
619 case aco_opcode::global_load_ubyte_d16
:
620 case aco_opcode::global_load_short_d16
:
621 case aco_opcode::ds_read_u8_d16
:
622 case aco_opcode::ds_read_u16_d16
:
623 case aco_opcode::buffer_load_ubyte_d16_hi
:
624 case aco_opcode::buffer_load_short_d16_hi
:
625 case aco_opcode::flat_load_ubyte_d16_hi
:
626 case aco_opcode::flat_load_short_d16_hi
:
627 case aco_opcode::scratch_load_ubyte_d16_hi
:
628 case aco_opcode::scratch_load_short_d16_hi
:
629 case aco_opcode::global_load_ubyte_d16_hi
:
630 case aco_opcode::global_load_short_d16_hi
:
631 case aco_opcode::ds_read_u8_d16_hi
:
632 case aco_opcode::ds_read_u16_d16_hi
:
633 return program
->sram_ecc_enabled
? 4 : 2;
634 case aco_opcode::v_mad_f16
:
635 case aco_opcode::v_mad_u16
:
636 case aco_opcode::v_mad_i16
:
637 case aco_opcode::v_fma_f16
:
638 case aco_opcode::v_div_fixup_f16
:
639 case aco_opcode::v_interp_p2_f16
:
646 return MAX2(chip
>= GFX10
? def
.bytes() : 4, instr_info
.definition_size
[(int)instr
->opcode
] / 8u);
649 } /* end namespace */
651 bool validate_ra(Program
*program
, const struct radv_nir_compiler_options
*options
) {
652 if (!(debug_flags
& DEBUG_VALIDATE_RA
))
656 aco::live live_vars
= aco::live_var_analysis(program
, options
);
657 std::vector
<std::vector
<Temp
>> phi_sgpr_ops(program
->blocks
.size());
659 std::map
<unsigned, Assignment
> assignments
;
660 for (Block
& block
: program
->blocks
) {
663 for (aco_ptr
<Instruction
>& instr
: block
.instructions
) {
664 if (instr
->opcode
== aco_opcode::p_phi
) {
665 for (unsigned i
= 0; i
< instr
->operands
.size(); i
++) {
666 if (instr
->operands
[i
].isTemp() &&
667 instr
->operands
[i
].getTemp().type() == RegType::sgpr
&&
668 instr
->operands
[i
].isFirstKill())
669 phi_sgpr_ops
[block
.logical_preds
[i
]].emplace_back(instr
->operands
[i
].getTemp());
673 loc
.instr
= instr
.get();
674 for (unsigned i
= 0; i
< instr
->operands
.size(); i
++) {
675 Operand
& op
= instr
->operands
[i
];
679 err
|= ra_fail(program
, loc
, Location(), "Operand %d is not assigned a register", i
);
680 if (assignments
.count(op
.tempId()) && assignments
[op
.tempId()].reg
!= op
.physReg())
681 err
|= ra_fail(program
, loc
, assignments
.at(op
.tempId()).firstloc
, "Operand %d has an inconsistent register assignment with instruction", i
);
682 if ((op
.getTemp().type() == RegType::vgpr
&& op
.physReg().reg_b
+ op
.bytes() > (256 + program
->config
->num_vgprs
) * 4) ||
683 (op
.getTemp().type() == RegType::sgpr
&& op
.physReg() + op
.size() > program
->config
->num_sgprs
&& op
.physReg() < program
->sgpr_limit
))
684 err
|= ra_fail(program
, loc
, assignments
.at(op
.tempId()).firstloc
, "Operand %d has an out-of-bounds register assignment", i
);
685 if (op
.physReg() == vcc
&& !program
->needs_vcc
)
686 err
|= ra_fail(program
, loc
, Location(), "Operand %d fixed to vcc but needs_vcc=false", i
);
687 if (op
.regClass().is_subdword() && !validate_subdword_operand(program
->chip_class
, instr
, i
))
688 err
|= ra_fail(program
, loc
, Location(), "Operand %d not aligned correctly", i
);
689 if (!assignments
[op
.tempId()].firstloc
.block
)
690 assignments
[op
.tempId()].firstloc
= loc
;
691 if (!assignments
[op
.tempId()].defloc
.block
)
692 assignments
[op
.tempId()].reg
= op
.physReg();
695 for (unsigned i
= 0; i
< instr
->definitions
.size(); i
++) {
696 Definition
& def
= instr
->definitions
[i
];
700 err
|= ra_fail(program
, loc
, Location(), "Definition %d is not assigned a register", i
);
701 if (assignments
[def
.tempId()].defloc
.block
)
702 err
|= ra_fail(program
, loc
, assignments
.at(def
.tempId()).defloc
, "Temporary %%%d also defined by instruction", def
.tempId());
703 if ((def
.getTemp().type() == RegType::vgpr
&& def
.physReg().reg_b
+ def
.bytes() > (256 + program
->config
->num_vgprs
) * 4) ||
704 (def
.getTemp().type() == RegType::sgpr
&& def
.physReg() + def
.size() > program
->config
->num_sgprs
&& def
.physReg() < program
->sgpr_limit
))
705 err
|= ra_fail(program
, loc
, assignments
.at(def
.tempId()).firstloc
, "Definition %d has an out-of-bounds register assignment", i
);
706 if (def
.physReg() == vcc
&& !program
->needs_vcc
)
707 err
|= ra_fail(program
, loc
, Location(), "Definition %d fixed to vcc but needs_vcc=false", i
);
708 if (def
.regClass().is_subdword() && !validate_subdword_definition(program
->chip_class
, instr
))
709 err
|= ra_fail(program
, loc
, Location(), "Definition %d not aligned correctly", i
);
710 if (!assignments
[def
.tempId()].firstloc
.block
)
711 assignments
[def
.tempId()].firstloc
= loc
;
712 assignments
[def
.tempId()].defloc
= loc
;
713 assignments
[def
.tempId()].reg
= def
.physReg();
718 for (Block
& block
: program
->blocks
) {
722 std::array
<unsigned, 2048> regs
; /* register file in bytes */
726 live
.insert(live_vars
.live_out
[block
.index
].begin(), live_vars
.live_out
[block
.index
].end());
727 /* remove killed p_phi sgpr operands */
728 for (Temp tmp
: phi_sgpr_ops
[block
.index
])
732 for (Temp tmp
: live
) {
733 PhysReg reg
= assignments
.at(tmp
.id()).reg
;
734 for (unsigned i
= 0; i
< tmp
.bytes(); i
++) {
735 if (regs
[reg
.reg_b
+ i
]) {
736 err
|= ra_fail(program
, loc
, Location(), "Assignment of element %d of %%%d already taken by %%%d in live-out", i
, tmp
.id(), regs
[reg
.reg_b
+ i
]);
738 regs
[reg
.reg_b
+ i
] = tmp
.id();
743 for (auto it
= block
.instructions
.rbegin(); it
!= block
.instructions
.rend(); ++it
) {
744 aco_ptr
<Instruction
>& instr
= *it
;
746 /* check killed p_phi sgpr operands */
747 if (instr
->opcode
== aco_opcode::p_logical_end
) {
748 for (Temp tmp
: phi_sgpr_ops
[block
.index
]) {
749 PhysReg reg
= assignments
.at(tmp
.id()).reg
;
750 for (unsigned i
= 0; i
< tmp
.bytes(); i
++) {
751 if (regs
[reg
.reg_b
+ i
])
752 err
|= ra_fail(program
, loc
, Location(), "Assignment of element %d of %%%d already taken by %%%d in live-out", i
, tmp
.id(), regs
[reg
.reg_b
+ i
]);
758 for (const Definition
& def
: instr
->definitions
) {
761 live
.erase(def
.getTemp());
764 /* don't count phi operands as live-in, since they are actually
765 * killed when they are copied at the predecessor */
766 if (instr
->opcode
!= aco_opcode::p_phi
&& instr
->opcode
!= aco_opcode::p_linear_phi
) {
767 for (const Operand
& op
: instr
->operands
) {
770 live
.insert(op
.getTemp());
775 for (Temp tmp
: live
) {
776 PhysReg reg
= assignments
.at(tmp
.id()).reg
;
777 for (unsigned i
= 0; i
< tmp
.bytes(); i
++)
778 regs
[reg
.reg_b
+ i
] = tmp
.id();
781 for (aco_ptr
<Instruction
>& instr
: block
.instructions
) {
782 loc
.instr
= instr
.get();
784 /* remove killed p_phi operands from regs */
785 if (instr
->opcode
== aco_opcode::p_logical_end
) {
786 for (Temp tmp
: phi_sgpr_ops
[block
.index
]) {
787 PhysReg reg
= assignments
.at(tmp
.id()).reg
;
788 for (unsigned i
= 0; i
< tmp
.bytes(); i
++)
789 regs
[reg
.reg_b
+ i
] = 0;
793 if (instr
->opcode
!= aco_opcode::p_phi
&& instr
->opcode
!= aco_opcode::p_linear_phi
) {
794 for (const Operand
& op
: instr
->operands
) {
797 if (op
.isFirstKillBeforeDef()) {
798 for (unsigned j
= 0; j
< op
.getTemp().bytes(); j
++)
799 regs
[op
.physReg().reg_b
+ j
] = 0;
804 for (unsigned i
= 0; i
< instr
->definitions
.size(); i
++) {
805 Definition
& def
= instr
->definitions
[i
];
808 Temp tmp
= def
.getTemp();
809 PhysReg reg
= assignments
.at(tmp
.id()).reg
;
810 for (unsigned j
= 0; j
< tmp
.bytes(); j
++) {
811 if (regs
[reg
.reg_b
+ j
])
812 err
|= ra_fail(program
, loc
, assignments
.at(regs
[reg
.reg_b
+ j
]).defloc
, "Assignment of element %d of %%%d already taken by %%%d from instruction", i
, tmp
.id(), regs
[reg
.reg_b
+ j
]);
813 regs
[reg
.reg_b
+ j
] = tmp
.id();
815 if (def
.regClass().is_subdword() && def
.bytes() < 4) {
816 unsigned written
= get_subdword_bytes_written(program
, instr
, i
);
817 /* If written=4, the instruction still might write the upper half. In that case, it's the lower half that isn't preserved */
818 for (unsigned j
= reg
.byte() & ~(written
- 1); j
< written
; j
++) {
819 unsigned written_reg
= reg
.reg() * 4u + j
;
820 if (regs
[written_reg
] && regs
[written_reg
] != def
.tempId())
821 err
|= ra_fail(program
, loc
, assignments
.at(regs
[written_reg
]).defloc
, "Assignment of element %d of %%%d overwrites the full register taken by %%%d from instruction", i
, tmp
.id(), regs
[written_reg
]);
826 for (const Definition
& def
: instr
->definitions
) {
830 for (unsigned j
= 0; j
< def
.getTemp().bytes(); j
++)
831 regs
[def
.physReg().reg_b
+ j
] = 0;
835 if (instr
->opcode
!= aco_opcode::p_phi
&& instr
->opcode
!= aco_opcode::p_linear_phi
) {
836 for (const Operand
& op
: instr
->operands
) {
839 if (op
.isLateKill() && op
.isFirstKill()) {
840 for (unsigned j
= 0; j
< op
.getTemp().bytes(); j
++)
841 regs
[op
.physReg().reg_b
+ j
] = 0;