v3d: Drop a perf note about merging unpack_half_*, which has been implemented.
[mesa.git] / src / broadcom / compiler / vir_lower_uniforms.c
1 /*
2 * Copyright © 2014 Broadcom
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
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21 * IN THE SOFTWARE.
22 */
23
24 /**
25 * @file v3d_vir_lower_uniforms.c
26 *
27 * This is the pre-code-generation pass for fixing up instructions that try to
28 * read from multiple uniform values.
29 */
30
31 #include "v3d_compiler.h"
32 #include "util/hash_table.h"
33 #include "util/u_math.h"
34
35 static inline uint32_t
36 index_hash(const void *key)
37 {
38 return (uintptr_t)key;
39 }
40
41 static inline bool
42 index_compare(const void *a, const void *b)
43 {
44 return a == b;
45 }
46
47 static void
48 add_uniform(struct hash_table *ht, struct qreg reg)
49 {
50 struct hash_entry *entry;
51 void *key = (void *)(uintptr_t)(reg.index + 1);
52
53 entry = _mesa_hash_table_search(ht, key);
54 if (entry) {
55 entry->data++;
56 } else {
57 _mesa_hash_table_insert(ht, key, (void *)(uintptr_t)1);
58 }
59 }
60
61 static void
62 remove_uniform(struct hash_table *ht, struct qreg reg)
63 {
64 struct hash_entry *entry;
65 void *key = (void *)(uintptr_t)(reg.index + 1);
66
67 entry = _mesa_hash_table_search(ht, key);
68 assert(entry);
69 entry->data = (void *)(((uintptr_t) entry->data) - 1);
70 if (entry->data == NULL)
71 _mesa_hash_table_remove(ht, entry);
72 }
73
74 static bool
75 is_lowerable_uniform(struct qinst *inst, int i)
76 {
77 if (inst->src[i].file != QFILE_UNIF)
78 return false;
79 return i != vir_get_implicit_uniform_src(inst);
80 }
81
82 /* Returns the number of different uniform values referenced by the
83 * instruction.
84 */
85 static uint32_t
86 vir_get_instruction_uniform_count(struct qinst *inst)
87 {
88 uint32_t count = 0;
89
90 for (int i = 0; i < vir_get_nsrc(inst); i++) {
91 if (inst->src[i].file != QFILE_UNIF)
92 continue;
93
94 bool is_duplicate = false;
95 for (int j = 0; j < i; j++) {
96 if (inst->src[j].file == QFILE_UNIF &&
97 inst->src[j].index == inst->src[i].index) {
98 is_duplicate = true;
99 break;
100 }
101 }
102 if (!is_duplicate)
103 count++;
104 }
105
106 return count;
107 }
108
109 void
110 vir_lower_uniforms(struct v3d_compile *c)
111 {
112 struct hash_table *ht =
113 _mesa_hash_table_create(c, index_hash, index_compare);
114
115 /* Walk the instruction list, finding which instructions have more
116 * than one uniform referenced, and add those uniform values to the
117 * ht.
118 */
119 vir_for_each_inst_inorder(inst, c) {
120 uint32_t nsrc = vir_get_nsrc(inst);
121
122 if (vir_get_instruction_uniform_count(inst) <= 1)
123 continue;
124
125 for (int i = 0; i < nsrc; i++) {
126 if (is_lowerable_uniform(inst, i))
127 add_uniform(ht, inst->src[i]);
128 }
129 }
130
131 while (ht->entries) {
132 /* Find the most commonly used uniform in instructions that
133 * need a uniform lowered.
134 */
135 uint32_t max_count = 0;
136 uint32_t max_index = 0;
137 hash_table_foreach(ht, entry) {
138 uint32_t count = (uintptr_t)entry->data;
139 uint32_t index = (uintptr_t)entry->key - 1;
140 if (count > max_count) {
141 max_count = count;
142 max_index = index;
143 }
144 }
145
146 struct qreg unif = vir_reg(QFILE_UNIF, max_index);
147
148 /* Now, find the instructions using this uniform and make them
149 * reference a temp instead.
150 */
151 vir_for_each_block(block, c) {
152 struct qreg temp = c->undef;
153
154 vir_for_each_inst(inst, block) {
155 uint32_t nsrc = vir_get_nsrc(inst);
156
157 uint32_t count = vir_get_instruction_uniform_count(inst);
158
159 if (count <= 1)
160 continue;
161
162 bool removed = false;
163 for (int i = 0; i < nsrc; i++) {
164 if (is_lowerable_uniform(inst, i) &&
165 inst->src[i].index == max_index) {
166 /* If the block doesn't have a
167 * load of the uniform yet,
168 * add it now. We could
169 * potentially do better and
170 * CSE MOVs from multiple
171 * blocks into dominating
172 * blocks, except that may
173 * cause troubles for register
174 * allocation.
175 */
176 if (temp.file == QFILE_NULL) {
177 c->cursor =
178 vir_before_inst(inst);
179 temp = vir_MOV(c, unif);
180 }
181
182 inst->src[i] = temp;
183 remove_uniform(ht, unif);
184 removed = true;
185 }
186 }
187 if (removed)
188 count--;
189
190 /* If the instruction doesn't need lowering any more,
191 * then drop it from the list.
192 */
193 if (count <= 1) {
194 for (int i = 0; i < nsrc; i++) {
195 if (is_lowerable_uniform(inst, i))
196 remove_uniform(ht, inst->src[i]);
197 }
198 }
199 }
200 }
201 }
202
203 _mesa_hash_table_destroy(ht, NULL);
204 }