1 /*
2 * Copyright © 2024 Imagination Technologies Ltd.
3 *
4 * SPDX-License-Identifier: MIT
5 */
6
7 /**
8 * \file pco_index.c
9 *
10 * \brief PCO indexing pass.
11 */
12
13 #include "pco.h"
14 #include "pco_internal.h"
15 #include "util/hash_table.h"
16 #include "util/macros.h"
17 #include "util/ralloc.h"
18
19 #include <assert.h>
20 #include <stdbool.h>
21
22 /**
23 * \brief Indexes all shader child structures.
24 *
25 * \param[in,out] shader PCO shader.
26 * \param[in] skip_ssa Whether to skip SSA indexing.
27 * \return True if the pass made progress.
28 */
pco_index(pco_shader * shader,bool skip_ssa)29 bool pco_index(pco_shader *shader, bool skip_ssa)
30 {
31 /* TODO */
32 if (shader->is_grouped)
33 return false;
34
35 shader->next_func = 0;
36 pco_foreach_func_in_shader (func, shader) {
37 unsigned *ssa_idx_map = NULL;
38 if (!skip_ssa) {
39 ssa_idx_map =
40 rzalloc_array_size(NULL, sizeof(*ssa_idx_map), func->next_ssa);
41 }
42
43 func->index = shader->next_func++;
44 func->next_instr = 0;
45 func->next_block = 0;
46
47 struct hash_table_u64 *vec_infos = NULL;
48 if (!skip_ssa) {
49 func->next_ssa = 0;
50 vec_infos = _mesa_hash_table_u64_create(func);
51 }
52
53 pco_foreach_block_in_func (block, func) {
54 block->index = func->next_block++;
55 pco_foreach_instr_in_block (instr, block) {
56 instr->index = func->next_instr++;
57 if (!skip_ssa) {
58 pco_foreach_instr_dest_ssa (pdest, instr) {
59 ssa_idx_map[pdest->val] = func->next_ssa++;
60 if (instr->op == PCO_OP_VEC) {
61 pco_vec_info *vec_info =
62 _mesa_hash_table_u64_search(func->vec_infos,
63 pdest->val);
64
65 ralloc_steal(vec_infos, vec_info);
66
67 _mesa_hash_table_u64_insert(vec_infos,
68 ssa_idx_map[pdest->val],
69 vec_info);
70 }
71 pdest->val = ssa_idx_map[pdest->val];
72 }
73 }
74 }
75 }
76
77 if (!skip_ssa) {
78 pco_foreach_instr_in_func (instr, func) {
79 pco_foreach_instr_src_ssa (psrc, instr) {
80 psrc->val = ssa_idx_map[psrc->val];
81 }
82 }
83 }
84
85 /* TODO: */
86 /* pco_foreach_if_in_func */
87 /* pco_foreach_loop_in_func */
88
89 if (!skip_ssa) {
90 _mesa_hash_table_u64_destroy(func->vec_infos);
91 func->vec_infos = vec_infos;
92 ralloc_free(ssa_idx_map);
93 }
94 }
95
96 return true;
97 }
98