intel/blorp: Rework our usage of ralloc when compiling shaders

Previously, we were creating the shader with a NULL ralloc context and then
trusting in blorp_compile_fs to clean it up.  The only problem was that
blorp_compile_fs didn't clean up its context properly so we were leaking.
When I went to fix that, I realized that it couldn't because it has to
return the shader binary which is allocated off of that context and used by
the caller.  The solution is to make blorp_compile_fs take a ralloc
context, allocate the nir_shaders directly off that context, and clean it
all up in whatever function creates the shader and calls blorp_compile_fs.

Signed-off-by: Jason Ekstrand <jason@jlekstrand.net>
Reviewed-by: Topi Pohjolainen <topi.pohjolainen@intel.com>
Reviewed-by: Timothy Arceri <timothy.arceri@collabora.com>
Cc: "12.0, 13.0" <mesa-stable@lists.freedesktop.org>
This commit is contained in:
Jason Ekstrand
2016-10-21 12:09:38 -07:00
parent ab92480272
commit 43dadb6edd
4 changed files with 14 additions and 15 deletions

View File

@@ -165,7 +165,8 @@ nir_uniform_type_size(const struct glsl_type *type)
} }
const unsigned * const unsigned *
blorp_compile_fs(struct blorp_context *blorp, struct nir_shader *nir, blorp_compile_fs(struct blorp_context *blorp, void *mem_ctx,
struct nir_shader *nir,
const struct brw_wm_prog_key *wm_key, const struct brw_wm_prog_key *wm_key,
bool use_repclear, bool use_repclear,
struct brw_blorp_prog_data *prog_data, struct brw_blorp_prog_data *prog_data,
@@ -173,13 +174,6 @@ blorp_compile_fs(struct blorp_context *blorp, struct nir_shader *nir,
{ {
const struct brw_compiler *compiler = blorp->compiler; const struct brw_compiler *compiler = blorp->compiler;
void *mem_ctx = ralloc_context(NULL);
/* Calling brw_preprocess_nir and friends is destructive and, if cloning is
* enabled, may end up completely replacing the nir_shader. Therefore, we
* own it and might as well put it in our context for easy cleanup.
*/
ralloc_steal(mem_ctx, nir);
nir->options = nir->options =
compiler->glsl_compiler_options[MESA_SHADER_FRAGMENT].NirOptions; compiler->glsl_compiler_options[MESA_SHADER_FRAGMENT].NirOptions;

View File

@@ -997,7 +997,7 @@ blorp_nir_manual_blend_bilinear(nir_builder *b, nir_ssa_def *pos,
* of samples). * of samples).
*/ */
static nir_shader * static nir_shader *
brw_blorp_build_nir_shader(struct blorp_context *blorp, brw_blorp_build_nir_shader(struct blorp_context *blorp, void *mem_ctx,
const struct brw_blorp_blit_prog_key *key) const struct brw_blorp_blit_prog_key *key)
{ {
const struct gen_device_info *devinfo = blorp->isl_dev->info; const struct gen_device_info *devinfo = blorp->isl_dev->info;
@@ -1044,7 +1044,7 @@ brw_blorp_build_nir_shader(struct blorp_context *blorp,
(key->dst_samples <= 1)); (key->dst_samples <= 1));
nir_builder b; nir_builder b;
nir_builder_init_simple_shader(&b, NULL, MESA_SHADER_FRAGMENT, NULL); nir_builder_init_simple_shader(&b, mem_ctx, MESA_SHADER_FRAGMENT, NULL);
struct brw_blorp_blit_vars v; struct brw_blorp_blit_vars v;
brw_blorp_blit_vars_init(&b, &v, key); brw_blorp_blit_vars_init(&b, &v, key);
@@ -1233,6 +1233,8 @@ brw_blorp_get_blit_kernel(struct blorp_context *blorp,
&params->wm_prog_kernel, &params->wm_prog_data)) &params->wm_prog_kernel, &params->wm_prog_data))
return; return;
void *mem_ctx = ralloc_context(NULL);
const unsigned *program; const unsigned *program;
unsigned program_size; unsigned program_size;
struct brw_blorp_prog_data prog_data; struct brw_blorp_prog_data prog_data;
@@ -1240,7 +1242,7 @@ brw_blorp_get_blit_kernel(struct blorp_context *blorp,
/* Try and compile with NIR first. If that fails, fall back to the old /* Try and compile with NIR first. If that fails, fall back to the old
* method of building shaders manually. * method of building shaders manually.
*/ */
nir_shader *nir = brw_blorp_build_nir_shader(blorp, prog_key); nir_shader *nir = brw_blorp_build_nir_shader(blorp, mem_ctx, prog_key);
struct brw_wm_prog_key wm_key; struct brw_wm_prog_key wm_key;
brw_blorp_init_wm_prog_key(&wm_key); brw_blorp_init_wm_prog_key(&wm_key);
wm_key.tex.compressed_multisample_layout_mask = wm_key.tex.compressed_multisample_layout_mask =
@@ -1248,13 +1250,15 @@ brw_blorp_get_blit_kernel(struct blorp_context *blorp,
wm_key.tex.msaa_16 = prog_key->tex_samples == 16; wm_key.tex.msaa_16 = prog_key->tex_samples == 16;
wm_key.multisample_fbo = prog_key->rt_samples > 1; wm_key.multisample_fbo = prog_key->rt_samples > 1;
program = blorp_compile_fs(blorp, nir, &wm_key, false, program = blorp_compile_fs(blorp, mem_ctx, nir, &wm_key, false,
&prog_data, &program_size); &prog_data, &program_size);
blorp->upload_shader(blorp, prog_key, sizeof(*prog_key), blorp->upload_shader(blorp, prog_key, sizeof(*prog_key),
program, program_size, program, program_size,
&prog_data, sizeof(prog_data), &prog_data, sizeof(prog_data),
&params->wm_prog_kernel, &params->wm_prog_data); &params->wm_prog_kernel, &params->wm_prog_data);
ralloc_free(mem_ctx);
} }
static void static void

View File

@@ -55,7 +55,7 @@ blorp_params_get_clear_kernel(struct blorp_context *blorp,
void *mem_ctx = ralloc_context(NULL); void *mem_ctx = ralloc_context(NULL);
nir_builder b; nir_builder b;
nir_builder_init_simple_shader(&b, NULL, MESA_SHADER_FRAGMENT, NULL); nir_builder_init_simple_shader(&b, mem_ctx, MESA_SHADER_FRAGMENT, NULL);
b.shader->info->name = ralloc_strdup(b.shader, "BLORP-clear"); b.shader->info->name = ralloc_strdup(b.shader, "BLORP-clear");
nir_variable *v_color = nir_variable_create(b.shader, nir_var_shader_in, nir_variable *v_color = nir_variable_create(b.shader, nir_var_shader_in,
@@ -76,7 +76,7 @@ blorp_params_get_clear_kernel(struct blorp_context *blorp,
struct brw_blorp_prog_data prog_data; struct brw_blorp_prog_data prog_data;
unsigned program_size; unsigned program_size;
const unsigned *program = const unsigned *program =
blorp_compile_fs(blorp, b.shader, &wm_key, use_replicated_data, blorp_compile_fs(blorp, mem_ctx, b.shader, &wm_key, use_replicated_data,
&prog_data, &program_size); &prog_data, &program_size);
blorp->upload_shader(blorp, &blorp_key, sizeof(blorp_key), blorp->upload_shader(blorp, &blorp_key, sizeof(blorp_key),

View File

@@ -310,7 +310,8 @@ struct brw_blorp_blit_prog_key
void brw_blorp_init_wm_prog_key(struct brw_wm_prog_key *wm_key); void brw_blorp_init_wm_prog_key(struct brw_wm_prog_key *wm_key);
const unsigned * const unsigned *
blorp_compile_fs(struct blorp_context *blorp, struct nir_shader *nir, blorp_compile_fs(struct blorp_context *blorp, void *mem_ctx,
struct nir_shader *nir,
const struct brw_wm_prog_key *wm_key, const struct brw_wm_prog_key *wm_key,
bool use_repclear, bool use_repclear,
struct brw_blorp_prog_data *prog_data, struct brw_blorp_prog_data *prog_data,