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_private.h"
29 #include "ac_nir_to_llvm.h"
33 unsigned char sha1
[20];
37 struct ac_shader_variant_info variant_info
;
38 struct ac_shader_config config
;
39 uint32_t rsrc1
, rsrc2
;
40 struct radv_shader_variant
*variant
;
45 radv_pipeline_cache_init(struct radv_pipeline_cache
*cache
,
46 struct radv_device
*device
)
48 cache
->device
= device
;
49 pthread_mutex_init(&cache
->mutex
, NULL
);
51 cache
->modified
= false;
52 cache
->kernel_count
= 0;
53 cache
->total_size
= 0;
54 cache
->table_size
= 1024;
55 const size_t byte_size
= cache
->table_size
* sizeof(cache
->hash_table
[0]);
56 cache
->hash_table
= malloc(byte_size
);
58 /* We don't consider allocation failure fatal, we just start with a 0-sized
60 if (cache
->hash_table
== NULL
||
61 (device
->debug_flags
& RADV_DEBUG_NO_CACHE
))
62 cache
->table_size
= 0;
64 memset(cache
->hash_table
, 0, byte_size
);
68 radv_pipeline_cache_finish(struct radv_pipeline_cache
*cache
)
70 for (unsigned i
= 0; i
< cache
->table_size
; ++i
)
71 if (cache
->hash_table
[i
]) {
72 if (cache
->hash_table
[i
]->variant
)
73 radv_shader_variant_destroy(cache
->device
,
74 cache
->hash_table
[i
]->variant
);
75 vk_free(&cache
->alloc
, cache
->hash_table
[i
]);
77 pthread_mutex_destroy(&cache
->mutex
);
78 free(cache
->hash_table
);
82 entry_size(struct cache_entry
*entry
)
84 return sizeof(*entry
) + entry
->code_size
;
88 radv_hash_shader(unsigned char *hash
, struct radv_shader_module
*module
,
89 const char *entrypoint
,
90 const VkSpecializationInfo
*spec_info
,
91 const struct radv_pipeline_layout
*layout
,
92 const union ac_shader_variant_key
*key
,
93 uint32_t is_geom_copy_shader
)
97 _mesa_sha1_init(&ctx
);
99 _mesa_sha1_update(&ctx
, key
, sizeof(*key
));
100 _mesa_sha1_update(&ctx
, module
->sha1
, sizeof(module
->sha1
));
101 _mesa_sha1_update(&ctx
, entrypoint
, strlen(entrypoint
));
103 _mesa_sha1_update(&ctx
, layout
->sha1
, sizeof(layout
->sha1
));
105 _mesa_sha1_update(&ctx
, spec_info
->pMapEntries
,
106 spec_info
->mapEntryCount
* sizeof spec_info
->pMapEntries
[0]);
107 _mesa_sha1_update(&ctx
, spec_info
->pData
, spec_info
->dataSize
);
109 _mesa_sha1_update(&ctx
, &is_geom_copy_shader
, 4);
110 _mesa_sha1_final(&ctx
, hash
);
114 static struct cache_entry
*
115 radv_pipeline_cache_search_unlocked(struct radv_pipeline_cache
*cache
,
116 const unsigned char *sha1
)
118 const uint32_t mask
= cache
->table_size
- 1;
119 const uint32_t start
= (*(uint32_t *) sha1
);
121 for (uint32_t i
= 0; i
< cache
->table_size
; i
++) {
122 const uint32_t index
= (start
+ i
) & mask
;
123 struct cache_entry
*entry
= cache
->hash_table
[index
];
128 if (memcmp(entry
->sha1
, sha1
, sizeof(entry
->sha1
)) == 0) {
133 unreachable("hash table should never be full");
136 static struct cache_entry
*
137 radv_pipeline_cache_search(struct radv_pipeline_cache
*cache
,
138 const unsigned char *sha1
)
140 struct cache_entry
*entry
;
142 pthread_mutex_lock(&cache
->mutex
);
144 entry
= radv_pipeline_cache_search_unlocked(cache
, sha1
);
146 pthread_mutex_unlock(&cache
->mutex
);
151 struct radv_shader_variant
*
152 radv_create_shader_variant_from_pipeline_cache(struct radv_device
*device
,
153 struct radv_pipeline_cache
*cache
,
154 const unsigned char *sha1
)
156 struct cache_entry
*entry
= NULL
;
159 entry
= radv_pipeline_cache_search(cache
, sha1
);
164 if (!entry
->variant
) {
165 struct radv_shader_variant
*variant
;
167 variant
= calloc(1, sizeof(struct radv_shader_variant
));
171 variant
->config
= entry
->config
;
172 variant
->info
= entry
->variant_info
;
173 variant
->rsrc1
= entry
->rsrc1
;
174 variant
->rsrc2
= entry
->rsrc2
;
175 variant
->code_size
= entry
->code_size
;
176 variant
->ref_count
= 1;
178 variant
->bo
= device
->ws
->buffer_create(device
->ws
, entry
->code_size
, 256,
179 RADEON_DOMAIN_VRAM
, RADEON_FLAG_CPU_ACCESS
);
181 void *ptr
= device
->ws
->buffer_map(variant
->bo
);
182 memcpy(ptr
, entry
->code
, entry
->code_size
);
183 device
->ws
->buffer_unmap(variant
->bo
);
185 entry
->variant
= variant
;
188 p_atomic_inc(&entry
->variant
->ref_count
);
189 return entry
->variant
;
194 radv_pipeline_cache_set_entry(struct radv_pipeline_cache
*cache
,
195 struct cache_entry
*entry
)
197 const uint32_t mask
= cache
->table_size
- 1;
198 const uint32_t start
= entry
->sha1_dw
[0];
200 /* We'll always be able to insert when we get here. */
201 assert(cache
->kernel_count
< cache
->table_size
/ 2);
203 for (uint32_t i
= 0; i
< cache
->table_size
; i
++) {
204 const uint32_t index
= (start
+ i
) & mask
;
205 if (!cache
->hash_table
[index
]) {
206 cache
->hash_table
[index
] = entry
;
211 cache
->total_size
+= entry_size(entry
);
212 cache
->kernel_count
++;
217 radv_pipeline_cache_grow(struct radv_pipeline_cache
*cache
)
219 const uint32_t table_size
= cache
->table_size
* 2;
220 const uint32_t old_table_size
= cache
->table_size
;
221 const size_t byte_size
= table_size
* sizeof(cache
->hash_table
[0]);
222 struct cache_entry
**table
;
223 struct cache_entry
**old_table
= cache
->hash_table
;
225 table
= malloc(byte_size
);
227 return VK_ERROR_OUT_OF_HOST_MEMORY
;
229 cache
->hash_table
= table
;
230 cache
->table_size
= table_size
;
231 cache
->kernel_count
= 0;
232 cache
->total_size
= 0;
234 memset(cache
->hash_table
, 0, byte_size
);
235 for (uint32_t i
= 0; i
< old_table_size
; i
++) {
236 struct cache_entry
*entry
= old_table
[i
];
240 radv_pipeline_cache_set_entry(cache
, entry
);
249 radv_pipeline_cache_add_entry(struct radv_pipeline_cache
*cache
,
250 struct cache_entry
*entry
)
252 if (cache
->kernel_count
== cache
->table_size
/ 2)
253 radv_pipeline_cache_grow(cache
);
255 /* Failing to grow that hash table isn't fatal, but may mean we don't
256 * have enough space to add this new kernel. Only add it if there's room.
258 if (cache
->kernel_count
< cache
->table_size
/ 2)
259 radv_pipeline_cache_set_entry(cache
, entry
);
262 struct radv_shader_variant
*
263 radv_pipeline_cache_insert_shader(struct radv_pipeline_cache
*cache
,
264 const unsigned char *sha1
,
265 struct radv_shader_variant
*variant
,
266 const void *code
, unsigned code_size
)
271 pthread_mutex_lock(&cache
->mutex
);
272 struct cache_entry
*entry
= radv_pipeline_cache_search_unlocked(cache
, sha1
);
274 if (entry
->variant
) {
275 radv_shader_variant_destroy(cache
->device
, variant
);
276 variant
= entry
->variant
;
278 entry
->variant
= variant
;
280 p_atomic_inc(&variant
->ref_count
);
281 pthread_mutex_unlock(&cache
->mutex
);
285 entry
= vk_alloc(&cache
->alloc
, sizeof(*entry
) + code_size
, 8,
286 VK_SYSTEM_ALLOCATION_SCOPE_CACHE
);
288 pthread_mutex_unlock(&cache
->mutex
);
292 memcpy(entry
->sha1
, sha1
, 20);
293 memcpy(entry
->code
, code
, code_size
);
294 entry
->config
= variant
->config
;
295 entry
->variant_info
= variant
->info
;
296 entry
->rsrc1
= variant
->rsrc1
;
297 entry
->rsrc2
= variant
->rsrc2
;
298 entry
->code_size
= code_size
;
299 entry
->variant
= variant
;
300 p_atomic_inc(&variant
->ref_count
);
302 radv_pipeline_cache_add_entry(cache
, entry
);
304 cache
->modified
= true;
305 pthread_mutex_unlock(&cache
->mutex
);
309 struct cache_header
{
310 uint32_t header_size
;
311 uint32_t header_version
;
314 uint8_t uuid
[VK_UUID_SIZE
];
318 radv_pipeline_cache_load(struct radv_pipeline_cache
*cache
,
319 const void *data
, size_t size
)
321 struct radv_device
*device
= cache
->device
;
322 struct cache_header header
;
324 if (size
< sizeof(header
))
326 memcpy(&header
, data
, sizeof(header
));
327 if (header
.header_size
< sizeof(header
))
329 if (header
.header_version
!= VK_PIPELINE_CACHE_HEADER_VERSION_ONE
)
331 if (header
.vendor_id
!= 0x1002)
333 if (header
.device_id
!= device
->physical_device
->rad_info
.pci_id
)
335 if (memcmp(header
.uuid
, device
->physical_device
->uuid
, VK_UUID_SIZE
) != 0)
338 char *end
= (void *) data
+ size
;
339 char *p
= (void *) data
+ header
.header_size
;
341 while (end
- p
>= sizeof(struct cache_entry
)) {
342 struct cache_entry
*entry
= (struct cache_entry
*)p
;
343 struct cache_entry
*dest_entry
;
344 if(end
- p
< sizeof(*entry
) + entry
->code_size
)
347 dest_entry
= vk_alloc(&cache
->alloc
, sizeof(*entry
) + entry
->code_size
,
348 8, VK_SYSTEM_ALLOCATION_SCOPE_CACHE
);
350 memcpy(dest_entry
, entry
, sizeof(*entry
) + entry
->code_size
);
351 dest_entry
->variant
= NULL
;
352 radv_pipeline_cache_add_entry(cache
, dest_entry
);
354 p
+= sizeof (*entry
) + entry
->code_size
;
358 VkResult
radv_CreatePipelineCache(
360 const VkPipelineCacheCreateInfo
* pCreateInfo
,
361 const VkAllocationCallbacks
* pAllocator
,
362 VkPipelineCache
* pPipelineCache
)
364 RADV_FROM_HANDLE(radv_device
, device
, _device
);
365 struct radv_pipeline_cache
*cache
;
367 assert(pCreateInfo
->sType
== VK_STRUCTURE_TYPE_PIPELINE_CACHE_CREATE_INFO
);
368 assert(pCreateInfo
->flags
== 0);
370 cache
= vk_alloc2(&device
->alloc
, pAllocator
,
372 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT
);
374 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
377 cache
->alloc
= *pAllocator
;
379 cache
->alloc
= device
->alloc
;
381 radv_pipeline_cache_init(cache
, device
);
383 if (pCreateInfo
->initialDataSize
> 0) {
384 radv_pipeline_cache_load(cache
,
385 pCreateInfo
->pInitialData
,
386 pCreateInfo
->initialDataSize
);
389 *pPipelineCache
= radv_pipeline_cache_to_handle(cache
);
394 void radv_DestroyPipelineCache(
396 VkPipelineCache _cache
,
397 const VkAllocationCallbacks
* pAllocator
)
399 RADV_FROM_HANDLE(radv_device
, device
, _device
);
400 RADV_FROM_HANDLE(radv_pipeline_cache
, cache
, _cache
);
404 radv_pipeline_cache_finish(cache
);
406 vk_free2(&device
->alloc
, pAllocator
, cache
);
409 VkResult
radv_GetPipelineCacheData(
411 VkPipelineCache _cache
,
415 RADV_FROM_HANDLE(radv_device
, device
, _device
);
416 RADV_FROM_HANDLE(radv_pipeline_cache
, cache
, _cache
);
417 struct cache_header
*header
;
418 VkResult result
= VK_SUCCESS
;
419 const size_t size
= sizeof(*header
) + cache
->total_size
;
424 if (*pDataSize
< sizeof(*header
)) {
426 return VK_INCOMPLETE
;
428 void *p
= pData
, *end
= pData
+ *pDataSize
;
430 header
->header_size
= sizeof(*header
);
431 header
->header_version
= VK_PIPELINE_CACHE_HEADER_VERSION_ONE
;
432 header
->vendor_id
= 0x1002;
433 header
->device_id
= device
->physical_device
->rad_info
.pci_id
;
434 memcpy(header
->uuid
, device
->physical_device
->uuid
, VK_UUID_SIZE
);
435 p
+= header
->header_size
;
437 struct cache_entry
*entry
;
438 for (uint32_t i
= 0; i
< cache
->table_size
; i
++) {
439 if (!cache
->hash_table
[i
])
441 entry
= cache
->hash_table
[i
];
442 const uint32_t size
= entry_size(entry
);
443 if (end
< p
+ size
) {
444 result
= VK_INCOMPLETE
;
448 memcpy(p
, entry
, size
);
449 ((struct cache_entry
*)p
)->variant
= NULL
;
452 *pDataSize
= p
- pData
;
458 radv_pipeline_cache_merge(struct radv_pipeline_cache
*dst
,
459 struct radv_pipeline_cache
*src
)
461 for (uint32_t i
= 0; i
< src
->table_size
; i
++) {
462 struct cache_entry
*entry
= src
->hash_table
[i
];
463 if (!entry
|| radv_pipeline_cache_search(dst
, entry
->sha1
))
466 radv_pipeline_cache_add_entry(dst
, entry
);
468 src
->hash_table
[i
] = NULL
;
472 VkResult
radv_MergePipelineCaches(
474 VkPipelineCache destCache
,
475 uint32_t srcCacheCount
,
476 const VkPipelineCache
* pSrcCaches
)
478 RADV_FROM_HANDLE(radv_pipeline_cache
, dst
, destCache
);
480 for (uint32_t i
= 0; i
< srcCacheCount
; i
++) {
481 RADV_FROM_HANDLE(radv_pipeline_cache
, src
, pSrcCaches
[i
]);
483 radv_pipeline_cache_merge(dst
, src
);