[Mesa-dev] [PATCH 2/2] r600,compute: Plug few memory leaks

Jan Vesely jan.vesely at rutgers.edu
Thu Jan 21 08:17:29 PST 2016


v2: drop inline keyword
    drop radeon_llvm_dispose_kernel_module wrapper

v3: move definitions to .c file
    use in radeonsi

Signed-off-by: Jan Vesely <jan.vesely at rutgers.edu>
---

I'd like to keep r600_destroy_shader, even if it's just on line.
I have additional patches that might add more members to that structure.

Jan

PS: sorry for the delay. I did not have access to my machine during internship.


 src/gallium/drivers/r600/evergreen_compute.c  | 24 +++++++++++++++++++++---
 src/gallium/drivers/r600/r600_llvm.c          | 12 +++++++-----
 src/gallium/drivers/r600/r600_llvm.h          |  2 ++
 src/gallium/drivers/radeon/r600_pipe_common.c | 20 ++++++++++++++++++++
 src/gallium/drivers/radeon/r600_pipe_common.h |  3 +++
 src/gallium/drivers/radeonsi/si_compute.c     |  3 ---
 src/gallium/drivers/radeonsi/si_shader.c      | 11 ++---------
 src/gallium/drivers/radeonsi/si_shader.h      |  1 -
 8 files changed, 55 insertions(+), 21 deletions(-)

diff --git a/src/gallium/drivers/r600/evergreen_compute.c b/src/gallium/drivers/r600/evergreen_compute.c
index 53799d3..27b2da1 100644
--- a/src/gallium/drivers/r600/evergreen_compute.c
+++ b/src/gallium/drivers/r600/evergreen_compute.c
@@ -225,7 +225,7 @@ void *evergreen_create_compute_state(
 		}
 	}
 #else
-	memset(&shader->binary, 0, sizeof(shader->binary));
+	radeon_shader_binary_init(&shader->binary);
 	radeon_elf_read(code, header->num_bytes, &shader->binary);
 	r600_create_shader(&shader->bc, &shader->binary, &use_kill);
 
@@ -245,13 +245,31 @@ void *evergreen_create_compute_state(
 	return shader;
 }
 
-void evergreen_delete_compute_state(struct pipe_context *ctx, void* state)
+void evergreen_delete_compute_state(struct pipe_context *ctx_, void* state)
 {
-	struct r600_pipe_compute *shader = (struct r600_pipe_compute *)state;
+	struct r600_context *ctx = (struct r600_context *)ctx_;
+	COMPUTE_DBG(ctx->screen, "*** evergreen_delete_compute_state\n");
+	struct r600_pipe_compute *shader = state;
 
 	if (!shader)
 		return;
 
+#ifdef HAVE_OPENCL
+#if HAVE_LLVM < 0x0306
+	for (unsigned i = 0; i < shader->num_kernels; i++) {
+		struct r600_kernel *kernel = &shader->kernels[i];
+		LLVMDisposeModule(module);
+	}
+	FREE(shader->kernels);
+	LLVMContextDispose(shader->llvm_ctx);
+#else
+	radeon_shader_binary_clean(&shader->binary);
+	r600_destroy_shader(&shader->bc);
+
+	/* TODO destroy shader->code_bo, shader->const_bo
+	 * we'll need something like r600_buffer_free */
+#endif
+#endif
 	FREE(shader);
 }
 
diff --git a/src/gallium/drivers/r600/r600_llvm.c b/src/gallium/drivers/r600/r600_llvm.c
index 8b91372..232db13 100644
--- a/src/gallium/drivers/r600/r600_llvm.c
+++ b/src/gallium/drivers/r600/r600_llvm.c
@@ -910,6 +910,11 @@ unsigned r600_create_shader(struct r600_bytecode *bc,
 	return 0;
 }
 
