2018-01-20 02:47:04 -08:00
|
|
|
/*
|
|
|
|
|
* Copyright © 2017 Intel Corporation
|
|
|
|
|
*
|
|
|
|
|
* 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
|
2018-08-19 00:31:46 -07:00
|
|
|
* 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:
|
2018-01-20 02:47:04 -08:00
|
|
|
*
|
2018-08-19 00:31:46 -07:00
|
|
|
* The above copyright notice and this permission notice shall be included
|
|
|
|
|
* in all copies or substantial portions of the Software.
|
2018-01-20 02:47:04 -08:00
|
|
|
*
|
2018-08-19 00:31:46 -07:00
|
|
|
* 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.
|
2018-01-20 02:47:04 -08:00
|
|
|
*/
|
2018-07-30 23:49:34 -07:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @file iris_program_cache.c
|
|
|
|
|
*
|
|
|
|
|
* The in-memory program cache. This is basically a hash table mapping
|
|
|
|
|
* API-specified shaders and a state key to a compiled variant. It also
|
|
|
|
|
* takes care of uploading shader assembly into a BO for use on the GPU.
|
|
|
|
|
*/
|
|
|
|
|
|
2018-01-20 02:47:04 -08:00
|
|
|
#include <stdio.h>
|
|
|
|
|
#include <errno.h>
|
|
|
|
|
#include "pipe/p_defines.h"
|
|
|
|
|
#include "pipe/p_state.h"
|
|
|
|
|
#include "pipe/p_context.h"
|
|
|
|
|
#include "pipe/p_screen.h"
|
|
|
|
|
#include "util/u_atomic.h"
|
2018-01-25 19:39:10 -08:00
|
|
|
#include "util/u_upload_mgr.h"
|
2018-01-20 02:47:04 -08:00
|
|
|
#include "compiler/nir/nir.h"
|
|
|
|
|
#include "compiler/nir/nir_builder.h"
|
2020-09-01 18:57:52 -05:00
|
|
|
#include "intel/common/gen_disasm.h"
|
2018-01-20 02:47:04 -08:00
|
|
|
#include "intel/compiler/brw_compiler.h"
|
|
|
|
|
#include "intel/compiler/brw_eu.h"
|
|
|
|
|
#include "intel/compiler/brw_nir.h"
|
|
|
|
|
#include "iris_context.h"
|
2018-04-21 23:27:15 -07:00
|
|
|
#include "iris_resource.h"
|
2018-01-20 02:47:04 -08:00
|
|
|
|
|
|
|
|
struct keybox {
|
2019-07-04 18:26:20 -05:00
|
|
|
uint16_t size;
|
2018-01-20 02:47:04 -08:00
|
|
|
enum iris_program_cache_id cache_id;
|
|
|
|
|
uint8_t data[0];
|
|
|
|
|
};
|
|
|
|
|
|
2018-04-21 23:27:15 -07:00
|
|
|
static struct keybox *
|
|
|
|
|
make_keybox(void *mem_ctx,
|
|
|
|
|
enum iris_program_cache_id cache_id,
|
|
|
|
|
const void *key,
|
|
|
|
|
uint32_t key_size)
|
|
|
|
|
{
|
2018-01-20 02:47:04 -08:00
|
|
|
struct keybox *keybox =
|
2018-04-21 23:27:15 -07:00
|
|
|
ralloc_size(mem_ctx, sizeof(struct keybox) + key_size);
|
2018-01-20 02:47:04 -08:00
|
|
|
|
|
|
|
|
keybox->cache_id = cache_id;
|
2018-04-21 23:27:15 -07:00
|
|
|
keybox->size = key_size;
|
|
|
|
|
memcpy(keybox->data, key, key_size);
|
2018-01-20 02:47:04 -08:00
|
|
|
|
|
|
|
|
return keybox;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static uint32_t
|
|
|
|
|
keybox_hash(const void *void_key)
|
|
|
|
|
{
|
|
|
|
|
const struct keybox *key = void_key;
|
|
|
|
|
return _mesa_hash_data(&key->cache_id, key->size + sizeof(key->cache_id));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static bool
|
|
|
|
|
keybox_equals(const void *void_a, const void *void_b)
|
|
|
|
|
{
|
|
|
|
|
const struct keybox *a = void_a, *b = void_b;
|
|
|
|
|
if (a->size != b->size)
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
return memcmp(a->data, b->data, a->size) == 0;
|
|
|
|
|
}
|
|
|
|
|
|
2018-11-21 18:15:28 -08:00
|
|
|
struct iris_compiled_shader *
|
2018-04-21 23:27:15 -07:00
|
|
|
iris_find_cached_shader(struct iris_context *ice,
|
|
|
|
|
enum iris_program_cache_id cache_id,
|
2018-11-21 18:15:28 -08:00
|
|
|
uint32_t key_size,
|
|
|
|
|
const void *key)
|
2018-04-21 23:27:15 -07:00
|
|
|
{
|
2019-06-05 13:15:35 -07:00
|
|
|
struct keybox *keybox = make_keybox(NULL, cache_id, key, key_size);
|
2018-04-21 23:27:15 -07:00
|
|
|
struct hash_entry *entry =
|
|
|
|
|
_mesa_hash_table_search(ice->shaders.cache, keybox);
|
|
|
|
|
|
2018-06-16 10:39:33 -07:00
|
|
|
ralloc_free(keybox);
|
|
|
|
|
|
2018-04-21 23:27:15 -07:00
|
|
|
return entry ? entry->data : NULL;
|
|
|
|
|
}
|
|
|
|
|
|
2018-01-20 02:47:04 -08:00
|
|
|
const void *
|
2018-01-25 19:39:10 -08:00
|
|
|
iris_find_previous_compile(const struct iris_context *ice,
|
2018-01-20 02:47:04 -08:00
|
|
|
enum iris_program_cache_id cache_id,
|
|
|
|
|
unsigned program_string_id)
|
|
|
|
|
{
|
2018-01-25 19:39:10 -08:00
|
|
|
hash_table_foreach(ice->shaders.cache, entry) {
|
2018-01-20 02:47:04 -08:00
|
|
|
const struct keybox *keybox = entry->key;
|
2019-02-21 17:20:39 -06:00
|
|
|
const struct brw_base_prog_key *key = (const void *)keybox->data;
|
2018-01-20 02:47:04 -08:00
|
|
|
if (keybox->cache_id == cache_id &&
|
2019-02-21 17:20:39 -06:00
|
|
|
key->program_string_id == program_string_id) {
|
2018-01-20 02:47:04 -08:00
|
|
|
return keybox->data;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
|
2019-06-05 13:12:58 -07:00
|
|
|
void
|
|
|
|
|
iris_delete_shader_variants(struct iris_context *ice,
|
|
|
|
|
struct iris_uncompiled_shader *ish)
|
|
|
|
|
{
|
|
|
|
|
struct hash_table *cache = ice->shaders.cache;
|
|
|
|
|
gl_shader_stage stage = ish->nir->info.stage;
|
|
|
|
|
enum iris_program_cache_id cache_id = stage;
|
|
|
|
|
|
|
|
|
|
hash_table_foreach(cache, entry) {
|
|
|
|
|
const struct keybox *keybox = entry->key;
|
|
|
|
|
const struct brw_base_prog_key *key = (const void *)keybox->data;
|
|
|
|
|
|
|
|
|
|
if (keybox->cache_id == cache_id &&
|
|
|
|
|
key->program_string_id == ish->program_id) {
|
|
|
|
|
struct iris_compiled_shader *shader = entry->data;
|
|
|
|
|
|
|
|
|
|
_mesa_hash_table_remove(cache, entry);
|
|
|
|
|
|
|
|
|
|
/* Shader variants may still be bound in the context even after
|
|
|
|
|
* the API-facing shader has been deleted. In particular, a draw
|
|
|
|
|
* may not have triggered iris_update_compiled_shaders() yet. In
|
|
|
|
|
* that case, we may be referring to that shader's VUE map, stream
|
|
|
|
|
* output settings, and so on. We also like to compare the old and
|
|
|
|
|
* new shader programs when swapping them out to flag dirty state.
|
|
|
|
|
*
|
|
|
|
|
* So, it's hazardous to delete a bound shader variant. We avoid
|
|
|
|
|
* doing so, choosing to instead move "deleted" shader variants to
|
|
|
|
|
* a list, deferring the actual deletion until they're not bound.
|
|
|
|
|
*
|
|
|
|
|
* For simplicity, we always move deleted variants to the list,
|
|
|
|
|
* even if we could delete them immediately. We'll then process
|
|
|
|
|
* the list, catching both these variants and any others.
|
|
|
|
|
*/
|
|
|
|
|
list_addtail(&shader->link, &ice->shaders.deleted_variants[stage]);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Process any pending deferred variant deletions. */
|
|
|
|
|
list_for_each_entry_safe(struct iris_compiled_shader, shader,
|
|
|
|
|
&ice->shaders.deleted_variants[stage], link) {
|
|
|
|
|
/* If the shader is still bound, defer deletion. */
|
|
|
|
|
if (ice->shaders.prog[stage] == shader)
|
|
|
|
|
continue;
|
|
|
|
|
|
|
|
|
|
list_del(&shader->link);
|
|
|
|
|
|
|
|
|
|
/* Actually delete the variant. */
|
|
|
|
|
pipe_resource_reference(&shader->assembly.res, NULL);
|
|
|
|
|
ralloc_free(shader);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2018-11-21 16:45:14 -08:00
|
|
|
struct iris_compiled_shader *
|
2018-04-21 23:27:15 -07:00
|
|
|
iris_upload_shader(struct iris_context *ice,
|
|
|
|
|
enum iris_program_cache_id cache_id,
|
|
|
|
|
uint32_t key_size,
|
|
|
|
|
const void *key,
|
|
|
|
|
const void *assembly,
|
2018-06-29 12:58:31 -07:00
|
|
|
struct brw_stage_prog_data *prog_data,
|
2018-11-09 02:04:23 -08:00
|
|
|
uint32_t *streamout,
|
|
|
|
|
enum brw_param_builtin *system_values,
|
2019-01-19 11:32:37 -08:00
|
|
|
unsigned num_system_values,
|
2020-08-11 10:07:55 -05:00
|
|
|
unsigned kernel_input_size,
|
2019-05-22 22:17:27 -07:00
|
|
|
unsigned num_cbufs,
|
|
|
|
|
const struct iris_binding_table *bt)
|
2018-01-20 02:47:04 -08:00
|
|
|
{
|
2018-01-25 19:39:10 -08:00
|
|
|
struct hash_table *cache = ice->shaders.cache;
|
2020-03-30 10:37:29 -04:00
|
|
|
struct iris_screen *screen = (struct iris_screen *)ice->ctx.screen;
|
2018-01-22 11:52:58 -08:00
|
|
|
struct iris_compiled_shader *shader =
|
2018-06-16 10:15:24 -07:00
|
|
|
rzalloc_size(cache, sizeof(struct iris_compiled_shader) +
|
2020-03-30 10:37:29 -04:00
|
|
|
screen->vtbl.derived_program_state_size(cache_id));
|
2018-01-20 02:47:04 -08:00
|
|
|
|
2019-10-19 12:21:17 -07:00
|
|
|
shader->assembly.res = NULL;
|
|
|
|
|
u_upload_alloc(ice->shaders.uploader, 0, prog_data->program_size, 64,
|
|
|
|
|
&shader->assembly.offset, &shader->assembly.res,
|
|
|
|
|
&shader->map);
|
|
|
|
|
memcpy(shader->map, assembly, prog_data->program_size);
|
|
|
|
|
|
|
|
|
|
struct iris_resource *res = (void *) shader->assembly.res;
|
|
|
|
|
uint64_t shader_data_addr = res->bo->gtt_offset +
|
|
|
|
|
shader->assembly.offset +
|
|
|
|
|
prog_data->const_data_offset;
|
|
|
|
|
|
|
|
|
|
struct brw_shader_reloc_value reloc_values[] = {
|
|
|
|
|
{
|
|
|
|
|
.id = IRIS_SHADER_RELOC_CONST_DATA_ADDR_LOW,
|
|
|
|
|
.value = shader_data_addr,
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
.id = IRIS_SHADER_RELOC_CONST_DATA_ADDR_HIGH,
|
|
|
|
|
.value = shader_data_addr >> 32,
|
|
|
|
|
},
|
|
|
|
|
};
|
|
|
|
|
brw_write_shader_relocs(&screen->devinfo, shader->map, prog_data,
|
|
|
|
|
reloc_values, ARRAY_SIZE(reloc_values));
|
2018-01-20 02:47:04 -08:00
|
|
|
|
2019-06-05 13:12:58 -07:00
|
|
|
list_inithead(&shader->link);
|
|
|
|
|
|
2018-01-22 11:52:58 -08:00
|
|
|
shader->prog_data = prog_data;
|
2018-07-11 12:45:19 -07:00
|
|
|
shader->streamout = streamout;
|
2018-11-09 02:04:23 -08:00
|
|
|
shader->system_values = system_values;
|
|
|
|
|
shader->num_system_values = num_system_values;
|
2020-08-11 10:07:55 -05:00
|
|
|
shader->kernel_input_size = kernel_input_size;
|
2019-01-19 11:32:37 -08:00
|
|
|
shader->num_cbufs = num_cbufs;
|
2019-05-22 22:17:27 -07:00
|
|
|
shader->bt = *bt;
|
2018-01-20 02:47:04 -08:00
|
|
|
|
2018-04-22 21:25:51 -07:00
|
|
|
ralloc_steal(shader, shader->prog_data);
|
2020-08-11 19:43:17 -05:00
|
|
|
ralloc_steal(shader->prog_data, (void *)prog_data->relocs);
|
2018-04-22 21:25:51 -07:00
|
|
|
ralloc_steal(shader->prog_data, prog_data->param);
|
|
|
|
|
ralloc_steal(shader->prog_data, prog_data->pull_param);
|
2018-07-11 12:45:19 -07:00
|
|
|
ralloc_steal(shader, shader->streamout);
|
2018-11-09 02:04:23 -08:00
|
|
|
ralloc_steal(shader, shader->system_values);
|
2018-04-22 21:25:51 -07:00
|
|
|
|
2018-04-21 23:27:15 -07:00
|
|
|
/* Store the 3DSTATE shader packets and other derived state. */
|
2020-03-30 10:37:29 -04:00
|
|
|
screen->vtbl.store_derived_program_state(ice, cache_id, shader);
|
2018-04-21 23:27:15 -07:00
|
|
|
|
2019-06-05 13:15:35 -07:00
|
|
|
struct keybox *keybox = make_keybox(shader, cache_id, key, key_size);
|
2018-04-21 23:27:15 -07:00
|
|
|
_mesa_hash_table_insert(ice->shaders.cache, keybox, shader);
|
|
|
|
|
|
|
|
|
|
return shader;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool
|
|
|
|
|
iris_blorp_lookup_shader(struct blorp_batch *blorp_batch,
|
|
|
|
|
const void *key, uint32_t key_size,
|
|
|
|
|
uint32_t *kernel_out, void *prog_data_out)
|
|
|
|
|
{
|
|
|
|
|
struct blorp_context *blorp = blorp_batch->blorp;
|
|
|
|
|
struct iris_context *ice = blorp->driver_ctx;
|
|
|
|
|
struct iris_batch *batch = blorp_batch->driver_batch;
|
|
|
|
|
struct iris_compiled_shader *shader =
|
2018-11-21 18:15:28 -08:00
|
|
|
iris_find_cached_shader(ice, IRIS_CACHE_BLORP, key_size, key);
|
2018-01-20 02:47:04 -08:00
|
|
|
|
2018-04-21 23:27:15 -07:00
|
|
|
if (!shader)
|
|
|
|
|
return false;
|
|
|
|
|
|
2018-06-28 00:57:49 -07:00
|
|
|
struct iris_bo *bo = iris_resource_bo(shader->assembly.res);
|
|
|
|
|
*kernel_out =
|
|
|
|
|
iris_bo_offset_from_base_address(bo) + shader->assembly.offset;
|
2018-04-21 23:27:15 -07:00
|
|
|
*((void **) prog_data_out) = shader->prog_data;
|
|
|
|
|
|
2020-05-29 16:38:43 -07:00
|
|
|
iris_use_pinned_bo(batch, bo, false, IRIS_DOMAIN_NONE);
|
2018-04-21 23:27:15 -07:00
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool
|
2020-03-12 14:27:13 -07:00
|
|
|
iris_blorp_upload_shader(struct blorp_batch *blorp_batch, uint32_t stage,
|
2018-04-21 23:27:15 -07:00
|
|
|
const void *key, uint32_t key_size,
|
|
|
|
|
const void *kernel, UNUSED uint32_t kernel_size,
|
2018-04-22 21:25:51 -07:00
|
|
|
const struct brw_stage_prog_data *prog_data_templ,
|
2018-04-21 23:27:15 -07:00
|
|
|
UNUSED uint32_t prog_data_size,
|
|
|
|
|
uint32_t *kernel_out, void *prog_data_out)
|
|
|
|
|
{
|
|
|
|
|
struct blorp_context *blorp = blorp_batch->blorp;
|
|
|
|
|
struct iris_context *ice = blorp->driver_ctx;
|
|
|
|
|
struct iris_batch *batch = blorp_batch->driver_batch;
|
|
|
|
|
|
2018-04-22 21:25:51 -07:00
|
|
|
void *prog_data = ralloc_size(NULL, prog_data_size);
|
|
|
|
|
memcpy(prog_data, prog_data_templ, prog_data_size);
|
|
|
|
|
|
2019-05-22 22:17:27 -07:00
|
|
|
struct iris_binding_table bt;
|
|
|
|
|
memset(&bt, 0, sizeof(bt));
|
|
|
|
|
|
2018-04-21 23:27:15 -07:00
|
|
|
struct iris_compiled_shader *shader =
|
|
|
|
|
iris_upload_shader(ice, IRIS_CACHE_BLORP, key_size, key, kernel,
|
2020-08-11 10:07:55 -05:00
|
|
|
prog_data, NULL, NULL, 0, 0, 0, &bt);
|
2018-04-21 23:27:15 -07:00
|
|
|
|
2018-06-28 00:57:49 -07:00
|
|
|
struct iris_bo *bo = iris_resource_bo(shader->assembly.res);
|
|
|
|
|
*kernel_out =
|
|
|
|
|
iris_bo_offset_from_base_address(bo) + shader->assembly.offset;
|
2018-04-21 23:27:15 -07:00
|
|
|
*((void **) prog_data_out) = shader->prog_data;
|
|
|
|
|
|
2020-05-29 16:38:43 -07:00
|
|
|
iris_use_pinned_bo(batch, bo, false, IRIS_DOMAIN_NONE);
|
2018-04-21 23:27:15 -07:00
|
|
|
|
|
|
|
|
return true;
|
2018-01-20 02:47:04 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
|
iris_init_program_cache(struct iris_context *ice)
|
|
|
|
|
{
|
2018-01-25 19:39:10 -08:00
|
|
|
ice->shaders.cache =
|
|
|
|
|
_mesa_hash_table_create(ice, keybox_hash, keybox_equals);
|
2018-01-20 02:47:04 -08:00
|
|
|
|
2018-01-25 19:39:10 -08:00
|
|
|
ice->shaders.uploader =
|
|
|
|
|
u_upload_create(&ice->ctx, 16384, PIPE_BIND_CUSTOM, PIPE_USAGE_IMMUTABLE,
|
2018-04-05 21:48:33 -07:00
|
|
|
IRIS_RESOURCE_FLAG_SHADER_MEMZONE);
|
2019-06-05 13:12:58 -07:00
|
|
|
|
|
|
|
|
for (int i = 0; i < MESA_SHADER_STAGES; i++)
|
|
|
|
|
list_inithead(&ice->shaders.deleted_variants[i]);
|
2018-01-20 02:47:04 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
|
iris_destroy_program_cache(struct iris_context *ice)
|
|
|
|
|
{
|
|
|
|
|
for (int i = 0; i < MESA_SHADER_STAGES; i++) {
|
2018-01-22 11:52:58 -08:00
|
|
|
ice->shaders.prog[i] = NULL;
|
2019-06-05 13:12:58 -07:00
|
|
|
|
|
|
|
|
list_for_each_entry_safe(struct iris_compiled_shader, shader,
|
|
|
|
|
&ice->shaders.deleted_variants[i], link) {
|
|
|
|
|
pipe_resource_reference(&shader->assembly.res, NULL);
|
|
|
|
|
}
|
2018-01-20 02:47:04 -08:00
|
|
|
}
|
2018-01-22 11:52:58 -08:00
|
|
|
|
2018-06-16 09:56:59 -07:00
|
|
|
hash_table_foreach(ice->shaders.cache, entry) {
|
|
|
|
|
struct iris_compiled_shader *shader = entry->data;
|
2018-06-28 00:57:49 -07:00
|
|
|
pipe_resource_reference(&shader->assembly.res, NULL);
|
2018-06-16 09:56:59 -07:00
|
|
|
}
|
|
|
|
|
|
2018-01-25 19:39:10 -08:00
|
|
|
u_upload_destroy(ice->shaders.uploader);
|
|
|
|
|
|
|
|
|
|
ralloc_free(ice->shaders.cache);
|
2018-01-20 02:47:04 -08:00
|
|
|
}
|