[Mesa-dev] [PATCH] ac: move all LLVM module initialization into ac_create_module

Marek Olšák maraeo at gmail.com
Sat Jun 30 04:54:30 UTC 2018


From: Marek Olšák <marek.olsak at amd.com>

This removes some ugly code around module initialization.
---
Dave, please rebase your code on top of this, and we don't need triple
and data_layout in ac_llvm_compiler_info.

 src/amd/common/ac_llvm_helper.cpp                  | 10 ++++++++++
 src/amd/common/ac_llvm_util.h                      |  1 +
 src/amd/vulkan/radv_nir_to_llvm.c                  | 12 ++----------
 src/gallium/drivers/radeonsi/si_pipe.c             | 14 +++-----------
 src/gallium/drivers/radeonsi/si_shader.h           |  2 --
 .../drivers/radeonsi/si_shader_tgsi_setup.c        |  5 +----
 6 files changed, 17 insertions(+), 27 deletions(-)

diff --git a/src/amd/common/ac_llvm_helper.cpp b/src/amd/common/ac_llvm_helper.cpp
index 1a2aee3bc9a..495bd98da9c 100644
--- a/src/amd/common/ac_llvm_helper.cpp
+++ b/src/amd/common/ac_llvm_helper.cpp
@@ -54,20 +54,30 @@ bool ac_is_sgpr_param(LLVMValueRef arg)
 LLVMValueRef ac_llvm_get_called_value(LLVMValueRef call)
 {
 	return LLVMGetCalledValue(call);
 }
 
 bool ac_llvm_is_function(LLVMValueRef v)
 {
 	return LLVMGetValueKind(v) == LLVMFunctionValueKind;
 }
 