+void r600_destroy_shader(struct r600_bytecode *bc)
+{
+	FREE(bc->bytecode);
+}
+
 unsigned r600_llvm_compile(
 	LLVMModuleRef mod,
 	enum radeon_family family,
@@ -922,17 +927,14 @@ unsigned r600_llvm_compile(
 	struct radeon_shader_binary binary;
 	const char * gpu_family = r600_get_llvm_processor_name(family);
 
-	memset(&binary, 0, sizeof(struct radeon_shader_binary));
+	radeon_shader_binary_init(&binary);
 	if (dump)
 		LLVMDumpModule(mod);
 	r = radeon_llvm_compile(mod, &binary, gpu_family, NULL, debug);
 
 	r = r600_create_shader(bc, &binary, use_kill);
 
-	FREE(binary.code);
-	FREE(binary.config);
-	FREE(binary.rodata);
-	FREE(binary.global_symbol_offsets);
+	radeon_shader_binary_clean(&binary);
 
 	return r;
 }
diff --git a/src/gallium/drivers/r600/r600_llvm.h b/src/gallium/drivers/r600/r600_llvm.h
index f570b73..3f7fc4b 100644
--- a/src/gallium/drivers/r600/r600_llvm.h
+++ b/src/gallium/drivers/r600/r600_llvm.h
@@ -30,6 +30,8 @@ unsigned r600_create_shader(struct r600_bytecode *bc,
 		const struct radeon_shader_binary *binary,
 		boolean *use_kill);
 
+void r600_destroy_shader(struct r600_bytecode *bc);
+
 void r600_shader_binary_read_config(const struct radeon_shader_binary *binary,
 		struct r600_bytecode *bc,
 		uint64_t symbol_offset,
diff --git a/src/gallium/drivers/radeon/r600_pipe_common.c b/src/gallium/drivers/radeon/r600_pipe_common.c
index e926f56..c328e7e 100644
--- a/src/gallium/drivers/radeon/r600_pipe_common.c
+++ b/src/gallium/drivers/radeon/r600_pipe_common.c
@@ -49,6 +49,26 @@ struct r600_multi_fence {
 };
 
 /*
+ * shader binary helpers.
+ */
+void radeon_shader_binary_init(struct radeon_shader_binary *b)
+{
+	memset(b, 0, sizeof(*b));
+}
+
+void radeon_shader_binary_clean(struct radeon_shader_binary *b)
+{
+	if (!b)
+		return;
+	FREE(b->code);
+	FREE(b->config);
+	FREE(b->rodata);
+	FREE(b->global_symbol_offsets);
+	FREE(b->relocs);
+	FREE(b->disasm_string);
+}
+
+/*
  * pipe_context
  */
 
diff --git a/src/gallium/drivers/radeon/r600_pipe_common.h b/src/gallium/drivers/radeon/r600_pipe_common.h
index 27f6e98..415db18 100644
--- a/src/gallium/drivers/radeon/r600_pipe_common.h
+++ b/src/gallium/drivers/radeon/r600_pipe_common.h
@@ -129,6 +129,9 @@ struct radeon_shader_binary {
 	char *disasm_string;
 };
 
+void radeon_shader_binary_init(struct radeon_shader_binary *b);
+void radeon_shader_binary_clean(struct radeon_shader_binary *b);
+
 struct r600_resource {
 	struct u_resource		b;
 
diff --git a/src/gallium/drivers/radeonsi/si_compute.c b/src/gallium/drivers/radeonsi/si_compute.c
index 5a08cbf..c1a5cc7 100644
--- a/src/gallium/drivers/radeonsi/si_compute.c
+++ b/src/gallium/drivers/radeonsi/si_compute.c
@@ -461,9 +461,6 @@ static void si_delete_compute_state(struct pipe_context *ctx, void* state){
 		LLVMContextDispose(program->llvm_ctx);
 	}
 #else
-	FREE(program->shader.binary.config);
-	FREE(program->shader.binary.rodata);
-	FREE(program->shader.binary.global_symbol_offsets);
 	si_shader_destroy(&program->shader);
 #endif
 
diff --git a/src/gallium/drivers/radeonsi/si_shader.c b/src/gallium/drivers/radeonsi/si_shader.c
index 2de7def..565cb2a 100644
--- a/src/gallium/drivers/radeonsi/si_shader.c
+++ b/src/gallium/drivers/radeonsi/si_shader.c
@@ -4226,14 +4226,6 @@ out:
 	return r;
 }
 
-void si_shader_destroy_binary(struct radeon_shader_binary *binary)
-{
-	FREE(binary->code);
-	FREE(binary->rodata);
-	FREE(binary->relocs);
-	FREE(binary->disasm_string);
-}
-
 void si_shader_destroy(struct si_shader *shader)
 {
 	if (shader->gs_copy_shader) {
@@ -4245,5 +4237,6 @@ void si_shader_destroy(struct si_shader *shader)
 		r600_resource_reference(&shader->scratch_bo, NULL);
 
 	r600_resource_reference(&shader->bo, NULL);
-	si_shader_destroy_binary(&shader->binary);
+
+	radeon_shader_binary_clean(&shader->binary);
 }
diff --git a/src/gallium/drivers/radeonsi/si_shader.h b/src/gallium/drivers/radeonsi/si_shader.h
index 1635358..00a3e3d 100644
--- a/src/gallium/drivers/radeonsi/si_shader.h
+++ b/src/gallium/drivers/radeonsi/si_shader.h
@@ -341,7 +341,6 @@ int si_compile_llvm(struct si_screen *sscreen,
 		    struct pipe_debug_callback *debug,
 		    unsigned processor);
 void si_shader_destroy(struct si_shader *shader);
-void si_shader_destroy_binary(struct radeon_shader_binary *binary);
 unsigned si_shader_io_get_unique_index(unsigned semantic_name, unsigned index);
 int si_shader_binary_upload(struct si_screen *sscreen, struct si_shader *shader);
 void si_shader_dump(struct si_screen *sscreen, struct si_shader *shader,
-- 
2.5.0



More information about the mesa-dev mailing list