1bf215546Sopenharmony_ci/* 2bf215546Sopenharmony_ci * Copyright (C) 2021 Collabora, Ltd. 3bf215546Sopenharmony_ci * 4bf215546Sopenharmony_ci * Permission is hereby granted, free of charge, to any person obtaining a 5bf215546Sopenharmony_ci * copy of this software and associated documentation files (the "Software"), 6bf215546Sopenharmony_ci * to deal in the Software without restriction, including without limitation 7bf215546Sopenharmony_ci * the rights to use, copy, modify, merge, publish, distribute, sublicense, 8bf215546Sopenharmony_ci * and/or sell copies of the Software, and to permit persons to whom the 9bf215546Sopenharmony_ci * Software is furnished to do so, subject to the following conditions: 10bf215546Sopenharmony_ci * 11bf215546Sopenharmony_ci * The above copyright notice and this permission notice (including the next 12bf215546Sopenharmony_ci * paragraph) shall be included in all copies or substantial portions of the 13bf215546Sopenharmony_ci * Software. 14bf215546Sopenharmony_ci * 15bf215546Sopenharmony_ci * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16bf215546Sopenharmony_ci * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17bf215546Sopenharmony_ci * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 18bf215546Sopenharmony_ci * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19bf215546Sopenharmony_ci * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20bf215546Sopenharmony_ci * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21bf215546Sopenharmony_ci * SOFTWARE. 22bf215546Sopenharmony_ci */ 23bf215546Sopenharmony_ci 24bf215546Sopenharmony_ci#include "compiler.h" 25bf215546Sopenharmony_ci#include "util/u_memory.h" 26bf215546Sopenharmony_ci 27bf215546Sopenharmony_ci/* Validatation doesn't make sense in release builds */ 28bf215546Sopenharmony_ci#ifndef NDEBUG 29bf215546Sopenharmony_ci 30bf215546Sopenharmony_ci/* Validate that all sources are initialized in all read components. This is 31bf215546Sopenharmony_ci * required for correct register allocation. We check a weaker condition, that 32bf215546Sopenharmony_ci * all sources that are read are written at some point (equivalently, the live 33bf215546Sopenharmony_ci * set is empty at the start of the program). TODO: Strengthen */ 34bf215546Sopenharmony_ci 35bf215546Sopenharmony_cibool 36bf215546Sopenharmony_cibi_validate_initialization(bi_context *ctx) 37bf215546Sopenharmony_ci{ 38bf215546Sopenharmony_ci bool success = true; 39bf215546Sopenharmony_ci 40bf215546Sopenharmony_ci /* Calculate the live set */ 41bf215546Sopenharmony_ci bi_block *entry = bi_entry_block(ctx); 42bf215546Sopenharmony_ci unsigned temp_count = bi_max_temp(ctx); 43bf215546Sopenharmony_ci bi_compute_liveness(ctx); 44bf215546Sopenharmony_ci 45bf215546Sopenharmony_ci /* Validate that the live set is indeed empty */ 46bf215546Sopenharmony_ci for (unsigned i = 0; i < temp_count; ++i) { 47bf215546Sopenharmony_ci if (entry->live_in[i] == 0) continue; 48bf215546Sopenharmony_ci 49bf215546Sopenharmony_ci fprintf(stderr, "%s%u\n", (i & PAN_IS_REG) ? "r" : "", i >> 1); 50bf215546Sopenharmony_ci success = false; 51bf215546Sopenharmony_ci } 52bf215546Sopenharmony_ci 53bf215546Sopenharmony_ci return success; 54bf215546Sopenharmony_ci} 55bf215546Sopenharmony_ci 56bf215546Sopenharmony_ci/* 57bf215546Sopenharmony_ci * Validate that there are no bi_registers accessed except at the beginning of 58bf215546Sopenharmony_ci * the start block, and that preloads are unique. This ensures RA can coalesce 59bf215546Sopenharmony_ci * preloads without interference tracking. 60bf215546Sopenharmony_ci */ 61bf215546Sopenharmony_cistatic bool 62bf215546Sopenharmony_cibi_validate_preload(bi_context *ctx) 63bf215546Sopenharmony_ci{ 64bf215546Sopenharmony_ci bool start = true; 65bf215546Sopenharmony_ci uint64_t preloaded = 0; 66bf215546Sopenharmony_ci 67bf215546Sopenharmony_ci bi_foreach_block(ctx, block) { 68bf215546Sopenharmony_ci bi_foreach_instr_in_block(block, I) { 69bf215546Sopenharmony_ci /* No instruction should have a register destination */ 70bf215546Sopenharmony_ci bi_foreach_dest(I, d) { 71bf215546Sopenharmony_ci if (I->dest[d].type == BI_INDEX_REGISTER) 72bf215546Sopenharmony_ci return false; 73bf215546Sopenharmony_ci } 74bf215546Sopenharmony_ci 75bf215546Sopenharmony_ci /* Preloads are register moves at the start */ 76bf215546Sopenharmony_ci bool is_preload = 77bf215546Sopenharmony_ci start && I->op == BI_OPCODE_MOV_I32 && 78bf215546Sopenharmony_ci I->src[0].type == BI_INDEX_REGISTER; 79bf215546Sopenharmony_ci 80bf215546Sopenharmony_ci /* After the first nonpreload, we're done preloading */ 81bf215546Sopenharmony_ci start &= is_preload; 82bf215546Sopenharmony_ci 83bf215546Sopenharmony_ci /* Only preloads may have a register source */ 84bf215546Sopenharmony_ci bi_foreach_src(I, s) { 85bf215546Sopenharmony_ci if (I->src[s].type == BI_INDEX_REGISTER && !is_preload) 86bf215546Sopenharmony_ci return false; 87bf215546Sopenharmony_ci } 88bf215546Sopenharmony_ci 89bf215546Sopenharmony_ci /* Check uniqueness */ 90bf215546Sopenharmony_ci if (is_preload) { 91bf215546Sopenharmony_ci unsigned r = I->src[0].value; 92bf215546Sopenharmony_ci 93bf215546Sopenharmony_ci if (preloaded & BITFIELD64_BIT(r)) 94bf215546Sopenharmony_ci return false; 95bf215546Sopenharmony_ci 96bf215546Sopenharmony_ci preloaded |= BITFIELD64_BIT(r); 97bf215546Sopenharmony_ci } 98bf215546Sopenharmony_ci } 99bf215546Sopenharmony_ci 100bf215546Sopenharmony_ci /* Only the first block may preload */ 101bf215546Sopenharmony_ci start = false; 102bf215546Sopenharmony_ci } 103bf215546Sopenharmony_ci 104bf215546Sopenharmony_ci return true; 105bf215546Sopenharmony_ci} 106bf215546Sopenharmony_ci 107bf215546Sopenharmony_ci/* 108bf215546Sopenharmony_ci * Type check the dimensionality of sources and destinations. This occurs in two 109bf215546Sopenharmony_ci * passes, first to gather all destination sizes, second to validate all source 110bf215546Sopenharmony_ci * sizes. Depends on SSA form. 111bf215546Sopenharmony_ci */ 112bf215546Sopenharmony_cistatic bool 113bf215546Sopenharmony_cibi_validate_width(bi_context *ctx) 114bf215546Sopenharmony_ci{ 115bf215546Sopenharmony_ci bool succ = true; 116bf215546Sopenharmony_ci uint8_t *width = calloc(ctx->ssa_alloc, sizeof(uint8_t)); 117bf215546Sopenharmony_ci 118bf215546Sopenharmony_ci bi_foreach_instr_global(ctx, I) { 119bf215546Sopenharmony_ci bi_foreach_dest(I, d) { 120bf215546Sopenharmony_ci if (bi_is_null(I->dest[d])) continue; 121bf215546Sopenharmony_ci if (!bi_is_ssa(I->dest[d])) continue; 122bf215546Sopenharmony_ci 123bf215546Sopenharmony_ci unsigned v = I->dest[d].value; 124bf215546Sopenharmony_ci assert(width[v] == 0 && "broken SSA"); 125bf215546Sopenharmony_ci 126bf215546Sopenharmony_ci width[v] = bi_count_write_registers(I, d); 127bf215546Sopenharmony_ci } 128bf215546Sopenharmony_ci } 129bf215546Sopenharmony_ci 130bf215546Sopenharmony_ci bi_foreach_instr_global(ctx, I) { 131bf215546Sopenharmony_ci bi_foreach_src(I, s) { 132bf215546Sopenharmony_ci if (!bi_is_ssa(I->src[s])) continue; 133bf215546Sopenharmony_ci 134bf215546Sopenharmony_ci unsigned v = I->src[s].value; 135bf215546Sopenharmony_ci unsigned n = bi_count_read_registers(I, s); 136bf215546Sopenharmony_ci 137bf215546Sopenharmony_ci if (width[v] != n) { 138bf215546Sopenharmony_ci succ = false; 139bf215546Sopenharmony_ci fprintf(stderr, 140bf215546Sopenharmony_ci "source %u, expected width %u, got width %u\n", 141bf215546Sopenharmony_ci s, n, width[v]); 142bf215546Sopenharmony_ci bi_print_instr(I, stderr); 143bf215546Sopenharmony_ci fprintf(stderr, "\n"); 144bf215546Sopenharmony_ci } 145bf215546Sopenharmony_ci } 146bf215546Sopenharmony_ci } 147bf215546Sopenharmony_ci 148bf215546Sopenharmony_ci free(width); 149bf215546Sopenharmony_ci return succ; 150bf215546Sopenharmony_ci} 151bf215546Sopenharmony_ci 152bf215546Sopenharmony_civoid 153bf215546Sopenharmony_cibi_validate(bi_context *ctx, const char *after) 154bf215546Sopenharmony_ci{ 155bf215546Sopenharmony_ci bool fail = false; 156bf215546Sopenharmony_ci 157bf215546Sopenharmony_ci if (bifrost_debug & BIFROST_DBG_NOVALIDATE) 158bf215546Sopenharmony_ci return; 159bf215546Sopenharmony_ci 160bf215546Sopenharmony_ci if (!bi_validate_initialization(ctx)) { 161bf215546Sopenharmony_ci fprintf(stderr, "Uninitialized data read after %s\n", after); 162bf215546Sopenharmony_ci fail = true; 163bf215546Sopenharmony_ci } 164bf215546Sopenharmony_ci 165bf215546Sopenharmony_ci if (!bi_validate_preload(ctx)) { 166bf215546Sopenharmony_ci fprintf(stderr, "Unexpected preload after %s\n", after); 167bf215546Sopenharmony_ci fail = true; 168bf215546Sopenharmony_ci } 169bf215546Sopenharmony_ci 170bf215546Sopenharmony_ci if (!bi_validate_width(ctx)) { 171bf215546Sopenharmony_ci fprintf(stderr, "Unexpected vector with after %s\n", after); 172bf215546Sopenharmony_ci fail = true; 173bf215546Sopenharmony_ci } 174bf215546Sopenharmony_ci 175bf215546Sopenharmony_ci if (fail) { 176bf215546Sopenharmony_ci bi_print_shader(ctx, stderr); 177bf215546Sopenharmony_ci exit(1); 178bf215546Sopenharmony_ci } 179bf215546Sopenharmony_ci} 180bf215546Sopenharmony_ci 181bf215546Sopenharmony_ci#endif /* NDEBUG */ 182