c29802d9ba1eb1dc270e5200188fa447db1a6c1b
[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 struct iris_compiled_shader *
87 iris_find_cached_shader(struct iris_context *ice,
88 enum iris_program_cache_id cache_id,
89 uint32_t key_size,
90 const void *key)
91 {
92 struct keybox *keybox = make_keybox(NULL, cache_id, key, key_size);
93 struct hash_entry *entry =
94 _mesa_hash_table_search(ice->shaders.cache, keybox);
95
96 ralloc_free(keybox);
97
98 return entry ? entry->data : NULL;
99 }
100
101 const void *
102 iris_find_previous_compile(const struct iris_context *ice,
103 enum iris_program_cache_id cache_id,
104 unsigned program_string_id)
105 {
106 hash_table_foreach(ice->shaders.cache, entry) {
107 const struct keybox *keybox = entry->key;
108 const struct brw_base_prog_key *key = (const void *)keybox->data;
109 if (keybox->cache_id == cache_id &&
110 key->program_string_id == program_string_id) {
111 return keybox->data;
112 }
113 }
114
115 return NULL;
116 }
117
118 void
119 iris_delete_shader_variants(struct iris_context *ice,
120 struct iris_uncompiled_shader *ish)
121 {
122 struct hash_table *cache = ice->shaders.cache;
123 gl_shader_stage stage = ish->nir->info.stage;
124 enum iris_program_cache_id cache_id = stage;
125
126 hash_table_foreach(cache, entry) {
127 const struct keybox *keybox = entry->key;
128 const struct brw_base_prog_key *key = (const void *)keybox->data;
129
130 if (keybox->cache_id == cache_id &&
131 key->program_string_id == ish->program_id) {
132 struct iris_compiled_shader *shader = entry->data;
133
134 _mesa_hash_table_remove(cache, entry);
135
136 /* Shader variants may still be bound in the context even after
137 * the API-facing shader has been deleted. In particular, a draw
138 * may not have triggered iris_update_compiled_shaders() yet. In
139 * that case, we may be referring to that shader's VUE map, stream
140 * output settings, and so on. We also like to compare the old and
141 * new shader programs when swapping them out to flag dirty state.
142 *
143 * So, it's hazardous to delete a bound shader variant. We avoid
144 * doing so, choosing to instead move "deleted" shader variants to
145 * a list, deferring the actual deletion until they're not bound.
146 *
147 * For simplicity, we always move deleted variants to the list,
148 * even if we could delete them immediately. We'll then process
149 * the list, catching both these variants and any others.
150 */
151 list_addtail(&shader->link, &ice->shaders.deleted_variants[stage]);
152 }
153 }
154
155 /* Process any pending deferred variant deletions. */
156 list_for_each_entry_safe(struct iris_compiled_shader, shader,
157 &ice->shaders.deleted_variants[stage], link) {
158 /* If the shader is still bound, defer deletion. */
159 if (ice->shaders.prog[stage] == shader)
160 continue;
161
162 list_del(&shader->link);
163
164 /* Actually delete the variant. */
165 pipe_resource_reference(&shader->assembly.res, NULL);
166 ralloc_free(shader);
167 }
168 }
169
170
171 /**
172 * Look for an existing entry in the cache that has identical assembly code.
173 *
174 * This is useful for programs generating shaders at runtime, where multiple
175 * distinct shaders (from an API perspective) may compile to the same assembly
176 * in our backend. This saves space in the program cache buffer.
177 */
178 static const struct iris_compiled_shader *
179 find_existing_assembly(struct hash_table *cache,
180 const void *assembly,
181 unsigned assembly_size)
182 {
183 hash_table_foreach(cache, entry) {
184 const struct iris_compiled_shader *existing = entry->data;
185 if (existing->prog_data->program_size == assembly_size &&
186 memcmp(existing->map, assembly, assembly_size) == 0)
187 return existing;
188 }
189 return NULL;
190 }
191
192 struct iris_compiled_shader *
193 iris_upload_shader(struct iris_context *ice,
194 enum iris_program_cache_id cache_id,
195 uint32_t key_size,
196 const void *key,
197 const void *assembly,
198 struct brw_stage_prog_data *prog_data,
199 uint32_t *streamout,
200 enum brw_param_builtin *system_values,
201 unsigned num_system_values,
202 unsigned kernel_input_size,
203 unsigned num_cbufs,
204 const struct iris_binding_table *bt)
205 {
206 struct hash_table *cache = ice->shaders.cache;
207 struct iris_screen *screen = (struct iris_screen *)ice->ctx.screen;
208 struct iris_compiled_shader *shader =
209 rzalloc_size(cache, sizeof(struct iris_compiled_shader) +
210 screen->vtbl.derived_program_state_size(cache_id));
211 const struct iris_compiled_shader *existing =
212 find_existing_assembly(cache, assembly, prog_data->program_size);
213
214 /* If we can find a matching prog in the cache already, then reuse the
215 * existing stuff without creating new copy into the underlying buffer
216 * object. This is notably useful for programs generating shaders at
217 * runtime, where multiple shaders may compile to the same thing in our
218 * backend.
219 */
220 if (existing) {
221 pipe_resource_reference(&shader->assembly.res, existing->assembly.res);
222 shader->assembly.offset = existing->assembly.offset;
223 shader->map = existing->map;
224 } else {
225 shader->assembly.res = NULL;
226 u_upload_alloc(ice->shaders.uploader, 0, prog_data->program_size, 64,
227 &shader->assembly.offset, &shader->assembly.res,
228 &shader->map);
229 memcpy(shader->map, assembly, prog_data->program_size);
230 }
231
232 list_inithead(&shader->link);
233
234 shader->prog_data = prog_data;
235 shader->streamout = streamout;
236 shader->system_values = system_values;
237 shader->num_system_values = num_system_values;
238 shader->kernel_input_size = kernel_input_size;
239 shader->num_cbufs = num_cbufs;
240 shader->bt = *bt;
241
242 ralloc_steal(shader, shader->prog_data);
243 ralloc_steal(shader->prog_data, prog_data->param);
244 ralloc_steal(shader->prog_data, prog_data->pull_param);
245 ralloc_steal(shader, shader->streamout);
246 ralloc_steal(shader, shader->system_values);
247
248 /* Store the 3DSTATE shader packets and other derived state. */
249 screen->vtbl.store_derived_program_state(ice, cache_id, shader);
250
251 struct keybox *keybox = make_keybox(shader, cache_id, key, key_size);
252 _mesa_hash_table_insert(ice->shaders.cache, keybox, shader);
253
254 return shader;
255 }
256
257 bool
258 iris_blorp_lookup_shader(struct blorp_batch *blorp_batch,
259 const void *key, uint32_t key_size,
260 uint32_t *kernel_out, void *prog_data_out)
261 {
262 struct blorp_context *blorp = blorp_batch->blorp;
263 struct iris_context *ice = blorp->driver_ctx;
264 struct iris_batch *batch = blorp_batch->driver_batch;
265 struct iris_compiled_shader *shader =
266 iris_find_cached_shader(ice, IRIS_CACHE_BLORP, key_size, key);
267
268 if (!shader)
269 return false;
270
271 struct iris_bo *bo = iris_resource_bo(shader->assembly.res);
272 *kernel_out =
273 iris_bo_offset_from_base_address(bo) + shader->assembly.offset;
274 *((void **) prog_data_out) = shader->prog_data;
275
276 iris_use_pinned_bo(batch, bo, false, IRIS_DOMAIN_NONE);
277
278 return true;
279 }
280
281 bool
282 iris_blorp_upload_shader(struct blorp_batch *blorp_batch, uint32_t stage,
283 const void *key, uint32_t key_size,
284 const void *kernel, UNUSED uint32_t kernel_size,
285 const struct brw_stage_prog_data *prog_data_templ,
286 UNUSED uint32_t prog_data_size,
287 uint32_t *kernel_out, void *prog_data_out)
288 {
289 struct blorp_context *blorp = blorp_batch->blorp;
290 struct iris_context *ice = blorp->driver_ctx;
291 struct iris_batch *batch = blorp_batch->driver_batch;
292
293 void *prog_data = ralloc_size(NULL, prog_data_size);
294 memcpy(prog_data, prog_data_templ, prog_data_size);
295
296 struct iris_binding_table bt;
297 memset(&bt, 0, sizeof(bt));
298
299 struct iris_compiled_shader *shader =
300 iris_upload_shader(ice, IRIS_CACHE_BLORP, key_size, key, kernel,
301 prog_data, NULL, NULL, 0, 0, 0, &bt);
302
303 struct iris_bo *bo = iris_resource_bo(shader->assembly.res);
304 *kernel_out =
305 iris_bo_offset_from_base_address(bo) + shader->assembly.offset;
306 *((void **) prog_data_out) = shader->prog_data;
307
308 iris_use_pinned_bo(batch, bo, false, IRIS_DOMAIN_NONE);
309
310 return true;
311 }
312
313 void
314 iris_init_program_cache(struct iris_context *ice)
315 {
316 ice->shaders.cache =
317 _mesa_hash_table_create(ice, keybox_hash, keybox_equals);
318
319 ice->shaders.uploader =
320 u_upload_create(&ice->ctx, 16384, PIPE_BIND_CUSTOM, PIPE_USAGE_IMMUTABLE,
321 IRIS_RESOURCE_FLAG_SHADER_MEMZONE);
322
323 for (int i = 0; i < MESA_SHADER_STAGES; i++)
324 list_inithead(&ice->shaders.deleted_variants[i]);
325 }
326
327 void
328 iris_destroy_program_cache(struct iris_context *ice)
329 {
330 for (int i = 0; i < MESA_SHADER_STAGES; i++) {
331 ice->shaders.prog[i] = NULL;
332
333 list_for_each_entry_safe(struct iris_compiled_shader, shader,
334 &ice->shaders.deleted_variants[i], link) {
335 pipe_resource_reference(&shader->assembly.res, NULL);
336 }
337 }
338
339 hash_table_foreach(ice->shaders.cache, entry) {
340 struct iris_compiled_shader *shader = entry->data;
341 pipe_resource_reference(&shader->assembly.res, NULL);
342 }
343
344 u_upload_destroy(ice->shaders.uploader);
345
346 ralloc_free(ice->shaders.cache);
347 }
348
349 static const char *
350 cache_name(enum iris_program_cache_id cache_id)
351 {
352 if (cache_id == IRIS_CACHE_BLORP)
353 return "BLORP";
354
355 return _mesa_shader_stage_to_string(cache_id);
356 }
357
358 void
359 iris_print_program_cache(struct iris_context *ice)
360 {
361 struct iris_screen *screen = (struct iris_screen *)ice->ctx.screen;
362 const struct gen_device_info *devinfo = &screen->devinfo;
363
364 hash_table_foreach(ice->shaders.cache, entry) {
365 const struct keybox *keybox = entry->key;
366 struct iris_compiled_shader *shader = entry->data;
367 fprintf(stderr, "%s:\n", cache_name(keybox->cache_id));
368 brw_disassemble_with_labels(devinfo, shader->map, 0,
369 shader->prog_data->program_size, stderr);
370 }
371 }