From 1d7fbe2cd1247edde06ac783813be096b88dc04b Mon Sep 17 00:00:00 2001 From: Rob Clark Date: Tue, 24 Jul 2018 14:12:24 -0400 Subject: [PATCH] freedreno/ir3: shader variant cache Cache that maps gallium hwcso (in this case, 'struct ir3_shader') plus shader variant key to a generation specific state object. This could eventually replace the linked list of shader variants, but for now it lets us re-use the work currently done in fdN_program_emit() Signed-off-by: Rob Clark --- .../drivers/freedreno/Makefile.sources | 2 + .../drivers/freedreno/a5xx/fd5_program.c | 2 + src/gallium/drivers/freedreno/ir3/ir3_cache.c | 129 ++++++++++++++++++ src/gallium/drivers/freedreno/ir3/ir3_cache.h | 79 +++++++++++ src/gallium/drivers/freedreno/meson.build | 2 + 5 files changed, 214 insertions(+) create mode 100644 src/gallium/drivers/freedreno/ir3/ir3_cache.c create mode 100644 src/gallium/drivers/freedreno/ir3/ir3_cache.h diff --git a/src/gallium/drivers/freedreno/Makefile.sources b/src/gallium/drivers/freedreno/Makefile.sources index aeecdf5ab2a..c76c53278a9 100644 --- a/src/gallium/drivers/freedreno/Makefile.sources +++ b/src/gallium/drivers/freedreno/Makefile.sources @@ -198,6 +198,8 @@ ir3_SOURCES := \ ir3/disasm-a3xx.c \ ir3/instr-a3xx.h \ ir3/ir3.c \ + ir3/ir3_cache.c \ + ir3/ir3_cache.h \ ir3/ir3_compiler_nir.c \ ir3/ir3_compiler.c \ ir3/ir3_compiler.h \ diff --git a/src/gallium/drivers/freedreno/a5xx/fd5_program.c b/src/gallium/drivers/freedreno/a5xx/fd5_program.c index a30678d0477..8a0b2db0edc 100644 --- a/src/gallium/drivers/freedreno/a5xx/fd5_program.c +++ b/src/gallium/drivers/freedreno/a5xx/fd5_program.c @@ -38,6 +38,8 @@ #include "fd5_texture.h" #include "fd5_format.h" +#include "ir3_cache.h" + static struct ir3_shader * create_shader_stateobj(struct pipe_context *pctx, const struct pipe_shader_state *cso, enum shader_t type) diff --git a/src/gallium/drivers/freedreno/ir3/ir3_cache.c b/src/gallium/drivers/freedreno/ir3/ir3_cache.c new file mode 100644 index 00000000000..05edeed4681 --- /dev/null +++ b/src/gallium/drivers/freedreno/ir3/ir3_cache.c @@ -0,0 +1,129 @@ +/* -*- mode: C; c-file-style: "k&r"; tab-width 4; indent-tabs-mode: t; -*- */ + +/* + * Copyright (C) 2015 Rob Clark + * + * Permission is hereby granted, free of charge, to any person obtaining a + * copy of this software and associated documentation files (the "Software"), + * to deal in the Software without restriction, including without limitation + * the rights to use, copy, modify, merge, publish, distribute, sublicense, + * and/or sell copies of the Software, and to permit persons to whom the + * Software is furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice (including the next + * paragraph) shall be included in all copies or substantial portions of the + * Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL + * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + * + * Authors: + * Rob Clark + */ + +#include "util/ralloc.h" +#include "util/hash_table.h" + +#include "ir3_cache.h" +#include "ir3_shader.h" + + +static uint32_t +key_hash(const void *_key) +{ + const struct ir3_cache_key *key = _key; + uint32_t hash = _mesa_fnv32_1a_offset_bias; + hash = _mesa_fnv32_1a_accumulate_block(hash, key, sizeof(*key)); + return hash; +} + +static bool +key_equals(const void *_a, const void *_b) +{ + const struct ir3_cache_key *a = _a; + const struct ir3_cache_key *b = _b; + // TODO we could optimize the key shader-variant key comparison by not + // ignoring has_per_samp.. not really sure if that helps.. + return memcmp(a, b, sizeof(struct ir3_cache_key)) == 0; +} + +struct ir3_cache { + /* cache mapping gallium/etc shader state-objs + shader-key to backend + * specific state-object + */ + struct hash_table *ht; + + const struct ir3_cache_funcs *funcs; + void *data; +}; + +struct ir3_cache * ir3_cache_create(const struct ir3_cache_funcs *funcs, void *data) +{ + struct ir3_cache *cache = rzalloc(NULL, struct ir3_cache); + + cache->ht = _mesa_hash_table_create(cache, key_hash, key_equals); + cache->funcs = funcs; + cache->data = data; + + return cache; +} + +void ir3_cache_destroy(struct ir3_cache *cache) +{ + /* _mesa_hash_table_destroy is so *almost* useful.. */ + struct hash_entry *entry; + hash_table_foreach(cache->ht, entry) { + cache->funcs->destroy_state(cache->data, entry->data); + } + + ralloc_free(cache); +} + +struct ir3_program_state * +ir3_cache_lookup(struct ir3_cache *cache, const struct ir3_cache_key *key, + struct pipe_debug_callback *debug) +{ + uint32_t hash = key_hash(key); + struct hash_entry *entry = + _mesa_hash_table_search_pre_hashed(cache->ht, hash, key); + + if (entry) { + return entry->data; + } + + struct ir3_shader_variant *bs = ir3_shader_variant(key->vs, key->key, true, debug); + struct ir3_shader_variant *vs = ir3_shader_variant(key->vs, key->key, false, debug); + struct ir3_shader_variant *fs = ir3_shader_variant(key->fs, key->key, false, debug); + + struct ir3_program_state *state = + cache->funcs->create_state(cache->data, bs, vs, fs, &key->key); + state->key = *key; + + /* NOTE: uses copy of key in state obj, because pointer passed by caller + * is probably on the stack + */ + _mesa_hash_table_insert_pre_hashed(cache->ht, hash, &state->key, state); + + return state; +} + +/* call when an API level state object is destroyed, to invalidate + * cache entries which reference that state object. + */ +void ir3_cache_invalidate(struct ir3_cache *cache, void *stobj) +{ + struct hash_entry *entry; + hash_table_foreach(cache->ht, entry) { + const struct ir3_cache_key *key = entry->key; + if ((key->fs == stobj) || (key->vs == stobj)) { + cache->funcs->destroy_state(cache->data, entry->data); + _mesa_hash_table_remove(cache->ht, entry); + return; + } + } +} diff --git a/src/gallium/drivers/freedreno/ir3/ir3_cache.h b/src/gallium/drivers/freedreno/ir3/ir3_cache.h new file mode 100644 index 00000000000..3d3a7f8050d --- /dev/null +++ b/src/gallium/drivers/freedreno/ir3/ir3_cache.h @@ -0,0 +1,79 @@ +/* + * Copyright (C) 2018 Rob Clark + * + * Permission is hereby granted, free of charge, to any person obtaining a + * copy of this software and associated documentation files (the "Software"), + * to deal in the Software without restriction, including without limitation + * the rights to use, copy, modify, merge, publish, distribute, sublicense, + * and/or sell copies of the Software, and to permit persons to whom the + * Software is furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice (including the next + * paragraph) shall be included in all copies or substantial portions of the + * Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL + * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + * + * Authors: + * Rob Clark + */ + +#ifndef IR3_CACHE_H_ +#define IR3_CACHE_H_ + +#include "ir3_shader.h" + +/* + * An in-memory cache for mapping shader state objects plus shader key to + * hw specific state object for the specified shader variant. This is to + * allow re-using things like the register setup for varying linkage, etc. + */ + +/* key into program state cache */ +struct ir3_cache_key { + struct ir3_shader *vs, *fs; // 4 dwords + struct ir3_shader_key key; // 7 dwords +}; + +/* per-gen backend program state object should subclass this for it's + * state object, mainly because we need a copy of the key that is not + * allocated on the stack + */ +struct ir3_program_state { + struct ir3_cache_key key; +}; + +struct ir3_cache_funcs { + struct ir3_program_state *(*create_state)(void *data, + struct ir3_shader_variant *bs, /* binning pass vs */ + struct ir3_shader_variant *vs, + struct ir3_shader_variant *fs, + const struct ir3_shader_key *key); + void (*destroy_state)(void *data, struct ir3_program_state *state); +}; + +struct ir3_cache; + +/* construct a shader cache. Free with ralloc_free() */ +struct ir3_cache * ir3_cache_create(const struct ir3_cache_funcs *funcs, void *data); +void ir3_cache_destroy(struct ir3_cache *cache); + +/* debug callback is used for shader-db logs in case the lookup triggers + * shader variant compilation. + */ +struct ir3_program_state * ir3_cache_lookup(struct ir3_cache *cache, + const struct ir3_cache_key *key, + struct pipe_debug_callback *debug); + +/* call when an API level state object is destroyed, to invalidate + * cache entries which reference that state object. + */ +void ir3_cache_invalidate(struct ir3_cache *cache, void *stobj); + +#endif /* IR3_CACHE_H_ */ diff --git a/src/gallium/drivers/freedreno/meson.build b/src/gallium/drivers/freedreno/meson.build index 0681175f734..9272602e547 100644 --- a/src/gallium/drivers/freedreno/meson.build +++ b/src/gallium/drivers/freedreno/meson.build @@ -218,6 +218,8 @@ files_libfreedreno = files( 'ir3/disasm-a3xx.c', 'ir3/instr-a3xx.h', 'ir3/ir3.c', + 'ir3/ir3_cache.c', + 'ir3/ir3_cache.h', 'ir3/ir3_compiler_nir.c', 'ir3/ir3_compiler.c', 'ir3/ir3_compiler.h', -- 2.30.2