freedreno: Remove the Emacs mode lines
[mesa.git] / src / gallium / drivers / freedreno / ir3 / ir3_nir.c
index 73c65d6ad272983388aa06128aaae7f5f41904ce..d934acc74273d461be17b959b8ea52089267f315 100644 (file)
@@ -1,5 +1,3 @@
-/* -*- mode: C; c-file-style: "k&r"; tab-width 4; indent-tabs-mode: t; -*- */
-
 /*
  * Copyright (C) 2015 Rob Clark <robclark@freedesktop.org>
  *
 
 #include "nir/tgsi_to_nir.h"
 
+
+static const nir_shader_compiler_options options = {
+               .lower_fpow = true,
+               .lower_scmp = true,
+               .lower_flrp32 = true,
+               .lower_flrp64 = true,
+               .lower_ffract = true,
+               .lower_fmod32 = true,
+               .lower_fmod64 = true,
+               .lower_fdiv = true,
+               .lower_ldexp = true,
+               .fuse_ffma = true,
+               .native_integers = true,
+               .vertex_id_zero_based = true,
+               .lower_extract_byte = true,
+               .lower_extract_word = true,
+               .lower_all_io_to_temps = true,
+               .lower_helper_invocation = true,
+};
+
 struct nir_shader *
 ir3_tgsi_to_nir(const struct tgsi_token *tokens)
 {
-       static const nir_shader_compiler_options options = {
-                       .lower_fpow = true,
-                       .lower_fsat = true,
-                       .lower_scmp = true,
-                       .lower_flrp = true,
-                       .lower_ffract = true,
-                       .native_integers = true,
-                       .lower_extract_byte = true,
-                       .lower_extract_word = true,
-       };
        return tgsi_to_nir(tokens, &options);
 }
 
+const nir_shader_compiler_options *
+ir3_get_compiler_options(struct ir3_compiler *compiler)
+{
+       return &options;
+}
+
 /* for given shader key, are any steps handled in nir? */
 bool
 ir3_key_lowers_nir(const struct ir3_shader_key *key)
 {
        return key->fsaturate_s | key->fsaturate_t | key->fsaturate_r |
                        key->vsaturate_s | key->vsaturate_t | key->vsaturate_r |
-                       key->ucp_enables | key->color_two_side;
+                       key->ucp_enables | key->color_two_side |
+                       key->fclamp_color | key->vclamp_color;
 }
 
 #define OPT(nir, pass, ...) ({                             \
@@ -68,6 +83,50 @@ ir3_key_lowers_nir(const struct ir3_shader_key *key)
 
 #define OPT_V(nir, pass, ...) NIR_PASS_V(nir, pass, ##__VA_ARGS__)
 
+static void
+ir3_optimize_loop(nir_shader *s)
+{
+       bool progress;
+       do {
+               progress = false;
+
+               OPT_V(s, nir_lower_vars_to_ssa);
+               progress |= OPT(s, nir_opt_copy_prop_vars);
+               progress |= OPT(s, nir_opt_dead_write_vars);
+               progress |= OPT(s, nir_lower_alu_to_scalar);
+               progress |= OPT(s, nir_lower_phis_to_scalar);
+
+               progress |= OPT(s, nir_copy_prop);
+               progress |= OPT(s, nir_opt_dce);
+               progress |= OPT(s, nir_opt_cse);
+               static int gcm = -1;
+               if (gcm == -1)
+                       gcm = env2u("GCM");
+               if (gcm == 1)
+                       progress |= OPT(s, nir_opt_gcm, true);
+               else if (gcm == 2)
+                       progress |= OPT(s, nir_opt_gcm, false);
+               progress |= OPT(s, nir_opt_peephole_select, 16);
+               progress |= OPT(s, nir_opt_intrinsics);
+               progress |= OPT(s, nir_opt_algebraic);
+               progress |= OPT(s, nir_opt_constant_folding);
+               progress |= OPT(s, nir_opt_dead_cf);
+               if (OPT(s, nir_opt_trivial_continues)) {
+                       progress |= true;
+                       /* If nir_opt_trivial_continues makes progress, then we need to clean
+                        * things up if we want any hope of nir_opt_if or nir_opt_loop_unroll
+                        * to make progress.
+                        */
+                       OPT(s, nir_copy_prop);
+                       OPT(s, nir_opt_dce);
+               }
+               progress |= OPT(s, nir_opt_if);
+               progress |= OPT(s, nir_opt_remove_phis);
+               progress |= OPT(s, nir_opt_undef);
+
+       } while (progress);
+}
+
 struct nir_shader *
 ir3_optimize_nir(struct ir3_shader *shader, nir_shader *s,
                const struct ir3_shader_key *key)
