| 1 |
/* |
| 2 |
* Copyright (C) 2022 Collabora Ltd. |
| 3 |
* SPDX-License-Identifier: MIT |
| 4 |
*/ |
| 5 |
|
| 6 |
#include "va_compiler.h" |
| 7 |
#include "valhall_enums.h" |
| 8 |
|
| 9 |
/* |
| 10 |
* Valhall sources may marked as the last use of a register, according |
| 11 |
* to the following rules: |
| 12 |
* |
| 13 |
* 1. The last use of a register should be marked allowing the hardware |
| 14 |
* to elide register writes. |
| 15 |
* 2. Staging sources may be read at any time before the asynchronous |
| 16 |
* instruction completes. If a register is used as both a staging source and |
| 17 |
* a regular source, the regular source cannot be marked until the program |
| 18 |
* waits for the asynchronous instruction. |
| 19 |
* 3. Marking a register pair marks both registers in the pair. |
| 20 |
* |
| 21 |
* Last use information follows immediately from (post-RA) liveness analysis: |
| 22 |
* a register is dead immediately after its last use. |
| 23 |
* |
| 24 |
* Staging information follows from scoreboard analysis: do not mark registers |
| 25 |
* that are read by a pending asynchronous instruction. Note that the Valhall |
| 26 |
* scoreboard analysis does not track reads, so we handle that with our own |
| 27 |
* (simplified) scoreboard analysis. |
| 28 |
* |
| 29 |
* Register pairs are marked conservatively: if either register in a pair cannot |
| 30 |
* be marked, do not mark either register. |
| 31 |
*/ |
| 32 |
|
| 33 |
static uint64_t |
| 34 |
bi_staging_read_mask(const bi_instr *I) |
| 35 |
{ |
| 36 |
uint64_t mask = 0; |
| 37 |
|
| 38 |
bi_foreach_src(I, s) { |
| 39 |
if (bi_is_staging_src(I, s) && !bi_is_null(I->src[s])) { |
| 40 |
assert(I->src[s].type == BI_INDEX_REGISTER); |
| 41 |
unsigned reg = I->src[s].value; |
| 42 |
unsigned count = bi_count_read_registers(I, s); |
| 43 |
|
| 44 |
mask |= (BITFIELD64_MASK(count) << reg); |
| 45 |
} |
| 46 |
} |
| 47 |
|
| 48 |
return mask; |
| 49 |
} |
| 50 |
|
| 51 |
static bool |
| 52 |
bi_writes_reg(const bi_instr *I, unsigned reg) |
| 53 |
{ |
| 54 |
bi_foreach_dest(I, d) { |
| 55 |
assert(I->dest[d].type == BI_INDEX_REGISTER); |
| 56 |
|
| 57 |
unsigned count = bi_count_write_registers(I, d); |
| 58 |
|
| 59 |
if (reg >= I->dest[d].value && (reg - I->dest[d].value) < count) |
| 60 |
return true; |
| 61 |
} |
| 62 |
|
| 63 |
return false; |
| 64 |
} |
| 65 |
|
| 66 |
static unsigned |
| 67 |
waits_on_slot(enum va_flow flow, unsigned slot) |
| 68 |
{ |
| 69 |
return (flow == VA_FLOW_WAIT) || (flow == VA_FLOW_WAIT0126) || |
| 70 |
(va_flow_is_wait_or_none(flow) && (flow & BITFIELD_BIT(slot))); |
| 71 |
} |
| 72 |
|
| 73 |
static void |
| 74 |
scoreboard_update(struct bi_scoreboard_state *st, const bi_instr *I) |
| 75 |
{ |
| 76 |
/* Mark read staging registers */ |
| 77 |
st->read[I->slot] |= bi_staging_read_mask(I); |
| 78 |
|
| 79 |
/* Unmark registers after they are waited on */ |
| 80 |
for (unsigned i = 0; i < VA_NUM_GENERAL_SLOTS; ++i) { |
| 81 |
if (waits_on_slot(I->flow, i)) |
| 82 |
st->read[i] = 0; |
| 83 |
} |
| 84 |
} |
| 85 |
|
| 86 |
static void |
| 87 |
va_analyze_scoreboard_reads(bi_context *ctx) |
| 88 |
{ |
| 89 |
u_worklist worklist; |
| 90 |
bi_worklist_init(ctx, &worklist); |
| 91 |
|
| 92 |
bi_foreach_block(ctx, block) { |
| 93 |
bi_worklist_push_tail(&worklist, block); |
| 94 |
|
| 95 |
/* Reset analysis from previous pass */ |
| 96 |
block->scoreboard_in = (struct bi_scoreboard_state){0}; |
| 97 |
block->scoreboard_out = (struct bi_scoreboard_state){0}; |
| 98 |
} |
| 99 |
|
| 100 |
/* Perform forward data flow analysis to calculate dependencies */ |
| 101 |
while (!u_worklist_is_empty(&worklist)) { |
| 102 |
/* Pop from the front for forward analysis */ |
| 103 |
bi_block *blk = bi_worklist_pop_head(&worklist); |
| 104 |
|
| 105 |
bi_foreach_predecessor(blk, pred) { |
| 106 |
for (unsigned i = 0; i < VA_NUM_GENERAL_SLOTS; ++i) |
| 107 |
blk->scoreboard_in.read[i] |= (*pred)->scoreboard_out.read[i]; |
| 108 |
} |
| 109 |
|
| 110 |
struct bi_scoreboard_state state = blk->scoreboard_in; |
| 111 |
|
| 112 |
bi_foreach_instr_in_block(blk, I) |
| 113 |
scoreboard_update(&state, I); |
| 114 |
|
| 115 |
/* If there was progress, reprocess successors */ |
| 116 |
if (memcmp(&state, &blk->scoreboard_out, sizeof(state)) != 0) { |
| 117 |
bi_foreach_successor(blk, succ) |
| 118 |
bi_worklist_push_tail(&worklist, succ); |
| 119 |
} |
| 120 |
|
| 121 |
blk->scoreboard_out = state; |
| 122 |
} |
| 123 |
|
| 124 |
u_worklist_fini(&worklist); |
| 125 |
} |
| 126 |
|
| 127 |
void |
| 128 |
va_mark_last(bi_context *ctx) |
| 129 |
{ |
| 130 |
/* Analyze the shader globally */ |
| 131 |
bi_postra_liveness(ctx); |
| 132 |
va_analyze_scoreboard_reads(ctx); |
| 133 |
|
| 134 |
bi_foreach_block(ctx, block) { |
| 135 |
uint64_t live = block->reg_live_out; |
| 136 |
|
| 137 |
/* Mark all last uses */ |
| 138 |
bi_foreach_instr_in_block_rev(block, I) { |
| 139 |
bi_foreach_src(I, s) { |
| 140 |
if (I->src[s].type != BI_INDEX_REGISTER) |
| 141 |
continue; |
| 142 |
|
| 143 |
unsigned nr = bi_count_read_registers(I, s); |
| 144 |
uint64_t mask = BITFIELD64_MASK(nr) << I->src[s].value; |
| 145 |
|
| 146 |
/* If the register dead after this instruction, it's the last use */ |
| 147 |
I->src[s].discard = (live & mask) == 0; |
| 148 |
|
| 149 |
/* If the register is overwritten this cycle, it is implicitly |
| 150 |
* discarded, but that won't show up in the liveness analysis. |
| 151 |
*/ |
| 152 |
I->src[s].discard |= bi_writes_reg(I, I->src[s].value); |
| 153 |
} |
| 154 |
|
| 155 |
live = bi_postra_liveness_ins(live, I); |
| 156 |
} |
| 157 |
|
| 158 |
struct bi_scoreboard_state st = block->scoreboard_in; |
| 159 |
|
| 160 |
bi_foreach_instr_in_block(block, I) { |
| 161 |
/* Unmark registers read by a pending async instruction */ |
| 162 |
bi_foreach_src(I, s) { |
| 163 |
if (!I->src[s].discard) |
| 164 |
continue; |
| 165 |
|
| 166 |
assert(I->src[s].type == BI_INDEX_REGISTER); |
| 167 |
|
| 168 |
uint64_t pending_regs = st.read[0] | st.read[1] | st.read[2]; |
| 169 |
bool pending = (pending_regs & BITFIELD64_BIT(I->src[s].value)); |
| 170 |
|
| 171 |
if (bi_is_staging_src(I, s) || pending) |
| 172 |
I->src[s].discard = false; |
| 173 |
} |
| 174 |
|
| 175 |
/* Unmark register pairs where one half must be preserved */ |
| 176 |
bi_foreach_src(I, s) { |
| 177 |
/* Only look for "real" architectural registers */ |
| 178 |
if (s >= 3) |
| 179 |
break; |
| 180 |
|
| 181 |
/* Only need to unmark split registers. */ |
| 182 |
if (va_src_info(I->op, s).size == VA_SIZE_64 && |
| 183 |
bi_count_read_registers(I, s) == 1) { |
| 184 |
bool both_discard = I->src[s].discard && I->src[s + 1].discard; |
| 185 |
|
| 186 |
I->src[s + 0].discard = both_discard; |
| 187 |
I->src[s + 1].discard = both_discard; |
| 188 |
} |
| 189 |
} |
| 190 |
|
| 191 |
scoreboard_update(&st, I); |
| 192 |
} |
| 193 |
} |
| 194 |
} |