| 1 |
/* |
| 2 |
* Copyright 2025 Valve Corporation |
| 3 |
* Copyright 2019-2022 Collabora, Ltd. |
| 4 |
* SPDX-License-Identifier: MIT |
| 5 |
*/ |
| 6 |
|
| 7 |
#include "util/bitset.h" |
| 8 |
#include "agx_compiler.h" |
| 9 |
#include "shader_enums.h" |
| 10 |
|
| 11 |
static void |
| 12 |
postra_liveness_ins(BITSET_WORD *live, agx_instr *I) |
| 13 |
{ |
| 14 |
agx_foreach_reg_dest(I, d) { |
| 15 |
unsigned reg = I->dest[d].value; |
| 16 |
BITSET_CLEAR_COUNT(live, reg, agx_index_size_16(I->dest[d])); |
| 17 |
} |
| 18 |
|
| 19 |
agx_foreach_reg_src(I, s) { |
| 20 |
unsigned reg = I->src[s].value; |
| 21 |
BITSET_SET_COUNT(live, reg, agx_index_size_16(I->src[s])); |
| 22 |
} |
| 23 |
} |
| 24 |
|
| 25 |
/* |
| 26 |
* Globally, liveness analysis uses a fixed-point algorithm based on a |
| 27 |
* worklist. We initialize a work list with the exit block. We iterate the work |
| 28 |
* list to compute live_in from live_out for each block on the work list, |
| 29 |
* adding the predecessors of the block to the work list if we made progress. |
| 30 |
*/ |
| 31 |
static void |
| 32 |
postra_liveness(agx_context *ctx) |
| 33 |
{ |
| 34 |
u_worklist worklist; |
| 35 |
agx_worklist_init(ctx, &worklist); |
| 36 |
|
| 37 |
agx_foreach_block(ctx, block) { |
| 38 |
BITSET_ZERO(block->reg_live_in); |
| 39 |
BITSET_ZERO(block->reg_live_out); |
| 40 |
|
| 41 |
agx_worklist_push_tail(&worklist, block); |
| 42 |
} |
| 43 |
|
| 44 |
while (!u_worklist_is_empty(&worklist)) { |
| 45 |
/* Pop off in reverse order since liveness is backwards */ |
| 46 |
agx_block *blk = agx_worklist_pop_tail(&worklist); |
| 47 |
|
| 48 |
/* Calculate liveness locally */ |
| 49 |
agx_foreach_successor(blk, succ) { |
| 50 |
BITSET_OR(blk->reg_live_out, blk->reg_live_out, succ->reg_live_in); |
| 51 |
} |
| 52 |
|
| 53 |
BITSET_DECLARE(live, AGX_NUM_REGS); |
| 54 |
memcpy(live, blk->reg_live_out, sizeof(live)); |
| 55 |
|
| 56 |
agx_foreach_instr_in_block_rev(blk, ins) { |
| 57 |
postra_liveness_ins(live, ins); |
| 58 |
} |
| 59 |
|
| 60 |
if (BITSET_EQUAL(blk->reg_live_in, live)) |
| 61 |
continue; |
| 62 |
|
| 63 |
/* We made progress, so we need to reprocess the predecessors */ |
| 64 |
memcpy(blk->reg_live_in, live, sizeof(live)); |
| 65 |
agx_foreach_predecessor(blk, pred) { |
| 66 |
agx_worklist_push_head(&worklist, *pred); |
| 67 |
} |
| 68 |
} |
| 69 |
|
| 70 |
u_worklist_fini(&worklist); |
| 71 |
} |
| 72 |
|
| 73 |
static bool |
| 74 |
writes_reg(const agx_instr *I, unsigned reg) |
| 75 |
{ |
| 76 |
agx_foreach_reg_dest(I, d) { |
| 77 |
unsigned count = agx_index_size_16(I->dest[d]); |
| 78 |
|
| 79 |
if (reg >= I->dest[d].value && (reg - I->dest[d].value) < count) |
| 80 |
return true; |
| 81 |
} |
| 82 |
|
| 83 |
return false; |
| 84 |
} |
| 85 |
|
| 86 |
/* |
| 87 |
* Mark last-use sources to allow the hardware to discard from the register |
| 88 |
* cache. Last use information follows immediately from (post-RA) liveness |
| 89 |
* analysis: a register is dead immediately after its last use. |
| 90 |
* |
| 91 |
* Mark cache hints on sources/destinations to encourage the hardware to make |
| 92 |
* better use of the register cache. This is a simple local analysis. |
| 93 |
*/ |
| 94 |
void |
| 95 |
agx_opt_register_cache(agx_context *ctx) |
| 96 |
{ |
| 97 |
/* Analyze the shader globally */ |
| 98 |
postra_liveness(ctx); |
| 99 |
|
| 100 |
agx_foreach_block(ctx, block) { |
| 101 |
/* Live-set at each point in the program */ |
| 102 |
BITSET_DECLARE(live, AGX_NUM_REGS); |
| 103 |
memcpy(live, block->reg_live_out, sizeof(live)); |
| 104 |
|
| 105 |
/* Set of registers read "soon" by an ALU instruction. These are |
| 106 |
* candidates for the .cache bit. |
| 107 |
*/ |
| 108 |
BITSET_DECLARE(alu_reads, AGX_NUM_REGS) = {0}; |
| 109 |
|
| 110 |
agx_foreach_instr_in_block_rev(block, I) { |
| 111 |
agx_foreach_reg_dest(I, d) { |
| 112 |
uint64_t reg = I->dest[d].value; |
| 113 |
unsigned nr = agx_index_size_16(I->dest[d]); |
| 114 |
|
| 115 |
I->dest[d].cache = BITSET_TEST(alu_reads, I->dest[d].value); |
| 116 |
BITSET_CLEAR_COUNT(alu_reads, reg, nr); |
| 117 |
} |
| 118 |
|
| 119 |
agx_foreach_reg_src(I, s) { |
| 120 |
I->src[s].cache = BITSET_TEST(alu_reads, I->src[s].value); |
| 121 |
} |
| 122 |
|
| 123 |
agx_foreach_reg_src(I, s) { |
| 124 |
uint64_t reg = I->src[s].value; |
| 125 |
unsigned nr = agx_index_size_16(I->src[s]); |
| 126 |
|
| 127 |
/* If the register dead after this instruction, it's the last use. |
| 128 |
* That includes if the register is overwritten this cycle, but that |
| 129 |
* won't show up in the liveness analysis. |
| 130 |
*/ |
| 131 |
bool lu = !BITSET_TEST_COUNT(live, reg, nr) || writes_reg(I, reg); |
| 132 |
|
| 133 |
/* Handling divergent blocks would require physical CFG awareness. |
| 134 |
* Just bail for now, skipping this pass won't affect correctness. |
| 135 |
*/ |
| 136 |
I->src[s].discard = lu && !block->divergent; |
| 137 |
|
| 138 |
/* Mark any source read by an ALU instruction in the same block as |
| 139 |
* wanting a .cache hint. This is better than just marking |
| 140 |
* everything, although it overly hints for very long blocks and |
| 141 |
* underhints for registers used across block boundaries. It's |
| 142 |
* probably good enough, though, and it's not clear how to do much |
| 143 |
* better given our limited understanding of the hardware. |
| 144 |
*/ |
| 145 |
if (agx_is_alu(I)) |
| 146 |
BITSET_SET_COUNT(alu_reads, reg, nr); |
| 147 |
|
| 148 |
assert(!(I->src[s].discard && I->src[s].cache)); |
| 149 |
} |
| 150 |
|
| 151 |
postra_liveness_ins(live, I); |
| 152 |
} |
| 153 |
} |
| 154 |
} |