Debian Codegraph @

src/panfrost/compiler/bifrost/bi_lower_spill.c

raw
1 /*
2 * Copyright 2025 Arm Ltd.
3 * SPDX-License-Identifier: MIT
4 */
5
6 #include "compiler.h"
7
8 struct lower_spill_ctx {
9 bi_context *shader;
10 uint32_t *sizes;
11 /* If bi_index::memory, tls_loc[idx.value] is valid. */
12 uint32_t *tls_loc;
13 /* Bytes of spill memory used so far. */
14 uint32_t spill_count;
15 };
16
17 static void
18 lower_memmov(struct lower_spill_ctx* ctx, bi_instr *I, uint32_t tls_base)
19 {
20 assert(I->op == BI_OPCODE_MEMMOV);
21 assert(I->nr_srcs == 1 && I->nr_dests == 1);
22 assert(I->src[0].memory || I->dest[0].memory);
23
24 bi_builder b = bi_init_builder(ctx->shader, bi_before_instr(I));
25
26 bi_index src = I->src[0];
27 bi_index dst = I->dest[0];
28
29 if (I->src[0].memory) {
30 unsigned offset = ctx->tls_loc[src.value];
31 unsigned bits = ctx->sizes[src.value] * 32;
32 assert(bits <= 128);
33 bi_load_tl(&b, bits, dst, tls_base + offset);
34 } else {
35 unsigned offset = ctx->tls_loc[dst.value];
36 unsigned bits = ctx->sizes[dst.value] * 32;
37 assert(bits <= 128);
38 bi_store_tl(&b, bits, src, tls_base + offset);
39 }
40
41 bi_remove_instruction(I);
42 }
43
44 static void
45 lower_mem_phi(struct lower_spill_ctx* ctx, bi_instr *I, uint32_t tls_base)
46 {
47 assert(I->op == BI_OPCODE_PHI);
48 assert(I->nr_dests == 1);
49
50 /* PHIs get lowered in bi_out_of_ssa, which expects memory operands to
51 * provide the actual TLS offset as bi_index::value.
52 */
53
54 if (I->dest[0].memory) {
55 const bi_index dst = I->dest[0];
56 /* bi_repair_ssa could make PHIs for MEMMOV sources which could be
57 * wider. But, those should all get eliminated as trivial because they
58 * are only defined once so they would all look like mX = PHI mX, mX.
59 */
60 assert(ctx->sizes[dst.value] == 1);
61 I->dest[0].value = tls_base + ctx->tls_loc[dst.value];
62 }
63
64 bi_foreach_src(I, s) {
65 const bi_index src = I->src[s];
66 if (!src.memory)
67 continue;
68
69 assert(ctx->tls_loc[src.value] != UINT32_MAX && "Undefined source");
70 assert(ctx->sizes[src.value] == 1);
71 I->src[s].value = tls_base + ctx->tls_loc[src.value];
72 }
73 }
74
75 static void
76 assign_tls_locations(struct lower_spill_ctx *ctx) {
77 bi_foreach_instr_global(ctx->shader, I) {
78 bi_foreach_ssa_dest(I, d) {
79 bi_index dst = I->dest[d];
80 if (!dst.memory)
81 continue;
82
83 assert(I->op == BI_OPCODE_MEMMOV || I->op == BI_OPCODE_PHI);
84 assert(ctx->tls_loc[dst.value] == UINT32_MAX && "Broken SSA");
85
86 ctx->tls_loc[dst.value] = ctx->spill_count;
87 ctx->spill_count += 4 * ctx->sizes[dst.value];
88 }
89 }
90 }
91
92 unsigned
93 bi_lower_spill(bi_context* ctx, uint32_t tls_base) {
94 void* memctx = ralloc_context(NULL);
95
96 uint32_t *sizes = rzalloc_array(memctx, uint32_t, ctx->ssa_alloc);
97 uint32_t *tls_loc = ralloc_array(memctx, uint32_t, ctx->ssa_alloc);
98 memset(tls_loc, 0xff, sizeof(uint32_t) * ctx->ssa_alloc);
99
100 bi_record_sizes(ctx, sizes);
101
102 struct lower_spill_ctx lctx = {
103 .shader = ctx,
104 .sizes = sizes,
105 .tls_loc = tls_loc,
106 .spill_count = 0,
107 };
108
109 assign_tls_locations(&lctx);
110
111 bi_foreach_instr_global_safe(ctx, I) {
112 switch (I->op) {
113 case BI_OPCODE_MEMMOV:
114 lower_memmov(&lctx, I, tls_base);
115 break;
116 case BI_OPCODE_PHI:
117 lower_mem_phi(&lctx, I, tls_base);
118 break;
119 default:
120 break;
121 }
122 }
123
124 ralloc_free(memctx);
125
126 return lctx.spill_count;
127 }