util: use C99 declaration in the for-loop hash_table_foreach() macro
[mesa.git] / src / intel / vulkan / anv_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 "compiler/blob.h"
25 #include "util/hash_table.h"
26 #include "util/debug.h"
27 #include "util/disk_cache.h"
28 #include "util/mesa-sha1.h"
29 #include "anv_private.h"
30
31 struct anv_shader_bin *
32 anv_shader_bin_create(struct anv_device *device,
33 const void *key_data, uint32_t key_size,
34 const void *kernel_data, uint32_t kernel_size,
35 const void *constant_data, uint32_t constant_data_size,
36 const struct brw_stage_prog_data *prog_data_in,
37 uint32_t prog_data_size, const void *prog_data_param_in,
38 const struct anv_pipeline_bind_map *bind_map)
39 {
40 struct anv_shader_bin *shader;
41 struct anv_shader_bin_key *key;
42 struct brw_stage_prog_data *prog_data;
43 uint32_t *prog_data_param;
44 struct anv_pipeline_binding *surface_to_descriptor, *sampler_to_descriptor;
45
46 ANV_MULTIALLOC(ma);
47 anv_multialloc_add(&ma, &shader, 1);
48 anv_multialloc_add_size(&ma, &key, sizeof(*key) + key_size);
49 anv_multialloc_add_size(&ma, &prog_data, prog_data_size);
50 anv_multialloc_add(&ma, &prog_data_param, prog_data_in->nr_params);
51 anv_multialloc_add(&ma, &surface_to_descriptor,
52 bind_map->surface_count);
53 anv_multialloc_add(&ma, &sampler_to_descriptor,
54 bind_map->sampler_count);
55
56 if (!anv_multialloc_alloc(&ma, &device->alloc,
57 VK_SYSTEM_ALLOCATION_SCOPE_DEVICE))
58 return NULL;
59
60 shader->ref_cnt = 1;
61
62 key->size = key_size;
63 memcpy(key->data, key_data, key_size);
64 shader->key = key;
65
66 shader->kernel =
67 anv_state_pool_alloc(&device->instruction_state_pool, kernel_size, 64);
68 memcpy(shader->kernel.map, kernel_data, kernel_size);
69 shader->kernel_size = kernel_size;
70
71 if (constant_data_size) {
72 shader->constant_data =
73 anv_state_pool_alloc(&device->dynamic_state_pool,
74 constant_data_size, 32);
75 memcpy(shader->constant_data.map, constant_data, constant_data_size);
76 } else {
77 shader->constant_data = ANV_STATE_NULL;
78 }
79 shader->constant_data_size = constant_data_size;
80
81 memcpy(prog_data, prog_data_in, prog_data_size);
82 memcpy(prog_data_param, prog_data_param_in,
83 prog_data->nr_params * sizeof(*prog_data_param));
84 prog_data->param = prog_data_param;
85 shader->prog_data = prog_data;
86 shader->prog_data_size = prog_data_size;
87
88 shader->bind_map = *bind_map;
89 typed_memcpy(surface_to_descriptor, bind_map->surface_to_descriptor,
90 bind_map->surface_count);
91 shader->bind_map.surface_to_descriptor = surface_to_descriptor;
92 typed_memcpy(sampler_to_descriptor, bind_map->sampler_to_descriptor,
93 bind_map->sampler_count);
94 shader->bind_map.sampler_to_descriptor = sampler_to_descriptor;
95
96 return shader;
97 }
98
99 void
100 anv_shader_bin_destroy(struct anv_device *device,
101 struct anv_shader_bin *shader)
102 {
103 assert(shader->ref_cnt == 0);
104 anv_state_pool_free(&device->instruction_state_pool, shader->kernel);
105 anv_state_pool_free(&device->dynamic_state_pool, shader->constant_data);
106 vk_free(&device->alloc, shader);
107 }
108
109 static bool
110 anv_shader_bin_write_to_blob(const struct anv_shader_bin *shader,
111 struct blob *blob)
112 {
113 bool ok;
114
115 ok = blob_write_uint32(blob, shader->key->size);
116 ok = blob_write_bytes(blob, shader->key->data, shader->key->size);
117
118 ok = blob_write_uint32(blob, shader->kernel_size);
119 ok = blob_write_bytes(blob, shader->kernel.map, shader->kernel_size);
120
121 ok = blob_write_uint32(blob, shader->constant_data_size);
122 ok = blob_write_bytes(blob, shader->constant_data.map,
123 shader->constant_data_size);
124
125 ok = blob_write_uint32(blob, shader->prog_data_size);
126 ok = blob_write_bytes(blob, shader->prog_data, shader->prog_data_size);
127 ok = blob_write_bytes(blob, shader->prog_data->param,
128 shader->prog_data->nr_params *
129 sizeof(*shader->prog_data->param));
130
131 ok = blob_write_uint32(blob, shader->bind_map.surface_count);
132 ok = blob_write_uint32(blob, shader->bind_map.sampler_count);
133 ok = blob_write_uint32(blob, shader->bind_map.image_count);
134 ok = blob_write_bytes(blob, shader->bind_map.surface_to_descriptor,
135 shader->bind_map.surface_count *
136 sizeof(*shader->bind_map.surface_to_descriptor));
137 ok = blob_write_bytes(blob, shader->bind_map.sampler_to_descriptor,
138 shader->bind_map.sampler_count *
139 sizeof(*shader->bind_map.sampler_to_descriptor));
140
141 return ok;
142 }
143
144 static struct anv_shader_bin *
145 anv_shader_bin_create_from_blob(struct anv_device *device,
146 struct blob_reader *blob)
147 {
148 uint32_t key_size = blob_read_uint32(blob);
149 const void *key_data = blob_read_bytes(blob, key_size);
150
151 uint32_t kernel_size = blob_read_uint32(blob);
152 const void *kernel_data = blob_read_bytes(blob, kernel_size);
153
154 uint32_t constant_data_size = blob_read_uint32(blob);
155 const void *constant_data = blob_read_bytes(blob, constant_data_size);
156
157 uint32_t prog_data_size = blob_read_uint32(blob);
158 const struct brw_stage_prog_data *prog_data =
159 blob_read_bytes(blob, prog_data_size);
160 if (blob->overrun)
161 return NULL;
162 const void *prog_data_param =
163 blob_read_bytes(blob, prog_data->nr_params * sizeof(*prog_data->param));
164
165 struct anv_pipeline_bind_map bind_map;
166 bind_map.surface_count = blob_read_uint32(blob);
167 bind_map.sampler_count = blob_read_uint32(blob);
168 bind_map.image_count = blob_read_uint32(blob);
169 bind_map.surface_to_descriptor = (void *)
170 blob_read_bytes(blob, bind_map.surface_count *
171 sizeof(*bind_map.surface_to_descriptor));
172 bind_map.sampler_to_descriptor = (void *)
173 blob_read_bytes(blob, bind_map.sampler_count *
174 sizeof(*bind_map.sampler_to_descriptor));
175
176 if (blob->overrun)
177 return NULL;
178
179 return anv_shader_bin_create(device,
180 key_data, key_size,
181 kernel_data, kernel_size,
182 constant_data, constant_data_size,
183 prog_data, prog_data_size, prog_data_param,
184 &bind_map);
185 }
186
187 /* Remaining work:
188 *
189 * - Compact binding table layout so it's tight and not dependent on
190 * descriptor set layout.
191 *
192 * - Review prog_data struct for size and cacheability: struct
193 * brw_stage_prog_data has binding_table which uses a lot of uint32_t for 8
194 * bit quantities etc; use bit fields for all bools, eg dual_src_blend.
195 */
196
197 static uint32_t
198 shader_bin_key_hash_func(const void *void_key)
199 {
200 const struct anv_shader_bin_key *key = void_key;
201 return _mesa_hash_data(key->data, key->size);
202 }
203
204 static bool
205 shader_bin_key_compare_func(const void *void_a, const void *void_b)
206 {
207 const struct anv_shader_bin_key *a = void_a, *b = void_b;
208 if (a->size != b->size)
209 return false;
210
211 return memcmp(a->data, b->data, a->size) == 0;
212 }
213
214 void
215 anv_pipeline_cache_init(struct anv_pipeline_cache *cache,
216 struct anv_device *device,
217 bool cache_enabled)
218 {
219 cache->device = device;
220 pthread_mutex_init(&cache->mutex, NULL);
221
222 if (cache_enabled) {
223 cache->cache = _mesa_hash_table_create(NULL, shader_bin_key_hash_func,
224 shader_bin_key_compare_func);
225 } else {
226 cache->cache = NULL;
227 }
228 }
229
230 void
231 anv_pipeline_cache_finish(struct anv_pipeline_cache *cache)
232 {
233 pthread_mutex_destroy(&cache->mutex);
234
235 if (cache->cache) {
236 /* This is a bit unfortunate. In order to keep things from randomly
237 * going away, the shader cache has to hold a reference to all shader
238 * binaries it contains. We unref them when we destroy the cache.
239 */
240 hash_table_foreach(cache->cache, entry)
241 anv_shader_bin_unref(cache->device, entry->data);
242
243 _mesa_hash_table_destroy(cache->cache, NULL);
244 }
245 }
246
247 static struct anv_shader_bin *
248 anv_pipeline_cache_search_locked(struct anv_pipeline_cache *cache,
249 const void *key_data, uint32_t key_size)
250 {
251 uint32_t vla[1 + DIV_ROUND_UP(key_size, sizeof(uint32_t))];
252 struct anv_shader_bin_key *key = (void *)vla;
253 key->size = key_size;
254 memcpy(key->data, key_data, key_size);
255
256 struct hash_entry *entry = _mesa_hash_table_search(cache->cache, key);
257 if (entry)
258 return entry->data;
259 else
260 return NULL;
261 }
262
263 struct anv_shader_bin *
264 anv_pipeline_cache_search(struct anv_pipeline_cache *cache,
265 const void *key_data, uint32_t key_size)
266 {
267 if (!cache->cache)
268 return NULL;
269
270 pthread_mutex_lock(&cache->mutex);
271
272 struct anv_shader_bin *shader =
273 anv_pipeline_cache_search_locked(cache, key_data, key_size);
274
275 pthread_mutex_unlock(&cache->mutex);
276
277 /* We increment refcount before handing it to the caller */
278 if (shader)
279 anv_shader_bin_ref(shader);
280
281 return shader;
282 }
283
284 static void
285 anv_pipeline_cache_add_shader_bin(struct anv_pipeline_cache *cache,
286 struct anv_shader_bin *bin)
287 {
288 if (!cache->cache)
289 return;
290
291 pthread_mutex_lock(&cache->mutex);
292
293 struct hash_entry *entry = _mesa_hash_table_search(cache->cache, bin->key);
294 if (entry == NULL) {
295 /* Take a reference for the cache */
296 anv_shader_bin_ref(bin);
297 _mesa_hash_table_insert(cache->cache, bin->key, bin);
298 }
299
300 pthread_mutex_unlock(&cache->mutex);
301 }
302
303 static struct anv_shader_bin *
304 anv_pipeline_cache_add_shader_locked(struct anv_pipeline_cache *cache,
305 const void *key_data, uint32_t key_size,
306 const void *kernel_data,
307 uint32_t kernel_size,
308 const void *constant_data,
309 uint32_t constant_data_size,
310 const struct brw_stage_prog_data *prog_data,
311 uint32_t prog_data_size,
312 const void *prog_data_param,
313 const struct anv_pipeline_bind_map *bind_map)
314 {
315 struct anv_shader_bin *shader =
316 anv_pipeline_cache_search_locked(cache, key_data, key_size);
317 if (shader)
318 return shader;
319
320 struct anv_shader_bin *bin =
321 anv_shader_bin_create(cache->device, key_data, key_size,
322 kernel_data, kernel_size,
323 constant_data, constant_data_size,
324 prog_data, prog_data_size, prog_data_param,
325 bind_map);
326 if (!bin)
327 return NULL;
328
329 _mesa_hash_table_insert(cache->cache, bin->key, bin);
330
331 return bin;
332 }
333
334 struct anv_shader_bin *
335 anv_pipeline_cache_upload_kernel(struct anv_pipeline_cache *cache,
336 const void *key_data, uint32_t key_size,
337 const void *kernel_data, uint32_t kernel_size,
338 const void *constant_data,
339 uint32_t constant_data_size,
340 const struct brw_stage_prog_data *prog_data,
341 uint32_t prog_data_size,
342 const struct anv_pipeline_bind_map *bind_map)
343 {
344 if (cache->cache) {
345 pthread_mutex_lock(&cache->mutex);
346
347 struct anv_shader_bin *bin =
348 anv_pipeline_cache_add_shader_locked(cache, key_data, key_size,
349 kernel_data, kernel_size,
350 constant_data, constant_data_size,
351 prog_data, prog_data_size,
352 prog_data->param, bind_map);
353
354 pthread_mutex_unlock(&cache->mutex);
355
356 /* We increment refcount before handing it to the caller */
357 if (bin)
358 anv_shader_bin_ref(bin);
359
360 return bin;
361 } else {
362 /* In this case, we're not caching it so the caller owns it entirely */
363 return anv_shader_bin_create(cache->device, key_data, key_size,
364 kernel_data, kernel_size,
365 constant_data, constant_data_size,
366 prog_data, prog_data_size,
367 prog_data->param, bind_map);
368 }
369 }
370
371 struct cache_header {
372 uint32_t header_size;
373 uint32_t header_version;
374 uint32_t vendor_id;
375 uint32_t device_id;
376 uint8_t uuid[VK_UUID_SIZE];
377 };
378
379 static void
380 anv_pipeline_cache_load(struct anv_pipeline_cache *cache,
381 const void *data, size_t size)
382 {
383 struct anv_device *device = cache->device;
384 struct anv_physical_device *pdevice = &device->instance->physicalDevice;
385
386 if (cache->cache == NULL)
387 return;
388
389 struct blob_reader blob;
390 blob_reader_init(&blob, data, size);
391
392 struct cache_header header;
393 blob_copy_bytes(&blob, &header, sizeof(header));
394 uint32_t count = blob_read_uint32(&blob);
395 if (blob.overrun)
396 return;
397
398 if (header.header_size < sizeof(header))
399 return;
400 if (header.header_version != VK_PIPELINE_CACHE_HEADER_VERSION_ONE)
401 return;
402 if (header.vendor_id != 0x8086)
403 return;
404 if (header.device_id != device->chipset_id)
405 return;
406 if (memcmp(header.uuid, pdevice->pipeline_cache_uuid, VK_UUID_SIZE) != 0)
407 return;
408
409 for (uint32_t i = 0; i < count; i++) {
410 struct anv_shader_bin *bin =
411 anv_shader_bin_create_from_blob(device, &blob);
412 if (!bin)
413 break;
414 _mesa_hash_table_insert(cache->cache, bin->key, bin);
415 }
416 }
417
418 VkResult anv_CreatePipelineCache(
419 VkDevice _device,
420 const VkPipelineCacheCreateInfo* pCreateInfo,
421 const VkAllocationCallbacks* pAllocator,
422 VkPipelineCache* pPipelineCache)
423 {
424 ANV_FROM_HANDLE(anv_device, device, _device);
425 struct anv_pipeline_cache *cache;
426
427 assert(pCreateInfo->sType == VK_STRUCTURE_TYPE_PIPELINE_CACHE_CREATE_INFO);
428 assert(pCreateInfo->flags == 0);
429
430 cache = vk_alloc2(&device->alloc, pAllocator,
431 sizeof(*cache), 8,
432 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
433 if (cache == NULL)
434 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
435
436 anv_pipeline_cache_init(cache, device,
437 device->instance->pipeline_cache_enabled);
438
439 if (pCreateInfo->initialDataSize > 0)
440 anv_pipeline_cache_load(cache,
441 pCreateInfo->pInitialData,
442 pCreateInfo->initialDataSize);
443
444 *pPipelineCache = anv_pipeline_cache_to_handle(cache);
445
446 return VK_SUCCESS;
447 }
448
449 void anv_DestroyPipelineCache(
450 VkDevice _device,
451 VkPipelineCache _cache,
452 const VkAllocationCallbacks* pAllocator)
453 {
454 ANV_FROM_HANDLE(anv_device, device, _device);
455 ANV_FROM_HANDLE(anv_pipeline_cache, cache, _cache);
456
457 if (!cache)
458 return;
459
460 anv_pipeline_cache_finish(cache);
461
462 vk_free2(&device->alloc, pAllocator, cache);
463 }
464
465 VkResult anv_GetPipelineCacheData(
466 VkDevice _device,
467 VkPipelineCache _cache,
468 size_t* pDataSize,
469 void* pData)
470 {
471 ANV_FROM_HANDLE(anv_device, device, _device);
472 ANV_FROM_HANDLE(anv_pipeline_cache, cache, _cache);
473 struct anv_physical_device *pdevice = &device->instance->physicalDevice;
474
475 struct blob blob;
476 if (pData) {
477 blob_init_fixed(&blob, pData, *pDataSize);
478 } else {
479 blob_init_fixed(&blob, NULL, SIZE_MAX);
480 }
481
482 struct cache_header header = {
483 .header_size = sizeof(struct cache_header),
484 .header_version = VK_PIPELINE_CACHE_HEADER_VERSION_ONE,
485 .vendor_id = 0x8086,
486 .device_id = device->chipset_id,
487 };
488 memcpy(header.uuid, pdevice->pipeline_cache_uuid, VK_UUID_SIZE);
489 blob_write_bytes(&blob, &header, sizeof(header));
490
491 uint32_t count = 0;
492 intptr_t count_offset = blob_reserve_uint32(&blob);
493 if (count_offset < 0) {
494 *pDataSize = 0;
495 blob_finish(&blob);
496 return VK_INCOMPLETE;
497 }
498
499 VkResult result = VK_SUCCESS;
500 if (cache->cache) {
501 hash_table_foreach(cache->cache, entry) {
502 struct anv_shader_bin *shader = entry->data;
503
504 size_t save_size = blob.size;
505 if (!anv_shader_bin_write_to_blob(shader, &blob)) {
506 /* If it fails reset to the previous size and bail */
507 blob.size = save_size;
508 result = VK_INCOMPLETE;
509 break;
510 }
511
512 count++;
513 }
514 }
515
516 blob_overwrite_uint32(&blob, count_offset, count);
517
518 *pDataSize = blob.size;
519
520 blob_finish(&blob);
521
522 return result;
523 }
524
525 VkResult anv_MergePipelineCaches(
526 VkDevice _device,
527 VkPipelineCache destCache,
528 uint32_t srcCacheCount,
529 const VkPipelineCache* pSrcCaches)
530 {
531 ANV_FROM_HANDLE(anv_pipeline_cache, dst, destCache);
532
533 if (!dst->cache)
534 return VK_SUCCESS;
535
536 for (uint32_t i = 0; i < srcCacheCount; i++) {
537 ANV_FROM_HANDLE(anv_pipeline_cache, src, pSrcCaches[i]);
538 if (!src->cache)
539 continue;
540
541 hash_table_foreach(src->cache, entry) {
542 struct anv_shader_bin *bin = entry->data;
543 assert(bin);
544
545 if (_mesa_hash_table_search(dst->cache, bin->key))
546 continue;
547
548 anv_shader_bin_ref(bin);
549 _mesa_hash_table_insert(dst->cache, bin->key, bin);
550 }
551 }
552
553 return VK_SUCCESS;
554 }
555
556 struct anv_shader_bin *
557 anv_device_search_for_kernel(struct anv_device *device,
558 struct anv_pipeline_cache *cache,
559 const void *key_data, uint32_t key_size)
560 {
561 struct anv_shader_bin *bin;
562
563 if (cache) {
564 bin = anv_pipeline_cache_search(cache, key_data, key_size);
565 if (bin)
566 return bin;
567 }
568
569 #ifdef ENABLE_SHADER_CACHE
570 struct disk_cache *disk_cache = device->instance->physicalDevice.disk_cache;
571 if (disk_cache && device->instance->pipeline_cache_enabled) {
572 cache_key cache_key;
573 disk_cache_compute_key(disk_cache, key_data, key_size, cache_key);
574
575 size_t buffer_size;
576 uint8_t *buffer = disk_cache_get(disk_cache, cache_key, &buffer_size);
577 if (buffer) {
578 struct blob_reader blob;
579 blob_reader_init(&blob, buffer, buffer_size);
580 bin = anv_shader_bin_create_from_blob(device, &blob);
581 free(buffer);
582
583 if (bin) {
584 if (cache)
585 anv_pipeline_cache_add_shader_bin(cache, bin);
586 return bin;
587 }
588 }
589 }
590 #endif
591
592 return NULL;
593 }
594
595 struct anv_shader_bin *
596 anv_device_upload_kernel(struct anv_device *device,
597 struct anv_pipeline_cache *cache,
598 const void *key_data, uint32_t key_size,
599 const void *kernel_data, uint32_t kernel_size,
600 const void *constant_data,
601 uint32_t constant_data_size,
602 const struct brw_stage_prog_data *prog_data,
603 uint32_t prog_data_size,
604 const struct anv_pipeline_bind_map *bind_map)
605 {
606 struct anv_shader_bin *bin;
607 if (cache) {
608 bin = anv_pipeline_cache_upload_kernel(cache, key_data, key_size,
609 kernel_data, kernel_size,
610 constant_data, constant_data_size,
611 prog_data, prog_data_size,
612 bind_map);
613 } else {
614 bin = anv_shader_bin_create(device, key_data, key_size,
615 kernel_data, kernel_size,
616 constant_data, constant_data_size,
617 prog_data, prog_data_size,
618 prog_data->param, bind_map);
619 }
620
621 if (bin == NULL)
622 return NULL;
623
624 #ifdef ENABLE_SHADER_CACHE
625 struct disk_cache *disk_cache = device->instance->physicalDevice.disk_cache;
626 if (disk_cache) {
627 struct blob binary;
628 blob_init(&binary);
629 anv_shader_bin_write_to_blob(bin, &binary);
630
631 if (!binary.out_of_memory) {
632 cache_key cache_key;
633 disk_cache_compute_key(disk_cache, key_data, key_size, cache_key);
634
635 disk_cache_put(disk_cache, cache_key, binary.data, binary.size, NULL);
636 }
637
638 blob_finish(&binary);
639 }
640 #endif
641
642 return bin;
643 }