ac: import lp_create_builder() from gallivm
[mesa.git] / src / gallium / drivers / radeonsi / si_shader_tgsi_setup.c
index 2ecb1128dbc44384f5ade379086ddc37e8abe49e..fc141ca1e03cc68be298f4a02d4d450f87190a97 100644 (file)
@@ -165,20 +165,20 @@ out:
 LLVMTypeRef tgsi2llvmtype(struct lp_build_tgsi_context *bld_base,
                          enum tgsi_opcode_type type)
 {
-       LLVMContextRef ctx = bld_base->base.gallivm->context;
+       struct si_shader_context *ctx = si_shader_context(bld_base);
 
        switch (type) {
        case TGSI_TYPE_UNSIGNED:
        case TGSI_TYPE_SIGNED:
-               return LLVMInt32TypeInContext(ctx);
+               return ctx->ac.i32;
        case TGSI_TYPE_UNSIGNED64:
        case TGSI_TYPE_SIGNED64:
-               return LLVMInt64TypeInContext(ctx);
+               return ctx->ac.i64;
        case TGSI_TYPE_DOUBLE:
-               return LLVMDoubleTypeInContext(ctx);
+               return ctx->ac.f64;
        case TGSI_TYPE_UNTYPED:
        case TGSI_TYPE_FLOAT:
-               return LLVMFloatTypeInContext(ctx);
+               return ctx->ac.f32;
        default: break;
        }
        return 0;
@@ -272,13 +272,11 @@ static LLVMValueRef emit_swizzle(struct lp_build_tgsi_context *bld_base,
 {
        struct si_shader_context *ctx = si_shader_context(bld_base);
        LLVMValueRef swizzles[4];
-       LLVMTypeRef i32t =
-               LLVMInt32TypeInContext(bld_base->base.gallivm->context);
 
-       swizzles[0] = LLVMConstInt(i32t, swizzle_x, 0);
-       swizzles[1] = LLVMConstInt(i32t, swizzle_y, 0);
-       swizzles[2] = LLVMConstInt(i32t, swizzle_z, 0);
-       swizzles[3] = LLVMConstInt(i32t, swizzle_w, 0);
+       swizzles[0] = LLVMConstInt(ctx->i32, swizzle_x, 0);
+       swizzles[1] = LLVMConstInt(ctx->i32, swizzle_y, 0);
+       swizzles[2] = LLVMConstInt(ctx->i32, swizzle_z, 0);
+       swizzles[3] = LLVMConstInt(ctx->i32, swizzle_w, 0);
 
        return LLVMBuildShuffleVector(ctx->ac.builder,
                                      value,
@@ -401,14 +399,14 @@ get_pointer_into_array(struct si_shader_context *ctx,
 
 LLVMValueRef
 si_llvm_emit_fetch_64bit(struct lp_build_tgsi_context *bld_base,
-                        enum tgsi_opcode_type type,
+                        LLVMTypeRef type,
                         LLVMValueRef ptr,
                         LLVMValueRef ptr2)
 {
        struct si_shader_context *ctx = si_shader_context(bld_base);
        LLVMValueRef result;
 
-       result = LLVMGetUndef(LLVMVectorType(LLVMIntTypeInContext(bld_base->base.gallivm->context, 32), bld_base->base.type.length * 2));
+       result = LLVMGetUndef(LLVMVectorType(ctx->i32, 2));
 
        result = LLVMBuildInsertElement(ctx->ac.builder,
                                        result,
@@ -418,7 +416,7 @@ si_llvm_emit_fetch_64bit(struct lp_build_tgsi_context *bld_base,
                                        result,
                                        ac_to_integer(&ctx->ac, ptr2),
                                        ctx->i32_1, "");
-       return bitcast(bld_base, type, result);
+       return LLVMBuildBitCast(ctx->ac.builder, result, type, "");
 }
 
 static LLVMValueRef
@@ -463,7 +461,8 @@ load_value_from_array(struct lp_build_tgsi_context *bld_base,
                        LLVMValueRef ptr_hi, val_hi;
                        ptr_hi = LLVMBuildGEP(builder, ptr, &ctx->i32_1, 1, "");
                        val_hi = LLVMBuildLoad(builder, ptr_hi, "");
-                       val = si_llvm_emit_fetch_64bit(bld_base, type, val, val_hi);
+                       val = si_llvm_emit_fetch_64bit(bld_base, tgsi2llvmtype(bld_base, type),
+                                                      val, val_hi);
                }
 
                return val;
@@ -578,7 +577,7 @@ LLVMValueRef si_llvm_emit_fetch(struct lp_build_tgsi_context *bld_base,
        case TGSI_FILE_IMMEDIATE: {
                LLVMTypeRef ctype = tgsi2llvmtype(bld_base, type);
                if (tgsi_type_is_64bit(type)) {
-                       result = LLVMGetUndef(LLVMVectorType(ctx->i32, bld_base->base.type.length * 2));
+                       result = LLVMGetUndef(LLVMVectorType(ctx->i32, 2));
                        result = LLVMConstInsertElement(result,
                                                        ctx->imms[reg->Register.Index * TGSI_NUM_CHANNELS + swizzle],
                                                        ctx->i32_0);
@@ -611,7 +610,8 @@ LLVMValueRef si_llvm_emit_fetch(struct lp_build_tgsi_context *bld_base,
                if (tgsi_type_is_64bit(type)) {
                        ptr = result;
                        ptr2 = input[swizzle + 1];
-                       return si_llvm_emit_fetch_64bit(bld_base, type, ptr, ptr2);
+                       return si_llvm_emit_fetch_64bit(bld_base, tgsi2llvmtype(bld_base, type),
+                                                       ptr, ptr2);
                }
                break;
        }
@@ -622,7 +622,7 @@ LLVMValueRef si_llvm_emit_fetch(struct lp_build_tgsi_context *bld_base,
                ptr = ctx->temps[reg->Register.Index * TGSI_NUM_CHANNELS + swizzle];
                if (tgsi_type_is_64bit(type)) {
                        ptr2 = ctx->temps[reg->Register.Index * TGSI_NUM_CHANNELS + swizzle + 1];
-                       return si_llvm_emit_fetch_64bit(bld_base, type,
+                       return si_llvm_emit_fetch_64bit(bld_base, tgsi2llvmtype(bld_base, type),
                                                        LLVMBuildLoad(builder, ptr, ""),
                                                        LLVMBuildLoad(builder, ptr2, ""));
                }
@@ -633,7 +633,7 @@ LLVMValueRef si_llvm_emit_fetch(struct lp_build_tgsi_context *bld_base,
                ptr = get_output_ptr(bld_base, reg->Register.Index, swizzle);
                if (tgsi_type_is_64bit(type)) {
                        ptr2 = get_output_ptr(bld_base, reg->Register.Index, swizzle + 1);
-                       return si_llvm_emit_fetch_64bit(bld_base, type,
+                       return si_llvm_emit_fetch_64bit(bld_base, tgsi2llvmtype(bld_base, type),
                                                        LLVMBuildLoad(builder, ptr, ""),
                                                        LLVMBuildLoad(builder, ptr2, ""));
                }
@@ -666,7 +666,8 @@ static LLVMValueRef fetch_system_value(struct lp_build_tgsi_context *bld_base,
                hi = LLVMBuildExtractElement(
                        builder, cval, LLVMConstInt(ctx->i32, swizzle + 1, 0), "");
 
-               return si_llvm_emit_fetch_64bit(bld_base, type, lo, hi);
+               return si_llvm_emit_fetch_64bit(bld_base, tgsi2llvmtype(bld_base, type),
+                                               lo, hi);
        }
 
        if (LLVMGetTypeKind(LLVMTypeOf(cval)) == LLVMVectorTypeKind) {
@@ -969,18 +970,16 @@ static void set_basicblock_name(LLVMBasicBlockRef bb, const char *base, int pc)
 static LLVMBasicBlockRef append_basic_block(struct si_shader_context *ctx,
                                            const char *name)
 {
-       struct gallivm_state *gallivm = &ctx->gallivm;
-
        assert(ctx->flow_depth >= 1);
 
        if (ctx->flow_depth >= 2) {
                struct si_llvm_flow *flow = &ctx->flow[ctx->flow_depth - 2];
 
-               return LLVMInsertBasicBlockInContext(gallivm->context,
+               return LLVMInsertBasicBlockInContext(ctx->ac.context,
                                                     flow->next_block, name);
        }
 
-       return LLVMAppendBasicBlockInContext(gallivm->context, ctx->main_fn, name);
+       return LLVMAppendBasicBlockInContext(ctx->ac.context, ctx->main_fn, name);
 }
 
 /* Emit a branch to the given default target for the current block if
@@ -1102,7 +1101,7 @@ static void if_emit(const struct lp_build_tgsi_action *action,
 
        cond = LLVMBuildFCmp(ctx->ac.builder, LLVMRealUNE,
                        emit_data->args[0],
-                       bld_base->base.zero, "");
+                       ctx->ac.f32_0, "");
 
        if_cond_emit(action, bld_base, emit_data, cond);
 }
@@ -1160,15 +1159,16 @@ void si_llvm_context_init(struct si_shader_context *ctx,
        LLVMDisposeTargetData(data_layout);
        LLVMDisposeMessage(data_layout_str);
 
-       bool unsafe_fpmath = (sscreen->b.debug_flags & DBG_UNSAFE_MATH) != 0;
-       enum lp_float_mode float_mode =
-               unsafe_fpmath ? LP_FLOAT_MODE_UNSAFE_FP_MATH :
-                               LP_FLOAT_MODE_NO_SIGNED_ZEROS_FP_MATH;
+       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 = lp_create_builder(ctx->gallivm.context,
+       ctx->gallivm.builder = ac_create_builder(ctx->gallivm.context,
                                                 float_mode);
 
-       ac_llvm_context_init(&ctx->ac, ctx->gallivm.context, sscreen->b.chip_class);
+       ac_llvm_context_init(&ctx->ac, ctx->gallivm.context,
+                            sscreen->info.chip_class, sscreen->info.family);
        ctx->ac.module = ctx->gallivm.module;
        ctx->ac.builder = ctx->gallivm.builder;
 
@@ -1195,10 +1195,10 @@ void si_llvm_context_init(struct si_shader_context *ctx,
        bld_base->emit_immediate = emit_immediate;
 
        /* metadata allowing 2.5 ULP */
-       ctx->fpmath_md_kind = LLVMGetMDKindIDInContext(ctx->gallivm.context,
+       ctx->fpmath_md_kind = LLVMGetMDKindIDInContext(ctx->ac.context,
                                                       "fpmath", 6);
-       LLVMValueRef arg = lp_build_const_float(&ctx->gallivm, 2.5);
-       ctx->fpmath_md_2p5_ulp = LLVMMDNodeInContext(ctx->gallivm.context,
+       LLVMValueRef arg = LLVMConstReal(ctx->ac.f32, 2.5);
+       ctx->fpmath_md_2p5_ulp = LLVMMDNodeInContext(ctx->ac.context,
                                                     &arg, 1);
 
        bld_base->op_actions[TGSI_OPCODE_BGNLOOP].emit = bgnloop_emit;
@@ -1213,13 +1213,13 @@ void si_llvm_context_init(struct si_shader_context *ctx,
        si_shader_context_init_alu(&ctx->bld_base);
        si_shader_context_init_mem(ctx);
 
-       ctx->voidt = LLVMVoidTypeInContext(ctx->gallivm.context);
-       ctx->i1 = LLVMInt1TypeInContext(ctx->gallivm.context);
-       ctx->i8 = LLVMInt8TypeInContext(ctx->gallivm.context);
-       ctx->i32 = LLVMInt32TypeInContext(ctx->gallivm.context);
-       ctx->i64 = LLVMInt64TypeInContext(ctx->gallivm.context);
-       ctx->i128 = LLVMIntTypeInContext(ctx->gallivm.context, 128);
-       ctx->f32 = LLVMFloatTypeInContext(ctx->gallivm.context);
+       ctx->voidt = LLVMVoidTypeInContext(ctx->ac.context);
+       ctx->i1 = LLVMInt1TypeInContext(ctx->ac.context);
+       ctx->i8 = LLVMInt8TypeInContext(ctx->ac.context);
+       ctx->i32 = LLVMInt32TypeInContext(ctx->ac.context);
+       ctx->i64 = LLVMInt64TypeInContext(ctx->ac.context);
+       ctx->i128 = LLVMIntTypeInContext(ctx->ac.context, 128);
+       ctx->f32 = LLVMFloatTypeInContext(ctx->ac.context);
        ctx->v2i32 = LLVMVectorType(ctx->i32, 2);
        ctx->v4i32 = LLVMVectorType(ctx->i32, 4);
        ctx->v4f32 = LLVMVectorType(ctx->f32, 4);
@@ -1306,24 +1306,24 @@ void si_llvm_create_func(struct si_shader_context *ctx,
        unsigned real_shader_type;
 
        if (num_return_elems)
-               ret_type = LLVMStructTypeInContext(ctx->gallivm.context,
+               ret_type = LLVMStructTypeInContext(ctx->ac.context,
                                                   return_types,
                                                   num_return_elems, true);
        else
-               ret_type = LLVMVoidTypeInContext(ctx->gallivm.context);
+               ret_type = ctx->voidt;
 
        /* Setup the function */
        ctx->return_type = ret_type;
        main_fn_type = LLVMFunctionType(ret_type, ParamTypes, ParamCount, 0);
        ctx->main_fn = LLVMAddFunction(ctx->gallivm.module, name, main_fn_type);
-       main_fn_body = LLVMAppendBasicBlockInContext(ctx->gallivm.context,
+       main_fn_body = LLVMAppendBasicBlockInContext(ctx->ac.context,
                        ctx->main_fn, "main_body");
        LLVMPositionBuilderAtEnd(ctx->ac.builder, main_fn_body);
 
        real_shader_type = ctx->type;
 
        /* LS is merged into HS (TCS), and ES is merged into GS. */
-       if (ctx->screen->b.chip_class >= GFX9) {
+       if (ctx->screen->info.chip_class >= GFX9) {
                if (ctx->shader->key.as_ls)
                        real_shader_type = PIPE_SHADER_TESS_CTRL;
                else if (ctx->shader->key.as_es)
@@ -1362,8 +1362,8 @@ void si_llvm_optimize_module(struct si_shader_context *ctx)
        LLVMTargetLibraryInfoRef target_library_info;
 
        /* Dump LLVM IR before any optimization passes */
-       if (ctx->screen->b.debug_flags & DBG_PREOPT_IR &&
-           si_can_dump_shader(&ctx->screen->b, ctx->type))
+       if (ctx->screen->debug_flags & DBG(PREOPT_IR) &&
+           si_can_dump_shader(ctx->screen, ctx->type))
                LLVMDumpModule(ctx->gallivm.module);
 
        /* Create the pass manager */
@@ -1372,7 +1372,7 @@ void si_llvm_optimize_module(struct si_shader_context *ctx)
        target_library_info = gallivm_create_target_library_info(triple);
        LLVMAddTargetLibraryInfo(target_library_info, gallivm->passmgr);
 
-       if (si_extra_shader_checks(&ctx->screen->b, ctx->type))
+       if (si_extra_shader_checks(ctx->screen, ctx->type))
                LLVMAddVerifierPass(gallivm->passmgr);
 
        LLVMAddAlwaysInlinerPass(gallivm->passmgr);