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