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