2 * Copyright © 2017 Intel Corporation
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:
11 * The above copyright notice and this permission notice shall be included
12 * in all copies or substantial portions of the Software.
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.
24 * @file iris_program_cache.c
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.
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"
49 enum iris_program_cache_id cache_id
;
54 key_size_for_cache(enum iris_program_cache_id cache_id
)
56 static const unsigned key_sizes
[] = {
57 [IRIS_CACHE_VS
] = sizeof(struct brw_vs_prog_key
),
58 [IRIS_CACHE_TCS
] = sizeof(struct brw_tcs_prog_key
),
59 [IRIS_CACHE_TES
] = sizeof(struct brw_tes_prog_key
),
60 [IRIS_CACHE_GS
] = sizeof(struct brw_gs_prog_key
),
61 [IRIS_CACHE_FS
] = sizeof(struct brw_wm_prog_key
),
62 [IRIS_CACHE_CS
] = sizeof(struct brw_cs_prog_key
),
65 /* BLORP keys aren't all the same size. */
66 assert(cache_id
!= IRIS_CACHE_BLORP
);
68 return key_sizes
[cache_id
];
71 static struct keybox
*
72 make_keybox(void *mem_ctx
,
73 enum iris_program_cache_id cache_id
,
77 struct keybox
*keybox
=
78 ralloc_size(mem_ctx
, sizeof(struct keybox
) + key_size
);
80 keybox
->cache_id
= cache_id
;
81 keybox
->size
= key_size
;
82 memcpy(keybox
->data
, key
, key_size
);
88 keybox_hash(const void *void_key
)
90 const struct keybox
*key
= void_key
;
91 return _mesa_hash_data(&key
->cache_id
, key
->size
+ sizeof(key
->cache_id
));
95 keybox_equals(const void *void_a
, const void *void_b
)
97 const struct keybox
*a
= void_a
, *b
= void_b
;
98 if (a
->size
!= b
->size
)
101 return memcmp(a
->data
, b
->data
, a
->size
) == 0;
105 dirty_flag_for_cache(enum iris_program_cache_id cache_id
)
107 assert(cache_id
<= MESA_SHADER_STAGES
);
110 // XXX: move this flagging out to a higher level, allow comparison of
111 // XXX: new and old programs to decide what bits to twiddle
112 // XXX: CLIP: toggle if barycentric modes has any NONPERSPECTIVE or not
113 if (cache_id
== IRIS_CACHE_FS
)
114 return IRIS_DIRTY_WM
| IRIS_DIRTY_FS
| IRIS_DIRTY_CLIP
| IRIS_DIRTY_SBE
;
115 if (cache_id
== IRIS_CACHE_VS
)
116 return IRIS_DIRTY_VS
| IRIS_DIRTY_VF_SGVS
;
118 return IRIS_DIRTY_VS
<< cache_id
| IRIS_DIRTY_BINDINGS_VS
<< cache_id
;
122 get_program_string_id(enum iris_program_cache_id cache_id
, const void *key
)
126 return ((struct brw_vs_prog_key
*) key
)->program_string_id
;
128 return ((struct brw_tcs_prog_key
*) key
)->program_string_id
;
130 return ((struct brw_tes_prog_key
*) key
)->program_string_id
;
132 return ((struct brw_gs_prog_key
*) key
)->program_string_id
;
134 return ((struct brw_cs_prog_key
*) key
)->program_string_id
;
136 return ((struct brw_wm_prog_key
*) key
)->program_string_id
;
138 unreachable("no program string id for this kind of program");
142 static struct iris_compiled_shader
*
143 iris_find_cached_shader(struct iris_context
*ice
,
144 enum iris_program_cache_id cache_id
,
148 struct keybox
*keybox
=
149 make_keybox(ice
->shaders
.cache
, cache_id
, key
, key_size
);
150 struct hash_entry
*entry
=
151 _mesa_hash_table_search(ice
->shaders
.cache
, keybox
);
155 return entry
? entry
->data
: NULL
;
159 * Looks for a program in the cache and binds it.
161 * If no program was found, returns false and leaves the binding alone.
164 iris_bind_cached_shader(struct iris_context
*ice
,
165 enum iris_program_cache_id cache_id
,
168 unsigned key_size
= key_size_for_cache(cache_id
);
169 struct iris_compiled_shader
*shader
=
170 iris_find_cached_shader(ice
, cache_id
, key
, key_size
);
176 if (!ice
->shaders
.prog
[cache_id
] ||
177 memcmp(shader
, ice
->shaders
.prog
[cache_id
], sizeof(*shader
)) != 0) {
178 ice
->shaders
.prog
[cache_id
] = shader
;
179 ice
->state
.dirty
|= dirty_flag_for_cache(cache_id
);
186 iris_unbind_shader(struct iris_context
*ice
,
187 enum iris_program_cache_id cache_id
)
189 if (ice
->shaders
.prog
[cache_id
]) {
190 ice
->shaders
.prog
[cache_id
] = NULL
;
191 ice
->state
.dirty
|= dirty_flag_for_cache(cache_id
);
196 iris_find_previous_compile(const struct iris_context
*ice
,
197 enum iris_program_cache_id cache_id
,
198 unsigned program_string_id
)
200 hash_table_foreach(ice
->shaders
.cache
, entry
) {
201 const struct keybox
*keybox
= entry
->key
;
202 if (keybox
->cache_id
== cache_id
&&
203 get_program_string_id(cache_id
, keybox
->data
) == program_string_id
) {
212 * Look for an existing entry in the cache that has identical assembly code.
214 * This is useful for programs generating shaders at runtime, where multiple
215 * distinct shaders (from an API perspective) may compile to the same assembly
216 * in our backend. This saves space in the program cache buffer.
218 static const struct iris_compiled_shader
*
219 find_existing_assembly(struct hash_table
*cache
,
220 const void *assembly
,
221 unsigned assembly_size
)
223 hash_table_foreach(cache
, entry
) {
224 const struct iris_compiled_shader
*existing
= entry
->data
;
225 if (existing
->prog_data
->program_size
== assembly_size
&&
226 memcmp(existing
->map
, assembly
, assembly_size
) == 0)
232 static struct iris_compiled_shader
*
233 iris_upload_shader(struct iris_context
*ice
,
234 enum iris_program_cache_id cache_id
,
237 const void *assembly
,
238 struct brw_stage_prog_data
*prog_data
,
241 struct iris_screen
*screen
= (void *) ice
->ctx
.screen
;
242 struct gen_device_info
*devinfo
= &screen
->devinfo
;
243 struct hash_table
*cache
= ice
->shaders
.cache
;
244 struct iris_compiled_shader
*shader
=
245 rzalloc_size(cache
, sizeof(struct iris_compiled_shader
) +
246 ice
->vtbl
.derived_program_state_size(cache_id
));
247 const struct iris_compiled_shader
*existing
=
248 find_existing_assembly(cache
, assembly
, prog_data
->program_size
);
250 /* If we can find a matching prog in the cache already, then reuse the
251 * existing stuff without creating new copy into the underlying buffer
252 * object. This is notably useful for programs generating shaders at
253 * runtime, where multiple shaders may compile to the same thing in our
257 pipe_resource_reference(&shader
->assembly
.res
, existing
->assembly
.res
);
258 shader
->assembly
.offset
= existing
->assembly
.offset
;
259 shader
->map
= existing
->map
;
261 shader
->assembly
.res
= NULL
;
262 u_upload_alloc(ice
->shaders
.uploader
, 0, prog_data
->program_size
, 64,
263 &shader
->assembly
.offset
, &shader
->assembly
.res
,
265 memcpy(shader
->map
, assembly
, prog_data
->program_size
);
268 shader
->prog_data
= prog_data
;
269 shader
->streamout
= streamout
;
271 ralloc_steal(shader
, shader
->prog_data
);
272 ralloc_steal(shader
->prog_data
, prog_data
->param
);
273 ralloc_steal(shader
->prog_data
, prog_data
->pull_param
);
274 ralloc_steal(shader
, shader
->streamout
);
276 /* Store the 3DSTATE shader packets and other derived state. */
277 ice
->vtbl
.store_derived_program_state(devinfo
, cache_id
, shader
);
279 struct keybox
*keybox
= make_keybox(cache
, cache_id
, key
, key_size
);
280 _mesa_hash_table_insert(ice
->shaders
.cache
, keybox
, shader
);
286 * Upload a new shader to the program cache, and bind it for use.
288 * \param prog_data must be ralloc'd and will be stolen.
291 iris_upload_and_bind_shader(struct iris_context
*ice
,
292 enum iris_program_cache_id cache_id
,
294 const void *assembly
,
295 struct brw_stage_prog_data
*prog_data
,
298 assert(cache_id
!= IRIS_CACHE_BLORP
);
300 struct iris_compiled_shader
*shader
=
301 iris_upload_shader(ice
, cache_id
, key_size_for_cache(cache_id
), key
,
302 assembly
, prog_data
, streamout
);
304 ice
->shaders
.prog
[cache_id
] = shader
;
305 ice
->state
.dirty
|= dirty_flag_for_cache(cache_id
);
309 iris_blorp_lookup_shader(struct blorp_batch
*blorp_batch
,
310 const void *key
, uint32_t key_size
,
311 uint32_t *kernel_out
, void *prog_data_out
)
313 struct blorp_context
*blorp
= blorp_batch
->blorp
;
314 struct iris_context
*ice
= blorp
->driver_ctx
;
315 struct iris_batch
*batch
= blorp_batch
->driver_batch
;
316 struct iris_compiled_shader
*shader
=
317 iris_find_cached_shader(ice
, IRIS_CACHE_BLORP
, key
, key_size
);
322 struct iris_bo
*bo
= iris_resource_bo(shader
->assembly
.res
);
324 iris_bo_offset_from_base_address(bo
) + shader
->assembly
.offset
;
325 *((void **) prog_data_out
) = shader
->prog_data
;
327 iris_use_pinned_bo(batch
, bo
, false);
333 iris_blorp_upload_shader(struct blorp_batch
*blorp_batch
,
334 const void *key
, uint32_t key_size
,
335 const void *kernel
, UNUSED
uint32_t kernel_size
,
336 const struct brw_stage_prog_data
*prog_data_templ
,
337 UNUSED
uint32_t prog_data_size
,
338 uint32_t *kernel_out
, void *prog_data_out
)
340 struct blorp_context
*blorp
= blorp_batch
->blorp
;
341 struct iris_context
*ice
= blorp
->driver_ctx
;
342 struct iris_batch
*batch
= blorp_batch
->driver_batch
;
344 void *prog_data
= ralloc_size(NULL
, prog_data_size
);
345 memcpy(prog_data
, prog_data_templ
, prog_data_size
);
347 struct iris_compiled_shader
*shader
=
348 iris_upload_shader(ice
, IRIS_CACHE_BLORP
, key_size
, key
, kernel
,
351 struct iris_bo
*bo
= iris_resource_bo(shader
->assembly
.res
);
353 iris_bo_offset_from_base_address(bo
) + shader
->assembly
.offset
;
354 *((void **) prog_data_out
) = shader
->prog_data
;
356 iris_use_pinned_bo(batch
, bo
, false);
362 iris_init_program_cache(struct iris_context
*ice
)
365 _mesa_hash_table_create(ice
, keybox_hash
, keybox_equals
);
367 ice
->shaders
.uploader
=
368 u_upload_create(&ice
->ctx
, 16384, PIPE_BIND_CUSTOM
, PIPE_USAGE_IMMUTABLE
,
369 IRIS_RESOURCE_FLAG_SHADER_MEMZONE
);
373 iris_destroy_program_cache(struct iris_context
*ice
)
375 for (int i
= 0; i
< MESA_SHADER_STAGES
; i
++) {
376 ice
->shaders
.prog
[i
] = NULL
;
379 hash_table_foreach(ice
->shaders
.cache
, entry
) {
380 struct iris_compiled_shader
*shader
= entry
->data
;
381 pipe_resource_reference(&shader
->assembly
.res
, NULL
);
384 u_upload_destroy(ice
->shaders
.uploader
);
386 ralloc_free(ice
->shaders
.cache
);
390 cache_name(enum iris_program_cache_id cache_id
)
392 if (cache_id
== IRIS_CACHE_BLORP
)
395 return _mesa_shader_stage_to_string(cache_id
);
399 iris_print_program_cache(struct iris_context
*ice
)
401 struct iris_screen
*screen
= (struct iris_screen
*)ice
->ctx
.screen
;
402 const struct gen_device_info
*devinfo
= &screen
->devinfo
;
404 hash_table_foreach(ice
->shaders
.cache
, entry
) {
405 const struct keybox
*keybox
= entry
->key
;
406 struct iris_compiled_shader
*shader
= entry
->data
;
407 fprintf(stderr
, "%s:\n", cache_name(keybox
->cache_id
));
408 brw_disassemble(devinfo
, shader
->map
, 0,
409 shader
->prog_data
->program_size
, stderr
);