iris: more blorp
[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 * on the rights to use, copy, modify, merge, publish, distribute, sub
8 * license, and/or sell copies of the Software, and to permit persons to whom
9 * the 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 NON-INFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHOR(S) AND/OR THEIR SUPPLIERS BE LIABLE FOR ANY CLAIM,
19 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
20 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
21 * USE OR OTHER DEALINGS IN THE SOFTWARE.
22 */
23 #include <stdio.h>
24 #include <errno.h>
25 #include "pipe/p_defines.h"
26 #include "pipe/p_state.h"
27 #include "pipe/p_context.h"
28 #include "pipe/p_screen.h"
29 #include "util/u_atomic.h"
30 #include "util/u_upload_mgr.h"
31 #include "compiler/nir/nir.h"
32 #include "compiler/nir/nir_builder.h"
33 #include "intel/compiler/brw_compiler.h"
34 #include "intel/compiler/brw_eu.h"
35 #include "intel/compiler/brw_nir.h"
36 #include "iris_context.h"
37 #include "iris_resource.h"
38
39 struct keybox {
40 uint8_t size;
41 enum iris_program_cache_id cache_id;
42 uint8_t data[0];
43 };
44
45 static uint32_t
46 key_size_for_cache(enum iris_program_cache_id cache_id)
47 {
48 static const unsigned key_sizes[] = {
49 [IRIS_CACHE_VS] = sizeof(struct brw_vs_prog_key),
50 [IRIS_CACHE_TCS] = sizeof(struct brw_tcs_prog_key),
51 [IRIS_CACHE_TES] = sizeof(struct brw_tes_prog_key),
52 [IRIS_CACHE_GS] = sizeof(struct brw_gs_prog_key),
53 [IRIS_CACHE_FS] = sizeof(struct brw_wm_prog_key),
54 [IRIS_CACHE_CS] = sizeof(struct brw_cs_prog_key),
55 };
56
57 /* BLORP keys aren't all the same size. */
58 assert(cache_id != IRIS_CACHE_BLORP);
59
60 return key_sizes[cache_id];
61 }
62
63 static struct keybox *
64 make_keybox(void *mem_ctx,
65 enum iris_program_cache_id cache_id,
66 const void *key,
67 uint32_t key_size)
68 {
69 struct keybox *keybox =
70 ralloc_size(mem_ctx, sizeof(struct keybox) + key_size);
71
72 keybox->cache_id = cache_id;
73 keybox->size = key_size;
74 memcpy(keybox->data, key, key_size);
75
76 return keybox;
77 }
78
79 static uint32_t
80 keybox_hash(const void *void_key)
81 {
82 const struct keybox *key = void_key;
83 return _mesa_hash_data(&key->cache_id, key->size + sizeof(key->cache_id));
84 }
85
86 static bool
87 keybox_equals(const void *void_a, const void *void_b)
88 {
89 const struct keybox *a = void_a, *b = void_b;
90 if (a->size != b->size)
91 return false;
92
93 return memcmp(a->data, b->data, a->size) == 0;
94 }
95
96 static uint64_t
97 dirty_flag_for_cache(enum iris_program_cache_id cache_id)
98 {
99 assert(cache_id <= MESA_SHADER_STAGES);
100 return IRIS_DIRTY_VS << cache_id;
101 }
102
103 static unsigned
104 get_program_string_id(enum iris_program_cache_id cache_id, const void *key)
105 {
106 switch (cache_id) {
107 case IRIS_CACHE_VS:
108 return ((struct brw_vs_prog_key *) key)->program_string_id;
109 case IRIS_CACHE_TCS:
110 return ((struct brw_tcs_prog_key *) key)->program_string_id;
111 case IRIS_CACHE_TES:
112 return ((struct brw_tes_prog_key *) key)->program_string_id;
113 case IRIS_CACHE_GS:
114 return ((struct brw_gs_prog_key *) key)->program_string_id;
115 case IRIS_CACHE_CS:
116 return ((struct brw_cs_prog_key *) key)->program_string_id;
117 case IRIS_CACHE_FS:
118 return ((struct brw_wm_prog_key *) key)->program_string_id;
119 default:
120 unreachable("no program string id for this kind of program");
121 }
122 }
123
124 static struct iris_compiled_shader *
125 iris_find_cached_shader(struct iris_context *ice,
126 enum iris_program_cache_id cache_id,
127 const void *key,
128 uint32_t key_size)
129 {
130 struct keybox *keybox =
131 make_keybox(ice->shaders.cache, cache_id, key, key_size);
132 struct hash_entry *entry =
133 _mesa_hash_table_search(ice->shaders.cache, keybox);
134
135 return entry ? entry->data : NULL;
136 }
137
138 /**
139 * Looks for a program in the cache and binds it.
140 *
141 * If no program was found, returns false and leaves the binding alone.
142 */
143 bool
144 iris_bind_cached_shader(struct iris_context *ice,
145 enum iris_program_cache_id cache_id,
146 const void *key)
147 {
148 unsigned key_size = key_size_for_cache(cache_id);
149 struct iris_compiled_shader *shader =
150 iris_find_cached_shader(ice, cache_id, key, key_size);
151
152 if (!shader)
153 return false;
154
155 if (memcmp(shader, ice->shaders.prog[cache_id], sizeof(*shader)) != 0) {
156 ice->shaders.prog[cache_id] = shader;
157 ice->state.dirty |= dirty_flag_for_cache(cache_id);
158 }
159
160 return true;
161 }
162
163 const void *
164 iris_find_previous_compile(const struct iris_context *ice,
165 enum iris_program_cache_id cache_id,
166 unsigned program_string_id)
167 {
168 hash_table_foreach(ice->shaders.cache, entry) {
169 const struct keybox *keybox = entry->key;
170 if (keybox->cache_id == cache_id &&
171 get_program_string_id(cache_id, keybox->data) == program_string_id) {
172 return keybox->data;
173 }
174 }
175
176 return NULL;
177 }
178
179 /**
180 * Look for an existing entry in the cache that has identical assembly code.
181 *
182 * This is useful for programs generating shaders at runtime, where multiple
183 * distinct shaders (from an API perspective) may compile to the same assembly
184 * in our backend. This saves space in the program cache buffer.
185 */
186 static const struct iris_compiled_shader *
187 find_existing_assembly(struct hash_table *cache,
188 const void *assembly,
189 unsigned assembly_size)
190 {
191 hash_table_foreach(cache, entry) {
192 const struct iris_compiled_shader *existing = entry->data;
193 if (existing->prog_data->program_size == assembly_size &&
194 memcmp(existing->map, assembly, assembly_size) == 0)
195 return existing;
196 }
197 return NULL;
198 }
199
200 static struct iris_compiled_shader *
201 iris_upload_shader(struct iris_context *ice,
202 enum iris_program_cache_id cache_id,
203 uint32_t key_size,
204 const void *key,
205 const void *assembly,
206 const struct brw_stage_prog_data *prog_data)
207 {
208 struct iris_screen *screen = (void *) ice->ctx.screen;
209 struct gen_device_info *devinfo = &screen->devinfo;
210 struct hash_table *cache = ice->shaders.cache;
211 struct iris_compiled_shader *shader =
212 ralloc_size(cache, sizeof(struct iris_compiled_shader) +
213 ice->vtbl.derived_program_state_size(cache_id));
214 const struct iris_compiled_shader *existing =
215 find_existing_assembly(cache, assembly, prog_data->program_size);
216
217 /* If we can find a matching prog in the cache already, then reuse the
218 * existing stuff without creating new copy into the underlying buffer
219 * object. This is notably useful for programs generating shaders at
220 * runtime, where multiple shaders may compile to the same thing in our
221 * backend.
222 */
223 if (existing) {
224 shader->offset = existing->offset;
225 } else {
226 shader->buffer = NULL;
227 u_upload_alloc(ice->shaders.uploader, 0, prog_data->program_size,
228 64, &shader->offset, &shader->buffer, &shader->map);
229 memcpy(shader->map, assembly, prog_data->program_size);
230 }
231
232 shader->prog_data = prog_data;
233
234 /* Store the 3DSTATE shader packets and other derived state. */
235 ice->vtbl.set_derived_program_state(devinfo, cache_id, shader);
236
237 struct keybox *keybox = make_keybox(cache, cache_id, key, key_size);
238 _mesa_hash_table_insert(ice->shaders.cache, keybox, shader);
239
240 return shader;
241 }
242
243 /**
244 * Upload a new shader to the program cache, and bind it for use.
245 *
246 * \param prog_data must be ralloc'd and will be stolen.
247 */
248 void
249 iris_upload_and_bind_shader(struct iris_context *ice,
250 enum iris_program_cache_id cache_id,
251 const void *key,
252 const void *assembly,
253 struct brw_stage_prog_data *prog_data)
254 {
255 assert(cache_id != IRIS_CACHE_BLORP);
256
257 struct iris_compiled_shader *shader =
258 iris_upload_shader(ice, cache_id, key_size_for_cache(cache_id), key,
259 assembly, prog_data);
260
261 ralloc_steal(shader, shader->prog_data);
262 ralloc_steal(shader->prog_data, prog_data->param);
263 ralloc_steal(shader->prog_data, prog_data->pull_param);
264
265 ice->shaders.prog[cache_id] = shader;
266 ice->state.dirty |= dirty_flag_for_cache(cache_id);
267 }
268
269 bool
270 iris_blorp_lookup_shader(struct blorp_batch *blorp_batch,
271 const void *key, uint32_t key_size,
272 uint32_t *kernel_out, void *prog_data_out)
273 {
274 struct blorp_context *blorp = blorp_batch->blorp;
275 struct iris_context *ice = blorp->driver_ctx;
276 struct iris_batch *batch = blorp_batch->driver_batch;
277 struct iris_compiled_shader *shader =
278 iris_find_cached_shader(ice, IRIS_CACHE_BLORP, key, key_size);
279
280 if (!shader)
281 return false;
282
283 struct iris_bo *bo = iris_resource_bo(shader->buffer);
284 *kernel_out = iris_bo_offset_from_base_address(bo) + shader->offset;
285 *((void **) prog_data_out) = shader->prog_data;
286
287 iris_use_pinned_bo(batch, bo, false);
288
289 return true;
290 }
291
292 bool
293 iris_blorp_upload_shader(struct blorp_batch *blorp_batch,
294 const void *key, uint32_t key_size,
295 const void *kernel, UNUSED uint32_t kernel_size,
296 const struct brw_stage_prog_data *prog_data,
297 UNUSED uint32_t prog_data_size,
298 uint32_t *kernel_out, void *prog_data_out)
299 {
300 struct blorp_context *blorp = blorp_batch->blorp;
301 struct iris_context *ice = blorp->driver_ctx;
302 struct iris_batch *batch = blorp_batch->driver_batch;
303
304 struct iris_compiled_shader *shader =
305 iris_upload_shader(ice, IRIS_CACHE_BLORP, key_size, key, kernel,
306 prog_data);
307
308 struct iris_bo *bo = iris_resource_bo(shader->buffer);
309 *kernel_out = iris_bo_offset_from_base_address(bo) + shader->offset;
310 *((void **) prog_data_out) = shader->prog_data;
311
312 iris_use_pinned_bo(batch, bo, false);
313
314 return true;
315 }
316
317 void
318 iris_init_program_cache(struct iris_context *ice)
319 {
320 ice->shaders.cache =
321 _mesa_hash_table_create(ice, keybox_hash, keybox_equals);
322
323 ice->shaders.uploader =
324 u_upload_create(&ice->ctx, 16384, PIPE_BIND_CUSTOM, PIPE_USAGE_IMMUTABLE,
325 IRIS_RESOURCE_FLAG_SHADER_MEMZONE);
326 }
327
328 void
329 iris_destroy_program_cache(struct iris_context *ice)
330 {
331 for (int i = 0; i < MESA_SHADER_STAGES; i++) {
332 ice->shaders.prog[i] = NULL;
333 }
334
335 u_upload_destroy(ice->shaders.uploader);
336
337 ralloc_free(ice->shaders.cache);
338 }
339
340 static const char *
341 cache_name(enum iris_program_cache_id cache_id)
342 {
343 if (cache_id == IRIS_CACHE_BLORP)
344 return "BLORP";
345
346 return _mesa_shader_stage_to_string(cache_id);
347 }
348
349 void
350 iris_print_program_cache(struct iris_context *ice)
351 {
352 struct iris_screen *screen = (struct iris_screen *)ice->ctx.screen;
353 const struct gen_device_info *devinfo = &screen->devinfo;
354
355 hash_table_foreach(ice->shaders.cache, entry) {
356 const struct keybox *keybox = entry->key;
357 struct iris_compiled_shader *shader = entry->data;
358 fprintf(stderr, "%s:\n", cache_name(keybox->cache_id));
359 brw_disassemble(devinfo, shader->map, 0,
360 shader->prog_data->program_size, stderr);
361 }
362 }