1 /*
2 * Copyright © 2016 Red Hat.
3 * Copyright © 2016 Bas Nieuwenhuizen
4 * Copyright © 2023 Valve Corporation
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the "Software"),
8 * to deal in the Software without restriction, including without limitation
9 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 * and/or sell copies of the Software, and to permit persons to whom the
11 * Software is furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice (including the next
14 * paragraph) shall be included in all copies or substantial portions of the
15 * Software.
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
22 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
23 * IN THE SOFTWARE.
24 */
25
26 #include "nir.h"
27 #include "nir_builder.h"
28 #include "radv_nir.h"
29 #include "radv_private.h"
30
31 bool
radv_nir_lower_intrinsics_early(nir_shader * nir,bool lower_view_index_to_zero)32 radv_nir_lower_intrinsics_early(nir_shader *nir, bool lower_view_index_to_zero)
33 {
34 nir_function_impl *entry = nir_shader_get_entrypoint(nir);
35 bool progress = false;
36 nir_builder b = nir_builder_create(entry);
37
38 nir_foreach_block (block, entry) {
39 nir_foreach_instr_safe (instr, block) {
40 if (instr->type != nir_instr_type_intrinsic)
41 continue;
42
43 nir_intrinsic_instr *intrin = nir_instr_as_intrinsic(instr);
44 b.cursor = nir_before_instr(&intrin->instr);
45
46 nir_def *def = NULL;
47 switch (intrin->intrinsic) {
48 case nir_intrinsic_is_sparse_texels_resident:
49 def = nir_ieq_imm(&b, intrin->src[0].ssa, 0);
50 break;
51 case nir_intrinsic_sparse_residency_code_and:
52 def = nir_ior(&b, intrin->src[0].ssa, intrin->src[1].ssa);
53 break;
54 case nir_intrinsic_load_view_index:
55 if (!lower_view_index_to_zero)
56 continue;
57 def = nir_imm_zero(&b, 1, 32);
58 break;
59 default:
60 continue;
61 }
62
63 nir_def_rewrite_uses(&intrin->def, def);
64
65 nir_instr_remove(instr);
66 progress = true;
67 }
68 }
69
70 if (progress)
71 nir_metadata_preserve(entry, nir_metadata_block_index | nir_metadata_dominance);
72 else
73 nir_metadata_preserve(entry, nir_metadata_all);
74
75 return progress;
76 }
77