From 0576cad958b459701a5dde73ac2f27afb91fe391 Mon Sep 17 00:00:00 2001 From: Alyssa Rosenzweig Date: Thu, 12 May 2022 20:21:42 -0400 Subject: [PATCH] pan/bi: Validate vector widths Now that our IR is much more strongly typed, and RA code quality depends on correct typing, add a validation pass to make sure we didn't screw it up. This pass found a massive number of bugs in early versions of this series. Signed-off-by: Alyssa Rosenzweig Part-of: --- src/panfrost/bifrost/bi_validate.c | 50 +++++++++++++++++++++++++++++- 1 file changed, 49 insertions(+), 1 deletion(-) diff --git a/src/panfrost/bifrost/bi_validate.c b/src/panfrost/bifrost/bi_validate.c index 3b23df791f5..778689799fe 100644 --- a/src/panfrost/bifrost/bi_validate.c +++ b/src/panfrost/bifrost/bi_validate.c @@ -104,6 +104,51 @@ bi_validate_preload(bi_context *ctx) return true; } +/* + * Type check the dimensionality of sources and destinations. This occurs in two + * passes, first to gather all destination sizes, second to validate all source + * sizes. Depends on SSA form. + */ +static bool +bi_validate_width(bi_context *ctx) +{ + bool succ = true; + uint8_t *width = calloc(ctx->ssa_alloc, sizeof(uint8_t)); + + bi_foreach_instr_global(ctx, I) { + bi_foreach_dest(I, d) { + if (bi_is_null(I->dest[d])) continue; + if (!bi_is_ssa(I->dest[d])) continue; + + unsigned v = I->dest[d].value; + assert(width[v] == 0 && "broken SSA"); + + width[v] = bi_count_write_registers(I, d); + } + } + + bi_foreach_instr_global(ctx, I) { + bi_foreach_src(I, s) { + if (!bi_is_ssa(I->src[s])) continue; + + unsigned v = I->src[s].value; + unsigned n = bi_count_read_registers(I, s); + + if (width[v] != n) { + succ = false; + fprintf(stderr, + "source %u, expected width %u, got width %u\n", + s, n, width[v]); + bi_print_instr(I, stderr); + fprintf(stderr, "\n"); + } + } + } + + free(width); + return succ; +} + void bi_validate(bi_context *ctx, const char *after) { @@ -122,7 +167,10 @@ bi_validate(bi_context *ctx, const char *after) fail = true; } - /* TODO: Validate more invariants */ + if (!bi_validate_width(ctx)) { + fprintf(stderr, "Unexpected vector with after %s\n", after); + fail = true; + } if (fail) { bi_print_shader(ctx, stderr);