• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
34 static bool
lower_clc_call_instr(nir_instr * instr,nir_builder * b,const nir_shader * clc_shader,struct hash_table * copy_vars)35 lower_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    nir_foreach_function(function, clc_shader) {
42       if (strcmp(function->name, call->callee->name) == 0) {
43          func = function;
44          break;
45       }
46    }
47    if (!func || !func->impl) {
48       return false;
49    }
50 
51    nir_ssa_def **params = rzalloc_array(b->shader, nir_ssa_def*, call->num_params);
52 
53    for (unsigned i = 0; i < call->num_params; i++) {
54       params[i] = nir_ssa_for_src(b, call->params[i],
55                                   call->callee->params[i].num_components);
56    }
57 
58    b->cursor = nir_instr_remove(&call->instr);
59    nir_inline_function_impl(b, func->impl, params, copy_vars);
60 
61    ralloc_free(params);
62 
63    return true;
64 }
65 
66 static bool
nir_lower_libclc_impl(nir_function_impl * impl,const nir_shader * clc_shader,struct hash_table * copy_vars)67 nir_lower_libclc_impl(nir_function_impl *impl,
68                       const nir_shader *clc_shader,
69                       struct hash_table *copy_vars)
70 {
71    nir_builder b;
72    nir_builder_init(&b, impl);
73 
74    bool progress = false;
75    nir_foreach_block_safe(block, impl) {
76       nir_foreach_instr_safe(instr, block) {
77          if (instr->type == nir_instr_type_call)
78             progress |= lower_clc_call_instr(instr, &b, clc_shader, copy_vars);
79       }
80    }
81 
82    if (progress) {
83       nir_index_ssa_defs(impl);
84       nir_index_local_regs(impl);
85       nir_metadata_preserve(impl, nir_metadata_none);
86    } else {
87       nir_metadata_preserve(impl, nir_metadata_all);
88    }
89 
90    return progress;
91 }
92 
93 bool
nir_lower_libclc(nir_shader * shader,const nir_shader * clc_shader)94 nir_lower_libclc(nir_shader *shader,
95                  const nir_shader *clc_shader)
96 {
97    void *ra_ctx = ralloc_context(NULL);
98    struct hash_table *copy_vars = _mesa_pointer_hash_table_create(ra_ctx);
99    bool progress = false, overall_progress = false;
100 
101    /* do progress passes inside the pass */
102    do {
103       progress = false;
104       nir_foreach_function(function, shader) {
105          if (function->impl)
106             progress |= nir_lower_libclc_impl(function->impl, clc_shader, copy_vars);
107       }
108       overall_progress |= progress;
109    } while (progress);
110 
111    ralloc_free(ra_ctx);
112 
113    return overall_progress;
114 }
115