iris: Use a uint16_t for key sizes
[mesa.git] / src / gallium / drivers / iris / iris_program_cache.c
1 /*
2 * Copyright © 2017 Intel Corporation
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 shall be included
12 * in all copies or substantial portions of the Software.
13 *
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
15 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
17 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
19 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
20 * DEALINGS IN THE SOFTWARE.
21 */
22
23 /**
24 * @file iris_program_cache.c
25 *
26 * The in-memory program cache. This is basically a hash table mapping
27 * API-specified shaders and a state key to a compiled variant. It also
28 * takes care of uploading shader assembly into a BO for use on the GPU.
29 */
30
31 #include <stdio.h>
32 #include <errno.h>
33 #include "pipe/p_defines.h"
34 #include "pipe/p_state.h"
35 #include "pipe/p_context.h"
36 #include "pipe/p_screen.h"
37 #include "util/u_atomic.h"
38 #include "util/u_upload_mgr.h"
39 #include "compiler/nir/nir.h"
40 #include "compiler/nir/nir_builder.h"
41 #include "intel/compiler/brw_compiler.h"
42 #include "intel/compiler/brw_eu.h"
43 #include "intel/compiler/brw_nir.h"
44 #include "iris_context.h"
45 #include "iris_resource.h"
46
47 struct keybox {
48 uint16_t size;
49 enum iris_program_cache_id cache_id;
50 uint8_t data[0];
51 };
52
53 static struct keybox *
54 make_keybox(void *mem_ctx,
55 enum iris_program_cache_id cache_id,
56 const void *key,
57 uint32_t key_size)
58 {
59 struct keybox *keybox =
60 ralloc_size(mem_ctx, sizeof(struct keybox) + key_size);
61
62 keybox->cache_id = cache_id;
63 keybox->size = key_size;
64 memcpy(keybox->data, key, key_size);
65
66 return keybox;
67 }
68
69 static uint32_t
70 keybox_hash(const void *void_key)
71 {
72 const struct keybox *key = void_key;
73 return _mesa_hash_data(&key->cache_id, key->size + sizeof(key->cache_id));
74 }
75
76 static bool
77 keybox_equals(const void *void_a, const void *void_b)
78 {
79 const struct keybox *a = void_a, *b = void_b;
80 if (a->size != b->size)
81 return false;
82
83 return memcmp(a->data, b->data, a->size) == 0;
84 }
85
86 static unsigned
87 get_program_string_id(enum iris_program_cache_id cache_id, const void *key)
88 {
89 switch (cache_id) {
90 case IRIS_CACHE_VS:
91 return ((struct brw_vs_prog_key *) key)->program_string_id;
92 case IRIS_CACHE_TCS:
93 return ((struct brw_tcs_prog_key *) key)->program_string_id;
94 case IRIS_CACHE_TES:
95 return ((struct brw_tes_prog_key *) key)->program_string_id;
96 case IRIS_CACHE_GS:
97 return ((struct brw_gs_prog_key *) key)->program_string_id;
98 case IRIS_CACHE_CS:
99 return ((struct brw_cs_prog_key *) key)->program_string_id;
100 case IRIS_CACHE_FS:
101 return ((struct brw_wm_prog_key *) key)->program_string_id;
102 default:
103 unreachable("no program string id for this kind of program");
104 }
105 }
106
107 struct iris_compiled_shader *
108 iris_find_cached_shader(struct iris_context *ice,
109 enum iris_program_cache_id cache_id,
110 uint32_t key_size,
111 const void *key)
112 {
113 struct keybox *keybox =
114 make_keybox(ice->shaders.cache, cache_id, key, key_size);
115 struct hash_entry *entry =
116 _mesa_hash_table_search(ice->shaders.cache, keybox);
117
118 ralloc_free(keybox);
119
120 return entry ? entry->data : NULL;
121 }
122
123 const void *
124 iris_find_previous_compile(const struct iris_context *ice,
125 enum iris_program_cache_id cache_id,
126 unsigned program_string_id)
127 {
128 hash_table_foreach(ice->shaders.cache, entry) {
129 const struct keybox *keybox = entry->key;
130 if (keybox->cache_id == cache_id &&
131 get_program_string_id(cache_id, keybox->data) == program_string_id) {
132 return keybox->data;
133 }
134 }
135
136 return NULL;
137 }
138
139 /**
140 * Look for an existing entry in the cache that has identical assembly code.
141 *
142 * This is useful for programs generating shaders at runtime, where multiple
143 * distinct shaders (from an API perspective) may compile to the same assembly
144 * in our backend. This saves space in the program cache buffer.
145 */
146 static const struct iris_compiled_shader *
147 find_existing_assembly(struct hash_table *cache,
148 const void *assembly,
149 unsigned assembly_size)
150 {
151 hash_table_foreach(cache, entry) {
152 const struct iris_compiled_shader *existing = entry->data;
153 if (existing->prog_data->program_size == assembly_size &&
154 memcmp(existing->map, assembly, assembly_size) == 0)
155 return existing;
156 }
157 return NULL;
158 }
159
160 struct iris_compiled_shader *
161 iris_upload_shader(struct iris_context *ice,
162 enum iris_program_cache_id cache_id,
163 uint32_t key_size,
164 const void *key,
165 const void *assembly,
166 struct brw_stage_prog_data *prog_data,
167 uint32_t *streamout,
168 enum brw_param_builtin *system_values,
169 unsigned num_system_values,
170 unsigned num_cbufs,
171 const struct iris_binding_table *bt)
172 {
173 struct hash_table *cache = ice->shaders.cache;
174 struct iris_compiled_shader *shader =
175 rzalloc_size(cache, sizeof(struct iris_compiled_shader) +
176 ice->vtbl.derived_program_state_size(cache_id));
177 const struct iris_compiled_shader *existing =
178 find_existing_assembly(cache, assembly, prog_data->program_size);
179
180 /* If we can find a matching prog in the cache already, then reuse the
181 * existing stuff without creating new copy into the underlying buffer
182 * object. This is notably useful for programs generating shaders at
183 * runtime, where multiple shaders may compile to the same thing in our
184 * backend.
185 */
186 if (existing) {
187 pipe_resource_reference(&shader->assembly.res, existing->assembly.res);
188 shader->assembly.offset = existing->assembly.offset;
189 shader->map = existing->map;
190 } else {
191 shader->assembly.res = NULL;
192 u_upload_alloc(ice->shaders.uploader, 0, prog_data->program_size, 64,
193 &shader->assembly.offset, &shader->assembly.res,
194 &shader->map);
195 memcpy(shader->map, assembly, prog_data->program_size);
196 }
197
198 shader->prog_data = prog_data;
199 shader->streamout = streamout;
200 shader->system_values = system_values;
201 shader->num_system_values = num_system_values;
202 shader->num_cbufs = num_cbufs;
203 shader->bt = *bt;
204
205 ralloc_steal(shader, shader->prog_data);
206 ralloc_steal(shader->prog_data, prog_data->param);
207 ralloc_steal(shader->prog_data, prog_data->pull_param);
208 ralloc_steal(shader, shader->streamout);
209 ralloc_steal(shader, shader->system_values);
210
211 /* Store the 3DSTATE shader packets and other derived state. */
212 ice->vtbl.store_derived_program_state(ice, cache_id, shader);
213
214 struct keybox *keybox = make_keybox(cache, cache_id, key, key_size);
215 _mesa_hash_table_insert(ice->shaders.cache, keybox, shader);
216
217 return shader;
218 }
219
220 bool
221 iris_blorp_lookup_shader(struct blorp_batch *blorp_batch,
222 const void *key, uint32_t key_size,
223 uint32_t *kernel_out, void *prog_data_out)
224 {
225 struct blorp_context *blorp = blorp_batch->blorp;
226 struct iris_context *ice = blorp->driver_ctx;
227 struct iris_batch *batch = blorp_batch->driver_batch;
228 struct iris_compiled_shader *shader =
229 iris_find_cached_shader(ice, IRIS_CACHE_BLORP, key_size, key);
230
231 if (!shader)
232 return false;
233
234 struct iris_bo *bo = iris_resource_bo(shader->assembly.res);
235 *kernel_out =
236 iris_bo_offset_from_base_address(bo) + shader->assembly.offset;
237 *((void **) prog_data_out) = shader->prog_data;
238
239 iris_use_pinned_bo(batch, bo, false);
240
241 return true;
242 }
243
244 bool
245 iris_blorp_upload_shader(struct blorp_batch *blorp_batch,
246 const void *key, uint32_t key_size,
247 const void *kernel, UNUSED uint32_t kernel_size,
248 const struct brw_stage_prog_data *prog_data_templ,
249 UNUSED uint32_t prog_data_size,
250 uint32_t *kernel_out, void *prog_data_out)
251 {
252 struct blorp_context *blorp = blorp_batch->blorp;
253 struct iris_context *ice = blorp->driver_ctx;
254 struct iris_batch *batch = blorp_batch->driver_batch;
255
256 void *prog_data = ralloc_size(NULL, prog_data_size);
257 memcpy(prog_data, prog_data_templ, prog_data_size);
258
259 struct iris_binding_table bt;
260 memset(&bt, 0, sizeof(bt));
261
262 struct iris_compiled_shader *shader =
263 iris_upload_shader(ice, IRIS_CACHE_BLORP, key_size, key, kernel,
264 prog_data, NULL, NULL, 0, 0, &bt);
265
266 struct iris_bo *bo = iris_resource_bo(shader->assembly.res);
267 *kernel_out =
268 iris_bo_offset_from_base_address(bo) + shader->assembly.offset;
269 *((void **) prog_data_out) = shader->prog_data;
270
271 iris_use_pinned_bo(batch, bo, false);
272
273 return true;
274 }
275
276 void
277 iris_init_program_cache(struct iris_context *ice)
278 {
279 ice->shaders.cache =
280 _mesa_hash_table_create(ice, keybox_hash, keybox_equals);
281
282 ice->shaders.uploader =
283 u_upload_create(&ice->ctx, 16384, PIPE_BIND_CUSTOM, PIPE_USAGE_IMMUTABLE,
284 IRIS_RESOURCE_FLAG_SHADER_MEMZONE);
285 }
286
287 void
288 iris_destroy_program_cache(struct iris_context *ice)
289 {
290 for (int i = 0; i < MESA_SHADER_STAGES; i++) {
291 ice->shaders.prog[i] = NULL;
292 }
293
294 hash_table_foreach(ice->shaders.cache, entry) {
295 struct iris_compiled_shader *shader = entry->data;
296 pipe_resource_reference(&shader->assembly.res, NULL);
297 }
298
299 u_upload_destroy(ice->shaders.uploader);
300
301 ralloc_free(ice->shaders.cache);
302 }
303
304 static const char *
305 cache_name(enum iris_program_cache_id cache_id)
306 {
307 if (cache_id == IRIS_CACHE_BLORP)
308 return "BLORP";
309
310 return _mesa_shader_stage_to_string(cache_id);
311 }
312
313 void
314 iris_print_program_cache(struct iris_context *ice)
315 {
316 struct iris_screen *screen = (struct iris_screen *)ice->ctx.screen;
317 const struct gen_device_info *devinfo = &screen->devinfo;
318
319 hash_table_foreach(ice->shaders.cache, entry) {
320 const struct keybox *keybox = entry->key;
321 struct iris_compiled_shader *shader = entry->data;
322 fprintf(stderr, "%s:\n", cache_name(keybox->cache_id));
323 brw_disassemble(devinfo, shader->map, 0,
324 shader->prog_data->program_size, stderr);
325 }
326 }