17ec681f3Smrg/*
27ec681f3Smrg * Copyright © 2019 Red Hat
37ec681f3Smrg *
47ec681f3Smrg * Permission is hereby granted, free of charge, to any person obtaining a
57ec681f3Smrg * copy of this software and associated documentation files (the "Software"),
67ec681f3Smrg * to deal in the Software without restriction, including without limitation
77ec681f3Smrg * the rights to use, copy, modify, merge, publish, distribute, sublicense,
87ec681f3Smrg * and/or sell copies of the Software, and to permit persons to whom the
97ec681f3Smrg * Software is furnished to do so, subject to the following conditions:
107ec681f3Smrg *
117ec681f3Smrg * The above copyright notice and this permission notice (including the next
127ec681f3Smrg * paragraph) shall be included in all copies or substantial portions of the
137ec681f3Smrg * Software.
147ec681f3Smrg *
157ec681f3Smrg * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
167ec681f3Smrg * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
177ec681f3Smrg * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
187ec681f3Smrg * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
197ec681f3Smrg * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
207ec681f3Smrg * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
217ec681f3Smrg * IN THE SOFTWARE.
227ec681f3Smrg *
237ec681f3Smrg */
247ec681f3Smrg
257ec681f3Smrg/* Pass to find libclc functions from a clc library shader and inline
267ec681f3Smrg * them into a user shader.
277ec681f3Smrg * This pass should only be called once, but it also has to iterate
287ec681f3Smrg * itself to make sure all instances are lowered, before validation.
297ec681f3Smrg */
307ec681f3Smrg#include "nir.h"
317ec681f3Smrg#include "nir_builder.h"
327ec681f3Smrg#include "nir_spirv.h"
337ec681f3Smrg
347ec681f3Smrgstatic bool
357ec681f3Smrglower_clc_call_instr(nir_instr *instr, nir_builder *b,
367ec681f3Smrg                     const nir_shader *clc_shader,
377ec681f3Smrg                     struct hash_table *copy_vars)
387ec681f3Smrg{
397ec681f3Smrg   nir_call_instr *call = nir_instr_as_call(instr);
407ec681f3Smrg   nir_function *func = NULL;
417ec681f3Smrg
427ec681f3Smrg   if (!call->callee->name)
437ec681f3Smrg      return false;
447ec681f3Smrg
457ec681f3Smrg   nir_foreach_function(function, clc_shader) {
467ec681f3Smrg      if (strcmp(function->name, call->callee->name) == 0) {
477ec681f3Smrg         func = function;
487ec681f3Smrg         break;
497ec681f3Smrg      }
507ec681f3Smrg   }
517ec681f3Smrg   if (!func || !func->impl) {
527ec681f3Smrg      return false;
537ec681f3Smrg   }
547ec681f3Smrg
557ec681f3Smrg   nir_ssa_def **params = rzalloc_array(b->shader, nir_ssa_def*, call->num_params);
567ec681f3Smrg
577ec681f3Smrg   for (unsigned i = 0; i < call->num_params; i++) {
587ec681f3Smrg      params[i] = nir_ssa_for_src(b, call->params[i],
597ec681f3Smrg                                  call->callee->params[i].num_components);
607ec681f3Smrg   }
617ec681f3Smrg
627ec681f3Smrg   b->cursor = nir_instr_remove(&call->instr);
637ec681f3Smrg   nir_inline_function_impl(b, func->impl, params, copy_vars);
647ec681f3Smrg
657ec681f3Smrg   ralloc_free(params);
667ec681f3Smrg
677ec681f3Smrg   return true;
687ec681f3Smrg}
697ec681f3Smrg
707ec681f3Smrgstatic bool
717ec681f3Smrgnir_lower_libclc_impl(nir_function_impl *impl,
727ec681f3Smrg                      const nir_shader *clc_shader,
737ec681f3Smrg                      struct hash_table *copy_vars)
747ec681f3Smrg{
757ec681f3Smrg   nir_builder b;
767ec681f3Smrg   nir_builder_init(&b, impl);
777ec681f3Smrg
787ec681f3Smrg   bool progress = false;
797ec681f3Smrg   nir_foreach_block_safe(block, impl) {
807ec681f3Smrg      nir_foreach_instr_safe(instr, block) {
817ec681f3Smrg         if (instr->type == nir_instr_type_call)
827ec681f3Smrg            progress |= lower_clc_call_instr(instr, &b, clc_shader, copy_vars);
837ec681f3Smrg      }
847ec681f3Smrg   }
857ec681f3Smrg
867ec681f3Smrg   if (progress) {
877ec681f3Smrg      nir_index_ssa_defs(impl);
887ec681f3Smrg      nir_index_local_regs(impl);
897ec681f3Smrg      nir_metadata_preserve(impl, nir_metadata_none);
907ec681f3Smrg   } else {
917ec681f3Smrg      nir_metadata_preserve(impl, nir_metadata_all);
927ec681f3Smrg   }
937ec681f3Smrg
947ec681f3Smrg   return progress;
957ec681f3Smrg}
967ec681f3Smrg
977ec681f3Smrgbool
987ec681f3Smrgnir_lower_libclc(nir_shader *shader,
997ec681f3Smrg                 const nir_shader *clc_shader)
1007ec681f3Smrg{
1017ec681f3Smrg   void *ra_ctx = ralloc_context(NULL);
1027ec681f3Smrg   struct hash_table *copy_vars = _mesa_pointer_hash_table_create(ra_ctx);
1037ec681f3Smrg   bool progress = false, overall_progress = false;
1047ec681f3Smrg
1057ec681f3Smrg   /* do progress passes inside the pass */
1067ec681f3Smrg   do {
1077ec681f3Smrg      progress = false;
1087ec681f3Smrg      nir_foreach_function(function, shader) {
1097ec681f3Smrg         if (function->impl)
1107ec681f3Smrg            progress |= nir_lower_libclc_impl(function->impl, clc_shader, copy_vars);
1117ec681f3Smrg      }
1127ec681f3Smrg      overall_progress |= progress;
1137ec681f3Smrg   } while (progress);
1147ec681f3Smrg
1157ec681f3Smrg   ralloc_free(ra_ctx);
1167ec681f3Smrg
1177ec681f3Smrg   return overall_progress;
1187ec681f3Smrg}
119