"Fossies" - the Fresh Open Source Software Archive

Member "mesa-20.1.8/src/gallium/drivers/zink/zink_compiler.c" (16 Sep 2020, 10538 Bytes) of package /linux/misc/mesa-20.1.8.tar.xz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "zink_compiler.c" see the Fossies "Dox" file reference documentation and the last Fossies "Diffs" side-by-side code changes report: 20.1.5_vs_20.2.0-rc1.

    1 /*
    2  * Copyright 2018 Collabora Ltd.
    3  *
    4  * Permission is hereby granted, free of charge, to any person obtaining a
    5  * copy of this software and associated documentation files (the "Software"),
    6  * to deal in the Software without restriction, including without limitation
    7  * on the rights to use, copy, modify, merge, publish, distribute, sub
    8  * license, and/or sell copies of the Software, and to permit persons to whom
    9  * the Software is furnished to do so, subject to the following conditions:
   10  *
   11  * The above copyright notice and this permission notice (including the next
   12  * paragraph) shall be included in all copies or substantial portions of the
   13  * Software.
   14  *
   15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
   16  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
   17  * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
   18  * THE AUTHOR(S) AND/OR THEIR SUPPLIERS BE LIABLE FOR ANY CLAIM,
   19  * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
   20  * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
   21  * USE OR OTHER DEALINGS IN THE SOFTWARE.
   22  */
   23 
   24 #include "zink_compiler.h"
   25 #include "zink_screen.h"
   26 #include "nir_to_spirv/nir_to_spirv.h"
   27 
   28 #include "pipe/p_state.h"
   29 
   30 #include "nir.h"
   31 #include "compiler/nir/nir_builder.h"
   32 
   33 #include "nir/tgsi_to_nir.h"
   34 #include "tgsi/tgsi_dump.h"
   35 #include "tgsi/tgsi_from_mesa.h"
   36 
   37 #include "util/u_memory.h"
   38 
   39 static bool
   40 lower_instr(nir_intrinsic_instr *instr, nir_builder *b)
   41 {
   42    b->cursor = nir_before_instr(&instr->instr);
   43 
   44    if (instr->intrinsic == nir_intrinsic_load_ubo) {
   45       nir_ssa_def *old_idx = nir_ssa_for_src(b, instr->src[0], 1);
   46       nir_ssa_def *new_idx = nir_iadd(b, old_idx, nir_imm_int(b, 1));
   47       nir_instr_rewrite_src(&instr->instr, &instr->src[0],
   48                             nir_src_for_ssa(new_idx));
   49       return true;
   50    }
   51 
   52    if (instr->intrinsic == nir_intrinsic_load_uniform) {
   53       nir_ssa_def *ubo_idx = nir_imm_int(b, 0);
   54       nir_ssa_def *ubo_offset =
   55          nir_iadd(b, nir_imm_int(b, nir_intrinsic_base(instr)),
   56                   nir_ssa_for_src(b, instr->src[0], 1));
   57 
   58       nir_intrinsic_instr *load =
   59          nir_intrinsic_instr_create(b->shader, nir_intrinsic_load_ubo);
   60       load->num_components = instr->num_components;
   61       load->src[0] = nir_src_for_ssa(ubo_idx);
   62       load->src[1] = nir_src_for_ssa(ubo_offset);
   63       assert(instr->dest.ssa.bit_size >= 8);
   64       nir_intrinsic_set_align(load, instr->dest.ssa.bit_size / 8, 0);
   65       nir_ssa_dest_init(&load->instr, &load->dest,
   66                         load->num_components, instr->dest.ssa.bit_size,
   67                         instr->dest.ssa.name);
   68       nir_builder_instr_insert(b, &load->instr);
   69       nir_ssa_def_rewrite_uses(&instr->dest.ssa, nir_src_for_ssa(&load->dest.ssa));
   70 
   71       nir_instr_remove(&instr->instr);
   72       return true;
   73    }
   74 
   75    return false;
   76 }
   77 
   78 static bool
   79 lower_uniforms_to_ubo(nir_shader *shader)
   80 {
   81    bool progress = false;
   82 
   83    nir_foreach_function(function, shader) {
   84       if (function->impl) {
   85          nir_builder builder;
   86          nir_builder_init(&builder, function->impl);
   87          nir_foreach_block(block, function->impl) {
   88             nir_foreach_instr_safe(instr, block) {
   89                if (instr->type == nir_instr_type_intrinsic)
   90                   progress |= lower_instr(nir_instr_as_intrinsic(instr),
   91                                           &builder);
   92             }
   93          }
   94 
   95          nir_metadata_preserve(function->impl, nir_metadata_block_index |
   96                                                nir_metadata_dominance);
   97       }
   98    }
   99 
  100    if (progress) {
  101       assert(shader->num_uniforms > 0);
  102       const struct glsl_type *type = glsl_array_type(glsl_vec4_type(),
  103                                                      shader->num_uniforms, 0);
  104       nir_variable *ubo = nir_variable_create(shader, nir_var_mem_ubo, type,
  105                                               "uniform_0");
  106       ubo->data.binding = 0;
  107 
  108       struct glsl_struct_field field = {
  109          .type = type,
  110          .name = "data",
  111          .location = -1,
  112       };
  113       ubo->interface_type =
  114             glsl_interface_type(&field, 1, GLSL_INTERFACE_PACKING_STD430,
  115                                 false, "__ubo0_interface");
  116    }
  117 
  118    return progress;
  119 }
  120 
  121 static bool
  122 lower_discard_if_instr(nir_intrinsic_instr *instr, nir_builder *b)
  123 {
  124    if (instr->intrinsic == nir_intrinsic_discard_if) {
  125       b->cursor = nir_before_instr(&instr->instr);
  126 
  127       nir_if *if_stmt = nir_push_if(b, nir_ssa_for_src(b, instr->src[0], 1));
  128       nir_intrinsic_instr *discard =
  129          nir_intrinsic_instr_create(b->shader, nir_intrinsic_discard);
  130       nir_builder_instr_insert(b, &discard->instr);
  131       nir_pop_if(b, if_stmt);
  132       nir_instr_remove(&instr->instr);
  133       return true;
  134    }
  135    assert(instr->intrinsic != nir_intrinsic_discard ||
  136           nir_block_last_instr(instr->instr.block) == &instr->instr);
  137 
  138    return false;
  139 }
  140 
  141 static bool
  142 lower_discard_if(nir_shader *shader)
  143 {
  144    bool progress = false;
  145 
  146    nir_foreach_function(function, shader) {
  147       if (function->impl) {
  148          nir_builder builder;
  149          nir_builder_init(&builder, function->impl);
  150          nir_foreach_block(block, function->impl) {
  151             nir_foreach_instr_safe(instr, block) {
  152                if (instr->type == nir_instr_type_intrinsic)
  153                   progress |= lower_discard_if_instr(
  154                                                   nir_instr_as_intrinsic(instr),
  155                                                   &builder);
  156             }
  157          }
  158 
  159          nir_metadata_preserve(function->impl, nir_metadata_dominance);
  160       }
  161    }
  162 
  163    return progress;
  164 }
  165 
  166 static const struct nir_shader_compiler_options nir_options = {
  167    .lower_all_io_to_temps = true,
  168    .lower_ffma = true,
  169    .lower_fdph = true,
  170    .lower_flrp32 = true,
  171    .lower_fpow = true,
  172    .lower_fsat = true,
  173 };
  174 
  175 const void *
  176 zink_get_compiler_options(struct pipe_screen *screen,
  177                           enum pipe_shader_ir ir,
  178                           enum pipe_shader_type shader)
  179 {
  180    assert(ir == PIPE_SHADER_IR_NIR);
  181    return &nir_options;
  182 }
  183 
  184 struct nir_shader *
  185 zink_tgsi_to_nir(struct pipe_screen *screen, const struct tgsi_token *tokens)
  186 {
  187    if (zink_debug & ZINK_DEBUG_TGSI) {
  188       fprintf(stderr, "TGSI shader:\n---8<---\n");
  189       tgsi_dump_to_file(tokens, 0, stderr);
  190       fprintf(stderr, "---8<---\n\n");
  191    }
  192 
  193    return tgsi_to_nir(tokens, screen);
  194 }
  195 
  196 static void
  197 optimize_nir(struct nir_shader *s)
  198 {
  199    bool progress;
  200    do {
  201       progress = false;
  202       NIR_PASS_V(s, nir_lower_vars_to_ssa);
  203       NIR_PASS(progress, s, nir_copy_prop);
  204       NIR_PASS(progress, s, nir_opt_remove_phis);
  205       NIR_PASS(progress, s, nir_opt_dce);
  206       NIR_PASS(progress, s, nir_opt_dead_cf);
  207       NIR_PASS(progress, s, nir_opt_cse);
  208       NIR_PASS(progress, s, nir_opt_peephole_select, 8, true, true);
  209       NIR_PASS(progress, s, nir_opt_algebraic);
  210       NIR_PASS(progress, s, nir_opt_constant_folding);
  211       NIR_PASS(progress, s, nir_opt_undef);
  212       NIR_PASS(progress, s, zink_nir_lower_b2b);
  213    } while (progress);
  214 }
  215 
  216 struct zink_shader *
  217 zink_compile_nir(struct zink_screen *screen, struct nir_shader *nir)
  218 {
  219    struct zink_shader *ret = CALLOC_STRUCT(zink_shader);
  220 
  221    NIR_PASS_V(nir, lower_uniforms_to_ubo);
  222    NIR_PASS_V(nir, nir_lower_clip_halfz);
  223    NIR_PASS_V(nir, nir_lower_regs_to_ssa);
  224    optimize_nir(nir);
  225    NIR_PASS_V(nir, nir_remove_dead_variables, nir_var_function_temp);
  226    NIR_PASS_V(nir, lower_discard_if);
  227    NIR_PASS_V(nir, nir_convert_from_ssa, true);
  228 
  229    if (zink_debug & ZINK_DEBUG_NIR) {
  230       fprintf(stderr, "NIR shader:\n---8<---\n");
  231       nir_print_shader(nir, stderr);
  232       fprintf(stderr, "---8<---\n");
  233    }
  234 
  235    ret->num_bindings = 0;
  236    nir_foreach_variable(var, &nir->uniforms) {
  237       if (var->data.mode == nir_var_mem_ubo) {
  238          int binding = zink_binding(nir->info.stage,
  239                                     VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER,
  240                                     var->data.binding);
  241          ret->bindings[ret->num_bindings].index = var->data.binding;
  242          ret->bindings[ret->num_bindings].binding = binding;
  243          ret->bindings[ret->num_bindings].type = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER;
  244          ret->num_bindings++;
  245       } else {
  246          assert(var->data.mode == nir_var_uniform);
  247          if (glsl_type_is_array(var->type) &&
  248              glsl_type_is_sampler(glsl_get_array_element(var->type))) {
  249             for (int i = 0; i < glsl_get_length(var->type); ++i) {
  250                int binding = zink_binding(nir->info.stage,
  251                                           VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER,
  252                                           var->data.binding + i);
  253                ret->bindings[ret->num_bindings].index = var->data.binding + i;
  254                ret->bindings[ret->num_bindings].binding = binding;
  255                ret->bindings[ret->num_bindings].type = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER;
  256                ret->num_bindings++;
  257             }
  258          } else if (glsl_type_is_sampler(var->type)) {
  259             int binding = zink_binding(nir->info.stage,
  260                                        VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER,
  261                                        var->data.binding);
  262             ret->bindings[ret->num_bindings].index = var->data.binding;
  263             ret->bindings[ret->num_bindings].binding = binding;
  264             ret->bindings[ret->num_bindings].type = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER;
  265             ret->num_bindings++;
  266          }
  267       }
  268    }
  269 
  270    ret->info = nir->info;
  271 
  272    struct spirv_shader *spirv = nir_to_spirv(nir);
  273    assert(spirv);
  274 
  275    if (zink_debug & ZINK_DEBUG_SPIRV) {
  276       char buf[256];
  277       static int i;
  278       snprintf(buf, sizeof(buf), "dump%02d.spv", i++);
  279       FILE *fp = fopen(buf, "wb");
  280       if (fp) {
  281          fwrite(spirv->words, sizeof(uint32_t), spirv->num_words, fp);
  282          fclose(fp);
  283          fprintf(stderr, "wrote '%s'...\n", buf);
  284       }
  285    }
  286 
  287    VkShaderModuleCreateInfo smci = {};
  288    smci.sType = VK_STRUCTURE_TYPE_SHADER_MODULE_CREATE_INFO;
  289    smci.codeSize = spirv->num_words * sizeof(uint32_t);
  290    smci.pCode = spirv->words;
  291 
  292    if (vkCreateShaderModule(screen->dev, &smci, NULL, &ret->shader_module) != VK_SUCCESS)
  293       return NULL;
  294 
  295    return ret;
  296 }
  297 
  298 void
  299 zink_shader_free(struct zink_screen *screen, struct zink_shader *shader)
  300 {
  301    vkDestroyShaderModule(screen->dev, shader->shader_module, NULL);
  302    FREE(shader);
  303 }