Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mesa
Path: blob/21.2-virgl/src/asahi/compiler/agx_register_allocate.c
7655 views
1
/*
2
* Copyright (C) 2021 Alyssa Rosenzweig <[email protected]>
3
*
4
* Permission is hereby granted, free of charge, to any person obtaining a
5
* copy of this software and associated documentation files (the "Software"),
6
* to deal in the Software without restriction, including without limitation
7
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
8
* and/or sell copies of the Software, and to permit persons to whom the
9
* Software is furnished to do so, subject to the following conditions:
10
*
11
* The above copyright notice and this permission notice (including the next
12
* paragraph) shall be included in all copies or substantial portions of the
13
* Software.
14
*
15
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
* SOFTWARE.
22
*/
23
24
#include "agx_compiler.h"
25
#include "agx_builder.h"
26
27
/* Trivial register allocator that never frees anything.
28
*
29
* TODO: Write a real register allocator.
30
* TODO: Handle phi nodes.
31
*/
32
33
/** Returns number of registers read by an instruction. TODO: 16-bit */
34
static unsigned
35
agx_read_registers(agx_instr *I, unsigned s)
36
{
37
unsigned size = I->src[s].size == AGX_SIZE_32 ? 2 : 1;
38
39
switch (I->op) {
40
default:
41
return size;
42
}
43
}
44
45
/** Returns number of registers written by an instruction */
46
static unsigned
47
agx_write_registers(agx_instr *I, unsigned d)
48
{
49
unsigned size = I->dest[d].size == AGX_SIZE_32 ? 2 : 1;
50
51
switch (I->op) {
52
case AGX_OPCODE_LD_VARY:
53
case AGX_OPCODE_DEVICE_LOAD:
54
case AGX_OPCODE_TEXTURE_SAMPLE:
55
case AGX_OPCODE_LD_TILE:
56
return 8;
57
case AGX_OPCODE_LD_VARY_FLAT:
58
return 6;
59
case AGX_OPCODE_P_COMBINE:
60
{
61
unsigned components = 0;
62
63
for (unsigned i = 0; i < 4; ++i) {
64
if (!agx_is_null(I->src[i]))
65
components = i + 1;
66
}
67
68
return components * size;
69
}
70
default:
71
return size;
72
}
73
}
74
75
static unsigned
76
agx_assign_regs(BITSET_WORD *used_regs, unsigned count, unsigned align, unsigned max)
77
{
78
for (unsigned reg = 0; reg < max; reg += align) {
79
bool conflict = false;
80
81
for (unsigned j = 0; j < count; ++j)
82
conflict |= BITSET_TEST(used_regs, reg + j);
83
84
if (!conflict) {
85
for (unsigned j = 0; j < count; ++j)
86
BITSET_SET(used_regs, reg + j);
87
88
return reg;
89
}
90
}
91
92
unreachable("Could not find a free register");
93
}
94
95
/** Assign registers to SSA values in a block. */
96
97
static void
98
agx_ra_assign_local(agx_block *block, uint8_t *ssa_to_reg, unsigned max_reg)
99
{
100
BITSET_DECLARE(used_regs, AGX_NUM_REGS) = { 0 };
101
102
agx_foreach_predecessor(block, pred) {
103
for (unsigned i = 0; i < BITSET_WORDS(AGX_NUM_REGS); ++i)
104
used_regs[i] |= pred->regs_out[i];
105
}
106
107
BITSET_SET(used_regs, 0); // control flow writes r0l
108
BITSET_SET(used_regs, 5*2); // TODO: precolouring, don't overwrite vertex ID
109
BITSET_SET(used_regs, (5*2 + 1));
110
111
agx_foreach_instr_in_block(block, I) {
112
/* First, free killed sources */
113
agx_foreach_src(I, s) {
114
if (I->src[s].type == AGX_INDEX_NORMAL && I->src[s].kill) {
115
unsigned reg = ssa_to_reg[I->src[s].value];
116
unsigned count = agx_read_registers(I, s);
117
118
for (unsigned i = 0; i < count; ++i)
119
BITSET_CLEAR(used_regs, reg + i);
120
}
121
}
122
123
/* Next, assign destinations. Always legal in SSA form. */
124
agx_foreach_dest(I, d) {
125
if (I->dest[d].type == AGX_INDEX_NORMAL) {
126
unsigned count = agx_write_registers(I, d);
127
unsigned align = (I->dest[d].size == AGX_SIZE_16) ? 1 : 2;
128
unsigned reg = agx_assign_regs(used_regs, count, align, max_reg);
129
130
ssa_to_reg[I->dest[d].value] = reg;
131
}
132
}
133
}
134
135
STATIC_ASSERT(sizeof(block->regs_out) == sizeof(used_regs));
136
memcpy(block->regs_out, used_regs, sizeof(used_regs));
137
}
138
139
void
140
agx_ra(agx_context *ctx)
141
{
142
unsigned *alloc = calloc(ctx->alloc, sizeof(unsigned));
143
144
agx_compute_liveness(ctx);
145
uint8_t *ssa_to_reg = calloc(ctx->alloc, sizeof(uint8_t));
146
agx_foreach_block(ctx, block)
147
agx_ra_assign_local(block, ssa_to_reg, ctx->max_register);
148
149
/* TODO: Coalesce combines */
150
151
agx_foreach_instr_global_safe(ctx, ins) {
152
/* Lower away RA pseudo-instructions */
153
if (ins->op == AGX_OPCODE_P_COMBINE) {
154
/* TODO: Optimize out the moves! */
155
assert(ins->dest[0].type == AGX_INDEX_NORMAL);
156
enum agx_size common_size = ins->dest[0].size;
157
unsigned base = ssa_to_reg[ins->dest[0].value];
158
unsigned size = common_size == AGX_SIZE_32 ? 2 : 1;
159
160
/* Move the sources */
161
agx_builder b = agx_init_builder(ctx, agx_after_instr(ins));
162
163
/* TODO: Eliminate the intermediate copy by handling parallel copies */
164
for (unsigned i = 0; i < 4; ++i) {
165
if (agx_is_null(ins->src[i])) continue;
166
unsigned base = ins->src[i].value;
167
if (ins->src[i].type == AGX_INDEX_NORMAL)
168
base = ssa_to_reg[base];
169
else
170
assert(ins->src[i].type == AGX_INDEX_REGISTER);
171
172
assert(ins->src[i].size == common_size);
173
174
agx_mov_to(&b, agx_register(124*2 + (i * size), common_size),
175
agx_register(base, common_size));
176
}
177
178
for (unsigned i = 0; i < 4; ++i) {
179
if (agx_is_null(ins->src[i])) continue;
180
agx_index src = ins->src[i];
181
182
if (src.type == AGX_INDEX_NORMAL)
183
src = agx_register(alloc[src.value], src.size);
184
185
agx_mov_to(&b, agx_register(base + (i * size), common_size),
186
agx_register(124*2 + (i * size), common_size));
187
}
188
189
/* We've lowered away, delete the old */
190
agx_remove_instruction(ins);
191
continue;
192
} else if (ins->op == AGX_OPCODE_P_EXTRACT) {
193
assert(ins->dest[0].type == AGX_INDEX_NORMAL);
194
assert(ins->dest[0].size == ins->src[0].size);
195
unsigned base = ins->src[0].value;
196
197
if (ins->src[0].type != AGX_INDEX_REGISTER) {
198
assert(ins->src[0].type == AGX_INDEX_NORMAL);
199
base = alloc[base];
200
}
201
202
unsigned size = ins->dest[0].size == AGX_SIZE_32 ? 2 : 1;
203
unsigned left = ssa_to_reg[ins->dest[0].value];
204
unsigned right = ssa_to_reg[ins->src[0].value] + (size * ins->imm);
205
206
if (left != right) {
207
agx_builder b = agx_init_builder(ctx, agx_after_instr(ins));
208
agx_mov_to(&b, agx_register(left, ins->dest[0].size),
209
agx_register(right, ins->src[0].size));
210
}
211
212
agx_remove_instruction(ins);
213
continue;
214
}
215
216
agx_foreach_src(ins, s) {
217
if (ins->src[s].type == AGX_INDEX_NORMAL) {
218
unsigned v = ssa_to_reg[ins->src[s].value];
219
ins->src[s] = agx_replace_index(ins->src[s], agx_register(v, ins->src[s].size));
220
}
221
}
222
223
agx_foreach_dest(ins, d) {
224
if (ins->dest[d].type == AGX_INDEX_NORMAL) {
225
unsigned v = ssa_to_reg[ins->dest[d].value];
226
ins->dest[d] = agx_replace_index(ins->dest[d], agx_register(v, ins->dest[d].size));
227
}
228
}
229
}
230
231
free(ssa_to_reg);
232
free(alloc);
233
}
234
235