5f6355f0d1a670981acc1954e342ec48058d9d23
[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 "radv_private.h"
27
28 #include "ac_nir_to_llvm.h"
29
30 struct cache_entry {
31 union {
32 unsigned char sha1[20];
33 uint32_t sha1_dw[5];
34 };
35 uint32_t code_size;
36 struct ac_shader_variant_info variant_info;
37 struct ac_shader_config config;
38 uint32_t rsrc1, rsrc2;
39 struct radv_shader_variant *variant;
40 uint32_t code[0];
41 };
42
43 void
44 radv_pipeline_cache_init(struct radv_pipeline_cache *cache,
45 struct radv_device *device)
46 {
47 cache->device = device;
48 pthread_mutex_init(&cache->mutex, NULL);
49
50 cache->modified = false;
51 cache->kernel_count = 0;
52 cache->total_size = 0;
53 cache->table_size = 1024;
54 const size_t byte_size = cache->table_size * sizeof(cache->hash_table[0]);
55 cache->hash_table = malloc(byte_size);
56
57 /* We don't consider allocation failure fatal, we just start with a 0-sized
58 * cache. */
59 if (cache->hash_table == NULL ||
60 (device->debug_flags & RADV_DEBUG_NO_CACHE))
61 cache->table_size = 0;
62 else
63 memset(cache->hash_table, 0, byte_size);
64 }
65
66 void
67 radv_pipeline_cache_finish(struct radv_pipeline_cache *cache)
68 {
69 for (unsigned i = 0; i < cache->table_size; ++i)
70 if (cache->hash_table[i]) {
71 if (cache->hash_table[i]->variant)
72 radv_shader_variant_destroy(cache->device,
73 cache->hash_table[i]->variant);
74 vk_free(&cache->alloc, cache->hash_table[i]);
75 }
76 pthread_mutex_destroy(&cache->mutex);
77 free(cache->hash_table);
78 }
79
80 static uint32_t
81 entry_size(struct cache_entry *entry)
82 {
83 return sizeof(*entry) + entry->code_size;
84 }
85
86 void
87 radv_hash_shader(unsigned char *hash, struct radv_shader_module *module,
88 const char *entrypoint,
89 const VkSpecializationInfo *spec_info,
90 const struct radv_pipeline_layout *layout,
91 const union ac_shader_variant_key *key,
92 uint32_t is_geom_copy_shader)
93 {
94 struct mesa_sha1 ctx;
95
96 _mesa_sha1_init(&ctx);
97 if (key)
98 _mesa_sha1_update(&ctx, key, sizeof(*key));
99 _mesa_sha1_update(&ctx, module->sha1, sizeof(module->sha1));
100 _mesa_sha1_update(&ctx, entrypoint, strlen(entrypoint));
101 if (layout)
102 _mesa_sha1_update(&ctx, layout->sha1, sizeof(layout->sha1));
103 if (spec_info) {
104 _mesa_sha1_update(&ctx, spec_info->pMapEntries,
105 spec_info->mapEntryCount * sizeof spec_info->pMapEntries[0]);
106 _mesa_sha1_update(&ctx, spec_info->pData, spec_info->dataSize);
107 }
108 _mesa_sha1_update(&ctx, &is_geom_copy_shader, 4);
109 _mesa_sha1_final(&ctx, hash);
110 }
111
112
113 static struct cache_entry *
114 radv_pipeline_cache_search_unlocked(struct radv_pipeline_cache *cache,
115 const unsigned char *sha1)
116 {
117 const uint32_t mask = cache->table_size - 1;
118 const uint32_t start = (*(uint32_t *) sha1);
119
120 for (uint32_t i = 0; i < cache->table_size; i++) {
121 const uint32_t index = (start + i) & mask;
122 struct cache_entry *entry = cache->hash_table[index];
123
124 if (!entry)
125 return NULL;
126
127 if (memcmp(entry->sha1, sha1, sizeof(entry->sha1)) == 0) {
128 return entry;
129 }
130 }
131
132 unreachable("hash table should never be full");
133 }
134
135 static struct cache_entry *
136 radv_pipeline_cache_search(struct radv_pipeline_cache *cache,
137 const unsigned char *sha1)
138 {
139 struct cache_entry *entry;
140
141 pthread_mutex_lock(&cache->mutex);
142
143 entry = radv_pipeline_cache_search_unlocked(cache, sha1);
144
145 pthread_mutex_unlock(&cache->mutex);
146
147 return entry;
148 }
149
150 struct radv_shader_variant *
151 radv_create_shader_variant_from_pipeline_cache(struct radv_device *device,
152 struct radv_pipeline_cache *cache,
153 const unsigned char *sha1)
154 {
155 struct cache_entry *entry = NULL;
156
157 if (cache)
158 entry = radv_pipeline_cache_search(cache, sha1);
159
160 if (!entry)
161 return NULL;
162
163 if (!entry->variant) {
164 struct radv_shader_variant *variant;
165
166 variant = calloc(1, sizeof(struct radv_shader_variant));
167 if (!variant)
168 return NULL;
169
170 variant->config = entry->config;
171 variant->info = entry->variant_info;
172 variant->rsrc1 = entry->rsrc1;
173 variant->rsrc2 = entry->rsrc2;
174 variant->ref_count = 1;
175
176 variant->bo = device->ws->buffer_create(device->ws, entry->code_size, 256,
177 RADEON_DOMAIN_VRAM, RADEON_FLAG_CPU_ACCESS);
178
179 void *ptr = device->ws->buffer_map(variant->bo);
180 memcpy(ptr, entry->code, entry->code_size);
181 device->ws->buffer_unmap(variant->bo);
182
183 entry->variant = variant;
184 }
185
186 __sync_fetch_and_add(&entry->variant->ref_count, 1);
187 return entry->variant;
188 }
189
190
191 static void
192 radv_pipeline_cache_set_entry(struct radv_pipeline_cache *cache,
193 struct cache_entry *entry)
194 {
195 const uint32_t mask = cache->table_size - 1;
196 const uint32_t start = entry->sha1_dw[0];
197
198 /* We'll always be able to insert when we get here. */
199 assert(cache->kernel_count < cache->table_size / 2);
200
201 for (uint32_t i = 0; i < cache->table_size; i++) {
202 const uint32_t index = (start + i) & mask;
203 if (!cache->hash_table[index]) {
204 cache->hash_table[index] = entry;
205 break;
206 }
207 }
208
209 cache->total_size += entry_size(entry);
210 cache->kernel_count++;
211 }
212
213
214 static VkResult
215 radv_pipeline_cache_grow(struct radv_pipeline_cache *cache)
216 {
217 const uint32_t table_size = cache->table_size * 2;
218 const uint32_t old_table_size = cache->table_size;
219 const size_t byte_size = table_size * sizeof(cache->hash_table[0]);
220 struct cache_entry **table;
221 struct cache_entry **old_table = cache->hash_table;
222
223 table = malloc(byte_size);
224 if (table == NULL)
225 return VK_ERROR_OUT_OF_HOST_MEMORY;
226
227 cache->hash_table = table;
228 cache->table_size = table_size;
229 cache->kernel_count = 0;
230 cache->total_size = 0;
231
232 memset(cache->hash_table, 0, byte_size);
233 for (uint32_t i = 0; i < old_table_size; i++) {
234 struct cache_entry *entry = old_table[i];
235 if (!entry)
236 continue;
237
238 radv_pipeline_cache_set_entry(cache, entry);
239 }
240
241 free(old_table);
242
243 return VK_SUCCESS;
244 }
245
246 static void
247 radv_pipeline_cache_add_entry(struct radv_pipeline_cache *cache,
248 struct cache_entry *entry)
249 {
250 if (cache->kernel_count == cache->table_size / 2)
251 radv_pipeline_cache_grow(cache);
252
253 /* Failing to grow that hash table isn't fatal, but may mean we don't
254 * have enough space to add this new kernel. Only add it if there's room.
255 */
256 if (cache->kernel_count < cache->table_size / 2)
257 radv_pipeline_cache_set_entry(cache, entry);
258 }
259
260 struct radv_shader_variant *
261 radv_pipeline_cache_insert_shader(struct radv_pipeline_cache *cache,
262 const unsigned char *sha1,
263 struct radv_shader_variant *variant,
264 const void *code, unsigned code_size)
265 {
266 if (!cache)
267 return variant;
268
269 pthread_mutex_lock(&cache->mutex);
270 struct cache_entry *entry = radv_pipeline_cache_search_unlocked(cache, sha1);
271 if (entry) {
272 if (entry->variant) {
273 radv_shader_variant_destroy(cache->device, variant);
274 variant = entry->variant;
275 } else {
276 entry->variant = variant;
277 }
278 __sync_fetch_and_add(&variant->ref_count, 1);
279 pthread_mutex_unlock(&cache->mutex);
280 return variant;
281 }
282
283 entry = vk_alloc(&cache->alloc, sizeof(*entry) + code_size, 8,
284 VK_SYSTEM_ALLOCATION_SCOPE_CACHE);
285 if (!entry) {
286 pthread_mutex_unlock(&cache->mutex);
287 return variant;
288 }
289
290 memcpy(entry->sha1, sha1, 20);
291 memcpy(entry->code, code, code_size);
292 entry->config = variant->config;
293 entry->variant_info = variant->info;
294 entry->rsrc1 = variant->rsrc1;
295 entry->rsrc2 = variant->rsrc2;
296 entry->code_size = code_size;
297 entry->variant = variant;
298 __sync_fetch_and_add(&variant->ref_count, 1);
299
300 radv_pipeline_cache_add_entry(cache, entry);
301
302 cache->modified = true;
303 pthread_mutex_unlock(&cache->mutex);
304 return variant;
305 }
306
307 struct cache_header {
308 uint32_t header_size;
309 uint32_t header_version;
310 uint32_t vendor_id;
311 uint32_t device_id;
312 uint8_t uuid[VK_UUID_SIZE];
313 };
314
315 void
316 radv_pipeline_cache_load(struct radv_pipeline_cache *cache,
317 const void *data, size_t size)
318 {
319 struct radv_device *device = cache->device;
320 struct cache_header header;
321
322 if (size < sizeof(header))
323 return;
324 memcpy(&header, data, sizeof(header));
325 if (header.header_size < sizeof(header))
326 return;
327 if (header.header_version != VK_PIPELINE_CACHE_HEADER_VERSION_ONE)
328 return;
329 if (header.vendor_id != 0x1002)
330 return;
331 if (header.device_id != device->physical_device->rad_info.pci_id)
332 return;
333 if (memcmp(header.uuid, device->physical_device->uuid, VK_UUID_SIZE) != 0)
334 return;
335
336 char *end = (void *) data + size;
337 char *p = (void *) data + header.header_size;
338
339 while (end - p >= sizeof(struct cache_entry)) {
340 struct cache_entry *entry = (struct cache_entry*)p;
341 struct cache_entry *dest_entry;
342 if(end - p < sizeof(*entry) + entry->code_size)
343 break;
344
345 dest_entry = vk_alloc(&cache->alloc, sizeof(*entry) + entry->code_size,
346 8, VK_SYSTEM_ALLOCATION_SCOPE_CACHE);
347 if (dest_entry) {
348 memcpy(dest_entry, entry, sizeof(*entry) + entry->code_size);
349 dest_entry->variant = NULL;
350 radv_pipeline_cache_add_entry(cache, dest_entry);
351 }
352 p += sizeof (*entry) + entry->code_size;
353 }
354 }
355
356 VkResult radv_CreatePipelineCache(
357 VkDevice _device,
358 const VkPipelineCacheCreateInfo* pCreateInfo,
359 const VkAllocationCallbacks* pAllocator,
360 VkPipelineCache* pPipelineCache)
361 {
362 RADV_FROM_HANDLE(radv_device, device, _device);
363 struct radv_pipeline_cache *cache;
364
365 assert(pCreateInfo->sType == VK_STRUCTURE_TYPE_PIPELINE_CACHE_CREATE_INFO);
366 assert(pCreateInfo->flags == 0);
367
368 cache = vk_alloc2(&device->alloc, pAllocator,
369 sizeof(*cache), 8,
370 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
371 if (cache == NULL)
372 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
373
374 if (pAllocator)
375 cache->alloc = *pAllocator;
376 else
377 cache->alloc = device->alloc;
378
379 radv_pipeline_cache_init(cache, device);
380
381 if (pCreateInfo->initialDataSize > 0) {
382 radv_pipeline_cache_load(cache,
383 pCreateInfo->pInitialData,
384 pCreateInfo->initialDataSize);
385 }
386
387 *pPipelineCache = radv_pipeline_cache_to_handle(cache);
388
389 return VK_SUCCESS;
390 }
391
392 void radv_DestroyPipelineCache(
393 VkDevice _device,
394 VkPipelineCache _cache,
395 const VkAllocationCallbacks* pAllocator)
396 {
397 RADV_FROM_HANDLE(radv_device, device, _device);
398 RADV_FROM_HANDLE(radv_pipeline_cache, cache, _cache);
399
400 if (!cache)
401 return;
402 radv_pipeline_cache_finish(cache);
403
404 vk_free2(&device->alloc, pAllocator, cache);
405 }
406
407 VkResult radv_GetPipelineCacheData(
408 VkDevice _device,
409 VkPipelineCache _cache,
410 size_t* pDataSize,
411 void* pData)
412 {
413 RADV_FROM_HANDLE(radv_device, device, _device);
414 RADV_FROM_HANDLE(radv_pipeline_cache, cache, _cache);
415 struct cache_header *header;
416 VkResult result = VK_SUCCESS;
417 const size_t size = sizeof(*header) + cache->total_size;
418 if (pData == NULL) {
419 *pDataSize = size;
420 return VK_SUCCESS;
421 }
422 if (*pDataSize < sizeof(*header)) {
423 *pDataSize = 0;
424 return VK_INCOMPLETE;
425 }
426 void *p = pData, *end = pData + *pDataSize;
427 header = p;
428 header->header_size = sizeof(*header);
429 header->header_version = VK_PIPELINE_CACHE_HEADER_VERSION_ONE;
430 header->vendor_id = 0x1002;
431 header->device_id = device->physical_device->rad_info.pci_id;
432 memcpy(header->uuid, device->physical_device->uuid, VK_UUID_SIZE);
433 p += header->header_size;
434
435 struct cache_entry *entry;
436 for (uint32_t i = 0; i < cache->table_size; i++) {
437 if (!cache->hash_table[i])
438 continue;
439 entry = cache->hash_table[i];
440 const uint32_t size = entry_size(entry);
441 if (end < p + size) {
442 result = VK_INCOMPLETE;
443 break;
444 }
445
446 memcpy(p, entry, size);
447 ((struct cache_entry*)p)->variant = NULL;
448 p += size;
449 }
450 *pDataSize = p - pData;
451
452 return result;
453 }
454
455 static void
456 radv_pipeline_cache_merge(struct radv_pipeline_cache *dst,
457 struct radv_pipeline_cache *src)
458 {
459 for (uint32_t i = 0; i < src->table_size; i++) {
460 struct cache_entry *entry = src->hash_table[i];
461 if (!entry || radv_pipeline_cache_search(dst, entry->sha1))
462 continue;
463
464 radv_pipeline_cache_add_entry(dst, entry);
465
466 src->hash_table[i] = NULL;
467 }
468 }
469
470 VkResult radv_MergePipelineCaches(
471 VkDevice _device,
472 VkPipelineCache destCache,
473 uint32_t srcCacheCount,
474 const VkPipelineCache* pSrcCaches)
475 {
476 RADV_FROM_HANDLE(radv_pipeline_cache, dst, destCache);
477
478 for (uint32_t i = 0; i < srcCacheCount; i++) {
479 RADV_FROM_HANDLE(radv_pipeline_cache, src, pSrcCaches[i]);
480
481 radv_pipeline_cache_merge(dst, src);
482 }
483
484 return VK_SUCCESS;
485 }