util: remove LIST_IS_EMPTY macro
[mesa.git] / src / gallium / drivers / radeonsi / si_shader_tgsi_setup.c
index a4c2a1f27aba86e44bdcb9947a12267c310f3bcc..1c6522322ff643c5271216db0f5af6d166d36947 100644 (file)
 
 #include "si_shader_internal.h"
 #include "si_pipe.h"
-
-#include "gallivm/lp_bld_const.h"
-#include "gallivm/lp_bld_gather.h"
-#include "gallivm/lp_bld_flow.h"
-#include "gallivm/lp_bld_init.h"
-#include "gallivm/lp_bld_intr.h"
-#include "gallivm/lp_bld_misc.h"
-#include "gallivm/lp_bld_swizzle.h"
-#include "tgsi/tgsi_info.h"
-#include "tgsi/tgsi_parse.h"
-#include "util/u_math.h"
+#include "ac_llvm_util.h"
 #include "util/u_memory.h"
-#include "util/u_debug.h"
-
-#include <stdio.h>
 
 enum si_llvm_calling_convention {
        RADEON_LLVM_AMDGPU_VS = 87,
@@ -93,17 +80,20 @@ static void si_diagnostic_handler(LLVMDiagnosticInfoRef di, void *context)
  *
  * @returns 0 for success, 1 for failure
  */
-unsigned si_llvm_compile(LLVMModuleRef M, struct ac_shader_binary *binary,
-                        struct si_compiler *compiler,
-                        struct pipe_debug_callback *debug)
+unsigned si_llvm_compile(LLVMModuleRef M, struct si_shader_binary *binary,
+                        struct ac_llvm_compiler *compiler,
+                        struct pipe_debug_callback *debug,
+                        bool less_optimized, unsigned wave_size)
 {
+       struct ac_compiler_passes *passes = compiler->passes;
+
+       if (wave_size == 32)
+               passes = compiler->passes_wave32;
+       else if (less_optimized && compiler->low_opt_passes)
+               passes = compiler->low_opt_passes;
+
        struct si_llvm_diagnostics diag;
-       char *err;
        LLVMContextRef llvm_ctx;
-       LLVMMemoryBufferRef out_buffer;
-       unsigned buffer_size;
-       const char *buffer_data;
-       LLVMBool mem_err;
 
        diag.debug = debug;
        diag.retval = 0;
@@ -113,39 +103,25 @@ unsigned si_llvm_compile(LLVMModuleRef M, struct ac_shader_binary *binary,
 
        LLVMContextSetDiagnosticHandler(llvm_ctx, si_diagnostic_handler, &diag);
 
-       /* Compile IR*/
-       mem_err = LLVMTargetMachineEmitToMemoryBuffer(compiler->tm, M,
-                                                     LLVMObjectFile, &err,
-                                                     &out_buffer);
-
-       /* Process Errors/Warnings */
-       if (mem_err) {
-               fprintf(stderr, "%s: %s", __FUNCTION__, err);
-               pipe_debug_message(debug, SHADER_INFO,
-                                  "LLVM emit error: %s", err);
-               FREE(err);
-               diag.retval = 1;
-               goto out;
-       }
-
-       /* Extract Shader Code*/
-       buffer_size = LLVMGetBufferSize(out_buffer);
-       buffer_data = LLVMGetBufferStart(out_buffer);
-
-       if (!ac_elf_read(buffer_data, buffer_size, binary)) {
-               fprintf(stderr, "radeonsi: cannot read an ELF shader binary\n");
+       /* Compile IR. */
+       if (!ac_compile_module_to_elf(passes, M, (char **)&binary->elf_buffer,
+                                     &binary->elf_size))
                diag.retval = 1;
-       }
 
-       /* Clean up */
-       LLVMDisposeMemoryBuffer(out_buffer);
-
-out:
        if (diag.retval != 0)
                pipe_debug_message(debug, SHADER_INFO, "LLVM compile failed");
        return diag.retval;
 }
 
+void si_shader_binary_clean(struct si_shader_binary *binary)
+{
+       free((void *)binary->elf_buffer);
+       binary->elf_buffer = NULL;
+
+       free(binary->llvm_ir_string);
+       binary->llvm_ir_string = NULL;
+}
+
 LLVMTypeRef tgsi2llvmtype(struct lp_build_tgsi_context *bld_base,
                          enum tgsi_opcode_type type)
 {
@@ -290,7 +266,6 @@ get_pointer_into_array(struct si_shader_context *ctx,
 {
        unsigned array_id;
        struct tgsi_array_info *array;
-       LLVMBuilderRef builder = ctx->ac.builder;
        LLVMValueRef idxs[2];
        LLVMValueRef index;
        LLVMValueRef alloca;
@@ -328,15 +303,10 @@ get_pointer_into_array(struct si_shader_context *ctx,
         */
        index = si_llvm_bound_index(ctx, index, array->range.Last - array->range.First + 1);
 
-       index = LLVMBuildMul(
-               builder, index,
-               LLVMConstInt(ctx->i32, util_bitcount(array->writemask), 0),
-               "");
-       index = LLVMBuildAdd(
-               builder, index,
-               LLVMConstInt(ctx->i32,
-                            util_bitcount(array->writemask & ((1 << swizzle) - 1)), 0),
-               "");
+       index = ac_build_imad(&ctx->ac, index,
+                             LLVMConstInt(ctx->i32, util_bitcount(array->writemask), 0),
+                             LLVMConstInt(ctx->i32,
+                                          util_bitcount(array->writemask & ((1 << swizzle) - 1)), 0));
        idxs[0] = ctx->i32_0;
        idxs[1] = index;
        return LLVMBuildGEP(ctx->ac.builder, alloca, idxs, 2, "");
@@ -349,18 +319,11 @@ si_llvm_emit_fetch_64bit(struct lp_build_tgsi_context *bld_base,
                         LLVMValueRef ptr2)
 {
        struct si_shader_context *ctx = si_shader_context(bld_base);
-       LLVMValueRef result;
-
-       result = LLVMGetUndef(LLVMVectorType(ctx->i32, 2));
-
-       result = LLVMBuildInsertElement(ctx->ac.builder,
-                                       result,
-                                       ac_to_integer(&ctx->ac, ptr),
-                                       ctx->i32_0, "");
-       result = LLVMBuildInsertElement(ctx->ac.builder,
-                                       result,
-                                       ac_to_integer(&ctx->ac, ptr2),
-                                       ctx->i32_1, "");
+       LLVMValueRef values[2] = {
+               ac_to_integer(&ctx->ac, ptr),
+               ac_to_integer(&ctx->ac, ptr2),
+       };
+       LLVMValueRef result = ac_build_gather_values(&ctx->ac, values, 2);
        return LLVMBuildBitCast(ctx->ac.builder, result, type, "");
 }
 
@@ -368,18 +331,21 @@ static LLVMValueRef
 emit_array_fetch(struct lp_build_tgsi_context *bld_base,
                 unsigned File, enum tgsi_opcode_type type,
                 struct tgsi_declaration_range range,
-                unsigned swizzle)
+                unsigned swizzle_in)
 {
        struct si_shader_context *ctx = si_shader_context(bld_base);
        unsigned i, size = range.Last - range.First + 1;
        LLVMTypeRef vec = LLVMVectorType(tgsi2llvmtype(bld_base, type), size);
        LLVMValueRef result = LLVMGetUndef(vec);
-
+       unsigned swizzle = swizzle_in;
        struct tgsi_full_src_register tmp_reg = {};
        tmp_reg.Register.File = File;
+       if (tgsi_type_is_64bit(type))
+               swizzle |= (swizzle_in + 1) << 16;
 
        for (i = 0; i < size; ++i) {
                tmp_reg.Register.Index = i + range.First;
+
                LLVMValueRef temp = si_llvm_emit_fetch(bld_base, &tmp_reg, type, swizzle);
                result = LLVMBuildInsertElement(ctx->ac.builder, result, temp,
                        LLVMConstInt(ctx->i32, i, 0), "array_vector");
@@ -496,19 +462,20 @@ get_output_ptr(struct lp_build_tgsi_context *bld_base, unsigned index,
 LLVMValueRef si_llvm_emit_fetch(struct lp_build_tgsi_context *bld_base,
                                const struct tgsi_full_src_register *reg,
                                enum tgsi_opcode_type type,
-                               unsigned swizzle)
+                               unsigned swizzle_in)
 {
        struct si_shader_context *ctx = si_shader_context(bld_base);
        LLVMBuilderRef builder = ctx->ac.builder;
        LLVMValueRef result = NULL, ptr, ptr2;
+       unsigned swizzle = swizzle_in & 0xffff;
 
-       if (swizzle == ~0) {
+       if (swizzle_in == ~0) {
                LLVMValueRef values[TGSI_NUM_CHANNELS];
                unsigned chan;
                for (chan = 0; chan < TGSI_NUM_CHANNELS; chan++) {
                        values[chan] = si_llvm_emit_fetch(bld_base, reg, type, chan);
                }
-               return lp_build_gather_values(&ctx->gallivm, values,
+               return ac_build_gather_values(&ctx->ac, values,
                                              TGSI_NUM_CHANNELS);
        }
 
@@ -527,7 +494,7 @@ LLVMValueRef si_llvm_emit_fetch(struct lp_build_tgsi_context *bld_base,
                                                        ctx->imms[reg->Register.Index * TGSI_NUM_CHANNELS + swizzle],
                                                        ctx->i32_0);
                        result = LLVMConstInsertElement(result,
-                                                       ctx->imms[reg->Register.Index * TGSI_NUM_CHANNELS + swizzle + 1],
+                                                       ctx->imms[reg->Register.Index * TGSI_NUM_CHANNELS + (swizzle_in >> 16)],
                                                        ctx->i32_1);
                        return LLVMConstBitCast(result, ctype);
                } else {
@@ -554,7 +521,7 @@ LLVMValueRef si_llvm_emit_fetch(struct lp_build_tgsi_context *bld_base,
 
                if (tgsi_type_is_64bit(type)) {
                        ptr = result;
-                       ptr2 = input[swizzle + 1];
+                       ptr2 = input[swizzle_in >> 16];
                        return si_llvm_emit_fetch_64bit(bld_base, tgsi2llvmtype(bld_base, type),
                                                        ptr, ptr2);
                }
@@ -566,7 +533,7 @@ LLVMValueRef si_llvm_emit_fetch(struct lp_build_tgsi_context *bld_base,
                        return LLVMGetUndef(tgsi2llvmtype(bld_base, type));
                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];
+                       ptr2 = ctx->temps[reg->Register.Index * TGSI_NUM_CHANNELS + (swizzle_in >> 16)];
                        return si_llvm_emit_fetch_64bit(bld_base, tgsi2llvmtype(bld_base, type),
                                                        LLVMBuildLoad(builder, ptr, ""),
                                                        LLVMBuildLoad(builder, ptr2, ""));
@@ -577,7 +544,7 @@ LLVMValueRef si_llvm_emit_fetch(struct lp_build_tgsi_context *bld_base,
        case TGSI_FILE_OUTPUT:
                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);
+                       ptr2 = get_output_ptr(bld_base, reg->Register.Index, (swizzle_in >> 16));
                        return si_llvm_emit_fetch_64bit(bld_base, tgsi2llvmtype(bld_base, type),
                                                        LLVMBuildLoad(builder, ptr, ""),
                                                        LLVMBuildLoad(builder, ptr2, ""));
@@ -595,11 +562,12 @@ LLVMValueRef si_llvm_emit_fetch(struct lp_build_tgsi_context *bld_base,
 static LLVMValueRef fetch_system_value(struct lp_build_tgsi_context *bld_base,
                                       const struct tgsi_full_src_register *reg,
                                       enum tgsi_opcode_type type,
-                                      unsigned swizzle)
+                                      unsigned swizzle_in)
 {
        struct si_shader_context *ctx = si_shader_context(bld_base);
        LLVMBuilderRef builder = ctx->ac.builder;
        LLVMValueRef cval = ctx->system_values[reg->Register.Index];
+       unsigned swizzle = swizzle_in & 0xffff;
 
        if (tgsi_type_is_64bit(type)) {
                LLVMValueRef lo, hi;
@@ -609,7 +577,7 @@ static LLVMValueRef fetch_system_value(struct lp_build_tgsi_context *bld_base,
                lo = LLVMBuildExtractElement(
                        builder, cval, LLVMConstInt(ctx->i32, swizzle, 0), "");
                hi = LLVMBuildExtractElement(
-                       builder, cval, LLVMConstInt(ctx->i32, swizzle + 1, 0), "");
+                       builder, cval, LLVMConstInt(ctx->i32, (swizzle_in >> 16), 0), "");
 
                return si_llvm_emit_fetch_64bit(bld_base, tgsi2llvmtype(bld_base, type),
                                                lo, hi);
@@ -638,9 +606,8 @@ static void emit_declaration(struct lp_build_tgsi_context *bld_base,
                for (idx = decl->Range.First; idx <= decl->Range.Last; idx++) {
                        unsigned chan;
                        for (chan = 0; chan < TGSI_NUM_CHANNELS; chan++) {
-                                ctx->addrs[idx][chan] = lp_build_alloca_undef(
-                                       &ctx->gallivm,
-                                       ctx->i32, "");
+                                ctx->addrs[idx][chan] = ac_build_alloca_undef(
+                                       &ctx->ac, ctx->i32, "");
                        }
                }
                break;
@@ -685,7 +652,7 @@ static void emit_declaration(struct lp_build_tgsi_context *bld_base,
                         */
                        if (array_size > 16 ||
                            !ctx->screen->llvm_has_working_vgpr_indexing) {
-                               array_alloca = lp_build_alloca_undef(&ctx->gallivm,
+                               array_alloca = ac_build_alloca_undef(&ctx->ac,
                                        LLVMArrayType(ctx->f32,
                                                      array_size), "array");
                                ctx->temp_array_allocas[id] = array_alloca;
@@ -698,12 +665,12 @@ static void emit_declaration(struct lp_build_tgsi_context *bld_base,
                }
                if (!array_alloca) {
                        for (i = 0; i < decl_size; ++i) {
-#ifdef DEBUG
+#ifndef NDEBUG
                                snprintf(name, sizeof(name), "TEMP%d.%c",
                                         first + i / 4, "xyzw"[i % 4]);
 #endif
                                ctx->temps[first * TGSI_NUM_CHANNELS + i] =
-                                       lp_build_alloca_undef(&ctx->gallivm,
+                                       ac_build_alloca_undef(&ctx->ac,
                                                              ctx->f32,
                                                              name);
                        }
@@ -721,15 +688,14 @@ static void emit_declaration(struct lp_build_tgsi_context *bld_base,
                                 * a shader ever reads from a channel that
                                 * it never writes to.
                                 */
-                               ctx->undef_alloca = lp_build_alloca_undef(
-                                       &ctx->gallivm,
-                                       ctx->f32, "undef");
+                               ctx->undef_alloca = ac_build_alloca_undef(
+                                       &ctx->ac, ctx->f32, "undef");
                        }
 
                        for (i = 0; i < decl_size; ++i) {
                                LLVMValueRef ptr;
                                if (writemask & (1 << (i % 4))) {
-#ifdef DEBUG
+#ifndef NDEBUG
                                        snprintf(name, sizeof(name), "TEMP%d.%c",
                                                 first + i / 4, "xyzw"[i % 4]);
 #endif
@@ -783,13 +749,12 @@ static void emit_declaration(struct lp_build_tgsi_context *bld_base,
                        if (ctx->outputs[idx][0])
                                continue;
                        for (chan = 0; chan < TGSI_NUM_CHANNELS; chan++) {
-#ifdef DEBUG
+#ifndef NDEBUG
                                snprintf(name, sizeof(name), "OUT%d.%c",
                                         idx, "xyzw"[chan % 4]);
 #endif
-                               ctx->outputs[idx][chan] = lp_build_alloca_undef(
-                                       &ctx->gallivm,
-                                       ctx->f32, name);
+                               ctx->outputs[idx][chan] = ac_build_alloca_undef(
+                                       &ctx->ac, ctx->f32, name);
                        }
                }
                break;
@@ -988,7 +953,9 @@ static void emit_immediate(struct lp_build_tgsi_context *bld_base,
 
 void si_llvm_context_init(struct si_shader_context *ctx,
                          struct si_screen *sscreen,
-                         struct si_compiler *compiler)
+                         struct ac_llvm_compiler *compiler,
+                         unsigned wave_size,
+                         unsigned ballot_mask_bits)
 {
        struct lp_type type;
 
@@ -1001,24 +968,14 @@ void si_llvm_context_init(struct si_shader_context *ctx,
        ctx->screen = sscreen;
        ctx->compiler = compiler;
 
-       ctx->gallivm.context = LLVMContextCreate();
-       ctx->gallivm.module = LLVMModuleCreateWithNameInContext("tgsi",
-                                               ctx->gallivm.context);
-       LLVMSetTarget(ctx->gallivm.module, compiler->triple);
-       LLVMSetDataLayout(ctx->gallivm.module, compiler->data_layout);
-
-       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;
+       ac_llvm_context_init(&ctx->ac, compiler, sscreen->info.chip_class,
+                            sscreen->info.family,
+                            AC_FLOAT_MODE_NO_SIGNED_ZEROS_FP_MATH,
+                            wave_size, ballot_mask_bits);
 
-       ctx->gallivm.builder = ac_create_builder(ctx->gallivm.context,
-                                                float_mode);
-
-       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;
+       ctx->gallivm.context = ctx->ac.context;
+       ctx->gallivm.module = ctx->ac.module;
+       ctx->gallivm.builder = ctx->ac.builder;
 
        struct lp_build_tgsi_context *bld_base = &ctx->bld_base;
 
@@ -1051,7 +1008,7 @@ void si_llvm_context_init(struct si_shader_context *ctx,
        bld_base->op_actions[TGSI_OPCODE_ENDIF].emit = endif_emit;
        bld_base->op_actions[TGSI_OPCODE_ENDLOOP].emit = endloop_emit;
 
-       si_shader_context_init_alu(&ctx->bld_base);
+       si_shader_context_init_alu(ctx);
        si_shader_context_init_mem(ctx);
 
        ctx->voidt = LLVMVoidTypeInContext(ctx->ac.context);
@@ -1068,23 +1025,20 @@ void si_llvm_context_init(struct si_shader_context *ctx,
 
        ctx->i32_0 = LLVMConstInt(ctx->i32, 0, 0);
        ctx->i32_1 = LLVMConstInt(ctx->i32, 1, 0);
+       ctx->i1false = LLVMConstInt(ctx->i1, 0, 0);
+       ctx->i1true = LLVMConstInt(ctx->i1, 1, 0);
 }
 
 /* Set the context to a certain TGSI shader. Can be called repeatedly
  * to change the shader. */
-void si_llvm_context_set_tgsi(struct si_shader_context *ctx,
-                             struct si_shader *shader)
+void si_llvm_context_set_ir(struct si_shader_context *ctx,
+                           struct si_shader *shader)
 {
-       const struct tgsi_shader_info *info = NULL;
-       const struct tgsi_token *tokens = NULL;
-
-       if (shader && shader->selector) {
-               info = &shader->selector->info;
-               tokens = shader->selector->tokens;
-       }
+       struct si_shader_selector *sel = shader->selector;
+       const struct tgsi_shader_info *info = &sel->info;
 
        ctx->shader = shader;
-       ctx->type = info ? info->processor : -1;
+       ctx->type = sel->type;
        ctx->bld_base.info = info;
 
        /* Clean up the old contents. */
@@ -1101,16 +1055,13 @@ void si_llvm_context_set_tgsi(struct si_shader_context *ctx,
        ctx->temps = NULL;
        ctx->temps_count = 0;
 
-       if (!info)
-               return;
-
        ctx->num_const_buffers = util_last_bit(info->const_buffers_declared);
        ctx->num_shader_buffers = util_last_bit(info->shader_buffers_declared);
 
        ctx->num_samplers = util_last_bit(info->samplers_declared);
        ctx->num_images = util_last_bit(info->images_declared);
 
-       if (!tokens)
+       if (sel->nir)
                return;
 
        if (info->array_max[TGSI_FILE_TEMPORARY] > 0) {
@@ -1119,7 +1070,7 @@ void si_llvm_context_set_tgsi(struct si_shader_context *ctx,
                ctx->temp_arrays = CALLOC(size, sizeof(ctx->temp_arrays[0]));
                ctx->temp_array_allocas = CALLOC(size, sizeof(ctx->temp_array_allocas[0]));
 
-               tgsi_scan_arrays(tokens, TGSI_FILE_TEMPORARY, size,
+               tgsi_scan_arrays(sel->tokens, TGSI_FILE_TEMPORARY, size,
                                 ctx->temp_arrays);
        }
        if (info->file_max[TGSI_FILE_IMMEDIATE] >= 0) {
@@ -1148,7 +1099,7 @@ void si_llvm_create_func(struct si_shader_context *ctx,
        LLVMTypeRef main_fn_type, ret_type;
        LLVMBasicBlockRef main_fn_body;
        enum si_llvm_calling_convention call_conv;
-       unsigned real_shader_type;
+       enum pipe_shader_type real_shader_type;
 
        if (num_return_elems)
                ret_type = LLVMStructTypeInContext(ctx->ac.context,
@@ -1171,7 +1122,7 @@ void si_llvm_create_func(struct si_shader_context *ctx,
        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)
+               else if (ctx->shader->key.as_es || ctx->shader->key.as_ngg)
                        real_shader_type = PIPE_SHADER_GEOMETRY;
        }
 
@@ -1181,8 +1132,7 @@ void si_llvm_create_func(struct si_shader_context *ctx,
                call_conv = RADEON_LLVM_AMDGPU_VS;
                break;
        case PIPE_SHADER_TESS_CTRL:
-               call_conv = HAVE_LLVM >= 0x0500 ? RADEON_LLVM_AMDGPU_HS :
-                                                 RADEON_LLVM_AMDGPU_VS;
+               call_conv = RADEON_LLVM_AMDGPU_HS;
                break;
        case PIPE_SHADER_GEOMETRY:
                call_conv = RADEON_LLVM_AMDGPU_GS;