| 1 |
/* |
| 2 |
* Copyright © 2024 Imagination Technologies Ltd. |
| 3 |
* |
| 4 |
* SPDX-License-Identifier: MIT |
| 5 |
*/ |
| 6 |
|
| 7 |
/** |
| 8 |
* \file pco_validate.c |
| 9 |
* |
| 10 |
* \brief PCO validation functions. |
| 11 |
*/ |
| 12 |
|
| 13 |
#include "pco.h" |
| 14 |
#include "pco_internal.h" |
| 15 |
#include "util/bitset.h" |
| 16 |
#include "util/macros.h" |
| 17 |
#include "util/ralloc.h" |
| 18 |
|
| 19 |
#include <inttypes.h> |
| 20 |
#include <stdio.h> |
| 21 |
|
| 22 |
#ifndef NDEBUG |
| 23 |
|
| 24 |
enum ref_cursor { |
| 25 |
REF_CURSOR_NONE, |
| 26 |
REF_CURSOR_INSTR_DEST, |
| 27 |
REF_CURSOR_INSTR_SRC, |
| 28 |
REF_CURSOR_IGRP_SRC, |
| 29 |
REF_CURSOR_IGRP_ISS, |
| 30 |
REF_CURSOR_IGRP_DEST, |
| 31 |
}; |
| 32 |
|
| 33 |
/** Validation state. */ |
| 34 |
struct val_state { |
| 35 |
const char *when; /** Description of the validation being done. */ |
| 36 |
pco_shader *shader; /** The shader being validated. */ |
| 37 |
pco_func *func; /** Current function being validated. */ |
| 38 |
pco_cf_node *cf_node; /** Current cf node being validated. */ |
| 39 |
pco_igrp *igrp; /** Current instruction group being validated. */ |
| 40 |
enum pco_op_phase phase; /** Phase of the instruction being validated. */ |
| 41 |
pco_instr *instr; /** Current instruction being validated. */ |
| 42 |
pco_ref *ref; /** Current reference being validated. */ |
| 43 |
enum ref_cursor ref_cursor; /** Current reference cursor. */ |
| 44 |
}; |
| 45 |
|
| 46 |
/** |
| 47 |
* \brief Asserts a condition, printing an error and aborting on failure. |
| 48 |
* |
| 49 |
* \param[in] state Validation state. |
| 50 |
* \param[in] cond Assertion condition. |
| 51 |
* \param[in] cond_str Assertion condition string. |
| 52 |
* \param[in] fmt Format string. |
| 53 |
*/ |
| 54 |
static void pco_assert(struct val_state *state, |
| 55 |
bool cond, |
| 56 |
const char *cond_str, |
| 57 |
const char *fmt, |
| 58 |
...) |
| 59 |
{ |
| 60 |
if (cond) |
| 61 |
return; |
| 62 |
|
| 63 |
printf("PCO validation failed "); |
| 64 |
|
| 65 |
if (state->when) |
| 66 |
printf("%s ", state->when); |
| 67 |
|
| 68 |
printf("with assertion \"%s\" - ", cond_str); |
| 69 |
|
| 70 |
va_list args; |
| 71 |
va_start(args, fmt); |
| 72 |
vprintf(fmt, args); |
| 73 |
va_end(args); |
| 74 |
|
| 75 |
printf(" - while validating"); |
| 76 |
|
| 77 |
if (state->ref_cursor != REF_CURSOR_NONE) { |
| 78 |
switch (state->ref_cursor) { |
| 79 |
case REF_CURSOR_INSTR_DEST: |
| 80 |
printf(" instr dest #%" PRIuPTR, state->ref - state->instr->dest); |
| 81 |
break; |
| 82 |
|
| 83 |
case REF_CURSOR_INSTR_SRC: |
| 84 |
printf(" instr src #%" PRIuPTR, state->ref - state->instr->src); |
| 85 |
break; |
| 86 |
|
| 87 |
case REF_CURSOR_IGRP_DEST: |
| 88 |
printf(" igrp dest #%" PRIuPTR, state->ref - state->igrp->srcs.s); |
| 89 |
break; |
| 90 |
|
| 91 |
case REF_CURSOR_IGRP_ISS: |
| 92 |
printf(" igrp iss #%" PRIuPTR, state->ref - state->igrp->iss.is); |
| 93 |
break; |
| 94 |
|
| 95 |
case REF_CURSOR_IGRP_SRC: |
| 96 |
printf(" igrp src #%" PRIuPTR, state->ref - state->igrp->srcs.s); |
| 97 |
break; |
| 98 |
|
| 99 |
default: |
| 100 |
UNREACHABLE(""); |
| 101 |
} |
| 102 |
|
| 103 |
printf(" ("); |
| 104 |
pco_print_ref(state->shader, *state->ref); |
| 105 |
printf(")"); |
| 106 |
} |
| 107 |
|
| 108 |
if (state->cf_node) { |
| 109 |
printf(" "); |
| 110 |
pco_print_cf_node_name(state->shader, state->cf_node); |
| 111 |
} |
| 112 |
|
| 113 |
if (state->igrp) { |
| 114 |
printf(" igrp "); |
| 115 |
pco_print_igrp(state->shader, state->igrp); |
| 116 |
} |
| 117 |
|
| 118 |
if (state->instr) { |
| 119 |
printf(" instr "); |
| 120 |
if (state->shader->is_grouped) { |
| 121 |
printf("(phase "); |
| 122 |
pco_print_phase(state->shader, |
| 123 |
state->instr->parent_igrp->hdr.alutype, |
| 124 |
state->instr->phase); |
| 125 |
printf(") "); |
| 126 |
} |
| 127 |
|
| 128 |
pco_print_instr(state->shader, state->instr); |
| 129 |
} |
| 130 |
|
| 131 |
if (state->func) { |
| 132 |
printf(" "); |
| 133 |
pco_print_cf_node_name(state->shader, &state->func->cf_node); |
| 134 |
} |
| 135 |
|
| 136 |
printf(".\n"); |
| 137 |
|
| 138 |
pco_print_shader(state->shader, stdout, state->when); |
| 139 |
|
| 140 |
abort(); |
| 141 |
} |
| 142 |
|
| 143 |
# define PCO_ASSERT(state, cond, fmt, ...) \ |
| 144 |
pco_assert(state, cond, #cond, fmt, ##__VA_ARGS__) |
| 145 |
|
| 146 |
/** |
| 147 |
* \brief Validates SSA assignments and uses. |
| 148 |
* |
| 149 |
* \param[in,out] state Validation state. |
| 150 |
*/ |
| 151 |
static void pco_validate_ssa(struct val_state *state) |
| 152 |
{ |
| 153 |
BITSET_WORD *ssa_writes; |
| 154 |
pco_foreach_func_in_shader (func, state->shader) { |
| 155 |
state->func = func; |
| 156 |
|
| 157 |
ssa_writes = rzalloc_array_size(NULL, |
| 158 |
sizeof(*ssa_writes), |
| 159 |
BITSET_WORDS(func->next_ssa)); |
| 160 |
|
| 161 |
/* Ensure sources have been defined before they're used. */ |
| 162 |
pco_foreach_instr_in_func (instr, func) { |
| 163 |
state->cf_node = &instr->parent_block->cf_node; |
| 164 |
state->instr = instr; |
| 165 |
state->ref_cursor = REF_CURSOR_INSTR_SRC; |
| 166 |
pco_foreach_instr_src_ssa (psrc, instr) { |
| 167 |
state->ref = psrc; |
| 168 |
PCO_ASSERT(state, |
| 169 |
BITSET_TEST(ssa_writes, psrc->val), |
| 170 |
"SSA source used before being defined"); |
| 171 |
} |
| 172 |
|
| 173 |
/* Ensure destinations are only defined once. */ |
| 174 |
state->ref_cursor = REF_CURSOR_INSTR_DEST; |
| 175 |
pco_foreach_instr_dest_ssa (pdest, instr) { |
| 176 |
state->ref = pdest; |
| 177 |
PCO_ASSERT(state, |
| 178 |
!BITSET_TEST(ssa_writes, pdest->val), |
| 179 |
"SSA destination defined to more than once"); |
| 180 |
BITSET_SET(ssa_writes, pdest->val); |
| 181 |
} |
| 182 |
|
| 183 |
state->instr = NULL; |
| 184 |
state->cf_node = NULL; |
| 185 |
} |
| 186 |
|
| 187 |
ralloc_free(ssa_writes); |
| 188 |
|
| 189 |
state->func = NULL; |
| 190 |
state->ref = NULL; |
| 191 |
state->ref_cursor = REF_CURSOR_NONE; |
| 192 |
} |
| 193 |
} |
| 194 |
|
| 195 |
/** |
| 196 |
* \brief Validates hardware source mappings. |
| 197 |
* |
| 198 |
* \param[in,out] state Validation state. |
| 199 |
*/ |
| 200 |
static void pco_validate_src_maps(struct val_state *state) |
| 201 |
{ |
| 202 |
/* Only check after the legalize pass has run. */ |
| 203 |
if (!state->shader->is_legalized) |
| 204 |
return; |
| 205 |
|
| 206 |
bool needs_s124; |
| 207 |
pco_foreach_func_in_shader (func, state->shader) { |
| 208 |
state->func = func; |
| 209 |
|
| 210 |
pco_foreach_instr_in_func (instr, func) { |
| 211 |
const struct pco_op_info *info = &pco_op_info[instr->op]; |
| 212 |
if (info->type == PCO_OP_TYPE_PSEUDO) |
| 213 |
continue; |
| 214 |
|
| 215 |
state->cf_node = &instr->parent_block->cf_node; |
| 216 |
state->instr = instr; |
| 217 |
|
| 218 |
state->ref_cursor = REF_CURSOR_INSTR_DEST; |
| 219 |
pco_foreach_instr_dest (pdest, instr) { |
| 220 |
state->ref = pdest; |
| 221 |
unsigned dest_index = pdest - instr->dest; |
| 222 |
|
| 223 |
if (!info->dest_intrn_map[dest_index]) |
| 224 |
continue; |
| 225 |
|
| 226 |
enum pco_io mapped_src = |
| 227 |
PCO_IO_S0 + info->dest_intrn_map[dest_index] - 1; |
| 228 |
|
| 229 |
bool valid = ref_src_map_valid(*pdest, mapped_src, &needs_s124); |
| 230 |
PCO_ASSERT(state, |
| 231 |
valid, |
| 232 |
"HW register reference should be mapped to %s", |
| 233 |
needs_s124 ? "S1/S2/S4" : "S0/S2/S3"); |
| 234 |
} |
| 235 |
|
| 236 |
state->ref_cursor = REF_CURSOR_INSTR_SRC; |
| 237 |
pco_foreach_instr_src (psrc, instr) { |
| 238 |
state->ref = psrc; |
| 239 |
unsigned src_index = psrc - instr->src; |
| 240 |
|
| 241 |
if (!info->src_intrn_map[src_index]) |
| 242 |
continue; |
| 243 |
|
| 244 |
enum pco_io mapped_src = |
| 245 |
PCO_IO_S0 + info->src_intrn_map[src_index] - 1; |
| 246 |
|
| 247 |
bool valid = ref_src_map_valid(*psrc, mapped_src, &needs_s124); |
| 248 |
PCO_ASSERT(state, |
| 249 |
valid, |
| 250 |
"HW register reference should be mapped to %s", |
| 251 |
needs_s124 ? "S1/S2/S4" : "S0/S2/S3"); |
| 252 |
} |
| 253 |
|
| 254 |
state->instr = NULL; |
| 255 |
state->cf_node = NULL; |
| 256 |
} |
| 257 |
|
| 258 |
state->func = NULL; |
| 259 |
state->ref = NULL; |
| 260 |
state->ref_cursor = REF_CURSOR_NONE; |
| 261 |
} |
| 262 |
} |
| 263 |
|
| 264 |
# define CHECK_IO(io, ref) \ |
| 265 |
case PCO_REF_MAP_##io: \ |
| 266 |
if (pco_ref_is_io(ref) && pco_ref_get_io(ref) == PCO_IO_##io) \ |
| 267 |
return true; \ |
| 268 |
break; |
| 269 |
|
| 270 |
/** |
| 271 |
* \brief Checks whether a ref type corresponds to a supported mapping. |
| 272 |
* |
| 273 |
* \param[in] ref Src/dest reference. |
| 274 |
* \param[in] ref_maps Supported mappings. |
| 275 |
* \return True if the ref type corresponds to a supported mapping. |
| 276 |
*/ |
| 277 |
static inline bool ref_is_in_map(pco_ref ref, enum pco_ref_map ref_maps) |
| 278 |
{ |
| 279 |
u_foreach_bit (_ref_map, ref_maps) { |
| 280 |
enum pco_ref_map ref_map = _ref_map; |
| 281 |
switch (ref_map) { |
| 282 |
case PCO_REF_MAP__: |
| 283 |
if (pco_ref_is_null(ref)) |
| 284 |
return true; |
| 285 |
break; |
| 286 |
|
| 287 |
CHECK_IO(S0, ref) |
| 288 |
CHECK_IO(S1, ref) |
| 289 |
CHECK_IO(S2, ref) |
| 290 |
CHECK_IO(S3, ref) |
| 291 |
CHECK_IO(S4, ref) |
| 292 |
CHECK_IO(S5, ref) |
| 293 |
|
| 294 |
CHECK_IO(W0, ref) |
| 295 |
CHECK_IO(W1, ref) |
| 296 |
|
| 297 |
CHECK_IO(IS0, ref) |
| 298 |
CHECK_IO(IS1, ref) |
| 299 |
CHECK_IO(IS2, ref) |
| 300 |
CHECK_IO(IS3, ref) |
| 301 |
CHECK_IO(IS4, ref) |
| 302 |
CHECK_IO(IS5, ref) |
| 303 |
|
| 304 |
CHECK_IO(FT0, ref) |
| 305 |
CHECK_IO(FT1, ref) |
| 306 |
CHECK_IO(FT1_INVERT, ref) |
| 307 |
CHECK_IO(FT2, ref) |
| 308 |
CHECK_IO(FTE, ref) |
| 309 |
CHECK_IO(FT3, ref) |
| 310 |
CHECK_IO(FT4, ref) |
| 311 |
CHECK_IO(FT5, ref) |
| 312 |
|
| 313 |
CHECK_IO(FTT, ref) |
| 314 |
|
| 315 |
case PCO_REF_MAP_P0: |
| 316 |
if (pco_ref_is_pred(ref) && ref.val == PCO_PRED_P0) |
| 317 |
return true; |
| 318 |
break; |
| 319 |
|
| 320 |
case PCO_REF_MAP_PE: |
| 321 |
if (pco_ref_is_pred(ref) && ref.val == PCO_PRED_PE) |
| 322 |
return true; |
| 323 |
break; |
| 324 |
|
| 325 |
case PCO_REF_MAP_IMM: |
| 326 |
if (pco_ref_is_imm(ref)) |
| 327 |
return true; |
| 328 |
break; |
| 329 |
|
| 330 |
case PCO_REF_MAP_DRC: |
| 331 |
if (pco_ref_is_drc(ref)) |
| 332 |
return true; |
| 333 |
break; |
| 334 |
|
| 335 |
case PCO_REF_MAP_TEMP: |
| 336 |
if ((pco_ref_is_reg(ref) || pco_ref_is_idx_reg(ref)) && |
| 337 |
pco_ref_get_reg_class(ref) == PCO_REG_CLASS_TEMP) |
| 338 |
return true; |
| 339 |
break; |
| 340 |
|
| 341 |
case PCO_REF_MAP_COEFF: |
| 342 |
if ((pco_ref_is_reg(ref) || pco_ref_is_idx_reg(ref)) && |
| 343 |
pco_ref_get_reg_class(ref) == PCO_REG_CLASS_COEFF) |
| 344 |
return true; |
| 345 |
break; |
| 346 |
|
| 347 |
default: |
| 348 |
UNREACHABLE(""); |
| 349 |
} |
| 350 |
} |
| 351 |
|
| 352 |
return false; |
| 353 |
} |
| 354 |
|
| 355 |
# undef CHECK_IO |
| 356 |
|
| 357 |
/** |
| 358 |
* \brief Validates I/O references for igrps. |
| 359 |
* |
| 360 |
* \param[in,out] state Validation state. |
| 361 |
*/ |
| 362 |
static void pco_validate_ref_maps(struct val_state *state) |
| 363 |
{ |
| 364 |
bool needs_s124; |
| 365 |
pco_foreach_func_in_shader (func, state->shader) { |
| 366 |
state->func = func; |
| 367 |
|
| 368 |
pco_foreach_igrp_in_func (igrp, func) { |
| 369 |
state->cf_node = &igrp->parent_block->cf_node; |
| 370 |
state->igrp = igrp; |
| 371 |
|
| 372 |
/* Igrp source mappings. */ |
| 373 |
state->ref_cursor = REF_CURSOR_IGRP_SRC; |
| 374 |
for (unsigned s = 0; s < ARRAY_SIZE(igrp->srcs.s); ++s) { |
| 375 |
state->ref = &igrp->srcs.s[s]; |
| 376 |
pco_ref src = igrp->srcs.s[s]; |
| 377 |
|
| 378 |
if (pco_ref_is_null(src)) |
| 379 |
continue; |
| 380 |
|
| 381 |
bool valid = ref_src_map_valid(src, PCO_IO_S0 + s, &needs_s124); |
| 382 |
PCO_ASSERT(state, |
| 383 |
valid, |
| 384 |
"HW register reference should be mapped to %s", |
| 385 |
needs_s124 ? "S1/S2/S4" : "S0/S2/S3"); |
| 386 |
} |
| 387 |
|
| 388 |
pco_foreach_instr_in_igrp (instr, igrp) { |
| 389 |
const struct pco_op_info *info = &pco_op_info[instr->op]; |
| 390 |
enum pco_op_phase phase = instr->phase; |
| 391 |
state->instr = instr; |
| 392 |
|
| 393 |
/* Instruction dests. */ |
| 394 |
state->ref_cursor = REF_CURSOR_INSTR_DEST; |
| 395 |
for (unsigned d = 0; d < instr->num_dests; ++d) { |
| 396 |
state->ref = &instr->dest[d]; |
| 397 |
PCO_ASSERT(state, |
| 398 |
ref_is_in_map(instr->dest[d], |
| 399 |
info->grp_dest_maps[phase][d]), |
| 400 |
"Invalid dest assignment."); |
| 401 |
} |
| 402 |
|
| 403 |
/* Instruction sources. */ |
| 404 |
state->ref_cursor = REF_CURSOR_INSTR_SRC; |
| 405 |
for (unsigned s = 0; s < instr->num_srcs; ++s) { |
| 406 |
state->ref = &instr->src[s]; |
| 407 |
PCO_ASSERT(state, |
| 408 |
ref_is_in_map(instr->src[s], |
| 409 |
info->grp_src_maps[phase][s]), |
| 410 |
"Invalid src assignment."); |
| 411 |
} |
| 412 |
|
| 413 |
state->instr = NULL; |
| 414 |
} |
| 415 |
|
| 416 |
state->igrp = NULL; |
| 417 |
state->cf_node = NULL; |
| 418 |
} |
| 419 |
|
| 420 |
state->func = NULL; |
| 421 |
state->ref = NULL; |
| 422 |
state->ref_cursor = REF_CURSOR_NONE; |
| 423 |
} |
| 424 |
} |
| 425 |
|
| 426 |
/** |
| 427 |
* \brief Validates a PCO shader. |
| 428 |
* |
| 429 |
* \param[in] shader PCO shader. |
| 430 |
* \param[in] when When the validation check is being run. |
| 431 |
*/ |
| 432 |
void pco_validate_shader(pco_shader *shader, const char *when) |
| 433 |
{ |
| 434 |
if (PCO_DEBUG(VAL_SKIP)) |
| 435 |
return; |
| 436 |
|
| 437 |
struct val_state state = { |
| 438 |
.when = when, |
| 439 |
.shader = shader, |
| 440 |
.phase = -1, |
| 441 |
}; |
| 442 |
|
| 443 |
if (!shader->is_grouped) { |
| 444 |
pco_validate_ssa(&state); |
| 445 |
pco_validate_src_maps(&state); |
| 446 |
} else { |
| 447 |
PCO_ASSERT(&state, |
| 448 |
shader->is_legalized, |
| 449 |
"Legalize pass should have been run before grouping"); |
| 450 |
pco_validate_ref_maps(&state); |
| 451 |
} |
| 452 |
} |
| 453 |
#else /* NDEBUG */ |
| 454 |
void pco_validate_shader(UNUSED pco_shader *shader, UNUSED const char *when) {} |
| 455 |
#endif /* NDEBUG */ |