1bf215546Sopenharmony_ci/*
2bf215546Sopenharmony_ci * Copyright © 2020 Google, Inc.
3bf215546Sopenharmony_ci *
4bf215546Sopenharmony_ci * Permission is hereby granted, free of charge, to any person obtaining a
5bf215546Sopenharmony_ci * copy of this software and associated documentation files (the "Software"),
6bf215546Sopenharmony_ci * to deal in the Software without restriction, including without limitation
7bf215546Sopenharmony_ci * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8bf215546Sopenharmony_ci * and/or sell copies of the Software, and to permit persons to whom the
9bf215546Sopenharmony_ci * Software is furnished to do so, subject to the following conditions:
10bf215546Sopenharmony_ci *
11bf215546Sopenharmony_ci * The above copyright notice and this permission notice (including the next
12bf215546Sopenharmony_ci * paragraph) shall be included in all copies or substantial portions of the
13bf215546Sopenharmony_ci * Software.
14bf215546Sopenharmony_ci *
15bf215546Sopenharmony_ci * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16bf215546Sopenharmony_ci * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17bf215546Sopenharmony_ci * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
18bf215546Sopenharmony_ci * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19bf215546Sopenharmony_ci * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20bf215546Sopenharmony_ci * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21bf215546Sopenharmony_ci * SOFTWARE.
22bf215546Sopenharmony_ci */
23bf215546Sopenharmony_ci
24bf215546Sopenharmony_ci#include "nir.h"
25bf215546Sopenharmony_ci#include "nir_builder.h"
26bf215546Sopenharmony_ci
27bf215546Sopenharmony_ci/* A pass to split intrinsics with discontinuous writemasks into ones
28bf215546Sopenharmony_ci * with contiguous writemasks starting with .x, ie:
29bf215546Sopenharmony_ci *
30bf215546Sopenharmony_ci *   vec4 32 ssa_76 = vec4 ssa_35, ssa_35, ssa_35, ssa_35
31bf215546Sopenharmony_ci *   intrinsic store_ssbo (ssa_76, ssa_105, ssa_106) (2, 0, 4, 0) // wrmask=y
32bf215546Sopenharmony_ci *
33bf215546Sopenharmony_ci * is turned into:
34bf215546Sopenharmony_ci *
35bf215546Sopenharmony_ci *   vec4 32 ssa_76 = vec4 ssa_35, ssa_35, ssa_35, ssa_35
36bf215546Sopenharmony_ci *   vec1 32 ssa_107 = load_const (0x00000001)
37bf215546Sopenharmony_ci *   vec1 32 ssa_108 = iadd ssa_106, ssa_107
38bf215546Sopenharmony_ci *   vec1 32 ssa_109 = mov ssa_76.y
39bf215546Sopenharmony_ci *   intrinsic store_ssbo (ssa_109, ssa_105, ssa_108) (1, 0, 4, 0) // wrmask=x
40bf215546Sopenharmony_ci *
41bf215546Sopenharmony_ci * and likewise:
42bf215546Sopenharmony_ci *
43bf215546Sopenharmony_ci *   vec4 32 ssa_76 = vec4 ssa_35, ssa_35, ssa_35, ssa_35
44bf215546Sopenharmony_ci *   intrinsic store_ssbo (ssa_76, ssa_105, ssa_106) (15, 0, 4, 0) // wrmask=xzw
45bf215546Sopenharmony_ci *
46bf215546Sopenharmony_ci * is split into:
47bf215546Sopenharmony_ci *
48bf215546Sopenharmony_ci *   // .x component:
49bf215546Sopenharmony_ci *   vec4 32 ssa_76 = vec4 ssa_35, ssa_35, ssa_35, ssa_35
50bf215546Sopenharmony_ci *   vec1 32 ssa_107 = load_const (0x00000000)
51bf215546Sopenharmony_ci *   vec1 32 ssa_108 = iadd ssa_106, ssa_107
52bf215546Sopenharmony_ci *   vec1 32 ssa_109 = mov ssa_76.x
53bf215546Sopenharmony_ci *   intrinsic store_ssbo (ssa_109, ssa_105, ssa_108) (1, 0, 4, 0) // wrmask=x
54bf215546Sopenharmony_ci *   // .zw components:
55bf215546Sopenharmony_ci *   vec1 32 ssa_110 = load_const (0x00000002)
56bf215546Sopenharmony_ci *   vec1 32 ssa_111 = iadd ssa_106, ssa_110
57bf215546Sopenharmony_ci *   vec2 32 ssa_112 = mov ssa_76.zw
58bf215546Sopenharmony_ci *   intrinsic store_ssbo (ssa_112, ssa_105, ssa_111) (3, 0, 4, 0) // wrmask=xy
59bf215546Sopenharmony_ci */
60bf215546Sopenharmony_ci
61bf215546Sopenharmony_cistatic int
62bf215546Sopenharmony_civalue_src(nir_intrinsic_op intrinsic)
63bf215546Sopenharmony_ci{
64bf215546Sopenharmony_ci   switch (intrinsic) {
65bf215546Sopenharmony_ci   case nir_intrinsic_store_output:
66bf215546Sopenharmony_ci   case nir_intrinsic_store_per_vertex_output:
67bf215546Sopenharmony_ci   case nir_intrinsic_store_ssbo:
68bf215546Sopenharmony_ci   case nir_intrinsic_store_shared:
69bf215546Sopenharmony_ci   case nir_intrinsic_store_global:
70bf215546Sopenharmony_ci   case nir_intrinsic_store_scratch:
71bf215546Sopenharmony_ci      return 0;
72bf215546Sopenharmony_ci   default:
73bf215546Sopenharmony_ci      return -1;
74bf215546Sopenharmony_ci   }
75bf215546Sopenharmony_ci}
76bf215546Sopenharmony_ci
77bf215546Sopenharmony_cistatic int
78bf215546Sopenharmony_cioffset_src(nir_intrinsic_op intrinsic)
79bf215546Sopenharmony_ci{
80bf215546Sopenharmony_ci   switch (intrinsic) {
81bf215546Sopenharmony_ci   case nir_intrinsic_store_output:
82bf215546Sopenharmony_ci   case nir_intrinsic_store_shared:
83bf215546Sopenharmony_ci   case nir_intrinsic_store_global:
84bf215546Sopenharmony_ci   case nir_intrinsic_store_scratch:
85bf215546Sopenharmony_ci      return 1;
86bf215546Sopenharmony_ci   case nir_intrinsic_store_per_vertex_output:
87bf215546Sopenharmony_ci   case nir_intrinsic_store_ssbo:
88bf215546Sopenharmony_ci      return 2;
89bf215546Sopenharmony_ci   default:
90bf215546Sopenharmony_ci      return -1;
91bf215546Sopenharmony_ci   }
92bf215546Sopenharmony_ci}
93bf215546Sopenharmony_ci
94bf215546Sopenharmony_cistatic void
95bf215546Sopenharmony_cisplit_wrmask(nir_builder *b, nir_intrinsic_instr *intr)
96bf215546Sopenharmony_ci{
97bf215546Sopenharmony_ci   const nir_intrinsic_info *info = &nir_intrinsic_infos[intr->intrinsic];
98bf215546Sopenharmony_ci
99bf215546Sopenharmony_ci   b->cursor = nir_before_instr(&intr->instr);
100bf215546Sopenharmony_ci
101bf215546Sopenharmony_ci   assert(!info->has_dest);  /* expecting only store intrinsics */
102bf215546Sopenharmony_ci
103bf215546Sopenharmony_ci   unsigned num_srcs   = info->num_srcs;
104bf215546Sopenharmony_ci   unsigned value_idx  = value_src(intr->intrinsic);
105bf215546Sopenharmony_ci   unsigned offset_idx = offset_src(intr->intrinsic);
106bf215546Sopenharmony_ci   unsigned num_comp   = nir_intrinsic_src_components(intr, value_idx);
107bf215546Sopenharmony_ci
108bf215546Sopenharmony_ci   unsigned wrmask = nir_intrinsic_write_mask(intr);
109bf215546Sopenharmony_ci   while (wrmask) {
110bf215546Sopenharmony_ci      unsigned first_component = ffs(wrmask) - 1;
111bf215546Sopenharmony_ci      unsigned length = ffs(~(wrmask >> first_component)) - 1;
112bf215546Sopenharmony_ci
113bf215546Sopenharmony_ci      nir_ssa_def *value  = nir_ssa_for_src(b, intr->src[value_idx], num_comp);
114bf215546Sopenharmony_ci      nir_ssa_def *offset = nir_ssa_for_src(b, intr->src[offset_idx], 1);
115bf215546Sopenharmony_ci
116bf215546Sopenharmony_ci      /* swizzle out the consecutive components that we'll store
117bf215546Sopenharmony_ci       * in this iteration:
118bf215546Sopenharmony_ci       */
119bf215546Sopenharmony_ci      unsigned cur_mask = (BITFIELD_MASK(length) << first_component);
120bf215546Sopenharmony_ci      value = nir_channels(b, value, cur_mask);
121bf215546Sopenharmony_ci
122bf215546Sopenharmony_ci      /* and create the replacement intrinsic: */
123bf215546Sopenharmony_ci      nir_intrinsic_instr *new_intr =
124bf215546Sopenharmony_ci            nir_intrinsic_instr_create(b->shader, intr->intrinsic);
125bf215546Sopenharmony_ci
126bf215546Sopenharmony_ci      nir_intrinsic_copy_const_indices(new_intr, intr);
127bf215546Sopenharmony_ci      nir_intrinsic_set_write_mask(new_intr, BITFIELD_MASK(length));
128bf215546Sopenharmony_ci
129bf215546Sopenharmony_ci      const int offset_units = value->bit_size / 8;
130bf215546Sopenharmony_ci
131bf215546Sopenharmony_ci      if (nir_intrinsic_has_align_mul(intr)) {
132bf215546Sopenharmony_ci         assert(nir_intrinsic_has_align_offset(intr));
133bf215546Sopenharmony_ci         unsigned align_mul = nir_intrinsic_align_mul(intr);
134bf215546Sopenharmony_ci         unsigned align_off = nir_intrinsic_align_offset(intr);
135bf215546Sopenharmony_ci
136bf215546Sopenharmony_ci         align_off += offset_units * first_component;
137bf215546Sopenharmony_ci         align_off = align_off % align_mul;
138bf215546Sopenharmony_ci
139bf215546Sopenharmony_ci         nir_intrinsic_set_align(new_intr, align_mul, align_off);
140bf215546Sopenharmony_ci      }
141bf215546Sopenharmony_ci
142bf215546Sopenharmony_ci      /* if the instruction has a BASE, fold the offset adjustment
143bf215546Sopenharmony_ci       * into that instead of adding alu instructions, otherwise add
144bf215546Sopenharmony_ci       * instructions
145bf215546Sopenharmony_ci       */
146bf215546Sopenharmony_ci      unsigned offset_adj = offset_units * first_component;
147bf215546Sopenharmony_ci      if (nir_intrinsic_has_base(intr)) {
148bf215546Sopenharmony_ci         nir_intrinsic_set_base(new_intr,
149bf215546Sopenharmony_ci               nir_intrinsic_base(intr) + offset_adj);
150bf215546Sopenharmony_ci      } else {
151bf215546Sopenharmony_ci         offset = nir_iadd(b, offset,
152bf215546Sopenharmony_ci               nir_imm_intN_t(b, offset_adj, offset->bit_size));
153bf215546Sopenharmony_ci      }
154bf215546Sopenharmony_ci
155bf215546Sopenharmony_ci      new_intr->num_components = length;
156bf215546Sopenharmony_ci
157bf215546Sopenharmony_ci      /* Copy the sources, replacing value/offset, and passing everything
158bf215546Sopenharmony_ci       * else through to the new instrution:
159bf215546Sopenharmony_ci       */
160bf215546Sopenharmony_ci      for (unsigned i = 0; i < num_srcs; i++) {
161bf215546Sopenharmony_ci         if (i == value_idx) {
162bf215546Sopenharmony_ci            new_intr->src[i] = nir_src_for_ssa(value);
163bf215546Sopenharmony_ci         } else if (i == offset_idx) {
164bf215546Sopenharmony_ci            new_intr->src[i] = nir_src_for_ssa(offset);
165bf215546Sopenharmony_ci         } else {
166bf215546Sopenharmony_ci            new_intr->src[i] = intr->src[i];
167bf215546Sopenharmony_ci         }
168bf215546Sopenharmony_ci      }
169bf215546Sopenharmony_ci
170bf215546Sopenharmony_ci      nir_builder_instr_insert(b, &new_intr->instr);
171bf215546Sopenharmony_ci
172bf215546Sopenharmony_ci      /* Clear the bits in the writemask that we just wrote, then try
173bf215546Sopenharmony_ci       * again to see if more channels are left.
174bf215546Sopenharmony_ci       */
175bf215546Sopenharmony_ci      wrmask &= ~cur_mask;
176bf215546Sopenharmony_ci   }
177bf215546Sopenharmony_ci
178bf215546Sopenharmony_ci   /* Finally remove the original intrinsic. */
179bf215546Sopenharmony_ci   nir_instr_remove(&intr->instr);
180bf215546Sopenharmony_ci}
181bf215546Sopenharmony_ci
182bf215546Sopenharmony_cistruct nir_lower_wrmasks_state {
183bf215546Sopenharmony_ci   nir_instr_filter_cb cb;
184bf215546Sopenharmony_ci   const void *data;
185bf215546Sopenharmony_ci};
186bf215546Sopenharmony_ci
187bf215546Sopenharmony_cistatic bool
188bf215546Sopenharmony_cinir_lower_wrmasks_instr(nir_builder *b, nir_instr *instr, void *data)
189bf215546Sopenharmony_ci{
190bf215546Sopenharmony_ci   struct nir_lower_wrmasks_state *state = data;
191bf215546Sopenharmony_ci
192bf215546Sopenharmony_ci   if (instr->type != nir_instr_type_intrinsic)
193bf215546Sopenharmony_ci      return false;
194bf215546Sopenharmony_ci
195bf215546Sopenharmony_ci   nir_intrinsic_instr *intr = nir_instr_as_intrinsic(instr);
196bf215546Sopenharmony_ci
197bf215546Sopenharmony_ci   /* if no wrmask, then skip it: */
198bf215546Sopenharmony_ci   if (!nir_intrinsic_has_write_mask(intr))
199bf215546Sopenharmony_ci      return false;
200bf215546Sopenharmony_ci
201bf215546Sopenharmony_ci   /* if wrmask is already contiguous, then nothing to do: */
202bf215546Sopenharmony_ci   if (nir_intrinsic_write_mask(intr) == BITFIELD_MASK(intr->num_components))
203bf215546Sopenharmony_ci      return false;
204bf215546Sopenharmony_ci
205bf215546Sopenharmony_ci   /* do we know how to lower this instruction? */
206bf215546Sopenharmony_ci   if (value_src(intr->intrinsic) < 0)
207bf215546Sopenharmony_ci      return false;
208bf215546Sopenharmony_ci
209bf215546Sopenharmony_ci   assert(offset_src(intr->intrinsic) >= 0);
210bf215546Sopenharmony_ci
211bf215546Sopenharmony_ci   /* does backend need us to lower this intrinsic? */
212bf215546Sopenharmony_ci   if (state->cb && !state->cb(instr, state->data))
213bf215546Sopenharmony_ci      return false;
214bf215546Sopenharmony_ci
215bf215546Sopenharmony_ci   split_wrmask(b, intr);
216bf215546Sopenharmony_ci
217bf215546Sopenharmony_ci   return true;
218bf215546Sopenharmony_ci}
219bf215546Sopenharmony_ci
220bf215546Sopenharmony_cibool
221bf215546Sopenharmony_cinir_lower_wrmasks(nir_shader *shader, nir_instr_filter_cb cb, const void *data)
222bf215546Sopenharmony_ci{
223bf215546Sopenharmony_ci   struct nir_lower_wrmasks_state state = {
224bf215546Sopenharmony_ci      .cb = cb,
225bf215546Sopenharmony_ci      .data = data,
226bf215546Sopenharmony_ci   };
227bf215546Sopenharmony_ci
228bf215546Sopenharmony_ci   return nir_shader_instructions_pass(shader,
229bf215546Sopenharmony_ci                                       nir_lower_wrmasks_instr,
230bf215546Sopenharmony_ci                                       nir_metadata_block_index |
231bf215546Sopenharmony_ci                                       nir_metadata_dominance,
232bf215546Sopenharmony_ci                                       &state);
233bf215546Sopenharmony_ci}
234