aco: fix sub-dword opsel/sdwa checks
[mesa.git] / src / amd / compiler / aco_validate.cpp
1 /*
2 * Copyright © 2018 Valve Corporation
3 *
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:
10 *
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
13 * Software.
14 *
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
21 * IN THE SOFTWARE.
22 *
23 */
24
25 #include "aco_ir.h"
26
27 #include <array>
28 #include <map>
29
30 namespace aco {
31
32 #ifndef NDEBUG
33 void perfwarn(bool cond, const char *msg, Instruction *instr)
34 {
35 if (cond) {
36 fprintf(stderr, "ACO performance warning: %s\n", msg);
37 if (instr) {
38 fprintf(stderr, "instruction: ");
39 aco_print_instr(instr, stderr);
40 fprintf(stderr, "\n");
41 }
42
43 if (debug_flags & DEBUG_PERFWARN)
44 exit(1);
45 }
46 }
47 #endif
48
49 void validate(Program* program, FILE * output)
50 {
51 if (!(debug_flags & DEBUG_VALIDATE))
52 return;
53
54 bool is_valid = true;
55 auto check = [&output, &is_valid](bool check, const char * msg, aco::Instruction * instr) -> void {
56 if (!check) {
57 fprintf(output, "%s: ", msg);
58 aco_print_instr(instr, output);
59 fprintf(output, "\n");
60 is_valid = false;
61 }
62 };
63 auto check_block = [&output, &is_valid](bool check, const char * msg, aco::Block * block) -> void {
64 if (!check) {
65 fprintf(output, "%s: BB%u\n", msg, block->index);
66 is_valid = false;
67 }
68 };
69
70 for (Block& block : program->blocks) {
71 for (aco_ptr<Instruction>& instr : block.instructions) {
72
73 /* check base format */
74 Format base_format = instr->format;
75 base_format = (Format)((uint32_t)base_format & ~(uint32_t)Format::SDWA);
76 base_format = (Format)((uint32_t)base_format & ~(uint32_t)Format::DPP);
77 if ((uint32_t)base_format & (uint32_t)Format::VOP1)
78 base_format = Format::VOP1;
79 else if ((uint32_t)base_format & (uint32_t)Format::VOP2)
80 base_format = Format::VOP2;
81 else if ((uint32_t)base_format & (uint32_t)Format::VOPC)
82 base_format = Format::VOPC;
83 else if ((uint32_t)base_format & (uint32_t)Format::VINTRP) {
84 if (instr->opcode == aco_opcode::v_interp_p1ll_f16 ||
85 instr->opcode == aco_opcode::v_interp_p1lv_f16 ||
86 instr->opcode == aco_opcode::v_interp_p2_legacy_f16 ||
87 instr->opcode == aco_opcode::v_interp_p2_f16) {
88 /* v_interp_*_fp16 are considered VINTRP by the compiler but
89 * they are emitted as VOP3.
90 */
91 base_format = Format::VOP3;
92 } else {
93 base_format = Format::VINTRP;
94 }
95 }
96 check(base_format == instr_info.format[(int)instr->opcode], "Wrong base format for instruction", instr.get());
97
98 /* check VOP3 modifiers */
99 if (((uint32_t)instr->format & (uint32_t)Format::VOP3) && instr->format != Format::VOP3) {
100 check(base_format == Format::VOP2 ||
101 base_format == Format::VOP1 ||
102 base_format == Format::VOPC ||
103 base_format == Format::VINTRP,
104 "Format cannot have VOP3A/VOP3B applied", instr.get());
105 }
106
107 /* check SDWA */
108 if (instr->isSDWA()) {
109 check(base_format == Format::VOP2 ||
110 base_format == Format::VOP1 ||
111 base_format == Format::VOPC,
112 "Format cannot have SDWA applied", instr.get());
113
114 check(program->chip_class >= GFX8, "SDWA is GFX8+ only", instr.get());
115
116 SDWA_instruction *sdwa = static_cast<SDWA_instruction*>(instr.get());
117 check(sdwa->omod == 0 || program->chip_class >= GFX9, "SDWA omod only supported on GFX9+", instr.get());
118 if (base_format == Format::VOPC) {
119 check(sdwa->clamp == false || program->chip_class == GFX8, "SDWA VOPC clamp only supported on GFX8", instr.get());
120 check((instr->definitions[0].isFixed() && instr->definitions[0].physReg() == vcc) ||
121 program->chip_class >= GFX9,
122 "SDWA+VOPC definition must be fixed to vcc on GFX8", instr.get());
123 }
124
125 if (instr->operands.size() >= 3) {
126 check(instr->operands[2].isFixed() && instr->operands[2].physReg() == vcc,
127 "3rd operand must be fixed to vcc with SDWA", instr.get());
128 }
129 if (instr->definitions.size() >= 2) {
130 check(instr->definitions[1].isFixed() && instr->definitions[1].physReg() == vcc,
131 "2nd definition must be fixed to vcc with SDWA", instr.get());
132 }
133
134 check(instr->opcode != aco_opcode::v_madmk_f32 &&
135 instr->opcode != aco_opcode::v_madak_f32 &&
136 instr->opcode != aco_opcode::v_madmk_f16 &&
137 instr->opcode != aco_opcode::v_madak_f16 &&
138 instr->opcode != aco_opcode::v_readfirstlane_b32 &&
139 instr->opcode != aco_opcode::v_clrexcp &&
140 instr->opcode != aco_opcode::v_swap_b32,
141 "SDWA can't be used with this opcode", instr.get());
142 if (program->chip_class != GFX8) {
143 check(instr->opcode != aco_opcode::v_mac_f32 &&
144 instr->opcode != aco_opcode::v_mac_f16 &&
145 instr->opcode != aco_opcode::v_fmac_f32 &&
146 instr->opcode != aco_opcode::v_fmac_f16,
147 "SDWA can't be used with this opcode", instr.get());
148 }
149
150 for (unsigned i = 0; i < MIN2(instr->operands.size(), 2); i++) {
151 if (instr->operands[i].hasRegClass() && instr->operands[i].regClass().is_subdword())
152 check((sdwa->sel[i] & sdwa_asuint) == (sdwa_isra | instr->operands[i].bytes()), "Unexpected SDWA sel for sub-dword operand", instr.get());
153 }
154 if (instr->definitions[0].regClass().is_subdword())
155 check((sdwa->dst_sel & sdwa_asuint) == (sdwa_isra | instr->definitions[0].bytes()), "Unexpected SDWA sel for sub-dword definition", instr.get());
156 }
157
158 /* check opsel */
159 if (instr->isVOP3()) {
160 VOP3A_instruction *vop3 = static_cast<VOP3A_instruction*>(instr.get());
161 check(vop3->opsel == 0 || program->chip_class >= GFX9, "Opsel is only supported on GFX9+", instr.get());
162 check((vop3->opsel & ~(0x10 | ((1 << instr->operands.size()) - 1))) == 0, "Unused bits in opsel must be zeroed out", instr.get());
163
164 for (unsigned i = 0; i < instr->operands.size(); i++) {
165 if (instr->operands[i].hasRegClass() && instr->operands[i].regClass().is_subdword() && !instr->operands[i].isFixed())
166 check((vop3->opsel & (1 << i)) == 0, "Unexpected opsel for sub-dword operand", instr.get());
167 }
168 if (instr->definitions[0].regClass().is_subdword() && !instr->definitions[0].isFixed())
169 check((vop3->opsel & (1 << 3)) == 0, "Unexpected opsel for sub-dword definition", instr.get());
170 }
171
172 /* check for undefs */
173 for (unsigned i = 0; i < instr->operands.size(); i++) {
174 if (instr->operands[i].isUndefined()) {
175 bool flat = instr->format == Format::FLAT || instr->format == Format::SCRATCH || instr->format == Format::GLOBAL;
176 bool can_be_undef = is_phi(instr) || instr->format == Format::EXP ||
177 instr->format == Format::PSEUDO_REDUCTION ||
178 instr->opcode == aco_opcode::p_create_vector ||
179 (flat && i == 1) || (instr->format == Format::MIMG && i == 1) ||
180 ((instr->format == Format::MUBUF || instr->format == Format::MTBUF) && i == 1);
181 check(can_be_undef, "Undefs can only be used in certain operands", instr.get());
182 } else {
183 check(instr->operands[i].isFixed() || instr->operands[i].isTemp() || instr->operands[i].isConstant(), "Uninitialized Operand", instr.get());
184 }
185 }
186
187 /* check subdword definitions */
188 for (unsigned i = 0; i < instr->definitions.size(); i++) {
189 if (instr->definitions[i].regClass().is_subdword())
190 check(instr->format == Format::PSEUDO || instr->definitions[i].bytes() <= 4, "Only Pseudo instructions can write subdword registers larger than 4 bytes", instr.get());
191 }
192
193 if (instr->isSALU() || instr->isVALU()) {
194 /* check literals */
195 Operand literal(s1);
196 for (unsigned i = 0; i < instr->operands.size(); i++)
197 {
198 Operand op = instr->operands[i];
199 if (!op.isLiteral())
200 continue;
201
202 check(instr->format == Format::SOP1 ||
203 instr->format == Format::SOP2 ||
204 instr->format == Format::SOPC ||
205 instr->format == Format::VOP1 ||
206 instr->format == Format::VOP2 ||
207 instr->format == Format::VOPC ||
208 (instr->isVOP3() && program->chip_class >= GFX10),
209 "Literal applied on wrong instruction format", instr.get());
210
211 check(literal.isUndefined() || (literal.size() == op.size() && literal.constantValue() == op.constantValue()), "Only 1 Literal allowed", instr.get());
212 literal = op;
213 check(!instr->isVALU() || instr->isVOP3() || i == 0 || i == 2, "Wrong source position for Literal argument", instr.get());
214 }
215
216 /* check num sgprs for VALU */
217 if (instr->isVALU()) {
218 bool is_shift64 = instr->opcode == aco_opcode::v_lshlrev_b64 ||
219 instr->opcode == aco_opcode::v_lshrrev_b64 ||
220 instr->opcode == aco_opcode::v_ashrrev_i64;
221 unsigned const_bus_limit = 1;
222 if (program->chip_class >= GFX10 && !is_shift64)
223 const_bus_limit = 2;
224
225 uint32_t scalar_mask = instr->isVOP3() ? 0x7 : 0x5;
226 if (instr->isSDWA())
227 scalar_mask = program->chip_class >= GFX9 ? 0x7 : 0x4;
228
229 check(instr->definitions[0].getTemp().type() == RegType::vgpr ||
230 (int) instr->format & (int) Format::VOPC ||
231 instr->opcode == aco_opcode::v_readfirstlane_b32 ||
232 instr->opcode == aco_opcode::v_readlane_b32 ||
233 instr->opcode == aco_opcode::v_readlane_b32_e64,
234 "Wrong Definition type for VALU instruction", instr.get());
235 unsigned num_sgprs = 0;
236 unsigned sgpr[] = {0, 0};
237 for (unsigned i = 0; i < instr->operands.size(); i++)
238 {
239 Operand op = instr->operands[i];
240 if (instr->opcode == aco_opcode::v_readfirstlane_b32 ||
241 instr->opcode == aco_opcode::v_readlane_b32 ||
242 instr->opcode == aco_opcode::v_readlane_b32_e64 ||
243 instr->opcode == aco_opcode::v_writelane_b32 ||
244 instr->opcode == aco_opcode::v_writelane_b32_e64) {
245 check(!op.isLiteral(), "No literal allowed on VALU instruction", instr.get());
246 check(i == 1 || (op.isTemp() && op.regClass().type() == RegType::vgpr && op.bytes() <= 4), "Wrong Operand type for VALU instruction", instr.get());
247 continue;
248 }
249 if (op.isTemp() && instr->operands[i].regClass().type() == RegType::sgpr) {
250 check(scalar_mask & (1 << i), "Wrong source position for SGPR argument", instr.get());
251
252 if (op.tempId() != sgpr[0] && op.tempId() != sgpr[1]) {
253 if (num_sgprs < 2)
254 sgpr[num_sgprs++] = op.tempId();
255 }
256 }
257
258 if (op.isConstant() && !op.isLiteral())
259 check(scalar_mask & (1 << i), "Wrong source position for constant argument", instr.get());
260 }
261 check(num_sgprs + (literal.isUndefined() ? 0 : 1) <= const_bus_limit, "Too many SGPRs/literals", instr.get());
262 }
263
264 if (instr->format == Format::SOP1 || instr->format == Format::SOP2) {
265 check(instr->definitions[0].getTemp().type() == RegType::sgpr, "Wrong Definition type for SALU instruction", instr.get());
266 for (const Operand& op : instr->operands) {
267 check(op.isConstant() || op.regClass().type() <= RegType::sgpr,
268 "Wrong Operand type for SALU instruction", instr.get());
269 }
270 }
271 }
272
273 switch (instr->format) {
274 case Format::PSEUDO: {
275 bool is_subdword = false;
276 bool has_const_sgpr = false;
277 bool has_literal = false;
278 for (Definition def : instr->definitions)
279 is_subdword |= def.regClass().is_subdword();
280 for (unsigned i = 0; i < instr->operands.size(); i++) {
281 if (instr->opcode == aco_opcode::p_extract_vector && i == 1)
282 continue;
283 Operand op = instr->operands[i];
284 is_subdword |= op.hasRegClass() && op.regClass().is_subdword();
285 has_const_sgpr |= op.isConstant() || (op.hasRegClass() && op.regClass().type() == RegType::sgpr);
286 has_literal |= op.isLiteral();
287 }
288
289 check(!is_subdword || !has_const_sgpr || program->chip_class >= GFX9,
290 "Sub-dword pseudo instructions can only take constants or SGPRs on GFX9+", instr.get());
291 check(!is_subdword || !has_literal, "Sub-dword pseudo instructions cannot take literals", instr.get());
292
293 if (instr->opcode == aco_opcode::p_create_vector) {
294 unsigned size = 0;
295 for (const Operand& op : instr->operands) {
296 size += op.bytes();
297 }
298 check(size == instr->definitions[0].bytes(), "Definition size does not match operand sizes", instr.get());
299 if (instr->definitions[0].getTemp().type() == RegType::sgpr) {
300 for (const Operand& op : instr->operands) {
301 check(op.isConstant() || op.regClass().type() == RegType::sgpr,
302 "Wrong Operand type for scalar vector", instr.get());
303 }
304 }
305 } else if (instr->opcode == aco_opcode::p_extract_vector) {
306 check((instr->operands[0].isTemp()) && instr->operands[1].isConstant(), "Wrong Operand types", instr.get());
307 check((instr->operands[1].constantValue() + 1) * instr->definitions[0].bytes() <= instr->operands[0].bytes(), "Index out of range", instr.get());
308 check(instr->definitions[0].getTemp().type() == RegType::vgpr || instr->operands[0].regClass().type() == RegType::sgpr,
309 "Cannot extract SGPR value from VGPR vector", instr.get());
310 } else if (instr->opcode == aco_opcode::p_parallelcopy) {
311 check(instr->definitions.size() == instr->operands.size(), "Number of Operands does not match number of Definitions", instr.get());
312 for (unsigned i = 0; i < instr->operands.size(); i++) {
313 if (instr->operands[i].isTemp())
314 check((instr->definitions[i].getTemp().type() == instr->operands[i].regClass().type()) ||
315 (instr->definitions[i].getTemp().type() == RegType::vgpr && instr->operands[i].regClass().type() == RegType::sgpr),
316 "Operand and Definition types do not match", instr.get());
317 }
318 } else if (instr->opcode == aco_opcode::p_phi) {
319 check(instr->operands.size() == block.logical_preds.size(), "Number of Operands does not match number of predecessors", instr.get());
320 check(instr->definitions[0].getTemp().type() == RegType::vgpr, "Logical Phi Definition must be vgpr", instr.get());
321 } else if (instr->opcode == aco_opcode::p_linear_phi) {
322 for (const Operand& op : instr->operands)
323 check(!op.isTemp() || op.getTemp().is_linear(), "Wrong Operand type", instr.get());
324 check(instr->operands.size() == block.linear_preds.size(), "Number of Operands does not match number of predecessors", instr.get());
325 }
326 break;
327 }
328 case Format::SMEM: {
329 if (instr->operands.size() >= 1)
330 check(instr->operands[0].isTemp() && instr->operands[0].regClass().type() == RegType::sgpr, "SMEM operands must be sgpr", instr.get());
331 if (instr->operands.size() >= 2)
332 check(instr->operands[1].isConstant() || (instr->operands[1].isTemp() && instr->operands[1].regClass().type() == RegType::sgpr),
333 "SMEM offset must be constant or sgpr", instr.get());
334 if (!instr->definitions.empty())
335 check(instr->definitions[0].getTemp().type() == RegType::sgpr, "SMEM result must be sgpr", instr.get());
336 break;
337 }
338 case Format::MTBUF:
339 case Format::MUBUF: {
340 check(instr->operands.size() > 1, "VMEM instructions must have at least one operand", instr.get());
341 check(instr->operands[1].hasRegClass() && instr->operands[1].regClass().type() == RegType::vgpr,
342 "VADDR must be in vgpr for VMEM instructions", instr.get());
343 check(instr->operands[0].isTemp() && instr->operands[0].regClass().type() == RegType::sgpr, "VMEM resource constant must be sgpr", instr.get());
344 check(instr->operands.size() < 4 || (instr->operands[3].isTemp() && instr->operands[3].regClass().type() == RegType::vgpr), "VMEM write data must be vgpr", instr.get());
345 break;
346 }
347 case Format::MIMG: {
348 check(instr->operands.size() == 3, "MIMG instructions must have exactly 3 operands", instr.get());
349 check(instr->operands[0].hasRegClass() && (instr->operands[0].regClass() == s4 || instr->operands[0].regClass() == s8),
350 "MIMG operands[0] (resource constant) must be in 4 or 8 SGPRs", instr.get());
351 if (instr->operands[1].hasRegClass() && instr->operands[1].regClass().type() == RegType::sgpr)
352 check(instr->operands[1].regClass() == s4, "MIMG operands[1] (sampler constant) must be 4 SGPRs", instr.get());
353 else if (instr->operands[1].hasRegClass() && instr->operands[1].regClass().type() == RegType::vgpr)
354 check((instr->definitions.empty() || instr->definitions[0].regClass() == instr->operands[1].regClass() ||
355 instr->opcode == aco_opcode::image_atomic_cmpswap || instr->opcode == aco_opcode::image_atomic_fcmpswap),
356 "MIMG operands[1] (VDATA) must be the same as definitions[0] for atomics", instr.get());
357 check(instr->operands[2].hasRegClass() && instr->operands[2].regClass().type() == RegType::vgpr,
358 "MIMG operands[2] (VADDR) must be VGPR", instr.get());
359 check(instr->definitions.empty() || (instr->definitions[0].isTemp() && instr->definitions[0].regClass().type() == RegType::vgpr),
360 "MIMG definitions[0] (VDATA) must be VGPR", instr.get());
361 break;
362 }
363 case Format::DS: {
364 for (const Operand& op : instr->operands) {
365 check((op.isTemp() && op.regClass().type() == RegType::vgpr) || op.physReg() == m0,
366 "Only VGPRs are valid DS instruction operands", instr.get());
367 }
368 if (!instr->definitions.empty())
369 check(instr->definitions[0].getTemp().type() == RegType::vgpr, "DS instruction must return VGPR", instr.get());
370 break;
371 }
372 case Format::EXP: {
373 for (unsigned i = 0; i < 4; i++)
374 check(instr->operands[i].hasRegClass() && instr->operands[i].regClass().type() == RegType::vgpr,
375 "Only VGPRs are valid Export arguments", instr.get());
376 break;
377 }
378 case Format::FLAT:
379 check(instr->operands[1].isUndefined(), "Flat instructions don't support SADDR", instr.get());
380 /* fallthrough */
381 case Format::GLOBAL:
382 case Format::SCRATCH: {
383 check(instr->operands[0].isTemp() && instr->operands[0].regClass().type() == RegType::vgpr, "FLAT/GLOBAL/SCRATCH address must be vgpr", instr.get());
384 check(instr->operands[1].hasRegClass() && instr->operands[1].regClass().type() == RegType::sgpr,
385 "FLAT/GLOBAL/SCRATCH sgpr address must be undefined or sgpr", instr.get());
386 if (!instr->definitions.empty())
387 check(instr->definitions[0].getTemp().type() == RegType::vgpr, "FLAT/GLOBAL/SCRATCH result must be vgpr", instr.get());
388 else
389 check(instr->operands[2].regClass().type() == RegType::vgpr, "FLAT/GLOBAL/SCRATCH data must be vgpr", instr.get());
390 break;
391 }
392 default:
393 break;
394 }
395 }
396 }
397
398 /* validate CFG */
399 for (unsigned i = 0; i < program->blocks.size(); i++) {
400 Block& block = program->blocks[i];
401 check_block(block.index == i, "block.index must match actual index", &block);
402
403 /* predecessors/successors should be sorted */
404 for (unsigned j = 0; j + 1 < block.linear_preds.size(); j++)
405 check_block(block.linear_preds[j] < block.linear_preds[j + 1], "linear predecessors must be sorted", &block);
406 for (unsigned j = 0; j + 1 < block.logical_preds.size(); j++)
407 check_block(block.logical_preds[j] < block.logical_preds[j + 1], "logical predecessors must be sorted", &block);
408 for (unsigned j = 0; j + 1 < block.linear_succs.size(); j++)
409 check_block(block.linear_succs[j] < block.linear_succs[j + 1], "linear successors must be sorted", &block);
410 for (unsigned j = 0; j + 1 < block.logical_succs.size(); j++)
411 check_block(block.logical_succs[j] < block.logical_succs[j + 1], "logical successors must be sorted", &block);
412
413 /* critical edges are not allowed */
414 if (block.linear_preds.size() > 1) {
415 for (unsigned pred : block.linear_preds)
416 check_block(program->blocks[pred].linear_succs.size() == 1, "linear critical edges are not allowed", &program->blocks[pred]);
417 for (unsigned pred : block.logical_preds)
418 check_block(program->blocks[pred].logical_succs.size() == 1, "logical critical edges are not allowed", &program->blocks[pred]);
419 }
420 }
421
422 assert(is_valid);
423 }
424
425 /* RA validation */
426 namespace {
427
428 struct Location {
429 Location() : block(NULL), instr(NULL) {}
430
431 Block *block;
432 Instruction *instr; //NULL if it's the block's live-in
433 };
434
435 struct Assignment {
436 Location defloc;
437 Location firstloc;
438 PhysReg reg;
439 };
440
441 bool ra_fail(FILE *output, Location loc, Location loc2, const char *fmt, ...) {
442 va_list args;
443 va_start(args, fmt);
444 char msg[1024];
445 vsprintf(msg, fmt, args);
446 va_end(args);
447
448 fprintf(stderr, "RA error found at instruction in BB%d:\n", loc.block->index);
449 if (loc.instr) {
450 aco_print_instr(loc.instr, stderr);
451 fprintf(stderr, "\n%s", msg);
452 } else {
453 fprintf(stderr, "%s", msg);
454 }
455 if (loc2.block) {
456 fprintf(stderr, " in BB%d:\n", loc2.block->index);
457 aco_print_instr(loc2.instr, stderr);
458 }
459 fprintf(stderr, "\n\n");
460
461 return true;
462 }
463
464 bool validate_subdword_operand(chip_class chip, const aco_ptr<Instruction>& instr, unsigned index)
465 {
466 Operand op = instr->operands[index];
467 unsigned byte = op.physReg().byte();
468
469 if (instr->format == Format::PSEUDO && chip >= GFX8)
470 return true;
471 if (instr->isSDWA() && (static_cast<SDWA_instruction *>(instr.get())->sel[index] & sdwa_asuint) == (sdwa_isra | op.bytes()))
472 return true;
473 if (byte == 2 && can_use_opsel(chip, instr->opcode, index, 1))
474 return true;
475
476 switch (instr->opcode) {
477 case aco_opcode::v_cvt_f32_ubyte1:
478 if (byte == 1)
479 return true;
480 break;
481 case aco_opcode::v_cvt_f32_ubyte2:
482 if (byte == 2)
483 return true;
484 break;
485 case aco_opcode::v_cvt_f32_ubyte3:
486 if (byte == 3)
487 return true;
488 break;
489 case aco_opcode::ds_write_b8_d16_hi:
490 case aco_opcode::ds_write_b16_d16_hi:
491 if (byte == 2 && index == 1)
492 return true;
493 break;
494 case aco_opcode::buffer_store_byte_d16_hi:
495 case aco_opcode::buffer_store_short_d16_hi:
496 if (byte == 2 && index == 3)
497 return true;
498 break;
499 case aco_opcode::flat_store_byte_d16_hi:
500 case aco_opcode::flat_store_short_d16_hi:
501 case aco_opcode::scratch_store_byte_d16_hi:
502 case aco_opcode::scratch_store_short_d16_hi:
503 case aco_opcode::global_store_byte_d16_hi:
504 case aco_opcode::global_store_short_d16_hi:
505 if (byte == 2 && index == 2)
506 return true;
507 default:
508 break;
509 }
510
511 return byte == 0;
512 }
513
514 bool validate_subdword_definition(chip_class chip, const aco_ptr<Instruction>& instr)
515 {
516 Definition def = instr->definitions[0];
517 unsigned byte = def.physReg().byte();
518
519 if (instr->format == Format::PSEUDO && chip >= GFX8)
520 return true;
521 if (instr->isSDWA() && static_cast<SDWA_instruction *>(instr.get())->dst_sel == (sdwa_isra | def.bytes()))
522 return true;
523 if (byte == 2 && can_use_opsel(chip, instr->opcode, -1, 1))
524 return true;
525
526 switch (instr->opcode) {
527 case aco_opcode::buffer_load_ubyte_d16_hi:
528 case aco_opcode::buffer_load_short_d16_hi:
529 case aco_opcode::flat_load_ubyte_d16_hi:
530 case aco_opcode::flat_load_short_d16_hi:
531 case aco_opcode::scratch_load_ubyte_d16_hi:
532 case aco_opcode::scratch_load_short_d16_hi:
533 case aco_opcode::global_load_ubyte_d16_hi:
534 case aco_opcode::global_load_short_d16_hi:
535 case aco_opcode::ds_read_u8_d16_hi:
536 case aco_opcode::ds_read_u16_d16_hi:
537 return byte == 2;
538 default:
539 break;
540 }
541
542 return byte == 0;
543 }
544
545 unsigned get_subdword_bytes_written(Program *program, const aco_ptr<Instruction>& instr, unsigned index)
546 {
547 chip_class chip = program->chip_class;
548 Definition def = instr->definitions[index];
549
550 if (instr->format == Format::PSEUDO)
551 return chip >= GFX8 ? def.bytes() : def.size() * 4u;
552 if (instr->isSDWA() && static_cast<SDWA_instruction *>(instr.get())->dst_sel == (sdwa_isra | def.bytes()))
553 return def.bytes();
554
555 switch (instr->opcode) {
556 case aco_opcode::buffer_load_ubyte_d16:
557 case aco_opcode::buffer_load_short_d16:
558 case aco_opcode::flat_load_ubyte_d16:
559 case aco_opcode::flat_load_short_d16:
560 case aco_opcode::scratch_load_ubyte_d16:
561 case aco_opcode::scratch_load_short_d16:
562 case aco_opcode::global_load_ubyte_d16:
563 case aco_opcode::global_load_short_d16:
564 case aco_opcode::ds_read_u8_d16:
565 case aco_opcode::ds_read_u16_d16:
566 case aco_opcode::buffer_load_ubyte_d16_hi:
567 case aco_opcode::buffer_load_short_d16_hi:
568 case aco_opcode::flat_load_ubyte_d16_hi:
569 case aco_opcode::flat_load_short_d16_hi:
570 case aco_opcode::scratch_load_ubyte_d16_hi:
571 case aco_opcode::scratch_load_short_d16_hi:
572 case aco_opcode::global_load_ubyte_d16_hi:
573 case aco_opcode::global_load_short_d16_hi:
574 case aco_opcode::ds_read_u8_d16_hi:
575 case aco_opcode::ds_read_u16_d16_hi:
576 return program->sram_ecc_enabled ? 4 : 2;
577 case aco_opcode::v_mad_f16:
578 case aco_opcode::v_mad_u16:
579 case aco_opcode::v_mad_i16:
580 case aco_opcode::v_fma_f16:
581 case aco_opcode::v_div_fixup_f16:
582 case aco_opcode::v_interp_p2_f16:
583 if (chip >= GFX9)
584 return 2;
585 default:
586 break;
587 }
588
589 return MAX2(chip >= GFX10 ? def.bytes() : 4, instr_info.definition_size[(int)instr->opcode] / 8u);
590 }
591
592 } /* end namespace */
593
594 bool validate_ra(Program *program, const struct radv_nir_compiler_options *options, FILE *output) {
595 if (!(debug_flags & DEBUG_VALIDATE_RA))
596 return false;
597
598 bool err = false;
599 aco::live live_vars = aco::live_var_analysis(program, options);
600 std::vector<std::vector<Temp>> phi_sgpr_ops(program->blocks.size());
601
602 std::map<unsigned, Assignment> assignments;
603 for (Block& block : program->blocks) {
604 Location loc;
605 loc.block = &block;
606 for (aco_ptr<Instruction>& instr : block.instructions) {
607 if (instr->opcode == aco_opcode::p_phi) {
608 for (unsigned i = 0; i < instr->operands.size(); i++) {
609 if (instr->operands[i].isTemp() &&
610 instr->operands[i].getTemp().type() == RegType::sgpr &&
611 instr->operands[i].isFirstKill())
612 phi_sgpr_ops[block.logical_preds[i]].emplace_back(instr->operands[i].getTemp());
613 }
614 }
615
616 loc.instr = instr.get();
617 for (unsigned i = 0; i < instr->operands.size(); i++) {
618 Operand& op = instr->operands[i];
619 if (!op.isTemp())
620 continue;
621 if (!op.isFixed())
622 err |= ra_fail(output, loc, Location(), "Operand %d is not assigned a register", i);
623 if (assignments.count(op.tempId()) && assignments[op.tempId()].reg != op.physReg())
624 err |= ra_fail(output, loc, assignments.at(op.tempId()).firstloc, "Operand %d has an inconsistent register assignment with instruction", i);
625 if ((op.getTemp().type() == RegType::vgpr && op.physReg().reg_b + op.bytes() > (256 + program->config->num_vgprs) * 4) ||
626 (op.getTemp().type() == RegType::sgpr && op.physReg() + op.size() > program->config->num_sgprs && op.physReg() < program->sgpr_limit))
627 err |= ra_fail(output, loc, assignments.at(op.tempId()).firstloc, "Operand %d has an out-of-bounds register assignment", i);
628 if (op.physReg() == vcc && !program->needs_vcc)
629 err |= ra_fail(output, loc, Location(), "Operand %d fixed to vcc but needs_vcc=false", i);
630 if (op.regClass().is_subdword() && !validate_subdword_operand(program->chip_class, instr, i))
631 err |= ra_fail(output, loc, Location(), "Operand %d not aligned correctly", i);
632 if (!assignments[op.tempId()].firstloc.block)
633 assignments[op.tempId()].firstloc = loc;
634 if (!assignments[op.tempId()].defloc.block)
635 assignments[op.tempId()].reg = op.physReg();
636 }
637
638 for (unsigned i = 0; i < instr->definitions.size(); i++) {
639 Definition& def = instr->definitions[i];
640 if (!def.isTemp())
641 continue;
642 if (!def.isFixed())
643 err |= ra_fail(output, loc, Location(), "Definition %d is not assigned a register", i);
644 if (assignments[def.tempId()].defloc.block)
645 err |= ra_fail(output, loc, assignments.at(def.tempId()).defloc, "Temporary %%%d also defined by instruction", def.tempId());
646 if ((def.getTemp().type() == RegType::vgpr && def.physReg().reg_b + def.bytes() > (256 + program->config->num_vgprs) * 4) ||
647 (def.getTemp().type() == RegType::sgpr && def.physReg() + def.size() > program->config->num_sgprs && def.physReg() < program->sgpr_limit))
648 err |= ra_fail(output, loc, assignments.at(def.tempId()).firstloc, "Definition %d has an out-of-bounds register assignment", i);
649 if (def.physReg() == vcc && !program->needs_vcc)
650 err |= ra_fail(output, loc, Location(), "Definition %d fixed to vcc but needs_vcc=false", i);
651 if (def.regClass().is_subdword() && !validate_subdword_definition(program->chip_class, instr))
652 err |= ra_fail(output, loc, Location(), "Definition %d not aligned correctly", i);
653 if (!assignments[def.tempId()].firstloc.block)
654 assignments[def.tempId()].firstloc = loc;
655 assignments[def.tempId()].defloc = loc;
656 assignments[def.tempId()].reg = def.physReg();
657 }
658 }
659 }
660
661 for (Block& block : program->blocks) {
662 Location loc;
663 loc.block = &block;
664
665 std::array<unsigned, 2048> regs; /* register file in bytes */
666 regs.fill(0);
667
668 std::set<Temp> live;
669 live.insert(live_vars.live_out[block.index].begin(), live_vars.live_out[block.index].end());
670 /* remove killed p_phi sgpr operands */
671 for (Temp tmp : phi_sgpr_ops[block.index])
672 live.erase(tmp);
673
674 /* check live out */
675 for (Temp tmp : live) {
676 PhysReg reg = assignments.at(tmp.id()).reg;
677 for (unsigned i = 0; i < tmp.bytes(); i++) {
678 if (regs[reg.reg_b + i]) {
679 err |= ra_fail(output, loc, Location(), "Assignment of element %d of %%%d already taken by %%%d in live-out", i, tmp.id(), regs[reg.reg_b + i]);
680 }
681 regs[reg.reg_b + i] = tmp.id();
682 }
683 }
684 regs.fill(0);
685
686 for (auto it = block.instructions.rbegin(); it != block.instructions.rend(); ++it) {
687 aco_ptr<Instruction>& instr = *it;
688
689 /* check killed p_phi sgpr operands */
690 if (instr->opcode == aco_opcode::p_logical_end) {
691 for (Temp tmp : phi_sgpr_ops[block.index]) {
692 PhysReg reg = assignments.at(tmp.id()).reg;
693 for (unsigned i = 0; i < tmp.bytes(); i++) {
694 if (regs[reg.reg_b + i])
695 err |= ra_fail(output, loc, Location(), "Assignment of element %d of %%%d already taken by %%%d in live-out", i, tmp.id(), regs[reg.reg_b + i]);
696 }
697 live.emplace(tmp);
698 }
699 }
700
701 for (const Definition& def : instr->definitions) {
702 if (!def.isTemp())
703 continue;
704 live.erase(def.getTemp());
705 }
706
707 /* don't count phi operands as live-in, since they are actually
708 * killed when they are copied at the predecessor */
709 if (instr->opcode != aco_opcode::p_phi && instr->opcode != aco_opcode::p_linear_phi) {
710 for (const Operand& op : instr->operands) {
711 if (!op.isTemp())
712 continue;
713 live.insert(op.getTemp());
714 }
715 }
716 }
717
718 for (Temp tmp : live) {
719 PhysReg reg = assignments.at(tmp.id()).reg;
720 for (unsigned i = 0; i < tmp.bytes(); i++)
721 regs[reg.reg_b + i] = tmp.id();
722 }
723
724 for (aco_ptr<Instruction>& instr : block.instructions) {
725 loc.instr = instr.get();
726
727 /* remove killed p_phi operands from regs */
728 if (instr->opcode == aco_opcode::p_logical_end) {
729 for (Temp tmp : phi_sgpr_ops[block.index]) {
730 PhysReg reg = assignments.at(tmp.id()).reg;
731 for (unsigned i = 0; i < tmp.bytes(); i++)
732 regs[reg.reg_b + i] = 0;
733 }
734 }
735
736 if (instr->opcode != aco_opcode::p_phi && instr->opcode != aco_opcode::p_linear_phi) {
737 for (const Operand& op : instr->operands) {
738 if (!op.isTemp())
739 continue;
740 if (op.isFirstKillBeforeDef()) {
741 for (unsigned j = 0; j < op.getTemp().bytes(); j++)
742 regs[op.physReg().reg_b + j] = 0;
743 }
744 }
745 }
746
747 for (unsigned i = 0; i < instr->definitions.size(); i++) {
748 Definition& def = instr->definitions[i];
749 if (!def.isTemp())
750 continue;
751 Temp tmp = def.getTemp();
752 PhysReg reg = assignments.at(tmp.id()).reg;
753 for (unsigned j = 0; j < tmp.bytes(); j++) {
754 if (regs[reg.reg_b + j])
755 err |= ra_fail(output, 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]);
756 regs[reg.reg_b + j] = tmp.id();
757 }
758 if (def.regClass().is_subdword() && def.bytes() < 4) {
759 unsigned written = get_subdword_bytes_written(program, instr, i);
760 /* If written=4, the instruction still might write the upper half. In that case, it's the lower half that isn't preserved */
761 for (unsigned j = reg.byte() & ~(written - 1); j < written; j++) {
762 unsigned written_reg = reg.reg() * 4u + j;
763 if (regs[written_reg] && regs[written_reg] != def.tempId())
764 err |= ra_fail(output, 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]);
765 }
766 }
767 }
768
769 for (const Definition& def : instr->definitions) {
770 if (!def.isTemp())
771 continue;
772 if (def.isKill()) {
773 for (unsigned j = 0; j < def.getTemp().bytes(); j++)
774 regs[def.physReg().reg_b + j] = 0;
775 }
776 }
777
778 if (instr->opcode != aco_opcode::p_phi && instr->opcode != aco_opcode::p_linear_phi) {
779 for (const Operand& op : instr->operands) {
780 if (!op.isTemp())
781 continue;
782 if (op.isLateKill() && op.isFirstKill()) {
783 for (unsigned j = 0; j < op.getTemp().bytes(); j++)
784 regs[op.physReg().reg_b + j] = 0;
785 }
786 }
787 }
788 }
789 }
790
791 return err;
792 }
793 }