radeonsi: make use of if/loop build helpers in ac
authorTimothy Arceri <tarceri@itsqueeze.com>
Tue, 6 Mar 2018 23:55:47 +0000 (10:55 +1100)
committerTimothy Arceri <tarceri@itsqueeze.com>
Wed, 7 Mar 2018 23:12:34 +0000 (10:12 +1100)
Reviewed-by: Marek Olšák <marek.olsak@amd.com>
src/gallium/drivers/radeonsi/si_shader_internal.h
src/gallium/drivers/radeonsi/si_shader_tgsi_setup.c

index 157dd46..8decd06 100644 (file)
@@ -43,13 +43,9 @@ struct ac_shader_binary;
 #define RADEON_LLVM_MAX_INPUTS 32 * 4
 #define RADEON_LLVM_MAX_OUTPUTS 32 * 4
 
-#define RADEON_LLVM_INITIAL_CF_DEPTH 4
-
 #define RADEON_LLVM_MAX_SYSTEM_VALUES 11
 #define RADEON_LLVM_MAX_ADDRS 16
 
-struct si_llvm_flow;
-
 struct si_shader_context {
        struct lp_build_tgsi_context bld_base;
        struct gallivm_state gallivm;
@@ -98,10 +94,6 @@ struct si_shader_context {
        LLVMValueRef *imms;
        unsigned imms_num;
 
-       struct si_llvm_flow *flow;
-       unsigned flow_depth;
-       unsigned flow_depth_max;
-
        struct lp_build_if_state merged_wrap_if_state;
 
        struct tgsi_array_info *temp_arrays;
index f060ad6..c8ac854 100644 (file)
 #include <llvm-c/Transforms/IPO.h>
 #include <llvm-c/Transforms/Scalar.h>
 
-/* Data for if/else/endif and bgnloop/endloop control flow structures.
- */
-struct si_llvm_flow {
-       /* Loop exit or next part of if/else/endif. */
-       LLVMBasicBlockRef next_block;
-       LLVMBasicBlockRef loop_entry_block;
-};
-
 enum si_llvm_calling_convention {
        RADEON_LLVM_AMDGPU_VS = 87,
        RADEON_LLVM_AMDGPU_GS = 88,
@@ -216,45 +208,6 @@ LLVMValueRef si_llvm_bound_index(struct si_shader_context *ctx,
        return index;
 }
 
-static struct si_llvm_flow *
-get_current_flow(struct si_shader_context *ctx)
-{
-       if (ctx->flow_depth > 0)
-               return &ctx->flow[ctx->flow_depth - 1];
-       return NULL;
-}
-
-static struct si_llvm_flow *
-get_innermost_loop(struct si_shader_context *ctx)
-{
-       for (unsigned i = ctx->flow_depth; i > 0; --i) {
-               if (ctx->flow[i - 1].loop_entry_block)
-                       return &ctx->flow[i - 1];
-       }
-       return NULL;
-}
-
-static struct si_llvm_flow *
-push_flow(struct si_shader_context *ctx)
-{
-       struct si_llvm_flow *flow;
-
-       if (ctx->flow_depth >= ctx->flow_depth_max) {
-               unsigned new_max = MAX2(ctx->flow_depth << 1, RADEON_LLVM_INITIAL_CF_DEPTH);
-               ctx->flow = REALLOC(ctx->flow,
-                                   ctx->flow_depth_max * sizeof(*ctx->flow),
-                                   new_max * sizeof(*ctx->flow));
-               ctx->flow_depth_max = new_max;
-       }
-
-       flow = &ctx->flow[ctx->flow_depth];
-       ctx->flow_depth++;
-
-       flow->next_block = NULL;
-       flow->loop_entry_block = NULL;
-       return flow;
-}
-
 static LLVMValueRef emit_swizzle(struct lp_build_tgsi_context *bld_base,
                                 LLVMValueRef value,
                                 unsigned swizzle_x,
@@ -946,42 +899,13 @@ void si_llvm_emit_store(struct lp_build_tgsi_context *bld_base,
        }
 }
 
-static void set_basicblock_name(LLVMBasicBlockRef bb, const char *base, int pc)
+static int get_line(int pc)
 {
-       char buf[32];
        /* Subtract 1 so that the number shown is that of the corresponding
         * opcode in the TGSI dump, e.g. an if block has the same suffix as
         * the instruction number of the corresponding TGSI IF.
         */
-       snprintf(buf, sizeof(buf), "%s%d", base, pc - 1);
-       LLVMSetValueName(LLVMBasicBlockAsValue(bb), buf);
-}
-
-/* Append a basic block at the level of the parent flow.
- */
-static LLVMBasicBlockRef append_basic_block(struct si_shader_context *ctx,
-                                           const char *name)
-{
-       assert(ctx->flow_depth >= 1);
-
-       if (ctx->flow_depth >= 2) {
-               struct si_llvm_flow *flow = &ctx->flow[ctx->flow_depth - 2];
-
-               return LLVMInsertBasicBlockInContext(ctx->ac.context,
-                                                    flow->next_block, name);
-       }
-
-       return LLVMAppendBasicBlockInContext(ctx->ac.context, ctx->main_fn, name);
-}
-
-/* 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);
+       return pc - 1;
 }
 
 static void bgnloop_emit(const struct lp_build_tgsi_action *action,
@@ -989,12 +913,7 @@ static void bgnloop_emit(const struct lp_build_tgsi_action *action,
                         struct lp_build_emit_data *emit_data)
 {
        struct si_shader_context *ctx = si_shader_context(bld_base);
-       struct si_llvm_flow *flow = push_flow(ctx);
-       flow->loop_entry_block = append_basic_block(ctx, "LOOP");
-       flow->next_block = append_basic_block(ctx, "ENDLOOP");
-       set_basicblock_name(flow->loop_entry_block, "loop", bld_base->pc);
-       LLVMBuildBr(ctx->ac.builder, flow->loop_entry_block);
-       LLVMPositionBuilderAtEnd(ctx->ac.builder, flow->loop_entry_block);
+       ac_build_bgnloop(&ctx->ac, get_line(bld_base->pc));
 }
 
 static void brk_emit(const struct lp_build_tgsi_action *action,
@@ -1002,9 +921,7 @@ static void brk_emit(const struct lp_build_tgsi_action *action,
                     struct lp_build_emit_data *emit_data)
 {
        struct si_shader_context *ctx = si_shader_context(bld_base);
-       struct si_llvm_flow *flow = get_innermost_loop(ctx);
-
-       LLVMBuildBr(ctx->ac.builder, flow->next_block);
+       ac_build_break(&ctx->ac);
 }
 
 static void cont_emit(const struct lp_build_tgsi_action *action,
@@ -1012,9 +929,7 @@ static void cont_emit(const struct lp_build_tgsi_action *action,
                      struct lp_build_emit_data *emit_data)
 {
        struct si_shader_context *ctx = si_shader_context(bld_base);
-       struct si_llvm_flow *flow = get_innermost_loop(ctx);
-
-       LLVMBuildBr(ctx->ac.builder, flow->loop_entry_block);
+       ac_build_continue(&ctx->ac);
 }
 
 static void else_emit(const struct lp_build_tgsi_action *action,
@@ -1022,18 +937,7 @@ static void else_emit(const struct lp_build_tgsi_action *action,
                      struct lp_build_emit_data *emit_data)
 {
        struct si_shader_context *ctx = si_shader_context(bld_base);
-       struct si_llvm_flow *current_branch = get_current_flow(ctx);
-       LLVMBasicBlockRef endif_block;
-
-       assert(!current_branch->loop_entry_block);
-
-       endif_block = append_basic_block(ctx, "ENDIF");
-       emit_default_branch(ctx->ac.builder, endif_block);
-
-       LLVMPositionBuilderAtEnd(ctx->ac.builder, current_branch->next_block);
-       set_basicblock_name(current_branch->next_block, "else", bld_base->pc);
-
-       current_branch->next_block = endif_block;
+       ac_build_else(&ctx->ac, get_line(bld_base->pc));
 }
 
 static void endif_emit(const struct lp_build_tgsi_action *action,
@@ -1041,15 +945,7 @@ static void endif_emit(const struct lp_build_tgsi_action *action,
                       struct lp_build_emit_data *emit_data)
 {
        struct si_shader_context *ctx = si_shader_context(bld_base);
-       struct si_llvm_flow *current_branch = get_current_flow(ctx);
-
-       assert(!current_branch->loop_entry_block);
-
-       emit_default_branch(ctx->ac.builder, current_branch->next_block);
-       LLVMPositionBuilderAtEnd(ctx->ac.builder, current_branch->next_block);
-       set_basicblock_name(current_branch->next_block, "endif", bld_base->pc);
-
-       ctx->flow_depth--;
+       ac_build_endif(&ctx->ac, get_line(bld_base->pc));
 }
 
 static void endloop_emit(const struct lp_build_tgsi_action *action,
@@ -1057,31 +953,7 @@ static void endloop_emit(const struct lp_build_tgsi_action *action,
                         struct lp_build_emit_data *emit_data)
 {
        struct si_shader_context *ctx = si_shader_context(bld_base);
-       struct si_llvm_flow *current_loop = get_current_flow(ctx);
-
-       assert(current_loop->loop_entry_block);
-
-       emit_default_branch(ctx->ac.builder, current_loop->loop_entry_block);
-
-       LLVMPositionBuilderAtEnd(ctx->ac.builder, current_loop->next_block);
-       set_basicblock_name(current_loop->next_block, "endloop", bld_base->pc);
-       ctx->flow_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)
-{
-       struct si_shader_context *ctx = si_shader_context(bld_base);
-       struct si_llvm_flow *flow = push_flow(ctx);
-       LLVMBasicBlockRef if_block;
-
-       if_block = append_basic_block(ctx, "IF");
-       flow->next_block = append_basic_block(ctx, "ELSE");
-       set_basicblock_name(if_block, "if", bld_base->pc);
-       LLVMBuildCondBr(ctx->ac.builder, cond, if_block, flow->next_block);
-       LLVMPositionBuilderAtEnd(ctx->ac.builder, if_block);
+       ac_build_endloop(&ctx->ac, get_line(bld_base->pc));
 }
 
 static void if_emit(const struct lp_build_tgsi_action *action,
@@ -1089,13 +961,7 @@ static void if_emit(const struct lp_build_tgsi_action *action,
                    struct lp_build_emit_data *emit_data)
 {
        struct si_shader_context *ctx = si_shader_context(bld_base);
-       LLVMValueRef cond;
-
-       cond = LLVMBuildFCmp(ctx->ac.builder, LLVMRealUNE,
-                       emit_data->args[0],
-                       ctx->ac.f32_0, "");
-
-       if_cond_emit(action, bld_base, emit_data, cond);
+       ac_build_if(&ctx->ac, emit_data->args[0], get_line(bld_base->pc));
 }
 
 static void uif_emit(const struct lp_build_tgsi_action *action,
@@ -1103,12 +969,7 @@ static void uif_emit(const struct lp_build_tgsi_action *action,
                     struct lp_build_emit_data *emit_data)
 {
        struct si_shader_context *ctx = si_shader_context(bld_base);
-       LLVMValueRef cond;
-
-       cond = LLVMBuildICmp(ctx->ac.builder, LLVMIntNE,
-               ac_to_integer(&ctx->ac, emit_data->args[0]), ctx->i32_0, "");
-
-       if_cond_emit(action, bld_base, emit_data, cond);
+       ac_build_uif(&ctx->ac, emit_data->args[0], get_line(bld_base->pc));
 }
 
 static void emit_immediate(struct lp_build_tgsi_context *bld_base,
@@ -1400,7 +1261,5 @@ void si_llvm_dispose(struct si_shader_context *ctx)
        FREE(ctx->imms);
        ctx->imms = NULL;
        ctx->imms_num = 0;
-       FREE(ctx->flow);
-       ctx->flow = NULL;
-       ctx->flow_depth_max = 0;
+       ac_llvm_context_dispose(&ctx->ac);
 }