Path: blob/21.2-virgl/src/gallium/drivers/crocus/crocus_program_cache.c
4570 views
/*1* Copyright © 2017 Intel Corporation2*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 shall be included11* in all copies or substantial portions of the Software.12*13* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS14* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,15* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL16* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER17* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING18* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER19* DEALINGS IN THE SOFTWARE.20*/2122/**23* @file crocus_program_cache.c24*25* The in-memory program cache. This is basically a hash table mapping26* API-specified shaders and a state key to a compiled variant. It also27* takes care of uploading shader assembly into a BO for use on the GPU.28*/2930#include <stdio.h>31#include <errno.h>32#include "pipe/p_defines.h"33#include "pipe/p_state.h"34#include "pipe/p_context.h"35#include "pipe/p_screen.h"36#include "util/u_atomic.h"37#include "util/u_upload_mgr.h"38#include "compiler/nir/nir.h"39#include "compiler/nir/nir_builder.h"40#include "intel/compiler/brw_compiler.h"41#include "intel/compiler/brw_eu.h"42#include "intel/compiler/brw_nir.h"43#include "crocus_context.h"44#include "crocus_resource.h"4546struct keybox {47uint16_t size;48enum crocus_program_cache_id cache_id;49uint8_t data[0];50};5152static struct keybox *53make_keybox(void *mem_ctx, enum crocus_program_cache_id cache_id,54const void *key, uint32_t key_size)55{56struct keybox *keybox =57ralloc_size(mem_ctx, sizeof(struct keybox) + key_size);5859keybox->cache_id = cache_id;60keybox->size = key_size;61memcpy(keybox->data, key, key_size);6263return keybox;64}6566static uint32_t67keybox_hash(const void *void_key)68{69const struct keybox *key = void_key;70return _mesa_hash_data(&key->cache_id, key->size + sizeof(key->cache_id));71}7273static bool74keybox_equals(const void *void_a, const void *void_b)75{76const struct keybox *a = void_a, *b = void_b;77if (a->size != b->size)78return false;7980return memcmp(a->data, b->data, a->size) == 0;81}8283struct crocus_compiled_shader *84crocus_find_cached_shader(struct crocus_context *ice,85enum crocus_program_cache_id cache_id,86uint32_t key_size, const void *key)87{88struct keybox *keybox = make_keybox(NULL, cache_id, key, key_size);89struct hash_entry *entry =90_mesa_hash_table_search(ice->shaders.cache, keybox);9192ralloc_free(keybox);9394return entry ? entry->data : NULL;95}9697const void *98crocus_find_previous_compile(const struct crocus_context *ice,99enum crocus_program_cache_id cache_id,100unsigned program_string_id)101{102hash_table_foreach(ice->shaders.cache, entry) {103const struct keybox *keybox = entry->key;104const struct brw_base_prog_key *key = (const void *)keybox->data;105if (keybox->cache_id == cache_id &&106key->program_string_id == program_string_id) {107return keybox->data;108}109}110111return NULL;112}113114/**115* Look for an existing entry in the cache that has identical assembly code.116*117* This is useful for programs generating shaders at runtime, where multiple118* distinct shaders (from an API perspective) may compile to the same assembly119* in our backend. This saves space in the program cache buffer.120*/121static const struct crocus_compiled_shader *122find_existing_assembly(struct hash_table *cache, void *map,123const void *assembly, unsigned assembly_size)124{125hash_table_foreach (cache, entry) {126const struct crocus_compiled_shader *existing = entry->data;127128if (existing->map_size != assembly_size)129continue;130131if (memcmp(map + existing->offset, assembly, assembly_size) == 0)132return existing;133}134return NULL;135}136137static void138crocus_cache_new_bo(struct crocus_context *ice,139uint32_t new_size)140{141struct crocus_screen *screen = (struct crocus_screen *)ice->ctx.screen;142struct crocus_bo *new_bo;143new_bo = crocus_bo_alloc(screen->bufmgr, "program cache", new_size);144145void *map = crocus_bo_map(NULL, new_bo, MAP_READ | MAP_WRITE |146MAP_ASYNC | MAP_PERSISTENT);147148if (ice->shaders.cache_next_offset != 0) {149memcpy(map, ice->shaders.cache_bo_map, ice->shaders.cache_next_offset);150}151152crocus_bo_unmap(ice->shaders.cache_bo);153crocus_bo_unreference(ice->shaders.cache_bo);154ice->shaders.cache_bo = new_bo;155ice->shaders.cache_bo_map = map;156157if (screen->devinfo.ver == 4) {158/* reemit all shaders on GEN4 only. */159ice->state.dirty |= CROCUS_DIRTY_CLIP | CROCUS_DIRTY_RASTER |160CROCUS_DIRTY_WM;161}162ice->batches[CROCUS_BATCH_RENDER].state_base_address_emitted = false;163ice->batches[CROCUS_BATCH_COMPUTE].state_base_address_emitted = false;164/* unset state base address */165}166167static uint32_t168crocus_alloc_item_data(struct crocus_context *ice, uint32_t size)169{170if (ice->shaders.cache_next_offset + size > ice->shaders.cache_bo->size) {171uint32_t new_size = ice->shaders.cache_bo->size * 2;172while (ice->shaders.cache_next_offset + size > new_size)173new_size *= 2;174175crocus_cache_new_bo(ice, new_size);176}177uint32_t offset = ice->shaders.cache_next_offset;178179/* Programs are always 64-byte aligned, so set up the next one now */180ice->shaders.cache_next_offset = ALIGN(offset + size, 64);181return offset;182}183184struct crocus_compiled_shader *185crocus_upload_shader(struct crocus_context *ice,186enum crocus_program_cache_id cache_id, uint32_t key_size,187const void *key, const void *assembly, uint32_t asm_size,188struct brw_stage_prog_data *prog_data,189uint32_t prog_data_size, uint32_t *streamout,190enum brw_param_builtin *system_values,191unsigned num_system_values, unsigned num_cbufs,192const struct crocus_binding_table *bt)193{194struct hash_table *cache = ice->shaders.cache;195struct crocus_compiled_shader *shader =196rzalloc_size(cache, sizeof(struct crocus_compiled_shader));197const struct crocus_compiled_shader *existing = find_existing_assembly(198cache, ice->shaders.cache_bo_map, assembly, asm_size);199200/* If we can find a matching prog in the cache already, then reuse the201* existing stuff without creating new copy into the underlying buffer202* object. This is notably useful for programs generating shaders at203* runtime, where multiple shaders may compile to the same thing in our204* backend.205*/206if (existing) {207shader->offset = existing->offset;208shader->map_size = existing->map_size;209} else {210shader->offset = crocus_alloc_item_data(ice, asm_size);211shader->map_size = asm_size;212213memcpy(ice->shaders.cache_bo_map + shader->offset, assembly, asm_size);214}215216shader->prog_data = prog_data;217shader->prog_data_size = prog_data_size;218shader->streamout = streamout;219shader->system_values = system_values;220shader->num_system_values = num_system_values;221shader->num_cbufs = num_cbufs;222shader->bt = *bt;223224ralloc_steal(shader, shader->prog_data);225if (prog_data_size > 16) {226ralloc_steal(shader->prog_data, prog_data->param);227ralloc_steal(shader->prog_data, prog_data->pull_param);228}229ralloc_steal(shader, shader->streamout);230ralloc_steal(shader, shader->system_values);231232struct keybox *keybox = make_keybox(shader, cache_id, key, key_size);233_mesa_hash_table_insert(ice->shaders.cache, keybox, shader);234235return shader;236}237238bool239crocus_blorp_lookup_shader(struct blorp_batch *blorp_batch, const void *key,240uint32_t key_size, uint32_t *kernel_out,241void *prog_data_out)242{243struct blorp_context *blorp = blorp_batch->blorp;244struct crocus_context *ice = blorp->driver_ctx;245struct crocus_compiled_shader *shader =246crocus_find_cached_shader(ice, CROCUS_CACHE_BLORP, key_size, key);247248if (!shader)249return false;250251*kernel_out = shader->offset;252*((void **)prog_data_out) = shader->prog_data;253254return true;255}256257bool258crocus_blorp_upload_shader(struct blorp_batch *blorp_batch, uint32_t stage,259const void *key, uint32_t key_size,260const void *kernel, uint32_t kernel_size,261const struct brw_stage_prog_data *prog_data_templ,262uint32_t prog_data_size, uint32_t *kernel_out,263void *prog_data_out)264{265struct blorp_context *blorp = blorp_batch->blorp;266struct crocus_context *ice = blorp->driver_ctx;267268struct brw_stage_prog_data *prog_data = ralloc_size(NULL, prog_data_size);269memcpy(prog_data, prog_data_templ, prog_data_size);270271struct crocus_binding_table bt;272memset(&bt, 0, sizeof(bt));273274struct crocus_compiled_shader *shader = crocus_upload_shader(275ice, CROCUS_CACHE_BLORP, key_size, key, kernel, kernel_size, prog_data,276prog_data_size, NULL, NULL, 0, 0, &bt);277278*kernel_out = shader->offset;279*((void **)prog_data_out) = shader->prog_data;280281return true;282}283284void285crocus_init_program_cache(struct crocus_context *ice)286{287struct crocus_screen *screen = (struct crocus_screen *)ice->ctx.screen;288ice->shaders.cache =289_mesa_hash_table_create(ice, keybox_hash, keybox_equals);290291ice->shaders.cache_bo =292crocus_bo_alloc(screen->bufmgr, "program_cache", 16384);293ice->shaders.cache_bo_map =294crocus_bo_map(NULL, ice->shaders.cache_bo,295MAP_READ | MAP_WRITE | MAP_ASYNC | MAP_PERSISTENT);296}297298void299crocus_destroy_program_cache(struct crocus_context *ice)300{301for (int i = 0; i < MESA_SHADER_STAGES; i++) {302ice->shaders.prog[i] = NULL;303}304305if (ice->shaders.cache_bo) {306crocus_bo_unmap(ice->shaders.cache_bo);307crocus_bo_unreference(ice->shaders.cache_bo);308ice->shaders.cache_bo_map = NULL;309ice->shaders.cache_bo = NULL;310}311312ralloc_free(ice->shaders.cache);313}314315static const char *316cache_name(enum crocus_program_cache_id cache_id)317{318if (cache_id == CROCUS_CACHE_BLORP)319return "BLORP";320321if (cache_id == CROCUS_CACHE_SF)322return "SF";323324if (cache_id == CROCUS_CACHE_CLIP)325return "CLIP";326327if (cache_id == CROCUS_CACHE_FF_GS)328return "FF_GS";329330return _mesa_shader_stage_to_string(cache_id);331}332333void334crocus_print_program_cache(struct crocus_context *ice)335{336struct crocus_screen *screen = (struct crocus_screen *)ice->ctx.screen;337const struct intel_device_info *devinfo = &screen->devinfo;338339hash_table_foreach(ice->shaders.cache, entry) {340const struct keybox *keybox = entry->key;341struct crocus_compiled_shader *shader = entry->data;342fprintf(stderr, "%s:\n", cache_name(keybox->cache_id));343brw_disassemble(devinfo, ice->shaders.cache_bo_map + shader->offset, 0,344shader->prog_data->program_size, NULL, stderr);345}346}347348349