2 * Copyright © 2015 Intel Corporation
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:
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
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
24 #include "util/mesa-sha1.h"
25 #include "util/debug.h"
26 #include "util/u_atomic.h"
27 #include "radv_debug.h"
28 #include "radv_private.h"
29 #include "radv_shader.h"
31 #include "ac_nir_to_llvm.h"
35 unsigned char sha1
[20];
39 struct ac_shader_variant_info variant_info
;
40 struct ac_shader_config config
;
41 uint32_t rsrc1
, rsrc2
;
42 struct radv_shader_variant
*variant
;
47 radv_pipeline_cache_init(struct radv_pipeline_cache
*cache
,
48 struct radv_device
*device
)
50 cache
->device
= device
;
51 pthread_mutex_init(&cache
->mutex
, NULL
);
53 cache
->modified
= false;
54 cache
->kernel_count
= 0;
55 cache
->total_size
= 0;
56 cache
->table_size
= 1024;
57 const size_t byte_size
= cache
->table_size
* sizeof(cache
->hash_table
[0]);
58 cache
->hash_table
= malloc(byte_size
);
60 /* We don't consider allocation failure fatal, we just start with a 0-sized
62 if (cache
->hash_table
== NULL
||
63 (device
->instance
->debug_flags
& RADV_DEBUG_NO_CACHE
))
64 cache
->table_size
= 0;
66 memset(cache
->hash_table
, 0, byte_size
);
70 radv_pipeline_cache_finish(struct radv_pipeline_cache
*cache
)
72 for (unsigned i
= 0; i
< cache
->table_size
; ++i
)
73 if (cache
->hash_table
[i
]) {
74 if (cache
->hash_table
[i
]->variant
)
75 radv_shader_variant_destroy(cache
->device
,
76 cache
->hash_table
[i
]->variant
);
77 vk_free(&cache
->alloc
, cache
->hash_table
[i
]);
79 pthread_mutex_destroy(&cache
->mutex
);
80 free(cache
->hash_table
);
84 entry_size(struct cache_entry
*entry
)
86 return sizeof(*entry
) + entry
->code_size
;
90 radv_hash_shader(unsigned char *hash
, struct radv_shader_module
*module
,
91 const char *entrypoint
,
92 const VkSpecializationInfo
*spec_info
,
93 const struct radv_pipeline_layout
*layout
,
94 const struct ac_shader_variant_key
*key
,
95 uint32_t is_geom_copy_shader
)
99 _mesa_sha1_init(&ctx
);
101 _mesa_sha1_update(&ctx
, key
, sizeof(*key
));
102 _mesa_sha1_update(&ctx
, module
->sha1
, sizeof(module
->sha1
));
103 _mesa_sha1_update(&ctx
, entrypoint
, strlen(entrypoint
));
105 _mesa_sha1_update(&ctx
, layout
->sha1
, sizeof(layout
->sha1
));
107 _mesa_sha1_update(&ctx
, spec_info
->pMapEntries
,
108 spec_info
->mapEntryCount
* sizeof spec_info
->pMapEntries
[0]);
109 _mesa_sha1_update(&ctx
, spec_info
->pData
, spec_info
->dataSize
);
111 _mesa_sha1_update(&ctx
, &is_geom_copy_shader
, 4);
112 _mesa_sha1_final(&ctx
, hash
);
116 static struct cache_entry
*
117 radv_pipeline_cache_search_unlocked(struct radv_pipeline_cache
*cache
,
118 const unsigned char *sha1
)
120 const uint32_t mask
= cache
->table_size
- 1;
121 const uint32_t start
= (*(uint32_t *) sha1
);
123 if (cache
->table_size
== 0)
126 for (uint32_t i
= 0; i
< cache
->table_size
; i
++) {
127 const uint32_t index
= (start
+ i
) & mask
;
128 struct cache_entry
*entry
= cache
->hash_table
[index
];
133 if (memcmp(entry
->sha1
, sha1
, sizeof(entry
->sha1
)) == 0) {
138 unreachable("hash table should never be full");
141 static struct cache_entry
*
142 radv_pipeline_cache_search(struct radv_pipeline_cache
*cache
,
143 const unsigned char *sha1
)
145 struct cache_entry
*entry
;
147 pthread_mutex_lock(&cache
->mutex
);
149 entry
= radv_pipeline_cache_search_unlocked(cache
, sha1
);
151 pthread_mutex_unlock(&cache
->mutex
);
156 struct radv_shader_variant
*
157 radv_create_shader_variant_from_pipeline_cache(struct radv_device
*device
,
158 struct radv_pipeline_cache
*cache
,
159 const unsigned char *sha1
)
161 struct cache_entry
*entry
= NULL
;
164 entry
= radv_pipeline_cache_search(cache
, sha1
);
166 entry
= radv_pipeline_cache_search(device
->mem_cache
, sha1
);
171 if (!entry
->variant
) {
172 struct radv_shader_variant
*variant
;
174 variant
= calloc(1, sizeof(struct radv_shader_variant
));
178 variant
->code_size
= entry
->code_size
;
179 variant
->config
= entry
->config
;
180 variant
->info
= entry
->variant_info
;
181 variant
->rsrc1
= entry
->rsrc1
;
182 variant
->rsrc2
= entry
->rsrc2
;
183 variant
->code_size
= entry
->code_size
;
184 variant
->ref_count
= 1;
186 void *ptr
= radv_alloc_shader_memory(device
, variant
);
187 memcpy(ptr
, entry
->code
, entry
->code_size
);
189 entry
->variant
= variant
;
192 p_atomic_inc(&entry
->variant
->ref_count
);
193 return entry
->variant
;
198 radv_pipeline_cache_set_entry(struct radv_pipeline_cache
*cache
,
199 struct cache_entry
*entry
)
201 const uint32_t mask
= cache
->table_size
- 1;
202 const uint32_t start
= entry
->sha1_dw
[0];
204 /* We'll always be able to insert when we get here. */
205 assert(cache
->kernel_count
< cache
->table_size
/ 2);
207 for (uint32_t i
= 0; i
< cache
->table_size
; i
++) {
208 const uint32_t index
= (start
+ i
) & mask
;
209 if (!cache
->hash_table
[index
]) {
210 cache
->hash_table
[index
] = entry
;
215 cache
->total_size
+= entry_size(entry
);
216 cache
->kernel_count
++;
221 radv_pipeline_cache_grow(struct radv_pipeline_cache
*cache
)
223 const uint32_t table_size
= cache
->table_size
* 2;
224 const uint32_t old_table_size
= cache
->table_size
;
225 const size_t byte_size
= table_size
* sizeof(cache
->hash_table
[0]);
226 struct cache_entry
**table
;
227 struct cache_entry
**old_table
= cache
->hash_table
;
229 table
= malloc(byte_size
);
231 return VK_ERROR_OUT_OF_HOST_MEMORY
;
233 cache
->hash_table
= table
;
234 cache
->table_size
= table_size
;
235 cache
->kernel_count
= 0;
236 cache
->total_size
= 0;
238 memset(cache
->hash_table
, 0, byte_size
);
239 for (uint32_t i
= 0; i
< old_table_size
; i
++) {
240 struct cache_entry
*entry
= old_table
[i
];
244 radv_pipeline_cache_set_entry(cache
, entry
);
253 radv_pipeline_cache_add_entry(struct radv_pipeline_cache
*cache
,
254 struct cache_entry
*entry
)
256 if (cache
->kernel_count
== cache
->table_size
/ 2)
257 radv_pipeline_cache_grow(cache
);
259 /* Failing to grow that hash table isn't fatal, but may mean we don't
260 * have enough space to add this new kernel. Only add it if there's room.
262 if (cache
->kernel_count
< cache
->table_size
/ 2)
263 radv_pipeline_cache_set_entry(cache
, entry
);
266 struct radv_shader_variant
*
267 radv_pipeline_cache_insert_shader(struct radv_device
*device
,
268 struct radv_pipeline_cache
*cache
,
269 const unsigned char *sha1
,
270 struct radv_shader_variant
*variant
,
271 const void *code
, unsigned code_size
)
274 cache
= device
->mem_cache
;
276 pthread_mutex_lock(&cache
->mutex
);
277 struct cache_entry
*entry
= radv_pipeline_cache_search_unlocked(cache
, sha1
);
279 if (entry
->variant
) {
280 radv_shader_variant_destroy(cache
->device
, variant
);
281 variant
= entry
->variant
;
283 entry
->variant
= variant
;
285 p_atomic_inc(&variant
->ref_count
);
286 pthread_mutex_unlock(&cache
->mutex
);
290 entry
= vk_alloc(&cache
->alloc
, sizeof(*entry
) + code_size
, 8,
291 VK_SYSTEM_ALLOCATION_SCOPE_CACHE
);
293 pthread_mutex_unlock(&cache
->mutex
);
297 memcpy(entry
->sha1
, sha1
, 20);
298 memcpy(entry
->code
, code
, code_size
);
299 entry
->config
= variant
->config
;
300 entry
->variant_info
= variant
->info
;
301 entry
->rsrc1
= variant
->rsrc1
;
302 entry
->rsrc2
= variant
->rsrc2
;
303 entry
->code_size
= code_size
;
304 entry
->variant
= variant
;
305 p_atomic_inc(&variant
->ref_count
);
307 radv_pipeline_cache_add_entry(cache
, entry
);
309 cache
->modified
= true;
310 pthread_mutex_unlock(&cache
->mutex
);
314 struct cache_header
{
315 uint32_t header_size
;
316 uint32_t header_version
;
319 uint8_t uuid
[VK_UUID_SIZE
];
323 radv_pipeline_cache_load(struct radv_pipeline_cache
*cache
,
324 const void *data
, size_t size
)
326 struct radv_device
*device
= cache
->device
;
327 struct cache_header header
;
329 if (size
< sizeof(header
))
331 memcpy(&header
, data
, sizeof(header
));
332 if (header
.header_size
< sizeof(header
))
334 if (header
.header_version
!= VK_PIPELINE_CACHE_HEADER_VERSION_ONE
)
336 if (header
.vendor_id
!= ATI_VENDOR_ID
)
338 if (header
.device_id
!= device
->physical_device
->rad_info
.pci_id
)
340 if (memcmp(header
.uuid
, device
->physical_device
->cache_uuid
, VK_UUID_SIZE
) != 0)
343 char *end
= (void *) data
+ size
;
344 char *p
= (void *) data
+ header
.header_size
;
346 while (end
- p
>= sizeof(struct cache_entry
)) {
347 struct cache_entry
*entry
= (struct cache_entry
*)p
;
348 struct cache_entry
*dest_entry
;
349 if(end
- p
< sizeof(*entry
) + entry
->code_size
)
352 dest_entry
= vk_alloc(&cache
->alloc
, sizeof(*entry
) + entry
->code_size
,
353 8, VK_SYSTEM_ALLOCATION_SCOPE_CACHE
);
355 memcpy(dest_entry
, entry
, sizeof(*entry
) + entry
->code_size
);
356 dest_entry
->variant
= NULL
;
357 radv_pipeline_cache_add_entry(cache
, dest_entry
);
359 p
+= sizeof (*entry
) + entry
->code_size
;
363 VkResult
radv_CreatePipelineCache(
365 const VkPipelineCacheCreateInfo
* pCreateInfo
,
366 const VkAllocationCallbacks
* pAllocator
,
367 VkPipelineCache
* pPipelineCache
)
369 RADV_FROM_HANDLE(radv_device
, device
, _device
);
370 struct radv_pipeline_cache
*cache
;
372 assert(pCreateInfo
->sType
== VK_STRUCTURE_TYPE_PIPELINE_CACHE_CREATE_INFO
);
373 assert(pCreateInfo
->flags
== 0);
375 cache
= vk_alloc2(&device
->alloc
, pAllocator
,
377 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT
);
379 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
382 cache
->alloc
= *pAllocator
;
384 cache
->alloc
= device
->alloc
;
386 radv_pipeline_cache_init(cache
, device
);
388 if (pCreateInfo
->initialDataSize
> 0) {
389 radv_pipeline_cache_load(cache
,
390 pCreateInfo
->pInitialData
,
391 pCreateInfo
->initialDataSize
);
394 *pPipelineCache
= radv_pipeline_cache_to_handle(cache
);
399 void radv_DestroyPipelineCache(
401 VkPipelineCache _cache
,
402 const VkAllocationCallbacks
* pAllocator
)
404 RADV_FROM_HANDLE(radv_device
, device
, _device
);
405 RADV_FROM_HANDLE(radv_pipeline_cache
, cache
, _cache
);
409 radv_pipeline_cache_finish(cache
);
411 vk_free2(&device
->alloc
, pAllocator
, cache
);
414 VkResult
radv_GetPipelineCacheData(
416 VkPipelineCache _cache
,
420 RADV_FROM_HANDLE(radv_device
, device
, _device
);
421 RADV_FROM_HANDLE(radv_pipeline_cache
, cache
, _cache
);
422 struct cache_header
*header
;
423 VkResult result
= VK_SUCCESS
;
424 const size_t size
= sizeof(*header
) + cache
->total_size
;
429 if (*pDataSize
< sizeof(*header
)) {
431 return VK_INCOMPLETE
;
433 void *p
= pData
, *end
= pData
+ *pDataSize
;
435 header
->header_size
= sizeof(*header
);
436 header
->header_version
= VK_PIPELINE_CACHE_HEADER_VERSION_ONE
;
437 header
->vendor_id
= ATI_VENDOR_ID
;
438 header
->device_id
= device
->physical_device
->rad_info
.pci_id
;
439 memcpy(header
->uuid
, device
->physical_device
->cache_uuid
, VK_UUID_SIZE
);
440 p
+= header
->header_size
;
442 struct cache_entry
*entry
;
443 for (uint32_t i
= 0; i
< cache
->table_size
; i
++) {
444 if (!cache
->hash_table
[i
])
446 entry
= cache
->hash_table
[i
];
447 const uint32_t size
= entry_size(entry
);
448 if (end
< p
+ size
) {
449 result
= VK_INCOMPLETE
;
453 memcpy(p
, entry
, size
);
454 ((struct cache_entry
*)p
)->variant
= NULL
;
457 *pDataSize
= p
- pData
;
463 radv_pipeline_cache_merge(struct radv_pipeline_cache
*dst
,
464 struct radv_pipeline_cache
*src
)
466 for (uint32_t i
= 0; i
< src
->table_size
; i
++) {
467 struct cache_entry
*entry
= src
->hash_table
[i
];
468 if (!entry
|| radv_pipeline_cache_search(dst
, entry
->sha1
))
471 radv_pipeline_cache_add_entry(dst
, entry
);
473 src
->hash_table
[i
] = NULL
;
477 VkResult
radv_MergePipelineCaches(
479 VkPipelineCache destCache
,
480 uint32_t srcCacheCount
,
481 const VkPipelineCache
* pSrcCaches
)
483 RADV_FROM_HANDLE(radv_pipeline_cache
, dst
, destCache
);
485 for (uint32_t i
= 0; i
< srcCacheCount
; i
++) {
486 RADV_FROM_HANDLE(radv_pipeline_cache
, src
, pSrcCaches
[i
]);
488 radv_pipeline_cache_merge(dst
, src
);