• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 static bool
is_not_xfb_output(nir_variable * var,void * data)69 is_not_xfb_output(nir_variable *var, void *data)
70 {
71    if (var->data.mode != nir_var_shader_out)
72       return true;
73 
74    return !var->data.explicit_xfb_buffer &&
75           !var->data.explicit_xfb_stride;
76 }
77 
78 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)79 vk_spirv_to_nir(struct vk_device *device,
80                 const uint32_t *spirv_data, size_t spirv_size_B,
81                 gl_shader_stage stage, const char *entrypoint_name,
82                 enum gl_subgroup_size subgroup_size,
83                 const VkSpecializationInfo *spec_info,
84                 const struct spirv_to_nir_options *spirv_options,
85                 const struct nir_shader_compiler_options *nir_options,
86                 void *mem_ctx)
87 {
88    assert(spirv_size_B >= 4 && spirv_size_B % 4 == 0);
89    assert(spirv_data[0] == SPIR_V_MAGIC_NUMBER);
90 
91    struct spirv_to_nir_options spirv_options_local = *spirv_options;
92    spirv_options_local.debug.func = spirv_nir_debug;
93    spirv_options_local.debug.private_data = (void *)device;
94    spirv_options_local.subgroup_size = subgroup_size;
95 
96    uint32_t num_spec_entries = 0;
97    struct nir_spirv_specialization *spec_entries =
98       vk_spec_info_to_nir_spirv(spec_info, &num_spec_entries);
99 
100    nir_shader *nir = spirv_to_nir(spirv_data, spirv_size_B / 4,
101                                   spec_entries, num_spec_entries,
102                                   stage, entrypoint_name,
103                                   &spirv_options_local, nir_options);
104    free(spec_entries);
105 
106    if (nir == NULL)
107       return NULL;
108 
109    assert(nir->info.stage == stage);
110    nir_validate_shader(nir, "after spirv_to_nir");
111    nir_validate_ssa_dominance(nir, "after spirv_to_nir");
112    if (mem_ctx != NULL)
113       ralloc_steal(mem_ctx, nir);
114 
115    /* We have to lower away local constant initializers right before we
116     * inline functions.  That way they get properly initialized at the top
117     * of the function and not at the top of its caller.
118     */
119    NIR_PASS_V(nir, nir_lower_variable_initializers, nir_var_function_temp);
120    NIR_PASS_V(nir, nir_lower_returns);
121    NIR_PASS_V(nir, nir_inline_functions);
122    NIR_PASS_V(nir, nir_copy_prop);
123    NIR_PASS_V(nir, nir_opt_deref);
124 
125    /* Pick off the single entrypoint that we want */
126    nir_remove_non_entrypoints(nir);
127 
128    /* Now that we've deleted all but the main function, we can go ahead and
129     * lower the rest of the constant initializers.  We do this here so that
130     * nir_remove_dead_variables and split_per_member_structs below see the
131     * corresponding stores.
132     */
133    NIR_PASS_V(nir, nir_lower_variable_initializers, ~0);
134 
135    /* Split member structs.  We do this before lower_io_to_temporaries so that
136     * it doesn't lower system values to temporaries by accident.
137     */
138    NIR_PASS_V(nir, nir_split_var_copies);
139    NIR_PASS_V(nir, nir_split_per_member_structs);
140 
141    nir_remove_dead_variables_options dead_vars_opts = {
142       .can_remove_var = is_not_xfb_output,
143    };
144    NIR_PASS_V(nir, nir_remove_dead_variables,
145               nir_var_shader_in | nir_var_shader_out | nir_var_system_value |
146               nir_var_shader_call_data | nir_var_ray_hit_attrib,
147               &dead_vars_opts);
148 
149    /* This needs to happen after remove_dead_vars because GLSLang likes to
150     * insert dead clip/cull vars and we don't want to clip/cull based on
151     * uninitialized garbage.
152     */
153    NIR_PASS_V(nir, nir_lower_clip_cull_distance_arrays);
154 
155    if (nir->info.stage == MESA_SHADER_VERTEX ||
156        nir->info.stage == MESA_SHADER_TESS_EVAL ||
157        nir->info.stage == MESA_SHADER_GEOMETRY)
158       NIR_PASS_V(nir, nir_shader_gather_xfb_info);
159 
160    NIR_PASS_V(nir, nir_propagate_invariant, false);
161 
162    return nir;
163 }
164