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 "va_compiler.h" 25bf215546Sopenharmony_ci#include "valhall.h" 26bf215546Sopenharmony_ci#include "bi_builder.h" 27bf215546Sopenharmony_ci 28bf215546Sopenharmony_ci/* Valhall has limits on access to fast-access uniforms: 29bf215546Sopenharmony_ci * 30bf215546Sopenharmony_ci * An instruction may access no more than a single 64-bit uniform slot. 31bf215546Sopenharmony_ci * An instruction may access no more than 64-bits of combined uniforms and constants. 32bf215546Sopenharmony_ci * An instruction may access no more than a single special immediate (e.g. lane_id). 33bf215546Sopenharmony_ci * 34bf215546Sopenharmony_ci * We validate these constraints. 35bf215546Sopenharmony_ci * 36bf215546Sopenharmony_ci * An instruction may only access a single page of (special or uniform) FAU. 37bf215546Sopenharmony_ci * This constraint does not need explicit validation: since FAU slots are 38bf215546Sopenharmony_ci * naturally aligned, they never cross page boundaries, so this condition is 39bf215546Sopenharmony_ci * implied by only acesssing a single 64-bit slot. 40bf215546Sopenharmony_ci */ 41bf215546Sopenharmony_ci 42bf215546Sopenharmony_cistruct fau_state { 43bf215546Sopenharmony_ci signed uniform_slot; 44bf215546Sopenharmony_ci bi_index buffer[2]; 45bf215546Sopenharmony_ci}; 46bf215546Sopenharmony_ci 47bf215546Sopenharmony_cistatic bool 48bf215546Sopenharmony_cifau_state_buffer(struct fau_state *fau, bi_index idx) 49bf215546Sopenharmony_ci{ 50bf215546Sopenharmony_ci for (unsigned i = 0; i < ARRAY_SIZE(fau->buffer); ++i) { 51bf215546Sopenharmony_ci if (bi_is_word_equiv(fau->buffer[i], idx)) 52bf215546Sopenharmony_ci return true; 53bf215546Sopenharmony_ci else if (bi_is_null(fau->buffer[i])) { 54bf215546Sopenharmony_ci fau->buffer[i] = idx; 55bf215546Sopenharmony_ci return true; 56bf215546Sopenharmony_ci } 57bf215546Sopenharmony_ci } 58bf215546Sopenharmony_ci 59bf215546Sopenharmony_ci return false; 60bf215546Sopenharmony_ci} 61bf215546Sopenharmony_ci 62bf215546Sopenharmony_cistatic bool 63bf215546Sopenharmony_cifau_state_uniform(struct fau_state *fau, bi_index idx) 64bf215546Sopenharmony_ci{ 65bf215546Sopenharmony_ci /* Each slot is 64-bits. The low/high half is encoded as the offset of the 66bf215546Sopenharmony_ci * bi_index, which we want to ignore. 67bf215546Sopenharmony_ci */ 68bf215546Sopenharmony_ci unsigned slot = (idx.value & 63); 69bf215546Sopenharmony_ci 70bf215546Sopenharmony_ci if (fau->uniform_slot < 0) 71bf215546Sopenharmony_ci fau->uniform_slot = slot; 72bf215546Sopenharmony_ci 73bf215546Sopenharmony_ci return fau->uniform_slot == slot; 74bf215546Sopenharmony_ci} 75bf215546Sopenharmony_ci 76bf215546Sopenharmony_cistatic bool 77bf215546Sopenharmony_cifau_is_special(enum bir_fau fau) 78bf215546Sopenharmony_ci{ 79bf215546Sopenharmony_ci return !(fau & (BIR_FAU_UNIFORM | BIR_FAU_IMMEDIATE)); 80bf215546Sopenharmony_ci} 81bf215546Sopenharmony_ci 82bf215546Sopenharmony_cistatic bool 83bf215546Sopenharmony_cifau_state_special(struct fau_state *fau, bi_index idx) 84bf215546Sopenharmony_ci{ 85bf215546Sopenharmony_ci for (unsigned i = 0; i < ARRAY_SIZE(fau->buffer); ++i) { 86bf215546Sopenharmony_ci bi_index buf = fau->buffer[i]; 87bf215546Sopenharmony_ci bool special = !bi_is_null(buf) && fau_is_special(buf.value); 88bf215546Sopenharmony_ci 89bf215546Sopenharmony_ci if (special && !bi_is_equiv(buf, idx)) 90bf215546Sopenharmony_ci return false; 91bf215546Sopenharmony_ci } 92bf215546Sopenharmony_ci 93bf215546Sopenharmony_ci return true; 94bf215546Sopenharmony_ci} 95bf215546Sopenharmony_ci 96bf215546Sopenharmony_cistatic bool 97bf215546Sopenharmony_civalid_src(struct fau_state *fau, unsigned fau_page, bi_index src) 98bf215546Sopenharmony_ci{ 99bf215546Sopenharmony_ci if (src.type != BI_INDEX_FAU) 100bf215546Sopenharmony_ci return true; 101bf215546Sopenharmony_ci 102bf215546Sopenharmony_ci bool valid = (fau_page == va_fau_page(src.value)); 103bf215546Sopenharmony_ci valid &= fau_state_buffer(fau, src); 104bf215546Sopenharmony_ci 105bf215546Sopenharmony_ci if (src.value & BIR_FAU_UNIFORM) 106bf215546Sopenharmony_ci valid &= fau_state_uniform(fau, src); 107bf215546Sopenharmony_ci else if (fau_is_special(src.value)) 108bf215546Sopenharmony_ci valid &= fau_state_special(fau, src); 109bf215546Sopenharmony_ci 110bf215546Sopenharmony_ci return valid; 111bf215546Sopenharmony_ci} 112bf215546Sopenharmony_ci 113bf215546Sopenharmony_cibool 114bf215546Sopenharmony_civa_validate_fau(bi_instr *I) 115bf215546Sopenharmony_ci{ 116bf215546Sopenharmony_ci bool valid = true; 117bf215546Sopenharmony_ci struct fau_state fau = { .uniform_slot = -1 }; 118bf215546Sopenharmony_ci unsigned fau_page = va_select_fau_page(I); 119bf215546Sopenharmony_ci 120bf215546Sopenharmony_ci bi_foreach_src(I, s) { 121bf215546Sopenharmony_ci valid &= valid_src(&fau, fau_page, I->src[s]); 122bf215546Sopenharmony_ci } 123bf215546Sopenharmony_ci 124bf215546Sopenharmony_ci return valid; 125bf215546Sopenharmony_ci} 126bf215546Sopenharmony_ci 127bf215546Sopenharmony_civoid 128bf215546Sopenharmony_civa_repair_fau(bi_builder *b, bi_instr *I) 129bf215546Sopenharmony_ci{ 130bf215546Sopenharmony_ci struct fau_state fau = { .uniform_slot = -1 }; 131bf215546Sopenharmony_ci unsigned fau_page = va_select_fau_page(I); 132bf215546Sopenharmony_ci 133bf215546Sopenharmony_ci bi_foreach_src(I, s) { 134bf215546Sopenharmony_ci struct fau_state push = fau; 135bf215546Sopenharmony_ci bi_index src = I->src[s]; 136bf215546Sopenharmony_ci 137bf215546Sopenharmony_ci if (!valid_src(&fau, fau_page, src)) { 138bf215546Sopenharmony_ci bi_index copy = bi_mov_i32(b, bi_strip_index(src)); 139bf215546Sopenharmony_ci I->src[s] = bi_replace_index(src, copy); 140bf215546Sopenharmony_ci 141bf215546Sopenharmony_ci /* Rollback update. Since the replacement move doesn't affect FAU 142bf215546Sopenharmony_ci * state, there is no need to call valid_src again. 143bf215546Sopenharmony_ci */ 144bf215546Sopenharmony_ci fau = push; 145bf215546Sopenharmony_ci } 146bf215546Sopenharmony_ci } 147bf215546Sopenharmony_ci} 148bf215546Sopenharmony_ci 149bf215546Sopenharmony_civoid 150bf215546Sopenharmony_civa_validate(FILE *fp, bi_context *ctx) 151bf215546Sopenharmony_ci{ 152bf215546Sopenharmony_ci bool errors = false; 153bf215546Sopenharmony_ci 154bf215546Sopenharmony_ci bi_foreach_instr_global(ctx, I) { 155bf215546Sopenharmony_ci if (!va_validate_fau(I)) { 156bf215546Sopenharmony_ci if (!errors) { 157bf215546Sopenharmony_ci fprintf(fp, "Validation failed, this is a bug. Shader:\n\n"); 158bf215546Sopenharmony_ci bi_print_shader(ctx, fp); 159bf215546Sopenharmony_ci fprintf(fp, "Offending code:\n"); 160bf215546Sopenharmony_ci } 161bf215546Sopenharmony_ci 162bf215546Sopenharmony_ci bi_print_instr(I, fp); 163bf215546Sopenharmony_ci fprintf(fp, "\n"); 164bf215546Sopenharmony_ci errors = true; 165bf215546Sopenharmony_ci } 166bf215546Sopenharmony_ci } 167bf215546Sopenharmony_ci 168bf215546Sopenharmony_ci if (errors) 169bf215546Sopenharmony_ci exit(1); 170bf215546Sopenharmony_ci} 171