1bf215546Sopenharmony_ci/**************************************************************************
2bf215546Sopenharmony_ci *
3bf215546Sopenharmony_ci * Copyright 2019 Red Hat.
4bf215546Sopenharmony_ci * All Rights Reserved.
5bf215546Sopenharmony_ci *
6bf215546Sopenharmony_ci * Permission is hereby granted, free of charge, to any person obtaining a
7bf215546Sopenharmony_ci * copy of this software and associated documentation files (the "Software"),
8bf215546Sopenharmony_ci * to deal in the Software without restriction, including without limitation
9bf215546Sopenharmony_ci * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10bf215546Sopenharmony_ci * and/or sell copies of the Software, and to permit persons to whom the
11bf215546Sopenharmony_ci * Software is furnished to do so, subject to the following conditions:
12bf215546Sopenharmony_ci *
13bf215546Sopenharmony_ci * The above copyright notice and this permission notice shall be included
14bf215546Sopenharmony_ci * in all copies or substantial portions of the Software.
15bf215546Sopenharmony_ci *
16bf215546Sopenharmony_ci * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
17bf215546Sopenharmony_ci * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18bf215546Sopenharmony_ci * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
19bf215546Sopenharmony_ci * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20bf215546Sopenharmony_ci * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21bf215546Sopenharmony_ci * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22bf215546Sopenharmony_ci * SOFTWARE.
23bf215546Sopenharmony_ci *
24bf215546Sopenharmony_ci **************************************************************************/
25bf215546Sopenharmony_ci
26bf215546Sopenharmony_ci#include "lp_bld_nir.h"
27bf215546Sopenharmony_ci#include "lp_bld_arit.h"
28bf215546Sopenharmony_ci#include "lp_bld_bitarit.h"
29bf215546Sopenharmony_ci#include "lp_bld_const.h"
30bf215546Sopenharmony_ci#include "lp_bld_conv.h"
31bf215546Sopenharmony_ci#include "lp_bld_gather.h"
32bf215546Sopenharmony_ci#include "lp_bld_logic.h"
33bf215546Sopenharmony_ci#include "lp_bld_quad.h"
34bf215546Sopenharmony_ci#include "lp_bld_flow.h"
35bf215546Sopenharmony_ci#include "lp_bld_intr.h"
36bf215546Sopenharmony_ci#include "lp_bld_struct.h"
37bf215546Sopenharmony_ci#include "lp_bld_debug.h"
38bf215546Sopenharmony_ci#include "lp_bld_printf.h"
39bf215546Sopenharmony_ci#include "nir_deref.h"
40bf215546Sopenharmony_ci#include "nir_search_helpers.h"
41bf215546Sopenharmony_ci
42bf215546Sopenharmony_ci
43bf215546Sopenharmony_ci// Doing AOS (and linear) codegen?
44bf215546Sopenharmony_cistatic bool
45bf215546Sopenharmony_ciis_aos(const struct lp_build_nir_context *bld_base)
46bf215546Sopenharmony_ci{
47bf215546Sopenharmony_ci   // AOS is used for vectors of uint8[16]
48bf215546Sopenharmony_ci   return bld_base->base.type.length == 16 && bld_base->base.type.width == 8;
49bf215546Sopenharmony_ci}
50bf215546Sopenharmony_ci
51bf215546Sopenharmony_ci
52bf215546Sopenharmony_cistatic void
53bf215546Sopenharmony_civisit_cf_list(struct lp_build_nir_context *bld_base,
54bf215546Sopenharmony_ci              struct exec_list *list);
55bf215546Sopenharmony_ci
56bf215546Sopenharmony_ci
57bf215546Sopenharmony_cistatic LLVMValueRef
58bf215546Sopenharmony_cicast_type(struct lp_build_nir_context *bld_base, LLVMValueRef val,
59bf215546Sopenharmony_ci          nir_alu_type alu_type, unsigned bit_size)
60bf215546Sopenharmony_ci{
61bf215546Sopenharmony_ci   LLVMBuilderRef builder = bld_base->base.gallivm->builder;
62bf215546Sopenharmony_ci   switch (alu_type) {
63bf215546Sopenharmony_ci   case nir_type_float:
64bf215546Sopenharmony_ci      switch (bit_size) {
65bf215546Sopenharmony_ci      case 16:
66bf215546Sopenharmony_ci         return LLVMBuildBitCast(builder, val, bld_base->half_bld.vec_type, "");
67bf215546Sopenharmony_ci      case 32:
68bf215546Sopenharmony_ci         return LLVMBuildBitCast(builder, val, bld_base->base.vec_type, "");
69bf215546Sopenharmony_ci      case 64:
70bf215546Sopenharmony_ci         return LLVMBuildBitCast(builder, val, bld_base->dbl_bld.vec_type, "");
71bf215546Sopenharmony_ci      default:
72bf215546Sopenharmony_ci         assert(0);
73bf215546Sopenharmony_ci         break;
74bf215546Sopenharmony_ci      }
75bf215546Sopenharmony_ci      break;
76bf215546Sopenharmony_ci   case nir_type_int:
77bf215546Sopenharmony_ci      switch (bit_size) {
78bf215546Sopenharmony_ci      case 8:
79bf215546Sopenharmony_ci         return LLVMBuildBitCast(builder, val, bld_base->int8_bld.vec_type, "");
80bf215546Sopenharmony_ci      case 16:
81bf215546Sopenharmony_ci         return LLVMBuildBitCast(builder, val, bld_base->int16_bld.vec_type, "");
82bf215546Sopenharmony_ci      case 32:
83bf215546Sopenharmony_ci         return LLVMBuildBitCast(builder, val, bld_base->int_bld.vec_type, "");
84bf215546Sopenharmony_ci      case 64:
85bf215546Sopenharmony_ci         return LLVMBuildBitCast(builder, val, bld_base->int64_bld.vec_type, "");
86bf215546Sopenharmony_ci      default:
87bf215546Sopenharmony_ci         assert(0);
88bf215546Sopenharmony_ci         break;
89bf215546Sopenharmony_ci      }
90bf215546Sopenharmony_ci      break;
91bf215546Sopenharmony_ci   case nir_type_uint:
92bf215546Sopenharmony_ci      switch (bit_size) {
93bf215546Sopenharmony_ci      case 8:
94bf215546Sopenharmony_ci         return LLVMBuildBitCast(builder, val, bld_base->uint8_bld.vec_type, "");
95bf215546Sopenharmony_ci      case 16:
96bf215546Sopenharmony_ci         return LLVMBuildBitCast(builder, val, bld_base->uint16_bld.vec_type, "");
97bf215546Sopenharmony_ci      case 1:
98bf215546Sopenharmony_ci      case 32:
99bf215546Sopenharmony_ci         return LLVMBuildBitCast(builder, val, bld_base->uint_bld.vec_type, "");
100bf215546Sopenharmony_ci      case 64:
101bf215546Sopenharmony_ci         return LLVMBuildBitCast(builder, val, bld_base->uint64_bld.vec_type, "");
102bf215546Sopenharmony_ci      default:
103bf215546Sopenharmony_ci         assert(0);
104bf215546Sopenharmony_ci         break;
105bf215546Sopenharmony_ci      }
106bf215546Sopenharmony_ci      break;
107bf215546Sopenharmony_ci   case nir_type_uint32:
108bf215546Sopenharmony_ci      return LLVMBuildBitCast(builder, val, bld_base->uint_bld.vec_type, "");
109bf215546Sopenharmony_ci   default:
110bf215546Sopenharmony_ci      return val;
111bf215546Sopenharmony_ci   }
112bf215546Sopenharmony_ci   return NULL;
113bf215546Sopenharmony_ci}
114bf215546Sopenharmony_ci
115bf215546Sopenharmony_ci
116bf215546Sopenharmony_cistatic unsigned
117bf215546Sopenharmony_ciglsl_sampler_to_pipe(int sampler_dim, bool is_array)
118bf215546Sopenharmony_ci{
119bf215546Sopenharmony_ci   unsigned pipe_target = PIPE_BUFFER;
120bf215546Sopenharmony_ci   switch (sampler_dim) {
121bf215546Sopenharmony_ci   case GLSL_SAMPLER_DIM_1D:
122bf215546Sopenharmony_ci      pipe_target = is_array ? PIPE_TEXTURE_1D_ARRAY : PIPE_TEXTURE_1D;
123bf215546Sopenharmony_ci      break;
124bf215546Sopenharmony_ci   case GLSL_SAMPLER_DIM_2D:
125bf215546Sopenharmony_ci      pipe_target = is_array ? PIPE_TEXTURE_2D_ARRAY : PIPE_TEXTURE_2D;
126bf215546Sopenharmony_ci      break;
127bf215546Sopenharmony_ci   case GLSL_SAMPLER_DIM_SUBPASS:
128bf215546Sopenharmony_ci   case GLSL_SAMPLER_DIM_SUBPASS_MS:
129bf215546Sopenharmony_ci      pipe_target = PIPE_TEXTURE_2D_ARRAY;
130bf215546Sopenharmony_ci      break;
131bf215546Sopenharmony_ci   case GLSL_SAMPLER_DIM_3D:
132bf215546Sopenharmony_ci      pipe_target = PIPE_TEXTURE_3D;
133bf215546Sopenharmony_ci      break;
134bf215546Sopenharmony_ci   case GLSL_SAMPLER_DIM_MS:
135bf215546Sopenharmony_ci      pipe_target = is_array ? PIPE_TEXTURE_2D_ARRAY : PIPE_TEXTURE_2D;
136bf215546Sopenharmony_ci      break;
137bf215546Sopenharmony_ci   case GLSL_SAMPLER_DIM_CUBE:
138bf215546Sopenharmony_ci      pipe_target = is_array ? PIPE_TEXTURE_CUBE_ARRAY : PIPE_TEXTURE_CUBE;
139bf215546Sopenharmony_ci      break;
140bf215546Sopenharmony_ci   case GLSL_SAMPLER_DIM_RECT:
141bf215546Sopenharmony_ci      pipe_target = PIPE_TEXTURE_RECT;
142bf215546Sopenharmony_ci      break;
143bf215546Sopenharmony_ci   case GLSL_SAMPLER_DIM_BUF:
144bf215546Sopenharmony_ci      pipe_target = PIPE_BUFFER;
145bf215546Sopenharmony_ci      break;
146bf215546Sopenharmony_ci   default:
147bf215546Sopenharmony_ci      break;
148bf215546Sopenharmony_ci   }
149bf215546Sopenharmony_ci   return pipe_target;
150bf215546Sopenharmony_ci}
151bf215546Sopenharmony_ci
152bf215546Sopenharmony_ci
153bf215546Sopenharmony_cistatic LLVMValueRef get_ssa_src(struct lp_build_nir_context *bld_base, nir_ssa_def *ssa)
154bf215546Sopenharmony_ci{
155bf215546Sopenharmony_ci   return bld_base->ssa_defs[ssa->index];
156bf215546Sopenharmony_ci}
157bf215546Sopenharmony_ci
158bf215546Sopenharmony_ci
159bf215546Sopenharmony_cistatic LLVMValueRef
160bf215546Sopenharmony_ciget_src(struct lp_build_nir_context *bld_base, nir_src src);
161bf215546Sopenharmony_ci
162bf215546Sopenharmony_ci
163bf215546Sopenharmony_cistatic LLVMValueRef
164bf215546Sopenharmony_ciget_reg_src(struct lp_build_nir_context *bld_base, nir_reg_src src)
165bf215546Sopenharmony_ci{
166bf215546Sopenharmony_ci   struct hash_entry *entry = _mesa_hash_table_search(bld_base->regs, src.reg);
167bf215546Sopenharmony_ci   LLVMValueRef reg_storage = (LLVMValueRef)entry->data;
168bf215546Sopenharmony_ci   struct lp_build_context *reg_bld = get_int_bld(bld_base, true, src.reg->bit_size);
169bf215546Sopenharmony_ci   LLVMValueRef indir_src = NULL;
170bf215546Sopenharmony_ci   if (src.indirect)
171bf215546Sopenharmony_ci      indir_src = get_src(bld_base, *src.indirect);
172bf215546Sopenharmony_ci   return bld_base->load_reg(bld_base, reg_bld, &src, indir_src, reg_storage);
173bf215546Sopenharmony_ci}
174bf215546Sopenharmony_ci
175bf215546Sopenharmony_ci
176bf215546Sopenharmony_cistatic LLVMValueRef
177bf215546Sopenharmony_ciget_src(struct lp_build_nir_context *bld_base, nir_src src)
178bf215546Sopenharmony_ci{
179bf215546Sopenharmony_ci   if (src.is_ssa)
180bf215546Sopenharmony_ci      return get_ssa_src(bld_base, src.ssa);
181bf215546Sopenharmony_ci   else
182bf215546Sopenharmony_ci      return get_reg_src(bld_base, src.reg);
183bf215546Sopenharmony_ci}
184bf215546Sopenharmony_ci
185bf215546Sopenharmony_ci
186bf215546Sopenharmony_cistatic void
187bf215546Sopenharmony_ciassign_ssa(struct lp_build_nir_context *bld_base, int idx, LLVMValueRef ptr)
188bf215546Sopenharmony_ci{
189bf215546Sopenharmony_ci   bld_base->ssa_defs[idx] = ptr;
190bf215546Sopenharmony_ci}
191bf215546Sopenharmony_ci
192bf215546Sopenharmony_ci
193bf215546Sopenharmony_cistatic void
194bf215546Sopenharmony_ciassign_ssa_dest(struct lp_build_nir_context *bld_base, const nir_ssa_def *ssa,
195bf215546Sopenharmony_ci                LLVMValueRef vals[NIR_MAX_VEC_COMPONENTS])
196bf215546Sopenharmony_ci{
197bf215546Sopenharmony_ci   if ((ssa->num_components == 1 || is_aos(bld_base))) {
198bf215546Sopenharmony_ci      assign_ssa(bld_base, ssa->index, vals[0]);
199bf215546Sopenharmony_ci   } else {
200bf215546Sopenharmony_ci      assign_ssa(bld_base, ssa->index,
201bf215546Sopenharmony_ci             lp_nir_array_build_gather_values(bld_base->base.gallivm->builder,
202bf215546Sopenharmony_ci                                              vals, ssa->num_components));
203bf215546Sopenharmony_ci   }
204bf215546Sopenharmony_ci}
205bf215546Sopenharmony_ci
206bf215546Sopenharmony_ci
207bf215546Sopenharmony_cistatic void
208bf215546Sopenharmony_ciassign_reg(struct lp_build_nir_context *bld_base, const nir_reg_dest *reg,
209bf215546Sopenharmony_ci           unsigned write_mask,
210bf215546Sopenharmony_ci           LLVMValueRef vals[NIR_MAX_VEC_COMPONENTS])
211bf215546Sopenharmony_ci{
212bf215546Sopenharmony_ci   struct hash_entry *entry = _mesa_hash_table_search(bld_base->regs, reg->reg);
213bf215546Sopenharmony_ci   LLVMValueRef reg_storage = (LLVMValueRef)entry->data;
214bf215546Sopenharmony_ci   struct lp_build_context *reg_bld = get_int_bld(bld_base, true, reg->reg->bit_size);
215bf215546Sopenharmony_ci   LLVMValueRef indir_src = NULL;
216bf215546Sopenharmony_ci   if (reg->indirect)
217bf215546Sopenharmony_ci      indir_src = get_src(bld_base, *reg->indirect);
218bf215546Sopenharmony_ci   bld_base->store_reg(bld_base, reg_bld, reg, write_mask ? write_mask : 0xf, indir_src, reg_storage, vals);
219bf215546Sopenharmony_ci}
220bf215546Sopenharmony_ci
221bf215546Sopenharmony_ci
222bf215546Sopenharmony_cistatic void
223bf215546Sopenharmony_ciassign_dest(struct lp_build_nir_context *bld_base,
224bf215546Sopenharmony_ci            const nir_dest *dest,
225bf215546Sopenharmony_ci            LLVMValueRef vals[NIR_MAX_VEC_COMPONENTS])
226bf215546Sopenharmony_ci{
227bf215546Sopenharmony_ci   if (dest->is_ssa)
228bf215546Sopenharmony_ci      assign_ssa_dest(bld_base, &dest->ssa, vals);
229bf215546Sopenharmony_ci   else
230bf215546Sopenharmony_ci      assign_reg(bld_base, &dest->reg, 0, vals);
231bf215546Sopenharmony_ci}
232bf215546Sopenharmony_ci
233bf215546Sopenharmony_ci
234bf215546Sopenharmony_cistatic void
235bf215546Sopenharmony_ciassign_alu_dest(struct lp_build_nir_context *bld_base,
236bf215546Sopenharmony_ci                const nir_alu_dest *dest,
237bf215546Sopenharmony_ci                LLVMValueRef vals[NIR_MAX_VEC_COMPONENTS])
238bf215546Sopenharmony_ci{
239bf215546Sopenharmony_ci   if (dest->dest.is_ssa)
240bf215546Sopenharmony_ci      assign_ssa_dest(bld_base, &dest->dest.ssa, vals);
241bf215546Sopenharmony_ci   else
242bf215546Sopenharmony_ci      assign_reg(bld_base, &dest->dest.reg, dest->write_mask, vals);
243bf215546Sopenharmony_ci}
244bf215546Sopenharmony_ci
245bf215546Sopenharmony_ci
246bf215546Sopenharmony_cistatic LLVMValueRef
247bf215546Sopenharmony_ciint_to_bool32(struct lp_build_nir_context *bld_base,
248bf215546Sopenharmony_ci              uint32_t src_bit_size,
249bf215546Sopenharmony_ci              bool is_unsigned,
250bf215546Sopenharmony_ci              LLVMValueRef val)
251bf215546Sopenharmony_ci{
252bf215546Sopenharmony_ci   LLVMBuilderRef builder = bld_base->base.gallivm->builder;
253bf215546Sopenharmony_ci   struct lp_build_context *int_bld =
254bf215546Sopenharmony_ci      get_int_bld(bld_base, is_unsigned, src_bit_size);
255bf215546Sopenharmony_ci   LLVMValueRef result = lp_build_compare(bld_base->base.gallivm,
256bf215546Sopenharmony_ci                                          int_bld->type, PIPE_FUNC_NOTEQUAL,
257bf215546Sopenharmony_ci                                          val, int_bld->zero);
258bf215546Sopenharmony_ci   if (src_bit_size == 16)
259bf215546Sopenharmony_ci      result = LLVMBuildSExt(builder, result, bld_base->int_bld.vec_type, "");
260bf215546Sopenharmony_ci   else if (src_bit_size == 64)
261bf215546Sopenharmony_ci      result = LLVMBuildTrunc(builder, result, bld_base->int_bld.vec_type, "");
262bf215546Sopenharmony_ci   return result;
263bf215546Sopenharmony_ci}
264bf215546Sopenharmony_ci
265bf215546Sopenharmony_ci
266bf215546Sopenharmony_cistatic LLVMValueRef
267bf215546Sopenharmony_ciflt_to_bool32(struct lp_build_nir_context *bld_base,
268bf215546Sopenharmony_ci              uint32_t src_bit_size,
269bf215546Sopenharmony_ci              LLVMValueRef val)
270bf215546Sopenharmony_ci{
271bf215546Sopenharmony_ci   LLVMBuilderRef builder = bld_base->base.gallivm->builder;
272bf215546Sopenharmony_ci   struct lp_build_context *flt_bld = get_flt_bld(bld_base, src_bit_size);
273bf215546Sopenharmony_ci   LLVMValueRef result =
274bf215546Sopenharmony_ci      lp_build_cmp(flt_bld, PIPE_FUNC_NOTEQUAL, val, flt_bld->zero);
275bf215546Sopenharmony_ci   if (src_bit_size == 64)
276bf215546Sopenharmony_ci      result = LLVMBuildTrunc(builder, result, bld_base->int_bld.vec_type, "");
277bf215546Sopenharmony_ci   if (src_bit_size == 16)
278bf215546Sopenharmony_ci      result = LLVMBuildSExt(builder, result, bld_base->int_bld.vec_type, "");
279bf215546Sopenharmony_ci   return result;
280bf215546Sopenharmony_ci}
281bf215546Sopenharmony_ci
282bf215546Sopenharmony_ci
283bf215546Sopenharmony_cistatic LLVMValueRef
284bf215546Sopenharmony_cifcmp32(struct lp_build_nir_context *bld_base,
285bf215546Sopenharmony_ci       enum pipe_compare_func compare,
286bf215546Sopenharmony_ci       uint32_t src_bit_size,
287bf215546Sopenharmony_ci       LLVMValueRef src[NIR_MAX_VEC_COMPONENTS])
288bf215546Sopenharmony_ci{
289bf215546Sopenharmony_ci   LLVMBuilderRef builder = bld_base->base.gallivm->builder;
290bf215546Sopenharmony_ci   struct lp_build_context *flt_bld = get_flt_bld(bld_base, src_bit_size);
291bf215546Sopenharmony_ci   LLVMValueRef result;
292bf215546Sopenharmony_ci
293bf215546Sopenharmony_ci   if (compare != PIPE_FUNC_NOTEQUAL)
294bf215546Sopenharmony_ci      result = lp_build_cmp_ordered(flt_bld, compare, src[0], src[1]);
295bf215546Sopenharmony_ci   else
296bf215546Sopenharmony_ci      result = lp_build_cmp(flt_bld, compare, src[0], src[1]);
297bf215546Sopenharmony_ci   if (src_bit_size == 64)
298bf215546Sopenharmony_ci      result = LLVMBuildTrunc(builder, result, bld_base->int_bld.vec_type, "");
299bf215546Sopenharmony_ci   else if (src_bit_size == 16)
300bf215546Sopenharmony_ci      result = LLVMBuildSExt(builder, result, bld_base->int_bld.vec_type, "");
301bf215546Sopenharmony_ci   return result;
302bf215546Sopenharmony_ci}
303bf215546Sopenharmony_ci
304bf215546Sopenharmony_ci
305bf215546Sopenharmony_cistatic LLVMValueRef
306bf215546Sopenharmony_ciicmp32(struct lp_build_nir_context *bld_base,
307bf215546Sopenharmony_ci       enum pipe_compare_func compare,
308bf215546Sopenharmony_ci       bool is_unsigned,
309bf215546Sopenharmony_ci       uint32_t src_bit_size,
310bf215546Sopenharmony_ci       LLVMValueRef src[NIR_MAX_VEC_COMPONENTS])
311bf215546Sopenharmony_ci{
312bf215546Sopenharmony_ci   LLVMBuilderRef builder = bld_base->base.gallivm->builder;
313bf215546Sopenharmony_ci   struct lp_build_context *i_bld =
314bf215546Sopenharmony_ci      get_int_bld(bld_base, is_unsigned, src_bit_size);
315bf215546Sopenharmony_ci   LLVMValueRef result = lp_build_cmp(i_bld, compare, src[0], src[1]);
316bf215546Sopenharmony_ci   if (src_bit_size < 32)
317bf215546Sopenharmony_ci      result = LLVMBuildSExt(builder, result, bld_base->int_bld.vec_type, "");
318bf215546Sopenharmony_ci   else if (src_bit_size == 64)
319bf215546Sopenharmony_ci      result = LLVMBuildTrunc(builder, result, bld_base->int_bld.vec_type, "");
320bf215546Sopenharmony_ci   return result;
321bf215546Sopenharmony_ci}
322bf215546Sopenharmony_ci
323bf215546Sopenharmony_ci
324bf215546Sopenharmony_ci/**
325bf215546Sopenharmony_ci * Get a source register value for an ALU instruction.
326bf215546Sopenharmony_ci * This is where swizzled are handled.  There should be no negation
327bf215546Sopenharmony_ci * or absolute value modifiers.
328bf215546Sopenharmony_ci * num_components indicates the number of components needed in the
329bf215546Sopenharmony_ci * returned array or vector.
330bf215546Sopenharmony_ci */
331bf215546Sopenharmony_cistatic LLVMValueRef
332bf215546Sopenharmony_ciget_alu_src(struct lp_build_nir_context *bld_base,
333bf215546Sopenharmony_ci            nir_alu_src src,
334bf215546Sopenharmony_ci            unsigned num_components)
335bf215546Sopenharmony_ci{
336bf215546Sopenharmony_ci   struct gallivm_state *gallivm = bld_base->base.gallivm;
337bf215546Sopenharmony_ci   LLVMBuilderRef builder = gallivm->builder;
338bf215546Sopenharmony_ci   LLVMValueRef value = get_src(bld_base, src.src);
339bf215546Sopenharmony_ci   bool need_swizzle = false;
340bf215546Sopenharmony_ci
341bf215546Sopenharmony_ci   assert(value);
342bf215546Sopenharmony_ci
343bf215546Sopenharmony_ci   if (is_aos(bld_base))
344bf215546Sopenharmony_ci      return value;
345bf215546Sopenharmony_ci
346bf215546Sopenharmony_ci   unsigned src_components = nir_src_num_components(src.src);
347bf215546Sopenharmony_ci   for (unsigned i = 0; i < num_components; ++i) {
348bf215546Sopenharmony_ci      assert(src.swizzle[i] < src_components);
349bf215546Sopenharmony_ci      if (src.swizzle[i] != i)
350bf215546Sopenharmony_ci         need_swizzle = true;
351bf215546Sopenharmony_ci   }
352bf215546Sopenharmony_ci
353bf215546Sopenharmony_ci   if (need_swizzle || num_components != src_components) {
354bf215546Sopenharmony_ci      if (src_components > 1 && num_components == 1) {
355bf215546Sopenharmony_ci         value = LLVMBuildExtractValue(gallivm->builder, value,
356bf215546Sopenharmony_ci                                       src.swizzle[0], "");
357bf215546Sopenharmony_ci      } else if (src_components == 1 && num_components > 1) {
358bf215546Sopenharmony_ci         LLVMValueRef values[] = {value, value, value, value,
359bf215546Sopenharmony_ci                                  value, value, value, value,
360bf215546Sopenharmony_ci                                  value, value, value, value,
361bf215546Sopenharmony_ci                                  value, value, value, value};
362bf215546Sopenharmony_ci         value = lp_nir_array_build_gather_values(builder, values, num_components);
363bf215546Sopenharmony_ci      } else {
364bf215546Sopenharmony_ci         LLVMValueRef arr = LLVMGetUndef(LLVMArrayType(LLVMTypeOf(LLVMBuildExtractValue(builder, value, 0, "")), num_components));
365bf215546Sopenharmony_ci         for (unsigned i = 0; i < num_components; i++)
366bf215546Sopenharmony_ci            arr = LLVMBuildInsertValue(builder, arr, LLVMBuildExtractValue(builder, value, src.swizzle[i], ""), i, "");
367bf215546Sopenharmony_ci         value = arr;
368bf215546Sopenharmony_ci      }
369bf215546Sopenharmony_ci   }
370bf215546Sopenharmony_ci   assert(!src.negate);
371bf215546Sopenharmony_ci   assert(!src.abs);
372bf215546Sopenharmony_ci   return value;
373bf215546Sopenharmony_ci}
374bf215546Sopenharmony_ci
375bf215546Sopenharmony_ci
376bf215546Sopenharmony_cistatic LLVMValueRef
377bf215546Sopenharmony_ciemit_b2f(struct lp_build_nir_context *bld_base,
378bf215546Sopenharmony_ci         LLVMValueRef src0,
379bf215546Sopenharmony_ci         unsigned bitsize)
380bf215546Sopenharmony_ci{
381bf215546Sopenharmony_ci   LLVMBuilderRef builder = bld_base->base.gallivm->builder;
382bf215546Sopenharmony_ci   LLVMValueRef result =
383bf215546Sopenharmony_ci      LLVMBuildAnd(builder, cast_type(bld_base, src0, nir_type_int, 32),
384bf215546Sopenharmony_ci                   LLVMBuildBitCast(builder,
385bf215546Sopenharmony_ci                                    lp_build_const_vec(bld_base->base.gallivm,
386bf215546Sopenharmony_ci                                                       bld_base->base.type,
387bf215546Sopenharmony_ci                                                       1.0),
388bf215546Sopenharmony_ci                                    bld_base->int_bld.vec_type, ""),
389bf215546Sopenharmony_ci                   "");
390bf215546Sopenharmony_ci   result = LLVMBuildBitCast(builder, result, bld_base->base.vec_type, "");
391bf215546Sopenharmony_ci   switch (bitsize) {
392bf215546Sopenharmony_ci   case 16:
393bf215546Sopenharmony_ci      result = LLVMBuildFPTrunc(builder, result,
394bf215546Sopenharmony_ci                                bld_base->half_bld.vec_type, "");
395bf215546Sopenharmony_ci      break;
396bf215546Sopenharmony_ci   case 32:
397bf215546Sopenharmony_ci      break;
398bf215546Sopenharmony_ci   case 64:
399bf215546Sopenharmony_ci      result = LLVMBuildFPExt(builder, result,
400bf215546Sopenharmony_ci                              bld_base->dbl_bld.vec_type, "");
401bf215546Sopenharmony_ci      break;
402bf215546Sopenharmony_ci   default:
403bf215546Sopenharmony_ci      unreachable("unsupported bit size.");
404bf215546Sopenharmony_ci   }
405bf215546Sopenharmony_ci   return result;
406bf215546Sopenharmony_ci}
407bf215546Sopenharmony_ci
408bf215546Sopenharmony_ci
409bf215546Sopenharmony_cistatic LLVMValueRef
410bf215546Sopenharmony_ciemit_b2i(struct lp_build_nir_context *bld_base,
411bf215546Sopenharmony_ci         LLVMValueRef src0,
412bf215546Sopenharmony_ci         unsigned bitsize)
413bf215546Sopenharmony_ci{
414bf215546Sopenharmony_ci   LLVMBuilderRef builder = bld_base->base.gallivm->builder;
415bf215546Sopenharmony_ci   LLVMValueRef result = LLVMBuildAnd(builder,
416bf215546Sopenharmony_ci                          cast_type(bld_base, src0, nir_type_int, 32),
417bf215546Sopenharmony_ci                          lp_build_const_int_vec(bld_base->base.gallivm,
418bf215546Sopenharmony_ci                                                 bld_base->base.type, 1), "");
419bf215546Sopenharmony_ci   switch (bitsize) {
420bf215546Sopenharmony_ci   case 8:
421bf215546Sopenharmony_ci      return LLVMBuildTrunc(builder, result, bld_base->int8_bld.vec_type, "");
422bf215546Sopenharmony_ci   case 16:
423bf215546Sopenharmony_ci      return LLVMBuildTrunc(builder, result, bld_base->int16_bld.vec_type, "");
424bf215546Sopenharmony_ci   case 32:
425bf215546Sopenharmony_ci      return result;
426bf215546Sopenharmony_ci   case 64:
427bf215546Sopenharmony_ci      return LLVMBuildZExt(builder, result, bld_base->int64_bld.vec_type, "");
428bf215546Sopenharmony_ci   default:
429bf215546Sopenharmony_ci      unreachable("unsupported bit size.");
430bf215546Sopenharmony_ci   }
431bf215546Sopenharmony_ci}
432bf215546Sopenharmony_ci
433bf215546Sopenharmony_ci
434bf215546Sopenharmony_cistatic LLVMValueRef
435bf215546Sopenharmony_ciemit_b32csel(struct lp_build_nir_context *bld_base,
436bf215546Sopenharmony_ci             unsigned src_bit_size[NIR_MAX_VEC_COMPONENTS],
437bf215546Sopenharmony_ci             LLVMValueRef src[NIR_MAX_VEC_COMPONENTS])
438bf215546Sopenharmony_ci{
439bf215546Sopenharmony_ci   LLVMValueRef sel = cast_type(bld_base, src[0], nir_type_int, 32);
440bf215546Sopenharmony_ci   LLVMValueRef v = lp_build_compare(bld_base->base.gallivm, bld_base->int_bld.type, PIPE_FUNC_NOTEQUAL, sel, bld_base->int_bld.zero);
441bf215546Sopenharmony_ci   struct lp_build_context *bld = get_int_bld(bld_base, false, src_bit_size[1]);
442bf215546Sopenharmony_ci   return lp_build_select(bld, v, src[1], src[2]);
443bf215546Sopenharmony_ci}
444bf215546Sopenharmony_ci
445bf215546Sopenharmony_ci
446bf215546Sopenharmony_cistatic LLVMValueRef
447bf215546Sopenharmony_cisplit_64bit(struct lp_build_nir_context *bld_base,
448bf215546Sopenharmony_ci            LLVMValueRef src,
449bf215546Sopenharmony_ci            bool hi)
450bf215546Sopenharmony_ci{
451bf215546Sopenharmony_ci   struct gallivm_state *gallivm = bld_base->base.gallivm;
452bf215546Sopenharmony_ci   LLVMValueRef shuffles[LP_MAX_VECTOR_WIDTH/32];
453bf215546Sopenharmony_ci   LLVMValueRef shuffles2[LP_MAX_VECTOR_WIDTH/32];
454bf215546Sopenharmony_ci   int len = bld_base->base.type.length * 2;
455bf215546Sopenharmony_ci   for (unsigned i = 0; i < bld_base->base.type.length; i++) {
456bf215546Sopenharmony_ci#if UTIL_ARCH_LITTLE_ENDIAN
457bf215546Sopenharmony_ci      shuffles[i] = lp_build_const_int32(gallivm, i * 2);
458bf215546Sopenharmony_ci      shuffles2[i] = lp_build_const_int32(gallivm, (i * 2) + 1);
459bf215546Sopenharmony_ci#else
460bf215546Sopenharmony_ci      shuffles[i] = lp_build_const_int32(gallivm, (i * 2) + 1);
461bf215546Sopenharmony_ci      shuffles2[i] = lp_build_const_int32(gallivm, (i * 2));
462bf215546Sopenharmony_ci#endif
463bf215546Sopenharmony_ci   }
464bf215546Sopenharmony_ci
465bf215546Sopenharmony_ci   src = LLVMBuildBitCast(gallivm->builder, src,
466bf215546Sopenharmony_ci           LLVMVectorType(LLVMInt32TypeInContext(gallivm->context), len), "");
467bf215546Sopenharmony_ci   return LLVMBuildShuffleVector(gallivm->builder, src,
468bf215546Sopenharmony_ci                                 LLVMGetUndef(LLVMTypeOf(src)),
469bf215546Sopenharmony_ci                                 LLVMConstVector(hi ? shuffles2 : shuffles,
470bf215546Sopenharmony_ci                                                 bld_base->base.type.length),
471bf215546Sopenharmony_ci                                 "");
472bf215546Sopenharmony_ci}
473bf215546Sopenharmony_ci
474bf215546Sopenharmony_ci
475bf215546Sopenharmony_cistatic LLVMValueRef
476bf215546Sopenharmony_cimerge_64bit(struct lp_build_nir_context *bld_base,
477bf215546Sopenharmony_ci            LLVMValueRef input,
478bf215546Sopenharmony_ci            LLVMValueRef input2)
479bf215546Sopenharmony_ci{
480bf215546Sopenharmony_ci   struct gallivm_state *gallivm = bld_base->base.gallivm;
481bf215546Sopenharmony_ci   LLVMBuilderRef builder = gallivm->builder;
482bf215546Sopenharmony_ci   int i;
483bf215546Sopenharmony_ci   LLVMValueRef shuffles[2 * (LP_MAX_VECTOR_WIDTH/32)];
484bf215546Sopenharmony_ci   int len = bld_base->base.type.length * 2;
485bf215546Sopenharmony_ci   assert(len <= (2 * (LP_MAX_VECTOR_WIDTH/32)));
486bf215546Sopenharmony_ci
487bf215546Sopenharmony_ci   for (i = 0; i < bld_base->base.type.length * 2; i+=2) {
488bf215546Sopenharmony_ci#if UTIL_ARCH_LITTLE_ENDIAN
489bf215546Sopenharmony_ci      shuffles[i] = lp_build_const_int32(gallivm, i / 2);
490bf215546Sopenharmony_ci      shuffles[i + 1] = lp_build_const_int32(gallivm, i / 2 + bld_base->base.type.length);
491bf215546Sopenharmony_ci#else
492bf215546Sopenharmony_ci      shuffles[i] = lp_build_const_int32(gallivm, i / 2 + bld_base->base.type.length);
493bf215546Sopenharmony_ci      shuffles[i + 1] = lp_build_const_int32(gallivm, i / 2);
494bf215546Sopenharmony_ci#endif
495bf215546Sopenharmony_ci   }
496bf215546Sopenharmony_ci   return LLVMBuildShuffleVector(builder, input, input2, LLVMConstVector(shuffles, len), "");
497bf215546Sopenharmony_ci}
498bf215546Sopenharmony_ci
499bf215546Sopenharmony_ci
500bf215546Sopenharmony_cistatic LLVMValueRef
501bf215546Sopenharmony_cisplit_16bit(struct lp_build_nir_context *bld_base,
502bf215546Sopenharmony_ci            LLVMValueRef src,
503bf215546Sopenharmony_ci            bool hi)
504bf215546Sopenharmony_ci{
505bf215546Sopenharmony_ci   struct gallivm_state *gallivm = bld_base->base.gallivm;
506bf215546Sopenharmony_ci   LLVMValueRef shuffles[LP_MAX_VECTOR_WIDTH/32];
507bf215546Sopenharmony_ci   LLVMValueRef shuffles2[LP_MAX_VECTOR_WIDTH/32];
508bf215546Sopenharmony_ci   int len = bld_base->base.type.length * 2;
509bf215546Sopenharmony_ci   for (unsigned i = 0; i < bld_base->base.type.length; i++) {
510bf215546Sopenharmony_ci#if UTIL_ARCH_LITTLE_ENDIAN
511bf215546Sopenharmony_ci      shuffles[i] = lp_build_const_int32(gallivm, i * 2);
512bf215546Sopenharmony_ci      shuffles2[i] = lp_build_const_int32(gallivm, (i * 2) + 1);
513bf215546Sopenharmony_ci#else
514bf215546Sopenharmony_ci      shuffles[i] = lp_build_const_int32(gallivm, (i * 2) + 1);
515bf215546Sopenharmony_ci      shuffles2[i] = lp_build_const_int32(gallivm, (i * 2));
516bf215546Sopenharmony_ci#endif
517bf215546Sopenharmony_ci   }
518bf215546Sopenharmony_ci
519bf215546Sopenharmony_ci   src = LLVMBuildBitCast(gallivm->builder, src, LLVMVectorType(LLVMInt16TypeInContext(gallivm->context), len), "");
520bf215546Sopenharmony_ci   return LLVMBuildShuffleVector(gallivm->builder, src,
521bf215546Sopenharmony_ci                                 LLVMGetUndef(LLVMTypeOf(src)),
522bf215546Sopenharmony_ci                                 LLVMConstVector(hi ? shuffles2 : shuffles,
523bf215546Sopenharmony_ci                                                 bld_base->base.type.length),
524bf215546Sopenharmony_ci                                 "");
525bf215546Sopenharmony_ci}
526bf215546Sopenharmony_ci
527bf215546Sopenharmony_ci
528bf215546Sopenharmony_cistatic LLVMValueRef
529bf215546Sopenharmony_cimerge_16bit(struct lp_build_nir_context *bld_base,
530bf215546Sopenharmony_ci            LLVMValueRef input,
531bf215546Sopenharmony_ci            LLVMValueRef input2)
532bf215546Sopenharmony_ci{
533bf215546Sopenharmony_ci   struct gallivm_state *gallivm = bld_base->base.gallivm;
534bf215546Sopenharmony_ci   LLVMBuilderRef builder = gallivm->builder;
535bf215546Sopenharmony_ci   int i;
536bf215546Sopenharmony_ci   LLVMValueRef shuffles[2 * (LP_MAX_VECTOR_WIDTH/32)];
537bf215546Sopenharmony_ci   int len = bld_base->int16_bld.type.length * 2;
538bf215546Sopenharmony_ci   assert(len <= (2 * (LP_MAX_VECTOR_WIDTH/32)));
539bf215546Sopenharmony_ci
540bf215546Sopenharmony_ci   for (i = 0; i < bld_base->int_bld.type.length * 2; i+=2) {
541bf215546Sopenharmony_ci#if UTIL_ARCH_LITTLE_ENDIAN
542bf215546Sopenharmony_ci      shuffles[i] = lp_build_const_int32(gallivm, i / 2);
543bf215546Sopenharmony_ci      shuffles[i + 1] = lp_build_const_int32(gallivm, i / 2 + bld_base->base.type.length);
544bf215546Sopenharmony_ci#else
545bf215546Sopenharmony_ci      shuffles[i] = lp_build_const_int32(gallivm, i / 2 + bld_base->base.type.length);
546bf215546Sopenharmony_ci      shuffles[i + 1] = lp_build_const_int32(gallivm, i / 2);
547bf215546Sopenharmony_ci#endif
548bf215546Sopenharmony_ci   }
549bf215546Sopenharmony_ci   return LLVMBuildShuffleVector(builder, input, input2, LLVMConstVector(shuffles, len), "");
550bf215546Sopenharmony_ci}
551bf215546Sopenharmony_ci
552bf215546Sopenharmony_ci
553bf215546Sopenharmony_cistatic LLVMValueRef
554bf215546Sopenharmony_ciget_signed_divisor(struct gallivm_state *gallivm,
555bf215546Sopenharmony_ci                   struct lp_build_context *int_bld,
556bf215546Sopenharmony_ci                   struct lp_build_context *mask_bld,
557bf215546Sopenharmony_ci                   int src_bit_size,
558bf215546Sopenharmony_ci                   LLVMValueRef src, LLVMValueRef divisor)
559bf215546Sopenharmony_ci{
560bf215546Sopenharmony_ci   LLVMBuilderRef builder = gallivm->builder;
561bf215546Sopenharmony_ci   /* However for signed divides SIGFPE can occur if the numerator is INT_MIN
562bf215546Sopenharmony_ci      and divisor is -1. */
563bf215546Sopenharmony_ci   /* set mask if numerator == INT_MIN */
564bf215546Sopenharmony_ci   long long min_val;
565bf215546Sopenharmony_ci   switch (src_bit_size) {
566bf215546Sopenharmony_ci   case 8:
567bf215546Sopenharmony_ci      min_val = INT8_MIN;
568bf215546Sopenharmony_ci      break;
569bf215546Sopenharmony_ci   case 16:
570bf215546Sopenharmony_ci      min_val = INT16_MIN;
571bf215546Sopenharmony_ci      break;
572bf215546Sopenharmony_ci   default:
573bf215546Sopenharmony_ci   case 32:
574bf215546Sopenharmony_ci      min_val = INT_MIN;
575bf215546Sopenharmony_ci      break;
576bf215546Sopenharmony_ci   case 64:
577bf215546Sopenharmony_ci      min_val = INT64_MIN;
578bf215546Sopenharmony_ci      break;
579bf215546Sopenharmony_ci   }
580bf215546Sopenharmony_ci   LLVMValueRef div_mask2 = lp_build_cmp(mask_bld, PIPE_FUNC_EQUAL, src,
581bf215546Sopenharmony_ci                                         lp_build_const_int_vec(gallivm, int_bld->type, min_val));
582bf215546Sopenharmony_ci   /* set another mask if divisor is - 1 */
583bf215546Sopenharmony_ci   LLVMValueRef div_mask3 = lp_build_cmp(mask_bld, PIPE_FUNC_EQUAL, divisor,
584bf215546Sopenharmony_ci                                         lp_build_const_int_vec(gallivm, int_bld->type, -1));
585bf215546Sopenharmony_ci   div_mask2 = LLVMBuildAnd(builder, div_mask2, div_mask3, "");
586bf215546Sopenharmony_ci
587bf215546Sopenharmony_ci   divisor = lp_build_select(mask_bld, div_mask2, int_bld->one, divisor);
588bf215546Sopenharmony_ci   return divisor;
589bf215546Sopenharmony_ci}
590bf215546Sopenharmony_ci
591bf215546Sopenharmony_ci
592bf215546Sopenharmony_cistatic LLVMValueRef
593bf215546Sopenharmony_cido_int_divide(struct lp_build_nir_context *bld_base,
594bf215546Sopenharmony_ci              bool is_unsigned, unsigned src_bit_size,
595bf215546Sopenharmony_ci              LLVMValueRef src, LLVMValueRef src2)
596bf215546Sopenharmony_ci{
597bf215546Sopenharmony_ci   struct gallivm_state *gallivm = bld_base->base.gallivm;
598bf215546Sopenharmony_ci   LLVMBuilderRef builder = gallivm->builder;
599bf215546Sopenharmony_ci   struct lp_build_context *int_bld = get_int_bld(bld_base, is_unsigned, src_bit_size);
600bf215546Sopenharmony_ci   struct lp_build_context *mask_bld = get_int_bld(bld_base, true, src_bit_size);
601bf215546Sopenharmony_ci
602bf215546Sopenharmony_ci   /* avoid divide by 0. Converted divisor from 0 to -1 */
603bf215546Sopenharmony_ci   LLVMValueRef div_mask = lp_build_cmp(mask_bld, PIPE_FUNC_EQUAL, src2,
604bf215546Sopenharmony_ci                                        mask_bld->zero);
605bf215546Sopenharmony_ci
606bf215546Sopenharmony_ci   LLVMValueRef divisor = LLVMBuildOr(builder, div_mask, src2, "");
607bf215546Sopenharmony_ci   if (!is_unsigned) {
608bf215546Sopenharmony_ci      divisor = get_signed_divisor(gallivm, int_bld, mask_bld,
609bf215546Sopenharmony_ci                                   src_bit_size, src, divisor);
610bf215546Sopenharmony_ci   }
611bf215546Sopenharmony_ci   LLVMValueRef result = lp_build_div(int_bld, src, divisor);
612bf215546Sopenharmony_ci
613bf215546Sopenharmony_ci   if (!is_unsigned) {
614bf215546Sopenharmony_ci      LLVMValueRef not_div_mask = LLVMBuildNot(builder, div_mask, "");
615bf215546Sopenharmony_ci      return LLVMBuildAnd(builder, not_div_mask, result, "");
616bf215546Sopenharmony_ci   } else
617bf215546Sopenharmony_ci      /* udiv by zero is guaranteed to return 0xffffffff at least with d3d10
618bf215546Sopenharmony_ci       * may as well do same for idiv */
619bf215546Sopenharmony_ci      return LLVMBuildOr(builder, div_mask, result, "");
620bf215546Sopenharmony_ci}
621bf215546Sopenharmony_ci
622bf215546Sopenharmony_ci
623bf215546Sopenharmony_cistatic LLVMValueRef
624bf215546Sopenharmony_cido_int_mod(struct lp_build_nir_context *bld_base,
625bf215546Sopenharmony_ci           bool is_unsigned, unsigned src_bit_size,
626bf215546Sopenharmony_ci           LLVMValueRef src, LLVMValueRef src2)
627bf215546Sopenharmony_ci{
628bf215546Sopenharmony_ci   struct gallivm_state *gallivm = bld_base->base.gallivm;
629bf215546Sopenharmony_ci   LLVMBuilderRef builder = gallivm->builder;
630bf215546Sopenharmony_ci   struct lp_build_context *int_bld = get_int_bld(bld_base, is_unsigned, src_bit_size);
631bf215546Sopenharmony_ci   struct lp_build_context *mask_bld = get_int_bld(bld_base, true, src_bit_size);
632bf215546Sopenharmony_ci   LLVMValueRef div_mask = lp_build_cmp(mask_bld, PIPE_FUNC_EQUAL, src2,
633bf215546Sopenharmony_ci                                        mask_bld->zero);
634bf215546Sopenharmony_ci   LLVMValueRef divisor = LLVMBuildOr(builder,
635bf215546Sopenharmony_ci                                      div_mask,
636bf215546Sopenharmony_ci                                      src2, "");
637bf215546Sopenharmony_ci   if (!is_unsigned) {
638bf215546Sopenharmony_ci      divisor = get_signed_divisor(gallivm, int_bld, mask_bld,
639bf215546Sopenharmony_ci                                   src_bit_size, src, divisor);
640bf215546Sopenharmony_ci   }
641bf215546Sopenharmony_ci   LLVMValueRef result = lp_build_mod(int_bld, src, divisor);
642bf215546Sopenharmony_ci   return LLVMBuildOr(builder, div_mask, result, "");
643bf215546Sopenharmony_ci}
644bf215546Sopenharmony_ci
645bf215546Sopenharmony_ci
646bf215546Sopenharmony_cistatic LLVMValueRef
647bf215546Sopenharmony_cido_quantize_to_f16(struct lp_build_nir_context *bld_base,
648bf215546Sopenharmony_ci                   LLVMValueRef src)
649bf215546Sopenharmony_ci{
650bf215546Sopenharmony_ci   struct gallivm_state *gallivm = bld_base->base.gallivm;
651bf215546Sopenharmony_ci   LLVMBuilderRef builder = gallivm->builder;
652bf215546Sopenharmony_ci   LLVMValueRef result, cond, cond2, temp;
653bf215546Sopenharmony_ci
654bf215546Sopenharmony_ci   result = LLVMBuildFPTrunc(builder, src, bld_base->half_bld.vec_type, "");
655bf215546Sopenharmony_ci   result = LLVMBuildFPExt(builder, result, bld_base->base.vec_type, "");
656bf215546Sopenharmony_ci
657bf215546Sopenharmony_ci   temp = lp_build_abs(get_flt_bld(bld_base, 32), result);
658bf215546Sopenharmony_ci   cond = LLVMBuildFCmp(builder, LLVMRealOGT,
659bf215546Sopenharmony_ci                        LLVMBuildBitCast(builder, lp_build_const_int_vec(gallivm, bld_base->uint_bld.type, 0x38800000), bld_base->base.vec_type, ""),
660bf215546Sopenharmony_ci                        temp, "");
661bf215546Sopenharmony_ci   cond2 = LLVMBuildFCmp(builder, LLVMRealONE, temp, bld_base->base.zero, "");
662bf215546Sopenharmony_ci   cond = LLVMBuildAnd(builder, cond, cond2, "");
663bf215546Sopenharmony_ci   result = LLVMBuildSelect(builder, cond, bld_base->base.zero, result, "");
664bf215546Sopenharmony_ci   return result;
665bf215546Sopenharmony_ci}
666bf215546Sopenharmony_ci
667bf215546Sopenharmony_ci
668bf215546Sopenharmony_cistatic LLVMValueRef
669bf215546Sopenharmony_cido_alu_action(struct lp_build_nir_context *bld_base,
670bf215546Sopenharmony_ci              const nir_alu_instr *instr,
671bf215546Sopenharmony_ci              unsigned src_bit_size[NIR_MAX_VEC_COMPONENTS],
672bf215546Sopenharmony_ci              LLVMValueRef src[NIR_MAX_VEC_COMPONENTS])
673bf215546Sopenharmony_ci{
674bf215546Sopenharmony_ci   struct gallivm_state *gallivm = bld_base->base.gallivm;
675bf215546Sopenharmony_ci   LLVMBuilderRef builder = gallivm->builder;
676bf215546Sopenharmony_ci   LLVMValueRef result;
677bf215546Sopenharmony_ci
678bf215546Sopenharmony_ci   switch (instr->op) {
679bf215546Sopenharmony_ci   case nir_op_b2f16:
680bf215546Sopenharmony_ci      result = emit_b2f(bld_base, src[0], 16);
681bf215546Sopenharmony_ci      break;
682bf215546Sopenharmony_ci   case nir_op_b2f32:
683bf215546Sopenharmony_ci      result = emit_b2f(bld_base, src[0], 32);
684bf215546Sopenharmony_ci      break;
685bf215546Sopenharmony_ci   case nir_op_b2f64:
686bf215546Sopenharmony_ci      result = emit_b2f(bld_base, src[0], 64);
687bf215546Sopenharmony_ci      break;
688bf215546Sopenharmony_ci   case nir_op_b2i8:
689bf215546Sopenharmony_ci      result = emit_b2i(bld_base, src[0], 8);
690bf215546Sopenharmony_ci      break;
691bf215546Sopenharmony_ci   case nir_op_b2i16:
692bf215546Sopenharmony_ci      result = emit_b2i(bld_base, src[0], 16);
693bf215546Sopenharmony_ci      break;
694bf215546Sopenharmony_ci   case nir_op_b2i32:
695bf215546Sopenharmony_ci      result = emit_b2i(bld_base, src[0], 32);
696bf215546Sopenharmony_ci      break;
697bf215546Sopenharmony_ci   case nir_op_b2i64:
698bf215546Sopenharmony_ci      result = emit_b2i(bld_base, src[0], 64);
699bf215546Sopenharmony_ci      break;
700bf215546Sopenharmony_ci   case nir_op_b32csel:
701bf215546Sopenharmony_ci      result = emit_b32csel(bld_base, src_bit_size, src);
702bf215546Sopenharmony_ci      break;
703bf215546Sopenharmony_ci   case nir_op_bit_count:
704bf215546Sopenharmony_ci      result = lp_build_popcount(get_int_bld(bld_base, false, src_bit_size[0]), src[0]);
705bf215546Sopenharmony_ci      if (src_bit_size[0] < 32)
706bf215546Sopenharmony_ci         result = LLVMBuildZExt(builder, result, bld_base->int_bld.vec_type, "");
707bf215546Sopenharmony_ci      else if (src_bit_size[0] > 32)
708bf215546Sopenharmony_ci         result = LLVMBuildTrunc(builder, result, bld_base->int_bld.vec_type, "");
709bf215546Sopenharmony_ci      break;
710bf215546Sopenharmony_ci   case nir_op_bitfield_select:
711bf215546Sopenharmony_ci      result = lp_build_xor(&bld_base->uint_bld, src[2], lp_build_and(&bld_base->uint_bld, src[0], lp_build_xor(&bld_base->uint_bld, src[1], src[2])));
712bf215546Sopenharmony_ci      break;
713bf215546Sopenharmony_ci   case nir_op_bitfield_reverse:
714bf215546Sopenharmony_ci      result = lp_build_bitfield_reverse(get_int_bld(bld_base, false, src_bit_size[0]), src[0]);
715bf215546Sopenharmony_ci      break;
716bf215546Sopenharmony_ci   case nir_op_f2b32:
717bf215546Sopenharmony_ci      result = flt_to_bool32(bld_base, src_bit_size[0], src[0]);
718bf215546Sopenharmony_ci      break;
719bf215546Sopenharmony_ci   case nir_op_f2f16:
720bf215546Sopenharmony_ci      if (src_bit_size[0] == 64)
721bf215546Sopenharmony_ci         src[0] = LLVMBuildFPTrunc(builder, src[0],
722bf215546Sopenharmony_ci                                   bld_base->base.vec_type, "");
723bf215546Sopenharmony_ci      result = LLVMBuildFPTrunc(builder, src[0],
724bf215546Sopenharmony_ci                                bld_base->half_bld.vec_type, "");
725bf215546Sopenharmony_ci      break;
726bf215546Sopenharmony_ci   case nir_op_f2f32:
727bf215546Sopenharmony_ci      if (src_bit_size[0] < 32)
728bf215546Sopenharmony_ci         result = LLVMBuildFPExt(builder, src[0],
729bf215546Sopenharmony_ci                                 bld_base->base.vec_type, "");
730bf215546Sopenharmony_ci      else
731bf215546Sopenharmony_ci         result = LLVMBuildFPTrunc(builder, src[0],
732bf215546Sopenharmony_ci                                   bld_base->base.vec_type, "");
733bf215546Sopenharmony_ci      break;
734bf215546Sopenharmony_ci   case nir_op_f2f64:
735bf215546Sopenharmony_ci      result = LLVMBuildFPExt(builder, src[0],
736bf215546Sopenharmony_ci                              bld_base->dbl_bld.vec_type, "");
737bf215546Sopenharmony_ci      break;
738bf215546Sopenharmony_ci   case nir_op_f2i8:
739bf215546Sopenharmony_ci      result = LLVMBuildFPToSI(builder,
740bf215546Sopenharmony_ci                               src[0],
741bf215546Sopenharmony_ci                               bld_base->uint8_bld.vec_type, "");
742bf215546Sopenharmony_ci      break;
743bf215546Sopenharmony_ci   case nir_op_f2i16:
744bf215546Sopenharmony_ci      result = LLVMBuildFPToSI(builder,
745bf215546Sopenharmony_ci                               src[0],
746bf215546Sopenharmony_ci                               bld_base->uint16_bld.vec_type, "");
747bf215546Sopenharmony_ci      break;
748bf215546Sopenharmony_ci   case nir_op_f2i32:
749bf215546Sopenharmony_ci      result = LLVMBuildFPToSI(builder, src[0], bld_base->base.int_vec_type, "");
750bf215546Sopenharmony_ci      break;
751bf215546Sopenharmony_ci   case nir_op_f2u8:
752bf215546Sopenharmony_ci      result = LLVMBuildFPToUI(builder,
753bf215546Sopenharmony_ci                               src[0],
754bf215546Sopenharmony_ci                               bld_base->uint8_bld.vec_type, "");
755bf215546Sopenharmony_ci      break;
756bf215546Sopenharmony_ci   case nir_op_f2u16:
757bf215546Sopenharmony_ci      result = LLVMBuildFPToUI(builder,
758bf215546Sopenharmony_ci                               src[0],
759bf215546Sopenharmony_ci                               bld_base->uint16_bld.vec_type, "");
760bf215546Sopenharmony_ci      break;
761bf215546Sopenharmony_ci   case nir_op_f2u32:
762bf215546Sopenharmony_ci      result = LLVMBuildFPToUI(builder,
763bf215546Sopenharmony_ci                               src[0],
764bf215546Sopenharmony_ci                               bld_base->base.int_vec_type, "");
765bf215546Sopenharmony_ci      break;
766bf215546Sopenharmony_ci   case nir_op_f2i64:
767bf215546Sopenharmony_ci      result = LLVMBuildFPToSI(builder,
768bf215546Sopenharmony_ci                               src[0],
769bf215546Sopenharmony_ci                               bld_base->int64_bld.vec_type, "");
770bf215546Sopenharmony_ci      break;
771bf215546Sopenharmony_ci   case nir_op_f2u64:
772bf215546Sopenharmony_ci      result = LLVMBuildFPToUI(builder,
773bf215546Sopenharmony_ci                               src[0],
774bf215546Sopenharmony_ci                               bld_base->uint64_bld.vec_type, "");
775bf215546Sopenharmony_ci      break;
776bf215546Sopenharmony_ci   case nir_op_fabs:
777bf215546Sopenharmony_ci      result = lp_build_abs(get_flt_bld(bld_base, src_bit_size[0]), src[0]);
778bf215546Sopenharmony_ci      break;
779bf215546Sopenharmony_ci   case nir_op_fadd:
780bf215546Sopenharmony_ci      result = lp_build_add(get_flt_bld(bld_base, src_bit_size[0]),
781bf215546Sopenharmony_ci                            src[0], src[1]);
782bf215546Sopenharmony_ci      break;
783bf215546Sopenharmony_ci   case nir_op_fceil:
784bf215546Sopenharmony_ci      result = lp_build_ceil(get_flt_bld(bld_base, src_bit_size[0]), src[0]);
785bf215546Sopenharmony_ci      break;
786bf215546Sopenharmony_ci   case nir_op_fcos:
787bf215546Sopenharmony_ci      result = lp_build_cos(get_flt_bld(bld_base, src_bit_size[0]), src[0]);
788bf215546Sopenharmony_ci      break;
789bf215546Sopenharmony_ci   case nir_op_fddx:
790bf215546Sopenharmony_ci   case nir_op_fddx_coarse:
791bf215546Sopenharmony_ci   case nir_op_fddx_fine:
792bf215546Sopenharmony_ci      result = lp_build_ddx(get_flt_bld(bld_base, src_bit_size[0]), src[0]);
793bf215546Sopenharmony_ci      break;
794bf215546Sopenharmony_ci   case nir_op_fddy:
795bf215546Sopenharmony_ci   case nir_op_fddy_coarse:
796bf215546Sopenharmony_ci   case nir_op_fddy_fine:
797bf215546Sopenharmony_ci      result = lp_build_ddy(get_flt_bld(bld_base, src_bit_size[0]), src[0]);
798bf215546Sopenharmony_ci      break;
799bf215546Sopenharmony_ci   case nir_op_fdiv:
800bf215546Sopenharmony_ci      result = lp_build_div(get_flt_bld(bld_base, src_bit_size[0]),
801bf215546Sopenharmony_ci                            src[0], src[1]);
802bf215546Sopenharmony_ci      break;
803bf215546Sopenharmony_ci   case nir_op_feq32:
804bf215546Sopenharmony_ci      result = fcmp32(bld_base, PIPE_FUNC_EQUAL, src_bit_size[0], src);
805bf215546Sopenharmony_ci      break;
806bf215546Sopenharmony_ci   case nir_op_fexp2:
807bf215546Sopenharmony_ci      result = lp_build_exp2(get_flt_bld(bld_base, src_bit_size[0]), src[0]);
808bf215546Sopenharmony_ci      break;
809bf215546Sopenharmony_ci   case nir_op_ffloor:
810bf215546Sopenharmony_ci      result = lp_build_floor(get_flt_bld(bld_base, src_bit_size[0]), src[0]);
811bf215546Sopenharmony_ci      break;
812bf215546Sopenharmony_ci   case nir_op_ffma:
813bf215546Sopenharmony_ci      result = lp_build_fmuladd(builder, src[0], src[1], src[2]);
814bf215546Sopenharmony_ci      break;
815bf215546Sopenharmony_ci   case nir_op_ffract: {
816bf215546Sopenharmony_ci      struct lp_build_context *flt_bld = get_flt_bld(bld_base, src_bit_size[0]);
817bf215546Sopenharmony_ci      LLVMValueRef tmp = lp_build_floor(flt_bld, src[0]);
818bf215546Sopenharmony_ci      result = lp_build_sub(flt_bld, src[0], tmp);
819bf215546Sopenharmony_ci      break;
820bf215546Sopenharmony_ci   }
821bf215546Sopenharmony_ci   case nir_op_fge:
822bf215546Sopenharmony_ci   case nir_op_fge32:
823bf215546Sopenharmony_ci      result = fcmp32(bld_base, PIPE_FUNC_GEQUAL, src_bit_size[0], src);
824bf215546Sopenharmony_ci      break;
825bf215546Sopenharmony_ci   case nir_op_find_lsb: {
826bf215546Sopenharmony_ci      struct lp_build_context *int_bld = get_int_bld(bld_base, false, src_bit_size[0]);
827bf215546Sopenharmony_ci      result = lp_build_cttz(int_bld, src[0]);
828bf215546Sopenharmony_ci      if (src_bit_size[0] < 32)
829bf215546Sopenharmony_ci         result = LLVMBuildZExt(builder, result, bld_base->uint_bld.vec_type, "");
830bf215546Sopenharmony_ci      else if (src_bit_size[0] > 32)
831bf215546Sopenharmony_ci         result = LLVMBuildTrunc(builder, result, bld_base->uint_bld.vec_type, "");
832bf215546Sopenharmony_ci      break;
833bf215546Sopenharmony_ci   }
834bf215546Sopenharmony_ci   case nir_op_fisfinite32:
835bf215546Sopenharmony_ci      unreachable("Should have been lowered in nir_opt_algebraic_late.");
836bf215546Sopenharmony_ci   case nir_op_flog2:
837bf215546Sopenharmony_ci      result = lp_build_log2_safe(get_flt_bld(bld_base, src_bit_size[0]), src[0]);
838bf215546Sopenharmony_ci      break;
839bf215546Sopenharmony_ci   case nir_op_flt:
840bf215546Sopenharmony_ci   case nir_op_flt32:
841bf215546Sopenharmony_ci      result = fcmp32(bld_base, PIPE_FUNC_LESS, src_bit_size[0], src);
842bf215546Sopenharmony_ci      break;
843bf215546Sopenharmony_ci   case nir_op_fmax:
844bf215546Sopenharmony_ci   case nir_op_fmin: {
845bf215546Sopenharmony_ci      enum gallivm_nan_behavior minmax_nan;
846bf215546Sopenharmony_ci      int first = 0;
847bf215546Sopenharmony_ci
848bf215546Sopenharmony_ci      /* If one of the sources is known to be a number (i.e., not NaN), then
849bf215546Sopenharmony_ci       * better code can be generated by passing that information along.
850bf215546Sopenharmony_ci       */
851bf215546Sopenharmony_ci      if (is_a_number(bld_base->range_ht, instr, 1,
852bf215546Sopenharmony_ci                      0 /* unused num_components */,
853bf215546Sopenharmony_ci                      NULL /* unused swizzle */)) {
854bf215546Sopenharmony_ci         minmax_nan = GALLIVM_NAN_RETURN_OTHER_SECOND_NONNAN;
855bf215546Sopenharmony_ci      } else if (is_a_number(bld_base->range_ht, instr, 0,
856bf215546Sopenharmony_ci                             0 /* unused num_components */,
857bf215546Sopenharmony_ci                             NULL /* unused swizzle */)) {
858bf215546Sopenharmony_ci         first = 1;
859bf215546Sopenharmony_ci         minmax_nan = GALLIVM_NAN_RETURN_OTHER_SECOND_NONNAN;
860bf215546Sopenharmony_ci      } else {
861bf215546Sopenharmony_ci         minmax_nan = GALLIVM_NAN_RETURN_OTHER;
862bf215546Sopenharmony_ci      }
863bf215546Sopenharmony_ci
864bf215546Sopenharmony_ci      if (instr->op == nir_op_fmin) {
865bf215546Sopenharmony_ci         result = lp_build_min_ext(get_flt_bld(bld_base, src_bit_size[0]),
866bf215546Sopenharmony_ci                                   src[first], src[1 - first], minmax_nan);
867bf215546Sopenharmony_ci      } else {
868bf215546Sopenharmony_ci         result = lp_build_max_ext(get_flt_bld(bld_base, src_bit_size[0]),
869bf215546Sopenharmony_ci                                   src[first], src[1 - first], minmax_nan);
870bf215546Sopenharmony_ci      }
871bf215546Sopenharmony_ci      break;
872bf215546Sopenharmony_ci   }
873bf215546Sopenharmony_ci   case nir_op_fmod: {
874bf215546Sopenharmony_ci      struct lp_build_context *flt_bld = get_flt_bld(bld_base, src_bit_size[0]);
875bf215546Sopenharmony_ci      result = lp_build_div(flt_bld, src[0], src[1]);
876bf215546Sopenharmony_ci      result = lp_build_floor(flt_bld, result);
877bf215546Sopenharmony_ci      result = lp_build_mul(flt_bld, src[1], result);
878bf215546Sopenharmony_ci      result = lp_build_sub(flt_bld, src[0], result);
879bf215546Sopenharmony_ci      break;
880bf215546Sopenharmony_ci   }
881bf215546Sopenharmony_ci   case nir_op_fmul:
882bf215546Sopenharmony_ci      result = lp_build_mul(get_flt_bld(bld_base, src_bit_size[0]),
883bf215546Sopenharmony_ci                            src[0], src[1]);
884bf215546Sopenharmony_ci      break;
885bf215546Sopenharmony_ci   case nir_op_fneu32:
886bf215546Sopenharmony_ci      result = fcmp32(bld_base, PIPE_FUNC_NOTEQUAL, src_bit_size[0], src);
887bf215546Sopenharmony_ci      break;
888bf215546Sopenharmony_ci   case nir_op_fneg:
889bf215546Sopenharmony_ci      result = lp_build_negate(get_flt_bld(bld_base, src_bit_size[0]), src[0]);
890bf215546Sopenharmony_ci      break;
891bf215546Sopenharmony_ci   case nir_op_fpow:
892bf215546Sopenharmony_ci      result = lp_build_pow(get_flt_bld(bld_base, src_bit_size[0]), src[0], src[1]);
893bf215546Sopenharmony_ci      break;
894bf215546Sopenharmony_ci   case nir_op_fquantize2f16:
895bf215546Sopenharmony_ci      result = do_quantize_to_f16(bld_base, src[0]);
896bf215546Sopenharmony_ci      break;
897bf215546Sopenharmony_ci   case nir_op_frcp:
898bf215546Sopenharmony_ci      result = lp_build_rcp(get_flt_bld(bld_base, src_bit_size[0]), src[0]);
899bf215546Sopenharmony_ci      break;
900bf215546Sopenharmony_ci   case nir_op_fround_even:
901bf215546Sopenharmony_ci      if (src_bit_size[0] == 16) {
902bf215546Sopenharmony_ci         struct lp_build_context *bld = get_flt_bld(bld_base, 16);
903bf215546Sopenharmony_ci         char intrinsic[64];
904bf215546Sopenharmony_ci         lp_format_intrinsic(intrinsic, 64, "llvm.roundeven", bld->vec_type);
905bf215546Sopenharmony_ci         result = lp_build_intrinsic_unary(builder, intrinsic, bld->vec_type, src[0]);
906bf215546Sopenharmony_ci      } else {
907bf215546Sopenharmony_ci         result = lp_build_round(get_flt_bld(bld_base, src_bit_size[0]), src[0]);
908bf215546Sopenharmony_ci      }
909bf215546Sopenharmony_ci      break;
910bf215546Sopenharmony_ci   case nir_op_frsq:
911bf215546Sopenharmony_ci      result = lp_build_rsqrt(get_flt_bld(bld_base, src_bit_size[0]), src[0]);
912bf215546Sopenharmony_ci      break;
913bf215546Sopenharmony_ci   case nir_op_fsat:
914bf215546Sopenharmony_ci      result = lp_build_clamp_zero_one_nanzero(get_flt_bld(bld_base, src_bit_size[0]), src[0]);
915bf215546Sopenharmony_ci      break;
916bf215546Sopenharmony_ci   case nir_op_fsign:
917bf215546Sopenharmony_ci      result = lp_build_sgn(get_flt_bld(bld_base, src_bit_size[0]), src[0]);
918bf215546Sopenharmony_ci      break;
919bf215546Sopenharmony_ci   case nir_op_fsin:
920bf215546Sopenharmony_ci      result = lp_build_sin(get_flt_bld(bld_base, src_bit_size[0]), src[0]);
921bf215546Sopenharmony_ci      break;
922bf215546Sopenharmony_ci   case nir_op_fsqrt:
923bf215546Sopenharmony_ci      result = lp_build_sqrt(get_flt_bld(bld_base, src_bit_size[0]), src[0]);
924bf215546Sopenharmony_ci      break;
925bf215546Sopenharmony_ci   case nir_op_ftrunc:
926bf215546Sopenharmony_ci      result = lp_build_trunc(get_flt_bld(bld_base, src_bit_size[0]), src[0]);
927bf215546Sopenharmony_ci      break;
928bf215546Sopenharmony_ci   case nir_op_i2b32:
929bf215546Sopenharmony_ci      result = int_to_bool32(bld_base, src_bit_size[0], false, src[0]);
930bf215546Sopenharmony_ci      break;
931bf215546Sopenharmony_ci   case nir_op_i2f16:
932bf215546Sopenharmony_ci      result = LLVMBuildSIToFP(builder, src[0],
933bf215546Sopenharmony_ci                               bld_base->half_bld.vec_type, "");
934bf215546Sopenharmony_ci      break;
935bf215546Sopenharmony_ci   case nir_op_i2f32:
936bf215546Sopenharmony_ci      result = lp_build_int_to_float(&bld_base->base, src[0]);
937bf215546Sopenharmony_ci      break;
938bf215546Sopenharmony_ci   case nir_op_i2f64:
939bf215546Sopenharmony_ci      result = lp_build_int_to_float(&bld_base->dbl_bld, src[0]);
940bf215546Sopenharmony_ci      break;
941bf215546Sopenharmony_ci   case nir_op_i2i8:
942bf215546Sopenharmony_ci      result = LLVMBuildTrunc(builder, src[0], bld_base->int8_bld.vec_type, "");
943bf215546Sopenharmony_ci      break;
944bf215546Sopenharmony_ci   case nir_op_i2i16:
945bf215546Sopenharmony_ci      if (src_bit_size[0] < 16)
946bf215546Sopenharmony_ci         result = LLVMBuildSExt(builder, src[0], bld_base->int16_bld.vec_type, "");
947bf215546Sopenharmony_ci      else
948bf215546Sopenharmony_ci         result = LLVMBuildTrunc(builder, src[0], bld_base->int16_bld.vec_type, "");
949bf215546Sopenharmony_ci      break;
950bf215546Sopenharmony_ci   case nir_op_i2i32:
951bf215546Sopenharmony_ci      if (src_bit_size[0] < 32)
952bf215546Sopenharmony_ci         result = LLVMBuildSExt(builder, src[0], bld_base->int_bld.vec_type, "");
953bf215546Sopenharmony_ci      else
954bf215546Sopenharmony_ci         result = LLVMBuildTrunc(builder, src[0], bld_base->int_bld.vec_type, "");
955bf215546Sopenharmony_ci      break;
956bf215546Sopenharmony_ci   case nir_op_i2i64:
957bf215546Sopenharmony_ci      result = LLVMBuildSExt(builder, src[0], bld_base->int64_bld.vec_type, "");
958bf215546Sopenharmony_ci      break;
959bf215546Sopenharmony_ci   case nir_op_iabs:
960bf215546Sopenharmony_ci      result = lp_build_abs(get_int_bld(bld_base, false, src_bit_size[0]), src[0]);
961bf215546Sopenharmony_ci      break;
962bf215546Sopenharmony_ci   case nir_op_iadd:
963bf215546Sopenharmony_ci      result = lp_build_add(get_int_bld(bld_base, false, src_bit_size[0]),
964bf215546Sopenharmony_ci                            src[0], src[1]);
965bf215546Sopenharmony_ci      break;
966bf215546Sopenharmony_ci   case nir_op_iand:
967bf215546Sopenharmony_ci      result = lp_build_and(get_int_bld(bld_base, false, src_bit_size[0]),
968bf215546Sopenharmony_ci                            src[0], src[1]);
969bf215546Sopenharmony_ci      break;
970bf215546Sopenharmony_ci   case nir_op_idiv:
971bf215546Sopenharmony_ci      result = do_int_divide(bld_base, false, src_bit_size[0], src[0], src[1]);
972bf215546Sopenharmony_ci      break;
973bf215546Sopenharmony_ci   case nir_op_ieq32:
974bf215546Sopenharmony_ci      result = icmp32(bld_base, PIPE_FUNC_EQUAL, false, src_bit_size[0], src);
975bf215546Sopenharmony_ci      break;
976bf215546Sopenharmony_ci   case nir_op_ige32:
977bf215546Sopenharmony_ci      result = icmp32(bld_base, PIPE_FUNC_GEQUAL, false, src_bit_size[0], src);
978bf215546Sopenharmony_ci      break;
979bf215546Sopenharmony_ci   case nir_op_ilt32:
980bf215546Sopenharmony_ci      result = icmp32(bld_base, PIPE_FUNC_LESS, false, src_bit_size[0], src);
981bf215546Sopenharmony_ci      break;
982bf215546Sopenharmony_ci   case nir_op_imax:
983bf215546Sopenharmony_ci      result = lp_build_max(get_int_bld(bld_base, false, src_bit_size[0]), src[0], src[1]);
984bf215546Sopenharmony_ci      break;
985bf215546Sopenharmony_ci   case nir_op_imin:
986bf215546Sopenharmony_ci      result = lp_build_min(get_int_bld(bld_base, false, src_bit_size[0]), src[0], src[1]);
987bf215546Sopenharmony_ci      break;
988bf215546Sopenharmony_ci   case nir_op_imul:
989bf215546Sopenharmony_ci   case nir_op_imul24:
990bf215546Sopenharmony_ci      result = lp_build_mul(get_int_bld(bld_base, false, src_bit_size[0]),
991bf215546Sopenharmony_ci                            src[0], src[1]);
992bf215546Sopenharmony_ci      break;
993bf215546Sopenharmony_ci   case nir_op_imul_high: {
994bf215546Sopenharmony_ci      LLVMValueRef hi_bits;
995bf215546Sopenharmony_ci      lp_build_mul_32_lohi(get_int_bld(bld_base, false, src_bit_size[0]), src[0], src[1], &hi_bits);
996bf215546Sopenharmony_ci      result = hi_bits;
997bf215546Sopenharmony_ci      break;
998bf215546Sopenharmony_ci   }
999bf215546Sopenharmony_ci   case nir_op_ine32:
1000bf215546Sopenharmony_ci      result = icmp32(bld_base, PIPE_FUNC_NOTEQUAL, false, src_bit_size[0], src);
1001bf215546Sopenharmony_ci      break;
1002bf215546Sopenharmony_ci   case nir_op_ineg:
1003bf215546Sopenharmony_ci      result = lp_build_negate(get_int_bld(bld_base, false, src_bit_size[0]), src[0]);
1004bf215546Sopenharmony_ci      break;
1005bf215546Sopenharmony_ci   case nir_op_inot:
1006bf215546Sopenharmony_ci      result = lp_build_not(get_int_bld(bld_base, false, src_bit_size[0]), src[0]);
1007bf215546Sopenharmony_ci      break;
1008bf215546Sopenharmony_ci   case nir_op_ior:
1009bf215546Sopenharmony_ci      result = lp_build_or(get_int_bld(bld_base, false, src_bit_size[0]),
1010bf215546Sopenharmony_ci                           src[0], src[1]);
1011bf215546Sopenharmony_ci      break;
1012bf215546Sopenharmony_ci   case nir_op_imod:
1013bf215546Sopenharmony_ci   case nir_op_irem:
1014bf215546Sopenharmony_ci      result = do_int_mod(bld_base, false, src_bit_size[0], src[0], src[1]);
1015bf215546Sopenharmony_ci      break;
1016bf215546Sopenharmony_ci   case nir_op_ishl: {
1017bf215546Sopenharmony_ci      struct lp_build_context *uint_bld = get_int_bld(bld_base, true, src_bit_size[0]);
1018bf215546Sopenharmony_ci      struct lp_build_context *int_bld = get_int_bld(bld_base, false, src_bit_size[0]);
1019bf215546Sopenharmony_ci      if (src_bit_size[0] == 64)
1020bf215546Sopenharmony_ci         src[1] = LLVMBuildZExt(builder, src[1], uint_bld->vec_type, "");
1021bf215546Sopenharmony_ci      if (src_bit_size[0] < 32)
1022bf215546Sopenharmony_ci         src[1] = LLVMBuildTrunc(builder, src[1], uint_bld->vec_type, "");
1023bf215546Sopenharmony_ci      src[1] = lp_build_and(uint_bld, src[1], lp_build_const_int_vec(gallivm, uint_bld->type, (src_bit_size[0] - 1)));
1024bf215546Sopenharmony_ci      result = lp_build_shl(int_bld, src[0], src[1]);
1025bf215546Sopenharmony_ci      break;
1026bf215546Sopenharmony_ci   }
1027bf215546Sopenharmony_ci   case nir_op_ishr: {
1028bf215546Sopenharmony_ci      struct lp_build_context *uint_bld = get_int_bld(bld_base, true, src_bit_size[0]);
1029bf215546Sopenharmony_ci      struct lp_build_context *int_bld = get_int_bld(bld_base, false, src_bit_size[0]);
1030bf215546Sopenharmony_ci      if (src_bit_size[0] == 64)
1031bf215546Sopenharmony_ci         src[1] = LLVMBuildZExt(builder, src[1], uint_bld->vec_type, "");
1032bf215546Sopenharmony_ci      if (src_bit_size[0] < 32)
1033bf215546Sopenharmony_ci         src[1] = LLVMBuildTrunc(builder, src[1], uint_bld->vec_type, "");
1034bf215546Sopenharmony_ci      src[1] = lp_build_and(uint_bld, src[1], lp_build_const_int_vec(gallivm, uint_bld->type, (src_bit_size[0] - 1)));
1035bf215546Sopenharmony_ci      result = lp_build_shr(int_bld, src[0], src[1]);
1036bf215546Sopenharmony_ci      break;
1037bf215546Sopenharmony_ci   }
1038bf215546Sopenharmony_ci   case nir_op_isign:
1039bf215546Sopenharmony_ci      result = lp_build_sgn(get_int_bld(bld_base, false, src_bit_size[0]), src[0]);
1040bf215546Sopenharmony_ci      break;
1041bf215546Sopenharmony_ci   case nir_op_isub:
1042bf215546Sopenharmony_ci      result = lp_build_sub(get_int_bld(bld_base, false, src_bit_size[0]),
1043bf215546Sopenharmony_ci                            src[0], src[1]);
1044bf215546Sopenharmony_ci      break;
1045bf215546Sopenharmony_ci   case nir_op_ixor:
1046bf215546Sopenharmony_ci      result = lp_build_xor(get_int_bld(bld_base, false, src_bit_size[0]),
1047bf215546Sopenharmony_ci                            src[0], src[1]);
1048bf215546Sopenharmony_ci      break;
1049bf215546Sopenharmony_ci   case nir_op_mov:
1050bf215546Sopenharmony_ci      result = src[0];
1051bf215546Sopenharmony_ci      break;
1052bf215546Sopenharmony_ci   case nir_op_unpack_64_2x32_split_x:
1053bf215546Sopenharmony_ci      result = split_64bit(bld_base, src[0], false);
1054bf215546Sopenharmony_ci      break;
1055bf215546Sopenharmony_ci   case nir_op_unpack_64_2x32_split_y:
1056bf215546Sopenharmony_ci      result = split_64bit(bld_base, src[0], true);
1057bf215546Sopenharmony_ci      break;
1058bf215546Sopenharmony_ci
1059bf215546Sopenharmony_ci   case nir_op_pack_32_2x16_split: {
1060bf215546Sopenharmony_ci      LLVMValueRef tmp = merge_16bit(bld_base, src[0], src[1]);
1061bf215546Sopenharmony_ci      result = LLVMBuildBitCast(builder, tmp, bld_base->base.vec_type, "");
1062bf215546Sopenharmony_ci      break;
1063bf215546Sopenharmony_ci   }
1064bf215546Sopenharmony_ci   case nir_op_unpack_32_2x16_split_x:
1065bf215546Sopenharmony_ci      result = split_16bit(bld_base, src[0], false);
1066bf215546Sopenharmony_ci      break;
1067bf215546Sopenharmony_ci   case nir_op_unpack_32_2x16_split_y:
1068bf215546Sopenharmony_ci      result = split_16bit(bld_base, src[0], true);
1069bf215546Sopenharmony_ci      break;
1070bf215546Sopenharmony_ci   case nir_op_pack_64_2x32_split: {
1071bf215546Sopenharmony_ci      LLVMValueRef tmp = merge_64bit(bld_base, src[0], src[1]);
1072bf215546Sopenharmony_ci      result = LLVMBuildBitCast(builder, tmp, bld_base->uint64_bld.vec_type, "");
1073bf215546Sopenharmony_ci      break;
1074bf215546Sopenharmony_ci   }
1075bf215546Sopenharmony_ci   case nir_op_pack_32_4x8_split: {
1076bf215546Sopenharmony_ci      LLVMValueRef tmp1 = merge_16bit(bld_base, src[0], src[1]);
1077bf215546Sopenharmony_ci      LLVMValueRef tmp2 = merge_16bit(bld_base, src[2], src[3]);
1078bf215546Sopenharmony_ci      tmp1 = LLVMBuildBitCast(builder, tmp1, bld_base->uint16_bld.vec_type, "");
1079bf215546Sopenharmony_ci      tmp2 = LLVMBuildBitCast(builder, tmp2, bld_base->uint16_bld.vec_type, "");
1080bf215546Sopenharmony_ci      LLVMValueRef tmp = merge_16bit(bld_base, tmp1, tmp2);
1081bf215546Sopenharmony_ci      result = LLVMBuildBitCast(builder, tmp, bld_base->uint_bld.vec_type, "");
1082bf215546Sopenharmony_ci      break;
1083bf215546Sopenharmony_ci   }
1084bf215546Sopenharmony_ci   case nir_op_u2f16:
1085bf215546Sopenharmony_ci      result = LLVMBuildUIToFP(builder, src[0],
1086bf215546Sopenharmony_ci                               bld_base->half_bld.vec_type, "");
1087bf215546Sopenharmony_ci      break;
1088bf215546Sopenharmony_ci   case nir_op_u2f32:
1089bf215546Sopenharmony_ci      result = LLVMBuildUIToFP(builder, src[0], bld_base->base.vec_type, "");
1090bf215546Sopenharmony_ci      break;
1091bf215546Sopenharmony_ci   case nir_op_u2f64:
1092bf215546Sopenharmony_ci      result = LLVMBuildUIToFP(builder, src[0], bld_base->dbl_bld.vec_type, "");
1093bf215546Sopenharmony_ci      break;
1094bf215546Sopenharmony_ci   case nir_op_u2u8:
1095bf215546Sopenharmony_ci      result = LLVMBuildTrunc(builder, src[0], bld_base->uint8_bld.vec_type, "");
1096bf215546Sopenharmony_ci      break;
1097bf215546Sopenharmony_ci   case nir_op_u2u16:
1098bf215546Sopenharmony_ci      if (src_bit_size[0] < 16)
1099bf215546Sopenharmony_ci         result = LLVMBuildZExt(builder, src[0], bld_base->uint16_bld.vec_type, "");
1100bf215546Sopenharmony_ci      else
1101bf215546Sopenharmony_ci         result = LLVMBuildTrunc(builder, src[0], bld_base->uint16_bld.vec_type, "");
1102bf215546Sopenharmony_ci      break;
1103bf215546Sopenharmony_ci   case nir_op_u2u32:
1104bf215546Sopenharmony_ci      if (src_bit_size[0] < 32)
1105bf215546Sopenharmony_ci         result = LLVMBuildZExt(builder, src[0], bld_base->uint_bld.vec_type, "");
1106bf215546Sopenharmony_ci      else
1107bf215546Sopenharmony_ci         result = LLVMBuildTrunc(builder, src[0], bld_base->uint_bld.vec_type, "");
1108bf215546Sopenharmony_ci      break;
1109bf215546Sopenharmony_ci   case nir_op_u2u64:
1110bf215546Sopenharmony_ci      result = LLVMBuildZExt(builder, src[0], bld_base->uint64_bld.vec_type, "");
1111bf215546Sopenharmony_ci      break;
1112bf215546Sopenharmony_ci   case nir_op_udiv:
1113bf215546Sopenharmony_ci      result = do_int_divide(bld_base, true, src_bit_size[0], src[0], src[1]);
1114bf215546Sopenharmony_ci      break;
1115bf215546Sopenharmony_ci   case nir_op_ufind_msb: {
1116bf215546Sopenharmony_ci      struct lp_build_context *uint_bld = get_int_bld(bld_base, true, src_bit_size[0]);
1117bf215546Sopenharmony_ci      result = lp_build_ctlz(uint_bld, src[0]);
1118bf215546Sopenharmony_ci      result = lp_build_sub(uint_bld, lp_build_const_int_vec(gallivm, uint_bld->type, src_bit_size[0] - 1), result);
1119bf215546Sopenharmony_ci      if (src_bit_size[0] < 32)
1120bf215546Sopenharmony_ci         result = LLVMBuildZExt(builder, result, bld_base->uint_bld.vec_type, "");
1121bf215546Sopenharmony_ci      else
1122bf215546Sopenharmony_ci         result = LLVMBuildTrunc(builder, result, bld_base->uint_bld.vec_type, "");
1123bf215546Sopenharmony_ci      break;
1124bf215546Sopenharmony_ci   }
1125bf215546Sopenharmony_ci   case nir_op_uge32:
1126bf215546Sopenharmony_ci      result = icmp32(bld_base, PIPE_FUNC_GEQUAL, true, src_bit_size[0], src);
1127bf215546Sopenharmony_ci      break;
1128bf215546Sopenharmony_ci   case nir_op_ult32:
1129bf215546Sopenharmony_ci      result = icmp32(bld_base, PIPE_FUNC_LESS, true, src_bit_size[0], src);
1130bf215546Sopenharmony_ci      break;
1131bf215546Sopenharmony_ci   case nir_op_umax:
1132bf215546Sopenharmony_ci      result = lp_build_max(get_int_bld(bld_base, true, src_bit_size[0]), src[0], src[1]);
1133bf215546Sopenharmony_ci      break;
1134bf215546Sopenharmony_ci   case nir_op_umin:
1135bf215546Sopenharmony_ci      result = lp_build_min(get_int_bld(bld_base, true, src_bit_size[0]), src[0], src[1]);
1136bf215546Sopenharmony_ci      break;
1137bf215546Sopenharmony_ci   case nir_op_umod:
1138bf215546Sopenharmony_ci      result = do_int_mod(bld_base, true, src_bit_size[0], src[0], src[1]);
1139bf215546Sopenharmony_ci      break;
1140bf215546Sopenharmony_ci   case nir_op_umul_high: {
1141bf215546Sopenharmony_ci      LLVMValueRef hi_bits;
1142bf215546Sopenharmony_ci      lp_build_mul_32_lohi(get_int_bld(bld_base, true, src_bit_size[0]), src[0], src[1], &hi_bits);
1143bf215546Sopenharmony_ci      result = hi_bits;
1144bf215546Sopenharmony_ci      break;
1145bf215546Sopenharmony_ci   }
1146bf215546Sopenharmony_ci   case nir_op_ushr: {
1147bf215546Sopenharmony_ci      struct lp_build_context *uint_bld = get_int_bld(bld_base, true, src_bit_size[0]);
1148bf215546Sopenharmony_ci      if (src_bit_size[0] == 64)
1149bf215546Sopenharmony_ci         src[1] = LLVMBuildZExt(builder, src[1], uint_bld->vec_type, "");
1150bf215546Sopenharmony_ci      if (src_bit_size[0] < 32)
1151bf215546Sopenharmony_ci         src[1] = LLVMBuildTrunc(builder, src[1], uint_bld->vec_type, "");
1152bf215546Sopenharmony_ci      src[1] = lp_build_and(uint_bld, src[1], lp_build_const_int_vec(gallivm, uint_bld->type, (src_bit_size[0] - 1)));
1153bf215546Sopenharmony_ci      result = lp_build_shr(uint_bld, src[0], src[1]);
1154bf215546Sopenharmony_ci      break;
1155bf215546Sopenharmony_ci   }
1156bf215546Sopenharmony_ci   case nir_op_bcsel: {
1157bf215546Sopenharmony_ci      LLVMTypeRef src1_type = LLVMTypeOf(src[1]);
1158bf215546Sopenharmony_ci      LLVMTypeRef src2_type = LLVMTypeOf(src[2]);
1159bf215546Sopenharmony_ci
1160bf215546Sopenharmony_ci      if (LLVMGetTypeKind(src1_type) == LLVMPointerTypeKind &&
1161bf215546Sopenharmony_ci          LLVMGetTypeKind(src2_type) != LLVMPointerTypeKind) {
1162bf215546Sopenharmony_ci         src[2] = LLVMBuildIntToPtr(builder, src[2], src1_type, "");
1163bf215546Sopenharmony_ci      } else if (LLVMGetTypeKind(src2_type) == LLVMPointerTypeKind &&
1164bf215546Sopenharmony_ci                 LLVMGetTypeKind(src1_type) != LLVMPointerTypeKind) {
1165bf215546Sopenharmony_ci         src[1] = LLVMBuildIntToPtr(builder, src[1], src2_type, "");
1166bf215546Sopenharmony_ci      }
1167bf215546Sopenharmony_ci
1168bf215546Sopenharmony_ci      for (int i = 1; i <= 2; i++) {
1169bf215546Sopenharmony_ci         LLVMTypeRef type = LLVMTypeOf(src[i]);
1170bf215546Sopenharmony_ci         if (LLVMGetTypeKind(type) == LLVMPointerTypeKind)
1171bf215546Sopenharmony_ci            break;
1172bf215546Sopenharmony_ci         src[i] = LLVMBuildBitCast(builder, src[i], get_int_bld(bld_base, true, src_bit_size[i])->vec_type, "");
1173bf215546Sopenharmony_ci      }
1174bf215546Sopenharmony_ci      return LLVMBuildSelect(builder, src[0], src[1], src[2], "");
1175bf215546Sopenharmony_ci   }
1176bf215546Sopenharmony_ci   default:
1177bf215546Sopenharmony_ci      assert(0);
1178bf215546Sopenharmony_ci      break;
1179bf215546Sopenharmony_ci   }
1180bf215546Sopenharmony_ci   return result;
1181bf215546Sopenharmony_ci}
1182bf215546Sopenharmony_ci
1183bf215546Sopenharmony_ci
1184bf215546Sopenharmony_cistatic void
1185bf215546Sopenharmony_civisit_alu(struct lp_build_nir_context *bld_base,
1186bf215546Sopenharmony_ci          const nir_alu_instr *instr)
1187bf215546Sopenharmony_ci{
1188bf215546Sopenharmony_ci   struct gallivm_state *gallivm = bld_base->base.gallivm;
1189bf215546Sopenharmony_ci   LLVMValueRef src[NIR_MAX_VEC_COMPONENTS];
1190bf215546Sopenharmony_ci   unsigned src_bit_size[NIR_MAX_VEC_COMPONENTS];
1191bf215546Sopenharmony_ci   const unsigned num_components = nir_dest_num_components(instr->dest.dest);
1192bf215546Sopenharmony_ci   unsigned src_components;
1193bf215546Sopenharmony_ci
1194bf215546Sopenharmony_ci   switch (instr->op) {
1195bf215546Sopenharmony_ci   case nir_op_vec2:
1196bf215546Sopenharmony_ci   case nir_op_vec3:
1197bf215546Sopenharmony_ci   case nir_op_vec4:
1198bf215546Sopenharmony_ci   case nir_op_vec8:
1199bf215546Sopenharmony_ci   case nir_op_vec16:
1200bf215546Sopenharmony_ci      src_components = 1;
1201bf215546Sopenharmony_ci      break;
1202bf215546Sopenharmony_ci   case nir_op_pack_half_2x16:
1203bf215546Sopenharmony_ci      src_components = 2;
1204bf215546Sopenharmony_ci      break;
1205bf215546Sopenharmony_ci   case nir_op_unpack_half_2x16:
1206bf215546Sopenharmony_ci      src_components = 1;
1207bf215546Sopenharmony_ci      break;
1208bf215546Sopenharmony_ci   case nir_op_cube_face_coord_amd:
1209bf215546Sopenharmony_ci   case nir_op_cube_face_index_amd:
1210bf215546Sopenharmony_ci      src_components = 3;
1211bf215546Sopenharmony_ci      break;
1212bf215546Sopenharmony_ci   case nir_op_fsum2:
1213bf215546Sopenharmony_ci   case nir_op_fsum3:
1214bf215546Sopenharmony_ci   case nir_op_fsum4:
1215bf215546Sopenharmony_ci      src_components = nir_op_infos[instr->op].input_sizes[0];
1216bf215546Sopenharmony_ci      break;
1217bf215546Sopenharmony_ci   default:
1218bf215546Sopenharmony_ci      src_components = num_components;
1219bf215546Sopenharmony_ci      break;
1220bf215546Sopenharmony_ci   }
1221bf215546Sopenharmony_ci
1222bf215546Sopenharmony_ci   for (unsigned i = 0; i < nir_op_infos[instr->op].num_inputs; i++) {
1223bf215546Sopenharmony_ci      src[i] = get_alu_src(bld_base, instr->src[i], src_components);
1224bf215546Sopenharmony_ci      src_bit_size[i] = nir_src_bit_size(instr->src[i].src);
1225bf215546Sopenharmony_ci   }
1226bf215546Sopenharmony_ci
1227bf215546Sopenharmony_ci   if (instr->op == nir_op_mov &&
1228bf215546Sopenharmony_ci       is_aos(bld_base) &&
1229bf215546Sopenharmony_ci       !instr->dest.dest.is_ssa) {
1230bf215546Sopenharmony_ci      for (unsigned i = 0; i < 4; i++) {
1231bf215546Sopenharmony_ci         if (instr->dest.write_mask & (1 << i)) {
1232bf215546Sopenharmony_ci            assign_reg(bld_base, &instr->dest.dest.reg, (1 << i), src);
1233bf215546Sopenharmony_ci         }
1234bf215546Sopenharmony_ci      }
1235bf215546Sopenharmony_ci      return;
1236bf215546Sopenharmony_ci   }
1237bf215546Sopenharmony_ci
1238bf215546Sopenharmony_ci   LLVMValueRef result[NIR_MAX_VEC_COMPONENTS];
1239bf215546Sopenharmony_ci   if (instr->op == nir_op_vec4 ||
1240bf215546Sopenharmony_ci       instr->op == nir_op_vec3 ||
1241bf215546Sopenharmony_ci       instr->op == nir_op_vec2 ||
1242bf215546Sopenharmony_ci       instr->op == nir_op_vec8 ||
1243bf215546Sopenharmony_ci       instr->op == nir_op_vec16) {
1244bf215546Sopenharmony_ci      for (unsigned i = 0; i < nir_op_infos[instr->op].num_inputs; i++) {
1245bf215546Sopenharmony_ci         result[i] = cast_type(bld_base, src[i],
1246bf215546Sopenharmony_ci                               nir_op_infos[instr->op].input_types[i],
1247bf215546Sopenharmony_ci                               src_bit_size[i]);
1248bf215546Sopenharmony_ci      }
1249bf215546Sopenharmony_ci   } else if (instr->op == nir_op_fsum4 ||
1250bf215546Sopenharmony_ci              instr->op == nir_op_fsum3 ||
1251bf215546Sopenharmony_ci              instr->op == nir_op_fsum2) {
1252bf215546Sopenharmony_ci      for (unsigned c = 0; c < nir_op_infos[instr->op].input_sizes[0]; c++) {
1253bf215546Sopenharmony_ci         LLVMValueRef temp_chan = LLVMBuildExtractValue(gallivm->builder,
1254bf215546Sopenharmony_ci                                                          src[0], c, "");
1255bf215546Sopenharmony_ci         temp_chan = cast_type(bld_base, temp_chan,
1256bf215546Sopenharmony_ci                               nir_op_infos[instr->op].input_types[0],
1257bf215546Sopenharmony_ci                               src_bit_size[0]);
1258bf215546Sopenharmony_ci         result[0] = (c == 0) ? temp_chan
1259bf215546Sopenharmony_ci            : lp_build_add(get_flt_bld(bld_base, src_bit_size[0]),
1260bf215546Sopenharmony_ci                           result[0], temp_chan);
1261bf215546Sopenharmony_ci      }
1262bf215546Sopenharmony_ci   } else if (is_aos(bld_base)) {
1263bf215546Sopenharmony_ci      if (instr->op == nir_op_fmul) {
1264bf215546Sopenharmony_ci         if (LLVMIsConstant(src[0])) {
1265bf215546Sopenharmony_ci            src[0] = lp_nir_aos_conv_const(gallivm, src[0], 1);
1266bf215546Sopenharmony_ci         }
1267bf215546Sopenharmony_ci         if (LLVMIsConstant(src[1])) {
1268bf215546Sopenharmony_ci            src[1] = lp_nir_aos_conv_const(gallivm, src[1], 1);
1269bf215546Sopenharmony_ci         }
1270bf215546Sopenharmony_ci      }
1271bf215546Sopenharmony_ci      result[0] = do_alu_action(bld_base, instr, src_bit_size, src);
1272bf215546Sopenharmony_ci   } else {
1273bf215546Sopenharmony_ci      /* Loop for R,G,B,A channels */
1274bf215546Sopenharmony_ci      for (unsigned c = 0; c < num_components; c++) {
1275bf215546Sopenharmony_ci         LLVMValueRef src_chan[NIR_MAX_VEC_COMPONENTS];
1276bf215546Sopenharmony_ci
1277bf215546Sopenharmony_ci         /* Loop over instruction operands */
1278bf215546Sopenharmony_ci         for (unsigned i = 0; i < nir_op_infos[instr->op].num_inputs; i++) {
1279bf215546Sopenharmony_ci            if (num_components > 1) {
1280bf215546Sopenharmony_ci               src_chan[i] = LLVMBuildExtractValue(gallivm->builder,
1281bf215546Sopenharmony_ci                                                     src[i], c, "");
1282bf215546Sopenharmony_ci            } else {
1283bf215546Sopenharmony_ci               src_chan[i] = src[i];
1284bf215546Sopenharmony_ci            }
1285bf215546Sopenharmony_ci            src_chan[i] = cast_type(bld_base, src_chan[i],
1286bf215546Sopenharmony_ci                                    nir_op_infos[instr->op].input_types[i],
1287bf215546Sopenharmony_ci                                    src_bit_size[i]);
1288bf215546Sopenharmony_ci         }
1289bf215546Sopenharmony_ci         result[c] = do_alu_action(bld_base, instr, src_bit_size, src_chan);
1290bf215546Sopenharmony_ci         result[c] = cast_type(bld_base, result[c],
1291bf215546Sopenharmony_ci                               nir_op_infos[instr->op].output_type,
1292bf215546Sopenharmony_ci                               nir_dest_bit_size(instr->dest.dest));
1293bf215546Sopenharmony_ci      }
1294bf215546Sopenharmony_ci   }
1295bf215546Sopenharmony_ci   assign_alu_dest(bld_base, &instr->dest, result);
1296bf215546Sopenharmony_ci}
1297bf215546Sopenharmony_ci
1298bf215546Sopenharmony_ci
1299bf215546Sopenharmony_cistatic void
1300bf215546Sopenharmony_civisit_load_const(struct lp_build_nir_context *bld_base,
1301bf215546Sopenharmony_ci                 const nir_load_const_instr *instr)
1302bf215546Sopenharmony_ci{
1303bf215546Sopenharmony_ci   LLVMValueRef result[NIR_MAX_VEC_COMPONENTS];
1304bf215546Sopenharmony_ci   bld_base->load_const(bld_base, instr, result);
1305bf215546Sopenharmony_ci   assign_ssa_dest(bld_base, &instr->def, result);
1306bf215546Sopenharmony_ci}
1307bf215546Sopenharmony_ci
1308bf215546Sopenharmony_ci
1309bf215546Sopenharmony_cistatic void
1310bf215546Sopenharmony_ciget_deref_offset(struct lp_build_nir_context *bld_base, nir_deref_instr *instr,
1311bf215546Sopenharmony_ci                 bool vs_in, unsigned *vertex_index_out,
1312bf215546Sopenharmony_ci                 LLVMValueRef *vertex_index_ref,
1313bf215546Sopenharmony_ci                 unsigned *const_out, LLVMValueRef *indir_out)
1314bf215546Sopenharmony_ci{
1315bf215546Sopenharmony_ci   LLVMBuilderRef builder = bld_base->base.gallivm->builder;
1316bf215546Sopenharmony_ci   nir_variable *var = nir_deref_instr_get_variable(instr);
1317bf215546Sopenharmony_ci   nir_deref_path path;
1318bf215546Sopenharmony_ci   unsigned idx_lvl = 1;
1319bf215546Sopenharmony_ci
1320bf215546Sopenharmony_ci   nir_deref_path_init(&path, instr, NULL);
1321bf215546Sopenharmony_ci
1322bf215546Sopenharmony_ci   if (vertex_index_out != NULL || vertex_index_ref != NULL) {
1323bf215546Sopenharmony_ci      if (vertex_index_ref) {
1324bf215546Sopenharmony_ci         *vertex_index_ref = get_src(bld_base, path.path[idx_lvl]->arr.index);
1325bf215546Sopenharmony_ci         if (vertex_index_out)
1326bf215546Sopenharmony_ci            *vertex_index_out = 0;
1327bf215546Sopenharmony_ci      } else {
1328bf215546Sopenharmony_ci         *vertex_index_out = nir_src_as_uint(path.path[idx_lvl]->arr.index);
1329bf215546Sopenharmony_ci      }
1330bf215546Sopenharmony_ci      ++idx_lvl;
1331bf215546Sopenharmony_ci   }
1332bf215546Sopenharmony_ci
1333bf215546Sopenharmony_ci   uint32_t const_offset = 0;
1334bf215546Sopenharmony_ci   LLVMValueRef offset = NULL;
1335bf215546Sopenharmony_ci
1336bf215546Sopenharmony_ci   if (var->data.compact && nir_src_is_const(instr->arr.index)) {
1337bf215546Sopenharmony_ci      assert(instr->deref_type == nir_deref_type_array);
1338bf215546Sopenharmony_ci      const_offset = nir_src_as_uint(instr->arr.index);
1339bf215546Sopenharmony_ci      goto out;
1340bf215546Sopenharmony_ci   }
1341bf215546Sopenharmony_ci
1342bf215546Sopenharmony_ci   for (; path.path[idx_lvl]; ++idx_lvl) {
1343bf215546Sopenharmony_ci      const struct glsl_type *parent_type = path.path[idx_lvl - 1]->type;
1344bf215546Sopenharmony_ci      if (path.path[idx_lvl]->deref_type == nir_deref_type_struct) {
1345bf215546Sopenharmony_ci         unsigned index = path.path[idx_lvl]->strct.index;
1346bf215546Sopenharmony_ci
1347bf215546Sopenharmony_ci         for (unsigned i = 0; i < index; i++) {
1348bf215546Sopenharmony_ci            const struct glsl_type *ft = glsl_get_struct_field(parent_type, i);
1349bf215546Sopenharmony_ci            const_offset += glsl_count_attribute_slots(ft, vs_in);
1350bf215546Sopenharmony_ci         }
1351bf215546Sopenharmony_ci      } else if (path.path[idx_lvl]->deref_type == nir_deref_type_array) {
1352bf215546Sopenharmony_ci         unsigned size = glsl_count_attribute_slots(path.path[idx_lvl]->type, vs_in);
1353bf215546Sopenharmony_ci         if (nir_src_is_const(path.path[idx_lvl]->arr.index)) {
1354bf215546Sopenharmony_ci           const_offset += nir_src_comp_as_int(path.path[idx_lvl]->arr.index, 0) * size;
1355bf215546Sopenharmony_ci         } else {
1356bf215546Sopenharmony_ci           LLVMValueRef idx_src = get_src(bld_base, path.path[idx_lvl]->arr.index);
1357bf215546Sopenharmony_ci           idx_src = cast_type(bld_base, idx_src, nir_type_uint, 32);
1358bf215546Sopenharmony_ci           LLVMValueRef array_off = lp_build_mul(&bld_base->uint_bld, lp_build_const_int_vec(bld_base->base.gallivm, bld_base->base.type, size),
1359bf215546Sopenharmony_ci                                               idx_src);
1360bf215546Sopenharmony_ci           if (offset)
1361bf215546Sopenharmony_ci             offset = lp_build_add(&bld_base->uint_bld, offset, array_off);
1362bf215546Sopenharmony_ci           else
1363bf215546Sopenharmony_ci             offset = array_off;
1364bf215546Sopenharmony_ci         }
1365bf215546Sopenharmony_ci      } else
1366bf215546Sopenharmony_ci         unreachable("Uhandled deref type in get_deref_instr_offset");
1367bf215546Sopenharmony_ci   }
1368bf215546Sopenharmony_ci
1369bf215546Sopenharmony_ciout:
1370bf215546Sopenharmony_ci   nir_deref_path_finish(&path);
1371bf215546Sopenharmony_ci
1372bf215546Sopenharmony_ci   if (const_offset && offset)
1373bf215546Sopenharmony_ci      offset = LLVMBuildAdd(builder, offset,
1374bf215546Sopenharmony_ci                            lp_build_const_int_vec(bld_base->base.gallivm, bld_base->uint_bld.type, const_offset),
1375bf215546Sopenharmony_ci                            "");
1376bf215546Sopenharmony_ci   *const_out = const_offset;
1377bf215546Sopenharmony_ci   *indir_out = offset;
1378bf215546Sopenharmony_ci}
1379bf215546Sopenharmony_ci
1380bf215546Sopenharmony_ci
1381bf215546Sopenharmony_cistatic void
1382bf215546Sopenharmony_civisit_load_input(struct lp_build_nir_context *bld_base,
1383bf215546Sopenharmony_ci                 nir_intrinsic_instr *instr,
1384bf215546Sopenharmony_ci                 LLVMValueRef result[NIR_MAX_VEC_COMPONENTS])
1385bf215546Sopenharmony_ci{
1386bf215546Sopenharmony_ci   nir_variable var = {0};
1387bf215546Sopenharmony_ci   var.data.location = nir_intrinsic_io_semantics(instr).location;
1388bf215546Sopenharmony_ci   var.data.driver_location = nir_intrinsic_base(instr);
1389bf215546Sopenharmony_ci   var.data.location_frac = nir_intrinsic_component(instr);
1390bf215546Sopenharmony_ci
1391bf215546Sopenharmony_ci   unsigned nc = nir_dest_num_components(instr->dest);
1392bf215546Sopenharmony_ci   unsigned bit_size = nir_dest_bit_size(instr->dest);
1393bf215546Sopenharmony_ci
1394bf215546Sopenharmony_ci   nir_src offset = *nir_get_io_offset_src(instr);
1395bf215546Sopenharmony_ci   bool indirect = !nir_src_is_const(offset);
1396bf215546Sopenharmony_ci   if (!indirect)
1397bf215546Sopenharmony_ci      assert(nir_src_as_uint(offset) == 0);
1398bf215546Sopenharmony_ci   LLVMValueRef indir_index = indirect ? get_src(bld_base, offset) : NULL;
1399bf215546Sopenharmony_ci
1400bf215546Sopenharmony_ci   bld_base->load_var(bld_base, nir_var_shader_in, nc, bit_size, &var, 0, NULL, 0, indir_index, result);
1401bf215546Sopenharmony_ci}
1402bf215546Sopenharmony_ci
1403bf215546Sopenharmony_ci
1404bf215546Sopenharmony_cistatic void
1405bf215546Sopenharmony_civisit_store_output(struct lp_build_nir_context *bld_base,
1406bf215546Sopenharmony_ci                   nir_intrinsic_instr *instr)
1407bf215546Sopenharmony_ci{
1408bf215546Sopenharmony_ci   nir_variable var = {0};
1409bf215546Sopenharmony_ci   var.data.location = nir_intrinsic_io_semantics(instr).location;
1410bf215546Sopenharmony_ci   var.data.driver_location = nir_intrinsic_base(instr);
1411bf215546Sopenharmony_ci   var.data.location_frac = nir_intrinsic_component(instr);
1412bf215546Sopenharmony_ci
1413bf215546Sopenharmony_ci   unsigned mask = nir_intrinsic_write_mask(instr);
1414bf215546Sopenharmony_ci
1415bf215546Sopenharmony_ci   unsigned bit_size = nir_src_bit_size(instr->src[0]);
1416bf215546Sopenharmony_ci   LLVMValueRef src = get_src(bld_base, instr->src[0]);
1417bf215546Sopenharmony_ci
1418bf215546Sopenharmony_ci   nir_src offset = *nir_get_io_offset_src(instr);
1419bf215546Sopenharmony_ci   bool indirect = !nir_src_is_const(offset);
1420bf215546Sopenharmony_ci   if (!indirect)
1421bf215546Sopenharmony_ci      assert(nir_src_as_uint(offset) == 0);
1422bf215546Sopenharmony_ci   LLVMValueRef indir_index = indirect ? get_src(bld_base, offset) : NULL;
1423bf215546Sopenharmony_ci
1424bf215546Sopenharmony_ci   if (mask == 0x1 && LLVMGetTypeKind(LLVMTypeOf(src)) == LLVMArrayTypeKind) {
1425bf215546Sopenharmony_ci      src = LLVMBuildExtractValue(bld_base->base.gallivm->builder,
1426bf215546Sopenharmony_ci                                  src, 0, "");
1427bf215546Sopenharmony_ci   }
1428bf215546Sopenharmony_ci
1429bf215546Sopenharmony_ci   bld_base->store_var(bld_base, nir_var_shader_out, util_last_bit(mask),
1430bf215546Sopenharmony_ci                       bit_size, &var, mask, NULL, 0, indir_index, src);
1431bf215546Sopenharmony_ci}
1432bf215546Sopenharmony_ci
1433bf215546Sopenharmony_ci
1434bf215546Sopenharmony_cistatic void
1435bf215546Sopenharmony_civisit_load_var(struct lp_build_nir_context *bld_base,
1436bf215546Sopenharmony_ci               nir_intrinsic_instr *instr,
1437bf215546Sopenharmony_ci               LLVMValueRef result[NIR_MAX_VEC_COMPONENTS])
1438bf215546Sopenharmony_ci{
1439bf215546Sopenharmony_ci   nir_deref_instr *deref = nir_instr_as_deref(instr->src[0].ssa->parent_instr);
1440bf215546Sopenharmony_ci   nir_variable *var = nir_deref_instr_get_variable(deref);
1441bf215546Sopenharmony_ci   assert(util_bitcount(deref->modes) == 1);
1442bf215546Sopenharmony_ci   nir_variable_mode mode = deref->modes;
1443bf215546Sopenharmony_ci   unsigned const_index;
1444bf215546Sopenharmony_ci   LLVMValueRef indir_index;
1445bf215546Sopenharmony_ci   LLVMValueRef indir_vertex_index = NULL;
1446bf215546Sopenharmony_ci   unsigned vertex_index = 0;
1447bf215546Sopenharmony_ci   unsigned nc = nir_dest_num_components(instr->dest);
1448bf215546Sopenharmony_ci   unsigned bit_size = nir_dest_bit_size(instr->dest);
1449bf215546Sopenharmony_ci   if (var) {
1450bf215546Sopenharmony_ci      bool vs_in = bld_base->shader->info.stage == MESA_SHADER_VERTEX &&
1451bf215546Sopenharmony_ci         var->data.mode == nir_var_shader_in;
1452bf215546Sopenharmony_ci      bool gs_in = bld_base->shader->info.stage == MESA_SHADER_GEOMETRY &&
1453bf215546Sopenharmony_ci         var->data.mode == nir_var_shader_in;
1454bf215546Sopenharmony_ci      bool tcs_in = bld_base->shader->info.stage == MESA_SHADER_TESS_CTRL &&
1455bf215546Sopenharmony_ci         var->data.mode == nir_var_shader_in;
1456bf215546Sopenharmony_ci      bool tcs_out = bld_base->shader->info.stage == MESA_SHADER_TESS_CTRL &&
1457bf215546Sopenharmony_ci         var->data.mode == nir_var_shader_out && !var->data.patch;
1458bf215546Sopenharmony_ci      bool tes_in = bld_base->shader->info.stage == MESA_SHADER_TESS_EVAL &&
1459bf215546Sopenharmony_ci         var->data.mode == nir_var_shader_in && !var->data.patch;
1460bf215546Sopenharmony_ci
1461bf215546Sopenharmony_ci      mode = var->data.mode;
1462bf215546Sopenharmony_ci
1463bf215546Sopenharmony_ci      get_deref_offset(bld_base, deref, vs_in,
1464bf215546Sopenharmony_ci                   gs_in ? &vertex_index : NULL,
1465bf215546Sopenharmony_ci                   (tcs_in || tcs_out || tes_in) ? &indir_vertex_index : NULL,
1466bf215546Sopenharmony_ci                   &const_index, &indir_index);
1467bf215546Sopenharmony_ci   }
1468bf215546Sopenharmony_ci   bld_base->load_var(bld_base, mode, nc, bit_size, var, vertex_index,
1469bf215546Sopenharmony_ci                      indir_vertex_index, const_index, indir_index, result);
1470bf215546Sopenharmony_ci}
1471bf215546Sopenharmony_ci
1472bf215546Sopenharmony_ci
1473bf215546Sopenharmony_cistatic void
1474bf215546Sopenharmony_civisit_store_var(struct lp_build_nir_context *bld_base,
1475bf215546Sopenharmony_ci                nir_intrinsic_instr *instr)
1476bf215546Sopenharmony_ci{
1477bf215546Sopenharmony_ci   nir_deref_instr *deref = nir_instr_as_deref(instr->src[0].ssa->parent_instr);
1478bf215546Sopenharmony_ci   nir_variable *var = nir_deref_instr_get_variable(deref);
1479bf215546Sopenharmony_ci   assert(util_bitcount(deref->modes) == 1);
1480bf215546Sopenharmony_ci   nir_variable_mode mode = deref->modes;
1481bf215546Sopenharmony_ci   int writemask = instr->const_index[0];
1482bf215546Sopenharmony_ci   unsigned bit_size = nir_src_bit_size(instr->src[1]);
1483bf215546Sopenharmony_ci   LLVMValueRef src = get_src(bld_base, instr->src[1]);
1484bf215546Sopenharmony_ci   unsigned const_index = 0;
1485bf215546Sopenharmony_ci   LLVMValueRef indir_index, indir_vertex_index = NULL;
1486bf215546Sopenharmony_ci   if (var) {
1487bf215546Sopenharmony_ci      bool tcs_out = bld_base->shader->info.stage == MESA_SHADER_TESS_CTRL &&
1488bf215546Sopenharmony_ci         var->data.mode == nir_var_shader_out && !var->data.patch;
1489bf215546Sopenharmony_ci      get_deref_offset(bld_base, deref, false, NULL,
1490bf215546Sopenharmony_ci                       tcs_out ? &indir_vertex_index : NULL,
1491bf215546Sopenharmony_ci                       &const_index, &indir_index);
1492bf215546Sopenharmony_ci   }
1493bf215546Sopenharmony_ci   bld_base->store_var(bld_base, mode, instr->num_components, bit_size,
1494bf215546Sopenharmony_ci                       var, writemask, indir_vertex_index, const_index,
1495bf215546Sopenharmony_ci                       indir_index, src);
1496bf215546Sopenharmony_ci}
1497bf215546Sopenharmony_ci
1498bf215546Sopenharmony_ci
1499bf215546Sopenharmony_cistatic void
1500bf215546Sopenharmony_civisit_load_ubo(struct lp_build_nir_context *bld_base,
1501bf215546Sopenharmony_ci               nir_intrinsic_instr *instr,
1502bf215546Sopenharmony_ci               LLVMValueRef result[NIR_MAX_VEC_COMPONENTS])
1503bf215546Sopenharmony_ci{
1504bf215546Sopenharmony_ci   struct gallivm_state *gallivm = bld_base->base.gallivm;
1505bf215546Sopenharmony_ci   LLVMBuilderRef builder = gallivm->builder;
1506bf215546Sopenharmony_ci   LLVMValueRef idx = get_src(bld_base, instr->src[0]);
1507bf215546Sopenharmony_ci   LLVMValueRef offset = get_src(bld_base, instr->src[1]);
1508bf215546Sopenharmony_ci
1509bf215546Sopenharmony_ci   bool offset_is_uniform = nir_src_is_always_uniform(instr->src[1]);
1510bf215546Sopenharmony_ci   idx = LLVMBuildExtractElement(builder, idx, lp_build_const_int32(gallivm, 0), "");
1511bf215546Sopenharmony_ci   bld_base->load_ubo(bld_base, nir_dest_num_components(instr->dest),
1512bf215546Sopenharmony_ci                      nir_dest_bit_size(instr->dest),
1513bf215546Sopenharmony_ci                      offset_is_uniform, idx, offset, result);
1514bf215546Sopenharmony_ci}
1515bf215546Sopenharmony_ci
1516bf215546Sopenharmony_ci
1517bf215546Sopenharmony_cistatic void
1518bf215546Sopenharmony_civisit_load_push_constant(struct lp_build_nir_context *bld_base,
1519bf215546Sopenharmony_ci                         nir_intrinsic_instr *instr,
1520bf215546Sopenharmony_ci                         LLVMValueRef result[4])
1521bf215546Sopenharmony_ci{
1522bf215546Sopenharmony_ci   struct gallivm_state *gallivm = bld_base->base.gallivm;
1523bf215546Sopenharmony_ci   LLVMValueRef offset = get_src(bld_base, instr->src[0]);
1524bf215546Sopenharmony_ci   LLVMValueRef idx = lp_build_const_int32(gallivm, 0);
1525bf215546Sopenharmony_ci   bool offset_is_uniform = nir_src_is_always_uniform(instr->src[0]);
1526bf215546Sopenharmony_ci
1527bf215546Sopenharmony_ci   bld_base->load_ubo(bld_base, nir_dest_num_components(instr->dest),
1528bf215546Sopenharmony_ci                      nir_dest_bit_size(instr->dest),
1529bf215546Sopenharmony_ci                      offset_is_uniform, idx, offset, result);
1530bf215546Sopenharmony_ci}
1531bf215546Sopenharmony_ci
1532bf215546Sopenharmony_ci
1533bf215546Sopenharmony_cistatic void
1534bf215546Sopenharmony_civisit_load_ssbo(struct lp_build_nir_context *bld_base,
1535bf215546Sopenharmony_ci                nir_intrinsic_instr *instr,
1536bf215546Sopenharmony_ci                LLVMValueRef result[NIR_MAX_VEC_COMPONENTS])
1537bf215546Sopenharmony_ci{
1538bf215546Sopenharmony_ci   LLVMValueRef idx = cast_type(bld_base, get_src(bld_base, instr->src[0]), nir_type_uint, 32);
1539bf215546Sopenharmony_ci   LLVMValueRef offset = get_src(bld_base, instr->src[1]);
1540bf215546Sopenharmony_ci   bool index_and_offset_are_uniform = nir_src_is_always_uniform(instr->src[0]) && nir_src_is_always_uniform(instr->src[1]);
1541bf215546Sopenharmony_ci   bld_base->load_mem(bld_base, nir_dest_num_components(instr->dest), nir_dest_bit_size(instr->dest),
1542bf215546Sopenharmony_ci                      index_and_offset_are_uniform, idx, offset, result);
1543bf215546Sopenharmony_ci}
1544bf215546Sopenharmony_ci
1545bf215546Sopenharmony_ci
1546bf215546Sopenharmony_cistatic void
1547bf215546Sopenharmony_civisit_store_ssbo(struct lp_build_nir_context *bld_base,
1548bf215546Sopenharmony_ci                 nir_intrinsic_instr *instr)
1549bf215546Sopenharmony_ci{
1550bf215546Sopenharmony_ci   LLVMValueRef val = get_src(bld_base, instr->src[0]);
1551bf215546Sopenharmony_ci   LLVMValueRef idx = cast_type(bld_base, get_src(bld_base, instr->src[1]), nir_type_uint, 32);
1552bf215546Sopenharmony_ci   LLVMValueRef offset = get_src(bld_base, instr->src[2]);
1553bf215546Sopenharmony_ci   bool index_and_offset_are_uniform = nir_src_is_always_uniform(instr->src[1]) && nir_src_is_always_uniform(instr->src[2]);
1554bf215546Sopenharmony_ci   int writemask = instr->const_index[0];
1555bf215546Sopenharmony_ci   int nc = nir_src_num_components(instr->src[0]);
1556bf215546Sopenharmony_ci   int bitsize = nir_src_bit_size(instr->src[0]);
1557bf215546Sopenharmony_ci   bld_base->store_mem(bld_base, writemask, nc, bitsize, index_and_offset_are_uniform, idx, offset, val);
1558bf215546Sopenharmony_ci}
1559bf215546Sopenharmony_ci
1560bf215546Sopenharmony_ci
1561bf215546Sopenharmony_cistatic void
1562bf215546Sopenharmony_civisit_get_ssbo_size(struct lp_build_nir_context *bld_base,
1563bf215546Sopenharmony_ci                    nir_intrinsic_instr *instr,
1564bf215546Sopenharmony_ci                    LLVMValueRef result[NIR_MAX_VEC_COMPONENTS])
1565bf215546Sopenharmony_ci{
1566bf215546Sopenharmony_ci   LLVMValueRef idx = cast_type(bld_base,
1567bf215546Sopenharmony_ci                                get_src(bld_base, instr->src[0]),
1568bf215546Sopenharmony_ci                                nir_type_uint, 32);
1569bf215546Sopenharmony_ci   result[0] = bld_base->get_ssbo_size(bld_base, idx);
1570bf215546Sopenharmony_ci}
1571bf215546Sopenharmony_ci
1572bf215546Sopenharmony_ci
1573bf215546Sopenharmony_cistatic void
1574bf215546Sopenharmony_civisit_ssbo_atomic(struct lp_build_nir_context *bld_base,
1575bf215546Sopenharmony_ci                  nir_intrinsic_instr *instr,
1576bf215546Sopenharmony_ci                  LLVMValueRef result[NIR_MAX_VEC_COMPONENTS])
1577bf215546Sopenharmony_ci{
1578bf215546Sopenharmony_ci   LLVMValueRef idx = cast_type(bld_base, get_src(bld_base, instr->src[0]),
1579bf215546Sopenharmony_ci                                nir_type_uint, 32);
1580bf215546Sopenharmony_ci   LLVMValueRef offset = get_src(bld_base, instr->src[1]);
1581bf215546Sopenharmony_ci   LLVMValueRef val = get_src(bld_base, instr->src[2]);
1582bf215546Sopenharmony_ci   LLVMValueRef val2 = NULL;
1583bf215546Sopenharmony_ci   int bitsize = nir_src_bit_size(instr->src[2]);
1584bf215546Sopenharmony_ci   if (instr->intrinsic == nir_intrinsic_ssbo_atomic_comp_swap)
1585bf215546Sopenharmony_ci      val2 = get_src(bld_base, instr->src[3]);
1586bf215546Sopenharmony_ci
1587bf215546Sopenharmony_ci   bld_base->atomic_mem(bld_base, instr->intrinsic, bitsize, idx,
1588bf215546Sopenharmony_ci                        offset, val, val2, &result[0]);
1589bf215546Sopenharmony_ci}
1590bf215546Sopenharmony_ci
1591bf215546Sopenharmony_ci
1592bf215546Sopenharmony_cistatic void
1593bf215546Sopenharmony_civisit_load_image(struct lp_build_nir_context *bld_base,
1594bf215546Sopenharmony_ci                 nir_intrinsic_instr *instr,
1595bf215546Sopenharmony_ci                 LLVMValueRef result[NIR_MAX_VEC_COMPONENTS])
1596bf215546Sopenharmony_ci{
1597bf215546Sopenharmony_ci   struct gallivm_state *gallivm = bld_base->base.gallivm;
1598bf215546Sopenharmony_ci   LLVMBuilderRef builder = gallivm->builder;
1599bf215546Sopenharmony_ci   nir_deref_instr *deref = nir_instr_as_deref(instr->src[0].ssa->parent_instr);
1600bf215546Sopenharmony_ci   nir_variable *var = nir_deref_instr_get_variable(deref);
1601bf215546Sopenharmony_ci   LLVMValueRef coord_val = get_src(bld_base, instr->src[1]);
1602bf215546Sopenharmony_ci   LLVMValueRef coords[5];
1603bf215546Sopenharmony_ci   struct lp_img_params params;
1604bf215546Sopenharmony_ci   const struct glsl_type *type = glsl_without_array(var->type);
1605bf215546Sopenharmony_ci   unsigned const_index;
1606bf215546Sopenharmony_ci   LLVMValueRef indir_index;
1607bf215546Sopenharmony_ci   get_deref_offset(bld_base, deref, false, NULL, NULL,
1608bf215546Sopenharmony_ci                    &const_index, &indir_index);
1609bf215546Sopenharmony_ci
1610bf215546Sopenharmony_ci   memset(&params, 0, sizeof(params));
1611bf215546Sopenharmony_ci   params.target = glsl_sampler_to_pipe(glsl_get_sampler_dim(type),
1612bf215546Sopenharmony_ci                                        glsl_sampler_type_is_array(type));
1613bf215546Sopenharmony_ci   for (unsigned i = 0; i < 4; i++)
1614bf215546Sopenharmony_ci      coords[i] = LLVMBuildExtractValue(builder, coord_val, i, "");
1615bf215546Sopenharmony_ci   if (params.target == PIPE_TEXTURE_1D_ARRAY)
1616bf215546Sopenharmony_ci      coords[2] = coords[1];
1617bf215546Sopenharmony_ci
1618bf215546Sopenharmony_ci   params.coords = coords;
1619bf215546Sopenharmony_ci   params.outdata = result;
1620bf215546Sopenharmony_ci   params.img_op = LP_IMG_LOAD;
1621bf215546Sopenharmony_ci   if (glsl_get_sampler_dim(type) == GLSL_SAMPLER_DIM_MS ||
1622bf215546Sopenharmony_ci       glsl_get_sampler_dim(type) == GLSL_SAMPLER_DIM_SUBPASS_MS) {
1623bf215546Sopenharmony_ci      params.ms_index = cast_type(bld_base, get_src(bld_base, instr->src[2]),
1624bf215546Sopenharmony_ci                                  nir_type_uint, 32);
1625bf215546Sopenharmony_ci   }
1626bf215546Sopenharmony_ci   params.image_index = var->data.binding + (indir_index ? 0 : const_index);
1627bf215546Sopenharmony_ci   params.image_index_offset = indir_index;
1628bf215546Sopenharmony_ci   bld_base->image_op(bld_base, &params);
1629bf215546Sopenharmony_ci}
1630bf215546Sopenharmony_ci
1631bf215546Sopenharmony_ci
1632bf215546Sopenharmony_cistatic void
1633bf215546Sopenharmony_civisit_store_image(struct lp_build_nir_context *bld_base,
1634bf215546Sopenharmony_ci                  nir_intrinsic_instr *instr)
1635bf215546Sopenharmony_ci{
1636bf215546Sopenharmony_ci   struct gallivm_state *gallivm = bld_base->base.gallivm;
1637bf215546Sopenharmony_ci   LLVMBuilderRef builder = gallivm->builder;
1638bf215546Sopenharmony_ci   nir_deref_instr *deref = nir_instr_as_deref(instr->src[0].ssa->parent_instr);
1639bf215546Sopenharmony_ci   nir_variable *var = nir_deref_instr_get_variable(deref);
1640bf215546Sopenharmony_ci   LLVMValueRef coord_val = get_src(bld_base, instr->src[1]);
1641bf215546Sopenharmony_ci   LLVMValueRef in_val = get_src(bld_base, instr->src[3]);
1642bf215546Sopenharmony_ci   LLVMValueRef coords[5];
1643bf215546Sopenharmony_ci   struct lp_img_params params;
1644bf215546Sopenharmony_ci   const struct glsl_type *type = glsl_without_array(var->type);
1645bf215546Sopenharmony_ci   unsigned const_index;
1646bf215546Sopenharmony_ci   LLVMValueRef indir_index;
1647bf215546Sopenharmony_ci   get_deref_offset(bld_base, deref, false, NULL, NULL,
1648bf215546Sopenharmony_ci                    &const_index, &indir_index);
1649bf215546Sopenharmony_ci
1650bf215546Sopenharmony_ci   memset(&params, 0, sizeof(params));
1651bf215546Sopenharmony_ci   params.target = glsl_sampler_to_pipe(glsl_get_sampler_dim(type), glsl_sampler_type_is_array(type));
1652bf215546Sopenharmony_ci   for (unsigned i = 0; i < 4; i++)
1653bf215546Sopenharmony_ci      coords[i] = LLVMBuildExtractValue(builder, coord_val, i, "");
1654bf215546Sopenharmony_ci   if (params.target == PIPE_TEXTURE_1D_ARRAY)
1655bf215546Sopenharmony_ci      coords[2] = coords[1];
1656bf215546Sopenharmony_ci   params.coords = coords;
1657bf215546Sopenharmony_ci
1658bf215546Sopenharmony_ci   for (unsigned i = 0; i < 4; i++) {
1659bf215546Sopenharmony_ci      params.indata[i] = LLVMBuildExtractValue(builder, in_val, i, "");
1660bf215546Sopenharmony_ci      params.indata[i] = LLVMBuildBitCast(builder, params.indata[i], bld_base->base.vec_type, "");
1661bf215546Sopenharmony_ci   }
1662bf215546Sopenharmony_ci   if (glsl_get_sampler_dim(type) == GLSL_SAMPLER_DIM_MS)
1663bf215546Sopenharmony_ci      params.ms_index = get_src(bld_base, instr->src[2]);
1664bf215546Sopenharmony_ci   params.img_op = LP_IMG_STORE;
1665bf215546Sopenharmony_ci   params.image_index = var->data.binding + (indir_index ? 0 : const_index);
1666bf215546Sopenharmony_ci   params.image_index_offset = indir_index;
1667bf215546Sopenharmony_ci
1668bf215546Sopenharmony_ci   if (params.target == PIPE_TEXTURE_1D_ARRAY)
1669bf215546Sopenharmony_ci      coords[2] = coords[1];
1670bf215546Sopenharmony_ci   bld_base->image_op(bld_base, &params);
1671bf215546Sopenharmony_ci}
1672bf215546Sopenharmony_ci
1673bf215546Sopenharmony_ci
1674bf215546Sopenharmony_cistatic void
1675bf215546Sopenharmony_civisit_atomic_image(struct lp_build_nir_context *bld_base,
1676bf215546Sopenharmony_ci                   nir_intrinsic_instr *instr,
1677bf215546Sopenharmony_ci                   LLVMValueRef result[NIR_MAX_VEC_COMPONENTS])
1678bf215546Sopenharmony_ci{
1679bf215546Sopenharmony_ci   struct gallivm_state *gallivm = bld_base->base.gallivm;
1680bf215546Sopenharmony_ci   LLVMBuilderRef builder = gallivm->builder;
1681bf215546Sopenharmony_ci   nir_deref_instr *deref = nir_instr_as_deref(instr->src[0].ssa->parent_instr);
1682bf215546Sopenharmony_ci   nir_variable *var = nir_deref_instr_get_variable(deref);
1683bf215546Sopenharmony_ci   struct lp_img_params params;
1684bf215546Sopenharmony_ci   LLVMValueRef coord_val = get_src(bld_base, instr->src[1]);
1685bf215546Sopenharmony_ci   LLVMValueRef in_val = get_src(bld_base, instr->src[3]);
1686bf215546Sopenharmony_ci   LLVMValueRef coords[5];
1687bf215546Sopenharmony_ci   const struct glsl_type *type = glsl_without_array(var->type);
1688bf215546Sopenharmony_ci   unsigned const_index;
1689bf215546Sopenharmony_ci   LLVMValueRef indir_index;
1690bf215546Sopenharmony_ci   get_deref_offset(bld_base, deref, false, NULL, NULL,
1691bf215546Sopenharmony_ci                    &const_index, &indir_index);
1692bf215546Sopenharmony_ci
1693bf215546Sopenharmony_ci   memset(&params, 0, sizeof(params));
1694bf215546Sopenharmony_ci
1695bf215546Sopenharmony_ci   switch (instr->intrinsic) {
1696bf215546Sopenharmony_ci   case nir_intrinsic_image_deref_atomic_add:
1697bf215546Sopenharmony_ci      params.op = LLVMAtomicRMWBinOpAdd;
1698bf215546Sopenharmony_ci      break;
1699bf215546Sopenharmony_ci   case nir_intrinsic_image_deref_atomic_exchange:
1700bf215546Sopenharmony_ci      params.op = LLVMAtomicRMWBinOpXchg;
1701bf215546Sopenharmony_ci      break;
1702bf215546Sopenharmony_ci   case nir_intrinsic_image_deref_atomic_and:
1703bf215546Sopenharmony_ci      params.op = LLVMAtomicRMWBinOpAnd;
1704bf215546Sopenharmony_ci      break;
1705bf215546Sopenharmony_ci   case nir_intrinsic_image_deref_atomic_or:
1706bf215546Sopenharmony_ci      params.op = LLVMAtomicRMWBinOpOr;
1707bf215546Sopenharmony_ci      break;
1708bf215546Sopenharmony_ci   case nir_intrinsic_image_deref_atomic_xor:
1709bf215546Sopenharmony_ci      params.op = LLVMAtomicRMWBinOpXor;
1710bf215546Sopenharmony_ci      break;
1711bf215546Sopenharmony_ci   case nir_intrinsic_image_deref_atomic_umin:
1712bf215546Sopenharmony_ci      params.op = LLVMAtomicRMWBinOpUMin;
1713bf215546Sopenharmony_ci      break;
1714bf215546Sopenharmony_ci   case nir_intrinsic_image_deref_atomic_umax:
1715bf215546Sopenharmony_ci      params.op = LLVMAtomicRMWBinOpUMax;
1716bf215546Sopenharmony_ci      break;
1717bf215546Sopenharmony_ci   case nir_intrinsic_image_deref_atomic_imin:
1718bf215546Sopenharmony_ci      params.op = LLVMAtomicRMWBinOpMin;
1719bf215546Sopenharmony_ci      break;
1720bf215546Sopenharmony_ci   case nir_intrinsic_image_deref_atomic_imax:
1721bf215546Sopenharmony_ci      params.op = LLVMAtomicRMWBinOpMax;
1722bf215546Sopenharmony_ci      break;
1723bf215546Sopenharmony_ci   default:
1724bf215546Sopenharmony_ci      break;
1725bf215546Sopenharmony_ci   }
1726bf215546Sopenharmony_ci
1727bf215546Sopenharmony_ci   params.target = glsl_sampler_to_pipe(glsl_get_sampler_dim(type),
1728bf215546Sopenharmony_ci                                        glsl_sampler_type_is_array(type));
1729bf215546Sopenharmony_ci   for (unsigned i = 0; i < 4; i++) {
1730bf215546Sopenharmony_ci      coords[i] = LLVMBuildExtractValue(builder, coord_val, i, "");
1731bf215546Sopenharmony_ci   }
1732bf215546Sopenharmony_ci   if (params.target == PIPE_TEXTURE_1D_ARRAY) {
1733bf215546Sopenharmony_ci      coords[2] = coords[1];
1734bf215546Sopenharmony_ci   }
1735bf215546Sopenharmony_ci
1736bf215546Sopenharmony_ci   params.coords = coords;
1737bf215546Sopenharmony_ci
1738bf215546Sopenharmony_ci   if (glsl_get_sampler_dim(type) == GLSL_SAMPLER_DIM_MS) {
1739bf215546Sopenharmony_ci      params.ms_index = get_src(bld_base, instr->src[2]);
1740bf215546Sopenharmony_ci   }
1741bf215546Sopenharmony_ci   if (instr->intrinsic == nir_intrinsic_image_deref_atomic_comp_swap) {
1742bf215546Sopenharmony_ci      LLVMValueRef cas_val = get_src(bld_base, instr->src[4]);
1743bf215546Sopenharmony_ci      params.indata[0] = in_val;
1744bf215546Sopenharmony_ci      params.indata2[0] = cas_val;
1745bf215546Sopenharmony_ci   } else {
1746bf215546Sopenharmony_ci      params.indata[0] = in_val;
1747bf215546Sopenharmony_ci   }
1748bf215546Sopenharmony_ci
1749bf215546Sopenharmony_ci   params.outdata = result;
1750bf215546Sopenharmony_ci   params.img_op =
1751bf215546Sopenharmony_ci      (instr->intrinsic == nir_intrinsic_image_deref_atomic_comp_swap)
1752bf215546Sopenharmony_ci      ? LP_IMG_ATOMIC_CAS : LP_IMG_ATOMIC;
1753bf215546Sopenharmony_ci   params.image_index = var->data.binding + (indir_index ? 0 : const_index);
1754bf215546Sopenharmony_ci   params.image_index_offset = indir_index;
1755bf215546Sopenharmony_ci
1756bf215546Sopenharmony_ci   bld_base->image_op(bld_base, &params);
1757bf215546Sopenharmony_ci}
1758bf215546Sopenharmony_ci
1759bf215546Sopenharmony_ci
1760bf215546Sopenharmony_cistatic void
1761bf215546Sopenharmony_civisit_image_size(struct lp_build_nir_context *bld_base,
1762bf215546Sopenharmony_ci                 nir_intrinsic_instr *instr,
1763bf215546Sopenharmony_ci                 LLVMValueRef result[NIR_MAX_VEC_COMPONENTS])
1764bf215546Sopenharmony_ci{
1765bf215546Sopenharmony_ci   nir_deref_instr *deref = nir_instr_as_deref(instr->src[0].ssa->parent_instr);
1766bf215546Sopenharmony_ci   nir_variable *var = nir_deref_instr_get_variable(deref);
1767bf215546Sopenharmony_ci   struct lp_sampler_size_query_params params = { 0 };
1768bf215546Sopenharmony_ci   unsigned const_index;
1769bf215546Sopenharmony_ci   LLVMValueRef indir_index;
1770bf215546Sopenharmony_ci   const struct glsl_type *type = glsl_without_array(var->type);
1771bf215546Sopenharmony_ci   get_deref_offset(bld_base, deref, false, NULL, NULL,
1772bf215546Sopenharmony_ci                    &const_index, &indir_index);
1773bf215546Sopenharmony_ci   params.texture_unit = var->data.binding + (indir_index ? 0 : const_index);
1774bf215546Sopenharmony_ci   params.texture_unit_offset = indir_index;
1775bf215546Sopenharmony_ci   params.target = glsl_sampler_to_pipe(glsl_get_sampler_dim(type),
1776bf215546Sopenharmony_ci                                        glsl_sampler_type_is_array(type));
1777bf215546Sopenharmony_ci   params.sizes_out = result;
1778bf215546Sopenharmony_ci
1779bf215546Sopenharmony_ci   bld_base->image_size(bld_base, &params);
1780bf215546Sopenharmony_ci}
1781bf215546Sopenharmony_ci
1782bf215546Sopenharmony_ci
1783bf215546Sopenharmony_cistatic void
1784bf215546Sopenharmony_civisit_image_samples(struct lp_build_nir_context *bld_base,
1785bf215546Sopenharmony_ci                    nir_intrinsic_instr *instr,
1786bf215546Sopenharmony_ci                    LLVMValueRef result[NIR_MAX_VEC_COMPONENTS])
1787bf215546Sopenharmony_ci{
1788bf215546Sopenharmony_ci   nir_deref_instr *deref = nir_instr_as_deref(instr->src[0].ssa->parent_instr);
1789bf215546Sopenharmony_ci   nir_variable *var = nir_deref_instr_get_variable(deref);
1790bf215546Sopenharmony_ci   struct lp_sampler_size_query_params params = { 0 };
1791bf215546Sopenharmony_ci   unsigned const_index;
1792bf215546Sopenharmony_ci   LLVMValueRef indir_index;
1793bf215546Sopenharmony_ci   const struct glsl_type *type = glsl_without_array(var->type);
1794bf215546Sopenharmony_ci   get_deref_offset(bld_base, deref, false, NULL, NULL,
1795bf215546Sopenharmony_ci                    &const_index, &indir_index);
1796bf215546Sopenharmony_ci
1797bf215546Sopenharmony_ci   params.texture_unit = var->data.binding + (indir_index ? 0 : const_index);
1798bf215546Sopenharmony_ci   params.texture_unit_offset = indir_index;
1799bf215546Sopenharmony_ci   params.target = glsl_sampler_to_pipe(glsl_get_sampler_dim(type),
1800bf215546Sopenharmony_ci                                        glsl_sampler_type_is_array(type));
1801bf215546Sopenharmony_ci   params.sizes_out = result;
1802bf215546Sopenharmony_ci   params.samples_only = true;
1803bf215546Sopenharmony_ci
1804bf215546Sopenharmony_ci   bld_base->image_size(bld_base, &params);
1805bf215546Sopenharmony_ci}
1806bf215546Sopenharmony_ci
1807bf215546Sopenharmony_cistatic void
1808bf215546Sopenharmony_civisit_shared_load(struct lp_build_nir_context *bld_base,
1809bf215546Sopenharmony_ci                  nir_intrinsic_instr *instr,
1810bf215546Sopenharmony_ci                  LLVMValueRef result[NIR_MAX_VEC_COMPONENTS])
1811bf215546Sopenharmony_ci{
1812bf215546Sopenharmony_ci   LLVMValueRef offset = get_src(bld_base, instr->src[0]);
1813bf215546Sopenharmony_ci   bool offset_is_uniform = nir_src_is_always_uniform(instr->src[0]);
1814bf215546Sopenharmony_ci   bld_base->load_mem(bld_base, nir_dest_num_components(instr->dest), nir_dest_bit_size(instr->dest),
1815bf215546Sopenharmony_ci                      offset_is_uniform, NULL, offset, result);
1816bf215546Sopenharmony_ci}
1817bf215546Sopenharmony_ci
1818bf215546Sopenharmony_ci
1819bf215546Sopenharmony_cistatic void
1820bf215546Sopenharmony_civisit_shared_store(struct lp_build_nir_context *bld_base,
1821bf215546Sopenharmony_ci                   nir_intrinsic_instr *instr)
1822bf215546Sopenharmony_ci{
1823bf215546Sopenharmony_ci   LLVMValueRef val = get_src(bld_base, instr->src[0]);
1824bf215546Sopenharmony_ci   LLVMValueRef offset = get_src(bld_base, instr->src[1]);
1825bf215546Sopenharmony_ci   bool offset_is_uniform = nir_src_is_always_uniform(instr->src[1]);
1826bf215546Sopenharmony_ci   int writemask = instr->const_index[1];
1827bf215546Sopenharmony_ci   int nc = nir_src_num_components(instr->src[0]);
1828bf215546Sopenharmony_ci   int bitsize = nir_src_bit_size(instr->src[0]);
1829bf215546Sopenharmony_ci   bld_base->store_mem(bld_base, writemask, nc, bitsize, offset_is_uniform, NULL, offset, val);
1830bf215546Sopenharmony_ci}
1831bf215546Sopenharmony_ci
1832bf215546Sopenharmony_ci
1833bf215546Sopenharmony_cistatic void
1834bf215546Sopenharmony_civisit_shared_atomic(struct lp_build_nir_context *bld_base,
1835bf215546Sopenharmony_ci                    nir_intrinsic_instr *instr,
1836bf215546Sopenharmony_ci                    LLVMValueRef result[NIR_MAX_VEC_COMPONENTS])
1837bf215546Sopenharmony_ci{
1838bf215546Sopenharmony_ci   LLVMValueRef offset = get_src(bld_base, instr->src[0]);
1839bf215546Sopenharmony_ci   LLVMValueRef val = get_src(bld_base, instr->src[1]);
1840bf215546Sopenharmony_ci   LLVMValueRef val2 = NULL;
1841bf215546Sopenharmony_ci   int bitsize = nir_src_bit_size(instr->src[1]);
1842bf215546Sopenharmony_ci   if (instr->intrinsic == nir_intrinsic_shared_atomic_comp_swap)
1843bf215546Sopenharmony_ci      val2 = get_src(bld_base, instr->src[2]);
1844bf215546Sopenharmony_ci
1845bf215546Sopenharmony_ci   bld_base->atomic_mem(bld_base, instr->intrinsic, bitsize, NULL, offset, val, val2, &result[0]);
1846bf215546Sopenharmony_ci}
1847bf215546Sopenharmony_ci
1848bf215546Sopenharmony_ci
1849bf215546Sopenharmony_cistatic void
1850bf215546Sopenharmony_civisit_barrier(struct lp_build_nir_context *bld_base)
1851bf215546Sopenharmony_ci{
1852bf215546Sopenharmony_ci   bld_base->barrier(bld_base);
1853bf215546Sopenharmony_ci}
1854bf215546Sopenharmony_ci
1855bf215546Sopenharmony_ci
1856bf215546Sopenharmony_cistatic void
1857bf215546Sopenharmony_civisit_discard(struct lp_build_nir_context *bld_base,
1858bf215546Sopenharmony_ci              nir_intrinsic_instr *instr)
1859bf215546Sopenharmony_ci{
1860bf215546Sopenharmony_ci   LLVMValueRef cond = NULL;
1861bf215546Sopenharmony_ci   if (instr->intrinsic == nir_intrinsic_discard_if) {
1862bf215546Sopenharmony_ci      cond = get_src(bld_base, instr->src[0]);
1863bf215546Sopenharmony_ci      cond = cast_type(bld_base, cond, nir_type_int, 32);
1864bf215546Sopenharmony_ci   }
1865bf215546Sopenharmony_ci   bld_base->discard(bld_base, cond);
1866bf215546Sopenharmony_ci}
1867bf215546Sopenharmony_ci
1868bf215546Sopenharmony_ci
1869bf215546Sopenharmony_cistatic void
1870bf215546Sopenharmony_civisit_load_kernel_input(struct lp_build_nir_context *bld_base,
1871bf215546Sopenharmony_ci                        nir_intrinsic_instr *instr,
1872bf215546Sopenharmony_ci                        LLVMValueRef result[NIR_MAX_VEC_COMPONENTS])
1873bf215546Sopenharmony_ci{
1874bf215546Sopenharmony_ci   LLVMValueRef offset = get_src(bld_base, instr->src[0]);
1875bf215546Sopenharmony_ci
1876bf215546Sopenharmony_ci   bool offset_is_uniform = nir_src_is_always_uniform(instr->src[0]);
1877bf215546Sopenharmony_ci   bld_base->load_kernel_arg(bld_base, nir_dest_num_components(instr->dest),
1878bf215546Sopenharmony_ci                             nir_dest_bit_size(instr->dest),
1879bf215546Sopenharmony_ci                             nir_src_bit_size(instr->src[0]),
1880bf215546Sopenharmony_ci                             offset_is_uniform, offset, result);
1881bf215546Sopenharmony_ci}
1882bf215546Sopenharmony_ci
1883bf215546Sopenharmony_ci
1884bf215546Sopenharmony_cistatic void
1885bf215546Sopenharmony_civisit_load_global(struct lp_build_nir_context *bld_base,
1886bf215546Sopenharmony_ci                  nir_intrinsic_instr *instr,
1887bf215546Sopenharmony_ci                  LLVMValueRef result[NIR_MAX_VEC_COMPONENTS])
1888bf215546Sopenharmony_ci{
1889bf215546Sopenharmony_ci   LLVMValueRef addr = get_src(bld_base, instr->src[0]);
1890bf215546Sopenharmony_ci   bool offset_is_uniform = nir_src_is_always_uniform(instr->src[0]);
1891bf215546Sopenharmony_ci   bld_base->load_global(bld_base, nir_dest_num_components(instr->dest), nir_dest_bit_size(instr->dest),
1892bf215546Sopenharmony_ci                         nir_src_bit_size(instr->src[0]),
1893bf215546Sopenharmony_ci                         offset_is_uniform, addr, result);
1894bf215546Sopenharmony_ci}
1895bf215546Sopenharmony_ci
1896bf215546Sopenharmony_ci
1897bf215546Sopenharmony_cistatic void
1898bf215546Sopenharmony_civisit_store_global(struct lp_build_nir_context *bld_base,
1899bf215546Sopenharmony_ci                   nir_intrinsic_instr *instr)
1900bf215546Sopenharmony_ci{
1901bf215546Sopenharmony_ci   LLVMValueRef val = get_src(bld_base, instr->src[0]);
1902bf215546Sopenharmony_ci   int nc = nir_src_num_components(instr->src[0]);
1903bf215546Sopenharmony_ci   int bitsize = nir_src_bit_size(instr->src[0]);
1904bf215546Sopenharmony_ci   LLVMValueRef addr = get_src(bld_base, instr->src[1]);
1905bf215546Sopenharmony_ci   int addr_bitsize = nir_src_bit_size(instr->src[1]);
1906bf215546Sopenharmony_ci   int writemask = instr->const_index[0];
1907bf215546Sopenharmony_ci   bld_base->store_global(bld_base, writemask, nc, bitsize,
1908bf215546Sopenharmony_ci                          addr_bitsize, addr, val);
1909bf215546Sopenharmony_ci}
1910bf215546Sopenharmony_ci
1911bf215546Sopenharmony_ci
1912bf215546Sopenharmony_cistatic void
1913bf215546Sopenharmony_civisit_global_atomic(struct lp_build_nir_context *bld_base,
1914bf215546Sopenharmony_ci                    nir_intrinsic_instr *instr,
1915bf215546Sopenharmony_ci                    LLVMValueRef result[NIR_MAX_VEC_COMPONENTS])
1916bf215546Sopenharmony_ci{
1917bf215546Sopenharmony_ci   LLVMValueRef addr = get_src(bld_base, instr->src[0]);
1918bf215546Sopenharmony_ci   LLVMValueRef val = get_src(bld_base, instr->src[1]);
1919bf215546Sopenharmony_ci   LLVMValueRef val2 = NULL;
1920bf215546Sopenharmony_ci   int addr_bitsize = nir_src_bit_size(instr->src[0]);
1921bf215546Sopenharmony_ci   int val_bitsize = nir_src_bit_size(instr->src[1]);
1922bf215546Sopenharmony_ci   if (instr->intrinsic == nir_intrinsic_global_atomic_comp_swap)
1923bf215546Sopenharmony_ci      val2 = get_src(bld_base, instr->src[2]);
1924bf215546Sopenharmony_ci
1925bf215546Sopenharmony_ci   bld_base->atomic_global(bld_base, instr->intrinsic, addr_bitsize,
1926bf215546Sopenharmony_ci                           val_bitsize, addr, val, val2, &result[0]);
1927bf215546Sopenharmony_ci}
1928bf215546Sopenharmony_ci
1929bf215546Sopenharmony_ci#if LLVM_VERSION_MAJOR >= 10
1930bf215546Sopenharmony_cistatic void visit_shuffle(struct lp_build_nir_context *bld_base,
1931bf215546Sopenharmony_ci                          nir_intrinsic_instr *instr,
1932bf215546Sopenharmony_ci                          LLVMValueRef dst[4])
1933bf215546Sopenharmony_ci{
1934bf215546Sopenharmony_ci   LLVMValueRef src = get_src(bld_base, instr->src[0]);
1935bf215546Sopenharmony_ci   src = cast_type(bld_base, src, nir_type_int, nir_src_bit_size(instr->src[0]));
1936bf215546Sopenharmony_ci   LLVMValueRef index = get_src(bld_base, instr->src[1]);
1937bf215546Sopenharmony_ci   index = cast_type(bld_base, index, nir_type_uint, nir_src_bit_size(instr->src[1]));
1938bf215546Sopenharmony_ci
1939bf215546Sopenharmony_ci   bld_base->shuffle(bld_base, src, index, instr, dst);
1940bf215546Sopenharmony_ci}
1941bf215546Sopenharmony_ci#endif
1942bf215546Sopenharmony_ci
1943bf215546Sopenharmony_ci
1944bf215546Sopenharmony_cistatic void
1945bf215546Sopenharmony_civisit_interp(struct lp_build_nir_context *bld_base,
1946bf215546Sopenharmony_ci             nir_intrinsic_instr *instr,
1947bf215546Sopenharmony_ci             LLVMValueRef result[NIR_MAX_VEC_COMPONENTS])
1948bf215546Sopenharmony_ci{
1949bf215546Sopenharmony_ci   struct gallivm_state *gallivm = bld_base->base.gallivm;
1950bf215546Sopenharmony_ci   LLVMBuilderRef builder = gallivm->builder;
1951bf215546Sopenharmony_ci   nir_deref_instr *deref = nir_instr_as_deref(instr->src[0].ssa->parent_instr);
1952bf215546Sopenharmony_ci   unsigned num_components = nir_dest_num_components(instr->dest);
1953bf215546Sopenharmony_ci   nir_variable *var = nir_deref_instr_get_variable(deref);
1954bf215546Sopenharmony_ci   unsigned const_index;
1955bf215546Sopenharmony_ci   LLVMValueRef indir_index;
1956bf215546Sopenharmony_ci   LLVMValueRef offsets[2] = { NULL, NULL };
1957bf215546Sopenharmony_ci   get_deref_offset(bld_base, deref, false, NULL, NULL,
1958bf215546Sopenharmony_ci                    &const_index, &indir_index);
1959bf215546Sopenharmony_ci   bool centroid = instr->intrinsic == nir_intrinsic_interp_deref_at_centroid;
1960bf215546Sopenharmony_ci   bool sample = false;
1961bf215546Sopenharmony_ci   if (instr->intrinsic == nir_intrinsic_interp_deref_at_offset) {
1962bf215546Sopenharmony_ci      for (unsigned i = 0; i < 2; i++) {
1963bf215546Sopenharmony_ci         offsets[i] = LLVMBuildExtractValue(builder, get_src(bld_base, instr->src[1]), i, "");
1964bf215546Sopenharmony_ci         offsets[i] = cast_type(bld_base, offsets[i], nir_type_float, 32);
1965bf215546Sopenharmony_ci      }
1966bf215546Sopenharmony_ci   } else if (instr->intrinsic == nir_intrinsic_interp_deref_at_sample) {
1967bf215546Sopenharmony_ci      offsets[0] = get_src(bld_base, instr->src[1]);
1968bf215546Sopenharmony_ci      offsets[0] = cast_type(bld_base, offsets[0], nir_type_int, 32);
1969bf215546Sopenharmony_ci      sample = true;
1970bf215546Sopenharmony_ci   }
1971bf215546Sopenharmony_ci   bld_base->interp_at(bld_base, num_components, var, centroid, sample,
1972bf215546Sopenharmony_ci                       const_index, indir_index, offsets, result);
1973bf215546Sopenharmony_ci}
1974bf215546Sopenharmony_ci
1975bf215546Sopenharmony_ci
1976bf215546Sopenharmony_cistatic void
1977bf215546Sopenharmony_civisit_load_scratch(struct lp_build_nir_context *bld_base,
1978bf215546Sopenharmony_ci                   nir_intrinsic_instr *instr,
1979bf215546Sopenharmony_ci                   LLVMValueRef result[NIR_MAX_VEC_COMPONENTS])
1980bf215546Sopenharmony_ci{
1981bf215546Sopenharmony_ci   LLVMValueRef offset = get_src(bld_base, instr->src[0]);
1982bf215546Sopenharmony_ci
1983bf215546Sopenharmony_ci   bld_base->load_scratch(bld_base, nir_dest_num_components(instr->dest),
1984bf215546Sopenharmony_ci                          nir_dest_bit_size(instr->dest), offset, result);
1985bf215546Sopenharmony_ci}
1986bf215546Sopenharmony_ci
1987bf215546Sopenharmony_ci
1988bf215546Sopenharmony_cistatic void
1989bf215546Sopenharmony_civisit_store_scratch(struct lp_build_nir_context *bld_base,
1990bf215546Sopenharmony_ci                    nir_intrinsic_instr *instr)
1991bf215546Sopenharmony_ci{
1992bf215546Sopenharmony_ci   LLVMValueRef val = get_src(bld_base, instr->src[0]);
1993bf215546Sopenharmony_ci   LLVMValueRef offset = get_src(bld_base, instr->src[1]);
1994bf215546Sopenharmony_ci   int writemask = instr->const_index[2];
1995bf215546Sopenharmony_ci   int nc = nir_src_num_components(instr->src[0]);
1996bf215546Sopenharmony_ci   int bitsize = nir_src_bit_size(instr->src[0]);
1997bf215546Sopenharmony_ci   bld_base->store_scratch(bld_base, writemask, nc, bitsize, offset, val);
1998bf215546Sopenharmony_ci}
1999bf215546Sopenharmony_ci
2000bf215546Sopenharmony_ci
2001bf215546Sopenharmony_cistatic void
2002bf215546Sopenharmony_civisit_intrinsic(struct lp_build_nir_context *bld_base,
2003bf215546Sopenharmony_ci                nir_intrinsic_instr *instr)
2004bf215546Sopenharmony_ci{
2005bf215546Sopenharmony_ci   LLVMValueRef result[NIR_MAX_VEC_COMPONENTS] = {0};
2006bf215546Sopenharmony_ci   switch (instr->intrinsic) {
2007bf215546Sopenharmony_ci   case nir_intrinsic_load_input:
2008bf215546Sopenharmony_ci      visit_load_input(bld_base, instr, result);
2009bf215546Sopenharmony_ci      break;
2010bf215546Sopenharmony_ci   case nir_intrinsic_store_output:
2011bf215546Sopenharmony_ci      visit_store_output(bld_base, instr);
2012bf215546Sopenharmony_ci      break;
2013bf215546Sopenharmony_ci   case nir_intrinsic_load_deref:
2014bf215546Sopenharmony_ci      visit_load_var(bld_base, instr, result);
2015bf215546Sopenharmony_ci      break;
2016bf215546Sopenharmony_ci   case nir_intrinsic_store_deref:
2017bf215546Sopenharmony_ci      visit_store_var(bld_base, instr);
2018bf215546Sopenharmony_ci      break;
2019bf215546Sopenharmony_ci   case nir_intrinsic_load_ubo:
2020bf215546Sopenharmony_ci      visit_load_ubo(bld_base, instr, result);
2021bf215546Sopenharmony_ci      break;
2022bf215546Sopenharmony_ci   case nir_intrinsic_load_push_constant:
2023bf215546Sopenharmony_ci      visit_load_push_constant(bld_base, instr, result);
2024bf215546Sopenharmony_ci      break;
2025bf215546Sopenharmony_ci   case nir_intrinsic_load_ssbo:
2026bf215546Sopenharmony_ci      visit_load_ssbo(bld_base, instr, result);
2027bf215546Sopenharmony_ci      break;
2028bf215546Sopenharmony_ci   case nir_intrinsic_store_ssbo:
2029bf215546Sopenharmony_ci      visit_store_ssbo(bld_base, instr);
2030bf215546Sopenharmony_ci      break;
2031bf215546Sopenharmony_ci   case nir_intrinsic_get_ssbo_size:
2032bf215546Sopenharmony_ci      visit_get_ssbo_size(bld_base, instr, result);
2033bf215546Sopenharmony_ci      break;
2034bf215546Sopenharmony_ci   case nir_intrinsic_load_vertex_id:
2035bf215546Sopenharmony_ci   case nir_intrinsic_load_primitive_id:
2036bf215546Sopenharmony_ci   case nir_intrinsic_load_instance_id:
2037bf215546Sopenharmony_ci   case nir_intrinsic_load_base_instance:
2038bf215546Sopenharmony_ci   case nir_intrinsic_load_base_vertex:
2039bf215546Sopenharmony_ci   case nir_intrinsic_load_first_vertex:
2040bf215546Sopenharmony_ci   case nir_intrinsic_load_workgroup_id:
2041bf215546Sopenharmony_ci   case nir_intrinsic_load_local_invocation_id:
2042bf215546Sopenharmony_ci   case nir_intrinsic_load_local_invocation_index:
2043bf215546Sopenharmony_ci   case nir_intrinsic_load_num_workgroups:
2044bf215546Sopenharmony_ci   case nir_intrinsic_load_invocation_id:
2045bf215546Sopenharmony_ci   case nir_intrinsic_load_front_face:
2046bf215546Sopenharmony_ci   case nir_intrinsic_load_draw_id:
2047bf215546Sopenharmony_ci   case nir_intrinsic_load_workgroup_size:
2048bf215546Sopenharmony_ci   case nir_intrinsic_load_work_dim:
2049bf215546Sopenharmony_ci   case nir_intrinsic_load_tess_coord:
2050bf215546Sopenharmony_ci   case nir_intrinsic_load_tess_level_outer:
2051bf215546Sopenharmony_ci   case nir_intrinsic_load_tess_level_inner:
2052bf215546Sopenharmony_ci   case nir_intrinsic_load_patch_vertices_in:
2053bf215546Sopenharmony_ci   case nir_intrinsic_load_sample_id:
2054bf215546Sopenharmony_ci   case nir_intrinsic_load_sample_pos:
2055bf215546Sopenharmony_ci   case nir_intrinsic_load_sample_mask_in:
2056bf215546Sopenharmony_ci   case nir_intrinsic_load_view_index:
2057bf215546Sopenharmony_ci   case nir_intrinsic_load_subgroup_invocation:
2058bf215546Sopenharmony_ci   case nir_intrinsic_load_subgroup_id:
2059bf215546Sopenharmony_ci   case nir_intrinsic_load_num_subgroups:
2060bf215546Sopenharmony_ci      bld_base->sysval_intrin(bld_base, instr, result);
2061bf215546Sopenharmony_ci      break;
2062bf215546Sopenharmony_ci   case nir_intrinsic_load_helper_invocation:
2063bf215546Sopenharmony_ci      bld_base->helper_invocation(bld_base, &result[0]);
2064bf215546Sopenharmony_ci      break;
2065bf215546Sopenharmony_ci   case nir_intrinsic_discard_if:
2066bf215546Sopenharmony_ci   case nir_intrinsic_discard:
2067bf215546Sopenharmony_ci      visit_discard(bld_base, instr);
2068bf215546Sopenharmony_ci      break;
2069bf215546Sopenharmony_ci   case nir_intrinsic_emit_vertex:
2070bf215546Sopenharmony_ci      bld_base->emit_vertex(bld_base, nir_intrinsic_stream_id(instr));
2071bf215546Sopenharmony_ci      break;
2072bf215546Sopenharmony_ci   case nir_intrinsic_end_primitive:
2073bf215546Sopenharmony_ci      bld_base->end_primitive(bld_base, nir_intrinsic_stream_id(instr));
2074bf215546Sopenharmony_ci      break;
2075bf215546Sopenharmony_ci   case nir_intrinsic_ssbo_atomic_add:
2076bf215546Sopenharmony_ci   case nir_intrinsic_ssbo_atomic_imin:
2077bf215546Sopenharmony_ci   case nir_intrinsic_ssbo_atomic_imax:
2078bf215546Sopenharmony_ci   case nir_intrinsic_ssbo_atomic_umin:
2079bf215546Sopenharmony_ci   case nir_intrinsic_ssbo_atomic_umax:
2080bf215546Sopenharmony_ci   case nir_intrinsic_ssbo_atomic_and:
2081bf215546Sopenharmony_ci   case nir_intrinsic_ssbo_atomic_or:
2082bf215546Sopenharmony_ci   case nir_intrinsic_ssbo_atomic_xor:
2083bf215546Sopenharmony_ci   case nir_intrinsic_ssbo_atomic_exchange:
2084bf215546Sopenharmony_ci   case nir_intrinsic_ssbo_atomic_comp_swap:
2085bf215546Sopenharmony_ci      visit_ssbo_atomic(bld_base, instr, result);
2086bf215546Sopenharmony_ci      break;
2087bf215546Sopenharmony_ci   case nir_intrinsic_image_deref_load:
2088bf215546Sopenharmony_ci      visit_load_image(bld_base, instr, result);
2089bf215546Sopenharmony_ci      break;
2090bf215546Sopenharmony_ci   case nir_intrinsic_image_deref_store:
2091bf215546Sopenharmony_ci      visit_store_image(bld_base, instr);
2092bf215546Sopenharmony_ci      break;
2093bf215546Sopenharmony_ci   case nir_intrinsic_image_deref_atomic_add:
2094bf215546Sopenharmony_ci   case nir_intrinsic_image_deref_atomic_imin:
2095bf215546Sopenharmony_ci   case nir_intrinsic_image_deref_atomic_imax:
2096bf215546Sopenharmony_ci   case nir_intrinsic_image_deref_atomic_umin:
2097bf215546Sopenharmony_ci   case nir_intrinsic_image_deref_atomic_umax:
2098bf215546Sopenharmony_ci   case nir_intrinsic_image_deref_atomic_and:
2099bf215546Sopenharmony_ci   case nir_intrinsic_image_deref_atomic_or:
2100bf215546Sopenharmony_ci   case nir_intrinsic_image_deref_atomic_xor:
2101bf215546Sopenharmony_ci   case nir_intrinsic_image_deref_atomic_exchange:
2102bf215546Sopenharmony_ci   case nir_intrinsic_image_deref_atomic_comp_swap:
2103bf215546Sopenharmony_ci      visit_atomic_image(bld_base, instr, result);
2104bf215546Sopenharmony_ci      break;
2105bf215546Sopenharmony_ci   case nir_intrinsic_image_deref_size:
2106bf215546Sopenharmony_ci      visit_image_size(bld_base, instr, result);
2107bf215546Sopenharmony_ci      break;
2108bf215546Sopenharmony_ci   case nir_intrinsic_image_deref_samples:
2109bf215546Sopenharmony_ci      visit_image_samples(bld_base, instr, result);
2110bf215546Sopenharmony_ci      break;
2111bf215546Sopenharmony_ci   case nir_intrinsic_load_shared:
2112bf215546Sopenharmony_ci      visit_shared_load(bld_base, instr, result);
2113bf215546Sopenharmony_ci      break;
2114bf215546Sopenharmony_ci   case nir_intrinsic_store_shared:
2115bf215546Sopenharmony_ci      visit_shared_store(bld_base, instr);
2116bf215546Sopenharmony_ci      break;
2117bf215546Sopenharmony_ci   case nir_intrinsic_shared_atomic_add:
2118bf215546Sopenharmony_ci   case nir_intrinsic_shared_atomic_imin:
2119bf215546Sopenharmony_ci   case nir_intrinsic_shared_atomic_umin:
2120bf215546Sopenharmony_ci   case nir_intrinsic_shared_atomic_imax:
2121bf215546Sopenharmony_ci   case nir_intrinsic_shared_atomic_umax:
2122bf215546Sopenharmony_ci   case nir_intrinsic_shared_atomic_and:
2123bf215546Sopenharmony_ci   case nir_intrinsic_shared_atomic_or:
2124bf215546Sopenharmony_ci   case nir_intrinsic_shared_atomic_xor:
2125bf215546Sopenharmony_ci   case nir_intrinsic_shared_atomic_exchange:
2126bf215546Sopenharmony_ci   case nir_intrinsic_shared_atomic_comp_swap:
2127bf215546Sopenharmony_ci      visit_shared_atomic(bld_base, instr, result);
2128bf215546Sopenharmony_ci      break;
2129bf215546Sopenharmony_ci   case nir_intrinsic_control_barrier:
2130bf215546Sopenharmony_ci   case nir_intrinsic_scoped_barrier:
2131bf215546Sopenharmony_ci      visit_barrier(bld_base);
2132bf215546Sopenharmony_ci      break;
2133bf215546Sopenharmony_ci   case nir_intrinsic_group_memory_barrier:
2134bf215546Sopenharmony_ci   case nir_intrinsic_memory_barrier:
2135bf215546Sopenharmony_ci   case nir_intrinsic_memory_barrier_shared:
2136bf215546Sopenharmony_ci   case nir_intrinsic_memory_barrier_buffer:
2137bf215546Sopenharmony_ci   case nir_intrinsic_memory_barrier_image:
2138bf215546Sopenharmony_ci   case nir_intrinsic_memory_barrier_tcs_patch:
2139bf215546Sopenharmony_ci      break;
2140bf215546Sopenharmony_ci   case nir_intrinsic_load_kernel_input:
2141bf215546Sopenharmony_ci      visit_load_kernel_input(bld_base, instr, result);
2142bf215546Sopenharmony_ci     break;
2143bf215546Sopenharmony_ci   case nir_intrinsic_load_global:
2144bf215546Sopenharmony_ci   case nir_intrinsic_load_global_constant:
2145bf215546Sopenharmony_ci      visit_load_global(bld_base, instr, result);
2146bf215546Sopenharmony_ci      break;
2147bf215546Sopenharmony_ci   case nir_intrinsic_store_global:
2148bf215546Sopenharmony_ci      visit_store_global(bld_base, instr);
2149bf215546Sopenharmony_ci      break;
2150bf215546Sopenharmony_ci   case nir_intrinsic_global_atomic_add:
2151bf215546Sopenharmony_ci   case nir_intrinsic_global_atomic_imin:
2152bf215546Sopenharmony_ci   case nir_intrinsic_global_atomic_umin:
2153bf215546Sopenharmony_ci   case nir_intrinsic_global_atomic_imax:
2154bf215546Sopenharmony_ci   case nir_intrinsic_global_atomic_umax:
2155bf215546Sopenharmony_ci   case nir_intrinsic_global_atomic_and:
2156bf215546Sopenharmony_ci   case nir_intrinsic_global_atomic_or:
2157bf215546Sopenharmony_ci   case nir_intrinsic_global_atomic_xor:
2158bf215546Sopenharmony_ci   case nir_intrinsic_global_atomic_exchange:
2159bf215546Sopenharmony_ci   case nir_intrinsic_global_atomic_comp_swap:
2160bf215546Sopenharmony_ci      visit_global_atomic(bld_base, instr, result);
2161bf215546Sopenharmony_ci      break;
2162bf215546Sopenharmony_ci   case nir_intrinsic_vote_all:
2163bf215546Sopenharmony_ci   case nir_intrinsic_vote_any:
2164bf215546Sopenharmony_ci   case nir_intrinsic_vote_ieq:
2165bf215546Sopenharmony_ci   case nir_intrinsic_vote_feq:
2166bf215546Sopenharmony_ci      bld_base->vote(bld_base, cast_type(bld_base, get_src(bld_base, instr->src[0]), nir_type_int, nir_src_bit_size(instr->src[0])), instr, result);
2167bf215546Sopenharmony_ci      break;
2168bf215546Sopenharmony_ci   case nir_intrinsic_elect:
2169bf215546Sopenharmony_ci      bld_base->elect(bld_base, result);
2170bf215546Sopenharmony_ci      break;
2171bf215546Sopenharmony_ci   case nir_intrinsic_reduce:
2172bf215546Sopenharmony_ci   case nir_intrinsic_inclusive_scan:
2173bf215546Sopenharmony_ci   case nir_intrinsic_exclusive_scan:
2174bf215546Sopenharmony_ci      bld_base->reduce(bld_base, cast_type(bld_base, get_src(bld_base, instr->src[0]), nir_type_int, nir_src_bit_size(instr->src[0])), instr, result);
2175bf215546Sopenharmony_ci      break;
2176bf215546Sopenharmony_ci   case nir_intrinsic_ballot:
2177bf215546Sopenharmony_ci      bld_base->ballot(bld_base, cast_type(bld_base, get_src(bld_base, instr->src[0]), nir_type_int, 32), instr, result);
2178bf215546Sopenharmony_ci      break;
2179bf215546Sopenharmony_ci#if LLVM_VERSION_MAJOR >= 10
2180bf215546Sopenharmony_ci   case nir_intrinsic_shuffle:
2181bf215546Sopenharmony_ci      visit_shuffle(bld_base, instr, result);
2182bf215546Sopenharmony_ci      break;
2183bf215546Sopenharmony_ci#endif
2184bf215546Sopenharmony_ci   case nir_intrinsic_read_invocation:
2185bf215546Sopenharmony_ci   case nir_intrinsic_read_first_invocation: {
2186bf215546Sopenharmony_ci      LLVMValueRef src1 = NULL;
2187bf215546Sopenharmony_ci      LLVMValueRef src0 = get_src(bld_base, instr->src[0]);
2188bf215546Sopenharmony_ci      if (instr->intrinsic == nir_intrinsic_read_invocation) {
2189bf215546Sopenharmony_ci         src1 = cast_type(bld_base, get_src(bld_base, instr->src[1]), nir_type_int, 32);
2190bf215546Sopenharmony_ci         src0 = cast_type(bld_base, src0, nir_type_int, nir_src_bit_size(instr->src[0]));
2191bf215546Sopenharmony_ci      }
2192bf215546Sopenharmony_ci      bld_base->read_invocation(bld_base, src0, nir_src_bit_size(instr->src[0]), src1, result);
2193bf215546Sopenharmony_ci      break;
2194bf215546Sopenharmony_ci   }
2195bf215546Sopenharmony_ci   case nir_intrinsic_interp_deref_at_offset:
2196bf215546Sopenharmony_ci   case nir_intrinsic_interp_deref_at_centroid:
2197bf215546Sopenharmony_ci   case nir_intrinsic_interp_deref_at_sample:
2198bf215546Sopenharmony_ci      visit_interp(bld_base, instr, result);
2199bf215546Sopenharmony_ci      break;
2200bf215546Sopenharmony_ci   case nir_intrinsic_load_scratch:
2201bf215546Sopenharmony_ci      visit_load_scratch(bld_base, instr, result);
2202bf215546Sopenharmony_ci      break;
2203bf215546Sopenharmony_ci   case nir_intrinsic_store_scratch:
2204bf215546Sopenharmony_ci      visit_store_scratch(bld_base, instr);
2205bf215546Sopenharmony_ci      break;
2206bf215546Sopenharmony_ci   default:
2207bf215546Sopenharmony_ci      fprintf(stderr, "Unsupported intrinsic: ");
2208bf215546Sopenharmony_ci      nir_print_instr(&instr->instr, stderr);
2209bf215546Sopenharmony_ci      fprintf(stderr, "\n");
2210bf215546Sopenharmony_ci      assert(0);
2211bf215546Sopenharmony_ci      break;
2212bf215546Sopenharmony_ci   }
2213bf215546Sopenharmony_ci   if (result[0]) {
2214bf215546Sopenharmony_ci      assign_dest(bld_base, &instr->dest, result);
2215bf215546Sopenharmony_ci   }
2216bf215546Sopenharmony_ci}
2217bf215546Sopenharmony_ci
2218bf215546Sopenharmony_ci
2219bf215546Sopenharmony_cistatic void
2220bf215546Sopenharmony_civisit_txs(struct lp_build_nir_context *bld_base, nir_tex_instr *instr)
2221bf215546Sopenharmony_ci{
2222bf215546Sopenharmony_ci   struct lp_sampler_size_query_params params = { 0 };
2223bf215546Sopenharmony_ci   LLVMValueRef sizes_out[NIR_MAX_VEC_COMPONENTS];
2224bf215546Sopenharmony_ci   LLVMValueRef explicit_lod = NULL;
2225bf215546Sopenharmony_ci   LLVMValueRef texture_unit_offset = NULL;
2226bf215546Sopenharmony_ci   for (unsigned i = 0; i < instr->num_srcs; i++) {
2227bf215546Sopenharmony_ci      switch (instr->src[i].src_type) {
2228bf215546Sopenharmony_ci      case nir_tex_src_lod:
2229bf215546Sopenharmony_ci         explicit_lod = cast_type(bld_base,
2230bf215546Sopenharmony_ci                                  get_src(bld_base, instr->src[i].src),
2231bf215546Sopenharmony_ci                                  nir_type_int, 32);
2232bf215546Sopenharmony_ci         break;
2233bf215546Sopenharmony_ci      case nir_tex_src_texture_offset:
2234bf215546Sopenharmony_ci         texture_unit_offset = get_src(bld_base, instr->src[i].src);
2235bf215546Sopenharmony_ci         break;
2236bf215546Sopenharmony_ci      default:
2237bf215546Sopenharmony_ci         break;
2238bf215546Sopenharmony_ci      }
2239bf215546Sopenharmony_ci   }
2240bf215546Sopenharmony_ci
2241bf215546Sopenharmony_ci   params.target = glsl_sampler_to_pipe(instr->sampler_dim, instr->is_array);
2242bf215546Sopenharmony_ci   params.texture_unit = instr->texture_index;
2243bf215546Sopenharmony_ci   params.explicit_lod = explicit_lod;
2244bf215546Sopenharmony_ci   params.is_sviewinfo = TRUE;
2245bf215546Sopenharmony_ci   params.sizes_out = sizes_out;
2246bf215546Sopenharmony_ci   params.samples_only = (instr->op == nir_texop_texture_samples);
2247bf215546Sopenharmony_ci   params.texture_unit_offset = texture_unit_offset;
2248bf215546Sopenharmony_ci
2249bf215546Sopenharmony_ci   if (instr->op == nir_texop_query_levels)
2250bf215546Sopenharmony_ci      params.explicit_lod = bld_base->uint_bld.zero;
2251bf215546Sopenharmony_ci   bld_base->tex_size(bld_base, &params);
2252bf215546Sopenharmony_ci   assign_dest(bld_base, &instr->dest,
2253bf215546Sopenharmony_ci               &sizes_out[instr->op == nir_texop_query_levels ? 3 : 0]);
2254bf215546Sopenharmony_ci}
2255bf215546Sopenharmony_ci
2256bf215546Sopenharmony_ci
2257bf215546Sopenharmony_cistatic enum lp_sampler_lod_property
2258bf215546Sopenharmony_cilp_build_nir_lod_property(struct lp_build_nir_context *bld_base,
2259bf215546Sopenharmony_ci                          nir_src lod_src)
2260bf215546Sopenharmony_ci{
2261bf215546Sopenharmony_ci   enum lp_sampler_lod_property lod_property;
2262bf215546Sopenharmony_ci
2263bf215546Sopenharmony_ci   if (nir_src_is_always_uniform(lod_src)) {
2264bf215546Sopenharmony_ci      lod_property = LP_SAMPLER_LOD_SCALAR;
2265bf215546Sopenharmony_ci   } else if (bld_base->shader->info.stage == MESA_SHADER_FRAGMENT) {
2266bf215546Sopenharmony_ci      if (gallivm_perf & GALLIVM_PERF_NO_QUAD_LOD)
2267bf215546Sopenharmony_ci         lod_property = LP_SAMPLER_LOD_PER_ELEMENT;
2268bf215546Sopenharmony_ci      else
2269bf215546Sopenharmony_ci         lod_property = LP_SAMPLER_LOD_PER_QUAD;
2270bf215546Sopenharmony_ci   } else {
2271bf215546Sopenharmony_ci      lod_property = LP_SAMPLER_LOD_PER_ELEMENT;
2272bf215546Sopenharmony_ci   }
2273bf215546Sopenharmony_ci   return lod_property;
2274bf215546Sopenharmony_ci}
2275bf215546Sopenharmony_ci
2276bf215546Sopenharmony_ci
2277bf215546Sopenharmony_cistatic void
2278bf215546Sopenharmony_civisit_tex(struct lp_build_nir_context *bld_base, nir_tex_instr *instr)
2279bf215546Sopenharmony_ci{
2280bf215546Sopenharmony_ci   struct gallivm_state *gallivm = bld_base->base.gallivm;
2281bf215546Sopenharmony_ci   LLVMBuilderRef builder = gallivm->builder;
2282bf215546Sopenharmony_ci   LLVMValueRef coords[5];
2283bf215546Sopenharmony_ci   LLVMValueRef offsets[3] = { NULL };
2284bf215546Sopenharmony_ci   LLVMValueRef explicit_lod = NULL, ms_index = NULL;
2285bf215546Sopenharmony_ci   struct lp_sampler_params params;
2286bf215546Sopenharmony_ci   struct lp_derivatives derivs;
2287bf215546Sopenharmony_ci   unsigned sample_key = 0;
2288bf215546Sopenharmony_ci   nir_deref_instr *texture_deref_instr = NULL;
2289bf215546Sopenharmony_ci   nir_deref_instr *sampler_deref_instr = NULL;
2290bf215546Sopenharmony_ci   LLVMValueRef texture_unit_offset = NULL;
2291bf215546Sopenharmony_ci   LLVMValueRef texel[NIR_MAX_VEC_COMPONENTS];
2292bf215546Sopenharmony_ci   unsigned lod_src = 0;
2293bf215546Sopenharmony_ci   LLVMValueRef coord_undef = LLVMGetUndef(bld_base->base.int_vec_type);
2294bf215546Sopenharmony_ci   unsigned coord_vals = is_aos(bld_base) ? 1 : instr->coord_components;
2295bf215546Sopenharmony_ci   memset(&params, 0, sizeof(params));
2296bf215546Sopenharmony_ci   enum lp_sampler_lod_property lod_property = LP_SAMPLER_LOD_SCALAR;
2297bf215546Sopenharmony_ci
2298bf215546Sopenharmony_ci   if (instr->op == nir_texop_txs || instr->op == nir_texop_query_levels || instr->op == nir_texop_texture_samples) {
2299bf215546Sopenharmony_ci      visit_txs(bld_base, instr);
2300bf215546Sopenharmony_ci      return;
2301bf215546Sopenharmony_ci   }
2302bf215546Sopenharmony_ci   if (instr->op == nir_texop_txf || instr->op == nir_texop_txf_ms)
2303bf215546Sopenharmony_ci      sample_key |= LP_SAMPLER_OP_FETCH << LP_SAMPLER_OP_TYPE_SHIFT;
2304bf215546Sopenharmony_ci   else if (instr->op == nir_texop_tg4) {
2305bf215546Sopenharmony_ci      sample_key |= LP_SAMPLER_OP_GATHER << LP_SAMPLER_OP_TYPE_SHIFT;
2306bf215546Sopenharmony_ci      sample_key |= (instr->component << LP_SAMPLER_GATHER_COMP_SHIFT);
2307bf215546Sopenharmony_ci   } else if (instr->op == nir_texop_lod)
2308bf215546Sopenharmony_ci      sample_key |= LP_SAMPLER_OP_LODQ << LP_SAMPLER_OP_TYPE_SHIFT;
2309bf215546Sopenharmony_ci   for (unsigned i = 0; i < instr->num_srcs; i++) {
2310bf215546Sopenharmony_ci      switch (instr->src[i].src_type) {
2311bf215546Sopenharmony_ci      case nir_tex_src_coord: {
2312bf215546Sopenharmony_ci         LLVMValueRef coord = get_src(bld_base, instr->src[i].src);
2313bf215546Sopenharmony_ci         if (coord_vals == 1)
2314bf215546Sopenharmony_ci            coords[0] = coord;
2315bf215546Sopenharmony_ci         else {
2316bf215546Sopenharmony_ci            for (unsigned chan = 0; chan < instr->coord_components; ++chan)
2317bf215546Sopenharmony_ci               coords[chan] = LLVMBuildExtractValue(builder, coord,
2318bf215546Sopenharmony_ci                                                    chan, "");
2319bf215546Sopenharmony_ci         }
2320bf215546Sopenharmony_ci         for (unsigned chan = coord_vals; chan < 5; chan++)
2321bf215546Sopenharmony_ci            coords[chan] = coord_undef;
2322bf215546Sopenharmony_ci
2323bf215546Sopenharmony_ci         break;
2324bf215546Sopenharmony_ci      }
2325bf215546Sopenharmony_ci      case nir_tex_src_texture_deref:
2326bf215546Sopenharmony_ci         texture_deref_instr = nir_src_as_deref(instr->src[i].src);
2327bf215546Sopenharmony_ci         break;
2328bf215546Sopenharmony_ci      case nir_tex_src_sampler_deref:
2329bf215546Sopenharmony_ci         sampler_deref_instr = nir_src_as_deref(instr->src[i].src);
2330bf215546Sopenharmony_ci         break;
2331bf215546Sopenharmony_ci      case nir_tex_src_comparator:
2332bf215546Sopenharmony_ci         sample_key |= LP_SAMPLER_SHADOW;
2333bf215546Sopenharmony_ci         coords[4] = get_src(bld_base, instr->src[i].src);
2334bf215546Sopenharmony_ci         coords[4] = cast_type(bld_base, coords[4], nir_type_float, 32);
2335bf215546Sopenharmony_ci         break;
2336bf215546Sopenharmony_ci      case nir_tex_src_bias:
2337bf215546Sopenharmony_ci         sample_key |= LP_SAMPLER_LOD_BIAS << LP_SAMPLER_LOD_CONTROL_SHIFT;
2338bf215546Sopenharmony_ci         lod_src = i;
2339bf215546Sopenharmony_ci         explicit_lod = cast_type(bld_base, get_src(bld_base, instr->src[i].src), nir_type_float, 32);
2340bf215546Sopenharmony_ci         break;
2341bf215546Sopenharmony_ci      case nir_tex_src_lod:
2342bf215546Sopenharmony_ci         sample_key |= LP_SAMPLER_LOD_EXPLICIT << LP_SAMPLER_LOD_CONTROL_SHIFT;
2343bf215546Sopenharmony_ci         lod_src = i;
2344bf215546Sopenharmony_ci         if (instr->op == nir_texop_txf)
2345bf215546Sopenharmony_ci            explicit_lod = cast_type(bld_base, get_src(bld_base, instr->src[i].src), nir_type_int, 32);
2346bf215546Sopenharmony_ci         else
2347bf215546Sopenharmony_ci            explicit_lod = cast_type(bld_base, get_src(bld_base, instr->src[i].src), nir_type_float, 32);
2348bf215546Sopenharmony_ci         break;
2349bf215546Sopenharmony_ci      case nir_tex_src_ddx: {
2350bf215546Sopenharmony_ci         int deriv_cnt = instr->coord_components;
2351bf215546Sopenharmony_ci         if (instr->is_array)
2352bf215546Sopenharmony_ci            deriv_cnt--;
2353bf215546Sopenharmony_ci         LLVMValueRef deriv_val = get_src(bld_base, instr->src[i].src);
2354bf215546Sopenharmony_ci         if (deriv_cnt == 1)
2355bf215546Sopenharmony_ci            derivs.ddx[0] = deriv_val;
2356bf215546Sopenharmony_ci         else
2357bf215546Sopenharmony_ci            for (unsigned chan = 0; chan < deriv_cnt; ++chan)
2358bf215546Sopenharmony_ci               derivs.ddx[chan] = LLVMBuildExtractValue(builder, deriv_val,
2359bf215546Sopenharmony_ci                                                        chan, "");
2360bf215546Sopenharmony_ci         for (unsigned chan = 0; chan < deriv_cnt; ++chan)
2361bf215546Sopenharmony_ci            derivs.ddx[chan] = cast_type(bld_base, derivs.ddx[chan], nir_type_float, 32);
2362bf215546Sopenharmony_ci         break;
2363bf215546Sopenharmony_ci      }
2364bf215546Sopenharmony_ci      case nir_tex_src_ddy: {
2365bf215546Sopenharmony_ci         int deriv_cnt = instr->coord_components;
2366bf215546Sopenharmony_ci         if (instr->is_array)
2367bf215546Sopenharmony_ci            deriv_cnt--;
2368bf215546Sopenharmony_ci         LLVMValueRef deriv_val = get_src(bld_base, instr->src[i].src);
2369bf215546Sopenharmony_ci         if (deriv_cnt == 1)
2370bf215546Sopenharmony_ci            derivs.ddy[0] = deriv_val;
2371bf215546Sopenharmony_ci         else
2372bf215546Sopenharmony_ci            for (unsigned chan = 0; chan < deriv_cnt; ++chan)
2373bf215546Sopenharmony_ci               derivs.ddy[chan] = LLVMBuildExtractValue(builder, deriv_val,
2374bf215546Sopenharmony_ci                                                        chan, "");
2375bf215546Sopenharmony_ci         for (unsigned chan = 0; chan < deriv_cnt; ++chan)
2376bf215546Sopenharmony_ci            derivs.ddy[chan] = cast_type(bld_base, derivs.ddy[chan], nir_type_float, 32);
2377bf215546Sopenharmony_ci         break;
2378bf215546Sopenharmony_ci      }
2379bf215546Sopenharmony_ci      case nir_tex_src_offset: {
2380bf215546Sopenharmony_ci         int offset_cnt = instr->coord_components;
2381bf215546Sopenharmony_ci         if (instr->is_array)
2382bf215546Sopenharmony_ci            offset_cnt--;
2383bf215546Sopenharmony_ci         LLVMValueRef offset_val = get_src(bld_base, instr->src[i].src);
2384bf215546Sopenharmony_ci         sample_key |= LP_SAMPLER_OFFSETS;
2385bf215546Sopenharmony_ci         if (offset_cnt == 1)
2386bf215546Sopenharmony_ci            offsets[0] = cast_type(bld_base, offset_val, nir_type_int, 32);
2387bf215546Sopenharmony_ci         else {
2388bf215546Sopenharmony_ci            for (unsigned chan = 0; chan < offset_cnt; ++chan) {
2389bf215546Sopenharmony_ci               offsets[chan] = LLVMBuildExtractValue(builder, offset_val,
2390bf215546Sopenharmony_ci                                                     chan, "");
2391bf215546Sopenharmony_ci               offsets[chan] = cast_type(bld_base, offsets[chan], nir_type_int, 32);
2392bf215546Sopenharmony_ci            }
2393bf215546Sopenharmony_ci         }
2394bf215546Sopenharmony_ci         break;
2395bf215546Sopenharmony_ci      }
2396bf215546Sopenharmony_ci      case nir_tex_src_ms_index:
2397bf215546Sopenharmony_ci         sample_key |= LP_SAMPLER_FETCH_MS;
2398bf215546Sopenharmony_ci         ms_index = cast_type(bld_base, get_src(bld_base, instr->src[i].src), nir_type_int, 32);
2399bf215546Sopenharmony_ci         break;
2400bf215546Sopenharmony_ci
2401bf215546Sopenharmony_ci      case nir_tex_src_texture_offset:
2402bf215546Sopenharmony_ci         texture_unit_offset = get_src(bld_base, instr->src[i].src);
2403bf215546Sopenharmony_ci         break;
2404bf215546Sopenharmony_ci      case nir_tex_src_sampler_offset:
2405bf215546Sopenharmony_ci         break;
2406bf215546Sopenharmony_ci      default:
2407bf215546Sopenharmony_ci         assert(0);
2408bf215546Sopenharmony_ci         break;
2409bf215546Sopenharmony_ci      }
2410bf215546Sopenharmony_ci   }
2411bf215546Sopenharmony_ci   if (!sampler_deref_instr)
2412bf215546Sopenharmony_ci      sampler_deref_instr = texture_deref_instr;
2413bf215546Sopenharmony_ci
2414bf215546Sopenharmony_ci   if (explicit_lod)
2415bf215546Sopenharmony_ci      lod_property = lp_build_nir_lod_property(bld_base, instr->src[lod_src].src);
2416bf215546Sopenharmony_ci
2417bf215546Sopenharmony_ci   if (instr->op == nir_texop_tex || instr->op == nir_texop_tg4 || instr->op == nir_texop_txb ||
2418bf215546Sopenharmony_ci       instr->op == nir_texop_txl || instr->op == nir_texop_txd || instr->op == nir_texop_lod)
2419bf215546Sopenharmony_ci      for (unsigned chan = 0; chan < coord_vals; ++chan)
2420bf215546Sopenharmony_ci         coords[chan] = cast_type(bld_base, coords[chan], nir_type_float, 32);
2421bf215546Sopenharmony_ci   else if (instr->op == nir_texop_txf || instr->op == nir_texop_txf_ms)
2422bf215546Sopenharmony_ci      for (unsigned chan = 0; chan < instr->coord_components; ++chan)
2423bf215546Sopenharmony_ci         coords[chan] = cast_type(bld_base, coords[chan], nir_type_int, 32);
2424bf215546Sopenharmony_ci
2425bf215546Sopenharmony_ci   if (instr->is_array && instr->sampler_dim == GLSL_SAMPLER_DIM_1D) {
2426bf215546Sopenharmony_ci      /* move layer coord for 1d arrays. */
2427bf215546Sopenharmony_ci      coords[2] = coords[1];
2428bf215546Sopenharmony_ci      coords[1] = coord_undef;
2429bf215546Sopenharmony_ci   }
2430bf215546Sopenharmony_ci
2431bf215546Sopenharmony_ci   uint32_t samp_base_index = 0, tex_base_index = 0;
2432bf215546Sopenharmony_ci   if (!sampler_deref_instr) {
2433bf215546Sopenharmony_ci      int samp_src_index = nir_tex_instr_src_index(instr, nir_tex_src_sampler_handle);
2434bf215546Sopenharmony_ci      if (samp_src_index == -1) {
2435bf215546Sopenharmony_ci         samp_base_index = instr->sampler_index;
2436bf215546Sopenharmony_ci      }
2437bf215546Sopenharmony_ci   }
2438bf215546Sopenharmony_ci   if (!texture_deref_instr) {
2439bf215546Sopenharmony_ci      int tex_src_index = nir_tex_instr_src_index(instr, nir_tex_src_texture_handle);
2440bf215546Sopenharmony_ci      if (tex_src_index == -1) {
2441bf215546Sopenharmony_ci         tex_base_index = instr->texture_index;
2442bf215546Sopenharmony_ci      }
2443bf215546Sopenharmony_ci   }
2444bf215546Sopenharmony_ci
2445bf215546Sopenharmony_ci   if (instr->op == nir_texop_txd) {
2446bf215546Sopenharmony_ci      sample_key |= LP_SAMPLER_LOD_DERIVATIVES << LP_SAMPLER_LOD_CONTROL_SHIFT;
2447bf215546Sopenharmony_ci      params.derivs = &derivs;
2448bf215546Sopenharmony_ci      if (bld_base->shader->info.stage == MESA_SHADER_FRAGMENT) {
2449bf215546Sopenharmony_ci         if (gallivm_perf & GALLIVM_PERF_NO_QUAD_LOD)
2450bf215546Sopenharmony_ci            lod_property = LP_SAMPLER_LOD_PER_ELEMENT;
2451bf215546Sopenharmony_ci         else
2452bf215546Sopenharmony_ci            lod_property = LP_SAMPLER_LOD_PER_QUAD;
2453bf215546Sopenharmony_ci      } else
2454bf215546Sopenharmony_ci         lod_property = LP_SAMPLER_LOD_PER_ELEMENT;
2455bf215546Sopenharmony_ci   }
2456bf215546Sopenharmony_ci
2457bf215546Sopenharmony_ci   sample_key |= lod_property << LP_SAMPLER_LOD_PROPERTY_SHIFT;
2458bf215546Sopenharmony_ci   params.sample_key = sample_key;
2459bf215546Sopenharmony_ci   params.offsets = offsets;
2460bf215546Sopenharmony_ci   params.texture_index = tex_base_index;
2461bf215546Sopenharmony_ci   params.texture_index_offset = texture_unit_offset;
2462bf215546Sopenharmony_ci   params.sampler_index = samp_base_index;
2463bf215546Sopenharmony_ci   params.coords = coords;
2464bf215546Sopenharmony_ci   params.texel = texel;
2465bf215546Sopenharmony_ci   params.lod = explicit_lod;
2466bf215546Sopenharmony_ci   params.ms_index = ms_index;
2467bf215546Sopenharmony_ci   params.aniso_filter_table = bld_base->aniso_filter_table;
2468bf215546Sopenharmony_ci   bld_base->tex(bld_base, &params);
2469bf215546Sopenharmony_ci
2470bf215546Sopenharmony_ci   if (nir_dest_bit_size(instr->dest) != 32) {
2471bf215546Sopenharmony_ci      assert(nir_dest_bit_size(instr->dest) == 16);
2472bf215546Sopenharmony_ci      LLVMTypeRef vec_type = NULL;
2473bf215546Sopenharmony_ci      bool is_float = false;
2474bf215546Sopenharmony_ci      switch (nir_alu_type_get_base_type(instr->dest_type)) {
2475bf215546Sopenharmony_ci      case nir_type_float:
2476bf215546Sopenharmony_ci         is_float = true;
2477bf215546Sopenharmony_ci         break;
2478bf215546Sopenharmony_ci      case nir_type_int:
2479bf215546Sopenharmony_ci         vec_type = bld_base->int16_bld.vec_type;
2480bf215546Sopenharmony_ci         break;
2481bf215546Sopenharmony_ci      case nir_type_uint:
2482bf215546Sopenharmony_ci         vec_type = bld_base->uint16_bld.vec_type;
2483bf215546Sopenharmony_ci         break;
2484bf215546Sopenharmony_ci      default:
2485bf215546Sopenharmony_ci         unreachable("unexpected alu type");
2486bf215546Sopenharmony_ci      }
2487bf215546Sopenharmony_ci      for (int i = 0; i < nir_dest_num_components(instr->dest); ++i) {
2488bf215546Sopenharmony_ci         if (is_float) {
2489bf215546Sopenharmony_ci            texel[i] = lp_build_float_to_half(gallivm, texel[i]);
2490bf215546Sopenharmony_ci         } else {
2491bf215546Sopenharmony_ci            texel[i] = LLVMBuildBitCast(builder, texel[i], bld_base->int_bld.vec_type, "");
2492bf215546Sopenharmony_ci            texel[i] = LLVMBuildTrunc(builder, texel[i], vec_type, "");
2493bf215546Sopenharmony_ci         }
2494bf215546Sopenharmony_ci      }
2495bf215546Sopenharmony_ci   }
2496bf215546Sopenharmony_ci
2497bf215546Sopenharmony_ci   assign_dest(bld_base, &instr->dest, texel);
2498bf215546Sopenharmony_ci}
2499bf215546Sopenharmony_ci
2500bf215546Sopenharmony_ci
2501bf215546Sopenharmony_cistatic void
2502bf215546Sopenharmony_civisit_ssa_undef(struct lp_build_nir_context *bld_base,
2503bf215546Sopenharmony_ci                const nir_ssa_undef_instr *instr)
2504bf215546Sopenharmony_ci{
2505bf215546Sopenharmony_ci   unsigned num_components = instr->def.num_components;
2506bf215546Sopenharmony_ci   LLVMValueRef undef[NIR_MAX_VEC_COMPONENTS];
2507bf215546Sopenharmony_ci   struct lp_build_context *undef_bld = get_int_bld(bld_base, true,
2508bf215546Sopenharmony_ci                                                    instr->def.bit_size);
2509bf215546Sopenharmony_ci   for (unsigned i = 0; i < num_components; i++)
2510bf215546Sopenharmony_ci      undef[i] = LLVMGetUndef(undef_bld->vec_type);
2511bf215546Sopenharmony_ci   memset(&undef[num_components], 0, NIR_MAX_VEC_COMPONENTS - num_components);
2512bf215546Sopenharmony_ci   assign_ssa_dest(bld_base, &instr->def, undef);
2513bf215546Sopenharmony_ci}
2514bf215546Sopenharmony_ci
2515bf215546Sopenharmony_ci
2516bf215546Sopenharmony_cistatic void
2517bf215546Sopenharmony_civisit_jump(struct lp_build_nir_context *bld_base,
2518bf215546Sopenharmony_ci           const nir_jump_instr *instr)
2519bf215546Sopenharmony_ci{
2520bf215546Sopenharmony_ci   switch (instr->type) {
2521bf215546Sopenharmony_ci   case nir_jump_break:
2522bf215546Sopenharmony_ci      bld_base->break_stmt(bld_base);
2523bf215546Sopenharmony_ci      break;
2524bf215546Sopenharmony_ci   case nir_jump_continue:
2525bf215546Sopenharmony_ci      bld_base->continue_stmt(bld_base);
2526bf215546Sopenharmony_ci      break;
2527bf215546Sopenharmony_ci   default:
2528bf215546Sopenharmony_ci      unreachable("Unknown jump instr\n");
2529bf215546Sopenharmony_ci   }
2530bf215546Sopenharmony_ci}
2531bf215546Sopenharmony_ci
2532bf215546Sopenharmony_ci
2533bf215546Sopenharmony_cistatic void
2534bf215546Sopenharmony_civisit_deref(struct lp_build_nir_context *bld_base,
2535bf215546Sopenharmony_ci            nir_deref_instr *instr)
2536bf215546Sopenharmony_ci{
2537bf215546Sopenharmony_ci   if (!nir_deref_mode_is_one_of(instr, nir_var_mem_shared |
2538bf215546Sopenharmony_ci                                        nir_var_mem_global)) {
2539bf215546Sopenharmony_ci      return;
2540bf215546Sopenharmony_ci   }
2541bf215546Sopenharmony_ci
2542bf215546Sopenharmony_ci   LLVMValueRef result = NULL;
2543bf215546Sopenharmony_ci   switch(instr->deref_type) {
2544bf215546Sopenharmony_ci   case nir_deref_type_var: {
2545bf215546Sopenharmony_ci      struct hash_entry *entry =
2546bf215546Sopenharmony_ci         _mesa_hash_table_search(bld_base->vars, instr->var);
2547bf215546Sopenharmony_ci      result = entry->data;
2548bf215546Sopenharmony_ci      break;
2549bf215546Sopenharmony_ci   }
2550bf215546Sopenharmony_ci   default:
2551bf215546Sopenharmony_ci      unreachable("Unhandled deref_instr deref type");
2552bf215546Sopenharmony_ci   }
2553bf215546Sopenharmony_ci
2554bf215546Sopenharmony_ci   assign_ssa(bld_base, instr->dest.ssa.index, result);
2555bf215546Sopenharmony_ci}
2556bf215546Sopenharmony_ci
2557bf215546Sopenharmony_ci
2558bf215546Sopenharmony_cistatic void
2559bf215546Sopenharmony_civisit_block(struct lp_build_nir_context *bld_base, nir_block *block)
2560bf215546Sopenharmony_ci{
2561bf215546Sopenharmony_ci   nir_foreach_instr(instr, block)
2562bf215546Sopenharmony_ci   {
2563bf215546Sopenharmony_ci      switch (instr->type) {
2564bf215546Sopenharmony_ci      case nir_instr_type_alu:
2565bf215546Sopenharmony_ci         visit_alu(bld_base, nir_instr_as_alu(instr));
2566bf215546Sopenharmony_ci         break;
2567bf215546Sopenharmony_ci      case nir_instr_type_load_const:
2568bf215546Sopenharmony_ci         visit_load_const(bld_base, nir_instr_as_load_const(instr));
2569bf215546Sopenharmony_ci         break;
2570bf215546Sopenharmony_ci      case nir_instr_type_intrinsic:
2571bf215546Sopenharmony_ci         visit_intrinsic(bld_base, nir_instr_as_intrinsic(instr));
2572bf215546Sopenharmony_ci         break;
2573bf215546Sopenharmony_ci      case nir_instr_type_tex:
2574bf215546Sopenharmony_ci         visit_tex(bld_base, nir_instr_as_tex(instr));
2575bf215546Sopenharmony_ci         break;
2576bf215546Sopenharmony_ci      case nir_instr_type_phi:
2577bf215546Sopenharmony_ci         assert(0);
2578bf215546Sopenharmony_ci         break;
2579bf215546Sopenharmony_ci      case nir_instr_type_ssa_undef:
2580bf215546Sopenharmony_ci         visit_ssa_undef(bld_base, nir_instr_as_ssa_undef(instr));
2581bf215546Sopenharmony_ci         break;
2582bf215546Sopenharmony_ci      case nir_instr_type_jump:
2583bf215546Sopenharmony_ci         visit_jump(bld_base, nir_instr_as_jump(instr));
2584bf215546Sopenharmony_ci         break;
2585bf215546Sopenharmony_ci      case nir_instr_type_deref:
2586bf215546Sopenharmony_ci         visit_deref(bld_base, nir_instr_as_deref(instr));
2587bf215546Sopenharmony_ci         break;
2588bf215546Sopenharmony_ci      default:
2589bf215546Sopenharmony_ci         fprintf(stderr, "Unknown NIR instr type: ");
2590bf215546Sopenharmony_ci         nir_print_instr(instr, stderr);
2591bf215546Sopenharmony_ci         fprintf(stderr, "\n");
2592bf215546Sopenharmony_ci         abort();
2593bf215546Sopenharmony_ci      }
2594bf215546Sopenharmony_ci   }
2595bf215546Sopenharmony_ci}
2596bf215546Sopenharmony_ci
2597bf215546Sopenharmony_ci
2598bf215546Sopenharmony_cistatic void
2599bf215546Sopenharmony_civisit_if(struct lp_build_nir_context *bld_base, nir_if *if_stmt)
2600bf215546Sopenharmony_ci{
2601bf215546Sopenharmony_ci   LLVMValueRef cond = get_src(bld_base, if_stmt->condition);
2602bf215546Sopenharmony_ci
2603bf215546Sopenharmony_ci   bld_base->if_cond(bld_base, cond);
2604bf215546Sopenharmony_ci   visit_cf_list(bld_base, &if_stmt->then_list);
2605bf215546Sopenharmony_ci
2606bf215546Sopenharmony_ci   if (!exec_list_is_empty(&if_stmt->else_list)) {
2607bf215546Sopenharmony_ci      bld_base->else_stmt(bld_base);
2608bf215546Sopenharmony_ci      visit_cf_list(bld_base, &if_stmt->else_list);
2609bf215546Sopenharmony_ci   }
2610bf215546Sopenharmony_ci   bld_base->endif_stmt(bld_base);
2611bf215546Sopenharmony_ci}
2612bf215546Sopenharmony_ci
2613bf215546Sopenharmony_ci
2614bf215546Sopenharmony_cistatic void
2615bf215546Sopenharmony_civisit_loop(struct lp_build_nir_context *bld_base, nir_loop *loop)
2616bf215546Sopenharmony_ci{
2617bf215546Sopenharmony_ci   bld_base->bgnloop(bld_base);
2618bf215546Sopenharmony_ci   visit_cf_list(bld_base, &loop->body);
2619bf215546Sopenharmony_ci   bld_base->endloop(bld_base);
2620bf215546Sopenharmony_ci}
2621bf215546Sopenharmony_ci
2622bf215546Sopenharmony_ci
2623bf215546Sopenharmony_cistatic void
2624bf215546Sopenharmony_civisit_cf_list(struct lp_build_nir_context *bld_base,
2625bf215546Sopenharmony_ci              struct exec_list *list)
2626bf215546Sopenharmony_ci{
2627bf215546Sopenharmony_ci   foreach_list_typed(nir_cf_node, node, node, list)
2628bf215546Sopenharmony_ci   {
2629bf215546Sopenharmony_ci      switch (node->type) {
2630bf215546Sopenharmony_ci      case nir_cf_node_block:
2631bf215546Sopenharmony_ci         visit_block(bld_base, nir_cf_node_as_block(node));
2632bf215546Sopenharmony_ci         break;
2633bf215546Sopenharmony_ci      case nir_cf_node_if:
2634bf215546Sopenharmony_ci         visit_if(bld_base, nir_cf_node_as_if(node));
2635bf215546Sopenharmony_ci         break;
2636bf215546Sopenharmony_ci      case nir_cf_node_loop:
2637bf215546Sopenharmony_ci         visit_loop(bld_base, nir_cf_node_as_loop(node));
2638bf215546Sopenharmony_ci         break;
2639bf215546Sopenharmony_ci      default:
2640bf215546Sopenharmony_ci         assert(0);
2641bf215546Sopenharmony_ci      }
2642bf215546Sopenharmony_ci   }
2643bf215546Sopenharmony_ci}
2644bf215546Sopenharmony_ci
2645bf215546Sopenharmony_ci
2646bf215546Sopenharmony_cistatic void
2647bf215546Sopenharmony_cihandle_shader_output_decl(struct lp_build_nir_context *bld_base,
2648bf215546Sopenharmony_ci                          struct nir_shader *nir,
2649bf215546Sopenharmony_ci                          struct nir_variable *variable)
2650bf215546Sopenharmony_ci{
2651bf215546Sopenharmony_ci   bld_base->emit_var_decl(bld_base, variable);
2652bf215546Sopenharmony_ci}
2653bf215546Sopenharmony_ci
2654bf215546Sopenharmony_ci
2655bf215546Sopenharmony_ci/* vector registers are stored as arrays in LLVM side,
2656bf215546Sopenharmony_ci   so we can use GEP on them, as to do exec mask stores
2657bf215546Sopenharmony_ci   we need to operate on a single components.
2658bf215546Sopenharmony_ci   arrays are:
2659bf215546Sopenharmony_ci   0.x, 1.x, 2.x, 3.x
2660bf215546Sopenharmony_ci   0.y, 1.y, 2.y, 3.y
2661bf215546Sopenharmony_ci   ....
2662bf215546Sopenharmony_ci*/
2663bf215546Sopenharmony_cistatic LLVMTypeRef
2664bf215546Sopenharmony_ciget_register_type(struct lp_build_nir_context *bld_base,
2665bf215546Sopenharmony_ci                  nir_register *reg)
2666bf215546Sopenharmony_ci{
2667bf215546Sopenharmony_ci   if (is_aos(bld_base))
2668bf215546Sopenharmony_ci      return bld_base->base.int_vec_type;
2669bf215546Sopenharmony_ci
2670bf215546Sopenharmony_ci   struct lp_build_context *int_bld =
2671bf215546Sopenharmony_ci      get_int_bld(bld_base, true, reg->bit_size == 1 ? 32 : reg->bit_size);
2672bf215546Sopenharmony_ci
2673bf215546Sopenharmony_ci   LLVMTypeRef type = int_bld->vec_type;
2674bf215546Sopenharmony_ci   if (reg->num_array_elems)
2675bf215546Sopenharmony_ci      type = LLVMArrayType(type, reg->num_array_elems);
2676bf215546Sopenharmony_ci   if (reg->num_components > 1)
2677bf215546Sopenharmony_ci      type = LLVMArrayType(type, reg->num_components);
2678bf215546Sopenharmony_ci
2679bf215546Sopenharmony_ci   return type;
2680bf215546Sopenharmony_ci}
2681bf215546Sopenharmony_ci
2682bf215546Sopenharmony_ci
2683bf215546Sopenharmony_cibool lp_build_nir_llvm(struct lp_build_nir_context *bld_base,
2684bf215546Sopenharmony_ci                       struct nir_shader *nir)
2685bf215546Sopenharmony_ci{
2686bf215546Sopenharmony_ci   struct nir_function *func;
2687bf215546Sopenharmony_ci
2688bf215546Sopenharmony_ci   nir_convert_from_ssa(nir, true);
2689bf215546Sopenharmony_ci   nir_lower_locals_to_regs(nir);
2690bf215546Sopenharmony_ci   nir_remove_dead_derefs(nir);
2691bf215546Sopenharmony_ci   nir_remove_dead_variables(nir, nir_var_function_temp, NULL);
2692bf215546Sopenharmony_ci
2693bf215546Sopenharmony_ci   if (is_aos(bld_base)) {
2694bf215546Sopenharmony_ci      nir_move_vec_src_uses_to_dest(nir);
2695bf215546Sopenharmony_ci      nir_lower_vec_to_movs(nir, NULL, NULL);
2696bf215546Sopenharmony_ci   }
2697bf215546Sopenharmony_ci
2698bf215546Sopenharmony_ci   nir_foreach_shader_out_variable(variable, nir)
2699bf215546Sopenharmony_ci      handle_shader_output_decl(bld_base, nir, variable);
2700bf215546Sopenharmony_ci
2701bf215546Sopenharmony_ci   if (nir->info.io_lowered) {
2702bf215546Sopenharmony_ci      uint64_t outputs_written = nir->info.outputs_written;
2703bf215546Sopenharmony_ci
2704bf215546Sopenharmony_ci      while (outputs_written) {
2705bf215546Sopenharmony_ci         unsigned location = u_bit_scan64(&outputs_written);
2706bf215546Sopenharmony_ci         nir_variable var = {0};
2707bf215546Sopenharmony_ci
2708bf215546Sopenharmony_ci         var.type = glsl_vec4_type();
2709bf215546Sopenharmony_ci         var.data.mode = nir_var_shader_out;
2710bf215546Sopenharmony_ci         var.data.location = location;
2711bf215546Sopenharmony_ci         var.data.driver_location = util_bitcount64(nir->info.outputs_written &
2712bf215546Sopenharmony_ci                                                    BITFIELD64_MASK(location));
2713bf215546Sopenharmony_ci         bld_base->emit_var_decl(bld_base, &var);
2714bf215546Sopenharmony_ci      }
2715bf215546Sopenharmony_ci   }
2716bf215546Sopenharmony_ci
2717bf215546Sopenharmony_ci   bld_base->regs = _mesa_hash_table_create(NULL, _mesa_hash_pointer,
2718bf215546Sopenharmony_ci                                            _mesa_key_pointer_equal);
2719bf215546Sopenharmony_ci   bld_base->vars = _mesa_hash_table_create(NULL, _mesa_hash_pointer,
2720bf215546Sopenharmony_ci                                            _mesa_key_pointer_equal);
2721bf215546Sopenharmony_ci   bld_base->range_ht = _mesa_pointer_hash_table_create(NULL);
2722bf215546Sopenharmony_ci
2723bf215546Sopenharmony_ci   func = (struct nir_function *)exec_list_get_head(&nir->functions);
2724bf215546Sopenharmony_ci
2725bf215546Sopenharmony_ci   nir_foreach_register(reg, &func->impl->registers) {
2726bf215546Sopenharmony_ci      LLVMTypeRef type = get_register_type(bld_base, reg);
2727bf215546Sopenharmony_ci      LLVMValueRef reg_alloc = lp_build_alloca(bld_base->base.gallivm,
2728bf215546Sopenharmony_ci                                               type, "reg");
2729bf215546Sopenharmony_ci      _mesa_hash_table_insert(bld_base->regs, reg, reg_alloc);
2730bf215546Sopenharmony_ci   }
2731bf215546Sopenharmony_ci   nir_index_ssa_defs(func->impl);
2732bf215546Sopenharmony_ci   bld_base->ssa_defs = calloc(func->impl->ssa_alloc, sizeof(LLVMValueRef));
2733bf215546Sopenharmony_ci   visit_cf_list(bld_base, &func->impl->body);
2734bf215546Sopenharmony_ci
2735bf215546Sopenharmony_ci   free(bld_base->ssa_defs);
2736bf215546Sopenharmony_ci   ralloc_free(bld_base->vars);
2737bf215546Sopenharmony_ci   ralloc_free(bld_base->regs);
2738bf215546Sopenharmony_ci   ralloc_free(bld_base->range_ht);
2739bf215546Sopenharmony_ci   return true;
2740bf215546Sopenharmony_ci}
2741bf215546Sopenharmony_ci
2742bf215546Sopenharmony_ci
2743bf215546Sopenharmony_ci/* do some basic opts to remove some things we don't want to see. */
2744bf215546Sopenharmony_civoid
2745bf215546Sopenharmony_cilp_build_opt_nir(struct nir_shader *nir)
2746bf215546Sopenharmony_ci{
2747bf215546Sopenharmony_ci   bool progress;
2748bf215546Sopenharmony_ci
2749bf215546Sopenharmony_ci   static const struct nir_lower_tex_options lower_tex_options = {
2750bf215546Sopenharmony_ci      .lower_tg4_offsets = true,
2751bf215546Sopenharmony_ci      .lower_txp = ~0u,
2752bf215546Sopenharmony_ci      .lower_invalid_implicit_lod = true,
2753bf215546Sopenharmony_ci   };
2754bf215546Sopenharmony_ci   NIR_PASS_V(nir, nir_lower_tex, &lower_tex_options);
2755bf215546Sopenharmony_ci   NIR_PASS_V(nir, nir_lower_frexp);
2756bf215546Sopenharmony_ci
2757bf215546Sopenharmony_ci   NIR_PASS_V(nir, nir_lower_flrp, 16|32|64, true);
2758bf215546Sopenharmony_ci   NIR_PASS_V(nir, nir_lower_fp16_casts);
2759bf215546Sopenharmony_ci   do {
2760bf215546Sopenharmony_ci      progress = false;
2761bf215546Sopenharmony_ci      NIR_PASS(progress, nir, nir_opt_constant_folding);
2762bf215546Sopenharmony_ci      NIR_PASS(progress, nir, nir_opt_algebraic);
2763bf215546Sopenharmony_ci      NIR_PASS(progress, nir, nir_lower_pack);
2764bf215546Sopenharmony_ci
2765bf215546Sopenharmony_ci      nir_lower_tex_options options = { .lower_invalid_implicit_lod = true, };
2766bf215546Sopenharmony_ci      NIR_PASS_V(nir, nir_lower_tex, &options);
2767bf215546Sopenharmony_ci
2768bf215546Sopenharmony_ci      const nir_lower_subgroups_options subgroups_options = {
2769bf215546Sopenharmony_ci         .subgroup_size = lp_native_vector_width / 32,
2770bf215546Sopenharmony_ci         .ballot_bit_size = 32,
2771bf215546Sopenharmony_ci         .ballot_components = 1,
2772bf215546Sopenharmony_ci         .lower_to_scalar = true,
2773bf215546Sopenharmony_ci         .lower_subgroup_masks = true,
2774bf215546Sopenharmony_ci         .lower_relative_shuffle = true,
2775bf215546Sopenharmony_ci      };
2776bf215546Sopenharmony_ci      NIR_PASS(progress, nir, nir_lower_subgroups, &subgroups_options);
2777bf215546Sopenharmony_ci   } while (progress);
2778bf215546Sopenharmony_ci
2779bf215546Sopenharmony_ci   do {
2780bf215546Sopenharmony_ci      progress = false;
2781bf215546Sopenharmony_ci      NIR_PASS(progress, nir, nir_opt_algebraic_late);
2782bf215546Sopenharmony_ci      if (progress) {
2783bf215546Sopenharmony_ci         NIR_PASS_V(nir, nir_copy_prop);
2784bf215546Sopenharmony_ci         NIR_PASS_V(nir, nir_opt_dce);
2785bf215546Sopenharmony_ci         NIR_PASS_V(nir, nir_opt_cse);
2786bf215546Sopenharmony_ci      }
2787bf215546Sopenharmony_ci   } while (progress);
2788bf215546Sopenharmony_ci
2789bf215546Sopenharmony_ci   if (nir_lower_bool_to_int32(nir)) {
2790bf215546Sopenharmony_ci      NIR_PASS_V(nir, nir_copy_prop);
2791bf215546Sopenharmony_ci      NIR_PASS_V(nir, nir_opt_dce);
2792bf215546Sopenharmony_ci   }
2793bf215546Sopenharmony_ci}
2794