summaryrefslogtreecommitdiff
path: root/src/freedreno/ir3/ir3_spill.c
blob: 1205dcc5b4b5453d446a24f24d3e22c82da5e8cc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
/*
 * Copyright (C) 2021 Valve Corporation
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the "Software"),
 * to deal in the Software without restriction, including without limitation
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
 * and/or sell copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice (including the next
 * paragraph) shall be included in all copies or substantial portions of the
 * Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */

#include "ir3_ra.h"
#include "ir3_shader.h"
#include "util/rb_tree.h"

/*
 * This pass does one thing so far:
 *
 * 1. Calculates the maximum register pressure. To do this, we need to use the
 * exact same technique that RA uses for combining meta_split instructions
 * with their sources, so that our calculation agrees with RA.
 *
 * It will also optionally spill registers once that's implemented.
 */

struct ra_spill_interval {
	struct ir3_reg_interval interval;
};

struct ra_spill_ctx {
	struct ir3_reg_ctx reg_ctx;

	struct ra_spill_interval *intervals;

	struct ir3_pressure cur_pressure, max_pressure;

	struct ir3_liveness *live;

	const struct ir3_compiler *compiler;
};

static void
ra_spill_interval_init(struct ra_spill_interval *interval, struct ir3_register *reg)
{
	ir3_reg_interval_init(&interval->interval, reg);
}

static void
ra_pressure_add(struct ir3_pressure *pressure, struct ra_spill_interval *interval)
{
	unsigned size = reg_size(interval->interval.reg);
	if (interval->interval.reg->flags & IR3_REG_SHARED)
		pressure->shared += size;
	else if (interval->interval.reg->flags & IR3_REG_HALF)
		pressure->half += size;
	else
		pressure->full += size;
}

static void
ra_pressure_sub(struct ir3_pressure *pressure, struct ra_spill_interval *interval)
{
	unsigned size = reg_size(interval->interval.reg);
	if (interval->interval.reg->flags & IR3_REG_SHARED)
		pressure->shared -= size;
	else if (interval->interval.reg->flags & IR3_REG_HALF)
		pressure->half -= size;
	else
		pressure->full -= size;
}

static struct ra_spill_interval *
ir3_reg_interval_to_interval(struct ir3_reg_interval *interval)
{
	return rb_node_data(struct ra_spill_interval, interval, interval);
}

static struct ra_spill_ctx *
ir3_reg_ctx_to_ctx(struct ir3_reg_ctx *ctx)
{
	return rb_node_data(struct ra_spill_ctx, ctx, reg_ctx);
}

static void
interval_add(struct ir3_reg_ctx *_ctx, struct ir3_reg_interval *_interval)
{
	struct ra_spill_interval *interval = ir3_reg_interval_to_interval(_interval);
	struct ra_spill_ctx *ctx = ir3_reg_ctx_to_ctx(_ctx);

	ra_pressure_add(&ctx->cur_pressure, interval);
}

static void
interval_delete(struct ir3_reg_ctx *_ctx, struct ir3_reg_interval *_interval)
{
	struct ra_spill_interval *interval = ir3_reg_interval_to_interval(_interval);
	struct ra_spill_ctx *ctx = ir3_reg_ctx_to_ctx(_ctx);

	ra_pressure_sub(&ctx->cur_pressure, interval);
}

static void
interval_readd(struct ir3_reg_ctx *_ctx, struct ir3_reg_interval *_parent,
			   struct ir3_reg_interval *_child)
{
	interval_add(_ctx, _child);
}

static void
spill_ctx_init(struct ra_spill_ctx *ctx)
{
	rb_tree_init(&ctx->reg_ctx.intervals);
	ctx->reg_ctx.interval_add = interval_add;
	ctx->reg_ctx.interval_delete = interval_delete;
	ctx->reg_ctx.interval_readd = interval_readd;
}