+LLVMModuleRef ac_create_module(LLVMTargetMachineRef tm, LLVMContextRef ctx)
+{
+   llvm::TargetMachine *TM = reinterpret_cast<llvm::TargetMachine*>(tm);
+   LLVMModuleRef module = LLVMModuleCreateWithNameInContext("mesa-shader", ctx);
+
+   llvm::unwrap(module)->setTargetTriple(TM->getTargetTriple().getTriple());
+   llvm::unwrap(module)->setDataLayout(TM->createDataLayout());
+   return module;
+}
+
 LLVMBuilderRef ac_create_builder(LLVMContextRef ctx,
 				 enum ac_float_mode float_mode)
 {
 	LLVMBuilderRef builder = LLVMCreateBuilderInContext(ctx);
 
 	llvm::FastMathFlags flags;
 
 	switch (float_mode) {
 	case AC_FLOAT_MODE_DEFAULT:
 		break;
diff --git a/src/amd/common/ac_llvm_util.h b/src/amd/common/ac_llvm_util.h
index 0aa803c5bc1..6e6d15bb56c 100644
--- a/src/amd/common/ac_llvm_util.h
+++ b/src/amd/common/ac_llvm_util.h
@@ -76,20 +76,21 @@ LLVMTargetRef ac_get_llvm_target(const char *triple);
 void ac_add_attr_dereferenceable(LLVMValueRef val, uint64_t bytes);
 bool ac_is_sgpr_param(LLVMValueRef param);
 void ac_add_function_attr(LLVMContextRef ctx, LLVMValueRef function,
                           int attr_idx, enum ac_func_attr attr);
 void ac_add_func_attributes(LLVMContextRef ctx, LLVMValueRef function,
 			    unsigned attrib_mask);
 void ac_dump_module(LLVMModuleRef module);
 
 LLVMValueRef ac_llvm_get_called_value(LLVMValueRef call);
 bool ac_llvm_is_function(LLVMValueRef v);
+LLVMModuleRef ac_create_module(LLVMTargetMachineRef tm, LLVMContextRef ctx);
 
 LLVMBuilderRef ac_create_builder(LLVMContextRef ctx,
 				 enum ac_float_mode float_mode);
 
 void
 ac_llvm_add_target_dep_function_attr(LLVMValueRef F,
 				     const char *name, unsigned value);
 
 static inline unsigned
 ac_get_load_intr_attribs(bool can_speculate)
diff --git a/src/amd/vulkan/radv_nir_to_llvm.c b/src/amd/vulkan/radv_nir_to_llvm.c
index cd8d86603bc..ce6d5e1547d 100644
--- a/src/amd/vulkan/radv_nir_to_llvm.c
+++ b/src/amd/vulkan/radv_nir_to_llvm.c
@@ -3158,28 +3158,21 @@ LLVMModuleRef ac_translate_nir_to_llvm(LLVMTargetMachineRef tm,
                                        const struct radv_nir_compiler_options *options)
 {
 	struct radv_shader_context ctx = {0};
 	unsigned i;
 	ctx.options = options;
 	ctx.shader_info = shader_info;
 	ctx.context = LLVMContextCreate();
 
 	ac_llvm_context_init(&ctx.ac, ctx.context, options->chip_class,
 			     options->family);
-	ctx.ac.module = LLVMModuleCreateWithNameInContext("shader", ctx.context);
-	LLVMSetTarget(ctx.ac.module, options->supports_spill ? "amdgcn-mesa-mesa3d" : "amdgcn--");
-
-	LLVMTargetDataRef data_layout = LLVMCreateTargetDataLayout(tm);
-	char *data_layout_str = LLVMCopyStringRepOfTargetData(data_layout);
-	LLVMSetDataLayout(ctx.ac.module, data_layout_str);
-	LLVMDisposeTargetData(data_layout);
-	LLVMDisposeMessage(data_layout_str);
+	ctx.ac.module = ac_create_module(tm, ctx.context);
 
 	enum ac_float_mode float_mode =
 		options->unsafe_math ? AC_FLOAT_MODE_UNSAFE_FP_MATH :
 				       AC_FLOAT_MODE_DEFAULT;
 
 	ctx.ac.builder = ac_create_builder(ctx.context, float_mode);
 
 	memset(shader_info, 0, sizeof(*shader_info));
 
 	for(int i = 0; i < shader_count; ++i)
@@ -3606,24 +3599,23 @@ radv_compile_gs_copy_shader(LLVMTargetMachineRef tm,
 			    struct radv_shader_variant_info *shader_info,
 			    const struct radv_nir_compiler_options *options)
 {
 	struct radv_shader_context ctx = {0};
 	ctx.context = LLVMContextCreate();
 	ctx.options = options;
 	ctx.shader_info = shader_info;
 
 	ac_llvm_context_init(&ctx.ac, ctx.context, options->chip_class,
 			     options->family);
-	ctx.ac.module = LLVMModuleCreateWithNameInContext("shader", ctx.context);
+	ctx.ac.module = ac_create_module(tm, ctx.context);
 
 	ctx.is_gs_copy_shader = true;
-	LLVMSetTarget(ctx.ac.module, "amdgcn--");
 
 	enum ac_float_mode float_mode =
 		options->unsafe_math ? AC_FLOAT_MODE_UNSAFE_FP_MATH :
 				       AC_FLOAT_MODE_DEFAULT;
 
 	ctx.ac.builder = ac_create_builder(ctx.context, float_mode);
 	ctx.stage = MESA_SHADER_VERTEX;
 
 	radv_nir_shader_info_pass(geom_shader, options, &shader_info->info);
 
diff --git a/src/gallium/drivers/radeonsi/si_pipe.c b/src/gallium/drivers/radeonsi/si_pipe.c
index ded8cb5eb08..5da8a4b9873 100644
--- a/src/gallium/drivers/radeonsi/si_pipe.c
+++ b/src/gallium/drivers/radeonsi/si_pipe.c
@@ -112,27 +112,28 @@ static const struct debug_named_value debug_options[] = {
 
 static void si_init_compiler(struct si_screen *sscreen,
 			     struct si_compiler *compiler)
 {
 	enum ac_target_machine_options tm_options =
 		(sscreen->debug_flags & DBG(SI_SCHED) ? AC_TM_SISCHED : 0) |
 		(sscreen->info.chip_class >= GFX9 ? AC_TM_FORCE_ENABLE_XNACK : 0) |
 		(sscreen->info.chip_class < GFX9 ? AC_TM_FORCE_DISABLE_XNACK : 0) |
 		(!sscreen->llvm_has_working_vgpr_indexing ? AC_TM_PROMOTE_ALLOCA_TO_SCRATCH : 0);
 
+	const char *triple;
 	compiler->tm = ac_create_target_machine(sscreen->info.family,
-						tm_options, &compiler->triple);
+						tm_options, &triple);
 	if (!compiler->tm)
 		return;
 
 	compiler->target_library_info =
-		gallivm_create_target_library_info(compiler->triple);
+		gallivm_create_target_library_info(triple);
 	if (!compiler->target_library_info)
 		return;
 
 	compiler->passmgr = LLVMCreatePassManager();
 	if (!compiler->passmgr)
 		return;
 
 	LLVMAddTargetLibraryInfo(compiler->target_library_info,
 				 compiler->passmgr);
 
@@ -143,33 +144,24 @@ static void si_init_compiler(struct si_screen *sscreen,
 	LLVMAddAlwaysInlinerPass(compiler->passmgr);
 	/* This pass should eliminate all the load and store instructions. */
 	LLVMAddPromoteMemoryToRegisterPass(compiler->passmgr);
 	LLVMAddScalarReplAggregatesPass(compiler->passmgr);
 	LLVMAddLICMPass(compiler->passmgr);
 	LLVMAddAggressiveDCEPass(compiler->passmgr);
 	LLVMAddCFGSimplificationPass(compiler->passmgr);
 	/* This is recommended by the instruction combining pass. */
 	LLVMAddEarlyCSEMemSSAPass(compiler->passmgr);
 	LLVMAddInstructionCombiningPass(compiler->passmgr);
-
-	/* Get the data layout. */
-	LLVMTargetDataRef data_layout = LLVMCreateTargetDataLayout(compiler->tm);
-	if (!data_layout)
-		return;
-	compiler->data_layout = LLVMCopyStringRepOfTargetData(data_layout);
-	LLVMDisposeTargetData(data_layout);
 }
 
 static void si_destroy_compiler(struct si_compiler *compiler)
 {
-	if (compiler->data_layout)
-		LLVMDisposeMessage((char*)compiler->data_layout);
 	if (compiler->passmgr)
 		LLVMDisposePassManager(compiler->passmgr);
 #if HAVE_LLVM >= 0x0700
 	/* This crashes on LLVM 5.0 and 6.0 and Ubuntu 18.04, so leak it there. */
 	if (compiler->target_library_info)
 		gallivm_dispose_target_library_info(compiler->target_library_info);
 #endif
 	if (compiler->tm)
 		LLVMDisposeTargetMachine(compiler->tm);
 }
diff --git a/src/gallium/drivers/radeonsi/si_shader.h b/src/gallium/drivers/radeonsi/si_shader.h
index 0dc27f29afd..6b71b399437 100644
--- a/src/gallium/drivers/radeonsi/si_shader.h
+++ b/src/gallium/drivers/radeonsi/si_shader.h
@@ -313,22 +313,20 @@ enum {
 	SI_FIX_FETCH_RGB_8_INT,	/* A = 1 */
 	SI_FIX_FETCH_RGB_16,
 	SI_FIX_FETCH_RGB_16_INT,
 };
 
 struct si_shader;
 
 /* Per-thread persistent LLVM objects. */
 struct si_compiler {
 	LLVMTargetMachineRef		tm;
-	const char			*triple;
-	const char			*data_layout;
 	LLVMTargetLibraryInfoRef	target_library_info;
 	LLVMPassManagerRef		passmgr;
 };
 
 /* State of the context creating the shader object. */
 struct si_compiler_ctx_state {
 	/* Should only be used by si_init_shader_selector_async and
 	 * si_build_shader_variant if thread_index == -1 (non-threaded). */
 	struct si_compiler		*compiler;
 
diff --git a/src/gallium/drivers/radeonsi/si_shader_tgsi_setup.c b/src/gallium/drivers/radeonsi/si_shader_tgsi_setup.c
index de3a6cbf0d7..2581d14e760 100644
--- a/src/gallium/drivers/radeonsi/si_shader_tgsi_setup.c
+++ b/src/gallium/drivers/radeonsi/si_shader_tgsi_setup.c
@@ -979,24 +979,21 @@ void si_llvm_context_init(struct si_shader_context *ctx,
 	/* Initialize the gallivm object:
 	 * We are only using the module, context, and builder fields of this struct.
 	 * This should be enough for us to be able to pass our gallivm struct to the
 	 * helper functions in the gallivm module.
 	 */
 	memset(ctx, 0, sizeof(*ctx));
 	ctx->screen = sscreen;
 	ctx->compiler = compiler;
 
 	ctx->gallivm.context = LLVMContextCreate();
-	ctx->gallivm.module = LLVMModuleCreateWithNameInContext("tgsi",
-						ctx->gallivm.context);
-	LLVMSetTarget(ctx->gallivm.module, compiler->triple);
-	LLVMSetDataLayout(ctx->gallivm.module, compiler->data_layout);
+	ctx->gallivm.module = ac_create_module(compiler->tm, ctx->gallivm.context);
 
 	bool unsafe_fpmath = (sscreen->debug_flags & DBG(UNSAFE_MATH)) != 0;
 	enum ac_float_mode float_mode =
 		unsafe_fpmath ? AC_FLOAT_MODE_UNSAFE_FP_MATH :
 				AC_FLOAT_MODE_NO_SIGNED_ZEROS_FP_MATH;
 
 	ctx->gallivm.builder = ac_create_builder(ctx->gallivm.context,
 						 float_mode);
 
 	ac_llvm_context_init(&ctx->ac, ctx->gallivm.context,
-- 
2.17.1



More information about the mesa-dev mailing list