xref: /aosp_15_r20/external/mesa3d/src/vulkan/runtime/vk_nir.c (revision 6104692788411f58d303aa86923a9ff6ecaded22)
1 /*
2  * Copyright © 2015 Intel Corporation
3  * Copyright © 2022 Collabora, LTD
4  *
5  * Permission is hereby granted, free of charge, to any person obtaining a
6  * copy of this software and associated documentation files (the "Software"),
7  * to deal in the Software without restriction, including without limitation
8  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9  * and/or sell copies of the Software, and to permit persons to whom the
10  * Software is furnished to do so, subject to the following conditions:
11  *
12  * The above copyright notice and this permission notice (including the next
13  * paragraph) shall be included in all copies or substantial portions of the
14  * Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
19  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
22  * IN THE SOFTWARE.
23  */
24 
25 #include "vk_nir.h"
26 
27 #include "compiler/nir/nir_xfb_info.h"
28 #include "compiler/spirv/nir_spirv.h"
29 #include "vk_device.h"
30 #include "vk_log.h"
31 #include "vk_physical_device.h"
32 #include "vk_util.h"
33 
34 #define SPIR_V_MAGIC_NUMBER 0x07230203
35 
36 uint32_t
vk_spirv_version(const uint32_t * spirv_data,size_t spirv_size_B)37 vk_spirv_version(const uint32_t *spirv_data, size_t spirv_size_B)
38 {
39    assert(spirv_size_B >= 8);
40    assert(spirv_data[0] == SPIR_V_MAGIC_NUMBER);
41    return spirv_data[1];
42 }
43 
44 static void
spirv_nir_debug(void * private_data,enum nir_spirv_debug_level level,size_t spirv_offset,const char * message)45 spirv_nir_debug(void *private_data,
46                 enum nir_spirv_debug_level level,
47                 size_t spirv_offset,
48                 const char *message)
49 {
50    const struct vk_object_base *log_obj = private_data;
51 
52    switch (level) {
53    case NIR_SPIRV_DEBUG_LEVEL_INFO:
54       //vk_logi(VK_LOG_OBJS(log_obj), "SPIR-V offset %lu: %s",
55       //        (unsigned long) spirv_offset, message);
56       break;
57    case NIR_SPIRV_DEBUG_LEVEL_WARNING:
58       vk_logw(VK_LOG_OBJS(log_obj), "SPIR-V offset %lu: %s",
59               (unsigned long) spirv_offset, message);
60       break;
61    case NIR_SPIRV_DEBUG_LEVEL_ERROR:
62       vk_loge(VK_LOG_OBJS(log_obj), "SPIR-V offset %lu: %s",
63               (unsigned long) spirv_offset, message);
64       break;
65    default:
66       break;
67    }
68 }
69 
70 bool
nir_vk_is_not_xfb_output(nir_variable * var,void * data)71 nir_vk_is_not_xfb_output(nir_variable *var, void *data)
72 {
73    if (var->data.mode != nir_var_shader_out)
74       return true;
75 
76    /* From the Vulkan 1.3.259 spec:
77     *
78     *    VUID-StandaloneSpirv-Offset-04716
79     *
80     *    "Only variables or block members in the output interface decorated
81     *    with Offset can be captured for transform feedback, and those
82     *    variables or block members must also be decorated with XfbBuffer
83     *    and XfbStride, or inherit XfbBuffer and XfbStride decorations from
84     *    a block containing them"
85     *
86     * glslang generates gl_PerVertex builtins when they are not declared,
87     * enabled XFB should not prevent them from being DCE'd.
88     *
89     * The logic should match nir_gather_xfb_info_with_varyings
90     */
91 
92    if (!var->data.explicit_xfb_buffer)
93       return true;
94 
95    bool is_array_block = var->interface_type != NULL &&
96       glsl_type_is_array(var->type) &&
97       glsl_without_array(var->type) == var->interface_type;
98 
99    if (!is_array_block) {
100       return !var->data.explicit_offset;
101    } else {
102       /* For array of blocks we have to check each element */
103       unsigned aoa_size = glsl_get_aoa_size(var->type);
104       const struct glsl_type *itype = var->interface_type;
105       unsigned nfields = glsl_get_length(itype);
106       for (unsigned b = 0; b < aoa_size; b++) {
107          for (unsigned f = 0; f < nfields; f++) {
108             if (glsl_get_struct_field_offset(itype, f) >= 0)
109                return false;
110          }
111       }
112 
113       return true;
114    }
115 }
116 
117 nir_shader *
vk_spirv_to_nir(struct vk_device * device,const uint32_t * spirv_data,size_t spirv_size_B,gl_shader_stage stage,const char * entrypoint_name,enum gl_subgroup_size subgroup_size,const VkSpecializationInfo * spec_info,const struct spirv_to_nir_options * spirv_options,const struct nir_shader_compiler_options * nir_options,bool internal,void * mem_ctx)118 vk_spirv_to_nir(struct vk_device *device,
119                 const uint32_t *spirv_data, size_t spirv_size_B,
120                 gl_shader_stage stage, const char *entrypoint_name,
121                 enum gl_subgroup_size subgroup_size,
122                 const VkSpecializationInfo *spec_info,
123                 const struct spirv_to_nir_options *spirv_options,
124                 const struct nir_shader_compiler_options *nir_options,
125                 bool internal,
126                 void *mem_ctx)
127 {
128    assert(spirv_size_B >= 4 && spirv_size_B % 4 == 0);
129    assert(spirv_data[0] == SPIR_V_MAGIC_NUMBER);
130 
131    const struct spirv_capabilities spirv_caps =
132       vk_physical_device_get_spirv_capabilities(device->physical);
133 
134    struct spirv_to_nir_options spirv_options_local = *spirv_options;
135    spirv_options_local.capabilities = &spirv_caps;
136    spirv_options_local.debug.func = spirv_nir_debug;
137    spirv_options_local.debug.private_data = (void *)device;
138    spirv_options_local.subgroup_size = subgroup_size;
139 
140    uint32_t num_spec_entries = 0;
141    struct nir_spirv_specialization *spec_entries =
142       vk_spec_info_to_nir_spirv(spec_info, &num_spec_entries);
143 
144    nir_shader *nir = spirv_to_nir(spirv_data, spirv_size_B / 4,
145                                   spec_entries, num_spec_entries,
146                                   stage, entrypoint_name,
147                                   &spirv_options_local, nir_options);
148    free(spec_entries);
149 
150    if (nir == NULL)
151       return NULL;
152 
153    assert(nir->info.stage == stage);
154    nir_validate_shader(nir, "after spirv_to_nir");
155    nir_validate_ssa_dominance(nir, "after spirv_to_nir");
156    if (mem_ctx != NULL)
157       ralloc_steal(mem_ctx, nir);
158 
159    nir->info.internal = internal;
160 
161    /* We have to lower away local constant initializers right before we
162     * inline functions.  That way they get properly initialized at the top
163     * of the function and not at the top of its caller.
164     */
165    NIR_PASS_V(nir, nir_lower_variable_initializers, nir_var_function_temp);
166    NIR_PASS_V(nir, nir_lower_returns);
167    NIR_PASS_V(nir, nir_inline_functions);
168    NIR_PASS_V(nir, nir_copy_prop);
169    NIR_PASS_V(nir, nir_opt_deref);
170 
171    /* Pick off the single entrypoint that we want */
172    nir_remove_non_entrypoints(nir);
173 
174    /* Now that we've deleted all but the main function, we can go ahead and
175     * lower the rest of the constant initializers.  We do this here so that
176     * nir_remove_dead_variables and split_per_member_structs below see the
177     * corresponding stores.
178     */
179    NIR_PASS_V(nir, nir_lower_variable_initializers, ~0);
180 
181    /* Split member structs.  We do this before lower_io_to_temporaries so that
182     * it doesn't lower system values to temporaries by accident.
183     */
184    NIR_PASS_V(nir, nir_split_var_copies);
185    NIR_PASS_V(nir, nir_split_per_member_structs);
186 
187    nir_remove_dead_variables_options dead_vars_opts = {
188       .can_remove_var = nir_vk_is_not_xfb_output,
189    };
190    NIR_PASS_V(nir, nir_remove_dead_variables,
191               nir_var_shader_in | nir_var_shader_out | nir_var_system_value |
192               nir_var_shader_call_data | nir_var_ray_hit_attrib,
193               &dead_vars_opts);
194 
195    /* This needs to happen after remove_dead_vars because GLSLang likes to
196     * insert dead clip/cull vars and we don't want to clip/cull based on
197     * uninitialized garbage.
198     */
199    NIR_PASS_V(nir, nir_lower_clip_cull_distance_arrays);
200 
201    if (nir->info.stage == MESA_SHADER_VERTEX ||
202        nir->info.stage == MESA_SHADER_TESS_EVAL ||
203        nir->info.stage == MESA_SHADER_GEOMETRY)
204       NIR_PASS_V(nir, nir_shader_gather_xfb_info);
205 
206    NIR_PASS_V(nir, nir_propagate_invariant, false);
207 
208    return nir;
209 }
210