• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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
index_hash(const void * key)36 index_hash(const void *key)
37 {
38         return (uintptr_t)key;
39 }
40 
41 static inline bool
index_compare(const void * a,const void * b)42 index_compare(const void *a, const void *b)
43 {
44         return a == b;
45 }
46 
47 static void
add_uniform(struct hash_table * ht,struct qreg reg)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
remove_uniform(struct hash_table * ht,struct qreg reg)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
is_lowerable_uniform(struct qinst * inst,int i)75 is_lowerable_uniform(struct qinst *inst, int i)
76 {
77         if (inst->src[i].file != QFILE_UNIF)
78                 return false;
79         if (vir_has_implicit_uniform(inst))
80                 return i != vir_get_implicit_uniform_src(inst);
81         return true;
82 }
83 
84 /* Returns the number of different uniform values referenced by the
85  * instruction.
86  */
87 static uint32_t
vir_get_instruction_uniform_count(struct qinst * inst)88 vir_get_instruction_uniform_count(struct qinst *inst)
89 {
90         uint32_t count = 0;
91 
92         for (int i = 0; i < vir_get_nsrc(inst); i++) {
93                 if (inst->src[i].file != QFILE_UNIF)
94                         continue;
95 
96                 bool is_duplicate = false;
97                 for (int j = 0; j < i; j++) {
98                         if (inst->src[j].file == QFILE_UNIF &&
99                             inst->src[j].index == inst->src[i].index) {
100                                 is_duplicate = true;
101                                 break;
102                         }
103                 }
104                 if (!is_duplicate)
105                         count++;
106         }
107 
108         return count;
109 }
110 
111 void
vir_lower_uniforms(struct v3d_compile * c)112 vir_lower_uniforms(struct v3d_compile *c)
113 {
114         struct hash_table *ht =
115                 _mesa_hash_table_create(c, index_hash, index_compare);
116 
117         /* Walk the instruction list, finding which instructions have more
118          * than one uniform referenced, and add those uniform values to the
119          * ht.
120          */
121         vir_for_each_inst_inorder(inst, c) {
122                 uint32_t nsrc = vir_get_nsrc(inst);
123 
124                 if (vir_get_instruction_uniform_count(inst) <= 1)
125                         continue;
126 
127                 for (int i = 0; i < nsrc; i++) {
128                         if (is_lowerable_uniform(inst, i))
129                                 add_uniform(ht, inst->src[i]);
130                 }
131         }
132 
133         while (ht->entries) {
134                 /* Find the most commonly used uniform in instructions that
135                  * need a uniform lowered.
136                  */
137                 uint32_t max_count = 0;
138                 uint32_t max_index = 0;
139                 struct hash_entry *entry;
140                 hash_table_foreach(ht, entry) {
141                         uint32_t count = (uintptr_t)entry->data;
142                         uint32_t index = (uintptr_t)entry->key - 1;
143                         if (count > max_count) {
144                                 max_count = count;
145                                 max_index = index;
146                         }
147                 }
148 
149                 struct qreg unif = vir_reg(QFILE_UNIF, max_index);
150 
151                 /* Now, find the instructions using this uniform and make them
152                  * reference a temp instead.
153                  */
154                 vir_for_each_block(block, c) {
155                         struct qinst *mov = NULL;
156 
157                         vir_for_each_inst(inst, block) {
158                                 uint32_t nsrc = vir_get_nsrc(inst);
159 
160                                 uint32_t count = vir_get_instruction_uniform_count(inst);
161 
162                                 if (count <= 1)
163                                         continue;
164 
165                                 /* If the block doesn't have a load of the
166                                  * uniform yet, add it.  We could potentially
167                                  * do better and CSE MOVs from multiple blocks
168                                  * into dominating blocks, except that may
169                                  * cause troubles for register allocation.
170                                  */
171                                 if (!mov) {
172                                         mov = vir_mul_inst(V3D_QPU_M_MOV,
173                                                            vir_get_temp(c),
174                                                            unif, c->undef);
175                                         list_add(&mov->link,
176                                                  &block->instructions);
177                                         c->defs[mov->dst.index] = mov;
178                                 }
179 
180                                 bool removed = false;
181                                 for (int i = 0; i < nsrc; i++) {
182                                         if (is_lowerable_uniform(inst, i) &&
183                                             inst->src[i].index == max_index) {
184                                                 inst->src[i].file =
185                                                         mov->dst.file;
186                                                 inst->src[i].index =
187                                                         mov->dst.index;
188                                                 remove_uniform(ht, unif);
189                                                 removed = true;
190                                         }
191                                 }
192                                 if (removed)
193                                         count--;
194 
195                                 /* If the instruction doesn't need lowering any more,
196                                  * then drop it from the list.
197                                  */
198                                 if (count <= 1) {
199                                         for (int i = 0; i < nsrc; i++) {
200                                                 if (is_lowerable_uniform(inst, i))
201                                                         remove_uniform(ht, inst->src[i]);
202                                         }
203                                 }
204                         }
205                 }
206         }
207 
208         _mesa_hash_table_destroy(ht, NULL);
209 }
210