static void
ra_spill_ctx_insert(struct ra_spill_ctx *ctx, struct ra_spill_interval *interval)
{
	ir3_reg_interval_insert(&ctx->reg_ctx, &interval->interval);
}

static void
ra_spill_ctx_remove(struct ra_spill_ctx *ctx, struct ra_spill_interval *interval)
{
	ir3_reg_interval_remove(&ctx->reg_ctx, &interval->interval);
}

static void
init_dst(struct ra_spill_ctx *ctx, struct ir3_register *dst)
{
	struct ra_spill_interval *interval = &ctx->intervals[dst->name];
	ra_spill_interval_init(interval, dst);
}

static void
insert_dst(struct ra_spill_ctx *ctx, struct ir3_register *dst)
{
	struct ra_spill_interval *interval = &ctx->intervals[dst->name];
	if (interval->interval.inserted)
		return;

	ra_spill_ctx_insert(ctx, interval);

	/* For precolored inputs, make sure we leave enough registers to allow for
	 * holes in the inputs. It can happen that the binning shader has a lower
	 * register pressure than the main shader, but the main shader decided to
	 * add holes between the inputs which means that the binning shader has a
	 * higher register demand.
	 */
	if (dst->instr->opc == OPC_META_INPUT &&
		dst->num != INVALID_REG) {
		physreg_t physreg = ra_reg_get_physreg(dst);
		physreg_t max = physreg + reg_size(dst);

		if (interval->interval.reg->flags & IR3_REG_SHARED)
			ctx->max_pressure.shared = MAX2(ctx->max_pressure.shared, max);
		else if (interval->interval.reg->flags & IR3_REG_HALF)
			ctx->max_pressure.half = MAX2(ctx->max_pressure.half, max);
		else
			ctx->max_pressure.full = MAX2(ctx->max_pressure.full, max);
	}
}

static void
remove_src_early(struct ra_spill_ctx *ctx, struct ir3_instruction *instr, struct ir3_register *src)
{
	if (!(src->flags & IR3_REG_FIRST_KILL))
		return;

	struct ra_spill_interval *interval = &ctx->intervals[src->def->name];

	if (!interval->interval.inserted || interval->interval.parent ||
		!rb_tree_is_empty(&interval->interval.children))
		return;

	ra_spill_ctx_remove(ctx, interval);
}

static void
remove_src(struct ra_spill_ctx *ctx, struct ir3_instruction *instr, struct ir3_register *src)
{
	if (!(src->flags & IR3_REG_FIRST_KILL))
		return;

	struct ra_spill_interval *interval = &ctx->intervals[src->def->name];

	if (!interval->interval.inserted)
		return;

	ra_spill_ctx_remove(ctx, interval);
}

static void
remove_dst(struct ra_spill_ctx *ctx, struct ir3_register *dst)
{
	struct ra_spill_interval *interval = &ctx->intervals[dst->name];

	if (!interval->interval.inserted)
		return;

	ra_spill_ctx_remove(ctx, interval);
}

static void
update_max_pressure(struct ra_spill_ctx *ctx)
{
	d("pressure:");
	d("\tfull: %u", ctx->cur_pressure.full);
	d("\thalf: %u", ctx->cur_pressure.half);
	d("\tshared: %u", ctx->cur_pressure.shared);

	ctx->max_pressure.full =
		MAX2(ctx->max_pressure.full, ctx->cur_pressure.full);
	ctx->max_pressure.half =
		MAX2(ctx->max_pressure.half, ctx->cur_pressure.half);
	ctx->max_pressure.shared =
		MAX2(ctx->max_pressure.shared, ctx->cur_pressure.shared);
}

