aboutsummaryrefslogtreecommitdiffstats
path: root/src/compiler/nir/nir_opt_large_constants.c
blob: 74d9ceef87781e4c49d44c768c1c3de67e8d2b91 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
/*
 * Copyright © 2018 Intel Corporation
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the "Software"),
 * to deal in the Software without restriction, including without limitation
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
 * and/or sell copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice (including the next
 * paragraph) shall be included in all copies or substantial portions of the
 * Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
 * IN THE SOFTWARE.
 */

#include "nir.h"
#include "nir_builder.h"
#include "nir_deref.h"

struct var_info {
   bool is_constant;
   bool found_read;
};

static nir_ssa_def *
build_constant_load(nir_builder *b, nir_deref_instr *deref,
                    glsl_type_size_align_func size_align)
{
   nir_variable *var = nir_deref_instr_get_variable(deref);

   const unsigned bit_size = glsl_get_bit_size(deref->type);
   const unsigned num_components = glsl_get_vector_elements(deref->type);

   UNUSED unsigned var_size, var_align;
   size_align(var->type, &var_size, &var_align);
   assert(var->data.location % var_align == 0);

   nir_intrinsic_instr *load =
      nir_intrinsic_instr_create(b->shader, nir_intrinsic_load_constant);
   load->num_components = num_components;
   nir_intrinsic_set_base(load, var->data.location);
   nir_intrinsic_set_range(load, var_size);
   load->src[0] = nir_src_for_ssa(nir_build_deref_offset(b, deref, size_align));
   nir_ssa_dest_init(&load->instr, &load->dest,
                     num_components, bit_size, NULL);
   nir_builder_instr_insert(b, &load->instr);

   if (load->dest.ssa.bit_size < 8) {
      /* Booleans are special-cased to be 32-bit
       *
       * Ideally, for drivers that can handle 32-bit booleans, we wouldn't
       * emit the i2b here.  However, at this point, the driver is likely to
       * still have 1-bit booleans so we need to at least convert bit sizes.
       * Unfortunately, we don't have a good way to annotate the load as
       * loading a known boolean value so the optimizer isn't going to be
       * able to get rid of the conversion.  Some day, we may solve that
       * problem but not today.
       */
      assert(glsl_type_is_boolean(deref->type));
      load->dest.ssa.bit_size = 32;
      return nir_i2b(b, &load->dest.ssa);
   } else {
      return &load->dest.ssa;
   }
}

static void
handle_constant_store(nir_builder *b, nir_intrinsic_instr *store,
                      glsl_type_size_align_func size_align)
{
   nir_deref_instr *deref = nir_src_as_deref(store->src[0]);
   assert(!nir_deref_instr_has_indirect(deref));

   nir_variable *var = nir_deref_instr_get_variable(deref);

   const unsigned bit_size = glsl_get_bit_size(deref->type);
   const unsigned num_components = glsl_get_vector_elements(deref->type);

   char *dst = (char *)b->shader->constant_data +
               var->data.location +
               nir_deref_instr_get_const_offset(deref, size_align);

   nir_const_value *val = nir_src_as_const_value(store->src[1]);
   switch (bit_size) {
   case 1:
      /* Booleans are special-cased to be 32-bit */
      for (unsigned i = 0; i < num_components; i++)
         ((int32_t *)dst)[i] = -(int)val->b[i];
      break;

   case 8:
      for (unsigned i = 0; i < num_components; i++)
         ((uint8_t *)dst)[i] = val->u8[i];
      break;

   case 16:
      for (unsigned i = 0; i < num_components; i++)
         ((uint16_t *)dst)[i] = val->u16[i];
      break;

   case 32:
      for (unsigned i = 0; i < num_components; i++)
         ((uint32_t *)dst)[i] = val->u32[i];
      break;

   case 64:
      for (unsigned i = 0; i < num_components; i++)
         ((uint64_t *)dst)[i] = val->u64[i];
      break;

   default:
      unreachable("Invalid bit size");
   }
}

/** Lower large constant variables to shader constant data
 *
 * This pass looks for large (type_size(var->type) > threshold) variables
 * which are statically constant and moves them into shader constant data.
 * This is especially useful when large tables are baked into the shader
 * source code because they can be moved into a UBO by the driver to reduce
 * register pressure and make indirect access cheaper.
 */
