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_log.h"
30 #include "vk_util.h"
31 
32 #define SPIR_V_MAGIC_NUMBER 0x07230203
33 
34 uint32_t
vk_spirv_version(const uint32_t * spirv_data,size_t spirv_size_B)35 vk_spirv_version(const uint32_t *spirv_data, size_t spirv_size_B)
36 {
37    assert(spirv_size_B >= 8);
38    assert(spirv_data[0] == SPIR_V_MAGIC_NUMBER);
39    return spirv_data[1];
40 }
41 
42 static void
spirv_nir_debug(void * private_data,enum nir_spirv_debug_level level,size_t spirv_offset,const char * message)43 spirv_nir_debug(void *private_data,
44                 enum nir_spirv_debug_level level,
45                 size_t spirv_offset,
46                 const char *message)
47 {
48    const struct vk_object_base *log_obj = private_data;
49 
50    switch (level) {
51    case NIR_SPIRV_DEBUG_LEVEL_INFO:
52       //vk_logi(VK_LOG_OBJS(log_obj), "SPIR-V offset %lu: %s",
53       //        (unsigned long) spirv_offset, message);
54       break;
55    case NIR_SPIRV_DEBUG_LEVEL_WARNING:
56       vk_logw(VK_LOG_OBJS(log_obj), "SPIR-V offset %lu: %s",
57               (unsigned long) spirv_offset, message);
58       break;
59    case NIR_SPIRV_DEBUG_LEVEL_ERROR:
60       vk_loge(VK_LOG_OBJS(log_obj), "SPIR-V offset %lu: %s",
61               (unsigned long) spirv_offset, message);
62       break;
63    default:
64       break;
65    }
66 }
67 
68 bool
nir_vk_is_not_xfb_output(nir_variable * var,void * data)69 nir_vk_is_not_xfb_output(nir_variable *var, void *data)
70 {
71    if (var->data.mode != nir_var_shader_out)
72       return true;
73 
74    /* From the Vulkan 1.3.259 spec:
75     *
76     *    VUID-StandaloneSpirv-Offset-04716
77     *
78     *    "Only variables or block members in the output interface decorated
79     *    with Offset can be captured for transform feedback, and those
80     *    variables or block members must also be decorated with XfbBuffer
81     *    and XfbStride, or inherit XfbBuffer and XfbStride decorations from
82     *    a block containing them"
83     *
84     * glslang generates gl_PerVertex builtins when they are not declared,
85     * enabled XFB should not prevent them from being DCE'd.
86     *
87     * The logic should match nir_gather_xfb_info_with_varyings
88     */
89 
90    if (!var->data.explicit_xfb_buffer)
91       return true;
92 
93    bool is_array_block = var->interface_type != NULL &&
94       glsl_type_is_array(var->type) &&
95       glsl_without_array(var->type) == var->interface_type;
96 
97    if (!is_array_block) {
98       return !var->data.explicit_offset;
99    } else {
100       /* For array of blocks we have to check each element */
101       unsigned aoa_size = glsl_get_aoa_size(var->type);
102       const struct glsl_type *itype = var->interface_type;
103       unsigned nfields = glsl_get_length(itype);
104       for (unsigned b = 0; b < aoa_size; b++) {
105          for (unsigned f = 0; f < nfields; f++) {
106             if (glsl_get_struct_field_offset(itype, f) >= 0)
107                return false;
108          }
109       }
110 
111       return true;
112    }
113 }
114 
115 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,void * mem_ctx)116 vk_spirv_to_nir(struct vk_device *device,
117                 const uint32_t *spirv_data, size_t spirv_size_B,
118                 gl_shader_stage stage, const char *entrypoint_name,
119                 enum gl_subgroup_size subgroup_size,
120                 const VkSpecializationInfo *spec_info,
121                 const struct spirv_to_nir_options *spirv_options,
122                 const struct nir_shader_compiler_options *nir_options,
123                 void *mem_ctx)
124 {
125    assert(spirv_size_B >= 4 && spirv_size_B % 4 == 0);
126    assert(spirv_data[0] == SPIR_V_MAGIC_NUMBER);
127 
128    struct spirv_to_nir_options spirv_options_local = *spirv_options;
129    spirv_options_local.debug.func = spirv_nir_debug;
130    spirv_options_local.debug.private_data = (void *)device;
131    spirv_options_local.subgroup_size = subgroup_size;
132 
133    uint32_t num_spec_entries = 0;
134    struct nir_spirv_specialization *spec_entries =
135       vk_spec_info_to_nir_spirv(spec_info, &num_spec_entries);
136 
137    nir_shader *nir = spirv_to_nir(spirv_data, spirv_size_B / 4,
138                                   spec_entries, num_spec_entries,
139                                   stage, entrypoint_name,
140                                   &spirv_options_local, nir_options);
141    free(spec_entries);
142 
143    if (nir == NULL)
144       return NULL;
145 
146    assert(nir->info.stage == stage);
147    nir_validate_shader(nir, "after spirv_to_nir");
148    nir_validate_ssa_dominance(nir, "after spirv_to_nir");
149    if (mem_ctx != NULL)
150       ralloc_steal(mem_ctx, nir);
151 
152    /* We have to lower away local constant initializers right before we
153     * inline functions.  That way they get properly initialized at the top
154     * of the function and not at the top of its caller.
155     */
156    NIR_PASS_V(nir, nir_lower_variable_initializers, nir_var_function_temp);
157    NIR_PASS_V(nir, nir_lower_returns);
158    NIR_PASS_V(nir, nir_inline_functions);
159    NIR_PASS_V(nir, nir_copy_prop);
160    NIR_PASS_V(nir, nir_opt_deref);
161 
162    /* Pick off the single entrypoint that we want */
163    nir_remove_non_entrypoints(nir);
164 
165    /* Now that we've deleted all but the main function, we can go ahead and
166     * lower the rest of the constant initializers.  We do this here so that
167     * nir_remove_dead_variables and split_per_member_structs below see the
168     * corresponding stores.
169     */
170    NIR_PASS_V(nir, nir_lower_variable_initializers, ~0);
171 
172    /* Split member structs.  We do this before lower_io_to_temporaries so that
173     * it doesn't lower system values to temporaries by accident.
174     */
175    NIR_PASS_V(nir, nir_split_var_copies);
176    NIR_PASS_V(nir, nir_split_per_member_structs);
177 
178    nir_remove_dead_variables_options dead_vars_opts = {
179       .can_remove_var = nir_vk_is_not_xfb_output,
180    };
181    NIR_PASS_V(nir, nir_remove_dead_variables,
182               nir_var_shader_in | nir_var_shader_out | nir_var_system_value |
183               nir_var_shader_call_data | nir_var_ray_hit_attrib,
184               &dead_vars_opts);
185 
186    /* This needs to happen after remove_dead_vars because GLSLang likes to
187     * insert dead clip/cull vars and we don't want to clip/cull based on
188     * uninitialized garbage.
189     */
190    NIR_PASS_V(nir, nir_lower_clip_cull_distance_arrays);
191 
192    if (nir->info.stage == MESA_SHADER_VERTEX ||
193        nir->info.stage == MESA_SHADER_TESS_EVAL ||
194        nir->info.stage == MESA_SHADER_GEOMETRY)
195       NIR_PASS_V(nir, nir_shader_gather_xfb_info);
196 
197    NIR_PASS_V(nir, nir_propagate_invariant, false);
198 
199    return nir;
200 }
201