| 1 |
/* |
| 2 |
* Copyright (C) 2019-2021 Collabora, Ltd. |
| 3 |
* SPDX-License-Identifier: MIT |
| 4 |
*/ |
| 5 |
|
| 6 |
#include "compiler.h" |
| 7 |
|
| 8 |
/* Bifrost texture operations have a `skip` bit, instructing helper invocations |
| 9 |
* to skip execution. Each clause has a `terminate_discarded_threads` bit, |
| 10 |
* which will terminate helper invocations. |
| 11 |
* |
| 12 |
* The terminate bit should be set on the last clause requiring helper |
| 13 |
* invocations. Without control flow, that's the last source-order instruction; |
| 14 |
* with control flow, there may be multiple such instructions (with ifs) or no |
| 15 |
* such instruction (with loops). |
| 16 |
* |
| 17 |
* The skip bit should be set unless the value of this instruction is required |
| 18 |
* by a future instruction requiring helper invocations. Consider: |
| 19 |
* |
| 20 |
* 0 = texture ... |
| 21 |
* 1 = fmul 0, #10 |
| 22 |
* 2 = dfdx 1 |
| 23 |
* store 2 |
| 24 |
* |
| 25 |
* Since the derivative calculation 2 requires helper invocations, the value 1 |
| 26 |
* must be calculated by helper invocations, and since it depends on 0, 0 must |
| 27 |
* be calculated by helpers. Hence the texture op does NOT have the skip bit |
| 28 |
* set, and the clause containing the derivative has the terminate bit set. |
| 29 |
* |
| 30 |
* Calculating the terminate bit occurs by forward dataflow analysis to |
| 31 |
* determine which blocks require helper invocations. A block requires |
| 32 |
* invocations in if any of its instructions use helper invocations, or if it |
| 33 |
* depends on a block that requires invocation. With that analysis, the |
| 34 |
* terminate bit is set on the last instruction using invocations within any |
| 35 |
* block that does *not* require invocations out. |
| 36 |
* |
| 37 |
* Likewise, calculating the execute bit requires backward dataflow analysis |
| 38 |
* with union as the join operation and the generating set being the union of |
| 39 |
* sources of instructions writing executed values. The skip bit is the inverse |
| 40 |
* of the execute bit. |
| 41 |
*/ |
| 42 |
|
| 43 |
static bool |
| 44 |
bi_has_skip_bit(enum bi_opcode op) |
| 45 |
{ |
| 46 |
switch (op) { |
| 47 |
case BI_OPCODE_TEX_SINGLE: |
| 48 |
case BI_OPCODE_TEXC: |
| 49 |
case BI_OPCODE_TEXC_DUAL: |
| 50 |
case BI_OPCODE_TEXS_2D_F16: |
| 51 |
case BI_OPCODE_TEXS_2D_F32: |
| 52 |
case BI_OPCODE_TEXS_CUBE_F16: |
| 53 |
case BI_OPCODE_TEXS_CUBE_F32: |
| 54 |
case BI_OPCODE_VAR_TEX_F16: |
| 55 |
case BI_OPCODE_VAR_TEX_F32: |
| 56 |
return true; |
| 57 |
default: |
| 58 |
return false; |
| 59 |
} |
| 60 |
} |
| 61 |
|
| 62 |
/* Does a given instruction require helper threads to be active (because it |
| 63 |
* reads from other subgroup lanes)? This only applies to fragment shaders. |
| 64 |
* Other shader stages do not have a notion of helper threads. */ |
| 65 |
|
| 66 |
bool |
| 67 |
bi_instr_uses_helpers(bi_instr *I) |
| 68 |
{ |
| 69 |
switch (I->op) { |
| 70 |
case BI_OPCODE_TEXC: |
| 71 |
case BI_OPCODE_TEXC_DUAL: |
| 72 |
case BI_OPCODE_TEXS_2D_F16: |
| 73 |
case BI_OPCODE_TEXS_2D_F32: |
| 74 |
case BI_OPCODE_TEXS_CUBE_F16: |
| 75 |
case BI_OPCODE_TEXS_CUBE_F32: |
| 76 |
case BI_OPCODE_VAR_TEX_F16: |
| 77 |
case BI_OPCODE_VAR_TEX_F32: |
| 78 |
return !I->lod_mode; /* set for zero, clear for computed */ |
| 79 |
case BI_OPCODE_TEX_SINGLE: |
| 80 |
return (I->va_lod_mode == BI_VA_LOD_MODE_COMPUTED_LOD) || |
| 81 |
(I->va_lod_mode == BI_VA_LOD_MODE_COMPUTED_BIAS); |
| 82 |
case BI_OPCODE_TEX_GRADIENT: |
| 83 |
/* If we don't use derivatives to compute the lod we need disabled lanes |
| 84 |
* to have valid texture coordinates. */ |
| 85 |
return !I->derivative_enable; |
| 86 |
case BI_OPCODE_WMASK: |
| 87 |
/* Helpers are needed to implement voting in fragment shaders. */ |
| 88 |
return true; |
| 89 |
case BI_OPCODE_CLPER_I32: |
| 90 |
case BI_OPCODE_CLPER_OLD_I32: |
| 91 |
/* Fragment shaders require helpers to implement derivatives. |
| 92 |
* Other shader stages don't have helpers at all */ |
| 93 |
return true; |
| 94 |
default: |
| 95 |
return false; |
| 96 |
} |
| 97 |
} |
| 98 |
|
| 99 |
static void |
| 100 |
bi_add_branch_compare_values(const bi_instr *I, BITSET_WORD *deps) |
| 101 |
{ |
| 102 |
switch (I->op) { |
| 103 |
case BI_OPCODE_BRANCHZI: |
| 104 |
case BI_OPCODE_BRANCHC_I16: |
| 105 |
case BI_OPCODE_BRANCHC_I32: |
| 106 |
BITSET_SET(deps, I->src[0].value); |
| 107 |
break; |
| 108 |
case BI_OPCODE_BRANCH_F16: |
| 109 |
case BI_OPCODE_BRANCH_F32: |
| 110 |
case BI_OPCODE_BRANCH_I16: |
| 111 |
case BI_OPCODE_BRANCH_I32: |
| 112 |
case BI_OPCODE_BRANCH_S16: |
| 113 |
case BI_OPCODE_BRANCH_S32: |
| 114 |
case BI_OPCODE_BRANCH_U16: |
| 115 |
case BI_OPCODE_BRANCH_U32: |
| 116 |
case BI_OPCODE_BRANCHZ_F16: |
| 117 |
case BI_OPCODE_BRANCHZ_F32: |
| 118 |
case BI_OPCODE_BRANCHZ_I16: |
| 119 |
case BI_OPCODE_BRANCHZ_I32: |
| 120 |
case BI_OPCODE_BRANCHZ_S16: |
| 121 |
case BI_OPCODE_BRANCHZ_S32: |
| 122 |
case BI_OPCODE_BRANCHZ_U16: |
| 123 |
case BI_OPCODE_BRANCHZ_U32: |
| 124 |
BITSET_SET(deps, I->src[0].value); |
| 125 |
BITSET_SET(deps, I->src[1].value); |
| 126 |
break; |
| 127 |
default: |
| 128 |
break; |
| 129 |
} |
| 130 |
} |
| 131 |
|
| 132 |
/* Does a block use helpers directly */ |
| 133 |
static bool |
| 134 |
bi_block_uses_helpers(bi_block *block) |
| 135 |
{ |
| 136 |
bi_foreach_instr_in_block(block, I) { |
| 137 |
if (bi_instr_uses_helpers(I)) |
| 138 |
return true; |
| 139 |
} |
| 140 |
|
| 141 |
return false; |
| 142 |
} |
| 143 |
|
| 144 |
bool |
| 145 |
bi_block_terminates_helpers(bi_block *block) |
| 146 |
{ |
| 147 |
/* Can't terminate if a successor needs helpers */ |
| 148 |
bi_foreach_successor(block, succ) { |
| 149 |
if (succ->pass_flags & 1) |
| 150 |
return false; |
| 151 |
} |
| 152 |
|
| 153 |
/* Otherwise we terminate */ |
| 154 |
return true; |
| 155 |
} |
| 156 |
|
| 157 |
/* |
| 158 |
* Propagate the pass flag up the control flow graph by performing depth-first |
| 159 |
* search on the directed control flow graph. |
| 160 |
*/ |
| 161 |
static void |
| 162 |
bi_propagate_pass_flag(bi_block *block) |
| 163 |
{ |
| 164 |
block->pass_flags = 1; |
| 165 |
|
| 166 |
bi_foreach_predecessor(block, pred) { |
| 167 |
if ((*pred)->pass_flags == 0) |
| 168 |
bi_propagate_pass_flag(*pred); |
| 169 |
} |
| 170 |
} |
| 171 |
|
| 172 |
void |
| 173 |
bi_analyze_helper_terminate(bi_context *ctx) |
| 174 |
{ |
| 175 |
/* Other shader stages do not have a notion of helper threads, so we |
| 176 |
* can skip the analysis. Don't run for blend shaders, either, since |
| 177 |
* they run in the context of another shader that we don't see. */ |
| 178 |
if (ctx->stage != MESA_SHADER_FRAGMENT || ctx->inputs->is_blend) |
| 179 |
return; |
| 180 |
|
| 181 |
/* Clear flags */ |
| 182 |
bi_foreach_block(ctx, block) |
| 183 |
block->pass_flags = 0; |
| 184 |
|
| 185 |
/* For each block, check if it uses helpers and propagate that fact if |
| 186 |
* so. We walk in reverse order to minimize the number of blocks tested: |
| 187 |
* if the (unique) last block uses helpers, only that block is tested. |
| 188 |
*/ |
| 189 |
bi_foreach_block_rev(ctx, block) { |
| 190 |
if (block->pass_flags == 0 && bi_block_uses_helpers(block)) |
| 191 |
bi_propagate_pass_flag(block); |
| 192 |
} |
| 193 |
} |
| 194 |
|
| 195 |
void |
| 196 |
bi_mark_clauses_td(bi_context *ctx) |
| 197 |
{ |
| 198 |
if (ctx->stage != MESA_SHADER_FRAGMENT || ctx->inputs->is_blend) |
| 199 |
return; |
| 200 |
|
| 201 |
/* Finally, mark clauses requiring helpers */ |
| 202 |
bi_foreach_block(ctx, block) { |
| 203 |
/* At the end, there are helpers iff we don't terminate */ |
| 204 |
bool helpers = !bi_block_terminates_helpers(block); |
| 205 |
|
| 206 |
bi_foreach_clause_in_block_rev(block, clause) { |
| 207 |
bi_foreach_instr_in_clause_rev(block, clause, I) { |
| 208 |
helpers |= bi_instr_uses_helpers(I); |
| 209 |
} |
| 210 |
|
| 211 |
clause->td = !helpers; |
| 212 |
} |
| 213 |
} |
| 214 |
} |
| 215 |
|
| 216 |
static bool |
| 217 |
bi_helper_block_update(BITSET_WORD *deps, bi_block *block) |
| 218 |
{ |
| 219 |
bool progress = false; |
| 220 |
|
| 221 |
bi_foreach_instr_in_block_rev(block, I) { |
| 222 |
/* If a destination is required by helper invocation... */ |
| 223 |
bi_foreach_dest(I, d) { |
| 224 |
if (!BITSET_TEST(deps, I->dest[d].value)) |
| 225 |
continue; |
| 226 |
|
| 227 |
/* ...so are the sources */ |
| 228 |
bi_foreach_ssa_src(I, s) { |
| 229 |
progress |= !BITSET_TEST(deps, I->src[s].value); |
| 230 |
BITSET_SET(deps, I->src[s].value); |
| 231 |
} |
| 232 |
|
| 233 |
break; |
| 234 |
} |
| 235 |
} |
| 236 |
|
| 237 |
return progress; |
| 238 |
} |
| 239 |
|
| 240 |
void |
| 241 |
bi_analyze_helper_requirements(bi_context *ctx) |
| 242 |
{ |
| 243 |
BITSET_WORD *deps = calloc(sizeof(BITSET_WORD), ctx->ssa_alloc); |
| 244 |
|
| 245 |
/* Initialize with the sources of instructions consuming |
| 246 |
* derivatives and the sources of conditional branch instructions */ |
| 247 |
|
| 248 |
bi_foreach_instr_global(ctx, I) { |
| 249 |
if (bi_instr_uses_helpers(I)) { |
| 250 |
bi_foreach_ssa_src(I, s) |
| 251 |
BITSET_SET(deps, I->src[s].value); |
| 252 |
} else { |
| 253 |
bi_add_branch_compare_values(I, deps); |
| 254 |
} |
| 255 |
} |
| 256 |
|
| 257 |
/* Propagate that up */ |
| 258 |
bool progress; |
| 259 |
do { |
| 260 |
progress = false; |
| 261 |
bi_foreach_block_rev(ctx, block) |
| 262 |
progress |= bi_helper_block_update(deps, block); |
| 263 |
} while (progress); |
| 264 |
|
| 265 |
/* Set the execute bits */ |
| 266 |
|
| 267 |
bi_foreach_instr_global(ctx, I) { |
| 268 |
if (!bi_has_skip_bit(I->op)) |
| 269 |
continue; |
| 270 |
|
| 271 |
bool exec = false; |
| 272 |
|
| 273 |
bi_foreach_dest(I, d) |
| 274 |
exec |= BITSET_TEST(deps, I->dest[d].value); |
| 275 |
|
| 276 |
I->skip = !exec; |
| 277 |
} |
| 278 |
|
| 279 |
free(deps); |
| 280 |
} |