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
27 #include "util/memstream.h"
28 #include "util/ralloc.h"
38 aco_log(Program* program, enum aco_compiler_debug_level level, const char* prefix,
39 const char* file, unsigned line, const char* fmt, va_list args)
43 if (program->debug.shorten_messages) {
44 msg = ralloc_vasprintf(NULL, fmt, args);
46 msg = ralloc_strdup(NULL, prefix);
47 ralloc_asprintf_append(&msg, " In file %s:%u\n", file, line);
48 ralloc_asprintf_append(&msg, " ");
49 ralloc_vasprintf_append(&msg, fmt, args);
52 if (program->debug.func)
53 program->debug.func(program->debug.private_data, level, msg);
55 fprintf(program->debug.output, "%s\n", msg);
61 _aco_perfwarn(Program* program, const char* file, unsigned line, const char* fmt, ...)
66 aco_log(program, ACO_COMPILER_DEBUG_LEVEL_PERFWARN, "ACO PERFWARN:\n", file, line, fmt, args);
71 _aco_err(Program* program, const char* file, unsigned line, const char* fmt, ...)
76 aco_log(program, ACO_COMPILER_DEBUG_LEVEL_ERROR, "ACO ERROR:\n", file, line, fmt, args);
81 validate_ir(Program* program)
84 auto check = [&program, &is_valid](bool success, const char* msg,
85 aco::Instruction* instr) -> void
90 struct u_memstream mem;
91 u_memstream_open(&mem, &out, &outsize);
92 FILE* const memf = u_memstream_get(&mem);
94 fprintf(memf, "%s: ", msg);
95 aco_print_instr(program->gfx_level, instr, memf);
96 u_memstream_close(&mem);
98 aco_err(program, "%s", out);
105 auto check_block = [&program, &is_valid](bool success, const char* msg,
106 aco::Block* block) -> void
109 aco_err(program, "%s: BB%u", msg, block->index);
114 for (Block& block : program->blocks) {
115 for (aco_ptr<Instruction>& instr : block.instructions) {
117 /* check base format */
118 Format base_format = instr->format;
119 base_format = (Format)((uint32_t)base_format & ~(uint32_t)Format::SDWA);
120 base_format = (Format)((uint32_t)base_format & ~(uint32_t)Format::DPP16);
121 base_format = (Format)((uint32_t)base_format & ~(uint32_t)Format::DPP8);
122 if ((uint32_t)base_format & (uint32_t)Format::VOP1)
123 base_format = Format::VOP1;
124 else if ((uint32_t)base_format & (uint32_t)Format::VOP2)
125 base_format = Format::VOP2;
126 else if ((uint32_t)base_format & (uint32_t)Format::VOPC)
127 base_format = Format::VOPC;
128 else if ((uint32_t)base_format & (uint32_t)Format::VINTRP) {
129 if (instr->opcode == aco_opcode::v_interp_p1ll_f16 ||
130 instr->opcode == aco_opcode::v_interp_p1lv_f16 ||
131 instr->opcode == aco_opcode::v_interp_p2_legacy_f16 ||
132 instr->opcode == aco_opcode::v_interp_p2_f16) {
133 /* v_interp_*_fp16 are considered VINTRP by the compiler but
134 * they are emitted as VOP3.
136 base_format = Format::VOP3;
138 base_format = Format::VINTRP;
141 check(base_format == instr_info.format[(int)instr->opcode],
142 "Wrong base format for instruction", instr.get());
144 /* check VOP3 modifiers */
145 if (instr->isVOP3() && withoutDPP(instr->format) != Format::VOP3) {
146 check(base_format == Format::VOP2 || base_format == Format::VOP1 ||
147 base_format == Format::VOPC || base_format == Format::VINTRP,
148 "Format cannot have VOP3/VOP3B applied", instr.get());
151 if (instr->isDPP()) {
152 check(base_format == Format::VOP2 || base_format == Format::VOP1 ||
153 base_format == Format::VOPC || base_format == Format::VOP3 ||
154 base_format == Format::VOP3P,
155 "Format cannot have DPP applied", instr.get());
156 check((!instr->isVOP3() && !instr->isVOP3P()) || program->gfx_level >= GFX11,
157 "VOP3+DPP is GFX11+ only", instr.get());
161 if (instr->isSDWA()) {
162 check(base_format == Format::VOP2 || base_format == Format::VOP1 ||
163 base_format == Format::VOPC,
164 "Format cannot have SDWA applied", instr.get());
166 check(program->gfx_level >= GFX8, "SDWA is GFX8 to GFX10.3 only", instr.get());
167 check(program->gfx_level < GFX11, "SDWA is GFX8 to GFX10.3 only", instr.get());
169 SDWA_instruction& sdwa = instr->sdwa();
170 check(sdwa.omod == 0 || program->gfx_level >= GFX9, "SDWA omod only supported on GFX9+",
172 if (base_format == Format::VOPC) {
173 check(sdwa.clamp == false || program->gfx_level == GFX8,
174 "SDWA VOPC clamp only supported on GFX8", instr.get());
175 check((instr->definitions[0].isFixed() && instr->definitions[0].physReg() == vcc) ||
176 program->gfx_level >= GFX9,
177 "SDWA+VOPC definition must be fixed to vcc on GFX8", instr.get());
179 const Definition& def = instr->definitions[0];
180 check(def.bytes() <= 4, "SDWA definitions must not be larger than 4 bytes",
182 check(def.bytes() >= sdwa.dst_sel.size() + sdwa.dst_sel.offset(),
183 "SDWA definition selection size must be at most definition size", instr.get());
185 sdwa.dst_sel.size() == 1 || sdwa.dst_sel.size() == 2 || sdwa.dst_sel.size() == 4,
186 "SDWA definition selection size must be 1, 2 or 4 bytes", instr.get());
187 check(sdwa.dst_sel.offset() % sdwa.dst_sel.size() == 0, "Invalid selection offset",
189 check(def.bytes() == 4 || def.bytes() == sdwa.dst_sel.size(),
190 "SDWA dst_sel size must be definition size for subdword definitions",
192 check(def.bytes() == 4 || sdwa.dst_sel.offset() == 0,
193 "SDWA dst_sel offset must be 0 for subdword definitions", instr.get());
196 for (unsigned i = 0; i < std::min<unsigned>(2, instr->operands.size()); i++) {
197 const Operand& op = instr->operands[i];
198 check(op.bytes() <= 4, "SDWA operands must not be larger than 4 bytes", instr.get());
199 check(op.bytes() >= sdwa.sel[i].size() + sdwa.sel[i].offset(),
200 "SDWA operand selection size must be at most operand size", instr.get());
201 check(sdwa.sel[i].size() == 1 || sdwa.sel[i].size() == 2 || sdwa.sel[i].size() == 4,
202 "SDWA operand selection size must be 1, 2 or 4 bytes", instr.get());
203 check(sdwa.sel[i].offset() % sdwa.sel[i].size() == 0, "Invalid selection offset",
206 if (instr->operands.size() >= 3) {
207 check(instr->operands[2].isFixed() && instr->operands[2].physReg() == vcc,
208 "3rd operand must be fixed to vcc with SDWA", instr.get());
210 if (instr->definitions.size() >= 2) {
211 check(instr->definitions[1].isFixed() && instr->definitions[1].physReg() == vcc,
212 "2nd definition must be fixed to vcc with SDWA", instr.get());
215 const bool sdwa_opcodes =
216 instr->opcode != aco_opcode::v_fmac_f32 && instr->opcode != aco_opcode::v_fmac_f16 &&
217 instr->opcode != aco_opcode::v_fmamk_f32 &&
218 instr->opcode != aco_opcode::v_fmaak_f32 &&
219 instr->opcode != aco_opcode::v_fmamk_f16 &&
220 instr->opcode != aco_opcode::v_fmaak_f16 &&
221 instr->opcode != aco_opcode::v_madmk_f32 &&
222 instr->opcode != aco_opcode::v_madak_f32 &&
223 instr->opcode != aco_opcode::v_madmk_f16 &&
224 instr->opcode != aco_opcode::v_madak_f16 &&
225 instr->opcode != aco_opcode::v_readfirstlane_b32 &&
226 instr->opcode != aco_opcode::v_clrexcp && instr->opcode != aco_opcode::v_swap_b32;
228 const bool feature_mac =
229 program->gfx_level == GFX8 &&
230 (instr->opcode == aco_opcode::v_mac_f32 && instr->opcode == aco_opcode::v_mac_f16);
232 check(sdwa_opcodes || feature_mac, "SDWA can't be used with this opcode", instr.get());
236 if (instr->isVOP3() || instr->isVOP1() || instr->isVOP2() || instr->isVOPC()) {
237 VALU_instruction& valu = instr->valu();
238 check(valu.opsel == 0 || program->gfx_level >= GFX9, "Opsel is only supported on GFX9+",
240 check(valu.opsel == 0 || instr->format == Format::VOP3 || program->gfx_level >= GFX11,
241 "Opsel is only supported for VOP3 before GFX11", instr.get());
243 for (unsigned i = 0; i < 3; i++) {
244 if (i >= instr->operands.size() ||
245 (!instr->isVOP3() && !instr->operands[i].isOfType(RegType::vgpr)) ||
246 (instr->operands[i].hasRegClass() &&
247 instr->operands[i].regClass().is_subdword() && !instr->operands[i].isFixed()))
248 check(!valu.opsel[i], "Unexpected opsel for operand", instr.get());
250 if (instr->definitions[0].regClass().is_subdword() && !instr->definitions[0].isFixed())
251 check(!valu.opsel[3], "Unexpected opsel for sub-dword definition", instr.get());
252 } else if (instr->opcode == aco_opcode::v_fma_mixlo_f16 ||
253 instr->opcode == aco_opcode::v_fma_mixhi_f16 ||
254 instr->opcode == aco_opcode::v_fma_mix_f32) {
255 check(instr->definitions[0].regClass() ==
256 (instr->opcode == aco_opcode::v_fma_mix_f32 ? v1 : v2b),
257 "v_fma_mix_f32/v_fma_mix_f16 must have v1/v2b definition", instr.get());
258 } else if (instr->isVOP3P()) {
259 VALU_instruction& vop3p = instr->valu();
260 for (unsigned i = 0; i < instr->operands.size(); i++) {
261 if (instr->operands[i].hasRegClass() &&
262 instr->operands[i].regClass().is_subdword() && !instr->operands[i].isFixed())
263 check(!vop3p.opsel_lo[i] && !vop3p.opsel_hi[i],
264 "Unexpected opsel for subdword operand", instr.get());
266 check(instr->definitions[0].regClass() == v1, "VOP3P must have v1 definition",
270 /* check for undefs */
271 for (unsigned i = 0; i < instr->operands.size(); i++) {
272 if (instr->operands[i].isUndefined()) {
273 bool flat = instr->isFlatLike();
274 bool can_be_undef = is_phi(instr) || instr->isEXP() || instr->isReduction() ||
275 instr->opcode == aco_opcode::p_create_vector ||
276 instr->opcode == aco_opcode::p_jump_to_epilog ||
277 instr->opcode == aco_opcode::p_dual_src_export_gfx11 ||
278 (instr->opcode == aco_opcode::p_interp_gfx11 && i == 0) ||
279 (instr->opcode == aco_opcode::p_bpermute_gfx11w64 && i == 0) ||
280 (flat && i == 1) || (instr->isMIMG() && (i == 1 || i == 2)) ||
281 ((instr->isMUBUF() || instr->isMTBUF()) && i == 1) ||
282 (instr->isScratch() && i == 0) ||
283 (instr->isDS() && i == 0) ||
284 (instr->opcode == aco_opcode::p_init_scratch && i == 0);
285 check(can_be_undef, "Undefs can only be used in certain operands", instr.get());
287 check(instr->operands[i].isFixed() || instr->operands[i].isTemp() ||
288 instr->operands[i].isConstant(),
289 "Uninitialized Operand", instr.get());
293 /* check subdword definitions */
294 for (unsigned i = 0; i < instr->definitions.size(); i++) {
295 if (instr->definitions[i].regClass().is_subdword())
296 check(instr->definitions[i].bytes() <= 4 || instr->isPseudo() || instr->isVMEM(),
297 "Only Pseudo and VMEM instructions can write subdword registers > 4 bytes",
301 if (instr->isSALU() || instr->isVALU()) {
304 for (unsigned i = 0; i < instr->operands.size(); i++) {
305 Operand op = instr->operands[i];
309 check(!instr->isDPP() && !instr->isSDWA() &&
310 (!instr->isVOP3() || program->gfx_level >= GFX10) &&
311 (!instr->isVOP3P() || program->gfx_level >= GFX10),
312 "Literal applied on wrong instruction format", instr.get());
314 check(literal.isUndefined() || (literal.size() == op.size() &&
315 literal.constantValue() == op.constantValue()),
316 "Only 1 Literal allowed", instr.get());
318 check(instr->isSALU() || instr->isVOP3() || instr->isVOP3P() || i == 0 || i == 2,
319 "Wrong source position for Literal argument", instr.get());
322 /* check num sgprs for VALU */
323 if (instr->isVALU()) {
324 bool is_shift64 = instr->opcode == aco_opcode::v_lshlrev_b64 ||
325 instr->opcode == aco_opcode::v_lshrrev_b64 ||
326 instr->opcode == aco_opcode::v_ashrrev_i64;
327 unsigned const_bus_limit = 1;
328 if (program->gfx_level >= GFX10 && !is_shift64)
331 uint32_t scalar_mask =
332 instr->isVOP3() || instr->isVOP3P() || instr->isVINTERP_INREG() ? 0x7 : 0x5;
334 scalar_mask = program->gfx_level >= GFX9 ? 0x7 : 0x4;
335 else if (instr->isDPP())
338 if (instr->isVOPC() || instr->opcode == aco_opcode::v_readfirstlane_b32 ||
339 instr->opcode == aco_opcode::v_readlane_b32 ||
340 instr->opcode == aco_opcode::v_readlane_b32_e64) {
341 check(instr->definitions[0].regClass().type() == RegType::sgpr,
342 "Wrong Definition type for VALU instruction", instr.get());
344 check(instr->definitions[0].regClass().type() == RegType::vgpr,
345 "Wrong Definition type for VALU instruction", instr.get());
348 unsigned num_sgprs = 0;
349 unsigned sgpr[] = {0, 0};
350 for (unsigned i = 0; i < instr->operands.size(); i++) {
351 Operand op = instr->operands[i];
352 if (instr->opcode == aco_opcode::v_readfirstlane_b32 ||
353 instr->opcode == aco_opcode::v_readlane_b32 ||
354 instr->opcode == aco_opcode::v_readlane_b32_e64) {
355 check(i != 1 || op.isOfType(RegType::sgpr) || op.isConstant(),
356 "Must be a SGPR or a constant", instr.get());
357 check(i == 1 || (op.isOfType(RegType::vgpr) && op.bytes() <= 4),
358 "Wrong Operand type for VALU instruction", instr.get());
361 if (instr->opcode == aco_opcode::v_permlane16_b32 ||
362 instr->opcode == aco_opcode::v_permlanex16_b32) {
363 check(i != 0 || op.isOfType(RegType::vgpr),
364 "Operand 0 of v_permlane must be VGPR", instr.get());
365 check(i == 0 || op.isOfType(RegType::sgpr) || op.isConstant(),
366 "Lane select operands of v_permlane must be SGPR or constant",
370 if (instr->opcode == aco_opcode::v_writelane_b32 ||
371 instr->opcode == aco_opcode::v_writelane_b32_e64) {
372 check(i != 2 || (op.isOfType(RegType::vgpr) && op.bytes() <= 4),
373 "Wrong Operand type for VALU instruction", instr.get());
374 check(i == 2 || op.isOfType(RegType::sgpr) || op.isConstant(),
375 "Must be a SGPR or a constant", instr.get());
378 if (op.isOfType(RegType::sgpr)) {
379 check(scalar_mask & (1 << i), "Wrong source position for SGPR argument",
382 if (op.tempId() != sgpr[0] && op.tempId() != sgpr[1]) {
384 sgpr[num_sgprs++] = op.tempId();
388 if (op.isConstant() && !op.isLiteral())
389 check(scalar_mask & (1 << i), "Wrong source position for constant argument",
392 check(num_sgprs + (literal.isUndefined() ? 0 : 1) <= const_bus_limit,
393 "Too many SGPRs/literals", instr.get());
395 /* Validate modifiers. */
396 check(!instr->valu().opsel || instr->isVOP3() || instr->isVOP1() ||
397 instr->isVOP2() || instr->isVOPC() || instr->isVINTERP_INREG(),
398 "OPSEL set for unsupported instruction format", instr.get());
399 check(!instr->valu().opsel_lo || instr->isVOP3P(),
400 "OPSEL_LO set for unsupported instruction format", instr.get());
401 check(!instr->valu().opsel_hi || instr->isVOP3P(),
402 "OPSEL_HI set for unsupported instruction format", instr.get());
403 check(!instr->valu().omod || instr->isVOP3() ||instr->isSDWA(),
404 "OMOD set for unsupported instruction format", instr.get());
405 check(!instr->valu().clamp || instr->isVOP3() || instr->isVOP3P() ||
406 instr->isSDWA() || instr->isVINTERP_INREG(),
407 "CLAMP set for unsupported instruction format", instr.get());
409 for (bool abs : instr->valu().abs) {
410 check(!abs || instr->isVOP3() || instr->isVOP3P() || instr->isSDWA() ||
412 "ABS/NEG_HI set for unsupported instruction format", instr.get());
414 for (bool neg : instr->valu().neg) {
415 check(!neg || instr->isVOP3() || instr->isVOP3P() || instr->isSDWA() ||
416 instr->isDPP16() || instr->isVINTERP_INREG(),
417 "NEG/NEG_LO set for unsupported instruction format", instr.get());
421 if (instr->isSOP1() || instr->isSOP2()) {
422 if (!instr->definitions.empty())
423 check(instr->definitions[0].regClass().type() == RegType::sgpr,
424 "Wrong Definition type for SALU instruction", instr.get());
425 for (const Operand& op : instr->operands) {
426 check(op.isConstant() || op.isOfType(RegType::sgpr),
427 "Wrong Operand type for SALU instruction", instr.get());
432 switch (instr->format) {
433 case Format::PSEUDO: {
434 if (instr->opcode == aco_opcode::p_create_vector) {
436 for (const Operand& op : instr->operands) {
437 check(op.bytes() < 4 || size % 4 == 0, "Operand is not aligned", instr.get());
440 check(size == instr->definitions[0].bytes(),
441 "Definition size does not match operand sizes", instr.get());
442 if (instr->definitions[0].regClass().type() == RegType::sgpr) {
443 for (const Operand& op : instr->operands) {
444 check(op.isConstant() || op.regClass().type() == RegType::sgpr,
445 "Wrong Operand type for scalar vector", instr.get());
448 } else if (instr->opcode == aco_opcode::p_extract_vector) {
449 check(!instr->operands[0].isConstant() && instr->operands[1].isConstant(),
450 "Wrong Operand types", instr.get());
451 check((instr->operands[1].constantValue() + 1) * instr->definitions[0].bytes() <=
452 instr->operands[0].bytes(),
453 "Index out of range", instr.get());
454 check(instr->definitions[0].regClass().type() == RegType::vgpr ||
455 instr->operands[0].regClass().type() == RegType::sgpr,
456 "Cannot extract SGPR value from VGPR vector", instr.get());
457 check(program->gfx_level >= GFX9 ||
458 !instr->definitions[0].regClass().is_subdword() ||
459 instr->operands[0].regClass().type() == RegType::vgpr,
460 "Cannot extract subdword from SGPR before GFX9+", instr.get());
461 } else if (instr->opcode == aco_opcode::p_split_vector) {
462 check(!instr->operands[0].isConstant(), "Operand must not be constant", instr.get());
464 for (const Definition& def : instr->definitions) {
467 check(size == instr->operands[0].bytes(),
468 "Operand size does not match definition sizes", instr.get());
469 if (instr->operands[0].isOfType(RegType::vgpr)) {
470 for (const Definition& def : instr->definitions)
471 check(def.regClass().type() == RegType::vgpr,
472 "Wrong Definition type for VGPR split_vector", instr.get());
474 for (const Definition& def : instr->definitions)
475 check(program->gfx_level >= GFX9 || !def.regClass().is_subdword(),
476 "Cannot split SGPR into subdword VGPRs before GFX9+", instr.get());
478 } else if (instr->opcode == aco_opcode::p_parallelcopy) {
479 check(instr->definitions.size() == instr->operands.size(),
480 "Number of Operands does not match number of Definitions", instr.get());
481 for (unsigned i = 0; i < instr->operands.size(); i++) {
482 check(instr->definitions[i].bytes() == instr->operands[i].bytes(),
483 "Operand and Definition size must match", instr.get());
484 if (instr->operands[i].hasRegClass()) {
485 check((instr->definitions[i].regClass().type() ==
486 instr->operands[i].regClass().type()) ||
487 (instr->definitions[i].regClass().type() == RegType::vgpr &&
488 instr->operands[i].regClass().type() == RegType::sgpr),
489 "Operand and Definition types do not match", instr.get());
490 check(instr->definitions[i].regClass().is_linear_vgpr() ==
491 instr->operands[i].regClass().is_linear_vgpr(),
492 "Operand and Definition types do not match", instr.get());
494 check(!instr->definitions[i].regClass().is_linear_vgpr(),
495 "Can only copy linear VGPRs into linear VGPRs, not constant/undef",
499 } else if (instr->opcode == aco_opcode::p_phi) {
500 check(instr->operands.size() == block.logical_preds.size(),
501 "Number of Operands does not match number of predecessors", instr.get());
502 check(instr->definitions[0].regClass().type() == RegType::vgpr,
503 "Logical Phi Definition must be vgpr", instr.get());
504 for (const Operand& op : instr->operands)
505 check(instr->definitions[0].size() == op.size(),
506 "Operand sizes must match Definition size", instr.get());
507 } else if (instr->opcode == aco_opcode::p_linear_phi) {
508 for (const Operand& op : instr->operands) {
509 check(!op.isTemp() || op.getTemp().is_linear(), "Wrong Operand type",
511 check(instr->definitions[0].size() == op.size(),
512 "Operand sizes must match Definition size", instr.get());
514 check(instr->operands.size() == block.linear_preds.size(),
515 "Number of Operands does not match number of predecessors", instr.get());
516 } else if (instr->opcode == aco_opcode::p_extract ||
517 instr->opcode == aco_opcode::p_insert) {
518 check(!instr->operands[0].isConstant(), "Data operand must not be constant",
520 check(instr->operands[1].isConstant(), "Index must be constant", instr.get());
521 if (instr->opcode == aco_opcode::p_extract)
522 check(instr->operands[3].isConstant(), "Sign-extend flag must be constant",
525 check(instr->definitions[0].regClass().type() != RegType::sgpr ||
526 instr->operands[0].regClass().type() == RegType::sgpr,
527 "Can't extract/insert VGPR to SGPR", instr.get());
529 if (instr->opcode == aco_opcode::p_insert)
530 check(instr->operands[0].bytes() == instr->definitions[0].bytes(),
531 "Sizes of p_insert data operand and definition must match", instr.get());
533 if (instr->definitions[0].regClass().type() == RegType::sgpr)
534 check(instr->definitions.size() >= 2 && instr->definitions[1].isFixed() &&
535 instr->definitions[1].physReg() == scc,
536 "SGPR extract/insert needs an SCC definition", instr.get());
538 unsigned data_bits = instr->operands[0].bytes() * 8u;
539 unsigned op_bits = instr->operands[2].constantValue();
541 if (instr->opcode == aco_opcode::p_insert) {
542 check(op_bits == 8 || op_bits == 16, "Size must be 8 or 16", instr.get());
543 check(op_bits < data_bits, "Size must be smaller than source", instr.get());
544 } else if (instr->opcode == aco_opcode::p_extract) {
545 check(op_bits == 8 || op_bits == 16 || op_bits == 32,
546 "Size must be 8 or 16 or 32", instr.get());
547 check(data_bits >= op_bits, "Can't extract more bits than what the data has.",
551 unsigned comp = data_bits / MAX2(op_bits, 1);
552 check(instr->operands[1].constantValue() < comp, "Index must be in-bounds",
554 } else if (instr->opcode == aco_opcode::p_jump_to_epilog) {
555 check(instr->definitions.size() == 0, "p_jump_to_epilog must have 0 definitions",
557 check(instr->operands.size() > 0 && instr->operands[0].isOfType(RegType::sgpr) &&
558 instr->operands[0].size() == 2,
559 "First operand of p_jump_to_epilog must be a SGPR", instr.get());
560 for (unsigned i = 1; i < instr->operands.size(); i++) {
562 instr->operands[i].isOfType(RegType::vgpr) || instr->operands[i].isUndefined(),
563 "Other operands of p_jump_to_epilog must be VGPRs or undef", instr.get());
565 } else if (instr->opcode == aco_opcode::p_dual_src_export_gfx11) {
566 check(instr->definitions.size() == 6,
567 "p_dual_src_export_gfx11 must have 6 definitions", instr.get());
568 check(instr->definitions[2].regClass().type() == RegType::vgpr &&
569 instr->definitions[2].regClass().size() == 1,
570 "Third definition of p_dual_src_export_gfx11 must be a v1", instr.get());
571 check(instr->definitions[3].regClass() == program->lane_mask,
572 "Fourth definition of p_dual_src_export_gfx11 must be a lane mask", instr.get());
573 check(instr->definitions[4].physReg() == vcc,
574 "Fifth definition of p_dual_src_export_gfx11 must be vcc", instr.get());
575 check(instr->definitions[5].physReg() == scc,
576 "Sixth definition of p_dual_src_export_gfx11 must be scc", instr.get());
577 check(instr->operands.size() == 8, "p_dual_src_export_gfx11 must have 8 operands",
579 for (unsigned i = 0; i < instr->operands.size(); i++) {
581 instr->operands[i].isOfType(RegType::vgpr) || instr->operands[i].isUndefined(),
582 "Operands of p_dual_src_export_gfx11 must be VGPRs or undef", instr.get());
584 } else if (instr->opcode == aco_opcode::p_start_linear_vgpr) {
585 check(instr->definitions.size() == 1, "Must have one definition", instr.get());
586 check(instr->operands.size() <= 1, "Must have one or zero operands", instr.get());
587 if (!instr->definitions.empty())
588 check(instr->definitions[0].regClass().is_linear_vgpr(),
589 "Definition must be linear VGPR", instr.get());
590 if (!instr->definitions.empty() && !instr->operands.empty())
591 check(instr->definitions[0].bytes() == instr->operands[0].bytes(),
592 "Operand size must match definition", instr.get());
596 case Format::PSEUDO_REDUCTION: {
597 for (const Operand& op : instr->operands)
598 check(op.regClass().type() == RegType::vgpr,
599 "All operands of PSEUDO_REDUCTION instructions must be in VGPRs.",
602 if (instr->opcode == aco_opcode::p_reduce &&
603 instr->reduction().cluster_size == program->wave_size)
604 check(instr->definitions[0].regClass().type() == RegType::sgpr ||
605 program->wave_size == 32,
606 "The result of unclustered reductions must go into an SGPR.", instr.get());
608 check(instr->definitions[0].regClass().type() == RegType::vgpr,
609 "The result of scans and clustered reductions must go into a VGPR.",
615 if (instr->operands.size() >= 1)
616 check(instr->operands[0].isOfType(RegType::sgpr), "SMEM operands must be sgpr",
618 if (instr->operands.size() >= 2)
619 check(instr->operands[1].isConstant() || instr->operands[1].isOfType(RegType::sgpr),
620 "SMEM offset must be constant or sgpr", instr.get());
621 if (!instr->definitions.empty())
622 check(instr->definitions[0].regClass().type() == RegType::sgpr,
623 "SMEM result must be sgpr", instr.get());
627 case Format::MUBUF: {
628 check(instr->operands.size() > 1, "VMEM instructions must have at least one operand",
630 check(instr->operands[1].isOfType(RegType::vgpr),
631 "VADDR must be in vgpr for VMEM instructions", instr.get());
632 check(instr->operands[0].isOfType(RegType::sgpr), "VMEM resource constant must be sgpr",
634 check(instr->operands.size() < 4 || instr->operands[3].isOfType(RegType::vgpr),
635 "VMEM write data must be vgpr", instr.get());
637 const bool d16 = instr->opcode == aco_opcode::buffer_load_dword || // FIXME: used to spill subdword variables
638 instr->opcode == aco_opcode::buffer_load_ubyte ||
639 instr->opcode == aco_opcode::buffer_load_sbyte ||
640 instr->opcode == aco_opcode::buffer_load_ushort ||
641 instr->opcode == aco_opcode::buffer_load_sshort ||
642 instr->opcode == aco_opcode::buffer_load_ubyte_d16 ||
643 instr->opcode == aco_opcode::buffer_load_ubyte_d16_hi ||
644 instr->opcode == aco_opcode::buffer_load_sbyte_d16 ||
645 instr->opcode == aco_opcode::buffer_load_sbyte_d16_hi ||
646 instr->opcode == aco_opcode::buffer_load_short_d16 ||
647 instr->opcode == aco_opcode::buffer_load_short_d16_hi ||
648 instr->opcode == aco_opcode::buffer_load_format_d16_x ||
649 instr->opcode == aco_opcode::buffer_load_format_d16_hi_x ||
650 instr->opcode == aco_opcode::buffer_load_format_d16_xy ||
651 instr->opcode == aco_opcode::buffer_load_format_d16_xyz ||
652 instr->opcode == aco_opcode::buffer_load_format_d16_xyzw ||
653 instr->opcode == aco_opcode::tbuffer_load_format_d16_x ||
654 instr->opcode == aco_opcode::tbuffer_load_format_d16_xy ||
655 instr->opcode == aco_opcode::tbuffer_load_format_d16_xyz ||
656 instr->opcode == aco_opcode::tbuffer_load_format_d16_xyzw;
657 if (instr->definitions.size()) {
658 check(instr->definitions[0].regClass().type() == RegType::vgpr,
659 "VMEM definitions[0] (VDATA) must be VGPR", instr.get());
660 check(d16 || !instr->definitions[0].regClass().is_subdword(),
661 "Only D16 opcodes can load subdword values.", instr.get());
662 check(instr->definitions[0].bytes() <= 8 || !d16,
663 "D16 opcodes can only load up to 8 bytes.", instr.get());
668 check(instr->operands.size() >= 4, "MIMG instructions must have at least 4 operands",
670 check(instr->operands[0].hasRegClass() &&
671 (instr->operands[0].regClass() == s4 || instr->operands[0].regClass() == s8),
672 "MIMG operands[0] (resource constant) must be in 4 or 8 SGPRs", instr.get());
673 if (instr->operands[1].hasRegClass())
674 check(instr->operands[1].regClass() == s4,
675 "MIMG operands[1] (sampler constant) must be 4 SGPRs", instr.get());
676 if (!instr->operands[2].isUndefined()) {
677 bool is_cmpswap = instr->opcode == aco_opcode::image_atomic_cmpswap ||
678 instr->opcode == aco_opcode::image_atomic_fcmpswap;
679 check(instr->definitions.empty() ||
680 (instr->definitions[0].regClass() == instr->operands[2].regClass() ||
682 "MIMG operands[2] (VDATA) must be the same as definitions[0] for atomics and "
687 if (instr->mimg().strict_wqm) {
688 check(instr->operands[3].hasRegClass() &&
689 instr->operands[3].regClass().is_linear_vgpr(),
690 "MIMG operands[3] must be temp linear VGPR.", instr.get());
692 unsigned total_size = 0;
693 for (unsigned i = 4; i < instr->operands.size(); i++) {
694 check(instr->operands[i].hasRegClass() && instr->operands[i].regClass() == v1,
695 "MIMG operands[4+] (VADDR) must be v1", instr.get());
696 total_size += instr->operands[i].bytes();
698 check(total_size <= instr->operands[3].bytes(),
699 "MIMG operands[4+] must fit within operands[3].", instr.get());
701 check(instr->operands.size() == 4 || program->gfx_level >= GFX10,
702 "NSA is only supported on GFX10+", instr.get());
703 for (unsigned i = 3; i < instr->operands.size(); i++) {
704 check(instr->operands[i].hasRegClass() &&
705 instr->operands[i].regClass().type() == RegType::vgpr,
706 "MIMG operands[3+] (VADDR) must be VGPR", instr.get());
707 if (instr->operands.size() > 4) {
708 if (program->gfx_level < GFX11) {
709 check(instr->operands[i].regClass() == v1,
710 "GFX10 MIMG VADDR must be v1 if NSA is used", instr.get());
712 if (instr->opcode != aco_opcode::image_bvh_intersect_ray &&
713 instr->opcode != aco_opcode::image_bvh64_intersect_ray && i < 7) {
714 check(instr->operands[i].regClass() == v1,
715 "first 4 GFX11 MIMG VADDR must be v1 if NSA is used", instr.get());
722 if (instr->definitions.size()) {
723 check(instr->definitions[0].regClass().type() == RegType::vgpr,
724 "MIMG definitions[0] (VDATA) must be VGPR", instr.get());
725 check(instr->mimg().d16 || !instr->definitions[0].regClass().is_subdword(),
726 "Only D16 MIMG instructions can load subdword values.", instr.get());
727 check(instr->definitions[0].bytes() <= 8 || !instr->mimg().d16,
728 "D16 MIMG instructions can only load up to 8 bytes.", instr.get());
733 for (const Operand& op : instr->operands) {
734 check(op.isOfType(RegType::vgpr) || op.physReg() == m0 || op.isUndefined(),
735 "Only VGPRs are valid DS instruction operands", instr.get());
737 if (!instr->definitions.empty())
738 check(instr->definitions[0].regClass().type() == RegType::vgpr,
739 "DS instruction must return VGPR", instr.get());
743 for (unsigned i = 0; i < 4; i++)
744 check(instr->operands[i].isOfType(RegType::vgpr),
745 "Only VGPRs are valid Export arguments", instr.get());
749 check(instr->operands[1].isUndefined(), "Flat instructions don't support SADDR",
753 check(instr->operands[0].isOfType(RegType::vgpr), "FLAT/GLOBAL address must be vgpr",
756 case Format::SCRATCH: {
757 check(instr->operands[0].isOfType(RegType::vgpr),
758 "FLAT/GLOBAL/SCRATCH address must be undefined or vgpr", instr.get());
759 check(instr->operands[1].isOfType(RegType::sgpr),
760 "FLAT/GLOBAL/SCRATCH sgpr address must be undefined or sgpr", instr.get());
761 if (instr->format == Format::SCRATCH && program->gfx_level < GFX10_3)
762 check(!instr->operands[0].isUndefined() || !instr->operands[1].isUndefined(),
763 "SCRATCH must have either SADDR or ADDR operand", instr.get());
764 if (!instr->definitions.empty())
765 check(instr->definitions[0].regClass().type() == RegType::vgpr,
766 "FLAT/GLOBAL/SCRATCH result must be vgpr", instr.get());
768 check(instr->operands[2].isOfType(RegType::vgpr),
769 "FLAT/GLOBAL/SCRATCH data must be vgpr", instr.get());
772 case Format::LDSDIR: {
773 check(instr->definitions.size() == 1 && instr->definitions[0].regClass() == v1, "LDSDIR must have an v1 definition", instr.get());
774 check(instr->operands.size() == 1, "LDSDIR must have an operand", instr.get());
775 if (!instr->operands.empty()) {
776 check(instr->operands[0].regClass() == s1, "LDSDIR must have an s1 operand", instr.get());
777 check(instr->operands[0].isFixed() && instr->operands[0].physReg() == m0, "LDSDIR must have an operand fixed to m0", instr.get());
787 for (unsigned i = 0; i < program->blocks.size(); i++) {
788 Block& block = program->blocks[i];
789 check_block(block.index == i, "block.index must match actual index", &block);
791 /* predecessors/successors should be sorted */
792 for (unsigned j = 0; j + 1 < block.linear_preds.size(); j++)
793 check_block(block.linear_preds[j] < block.linear_preds[j + 1],
794 "linear predecessors must be sorted", &block);
795 for (unsigned j = 0; j + 1 < block.logical_preds.size(); j++)
796 check_block(block.logical_preds[j] < block.logical_preds[j + 1],
797 "logical predecessors must be sorted", &block);
798 for (unsigned j = 0; j + 1 < block.linear_succs.size(); j++)
799 check_block(block.linear_succs[j] < block.linear_succs[j + 1],
800 "linear successors must be sorted", &block);
801 for (unsigned j = 0; j + 1 < block.logical_succs.size(); j++)
802 check_block(block.logical_succs[j] < block.logical_succs[j + 1],
803 "logical successors must be sorted", &block);
805 /* critical edges are not allowed */
806 if (block.linear_preds.size() > 1) {
807 for (unsigned pred : block.linear_preds)
808 check_block(program->blocks[pred].linear_succs.size() == 1,
809 "linear critical edges are not allowed", &program->blocks[pred]);
810 for (unsigned pred : block.logical_preds)
811 check_block(program->blocks[pred].logical_succs.size() == 1,
812 "logical critical edges are not allowed", &program->blocks[pred]);
823 Location() : block(NULL), instr(NULL) {}
826 Instruction* instr; // NULL if it's the block's live-in
837 ra_fail(Program* program, Location loc, Location loc2, const char* fmt, ...)
842 vsprintf(msg, fmt, args);
847 struct u_memstream mem;
848 u_memstream_open(&mem, &out, &outsize);
849 FILE* const memf = u_memstream_get(&mem);
851 fprintf(memf, "RA error found at instruction in BB%d:\n", loc.block->index);
853 aco_print_instr(program->gfx_level, loc.instr, memf);
854 fprintf(memf, "\n%s", msg);
856 fprintf(memf, "%s", msg);
859 fprintf(memf, " in BB%d:\n", loc2.block->index);
860 aco_print_instr(program->gfx_level, loc2.instr, memf);
862 fprintf(memf, "\n\n");
863 u_memstream_close(&mem);
865 aco_err(program, "%s", out);
872 validate_subdword_operand(amd_gfx_level gfx_level, const aco_ptr<Instruction>& instr,
875 Operand op = instr->operands[index];
876 unsigned byte = op.physReg().byte();
878 if (instr->opcode == aco_opcode::p_as_uniform)
880 if (instr->isPseudo() && gfx_level >= GFX8)
883 return byte + instr->sdwa().sel[index].offset() + instr->sdwa().sel[index].size() <= 4 &&
884 byte % instr->sdwa().sel[index].size() == 0;
885 if (instr->isVOP3P()) {
886 bool fma_mix = instr->opcode == aco_opcode::v_fma_mixlo_f16 ||
887 instr->opcode == aco_opcode::v_fma_mixhi_f16 ||
888 instr->opcode == aco_opcode::v_fma_mix_f32;
889 return instr->valu().opsel_lo[index] == (byte >> 1) &&
890 instr->valu().opsel_hi[index] == (fma_mix || (byte >> 1));
892 if (byte == 2 && can_use_opsel(gfx_level, instr->opcode, index))
895 switch (instr->opcode) {
896 case aco_opcode::v_cvt_f32_ubyte1:
900 case aco_opcode::v_cvt_f32_ubyte2:
904 case aco_opcode::v_cvt_f32_ubyte3:
908 case aco_opcode::ds_write_b8_d16_hi:
909 case aco_opcode::ds_write_b16_d16_hi:
910 if (byte == 2 && index == 1)
913 case aco_opcode::buffer_store_byte_d16_hi:
914 case aco_opcode::buffer_store_short_d16_hi:
915 case aco_opcode::buffer_store_format_d16_hi_x:
916 if (byte == 2 && index == 3)
919 case aco_opcode::flat_store_byte_d16_hi:
920 case aco_opcode::flat_store_short_d16_hi:
921 case aco_opcode::scratch_store_byte_d16_hi:
922 case aco_opcode::scratch_store_short_d16_hi:
923 case aco_opcode::global_store_byte_d16_hi:
924 case aco_opcode::global_store_short_d16_hi:
925 if (byte == 2 && index == 2)
935 validate_subdword_definition(amd_gfx_level gfx_level, const aco_ptr<Instruction>& instr)
937 Definition def = instr->definitions[0];
938 unsigned byte = def.physReg().byte();
940 if (instr->isPseudo() && gfx_level >= GFX8)
943 return byte + instr->sdwa().dst_sel.offset() + instr->sdwa().dst_sel.size() <= 4 &&
944 byte % instr->sdwa().dst_sel.size() == 0;
945 if (byte == 2 && can_use_opsel(gfx_level, instr->opcode, -1))
948 switch (instr->opcode) {
949 case aco_opcode::v_fma_mixhi_f16:
950 case aco_opcode::buffer_load_ubyte_d16_hi:
951 case aco_opcode::buffer_load_sbyte_d16_hi:
952 case aco_opcode::buffer_load_short_d16_hi:
953 case aco_opcode::buffer_load_format_d16_hi_x:
954 case aco_opcode::flat_load_ubyte_d16_hi:
955 case aco_opcode::flat_load_short_d16_hi:
956 case aco_opcode::scratch_load_ubyte_d16_hi:
957 case aco_opcode::scratch_load_short_d16_hi:
958 case aco_opcode::global_load_ubyte_d16_hi:
959 case aco_opcode::global_load_short_d16_hi:
960 case aco_opcode::ds_read_u8_d16_hi:
961 case aco_opcode::ds_read_u16_d16_hi: return byte == 2;
969 get_subdword_bytes_written(Program* program, const aco_ptr<Instruction>& instr, unsigned index)
971 amd_gfx_level gfx_level = program->gfx_level;
972 Definition def = instr->definitions[index];
974 if (instr->isPseudo())
975 return gfx_level >= GFX8 ? def.bytes() : def.size() * 4u;
976 if (instr->isVALU()) {
977 assert(def.bytes() <= 2);
979 return instr->sdwa().dst_sel.size();
981 if (instr_is_16bit(gfx_level, instr->opcode))
987 if (instr->isMIMG()) {
988 assert(instr->mimg().d16);
989 return program->dev.sram_ecc_enabled ? def.size() * 4u : def.bytes();
992 switch (instr->opcode) {
993 case aco_opcode::buffer_load_ubyte_d16:
994 case aco_opcode::buffer_load_sbyte_d16:
995 case aco_opcode::buffer_load_short_d16:
996 case aco_opcode::buffer_load_format_d16_x:
997 case aco_opcode::tbuffer_load_format_d16_x:
998 case aco_opcode::flat_load_ubyte_d16:
999 case aco_opcode::flat_load_short_d16:
1000 case aco_opcode::scratch_load_ubyte_d16:
1001 case aco_opcode::scratch_load_short_d16:
1002 case aco_opcode::global_load_ubyte_d16:
1003 case aco_opcode::global_load_short_d16:
1004 case aco_opcode::ds_read_u8_d16:
1005 case aco_opcode::ds_read_u16_d16:
1006 case aco_opcode::buffer_load_ubyte_d16_hi:
1007 case aco_opcode::buffer_load_sbyte_d16_hi:
1008 case aco_opcode::buffer_load_short_d16_hi:
1009 case aco_opcode::buffer_load_format_d16_hi_x:
1010 case aco_opcode::flat_load_ubyte_d16_hi:
1011 case aco_opcode::flat_load_short_d16_hi:
1012 case aco_opcode::scratch_load_ubyte_d16_hi:
1013 case aco_opcode::scratch_load_short_d16_hi:
1014 case aco_opcode::global_load_ubyte_d16_hi:
1015 case aco_opcode::global_load_short_d16_hi:
1016 case aco_opcode::ds_read_u8_d16_hi:
1017 case aco_opcode::ds_read_u16_d16_hi: return program->dev.sram_ecc_enabled ? 4 : 2;
1018 case aco_opcode::buffer_load_format_d16_xyz:
1019 case aco_opcode::tbuffer_load_format_d16_xyz: return program->dev.sram_ecc_enabled ? 8 : 6;
1020 default: return def.size() * 4;
1025 validate_instr_defs(Program* program, std::array<unsigned, 2048>& regs,
1026 const std::vector<Assignment>& assignments, const Location& loc,
1027 aco_ptr<Instruction>& instr)
1031 for (unsigned i = 0; i < instr->definitions.size(); i++) {
1032 Definition& def = instr->definitions[i];
1035 Temp tmp = def.getTemp();
1036 PhysReg reg = assignments[tmp.id()].reg;
1037 for (unsigned j = 0; j < tmp.bytes(); j++) {
1038 if (regs[reg.reg_b + j])
1040 ra_fail(program, loc, assignments[regs[reg.reg_b + j]].defloc,
1041 "Assignment of element %d of %%%d already taken by %%%d from instruction", i,
1042 tmp.id(), regs[reg.reg_b + j]);
1043 regs[reg.reg_b + j] = tmp.id();
1045 if (def.regClass().is_subdword() && def.bytes() < 4) {
1046 unsigned written = get_subdword_bytes_written(program, instr, i);
1047 /* If written=4, the instruction still might write the upper half. In that case, it's
1048 * the lower half that isn't preserved */
1049 for (unsigned j = reg.byte() & ~(written - 1); j < written; j++) {
1050 unsigned written_reg = reg.reg() * 4u + j;
1051 if (regs[written_reg] && regs[written_reg] != def.tempId())
1052 err |= ra_fail(program, loc, assignments[regs[written_reg]].defloc,
1053 "Assignment of element %d of %%%d overwrites the full register "
1054 "taken by %%%d from instruction",
1055 i, tmp.id(), regs[written_reg]);
1060 for (const Definition& def : instr->definitions) {
1064 for (unsigned j = 0; j < def.getTemp().bytes(); j++)
1065 regs[def.physReg().reg_b + j] = 0;
1072 } /* end namespace */
1075 validate_ra(Program* program)
1077 if (!(debug_flags & DEBUG_VALIDATE_RA))
1081 aco::live live_vars = aco::live_var_analysis(program);
1082 std::vector<std::vector<Temp>> phi_sgpr_ops(program->blocks.size());
1083 uint16_t sgpr_limit = get_addr_sgpr_from_waves(program, program->num_waves);
1085 std::vector<Assignment> assignments(program->peekAllocationId());
1086 for (Block& block : program->blocks) {
1089 for (aco_ptr<Instruction>& instr : block.instructions) {
1090 if (instr->opcode == aco_opcode::p_phi) {
1091 for (unsigned i = 0; i < instr->operands.size(); i++) {
1092 if (instr->operands[i].isTemp() &&
1093 instr->operands[i].getTemp().type() == RegType::sgpr &&
1094 instr->operands[i].isFirstKill())
1095 phi_sgpr_ops[block.logical_preds[i]].emplace_back(instr->operands[i].getTemp());
1099 loc.instr = instr.get();
1100 for (unsigned i = 0; i < instr->operands.size(); i++) {
1101 Operand& op = instr->operands[i];
1105 err |= ra_fail(program, loc, Location(), "Operand %d is not assigned a register", i);
1106 if (assignments[op.tempId()].valid && assignments[op.tempId()].reg != op.physReg())
1108 ra_fail(program, loc, assignments[op.tempId()].firstloc,
1109 "Operand %d has an inconsistent register assignment with instruction", i);
1110 if ((op.getTemp().type() == RegType::vgpr &&
1111 op.physReg().reg_b + op.bytes() > (256 + program->config->num_vgprs) * 4) ||
1112 (op.getTemp().type() == RegType::sgpr &&
1113 op.physReg() + op.size() > program->config->num_sgprs &&
1114 op.physReg() < sgpr_limit))
1115 err |= ra_fail(program, loc, assignments[op.tempId()].firstloc,
1116 "Operand %d has an out-of-bounds register assignment", i);
1117 if (op.physReg() == vcc && !program->needs_vcc)
1118 err |= ra_fail(program, loc, Location(),
1119 "Operand %d fixed to vcc but needs_vcc=false", i);
1120 if (op.regClass().is_subdword() &&
1121 !validate_subdword_operand(program->gfx_level, instr, i))
1122 err |= ra_fail(program, loc, Location(), "Operand %d not aligned correctly", i);
1123 if (!assignments[op.tempId()].firstloc.block)
1124 assignments[op.tempId()].firstloc = loc;
1125 if (!assignments[op.tempId()].defloc.block) {
1126 assignments[op.tempId()].reg = op.physReg();
1127 assignments[op.tempId()].valid = true;
1131 for (unsigned i = 0; i < instr->definitions.size(); i++) {
1132 Definition& def = instr->definitions[i];
1137 ra_fail(program, loc, Location(), "Definition %d is not assigned a register", i);
1138 if (assignments[def.tempId()].defloc.block)
1139 err |= ra_fail(program, loc, assignments[def.tempId()].defloc,
1140 "Temporary %%%d also defined by instruction", def.tempId());
1141 if ((def.getTemp().type() == RegType::vgpr &&
1142 def.physReg().reg_b + def.bytes() > (256 + program->config->num_vgprs) * 4) ||
1143 (def.getTemp().type() == RegType::sgpr &&
1144 def.physReg() + def.size() > program->config->num_sgprs &&
1145 def.physReg() < sgpr_limit))
1146 err |= ra_fail(program, loc, assignments[def.tempId()].firstloc,
1147 "Definition %d has an out-of-bounds register assignment", i);
1148 if (def.physReg() == vcc && !program->needs_vcc)
1149 err |= ra_fail(program, loc, Location(),
1150 "Definition %d fixed to vcc but needs_vcc=false", i);
1151 if (def.regClass().is_subdword() &&
1152 !validate_subdword_definition(program->gfx_level, instr))
1153 err |= ra_fail(program, loc, Location(), "Definition %d not aligned correctly", i);
1154 if (!assignments[def.tempId()].firstloc.block)
1155 assignments[def.tempId()].firstloc = loc;
1156 assignments[def.tempId()].defloc = loc;
1157 assignments[def.tempId()].reg = def.physReg();
1158 assignments[def.tempId()].valid = true;
1163 for (Block& block : program->blocks) {
1167 std::array<unsigned, 2048> regs; /* register file in bytes */
1170 IDSet live = live_vars.live_out[block.index];
1171 /* remove killed p_phi sgpr operands */
1172 for (Temp tmp : phi_sgpr_ops[block.index])
1173 live.erase(tmp.id());
1175 /* check live out */
1176 for (unsigned id : live) {
1177 Temp tmp(id, program->temp_rc[id]);
1178 PhysReg reg = assignments[id].reg;
1179 for (unsigned i = 0; i < tmp.bytes(); i++) {
1180 if (regs[reg.reg_b + i]) {
1181 err |= ra_fail(program, loc, Location(),
1182 "Assignment of element %d of %%%d already taken by %%%d in live-out",
1183 i, id, regs[reg.reg_b + i]);
1185 regs[reg.reg_b + i] = id;
1190 for (auto it = block.instructions.rbegin(); it != block.instructions.rend(); ++it) {
1191 aco_ptr<Instruction>& instr = *it;
1193 /* check killed p_phi sgpr operands */
1194 if (instr->opcode == aco_opcode::p_logical_end) {
1195 for (Temp tmp : phi_sgpr_ops[block.index]) {
1196 PhysReg reg = assignments[tmp.id()].reg;
1197 for (unsigned i = 0; i < tmp.bytes(); i++) {
1198 if (regs[reg.reg_b + i])
1200 program, loc, Location(),
1201 "Assignment of element %d of %%%d already taken by %%%d in live-out", i,
1202 tmp.id(), regs[reg.reg_b + i]);
1204 live.insert(tmp.id());
1208 for (const Definition& def : instr->definitions) {
1211 live.erase(def.tempId());
1214 /* don't count phi operands as live-in, since they are actually
1215 * killed when they are copied at the predecessor */
1216 if (instr->opcode != aco_opcode::p_phi && instr->opcode != aco_opcode::p_linear_phi) {
1217 for (const Operand& op : instr->operands) {
1220 live.insert(op.tempId());
1225 for (unsigned id : live) {
1226 Temp tmp(id, program->temp_rc[id]);
1227 PhysReg reg = assignments[id].reg;
1228 for (unsigned i = 0; i < tmp.bytes(); i++)
1229 regs[reg.reg_b + i] = id;
1232 for (aco_ptr<Instruction>& instr : block.instructions) {
1233 loc.instr = instr.get();
1235 /* remove killed p_phi operands from regs */
1236 if (instr->opcode == aco_opcode::p_logical_end) {
1237 for (Temp tmp : phi_sgpr_ops[block.index]) {
1238 PhysReg reg = assignments[tmp.id()].reg;
1239 for (unsigned i = 0; i < tmp.bytes(); i++)
1240 regs[reg.reg_b + i] = 0;
1244 if (instr->opcode != aco_opcode::p_phi && instr->opcode != aco_opcode::p_linear_phi) {
1245 for (const Operand& op : instr->operands) {
1248 if (op.isFirstKillBeforeDef()) {
1249 for (unsigned j = 0; j < op.getTemp().bytes(); j++)
1250 regs[op.physReg().reg_b + j] = 0;
1255 if (!instr->isBranch() || block.linear_succs.size() != 1)
1256 err |= validate_instr_defs(program, regs, assignments, loc, instr);
1258 if (!is_phi(instr)) {
1259 for (const Operand& op : instr->operands) {
1262 if (op.isLateKill() && op.isFirstKill()) {
1263 for (unsigned j = 0; j < op.getTemp().bytes(); j++)
1264 regs[op.physReg().reg_b + j] = 0;
1267 } else if (block.linear_preds.size() != 1 ||
1268 program->blocks[block.linear_preds[0]].linear_succs.size() == 1) {
1269 for (unsigned pred : block.linear_preds) {
1270 aco_ptr<Instruction>& br = program->blocks[pred].instructions.back();
1271 assert(br->isBranch());
1272 err |= validate_instr_defs(program, regs, assignments, loc, br);