[Mesa-dev] [PATCH 1/4] radeon/llvm: Use a struct for storing compiled code

Tom Stellard tom at stellard.net
Fri Apr 5 11:54:12 PDT 2013


From: Tom Stellard <thomas.stellard at amd.com>

---
 src/gallium/drivers/r600/r600_llvm.c            |  8 +++++--
 src/gallium/drivers/radeon/radeon_llvm_emit.cpp | 12 +++++-----
 src/gallium/drivers/radeon/radeon_llvm_emit.h   | 11 +++++++--
 src/gallium/drivers/radeonsi/radeonsi_shader.c  | 31 +++++++++++++------------
 4 files changed, 37 insertions(+), 25 deletions(-)

diff --git a/src/gallium/drivers/r600/r600_llvm.c b/src/gallium/drivers/r600/r600_llvm.c
index be8ad15..e98b87f 100644
--- a/src/gallium/drivers/r600/r600_llvm.c
+++ b/src/gallium/drivers/r600/r600_llvm.c
@@ -620,9 +620,13 @@ unsigned r600_llvm_compile(
 	enum radeon_family family,
 	unsigned dump)
 {
+	unsigned r;
+	struct radeon_llvm_binary binary;
 	const char * gpu_family = r600_llvm_gpu_string(family);
-	return radeon_llvm_compile(mod, inst_bytes, inst_byte_count,
-							gpu_family, dump);
+	r = radeon_llvm_compile(mod, &binary, gpu_family, dump);
+	*inst_bytes = binary.code;
+	*inst_byte_count = binary.code_size;
+	return r;
 }
 
 #endif
diff --git a/src/gallium/drivers/radeon/radeon_llvm_emit.cpp b/src/gallium/drivers/radeon/radeon_llvm_emit.cpp
index a23532b..9c5fd78 100644
--- a/src/gallium/drivers/radeon/radeon_llvm_emit.cpp
+++ b/src/gallium/drivers/radeon/radeon_llvm_emit.cpp
@@ -96,9 +96,8 @@ radeon_llvm_shader_type(LLVMValueRef F, unsigned type)
  * caller's responsibility to free it.
  */
 extern "C" unsigned
