2
* Copyright © 2021 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 "vk_pipeline_cache.h"
27
#include "vk_common_entrypoints.h"
28
#include "vk_device.h"
30
#include "vk_physical_device.h"
32
#include "compiler/nir/nir_serialize.h"
34
#include "util/blob.h"
35
#include "util/debug.h"
36
#include "util/disk_cache.h"
37
#include "util/hash_table.h"
40
struct raw_data_object {
41
struct vk_pipeline_cache_object base;
47
static struct raw_data_object *
48
raw_data_object_create(struct vk_device *device,
49
const void *key_data, size_t key_size,
50
const void *data, size_t data_size);
53
raw_data_object_serialize(struct vk_pipeline_cache_object *object,
56
struct raw_data_object *data_obj =
57
container_of(object, struct raw_data_object, base);
59
blob_write_bytes(blob, data_obj->data, data_obj->data_size);
64
static struct vk_pipeline_cache_object *
65
raw_data_object_deserialize(struct vk_device *device,
68
struct blob_reader *blob)
70
/* We consume the entire blob_reader. Each call to ops->deserialize()
71
* happens with a brand new blob reader for error checking anyway so we
72
* can assume the blob consumes the entire reader and we don't need to
73
* serialize the data size separately.
75
assert(blob->current < blob->end);
76
size_t data_size = blob->end - blob->current;
77
const void *data = blob_read_bytes(blob, data_size);
79
struct raw_data_object *data_obj =
80
raw_data_object_create(device, key_data, key_size, data, data_size);
82
return data_obj ? &data_obj->base : NULL;
86
raw_data_object_destroy(struct vk_pipeline_cache_object *object)
88
struct raw_data_object *data_obj =
89
container_of(object, struct raw_data_object, base);
91
vk_free(&data_obj->base.device->alloc, data_obj);
94
static const struct vk_pipeline_cache_object_ops raw_data_object_ops = {
95
.serialize = raw_data_object_serialize,
96
.deserialize = raw_data_object_deserialize,
97
.destroy = raw_data_object_destroy,
100
static struct raw_data_object *
101
raw_data_object_create(struct vk_device *device,
102
const void *key_data, size_t key_size,
103
const void *data, size_t data_size)
106
VK_MULTIALLOC_DECL(&ma, struct raw_data_object, data_obj, 1);
107
VK_MULTIALLOC_DECL_SIZE(&ma, char, obj_key_data, key_size);
108
VK_MULTIALLOC_DECL_SIZE(&ma, char, obj_data, data_size);
110
if (!vk_multialloc_alloc(&ma, &device->alloc,
111
VK_SYSTEM_ALLOCATION_SCOPE_DEVICE))
114
vk_pipeline_cache_object_init(device, &data_obj->base,
115
&raw_data_object_ops,
116
obj_key_data, key_size);
117
data_obj->data = obj_data;
118
data_obj->data_size = data_size;
120
memcpy(obj_key_data, key_data, key_size);
121
memcpy(obj_data, data, data_size);
127
object_keys_equal(const void *void_a, const void *void_b)
129
const struct vk_pipeline_cache_object *a = void_a, *b = void_b;
130
if (a->key_size != b->key_size)
133
return memcmp(a->key_data, b->key_data, a->key_size) == 0;
137
object_key_hash(const void *void_object)
139
const struct vk_pipeline_cache_object *object = void_object;
140
return _mesa_hash_data(object->key_data, object->key_size);
144
vk_pipeline_cache_lock(struct vk_pipeline_cache *cache)
147
if (!(cache->flags & VK_PIPELINE_CACHE_CREATE_EXTERNALLY_SYNCHRONIZED_BIT))
148
simple_mtx_lock(&cache->lock);
152
vk_pipeline_cache_unlock(struct vk_pipeline_cache *cache)
154
if (!(cache->flags & VK_PIPELINE_CACHE_CREATE_EXTERNALLY_SYNCHRONIZED_BIT))
155
simple_mtx_unlock(&cache->lock);
159
vk_pipeline_cache_remove_object(struct vk_pipeline_cache *cache,
161
struct vk_pipeline_cache_object *object)
163
vk_pipeline_cache_lock(cache);
164
struct set_entry *entry =
165
_mesa_set_search_pre_hashed(cache->object_cache, hash, object);
166
if (entry && entry->key == (const void *)object) {
167
/* Drop the reference owned by the cache */
168
vk_pipeline_cache_object_unref(object);
170
_mesa_set_remove(cache->object_cache, entry);
172
vk_pipeline_cache_unlock(cache);
174
/* Drop our reference */
175
vk_pipeline_cache_object_unref(object);
178
/* Consumes references to both search and replace and produces a reference */
179
static struct vk_pipeline_cache_object *
180
vk_pipeline_cache_replace_object(struct vk_pipeline_cache *cache,
182
struct vk_pipeline_cache_object *search,
183
struct vk_pipeline_cache_object *replace)
185
assert(object_keys_equal(search, replace));
187
vk_pipeline_cache_lock(cache);
188
struct set_entry *entry =
189
_mesa_set_search_pre_hashed(cache->object_cache, hash, search);
191
struct vk_pipeline_cache_object *found = NULL;
193
if (entry->key == (const void *)search) {
194
/* Drop the reference owned by the cache */
195
vk_pipeline_cache_object_unref(search);
197
entry->key = vk_pipeline_cache_object_ref(replace);
199
found = vk_pipeline_cache_object_ref((void *)entry->key);
202
/* I guess the object was purged? Re-add it to the cache */
203
vk_pipeline_cache_object_ref(replace);
204
_mesa_set_add_pre_hashed(cache->object_cache, hash, replace);
206
vk_pipeline_cache_unlock(cache);
208
vk_pipeline_cache_object_unref(search);
211
vk_pipeline_cache_object_unref(replace);
219
vk_pipeline_cache_object_serialize(struct vk_pipeline_cache *cache,
220
struct vk_pipeline_cache_object *object,
221
struct blob *blob, uint32_t *data_size)
223
if (object->ops->serialize == NULL)
226
assert(blob->size == align64(blob->size, VK_PIPELINE_CACHE_BLOB_ALIGN));
227
size_t start = blob->size;
229
/* Special case for if we're writing to a NULL blob (just to get the size)
230
* and we already know the data size of the allocation. This should make
231
* the first GetPipelineCacheData() call to get the data size faster in the
232
* common case where a bunch of our objects were loaded from a previous
233
* cache or where we've already serialized the cache once.
235
if (blob->data == NULL && blob->fixed_allocation) {
236
*data_size = p_atomic_read(&object->data_size);
237
if (*data_size > 0) {
238
blob_write_bytes(blob, NULL, *data_size);
243
if (!object->ops->serialize(object, blob)) {
244
vk_logw(VK_LOG_OBJS(cache),
245
"Failed to serialize pipeline cache object");
249
size_t size = blob->size - start;
250
if (size > UINT32_MAX) {
251
vk_logw(VK_LOG_OBJS(cache),
252
"Skipping giant (4 GiB or larger) object");
256
if (blob->out_of_memory) {
257
vk_logw(VK_LOG_OBJS(cache),
258
"Insufficient memory for pipeline cache data");
262
*data_size = (uint32_t)size;
263
p_atomic_set(&object->data_size, *data_size);
268
static struct vk_pipeline_cache_object *
269
vk_pipeline_cache_object_deserialize(struct vk_pipeline_cache *cache,
270
const void *key_data, uint32_t key_size,
271
const void *data, size_t data_size,
272
const struct vk_pipeline_cache_object_ops *ops)
275
ops = &raw_data_object_ops;
277
if (unlikely(ops->deserialize == NULL)) {
278
vk_logw(VK_LOG_OBJS(cache),
279
"Pipeline cache object cannot be deserialized");
283
struct blob_reader reader;
284
blob_reader_init(&reader, data, data_size);
286
struct vk_pipeline_cache_object *object =
287
ops->deserialize(cache->base.device, key_data, key_size, &reader);
289
if (object == NULL) {
290
vk_logw(VK_LOG_OBJS(cache),
291
"Deserializing pipeline cache object failed");
295
assert(reader.current == reader.end && !reader.overrun);
296
assert(object->device == cache->base.device);
297
assert(object->ops == ops);
298
assert(object->ref_cnt == 1);
299
assert(object->key_size == key_size);
300
assert(memcmp(object->key_data, key_data, key_size) == 0);
305
struct vk_pipeline_cache_object *
306
vk_pipeline_cache_lookup_object(struct vk_pipeline_cache *cache,
307
const void *key_data, size_t key_size,
308
const struct vk_pipeline_cache_object_ops *ops,
311
assert(key_size <= UINT32_MAX);
314
if (cache_hit != NULL)
317
struct vk_pipeline_cache_object key = {
318
.key_data = key_data,
319
.key_size = key_size,
321
uint32_t hash = object_key_hash(&key);
323
struct vk_pipeline_cache_object *object = NULL;
325
if (cache != NULL && cache->object_cache != NULL) {
326
vk_pipeline_cache_lock(cache);
327
struct set_entry *entry =
328
_mesa_set_search_pre_hashed(cache->object_cache, hash, &key);
330
object = vk_pipeline_cache_object_ref((void *)entry->key);
331
if (cache_hit != NULL)
334
vk_pipeline_cache_unlock(cache);
337
if (object == NULL) {
338
#ifdef ENABLE_SHADER_CACHE
339
struct disk_cache *disk_cache = cache->base.device->physical->disk_cache;
340
if (disk_cache != NULL) {
342
disk_cache_compute_key(disk_cache, key_data, key_size, cache_key);
345
uint8_t *data = disk_cache_get(disk_cache, cache_key, &data_size);
347
object = vk_pipeline_cache_object_deserialize(cache,
353
return vk_pipeline_cache_add_object(cache, object);
358
/* No disk cache or not found in the disk cache */
362
if (object->ops == &raw_data_object_ops && ops != &raw_data_object_ops) {
363
/* The object isn't fully formed yet and we need to deserialize it into
364
* a real object before it can be used.
366
struct raw_data_object *data_obj =
367
container_of(object, struct raw_data_object, base);
369
struct vk_pipeline_cache_object *real_object =
370
vk_pipeline_cache_object_deserialize(cache,
371
data_obj->base.key_data,
372
data_obj->base.key_size,
374
data_obj->data_size, ops);
375
if (real_object == NULL) {
376
vk_pipeline_cache_remove_object(cache, hash, object);
380
object = vk_pipeline_cache_replace_object(cache, hash, object,
384
assert(object->ops == ops);
389
struct vk_pipeline_cache_object *
390
vk_pipeline_cache_add_object(struct vk_pipeline_cache *cache,
391
struct vk_pipeline_cache_object *object)
393
assert(object->ops != NULL);
395
if (cache->object_cache == NULL)
398
uint32_t hash = object_key_hash(object);
400
vk_pipeline_cache_lock(cache);
402
struct set_entry *entry =
403
_mesa_set_search_or_add_pre_hashed(cache->object_cache,
404
hash, object, &found);
406
struct vk_pipeline_cache_object *found_object = NULL;
408
found_object = vk_pipeline_cache_object_ref((void *)entry->key);
410
/* The cache now owns a reference */
411
vk_pipeline_cache_object_ref(object);
413
vk_pipeline_cache_unlock(cache);
416
vk_pipeline_cache_object_unref(object);
419
/* If it wasn't in the object cache, it might not be in the disk cache
420
* either. Better try and add it.
423
#ifdef ENABLE_SHADER_CACHE
424
struct disk_cache *disk_cache = cache->base.device->physical->disk_cache;
425
if (object->ops->serialize != NULL && disk_cache) {
429
if (object->ops->serialize(object, &blob) && !blob.out_of_memory) {
431
disk_cache_compute_key(disk_cache, object->key_data,
432
object->key_size, cache_key);
434
disk_cache_put(disk_cache, cache_key, blob.data, blob.size, NULL);
446
vk_pipeline_cache_lookup_nir(struct vk_pipeline_cache *cache,
447
const void *key_data, size_t key_size,
448
const struct nir_shader_compiler_options *nir_options,
449
bool *cache_hit, void *mem_ctx)
451
struct vk_pipeline_cache_object *object =
452
vk_pipeline_cache_lookup_object(cache, key_data, key_size,
453
&raw_data_object_ops, cache_hit);
457
struct raw_data_object *data_obj =
458
container_of(object, struct raw_data_object, base);
460
struct blob_reader blob;
461
blob_reader_init(&blob, data_obj->data, data_obj->data_size);
463
nir_shader *nir = nir_deserialize(mem_ctx, nir_options, &blob);
464
vk_pipeline_cache_object_unref(object);
475
vk_pipeline_cache_add_nir(struct vk_pipeline_cache *cache,
476
const void *key_data, size_t key_size,
477
const nir_shader *nir)
482
nir_serialize(&blob, nir, false);
483
if (blob.out_of_memory) {
484
vk_logw(VK_LOG_OBJS(cache), "Ran out of memory serializing NIR shader");
489
struct raw_data_object *data_obj =
490
raw_data_object_create(cache->base.device,
492
blob.data, blob.size);
495
struct vk_pipeline_cache_object *cached =
496
vk_pipeline_cache_add_object(cache, &data_obj->base);
497
vk_pipeline_cache_object_unref(cached);
501
find_type_for_ops(const struct vk_physical_device *pdevice,
502
const struct vk_pipeline_cache_object_ops *ops)
504
const struct vk_pipeline_cache_object_ops *const *import_ops =
505
pdevice->pipeline_cache_import_ops;
507
if (import_ops == NULL)
510
for (int32_t i = 0; import_ops[i]; i++) {
511
if (import_ops[i] == ops)
518
static const struct vk_pipeline_cache_object_ops *
519
find_ops_for_type(const struct vk_physical_device *pdevice,
522
const struct vk_pipeline_cache_object_ops *const *import_ops =
523
pdevice->pipeline_cache_import_ops;
525
if (import_ops == NULL || type < 0)
528
return import_ops[type];
532
vk_pipeline_cache_load(struct vk_pipeline_cache *cache,
533
const void *data, size_t size)
535
struct blob_reader blob;
536
blob_reader_init(&blob, data, size);
538
struct vk_pipeline_cache_header header;
539
blob_copy_bytes(&blob, &header, sizeof(header));
540
uint32_t count = blob_read_uint32(&blob);
544
if (memcmp(&header, &cache->header, sizeof(header)) != 0)
547
for (uint32_t i = 0; i < count; i++) {
548
int32_t type = blob_read_uint32(&blob);
549
uint32_t key_size = blob_read_uint32(&blob);
550
uint32_t data_size = blob_read_uint32(&blob);
551
const void *key_data = blob_read_bytes(&blob, key_size);
552
blob_reader_align(&blob, VK_PIPELINE_CACHE_BLOB_ALIGN);
553
const void *data = blob_read_bytes(&blob, data_size);
557
const struct vk_pipeline_cache_object_ops *ops =
558
find_ops_for_type(cache->base.device->physical, type);
560
struct vk_pipeline_cache_object *object =
561
vk_pipeline_cache_object_deserialize(cache,
563
data, data_size, ops);
567
object = vk_pipeline_cache_add_object(cache, object);
568
vk_pipeline_cache_object_unref(object);
572
struct vk_pipeline_cache *
573
vk_pipeline_cache_create(struct vk_device *device,
574
const struct vk_pipeline_cache_create_info *info,
575
const VkAllocationCallbacks *pAllocator)
577
static const struct VkPipelineCacheCreateInfo default_create_info = {
578
.sType = VK_STRUCTURE_TYPE_PIPELINE_CACHE_CREATE_INFO,
580
struct vk_pipeline_cache *cache;
582
const struct VkPipelineCacheCreateInfo *pCreateInfo =
583
info->pCreateInfo != NULL ? info->pCreateInfo : &default_create_info;
585
assert(pCreateInfo->sType == VK_STRUCTURE_TYPE_PIPELINE_CACHE_CREATE_INFO);
587
cache = vk_object_zalloc(device, pAllocator, sizeof(*cache),
588
VK_OBJECT_TYPE_PIPELINE_CACHE);
592
cache->flags = pCreateInfo->flags;
594
struct VkPhysicalDeviceProperties pdevice_props;
595
device->physical->dispatch_table.GetPhysicalDeviceProperties(
596
vk_physical_device_to_handle(device->physical), &pdevice_props);
598
cache->header = (struct vk_pipeline_cache_header) {
599
.header_size = sizeof(struct vk_pipeline_cache_header),
600
.header_version = VK_PIPELINE_CACHE_HEADER_VERSION_ONE,
601
.vendor_id = pdevice_props.vendorID,
602
.device_id = pdevice_props.deviceID,
604
memcpy(cache->header.uuid, pdevice_props.pipelineCacheUUID, VK_UUID_SIZE);
606
simple_mtx_init(&cache->lock, mtx_plain);
608
if (info->force_enable ||
609
env_var_as_boolean("VK_ENABLE_PIPELINE_CACHE", true)) {
610
cache->object_cache = _mesa_set_create(NULL, object_key_hash,
614
if (cache->object_cache && pCreateInfo->initialDataSize > 0) {
615
vk_pipeline_cache_load(cache, pCreateInfo->pInitialData,
616
pCreateInfo->initialDataSize);
623
object_unref_cb(struct set_entry *entry)
625
vk_pipeline_cache_object_unref((void *)entry->key);
629
vk_pipeline_cache_destroy(struct vk_pipeline_cache *cache,
630
const VkAllocationCallbacks *pAllocator)
632
if (cache->object_cache)
633
_mesa_set_destroy(cache->object_cache, object_unref_cb);
634
simple_mtx_destroy(&cache->lock);
635
vk_object_free(cache->base.device, pAllocator, cache);
638
VKAPI_ATTR VkResult VKAPI_CALL
639
vk_common_CreatePipelineCache(VkDevice _device,
640
const VkPipelineCacheCreateInfo *pCreateInfo,
641
const VkAllocationCallbacks *pAllocator,
642
VkPipelineCache *pPipelineCache)
644
VK_FROM_HANDLE(vk_device, device, _device);
645
struct vk_pipeline_cache *cache;
647
struct vk_pipeline_cache_create_info info = {
648
.pCreateInfo = pCreateInfo,
650
cache = vk_pipeline_cache_create(device, &info, pAllocator);
652
return VK_ERROR_OUT_OF_HOST_MEMORY;
654
*pPipelineCache = vk_pipeline_cache_to_handle(cache);
659
VKAPI_ATTR void VKAPI_CALL
660
vk_common_DestroyPipelineCache(VkDevice device,
661
VkPipelineCache pipelineCache,
662
const VkAllocationCallbacks *pAllocator)
664
VK_FROM_HANDLE(vk_pipeline_cache, cache, pipelineCache);
669
assert(cache->base.device == vk_device_from_handle(device));
670
vk_pipeline_cache_destroy(cache, pAllocator);
673
VKAPI_ATTR VkResult VKAPI_CALL
674
vk_common_GetPipelineCacheData(VkDevice _device,
675
VkPipelineCache pipelineCache,
679
VK_FROM_HANDLE(vk_device, device, _device);
680
VK_FROM_HANDLE(vk_pipeline_cache, cache, pipelineCache);
684
blob_init_fixed(&blob, pData, *pDataSize);
686
blob_init_fixed(&blob, NULL, SIZE_MAX);
689
blob_write_bytes(&blob, &cache->header, sizeof(cache->header));
692
intptr_t count_offset = blob_reserve_uint32(&blob);
693
if (count_offset < 0) {
696
return VK_INCOMPLETE;
699
vk_pipeline_cache_lock(cache);
701
VkResult result = VK_SUCCESS;
702
if (cache->object_cache != NULL) {
703
set_foreach(cache->object_cache, entry) {
704
struct vk_pipeline_cache_object *object = (void *)entry->key;
706
if (object->ops->serialize == NULL)
709
size_t blob_size_save = blob.size;
711
int32_t type = find_type_for_ops(device->physical, object->ops);
712
blob_write_uint32(&blob, type);
713
blob_write_uint32(&blob, object->key_size);
714
intptr_t data_size_resv = blob_reserve_uint32(&blob);
715
blob_write_bytes(&blob, object->key_data, object->key_size);
717
blob_align(&blob, VK_PIPELINE_CACHE_BLOB_ALIGN);
720
if (!vk_pipeline_cache_object_serialize(cache, object,
721
&blob, &data_size)) {
722
blob.size = blob_size_save;
723
if (blob.out_of_memory) {
724
result = VK_INCOMPLETE;
728
/* Failed for some other reason; keep going */
732
/* vk_pipeline_cache_object_serialize should have failed */
733
assert(!blob.out_of_memory);
735
assert(data_size_resv >= 0);
736
blob_overwrite_uint32(&blob, data_size_resv, data_size);
740
vk_pipeline_cache_unlock(cache);
742
blob_overwrite_uint32(&blob, count_offset, count);
744
*pDataSize = blob.size;
751
VKAPI_ATTR VkResult VKAPI_CALL
752
vk_common_MergePipelineCaches(VkDevice device,
753
VkPipelineCache dstCache,
754
uint32_t srcCacheCount,
755
const VkPipelineCache *pSrcCaches)
757
VK_FROM_HANDLE(vk_pipeline_cache, dst, dstCache);
759
if (!dst->object_cache)
762
vk_pipeline_cache_lock(dst);
764
for (uint32_t i = 0; i < srcCacheCount; i++) {
765
VK_FROM_HANDLE(vk_pipeline_cache, src, pSrcCaches[i]);
767
if (!src->object_cache)
774
vk_pipeline_cache_lock(src);
776
set_foreach(src->object_cache, src_entry) {
777
struct vk_pipeline_cache_object *src_object = (void *)src_entry->key;
779
bool found_in_dst = false;
780
struct set_entry *dst_entry =
781
_mesa_set_search_or_add_pre_hashed(dst->object_cache,
783
src_object, &found_in_dst);
785
struct vk_pipeline_cache_object *dst_object = (void *)dst_entry->key;
786
if (dst_object->ops == &raw_data_object_ops &&
787
src_object->ops != &raw_data_object_ops) {
788
/* Even though dst has the object, it only has the blob version
789
* which isn't as useful. Replace it with the real object.
791
vk_pipeline_cache_object_unref(dst_object);
792
dst_entry->key = vk_pipeline_cache_object_ref(src_object);
795
/* We inserted src_object in dst so it needs a reference */
796
assert(dst_entry->key == (const void *)src_object);
797
vk_pipeline_cache_object_ref(src_object);
801
vk_pipeline_cache_unlock(src);
804
vk_pipeline_cache_unlock(dst);