bool
nir_opt_large_constants(nir_shader *shader,
                        glsl_type_size_align_func size_align,
                        unsigned threshold)
{
   /* Default to a natural alignment if none is provided */
   if (size_align == NULL)
      size_align = glsl_get_natural_size_align_bytes;

   /* This only works with a single entrypoint */
   nir_function_impl *impl = nir_shader_get_entrypoint(shader);

   /* This pass can only be run once */
   assert(shader->constant_data == NULL && shader->constant_data_size == 0);

   /* The index parameter is unused for local variables so we'll use it for
    * indexing into our array of variable metadata.
    */
   unsigned num_locals = 0;
   nir_foreach_variable(var, &impl->locals)
      var->data.index = num_locals++;

   struct var_info *var_infos = malloc(num_locals * sizeof(struct var_info));
   for (unsigned i = 0; i < num_locals; i++) {
      var_infos[i] = (struct var_info) {
         .is_constant = true,
         .found_read = false,
      };
   }

   /* First, walk through the shader and figure out what variables we can
    * lower to the constant blob.
    */
   bool first_block = true;
   nir_foreach_block(block, impl) {
      nir_foreach_instr(instr, block) {
         if (instr->type != nir_instr_type_intrinsic)
            continue;

         nir_intrinsic_instr *intrin = nir_instr_as_intrinsic(instr);

         bool src_is_const = false;
         nir_deref_instr *src_deref = NULL, *dst_deref = NULL;
         switch (intrin->intrinsic) {
         case nir_intrinsic_store_deref:
            dst_deref = nir_src_as_deref(intrin->src[0]);
            src_is_const = nir_src_is_const(intrin->src[1]);
            break;

         case nir_intrinsic_load_deref:
            src_deref = nir_src_as_deref(intrin->src[0]);
            break;

         case nir_intrinsic_copy_deref:
            /* We always assume the src and therefore the dst are not
             * constants here. Copy and constant propagation passes should
             * have taken care of this in most cases anyway.
             */
            dst_deref = nir_src_as_deref(intrin->src[0]);
            src_deref = nir_src_as_deref(intrin->src[1]);
            src_is_const = false;
            break;

         default:
            continue;
         }

         if (dst_deref && dst_deref->mode == nir_var_function_temp) {
            nir_variable *var = nir_deref_instr_get_variable(dst_deref);
            assert(var->data.mode == nir_var_function_temp);

            /* We only consider variables constant if they only have constant
             * stores, all the stores come before any reads, and all stores
             * come in the first block.  We also can't handle indirect stores.
             */
            struct var_info *info = &var_infos[var->data.index];
            if (!src_is_const || info->found_read || !first_block ||
                nir_deref_instr_has_indirect(dst_deref))
               info->is_constant = false;
         }

         if (src_deref && src_deref->mode == nir_var_function_temp) {
            nir_variable *var = nir_deref_instr_get_variable(src_deref);
            assert(var->data.mode == nir_var_function_temp);

            var_infos[var->data.index].found_read = true;
         }
      }
      first_block = false;
   }

   shader->constant_data_size = 0;
   nir_foreach_variable(var, &impl->locals) {
      struct var_info *info = &var_infos[var->data.index];
      if (!info->is_constant)
         continue;

      unsigned var_size, var_align;
      size_align(var->type, &var_size, &var_align);
      if (var_size <= threshold || !info->found_read) {
         /* Don't bother lowering small stuff or data that's never read */
         info->is_constant = false;
         continue;
      }

      var->data.location = ALIGN_POT(shader->constant_data_size, var_align);
      shader->constant_data_size = var->data.location + var_size;
   }

   if (shader->constant_data_size == 0) {
      free(var_infos);
      return false;
   }

   shader->constant_data = rzalloc_size(shader, shader->constant_data_size);

   nir_builder b;
   nir_builder_init(&b, impl);

   nir_foreach_block(block, impl) {
      nir_foreach_instr_safe(instr, block) {
         if (instr->type != nir_instr_type_intrinsic)
            continue;

         nir_intrinsic_instr *intrin = nir_instr_as_intrinsic(instr);

         switch (intrin->intrinsic) {
         case nir_intrinsic_load_deref: {
            nir_deref_instr *deref = nir_src_as_deref(intrin->src[0]);
            if (deref->mode != nir_var_function_temp)
               continue;

            nir_variable *var = nir_deref_instr_get_variable(deref);
            struct var_info *info = &var_infos[var->data.index];
            if (info->is_constant) {
               b.cursor = nir_after_instr(&intrin->instr);
               nir_ssa_def *val = build_constant_load(&b, deref, size_align);
               nir_ssa_def_rewrite_uses(&intrin->dest.ssa,
                                        nir_src_for_ssa(val));
               nir_instr_remove(&intrin->instr);
               nir_deref_instr_remove_if_unused(deref);
            }
            break;
         }

         case nir_intrinsic_store_deref: {
            nir_deref_instr *deref = nir_src_as_deref(intrin->src[0]);
            if (deref->mode != nir_var_function_temp)
               continue;

            nir_variable *var = nir_deref_instr_get_variable(deref);
            struct var_info *info = &var_infos[var->data.index];
            if (info->is_constant) {
               b.cursor = nir_after_instr(&intrin->instr);
               handle_constant_store(&b, intrin, size_align);
               nir_instr_remove(&intrin->instr);
               nir_deref_instr_remove_if_unused(deref);
            }
            break;
         }

         case nir_intrinsic_copy_deref: {
            nir_deref_instr *deref = nir_src_as_deref(intrin->src[1]);
            if (deref->mode != nir_var_function_temp)
               continue;

            nir_variable *var = nir_deref_instr_get_variable(deref);
            struct var_info *info = &var_infos[var->data.index];
            if (info->is_constant) {
               b.cursor = nir_after_instr(&intrin->instr);
               nir_ssa_def *val = build_constant_load(&b, deref, size_align);
               nir_store_deref(&b, nir_src_as_deref(intrin->src[0]), val, ~0);
               nir_instr_remove(&intrin->instr);
               nir_deref_instr_remove_if_unused(deref);
            }
            break;
         }

         default:
            continue;
         }
      }
   }

   /* Clean up the now unused variables */
   nir_foreach_variable_safe(var, &impl->locals) {
      if (var_infos[var->data.index].is_constant)
         exec_node_remove(&var->node);
   }

   free(var_infos);

   nir_metadata_preserve(impl, nir_metadata_block_index |
                               nir_metadata_dominance);
   return true;
}