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 "compiler/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"
32 struct anv_shader_bin
*
33 anv_shader_bin_create(struct anv_device
*device
,
34 const void *key_data
, uint32_t key_size
,
35 const void *kernel_data
, uint32_t kernel_size
,
36 const void *constant_data
, uint32_t constant_data_size
,
37 const struct brw_stage_prog_data
*prog_data_in
,
38 uint32_t prog_data_size
, const void *prog_data_param_in
,
39 const struct anv_pipeline_bind_map
*bind_map
)
41 struct anv_shader_bin
*shader
;
42 struct anv_shader_bin_key
*key
;
43 struct brw_stage_prog_data
*prog_data
;
44 uint32_t *prog_data_param
;
45 struct anv_pipeline_binding
*surface_to_descriptor
, *sampler_to_descriptor
;
48 anv_multialloc_add(&ma
, &shader
, 1);
49 anv_multialloc_add_size(&ma
, &key
, sizeof(*key
) + key_size
);
50 anv_multialloc_add_size(&ma
, &prog_data
, prog_data_size
);
51 anv_multialloc_add(&ma
, &prog_data_param
, prog_data_in
->nr_params
);
52 anv_multialloc_add(&ma
, &surface_to_descriptor
,
53 bind_map
->surface_count
);
54 anv_multialloc_add(&ma
, &sampler_to_descriptor
,
55 bind_map
->sampler_count
);
57 if (!anv_multialloc_alloc(&ma
, &device
->alloc
,
58 VK_SYSTEM_ALLOCATION_SCOPE_DEVICE
))
64 memcpy(key
->data
, key_data
, key_size
);
68 anv_state_pool_alloc(&device
->instruction_state_pool
, kernel_size
, 64);
69 memcpy(shader
->kernel
.map
, kernel_data
, kernel_size
);
70 shader
->kernel_size
= kernel_size
;
72 if (constant_data_size
) {
73 shader
->constant_data
=
74 anv_state_pool_alloc(&device
->dynamic_state_pool
,
75 constant_data_size
, 32);
76 memcpy(shader
->constant_data
.map
, constant_data
, constant_data_size
);
78 shader
->constant_data
= ANV_STATE_NULL
;
80 shader
->constant_data_size
= constant_data_size
;
82 memcpy(prog_data
, prog_data_in
, prog_data_size
);
83 memcpy(prog_data_param
, prog_data_param_in
,
84 prog_data
->nr_params
* sizeof(*prog_data_param
));
85 prog_data
->param
= prog_data_param
;
86 shader
->prog_data
= prog_data
;
87 shader
->prog_data_size
= prog_data_size
;
89 shader
->bind_map
= *bind_map
;
90 typed_memcpy(surface_to_descriptor
, bind_map
->surface_to_descriptor
,
91 bind_map
->surface_count
);
92 shader
->bind_map
.surface_to_descriptor
= surface_to_descriptor
;
93 typed_memcpy(sampler_to_descriptor
, bind_map
->sampler_to_descriptor
,
94 bind_map
->sampler_count
);
95 shader
->bind_map
.sampler_to_descriptor
= sampler_to_descriptor
;
101 anv_shader_bin_destroy(struct anv_device
*device
,
102 struct anv_shader_bin
*shader
)
104 assert(shader
->ref_cnt
== 0);
105 anv_state_pool_free(&device
->instruction_state_pool
, shader
->kernel
);
106 anv_state_pool_free(&device
->dynamic_state_pool
, shader
->constant_data
);
107 vk_free(&device
->alloc
, shader
);
111 anv_shader_bin_write_to_blob(const struct anv_shader_bin
*shader
,
116 ok
= blob_write_uint32(blob
, shader
->key
->size
);
117 ok
= blob_write_bytes(blob
, shader
->key
->data
, shader
->key
->size
);
119 ok
= blob_write_uint32(blob
, shader
->kernel_size
);
120 ok
= blob_write_bytes(blob
, shader
->kernel
.map
, shader
->kernel_size
);
122 ok
= blob_write_uint32(blob
, shader
->constant_data_size
);
123 ok
= blob_write_bytes(blob
, shader
->constant_data
.map
,
124 shader
->constant_data_size
);
126 ok
= blob_write_uint32(blob
, shader
->prog_data_size
);
127 ok
= blob_write_bytes(blob
, shader
->prog_data
, shader
->prog_data_size
);
128 ok
= blob_write_bytes(blob
, shader
->prog_data
->param
,
129 shader
->prog_data
->nr_params
*
130 sizeof(*shader
->prog_data
->param
));
132 ok
= blob_write_uint32(blob
, shader
->bind_map
.surface_count
);
133 ok
= blob_write_uint32(blob
, shader
->bind_map
.sampler_count
);
134 ok
= blob_write_uint32(blob
, shader
->bind_map
.image_count
);
135 ok
= blob_write_bytes(blob
, shader
->bind_map
.surface_to_descriptor
,
136 shader
->bind_map
.surface_count
*
137 sizeof(*shader
->bind_map
.surface_to_descriptor
));
138 ok
= blob_write_bytes(blob
, shader
->bind_map
.sampler_to_descriptor
,
139 shader
->bind_map
.sampler_count
*
140 sizeof(*shader
->bind_map
.sampler_to_descriptor
));
145 static struct anv_shader_bin
*
146 anv_shader_bin_create_from_blob(struct anv_device
*device
,
147 struct blob_reader
*blob
)
149 uint32_t key_size
= blob_read_uint32(blob
);
150 const void *key_data
= blob_read_bytes(blob
, key_size
);
152 uint32_t kernel_size
= blob_read_uint32(blob
);
153 const void *kernel_data
= blob_read_bytes(blob
, kernel_size
);
155 uint32_t constant_data_size
= blob_read_uint32(blob
);
156 const void *constant_data
= blob_read_bytes(blob
, constant_data_size
);
158 uint32_t prog_data_size
= blob_read_uint32(blob
);
159 const struct brw_stage_prog_data
*prog_data
=
160 blob_read_bytes(blob
, prog_data_size
);
163 const void *prog_data_param
=
164 blob_read_bytes(blob
, prog_data
->nr_params
* sizeof(*prog_data
->param
));
166 struct anv_pipeline_bind_map bind_map
;
167 bind_map
.surface_count
= blob_read_uint32(blob
);
168 bind_map
.sampler_count
= blob_read_uint32(blob
);
169 bind_map
.image_count
= blob_read_uint32(blob
);
170 bind_map
.surface_to_descriptor
= (void *)
171 blob_read_bytes(blob
, bind_map
.surface_count
*
172 sizeof(*bind_map
.surface_to_descriptor
));
173 bind_map
.sampler_to_descriptor
= (void *)
174 blob_read_bytes(blob
, bind_map
.sampler_count
*
175 sizeof(*bind_map
.sampler_to_descriptor
));
180 return anv_shader_bin_create(device
,
182 kernel_data
, kernel_size
,
183 constant_data
, constant_data_size
,
184 prog_data
, prog_data_size
, prog_data_param
,
190 * - Compact binding table layout so it's tight and not dependent on
191 * descriptor set layout.
193 * - Review prog_data struct for size and cacheability: struct
194 * brw_stage_prog_data has binding_table which uses a lot of uint32_t for 8
195 * bit quantities etc; use bit fields for all bools, eg dual_src_blend.
199 shader_bin_key_hash_func(const void *void_key
)
201 const struct anv_shader_bin_key
*key
= void_key
;
202 return _mesa_hash_data(key
->data
, key
->size
);
206 shader_bin_key_compare_func(const void *void_a
, const void *void_b
)
208 const struct anv_shader_bin_key
*a
= void_a
, *b
= void_b
;
209 if (a
->size
!= b
->size
)
212 return memcmp(a
->data
, b
->data
, a
->size
) == 0;
216 sha1_hash_func(const void *sha1
)
218 return _mesa_hash_data(sha1
, 20);
222 sha1_compare_func(const void *sha1_a
, const void *sha1_b
)
224 return memcmp(sha1_a
, sha1_b
, 20) == 0;
228 anv_pipeline_cache_init(struct anv_pipeline_cache
*cache
,
229 struct anv_device
*device
,
232 cache
->device
= device
;
233 pthread_mutex_init(&cache
->mutex
, NULL
);
236 cache
->cache
= _mesa_hash_table_create(NULL
, shader_bin_key_hash_func
,
237 shader_bin_key_compare_func
);
238 cache
->nir_cache
= _mesa_hash_table_create(NULL
, sha1_hash_func
,
242 cache
->nir_cache
= NULL
;
247 anv_pipeline_cache_finish(struct anv_pipeline_cache
*cache
)
249 pthread_mutex_destroy(&cache
->mutex
);
252 /* This is a bit unfortunate. In order to keep things from randomly
253 * going away, the shader cache has to hold a reference to all shader
254 * binaries it contains. We unref them when we destroy the cache.
256 hash_table_foreach(cache
->cache
, entry
)
257 anv_shader_bin_unref(cache
->device
, entry
->data
);
259 _mesa_hash_table_destroy(cache
->cache
, NULL
);
263 static struct anv_shader_bin
*
264 anv_pipeline_cache_search_locked(struct anv_pipeline_cache
*cache
,
265 const void *key_data
, uint32_t key_size
)
267 uint32_t vla
[1 + DIV_ROUND_UP(key_size
, sizeof(uint32_t))];
268 struct anv_shader_bin_key
*key
= (void *)vla
;
269 key
->size
= key_size
;
270 memcpy(key
->data
, key_data
, key_size
);
272 struct hash_entry
*entry
= _mesa_hash_table_search(cache
->cache
, key
);
279 struct anv_shader_bin
*
280 anv_pipeline_cache_search(struct anv_pipeline_cache
*cache
,
281 const void *key_data
, uint32_t key_size
)
286 pthread_mutex_lock(&cache
->mutex
);
288 struct anv_shader_bin
*shader
=
289 anv_pipeline_cache_search_locked(cache
, key_data
, key_size
);
291 pthread_mutex_unlock(&cache
->mutex
);
293 /* We increment refcount before handing it to the caller */
295 anv_shader_bin_ref(shader
);
301 anv_pipeline_cache_add_shader_bin(struct anv_pipeline_cache
*cache
,
302 struct anv_shader_bin
*bin
)
307 pthread_mutex_lock(&cache
->mutex
);
309 struct hash_entry
*entry
= _mesa_hash_table_search(cache
->cache
, bin
->key
);
311 /* Take a reference for the cache */
312 anv_shader_bin_ref(bin
);
313 _mesa_hash_table_insert(cache
->cache
, bin
->key
, bin
);
316 pthread_mutex_unlock(&cache
->mutex
);
319 static struct anv_shader_bin
*
320 anv_pipeline_cache_add_shader_locked(struct anv_pipeline_cache
*cache
,
321 const void *key_data
, uint32_t key_size
,
322 const void *kernel_data
,
323 uint32_t kernel_size
,
324 const void *constant_data
,
325 uint32_t constant_data_size
,
326 const struct brw_stage_prog_data
*prog_data
,
327 uint32_t prog_data_size
,
328 const void *prog_data_param
,
329 const struct anv_pipeline_bind_map
*bind_map
)
331 struct anv_shader_bin
*shader
=
332 anv_pipeline_cache_search_locked(cache
, key_data
, key_size
);
336 struct anv_shader_bin
*bin
=
337 anv_shader_bin_create(cache
->device
, key_data
, key_size
,
338 kernel_data
, kernel_size
,
339 constant_data
, constant_data_size
,
340 prog_data
, prog_data_size
, prog_data_param
,
345 _mesa_hash_table_insert(cache
->cache
, bin
->key
, bin
);
350 struct anv_shader_bin
*
351 anv_pipeline_cache_upload_kernel(struct anv_pipeline_cache
*cache
,
352 const void *key_data
, uint32_t key_size
,
353 const void *kernel_data
, uint32_t kernel_size
,
354 const void *constant_data
,
355 uint32_t constant_data_size
,
356 const struct brw_stage_prog_data
*prog_data
,
357 uint32_t prog_data_size
,
358 const struct anv_pipeline_bind_map
*bind_map
)
361 pthread_mutex_lock(&cache
->mutex
);
363 struct anv_shader_bin
*bin
=
364 anv_pipeline_cache_add_shader_locked(cache
, key_data
, key_size
,
365 kernel_data
, kernel_size
,
366 constant_data
, constant_data_size
,
367 prog_data
, prog_data_size
,
368 prog_data
->param
, bind_map
);
370 pthread_mutex_unlock(&cache
->mutex
);
372 /* We increment refcount before handing it to the caller */
374 anv_shader_bin_ref(bin
);
378 /* In this case, we're not caching it so the caller owns it entirely */
379 return anv_shader_bin_create(cache
->device
, key_data
, key_size
,
380 kernel_data
, kernel_size
,
381 constant_data
, constant_data_size
,
382 prog_data
, prog_data_size
,
383 prog_data
->param
, bind_map
);
387 struct cache_header
{
388 uint32_t header_size
;
389 uint32_t header_version
;
392 uint8_t uuid
[VK_UUID_SIZE
];
396 anv_pipeline_cache_load(struct anv_pipeline_cache
*cache
,
397 const void *data
, size_t size
)
399 struct anv_device
*device
= cache
->device
;
400 struct anv_physical_device
*pdevice
= &device
->instance
->physicalDevice
;
402 if (cache
->cache
== NULL
)
405 struct blob_reader blob
;
406 blob_reader_init(&blob
, data
, size
);
408 struct cache_header header
;
409 blob_copy_bytes(&blob
, &header
, sizeof(header
));
410 uint32_t count
= blob_read_uint32(&blob
);
414 if (header
.header_size
< sizeof(header
))
416 if (header
.header_version
!= VK_PIPELINE_CACHE_HEADER_VERSION_ONE
)
418 if (header
.vendor_id
!= 0x8086)
420 if (header
.device_id
!= device
->chipset_id
)
422 if (memcmp(header
.uuid
, pdevice
->pipeline_cache_uuid
, VK_UUID_SIZE
) != 0)
425 for (uint32_t i
= 0; i
< count
; i
++) {
426 struct anv_shader_bin
*bin
=
427 anv_shader_bin_create_from_blob(device
, &blob
);
430 _mesa_hash_table_insert(cache
->cache
, bin
->key
, bin
);
434 VkResult
anv_CreatePipelineCache(
436 const VkPipelineCacheCreateInfo
* pCreateInfo
,
437 const VkAllocationCallbacks
* pAllocator
,
438 VkPipelineCache
* pPipelineCache
)
440 ANV_FROM_HANDLE(anv_device
, device
, _device
);
441 struct anv_pipeline_cache
*cache
;
443 assert(pCreateInfo
->sType
== VK_STRUCTURE_TYPE_PIPELINE_CACHE_CREATE_INFO
);
444 assert(pCreateInfo
->flags
== 0);
446 cache
= vk_alloc2(&device
->alloc
, pAllocator
,
448 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT
);
450 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
452 anv_pipeline_cache_init(cache
, device
,
453 device
->instance
->pipeline_cache_enabled
);
455 if (pCreateInfo
->initialDataSize
> 0)
456 anv_pipeline_cache_load(cache
,
457 pCreateInfo
->pInitialData
,
458 pCreateInfo
->initialDataSize
);
460 *pPipelineCache
= anv_pipeline_cache_to_handle(cache
);
465 void anv_DestroyPipelineCache(
467 VkPipelineCache _cache
,
468 const VkAllocationCallbacks
* pAllocator
)
470 ANV_FROM_HANDLE(anv_device
, device
, _device
);
471 ANV_FROM_HANDLE(anv_pipeline_cache
, cache
, _cache
);
476 anv_pipeline_cache_finish(cache
);
478 vk_free2(&device
->alloc
, pAllocator
, cache
);
481 VkResult
anv_GetPipelineCacheData(
483 VkPipelineCache _cache
,
487 ANV_FROM_HANDLE(anv_device
, device
, _device
);
488 ANV_FROM_HANDLE(anv_pipeline_cache
, cache
, _cache
);
489 struct anv_physical_device
*pdevice
= &device
->instance
->physicalDevice
;
493 blob_init_fixed(&blob
, pData
, *pDataSize
);
495 blob_init_fixed(&blob
, NULL
, SIZE_MAX
);
498 struct cache_header header
= {
499 .header_size
= sizeof(struct cache_header
),
500 .header_version
= VK_PIPELINE_CACHE_HEADER_VERSION_ONE
,
502 .device_id
= device
->chipset_id
,
504 memcpy(header
.uuid
, pdevice
->pipeline_cache_uuid
, VK_UUID_SIZE
);
505 blob_write_bytes(&blob
, &header
, sizeof(header
));
508 intptr_t count_offset
= blob_reserve_uint32(&blob
);
509 if (count_offset
< 0) {
512 return VK_INCOMPLETE
;
515 VkResult result
= VK_SUCCESS
;
517 hash_table_foreach(cache
->cache
, entry
) {
518 struct anv_shader_bin
*shader
= entry
->data
;
520 size_t save_size
= blob
.size
;
521 if (!anv_shader_bin_write_to_blob(shader
, &blob
)) {
522 /* If it fails reset to the previous size and bail */
523 blob
.size
= save_size
;
524 result
= VK_INCOMPLETE
;
532 blob_overwrite_uint32(&blob
, count_offset
, count
);
534 *pDataSize
= blob
.size
;
541 VkResult
anv_MergePipelineCaches(
543 VkPipelineCache destCache
,
544 uint32_t srcCacheCount
,
545 const VkPipelineCache
* pSrcCaches
)
547 ANV_FROM_HANDLE(anv_pipeline_cache
, dst
, destCache
);
552 for (uint32_t i
= 0; i
< srcCacheCount
; i
++) {
553 ANV_FROM_HANDLE(anv_pipeline_cache
, src
, pSrcCaches
[i
]);
557 hash_table_foreach(src
->cache
, entry
) {
558 struct anv_shader_bin
*bin
= entry
->data
;
561 if (_mesa_hash_table_search(dst
->cache
, bin
->key
))
564 anv_shader_bin_ref(bin
);
565 _mesa_hash_table_insert(dst
->cache
, bin
->key
, bin
);
572 struct anv_shader_bin
*
573 anv_device_search_for_kernel(struct anv_device
*device
,
574 struct anv_pipeline_cache
*cache
,
575 const void *key_data
, uint32_t key_size
)
577 struct anv_shader_bin
*bin
;
580 bin
= anv_pipeline_cache_search(cache
, key_data
, key_size
);
585 #ifdef ENABLE_SHADER_CACHE
586 struct disk_cache
*disk_cache
= device
->instance
->physicalDevice
.disk_cache
;
587 if (disk_cache
&& device
->instance
->pipeline_cache_enabled
) {
589 disk_cache_compute_key(disk_cache
, key_data
, key_size
, cache_key
);
592 uint8_t *buffer
= disk_cache_get(disk_cache
, cache_key
, &buffer_size
);
594 struct blob_reader blob
;
595 blob_reader_init(&blob
, buffer
, buffer_size
);
596 bin
= anv_shader_bin_create_from_blob(device
, &blob
);
601 anv_pipeline_cache_add_shader_bin(cache
, bin
);
611 struct anv_shader_bin
*
612 anv_device_upload_kernel(struct anv_device
*device
,
613 struct anv_pipeline_cache
*cache
,
614 const void *key_data
, uint32_t key_size
,
615 const void *kernel_data
, uint32_t kernel_size
,
616 const void *constant_data
,
617 uint32_t constant_data_size
,
618 const struct brw_stage_prog_data
*prog_data
,
619 uint32_t prog_data_size
,
620 const struct anv_pipeline_bind_map
*bind_map
)
622 struct anv_shader_bin
*bin
;
624 bin
= anv_pipeline_cache_upload_kernel(cache
, key_data
, key_size
,
625 kernel_data
, kernel_size
,
626 constant_data
, constant_data_size
,
627 prog_data
, prog_data_size
,
630 bin
= anv_shader_bin_create(device
, key_data
, key_size
,
631 kernel_data
, kernel_size
,
632 constant_data
, constant_data_size
,
633 prog_data
, prog_data_size
,
634 prog_data
->param
, bind_map
);
640 #ifdef ENABLE_SHADER_CACHE
641 struct disk_cache
*disk_cache
= device
->instance
->physicalDevice
.disk_cache
;
645 anv_shader_bin_write_to_blob(bin
, &binary
);
647 if (!binary
.out_of_memory
) {
649 disk_cache_compute_key(disk_cache
, key_data
, key_size
, cache_key
);
651 disk_cache_put(disk_cache
, cache_key
, binary
.data
, binary
.size
, NULL
);
654 blob_finish(&binary
);
661 struct serialized_nir
{
662 unsigned char sha1_key
[20];
668 anv_device_search_for_nir(struct anv_device
*device
,
669 struct anv_pipeline_cache
*cache
,
670 const nir_shader_compiler_options
*nir_options
,
671 unsigned char sha1_key
[20],
674 if (cache
&& cache
->nir_cache
) {
675 const struct serialized_nir
*snir
= NULL
;
677 pthread_mutex_lock(&cache
->mutex
);
678 struct hash_entry
*entry
=
679 _mesa_hash_table_search(cache
->nir_cache
, sha1_key
);
682 pthread_mutex_unlock(&cache
->mutex
);
685 struct blob_reader blob
;
686 blob_reader_init(&blob
, snir
->data
, snir
->size
);
688 nir_shader
*nir
= nir_deserialize(mem_ctx
, nir_options
, &blob
);
701 anv_device_upload_nir(struct anv_device
*device
,
702 struct anv_pipeline_cache
*cache
,
703 const struct nir_shader
*nir
,
704 unsigned char sha1_key
[20])
706 if (cache
&& cache
->nir_cache
) {
707 pthread_mutex_lock(&cache
->mutex
);
708 struct hash_entry
*entry
=
709 _mesa_hash_table_search(cache
->nir_cache
, sha1_key
);
710 pthread_mutex_unlock(&cache
->mutex
);
717 nir_serialize(&blob
, nir
);
718 if (blob
.out_of_memory
) {
723 pthread_mutex_lock(&cache
->mutex
);
724 /* Because ralloc isn't thread-safe, we have to do all this inside the
725 * lock. We could unlock for the big memcpy but it's probably not worth
728 entry
= _mesa_hash_table_search(cache
->nir_cache
, sha1_key
);
730 pthread_mutex_unlock(&cache
->mutex
);
734 struct serialized_nir
*snir
=
735 ralloc_size(cache
->nir_cache
, sizeof(*snir
) + blob
.size
);
736 memcpy(snir
->sha1_key
, sha1_key
, 20);
737 snir
->size
= blob
.size
;
738 memcpy(snir
->data
, blob
.data
, blob
.size
);
740 _mesa_hash_table_insert(cache
->nir_cache
, snir
->sha1_key
, snir
);
742 pthread_mutex_unlock(&cache
->mutex
);