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@microsoft.com>
Part-of: <https://gitlab.freedesktop.org/mesa/mesa/-/merge_requests/17039>
This commit is contained in:
Boris Brezillon 2022-06-15 07:03:55 -07:00 committed by Marge Bot
parent c9b2c79d16
commit afb64e10c1
6 changed files with 74 additions and 98 deletions

View file

@ -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);

View file

@ -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) {

View file

@ -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;
}

View file

@ -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
}

View file

@ -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);

View file

@ -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);