Mesa (main): microsoft/compiler: Move d3d12_fix_io_uint_type() to dxil_nir.c

GitLab Mirror gitlab-mirror at kemper.freedesktop.org
Wed Jun 15 22:47:32 UTC 2022


Module: Mesa
Branch: main
Commit: afb64e10c1f19b7f76454e052c95d3b2bd2957ba
URL:    http://cgit.freedesktop.org/mesa/mesa/commit/?id=afb64e10c1f19b7f76454e052c95d3b2bd2957ba

Author: Boris Brezillon <boris.brezillon at collabora.com>
Date:   Wed Jun 15 07:03:55 2022 -0700

microsoft/compiler: Move d3d12_fix_io_uint_type() to dxil_nir.c

We currently have two implementations of the same logic. Let's pick
the d3d12 one, move it to dxil_nir.c and let nir_to_dxil() call it
when appropriate.

Reviewed-by: Jesse Natalie <jenatali at microsoft.com>
Part-of: <https://gitlab.freedesktop.org/mesa/mesa/-/merge_requests/17039>

---

 src/gallium/drivers/d3d12/d3d12_compiler.cpp |  8 ----
 src/gallium/drivers/d3d12/d3d12_nir_passes.c | 54 ------------------------
 src/microsoft/compiler/dxil_nir.c            | 61 ++++++++++++++++++++++++++++
 src/microsoft/compiler/dxil_nir.h            |  1 +
 src/microsoft/compiler/nir_to_dxil.c         | 12 ++++++
 src/microsoft/spirv_to_dxil/dxil_spirv_nir.c | 36 ----------------
 6 files changed, 74 insertions(+), 98 deletions(-)

diff --git a/src/gallium/drivers/d3d12/d3d12_compiler.cpp b/src/gallium/drivers/d3d12/d3d12_compiler.cpp
index 0219c6044dc..e7cdba8c610 100644
--- a/src/gallium/drivers/d3d12/d3d12_compiler.cpp
+++ b/src/gallium/drivers/d3d12/d3d12_compiler.cpp
@@ -1406,14 +1406,6 @@ d3d12_create_shader(struct d3d12_context *ctx,
    d3d12_shader_selector *prev = get_prev_shader(ctx, sel->stage);
    d3d12_shader_selector *next = get_next_shader(ctx, sel->stage);
 
-   uint64_t in_mask = nir->info.stage == MESA_SHADER_VERTEX ?
-                         0 : (VARYING_BIT_PRIMITIVE_ID | VARYING_BIT_VIEWPORT);
-
-   uint64_t out_mask = nir->info.stage == MESA_SHADER_FRAGMENT ?
-                          (1ull << FRAG_RESULT_STENCIL) | (1ull << FRAG_RESULT_SAMPLE_MASK) :
-                          (VARYING_BIT_PRIMITIVE_ID | VARYING_BIT_VIEWPORT);
-
-   d3d12_fix_io_uint_type(nir, in_mask, out_mask);
    NIR_PASS_V(nir, dxil_nir_split_clip_cull_distance);
    NIR_PASS_V(nir, d3d12_split_multistream_varyings);
 
diff --git a/src/gallium/drivers/d3d12/d3d12_nir_passes.c b/src/gallium/drivers/d3d12/d3d12_nir_passes.c
index 2ecef979d55..ea34c123168 100644
--- a/src/gallium/drivers/d3d12/d3d12_nir_passes.c
+++ b/src/gallium/drivers/d3d12/d3d12_nir_passes.c
@@ -679,60 +679,6 @@ d3d12_add_missing_dual_src_target(struct nir_shader *s,
                                nir_metadata_dominance);
 }
 
