| 1 |
/* |
| 2 |
* Copyright © 2025 Imagination Technologies Ltd. |
| 3 |
* Copyright (C) 2020-2021 Collabora, Ltd. |
| 4 |
* Copyright © 2020 Valve Corporation |
| 5 |
* |
| 6 |
* SPDX-License-Identifier: MIT |
| 7 |
*/ |
| 8 |
|
| 9 |
#include "compiler/nir/nir.h" |
| 10 |
#include "compiler/nir/nir_builder.h" |
| 11 |
#include "pco_internal.h" |
| 12 |
|
| 13 |
static nir_def *get_is_null(nir_builder *b, |
| 14 |
nir_instr *instr, |
| 15 |
nir_def **def, |
| 16 |
pco_nir_lower_null_descriptor_options options) |
| 17 |
{ |
| 18 |
bool is_deref = false; |
| 19 |
*def = NULL; |
| 20 |
|
| 21 |
if (instr->type == nir_instr_type_intrinsic) { |
| 22 |
nir_intrinsic_instr *intr = nir_instr_as_intrinsic(instr); |
| 23 |
switch (intr->intrinsic) { |
| 24 |
case nir_intrinsic_image_deref_size: |
| 25 |
case nir_intrinsic_image_deref_levels: |
| 26 |
case nir_intrinsic_image_deref_samples: |
| 27 |
case nir_intrinsic_image_deref_load: |
| 28 |
case nir_intrinsic_image_deref_store: |
| 29 |
case nir_intrinsic_image_deref_atomic: |
| 30 |
case nir_intrinsic_image_deref_atomic_swap: |
| 31 |
if (!(options & pco_nir_lower_null_descriptor_image)) |
| 32 |
return NULL; |
| 33 |
|
| 34 |
is_deref = true; |
| 35 |
break; |
| 36 |
|
| 37 |
case nir_intrinsic_load_global: |
| 38 |
case nir_intrinsic_load_global_2x32: |
| 39 |
case nir_intrinsic_load_global_constant: |
| 40 |
case nir_intrinsic_global_atomic: |
| 41 |
case nir_intrinsic_global_atomic_2x32: |
| 42 |
case nir_intrinsic_global_atomic_swap: |
| 43 |
case nir_intrinsic_global_atomic_swap_2x32: |
| 44 |
case nir_intrinsic_store_global: |
| 45 |
case nir_intrinsic_store_global_2x32: |
| 46 |
if (!(options & pco_nir_lower_null_descriptor_global)) |
| 47 |
return NULL; |
| 48 |
|
| 49 |
break; |
| 50 |
|
| 51 |
case nir_intrinsic_get_ubo_size: |
| 52 |
case nir_intrinsic_load_ubo: |
| 53 |
if (!(options & pco_nir_lower_null_descriptor_ubo)) |
| 54 |
return NULL; |
| 55 |
|
| 56 |
break; |
| 57 |
|
| 58 |
case nir_intrinsic_get_ssbo_size: |
| 59 |
case nir_intrinsic_load_ssbo: |
| 60 |
case nir_intrinsic_ssbo_atomic: |
| 61 |
case nir_intrinsic_ssbo_atomic_swap: |
| 62 |
case nir_intrinsic_store_ssbo: |
| 63 |
if (!(options & pco_nir_lower_null_descriptor_ssbo)) |
| 64 |
return NULL; |
| 65 |
|
| 66 |
break; |
| 67 |
|
| 68 |
default: |
| 69 |
return NULL; |
| 70 |
} |
| 71 |
|
| 72 |
nir_src *index = nir_get_io_index_src(intr); |
| 73 |
assert(index || is_deref); |
| 74 |
|
| 75 |
if (nir_intrinsic_infos[intr->intrinsic].has_dest) |
| 76 |
*def = &intr->def; |
| 77 |
|
| 78 |
return nir_is_null_descriptor(b, |
| 79 |
is_deref ? intr->src[0].ssa : index->ssa); |
| 80 |
} |
| 81 |
|
| 82 |
if (instr->type == nir_instr_type_tex) { |
| 83 |
if (!(options & pco_nir_lower_null_descriptor_texture)) |
| 84 |
return NULL; |
| 85 |
|
| 86 |
nir_tex_instr *tex = nir_instr_as_tex(instr); |
| 87 |
nir_def *deref_def = nir_get_tex_src(tex, nir_tex_src_texture_deref); |
| 88 |
if (!deref_def) |
| 89 |
return NULL; |
| 90 |
|
| 91 |
*def = &tex->def; |
| 92 |
return nir_is_null_descriptor(b, deref_def); |
| 93 |
} |
| 94 |
|
| 95 |
return NULL; |
| 96 |
} |
| 97 |
|
| 98 |
static bool lower(nir_builder *b, nir_instr *instr, void *data) |
| 99 |
{ |
| 100 |
pco_nir_lower_null_descriptor_options *options = data; |
| 101 |
b->cursor = nir_before_instr(instr); |
| 102 |
|
| 103 |
nir_def *def; |
| 104 |
nir_def *is_null = get_is_null(b, instr, &def, *options); |
| 105 |
|
| 106 |
if (!is_null) |
| 107 |
return false; |
| 108 |
|
| 109 |
nir_def *zero = NULL; |
| 110 |
nir_if *nif = nir_push_if(b, nir_inot(b, is_null)); |
| 111 |
nir_instr_remove(instr); |
| 112 |
nir_builder_instr_insert(b, instr); |
| 113 |
if (def) { |
| 114 |
nir_push_else(b, nif); |
| 115 |
zero = nir_imm_zero(b, def->num_components, def->bit_size); |
| 116 |
} |
| 117 |
nir_pop_if(b, nif); |
| 118 |
|
| 119 |
if (def) { |
| 120 |
nir_def *phi = nir_if_phi(b, def, zero); |
| 121 |
|
| 122 |
/* We can't use nir_def_rewrite_uses_after on phis, so use the global |
| 123 |
* version and fixup the phi manually |
| 124 |
*/ |
| 125 |
nir_def_rewrite_uses(def, phi); |
| 126 |
|
| 127 |
nir_instr *phi_instr = nir_def_instr(phi); |
| 128 |
nir_phi_instr *phi_as_phi = nir_instr_as_phi(phi_instr); |
| 129 |
nir_phi_src *phi_src = |
| 130 |
nir_phi_get_src_from_block(phi_as_phi, instr->block); |
| 131 |
nir_src_rewrite(&phi_src->src, def); |
| 132 |
} |
| 133 |
|
| 134 |
return true; |
| 135 |
} |
| 136 |
|
| 137 |
bool pco_nir_lower_null_descriptors( |
| 138 |
nir_shader *shader, |
| 139 |
pco_nir_lower_null_descriptor_options options) |
| 140 |
{ |
| 141 |
return nir_shader_instructions_pass(shader, |
| 142 |
lower, |
| 143 |
nir_metadata_none, |
| 144 |
&options); |
| 145 |
} |