| 1 |
/* |
| 2 |
* Copyright (C) 2021 Collabora Ltd. |
| 3 |
* SPDX-License-Identifier: MIT |
| 4 |
*/ |
| 5 |
|
| 6 |
#include "bi_builder.h" |
| 7 |
#include "va_compiler.h" |
| 8 |
#include "valhall.h" |
| 9 |
|
| 10 |
/* Valhall has limits on access to fast-access uniforms: |
| 11 |
* |
| 12 |
* An instruction may access no more than a single 64-bit uniform slot. |
| 13 |
* An instruction may access no more than 64-bits of combined uniforms and |
| 14 |
* constants. An instruction may access no more than a single special immediate |
| 15 |
* (e.g. lane_id). |
| 16 |
* |
| 17 |
* We validate these constraints. |
| 18 |
* |
| 19 |
* An instruction may only access a single page of (special or uniform) FAU. |
| 20 |
* This constraint does not need explicit validation: since FAU slots are |
| 21 |
* naturally aligned, they never cross page boundaries, so this condition is |
| 22 |
* implied by only acesssing a single 64-bit slot. |
| 23 |
*/ |
| 24 |
|
| 25 |
struct fau_state { |
| 26 |
signed uniform_slot; |
| 27 |
bi_index buffer[2]; |
| 28 |
}; |
| 29 |
|
| 30 |
static bool |
| 31 |
fau_state_buffer(struct fau_state *fau, bi_index idx) |
| 32 |
{ |
| 33 |
for (unsigned i = 0; i < ARRAY_SIZE(fau->buffer); ++i) { |
| 34 |
if (bi_is_word_equiv(fau->buffer[i], idx)) |
| 35 |
return true; |
| 36 |
else if (bi_is_null(fau->buffer[i])) { |
| 37 |
fau->buffer[i] = idx; |
| 38 |
return true; |
| 39 |
} |
| 40 |
} |
| 41 |
|
| 42 |
return false; |
| 43 |
} |
| 44 |
|
| 45 |
/* Instructions executed in the execution engine may only encode one FAU index. |
| 46 |
* Instructions executed by the message unit may encode two FAU indices, |
| 47 |
* except for ATEST and BLEND which may sometimes run in the execution engine. |
| 48 |
*/ |
| 49 |
static bool |
| 50 |
can_use_two_fau_indices(enum bi_opcode op) |
| 51 |
{ |
| 52 |
return bi_opcode_props[op].message != BIFROST_MESSAGE_NONE && |
| 53 |
op != BI_OPCODE_ATEST && |
| 54 |
op != BI_OPCODE_BLEND; |
| 55 |
} |
| 56 |
|
| 57 |
static bool |
| 58 |
can_run_on_message_unit(enum bi_opcode op) |
| 59 |
{ |
| 60 |
return bi_opcode_props[op].message != BIFROST_MESSAGE_NONE || |
| 61 |
op == BI_OPCODE_ATEST || op == BI_OPCODE_BLEND; |
| 62 |
} |
| 63 |
|
| 64 |
static bool |
| 65 |
fau_is_special(enum bir_fau fau) |
| 66 |
{ |
| 67 |
return !(fau & (BIR_FAU_UNIFORM | BIR_FAU_IMMEDIATE)); |
| 68 |
} |
| 69 |
|
| 70 |
static bool |
| 71 |
fau_state_uniform(struct fau_state *fau, bi_index idx, enum bi_opcode op) |
| 72 |
{ |
| 73 |
/* Each slot is 64-bits. The low/high half is encoded as the offset of the |
| 74 |
* bi_index, which we want to ignore. |
| 75 |
*/ |
| 76 |
unsigned slot = (idx.value & 63); |
| 77 |
|
| 78 |
if (fau->uniform_slot < 0) |
| 79 |
fau->uniform_slot = slot; |
| 80 |
|
| 81 |
if (fau->uniform_slot != slot) |
| 82 |
return false; |
| 83 |
|
| 84 |
if (!can_use_two_fau_indices(op)) { |
| 85 |
for (unsigned i = 0; i < ARRAY_SIZE(fau->buffer); ++i) { |
| 86 |
bi_index buf = fau->buffer[i]; |
| 87 |
if (!bi_is_null(buf) && fau_is_special(buf.value)) |
| 88 |
return false; |
| 89 |
} |
| 90 |
} |
| 91 |
|
| 92 |
return true; |
| 93 |
} |
| 94 |
|
| 95 |
static bool |
| 96 |
fau_state_special(struct fau_state *fau, bi_index idx, enum bi_opcode op) |
| 97 |
{ |
| 98 |
for (unsigned i = 0; i < ARRAY_SIZE(fau->buffer); ++i) { |
| 99 |
bi_index buf = fau->buffer[i]; |
| 100 |
bool special = !bi_is_null(buf) && fau_is_special(buf.value); |
| 101 |
|
| 102 |
if (special && !bi_is_equiv(buf, idx)) |
| 103 |
return false; |
| 104 |
} |
| 105 |
|
| 106 |
/* Instructions executed by the messaging unit should not encode WARP_ID or |
| 107 |
* anything from special page 3. */ |
| 108 |
if (can_run_on_message_unit(op) && |
| 109 |
(va_fau_page(idx.value) == 3 || idx.value == BIR_FAU_WARP_ID)) |
| 110 |
return false; |
| 111 |
|
| 112 |
return fau->uniform_slot == -1 || can_use_two_fau_indices(op); |
| 113 |
} |
| 114 |
|
| 115 |
static bool |
| 116 |
valid_src(struct fau_state *fau, unsigned fau_page, bi_index src, |
| 117 |
enum bi_opcode op) |
| 118 |
{ |
| 119 |
if (src.type != BI_INDEX_FAU) |
| 120 |
return true; |
| 121 |
|
| 122 |
if (src.value & BIR_FAU_IMMEDIATE) { |
| 123 |
/* There are no small constant restrictions in message instructions */ |
| 124 |
if (can_use_two_fau_indices(op)) |
| 125 |
return true; |
| 126 |
/* Otherwise we just need to ensure that we don't access more than a |
| 127 |
* total of 64 bits of distinct FAU+small constant values */ |
| 128 |
return fau_state_buffer(fau, src); |
| 129 |
} |
| 130 |
|
| 131 |
bool valid = (fau_page == va_fau_page(src.value)); |
| 132 |
valid &= fau_state_buffer(fau, src); |
| 133 |
|
| 134 |
if (src.value & BIR_FAU_UNIFORM) |
| 135 |
valid &= fau_state_uniform(fau, src, op); |
| 136 |
else if (fau_is_special(src.value)) |
| 137 |
valid &= fau_state_special(fau, src, op); |
| 138 |
|
| 139 |
return valid; |
| 140 |
} |
| 141 |
|
| 142 |
bool |
| 143 |
va_validate_fau(bi_instr *I) |
| 144 |
{ |
| 145 |
bool valid = true; |
| 146 |
struct fau_state fau = {.uniform_slot = -1}; |
| 147 |
unsigned fau_page = va_select_fau_page(I); |
| 148 |
|
| 149 |
bi_foreach_src(I, s) { |
| 150 |
valid &= valid_src(&fau, fau_page, I->src[s], I->op); |
| 151 |
} |
| 152 |
|
| 153 |
return valid; |
| 154 |
} |
| 155 |
|
| 156 |
void |
| 157 |
va_repair_fau(bi_builder *b, bi_instr *I) |
| 158 |
{ |
| 159 |
struct fau_state fau = {.uniform_slot = -1}; |
| 160 |
unsigned fau_page = va_select_fau_page(I); |
| 161 |
|
| 162 |
bi_foreach_src(I, s) { |
| 163 |
struct fau_state push = fau; |
| 164 |
bi_index src = I->src[s]; |
| 165 |
|
| 166 |
if (!valid_src(&fau, fau_page, src, I->op)) { |
| 167 |
bi_replace_src(I, s, bi_mov_i32(b, bi_strip_index(src))); |
| 168 |
|
| 169 |
/* Rollback update. Since the replacement move doesn't affect FAU |
| 170 |
* state, there is no need to call valid_src again. |
| 171 |
*/ |
| 172 |
fau = push; |
| 173 |
} |
| 174 |
} |
| 175 |
} |
| 176 |
|
| 177 |
void |
| 178 |
va_validate(FILE *fp, bi_context *ctx) |
| 179 |
{ |
| 180 |
bool errors = false; |
| 181 |
|
| 182 |
bi_foreach_instr_global(ctx, I) { |
| 183 |
if (!va_validate_fau(I)) { |
| 184 |
if (!errors) { |
| 185 |
fprintf(fp, "Validation failed, this is a bug. Shader:\n\n"); |
| 186 |
bi_print_shader(ctx, fp); |
| 187 |
fprintf(fp, "Offending code:\n"); |
| 188 |
} |
| 189 |
|
| 190 |
bi_print_instr(I, fp); |
| 191 |
fprintf(fp, "\n"); |
| 192 |
errors = true; |
| 193 |
} |
| 194 |
} |
| 195 |
|
| 196 |
if (errors) |
| 197 |
exit(1); |
| 198 |
} |