-static bool
-fix_io_uint_type(struct nir_shader *s, nir_variable_mode modes, int slot)
-{
-   nir_variable *fixed_var = NULL;
-   nir_foreach_variable_with_modes(var, s, modes) {
-      if (var->data.location == slot) {
-         var->type = glsl_uint_type();
-         fixed_var = var;
-         break;
-      }
-   }
-
-   assert(fixed_var);
-
-   nir_foreach_function(function, s) {
-      if (function->impl) {
-         nir_foreach_block(block, function->impl) {
-            nir_foreach_instr_safe(instr, block) {
-               if (instr->type == nir_instr_type_deref) {
-                  nir_deref_instr *deref = nir_instr_as_deref(instr);
-                  if (deref->var == fixed_var)
-                     deref->type = fixed_var->type;
-               }
-            }
-         }
-      }
-   }
-   return true;
-}
-
-bool
-d3d12_fix_io_uint_type(struct nir_shader *s, uint64_t in_mask, uint64_t out_mask)
-{
-   if (!(s->info.outputs_written & out_mask) &&
-       !(s->info.inputs_read & in_mask))
-      return false;
-
-   bool progress = false;
-
-   while (in_mask) {
-      int slot = u_bit_scan64(&in_mask);
-      progress |= (s->info.inputs_read & (1ull << slot)) &&
-                  fix_io_uint_type(s, nir_var_shader_in, slot);
-   }
-
-   while (out_mask) {
-      int slot = u_bit_scan64(&out_mask);
-      progress |= (s->info.outputs_written & (1ull << slot)) &&
-                  fix_io_uint_type(s, nir_var_shader_out, slot);
-   }
-
-   return progress;
-}
-
 static bool
 lower_load_ubo_packed_filter(const nir_instr *instr,
                              UNUSED const void *_options) {
diff --git a/src/microsoft/compiler/dxil_nir.c b/src/microsoft/compiler/dxil_nir.c
index 97a28515565..c309338d45d 100644
--- a/src/microsoft/compiler/dxil_nir.c
+++ b/src/microsoft/compiler/dxil_nir.c
@@ -1968,3 +1968,64 @@ dxil_nir_lower_fquantize2f16(nir_shader *s)
 {
    return nir_shader_lower_instructions(s, is_fquantize2f16, lower_fquantize2f16, NULL);
 }
+
+static bool
+fix_io_uint_deref_types(struct nir_builder *builder, nir_instr *instr, void *data)
+{
+   if (instr->type != nir_instr_type_deref)
+      return false;
+
+   nir_deref_instr *deref = nir_instr_as_deref(instr);
+   nir_variable *var =
+      deref->deref_type == nir_deref_type_var ? deref->var : NULL;
+
+   if (var == data) {
+      deref->type = var->type;
+      return true;
+   }
+
+   return false;
+}
+
+static bool
+fix_io_uint_type(nir_shader *s, nir_variable_mode modes, int slot)
+{
+   nir_variable *fixed_var = NULL;
+   nir_foreach_variable_with_modes(var, s, modes) {
+      if (var->data.location == slot) {
+         assert(var->type = glsl_int_type());
+         var->type = glsl_uint_type();
+         fixed_var = var;
+         break;
+      }
+   }
+
+   assert(fixed_var);
+
+   return nir_shader_instructions_pass(s, fix_io_uint_deref_types,
+                                       nir_metadata_all, fixed_var);
+}
+
+bool
+dxil_nir_fix_io_uint_type(nir_shader *s, uint64_t in_mask, uint64_t out_mask)
+{
+   if (!(s->info.outputs_written & out_mask) &&
+       !(s->info.inputs_read & in_mask))
+      return false;
+
+   bool progress = false;
+
+   while (in_mask) {
+      int slot = u_bit_scan64(&in_mask);
+      progress |= (s->info.inputs_read & (1ull << slot)) &&
+                  fix_io_uint_type(s, nir_var_shader_in, slot);
+   }
+
+   while (out_mask) {
+      int slot = u_bit_scan64(&out_mask);
+      progress |= (s->info.outputs_written & (1ull << slot)) &&
+                  fix_io_uint_type(s, nir_var_shader_out, slot);
+   }
+
+   return progress;
+}
diff --git a/src/microsoft/compiler/dxil_nir.h b/src/microsoft/compiler/dxil_nir.h
index a7fbf13d7f3..cb853efd57f 100644
--- a/src/microsoft/compiler/dxil_nir.h
+++ b/src/microsoft/compiler/dxil_nir.h
@@ -74,6 +74,7 @@ void dxil_nir_split_tess_ctrl(nir_shader *nir, nir_function **patch_const_func);
 bool dxil_nir_fixup_tess_level_for_domain(nir_shader *nir);
 bool dxil_nir_set_tcs_patches_in(nir_shader *nir, unsigned num_control_points);
 bool dxil_nir_lower_ubo_array_one_to_static(nir_shader *s);
+bool dxil_nir_fix_io_uint_type(nir_shader *s, uint64_t in_mask, uint64_t out_mask);
 
 #ifdef __cplusplus
 }
