2 * Copyright © 2015 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 (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
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
24 #include "util/blob.h"
25 #include "util/hash_table.h"
26 #include "util/debug.h"
27 #include "util/disk_cache.h"
28 #include "util/mesa-sha1.h"
29 #include "nir/nir_serialize.h"
30 #include "anv_private.h"
31 #include "nir/nir_xfb_info.h"
33 struct anv_shader_bin
*
34 anv_shader_bin_create(struct anv_device
*device
,
35 const void *key_data
, uint32_t key_size
,
36 const void *kernel_data
, uint32_t kernel_size
,
37 const void *constant_data
, uint32_t constant_data_size
,
38 const struct brw_stage_prog_data
*prog_data_in
,
39 uint32_t prog_data_size
, const void *prog_data_param_in
,
40 const struct brw_compile_stats
*stats
, uint32_t num_stats
,
41 const nir_xfb_info
*xfb_info_in
,
42 const struct anv_pipeline_bind_map
*bind_map
)
44 struct anv_shader_bin
*shader
;
45 struct anv_shader_bin_key
*key
;
46 struct brw_stage_prog_data
*prog_data
;
47 uint32_t *prog_data_param
;
48 nir_xfb_info
*xfb_info
;
49 struct anv_pipeline_binding
*surface_to_descriptor
, *sampler_to_descriptor
;
52 anv_multialloc_add(&ma
, &shader
, 1);
53 anv_multialloc_add_size(&ma
, &key
, sizeof(*key
) + key_size
);
54 anv_multialloc_add_size(&ma
, &prog_data
, prog_data_size
);
55 anv_multialloc_add(&ma
, &prog_data_param
, prog_data_in
->nr_params
);
57 uint32_t xfb_info_size
= nir_xfb_info_size(xfb_info_in
->output_count
);
58 anv_multialloc_add_size(&ma
, &xfb_info
, xfb_info_size
);
60 anv_multialloc_add(&ma
, &surface_to_descriptor
,
61 bind_map
->surface_count
);
62 anv_multialloc_add(&ma
, &sampler_to_descriptor
,
63 bind_map
->sampler_count
);
65 if (!anv_multialloc_alloc(&ma
, &device
->alloc
,
66 VK_SYSTEM_ALLOCATION_SCOPE_DEVICE
))
72 memcpy(key
->data
, key_data
, key_size
);
76 anv_state_pool_alloc(&device
->instruction_state_pool
, kernel_size
, 64);
77 memcpy(shader
->kernel
.map
, kernel_data
, kernel_size
);
78 shader
->kernel_size
= kernel_size
;
80 if (constant_data_size
) {
81 shader
->constant_data
=
82 anv_state_pool_alloc(&device
->dynamic_state_pool
,
83 constant_data_size
, 32);
84 memcpy(shader
->constant_data
.map
, constant_data
, constant_data_size
);
86 shader
->constant_data
= ANV_STATE_NULL
;
88 shader
->constant_data_size
= constant_data_size
;
90 memcpy(prog_data
, prog_data_in
, prog_data_size
);
91 memcpy(prog_data_param
, prog_data_param_in
,
92 prog_data
->nr_params
* sizeof(*prog_data_param
));
93 prog_data
->param
= prog_data_param
;
94 shader
->prog_data
= prog_data
;
95 shader
->prog_data_size
= prog_data_size
;
97 assert(num_stats
<= ARRAY_SIZE(shader
->stats
));
98 typed_memcpy(shader
->stats
, stats
, num_stats
);
99 shader
->num_stats
= num_stats
;
102 *xfb_info
= *xfb_info_in
;
103 typed_memcpy(xfb_info
->outputs
, xfb_info_in
->outputs
,
104 xfb_info_in
->output_count
);
105 shader
->xfb_info
= xfb_info
;
107 shader
->xfb_info
= NULL
;
110 shader
->bind_map
= *bind_map
;
111 typed_memcpy(surface_to_descriptor
, bind_map
->surface_to_descriptor
,
112 bind_map
->surface_count
);
113 shader
->bind_map
.surface_to_descriptor
= surface_to_descriptor
;
114 typed_memcpy(sampler_to_descriptor
, bind_map
->sampler_to_descriptor
,
115 bind_map
->sampler_count
);
116 shader
->bind_map
.sampler_to_descriptor
= sampler_to_descriptor
;
122 anv_shader_bin_destroy(struct anv_device
*device
,
123 struct anv_shader_bin
*shader
)
125 assert(shader
->ref_cnt
== 0);
126 anv_state_pool_free(&device
->instruction_state_pool
, shader
->kernel
);
127 anv_state_pool_free(&device
->dynamic_state_pool
, shader
->constant_data
);
128 vk_free(&device
->alloc
, shader
);
132 anv_shader_bin_write_to_blob(const struct anv_shader_bin
*shader
,
135 blob_write_uint32(blob
, shader
->key
->size
);
136 blob_write_bytes(blob
, shader
->key
->data
, shader
->key
->size
);
138 blob_write_uint32(blob
, shader
->kernel_size
);
139 blob_write_bytes(blob
, shader
->kernel
.map
, shader
->kernel_size
);
141 blob_write_uint32(blob
, shader
->constant_data_size
);
142 blob_write_bytes(blob
, shader
->constant_data
.map
,
143 shader
->constant_data_size
);
145 blob_write_uint32(blob
, shader
->prog_data_size
);
146 blob_write_bytes(blob
, shader
->prog_data
, shader
->prog_data_size
);
147 blob_write_bytes(blob
, shader
->prog_data
->param
,
148 shader
->prog_data
->nr_params
*
149 sizeof(*shader
->prog_data
->param
));
151 blob_write_uint32(blob
, shader
->num_stats
);
152 blob_write_bytes(blob
, shader
->stats
,
153 shader
->num_stats
* sizeof(shader
->stats
[0]));
155 if (shader
->xfb_info
) {
156 uint32_t xfb_info_size
=
157 nir_xfb_info_size(shader
->xfb_info
->output_count
);
158 blob_write_uint32(blob
, xfb_info_size
);
159 blob_write_bytes(blob
, shader
->xfb_info
, xfb_info_size
);
161 blob_write_uint32(blob
, 0);
164 blob_write_uint32(blob
, shader
->bind_map
.surface_count
);
165 blob_write_uint32(blob
, shader
->bind_map
.sampler_count
);
166 blob_write_bytes(blob
, shader
->bind_map
.surface_to_descriptor
,
167 shader
->bind_map
.surface_count
*
168 sizeof(*shader
->bind_map
.surface_to_descriptor
));
169 blob_write_bytes(blob
, shader
->bind_map
.sampler_to_descriptor
,
170 shader
->bind_map
.sampler_count
*
171 sizeof(*shader
->bind_map
.sampler_to_descriptor
));
172 blob_write_bytes(blob
, shader
->bind_map
.push_ranges
,
173 sizeof(shader
->bind_map
.push_ranges
));
175 return !blob
->out_of_memory
;
178 static struct anv_shader_bin
*
179 anv_shader_bin_create_from_blob(struct anv_device
*device
,
180 struct blob_reader
*blob
)
182 uint32_t key_size
= blob_read_uint32(blob
);
183 const void *key_data
= blob_read_bytes(blob
, key_size
);
185 uint32_t kernel_size
= blob_read_uint32(blob
);
186 const void *kernel_data
= blob_read_bytes(blob
, kernel_size
);
188 uint32_t constant_data_size
= blob_read_uint32(blob
);
189 const void *constant_data
= blob_read_bytes(blob
, constant_data_size
);
191 uint32_t prog_data_size
= blob_read_uint32(blob
);
192 const struct brw_stage_prog_data
*prog_data
=
193 blob_read_bytes(blob
, prog_data_size
);
196 const void *prog_data_param
=
197 blob_read_bytes(blob
, prog_data
->nr_params
* sizeof(*prog_data
->param
));
199 uint32_t num_stats
= blob_read_uint32(blob
);
200 const struct brw_compile_stats
*stats
=
201 blob_read_bytes(blob
, num_stats
* sizeof(stats
[0]));
203 const nir_xfb_info
*xfb_info
= NULL
;
204 uint32_t xfb_size
= blob_read_uint32(blob
);
206 xfb_info
= blob_read_bytes(blob
, xfb_size
);
208 struct anv_pipeline_bind_map bind_map
;
209 bind_map
.surface_count
= blob_read_uint32(blob
);
210 bind_map
.sampler_count
= blob_read_uint32(blob
);
211 bind_map
.surface_to_descriptor
= (void *)
212 blob_read_bytes(blob
, bind_map
.surface_count
*
213 sizeof(*bind_map
.surface_to_descriptor
));
214 bind_map
.sampler_to_descriptor
= (void *)
215 blob_read_bytes(blob
, bind_map
.sampler_count
*
216 sizeof(*bind_map
.sampler_to_descriptor
));
217 blob_copy_bytes(blob
, bind_map
.push_ranges
, sizeof(bind_map
.push_ranges
));
222 return anv_shader_bin_create(device
,
224 kernel_data
, kernel_size
,
225 constant_data
, constant_data_size
,
226 prog_data
, prog_data_size
, prog_data_param
,
227 stats
, num_stats
, xfb_info
, &bind_map
);
232 * - Compact binding table layout so it's tight and not dependent on
233 * descriptor set layout.
235 * - Review prog_data struct for size and cacheability: struct
236 * brw_stage_prog_data has binding_table which uses a lot of uint32_t for 8
237 * bit quantities etc; use bit fields for all bools, eg dual_src_blend.
241 shader_bin_key_hash_func(const void *void_key
)
243 const struct anv_shader_bin_key
*key
= void_key
;
244 return _mesa_hash_data(key
->data
, key
->size
);
248 shader_bin_key_compare_func(const void *void_a
, const void *void_b
)
250 const struct anv_shader_bin_key
*a
= void_a
, *b
= void_b
;
251 if (a
->size
!= b
->size
)
254 return memcmp(a
->data
, b
->data
, a
->size
) == 0;
258 sha1_hash_func(const void *sha1
)
260 return _mesa_hash_data(sha1
, 20);
264 sha1_compare_func(const void *sha1_a
, const void *sha1_b
)
266 return memcmp(sha1_a
, sha1_b
, 20) == 0;
270 anv_pipeline_cache_init(struct anv_pipeline_cache
*cache
,
271 struct anv_device
*device
,
274 cache
->device
= device
;
275 pthread_mutex_init(&cache
->mutex
, NULL
);
278 cache
->cache
= _mesa_hash_table_create(NULL
, shader_bin_key_hash_func
,
279 shader_bin_key_compare_func
);
280 cache
->nir_cache
= _mesa_hash_table_create(NULL
, sha1_hash_func
,
284 cache
->nir_cache
= NULL
;
289 anv_pipeline_cache_finish(struct anv_pipeline_cache
*cache
)
291 pthread_mutex_destroy(&cache
->mutex
);
294 /* This is a bit unfortunate. In order to keep things from randomly
295 * going away, the shader cache has to hold a reference to all shader
296 * binaries it contains. We unref them when we destroy the cache.
298 hash_table_foreach(cache
->cache
, entry
)
299 anv_shader_bin_unref(cache
->device
, entry
->data
);
301 _mesa_hash_table_destroy(cache
->cache
, NULL
);
304 if (cache
->nir_cache
) {
305 hash_table_foreach(cache
->nir_cache
, entry
)
306 ralloc_free(entry
->data
);
308 _mesa_hash_table_destroy(cache
->nir_cache
, NULL
);
312 static struct anv_shader_bin
*
313 anv_pipeline_cache_search_locked(struct anv_pipeline_cache
*cache
,
314 const void *key_data
, uint32_t key_size
)
316 uint32_t vla
[1 + DIV_ROUND_UP(key_size
, sizeof(uint32_t))];
317 struct anv_shader_bin_key
*key
= (void *)vla
;
318 key
->size
= key_size
;
319 memcpy(key
->data
, key_data
, key_size
);
321 struct hash_entry
*entry
= _mesa_hash_table_search(cache
->cache
, key
);
328 struct anv_shader_bin
*
329 anv_pipeline_cache_search(struct anv_pipeline_cache
*cache
,
330 const void *key_data
, uint32_t key_size
)
335 pthread_mutex_lock(&cache
->mutex
);
337 struct anv_shader_bin
*shader
=
338 anv_pipeline_cache_search_locked(cache
, key_data
, key_size
);
340 pthread_mutex_unlock(&cache
->mutex
);
342 /* We increment refcount before handing it to the caller */
344 anv_shader_bin_ref(shader
);
350 anv_pipeline_cache_add_shader_bin(struct anv_pipeline_cache
*cache
,
351 struct anv_shader_bin
*bin
)
356 pthread_mutex_lock(&cache
->mutex
);
358 struct hash_entry
*entry
= _mesa_hash_table_search(cache
->cache
, bin
->key
);
360 /* Take a reference for the cache */
361 anv_shader_bin_ref(bin
);
362 _mesa_hash_table_insert(cache
->cache
, bin
->key
, bin
);
365 pthread_mutex_unlock(&cache
->mutex
);
368 static struct anv_shader_bin
*
369 anv_pipeline_cache_add_shader_locked(struct anv_pipeline_cache
*cache
,
370 const void *key_data
, uint32_t key_size
,
371 const void *kernel_data
,
372 uint32_t kernel_size
,
373 const void *constant_data
,
374 uint32_t constant_data_size
,
375 const struct brw_stage_prog_data
*prog_data
,
376 uint32_t prog_data_size
,
377 const void *prog_data_param
,
378 const struct brw_compile_stats
*stats
,
380 const nir_xfb_info
*xfb_info
,
381 const struct anv_pipeline_bind_map
*bind_map
)
383 struct anv_shader_bin
*shader
=
384 anv_pipeline_cache_search_locked(cache
, key_data
, key_size
);
388 struct anv_shader_bin
*bin
=
389 anv_shader_bin_create(cache
->device
, key_data
, key_size
,
390 kernel_data
, kernel_size
,
391 constant_data
, constant_data_size
,
392 prog_data
, prog_data_size
, prog_data_param
,
393 stats
, num_stats
, xfb_info
, bind_map
);
397 _mesa_hash_table_insert(cache
->cache
, bin
->key
, bin
);
402 struct anv_shader_bin
*
403 anv_pipeline_cache_upload_kernel(struct anv_pipeline_cache
*cache
,
404 const void *key_data
, uint32_t key_size
,
405 const void *kernel_data
, uint32_t kernel_size
,
406 const void *constant_data
,
407 uint32_t constant_data_size
,
408 const struct brw_stage_prog_data
*prog_data
,
409 uint32_t prog_data_size
,
410 const struct brw_compile_stats
*stats
,
412 const nir_xfb_info
*xfb_info
,
413 const struct anv_pipeline_bind_map
*bind_map
)
416 pthread_mutex_lock(&cache
->mutex
);
418 struct anv_shader_bin
*bin
=
419 anv_pipeline_cache_add_shader_locked(cache
, key_data
, key_size
,
420 kernel_data
, kernel_size
,
421 constant_data
, constant_data_size
,
422 prog_data
, prog_data_size
,
427 pthread_mutex_unlock(&cache
->mutex
);
429 /* We increment refcount before handing it to the caller */
431 anv_shader_bin_ref(bin
);
435 /* In this case, we're not caching it so the caller owns it entirely */
436 return anv_shader_bin_create(cache
->device
, key_data
, key_size
,
437 kernel_data
, kernel_size
,
438 constant_data
, constant_data_size
,
439 prog_data
, prog_data_size
,
446 struct cache_header
{
447 uint32_t header_size
;
448 uint32_t header_version
;
451 uint8_t uuid
[VK_UUID_SIZE
];
455 anv_pipeline_cache_load(struct anv_pipeline_cache
*cache
,
456 const void *data
, size_t size
)
458 struct anv_device
*device
= cache
->device
;
459 struct anv_physical_device
*pdevice
= &device
->instance
->physicalDevice
;
461 if (cache
->cache
== NULL
)
464 struct blob_reader blob
;
465 blob_reader_init(&blob
, data
, size
);
467 struct cache_header header
;
468 blob_copy_bytes(&blob
, &header
, sizeof(header
));
469 uint32_t count
= blob_read_uint32(&blob
);
473 if (header
.header_size
< sizeof(header
))
475 if (header
.header_version
!= VK_PIPELINE_CACHE_HEADER_VERSION_ONE
)
477 if (header
.vendor_id
!= 0x8086)
479 if (header
.device_id
!= device
->chipset_id
)
481 if (memcmp(header
.uuid
, pdevice
->pipeline_cache_uuid
, VK_UUID_SIZE
) != 0)
484 for (uint32_t i
= 0; i
< count
; i
++) {
485 struct anv_shader_bin
*bin
=
486 anv_shader_bin_create_from_blob(device
, &blob
);
489 _mesa_hash_table_insert(cache
->cache
, bin
->key
, bin
);
493 VkResult
anv_CreatePipelineCache(
495 const VkPipelineCacheCreateInfo
* pCreateInfo
,
496 const VkAllocationCallbacks
* pAllocator
,
497 VkPipelineCache
* pPipelineCache
)
499 ANV_FROM_HANDLE(anv_device
, device
, _device
);
500 struct anv_pipeline_cache
*cache
;
502 assert(pCreateInfo
->sType
== VK_STRUCTURE_TYPE_PIPELINE_CACHE_CREATE_INFO
);
503 assert(pCreateInfo
->flags
== 0);
505 cache
= vk_alloc2(&device
->alloc
, pAllocator
,
507 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT
);
509 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
511 anv_pipeline_cache_init(cache
, device
,
512 device
->instance
->pipeline_cache_enabled
);
514 if (pCreateInfo
->initialDataSize
> 0)
515 anv_pipeline_cache_load(cache
,
516 pCreateInfo
->pInitialData
,
517 pCreateInfo
->initialDataSize
);
519 *pPipelineCache
= anv_pipeline_cache_to_handle(cache
);
524 void anv_DestroyPipelineCache(
526 VkPipelineCache _cache
,
527 const VkAllocationCallbacks
* pAllocator
)
529 ANV_FROM_HANDLE(anv_device
, device
, _device
);
530 ANV_FROM_HANDLE(anv_pipeline_cache
, cache
, _cache
);
535 anv_pipeline_cache_finish(cache
);
537 vk_free2(&device
->alloc
, pAllocator
, cache
);
540 VkResult
anv_GetPipelineCacheData(
542 VkPipelineCache _cache
,
546 ANV_FROM_HANDLE(anv_device
, device
, _device
);
547 ANV_FROM_HANDLE(anv_pipeline_cache
, cache
, _cache
);
548 struct anv_physical_device
*pdevice
= &device
->instance
->physicalDevice
;
552 blob_init_fixed(&blob
, pData
, *pDataSize
);
554 blob_init_fixed(&blob
, NULL
, SIZE_MAX
);
557 struct cache_header header
= {
558 .header_size
= sizeof(struct cache_header
),
559 .header_version
= VK_PIPELINE_CACHE_HEADER_VERSION_ONE
,
561 .device_id
= device
->chipset_id
,
563 memcpy(header
.uuid
, pdevice
->pipeline_cache_uuid
, VK_UUID_SIZE
);
564 blob_write_bytes(&blob
, &header
, sizeof(header
));
567 intptr_t count_offset
= blob_reserve_uint32(&blob
);
568 if (count_offset
< 0) {
571 return VK_INCOMPLETE
;
574 VkResult result
= VK_SUCCESS
;
576 hash_table_foreach(cache
->cache
, entry
) {
577 struct anv_shader_bin
*shader
= entry
->data
;
579 size_t save_size
= blob
.size
;
580 if (!anv_shader_bin_write_to_blob(shader
, &blob
)) {
581 /* If it fails reset to the previous size and bail */
582 blob
.size
= save_size
;
583 result
= VK_INCOMPLETE
;
591 blob_overwrite_uint32(&blob
, count_offset
, count
);
593 *pDataSize
= blob
.size
;
600 VkResult
anv_MergePipelineCaches(
602 VkPipelineCache destCache
,
603 uint32_t srcCacheCount
,
604 const VkPipelineCache
* pSrcCaches
)
606 ANV_FROM_HANDLE(anv_pipeline_cache
, dst
, destCache
);
611 for (uint32_t i
= 0; i
< srcCacheCount
; i
++) {
612 ANV_FROM_HANDLE(anv_pipeline_cache
, src
, pSrcCaches
[i
]);
616 hash_table_foreach(src
->cache
, entry
) {
617 struct anv_shader_bin
*bin
= entry
->data
;
620 if (_mesa_hash_table_search(dst
->cache
, bin
->key
))
623 anv_shader_bin_ref(bin
);
624 _mesa_hash_table_insert(dst
->cache
, bin
->key
, bin
);
631 struct anv_shader_bin
*
632 anv_device_search_for_kernel(struct anv_device
*device
,
633 struct anv_pipeline_cache
*cache
,
634 const void *key_data
, uint32_t key_size
,
635 bool *user_cache_hit
)
637 struct anv_shader_bin
*bin
;
639 *user_cache_hit
= false;
642 bin
= anv_pipeline_cache_search(cache
, key_data
, key_size
);
644 *user_cache_hit
= cache
!= &device
->default_pipeline_cache
;
649 #ifdef ENABLE_SHADER_CACHE
650 struct disk_cache
*disk_cache
= device
->instance
->physicalDevice
.disk_cache
;
651 if (disk_cache
&& device
->instance
->pipeline_cache_enabled
) {
653 disk_cache_compute_key(disk_cache
, key_data
, key_size
, cache_key
);
656 uint8_t *buffer
= disk_cache_get(disk_cache
, cache_key
, &buffer_size
);
658 struct blob_reader blob
;
659 blob_reader_init(&blob
, buffer
, buffer_size
);
660 bin
= anv_shader_bin_create_from_blob(device
, &blob
);
665 anv_pipeline_cache_add_shader_bin(cache
, bin
);
675 struct anv_shader_bin
*
676 anv_device_upload_kernel(struct anv_device
*device
,
677 struct anv_pipeline_cache
*cache
,
678 const void *key_data
, uint32_t key_size
,
679 const void *kernel_data
, uint32_t kernel_size
,
680 const void *constant_data
,
681 uint32_t constant_data_size
,
682 const struct brw_stage_prog_data
*prog_data
,
683 uint32_t prog_data_size
,
684 const struct brw_compile_stats
*stats
,
686 const nir_xfb_info
*xfb_info
,
687 const struct anv_pipeline_bind_map
*bind_map
)
689 struct anv_shader_bin
*bin
;
691 bin
= anv_pipeline_cache_upload_kernel(cache
, key_data
, key_size
,
692 kernel_data
, kernel_size
,
693 constant_data
, constant_data_size
,
694 prog_data
, prog_data_size
,
698 bin
= anv_shader_bin_create(device
, key_data
, key_size
,
699 kernel_data
, kernel_size
,
700 constant_data
, constant_data_size
,
701 prog_data
, prog_data_size
,
710 #ifdef ENABLE_SHADER_CACHE
711 struct disk_cache
*disk_cache
= device
->instance
->physicalDevice
.disk_cache
;
715 if (anv_shader_bin_write_to_blob(bin
, &binary
)) {
717 disk_cache_compute_key(disk_cache
, key_data
, key_size
, cache_key
);
719 disk_cache_put(disk_cache
, cache_key
, binary
.data
, binary
.size
, NULL
);
722 blob_finish(&binary
);
729 struct serialized_nir
{
730 unsigned char sha1_key
[20];
736 anv_device_search_for_nir(struct anv_device
*device
,
737 struct anv_pipeline_cache
*cache
,
738 const nir_shader_compiler_options
*nir_options
,
739 unsigned char sha1_key
[20],
742 if (cache
&& cache
->nir_cache
) {
743 const struct serialized_nir
*snir
= NULL
;
745 pthread_mutex_lock(&cache
->mutex
);
746 struct hash_entry
*entry
=
747 _mesa_hash_table_search(cache
->nir_cache
, sha1_key
);
750 pthread_mutex_unlock(&cache
->mutex
);
753 struct blob_reader blob
;
754 blob_reader_init(&blob
, snir
->data
, snir
->size
);
756 nir_shader
*nir
= nir_deserialize(mem_ctx
, nir_options
, &blob
);
769 anv_device_upload_nir(struct anv_device
*device
,
770 struct anv_pipeline_cache
*cache
,
771 const struct nir_shader
*nir
,
772 unsigned char sha1_key
[20])
774 if (cache
&& cache
->nir_cache
) {
775 pthread_mutex_lock(&cache
->mutex
);
776 struct hash_entry
*entry
=
777 _mesa_hash_table_search(cache
->nir_cache
, sha1_key
);
778 pthread_mutex_unlock(&cache
->mutex
);
785 nir_serialize(&blob
, nir
, false);
786 if (blob
.out_of_memory
) {
791 pthread_mutex_lock(&cache
->mutex
);
792 /* Because ralloc isn't thread-safe, we have to do all this inside the
793 * lock. We could unlock for the big memcpy but it's probably not worth
796 entry
= _mesa_hash_table_search(cache
->nir_cache
, sha1_key
);
799 pthread_mutex_unlock(&cache
->mutex
);
803 struct serialized_nir
*snir
=
804 ralloc_size(cache
->nir_cache
, sizeof(*snir
) + blob
.size
);
805 memcpy(snir
->sha1_key
, sha1_key
, 20);
806 snir
->size
= blob
.size
;
807 memcpy(snir
->data
, blob
.data
, blob
.size
);
811 _mesa_hash_table_insert(cache
->nir_cache
, snir
->sha1_key
, snir
);
813 pthread_mutex_unlock(&cache
->mutex
);