1/*
2 * Copyright © 2019 Red Hat
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 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * 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 NONINFRINGEMENT.  IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21 * IN THE SOFTWARE.
22 *
23 */
24
25/* Pass to find libclc functions from a clc library shader and inline
26 * them into a user shader.
27 * This pass should only be called once, but it also has to iterate
28 * itself to make sure all instances are lowered, before validation.
29 */
30#include "nir.h"
31#include "nir_builder.h"
32#include "nir_spirv.h"
33
34static bool
35lower_clc_call_instr(nir_instr *instr, nir_builder *b,
36                     const nir_shader *clc_shader,
37                     struct hash_table *copy_vars)
38{
39   nir_call_instr *call = nir_instr_as_call(instr);
40   nir_function *func = NULL;
41
42   if (!call->callee->name)
43      return false;
44
45   nir_foreach_function(function, clc_shader) {
46      if (strcmp(function->name, call->callee->name) == 0) {
47         func = function;
48         break;
49      }
50   }
51   if (!func || !func->impl) {
52      return false;
53   }
54
55   nir_ssa_def **params = rzalloc_array(b->shader, nir_ssa_def*, call->num_params);
56
57   for (unsigned i = 0; i < call->num_params; i++) {
58      params[i] = nir_ssa_for_src(b, call->params[i],
59                                  call->callee->params[i].num_components);
60   }
61
62   b->cursor = nir_instr_remove(&call->instr);
63   nir_inline_function_impl(b, func->impl, params, copy_vars);
64
65   ralloc_free(params);
66
67   return true;
68}
69
70static bool
71nir_lower_libclc_impl(nir_function_impl *impl,
72                      const nir_shader *clc_shader,
73                      struct hash_table *copy_vars)
74{
75   nir_builder b;
76   nir_builder_init(&b, impl);
77
78   bool progress = false;
79   nir_foreach_block_safe(block, impl) {
80      nir_foreach_instr_safe(instr, block) {
81         if (instr->type == nir_instr_type_call)
82            progress |= lower_clc_call_instr(instr, &b, clc_shader, copy_vars);
83      }
84   }
85
86   if (progress) {
87      nir_index_ssa_defs(impl);
88      nir_index_local_regs(impl);
89      nir_metadata_preserve(impl, nir_metadata_none);
90   } else {
91      nir_metadata_preserve(impl, nir_metadata_all);
92   }
93
94   return progress;
95}
96
97bool
98nir_lower_libclc(nir_shader *shader,
99                 const nir_shader *clc_shader)
100{
101   void *ra_ctx = ralloc_context(NULL);
102   struct hash_table *copy_vars = _mesa_pointer_hash_table_create(ra_ctx);
103   bool progress = false, overall_progress = false;
104
105   /* do progress passes inside the pass */
106   do {
107      progress = false;
108      nir_foreach_function(function, shader) {
109         if (function->impl)
110            progress |= nir_lower_libclc_impl(function->impl, clc_shader, copy_vars);
111      }
112      overall_progress |= progress;
113   } while (progress);
114
115   ralloc_free(ra_ctx);
116
117   return overall_progress;
118}
119