nir/vtn: Add type constant to image intrinsics
[mesa.git] / src / compiler / nir / nir_split_vars.c
index 93b5e15e8df11dae51c7ea151ef19d1a8d64fd2c..6aee3109d4121a2b8f94455d4a513dfec97c7234 100644 (file)
@@ -156,6 +156,7 @@ static bool
 split_var_list_structs(nir_shader *shader,
                        nir_function_impl *impl,
                        struct exec_list *vars,
+                       nir_variable_mode mode,
                        struct hash_table *var_field_map,
                        struct set **complex_vars,
                        void *mem_ctx)
@@ -172,7 +173,10 @@ split_var_list_structs(nir_shader *shader,
    /* To avoid list confusion (we'll be adding things as we split variables),
     * pull all of the variables we plan to split off of the list
     */
-   nir_foreach_variable_safe(var, vars) {
+   nir_foreach_variable_in_list_safe(var, vars) {
+      if (var->data.mode != mode)
+         continue;
+
       if (!glsl_type_is_struct_or_ifc(glsl_without_array(var->type)))
          continue;
 
@@ -189,7 +193,7 @@ split_var_list_structs(nir_shader *shader,
       exec_list_push_tail(&split_vars, &var->node);
    }
 
-   nir_foreach_variable(var, &split_vars) {
+   nir_foreach_variable_in_list(var, &split_vars) {
       state.base_var = var;
 
       struct field *root_field = ralloc(mem_ctx, struct field);
@@ -304,7 +308,8 @@ nir_split_struct_vars(nir_shader *shader, nir_variable_mode modes)
    bool has_global_splits = false;
    if (modes & nir_var_shader_temp) {
       has_global_splits = split_var_list_structs(shader, NULL,
-                                                 &shader->globals,
+                                                 &shader->variables,
+                                                 nir_var_shader_temp,
                                                  var_field_map,
                                                  &complex_vars,
                                                  mem_ctx);
@@ -319,6 +324,7 @@ nir_split_struct_vars(nir_shader *shader, nir_variable_mode modes)
       if (modes & nir_var_function_temp) {
          has_local_splits = split_var_list_structs(shader, function->impl,
                                                    &function->impl->locals,
+                                                   nir_var_function_temp,
                                                    var_field_map,
                                                    &complex_vars,
                                                    mem_ctx);
@@ -369,13 +375,17 @@ struct array_var_info {
 static bool
 init_var_list_array_infos(nir_shader *shader,
                           struct exec_list *vars,
+                          nir_variable_mode mode,
                           struct hash_table *var_info_map,
                           struct set **complex_vars,
                           void *mem_ctx)
 {
    bool has_array = false;
 
-   nir_foreach_variable(var, vars) {
+   nir_foreach_variable_in_list(var, vars) {
+      if (var->data.mode != mode)
+         continue;
+
       int num_levels = num_array_levels_in_array_of_vector_type(var->type);
       if (num_levels <= 0)
          continue;
@@ -537,13 +547,17 @@ static bool
 split_var_list_arrays(nir_shader *shader,
                       nir_function_impl *impl,
                       struct exec_list *vars,
+                      nir_variable_mode mode,
                       struct hash_table *var_info_map,
                       void *mem_ctx)
 {
    struct exec_list split_vars;
    exec_list_make_empty(&split_vars);
 
-   nir_foreach_variable_safe(var, vars) {
+   nir_foreach_variable_in_list_safe(var, vars) {
+      if (var->data.mode != mode)
+         continue;
+
       struct array_var_info *info = get_array_var_info(var, var_info_map);
       if (!info)
          continue;
@@ -587,7 +601,7 @@ split_var_list_arrays(nir_shader *shader,
       }
    }
 
-   nir_foreach_variable(var, &split_vars) {
+   nir_foreach_variable_in_list(var, &split_vars) {
       struct array_var_info *info = get_array_var_info(var, var_info_map);
       create_split_array_vars(info, 0, &info->root_split, var->name,
                               shader, impl, mem_ctx);
@@ -858,7 +872,8 @@ nir_split_array_vars(nir_shader *shader, nir_variable_mode modes)
    bool has_global_array = false;
    if (modes & nir_var_shader_temp) {
       has_global_array = init_var_list_array_infos(shader,
-                                                   &shader->globals,
+                                                   &shader->variables,
+                                                   nir_var_shader_temp,
                                                    var_info_map,
                                                    &complex_vars,
                                                    mem_ctx);
@@ -873,6 +888,7 @@ nir_split_array_vars(nir_shader *shader, nir_variable_mode modes)
       if (modes & nir_var_function_temp) {
          has_local_array = init_var_list_array_infos(shader,
                                                      &function->impl->locals,
+                                                     nir_var_function_temp,
                                                      var_info_map,
                                                      &complex_vars,
                                                      mem_ctx);
@@ -887,13 +903,15 @@ nir_split_array_vars(nir_shader *shader, nir_variable_mode modes)
    /* If we failed to find any arrays of arrays, bail early. */
    if (!has_any_array) {
       ralloc_free(mem_ctx);
+      nir_shader_preserve_all_metadata(shader);
       return false;
    }
 
    bool has_global_splits = false;
    if (modes & nir_var_shader_temp) {
       has_global_splits = split_var_list_arrays(shader, NULL,
-                                                &shader->globals,
+                                                &shader->variables,
+                                                nir_var_shader_temp,
                                                 var_info_map, mem_ctx);
    }
 
@@ -906,6 +924,7 @@ nir_split_array_vars(nir_shader *shader, nir_variable_mode modes)
       if (modes & nir_var_function_temp) {
          has_local_splits = split_var_list_arrays(shader, function->impl,
                                                   &function->impl->locals,
+                                                  nir_var_function_temp,
                                                   var_info_map, mem_ctx);
       }
 
@@ -1232,6 +1251,7 @@ find_used_components_impl(nir_function_impl *impl,
 
 static bool
 shrink_vec_var_list(struct exec_list *vars,
+                    nir_variable_mode mode,
                     struct hash_table *var_usage_map)
 {
    /* Initialize the components kept field of each variable.  This is the
@@ -1250,7 +1270,10 @@ shrink_vec_var_list(struct exec_list *vars,
     * Also, if we have a copy that to/from something we can't shrink, we need
     * to leave components and array_len of any wildcards alone.
     */
-   nir_foreach_variable(var, vars) {
+   nir_foreach_variable_in_list(var, vars) {
+      if (var->data.mode != mode)
+         continue;
+
       struct vec_var_usage *usage =
          get_vec_var_usage(var, var_usage_map, false, NULL);
       if (!usage)
@@ -1283,7 +1306,10 @@ shrink_vec_var_list(struct exec_list *vars,
    bool fp_progress;
    do {
       fp_progress = false;
-      nir_foreach_variable(var, vars) {
+      nir_foreach_variable_in_list(var, vars) {
+         if (var->data.mode != mode)
+            continue;
+
          struct vec_var_usage *var_usage =
             get_vec_var_usage(var, var_usage_map, false, NULL);
          if (!var_usage || !var_usage->vars_copied)
@@ -1320,7 +1346,10 @@ shrink_vec_var_list(struct exec_list *vars,
    } while (fp_progress);
 
    bool vars_shrunk = false;
-   nir_foreach_variable_safe(var, vars) {
+   nir_foreach_variable_in_list_safe(var, vars) {
+      if (var->data.mode != mode)
+         continue;
+
       struct vec_var_usage *usage =
          get_vec_var_usage(var, var_usage_map, false, NULL);
       if (!usage)
@@ -1589,8 +1618,11 @@ function_impl_has_vars_with_modes(nir_function_impl *impl,
 {
    nir_shader *shader = impl->function->shader;
 
-   if ((modes & nir_var_shader_temp) && !exec_list_is_empty(&shader->globals))
-      return true;
+   if (modes & ~nir_var_function_temp) {
+      nir_foreach_variable_with_modes(var, shader,
+                                      modes & ~nir_var_function_temp)
+         return true;
+   }
 
    if ((modes & nir_var_function_temp) && !exec_list_is_empty(&impl->locals))
       return true;
@@ -1634,12 +1666,16 @@ nir_shrink_vec_array_vars(nir_shader *shader, nir_variable_mode modes)
    }
    if (!has_vars_to_shrink) {
       ralloc_free(mem_ctx);
+      nir_shader_preserve_all_metadata(shader);
       return false;
    }
 
    bool globals_shrunk = false;
-   if (modes & nir_var_shader_temp)
-      globals_shrunk = shrink_vec_var_list(&shader->globals, var_usage_map);
+   if (modes & nir_var_shader_temp) {
+      globals_shrunk = shrink_vec_var_list(&shader->variables,
+                                           nir_var_shader_temp,
+                                           var_usage_map);
+   }
 
    bool progress = false;
    nir_foreach_function(function, shader) {
@@ -1649,6 +1685,7 @@ nir_shrink_vec_array_vars(nir_shader *shader, nir_variable_mode modes)
       bool locals_shrunk = false;
       if (modes & nir_var_function_temp) {
          locals_shrunk = shrink_vec_var_list(&function->impl->locals,
+                                             nir_var_function_temp,
                                              var_usage_map);
       }