static void
handle_instr(struct ra_spill_ctx *ctx, struct ir3_instruction *instr)
{
	if (RA_DEBUG) {
		printf("processing: ");
		ir3_print_instr(instr);
	}

	ra_foreach_dst(dst, instr) {
		init_dst(ctx, dst);
	}

	/* Handle tied destinations. If a destination is tied to a source and that
	 * source is live-through, then we need to allocate a new register for the
	 * destination which is live-through itself and cannot overlap the
	 * sources.
	 */

	ra_foreach_dst(dst, instr) {
		struct ir3_register *tied_src = dst->tied;
		if (tied_src && !(tied_src->flags & IR3_REG_FIRST_KILL))
			insert_dst(ctx, dst);
	}

	update_max_pressure(ctx);

	ra_foreach_src(src, instr) {
		if (src->flags & IR3_REG_FIRST_KILL)
			remove_src_early(ctx, instr, src);
	}


	ra_foreach_dst(dst, instr) {
		insert_dst(ctx, dst);
	}

	update_max_pressure(ctx);

	for (unsigned i = 0; i < instr->regs_count; i++) {
		if (ra_reg_is_src(instr->regs[i]) && 
			(instr->regs[i]->flags & IR3_REG_FIRST_KILL))
			remove_src(ctx, instr, instr->regs[i]);
		else if (ra_reg_is_dst(instr->regs[i]) &&
				 (instr->regs[i]->flags & IR3_REG_UNUSED))
			remove_dst(ctx, instr->regs[i]);
	}
}

static void
handle_input_phi(struct ra_spill_ctx *ctx, struct ir3_instruction *instr)
{
	init_dst(ctx, instr->regs[0]);
	insert_dst(ctx, instr->regs[0]);
}

static void
remove_input_phi(struct ra_spill_ctx *ctx, struct ir3_instruction *instr)
{
	ra_foreach_src(src, instr)
		remove_src(ctx, instr, src);
	if (instr->regs[0]->flags & IR3_REG_UNUSED)
		remove_dst(ctx, instr->regs[0]);
}

static void
handle_live_in(struct ra_spill_ctx *ctx, struct ir3_register *def)
{
	struct ra_spill_interval *interval = &ctx->intervals[def->name];
	ra_spill_interval_init(interval, def);
	insert_dst(ctx, def);
}

static void
handle_block(struct ra_spill_ctx *ctx, struct ir3_block *block)
{
	memset(&ctx->cur_pressure, 0, sizeof(ctx->cur_pressure));
	rb_tree_init(&ctx->reg_ctx.intervals);

	unsigned name;
	BITSET_FOREACH_SET(name, ctx->live->live_in[block->index],
					   ctx->live->definitions_count) {
		struct ir3_register *reg = ctx->live->definitions[name];
		handle_live_in(ctx, reg);
	}

	foreach_instr (instr, &block->instr_list) {
		if (instr->opc != OPC_META_PHI && instr->opc != OPC_META_INPUT &&
			instr->opc != OPC_META_TEX_PREFETCH)
			break;
		handle_input_phi(ctx, instr);
	}

	update_max_pressure(ctx);

	foreach_instr (instr, &block->instr_list) {
		if (instr->opc == OPC_META_PHI || instr->opc == OPC_META_INPUT ||
			instr->opc == OPC_META_TEX_PREFETCH)
			remove_input_phi(ctx, instr);
		else
			handle_instr(ctx, instr);
	}
}

void
ir3_calc_pressure(struct ir3_shader_variant *v, struct ir3_liveness *live,
				  struct ir3_pressure *max_pressure)
{
	struct ra_spill_ctx ctx = {};
	ctx.live = live;
	ctx.intervals = calloc(live->definitions_count, sizeof(*ctx.intervals));
	ctx.compiler = v->shader->compiler;
	spill_ctx_init(&ctx);

	foreach_block (block, &v->ir->block_list) {
		handle_block(&ctx, block);
	}

	assert(ctx.cur_pressure.full == 0);
	assert(ctx.cur_pressure.half == 0);
	assert(ctx.cur_pressure.shared == 0);

	free(ctx.intervals);

	*max_pressure = ctx.max_pressure;
}