-radeon_llvm_compile(LLVMModuleRef M, unsigned char ** bytes,
-                 unsigned * byte_count, const char * gpu_family,
-                 unsigned dump) {
+radeon_llvm_compile(LLVMModuleRef M, struct radeon_llvm_binary *binary,
+                 const char * gpu_family, unsigned dump) {
 
    Triple AMDGPUTriple(sys::getDefaultTargetTriple());
 
@@ -155,9 +154,10 @@ radeon_llvm_compile(LLVMModuleRef M, unsigned char ** bytes,
    out.flush();
    std::string &data = oStream.str();
 
-   *bytes = (unsigned char*)malloc(data.length() * sizeof(unsigned char));
-   memcpy(*bytes, data.c_str(), data.length() * sizeof(unsigned char));
-   *byte_count = data.length();
+
+   binary->code = (unsigned char*)malloc(data.length() * sizeof(unsigned char));
+   memcpy(binary->code, data.c_str(), data.length() * sizeof(unsigned char));
+   binary->code_size = data.length();
 
    return 0;
 }
diff --git a/src/gallium/drivers/radeon/radeon_llvm_emit.h b/src/gallium/drivers/radeon/radeon_llvm_emit.h
index b68100f..f78fc19 100644
--- a/src/gallium/drivers/radeon/radeon_llvm_emit.h
+++ b/src/gallium/drivers/radeon/radeon_llvm_emit.h
@@ -29,6 +29,14 @@
 
 #include <llvm-c/Core.h>
 
+struct radeon_llvm_binary {
+	unsigned char *code;
+	unsigned code_size;
+	unsigned char *config;
+	unsigned config_size;
+};
+
+
 #ifdef __cplusplus
 extern "C" {
 #endif
@@ -42,8 +50,7 @@ unsigned radeon_llvm_bitcode_compile(
 
 unsigned  radeon_llvm_compile(
 	LLVMModuleRef M,
-	unsigned char ** bytes,
-	unsigned * byte_count,
+	struct radeon_llvm_binary *binary,
 	const char * gpu_family,
 	unsigned dump);
 
diff --git a/src/gallium/drivers/radeonsi/radeonsi_shader.c b/src/gallium/drivers/radeonsi/radeonsi_shader.c
index 5fdf46e..218997f 100644
--- a/src/gallium/drivers/radeonsi/radeonsi_shader.c
+++ b/src/gallium/drivers/radeonsi/radeonsi_shader.c
@@ -1103,8 +1103,7 @@ int si_pipe_shader_create(
 	struct tgsi_shader_info shader_info;
 	struct lp_build_tgsi_context * bld_base;
 	LLVMModuleRef mod;
-	unsigned char * inst_bytes;
-	unsigned inst_byte_count;
+	struct radeon_llvm_binary binary;
 	unsigned i;
 	uint32_t *ptr;
 	bool dump;
@@ -1165,19 +1164,20 @@ int si_pipe_shader_create(
 	if (dump) {
 		LLVMDumpModule(mod);
 	}
-	radeon_llvm_compile(mod, &inst_bytes, &inst_byte_count, "SI", dump);
+	memset(&binary, 0, sizeof(binary));
+	radeon_llvm_compile(mod, &binary, "SI", dump);
 	if (dump) {
 		fprintf(stderr, "SI CODE:\n");
-		for (i = 0; i < inst_byte_count; i+=4 ) {
-			fprintf(stderr, "%02x%02x%02x%02x\n", inst_bytes[i + 3],
-				inst_bytes[i + 2], inst_bytes[i + 1],
-				inst_bytes[i]);
+		for (i = 0; i < binary.code_size; i+=4 ) {
+			fprintf(stderr, "%02x%02x%02x%02x\n", binary.code[i + 3],
+				binary.code[i + 2], binary.code[i + 1],
+				binary.code[i]);
 		}
 	}
 
-	shader->num_sgprs = util_le32_to_cpu(*(uint32_t*)inst_bytes);
-	shader->num_vgprs = util_le32_to_cpu(*(uint32_t*)(inst_bytes + 4));
-	shader->spi_ps_input_ena = util_le32_to_cpu(*(uint32_t*)(inst_bytes + 8));
+	shader->num_sgprs = util_le32_to_cpu(*(uint32_t*)binary.code);
+	shader->num_vgprs = util_le32_to_cpu(*(uint32_t*)(binary.code + 4));
+	shader->spi_ps_input_ena = util_le32_to_cpu(*(uint32_t*)(binary.code + 8));
 
 	radeon_llvm_dispose(&si_shader_ctx.radeon_bld);
 	tgsi_parse_free(&si_shader_ctx.parse);
@@ -1185,7 +1185,7 @@ int si_pipe_shader_create(
 	/* copy new shader */
 	si_resource_reference(&shader->bo, NULL);
 	shader->bo = si_resource_create_custom(ctx->screen, PIPE_USAGE_IMMUTABLE,
-					       inst_byte_count - 12);
+					       binary.code_size - 12);
 	if (shader->bo == NULL) {
 		FREE(si_shader_ctx.constants);
 		FREE(si_shader_ctx.resources);
@@ -1195,18 +1195,19 @@ int si_pipe_shader_create(
 
 	ptr = (uint32_t*)rctx->ws->buffer_map(shader->bo->cs_buf, rctx->cs, PIPE_TRANSFER_WRITE);
 	if (0 /*R600_BIG_ENDIAN*/) {
-		for (i = 0; i < (inst_byte_count-12)/4; ++i) {
-			ptr[i] = util_bswap32(*(uint32_t*)(inst_bytes+12 + i*4));
+		for (i = 0; i < (binary.code_size-12)/4; ++i) {
+			ptr[i] = util_bswap32(*(uint32_t*)(binary.code+12 + i*4));
 		}
 	} else {
-		memcpy(ptr, inst_bytes + 12, inst_byte_count - 12);
+		memcpy(ptr, binary.code + 12, binary.code_size - 12);
 	}
 	rctx->ws->buffer_unmap(shader->bo->cs_buf);
 
 	FREE(si_shader_ctx.constants);
 	FREE(si_shader_ctx.resources);
 	FREE(si_shader_ctx.samplers);
-	free(inst_bytes);
+	free(binary.code);
+	free(binary.config);
 
 	return 0;
 }
-- 
1.8.1.5



More information about the mesa-dev mailing list