Path: blob/21.2-virgl/src/freedreno/ir3/ir3_disk_cache.c
4565 views
/*1* Copyright © 2020 Google, Inc.2*3* Permission is hereby granted, free of charge, to any person obtaining a4* copy of this software and associated documentation files (the "Software"),5* to deal in the Software without restriction, including without limitation6* the rights to use, copy, modify, merge, publish, distribute, sublicense,7* and/or sell copies of the Software, and to permit persons to whom the8* Software is furnished to do so, subject to the following conditions:9*10* The above copyright notice and this permission notice (including the next11* paragraph) shall be included in all copies or substantial portions of the12* Software.13*14* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR15* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,16* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL17* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER18* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,19* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE20* SOFTWARE.21*/2223#include "nir_serialize.h"2425#include "ir3_compiler.h"26#include "ir3_nir.h"2728#define debug 02930/*31* Shader disk-cache implementation.32*33* Note that at least in the EGL_ANDROID_blob_cache, we should never34* rely on inter-dependencies between different cache entries:35*36* No guarantees are made as to whether a given key/value pair is present in37* the cache after the set call. If a different value has been associated38* with the given key in the past then it is undefined which value, if any,39* is associated with the key after the set call. Note that while there are40* no guarantees, the cache implementation should attempt to cache the most41* recently set value for a given key.42*43* for this reason, because binning pass variants share const_state with44* their draw-pass counterpart, both variants are serialized together.45*/4647void48ir3_disk_cache_init(struct ir3_compiler *compiler)49{50if (ir3_shader_debug & IR3_DBG_NOCACHE)51return;5253/* array length = print length + nul char + 1 extra to verify it's unused */54char renderer[7];55ASSERTED int len =56snprintf(renderer, sizeof(renderer), "FD%03d", compiler->gpu_id);57assert(len == sizeof(renderer) - 2);5859const struct build_id_note *note =60build_id_find_nhdr_for_addr(ir3_disk_cache_init);61assert(note && build_id_length(note) == 20); /* sha1 */6263const uint8_t *id_sha1 = build_id_data(note);64assert(id_sha1);6566char timestamp[41];67_mesa_sha1_format(timestamp, id_sha1);6869uint64_t driver_flags = ir3_shader_debug;70if (compiler->robust_ubo_access)71driver_flags |= IR3_DBG_ROBUST_UBO_ACCESS;72compiler->disk_cache = disk_cache_create(renderer, timestamp, driver_flags);73}7475void76ir3_disk_cache_init_shader_key(struct ir3_compiler *compiler,77struct ir3_shader *shader)78{79if (!compiler->disk_cache)80return;8182struct mesa_sha1 ctx;8384_mesa_sha1_init(&ctx);8586/* Serialize the NIR to a binary blob that we can hash for the disk87* cache. Drop unnecessary information (like variable names)88* so the serialized NIR is smaller, and also to let us detect more89* isomorphic shaders when hashing, increasing cache hits.90*/91struct blob blob;92blob_init(&blob);93nir_serialize(&blob, shader->nir, true);94_mesa_sha1_update(&ctx, blob.data, blob.size);95blob_finish(&blob);9697/* Note that on some gens stream-out is lowered in ir3 to stg. For later98* gens we maybe don't need to include stream-out in the cache key.99*/100_mesa_sha1_update(&ctx, &shader->stream_output,101sizeof(shader->stream_output));102103_mesa_sha1_final(&ctx, shader->cache_key);104}105106static void107compute_variant_key(struct ir3_compiler *compiler, struct ir3_shader_variant *v,108cache_key cache_key)109{110struct blob blob;111blob_init(&blob);112113blob_write_bytes(&blob, &v->shader->cache_key, sizeof(v->shader->cache_key));114blob_write_bytes(&blob, &v->key, sizeof(v->key));115blob_write_uint8(&blob, v->binning_pass);116117disk_cache_compute_key(compiler->disk_cache, blob.data, blob.size,118cache_key);119120blob_finish(&blob);121}122123static void124retrieve_variant(struct blob_reader *blob, struct ir3_shader_variant *v)125{126blob_copy_bytes(blob, VARIANT_CACHE_PTR(v), VARIANT_CACHE_SIZE);127128/*129* pointers need special handling:130*/131132v->bin = rzalloc_size(v, v->info.size);133blob_copy_bytes(blob, v->bin, v->info.size);134135if (!v->binning_pass) {136blob_copy_bytes(blob, v->const_state, sizeof(*v->const_state));137unsigned immeds_sz = v->const_state->immediates_size *138sizeof(v->const_state->immediates[0]);139v->const_state->immediates = ralloc_size(v->const_state, immeds_sz);140blob_copy_bytes(blob, v->const_state->immediates, immeds_sz);141}142}143144static void145store_variant(struct blob *blob, struct ir3_shader_variant *v)146{147blob_write_bytes(blob, VARIANT_CACHE_PTR(v), VARIANT_CACHE_SIZE);148149/*150* pointers need special handling:151*/152153blob_write_bytes(blob, v->bin, v->info.size);154155/* No saving constant_data, it's already baked into bin at this point. */156157if (!v->binning_pass) {158blob_write_bytes(blob, v->const_state, sizeof(*v->const_state));159unsigned immeds_sz = v->const_state->immediates_size *160sizeof(v->const_state->immediates[0]);161blob_write_bytes(blob, v->const_state->immediates, immeds_sz);162}163}164165bool166ir3_disk_cache_retrieve(struct ir3_compiler *compiler,167struct ir3_shader_variant *v)168{169if (!compiler->disk_cache)170return false;171172cache_key cache_key;173174compute_variant_key(compiler, v, cache_key);175176if (debug) {177char sha1[41];178_mesa_sha1_format(sha1, cache_key);179fprintf(stderr, "[mesa disk cache] retrieving variant %s: ", sha1);180}181182size_t size;183void *buffer = disk_cache_get(compiler->disk_cache, cache_key, &size);184185if (debug)186fprintf(stderr, "%s\n", buffer ? "found" : "missing");187188if (!buffer)189return false;190191struct blob_reader blob;192blob_reader_init(&blob, buffer, size);193194retrieve_variant(&blob, v);195196if (v->binning)197retrieve_variant(&blob, v->binning);198199free(buffer);200201return true;202}203204void205ir3_disk_cache_store(struct ir3_compiler *compiler,206struct ir3_shader_variant *v)207{208if (!compiler->disk_cache)209return;210211cache_key cache_key;212213compute_variant_key(compiler, v, cache_key);214215if (debug) {216char sha1[41];217_mesa_sha1_format(sha1, cache_key);218fprintf(stderr, "[mesa disk cache] storing variant %s\n", sha1);219}220221struct blob blob;222blob_init(&blob);223224store_variant(&blob, v);225226if (v->binning)227store_variant(&blob, v->binning);228229disk_cache_put(compiler->disk_cache, cache_key, blob.data, blob.size, NULL);230blob_finish(&blob);231}232233234