summaryrefslogtreecommitdiff
path: root/src/compiler/nir/nir_lower_memcpy.c
blob: 03503ad262a3f4e755460c79ec5a88404d557ea4 (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
/*
 * Copyright © 2020 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_builder.h"

#include <string.h>

/** Returns the type to use for a copy of the given size.
 *
 * The actual type doesn't matter here all that much as we're just going to do
 * a load/store on it and never any arithmetic.
 */
static const struct glsl_type *
copy_type_for_byte_size(unsigned size)
{
   switch (size) {
   case 1:  return glsl_vector_type(GLSL_TYPE_UINT8, 1);
   case 2:  return glsl_vector_type(GLSL_TYPE_UINT16, 1);
   case 4:  return glsl_vector_type(GLSL_TYPE_UINT, 1);
   case 8:  return glsl_vector_type(GLSL_TYPE_UINT, 2);
   case 16: return glsl_vector_type(GLSL_TYPE_UINT, 4);
   default:
      unreachable("Unsupported size");
   }
}

static nir_ssa_def *
memcpy_load_deref_elem(nir_builder *b, nir_deref_instr *parent,
                       nir_ssa_def *index)
{
   nir_deref_instr *deref;

   index = nir_i2iN(b, index, nir_dest_bit_size(parent->dest));
   assert(parent->deref_type == nir_deref_type_cast);
   deref = nir_build_deref_ptr_as_array(b, parent, index);

   return nir_load_deref(b, deref);
}

static nir_ssa_def *
memcpy_load_deref_elem_imm(nir_builder *b, nir_deref_instr *parent,
                           uint64_t index)
{
   nir_ssa_def *idx = nir_imm_intN_t(b, index, parent->dest.ssa.bit_size);
   return memcpy_load_deref_elem(b, parent, idx);
}

static void
memcpy_store_deref_elem(nir_builder *b, nir_deref_instr *parent,
                        nir_ssa_def *index, nir_ssa_def *value)
{
   nir_deref_instr *deref;

   index = nir_i2iN(b, index, nir_dest_bit_size(parent->dest));
   assert(parent->deref_type == nir_deref_type_cast);
   deref = nir_build_deref_ptr_as_array(b, parent, index);
   nir_store_deref(b, deref, value, ~0);
}

static void
memcpy_store_deref_elem_imm(nir_builder *b, nir_deref_instr *parent,
                            uint64_t index, nir_ssa_def *value)
{
   nir_ssa_def *idx = nir_imm_intN_t(b, index, parent->dest.ssa.bit_size);
   memcpy_store_deref_elem(b, parent, idx, value);
}

static bool
lower_memcpy_impl(nir_function_impl *impl)
{
   nir_builder b;
   nir_builder_init(&b, impl);

   bool found_const_memcpy = false, found_non_const_memcpy = false;

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

         nir_intrinsic_instr *cpy = nir_instr_as_intrinsic(instr);
         if (cpy->intrinsic != nir_intrinsic_memcpy_deref)
            continue;

         b.cursor = nir_instr_remove(&cpy->instr);

         nir_deref_instr *dst = nir_src_as_deref(cpy->src[0]);
         nir_deref_instr *src = nir_src_as_deref(cpy->src[1]);
         if (nir_src_is_const(cpy->src[2])) {
            found_const_memcpy = true;
            uint64_t size = nir_src_as_uint(cpy->src[2]);
            uint64_t offset = 0;
            while (offset < size) {
               uint64_t remaining = size - offset;
               /* Find the largest chunk size power-of-two (MSB in remaining)
                * and limit our chunk to 16B (a vec4). It's important to do as
                * many 16B chunks as possible first so that the index
                * computation is correct for
                * memcpy_(load|store)_deref_elem_imm.
                */
               unsigned copy_size = 1u << MIN2(util_last_bit64(remaining) - 1, 4);
               const struct glsl_type *copy_type =
                  copy_type_for_byte_size(copy_size);

               nir_deref_instr *copy_dst =
                  nir_build_deref_cast(&b, &dst->dest.ssa, dst->modes,
                                       copy_type, copy_size);
               nir_deref_instr *copy_src =
                  nir_build_deref_cast(&b, &src->dest.ssa, src->modes,
                                       copy_type, copy_size);

               uint64_t index = offset / copy_size;
               nir_ssa_def *value =
                  memcpy_load_deref_elem_imm(&b, copy_src, index);
               memcpy_store_deref_elem_imm(&b, copy_dst, index, value);
               offset += copy_size;
            }
         } else {
            found_non_const_memcpy = true;
            assert(cpy->src[2].is_ssa);
            nir_ssa_def *size = cpy->src[2].ssa;

            /* In this case, we don't have any idea what the size is so we
             * emit a loop which copies one byte at a time.
             */
            nir_deref_instr *copy_dst =
               nir_build_deref_cast(&b, &dst->dest.ssa, dst->modes,
                                    glsl_uint8_t_type(), 1);
            nir_deref_instr *copy_src =
               nir_build_deref_cast(&b, &src->dest.ssa, src->modes,
                                    glsl_uint8_t_type(), 1);

            nir_variable *i = nir_local_variable_create(impl,
               glsl_uintN_t_type(size->bit_size), NULL);
            nir_store_var(&b, i, nir_imm_intN_t(&b, 0, size->bit_size), ~0);
            nir_push_loop(&b);
            {
               nir_ssa_def *index = nir_load_var(&b, i);
               nir_push_if(&b, nir_uge(&b, index, size));
               {
                  nir_jump(&b, nir_jump_break);
               }
               nir_pop_if(&b, NULL);

               nir_ssa_def *value =
                  memcpy_load_deref_elem(&b, copy_src, index);
               memcpy_store_deref_elem(&b, copy_dst, index, value);
               nir_store_var(&b, i, nir_iadd_imm(&b, index, 1),  ~0);
            }
            nir_pop_loop(&b, NULL);
         }
      }
   }

   if (found_non_const_memcpy) {
      nir_metadata_preserve(impl, nir_metadata_none);
   } else if (found_const_memcpy) {
      nir_metadata_preserve(impl, nir_metadata_block_index |
                                  nir_metadata_dominance);
   } else {
      nir_metadata_preserve(impl, nir_metadata_all);
   }

   return found_const_memcpy || found_non_const_memcpy;
}

bool
nir_lower_memcpy(nir_shader *shader)
{
   bool progress = false;

   nir_foreach_function(function, shader) {
      if (function->impl && lower_memcpy_impl(function->impl))
         progress = true;
   }

   return progress;
}