gallium/radeon: add radeon_llvm_bound_index for bounds checking
authorNicolai Hähnle <nicolai.haehnle@amd.com>
Mon, 8 Aug 2016 21:52:06 +0000 (23:52 +0200)
committerNicolai Hähnle <nicolai.haehnle@amd.com>
Wed, 17 Aug 2016 10:11:24 +0000 (12:11 +0200)
Reviewed-by: Marek Olšák <marek.olsak@amd.com>
src/gallium/drivers/radeon/radeon_llvm.h
src/gallium/drivers/radeon/radeon_setup_tgsi_llvm.c
src/gallium/drivers/radeonsi/si_shader.c

index 0276ef3e02a62500f2c5f32985d8d4180f8fd1e1..da5b7f58e154e28224b26bb55617734d30828455 100644 (file)
@@ -116,6 +116,10 @@ LLVMTypeRef tgsi2llvmtype(struct lp_build_tgsi_context *bld_base,
 LLVMValueRef bitcast(struct lp_build_tgsi_context *bld_base,
                     enum tgsi_opcode_type type, LLVMValueRef value);
 
+LLVMValueRef radeon_llvm_bound_index(struct radeon_llvm_context *ctx,
+                                    LLVMValueRef index,
+                                    unsigned num);
+
 void radeon_llvm_emit_prepare_cube_coords(struct lp_build_tgsi_context *bld_base,
                                          struct lp_build_emit_data *emit_data,
                                          LLVMValueRef *coords_arg,
index dd7d60b136c82a986be8b8dba5247a7e344393b5..7cdf2287d473b2b9e2e731f7fd4ab2a855431b1b 100644 (file)
@@ -73,6 +73,35 @@ LLVMValueRef bitcast(struct lp_build_tgsi_context *bld_base,
                return value;
 }
 
+/**
+ * Return a value that is equal to the given i32 \p index if it lies in [0,num)
+ * or an undefined value in the same interval otherwise.
+ */
+LLVMValueRef radeon_llvm_bound_index(struct radeon_llvm_context *ctx,
+                                    LLVMValueRef index,
+                                    unsigned num)
+{
+       struct gallivm_state *gallivm = &ctx->gallivm;
+       LLVMBuilderRef builder = gallivm->builder;
+       LLVMValueRef c_max = lp_build_const_int32(gallivm, num - 1);
+       LLVMValueRef cc;
+
+       if (util_is_power_of_two(num)) {
+               index = LLVMBuildAnd(builder, index, c_max, "");
+       } else {
+               /* In theory, this MAX pattern should result in code that is
+                * as good as the bit-wise AND above.
+                *
+                * In practice, LLVM generates worse code (at the time of
+                * writing), because its value tracking is not strong enough.
+                */
+               cc = LLVMBuildICmp(builder, LLVMIntULE, index, c_max, "");
+               index = LLVMBuildSelect(builder, cc, index, c_max, "");
+       }
+
+       return index;
+}
+
 static struct radeon_llvm_loop *get_current_loop(struct radeon_llvm_context *ctx)
 {
        return ctx->loop_depth > 0 ? ctx->loop + (ctx->loop_depth - 1) : NULL;
index 06b5c9c2c4876258933378a6ea18123259d73d63..a5b566e548900fc22ffbded0ed98ebbf2fc1e376 100644 (file)
@@ -565,11 +565,7 @@ static LLVMValueRef get_bounded_indirect_index(struct si_shader_context *ctx,
                                               const struct tgsi_ind_register *ind,
                                               int rel_index, unsigned num)
 {
-       struct gallivm_state *gallivm = &ctx->radeon_bld.gallivm;
-       LLVMBuilderRef builder = gallivm->builder;
        LLVMValueRef result = get_indirect_index(ctx, ind, rel_index);
-       LLVMValueRef c_max = LLVMConstInt(ctx->i32, num - 1, 0);
-       LLVMValueRef cc;
 
        /* LLVM 3.8: If indirect resource indexing is used:
         * - SI & CIK hang
@@ -578,20 +574,7 @@ static LLVMValueRef get_bounded_indirect_index(struct si_shader_context *ctx,
        if (HAVE_LLVM <= 0x0308)
                return LLVMGetUndef(ctx->i32);
 
-       if (util_is_power_of_two(num)) {
-               result = LLVMBuildAnd(builder, result, c_max, "");
-       } else {
-               /* In theory, this MAX pattern should result in code that is
-                * as good as the bit-wise AND above.
-                *
-                * In practice, LLVM generates worse code (at the time of
-                * writing), because its value tracking is not strong enough.
-                */
-               cc = LLVMBuildICmp(builder, LLVMIntULE, result, c_max, "");
-               result = LLVMBuildSelect(builder, cc, result, c_max, "");
-       }
-
-       return result;
+       return radeon_llvm_bound_index(&ctx->radeon_bld, result, num);
 }