diff --git a/src/microsoft/compiler/nir_to_dxil.c b/src/microsoft/compiler/nir_to_dxil.c
index e325f2a42e7..095d9f35115 100644
--- a/src/microsoft/compiler/nir_to_dxil.c
+++ b/src/microsoft/compiler/nir_to_dxil.c
@@ -5727,6 +5727,18 @@ nir_to_dxil(struct nir_shader *s, const struct nir_to_dxil_options *opts,
    ctx->mod.major_version = 6;
    ctx->mod.minor_version = 1;
 
+   if (s->info.stage <= MESA_SHADER_FRAGMENT) {
+      uint64_t in_mask =
+         s->info.stage == MESA_SHADER_VERTEX ?
+         0 : (VARYING_BIT_PRIMITIVE_ID | VARYING_BIT_VIEWPORT);
+      uint64_t out_mask =
+         s->info.stage == MESA_SHADER_FRAGMENT ?
+         ((1ull << FRAG_RESULT_STENCIL) | (1ull << FRAG_RESULT_SAMPLE_MASK)) :
+         (VARYING_BIT_PRIMITIVE_ID | VARYING_BIT_VIEWPORT);
+
+      NIR_PASS_V(s, dxil_nir_fix_io_uint_type, in_mask, out_mask);
+   }
+
    NIR_PASS_V(s, dxil_nir_lower_fquantize2f16);
    NIR_PASS_V(s, nir_lower_frexp);
    NIR_PASS_V(s, nir_lower_flrp, 16 | 32 | 64, true);
diff --git a/src/microsoft/spirv_to_dxil/dxil_spirv_nir.c b/src/microsoft/spirv_to_dxil/dxil_spirv_nir.c
index 5f0f8a5994f..0647ce4e4f1 100644
--- a/src/microsoft/spirv_to_dxil/dxil_spirv_nir.c
+++ b/src/microsoft/spirv_to_dxil/dxil_spirv_nir.c
@@ -475,41 +475,6 @@ dxil_spirv_nir_discard_point_size_var(nir_shader *shader)
    return true;
 }
 
-static bool
-fix_sample_mask_type(struct nir_builder *builder, nir_instr *instr,
-                     void *cb_data)
-{
-   if (instr->type != nir_instr_type_deref)
-      return false;
-
-   nir_deref_instr *deref = nir_instr_as_deref(instr);
-   nir_variable *var =
-      deref->deref_type == nir_deref_type_var ? deref->var : NULL;
-
-   if (!var || var->data.mode != nir_var_shader_out ||
-       var->data.location != FRAG_RESULT_SAMPLE_MASK ||
-       deref->type == glsl_uint_type())
-      return false;
-
-   assert(glsl_without_array(deref->type) == glsl_int_type());
-   deref->type = glsl_uint_type();
-   return true;
-}
-
-static bool
-dxil_spirv_nir_fix_sample_mask_type(nir_shader *shader)
-{
-   nir_foreach_variable_with_modes(var, shader, nir_var_shader_out) {
-      if (var->data.location == FRAG_RESULT_SAMPLE_MASK &&
-          var->type != glsl_uint_type()) {
-         var->type = glsl_uint_type();
-      }
-   }
-
-   return nir_shader_instructions_pass(shader, fix_sample_mask_type,
-                                       nir_metadata_all, NULL);
-}
-
 static bool
 kill_undefined_varyings(struct nir_builder *b,
                         nir_instr *instr,
@@ -813,7 +778,6 @@ dxil_spirv_nir_passes(nir_shader *nir,
    };
    NIR_PASS_V(nir, nir_lower_tex, &lower_tex_options);
 
-   NIR_PASS_V(nir, dxil_spirv_nir_fix_sample_mask_type);
    NIR_PASS_V(nir, dxil_nir_lower_atomics_to_dxil);
    NIR_PASS_V(nir, dxil_nir_split_clip_cull_distance);
    NIR_PASS_V(nir, dxil_nir_lower_loads_stores_to_dxil);



More information about the mesa-commit mailing list