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 "tu_private.h"
25 #include "util/debug.h"
26 #include "util/disk_cache.h"
27 #include "util/mesa-sha1.h"
28 #include "util/u_atomic.h"
30 struct cache_entry_variant_info
38 unsigned char sha1
[20];
41 uint32_t code_sizes
[MESA_SHADER_STAGES
];
42 struct tu_shader_variant
*variants
[MESA_SHADER_STAGES
];
47 tu_pipeline_cache_init(struct tu_pipeline_cache
*cache
,
48 struct tu_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
61 * cache. Disable caching when we want to keep shader debug info, since
62 * we don't get the debug info on cached shaders. */
63 if (cache
->hash_table
== NULL
)
64 cache
->table_size
= 0;
66 memset(cache
->hash_table
, 0, byte_size
);
70 tu_pipeline_cache_finish(struct tu_pipeline_cache
*cache
)
72 for (unsigned i
= 0; i
< cache
->table_size
; ++i
)
73 if (cache
->hash_table
[i
]) {
74 vk_free(&cache
->alloc
, cache
->hash_table
[i
]);
76 pthread_mutex_destroy(&cache
->mutex
);
77 free(cache
->hash_table
);
81 entry_size(struct cache_entry
*entry
)
83 size_t ret
= sizeof(*entry
);
84 for (int i
= 0; i
< MESA_SHADER_STAGES
; ++i
)
85 if (entry
->code_sizes
[i
])
86 ret
+= sizeof(struct cache_entry_variant_info
) + entry
->code_sizes
[i
];
91 tu_hash_shaders(unsigned char *hash
,
92 const VkPipelineShaderStageCreateInfo
**stages
,
93 const struct tu_pipeline_layout
*layout
,
94 const struct tu_pipeline_key
*key
,
99 _mesa_sha1_init(&ctx
);
101 _mesa_sha1_update(&ctx
, key
, sizeof(*key
));
103 _mesa_sha1_update(&ctx
, layout
->sha1
, sizeof(layout
->sha1
));
105 for (int i
= 0; i
< MESA_SHADER_STAGES
; ++i
) {
107 TU_FROM_HANDLE(tu_shader_module
, module
, stages
[i
]->module
);
108 const VkSpecializationInfo
*spec_info
= stages
[i
]->pSpecializationInfo
;
110 _mesa_sha1_update(&ctx
, module
->sha1
, sizeof(module
->sha1
));
111 _mesa_sha1_update(&ctx
, stages
[i
]->pName
, strlen(stages
[i
]->pName
));
113 _mesa_sha1_update(&ctx
,
114 spec_info
->pMapEntries
,
115 spec_info
->mapEntryCount
*
116 sizeof spec_info
->pMapEntries
[0]);
117 _mesa_sha1_update(&ctx
, spec_info
->pData
, spec_info
->dataSize
);
121 _mesa_sha1_update(&ctx
, &flags
, 4);
122 _mesa_sha1_final(&ctx
, hash
);
125 static struct cache_entry
*
126 tu_pipeline_cache_search_unlocked(struct tu_pipeline_cache
*cache
,
127 const unsigned char *sha1
)
129 const uint32_t mask
= cache
->table_size
- 1;
130 const uint32_t start
= (*(uint32_t *)sha1
);
132 if (cache
->table_size
== 0)
135 for (uint32_t i
= 0; i
< cache
->table_size
; i
++) {
136 const uint32_t index
= (start
+ i
) & mask
;
137 struct cache_entry
*entry
= cache
->hash_table
[index
];
142 if (memcmp(entry
->sha1
, sha1
, sizeof(entry
->sha1
)) == 0) {
147 unreachable("hash table should never be full");
150 static struct cache_entry
*
151 tu_pipeline_cache_search(struct tu_pipeline_cache
*cache
,
152 const unsigned char *sha1
)
154 struct cache_entry
*entry
;
156 pthread_mutex_lock(&cache
->mutex
);
158 entry
= tu_pipeline_cache_search_unlocked(cache
, sha1
);
160 pthread_mutex_unlock(&cache
->mutex
);
166 tu_pipeline_cache_set_entry(struct tu_pipeline_cache
*cache
,
167 struct cache_entry
*entry
)
169 const uint32_t mask
= cache
->table_size
- 1;
170 const uint32_t start
= entry
->sha1_dw
[0];
172 /* We'll always be able to insert when we get here. */
173 assert(cache
->kernel_count
< cache
->table_size
/ 2);
175 for (uint32_t i
= 0; i
< cache
->table_size
; i
++) {
176 const uint32_t index
= (start
+ i
) & mask
;
177 if (!cache
->hash_table
[index
]) {
178 cache
->hash_table
[index
] = entry
;
183 cache
->total_size
+= entry_size(entry
);
184 cache
->kernel_count
++;
188 tu_pipeline_cache_grow(struct tu_pipeline_cache
*cache
)
190 const uint32_t table_size
= cache
->table_size
* 2;
191 const uint32_t old_table_size
= cache
->table_size
;
192 const size_t byte_size
= table_size
* sizeof(cache
->hash_table
[0]);
193 struct cache_entry
**table
;
194 struct cache_entry
**old_table
= cache
->hash_table
;
196 table
= malloc(byte_size
);
198 return vk_error(cache
->device
->instance
, VK_ERROR_OUT_OF_HOST_MEMORY
);
200 cache
->hash_table
= table
;
201 cache
->table_size
= table_size
;
202 cache
->kernel_count
= 0;
203 cache
->total_size
= 0;
205 memset(cache
->hash_table
, 0, byte_size
);
206 for (uint32_t i
= 0; i
< old_table_size
; i
++) {
207 struct cache_entry
*entry
= old_table
[i
];
211 tu_pipeline_cache_set_entry(cache
, entry
);
220 tu_pipeline_cache_add_entry(struct tu_pipeline_cache
*cache
,
221 struct cache_entry
*entry
)
223 if (cache
->kernel_count
== cache
->table_size
/ 2)
224 tu_pipeline_cache_grow(cache
);
226 /* Failing to grow that hash table isn't fatal, but may mean we don't
227 * have enough space to add this new kernel. Only add it if there's room.
229 if (cache
->kernel_count
< cache
->table_size
/ 2)
230 tu_pipeline_cache_set_entry(cache
, entry
);
235 uint32_t header_size
;
236 uint32_t header_version
;
239 uint8_t uuid
[VK_UUID_SIZE
];
243 tu_pipeline_cache_load(struct tu_pipeline_cache
*cache
,
247 struct tu_device
*device
= cache
->device
;
248 struct cache_header header
;
250 if (size
< sizeof(header
))
252 memcpy(&header
, data
, sizeof(header
));
253 if (header
.header_size
< sizeof(header
))
255 if (header
.header_version
!= VK_PIPELINE_CACHE_HEADER_VERSION_ONE
)
257 if (header
.vendor_id
!= 0 /* TODO */)
259 if (header
.device_id
!= 0 /* TODO */)
261 if (memcmp(header
.uuid
, device
->physical_device
->cache_uuid
, VK_UUID_SIZE
) !=
265 char *end
= (void *)data
+ size
;
266 char *p
= (void *)data
+ header
.header_size
;
268 while (end
- p
>= sizeof(struct cache_entry
)) {
269 struct cache_entry
*entry
= (struct cache_entry
*)p
;
270 struct cache_entry
*dest_entry
;
271 size_t size
= entry_size(entry
);
276 vk_alloc(&cache
->alloc
, size
, 8, VK_SYSTEM_ALLOCATION_SCOPE_CACHE
);
278 memcpy(dest_entry
, entry
, size
);
279 for (int i
= 0; i
< MESA_SHADER_STAGES
; ++i
)
280 dest_entry
->variants
[i
] = NULL
;
281 tu_pipeline_cache_add_entry(cache
, dest_entry
);
288 tu_CreatePipelineCache(VkDevice _device
,
289 const VkPipelineCacheCreateInfo
*pCreateInfo
,
290 const VkAllocationCallbacks
*pAllocator
,
291 VkPipelineCache
*pPipelineCache
)
293 TU_FROM_HANDLE(tu_device
, device
, _device
);
294 struct tu_pipeline_cache
*cache
;
296 assert(pCreateInfo
->sType
== VK_STRUCTURE_TYPE_PIPELINE_CACHE_CREATE_INFO
);
297 assert(pCreateInfo
->flags
== 0);
299 cache
= vk_alloc2(&device
->alloc
,
303 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT
);
305 return vk_error(device
->instance
, VK_ERROR_OUT_OF_HOST_MEMORY
);
308 cache
->alloc
= *pAllocator
;
310 cache
->alloc
= device
->alloc
;
312 tu_pipeline_cache_init(cache
, device
);
314 if (pCreateInfo
->initialDataSize
> 0) {
315 tu_pipeline_cache_load(
316 cache
, pCreateInfo
->pInitialData
, pCreateInfo
->initialDataSize
);
319 *pPipelineCache
= tu_pipeline_cache_to_handle(cache
);
325 tu_DestroyPipelineCache(VkDevice _device
,
326 VkPipelineCache _cache
,
327 const VkAllocationCallbacks
*pAllocator
)
329 TU_FROM_HANDLE(tu_device
, device
, _device
);
330 TU_FROM_HANDLE(tu_pipeline_cache
, cache
, _cache
);
334 tu_pipeline_cache_finish(cache
);
336 vk_free2(&device
->alloc
, pAllocator
, cache
);
340 tu_GetPipelineCacheData(VkDevice _device
,
341 VkPipelineCache _cache
,
345 TU_FROM_HANDLE(tu_device
, device
, _device
);
346 TU_FROM_HANDLE(tu_pipeline_cache
, cache
, _cache
);
347 struct cache_header
*header
;
348 VkResult result
= VK_SUCCESS
;
350 pthread_mutex_lock(&cache
->mutex
);
352 const size_t size
= sizeof(*header
) + cache
->total_size
;
354 pthread_mutex_unlock(&cache
->mutex
);
358 if (*pDataSize
< sizeof(*header
)) {
359 pthread_mutex_unlock(&cache
->mutex
);
361 return VK_INCOMPLETE
;
363 void *p
= pData
, *end
= pData
+ *pDataSize
;
365 header
->header_size
= sizeof(*header
);
366 header
->header_version
= VK_PIPELINE_CACHE_HEADER_VERSION_ONE
;
367 header
->vendor_id
= 0 /* TODO */;
368 header
->device_id
= 0 /* TODO */;
369 memcpy(header
->uuid
, device
->physical_device
->cache_uuid
, VK_UUID_SIZE
);
370 p
+= header
->header_size
;
372 struct cache_entry
*entry
;
373 for (uint32_t i
= 0; i
< cache
->table_size
; i
++) {
374 if (!cache
->hash_table
[i
])
376 entry
= cache
->hash_table
[i
];
377 const uint32_t size
= entry_size(entry
);
378 if (end
< p
+ size
) {
379 result
= VK_INCOMPLETE
;
383 memcpy(p
, entry
, size
);
384 for (int j
= 0; j
< MESA_SHADER_STAGES
; ++j
)
385 ((struct cache_entry
*)p
)->variants
[j
] = NULL
;
388 *pDataSize
= p
- pData
;
390 pthread_mutex_unlock(&cache
->mutex
);
395 tu_pipeline_cache_merge(struct tu_pipeline_cache
*dst
,
396 struct tu_pipeline_cache
*src
)
398 for (uint32_t i
= 0; i
< src
->table_size
; i
++) {
399 struct cache_entry
*entry
= src
->hash_table
[i
];
400 if (!entry
|| tu_pipeline_cache_search(dst
, entry
->sha1
))
403 tu_pipeline_cache_add_entry(dst
, entry
);
405 src
->hash_table
[i
] = NULL
;
410 tu_MergePipelineCaches(VkDevice _device
,
411 VkPipelineCache destCache
,
412 uint32_t srcCacheCount
,
413 const VkPipelineCache
*pSrcCaches
)
415 TU_FROM_HANDLE(tu_pipeline_cache
, dst
, destCache
);
417 for (uint32_t i
= 0; i
< srcCacheCount
; i
++) {
418 TU_FROM_HANDLE(tu_pipeline_cache
, src
, pSrcCaches
[i
]);
420 tu_pipeline_cache_merge(dst
, src
);