Debian Codegraph @

src/imagination/pco/pco.c

raw
1 /*
2 * Copyright © 2024 Imagination Technologies Ltd.
3 *
4 * SPDX-License-Identifier: MIT
5 */
6
7 /**
8 * \file pco.c
9 *
10 * \brief Main compiler interface.
11 */
12
13 #include "compiler/glsl_types.h"
14 #include "nir_serialize.h"
15 #include "pco.h"
16 #include "pco_internal.h"
17 #include "util/blob.h"
18 #include "util/hash_table.h"
19 #include "util/list.h"
20 #include "util/macros.h"
21 #include "util/ralloc.h"
22 #include "util/shader_stats.h"
23
24 #include <assert.h>
25 #include <stdbool.h>
26
27 /**
28 * \brief PCO compiler context destructor.
29 *
30 * \param[in,out] ptr PCO compiler context pointer.
31 */
32 static void pco_ctx_destructor(UNUSED void *ptr)
33 {
34 glsl_type_singleton_decref();
35 }
36
37 /**
38 * \brief Allocates and sets up a PCO compiler context.
39 *
40 * \param[in] dev_info Device info.
41 * \param[in] mem_ctx Ralloc memory allocation context.
42 * \return The PCO compiler context, or NULL on failure.
43 */
44 pco_ctx *pco_ctx_create(const struct pvr_device_info *dev_info, void *mem_ctx)
45 {
46 pco_ctx *ctx = rzalloc_size(mem_ctx, sizeof(*ctx));
47
48 ctx->dev_info = dev_info;
49
50 pco_debug_init();
51
52 #ifndef NDEBUG
53 /* Ensure NIR debug variables are processed. */
54 nir_process_debug_variable();
55 #endif /* NDEBUG */
56
57 glsl_type_singleton_init_or_ref();
58 ralloc_set_destructor(ctx, pco_ctx_destructor);
59
60 return ctx;
61 }
62
63 void pco_ctx_setup_usclib(pco_ctx *ctx, const void *data, unsigned size)
64 {
65 struct blob_reader blob_reader;
66 blob_reader_init(&blob_reader, data, size);
67 ctx->usclib = nir_deserialize(ctx, pco_nir_options(), &blob_reader);
68 }
69
70 /**
71 * \brief Updates the device info for a PCO compiler context.
72 *
73 * \param[in,out] ctx PCO compiler context.
74 * \param[in] dev_info Device info.
75 */
76 void pco_ctx_update_dev_info(pco_ctx *ctx,
77 const struct pvr_device_info *dev_info)
78 {
79 ctx->dev_info = dev_info;
80 }
81
82 /**
83 * \brief Allocates and sets up a PCO shader from a NIR shader.
84 *
85 * \param[in] ctx PCO compiler context.
86 * \param[in] nir The NIR shader.
87 * \return The PCO shader, or NULL on failure.
88 */
89 pco_shader *pco_shader_create(pco_ctx *ctx, nir_shader *nir, void *mem_ctx)
90 {
91 pco_shader *shader = rzalloc_size(mem_ctx, sizeof(*shader));
92
93 shader->ctx = ctx;
94 shader->nir = nir;
95 shader->stage = nir->info.stage;
96 shader->name = ralloc_strdup(shader, nir->info.name);
97 shader->is_internal = nir->info.internal;
98 shader->is_grouped = false;
99 list_inithead(&shader->funcs);
100
101 return shader;
102 }
103
104 /**
105 * \brief Sets up a PCO cf node.
106 *
107 * \param[in,out] cf_node PCO cf node.
108 * \param[in] type CF node type.
109 */
110 static inline void init_cf_node(pco_cf_node *cf_node,
111 enum pco_cf_node_type type)
112 {
113 cf_node->type = type;
114 cf_node->parent = NULL;
115 }
116
117 /**
118 * \brief Allocates and sets up a PCO function.
119 *
120 * \param[in,out] shader PCO shader.
121 * \param[in] type The function type.
122 * \param[in] num_params The number of parameters.
123 * \return The PCO function, or NULL on failure.
124 */
125 pco_func *pco_func_create(pco_shader *shader,
126 enum pco_func_type type,
127 unsigned num_params)
128 {
129 pco_func *func = rzalloc_size(shader, sizeof(*func));
130 pco_func *preamble = pco_preamble(shader);
131
132 /* Add the function to the shader; preamble goes first, then entrypoint.
133 * The rest of the functions will get appended.
134 */
135 if (type == PCO_FUNC_TYPE_PREAMBLE) {
136 assert(!preamble);
137 list_add(&func->link, &shader->funcs);
138 } else if (type == PCO_FUNC_TYPE_ENTRYPOINT) {
139 assert(!pco_entrypoint(shader));
140 if (!preamble)
141 list_add(&func->link, &shader->funcs);
142 else
143 list_add(&func->link, &preamble->link);
144 } else {
145 list_addtail(&func->link, &shader->funcs);
146 }
147
148 init_cf_node(&func->cf_node, PCO_CF_NODE_TYPE_FUNC);
149 func->parent_shader = shader;
150 func->type = type;
151 func->index = shader->next_func++;
152
153 list_inithead(&func->body);
154
155 func->num_params = num_params;
156 if (num_params) {
157 func->params =
158 rzalloc_array_size(func, sizeof(*func->params), num_params);
159 }
160
161 func->vec_infos = _mesa_hash_table_u64_create(func);
162
163 func->enc_offset = ~0U;
164
165 return func;
166 }
167
168 /**
169 * \brief Allocates and sets up a PCO block.
170 *
171 * \param[in,out] func Parent function.
172 * \return The PCO block, or NULL on failure.
173 */
174 pco_block *pco_block_create(pco_func *func)
175 {
176 pco_block *block = rzalloc_size(func, sizeof(*block));
177
178 init_cf_node(&block->cf_node, PCO_CF_NODE_TYPE_BLOCK);
179 block->parent_func = func;
180 list_inithead(&block->instrs);
181 block->index = func->next_block++;
182
183 return block;
184 }
185
186 /**
187 * \brief Allocates and sets up a PCO if construct.
188 *
189 * \param[in,out] func Parent function.
190 * \return The PCO if construct, or NULL on failure.
191 */
192 pco_if *pco_if_create(pco_func *func)
193 {
194 pco_if *pif = rzalloc_size(func, sizeof(*pif));
195
196 init_cf_node(&pif->cf_node, PCO_CF_NODE_TYPE_IF);
197 pif->parent_func = func;
198 list_inithead(&pif->prologue);
199 list_inithead(&pif->then_body);
200 list_inithead(&pif->interlogue);
201 list_inithead(&pif->else_body);
202 list_inithead(&pif->epilogue);
203 pif->index = func->next_if++;
204
205 return pif;
206 }
207
208 /**
209 * \brief Allocates and sets up a PCO loop.
210 *
211 * \param[in,out] func Parent function.
212 * \return The PCO loop, or NULL on failure.
213 */
214 pco_loop *pco_loop_create(pco_func *func)
215 {
216 pco_loop *loop = rzalloc_size(func, sizeof(*loop));
217
218 init_cf_node(&loop->cf_node, PCO_CF_NODE_TYPE_LOOP);
219 loop->parent_func = func;
220 list_inithead(&loop->prologue);
221 list_inithead(&loop->body);
222 list_inithead(&loop->interlogue);
223 list_inithead(&loop->epilogue);
224 loop->index = func->next_loop++;
225
226 return loop;
227 }
228
229 /**
230 * \brief Allocates and sets up a PCO instruction.
231 *
232 * \param[in,out] func Parent function.
233 * \param[in] num_dests Number of destinations.
234 * \param[in] num_srcs Number of sources.
235 * \return The PCO instruction, or NULL on failure.
236 */
237 pco_instr *
238 pco_instr_create(pco_func *func, unsigned num_dests, unsigned num_srcs)
239 {
240 pco_instr *instr;
241 unsigned size = sizeof(*instr);
242 size += num_dests * sizeof(*instr->dest);
243 size += num_srcs * sizeof(*instr->src);
244
245 instr = rzalloc_size(func, size);
246
247 instr->parent_func = func;
248
249 instr->num_dests = num_dests;
250 instr->dest = (pco_ref *)(instr + 1);
251
252 instr->num_srcs = num_srcs;
253 instr->src = instr->dest + num_dests;
254
255 list_inithead(&instr->phi_srcs);
256
257 instr->index = func->next_instr++;
258
259 return instr;
260 }
261
262 /**
263 * \brief Allocates and sets up a PCO instruction group.
264 *
265 * \param[in,out] func Parent function.
266 * \return The PCO instruction group, or NULL on failure.
267 */
268 pco_igrp *pco_igrp_create(pco_func *func)
269 {
270 pco_igrp *igrp = rzalloc_size(func, sizeof(*igrp));
271
272 igrp->parent_func = func;
273 igrp->index = func->next_igrp++;
274
275 return igrp;
276 }
277
278 /**
279 * \brief Deletes a PCO instruction.
280 *
281 * \param[in,out] instr PCO instruction.
282 */
283 void pco_instr_delete(pco_instr *instr)
284 {
285 list_del(&instr->link);
286 ralloc_free(instr);
287 }
288
289 /**
290 * \brief Returns the shader data.
291 *
292 * \param[in] shader PCO shader.
293 */
294 pco_data *pco_shader_data(pco_shader *shader)
295 {
296 return &shader->data;
297 }
298
299 /**
300 * \brief Returns precompilation data for a shader.
301 *
302 * \param[in] shader PCO shader.
303 * \return The precompilation data.
304 */
305 pco_precomp_data pco_get_precomp_data(pco_shader *shader)
306 {
307 assert(pco_shader_binary_size(shader));
308
309 unsigned size_dwords = pco_shader_binary_size(shader) / sizeof(uint32_t);
310 assert(size_dwords <= UINT16_MAX);
311
312 return (pco_precomp_data){
313 .temps = shader->data.common.temps,
314 .vtxins = shader->data.common.vtxins,
315 .coeffs = shader->data.common.coeffs,
316 .shareds = shader->data.common.shareds,
317 .size_dwords = size_dwords,
318 };
319 }
320
321 /**
322 * \brief Returns statistics for a shader.
323 *
324 * \param[in] shader PCO shader.
325 * \return The shader statistics.
326 */
327 struct pvr_stats pco_get_pvr_stats(pco_shader *shader)
328 {
329 assert(shader->is_grouped);
330
331 unsigned shader_size = pco_shader_binary_size(shader);
332 assert(shader_size > 0);
333
334 unsigned loop_count = 0;
335 unsigned igrp_count = 0;
336
337 unsigned main_count = 0;
338 unsigned bitwise_count = 0;
339 unsigned control_count = 0;
340
341 pco_foreach_func_in_shader (func, shader) {
342 pco_foreach_loop_in_func (loop, func) {
343 loop_count++;
344 }
345 pco_foreach_igrp_in_func (igrp, func) {
346 igrp_count++;
347 switch (igrp->hdr.alutype) {
348 case PCO_ALUTYPE_MAIN:
349 main_count++;
350 break;
351
352 case PCO_ALUTYPE_BITWISE:
353 bitwise_count++;
354 break;
355
356 case PCO_ALUTYPE_CONTROL:
357 control_count++;
358 break;
359
360 default:
361 UNREACHABLE("Invalid pco_alutype");
362 }
363 }
364 }
365
366 return (struct pvr_stats){
367 .isa = PVR_STAT_ROGUE,
368 .rogue = (struct rogue_stats) {
369 .code_size = shader_size,
370 .scratch_size = shader->data.common.scratch,
371 .spill_count = shader->data.common.spilled_temps,
372 .temp_count = shader->data.common.temps,
373 .vtxin_count = shader->data.common.vtxins,
374 .loop_count = loop_count,
375 .inst_group_count = igrp_count,
376 .main_inst_group_count = main_count,
377 .bitwise_inst_group_count = bitwise_count,
378 .control_inst_group_count = control_count,
379 },
380 };
381 }
382
383 enum pco_pck_format pco_pipe_to_pck_format(enum pipe_format format,
384 bool *scale,
385 bool *roundzero,
386 bool *split)
387 {
388 enum pco_pck_format pck_format = ~0;
389 *scale = false;
390 *roundzero = false;
391 *split = false;
392
393 switch (format) {
394 case PIPE_FORMAT_R8_UNORM:
395 case PIPE_FORMAT_R8G8_UNORM:
396 case PIPE_FORMAT_R8G8B8_UNORM:
397 case PIPE_FORMAT_R8G8B8A8_UNORM:
398 pck_format = PCO_PCK_FORMAT_U8888;
399 *scale = true;
400 break;
401
402 case PIPE_FORMAT_R8_SNORM:
403 case PIPE_FORMAT_R8G8_SNORM:
404 case PIPE_FORMAT_R8G8B8_SNORM:
405 case PIPE_FORMAT_R8G8B8A8_SNORM:
406 pck_format = PCO_PCK_FORMAT_S8888;
407 *scale = true;
408 break;
409
410 case PIPE_FORMAT_R11G11B10_FLOAT:
411 pck_format = PCO_PCK_FORMAT_F111110;
412 break;
413
414 /* TODO: better way to do the 1x2 component. */
415 case PIPE_FORMAT_R10G10B10A2_UNORM:
416 pck_format = PCO_PCK_FORMAT_U1010102;
417 *scale = true;
418 break;
419
420 /* TODO: better way to do the 1x2 component. */
421 case PIPE_FORMAT_R10G10B10A2_SNORM:
422 pck_format = PCO_PCK_FORMAT_S1010102;
423 *scale = true;
424 break;
425
426 case PIPE_FORMAT_R16_FLOAT:
427 case PIPE_FORMAT_R16G16_FLOAT:
428 case PIPE_FORMAT_R16G16B16_FLOAT:
429 case PIPE_FORMAT_R16G16B16A16_FLOAT:
430 pck_format = PCO_PCK_FORMAT_F16F16;
431 *split = true;
432 break;
433
434 case PIPE_FORMAT_R16_UNORM:
435 case PIPE_FORMAT_R16G16_UNORM:
436 case PIPE_FORMAT_R16G16B16_UNORM:
437 case PIPE_FORMAT_R16G16B16A16_UNORM:
438 pck_format = PCO_PCK_FORMAT_U1616;
439 *scale = true;
440 *split = true;
441 break;
442
443 case PIPE_FORMAT_R16_SNORM:
444 case PIPE_FORMAT_R16G16_SNORM:
445 case PIPE_FORMAT_R16G16B16_SNORM:
446 case PIPE_FORMAT_R16G16B16A16_SNORM:
447 pck_format = PCO_PCK_FORMAT_S1616;
448 *scale = true;
449 *split = true;
450 break;
451
452 default:
453 break;
454 }
455
456 return pck_format;
457 }