radv: Add the concept of radv shader binaries.
[mesa.git] / src / amd / vulkan / radv_pipeline_cache.c
1 /*
2 * Copyright © 2015 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 * 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:
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 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
21 * IN THE SOFTWARE.
22 */
23
24 #include "util/mesa-sha1.h"
25 #include "util/debug.h"
26 #include "util/disk_cache.h"
27 #include "util/u_atomic.h"
28 #include "radv_debug.h"
29 #include "radv_private.h"
30 #include "radv_shader.h"
31
32 #include "ac_nir_to_llvm.h"
33
34 struct cache_entry {
35 union {
36 unsigned char sha1[20];
37 uint32_t sha1_dw[5];
38 };
39 uint32_t binary_sizes[MESA_SHADER_STAGES];
40 struct radv_shader_variant *variants[MESA_SHADER_STAGES];
41 char code[0];
42 };
43
44 void
45 radv_pipeline_cache_init(struct radv_pipeline_cache *cache,
46 struct radv_device *device)
47 {
48 cache->device = device;
49 pthread_mutex_init(&cache->mutex, NULL);
50
51 cache->modified = false;
52 cache->kernel_count = 0;
53 cache->total_size = 0;
54 cache->table_size = 1024;
55 const size_t byte_size = cache->table_size * sizeof(cache->hash_table[0]);
56 cache->hash_table = malloc(byte_size);
57
58 /* We don't consider allocation failure fatal, we just start with a 0-sized
59 * cache. Disable caching when we want to keep shader debug info, since
60 * we don't get the debug info on cached shaders. */
61 if (cache->hash_table == NULL ||
62 (device->instance->debug_flags & RADV_DEBUG_NO_CACHE) ||
63 device->keep_shader_info)
64 cache->table_size = 0;
65 else
66 memset(cache->hash_table, 0, byte_size);
67 }
68
69 void
70 radv_pipeline_cache_finish(struct radv_pipeline_cache *cache)
71 {
72 for (unsigned i = 0; i < cache->table_size; ++i)
73 if (cache->hash_table[i]) {
74 for(int j = 0; j < MESA_SHADER_STAGES; ++j) {
75 if (cache->hash_table[i]->variants[j])
76 radv_shader_variant_destroy(cache->device,
77 cache->hash_table[i]->variants[j]);
78 }
79 vk_free(&cache->alloc, cache->hash_table[i]);
80 }
81 pthread_mutex_destroy(&cache->mutex);
82 free(cache->hash_table);
83 }
84
85 static uint32_t
86 entry_size(struct cache_entry *entry)
87 {
88 size_t ret = sizeof(*entry);
89 for (int i = 0; i < MESA_SHADER_STAGES; ++i)
90 if (entry->binary_sizes[i])
91 ret += entry->binary_sizes[i];
92 return ret;
93 }
94
95 void
96 radv_hash_shaders(unsigned char *hash,
97 const VkPipelineShaderStageCreateInfo **stages,
98 const struct radv_pipeline_layout *layout,
99 const struct radv_pipeline_key *key,
100 uint32_t flags)
101 {
102 struct mesa_sha1 ctx;
103
104 _mesa_sha1_init(&ctx);
105 if (key)
106 _mesa_sha1_update(&ctx, key, sizeof(*key));
107 if (layout)
108 _mesa_sha1_update(&ctx, layout->sha1, sizeof(layout->sha1));
109
110 for (int i = 0; i < MESA_SHADER_STAGES; ++i) {
111 if (stages[i]) {
112 RADV_FROM_HANDLE(radv_shader_module, module, stages[i]->module);
113 const VkSpecializationInfo *spec_info = stages[i]->pSpecializationInfo;
114
115 _mesa_sha1_update(&ctx, module->sha1, sizeof(module->sha1));
116 _mesa_sha1_update(&ctx, stages[i]->pName, strlen(stages[i]->pName));
117 if (spec_info) {
118 _mesa_sha1_update(&ctx, spec_info->pMapEntries,
119 spec_info->mapEntryCount * sizeof spec_info->pMapEntries[0]);
120 _mesa_sha1_update(&ctx, spec_info->pData, spec_info->dataSize);
121 }
122 }
123 }
124 _mesa_sha1_update(&ctx, &flags, 4);
125 _mesa_sha1_final(&ctx, hash);
126 }
127
128
129 static struct cache_entry *
130 radv_pipeline_cache_search_unlocked(struct radv_pipeline_cache *cache,
131 const unsigned char *sha1)
132 {
133 const uint32_t mask = cache->table_size - 1;
134 const uint32_t start = (*(uint32_t *) sha1);
135
136 if (cache->table_size == 0)
137 return NULL;
138
139 for (uint32_t i = 0; i < cache->table_size; i++) {
140 const uint32_t index = (start + i) & mask;
141 struct cache_entry *entry = cache->hash_table[index];
142
143 if (!entry)
144 return NULL;
145
146 if (memcmp(entry->sha1, sha1, sizeof(entry->sha1)) == 0) {
147 return entry;
148 }
149 }
150
151 unreachable("hash table should never be full");
152 }
153
154 static struct cache_entry *
155 radv_pipeline_cache_search(struct radv_pipeline_cache *cache,
156 const unsigned char *sha1)
157 {
158 struct cache_entry *entry;
159
160 pthread_mutex_lock(&cache->mutex);
161
162 entry = radv_pipeline_cache_search_unlocked(cache, sha1);
163
164 pthread_mutex_unlock(&cache->mutex);
165
166 return entry;
167 }
168
169 static void
170 radv_pipeline_cache_set_entry(struct radv_pipeline_cache *cache,
171 struct cache_entry *entry)
172 {
173 const uint32_t mask = cache->table_size - 1;
174 const uint32_t start = entry->sha1_dw[0];
175
176 /* We'll always be able to insert when we get here. */
177 assert(cache->kernel_count < cache->table_size / 2);
178
179 for (uint32_t i = 0; i < cache->table_size; i++) {
180 const uint32_t index = (start + i) & mask;
181 if (!cache->hash_table[index]) {
182 cache->hash_table[index] = entry;
183 break;
184 }
185 }
186
187 cache->total_size += entry_size(entry);
188 cache->kernel_count++;
189 }
190
191
192 static VkResult
193 radv_pipeline_cache_grow(struct radv_pipeline_cache *cache)
194 {
195 const uint32_t table_size = cache->table_size * 2;
196 const uint32_t old_table_size = cache->table_size;
197 const size_t byte_size = table_size * sizeof(cache->hash_table[0]);
198 struct cache_entry **table;
199 struct cache_entry **old_table = cache->hash_table;
200
201 table = malloc(byte_size);
202 if (table == NULL)
203 return vk_error(cache->device->instance, VK_ERROR_OUT_OF_HOST_MEMORY);
204
205 cache->hash_table = table;
206 cache->table_size = table_size;
207 cache->kernel_count = 0;
208 cache->total_size = 0;
209
210 memset(cache->hash_table, 0, byte_size);
211 for (uint32_t i = 0; i < old_table_size; i++) {
212 struct cache_entry *entry = old_table[i];
213 if (!entry)
214 continue;
215
216 radv_pipeline_cache_set_entry(cache, entry);
217 }
218
219 free(old_table);
220
221 return VK_SUCCESS;
222 }
223
224 static void
225 radv_pipeline_cache_add_entry(struct radv_pipeline_cache *cache,
226 struct cache_entry *entry)
227 {
228 if (cache->kernel_count == cache->table_size / 2)
229 radv_pipeline_cache_grow(cache);
230
231 /* Failing to grow that hash table isn't fatal, but may mean we don't
232 * have enough space to add this new kernel. Only add it if there's room.
233 */
234 if (cache->kernel_count < cache->table_size / 2)
235 radv_pipeline_cache_set_entry(cache, entry);
236 }
237
238 static bool
239 radv_is_cache_disabled(struct radv_device *device)
240 {
241 /* Pipeline caches can be disabled with RADV_DEBUG=nocache, with
242 * MESA_GLSL_CACHE_DISABLE=1, and when VK_AMD_shader_info is requested.
243 */
244 return (device->instance->debug_flags & RADV_DEBUG_NO_CACHE) ||
245 device->keep_shader_info;
246 }
247
248 bool
249 radv_create_shader_variants_from_pipeline_cache(struct radv_device *device,
250 struct radv_pipeline_cache *cache,
251 const unsigned char *sha1,
252 struct radv_shader_variant **variants,
253 bool *found_in_application_cache)
254 {
255 struct cache_entry *entry;
256
257 if (!cache) {
258 cache = device->mem_cache;
259 *found_in_application_cache = false;
260 }
261
262 pthread_mutex_lock(&cache->mutex);
263
264 entry = radv_pipeline_cache_search_unlocked(cache, sha1);
265
266 if (!entry) {
267 *found_in_application_cache = false;
268
269 /* Don't cache when we want debug info, since this isn't
270 * present in the cache.
271 */
272 if (radv_is_cache_disabled(device) || !device->physical_device->disk_cache) {
273 pthread_mutex_unlock(&cache->mutex);
274 return false;
275 }
276
277 uint8_t disk_sha1[20];
278 disk_cache_compute_key(device->physical_device->disk_cache,
279 sha1, 20, disk_sha1);
280 entry = (struct cache_entry *)
281 disk_cache_get(device->physical_device->disk_cache,
282 disk_sha1, NULL);
283 if (!entry) {
284 pthread_mutex_unlock(&cache->mutex);
285 return false;
286 } else {
287 size_t size = entry_size(entry);
288 struct cache_entry *new_entry = vk_alloc(&cache->alloc, size, 8,
289 VK_SYSTEM_ALLOCATION_SCOPE_CACHE);
290 if (!new_entry) {
291 free(entry);
292 pthread_mutex_unlock(&cache->mutex);
293 return false;
294 }
295
296 memcpy(new_entry, entry, entry_size(entry));
297 free(entry);
298 entry = new_entry;
299
300 radv_pipeline_cache_add_entry(cache, new_entry);
301 }
302 }
303
304 char *p = entry->code;
305 for(int i = 0; i < MESA_SHADER_STAGES; ++i) {
306 if (!entry->variants[i] && entry->binary_sizes[i]) {
307 struct radv_shader_binary *binary = calloc(1, entry->binary_sizes[i]);
308 memcpy(binary, p, entry->binary_sizes[i]);
309 p += entry->binary_sizes[i];
310
311 entry->variants[i] = radv_shader_variant_create(device, binary);
312 } else if (entry->binary_sizes[i]) {
313 p += entry->binary_sizes[i];
314 }
315
316 }
317
318 for (int i = 0; i < MESA_SHADER_STAGES; ++i)
319 if (entry->variants[i])
320 p_atomic_inc(&entry->variants[i]->ref_count);
321
322 memcpy(variants, entry->variants, sizeof(entry->variants));
323 pthread_mutex_unlock(&cache->mutex);
324 return true;
325 }
326
327 void
328 radv_pipeline_cache_insert_shaders(struct radv_device *device,
329 struct radv_pipeline_cache *cache,
330 const unsigned char *sha1,
331 struct radv_shader_variant **variants,
332 struct radv_shader_binary *const *binaries)
333 {
334 if (!cache)
335 cache = device->mem_cache;
336
337 pthread_mutex_lock(&cache->mutex);
338 struct cache_entry *entry = radv_pipeline_cache_search_unlocked(cache, sha1);
339 if (entry) {
340 for (int i = 0; i < MESA_SHADER_STAGES; ++i) {
341 if (entry->variants[i]) {
342 radv_shader_variant_destroy(cache->device, variants[i]);
343 variants[i] = entry->variants[i];
344 } else {
345 entry->variants[i] = variants[i];
346 }
347 if (variants[i])
348 p_atomic_inc(&variants[i]->ref_count);
349 }
350 pthread_mutex_unlock(&cache->mutex);
351 return;
352 }
353
354 /* Don't cache when we want debug info, since this isn't
355 * present in the cache.
356 */
357 if (radv_is_cache_disabled(device)) {
358 pthread_mutex_unlock(&cache->mutex);
359 return;
360 }
361
362 size_t size = sizeof(*entry);
363 for (int i = 0; i < MESA_SHADER_STAGES; ++i)
364 if (variants[i])
365 size += binaries[i]->total_size;
366
367
368 entry = vk_alloc(&cache->alloc, size, 8,
369 VK_SYSTEM_ALLOCATION_SCOPE_CACHE);
370 if (!entry) {
371 pthread_mutex_unlock(&cache->mutex);
372 return;
373 }
374
375 memset(entry, 0, sizeof(*entry));
376 memcpy(entry->sha1, sha1, 20);
377
378 char* p = entry->code;
379
380 for (int i = 0; i < MESA_SHADER_STAGES; ++i) {
381 if (!variants[i])
382 continue;
383
384 entry->binary_sizes[i] = binaries[i]->total_size;
385
386 memcpy(p, binaries[i], binaries[i]->total_size);
387 p += binaries[i]->total_size;
388 }
389
390 /* Always add cache items to disk. This will allow collection of
391 * compiled shaders by third parties such as steam, even if the app
392 * implements its own pipeline cache.
393 */
394 if (device->physical_device->disk_cache) {
395 uint8_t disk_sha1[20];
396 disk_cache_compute_key(device->physical_device->disk_cache, sha1, 20,
397 disk_sha1);
398 disk_cache_put(device->physical_device->disk_cache,
399 disk_sha1, entry, entry_size(entry), NULL);
400 }
401
402 /* We delay setting the variant so we have reproducible disk cache
403 * items.
404 */
405 for (int i = 0; i < MESA_SHADER_STAGES; ++i) {
406 if (!variants[i])
407 continue;
408
409 entry->variants[i] = variants[i];
410 p_atomic_inc(&variants[i]->ref_count);
411 }
412
413 radv_pipeline_cache_add_entry(cache, entry);
414
415 cache->modified = true;
416 pthread_mutex_unlock(&cache->mutex);
417 return;
418 }
419
420 struct cache_header {
421 uint32_t header_size;
422 uint32_t header_version;
423 uint32_t vendor_id;
424 uint32_t device_id;
425 uint8_t uuid[VK_UUID_SIZE];
426 };
427
428 bool
429 radv_pipeline_cache_load(struct radv_pipeline_cache *cache,
430 const void *data, size_t size)
431 {
432 struct radv_device *device = cache->device;
433 struct cache_header header;
434
435 if (size < sizeof(header))
436 return false;
437 memcpy(&header, data, sizeof(header));
438 if (header.header_size < sizeof(header))
439 return false;
440 if (header.header_version != VK_PIPELINE_CACHE_HEADER_VERSION_ONE)
441 return false;
442 if (header.vendor_id != ATI_VENDOR_ID)
443 return false;
444 if (header.device_id != device->physical_device->rad_info.pci_id)
445 return false;
446 if (memcmp(header.uuid, device->physical_device->cache_uuid, VK_UUID_SIZE) != 0)
447 return false;
448
449 char *end = (void *) data + size;
450 char *p = (void *) data + header.header_size;
451
452 while (end - p >= sizeof(struct cache_entry)) {
453 struct cache_entry *entry = (struct cache_entry*)p;
454 struct cache_entry *dest_entry;
455 size_t size = entry_size(entry);
456 if(end - p < size)
457 break;
458
459 dest_entry = vk_alloc(&cache->alloc, size,
460 8, VK_SYSTEM_ALLOCATION_SCOPE_CACHE);
461 if (dest_entry) {
462 memcpy(dest_entry, entry, size);
463 for (int i = 0; i < MESA_SHADER_STAGES; ++i)
464 dest_entry->variants[i] = NULL;
465 radv_pipeline_cache_add_entry(cache, dest_entry);
466 }
467 p += size;
468 }
469
470 return true;
471 }
472
473 VkResult radv_CreatePipelineCache(
474 VkDevice _device,
475 const VkPipelineCacheCreateInfo* pCreateInfo,
476 const VkAllocationCallbacks* pAllocator,
477 VkPipelineCache* pPipelineCache)
478 {
479 RADV_FROM_HANDLE(radv_device, device, _device);
480 struct radv_pipeline_cache *cache;
481
482 assert(pCreateInfo->sType == VK_STRUCTURE_TYPE_PIPELINE_CACHE_CREATE_INFO);
483 assert(pCreateInfo->flags == 0);
484
485 cache = vk_alloc2(&device->alloc, pAllocator,
486 sizeof(*cache), 8,
487 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
488 if (cache == NULL)
489 return vk_error(device->instance, VK_ERROR_OUT_OF_HOST_MEMORY);
490
491 if (pAllocator)
492 cache->alloc = *pAllocator;
493 else
494 cache->alloc = device->alloc;
495
496 radv_pipeline_cache_init(cache, device);
497
498 if (pCreateInfo->initialDataSize > 0) {
499 radv_pipeline_cache_load(cache,
500 pCreateInfo->pInitialData,
501 pCreateInfo->initialDataSize);
502 }
503
504 *pPipelineCache = radv_pipeline_cache_to_handle(cache);
505
506 return VK_SUCCESS;
507 }
508
509 void radv_DestroyPipelineCache(
510 VkDevice _device,
511 VkPipelineCache _cache,
512 const VkAllocationCallbacks* pAllocator)
513 {
514 RADV_FROM_HANDLE(radv_device, device, _device);
515 RADV_FROM_HANDLE(radv_pipeline_cache, cache, _cache);
516
517 if (!cache)
518 return;
519 radv_pipeline_cache_finish(cache);
520
521 vk_free2(&device->alloc, pAllocator, cache);
522 }
523
524 VkResult radv_GetPipelineCacheData(
525 VkDevice _device,
526 VkPipelineCache _cache,
527 size_t* pDataSize,
528 void* pData)
529 {
530 RADV_FROM_HANDLE(radv_device, device, _device);
531 RADV_FROM_HANDLE(radv_pipeline_cache, cache, _cache);
532 struct cache_header *header;
533 VkResult result = VK_SUCCESS;
534
535 pthread_mutex_lock(&cache->mutex);
536
537 const size_t size = sizeof(*header) + cache->total_size;
538 if (pData == NULL) {
539 pthread_mutex_unlock(&cache->mutex);
540 *pDataSize = size;
541 return VK_SUCCESS;
542 }
543 if (*pDataSize < sizeof(*header)) {
544 pthread_mutex_unlock(&cache->mutex);
545 *pDataSize = 0;
546 return VK_INCOMPLETE;
547 }
548 void *p = pData, *end = pData + *pDataSize;
549 header = p;
550 header->header_size = sizeof(*header);
551 header->header_version = VK_PIPELINE_CACHE_HEADER_VERSION_ONE;
552 header->vendor_id = ATI_VENDOR_ID;
553 header->device_id = device->physical_device->rad_info.pci_id;
554 memcpy(header->uuid, device->physical_device->cache_uuid, VK_UUID_SIZE);
555 p += header->header_size;
556
557 struct cache_entry *entry;
558 for (uint32_t i = 0; i < cache->table_size; i++) {
559 if (!cache->hash_table[i])
560 continue;
561 entry = cache->hash_table[i];
562 const uint32_t size = entry_size(entry);
563 if (end < p + size) {
564 result = VK_INCOMPLETE;
565 break;
566 }
567
568 memcpy(p, entry, size);
569 for(int j = 0; j < MESA_SHADER_STAGES; ++j)
570 ((struct cache_entry*)p)->variants[j] = NULL;
571 p += size;
572 }
573 *pDataSize = p - pData;
574
575 pthread_mutex_unlock(&cache->mutex);
576 return result;
577 }
578
579 static void
580 radv_pipeline_cache_merge(struct radv_pipeline_cache *dst,
581 struct radv_pipeline_cache *src)
582 {
583 for (uint32_t i = 0; i < src->table_size; i++) {
584 struct cache_entry *entry = src->hash_table[i];
585 if (!entry || radv_pipeline_cache_search(dst, entry->sha1))
586 continue;
587
588 radv_pipeline_cache_add_entry(dst, entry);
589
590 src->hash_table[i] = NULL;
591 }
592 }
593
594 VkResult radv_MergePipelineCaches(
595 VkDevice _device,
596 VkPipelineCache destCache,
597 uint32_t srcCacheCount,
598 const VkPipelineCache* pSrcCaches)
599 {
600 RADV_FROM_HANDLE(radv_pipeline_cache, dst, destCache);
601
602 for (uint32_t i = 0; i < srcCacheCount; i++) {
603 RADV_FROM_HANDLE(radv_pipeline_cache, src, pSrcCaches[i]);
604
605 radv_pipeline_cache_merge(dst, src);
606 }
607
608 return VK_SUCCESS;
609 }