[Mesa-dev] [PATCH 18/19] radeonsi: optimization barriers to work around LLVM deficiencies

Nicolai Hähnle nhaehnle at gmail.com
Fri Mar 31 17:14:18 UTC 2017


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

Notably, llvm.amdgcn.readfirstlane and llvm.amdgcn.icmp may be hoisted
out of loops or if/else branches in cases like

  if (cond) {
    v = readFirstInvocationARB(x);
    ... use v ...
  } else {
    v = readFirstInvocationARB(x);
    ... use v ...
  }
===>
  v = readFirstInvocationARB(x);
  if (cond) {
    ... use v ...
  } else {
    ... use v ...
  }

The optimization barrier is a heavy hammer to stop that until LLVM
is taught the semantics of the intrinsic properly.
---
 src/gallium/drivers/radeonsi/si_shader.c | 16 ++++++++++++----
 1 file changed, 12 insertions(+), 4 deletions(-)

diff --git a/src/gallium/drivers/radeonsi/si_shader.c b/src/gallium/drivers/radeonsi/si_shader.c
index a56e886..a24db0d 100644
--- a/src/gallium/drivers/radeonsi/si_shader.c
+++ b/src/gallium/drivers/radeonsi/si_shader.c
@@ -3146,21 +3146,20 @@ static void build_tex_intrinsic(const struct lp_build_tgsi_action *action,
 				struct lp_build_tgsi_context *bld_base,
 				struct lp_build_emit_data *emit_data);
 
 /* Prevent optimizations (at least of memory accesses) across the current
  * point in the program by emitting empty inline assembly that is marked as
  * having side effects.
  *
  * Optionally, a value can be passed through the inline assembly to prevent
  * LLVM from hoisting calls to ReadNone functions.
  */
-#if 0 /* unused currently */
 static void emit_optimization_barrier(struct si_shader_context *ctx,
 				      LLVMValueRef *pvgpr)
 {
 	static int counter = 0;
 
 	LLVMBuilderRef builder = ctx->gallivm.builder;
 	char code[16];
 
 	snprintf(code, sizeof(code), "; %d", p_atomic_inc_return(&counter));
 
@@ -3180,21 +3179,20 @@ static void emit_optimization_barrier(struct si_shader_context *ctx,
 
 		vgpr = LLVMBuildBitCast(builder, vgpr, LLVMVectorType(ctx->i32, vgpr_size / 4), "");
 		vgpr0 = LLVMBuildExtractElement(builder, vgpr, ctx->i32_0, "");
 		vgpr0 = LLVMBuildCall(builder, inlineasm, &vgpr0, 1, "");
 		vgpr = LLVMBuildInsertElement(builder, vgpr, vgpr0, ctx->i32_0, "");
 		vgpr = LLVMBuildBitCast(builder, vgpr, vgpr_type, "");
 
 		*pvgpr = vgpr;
 	}
 }
-#endif
 
 /* Combine these with & instead of |. */
 #define NOOP_WAITCNT 0xf7f
 #define LGKM_CNT 0x07f
 #define VM_CNT 0xf70
 
 static void emit_waitcnt(struct si_shader_context *ctx, unsigned simm16)
 {
 	struct gallivm_state *gallivm = &ctx->gallivm;
 	LLVMBuilderRef builder = gallivm->builder;
@@ -5201,22 +5199,27 @@ static void build_interp_intrinsic(const struct lp_build_tgsi_action *action,
 static LLVMValueRef si_emit_ballot(struct si_shader_context *ctx,
 				   LLVMValueRef value)
 {
 	struct gallivm_state *gallivm = &ctx->gallivm;
 	LLVMValueRef args[3] = {
 		value,
 		ctx->i32_0,
 		LLVMConstInt(ctx->i32, LLVMIntNE, 0)
 	};
 
-	if (LLVMTypeOf(value) != ctx->i32)
-		args[0] = LLVMBuildBitCast(gallivm->builder, value, ctx->i32, "");
+	/* We currently have no other way to prevent LLVM from lifting the icmp
+	 * calls to a dominating basic block.
+	 */
+	emit_optimization_barrier(ctx, &args[0]);
+
+	if (LLVMTypeOf(args[0]) != ctx->i32)
+		args[0] = LLVMBuildBitCast(gallivm->builder, args[0], ctx->i32, "");
 
 	return lp_build_intrinsic(gallivm->builder,
 				  "llvm.amdgcn.icmp.i32",
 				  ctx->i64, args, 3,
 				  LP_FUNC_ATTR_NOUNWIND |
 				  LP_FUNC_ATTR_READNONE |
 				  LP_FUNC_ATTR_CONVERGENT);
 }
 
 static void vote_all_emit(
@@ -5307,20 +5310,25 @@ static void read_invoc_fetch_args(
 }
 
 static void read_lane_emit(
 	const struct lp_build_tgsi_action *action,
 	struct lp_build_tgsi_context *bld_base,
 	struct lp_build_emit_data *emit_data)
 {
 	struct si_shader_context *ctx = si_shader_context(bld_base);
 	LLVMBuilderRef builder = ctx->gallivm.builder;
 
+	/* We currently have no other way to prevent LLVM from lifting the icmp
+	 * calls to a dominating basic block.
+	 */
+	emit_optimization_barrier(ctx, &emit_data->args[0]);
+
 	for (unsigned i = 0; i < emit_data->arg_count; ++i) {
 		emit_data->args[i] = LLVMBuildBitCast(builder, emit_data->args[i],
 						      ctx->i32, "");
 	}
 
 	emit_data->output[emit_data->chan] =
 		ac_build_intrinsic(&ctx->ac, action->intr_name,
 				   ctx->i32, emit_data->args, emit_data->arg_count,
 				   AC_FUNC_ATTR_READNONE |
 				   AC_FUNC_ATTR_CONVERGENT);
-- 
2.9.3



More information about the mesa-dev mailing list