[Mesa-dev] [PATCH 1/7] gallium/radeon: cleanup and fix branch emits

Nicolai Hähnle nhaehnle at gmail.com
Thu Sep 29 13:15:51 UTC 2016


From: Nicolai Hähnle <nicolai.haehnle at amd.com>

Some of the existing code is needlessly complicated. The basic principle
should be: control-flow opcodes emit branches to properly terminate the
current block, _unless_ the current block already has a terminator (which
happens if and only if there was a BRK or CONT).

This also fixes a bug where multiple terminators were created in a block.

Bugzilla: https://bugs.freedesktop.org/show_bug.cgi?id=97887
Cc: mesa-stable at lists.freedesktop.org
---
 .../drivers/radeon/radeon_setup_tgsi_llvm.c        | 51 ++++++----------------
 1 file changed, 14 insertions(+), 37 deletions(-)

diff --git a/src/gallium/drivers/radeon/radeon_setup_tgsi_llvm.c b/src/gallium/drivers/radeon/radeon_setup_tgsi_llvm.c
index bcb3143..201bed8 100644
--- a/src/gallium/drivers/radeon/radeon_setup_tgsi_llvm.c
+++ b/src/gallium/drivers/radeon/radeon_setup_tgsi_llvm.c
@@ -789,20 +789,30 @@ void radeon_llvm_emit_store(struct lp_build_tgsi_context *bld_base,
 				val2 = LLVMBuildExtractElement(builder, ptr,
 								bld_base->uint_bld.one, "");
 
 				LLVMBuildStore(builder, bitcast(bld_base, TGSI_TYPE_FLOAT, value), temp_ptr);
 				LLVMBuildStore(builder, bitcast(bld_base, TGSI_TYPE_FLOAT, val2), temp_ptr2);
 			}
 		}
 	}
 }
 
+/* Emit a branch to the given default target for the current block if
+ * applicable -- that is, if the current block does not already contain a
+ * branch from a break or continue.
+ */
+static void emit_default_branch(LLVMBuilderRef builder, LLVMBasicBlockRef target)
+{
+	if (!LLVMGetBasicBlockTerminator(LLVMGetInsertBlock(builder)))
+		 LLVMBuildBr(builder, target);
+}
+
 static void bgnloop_emit(const struct lp_build_tgsi_action *action,
 			 struct lp_build_tgsi_context *bld_base,
 			 struct lp_build_emit_data *emit_data)
 {
 	struct radeon_llvm_context *ctx = radeon_llvm_context(bld_base);
 	struct gallivm_state *gallivm = bld_base->base.gallivm;
 	LLVMBasicBlockRef loop_block;
 	LLVMBasicBlockRef endloop_block;
 	endloop_block = LLVMAppendBasicBlockInContext(gallivm->context,
 						ctx->main_fn, "ENDLOOP");
@@ -849,88 +859,55 @@ static void cont_emit(const struct lp_build_tgsi_action *action,
 	LLVMBuildBr(gallivm->builder, current_loop->loop_block);
 }
 
 static void else_emit(const struct lp_build_tgsi_action *action,
 		      struct lp_build_tgsi_context *bld_base,
 		      struct lp_build_emit_data *emit_data)
 {
 	struct radeon_llvm_context *ctx = radeon_llvm_context(bld_base);
 	struct gallivm_state *gallivm = bld_base->base.gallivm;
 	struct radeon_llvm_branch *current_branch = get_current_branch(ctx);
-	LLVMBasicBlockRef current_block = LLVMGetInsertBlock(gallivm->builder);
-
-	/* We need to add a terminator to the current block if the previous
-	 * instruction was an ENDIF.Example:
-	 * IF
-	 *   [code]
-	 *   IF
-	 *     [code]
-	 *   ELSE
-	 *    [code]
-	 *   ENDIF <--
-	 * ELSE<--
-	 *   [code]
-	 * ENDIF
-	 */
 
-	if (current_block != current_branch->if_block) {
-		LLVMBuildBr(gallivm->builder, current_branch->endif_block);
-	}
-	if (!LLVMGetBasicBlockTerminator(current_branch->if_block)) {
-		LLVMBuildBr(gallivm->builder, current_branch->endif_block);
-	}
+	emit_default_branch(gallivm->builder, current_branch->endif_block);
 	current_branch->has_else = 1;
 	LLVMPositionBuilderAtEnd(gallivm->builder, current_branch->else_block);
 }
 
 static void endif_emit(const struct lp_build_tgsi_action *action,
 		       struct lp_build_tgsi_context *bld_base,
 		       struct lp_build_emit_data *emit_data)
 {
 	struct radeon_llvm_context *ctx = radeon_llvm_context(bld_base);
 	struct gallivm_state *gallivm = bld_base->base.gallivm;
 	struct radeon_llvm_branch *current_branch = get_current_branch(ctx);
-	LLVMBasicBlockRef current_block = LLVMGetInsertBlock(gallivm->builder);
 
-	/* If we have consecutive ENDIF instructions, then the first ENDIF
-	 * will not have a terminator, so we need to add one. */
-	if (current_block != current_branch->if_block
-			&& current_block != current_branch->else_block
-			&& !LLVMGetBasicBlockTerminator(current_block)) {
+	emit_default_branch(gallivm->builder, current_branch->endif_block);
 
-		 LLVMBuildBr(gallivm->builder, current_branch->endif_block);
-	}
+	/* Need to fixup an empty else block if there was no ELSE opcode. */
 	if (!LLVMGetBasicBlockTerminator(current_branch->else_block)) {
 		LLVMPositionBuilderAtEnd(gallivm->builder, current_branch->else_block);
 		LLVMBuildBr(gallivm->builder, current_branch->endif_block);
 	}
 
-	if (!LLVMGetBasicBlockTerminator(current_branch->if_block)) {
-		LLVMPositionBuilderAtEnd(gallivm->builder, current_branch->if_block);
-		LLVMBuildBr(gallivm->builder, current_branch->endif_block);
-	}
-
 	LLVMPositionBuilderAtEnd(gallivm->builder, current_branch->endif_block);
 	ctx->branch_depth--;
 }
 
 static void endloop_emit(const struct lp_build_tgsi_action *action,
 			 struct lp_build_tgsi_context *bld_base,
 			 struct lp_build_emit_data *emit_data)
 {
 	struct radeon_llvm_context *ctx = radeon_llvm_context(bld_base);
 	struct gallivm_state *gallivm = bld_base->base.gallivm;
 	struct radeon_llvm_loop *current_loop = get_current_loop(ctx);
 
-	if (!LLVMGetBasicBlockTerminator(LLVMGetInsertBlock(gallivm->builder))) {
-		 LLVMBuildBr(gallivm->builder, current_loop->loop_block);
-	}
+	emit_default_branch(gallivm->builder, current_loop->loop_block);
 
 	LLVMPositionBuilderAtEnd(gallivm->builder, current_loop->endloop_block);
 	ctx->loop_depth--;
 }
 
 static void if_cond_emit(const struct lp_build_tgsi_action *action,
 			 struct lp_build_tgsi_context *bld_base,
 			 struct lp_build_emit_data *emit_data,
 			 LLVMValueRef cond)
 {
-- 
2.7.4



More information about the mesa-dev mailing list