@@ -75,12 +134,10 @@ ir3_optimize_nir(struct ir3_shader *shader, nir_shader *s,
        struct nir_lower_tex_options tex_options = {
                        .lower_rect = 0,
        };
-       bool progress;
 
        if (key) {
                switch (shader->type) {
                case SHADER_FRAGMENT:
-               case SHADER_COMPUTE:
                        tex_options.saturate_s = key->fsaturate_s;
                        tex_options.saturate_t = key->fsaturate_t;
                        tex_options.saturate_r = key->fsaturate_r;
@@ -90,6 +147,9 @@ ir3_optimize_nir(struct ir3_shader *shader, nir_shader *s,
                        tex_options.saturate_t = key->vsaturate_t;
                        tex_options.saturate_r = key->vsaturate_r;
                        break;
+               default:
+                       /* TODO */
+                       break;
                }
        }
 
@@ -108,40 +168,44 @@ ir3_optimize_nir(struct ir3_shader *shader, nir_shader *s,
        }
 
        OPT_V(s, nir_opt_global_to_local);
-       OPT_V(s, nir_convert_to_ssa);
+       OPT_V(s, nir_lower_regs_to_ssa);
 
        if (key) {
-               if (s->stage == MESA_SHADER_VERTEX) {
+               if (s->info.stage == MESA_SHADER_VERTEX) {
                        OPT_V(s, nir_lower_clip_vs, key->ucp_enables);
-               } else if (s->stage == MESA_SHADER_FRAGMENT) {
+                       if (key->vclamp_color)
+                               OPT_V(s, nir_lower_clamp_color_outputs);
+               } else if (s->info.stage == MESA_SHADER_FRAGMENT) {
                        OPT_V(s, nir_lower_clip_fs, key->ucp_enables);
+                       if (key->fclamp_color)
+                               OPT_V(s, nir_lower_clamp_color_outputs);
                }
                if (key->color_two_side) {
                        OPT_V(s, nir_lower_two_sided_color);
                }
+       } else {
+               /* only want to do this the first time (when key is null)
+                * and not again on any potential 2nd variant lowering pass:
+                */
+               OPT_V(s, ir3_nir_apply_trig_workarounds);
        }
 
        OPT_V(s, nir_lower_tex, &tex_options);
-       OPT_V(s, nir_lower_idiv);
        OPT_V(s, nir_lower_load_const_to_scalar);
+       if (shader->compiler->gpu_id < 500)
+               OPT_V(s, ir3_nir_lower_tg4_to_tex);
 
-       do {
-               progress = false;
+       ir3_optimize_loop(s);
 
-               OPT_V(s, nir_lower_vars_to_ssa);
-               OPT_V(s, nir_lower_alu_to_scalar);
-               OPT_V(s, nir_lower_phis_to_scalar);
-
-               progress |= OPT(s, nir_copy_prop);
-               progress |= OPT(s, nir_opt_dce);
-               progress |= OPT(s, nir_opt_cse);
-               progress |= OPT(s, ir3_nir_lower_if_else);
-               progress |= OPT(s, nir_opt_algebraic);
-               progress |= OPT(s, nir_opt_constant_folding);
+       /* do idiv lowering after first opt loop to give a chance for
+        * divide by immed power-of-two to be caught first:
+        */
+       if (OPT(s, nir_lower_idiv))
+               ir3_optimize_loop(s);
 
-       } while (progress);
+       OPT_V(s, nir_remove_dead_variables, nir_var_local);
 
-       OPT_V(s, nir_remove_dead_variables);
+       OPT_V(s, nir_move_load_const);
 
        if (fd_mesa_debug & FD_DBG_DISASM) {
                debug_printf("----------------------\n");
@@ -153,3 +217,55 @@ ir3_optimize_nir(struct ir3_shader *shader, nir_shader *s,
 
        return s;
 }
+
+void
+ir3_nir_scan_driver_consts(nir_shader *shader,
+               struct ir3_driver_const_layout *layout)
+{
+       nir_foreach_function(function, shader) {
+               if (!function->impl)
+                       continue;
+
+               nir_foreach_block(block, function->impl) {
+                       nir_foreach_instr(instr, block) {
+                               if (instr->type != nir_instr_type_intrinsic)
+                                       continue;
+
+                               nir_intrinsic_instr *intr =
+                                       nir_instr_as_intrinsic(instr);
+                               unsigned idx;
+
+                               switch (intr->intrinsic) {
+                               case nir_intrinsic_get_buffer_size:
+                                       idx = nir_src_as_const_value(intr->src[0])->u32[0];
+                                       if (layout->ssbo_size.mask & (1 << idx))
+                                               break;
+                                       layout->ssbo_size.mask |= (1 << idx);
+                                       layout->ssbo_size.off[idx] =
+                                               layout->ssbo_size.count;
+                                       layout->ssbo_size.count += 1; /* one const per */
+                                       break;
+                               case nir_intrinsic_image_deref_atomic_add:
+                               case nir_intrinsic_image_deref_atomic_min:
+                               case nir_intrinsic_image_deref_atomic_max:
+                               case nir_intrinsic_image_deref_atomic_and:
+                               case nir_intrinsic_image_deref_atomic_or:
+                               case nir_intrinsic_image_deref_atomic_xor:
+                               case nir_intrinsic_image_deref_atomic_exchange:
+                               case nir_intrinsic_image_deref_atomic_comp_swap:
+                               case nir_intrinsic_image_deref_store:
+                                       idx = nir_intrinsic_get_var(intr, 0)->data.driver_location;
+                                       if (layout->image_dims.mask & (1 << idx))
+                                               break;
+                                       layout->image_dims.mask |= (1 << idx);
+                                       layout->image_dims.off[idx] =
+                                               layout->image_dims.count;
+                                       layout->image_dims.count += 3; /* three const per */
+                                       break;
+                               default:
+                                       break;
+                               }
+                       }
+               }
+       }
+}