1bf215546Sopenharmony_ci/*
2bf215546Sopenharmony_ci * Copyright © 2016 Bas Nieuwenhuizen
3bf215546Sopenharmony_ci *
4bf215546Sopenharmony_ci * Permission is hereby granted, free of charge, to any person obtaining a
5bf215546Sopenharmony_ci * copy of this software and associated documentation files (the "Software"),
6bf215546Sopenharmony_ci * to deal in the Software without restriction, including without limitation
7bf215546Sopenharmony_ci * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8bf215546Sopenharmony_ci * and/or sell copies of the Software, and to permit persons to whom the
9bf215546Sopenharmony_ci * Software is furnished to do so, subject to the following conditions:
10bf215546Sopenharmony_ci *
11bf215546Sopenharmony_ci * The above copyright notice and this permission notice (including the next
12bf215546Sopenharmony_ci * paragraph) shall be included in all copies or substantial portions of the
13bf215546Sopenharmony_ci * Software.
14bf215546Sopenharmony_ci *
15bf215546Sopenharmony_ci * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16bf215546Sopenharmony_ci * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17bf215546Sopenharmony_ci * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
18bf215546Sopenharmony_ci * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19bf215546Sopenharmony_ci * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20bf215546Sopenharmony_ci * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21bf215546Sopenharmony_ci * IN THE SOFTWARE.
22bf215546Sopenharmony_ci */
23bf215546Sopenharmony_ci
24bf215546Sopenharmony_ci#include "ac_nir_to_llvm.h"
25bf215546Sopenharmony_ci#include "ac_gpu_info.h"
26bf215546Sopenharmony_ci#include "ac_binary.h"
27bf215546Sopenharmony_ci#include "ac_llvm_build.h"
28bf215546Sopenharmony_ci#include "ac_llvm_util.h"
29bf215546Sopenharmony_ci#include "ac_shader_abi.h"
30bf215546Sopenharmony_ci#include "ac_shader_util.h"
31bf215546Sopenharmony_ci#include "nir/nir.h"
32bf215546Sopenharmony_ci#include "nir/nir_deref.h"
33bf215546Sopenharmony_ci#include "sid.h"
34bf215546Sopenharmony_ci#include "util/bitscan.h"
35bf215546Sopenharmony_ci#include "util/u_math.h"
36bf215546Sopenharmony_ci#include <llvm/Config/llvm-config.h>
37bf215546Sopenharmony_ci
38bf215546Sopenharmony_cistruct ac_nir_context {
39bf215546Sopenharmony_ci   struct ac_llvm_context ac;
40bf215546Sopenharmony_ci   struct ac_shader_abi *abi;
41bf215546Sopenharmony_ci   const struct ac_shader_args *args;
42bf215546Sopenharmony_ci
43bf215546Sopenharmony_ci   gl_shader_stage stage;
44bf215546Sopenharmony_ci   shader_info *info;
45bf215546Sopenharmony_ci
46bf215546Sopenharmony_ci   LLVMValueRef *ssa_defs;
47bf215546Sopenharmony_ci
48bf215546Sopenharmony_ci   LLVMValueRef scratch;
49bf215546Sopenharmony_ci   LLVMValueRef constant_data;
50bf215546Sopenharmony_ci
51bf215546Sopenharmony_ci   struct hash_table *defs;
52bf215546Sopenharmony_ci   struct hash_table *phis;
53bf215546Sopenharmony_ci   struct hash_table *vars;
54bf215546Sopenharmony_ci   struct hash_table *verified_interp;
55bf215546Sopenharmony_ci
56bf215546Sopenharmony_ci   LLVMValueRef main_function;
57bf215546Sopenharmony_ci   LLVMBasicBlockRef continue_block;
58bf215546Sopenharmony_ci   LLVMBasicBlockRef break_block;
59bf215546Sopenharmony_ci
60bf215546Sopenharmony_ci   LLVMValueRef vertex_id_replaced;
61bf215546Sopenharmony_ci   LLVMValueRef instance_id_replaced;
62bf215546Sopenharmony_ci   LLVMValueRef tes_u_replaced;
63bf215546Sopenharmony_ci   LLVMValueRef tes_v_replaced;
64bf215546Sopenharmony_ci   LLVMValueRef tes_rel_patch_id_replaced;
65bf215546Sopenharmony_ci   LLVMValueRef tes_patch_id_replaced;
66bf215546Sopenharmony_ci};
67bf215546Sopenharmony_ci
68bf215546Sopenharmony_cistatic LLVMValueRef get_sampler_desc_index(struct ac_nir_context *ctx, nir_deref_instr *deref_instr,
69bf215546Sopenharmony_ci                                           const nir_instr *instr, bool image);
70bf215546Sopenharmony_ci
71bf215546Sopenharmony_cistatic LLVMValueRef get_sampler_desc(struct ac_nir_context *ctx, nir_deref_instr *deref_instr,
72bf215546Sopenharmony_ci                                     enum ac_descriptor_type desc_type, const nir_instr *instr,
73bf215546Sopenharmony_ci                                     LLVMValueRef index, bool image, bool write);
74bf215546Sopenharmony_ci
75bf215546Sopenharmony_cistatic LLVMTypeRef get_def_type(struct ac_nir_context *ctx, const nir_ssa_def *def)
76bf215546Sopenharmony_ci{
77bf215546Sopenharmony_ci   LLVMTypeRef type = LLVMIntTypeInContext(ctx->ac.context, def->bit_size);
78bf215546Sopenharmony_ci   if (def->num_components > 1) {
79bf215546Sopenharmony_ci      type = LLVMVectorType(type, def->num_components);
80bf215546Sopenharmony_ci   }
81bf215546Sopenharmony_ci   return type;
82bf215546Sopenharmony_ci}
83bf215546Sopenharmony_ci
84bf215546Sopenharmony_cistatic LLVMValueRef get_src(struct ac_nir_context *nir, nir_src src)
85bf215546Sopenharmony_ci{
86bf215546Sopenharmony_ci   assert(src.is_ssa);
87bf215546Sopenharmony_ci   return nir->ssa_defs[src.ssa->index];
88bf215546Sopenharmony_ci}
89bf215546Sopenharmony_ci
90bf215546Sopenharmony_cistatic LLVMValueRef get_memory_ptr_t(struct ac_nir_context *ctx, nir_src src, LLVMTypeRef elem_type, unsigned c_off)
91bf215546Sopenharmony_ci{
92bf215546Sopenharmony_ci   LLVMValueRef ptr = get_src(ctx, src);
93bf215546Sopenharmony_ci   LLVMValueRef lds_i8 = ctx->ac.lds;
94bf215546Sopenharmony_ci   if (ctx->stage != MESA_SHADER_COMPUTE)
95bf215546Sopenharmony_ci      lds_i8 = LLVMBuildBitCast(ctx->ac.builder, ctx->ac.lds, LLVMPointerType(ctx->ac.i8, AC_ADDR_SPACE_LDS), "");
96bf215546Sopenharmony_ci
97bf215546Sopenharmony_ci   ptr = LLVMBuildAdd(ctx->ac.builder, ptr, LLVMConstInt(ctx->ac.i32, c_off, 0), "");
98bf215546Sopenharmony_ci   ptr = LLVMBuildGEP2(ctx->ac.builder, ctx->ac.i8, lds_i8, &ptr, 1, "");
99bf215546Sopenharmony_ci   int addr_space = LLVMGetPointerAddressSpace(LLVMTypeOf(ptr));
100bf215546Sopenharmony_ci
101bf215546Sopenharmony_ci   return LLVMBuildBitCast(ctx->ac.builder, ptr, LLVMPointerType(elem_type, addr_space), "");
102bf215546Sopenharmony_ci}
103bf215546Sopenharmony_ci
104bf215546Sopenharmony_cistatic LLVMValueRef get_memory_ptr(struct ac_nir_context *ctx, nir_src src, unsigned bit_size, unsigned c_off)
105bf215546Sopenharmony_ci{
106bf215546Sopenharmony_ci   return get_memory_ptr_t(ctx, src, LLVMIntTypeInContext(ctx->ac.context, bit_size), c_off);
107bf215546Sopenharmony_ci}
108bf215546Sopenharmony_ci
109bf215546Sopenharmony_cistatic LLVMBasicBlockRef get_block(struct ac_nir_context *nir, const struct nir_block *b)
110bf215546Sopenharmony_ci{
111bf215546Sopenharmony_ci   struct hash_entry *entry = _mesa_hash_table_search(nir->defs, b);
112bf215546Sopenharmony_ci   return (LLVMBasicBlockRef)entry->data;
113bf215546Sopenharmony_ci}
114bf215546Sopenharmony_ci
115bf215546Sopenharmony_cistatic LLVMValueRef get_alu_src(struct ac_nir_context *ctx, nir_alu_src src,
116bf215546Sopenharmony_ci                                unsigned num_components)
117bf215546Sopenharmony_ci{
118bf215546Sopenharmony_ci   LLVMValueRef value = get_src(ctx, src.src);
119bf215546Sopenharmony_ci   bool need_swizzle = false;
120bf215546Sopenharmony_ci
121bf215546Sopenharmony_ci   assert(value);
122bf215546Sopenharmony_ci   unsigned src_components = ac_get_llvm_num_components(value);
123bf215546Sopenharmony_ci   for (unsigned i = 0; i < num_components; ++i) {
124bf215546Sopenharmony_ci      assert(src.swizzle[i] < src_components);
125bf215546Sopenharmony_ci      if (src.swizzle[i] != i)
126bf215546Sopenharmony_ci         need_swizzle = true;
127bf215546Sopenharmony_ci   }
128bf215546Sopenharmony_ci
129bf215546Sopenharmony_ci   if (need_swizzle || num_components != src_components) {
130bf215546Sopenharmony_ci      LLVMValueRef masks[] = {LLVMConstInt(ctx->ac.i32, src.swizzle[0], false),
131bf215546Sopenharmony_ci                              LLVMConstInt(ctx->ac.i32, src.swizzle[1], false),
132bf215546Sopenharmony_ci                              LLVMConstInt(ctx->ac.i32, src.swizzle[2], false),
133bf215546Sopenharmony_ci                              LLVMConstInt(ctx->ac.i32, src.swizzle[3], false)};
134bf215546Sopenharmony_ci
135bf215546Sopenharmony_ci      if (src_components > 1 && num_components == 1) {
136bf215546Sopenharmony_ci         value = LLVMBuildExtractElement(ctx->ac.builder, value, masks[0], "");
137bf215546Sopenharmony_ci      } else if (src_components == 1 && num_components > 1) {
138bf215546Sopenharmony_ci         LLVMValueRef values[] = {value, value, value, value};
139bf215546Sopenharmony_ci         value = ac_build_gather_values(&ctx->ac, values, num_components);
140bf215546Sopenharmony_ci      } else {
141bf215546Sopenharmony_ci         LLVMValueRef swizzle = LLVMConstVector(masks, num_components);
142bf215546Sopenharmony_ci         value = LLVMBuildShuffleVector(ctx->ac.builder, value, value, swizzle, "");
143bf215546Sopenharmony_ci      }
144bf215546Sopenharmony_ci   }
145bf215546Sopenharmony_ci   assert(!src.negate);
146bf215546Sopenharmony_ci   assert(!src.abs);
147bf215546Sopenharmony_ci   return value;
148bf215546Sopenharmony_ci}
149bf215546Sopenharmony_ci
150bf215546Sopenharmony_cistatic LLVMValueRef emit_int_cmp(struct ac_llvm_context *ctx, LLVMIntPredicate pred,
151bf215546Sopenharmony_ci                                 LLVMValueRef src0, LLVMValueRef src1)
152bf215546Sopenharmony_ci{
153bf215546Sopenharmony_ci   src0 = ac_to_integer(ctx, src0);
154bf215546Sopenharmony_ci   src1 = ac_to_integer(ctx, src1);
155bf215546Sopenharmony_ci   return LLVMBuildICmp(ctx->builder, pred, src0, src1, "");
156bf215546Sopenharmony_ci}
157bf215546Sopenharmony_ci
158bf215546Sopenharmony_cistatic LLVMValueRef emit_float_cmp(struct ac_llvm_context *ctx, LLVMRealPredicate pred,
159bf215546Sopenharmony_ci                                   LLVMValueRef src0, LLVMValueRef src1)
160bf215546Sopenharmony_ci{
161bf215546Sopenharmony_ci   src0 = ac_to_float(ctx, src0);
162bf215546Sopenharmony_ci   src1 = ac_to_float(ctx, src1);
163bf215546Sopenharmony_ci   return LLVMBuildFCmp(ctx->builder, pred, src0, src1, "");
164bf215546Sopenharmony_ci}
165bf215546Sopenharmony_ci
166bf215546Sopenharmony_cistatic LLVMValueRef emit_intrin_1f_param(struct ac_llvm_context *ctx, const char *intrin,
167bf215546Sopenharmony_ci                                         LLVMTypeRef result_type, LLVMValueRef src0)
168bf215546Sopenharmony_ci{
169bf215546Sopenharmony_ci   char name[64], type[64];
170bf215546Sopenharmony_ci   LLVMValueRef params[] = {
171bf215546Sopenharmony_ci      ac_to_float(ctx, src0),
172bf215546Sopenharmony_ci   };
173bf215546Sopenharmony_ci
174bf215546Sopenharmony_ci   ac_build_type_name_for_intr(LLVMTypeOf(params[0]), type, sizeof(type));
175bf215546Sopenharmony_ci   ASSERTED const int length = snprintf(name, sizeof(name), "%s.%s", intrin, type);
176bf215546Sopenharmony_ci   assert(length < sizeof(name));
177bf215546Sopenharmony_ci   return ac_build_intrinsic(ctx, name, result_type, params, 1, AC_FUNC_ATTR_READNONE);
178bf215546Sopenharmony_ci}
179bf215546Sopenharmony_ci
180bf215546Sopenharmony_cistatic LLVMValueRef emit_intrin_1f_param_scalar(struct ac_llvm_context *ctx, const char *intrin,
181bf215546Sopenharmony_ci                                                LLVMTypeRef result_type, LLVMValueRef src0)
182bf215546Sopenharmony_ci{
183bf215546Sopenharmony_ci   if (LLVMGetTypeKind(result_type) != LLVMVectorTypeKind)
184bf215546Sopenharmony_ci      return emit_intrin_1f_param(ctx, intrin, result_type, src0);
185bf215546Sopenharmony_ci
186bf215546Sopenharmony_ci   LLVMTypeRef elem_type = LLVMGetElementType(result_type);
187bf215546Sopenharmony_ci   LLVMValueRef ret = LLVMGetUndef(result_type);
188bf215546Sopenharmony_ci
189bf215546Sopenharmony_ci   /* Scalarize the intrinsic, because vectors are not supported. */
190bf215546Sopenharmony_ci   for (unsigned i = 0; i < LLVMGetVectorSize(result_type); i++) {
191bf215546Sopenharmony_ci      char name[64], type[64];
192bf215546Sopenharmony_ci      LLVMValueRef params[] = {
193bf215546Sopenharmony_ci         ac_to_float(ctx, ac_llvm_extract_elem(ctx, src0, i)),
194bf215546Sopenharmony_ci      };
195bf215546Sopenharmony_ci
196bf215546Sopenharmony_ci      ac_build_type_name_for_intr(LLVMTypeOf(params[0]), type, sizeof(type));
197bf215546Sopenharmony_ci      ASSERTED const int length = snprintf(name, sizeof(name), "%s.%s", intrin, type);
198bf215546Sopenharmony_ci      assert(length < sizeof(name));
199bf215546Sopenharmony_ci      ret = LLVMBuildInsertElement(
200bf215546Sopenharmony_ci         ctx->builder, ret,
201bf215546Sopenharmony_ci         ac_build_intrinsic(ctx, name, elem_type, params, 1, AC_FUNC_ATTR_READNONE),
202bf215546Sopenharmony_ci         LLVMConstInt(ctx->i32, i, 0), "");
203bf215546Sopenharmony_ci   }
204bf215546Sopenharmony_ci   return ret;
205bf215546Sopenharmony_ci}
206bf215546Sopenharmony_ci
207bf215546Sopenharmony_cistatic LLVMValueRef emit_intrin_2f_param(struct ac_llvm_context *ctx, const char *intrin,
208bf215546Sopenharmony_ci                                         LLVMTypeRef result_type, LLVMValueRef src0,
209bf215546Sopenharmony_ci                                         LLVMValueRef src1)
210bf215546Sopenharmony_ci{
211bf215546Sopenharmony_ci   char name[64], type[64];
212bf215546Sopenharmony_ci   LLVMValueRef params[] = {
213bf215546Sopenharmony_ci      ac_to_float(ctx, src0),
214bf215546Sopenharmony_ci      ac_to_float(ctx, src1),
215bf215546Sopenharmony_ci   };
216bf215546Sopenharmony_ci
217bf215546Sopenharmony_ci   ac_build_type_name_for_intr(LLVMTypeOf(params[0]), type, sizeof(type));
218bf215546Sopenharmony_ci   ASSERTED const int length = snprintf(name, sizeof(name), "%s.%s", intrin, type);
219bf215546Sopenharmony_ci   assert(length < sizeof(name));
220bf215546Sopenharmony_ci   return ac_build_intrinsic(ctx, name, result_type, params, 2, AC_FUNC_ATTR_READNONE);
221bf215546Sopenharmony_ci}
222bf215546Sopenharmony_ci
223bf215546Sopenharmony_cistatic LLVMValueRef emit_intrin_3f_param(struct ac_llvm_context *ctx, const char *intrin,
224bf215546Sopenharmony_ci                                         LLVMTypeRef result_type, LLVMValueRef src0,
225bf215546Sopenharmony_ci                                         LLVMValueRef src1, LLVMValueRef src2)
226bf215546Sopenharmony_ci{
227bf215546Sopenharmony_ci   char name[64], type[64];
228bf215546Sopenharmony_ci   LLVMValueRef params[] = {
229bf215546Sopenharmony_ci      ac_to_float(ctx, src0),
230bf215546Sopenharmony_ci      ac_to_float(ctx, src1),
231bf215546Sopenharmony_ci      ac_to_float(ctx, src2),
232bf215546Sopenharmony_ci   };
233bf215546Sopenharmony_ci
234bf215546Sopenharmony_ci   ac_build_type_name_for_intr(LLVMTypeOf(params[0]), type, sizeof(type));
235bf215546Sopenharmony_ci   ASSERTED const int length = snprintf(name, sizeof(name), "%s.%s", intrin, type);
236bf215546Sopenharmony_ci   assert(length < sizeof(name));
237bf215546Sopenharmony_ci   return ac_build_intrinsic(ctx, name, result_type, params, 3, AC_FUNC_ATTR_READNONE);
238bf215546Sopenharmony_ci}
239bf215546Sopenharmony_ci
240bf215546Sopenharmony_cistatic LLVMValueRef emit_bcsel(struct ac_llvm_context *ctx, LLVMValueRef src0, LLVMValueRef src1,
241bf215546Sopenharmony_ci                               LLVMValueRef src2)
242bf215546Sopenharmony_ci{
243bf215546Sopenharmony_ci   LLVMTypeRef src1_type = LLVMTypeOf(src1);
244bf215546Sopenharmony_ci   LLVMTypeRef src2_type = LLVMTypeOf(src2);
245bf215546Sopenharmony_ci
246bf215546Sopenharmony_ci   if (LLVMGetTypeKind(src1_type) == LLVMPointerTypeKind &&
247bf215546Sopenharmony_ci       LLVMGetTypeKind(src2_type) != LLVMPointerTypeKind) {
248bf215546Sopenharmony_ci      src2 = LLVMBuildIntToPtr(ctx->builder, src2, src1_type, "");
249bf215546Sopenharmony_ci   } else if (LLVMGetTypeKind(src2_type) == LLVMPointerTypeKind &&
250bf215546Sopenharmony_ci              LLVMGetTypeKind(src1_type) != LLVMPointerTypeKind) {
251bf215546Sopenharmony_ci      src1 = LLVMBuildIntToPtr(ctx->builder, src1, src2_type, "");
252bf215546Sopenharmony_ci   }
253bf215546Sopenharmony_ci
254bf215546Sopenharmony_ci   return LLVMBuildSelect(ctx->builder, src0, ac_to_integer_or_pointer(ctx, src1),
255bf215546Sopenharmony_ci                          ac_to_integer_or_pointer(ctx, src2), "");
256bf215546Sopenharmony_ci}
257bf215546Sopenharmony_ci
258bf215546Sopenharmony_cistatic LLVMValueRef emit_iabs(struct ac_llvm_context *ctx, LLVMValueRef src0)
259bf215546Sopenharmony_ci{
260bf215546Sopenharmony_ci   return ac_build_imax(ctx, src0, LLVMBuildNeg(ctx->builder, src0, ""));
261bf215546Sopenharmony_ci}
262bf215546Sopenharmony_ci
263bf215546Sopenharmony_cistatic LLVMValueRef emit_uint_carry(struct ac_llvm_context *ctx, const char *intrin,
264bf215546Sopenharmony_ci                                    LLVMValueRef src0, LLVMValueRef src1)
265bf215546Sopenharmony_ci{
266bf215546Sopenharmony_ci   LLVMTypeRef ret_type;
267bf215546Sopenharmony_ci   LLVMTypeRef types[] = {ctx->i32, ctx->i1};
268bf215546Sopenharmony_ci   LLVMValueRef res;
269bf215546Sopenharmony_ci   LLVMValueRef params[] = {src0, src1};
270bf215546Sopenharmony_ci   ret_type = LLVMStructTypeInContext(ctx->context, types, 2, true);
271bf215546Sopenharmony_ci
272bf215546Sopenharmony_ci   res = ac_build_intrinsic(ctx, intrin, ret_type, params, 2, AC_FUNC_ATTR_READNONE);
273bf215546Sopenharmony_ci
274bf215546Sopenharmony_ci   res = LLVMBuildExtractValue(ctx->builder, res, 1, "");
275bf215546Sopenharmony_ci   res = LLVMBuildZExt(ctx->builder, res, ctx->i32, "");
276bf215546Sopenharmony_ci   return res;
277bf215546Sopenharmony_ci}
278bf215546Sopenharmony_ci
279bf215546Sopenharmony_cistatic LLVMValueRef emit_b2f(struct ac_llvm_context *ctx, LLVMValueRef src0, unsigned bitsize)
280bf215546Sopenharmony_ci{
281bf215546Sopenharmony_ci   assert(ac_get_elem_bits(ctx, LLVMTypeOf(src0)) == 1);
282bf215546Sopenharmony_ci
283bf215546Sopenharmony_ci   switch (bitsize) {
284bf215546Sopenharmony_ci   case 16:
285bf215546Sopenharmony_ci      if (LLVMGetTypeKind(LLVMTypeOf(src0)) == LLVMVectorTypeKind) {
286bf215546Sopenharmony_ci         assert(LLVMGetVectorSize(LLVMTypeOf(src0)) == 2);
287bf215546Sopenharmony_ci         LLVMValueRef f[] = {
288bf215546Sopenharmony_ci            LLVMBuildSelect(ctx->builder, ac_llvm_extract_elem(ctx, src0, 0),
289bf215546Sopenharmony_ci                            ctx->f16_1, ctx->f16_0, ""),
290bf215546Sopenharmony_ci            LLVMBuildSelect(ctx->builder, ac_llvm_extract_elem(ctx, src0, 1),
291bf215546Sopenharmony_ci                            ctx->f16_1, ctx->f16_0, ""),
292bf215546Sopenharmony_ci         };
293bf215546Sopenharmony_ci         return ac_build_gather_values(ctx, f, 2);
294bf215546Sopenharmony_ci      }
295bf215546Sopenharmony_ci      return LLVMBuildSelect(ctx->builder, src0, ctx->f16_1, ctx->f16_0, "");
296bf215546Sopenharmony_ci   case 32:
297bf215546Sopenharmony_ci      return LLVMBuildSelect(ctx->builder, src0, ctx->f32_1, ctx->f32_0, "");
298bf215546Sopenharmony_ci   case 64:
299bf215546Sopenharmony_ci      return LLVMBuildSelect(ctx->builder, src0, ctx->f64_1, ctx->f64_0, "");
300bf215546Sopenharmony_ci   default:
301bf215546Sopenharmony_ci      unreachable("Unsupported bit size.");
302bf215546Sopenharmony_ci   }
303bf215546Sopenharmony_ci}
304bf215546Sopenharmony_ci
305bf215546Sopenharmony_cistatic LLVMValueRef emit_f2b(struct ac_llvm_context *ctx, LLVMValueRef src0)
306bf215546Sopenharmony_ci{
307bf215546Sopenharmony_ci   src0 = ac_to_float(ctx, src0);
308bf215546Sopenharmony_ci   LLVMValueRef zero = LLVMConstNull(LLVMTypeOf(src0));
309bf215546Sopenharmony_ci   return LLVMBuildFCmp(ctx->builder, LLVMRealUNE, src0, zero, "");
310bf215546Sopenharmony_ci}
311bf215546Sopenharmony_ci
312bf215546Sopenharmony_cistatic LLVMValueRef emit_b2i(struct ac_llvm_context *ctx, LLVMValueRef src0, unsigned bitsize)
313bf215546Sopenharmony_ci{
314bf215546Sopenharmony_ci   switch (bitsize) {
315bf215546Sopenharmony_ci   case 8:
316bf215546Sopenharmony_ci      return LLVMBuildSelect(ctx->builder, src0, ctx->i8_1, ctx->i8_0, "");
317bf215546Sopenharmony_ci   case 16:
318bf215546Sopenharmony_ci      return LLVMBuildSelect(ctx->builder, src0, ctx->i16_1, ctx->i16_0, "");
319bf215546Sopenharmony_ci   case 32:
320bf215546Sopenharmony_ci      return LLVMBuildSelect(ctx->builder, src0, ctx->i32_1, ctx->i32_0, "");
321bf215546Sopenharmony_ci   case 64:
322bf215546Sopenharmony_ci      return LLVMBuildSelect(ctx->builder, src0, ctx->i64_1, ctx->i64_0, "");
323bf215546Sopenharmony_ci   default:
324bf215546Sopenharmony_ci      unreachable("Unsupported bit size.");
325bf215546Sopenharmony_ci   }
326bf215546Sopenharmony_ci}
327bf215546Sopenharmony_ci
328bf215546Sopenharmony_cistatic LLVMValueRef emit_i2b(struct ac_llvm_context *ctx, LLVMValueRef src0)
329bf215546Sopenharmony_ci{
330bf215546Sopenharmony_ci   LLVMValueRef zero = LLVMConstNull(LLVMTypeOf(src0));
331bf215546Sopenharmony_ci   return LLVMBuildICmp(ctx->builder, LLVMIntNE, src0, zero, "");
332bf215546Sopenharmony_ci}
333bf215546Sopenharmony_ci
334bf215546Sopenharmony_cistatic LLVMValueRef emit_f2f16(struct ac_llvm_context *ctx, LLVMValueRef src0)
335bf215546Sopenharmony_ci{
336bf215546Sopenharmony_ci   LLVMValueRef result;
337bf215546Sopenharmony_ci   LLVMValueRef cond = NULL;
338bf215546Sopenharmony_ci
339bf215546Sopenharmony_ci   src0 = ac_to_float(ctx, src0);
340bf215546Sopenharmony_ci   result = LLVMBuildFPTrunc(ctx->builder, src0, ctx->f16, "");
341bf215546Sopenharmony_ci
342bf215546Sopenharmony_ci   if (ctx->gfx_level >= GFX8) {
343bf215546Sopenharmony_ci      LLVMValueRef args[2];
344bf215546Sopenharmony_ci      /* Check if the result is a denormal - and flush to 0 if so. */
345bf215546Sopenharmony_ci      args[0] = result;
346bf215546Sopenharmony_ci      args[1] = LLVMConstInt(ctx->i32, N_SUBNORMAL | P_SUBNORMAL, false);
347bf215546Sopenharmony_ci      cond =
348bf215546Sopenharmony_ci         ac_build_intrinsic(ctx, "llvm.amdgcn.class.f16", ctx->i1, args, 2, AC_FUNC_ATTR_READNONE);
349bf215546Sopenharmony_ci   }
350bf215546Sopenharmony_ci
351bf215546Sopenharmony_ci   /* need to convert back up to f32 */
352bf215546Sopenharmony_ci   result = LLVMBuildFPExt(ctx->builder, result, ctx->f32, "");
353bf215546Sopenharmony_ci
354bf215546Sopenharmony_ci   if (ctx->gfx_level >= GFX8)
355bf215546Sopenharmony_ci      result = LLVMBuildSelect(ctx->builder, cond, ctx->f32_0, result, "");
356bf215546Sopenharmony_ci   else {
357bf215546Sopenharmony_ci      /* for GFX6-GFX7 */
358bf215546Sopenharmony_ci      /* 0x38800000 is smallest half float value (2^-14) in 32-bit float,
359bf215546Sopenharmony_ci       * so compare the result and flush to 0 if it's smaller.
360bf215546Sopenharmony_ci       */
361bf215546Sopenharmony_ci      LLVMValueRef temp, cond2;
362bf215546Sopenharmony_ci      temp = emit_intrin_1f_param(ctx, "llvm.fabs", ctx->f32, result);
363bf215546Sopenharmony_ci      cond = LLVMBuildFCmp(
364bf215546Sopenharmony_ci         ctx->builder, LLVMRealOGT,
365bf215546Sopenharmony_ci         LLVMBuildBitCast(ctx->builder, LLVMConstInt(ctx->i32, 0x38800000, false), ctx->f32, ""),
366bf215546Sopenharmony_ci         temp, "");
367bf215546Sopenharmony_ci      cond2 = LLVMBuildFCmp(ctx->builder, LLVMRealONE, temp, ctx->f32_0, "");
368bf215546Sopenharmony_ci      cond = LLVMBuildAnd(ctx->builder, cond, cond2, "");
369bf215546Sopenharmony_ci      result = LLVMBuildSelect(ctx->builder, cond, ctx->f32_0, result, "");
370bf215546Sopenharmony_ci   }
371bf215546Sopenharmony_ci   return result;
372bf215546Sopenharmony_ci}
373bf215546Sopenharmony_ci
374bf215546Sopenharmony_cistatic LLVMValueRef emit_umul_high(struct ac_llvm_context *ctx, LLVMValueRef src0,
375bf215546Sopenharmony_ci                                   LLVMValueRef src1)
376bf215546Sopenharmony_ci{
377bf215546Sopenharmony_ci   LLVMValueRef dst64, result;
378bf215546Sopenharmony_ci   src0 = LLVMBuildZExt(ctx->builder, src0, ctx->i64, "");
379bf215546Sopenharmony_ci   src1 = LLVMBuildZExt(ctx->builder, src1, ctx->i64, "");
380bf215546Sopenharmony_ci
381bf215546Sopenharmony_ci   dst64 = LLVMBuildMul(ctx->builder, src0, src1, "");
382bf215546Sopenharmony_ci   dst64 = LLVMBuildLShr(ctx->builder, dst64, LLVMConstInt(ctx->i64, 32, false), "");
383bf215546Sopenharmony_ci   result = LLVMBuildTrunc(ctx->builder, dst64, ctx->i32, "");
384bf215546Sopenharmony_ci   return result;
385bf215546Sopenharmony_ci}
386bf215546Sopenharmony_ci
387bf215546Sopenharmony_cistatic LLVMValueRef emit_imul_high(struct ac_llvm_context *ctx, LLVMValueRef src0,
388bf215546Sopenharmony_ci                                   LLVMValueRef src1)
389bf215546Sopenharmony_ci{
390bf215546Sopenharmony_ci   LLVMValueRef dst64, result;
391bf215546Sopenharmony_ci   src0 = LLVMBuildSExt(ctx->builder, src0, ctx->i64, "");
392bf215546Sopenharmony_ci   src1 = LLVMBuildSExt(ctx->builder, src1, ctx->i64, "");
393bf215546Sopenharmony_ci
394bf215546Sopenharmony_ci   dst64 = LLVMBuildMul(ctx->builder, src0, src1, "");
395bf215546Sopenharmony_ci   dst64 = LLVMBuildAShr(ctx->builder, dst64, LLVMConstInt(ctx->i64, 32, false), "");
396bf215546Sopenharmony_ci   result = LLVMBuildTrunc(ctx->builder, dst64, ctx->i32, "");
397bf215546Sopenharmony_ci   return result;
398bf215546Sopenharmony_ci}
399bf215546Sopenharmony_ci
400bf215546Sopenharmony_cistatic LLVMValueRef emit_bfm(struct ac_llvm_context *ctx, LLVMValueRef bits, LLVMValueRef offset)
401bf215546Sopenharmony_ci{
402bf215546Sopenharmony_ci   /* mask = ((1 << bits) - 1) << offset */
403bf215546Sopenharmony_ci   return LLVMBuildShl(
404bf215546Sopenharmony_ci      ctx->builder,
405bf215546Sopenharmony_ci      LLVMBuildSub(ctx->builder, LLVMBuildShl(ctx->builder, ctx->i32_1, bits, ""), ctx->i32_1, ""),
406bf215546Sopenharmony_ci      offset, "");
407bf215546Sopenharmony_ci}
408bf215546Sopenharmony_ci
409bf215546Sopenharmony_cistatic LLVMValueRef emit_bitfield_select(struct ac_llvm_context *ctx, LLVMValueRef mask,
410bf215546Sopenharmony_ci                                         LLVMValueRef insert, LLVMValueRef base)
411bf215546Sopenharmony_ci{
412bf215546Sopenharmony_ci   /* Calculate:
413bf215546Sopenharmony_ci    *   (mask & insert) | (~mask & base) = base ^ (mask & (insert ^ base))
414bf215546Sopenharmony_ci    * Use the right-hand side, which the LLVM backend can convert to V_BFI.
415bf215546Sopenharmony_ci    */
416bf215546Sopenharmony_ci   return LLVMBuildXor(
417bf215546Sopenharmony_ci      ctx->builder, base,
418bf215546Sopenharmony_ci      LLVMBuildAnd(ctx->builder, mask, LLVMBuildXor(ctx->builder, insert, base, ""), ""), "");
419bf215546Sopenharmony_ci}
420bf215546Sopenharmony_ci
421bf215546Sopenharmony_cistatic LLVMValueRef emit_pack_2x16(struct ac_llvm_context *ctx, LLVMValueRef src0,
422bf215546Sopenharmony_ci                                   LLVMValueRef (*pack)(struct ac_llvm_context *ctx,
423bf215546Sopenharmony_ci                                                        LLVMValueRef args[2]))
424bf215546Sopenharmony_ci{
425bf215546Sopenharmony_ci   LLVMValueRef comp[2];
426bf215546Sopenharmony_ci
427bf215546Sopenharmony_ci   src0 = ac_to_float(ctx, src0);
428bf215546Sopenharmony_ci   comp[0] = LLVMBuildExtractElement(ctx->builder, src0, ctx->i32_0, "");
429bf215546Sopenharmony_ci   comp[1] = LLVMBuildExtractElement(ctx->builder, src0, ctx->i32_1, "");
430bf215546Sopenharmony_ci
431bf215546Sopenharmony_ci   return LLVMBuildBitCast(ctx->builder, pack(ctx, comp), ctx->i32, "");
432bf215546Sopenharmony_ci}
433bf215546Sopenharmony_ci
434bf215546Sopenharmony_cistatic LLVMValueRef emit_unpack_half_2x16(struct ac_llvm_context *ctx, LLVMValueRef src0)
435bf215546Sopenharmony_ci{
436bf215546Sopenharmony_ci   LLVMValueRef const16 = LLVMConstInt(ctx->i32, 16, false);
437bf215546Sopenharmony_ci   LLVMValueRef temps[2], val;
438bf215546Sopenharmony_ci   int i;
439bf215546Sopenharmony_ci
440bf215546Sopenharmony_ci   for (i = 0; i < 2; i++) {
441bf215546Sopenharmony_ci      val = i == 1 ? LLVMBuildLShr(ctx->builder, src0, const16, "") : src0;
442bf215546Sopenharmony_ci      val = LLVMBuildTrunc(ctx->builder, val, ctx->i16, "");
443bf215546Sopenharmony_ci      val = LLVMBuildBitCast(ctx->builder, val, ctx->f16, "");
444bf215546Sopenharmony_ci      temps[i] = LLVMBuildFPExt(ctx->builder, val, ctx->f32, "");
445bf215546Sopenharmony_ci   }
446bf215546Sopenharmony_ci   return ac_build_gather_values(ctx, temps, 2);
447bf215546Sopenharmony_ci}
448bf215546Sopenharmony_ci
449bf215546Sopenharmony_cistatic LLVMValueRef emit_ddxy(struct ac_nir_context *ctx, nir_op op, LLVMValueRef src0)
450bf215546Sopenharmony_ci{
451bf215546Sopenharmony_ci   unsigned mask;
452bf215546Sopenharmony_ci   int idx;
453bf215546Sopenharmony_ci   LLVMValueRef result;
454bf215546Sopenharmony_ci
455bf215546Sopenharmony_ci   if (op == nir_op_fddx_fine)
456bf215546Sopenharmony_ci      mask = AC_TID_MASK_LEFT;
457bf215546Sopenharmony_ci   else if (op == nir_op_fddy_fine)
458bf215546Sopenharmony_ci      mask = AC_TID_MASK_TOP;
459bf215546Sopenharmony_ci   else
460bf215546Sopenharmony_ci      mask = AC_TID_MASK_TOP_LEFT;
461bf215546Sopenharmony_ci
462bf215546Sopenharmony_ci   /* for DDX we want to next X pixel, DDY next Y pixel. */
463bf215546Sopenharmony_ci   if (op == nir_op_fddx_fine || op == nir_op_fddx_coarse || op == nir_op_fddx)
464bf215546Sopenharmony_ci      idx = 1;
465bf215546Sopenharmony_ci   else
466bf215546Sopenharmony_ci      idx = 2;
467bf215546Sopenharmony_ci
468bf215546Sopenharmony_ci   result = ac_build_ddxy(&ctx->ac, mask, idx, src0);
469bf215546Sopenharmony_ci   return result;
470bf215546Sopenharmony_ci}
471bf215546Sopenharmony_ci
472bf215546Sopenharmony_cistruct waterfall_context {
473bf215546Sopenharmony_ci   LLVMBasicBlockRef phi_bb[2];
474bf215546Sopenharmony_ci   bool use_waterfall;
475bf215546Sopenharmony_ci};
476bf215546Sopenharmony_ci
477bf215546Sopenharmony_ci/* To deal with divergent descriptors we can create a loop that handles all
478bf215546Sopenharmony_ci * lanes with the same descriptor on a given iteration (henceforth a
479bf215546Sopenharmony_ci * waterfall loop).
480bf215546Sopenharmony_ci *
481bf215546Sopenharmony_ci * These helper create the begin and end of the loop leaving the caller
482bf215546Sopenharmony_ci * to implement the body.
483bf215546Sopenharmony_ci *
484bf215546Sopenharmony_ci * params:
485bf215546Sopenharmony_ci *  - ctx is the usal nir context
486bf215546Sopenharmony_ci *  - wctx is a temporary struct containing some loop info. Can be left uninitialized.
487bf215546Sopenharmony_ci *  - value is the possibly divergent value for which we built the loop
488bf215546Sopenharmony_ci *  - divergent is whether value is actually divergent. If false we just pass
489bf215546Sopenharmony_ci *     things through.
490bf215546Sopenharmony_ci */
491bf215546Sopenharmony_cistatic LLVMValueRef enter_waterfall(struct ac_nir_context *ctx, struct waterfall_context *wctx,
492bf215546Sopenharmony_ci                                    LLVMValueRef value, bool divergent)
493bf215546Sopenharmony_ci{
494bf215546Sopenharmony_ci   /* If the app claims the value is divergent but it is constant we can
495bf215546Sopenharmony_ci    * end up with a dynamic index of NULL. */
496bf215546Sopenharmony_ci   if (!value)
497bf215546Sopenharmony_ci      divergent = false;
498bf215546Sopenharmony_ci
499bf215546Sopenharmony_ci   wctx->use_waterfall = divergent;
500bf215546Sopenharmony_ci   if (!divergent)
501bf215546Sopenharmony_ci      return value;
502bf215546Sopenharmony_ci
503bf215546Sopenharmony_ci   ac_build_bgnloop(&ctx->ac, 6000);
504bf215546Sopenharmony_ci
505bf215546Sopenharmony_ci   LLVMValueRef active = LLVMConstInt(ctx->ac.i1, 1, false);
506bf215546Sopenharmony_ci   LLVMValueRef scalar_value[NIR_MAX_VEC_COMPONENTS];
507bf215546Sopenharmony_ci
508bf215546Sopenharmony_ci   for (unsigned i = 0; i < ac_get_llvm_num_components(value); i++) {
509bf215546Sopenharmony_ci      LLVMValueRef comp = ac_llvm_extract_elem(&ctx->ac, value, i);
510bf215546Sopenharmony_ci      scalar_value[i] = ac_build_readlane(&ctx->ac, comp, NULL);
511bf215546Sopenharmony_ci      active = LLVMBuildAnd(ctx->ac.builder, active,
512bf215546Sopenharmony_ci                            LLVMBuildICmp(ctx->ac.builder, LLVMIntEQ, comp, scalar_value[i], ""), "");
513bf215546Sopenharmony_ci   }
514bf215546Sopenharmony_ci
515bf215546Sopenharmony_ci   wctx->phi_bb[0] = LLVMGetInsertBlock(ctx->ac.builder);
516bf215546Sopenharmony_ci   ac_build_ifcc(&ctx->ac, active, 6001);
517bf215546Sopenharmony_ci
518bf215546Sopenharmony_ci   return ac_build_gather_values(&ctx->ac, scalar_value, ac_get_llvm_num_components(value));
519bf215546Sopenharmony_ci}
520bf215546Sopenharmony_ci
521bf215546Sopenharmony_cistatic LLVMValueRef exit_waterfall(struct ac_nir_context *ctx, struct waterfall_context *wctx,
522bf215546Sopenharmony_ci                                   LLVMValueRef value)
523bf215546Sopenharmony_ci{
524bf215546Sopenharmony_ci   LLVMValueRef ret = NULL;
525bf215546Sopenharmony_ci   LLVMValueRef phi_src[2];
526bf215546Sopenharmony_ci   LLVMValueRef cc_phi_src[2] = {
527bf215546Sopenharmony_ci      LLVMConstInt(ctx->ac.i32, 0, false),
528bf215546Sopenharmony_ci      LLVMConstInt(ctx->ac.i32, 0xffffffff, false),
529bf215546Sopenharmony_ci   };
530bf215546Sopenharmony_ci
531bf215546Sopenharmony_ci   if (!wctx->use_waterfall)
532bf215546Sopenharmony_ci      return value;
533bf215546Sopenharmony_ci
534bf215546Sopenharmony_ci   wctx->phi_bb[1] = LLVMGetInsertBlock(ctx->ac.builder);
535bf215546Sopenharmony_ci
536bf215546Sopenharmony_ci   ac_build_endif(&ctx->ac, 6001);
537bf215546Sopenharmony_ci
538bf215546Sopenharmony_ci   if (value) {
539bf215546Sopenharmony_ci      phi_src[0] = LLVMGetUndef(LLVMTypeOf(value));
540bf215546Sopenharmony_ci      phi_src[1] = value;
541bf215546Sopenharmony_ci
542bf215546Sopenharmony_ci      ret = ac_build_phi(&ctx->ac, LLVMTypeOf(value), 2, phi_src, wctx->phi_bb);
543bf215546Sopenharmony_ci   }
544bf215546Sopenharmony_ci
545bf215546Sopenharmony_ci   /*
546bf215546Sopenharmony_ci    * By using the optimization barrier on the exit decision, we decouple
547bf215546Sopenharmony_ci    * the operations from the break, and hence avoid LLVM hoisting the
548bf215546Sopenharmony_ci    * opteration into the break block.
549bf215546Sopenharmony_ci    */
550bf215546Sopenharmony_ci   LLVMValueRef cc = ac_build_phi(&ctx->ac, ctx->ac.i32, 2, cc_phi_src, wctx->phi_bb);
551bf215546Sopenharmony_ci   ac_build_optimization_barrier(&ctx->ac, &cc, false);
552bf215546Sopenharmony_ci
553bf215546Sopenharmony_ci   LLVMValueRef active =
554bf215546Sopenharmony_ci      LLVMBuildICmp(ctx->ac.builder, LLVMIntNE, cc, ctx->ac.i32_0, "uniform_active2");
555bf215546Sopenharmony_ci   ac_build_ifcc(&ctx->ac, active, 6002);
556bf215546Sopenharmony_ci   ac_build_break(&ctx->ac);
557bf215546Sopenharmony_ci   ac_build_endif(&ctx->ac, 6002);
558bf215546Sopenharmony_ci
559bf215546Sopenharmony_ci   ac_build_endloop(&ctx->ac, 6000);
560bf215546Sopenharmony_ci   return ret;
561bf215546Sopenharmony_ci}
562bf215546Sopenharmony_ci
563bf215546Sopenharmony_cistatic void visit_alu(struct ac_nir_context *ctx, const nir_alu_instr *instr)
564bf215546Sopenharmony_ci{
565bf215546Sopenharmony_ci   LLVMValueRef src[16], result = NULL;
566bf215546Sopenharmony_ci   unsigned num_components = instr->dest.dest.ssa.num_components;
567bf215546Sopenharmony_ci   unsigned src_components;
568bf215546Sopenharmony_ci   LLVMTypeRef def_type = get_def_type(ctx, &instr->dest.dest.ssa);
569bf215546Sopenharmony_ci
570bf215546Sopenharmony_ci   assert(nir_op_infos[instr->op].num_inputs <= ARRAY_SIZE(src));
571bf215546Sopenharmony_ci   switch (instr->op) {
572bf215546Sopenharmony_ci   case nir_op_vec2:
573bf215546Sopenharmony_ci   case nir_op_vec3:
574bf215546Sopenharmony_ci   case nir_op_vec4:
575bf215546Sopenharmony_ci   case nir_op_vec5:
576bf215546Sopenharmony_ci   case nir_op_vec8:
577bf215546Sopenharmony_ci   case nir_op_vec16:
578bf215546Sopenharmony_ci   case nir_op_unpack_32_2x16:
579bf215546Sopenharmony_ci   case nir_op_unpack_64_2x32:
580bf215546Sopenharmony_ci   case nir_op_unpack_64_4x16:
581bf215546Sopenharmony_ci      src_components = 1;
582bf215546Sopenharmony_ci      break;
583bf215546Sopenharmony_ci   case nir_op_pack_half_2x16:
584bf215546Sopenharmony_ci   case nir_op_pack_snorm_2x16:
585bf215546Sopenharmony_ci   case nir_op_pack_unorm_2x16:
586bf215546Sopenharmony_ci   case nir_op_pack_uint_2x16:
587bf215546Sopenharmony_ci   case nir_op_pack_sint_2x16:
588bf215546Sopenharmony_ci   case nir_op_pack_32_2x16:
589bf215546Sopenharmony_ci   case nir_op_pack_64_2x32:
590bf215546Sopenharmony_ci      src_components = 2;
591bf215546Sopenharmony_ci      break;
592bf215546Sopenharmony_ci   case nir_op_unpack_half_2x16:
593bf215546Sopenharmony_ci      src_components = 1;
594bf215546Sopenharmony_ci      break;
595bf215546Sopenharmony_ci   case nir_op_cube_face_coord_amd:
596bf215546Sopenharmony_ci   case nir_op_cube_face_index_amd:
597bf215546Sopenharmony_ci      src_components = 3;
598bf215546Sopenharmony_ci      break;
599bf215546Sopenharmony_ci   case nir_op_pack_32_4x8:
600bf215546Sopenharmony_ci   case nir_op_pack_64_4x16:
601bf215546Sopenharmony_ci      src_components = 4;
602bf215546Sopenharmony_ci      break;
603bf215546Sopenharmony_ci   default:
604bf215546Sopenharmony_ci      src_components = num_components;
605bf215546Sopenharmony_ci      break;
606bf215546Sopenharmony_ci   }
607bf215546Sopenharmony_ci   for (unsigned i = 0; i < nir_op_infos[instr->op].num_inputs; i++)
608bf215546Sopenharmony_ci      src[i] = get_alu_src(ctx, instr->src[i], src_components);
609bf215546Sopenharmony_ci
610bf215546Sopenharmony_ci   switch (instr->op) {
611bf215546Sopenharmony_ci   case nir_op_mov:
612bf215546Sopenharmony_ci      result = src[0];
613bf215546Sopenharmony_ci      break;
614bf215546Sopenharmony_ci   case nir_op_fneg:
615bf215546Sopenharmony_ci      src[0] = ac_to_float(&ctx->ac, src[0]);
616bf215546Sopenharmony_ci      result = LLVMBuildFNeg(ctx->ac.builder, src[0], "");
617bf215546Sopenharmony_ci      if (ctx->ac.float_mode == AC_FLOAT_MODE_DENORM_FLUSH_TO_ZERO) {
618bf215546Sopenharmony_ci         /* fneg will be optimized by backend compiler with sign
619bf215546Sopenharmony_ci          * bit removed via XOR. This is probably a LLVM bug.
620bf215546Sopenharmony_ci          */
621bf215546Sopenharmony_ci         result = ac_build_canonicalize(&ctx->ac, result, instr->dest.dest.ssa.bit_size);
622bf215546Sopenharmony_ci      }
623bf215546Sopenharmony_ci      break;
624bf215546Sopenharmony_ci   case nir_op_ineg:
625bf215546Sopenharmony_ci      if (instr->no_unsigned_wrap)
626bf215546Sopenharmony_ci         result = LLVMBuildNUWNeg(ctx->ac.builder, src[0], "");
627bf215546Sopenharmony_ci      else if (instr->no_signed_wrap)
628bf215546Sopenharmony_ci         result = LLVMBuildNSWNeg(ctx->ac.builder, src[0], "");
629bf215546Sopenharmony_ci      else
630bf215546Sopenharmony_ci         result = LLVMBuildNeg(ctx->ac.builder, src[0], "");
631bf215546Sopenharmony_ci      break;
632bf215546Sopenharmony_ci   case nir_op_inot:
633bf215546Sopenharmony_ci      result = LLVMBuildNot(ctx->ac.builder, src[0], "");
634bf215546Sopenharmony_ci      break;
635bf215546Sopenharmony_ci   case nir_op_iadd:
636bf215546Sopenharmony_ci      if (instr->no_unsigned_wrap)
637bf215546Sopenharmony_ci         result = LLVMBuildNUWAdd(ctx->ac.builder, src[0], src[1], "");
638bf215546Sopenharmony_ci      else if (instr->no_signed_wrap)
639bf215546Sopenharmony_ci         result = LLVMBuildNSWAdd(ctx->ac.builder, src[0], src[1], "");
640bf215546Sopenharmony_ci      else
641bf215546Sopenharmony_ci         result = LLVMBuildAdd(ctx->ac.builder, src[0], src[1], "");
642bf215546Sopenharmony_ci      break;
643bf215546Sopenharmony_ci   case nir_op_uadd_sat:
644bf215546Sopenharmony_ci   case nir_op_iadd_sat: {
645bf215546Sopenharmony_ci      char name[64], type[64];
646bf215546Sopenharmony_ci      ac_build_type_name_for_intr(def_type, type, sizeof(type));
647bf215546Sopenharmony_ci      snprintf(name, sizeof(name), "llvm.%cadd.sat.%s",
648bf215546Sopenharmony_ci               instr->op == nir_op_uadd_sat ? 'u' : 's', type);
649bf215546Sopenharmony_ci      result = ac_build_intrinsic(&ctx->ac, name, def_type, src, 2, AC_FUNC_ATTR_READNONE);
650bf215546Sopenharmony_ci      break;
651bf215546Sopenharmony_ci   }
652bf215546Sopenharmony_ci   case nir_op_usub_sat:
653bf215546Sopenharmony_ci   case nir_op_isub_sat: {
654bf215546Sopenharmony_ci      char name[64], type[64];
655bf215546Sopenharmony_ci      ac_build_type_name_for_intr(def_type, type, sizeof(type));
656bf215546Sopenharmony_ci      snprintf(name, sizeof(name), "llvm.%csub.sat.%s",
657bf215546Sopenharmony_ci               instr->op == nir_op_usub_sat ? 'u' : 's', type);
658bf215546Sopenharmony_ci      result = ac_build_intrinsic(&ctx->ac, name, def_type, src, 2, AC_FUNC_ATTR_READNONE);
659bf215546Sopenharmony_ci      break;
660bf215546Sopenharmony_ci   }
661bf215546Sopenharmony_ci   case nir_op_fadd:
662bf215546Sopenharmony_ci      src[0] = ac_to_float(&ctx->ac, src[0]);
663bf215546Sopenharmony_ci      src[1] = ac_to_float(&ctx->ac, src[1]);
664bf215546Sopenharmony_ci      result = LLVMBuildFAdd(ctx->ac.builder, src[0], src[1], "");
665bf215546Sopenharmony_ci      break;
666bf215546Sopenharmony_ci   case nir_op_fsub:
667bf215546Sopenharmony_ci      src[0] = ac_to_float(&ctx->ac, src[0]);
668bf215546Sopenharmony_ci      src[1] = ac_to_float(&ctx->ac, src[1]);
669bf215546Sopenharmony_ci      result = LLVMBuildFSub(ctx->ac.builder, src[0], src[1], "");
670bf215546Sopenharmony_ci      break;
671bf215546Sopenharmony_ci   case nir_op_isub:
672bf215546Sopenharmony_ci      if (instr->no_unsigned_wrap)
673bf215546Sopenharmony_ci         result = LLVMBuildNUWSub(ctx->ac.builder, src[0], src[1], "");
674bf215546Sopenharmony_ci      else if (instr->no_signed_wrap)
675bf215546Sopenharmony_ci         result = LLVMBuildNSWSub(ctx->ac.builder, src[0], src[1], "");
676bf215546Sopenharmony_ci      else
677bf215546Sopenharmony_ci         result = LLVMBuildSub(ctx->ac.builder, src[0], src[1], "");
678bf215546Sopenharmony_ci      break;
679bf215546Sopenharmony_ci   case nir_op_imul:
680bf215546Sopenharmony_ci      if (instr->no_unsigned_wrap)
681bf215546Sopenharmony_ci         result = LLVMBuildNUWMul(ctx->ac.builder, src[0], src[1], "");
682bf215546Sopenharmony_ci      else if (instr->no_signed_wrap)
683bf215546Sopenharmony_ci         result = LLVMBuildNSWMul(ctx->ac.builder, src[0], src[1], "");
684bf215546Sopenharmony_ci      else
685bf215546Sopenharmony_ci         result = LLVMBuildMul(ctx->ac.builder, src[0], src[1], "");
686bf215546Sopenharmony_ci      break;
687bf215546Sopenharmony_ci   case nir_op_imod:
688bf215546Sopenharmony_ci      result = LLVMBuildSRem(ctx->ac.builder, src[0], src[1], "");
689bf215546Sopenharmony_ci      break;
690bf215546Sopenharmony_ci   case nir_op_umod:
691bf215546Sopenharmony_ci      result = LLVMBuildURem(ctx->ac.builder, src[0], src[1], "");
692bf215546Sopenharmony_ci      break;
693bf215546Sopenharmony_ci   case nir_op_irem:
694bf215546Sopenharmony_ci      result = LLVMBuildSRem(ctx->ac.builder, src[0], src[1], "");
695bf215546Sopenharmony_ci      break;
696bf215546Sopenharmony_ci   case nir_op_idiv:
697bf215546Sopenharmony_ci      result = LLVMBuildSDiv(ctx->ac.builder, src[0], src[1], "");
698bf215546Sopenharmony_ci      break;
699bf215546Sopenharmony_ci   case nir_op_udiv:
700bf215546Sopenharmony_ci      result = LLVMBuildUDiv(ctx->ac.builder, src[0], src[1], "");
701bf215546Sopenharmony_ci      break;
702bf215546Sopenharmony_ci   case nir_op_fmul:
703bf215546Sopenharmony_ci      src[0] = ac_to_float(&ctx->ac, src[0]);
704bf215546Sopenharmony_ci      src[1] = ac_to_float(&ctx->ac, src[1]);
705bf215546Sopenharmony_ci      result = LLVMBuildFMul(ctx->ac.builder, src[0], src[1], "");
706bf215546Sopenharmony_ci      break;
707bf215546Sopenharmony_ci   case nir_op_fmulz:
708bf215546Sopenharmony_ci      assert(LLVM_VERSION_MAJOR >= 12);
709bf215546Sopenharmony_ci      src[0] = ac_to_float(&ctx->ac, src[0]);
710bf215546Sopenharmony_ci      src[1] = ac_to_float(&ctx->ac, src[1]);
711bf215546Sopenharmony_ci      result = ac_build_intrinsic(&ctx->ac, "llvm.amdgcn.fmul.legacy", ctx->ac.f32,
712bf215546Sopenharmony_ci                                  src, 2, AC_FUNC_ATTR_READNONE);
713bf215546Sopenharmony_ci      break;
714bf215546Sopenharmony_ci   case nir_op_frcp:
715bf215546Sopenharmony_ci      /* For doubles, we need precise division to pass GLCTS. */
716bf215546Sopenharmony_ci      if (ctx->ac.float_mode == AC_FLOAT_MODE_DEFAULT_OPENGL && ac_get_type_size(def_type) == 8) {
717bf215546Sopenharmony_ci         result = LLVMBuildFDiv(ctx->ac.builder, ctx->ac.f64_1, ac_to_float(&ctx->ac, src[0]), "");
718bf215546Sopenharmony_ci      } else {
719bf215546Sopenharmony_ci         result = emit_intrin_1f_param_scalar(&ctx->ac, "llvm.amdgcn.rcp",
720bf215546Sopenharmony_ci                                              ac_to_float_type(&ctx->ac, def_type), src[0]);
721bf215546Sopenharmony_ci      }
722bf215546Sopenharmony_ci      if (ctx->abi->clamp_div_by_zero)
723bf215546Sopenharmony_ci         result = ac_build_fmin(&ctx->ac, result,
724bf215546Sopenharmony_ci                                LLVMConstReal(ac_to_float_type(&ctx->ac, def_type), FLT_MAX));
725bf215546Sopenharmony_ci      break;
726bf215546Sopenharmony_ci   case nir_op_iand:
727bf215546Sopenharmony_ci      result = LLVMBuildAnd(ctx->ac.builder, src[0], src[1], "");
728bf215546Sopenharmony_ci      break;
729bf215546Sopenharmony_ci   case nir_op_ior:
730bf215546Sopenharmony_ci      result = LLVMBuildOr(ctx->ac.builder, src[0], src[1], "");
731bf215546Sopenharmony_ci      break;
732bf215546Sopenharmony_ci   case nir_op_ixor:
733bf215546Sopenharmony_ci      result = LLVMBuildXor(ctx->ac.builder, src[0], src[1], "");
734bf215546Sopenharmony_ci      break;
735bf215546Sopenharmony_ci   case nir_op_ishl:
736bf215546Sopenharmony_ci      if (ac_get_elem_bits(&ctx->ac, LLVMTypeOf(src[1])) <
737bf215546Sopenharmony_ci          ac_get_elem_bits(&ctx->ac, LLVMTypeOf(src[0])))
738bf215546Sopenharmony_ci         src[1] = LLVMBuildZExt(ctx->ac.builder, src[1], LLVMTypeOf(src[0]), "");
739bf215546Sopenharmony_ci      else if (ac_get_elem_bits(&ctx->ac, LLVMTypeOf(src[1])) >
740bf215546Sopenharmony_ci               ac_get_elem_bits(&ctx->ac, LLVMTypeOf(src[0])))
741bf215546Sopenharmony_ci         src[1] = LLVMBuildTrunc(ctx->ac.builder, src[1], LLVMTypeOf(src[0]), "");
742bf215546Sopenharmony_ci      result = LLVMBuildShl(ctx->ac.builder, src[0], src[1], "");
743bf215546Sopenharmony_ci      break;
744bf215546Sopenharmony_ci   case nir_op_ishr:
745bf215546Sopenharmony_ci      if (ac_get_elem_bits(&ctx->ac, LLVMTypeOf(src[1])) <
746bf215546Sopenharmony_ci          ac_get_elem_bits(&ctx->ac, LLVMTypeOf(src[0])))
747bf215546Sopenharmony_ci         src[1] = LLVMBuildZExt(ctx->ac.builder, src[1], LLVMTypeOf(src[0]), "");
748bf215546Sopenharmony_ci      else if (ac_get_elem_bits(&ctx->ac, LLVMTypeOf(src[1])) >
749bf215546Sopenharmony_ci               ac_get_elem_bits(&ctx->ac, LLVMTypeOf(src[0])))
750bf215546Sopenharmony_ci         src[1] = LLVMBuildTrunc(ctx->ac.builder, src[1], LLVMTypeOf(src[0]), "");
751bf215546Sopenharmony_ci      result = LLVMBuildAShr(ctx->ac.builder, src[0], src[1], "");
752bf215546Sopenharmony_ci      break;
753bf215546Sopenharmony_ci   case nir_op_ushr:
754bf215546Sopenharmony_ci      if (ac_get_elem_bits(&ctx->ac, LLVMTypeOf(src[1])) <
755bf215546Sopenharmony_ci          ac_get_elem_bits(&ctx->ac, LLVMTypeOf(src[0])))
756bf215546Sopenharmony_ci         src[1] = LLVMBuildZExt(ctx->ac.builder, src[1], LLVMTypeOf(src[0]), "");
757bf215546Sopenharmony_ci      else if (ac_get_elem_bits(&ctx->ac, LLVMTypeOf(src[1])) >
758bf215546Sopenharmony_ci               ac_get_elem_bits(&ctx->ac, LLVMTypeOf(src[0])))
759bf215546Sopenharmony_ci         src[1] = LLVMBuildTrunc(ctx->ac.builder, src[1], LLVMTypeOf(src[0]), "");
760bf215546Sopenharmony_ci      result = LLVMBuildLShr(ctx->ac.builder, src[0], src[1], "");
761bf215546Sopenharmony_ci      break;
762bf215546Sopenharmony_ci   case nir_op_ilt:
763bf215546Sopenharmony_ci      result = emit_int_cmp(&ctx->ac, LLVMIntSLT, src[0], src[1]);
764bf215546Sopenharmony_ci      break;
765bf215546Sopenharmony_ci   case nir_op_ine:
766bf215546Sopenharmony_ci      result = emit_int_cmp(&ctx->ac, LLVMIntNE, src[0], src[1]);
767bf215546Sopenharmony_ci      break;
768bf215546Sopenharmony_ci   case nir_op_ieq:
769bf215546Sopenharmony_ci      result = emit_int_cmp(&ctx->ac, LLVMIntEQ, src[0], src[1]);
770bf215546Sopenharmony_ci      break;
771bf215546Sopenharmony_ci   case nir_op_ige:
772bf215546Sopenharmony_ci      result = emit_int_cmp(&ctx->ac, LLVMIntSGE, src[0], src[1]);
773bf215546Sopenharmony_ci      break;
774bf215546Sopenharmony_ci   case nir_op_ult:
775bf215546Sopenharmony_ci      result = emit_int_cmp(&ctx->ac, LLVMIntULT, src[0], src[1]);
776bf215546Sopenharmony_ci      break;
777bf215546Sopenharmony_ci   case nir_op_uge:
778bf215546Sopenharmony_ci      result = emit_int_cmp(&ctx->ac, LLVMIntUGE, src[0], src[1]);
779bf215546Sopenharmony_ci      break;
780bf215546Sopenharmony_ci   case nir_op_feq:
781bf215546Sopenharmony_ci      result = emit_float_cmp(&ctx->ac, LLVMRealOEQ, src[0], src[1]);
782bf215546Sopenharmony_ci      break;
783bf215546Sopenharmony_ci   case nir_op_fneu:
784bf215546Sopenharmony_ci      result = emit_float_cmp(&ctx->ac, LLVMRealUNE, src[0], src[1]);
785bf215546Sopenharmony_ci      break;
786bf215546Sopenharmony_ci   case nir_op_flt:
787bf215546Sopenharmony_ci      result = emit_float_cmp(&ctx->ac, LLVMRealOLT, src[0], src[1]);
788bf215546Sopenharmony_ci      break;
789bf215546Sopenharmony_ci   case nir_op_fge:
790bf215546Sopenharmony_ci      result = emit_float_cmp(&ctx->ac, LLVMRealOGE, src[0], src[1]);
791bf215546Sopenharmony_ci      break;
792bf215546Sopenharmony_ci   case nir_op_fabs:
793bf215546Sopenharmony_ci      result =
794bf215546Sopenharmony_ci         emit_intrin_1f_param(&ctx->ac, "llvm.fabs", ac_to_float_type(&ctx->ac, def_type), src[0]);
795bf215546Sopenharmony_ci      if (ctx->ac.float_mode == AC_FLOAT_MODE_DENORM_FLUSH_TO_ZERO) {
796bf215546Sopenharmony_ci         /* fabs will be optimized by backend compiler with sign
797bf215546Sopenharmony_ci          * bit removed via AND.
798bf215546Sopenharmony_ci          */
799bf215546Sopenharmony_ci         result = ac_build_canonicalize(&ctx->ac, result, instr->dest.dest.ssa.bit_size);
800bf215546Sopenharmony_ci      }
801bf215546Sopenharmony_ci      break;
802bf215546Sopenharmony_ci   case nir_op_fsat:
803bf215546Sopenharmony_ci      src[0] = ac_to_float(&ctx->ac, src[0]);
804bf215546Sopenharmony_ci      result = ac_build_fsat(&ctx->ac, src[0],
805bf215546Sopenharmony_ci                             ac_to_float_type(&ctx->ac, def_type));
806bf215546Sopenharmony_ci      break;
807bf215546Sopenharmony_ci   case nir_op_iabs:
808bf215546Sopenharmony_ci      result = emit_iabs(&ctx->ac, src[0]);
809bf215546Sopenharmony_ci      break;
810bf215546Sopenharmony_ci   case nir_op_imax:
811bf215546Sopenharmony_ci      result = ac_build_imax(&ctx->ac, src[0], src[1]);
812bf215546Sopenharmony_ci      break;
813bf215546Sopenharmony_ci   case nir_op_imin:
814bf215546Sopenharmony_ci      result = ac_build_imin(&ctx->ac, src[0], src[1]);
815bf215546Sopenharmony_ci      break;
816bf215546Sopenharmony_ci   case nir_op_umax:
817bf215546Sopenharmony_ci      result = ac_build_umax(&ctx->ac, src[0], src[1]);
818bf215546Sopenharmony_ci      break;
819bf215546Sopenharmony_ci   case nir_op_umin:
820bf215546Sopenharmony_ci      result = ac_build_umin(&ctx->ac, src[0], src[1]);
821bf215546Sopenharmony_ci      break;
822bf215546Sopenharmony_ci   case nir_op_isign:
823bf215546Sopenharmony_ci      result = ac_build_isign(&ctx->ac, src[0]);
824bf215546Sopenharmony_ci      break;
825bf215546Sopenharmony_ci   case nir_op_fsign:
826bf215546Sopenharmony_ci      src[0] = ac_to_float(&ctx->ac, src[0]);
827bf215546Sopenharmony_ci      result = ac_build_fsign(&ctx->ac, src[0]);
828bf215546Sopenharmony_ci      break;
829bf215546Sopenharmony_ci   case nir_op_ffloor:
830bf215546Sopenharmony_ci      result =
831bf215546Sopenharmony_ci         emit_intrin_1f_param(&ctx->ac, "llvm.floor", ac_to_float_type(&ctx->ac, def_type), src[0]);
832bf215546Sopenharmony_ci      break;
833bf215546Sopenharmony_ci   case nir_op_ftrunc:
834bf215546Sopenharmony_ci      result =
835bf215546Sopenharmony_ci         emit_intrin_1f_param(&ctx->ac, "llvm.trunc", ac_to_float_type(&ctx->ac, def_type), src[0]);
836bf215546Sopenharmony_ci      break;
837bf215546Sopenharmony_ci   case nir_op_fceil:
838bf215546Sopenharmony_ci      result =
839bf215546Sopenharmony_ci         emit_intrin_1f_param(&ctx->ac, "llvm.ceil", ac_to_float_type(&ctx->ac, def_type), src[0]);
840bf215546Sopenharmony_ci      break;
841bf215546Sopenharmony_ci   case nir_op_fround_even:
842bf215546Sopenharmony_ci      result =
843bf215546Sopenharmony_ci         emit_intrin_1f_param(&ctx->ac, "llvm.rint", ac_to_float_type(&ctx->ac, def_type), src[0]);
844bf215546Sopenharmony_ci      break;
845bf215546Sopenharmony_ci   case nir_op_ffract:
846bf215546Sopenharmony_ci      result = emit_intrin_1f_param_scalar(&ctx->ac, "llvm.amdgcn.fract",
847bf215546Sopenharmony_ci                                           ac_to_float_type(&ctx->ac, def_type), src[0]);
848bf215546Sopenharmony_ci      break;
849bf215546Sopenharmony_ci   case nir_op_fsin:
850bf215546Sopenharmony_ci      result =
851bf215546Sopenharmony_ci         emit_intrin_1f_param(&ctx->ac, "llvm.sin", ac_to_float_type(&ctx->ac, def_type), src[0]);
852bf215546Sopenharmony_ci      break;
853bf215546Sopenharmony_ci   case nir_op_fcos:
854bf215546Sopenharmony_ci      result =
855bf215546Sopenharmony_ci         emit_intrin_1f_param(&ctx->ac, "llvm.cos", ac_to_float_type(&ctx->ac, def_type), src[0]);
856bf215546Sopenharmony_ci      break;
857bf215546Sopenharmony_ci   case nir_op_fsin_amd:
858bf215546Sopenharmony_ci   case nir_op_fcos_amd:
859bf215546Sopenharmony_ci      /* before GFX9, v_sin_f32 and v_cos_f32 had a valid input domain of [-256, +256] */
860bf215546Sopenharmony_ci      if (ctx->ac.gfx_level < GFX9)
861bf215546Sopenharmony_ci         src[0] = emit_intrin_1f_param_scalar(&ctx->ac, "llvm.amdgcn.fract",
862bf215546Sopenharmony_ci                                              ac_to_float_type(&ctx->ac, def_type), src[0]);
863bf215546Sopenharmony_ci      result =
864bf215546Sopenharmony_ci         emit_intrin_1f_param(&ctx->ac, instr->op == nir_op_fsin_amd ? "llvm.amdgcn.sin" : "llvm.amdgcn.cos",
865bf215546Sopenharmony_ci                              ac_to_float_type(&ctx->ac, def_type), src[0]);
866bf215546Sopenharmony_ci      break;
867bf215546Sopenharmony_ci   case nir_op_fsqrt:
868bf215546Sopenharmony_ci      result =
869bf215546Sopenharmony_ci         emit_intrin_1f_param(&ctx->ac, "llvm.sqrt", ac_to_float_type(&ctx->ac, def_type), src[0]);
870bf215546Sopenharmony_ci      break;
871bf215546Sopenharmony_ci   case nir_op_fexp2:
872bf215546Sopenharmony_ci      result =
873bf215546Sopenharmony_ci         emit_intrin_1f_param(&ctx->ac, "llvm.exp2", ac_to_float_type(&ctx->ac, def_type), src[0]);
874bf215546Sopenharmony_ci      break;
875bf215546Sopenharmony_ci   case nir_op_flog2:
876bf215546Sopenharmony_ci      result =
877bf215546Sopenharmony_ci         emit_intrin_1f_param(&ctx->ac, "llvm.log2", ac_to_float_type(&ctx->ac, def_type), src[0]);
878bf215546Sopenharmony_ci      break;
879bf215546Sopenharmony_ci   case nir_op_frsq:
880bf215546Sopenharmony_ci      result = emit_intrin_1f_param_scalar(&ctx->ac, "llvm.amdgcn.rsq",
881bf215546Sopenharmony_ci                                           ac_to_float_type(&ctx->ac, def_type), src[0]);
882bf215546Sopenharmony_ci      if (ctx->abi->clamp_div_by_zero)
883bf215546Sopenharmony_ci         result = ac_build_fmin(&ctx->ac, result,
884bf215546Sopenharmony_ci                                LLVMConstReal(ac_to_float_type(&ctx->ac, def_type), FLT_MAX));
885bf215546Sopenharmony_ci      break;
886bf215546Sopenharmony_ci   case nir_op_frexp_exp:
887bf215546Sopenharmony_ci      src[0] = ac_to_float(&ctx->ac, src[0]);
888bf215546Sopenharmony_ci      result = ac_build_frexp_exp(&ctx->ac, src[0], ac_get_elem_bits(&ctx->ac, LLVMTypeOf(src[0])));
889bf215546Sopenharmony_ci      if (ac_get_elem_bits(&ctx->ac, LLVMTypeOf(src[0])) == 16)
890bf215546Sopenharmony_ci         result = LLVMBuildSExt(ctx->ac.builder, result, ctx->ac.i32, "");
891bf215546Sopenharmony_ci      break;
892bf215546Sopenharmony_ci   case nir_op_frexp_sig:
893bf215546Sopenharmony_ci      src[0] = ac_to_float(&ctx->ac, src[0]);
894bf215546Sopenharmony_ci      result = ac_build_frexp_mant(&ctx->ac, src[0], instr->dest.dest.ssa.bit_size);
895bf215546Sopenharmony_ci      break;
896bf215546Sopenharmony_ci   case nir_op_fpow:
897bf215546Sopenharmony_ci      if (instr->dest.dest.ssa.bit_size != 32) {
898bf215546Sopenharmony_ci         /* 16 and 64 bits */
899bf215546Sopenharmony_ci         result = emit_intrin_1f_param(&ctx->ac, "llvm.log2",
900bf215546Sopenharmony_ci                                       ac_to_float_type(&ctx->ac, def_type), src[0]);
901bf215546Sopenharmony_ci         result = LLVMBuildFMul(ctx->ac.builder, result, ac_to_float(&ctx->ac, src[1]), "");
902bf215546Sopenharmony_ci         result = emit_intrin_1f_param(&ctx->ac, "llvm.exp2",
903bf215546Sopenharmony_ci                                       ac_to_float_type(&ctx->ac, def_type), result);
904bf215546Sopenharmony_ci         break;
905bf215546Sopenharmony_ci      }
906bf215546Sopenharmony_ci      if (LLVM_VERSION_MAJOR >= 12) {
907bf215546Sopenharmony_ci         result = emit_intrin_1f_param(&ctx->ac, "llvm.log2",
908bf215546Sopenharmony_ci                                       ac_to_float_type(&ctx->ac, def_type), src[0]);
909bf215546Sopenharmony_ci         result = ac_build_intrinsic(&ctx->ac, "llvm.amdgcn.fmul.legacy", ctx->ac.f32,
910bf215546Sopenharmony_ci                                     (LLVMValueRef[]){result, ac_to_float(&ctx->ac, src[1])},
911bf215546Sopenharmony_ci                                     2, AC_FUNC_ATTR_READNONE);
912bf215546Sopenharmony_ci         result = emit_intrin_1f_param(&ctx->ac, "llvm.exp2",
913bf215546Sopenharmony_ci                                       ac_to_float_type(&ctx->ac, def_type), result);
914bf215546Sopenharmony_ci         break;
915bf215546Sopenharmony_ci      }
916bf215546Sopenharmony_ci      /* Older LLVM doesn't have fmul.legacy. */
917bf215546Sopenharmony_ci      result = emit_intrin_2f_param(&ctx->ac, "llvm.pow", ac_to_float_type(&ctx->ac, def_type),
918bf215546Sopenharmony_ci                                    src[0], src[1]);
919bf215546Sopenharmony_ci      break;
920bf215546Sopenharmony_ci   case nir_op_fmax:
921bf215546Sopenharmony_ci      result = emit_intrin_2f_param(&ctx->ac, "llvm.maxnum", ac_to_float_type(&ctx->ac, def_type),
922bf215546Sopenharmony_ci                                    src[0], src[1]);
923bf215546Sopenharmony_ci      if (ctx->ac.gfx_level < GFX9 && instr->dest.dest.ssa.bit_size == 32) {
924bf215546Sopenharmony_ci         /* Only pre-GFX9 chips do not flush denorms. */
925bf215546Sopenharmony_ci         result = ac_build_canonicalize(&ctx->ac, result, instr->dest.dest.ssa.bit_size);
926bf215546Sopenharmony_ci      }
927bf215546Sopenharmony_ci      break;
928bf215546Sopenharmony_ci   case nir_op_fmin:
929bf215546Sopenharmony_ci      result = emit_intrin_2f_param(&ctx->ac, "llvm.minnum", ac_to_float_type(&ctx->ac, def_type),
930bf215546Sopenharmony_ci                                    src[0], src[1]);
931bf215546Sopenharmony_ci      if (ctx->ac.gfx_level < GFX9 && instr->dest.dest.ssa.bit_size == 32) {
932bf215546Sopenharmony_ci         /* Only pre-GFX9 chips do not flush denorms. */
933bf215546Sopenharmony_ci         result = ac_build_canonicalize(&ctx->ac, result, instr->dest.dest.ssa.bit_size);
934bf215546Sopenharmony_ci      }
935bf215546Sopenharmony_ci      break;
936bf215546Sopenharmony_ci   case nir_op_ffma:
937bf215546Sopenharmony_ci      /* FMA is slow on gfx6-8, so it shouldn't be used. */
938bf215546Sopenharmony_ci      assert(instr->dest.dest.ssa.bit_size != 32 || ctx->ac.gfx_level >= GFX9);
939bf215546Sopenharmony_ci      result = emit_intrin_3f_param(&ctx->ac, "llvm.fma", ac_to_float_type(&ctx->ac, def_type),
940bf215546Sopenharmony_ci                                    src[0], src[1], src[2]);
941bf215546Sopenharmony_ci      break;
942bf215546Sopenharmony_ci   case nir_op_ffmaz:
943bf215546Sopenharmony_ci      assert(LLVM_VERSION_MAJOR >= 12 && ctx->ac.gfx_level >= GFX10_3);
944bf215546Sopenharmony_ci      src[0] = ac_to_float(&ctx->ac, src[0]);
945bf215546Sopenharmony_ci      src[1] = ac_to_float(&ctx->ac, src[1]);
946bf215546Sopenharmony_ci      src[2] = ac_to_float(&ctx->ac, src[2]);
947bf215546Sopenharmony_ci      result = ac_build_intrinsic(&ctx->ac, "llvm.amdgcn.fma.legacy", ctx->ac.f32,
948bf215546Sopenharmony_ci                                  src, 3, AC_FUNC_ATTR_READNONE);
949bf215546Sopenharmony_ci      break;
950bf215546Sopenharmony_ci   case nir_op_ldexp:
951bf215546Sopenharmony_ci      src[0] = ac_to_float(&ctx->ac, src[0]);
952bf215546Sopenharmony_ci      if (ac_get_elem_bits(&ctx->ac, def_type) == 32)
953bf215546Sopenharmony_ci         result = ac_build_intrinsic(&ctx->ac, "llvm.amdgcn.ldexp.f32", ctx->ac.f32, src, 2,
954bf215546Sopenharmony_ci                                     AC_FUNC_ATTR_READNONE);
955bf215546Sopenharmony_ci      else if (ac_get_elem_bits(&ctx->ac, def_type) == 16)
956bf215546Sopenharmony_ci         result = ac_build_intrinsic(&ctx->ac, "llvm.amdgcn.ldexp.f16", ctx->ac.f16, src, 2,
957bf215546Sopenharmony_ci                                     AC_FUNC_ATTR_READNONE);
958bf215546Sopenharmony_ci      else
959bf215546Sopenharmony_ci         result = ac_build_intrinsic(&ctx->ac, "llvm.amdgcn.ldexp.f64", ctx->ac.f64, src, 2,
960bf215546Sopenharmony_ci                                     AC_FUNC_ATTR_READNONE);
961bf215546Sopenharmony_ci      break;
962bf215546Sopenharmony_ci   case nir_op_bfm:
963bf215546Sopenharmony_ci      result = emit_bfm(&ctx->ac, src[0], src[1]);
964bf215546Sopenharmony_ci      break;
965bf215546Sopenharmony_ci   case nir_op_bitfield_select:
966bf215546Sopenharmony_ci      result = emit_bitfield_select(&ctx->ac, src[0], src[1], src[2]);
967bf215546Sopenharmony_ci      break;
968bf215546Sopenharmony_ci   case nir_op_ubfe:
969bf215546Sopenharmony_ci      result = ac_build_bfe(&ctx->ac, src[0], src[1], src[2], false);
970bf215546Sopenharmony_ci      break;
971bf215546Sopenharmony_ci   case nir_op_ibfe:
972bf215546Sopenharmony_ci      result = ac_build_bfe(&ctx->ac, src[0], src[1], src[2], true);
973bf215546Sopenharmony_ci      break;
974bf215546Sopenharmony_ci   case nir_op_bitfield_reverse:
975bf215546Sopenharmony_ci      result = ac_build_bitfield_reverse(&ctx->ac, src[0]);
976bf215546Sopenharmony_ci      break;
977bf215546Sopenharmony_ci   case nir_op_bit_count:
978bf215546Sopenharmony_ci      result = ac_build_bit_count(&ctx->ac, src[0]);
979bf215546Sopenharmony_ci      break;
980bf215546Sopenharmony_ci   case nir_op_vec2:
981bf215546Sopenharmony_ci   case nir_op_vec3:
982bf215546Sopenharmony_ci   case nir_op_vec4:
983bf215546Sopenharmony_ci   case nir_op_vec5:
984bf215546Sopenharmony_ci   case nir_op_vec8:
985bf215546Sopenharmony_ci   case nir_op_vec16:
986bf215546Sopenharmony_ci      for (unsigned i = 0; i < nir_op_infos[instr->op].num_inputs; i++)
987bf215546Sopenharmony_ci         src[i] = ac_to_integer(&ctx->ac, src[i]);
988bf215546Sopenharmony_ci      result = ac_build_gather_values(&ctx->ac, src, num_components);
989bf215546Sopenharmony_ci      break;
990bf215546Sopenharmony_ci   case nir_op_f2i8:
991bf215546Sopenharmony_ci   case nir_op_f2i16:
992bf215546Sopenharmony_ci   case nir_op_f2imp:
993bf215546Sopenharmony_ci   case nir_op_f2i32:
994bf215546Sopenharmony_ci   case nir_op_f2i64:
995bf215546Sopenharmony_ci      src[0] = ac_to_float(&ctx->ac, src[0]);
996bf215546Sopenharmony_ci      result = LLVMBuildFPToSI(ctx->ac.builder, src[0], def_type, "");
997bf215546Sopenharmony_ci      break;
998bf215546Sopenharmony_ci   case nir_op_f2u8:
999bf215546Sopenharmony_ci   case nir_op_f2u16:
1000bf215546Sopenharmony_ci   case nir_op_f2ump:
1001bf215546Sopenharmony_ci   case nir_op_f2u32:
1002bf215546Sopenharmony_ci   case nir_op_f2u64:
1003bf215546Sopenharmony_ci      src[0] = ac_to_float(&ctx->ac, src[0]);
1004bf215546Sopenharmony_ci      result = LLVMBuildFPToUI(ctx->ac.builder, src[0], def_type, "");
1005bf215546Sopenharmony_ci      break;
1006bf215546Sopenharmony_ci   case nir_op_i2f16:
1007bf215546Sopenharmony_ci   case nir_op_i2fmp:
1008bf215546Sopenharmony_ci   case nir_op_i2f32:
1009bf215546Sopenharmony_ci   case nir_op_i2f64:
1010bf215546Sopenharmony_ci      result = LLVMBuildSIToFP(ctx->ac.builder, src[0], ac_to_float_type(&ctx->ac, def_type), "");
1011bf215546Sopenharmony_ci      break;
1012bf215546Sopenharmony_ci   case nir_op_u2f16:
1013bf215546Sopenharmony_ci   case nir_op_u2fmp:
1014bf215546Sopenharmony_ci   case nir_op_u2f32:
1015bf215546Sopenharmony_ci   case nir_op_u2f64:
1016bf215546Sopenharmony_ci      result = LLVMBuildUIToFP(ctx->ac.builder, src[0], ac_to_float_type(&ctx->ac, def_type), "");
1017bf215546Sopenharmony_ci      break;
1018bf215546Sopenharmony_ci   case nir_op_f2f16_rtz:
1019bf215546Sopenharmony_ci   case nir_op_f2f16:
1020bf215546Sopenharmony_ci   case nir_op_f2fmp:
1021bf215546Sopenharmony_ci      src[0] = ac_to_float(&ctx->ac, src[0]);
1022bf215546Sopenharmony_ci
1023bf215546Sopenharmony_ci      /* For OpenGL, we want fast packing with v_cvt_pkrtz_f16, but if we use it,
1024bf215546Sopenharmony_ci       * all f32->f16 conversions have to round towards zero, because both scalar
1025bf215546Sopenharmony_ci       * and vec2 down-conversions have to round equally.
1026bf215546Sopenharmony_ci       */
1027bf215546Sopenharmony_ci      if (ctx->ac.float_mode == AC_FLOAT_MODE_DEFAULT_OPENGL || instr->op == nir_op_f2f16_rtz) {
1028bf215546Sopenharmony_ci         src[0] = ac_to_float(&ctx->ac, src[0]);
1029bf215546Sopenharmony_ci
1030bf215546Sopenharmony_ci         if (LLVMTypeOf(src[0]) == ctx->ac.f64)
1031bf215546Sopenharmony_ci            src[0] = LLVMBuildFPTrunc(ctx->ac.builder, src[0], ctx->ac.f32, "");
1032bf215546Sopenharmony_ci
1033bf215546Sopenharmony_ci         /* Fast path conversion. This only works if NIR is vectorized
1034bf215546Sopenharmony_ci          * to vec2 16.
1035bf215546Sopenharmony_ci          */
1036bf215546Sopenharmony_ci         if (LLVMTypeOf(src[0]) == ctx->ac.v2f32) {
1037bf215546Sopenharmony_ci            LLVMValueRef args[] = {
1038bf215546Sopenharmony_ci               ac_llvm_extract_elem(&ctx->ac, src[0], 0),
1039bf215546Sopenharmony_ci               ac_llvm_extract_elem(&ctx->ac, src[0], 1),
1040bf215546Sopenharmony_ci            };
1041bf215546Sopenharmony_ci            result = ac_build_cvt_pkrtz_f16(&ctx->ac, args);
1042bf215546Sopenharmony_ci            break;
1043bf215546Sopenharmony_ci         }
1044bf215546Sopenharmony_ci
1045bf215546Sopenharmony_ci         assert(ac_get_llvm_num_components(src[0]) == 1);
1046bf215546Sopenharmony_ci         LLVMValueRef param[2] = {src[0], LLVMGetUndef(ctx->ac.f32)};
1047bf215546Sopenharmony_ci         result = ac_build_cvt_pkrtz_f16(&ctx->ac, param);
1048bf215546Sopenharmony_ci         result = LLVMBuildExtractElement(ctx->ac.builder, result, ctx->ac.i32_0, "");
1049bf215546Sopenharmony_ci      } else {
1050bf215546Sopenharmony_ci         if (ac_get_elem_bits(&ctx->ac, LLVMTypeOf(src[0])) < ac_get_elem_bits(&ctx->ac, def_type))
1051bf215546Sopenharmony_ci            result =
1052bf215546Sopenharmony_ci               LLVMBuildFPExt(ctx->ac.builder, src[0], ac_to_float_type(&ctx->ac, def_type), "");
1053bf215546Sopenharmony_ci         else
1054bf215546Sopenharmony_ci            result =
1055bf215546Sopenharmony_ci               LLVMBuildFPTrunc(ctx->ac.builder, src[0], ac_to_float_type(&ctx->ac, def_type), "");
1056bf215546Sopenharmony_ci      }
1057bf215546Sopenharmony_ci      break;
1058bf215546Sopenharmony_ci   case nir_op_f2f16_rtne:
1059bf215546Sopenharmony_ci   case nir_op_f2f32:
1060bf215546Sopenharmony_ci   case nir_op_f2f64:
1061bf215546Sopenharmony_ci      src[0] = ac_to_float(&ctx->ac, src[0]);
1062bf215546Sopenharmony_ci      if (ac_get_elem_bits(&ctx->ac, LLVMTypeOf(src[0])) < ac_get_elem_bits(&ctx->ac, def_type))
1063bf215546Sopenharmony_ci         result = LLVMBuildFPExt(ctx->ac.builder, src[0], ac_to_float_type(&ctx->ac, def_type), "");
1064bf215546Sopenharmony_ci      else
1065bf215546Sopenharmony_ci         result =
1066bf215546Sopenharmony_ci            LLVMBuildFPTrunc(ctx->ac.builder, src[0], ac_to_float_type(&ctx->ac, def_type), "");
1067bf215546Sopenharmony_ci      break;
1068bf215546Sopenharmony_ci   case nir_op_u2u8:
1069bf215546Sopenharmony_ci   case nir_op_u2u16:
1070bf215546Sopenharmony_ci   case nir_op_u2u32:
1071bf215546Sopenharmony_ci   case nir_op_u2u64:
1072bf215546Sopenharmony_ci      if (ac_get_elem_bits(&ctx->ac, LLVMTypeOf(src[0])) < ac_get_elem_bits(&ctx->ac, def_type))
1073bf215546Sopenharmony_ci         result = LLVMBuildZExt(ctx->ac.builder, src[0], def_type, "");
1074bf215546Sopenharmony_ci      else
1075bf215546Sopenharmony_ci         result = LLVMBuildTrunc(ctx->ac.builder, src[0], def_type, "");
1076bf215546Sopenharmony_ci      break;
1077bf215546Sopenharmony_ci   case nir_op_i2i8:
1078bf215546Sopenharmony_ci   case nir_op_i2i16:
1079bf215546Sopenharmony_ci   case nir_op_i2imp:
1080bf215546Sopenharmony_ci   case nir_op_i2i32:
1081bf215546Sopenharmony_ci   case nir_op_i2i64:
1082bf215546Sopenharmony_ci      if (ac_get_elem_bits(&ctx->ac, LLVMTypeOf(src[0])) < ac_get_elem_bits(&ctx->ac, def_type))
1083bf215546Sopenharmony_ci         result = LLVMBuildSExt(ctx->ac.builder, src[0], def_type, "");
1084bf215546Sopenharmony_ci      else
1085bf215546Sopenharmony_ci         result = LLVMBuildTrunc(ctx->ac.builder, src[0], def_type, "");
1086bf215546Sopenharmony_ci      break;
1087bf215546Sopenharmony_ci   case nir_op_bcsel:
1088bf215546Sopenharmony_ci      result = emit_bcsel(&ctx->ac, src[0], src[1], src[2]);
1089bf215546Sopenharmony_ci      break;
1090bf215546Sopenharmony_ci   case nir_op_find_lsb:
1091bf215546Sopenharmony_ci      result = ac_find_lsb(&ctx->ac, ctx->ac.i32, src[0]);
1092bf215546Sopenharmony_ci      break;
1093bf215546Sopenharmony_ci   case nir_op_ufind_msb:
1094bf215546Sopenharmony_ci      result = ac_build_umsb(&ctx->ac, src[0], ctx->ac.i32);
1095bf215546Sopenharmony_ci      break;
1096bf215546Sopenharmony_ci   case nir_op_ifind_msb:
1097bf215546Sopenharmony_ci      result = ac_build_imsb(&ctx->ac, src[0], ctx->ac.i32);
1098bf215546Sopenharmony_ci      break;
1099bf215546Sopenharmony_ci  case nir_op_uclz: {
1100bf215546Sopenharmony_ci      LLVMValueRef params[2] = {
1101bf215546Sopenharmony_ci         src[0],
1102bf215546Sopenharmony_ci         ctx->ac.i1false,
1103bf215546Sopenharmony_ci      };
1104bf215546Sopenharmony_ci      result = ac_build_intrinsic(&ctx->ac, "llvm.ctlz.i32", ctx->ac.i32, params, 2, AC_FUNC_ATTR_READNONE);
1105bf215546Sopenharmony_ci      break;
1106bf215546Sopenharmony_ci  }
1107bf215546Sopenharmony_ci   case nir_op_uadd_carry:
1108bf215546Sopenharmony_ci      result = emit_uint_carry(&ctx->ac, "llvm.uadd.with.overflow.i32", src[0], src[1]);
1109bf215546Sopenharmony_ci      break;
1110bf215546Sopenharmony_ci   case nir_op_usub_borrow:
1111bf215546Sopenharmony_ci      result = emit_uint_carry(&ctx->ac, "llvm.usub.with.overflow.i32", src[0], src[1]);
1112bf215546Sopenharmony_ci      break;
1113bf215546Sopenharmony_ci   case nir_op_b2f16:
1114bf215546Sopenharmony_ci   case nir_op_b2f32:
1115bf215546Sopenharmony_ci   case nir_op_b2f64:
1116bf215546Sopenharmony_ci      result = emit_b2f(&ctx->ac, src[0], instr->dest.dest.ssa.bit_size);
1117bf215546Sopenharmony_ci      break;
1118bf215546Sopenharmony_ci   case nir_op_f2b1:
1119bf215546Sopenharmony_ci      result = emit_f2b(&ctx->ac, src[0]);
1120bf215546Sopenharmony_ci      break;
1121bf215546Sopenharmony_ci   case nir_op_b2i8:
1122bf215546Sopenharmony_ci   case nir_op_b2i16:
1123bf215546Sopenharmony_ci   case nir_op_b2i32:
1124bf215546Sopenharmony_ci   case nir_op_b2i64:
1125bf215546Sopenharmony_ci      result = emit_b2i(&ctx->ac, src[0], instr->dest.dest.ssa.bit_size);
1126bf215546Sopenharmony_ci      break;
1127bf215546Sopenharmony_ci   case nir_op_i2b1:
1128bf215546Sopenharmony_ci   case nir_op_b2b1: /* after loads */
1129bf215546Sopenharmony_ci      result = emit_i2b(&ctx->ac, src[0]);
1130bf215546Sopenharmony_ci      break;
1131bf215546Sopenharmony_ci   case nir_op_b2b16: /* before stores */
1132bf215546Sopenharmony_ci      result = LLVMBuildZExt(ctx->ac.builder, src[0], ctx->ac.i16, "");
1133bf215546Sopenharmony_ci      break;
1134bf215546Sopenharmony_ci   case nir_op_b2b32: /* before stores */
1135bf215546Sopenharmony_ci      result = LLVMBuildZExt(ctx->ac.builder, src[0], ctx->ac.i32, "");
1136bf215546Sopenharmony_ci      break;
1137bf215546Sopenharmony_ci   case nir_op_fquantize2f16:
1138bf215546Sopenharmony_ci      result = emit_f2f16(&ctx->ac, src[0]);
1139bf215546Sopenharmony_ci      break;
1140bf215546Sopenharmony_ci   case nir_op_umul_high:
1141bf215546Sopenharmony_ci      result = emit_umul_high(&ctx->ac, src[0], src[1]);
1142bf215546Sopenharmony_ci      break;
1143bf215546Sopenharmony_ci   case nir_op_imul_high:
1144bf215546Sopenharmony_ci      result = emit_imul_high(&ctx->ac, src[0], src[1]);
1145bf215546Sopenharmony_ci      break;
1146bf215546Sopenharmony_ci   case nir_op_pack_half_2x16:
1147bf215546Sopenharmony_ci      result = emit_pack_2x16(&ctx->ac, src[0], ac_build_cvt_pkrtz_f16);
1148bf215546Sopenharmony_ci      break;
1149bf215546Sopenharmony_ci   case nir_op_pack_half_2x16_split:
1150bf215546Sopenharmony_ci      src[0] = ac_to_float(&ctx->ac, src[0]);
1151bf215546Sopenharmony_ci      src[1] = ac_to_float(&ctx->ac, src[1]);
1152bf215546Sopenharmony_ci      result = LLVMBuildBitCast(ctx->ac.builder,
1153bf215546Sopenharmony_ci                                ac_build_cvt_pkrtz_f16(&ctx->ac, src),
1154bf215546Sopenharmony_ci                                ctx->ac.i32, "");
1155bf215546Sopenharmony_ci      break;
1156bf215546Sopenharmony_ci   case nir_op_pack_snorm_2x16:
1157bf215546Sopenharmony_ci      result = emit_pack_2x16(&ctx->ac, src[0], ac_build_cvt_pknorm_i16);
1158bf215546Sopenharmony_ci      break;
1159bf215546Sopenharmony_ci   case nir_op_pack_unorm_2x16:
1160bf215546Sopenharmony_ci      result = emit_pack_2x16(&ctx->ac, src[0], ac_build_cvt_pknorm_u16);
1161bf215546Sopenharmony_ci      break;
1162bf215546Sopenharmony_ci   case nir_op_pack_uint_2x16: {
1163bf215546Sopenharmony_ci      LLVMValueRef comp[2];
1164bf215546Sopenharmony_ci
1165bf215546Sopenharmony_ci      comp[0] = LLVMBuildExtractElement(ctx->ac.builder, src[0], ctx->ac.i32_0, "");
1166bf215546Sopenharmony_ci      comp[1] = LLVMBuildExtractElement(ctx->ac.builder, src[0], ctx->ac.i32_1, "");
1167bf215546Sopenharmony_ci
1168bf215546Sopenharmony_ci      result = ac_build_cvt_pk_u16(&ctx->ac, comp, 16, false);
1169bf215546Sopenharmony_ci      break;
1170bf215546Sopenharmony_ci   }
1171bf215546Sopenharmony_ci   case nir_op_pack_sint_2x16: {
1172bf215546Sopenharmony_ci      LLVMValueRef comp[2];
1173bf215546Sopenharmony_ci
1174bf215546Sopenharmony_ci      comp[0] = LLVMBuildExtractElement(ctx->ac.builder, src[0], ctx->ac.i32_0, "");
1175bf215546Sopenharmony_ci      comp[1] = LLVMBuildExtractElement(ctx->ac.builder, src[0], ctx->ac.i32_1, "");
1176bf215546Sopenharmony_ci
1177bf215546Sopenharmony_ci      result = ac_build_cvt_pk_i16(&ctx->ac, comp, 16, false);
1178bf215546Sopenharmony_ci      break;
1179bf215546Sopenharmony_ci   }
1180bf215546Sopenharmony_ci   case nir_op_unpack_half_2x16:
1181bf215546Sopenharmony_ci      result = emit_unpack_half_2x16(&ctx->ac, src[0]);
1182bf215546Sopenharmony_ci      break;
1183bf215546Sopenharmony_ci   case nir_op_unpack_half_2x16_split_x: {
1184bf215546Sopenharmony_ci      assert(ac_get_llvm_num_components(src[0]) == 1);
1185bf215546Sopenharmony_ci      LLVMValueRef tmp = emit_unpack_half_2x16(&ctx->ac, src[0]);
1186bf215546Sopenharmony_ci      result = LLVMBuildExtractElement(ctx->ac.builder, tmp, ctx->ac.i32_0, "");
1187bf215546Sopenharmony_ci      break;
1188bf215546Sopenharmony_ci   }
1189bf215546Sopenharmony_ci   case nir_op_unpack_half_2x16_split_y: {
1190bf215546Sopenharmony_ci      assert(ac_get_llvm_num_components(src[0]) == 1);
1191bf215546Sopenharmony_ci      LLVMValueRef tmp = emit_unpack_half_2x16(&ctx->ac, src[0]);
1192bf215546Sopenharmony_ci      result = LLVMBuildExtractElement(ctx->ac.builder, tmp, ctx->ac.i32_1, "");
1193bf215546Sopenharmony_ci      break;
1194bf215546Sopenharmony_ci   }
1195bf215546Sopenharmony_ci   case nir_op_fddx:
1196bf215546Sopenharmony_ci   case nir_op_fddy:
1197bf215546Sopenharmony_ci   case nir_op_fddx_fine:
1198bf215546Sopenharmony_ci   case nir_op_fddy_fine:
1199bf215546Sopenharmony_ci   case nir_op_fddx_coarse:
1200bf215546Sopenharmony_ci   case nir_op_fddy_coarse:
1201bf215546Sopenharmony_ci      result = emit_ddxy(ctx, instr->op, src[0]);
1202bf215546Sopenharmony_ci      break;
1203bf215546Sopenharmony_ci
1204bf215546Sopenharmony_ci   case nir_op_unpack_64_4x16: {
1205bf215546Sopenharmony_ci      result = LLVMBuildBitCast(ctx->ac.builder, src[0], ctx->ac.v4i16, "");
1206bf215546Sopenharmony_ci      break;
1207bf215546Sopenharmony_ci   }
1208bf215546Sopenharmony_ci   case nir_op_pack_64_4x16: {
1209bf215546Sopenharmony_ci      result = LLVMBuildBitCast(ctx->ac.builder, src[0], ctx->ac.i64, "");
1210bf215546Sopenharmony_ci      break;
1211bf215546Sopenharmony_ci   }
1212bf215546Sopenharmony_ci
1213bf215546Sopenharmony_ci   case nir_op_unpack_64_2x32: {
1214bf215546Sopenharmony_ci      result = LLVMBuildBitCast(ctx->ac.builder, src[0],
1215bf215546Sopenharmony_ci            ctx->ac.v2i32, "");
1216bf215546Sopenharmony_ci      break;
1217bf215546Sopenharmony_ci   }
1218bf215546Sopenharmony_ci   case nir_op_unpack_64_2x32_split_x: {
1219bf215546Sopenharmony_ci      assert(ac_get_llvm_num_components(src[0]) == 1);
1220bf215546Sopenharmony_ci      LLVMValueRef tmp = LLVMBuildBitCast(ctx->ac.builder, src[0], ctx->ac.v2i32, "");
1221bf215546Sopenharmony_ci      result = LLVMBuildExtractElement(ctx->ac.builder, tmp, ctx->ac.i32_0, "");
1222bf215546Sopenharmony_ci      break;
1223bf215546Sopenharmony_ci   }
1224bf215546Sopenharmony_ci   case nir_op_unpack_64_2x32_split_y: {
1225bf215546Sopenharmony_ci      assert(ac_get_llvm_num_components(src[0]) == 1);
1226bf215546Sopenharmony_ci      LLVMValueRef tmp = LLVMBuildBitCast(ctx->ac.builder, src[0], ctx->ac.v2i32, "");
1227bf215546Sopenharmony_ci      result = LLVMBuildExtractElement(ctx->ac.builder, tmp, ctx->ac.i32_1, "");
1228bf215546Sopenharmony_ci      break;
1229bf215546Sopenharmony_ci   }
1230bf215546Sopenharmony_ci
1231bf215546Sopenharmony_ci   case nir_op_pack_64_2x32: {
1232bf215546Sopenharmony_ci      result = LLVMBuildBitCast(ctx->ac.builder, src[0],
1233bf215546Sopenharmony_ci            ctx->ac.i64, "");
1234bf215546Sopenharmony_ci      break;
1235bf215546Sopenharmony_ci   }
1236bf215546Sopenharmony_ci   case nir_op_pack_64_2x32_split: {
1237bf215546Sopenharmony_ci      LLVMValueRef tmp = ac_build_gather_values(&ctx->ac, src, 2);
1238bf215546Sopenharmony_ci      result = LLVMBuildBitCast(ctx->ac.builder, tmp, ctx->ac.i64, "");
1239bf215546Sopenharmony_ci      break;
1240bf215546Sopenharmony_ci   }
1241bf215546Sopenharmony_ci
1242bf215546Sopenharmony_ci   case nir_op_pack_32_4x8:
1243bf215546Sopenharmony_ci   case nir_op_pack_32_2x16: {
1244bf215546Sopenharmony_ci      result = LLVMBuildBitCast(ctx->ac.builder, src[0],
1245bf215546Sopenharmony_ci            ctx->ac.i32, "");
1246bf215546Sopenharmony_ci      break;
1247bf215546Sopenharmony_ci   }
1248bf215546Sopenharmony_ci   case nir_op_pack_32_2x16_split: {
1249bf215546Sopenharmony_ci      LLVMValueRef tmp = ac_build_gather_values(&ctx->ac, src, 2);
1250bf215546Sopenharmony_ci      result = LLVMBuildBitCast(ctx->ac.builder, tmp, ctx->ac.i32, "");
1251bf215546Sopenharmony_ci      break;
1252bf215546Sopenharmony_ci   }
1253bf215546Sopenharmony_ci
1254bf215546Sopenharmony_ci   case nir_op_unpack_32_2x16: {
1255bf215546Sopenharmony_ci      result = LLVMBuildBitCast(ctx->ac.builder, src[0],
1256bf215546Sopenharmony_ci            ctx->ac.v2i16, "");
1257bf215546Sopenharmony_ci      break;
1258bf215546Sopenharmony_ci   }
1259bf215546Sopenharmony_ci   case nir_op_unpack_32_2x16_split_x: {
1260bf215546Sopenharmony_ci      LLVMValueRef tmp = LLVMBuildBitCast(ctx->ac.builder, src[0], ctx->ac.v2i16, "");
1261bf215546Sopenharmony_ci      result = LLVMBuildExtractElement(ctx->ac.builder, tmp, ctx->ac.i32_0, "");
1262bf215546Sopenharmony_ci      break;
1263bf215546Sopenharmony_ci   }
1264bf215546Sopenharmony_ci   case nir_op_unpack_32_2x16_split_y: {
1265bf215546Sopenharmony_ci      LLVMValueRef tmp = LLVMBuildBitCast(ctx->ac.builder, src[0], ctx->ac.v2i16, "");
1266bf215546Sopenharmony_ci      result = LLVMBuildExtractElement(ctx->ac.builder, tmp, ctx->ac.i32_1, "");
1267bf215546Sopenharmony_ci      break;
1268bf215546Sopenharmony_ci   }
1269bf215546Sopenharmony_ci
1270bf215546Sopenharmony_ci   case nir_op_cube_face_coord_amd: {
1271bf215546Sopenharmony_ci      src[0] = ac_to_float(&ctx->ac, src[0]);
1272bf215546Sopenharmony_ci      LLVMValueRef results[2];
1273bf215546Sopenharmony_ci      LLVMValueRef in[3];
1274bf215546Sopenharmony_ci      for (unsigned chan = 0; chan < 3; chan++)
1275bf215546Sopenharmony_ci         in[chan] = ac_llvm_extract_elem(&ctx->ac, src[0], chan);
1276bf215546Sopenharmony_ci      results[0] = ac_build_intrinsic(&ctx->ac, "llvm.amdgcn.cubesc", ctx->ac.f32, in, 3,
1277bf215546Sopenharmony_ci                                      AC_FUNC_ATTR_READNONE);
1278bf215546Sopenharmony_ci      results[1] = ac_build_intrinsic(&ctx->ac, "llvm.amdgcn.cubetc", ctx->ac.f32, in, 3,
1279bf215546Sopenharmony_ci                                      AC_FUNC_ATTR_READNONE);
1280bf215546Sopenharmony_ci      LLVMValueRef ma = ac_build_intrinsic(&ctx->ac, "llvm.amdgcn.cubema", ctx->ac.f32, in, 3,
1281bf215546Sopenharmony_ci                                           AC_FUNC_ATTR_READNONE);
1282bf215546Sopenharmony_ci      results[0] = ac_build_fdiv(&ctx->ac, results[0], ma);
1283bf215546Sopenharmony_ci      results[1] = ac_build_fdiv(&ctx->ac, results[1], ma);
1284bf215546Sopenharmony_ci      LLVMValueRef offset = LLVMConstReal(ctx->ac.f32, 0.5);
1285bf215546Sopenharmony_ci      results[0] = LLVMBuildFAdd(ctx->ac.builder, results[0], offset, "");
1286bf215546Sopenharmony_ci      results[1] = LLVMBuildFAdd(ctx->ac.builder, results[1], offset, "");
1287bf215546Sopenharmony_ci      result = ac_build_gather_values(&ctx->ac, results, 2);
1288bf215546Sopenharmony_ci      break;
1289bf215546Sopenharmony_ci   }
1290bf215546Sopenharmony_ci
1291bf215546Sopenharmony_ci   case nir_op_cube_face_index_amd: {
1292bf215546Sopenharmony_ci      src[0] = ac_to_float(&ctx->ac, src[0]);
1293bf215546Sopenharmony_ci      LLVMValueRef in[3];
1294bf215546Sopenharmony_ci      for (unsigned chan = 0; chan < 3; chan++)
1295bf215546Sopenharmony_ci         in[chan] = ac_llvm_extract_elem(&ctx->ac, src[0], chan);
1296bf215546Sopenharmony_ci      result = ac_build_intrinsic(&ctx->ac, "llvm.amdgcn.cubeid", ctx->ac.f32, in, 3,
1297bf215546Sopenharmony_ci                                  AC_FUNC_ATTR_READNONE);
1298bf215546Sopenharmony_ci      break;
1299bf215546Sopenharmony_ci   }
1300bf215546Sopenharmony_ci
1301bf215546Sopenharmony_ci   case nir_op_extract_u8:
1302bf215546Sopenharmony_ci   case nir_op_extract_i8:
1303bf215546Sopenharmony_ci   case nir_op_extract_u16:
1304bf215546Sopenharmony_ci   case nir_op_extract_i16: {
1305bf215546Sopenharmony_ci      bool is_signed = instr->op == nir_op_extract_i16 || instr->op == nir_op_extract_i8;
1306bf215546Sopenharmony_ci      unsigned size = instr->op == nir_op_extract_u8 || instr->op == nir_op_extract_i8 ? 8 : 16;
1307bf215546Sopenharmony_ci      LLVMValueRef offset = LLVMConstInt(LLVMTypeOf(src[0]), nir_src_as_uint(instr->src[1].src) * size, false);
1308bf215546Sopenharmony_ci      result = LLVMBuildLShr(ctx->ac.builder, src[0], offset, "");
1309bf215546Sopenharmony_ci      result = LLVMBuildTrunc(ctx->ac.builder, result, LLVMIntTypeInContext(ctx->ac.context, size), "");
1310bf215546Sopenharmony_ci      if (is_signed)
1311bf215546Sopenharmony_ci         result = LLVMBuildSExt(ctx->ac.builder, result, LLVMTypeOf(src[0]), "");
1312bf215546Sopenharmony_ci      else
1313bf215546Sopenharmony_ci         result = LLVMBuildZExt(ctx->ac.builder, result, LLVMTypeOf(src[0]), "");
1314bf215546Sopenharmony_ci      break;
1315bf215546Sopenharmony_ci   }
1316bf215546Sopenharmony_ci
1317bf215546Sopenharmony_ci   case nir_op_insert_u8:
1318bf215546Sopenharmony_ci   case nir_op_insert_u16: {
1319bf215546Sopenharmony_ci      unsigned size = instr->op == nir_op_insert_u8 ? 8 : 16;
1320bf215546Sopenharmony_ci      LLVMValueRef offset = LLVMConstInt(LLVMTypeOf(src[0]), nir_src_as_uint(instr->src[1].src) * size, false);
1321bf215546Sopenharmony_ci      LLVMValueRef mask = LLVMConstInt(LLVMTypeOf(src[0]), u_bit_consecutive(0, size), false);
1322bf215546Sopenharmony_ci      result = LLVMBuildShl(ctx->ac.builder, LLVMBuildAnd(ctx->ac.builder, src[0], mask, ""), offset, "");
1323bf215546Sopenharmony_ci      break;
1324bf215546Sopenharmony_ci   }
1325bf215546Sopenharmony_ci
1326bf215546Sopenharmony_ci   case nir_op_sdot_4x8_iadd:
1327bf215546Sopenharmony_ci   case nir_op_udot_4x8_uadd:
1328bf215546Sopenharmony_ci   case nir_op_sdot_4x8_iadd_sat:
1329bf215546Sopenharmony_ci   case nir_op_udot_4x8_uadd_sat: {
1330bf215546Sopenharmony_ci      const char *name = instr->op == nir_op_sdot_4x8_iadd ||
1331bf215546Sopenharmony_ci                         instr->op == nir_op_sdot_4x8_iadd_sat
1332bf215546Sopenharmony_ci                         ? "llvm.amdgcn.sdot4" : "llvm.amdgcn.udot4";
1333bf215546Sopenharmony_ci      src[3] = LLVMConstInt(ctx->ac.i1, instr->op == nir_op_sdot_4x8_iadd_sat ||
1334bf215546Sopenharmony_ci                                        instr->op == nir_op_udot_4x8_uadd_sat, false);
1335bf215546Sopenharmony_ci      result = ac_build_intrinsic(&ctx->ac, name, def_type, src, 4, AC_FUNC_ATTR_READNONE);
1336bf215546Sopenharmony_ci      break;
1337bf215546Sopenharmony_ci   }
1338bf215546Sopenharmony_ci
1339bf215546Sopenharmony_ci   case nir_op_sdot_2x16_iadd:
1340bf215546Sopenharmony_ci   case nir_op_udot_2x16_uadd:
1341bf215546Sopenharmony_ci   case nir_op_sdot_2x16_iadd_sat:
1342bf215546Sopenharmony_ci   case nir_op_udot_2x16_uadd_sat: {
1343bf215546Sopenharmony_ci      const char *name = instr->op == nir_op_sdot_2x16_iadd ||
1344bf215546Sopenharmony_ci                         instr->op == nir_op_sdot_2x16_iadd_sat
1345bf215546Sopenharmony_ci                         ? "llvm.amdgcn.sdot2" : "llvm.amdgcn.udot2";
1346bf215546Sopenharmony_ci      src[0] = LLVMBuildBitCast(ctx->ac.builder, src[0], ctx->ac.v2i16, "");
1347bf215546Sopenharmony_ci      src[1] = LLVMBuildBitCast(ctx->ac.builder, src[1], ctx->ac.v2i16, "");
1348bf215546Sopenharmony_ci      src[3] = LLVMConstInt(ctx->ac.i1, instr->op == nir_op_sdot_2x16_iadd_sat ||
1349bf215546Sopenharmony_ci                                        instr->op == nir_op_udot_2x16_uadd_sat, false);
1350bf215546Sopenharmony_ci      result = ac_build_intrinsic(&ctx->ac, name, def_type, src, 4, AC_FUNC_ATTR_READNONE);
1351bf215546Sopenharmony_ci      break;
1352bf215546Sopenharmony_ci   }
1353bf215546Sopenharmony_ci
1354bf215546Sopenharmony_ci   case nir_op_sad_u8x4:
1355bf215546Sopenharmony_ci      result = ac_build_intrinsic(&ctx->ac, "llvm.amdgcn.sad.u8", ctx->ac.i32,
1356bf215546Sopenharmony_ci                                  (LLVMValueRef[]){src[0], src[1], src[2]}, 3,
1357bf215546Sopenharmony_ci                                  AC_FUNC_ATTR_READNONE);
1358bf215546Sopenharmony_ci      break;
1359bf215546Sopenharmony_ci
1360bf215546Sopenharmony_ci   default:
1361bf215546Sopenharmony_ci      fprintf(stderr, "Unknown NIR alu instr: ");
1362bf215546Sopenharmony_ci      nir_print_instr(&instr->instr, stderr);
1363bf215546Sopenharmony_ci      fprintf(stderr, "\n");
1364bf215546Sopenharmony_ci      abort();
1365bf215546Sopenharmony_ci   }
1366bf215546Sopenharmony_ci
1367bf215546Sopenharmony_ci   if (result) {
1368bf215546Sopenharmony_ci      assert(instr->dest.dest.is_ssa);
1369bf215546Sopenharmony_ci      result = ac_to_integer_or_pointer(&ctx->ac, result);
1370bf215546Sopenharmony_ci      ctx->ssa_defs[instr->dest.dest.ssa.index] = result;
1371bf215546Sopenharmony_ci   }
1372bf215546Sopenharmony_ci}
1373bf215546Sopenharmony_ci
1374bf215546Sopenharmony_cistatic void visit_load_const(struct ac_nir_context *ctx, const nir_load_const_instr *instr)
1375bf215546Sopenharmony_ci{
1376bf215546Sopenharmony_ci   LLVMValueRef values[4], value = NULL;
1377bf215546Sopenharmony_ci   LLVMTypeRef element_type = LLVMIntTypeInContext(ctx->ac.context, instr->def.bit_size);
1378bf215546Sopenharmony_ci
1379bf215546Sopenharmony_ci   for (unsigned i = 0; i < instr->def.num_components; ++i) {
1380bf215546Sopenharmony_ci      switch (instr->def.bit_size) {
1381bf215546Sopenharmony_ci      case 1:
1382bf215546Sopenharmony_ci         values[i] = LLVMConstInt(element_type, instr->value[i].b, false);
1383bf215546Sopenharmony_ci         break;
1384bf215546Sopenharmony_ci      case 8:
1385bf215546Sopenharmony_ci         values[i] = LLVMConstInt(element_type, instr->value[i].u8, false);
1386bf215546Sopenharmony_ci         break;
1387bf215546Sopenharmony_ci      case 16:
1388bf215546Sopenharmony_ci         values[i] = LLVMConstInt(element_type, instr->value[i].u16, false);
1389bf215546Sopenharmony_ci         break;
1390bf215546Sopenharmony_ci      case 32:
1391bf215546Sopenharmony_ci         values[i] = LLVMConstInt(element_type, instr->value[i].u32, false);
1392bf215546Sopenharmony_ci         break;
1393bf215546Sopenharmony_ci      case 64:
1394bf215546Sopenharmony_ci         values[i] = LLVMConstInt(element_type, instr->value[i].u64, false);
1395bf215546Sopenharmony_ci         break;
1396bf215546Sopenharmony_ci      default:
1397bf215546Sopenharmony_ci         fprintf(stderr, "unsupported nir load_const bit_size: %d\n", instr->def.bit_size);
1398bf215546Sopenharmony_ci         abort();
1399bf215546Sopenharmony_ci      }
1400bf215546Sopenharmony_ci   }
1401bf215546Sopenharmony_ci   if (instr->def.num_components > 1) {
1402bf215546Sopenharmony_ci      value = LLVMConstVector(values, instr->def.num_components);
1403bf215546Sopenharmony_ci   } else
1404bf215546Sopenharmony_ci      value = values[0];
1405bf215546Sopenharmony_ci
1406bf215546Sopenharmony_ci   ctx->ssa_defs[instr->def.index] = value;
1407bf215546Sopenharmony_ci}
1408bf215546Sopenharmony_ci
1409bf215546Sopenharmony_cistatic LLVMValueRef get_buffer_size(struct ac_nir_context *ctx, LLVMValueRef descriptor,
1410bf215546Sopenharmony_ci                                    bool in_elements)
1411bf215546Sopenharmony_ci{
1412bf215546Sopenharmony_ci   LLVMValueRef size =
1413bf215546Sopenharmony_ci      LLVMBuildExtractElement(ctx->ac.builder, descriptor, LLVMConstInt(ctx->ac.i32, 2, false), "");
1414bf215546Sopenharmony_ci
1415bf215546Sopenharmony_ci   /* GFX8 only */
1416bf215546Sopenharmony_ci   if (ctx->ac.gfx_level == GFX8 && in_elements) {
1417bf215546Sopenharmony_ci      /* On GFX8, the descriptor contains the size in bytes,
1418bf215546Sopenharmony_ci       * but TXQ must return the size in elements.
1419bf215546Sopenharmony_ci       * The stride is always non-zero for resources using TXQ.
1420bf215546Sopenharmony_ci       */
1421bf215546Sopenharmony_ci      LLVMValueRef stride = LLVMBuildExtractElement(ctx->ac.builder, descriptor, ctx->ac.i32_1, "");
1422bf215546Sopenharmony_ci      stride = LLVMBuildLShr(ctx->ac.builder, stride, LLVMConstInt(ctx->ac.i32, 16, false), "");
1423bf215546Sopenharmony_ci      stride = LLVMBuildAnd(ctx->ac.builder, stride, LLVMConstInt(ctx->ac.i32, 0x3fff, false), "");
1424bf215546Sopenharmony_ci
1425bf215546Sopenharmony_ci      size = LLVMBuildUDiv(ctx->ac.builder, size, stride, "");
1426bf215546Sopenharmony_ci   }
1427bf215546Sopenharmony_ci   return size;
1428bf215546Sopenharmony_ci}
1429bf215546Sopenharmony_ci
1430bf215546Sopenharmony_ci/* Gather4 should follow the same rules as bilinear filtering, but the hardware
1431bf215546Sopenharmony_ci * incorrectly forces nearest filtering if the texture format is integer.
1432bf215546Sopenharmony_ci * The only effect it has on Gather4, which always returns 4 texels for
1433bf215546Sopenharmony_ci * bilinear filtering, is that the final coordinates are off by 0.5 of
1434bf215546Sopenharmony_ci * the texel size.
1435bf215546Sopenharmony_ci *
1436bf215546Sopenharmony_ci * The workaround is to subtract 0.5 from the unnormalized coordinates,
1437bf215546Sopenharmony_ci * or (0.5 / size) from the normalized coordinates.
1438bf215546Sopenharmony_ci *
1439bf215546Sopenharmony_ci * However, cube textures with 8_8_8_8 data formats require a different
1440bf215546Sopenharmony_ci * workaround of overriding the num format to USCALED/SSCALED. This would lose
1441bf215546Sopenharmony_ci * precision in 32-bit data formats, so it needs to be applied dynamically at
1442bf215546Sopenharmony_ci * runtime. In this case, return an i1 value that indicates whether the
1443bf215546Sopenharmony_ci * descriptor was overridden (and hence a fixup of the sampler result is needed).
1444bf215546Sopenharmony_ci */
1445bf215546Sopenharmony_cistatic LLVMValueRef lower_gather4_integer(struct ac_llvm_context *ctx, struct ac_image_args *args,
1446bf215546Sopenharmony_ci                                          const nir_tex_instr *instr)
1447bf215546Sopenharmony_ci{
1448bf215546Sopenharmony_ci   nir_alu_type stype = nir_alu_type_get_base_type(instr->dest_type);
1449bf215546Sopenharmony_ci   LLVMValueRef wa_8888 = NULL;
1450bf215546Sopenharmony_ci   LLVMValueRef half_texel[2];
1451bf215546Sopenharmony_ci   LLVMValueRef result;
1452bf215546Sopenharmony_ci
1453bf215546Sopenharmony_ci   assert(stype == nir_type_int || stype == nir_type_uint);
1454bf215546Sopenharmony_ci
1455bf215546Sopenharmony_ci   if (instr->sampler_dim == GLSL_SAMPLER_DIM_CUBE) {
1456bf215546Sopenharmony_ci      LLVMValueRef formats;
1457bf215546Sopenharmony_ci      LLVMValueRef data_format;
1458bf215546Sopenharmony_ci      LLVMValueRef wa_formats;
1459bf215546Sopenharmony_ci
1460bf215546Sopenharmony_ci      formats = LLVMBuildExtractElement(ctx->builder, args->resource, ctx->i32_1, "");
1461bf215546Sopenharmony_ci
1462bf215546Sopenharmony_ci      data_format = LLVMBuildLShr(ctx->builder, formats, LLVMConstInt(ctx->i32, 20, false), "");
1463bf215546Sopenharmony_ci      data_format =
1464bf215546Sopenharmony_ci         LLVMBuildAnd(ctx->builder, data_format, LLVMConstInt(ctx->i32, (1u << 6) - 1, false), "");
1465bf215546Sopenharmony_ci      wa_8888 = LLVMBuildICmp(ctx->builder, LLVMIntEQ, data_format,
1466bf215546Sopenharmony_ci                              LLVMConstInt(ctx->i32, V_008F14_IMG_DATA_FORMAT_8_8_8_8, false), "");
1467bf215546Sopenharmony_ci
1468bf215546Sopenharmony_ci      uint32_t wa_num_format = stype == nir_type_uint
1469bf215546Sopenharmony_ci                                  ? S_008F14_NUM_FORMAT(V_008F14_IMG_NUM_FORMAT_USCALED)
1470bf215546Sopenharmony_ci                                  : S_008F14_NUM_FORMAT(V_008F14_IMG_NUM_FORMAT_SSCALED);
1471bf215546Sopenharmony_ci      wa_formats = LLVMBuildAnd(ctx->builder, formats,
1472bf215546Sopenharmony_ci                                LLVMConstInt(ctx->i32, C_008F14_NUM_FORMAT, false), "");
1473bf215546Sopenharmony_ci      wa_formats =
1474bf215546Sopenharmony_ci         LLVMBuildOr(ctx->builder, wa_formats, LLVMConstInt(ctx->i32, wa_num_format, false), "");
1475bf215546Sopenharmony_ci
1476bf215546Sopenharmony_ci      formats = LLVMBuildSelect(ctx->builder, wa_8888, wa_formats, formats, "");
1477bf215546Sopenharmony_ci      args->resource =
1478bf215546Sopenharmony_ci         LLVMBuildInsertElement(ctx->builder, args->resource, formats, ctx->i32_1, "");
1479bf215546Sopenharmony_ci   }
1480bf215546Sopenharmony_ci
1481bf215546Sopenharmony_ci   if (instr->sampler_dim == GLSL_SAMPLER_DIM_RECT) {
1482bf215546Sopenharmony_ci      assert(!wa_8888);
1483bf215546Sopenharmony_ci      half_texel[0] = half_texel[1] = LLVMConstReal(ctx->f32, -0.5);
1484bf215546Sopenharmony_ci   } else {
1485bf215546Sopenharmony_ci      struct ac_image_args resinfo = {0};
1486bf215546Sopenharmony_ci      LLVMBasicBlockRef bbs[2];
1487bf215546Sopenharmony_ci
1488bf215546Sopenharmony_ci      LLVMValueRef unnorm = NULL;
1489bf215546Sopenharmony_ci      LLVMValueRef default_offset = ctx->f32_0;
1490bf215546Sopenharmony_ci      if (instr->sampler_dim == GLSL_SAMPLER_DIM_2D && !instr->is_array) {
1491bf215546Sopenharmony_ci         /* In vulkan, whether the sampler uses unnormalized
1492bf215546Sopenharmony_ci          * coordinates or not is a dynamic property of the
1493bf215546Sopenharmony_ci          * sampler. Hence, to figure out whether or not we
1494bf215546Sopenharmony_ci          * need to divide by the texture size, we need to test
1495bf215546Sopenharmony_ci          * the sampler at runtime. This tests the bit set by
1496bf215546Sopenharmony_ci          * radv_init_sampler().
1497bf215546Sopenharmony_ci          */
1498bf215546Sopenharmony_ci         LLVMValueRef sampler0 =
1499bf215546Sopenharmony_ci            LLVMBuildExtractElement(ctx->builder, args->sampler, ctx->i32_0, "");
1500bf215546Sopenharmony_ci         sampler0 = LLVMBuildLShr(ctx->builder, sampler0, LLVMConstInt(ctx->i32, 15, false), "");
1501bf215546Sopenharmony_ci         sampler0 = LLVMBuildAnd(ctx->builder, sampler0, ctx->i32_1, "");
1502bf215546Sopenharmony_ci         unnorm = LLVMBuildICmp(ctx->builder, LLVMIntEQ, sampler0, ctx->i32_1, "");
1503bf215546Sopenharmony_ci         default_offset = LLVMConstReal(ctx->f32, -0.5);
1504bf215546Sopenharmony_ci      }
1505bf215546Sopenharmony_ci
1506bf215546Sopenharmony_ci      bbs[0] = LLVMGetInsertBlock(ctx->builder);
1507bf215546Sopenharmony_ci      if (wa_8888 || unnorm) {
1508bf215546Sopenharmony_ci         assert(!(wa_8888 && unnorm));
1509bf215546Sopenharmony_ci         LLVMValueRef not_needed = wa_8888 ? wa_8888 : unnorm;
1510bf215546Sopenharmony_ci         /* Skip the texture size query entirely if we don't need it. */
1511bf215546Sopenharmony_ci         ac_build_ifcc(ctx, LLVMBuildNot(ctx->builder, not_needed, ""), 2000);
1512bf215546Sopenharmony_ci         bbs[1] = LLVMGetInsertBlock(ctx->builder);
1513bf215546Sopenharmony_ci      }
1514bf215546Sopenharmony_ci
1515bf215546Sopenharmony_ci      /* Query the texture size. */
1516bf215546Sopenharmony_ci      resinfo.dim = ac_get_sampler_dim(ctx->gfx_level, instr->sampler_dim, instr->is_array);
1517bf215546Sopenharmony_ci      resinfo.opcode = ac_image_get_resinfo;
1518bf215546Sopenharmony_ci      resinfo.dmask = 0xf;
1519bf215546Sopenharmony_ci      resinfo.lod = ctx->i32_0;
1520bf215546Sopenharmony_ci      resinfo.resource = args->resource;
1521bf215546Sopenharmony_ci      resinfo.attributes = AC_FUNC_ATTR_READNONE;
1522bf215546Sopenharmony_ci      LLVMValueRef size = ac_build_image_opcode(ctx, &resinfo);
1523bf215546Sopenharmony_ci
1524bf215546Sopenharmony_ci      /* Compute -0.5 / size. */
1525bf215546Sopenharmony_ci      for (unsigned c = 0; c < 2; c++) {
1526bf215546Sopenharmony_ci         half_texel[c] =
1527bf215546Sopenharmony_ci            LLVMBuildExtractElement(ctx->builder, size, LLVMConstInt(ctx->i32, c, 0), "");
1528bf215546Sopenharmony_ci         half_texel[c] = LLVMBuildUIToFP(ctx->builder, half_texel[c], ctx->f32, "");
1529bf215546Sopenharmony_ci         half_texel[c] = ac_build_fdiv(ctx, ctx->f32_1, half_texel[c]);
1530bf215546Sopenharmony_ci         half_texel[c] =
1531bf215546Sopenharmony_ci            LLVMBuildFMul(ctx->builder, half_texel[c], LLVMConstReal(ctx->f32, -0.5), "");
1532bf215546Sopenharmony_ci      }
1533bf215546Sopenharmony_ci
1534bf215546Sopenharmony_ci      if (wa_8888 || unnorm) {
1535bf215546Sopenharmony_ci         ac_build_endif(ctx, 2000);
1536bf215546Sopenharmony_ci
1537bf215546Sopenharmony_ci         for (unsigned c = 0; c < 2; c++) {
1538bf215546Sopenharmony_ci            LLVMValueRef values[2] = {default_offset, half_texel[c]};
1539bf215546Sopenharmony_ci            half_texel[c] = ac_build_phi(ctx, ctx->f32, 2, values, bbs);
1540bf215546Sopenharmony_ci         }
1541bf215546Sopenharmony_ci      }
1542bf215546Sopenharmony_ci   }
1543bf215546Sopenharmony_ci
1544bf215546Sopenharmony_ci   for (unsigned c = 0; c < 2; c++) {
1545bf215546Sopenharmony_ci      LLVMValueRef tmp;
1546bf215546Sopenharmony_ci      tmp = LLVMBuildBitCast(ctx->builder, args->coords[c], ctx->f32, "");
1547bf215546Sopenharmony_ci      args->coords[c] = LLVMBuildFAdd(ctx->builder, tmp, half_texel[c], "");
1548bf215546Sopenharmony_ci   }
1549bf215546Sopenharmony_ci
1550bf215546Sopenharmony_ci   args->attributes = AC_FUNC_ATTR_READNONE;
1551bf215546Sopenharmony_ci   result = ac_build_image_opcode(ctx, args);
1552bf215546Sopenharmony_ci
1553bf215546Sopenharmony_ci   if (instr->sampler_dim == GLSL_SAMPLER_DIM_CUBE) {
1554bf215546Sopenharmony_ci      LLVMValueRef tmp, tmp2;
1555bf215546Sopenharmony_ci
1556bf215546Sopenharmony_ci      /* if the cube workaround is in place, f2i the result. */
1557bf215546Sopenharmony_ci      for (unsigned c = 0; c < 4; c++) {
1558bf215546Sopenharmony_ci         tmp = LLVMBuildExtractElement(ctx->builder, result, LLVMConstInt(ctx->i32, c, false), "");
1559bf215546Sopenharmony_ci         if (stype == nir_type_uint)
1560bf215546Sopenharmony_ci            tmp2 = LLVMBuildFPToUI(ctx->builder, tmp, ctx->i32, "");
1561bf215546Sopenharmony_ci         else
1562bf215546Sopenharmony_ci            tmp2 = LLVMBuildFPToSI(ctx->builder, tmp, ctx->i32, "");
1563bf215546Sopenharmony_ci         tmp = LLVMBuildBitCast(ctx->builder, tmp, ctx->i32, "");
1564bf215546Sopenharmony_ci         tmp2 = LLVMBuildBitCast(ctx->builder, tmp2, ctx->i32, "");
1565bf215546Sopenharmony_ci         tmp = LLVMBuildSelect(ctx->builder, wa_8888, tmp2, tmp, "");
1566bf215546Sopenharmony_ci         tmp = LLVMBuildBitCast(ctx->builder, tmp, ctx->f32, "");
1567bf215546Sopenharmony_ci         result =
1568bf215546Sopenharmony_ci            LLVMBuildInsertElement(ctx->builder, result, tmp, LLVMConstInt(ctx->i32, c, false), "");
1569bf215546Sopenharmony_ci      }
1570bf215546Sopenharmony_ci   }
1571bf215546Sopenharmony_ci   return result;
1572bf215546Sopenharmony_ci}
1573bf215546Sopenharmony_ci
1574bf215546Sopenharmony_cistatic LLVMValueRef build_tex_intrinsic(struct ac_nir_context *ctx, const nir_tex_instr *instr,
1575bf215546Sopenharmony_ci                                        struct ac_image_args *args)
1576bf215546Sopenharmony_ci{
1577bf215546Sopenharmony_ci   assert((!args->tfe || !args->d16) && "unsupported");
1578bf215546Sopenharmony_ci
1579bf215546Sopenharmony_ci   if (instr->sampler_dim == GLSL_SAMPLER_DIM_BUF) {
1580bf215546Sopenharmony_ci      unsigned mask = nir_ssa_def_components_read(&instr->dest.ssa);
1581bf215546Sopenharmony_ci
1582bf215546Sopenharmony_ci      assert(instr->dest.is_ssa);
1583bf215546Sopenharmony_ci
1584bf215546Sopenharmony_ci      /* Buffers don't support A16. */
1585bf215546Sopenharmony_ci      if (args->a16)
1586bf215546Sopenharmony_ci         args->coords[0] = LLVMBuildZExt(ctx->ac.builder, args->coords[0], ctx->ac.i32, "");
1587bf215546Sopenharmony_ci
1588bf215546Sopenharmony_ci      return ac_build_buffer_load_format(&ctx->ac, args->resource, args->coords[0], ctx->ac.i32_0,
1589bf215546Sopenharmony_ci                                         util_last_bit(mask), 0, true,
1590bf215546Sopenharmony_ci                                         instr->dest.ssa.bit_size == 16,
1591bf215546Sopenharmony_ci                                         args->tfe);
1592bf215546Sopenharmony_ci   }
1593bf215546Sopenharmony_ci
1594bf215546Sopenharmony_ci   args->opcode = ac_image_sample;
1595bf215546Sopenharmony_ci
1596bf215546Sopenharmony_ci   switch (instr->op) {
1597bf215546Sopenharmony_ci   case nir_texop_txf:
1598bf215546Sopenharmony_ci   case nir_texop_txf_ms:
1599bf215546Sopenharmony_ci   case nir_texop_samples_identical:
1600bf215546Sopenharmony_ci      args->opcode = args->level_zero || instr->sampler_dim == GLSL_SAMPLER_DIM_MS
1601bf215546Sopenharmony_ci                        ? ac_image_load
1602bf215546Sopenharmony_ci                        : ac_image_load_mip;
1603bf215546Sopenharmony_ci      args->level_zero = false;
1604bf215546Sopenharmony_ci      break;
1605bf215546Sopenharmony_ci   case nir_texop_txs:
1606bf215546Sopenharmony_ci   case nir_texop_query_levels:
1607bf215546Sopenharmony_ci      args->opcode = ac_image_get_resinfo;
1608bf215546Sopenharmony_ci      if (!args->lod)
1609bf215546Sopenharmony_ci         args->lod = ctx->ac.i32_0;
1610bf215546Sopenharmony_ci      args->level_zero = false;
1611bf215546Sopenharmony_ci      break;
1612bf215546Sopenharmony_ci   case nir_texop_tex:
1613bf215546Sopenharmony_ci      if (ctx->stage != MESA_SHADER_FRAGMENT &&
1614bf215546Sopenharmony_ci          (ctx->stage != MESA_SHADER_COMPUTE ||
1615bf215546Sopenharmony_ci           ctx->info->cs.derivative_group == DERIVATIVE_GROUP_NONE)) {
1616bf215546Sopenharmony_ci         assert(!args->lod);
1617bf215546Sopenharmony_ci         args->level_zero = true;
1618bf215546Sopenharmony_ci      }
1619bf215546Sopenharmony_ci      break;
1620bf215546Sopenharmony_ci   case nir_texop_tg4:
1621bf215546Sopenharmony_ci      args->opcode = ac_image_gather4;
1622bf215546Sopenharmony_ci      if (!args->lod && !args->bias)
1623bf215546Sopenharmony_ci         args->level_zero = true;
1624bf215546Sopenharmony_ci      break;
1625bf215546Sopenharmony_ci   case nir_texop_lod:
1626bf215546Sopenharmony_ci      args->opcode = ac_image_get_lod;
1627bf215546Sopenharmony_ci      break;
1628bf215546Sopenharmony_ci   case nir_texop_fragment_fetch_amd:
1629bf215546Sopenharmony_ci   case nir_texop_fragment_mask_fetch_amd:
1630bf215546Sopenharmony_ci      args->opcode = ac_image_load;
1631bf215546Sopenharmony_ci      args->level_zero = false;
1632bf215546Sopenharmony_ci      break;
1633bf215546Sopenharmony_ci   default:
1634bf215546Sopenharmony_ci      break;
1635bf215546Sopenharmony_ci   }
1636bf215546Sopenharmony_ci
1637bf215546Sopenharmony_ci   /* Aldebaran doesn't have image_sample_lz, but image_sample behaves like lz. */
1638bf215546Sopenharmony_ci   if (!ctx->ac.has_3d_cube_border_color_mipmap)
1639bf215546Sopenharmony_ci      args->level_zero = false;
1640bf215546Sopenharmony_ci
1641bf215546Sopenharmony_ci   if (instr->op == nir_texop_tg4 && ctx->ac.gfx_level <= GFX8 &&
1642bf215546Sopenharmony_ci       (instr->dest_type & (nir_type_int | nir_type_uint))) {
1643bf215546Sopenharmony_ci      return lower_gather4_integer(&ctx->ac, args, instr);
1644bf215546Sopenharmony_ci   }
1645bf215546Sopenharmony_ci
1646bf215546Sopenharmony_ci   /* Fixup for GFX9 which allocates 1D textures as 2D. */
1647bf215546Sopenharmony_ci   if (instr->op == nir_texop_lod && ctx->ac.gfx_level == GFX9) {
1648bf215546Sopenharmony_ci      if ((args->dim == ac_image_2darray || args->dim == ac_image_2d) && !args->coords[1]) {
1649bf215546Sopenharmony_ci         args->coords[1] = ctx->ac.i32_0;
1650bf215546Sopenharmony_ci      }
1651bf215546Sopenharmony_ci   }
1652bf215546Sopenharmony_ci
1653bf215546Sopenharmony_ci   args->attributes = AC_FUNC_ATTR_READNONE;
1654bf215546Sopenharmony_ci   bool cs_derivs =
1655bf215546Sopenharmony_ci      ctx->stage == MESA_SHADER_COMPUTE && ctx->info->cs.derivative_group != DERIVATIVE_GROUP_NONE;
1656bf215546Sopenharmony_ci   if (ctx->stage == MESA_SHADER_FRAGMENT || cs_derivs) {
1657bf215546Sopenharmony_ci      /* Prevent texture instructions with implicit derivatives from being
1658bf215546Sopenharmony_ci       * sinked into branches. */
1659bf215546Sopenharmony_ci      switch (instr->op) {
1660bf215546Sopenharmony_ci      case nir_texop_tex:
1661bf215546Sopenharmony_ci      case nir_texop_txb:
1662bf215546Sopenharmony_ci      case nir_texop_lod:
1663bf215546Sopenharmony_ci         args->attributes |= AC_FUNC_ATTR_CONVERGENT;
1664bf215546Sopenharmony_ci         break;
1665bf215546Sopenharmony_ci      default:
1666bf215546Sopenharmony_ci         break;
1667bf215546Sopenharmony_ci      }
1668bf215546Sopenharmony_ci   }
1669bf215546Sopenharmony_ci
1670bf215546Sopenharmony_ci   return ac_build_image_opcode(&ctx->ac, args);
1671bf215546Sopenharmony_ci}
1672bf215546Sopenharmony_ci
1673bf215546Sopenharmony_cistatic LLVMValueRef visit_load_push_constant(struct ac_nir_context *ctx, nir_intrinsic_instr *instr)
1674bf215546Sopenharmony_ci{
1675bf215546Sopenharmony_ci   LLVMValueRef ptr, addr;
1676bf215546Sopenharmony_ci   LLVMValueRef src0 = get_src(ctx, instr->src[0]);
1677bf215546Sopenharmony_ci   unsigned index = nir_intrinsic_base(instr);
1678bf215546Sopenharmony_ci
1679bf215546Sopenharmony_ci   addr = LLVMConstInt(ctx->ac.i32, index, 0);
1680bf215546Sopenharmony_ci   addr = LLVMBuildAdd(ctx->ac.builder, addr, src0, "");
1681bf215546Sopenharmony_ci
1682bf215546Sopenharmony_ci   /* Load constant values from user SGPRS when possible, otherwise
1683bf215546Sopenharmony_ci    * fallback to the default path that loads directly from memory.
1684bf215546Sopenharmony_ci    */
1685bf215546Sopenharmony_ci   if (LLVMIsConstant(src0) && instr->dest.ssa.bit_size >= 32) {
1686bf215546Sopenharmony_ci      unsigned count = instr->dest.ssa.num_components;
1687bf215546Sopenharmony_ci      unsigned offset = index;
1688bf215546Sopenharmony_ci
1689bf215546Sopenharmony_ci      if (instr->dest.ssa.bit_size == 64)
1690bf215546Sopenharmony_ci         count *= 2;
1691bf215546Sopenharmony_ci
1692bf215546Sopenharmony_ci      offset += LLVMConstIntGetZExtValue(src0);
1693bf215546Sopenharmony_ci      offset /= 4;
1694bf215546Sopenharmony_ci
1695bf215546Sopenharmony_ci      uint64_t mask = BITFIELD64_MASK(count) << offset;
1696bf215546Sopenharmony_ci      if ((ctx->args->inline_push_const_mask | mask) == ctx->args->inline_push_const_mask &&
1697bf215546Sopenharmony_ci          offset + count <= (sizeof(ctx->args->inline_push_const_mask) * 8u)) {
1698bf215546Sopenharmony_ci         LLVMValueRef *const push_constants = alloca(count * sizeof(LLVMValueRef));
1699bf215546Sopenharmony_ci         unsigned arg_index =
1700bf215546Sopenharmony_ci            util_bitcount64(ctx->args->inline_push_const_mask & BITFIELD64_MASK(offset));
1701bf215546Sopenharmony_ci         for (unsigned i = 0; i < count; i++)
1702bf215546Sopenharmony_ci            push_constants[i] = ac_get_arg(&ctx->ac, ctx->args->inline_push_consts[arg_index++]);
1703bf215546Sopenharmony_ci         LLVMValueRef res = ac_build_gather_values(&ctx->ac, push_constants, count);
1704bf215546Sopenharmony_ci         return instr->dest.ssa.bit_size == 64
1705bf215546Sopenharmony_ci                   ? LLVMBuildBitCast(ctx->ac.builder, res, get_def_type(ctx, &instr->dest.ssa), "")
1706bf215546Sopenharmony_ci                   : res;
1707bf215546Sopenharmony_ci      }
1708bf215546Sopenharmony_ci   }
1709bf215546Sopenharmony_ci
1710bf215546Sopenharmony_ci   ptr = LLVMBuildGEP(ctx->ac.builder, ac_get_arg(&ctx->ac, ctx->args->push_constants), &addr, 1, "");
1711bf215546Sopenharmony_ci
1712bf215546Sopenharmony_ci   if (instr->dest.ssa.bit_size == 8) {
1713bf215546Sopenharmony_ci      unsigned load_dwords = instr->dest.ssa.num_components > 1 ? 2 : 1;
1714bf215546Sopenharmony_ci      LLVMTypeRef vec_type = LLVMVectorType(ctx->ac.i8, 4 * load_dwords);
1715bf215546Sopenharmony_ci      ptr = ac_cast_ptr(&ctx->ac, ptr, vec_type);
1716bf215546Sopenharmony_ci      LLVMValueRef res = LLVMBuildLoad2(ctx->ac.builder, vec_type, ptr, "");
1717bf215546Sopenharmony_ci
1718bf215546Sopenharmony_ci      LLVMValueRef params[3];
1719bf215546Sopenharmony_ci      if (load_dwords > 1) {
1720bf215546Sopenharmony_ci         LLVMValueRef res_vec = LLVMBuildBitCast(ctx->ac.builder, res, ctx->ac.v2i32, "");
1721bf215546Sopenharmony_ci         params[0] = LLVMBuildExtractElement(ctx->ac.builder, res_vec,
1722bf215546Sopenharmony_ci                                             LLVMConstInt(ctx->ac.i32, 1, false), "");
1723bf215546Sopenharmony_ci         params[1] = LLVMBuildExtractElement(ctx->ac.builder, res_vec,
1724bf215546Sopenharmony_ci                                             LLVMConstInt(ctx->ac.i32, 0, false), "");
1725bf215546Sopenharmony_ci      } else {
1726bf215546Sopenharmony_ci         res = LLVMBuildBitCast(ctx->ac.builder, res, ctx->ac.i32, "");
1727bf215546Sopenharmony_ci         params[0] = ctx->ac.i32_0;
1728bf215546Sopenharmony_ci         params[1] = res;
1729bf215546Sopenharmony_ci      }
1730bf215546Sopenharmony_ci      params[2] = addr;
1731bf215546Sopenharmony_ci      res = ac_build_intrinsic(&ctx->ac, "llvm.amdgcn.alignbyte", ctx->ac.i32, params, 3, 0);
1732bf215546Sopenharmony_ci
1733bf215546Sopenharmony_ci      res = LLVMBuildTrunc(
1734bf215546Sopenharmony_ci         ctx->ac.builder, res,
1735bf215546Sopenharmony_ci         LLVMIntTypeInContext(ctx->ac.context, instr->dest.ssa.num_components * 8), "");
1736bf215546Sopenharmony_ci      if (instr->dest.ssa.num_components > 1)
1737bf215546Sopenharmony_ci         res = LLVMBuildBitCast(ctx->ac.builder, res,
1738bf215546Sopenharmony_ci                                LLVMVectorType(ctx->ac.i8, instr->dest.ssa.num_components), "");
1739bf215546Sopenharmony_ci      return res;
1740bf215546Sopenharmony_ci   } else if (instr->dest.ssa.bit_size == 16) {
1741bf215546Sopenharmony_ci      unsigned load_dwords = instr->dest.ssa.num_components / 2 + 1;
1742bf215546Sopenharmony_ci      LLVMTypeRef vec_type = LLVMVectorType(ctx->ac.i16, 2 * load_dwords);
1743bf215546Sopenharmony_ci      ptr = ac_cast_ptr(&ctx->ac, ptr, vec_type);
1744bf215546Sopenharmony_ci      LLVMValueRef res = LLVMBuildLoad2(ctx->ac.builder, vec_type, ptr, "");
1745bf215546Sopenharmony_ci      res = LLVMBuildBitCast(ctx->ac.builder, res, vec_type, "");
1746bf215546Sopenharmony_ci      LLVMValueRef cond = LLVMBuildLShr(ctx->ac.builder, addr, ctx->ac.i32_1, "");
1747bf215546Sopenharmony_ci      cond = LLVMBuildTrunc(ctx->ac.builder, cond, ctx->ac.i1, "");
1748bf215546Sopenharmony_ci      LLVMValueRef mask[] = {
1749bf215546Sopenharmony_ci         LLVMConstInt(ctx->ac.i32, 0, false), LLVMConstInt(ctx->ac.i32, 1, false),
1750bf215546Sopenharmony_ci         LLVMConstInt(ctx->ac.i32, 2, false), LLVMConstInt(ctx->ac.i32, 3, false),
1751bf215546Sopenharmony_ci         LLVMConstInt(ctx->ac.i32, 4, false)};
1752bf215546Sopenharmony_ci      LLVMValueRef swizzle_aligned = LLVMConstVector(&mask[0], instr->dest.ssa.num_components);
1753bf215546Sopenharmony_ci      LLVMValueRef swizzle_unaligned = LLVMConstVector(&mask[1], instr->dest.ssa.num_components);
1754bf215546Sopenharmony_ci      LLVMValueRef shuffle_aligned =
1755bf215546Sopenharmony_ci         LLVMBuildShuffleVector(ctx->ac.builder, res, res, swizzle_aligned, "");
1756bf215546Sopenharmony_ci      LLVMValueRef shuffle_unaligned =
1757bf215546Sopenharmony_ci         LLVMBuildShuffleVector(ctx->ac.builder, res, res, swizzle_unaligned, "");
1758bf215546Sopenharmony_ci      res = LLVMBuildSelect(ctx->ac.builder, cond, shuffle_unaligned, shuffle_aligned, "");
1759bf215546Sopenharmony_ci      return LLVMBuildBitCast(ctx->ac.builder, res, get_def_type(ctx, &instr->dest.ssa), "");
1760bf215546Sopenharmony_ci   }
1761bf215546Sopenharmony_ci
1762bf215546Sopenharmony_ci   LLVMTypeRef ptr_type = get_def_type(ctx, &instr->dest.ssa);
1763bf215546Sopenharmony_ci   ptr = ac_cast_ptr(&ctx->ac, ptr, ptr_type);
1764bf215546Sopenharmony_ci
1765bf215546Sopenharmony_ci   return LLVMBuildLoad2(ctx->ac.builder, ptr_type, ptr, "");
1766bf215546Sopenharmony_ci}
1767bf215546Sopenharmony_ci
1768bf215546Sopenharmony_cistatic LLVMValueRef visit_get_ssbo_size(struct ac_nir_context *ctx,
1769bf215546Sopenharmony_ci                                        const nir_intrinsic_instr *instr)
1770bf215546Sopenharmony_ci{
1771bf215546Sopenharmony_ci   bool non_uniform = nir_intrinsic_access(instr) & ACCESS_NON_UNIFORM;
1772bf215546Sopenharmony_ci   LLVMValueRef rsrc = ctx->abi->load_ssbo(ctx->abi, get_src(ctx, instr->src[0]), false, non_uniform);
1773bf215546Sopenharmony_ci   return get_buffer_size(ctx, rsrc, false);
1774bf215546Sopenharmony_ci}
1775bf215546Sopenharmony_ci
1776bf215546Sopenharmony_cistatic LLVMValueRef extract_vector_range(struct ac_llvm_context *ctx, LLVMValueRef src,
1777bf215546Sopenharmony_ci                                         unsigned start, unsigned count)
1778bf215546Sopenharmony_ci{
1779bf215546Sopenharmony_ci   LLVMValueRef mask[] = {ctx->i32_0, ctx->i32_1, LLVMConstInt(ctx->i32, 2, false),
1780bf215546Sopenharmony_ci                          LLVMConstInt(ctx->i32, 3, false)};
1781bf215546Sopenharmony_ci
1782bf215546Sopenharmony_ci   unsigned src_elements = ac_get_llvm_num_components(src);
1783bf215546Sopenharmony_ci
1784bf215546Sopenharmony_ci   if (count == src_elements) {
1785bf215546Sopenharmony_ci      assert(start == 0);
1786bf215546Sopenharmony_ci      return src;
1787bf215546Sopenharmony_ci   } else if (count == 1) {
1788bf215546Sopenharmony_ci      assert(start < src_elements);
1789bf215546Sopenharmony_ci      return LLVMBuildExtractElement(ctx->builder, src, mask[start], "");
1790bf215546Sopenharmony_ci   } else {
1791bf215546Sopenharmony_ci      assert(start + count <= src_elements);
1792bf215546Sopenharmony_ci      assert(count <= 4);
1793bf215546Sopenharmony_ci      LLVMValueRef swizzle = LLVMConstVector(&mask[start], count);
1794bf215546Sopenharmony_ci      return LLVMBuildShuffleVector(ctx->builder, src, src, swizzle, "");
1795bf215546Sopenharmony_ci   }
1796bf215546Sopenharmony_ci}
1797bf215546Sopenharmony_ci
1798bf215546Sopenharmony_cistatic unsigned get_cache_policy(struct ac_nir_context *ctx, enum gl_access_qualifier access,
1799bf215546Sopenharmony_ci                                 bool may_store_unaligned, bool writeonly_memory)
1800bf215546Sopenharmony_ci{
1801bf215546Sopenharmony_ci   unsigned cache_policy = 0;
1802bf215546Sopenharmony_ci
1803bf215546Sopenharmony_ci   /* GFX6 has a TC L1 bug causing corruption of 8bit/16bit stores.  All
1804bf215546Sopenharmony_ci    * store opcodes not aligned to a dword are affected. The only way to
1805bf215546Sopenharmony_ci    * get unaligned stores is through shader images.
1806bf215546Sopenharmony_ci    */
1807bf215546Sopenharmony_ci   if (((may_store_unaligned && ctx->ac.gfx_level == GFX6) ||
1808bf215546Sopenharmony_ci        /* If this is write-only, don't keep data in L1 to prevent
1809bf215546Sopenharmony_ci         * evicting L1 cache lines that may be needed by other
1810bf215546Sopenharmony_ci         * instructions.
1811bf215546Sopenharmony_ci         */
1812bf215546Sopenharmony_ci        writeonly_memory || access & (ACCESS_COHERENT | ACCESS_VOLATILE))) {
1813bf215546Sopenharmony_ci      cache_policy |= ac_glc;
1814bf215546Sopenharmony_ci   }
1815bf215546Sopenharmony_ci
1816bf215546Sopenharmony_ci   if (access & ACCESS_STREAM_CACHE_POLICY)
1817bf215546Sopenharmony_ci      cache_policy |= ac_slc | ac_glc;
1818bf215546Sopenharmony_ci
1819bf215546Sopenharmony_ci   return cache_policy;
1820bf215546Sopenharmony_ci}
1821bf215546Sopenharmony_ci
1822bf215546Sopenharmony_cistatic LLVMValueRef enter_waterfall_ssbo(struct ac_nir_context *ctx, struct waterfall_context *wctx,
1823bf215546Sopenharmony_ci                                         const nir_intrinsic_instr *instr, nir_src src)
1824bf215546Sopenharmony_ci{
1825bf215546Sopenharmony_ci   return enter_waterfall(ctx, wctx, get_src(ctx, src),
1826bf215546Sopenharmony_ci                          nir_intrinsic_access(instr) & ACCESS_NON_UNIFORM);
1827bf215546Sopenharmony_ci}
1828bf215546Sopenharmony_ci
1829bf215546Sopenharmony_cistatic void visit_store_ssbo(struct ac_nir_context *ctx, nir_intrinsic_instr *instr)
1830bf215546Sopenharmony_ci{
1831bf215546Sopenharmony_ci   if (ctx->ac.postponed_kill) {
1832bf215546Sopenharmony_ci      LLVMValueRef cond = LLVMBuildLoad2(ctx->ac.builder, ctx->ac.i1, ctx->ac.postponed_kill, "");
1833bf215546Sopenharmony_ci      ac_build_ifcc(&ctx->ac, cond, 7000);
1834bf215546Sopenharmony_ci   }
1835bf215546Sopenharmony_ci
1836bf215546Sopenharmony_ci   LLVMValueRef src_data = get_src(ctx, instr->src[0]);
1837bf215546Sopenharmony_ci   int elem_size_bytes = ac_get_elem_bits(&ctx->ac, LLVMTypeOf(src_data)) / 8;
1838bf215546Sopenharmony_ci   unsigned writemask = nir_intrinsic_write_mask(instr);
1839bf215546Sopenharmony_ci   enum gl_access_qualifier access = nir_intrinsic_access(instr);
1840bf215546Sopenharmony_ci   bool writeonly_memory = access & ACCESS_NON_READABLE;
1841bf215546Sopenharmony_ci   unsigned cache_policy = get_cache_policy(ctx, access, false, writeonly_memory);
1842bf215546Sopenharmony_ci
1843bf215546Sopenharmony_ci   struct waterfall_context wctx;
1844bf215546Sopenharmony_ci   LLVMValueRef rsrc_base = enter_waterfall_ssbo(ctx, &wctx, instr, instr->src[1]);
1845bf215546Sopenharmony_ci
1846bf215546Sopenharmony_ci   LLVMValueRef rsrc = ctx->abi->load_ssbo(ctx->abi, rsrc_base, true, false);
1847bf215546Sopenharmony_ci   LLVMValueRef base_data = src_data;
1848bf215546Sopenharmony_ci   base_data = ac_trim_vector(&ctx->ac, base_data, instr->num_components);
1849bf215546Sopenharmony_ci   LLVMValueRef base_offset = get_src(ctx, instr->src[2]);
1850bf215546Sopenharmony_ci
1851bf215546Sopenharmony_ci   while (writemask) {
1852bf215546Sopenharmony_ci      int start, count;
1853bf215546Sopenharmony_ci      LLVMValueRef data, offset;
1854bf215546Sopenharmony_ci      LLVMTypeRef data_type;
1855bf215546Sopenharmony_ci
1856bf215546Sopenharmony_ci      u_bit_scan_consecutive_range(&writemask, &start, &count);
1857bf215546Sopenharmony_ci
1858bf215546Sopenharmony_ci      if (count == 3 && elem_size_bytes != 4) {
1859bf215546Sopenharmony_ci         writemask |= 1 << (start + 2);
1860bf215546Sopenharmony_ci         count = 2;
1861bf215546Sopenharmony_ci      }
1862bf215546Sopenharmony_ci      int num_bytes = count * elem_size_bytes; /* count in bytes */
1863bf215546Sopenharmony_ci
1864bf215546Sopenharmony_ci      /* we can only store 4 DWords at the same time.
1865bf215546Sopenharmony_ci       * can only happen for 64 Bit vectors. */
1866bf215546Sopenharmony_ci      if (num_bytes > 16) {
1867bf215546Sopenharmony_ci         writemask |= ((1u << (count - 2)) - 1u) << (start + 2);
1868bf215546Sopenharmony_ci         count = 2;
1869bf215546Sopenharmony_ci         num_bytes = 16;
1870bf215546Sopenharmony_ci      }
1871bf215546Sopenharmony_ci
1872bf215546Sopenharmony_ci      /* check alignment of 16 Bit stores */
1873bf215546Sopenharmony_ci      if (elem_size_bytes == 2 && num_bytes > 2 && (start % 2) == 1) {
1874bf215546Sopenharmony_ci         writemask |= ((1u << (count - 1)) - 1u) << (start + 1);
1875bf215546Sopenharmony_ci         count = 1;
1876bf215546Sopenharmony_ci         num_bytes = 2;
1877bf215546Sopenharmony_ci      }
1878bf215546Sopenharmony_ci
1879bf215546Sopenharmony_ci      /* Due to alignment issues, split stores of 8-bit/16-bit
1880bf215546Sopenharmony_ci       * vectors.
1881bf215546Sopenharmony_ci       */
1882bf215546Sopenharmony_ci      if (ctx->ac.gfx_level == GFX6 && count > 1 && elem_size_bytes < 4) {
1883bf215546Sopenharmony_ci         writemask |= ((1u << (count - 1)) - 1u) << (start + 1);
1884bf215546Sopenharmony_ci         count = 1;
1885bf215546Sopenharmony_ci         num_bytes = elem_size_bytes;
1886bf215546Sopenharmony_ci      }
1887bf215546Sopenharmony_ci
1888bf215546Sopenharmony_ci      data = extract_vector_range(&ctx->ac, base_data, start, count);
1889bf215546Sopenharmony_ci
1890bf215546Sopenharmony_ci      offset = LLVMBuildAdd(ctx->ac.builder, base_offset,
1891bf215546Sopenharmony_ci                            LLVMConstInt(ctx->ac.i32, start * elem_size_bytes, false), "");
1892bf215546Sopenharmony_ci
1893bf215546Sopenharmony_ci      if (num_bytes == 1) {
1894bf215546Sopenharmony_ci         ac_build_buffer_store_byte(&ctx->ac, rsrc, data, offset, ctx->ac.i32_0, cache_policy);
1895bf215546Sopenharmony_ci      } else if (num_bytes == 2) {
1896bf215546Sopenharmony_ci         ac_build_buffer_store_short(&ctx->ac, rsrc, data, offset, ctx->ac.i32_0, cache_policy);
1897bf215546Sopenharmony_ci      } else {
1898bf215546Sopenharmony_ci         switch (num_bytes) {
1899bf215546Sopenharmony_ci         case 16: /* v4f32 */
1900bf215546Sopenharmony_ci            data_type = ctx->ac.v4f32;
1901bf215546Sopenharmony_ci            break;
1902bf215546Sopenharmony_ci         case 12: /* v3f32 */
1903bf215546Sopenharmony_ci            data_type = ctx->ac.v3f32;
1904bf215546Sopenharmony_ci            break;
1905bf215546Sopenharmony_ci         case 8: /* v2f32 */
1906bf215546Sopenharmony_ci            data_type = ctx->ac.v2f32;
1907bf215546Sopenharmony_ci            break;
1908bf215546Sopenharmony_ci         case 4: /* f32 */
1909bf215546Sopenharmony_ci            data_type = ctx->ac.f32;
1910bf215546Sopenharmony_ci            break;
1911bf215546Sopenharmony_ci         default:
1912bf215546Sopenharmony_ci            unreachable("Malformed vector store.");
1913bf215546Sopenharmony_ci         }
1914bf215546Sopenharmony_ci         data = LLVMBuildBitCast(ctx->ac.builder, data, data_type, "");
1915bf215546Sopenharmony_ci
1916bf215546Sopenharmony_ci         ac_build_buffer_store_dword(&ctx->ac, rsrc, data, NULL, offset,
1917bf215546Sopenharmony_ci                                     ctx->ac.i32_0, cache_policy);
1918bf215546Sopenharmony_ci      }
1919bf215546Sopenharmony_ci   }
1920bf215546Sopenharmony_ci
1921bf215546Sopenharmony_ci   exit_waterfall(ctx, &wctx, NULL);
1922bf215546Sopenharmony_ci
1923bf215546Sopenharmony_ci   if (ctx->ac.postponed_kill)
1924bf215546Sopenharmony_ci      ac_build_endif(&ctx->ac, 7000);
1925bf215546Sopenharmony_ci}
1926bf215546Sopenharmony_ci
1927bf215546Sopenharmony_cistatic LLVMValueRef emit_ssbo_comp_swap_64(struct ac_nir_context *ctx, LLVMValueRef descriptor,
1928bf215546Sopenharmony_ci                                           LLVMValueRef offset, LLVMValueRef compare,
1929bf215546Sopenharmony_ci                                           LLVMValueRef exchange, bool image)
1930bf215546Sopenharmony_ci{
1931bf215546Sopenharmony_ci   LLVMBasicBlockRef start_block = NULL, then_block = NULL;
1932bf215546Sopenharmony_ci   if (ctx->abi->robust_buffer_access || image) {
1933bf215546Sopenharmony_ci      LLVMValueRef size = ac_llvm_extract_elem(&ctx->ac, descriptor, 2);
1934bf215546Sopenharmony_ci
1935bf215546Sopenharmony_ci      LLVMValueRef cond = LLVMBuildICmp(ctx->ac.builder, LLVMIntULT, offset, size, "");
1936bf215546Sopenharmony_ci      start_block = LLVMGetInsertBlock(ctx->ac.builder);
1937bf215546Sopenharmony_ci
1938bf215546Sopenharmony_ci      ac_build_ifcc(&ctx->ac, cond, -1);
1939bf215546Sopenharmony_ci
1940bf215546Sopenharmony_ci      then_block = LLVMGetInsertBlock(ctx->ac.builder);
1941bf215546Sopenharmony_ci   }
1942bf215546Sopenharmony_ci
1943bf215546Sopenharmony_ci   if (image)
1944bf215546Sopenharmony_ci      offset = LLVMBuildMul(ctx->ac.builder, offset, LLVMConstInt(ctx->ac.i32, 8, false), "");
1945bf215546Sopenharmony_ci
1946bf215546Sopenharmony_ci   LLVMValueRef ptr_parts[2] = {
1947bf215546Sopenharmony_ci      ac_llvm_extract_elem(&ctx->ac, descriptor, 0),
1948bf215546Sopenharmony_ci      LLVMBuildAnd(ctx->ac.builder, ac_llvm_extract_elem(&ctx->ac, descriptor, 1),
1949bf215546Sopenharmony_ci                   LLVMConstInt(ctx->ac.i32, 65535, 0), "")};
1950bf215546Sopenharmony_ci
1951bf215546Sopenharmony_ci   ptr_parts[1] = LLVMBuildTrunc(ctx->ac.builder, ptr_parts[1], ctx->ac.i16, "");
1952bf215546Sopenharmony_ci   ptr_parts[1] = LLVMBuildSExt(ctx->ac.builder, ptr_parts[1], ctx->ac.i32, "");
1953bf215546Sopenharmony_ci
1954bf215546Sopenharmony_ci   offset = LLVMBuildZExt(ctx->ac.builder, offset, ctx->ac.i64, "");
1955bf215546Sopenharmony_ci
1956bf215546Sopenharmony_ci   LLVMValueRef ptr = ac_build_gather_values(&ctx->ac, ptr_parts, 2);
1957bf215546Sopenharmony_ci   ptr = LLVMBuildBitCast(ctx->ac.builder, ptr, ctx->ac.i64, "");
1958bf215546Sopenharmony_ci   ptr = LLVMBuildAdd(ctx->ac.builder, ptr, offset, "");
1959bf215546Sopenharmony_ci   ptr = LLVMBuildIntToPtr(ctx->ac.builder, ptr, LLVMPointerType(ctx->ac.i64, AC_ADDR_SPACE_GLOBAL),
1960bf215546Sopenharmony_ci                           "");
1961bf215546Sopenharmony_ci
1962bf215546Sopenharmony_ci   LLVMValueRef result =
1963bf215546Sopenharmony_ci      ac_build_atomic_cmp_xchg(&ctx->ac, ptr, compare, exchange, "singlethread-one-as");
1964bf215546Sopenharmony_ci   result = LLVMBuildExtractValue(ctx->ac.builder, result, 0, "");
1965bf215546Sopenharmony_ci
1966bf215546Sopenharmony_ci   if (ctx->abi->robust_buffer_access || image) {
1967bf215546Sopenharmony_ci      ac_build_endif(&ctx->ac, -1);
1968bf215546Sopenharmony_ci
1969bf215546Sopenharmony_ci      LLVMBasicBlockRef incoming_blocks[2] = {
1970bf215546Sopenharmony_ci         start_block,
1971bf215546Sopenharmony_ci         then_block,
1972bf215546Sopenharmony_ci      };
1973bf215546Sopenharmony_ci
1974bf215546Sopenharmony_ci      LLVMValueRef incoming_values[2] = {
1975bf215546Sopenharmony_ci         LLVMConstInt(ctx->ac.i64, 0, 0),
1976bf215546Sopenharmony_ci         result,
1977bf215546Sopenharmony_ci      };
1978bf215546Sopenharmony_ci      LLVMValueRef ret = LLVMBuildPhi(ctx->ac.builder, ctx->ac.i64, "");
1979bf215546Sopenharmony_ci      LLVMAddIncoming(ret, incoming_values, incoming_blocks, 2);
1980bf215546Sopenharmony_ci      return ret;
1981bf215546Sopenharmony_ci   } else {
1982bf215546Sopenharmony_ci      return result;
1983bf215546Sopenharmony_ci   }
1984bf215546Sopenharmony_ci}
1985bf215546Sopenharmony_ci
1986bf215546Sopenharmony_cistatic LLVMValueRef visit_atomic_ssbo(struct ac_nir_context *ctx, nir_intrinsic_instr *instr)
1987bf215546Sopenharmony_ci{
1988bf215546Sopenharmony_ci   if (ctx->ac.postponed_kill) {
1989bf215546Sopenharmony_ci      LLVMValueRef cond = LLVMBuildLoad2(ctx->ac.builder, ctx->ac.i1, ctx->ac.postponed_kill, "");
1990bf215546Sopenharmony_ci      ac_build_ifcc(&ctx->ac, cond, 7001);
1991bf215546Sopenharmony_ci   }
1992bf215546Sopenharmony_ci
1993bf215546Sopenharmony_ci   LLVMTypeRef return_type = LLVMTypeOf(get_src(ctx, instr->src[2]));
1994bf215546Sopenharmony_ci   const char *op;
1995bf215546Sopenharmony_ci   char name[64], type[8];
1996bf215546Sopenharmony_ci   LLVMValueRef params[6], descriptor;
1997bf215546Sopenharmony_ci   LLVMValueRef result;
1998bf215546Sopenharmony_ci   int arg_count = 0;
1999bf215546Sopenharmony_ci
2000bf215546Sopenharmony_ci   struct waterfall_context wctx;
2001bf215546Sopenharmony_ci   LLVMValueRef rsrc_base = enter_waterfall_ssbo(ctx, &wctx, instr, instr->src[0]);
2002bf215546Sopenharmony_ci
2003bf215546Sopenharmony_ci   switch (instr->intrinsic) {
2004bf215546Sopenharmony_ci   case nir_intrinsic_ssbo_atomic_add:
2005bf215546Sopenharmony_ci      op = "add";
2006bf215546Sopenharmony_ci      break;
2007bf215546Sopenharmony_ci   case nir_intrinsic_ssbo_atomic_imin:
2008bf215546Sopenharmony_ci      op = "smin";
2009bf215546Sopenharmony_ci      break;
2010bf215546Sopenharmony_ci   case nir_intrinsic_ssbo_atomic_umin:
2011bf215546Sopenharmony_ci      op = "umin";
2012bf215546Sopenharmony_ci      break;
2013bf215546Sopenharmony_ci   case nir_intrinsic_ssbo_atomic_imax:
2014bf215546Sopenharmony_ci      op = "smax";
2015bf215546Sopenharmony_ci      break;
2016bf215546Sopenharmony_ci   case nir_intrinsic_ssbo_atomic_umax:
2017bf215546Sopenharmony_ci      op = "umax";
2018bf215546Sopenharmony_ci      break;
2019bf215546Sopenharmony_ci   case nir_intrinsic_ssbo_atomic_and:
2020bf215546Sopenharmony_ci      op = "and";
2021bf215546Sopenharmony_ci      break;
2022bf215546Sopenharmony_ci   case nir_intrinsic_ssbo_atomic_or:
2023bf215546Sopenharmony_ci      op = "or";
2024bf215546Sopenharmony_ci      break;
2025bf215546Sopenharmony_ci   case nir_intrinsic_ssbo_atomic_xor:
2026bf215546Sopenharmony_ci      op = "xor";
2027bf215546Sopenharmony_ci      break;
2028bf215546Sopenharmony_ci   case nir_intrinsic_ssbo_atomic_exchange:
2029bf215546Sopenharmony_ci      op = "swap";
2030bf215546Sopenharmony_ci      break;
2031bf215546Sopenharmony_ci   case nir_intrinsic_ssbo_atomic_comp_swap:
2032bf215546Sopenharmony_ci      op = "cmpswap";
2033bf215546Sopenharmony_ci      break;
2034bf215546Sopenharmony_ci   case nir_intrinsic_ssbo_atomic_fmin:
2035bf215546Sopenharmony_ci      op = "fmin";
2036bf215546Sopenharmony_ci      break;
2037bf215546Sopenharmony_ci   case nir_intrinsic_ssbo_atomic_fmax:
2038bf215546Sopenharmony_ci      op = "fmax";
2039bf215546Sopenharmony_ci      break;
2040bf215546Sopenharmony_ci   default:
2041bf215546Sopenharmony_ci      abort();
2042bf215546Sopenharmony_ci   }
2043bf215546Sopenharmony_ci
2044bf215546Sopenharmony_ci   descriptor = ctx->abi->load_ssbo(ctx->abi, rsrc_base, true, false);
2045bf215546Sopenharmony_ci
2046bf215546Sopenharmony_ci   if (instr->intrinsic == nir_intrinsic_ssbo_atomic_comp_swap && return_type == ctx->ac.i64) {
2047bf215546Sopenharmony_ci      result = emit_ssbo_comp_swap_64(ctx, descriptor, get_src(ctx, instr->src[1]),
2048bf215546Sopenharmony_ci                                      get_src(ctx, instr->src[2]), get_src(ctx, instr->src[3]), false);
2049bf215546Sopenharmony_ci   } else {
2050bf215546Sopenharmony_ci      LLVMValueRef data = ac_llvm_extract_elem(&ctx->ac, get_src(ctx, instr->src[2]), 0);
2051bf215546Sopenharmony_ci
2052bf215546Sopenharmony_ci      if (instr->intrinsic == nir_intrinsic_ssbo_atomic_comp_swap) {
2053bf215546Sopenharmony_ci         params[arg_count++] = ac_llvm_extract_elem(&ctx->ac, get_src(ctx, instr->src[3]), 0);
2054bf215546Sopenharmony_ci      }
2055bf215546Sopenharmony_ci      if (instr->intrinsic == nir_intrinsic_ssbo_atomic_fmin ||
2056bf215546Sopenharmony_ci          instr->intrinsic == nir_intrinsic_ssbo_atomic_fmax) {
2057bf215546Sopenharmony_ci         data = ac_to_float(&ctx->ac, data);
2058bf215546Sopenharmony_ci         return_type = LLVMTypeOf(data);
2059bf215546Sopenharmony_ci      }
2060bf215546Sopenharmony_ci      params[arg_count++] = data;
2061bf215546Sopenharmony_ci      params[arg_count++] = descriptor;
2062bf215546Sopenharmony_ci      params[arg_count++] = get_src(ctx, instr->src[1]); /* voffset */
2063bf215546Sopenharmony_ci      params[arg_count++] = ctx->ac.i32_0;               /* soffset */
2064bf215546Sopenharmony_ci      params[arg_count++] = ctx->ac.i32_0;               /* slc */
2065bf215546Sopenharmony_ci
2066bf215546Sopenharmony_ci      ac_build_type_name_for_intr(return_type, type, sizeof(type));
2067bf215546Sopenharmony_ci      snprintf(name, sizeof(name), "llvm.amdgcn.raw.buffer.atomic.%s.%s", op, type);
2068bf215546Sopenharmony_ci
2069bf215546Sopenharmony_ci      result = ac_build_intrinsic(&ctx->ac, name, return_type, params, arg_count, 0);
2070bf215546Sopenharmony_ci
2071bf215546Sopenharmony_ci      if (instr->intrinsic == nir_intrinsic_ssbo_atomic_fmin ||
2072bf215546Sopenharmony_ci          instr->intrinsic == nir_intrinsic_ssbo_atomic_fmax) {
2073bf215546Sopenharmony_ci         result = ac_to_integer(&ctx->ac, result);
2074bf215546Sopenharmony_ci      }
2075bf215546Sopenharmony_ci   }
2076bf215546Sopenharmony_ci
2077bf215546Sopenharmony_ci   result = exit_waterfall(ctx, &wctx, result);
2078bf215546Sopenharmony_ci   if (ctx->ac.postponed_kill)
2079bf215546Sopenharmony_ci      ac_build_endif(&ctx->ac, 7001);
2080bf215546Sopenharmony_ci   return result;
2081bf215546Sopenharmony_ci}
2082bf215546Sopenharmony_ci
2083bf215546Sopenharmony_cistatic LLVMValueRef visit_load_buffer(struct ac_nir_context *ctx, nir_intrinsic_instr *instr)
2084bf215546Sopenharmony_ci{
2085bf215546Sopenharmony_ci   struct waterfall_context wctx;
2086bf215546Sopenharmony_ci   LLVMValueRef rsrc_base = enter_waterfall_ssbo(ctx, &wctx, instr, instr->src[0]);
2087bf215546Sopenharmony_ci
2088bf215546Sopenharmony_ci   int elem_size_bytes = instr->dest.ssa.bit_size / 8;
2089bf215546Sopenharmony_ci   int num_components = instr->num_components;
2090bf215546Sopenharmony_ci   enum gl_access_qualifier access = nir_intrinsic_access(instr);
2091bf215546Sopenharmony_ci   unsigned cache_policy = get_cache_policy(ctx, access, false, false);
2092bf215546Sopenharmony_ci
2093bf215546Sopenharmony_ci   LLVMValueRef offset = get_src(ctx, instr->src[1]);
2094bf215546Sopenharmony_ci   LLVMValueRef rsrc = ctx->abi->load_ssbo(ctx->abi, rsrc_base, false, false);
2095bf215546Sopenharmony_ci   LLVMValueRef vindex = ctx->ac.i32_0;
2096bf215546Sopenharmony_ci
2097bf215546Sopenharmony_ci   LLVMTypeRef def_type = get_def_type(ctx, &instr->dest.ssa);
2098bf215546Sopenharmony_ci   LLVMTypeRef def_elem_type = num_components > 1 ? LLVMGetElementType(def_type) : def_type;
2099bf215546Sopenharmony_ci
2100bf215546Sopenharmony_ci   LLVMValueRef results[4];
2101bf215546Sopenharmony_ci   for (int i = 0; i < num_components;) {
2102bf215546Sopenharmony_ci      int num_elems = num_components - i;
2103bf215546Sopenharmony_ci      if (elem_size_bytes < 4 && nir_intrinsic_align(instr) % 4 != 0)
2104bf215546Sopenharmony_ci         num_elems = 1;
2105bf215546Sopenharmony_ci      if (num_elems * elem_size_bytes > 16)
2106bf215546Sopenharmony_ci         num_elems = 16 / elem_size_bytes;
2107bf215546Sopenharmony_ci      int load_bytes = num_elems * elem_size_bytes;
2108bf215546Sopenharmony_ci
2109bf215546Sopenharmony_ci      LLVMValueRef immoffset = LLVMConstInt(ctx->ac.i32, i * elem_size_bytes, false);
2110bf215546Sopenharmony_ci      LLVMValueRef voffset = LLVMBuildAdd(ctx->ac.builder, offset, immoffset, "");
2111bf215546Sopenharmony_ci
2112bf215546Sopenharmony_ci      LLVMValueRef ret;
2113bf215546Sopenharmony_ci
2114bf215546Sopenharmony_ci      if (load_bytes == 1) {
2115bf215546Sopenharmony_ci         ret = ac_build_buffer_load_byte(&ctx->ac, rsrc, voffset, ctx->ac.i32_0,
2116bf215546Sopenharmony_ci                                          cache_policy);
2117bf215546Sopenharmony_ci      } else if (load_bytes == 2) {
2118bf215546Sopenharmony_ci         ret = ac_build_buffer_load_short(&ctx->ac, rsrc, voffset, ctx->ac.i32_0,
2119bf215546Sopenharmony_ci                                           cache_policy);
2120bf215546Sopenharmony_ci      } else {
2121bf215546Sopenharmony_ci         int num_channels = util_next_power_of_two(load_bytes) / 4;
2122bf215546Sopenharmony_ci         bool can_speculate = access & ACCESS_CAN_REORDER;
2123bf215546Sopenharmony_ci
2124bf215546Sopenharmony_ci         ret = ac_build_buffer_load(&ctx->ac, rsrc, num_channels, vindex, voffset, ctx->ac.i32_0,
2125bf215546Sopenharmony_ci                                    ctx->ac.f32, cache_policy, can_speculate, false);
2126bf215546Sopenharmony_ci      }
2127bf215546Sopenharmony_ci
2128bf215546Sopenharmony_ci      LLVMTypeRef byte_vec = LLVMVectorType(ctx->ac.i8, ac_get_type_size(LLVMTypeOf(ret)));
2129bf215546Sopenharmony_ci      ret = LLVMBuildBitCast(ctx->ac.builder, ret, byte_vec, "");
2130bf215546Sopenharmony_ci      ret = ac_trim_vector(&ctx->ac, ret, load_bytes);
2131bf215546Sopenharmony_ci
2132bf215546Sopenharmony_ci      LLVMTypeRef ret_type = LLVMVectorType(def_elem_type, num_elems);
2133bf215546Sopenharmony_ci      ret = LLVMBuildBitCast(ctx->ac.builder, ret, ret_type, "");
2134bf215546Sopenharmony_ci
2135bf215546Sopenharmony_ci      for (unsigned j = 0; j < num_elems; j++) {
2136bf215546Sopenharmony_ci         results[i + j] =
2137bf215546Sopenharmony_ci            LLVMBuildExtractElement(ctx->ac.builder, ret, LLVMConstInt(ctx->ac.i32, j, false), "");
2138bf215546Sopenharmony_ci      }
2139bf215546Sopenharmony_ci      i += num_elems;
2140bf215546Sopenharmony_ci   }
2141bf215546Sopenharmony_ci
2142bf215546Sopenharmony_ci   LLVMValueRef ret = ac_build_gather_values(&ctx->ac, results, num_components);
2143bf215546Sopenharmony_ci   return exit_waterfall(ctx, &wctx, ret);
2144bf215546Sopenharmony_ci}
2145bf215546Sopenharmony_ci
2146bf215546Sopenharmony_cistatic LLVMValueRef enter_waterfall_ubo(struct ac_nir_context *ctx, struct waterfall_context *wctx,
2147bf215546Sopenharmony_ci                                        const nir_intrinsic_instr *instr)
2148bf215546Sopenharmony_ci{
2149bf215546Sopenharmony_ci   return enter_waterfall(ctx, wctx, get_src(ctx, instr->src[0]),
2150bf215546Sopenharmony_ci                          nir_intrinsic_access(instr) & ACCESS_NON_UNIFORM);
2151bf215546Sopenharmony_ci}
2152bf215546Sopenharmony_ci
2153bf215546Sopenharmony_cistatic LLVMValueRef get_global_address(struct ac_nir_context *ctx,
2154bf215546Sopenharmony_ci                                       nir_intrinsic_instr *instr,
2155bf215546Sopenharmony_ci                                       LLVMTypeRef type)
2156bf215546Sopenharmony_ci{
2157bf215546Sopenharmony_ci   bool is_store = instr->intrinsic == nir_intrinsic_store_global ||
2158bf215546Sopenharmony_ci                   instr->intrinsic == nir_intrinsic_store_global_amd;
2159bf215546Sopenharmony_ci   LLVMValueRef addr = get_src(ctx, instr->src[is_store ? 1 : 0]);
2160bf215546Sopenharmony_ci
2161bf215546Sopenharmony_ci   LLVMTypeRef ptr_type = LLVMPointerType(type, AC_ADDR_SPACE_GLOBAL);
2162bf215546Sopenharmony_ci
2163bf215546Sopenharmony_ci   if (nir_intrinsic_has_base(instr)) {
2164bf215546Sopenharmony_ci      /* _amd variants */
2165bf215546Sopenharmony_ci      uint32_t base = nir_intrinsic_base(instr);
2166bf215546Sopenharmony_ci      unsigned num_src = nir_intrinsic_infos[instr->intrinsic].num_srcs;
2167bf215546Sopenharmony_ci      LLVMValueRef offset = get_src(ctx, instr->src[num_src - 1]);
2168bf215546Sopenharmony_ci      offset = LLVMBuildAdd(ctx->ac.builder, offset, LLVMConstInt(ctx->ac.i32, base, false), "");
2169bf215546Sopenharmony_ci
2170bf215546Sopenharmony_ci      LLVMTypeRef i8_ptr_type = LLVMPointerType(ctx->ac.i8, AC_ADDR_SPACE_GLOBAL);
2171bf215546Sopenharmony_ci      addr = LLVMBuildIntToPtr(ctx->ac.builder, addr, i8_ptr_type, "");
2172bf215546Sopenharmony_ci      addr = LLVMBuildGEP(ctx->ac.builder, addr, &offset, 1, "");
2173bf215546Sopenharmony_ci      return type == ctx->ac.i8 ? addr : LLVMBuildBitCast(ctx->ac.builder, addr, ptr_type, "");
2174bf215546Sopenharmony_ci   } else {
2175bf215546Sopenharmony_ci      return LLVMBuildIntToPtr(ctx->ac.builder, addr, ptr_type, "");
2176bf215546Sopenharmony_ci   }
2177bf215546Sopenharmony_ci}
2178bf215546Sopenharmony_ci
2179bf215546Sopenharmony_cistatic LLVMValueRef visit_load_global(struct ac_nir_context *ctx,
2180bf215546Sopenharmony_ci                                      nir_intrinsic_instr *instr)
2181bf215546Sopenharmony_ci{
2182bf215546Sopenharmony_ci   LLVMTypeRef result_type = get_def_type(ctx, &instr->dest.ssa);
2183bf215546Sopenharmony_ci   LLVMValueRef val;
2184bf215546Sopenharmony_ci   LLVMValueRef addr = get_global_address(ctx, instr, result_type);
2185bf215546Sopenharmony_ci
2186bf215546Sopenharmony_ci   val = LLVMBuildLoad2(ctx->ac.builder, result_type, addr, "");
2187bf215546Sopenharmony_ci
2188bf215546Sopenharmony_ci   if (nir_intrinsic_access(instr) & (ACCESS_COHERENT | ACCESS_VOLATILE)) {
2189bf215546Sopenharmony_ci      LLVMSetOrdering(val, LLVMAtomicOrderingMonotonic);
2190bf215546Sopenharmony_ci      LLVMSetAlignment(val, ac_get_type_size(result_type));
2191bf215546Sopenharmony_ci   }
2192bf215546Sopenharmony_ci
2193bf215546Sopenharmony_ci   return val;
2194bf215546Sopenharmony_ci}
2195bf215546Sopenharmony_ci
2196bf215546Sopenharmony_cistatic void visit_store_global(struct ac_nir_context *ctx,
2197bf215546Sopenharmony_ci				     nir_intrinsic_instr *instr)
2198bf215546Sopenharmony_ci{
2199bf215546Sopenharmony_ci   if (ctx->ac.postponed_kill) {
2200bf215546Sopenharmony_ci      LLVMValueRef cond = LLVMBuildLoad2(ctx->ac.builder, ctx->ac.i1, ctx->ac.postponed_kill, "");
2201bf215546Sopenharmony_ci      ac_build_ifcc(&ctx->ac, cond, 7002);
2202bf215546Sopenharmony_ci   }
2203bf215546Sopenharmony_ci
2204bf215546Sopenharmony_ci   LLVMValueRef data = get_src(ctx, instr->src[0]);
2205bf215546Sopenharmony_ci   LLVMTypeRef type = LLVMTypeOf(data);
2206bf215546Sopenharmony_ci   LLVMValueRef addr = get_global_address(ctx, instr, type);
2207bf215546Sopenharmony_ci   LLVMValueRef val;
2208bf215546Sopenharmony_ci
2209bf215546Sopenharmony_ci   val = LLVMBuildStore(ctx->ac.builder, data, addr);
2210bf215546Sopenharmony_ci
2211bf215546Sopenharmony_ci   if (nir_intrinsic_access(instr) & (ACCESS_COHERENT | ACCESS_VOLATILE)) {
2212bf215546Sopenharmony_ci      LLVMSetOrdering(val, LLVMAtomicOrderingMonotonic);
2213bf215546Sopenharmony_ci      LLVMSetAlignment(val, ac_get_type_size(type));
2214bf215546Sopenharmony_ci   }
2215bf215546Sopenharmony_ci
2216bf215546Sopenharmony_ci   if (ctx->ac.postponed_kill)
2217bf215546Sopenharmony_ci      ac_build_endif(&ctx->ac, 7002);
2218bf215546Sopenharmony_ci}
2219bf215546Sopenharmony_ci
2220bf215546Sopenharmony_cistatic LLVMValueRef visit_global_atomic(struct ac_nir_context *ctx,
2221bf215546Sopenharmony_ci					nir_intrinsic_instr *instr)
2222bf215546Sopenharmony_ci{
2223bf215546Sopenharmony_ci   if (ctx->ac.postponed_kill) {
2224bf215546Sopenharmony_ci      LLVMValueRef cond = LLVMBuildLoad2(ctx->ac.builder, ctx->ac.i1, ctx->ac.postponed_kill, "");
2225bf215546Sopenharmony_ci      ac_build_ifcc(&ctx->ac, cond, 7002);
2226bf215546Sopenharmony_ci   }
2227bf215546Sopenharmony_ci
2228bf215546Sopenharmony_ci   LLVMValueRef data = get_src(ctx, instr->src[1]);
2229bf215546Sopenharmony_ci   LLVMAtomicRMWBinOp op;
2230bf215546Sopenharmony_ci   LLVMValueRef result;
2231bf215546Sopenharmony_ci
2232bf215546Sopenharmony_ci   /* use "singlethread" sync scope to implement relaxed ordering */
2233bf215546Sopenharmony_ci   const char *sync_scope = "singlethread-one-as";
2234bf215546Sopenharmony_ci
2235bf215546Sopenharmony_ci   if (instr->intrinsic == nir_intrinsic_global_atomic_fmin ||
2236bf215546Sopenharmony_ci       instr->intrinsic == nir_intrinsic_global_atomic_fmax ||
2237bf215546Sopenharmony_ci       instr->intrinsic == nir_intrinsic_global_atomic_fmin_amd ||
2238bf215546Sopenharmony_ci       instr->intrinsic == nir_intrinsic_global_atomic_fmax_amd) {
2239bf215546Sopenharmony_ci      data = ac_to_float(&ctx->ac, data);
2240bf215546Sopenharmony_ci   }
2241bf215546Sopenharmony_ci
2242bf215546Sopenharmony_ci   LLVMTypeRef data_type = LLVMTypeOf(data);
2243bf215546Sopenharmony_ci
2244bf215546Sopenharmony_ci   LLVMValueRef addr = get_global_address(ctx, instr, data_type);
2245bf215546Sopenharmony_ci
2246bf215546Sopenharmony_ci   if (instr->intrinsic == nir_intrinsic_global_atomic_comp_swap ||
2247bf215546Sopenharmony_ci       instr->intrinsic == nir_intrinsic_global_atomic_comp_swap_amd) {
2248bf215546Sopenharmony_ci      LLVMValueRef data1 = get_src(ctx, instr->src[2]);
2249bf215546Sopenharmony_ci      result = ac_build_atomic_cmp_xchg(&ctx->ac, addr, data, data1, sync_scope);
2250bf215546Sopenharmony_ci      result = LLVMBuildExtractValue(ctx->ac.builder, result, 0, "");
2251bf215546Sopenharmony_ci   } else if (instr->intrinsic == nir_intrinsic_global_atomic_fmin ||
2252bf215546Sopenharmony_ci              instr->intrinsic == nir_intrinsic_global_atomic_fmax ||
2253bf215546Sopenharmony_ci              instr->intrinsic == nir_intrinsic_global_atomic_fmin_amd ||
2254bf215546Sopenharmony_ci              instr->intrinsic == nir_intrinsic_global_atomic_fmax_amd) {
2255bf215546Sopenharmony_ci      const char *op = instr->intrinsic == nir_intrinsic_global_atomic_fmin ? "fmin" : "fmax";
2256bf215546Sopenharmony_ci      char name[64], type[8];
2257bf215546Sopenharmony_ci      LLVMValueRef params[2];
2258bf215546Sopenharmony_ci      int arg_count = 0;
2259bf215546Sopenharmony_ci
2260bf215546Sopenharmony_ci      params[arg_count++] = addr;
2261bf215546Sopenharmony_ci      params[arg_count++] = data;
2262bf215546Sopenharmony_ci
2263bf215546Sopenharmony_ci      ac_build_type_name_for_intr(data_type, type, sizeof(type));
2264bf215546Sopenharmony_ci      snprintf(name, sizeof(name), "llvm.amdgcn.global.atomic.%s.%s.p1%s.%s", op, type, type, type);
2265bf215546Sopenharmony_ci
2266bf215546Sopenharmony_ci      result = ac_build_intrinsic(&ctx->ac, name, data_type, params, arg_count, 0);
2267bf215546Sopenharmony_ci      result = ac_to_integer(&ctx->ac, result);
2268bf215546Sopenharmony_ci   } else {
2269bf215546Sopenharmony_ci      switch (instr->intrinsic) {
2270bf215546Sopenharmony_ci      case nir_intrinsic_global_atomic_add:
2271bf215546Sopenharmony_ci      case nir_intrinsic_global_atomic_add_amd:
2272bf215546Sopenharmony_ci         op = LLVMAtomicRMWBinOpAdd;
2273bf215546Sopenharmony_ci         break;
2274bf215546Sopenharmony_ci      case nir_intrinsic_global_atomic_umin:
2275bf215546Sopenharmony_ci      case nir_intrinsic_global_atomic_umin_amd:
2276bf215546Sopenharmony_ci         op = LLVMAtomicRMWBinOpUMin;
2277bf215546Sopenharmony_ci         break;
2278bf215546Sopenharmony_ci      case nir_intrinsic_global_atomic_umax:
2279bf215546Sopenharmony_ci      case nir_intrinsic_global_atomic_umax_amd:
2280bf215546Sopenharmony_ci         op = LLVMAtomicRMWBinOpUMax;
2281bf215546Sopenharmony_ci         break;
2282bf215546Sopenharmony_ci      case nir_intrinsic_global_atomic_imin:
2283bf215546Sopenharmony_ci      case nir_intrinsic_global_atomic_imin_amd:
2284bf215546Sopenharmony_ci         op = LLVMAtomicRMWBinOpMin;
2285bf215546Sopenharmony_ci         break;
2286bf215546Sopenharmony_ci      case nir_intrinsic_global_atomic_imax:
2287bf215546Sopenharmony_ci      case nir_intrinsic_global_atomic_imax_amd:
2288bf215546Sopenharmony_ci         op = LLVMAtomicRMWBinOpMax;
2289bf215546Sopenharmony_ci         break;
2290bf215546Sopenharmony_ci      case nir_intrinsic_global_atomic_and:
2291bf215546Sopenharmony_ci      case nir_intrinsic_global_atomic_and_amd:
2292bf215546Sopenharmony_ci         op = LLVMAtomicRMWBinOpAnd;
2293bf215546Sopenharmony_ci         break;
2294bf215546Sopenharmony_ci      case nir_intrinsic_global_atomic_or:
2295bf215546Sopenharmony_ci      case nir_intrinsic_global_atomic_or_amd:
2296bf215546Sopenharmony_ci         op = LLVMAtomicRMWBinOpOr;
2297bf215546Sopenharmony_ci         break;
2298bf215546Sopenharmony_ci      case nir_intrinsic_global_atomic_xor:
2299bf215546Sopenharmony_ci      case nir_intrinsic_global_atomic_xor_amd:
2300bf215546Sopenharmony_ci         op = LLVMAtomicRMWBinOpXor;
2301bf215546Sopenharmony_ci         break;
2302bf215546Sopenharmony_ci      case nir_intrinsic_global_atomic_exchange:
2303bf215546Sopenharmony_ci      case nir_intrinsic_global_atomic_exchange_amd:
2304bf215546Sopenharmony_ci         op = LLVMAtomicRMWBinOpXchg;
2305bf215546Sopenharmony_ci         break;
2306bf215546Sopenharmony_ci      default:
2307bf215546Sopenharmony_ci         unreachable("Invalid global atomic operation");
2308bf215546Sopenharmony_ci      }
2309bf215546Sopenharmony_ci
2310bf215546Sopenharmony_ci      result = ac_build_atomic_rmw(&ctx->ac, op, addr, ac_to_integer(&ctx->ac, data), sync_scope);
2311bf215546Sopenharmony_ci   }
2312bf215546Sopenharmony_ci
2313bf215546Sopenharmony_ci   if (ctx->ac.postponed_kill)
2314bf215546Sopenharmony_ci      ac_build_endif(&ctx->ac, 7002);
2315bf215546Sopenharmony_ci
2316bf215546Sopenharmony_ci   return result;
2317bf215546Sopenharmony_ci}
2318bf215546Sopenharmony_ci
2319bf215546Sopenharmony_cistatic LLVMValueRef visit_load_ubo_buffer(struct ac_nir_context *ctx, nir_intrinsic_instr *instr)
2320bf215546Sopenharmony_ci{
2321bf215546Sopenharmony_ci   struct waterfall_context wctx;
2322bf215546Sopenharmony_ci   LLVMValueRef rsrc_base = enter_waterfall_ubo(ctx, &wctx, instr);
2323bf215546Sopenharmony_ci
2324bf215546Sopenharmony_ci   LLVMValueRef ret;
2325bf215546Sopenharmony_ci   LLVMValueRef rsrc = rsrc_base;
2326bf215546Sopenharmony_ci   LLVMValueRef offset = get_src(ctx, instr->src[1]);
2327bf215546Sopenharmony_ci   int num_components = instr->num_components;
2328bf215546Sopenharmony_ci
2329bf215546Sopenharmony_ci   if (ctx->abi->load_ubo)
2330bf215546Sopenharmony_ci      rsrc = ctx->abi->load_ubo(ctx->abi, rsrc);
2331bf215546Sopenharmony_ci
2332bf215546Sopenharmony_ci   /* Convert to a scalar 32-bit load. */
2333bf215546Sopenharmony_ci   if (instr->dest.ssa.bit_size == 64)
2334bf215546Sopenharmony_ci      num_components *= 2;
2335bf215546Sopenharmony_ci   else if (instr->dest.ssa.bit_size == 16)
2336bf215546Sopenharmony_ci      num_components = DIV_ROUND_UP(num_components, 2);
2337bf215546Sopenharmony_ci   else if (instr->dest.ssa.bit_size == 8)
2338bf215546Sopenharmony_ci      num_components = DIV_ROUND_UP(num_components, 4);
2339bf215546Sopenharmony_ci
2340bf215546Sopenharmony_ci   ret =
2341bf215546Sopenharmony_ci      ac_build_buffer_load(&ctx->ac, rsrc, num_components, NULL, offset, NULL,
2342bf215546Sopenharmony_ci                           ctx->ac.f32, 0, true, true);
2343bf215546Sopenharmony_ci
2344bf215546Sopenharmony_ci   /* Convert to the original type. */
2345bf215546Sopenharmony_ci   if (instr->dest.ssa.bit_size == 64) {
2346bf215546Sopenharmony_ci      ret = LLVMBuildBitCast(ctx->ac.builder, ret,
2347bf215546Sopenharmony_ci                             LLVMVectorType(ctx->ac.i64, num_components / 2), "");
2348bf215546Sopenharmony_ci   } else if (instr->dest.ssa.bit_size == 16) {
2349bf215546Sopenharmony_ci      ret = LLVMBuildBitCast(ctx->ac.builder, ret,
2350bf215546Sopenharmony_ci                             LLVMVectorType(ctx->ac.i16, num_components * 2), "");
2351bf215546Sopenharmony_ci   } else if (instr->dest.ssa.bit_size == 8) {
2352bf215546Sopenharmony_ci      ret = LLVMBuildBitCast(ctx->ac.builder, ret,
2353bf215546Sopenharmony_ci                             LLVMVectorType(ctx->ac.i8, num_components * 4), "");
2354bf215546Sopenharmony_ci   }
2355bf215546Sopenharmony_ci
2356bf215546Sopenharmony_ci   ret = ac_trim_vector(&ctx->ac, ret, instr->num_components);
2357bf215546Sopenharmony_ci   ret = LLVMBuildBitCast(ctx->ac.builder, ret, get_def_type(ctx, &instr->dest.ssa), "");
2358bf215546Sopenharmony_ci
2359bf215546Sopenharmony_ci   return exit_waterfall(ctx, &wctx, ret);
2360bf215546Sopenharmony_ci}
2361bf215546Sopenharmony_ci
2362bf215546Sopenharmony_cistatic unsigned type_scalar_size_bytes(const struct glsl_type *type)
2363bf215546Sopenharmony_ci{
2364bf215546Sopenharmony_ci   assert(glsl_type_is_vector_or_scalar(type) || glsl_type_is_matrix(type));
2365bf215546Sopenharmony_ci   return glsl_type_is_boolean(type) ? 4 : glsl_get_bit_size(type) / 8;
2366bf215546Sopenharmony_ci}
2367bf215546Sopenharmony_ci
2368bf215546Sopenharmony_cistatic void visit_store_output(struct ac_nir_context *ctx, nir_intrinsic_instr *instr)
2369bf215546Sopenharmony_ci{
2370bf215546Sopenharmony_ci   if (ctx->ac.postponed_kill) {
2371bf215546Sopenharmony_ci      LLVMValueRef cond = LLVMBuildLoad2(ctx->ac.builder, ctx->ac.i1, ctx->ac.postponed_kill, "");
2372bf215546Sopenharmony_ci      ac_build_ifcc(&ctx->ac, cond, 7002);
2373bf215546Sopenharmony_ci   }
2374bf215546Sopenharmony_ci
2375bf215546Sopenharmony_ci   unsigned base = nir_intrinsic_base(instr);
2376bf215546Sopenharmony_ci   unsigned writemask = nir_intrinsic_write_mask(instr);
2377bf215546Sopenharmony_ci   unsigned component = nir_intrinsic_component(instr);
2378bf215546Sopenharmony_ci   LLVMValueRef src = ac_to_float(&ctx->ac, get_src(ctx, instr->src[0]));
2379bf215546Sopenharmony_ci   nir_src offset = *nir_get_io_offset_src(instr);
2380bf215546Sopenharmony_ci
2381bf215546Sopenharmony_ci   /* No indirect indexing is allowed here. */
2382bf215546Sopenharmony_ci   assert(nir_src_is_const(offset) && nir_src_as_uint(offset) == 0);
2383bf215546Sopenharmony_ci
2384bf215546Sopenharmony_ci   switch (ac_get_elem_bits(&ctx->ac, LLVMTypeOf(src))) {
2385bf215546Sopenharmony_ci   case 16:
2386bf215546Sopenharmony_ci   case 32:
2387bf215546Sopenharmony_ci      break;
2388bf215546Sopenharmony_ci   case 64:
2389bf215546Sopenharmony_ci      unreachable("64-bit IO should have been lowered to 32 bits");
2390bf215546Sopenharmony_ci      return;
2391bf215546Sopenharmony_ci   default:
2392bf215546Sopenharmony_ci      unreachable("unhandled store_output bit size");
2393bf215546Sopenharmony_ci      return;
2394bf215546Sopenharmony_ci   }
2395bf215546Sopenharmony_ci
2396bf215546Sopenharmony_ci   writemask <<= component;
2397bf215546Sopenharmony_ci
2398bf215546Sopenharmony_ci   for (unsigned chan = 0; chan < 8; chan++) {
2399bf215546Sopenharmony_ci      if (!(writemask & (1 << chan)))
2400bf215546Sopenharmony_ci         continue;
2401bf215546Sopenharmony_ci
2402bf215546Sopenharmony_ci      LLVMValueRef value = ac_llvm_extract_elem(&ctx->ac, src, chan - component);
2403bf215546Sopenharmony_ci      LLVMValueRef output_addr = ctx->abi->outputs[base * 4 + chan];
2404bf215546Sopenharmony_ci
2405bf215546Sopenharmony_ci      if (!ctx->abi->is_16bit[base * 4 + chan] &&
2406bf215546Sopenharmony_ci          LLVMTypeOf(value) == ctx->ac.f16) {
2407bf215546Sopenharmony_ci         LLVMValueRef output, index;
2408bf215546Sopenharmony_ci
2409bf215546Sopenharmony_ci         /* Insert the 16-bit value into the low or high bits of the 32-bit output
2410bf215546Sopenharmony_ci          * using read-modify-write.
2411bf215546Sopenharmony_ci          */
2412bf215546Sopenharmony_ci         index = LLVMConstInt(ctx->ac.i32, nir_intrinsic_io_semantics(instr).high_16bits, 0);
2413bf215546Sopenharmony_ci         output = LLVMBuildLoad2(ctx->ac.builder, ctx->ac.v2f16, output_addr, "");
2414bf215546Sopenharmony_ci         output = LLVMBuildInsertElement(ctx->ac.builder, output, value, index, "");
2415bf215546Sopenharmony_ci         value = LLVMBuildBitCast(ctx->ac.builder, output, ctx->ac.f32, "");
2416bf215546Sopenharmony_ci      }
2417bf215546Sopenharmony_ci      LLVMBuildStore(ctx->ac.builder, value, output_addr);
2418bf215546Sopenharmony_ci   }
2419bf215546Sopenharmony_ci
2420bf215546Sopenharmony_ci   if (ctx->ac.postponed_kill)
2421bf215546Sopenharmony_ci      ac_build_endif(&ctx->ac, 7002);
2422bf215546Sopenharmony_ci}
2423bf215546Sopenharmony_ci
2424bf215546Sopenharmony_cistatic int image_type_to_components_count(enum glsl_sampler_dim dim, bool array)
2425bf215546Sopenharmony_ci{
2426bf215546Sopenharmony_ci   switch (dim) {
2427bf215546Sopenharmony_ci   case GLSL_SAMPLER_DIM_BUF:
2428bf215546Sopenharmony_ci      return 1;
2429bf215546Sopenharmony_ci   case GLSL_SAMPLER_DIM_1D:
2430bf215546Sopenharmony_ci      return array ? 2 : 1;
2431bf215546Sopenharmony_ci   case GLSL_SAMPLER_DIM_2D:
2432bf215546Sopenharmony_ci      return array ? 3 : 2;
2433bf215546Sopenharmony_ci   case GLSL_SAMPLER_DIM_MS:
2434bf215546Sopenharmony_ci      return array ? 4 : 3;
2435bf215546Sopenharmony_ci   case GLSL_SAMPLER_DIM_3D:
2436bf215546Sopenharmony_ci   case GLSL_SAMPLER_DIM_CUBE:
2437bf215546Sopenharmony_ci      return 3;
2438bf215546Sopenharmony_ci   case GLSL_SAMPLER_DIM_RECT:
2439bf215546Sopenharmony_ci   case GLSL_SAMPLER_DIM_SUBPASS:
2440bf215546Sopenharmony_ci      return 2;
2441bf215546Sopenharmony_ci   case GLSL_SAMPLER_DIM_SUBPASS_MS:
2442bf215546Sopenharmony_ci      return 3;
2443bf215546Sopenharmony_ci   default:
2444bf215546Sopenharmony_ci      break;
2445bf215546Sopenharmony_ci   }
2446bf215546Sopenharmony_ci   return 0;
2447bf215546Sopenharmony_ci}
2448bf215546Sopenharmony_ci
2449bf215546Sopenharmony_cistatic LLVMValueRef adjust_sample_index_using_fmask(struct ac_llvm_context *ctx,
2450bf215546Sopenharmony_ci                                                    LLVMValueRef coord_x, LLVMValueRef coord_y,
2451bf215546Sopenharmony_ci                                                    LLVMValueRef coord_z, LLVMValueRef sample_index,
2452bf215546Sopenharmony_ci                                                    LLVMValueRef fmask_desc_ptr)
2453bf215546Sopenharmony_ci{
2454bf215546Sopenharmony_ci   if (!fmask_desc_ptr)
2455bf215546Sopenharmony_ci      return sample_index;
2456bf215546Sopenharmony_ci
2457bf215546Sopenharmony_ci   unsigned sample_chan = coord_z ? 3 : 2;
2458bf215546Sopenharmony_ci   LLVMValueRef addr[4] = {coord_x, coord_y, coord_z};
2459bf215546Sopenharmony_ci   addr[sample_chan] = sample_index;
2460bf215546Sopenharmony_ci
2461bf215546Sopenharmony_ci   ac_apply_fmask_to_sample(ctx, fmask_desc_ptr, addr, coord_z != NULL);
2462bf215546Sopenharmony_ci   return addr[sample_chan];
2463bf215546Sopenharmony_ci}
2464bf215546Sopenharmony_ci
2465bf215546Sopenharmony_cistatic nir_deref_instr *get_image_deref(const nir_intrinsic_instr *instr)
2466bf215546Sopenharmony_ci{
2467bf215546Sopenharmony_ci   assert(instr->src[0].is_ssa);
2468bf215546Sopenharmony_ci   return nir_instr_as_deref(instr->src[0].ssa->parent_instr);
2469bf215546Sopenharmony_ci}
2470bf215546Sopenharmony_ci
2471bf215546Sopenharmony_cistatic LLVMValueRef get_image_descriptor(struct ac_nir_context *ctx,
2472bf215546Sopenharmony_ci                                         const nir_intrinsic_instr *instr,
2473bf215546Sopenharmony_ci                                         LLVMValueRef dynamic_index,
2474bf215546Sopenharmony_ci                                         enum ac_descriptor_type desc_type, bool write)
2475bf215546Sopenharmony_ci{
2476bf215546Sopenharmony_ci   nir_deref_instr *deref_instr = instr->src[0].ssa->parent_instr->type == nir_instr_type_deref
2477bf215546Sopenharmony_ci                                     ? nir_instr_as_deref(instr->src[0].ssa->parent_instr)
2478bf215546Sopenharmony_ci                                     : NULL;
2479bf215546Sopenharmony_ci
2480bf215546Sopenharmony_ci   return get_sampler_desc(ctx, deref_instr, desc_type, &instr->instr, dynamic_index, true, write);
2481bf215546Sopenharmony_ci}
2482bf215546Sopenharmony_ci
2483bf215546Sopenharmony_cistatic void get_image_coords(struct ac_nir_context *ctx, const nir_intrinsic_instr *instr,
2484bf215546Sopenharmony_ci                             LLVMValueRef dynamic_desc_index, struct ac_image_args *args,
2485bf215546Sopenharmony_ci                             enum glsl_sampler_dim dim, bool is_array)
2486bf215546Sopenharmony_ci{
2487bf215546Sopenharmony_ci   LLVMValueRef src0 = get_src(ctx, instr->src[1]);
2488bf215546Sopenharmony_ci   LLVMValueRef masks[] = {
2489bf215546Sopenharmony_ci      LLVMConstInt(ctx->ac.i32, 0, false),
2490bf215546Sopenharmony_ci      LLVMConstInt(ctx->ac.i32, 1, false),
2491bf215546Sopenharmony_ci      LLVMConstInt(ctx->ac.i32, 2, false),
2492bf215546Sopenharmony_ci      LLVMConstInt(ctx->ac.i32, 3, false),
2493bf215546Sopenharmony_ci   };
2494bf215546Sopenharmony_ci   LLVMValueRef sample_index = ac_llvm_extract_elem(&ctx->ac, get_src(ctx, instr->src[2]), 0);
2495bf215546Sopenharmony_ci
2496bf215546Sopenharmony_ci   int count;
2497bf215546Sopenharmony_ci   ASSERTED bool add_frag_pos =
2498bf215546Sopenharmony_ci      (dim == GLSL_SAMPLER_DIM_SUBPASS || dim == GLSL_SAMPLER_DIM_SUBPASS_MS);
2499bf215546Sopenharmony_ci   bool is_ms = (dim == GLSL_SAMPLER_DIM_MS || dim == GLSL_SAMPLER_DIM_SUBPASS_MS);
2500bf215546Sopenharmony_ci   bool gfx9_1d = ctx->ac.gfx_level == GFX9 && dim == GLSL_SAMPLER_DIM_1D;
2501bf215546Sopenharmony_ci   assert(!add_frag_pos && "Input attachments should be lowered by this point.");
2502bf215546Sopenharmony_ci   count = image_type_to_components_count(dim, is_array);
2503bf215546Sopenharmony_ci
2504bf215546Sopenharmony_ci   if (ctx->ac.gfx_level < GFX11 &&
2505bf215546Sopenharmony_ci       is_ms && (instr->intrinsic == nir_intrinsic_image_deref_load ||
2506bf215546Sopenharmony_ci                 instr->intrinsic == nir_intrinsic_bindless_image_load ||
2507bf215546Sopenharmony_ci                 instr->intrinsic == nir_intrinsic_image_deref_sparse_load ||
2508bf215546Sopenharmony_ci                 instr->intrinsic == nir_intrinsic_bindless_image_sparse_load)) {
2509bf215546Sopenharmony_ci      LLVMValueRef fmask_load_address[3];
2510bf215546Sopenharmony_ci
2511bf215546Sopenharmony_ci      fmask_load_address[0] = LLVMBuildExtractElement(ctx->ac.builder, src0, masks[0], "");
2512bf215546Sopenharmony_ci      fmask_load_address[1] = LLVMBuildExtractElement(ctx->ac.builder, src0, masks[1], "");
2513bf215546Sopenharmony_ci      if (is_array)
2514bf215546Sopenharmony_ci         fmask_load_address[2] = LLVMBuildExtractElement(ctx->ac.builder, src0, masks[2], "");
2515bf215546Sopenharmony_ci      else
2516bf215546Sopenharmony_ci         fmask_load_address[2] = NULL;
2517bf215546Sopenharmony_ci
2518bf215546Sopenharmony_ci      sample_index = adjust_sample_index_using_fmask(
2519bf215546Sopenharmony_ci         &ctx->ac, fmask_load_address[0], fmask_load_address[1], fmask_load_address[2],
2520bf215546Sopenharmony_ci         sample_index, get_image_descriptor(ctx, instr, dynamic_desc_index, AC_DESC_FMASK, false));
2521bf215546Sopenharmony_ci   }
2522bf215546Sopenharmony_ci   if (count == 1 && !gfx9_1d) {
2523bf215546Sopenharmony_ci      if (instr->src[1].ssa->num_components)
2524bf215546Sopenharmony_ci         args->coords[0] = LLVMBuildExtractElement(ctx->ac.builder, src0, masks[0], "");
2525bf215546Sopenharmony_ci      else
2526bf215546Sopenharmony_ci         args->coords[0] = src0;
2527bf215546Sopenharmony_ci   } else {
2528bf215546Sopenharmony_ci      int chan;
2529bf215546Sopenharmony_ci      if (is_ms)
2530bf215546Sopenharmony_ci         count--;
2531bf215546Sopenharmony_ci      for (chan = 0; chan < count; ++chan) {
2532bf215546Sopenharmony_ci         args->coords[chan] = ac_llvm_extract_elem(&ctx->ac, src0, chan);
2533bf215546Sopenharmony_ci      }
2534bf215546Sopenharmony_ci
2535bf215546Sopenharmony_ci      if (gfx9_1d) {
2536bf215546Sopenharmony_ci         if (is_array) {
2537bf215546Sopenharmony_ci            args->coords[2] = args->coords[1];
2538bf215546Sopenharmony_ci            args->coords[1] = ctx->ac.i32_0;
2539bf215546Sopenharmony_ci         } else
2540bf215546Sopenharmony_ci            args->coords[1] = ctx->ac.i32_0;
2541bf215546Sopenharmony_ci         count++;
2542bf215546Sopenharmony_ci      }
2543bf215546Sopenharmony_ci      if (ctx->ac.gfx_level == GFX9 && dim == GLSL_SAMPLER_DIM_2D && !is_array) {
2544bf215546Sopenharmony_ci         /* The hw can't bind a slice of a 3D image as a 2D
2545bf215546Sopenharmony_ci          * image, because it ignores BASE_ARRAY if the target
2546bf215546Sopenharmony_ci          * is 3D. The workaround is to read BASE_ARRAY and set
2547bf215546Sopenharmony_ci          * it as the 3rd address operand for all 2D images.
2548bf215546Sopenharmony_ci          */
2549bf215546Sopenharmony_ci         LLVMValueRef first_layer, const5, mask;
2550bf215546Sopenharmony_ci
2551bf215546Sopenharmony_ci         const5 = LLVMConstInt(ctx->ac.i32, 5, 0);
2552bf215546Sopenharmony_ci         mask = LLVMConstInt(ctx->ac.i32, S_008F24_BASE_ARRAY(~0), 0);
2553bf215546Sopenharmony_ci         first_layer = LLVMBuildExtractElement(ctx->ac.builder, args->resource, const5, "");
2554bf215546Sopenharmony_ci         first_layer = LLVMBuildAnd(ctx->ac.builder, first_layer, mask, "");
2555bf215546Sopenharmony_ci
2556bf215546Sopenharmony_ci         args->coords[count] = first_layer;
2557bf215546Sopenharmony_ci         count++;
2558bf215546Sopenharmony_ci      }
2559bf215546Sopenharmony_ci
2560bf215546Sopenharmony_ci      if (is_ms) {
2561bf215546Sopenharmony_ci         args->coords[count] = sample_index;
2562bf215546Sopenharmony_ci         count++;
2563bf215546Sopenharmony_ci      }
2564bf215546Sopenharmony_ci   }
2565bf215546Sopenharmony_ci}
2566bf215546Sopenharmony_ci
2567bf215546Sopenharmony_cistatic LLVMValueRef enter_waterfall_image(struct ac_nir_context *ctx,
2568bf215546Sopenharmony_ci                                          struct waterfall_context *wctx,
2569bf215546Sopenharmony_ci                                          const nir_intrinsic_instr *instr)
2570bf215546Sopenharmony_ci{
2571bf215546Sopenharmony_ci   nir_deref_instr *deref_instr = NULL;
2572bf215546Sopenharmony_ci
2573bf215546Sopenharmony_ci   if (instr->src[0].ssa->parent_instr->type == nir_instr_type_deref)
2574bf215546Sopenharmony_ci      deref_instr = nir_instr_as_deref(instr->src[0].ssa->parent_instr);
2575bf215546Sopenharmony_ci
2576bf215546Sopenharmony_ci   LLVMValueRef value = get_sampler_desc_index(ctx, deref_instr, &instr->instr, true);
2577bf215546Sopenharmony_ci   return enter_waterfall(ctx, wctx, value, nir_intrinsic_access(instr) & ACCESS_NON_UNIFORM);
2578bf215546Sopenharmony_ci}
2579bf215546Sopenharmony_ci
2580bf215546Sopenharmony_cistatic LLVMValueRef visit_image_load(struct ac_nir_context *ctx, const nir_intrinsic_instr *instr,
2581bf215546Sopenharmony_ci                                     bool bindless)
2582bf215546Sopenharmony_ci{
2583bf215546Sopenharmony_ci   LLVMValueRef res;
2584bf215546Sopenharmony_ci
2585bf215546Sopenharmony_ci   enum glsl_sampler_dim dim;
2586bf215546Sopenharmony_ci   enum gl_access_qualifier access = nir_intrinsic_access(instr);
2587bf215546Sopenharmony_ci   bool is_array;
2588bf215546Sopenharmony_ci   if (bindless) {
2589bf215546Sopenharmony_ci      dim = nir_intrinsic_image_dim(instr);
2590bf215546Sopenharmony_ci      is_array = nir_intrinsic_image_array(instr);
2591bf215546Sopenharmony_ci   } else {
2592bf215546Sopenharmony_ci      const nir_deref_instr *image_deref = get_image_deref(instr);
2593bf215546Sopenharmony_ci      const struct glsl_type *type = image_deref->type;
2594bf215546Sopenharmony_ci      const nir_variable *var = nir_deref_instr_get_variable(image_deref);
2595bf215546Sopenharmony_ci      dim = glsl_get_sampler_dim(type);
2596bf215546Sopenharmony_ci      access |= var->data.access;
2597bf215546Sopenharmony_ci      is_array = glsl_sampler_type_is_array(type);
2598bf215546Sopenharmony_ci   }
2599bf215546Sopenharmony_ci
2600bf215546Sopenharmony_ci   struct waterfall_context wctx;
2601bf215546Sopenharmony_ci   LLVMValueRef dynamic_index = enter_waterfall_image(ctx, &wctx, instr);
2602bf215546Sopenharmony_ci
2603bf215546Sopenharmony_ci   struct ac_image_args args = {0};
2604bf215546Sopenharmony_ci
2605bf215546Sopenharmony_ci   args.cache_policy = get_cache_policy(ctx, access, false, false);
2606bf215546Sopenharmony_ci   args.tfe = instr->intrinsic == nir_intrinsic_image_deref_sparse_load ||
2607bf215546Sopenharmony_ci              instr->intrinsic == nir_intrinsic_bindless_image_sparse_load;
2608bf215546Sopenharmony_ci
2609bf215546Sopenharmony_ci   if (dim == GLSL_SAMPLER_DIM_BUF) {
2610bf215546Sopenharmony_ci      unsigned num_channels = util_last_bit(nir_ssa_def_components_read(&instr->dest.ssa));
2611bf215546Sopenharmony_ci      if (instr->dest.ssa.bit_size == 64)
2612bf215546Sopenharmony_ci         num_channels = num_channels < 4 ? 2 : 4;
2613bf215546Sopenharmony_ci      LLVMValueRef rsrc, vindex;
2614bf215546Sopenharmony_ci
2615bf215546Sopenharmony_ci      rsrc = get_image_descriptor(ctx, instr, dynamic_index, AC_DESC_BUFFER, false);
2616bf215546Sopenharmony_ci      vindex =
2617bf215546Sopenharmony_ci         LLVMBuildExtractElement(ctx->ac.builder, get_src(ctx, instr->src[1]), ctx->ac.i32_0, "");
2618bf215546Sopenharmony_ci
2619bf215546Sopenharmony_ci      assert(instr->dest.is_ssa);
2620bf215546Sopenharmony_ci      bool can_speculate = access & ACCESS_CAN_REORDER;
2621bf215546Sopenharmony_ci      res = ac_build_buffer_load_format(&ctx->ac, rsrc, vindex, ctx->ac.i32_0, num_channels,
2622bf215546Sopenharmony_ci                                        args.cache_policy, can_speculate,
2623bf215546Sopenharmony_ci                                        instr->dest.ssa.bit_size == 16,
2624bf215546Sopenharmony_ci                                        args.tfe);
2625bf215546Sopenharmony_ci      res = ac_build_expand(&ctx->ac, res, num_channels, args.tfe ? 5 : 4);
2626bf215546Sopenharmony_ci
2627bf215546Sopenharmony_ci      res = ac_trim_vector(&ctx->ac, res, instr->dest.ssa.num_components);
2628bf215546Sopenharmony_ci      res = ac_to_integer(&ctx->ac, res);
2629bf215546Sopenharmony_ci   } else {
2630bf215546Sopenharmony_ci      bool level_zero = nir_src_is_const(instr->src[3]) && nir_src_as_uint(instr->src[3]) == 0;
2631bf215546Sopenharmony_ci
2632bf215546Sopenharmony_ci      args.opcode = level_zero ? ac_image_load : ac_image_load_mip;
2633bf215546Sopenharmony_ci      args.resource = get_image_descriptor(ctx, instr, dynamic_index, AC_DESC_IMAGE, false);
2634bf215546Sopenharmony_ci      get_image_coords(ctx, instr, dynamic_index, &args, dim, is_array);
2635bf215546Sopenharmony_ci      args.dim = ac_get_image_dim(ctx->ac.gfx_level, dim, is_array);
2636bf215546Sopenharmony_ci      if (!level_zero)
2637bf215546Sopenharmony_ci         args.lod = get_src(ctx, instr->src[3]);
2638bf215546Sopenharmony_ci      args.dmask = 15;
2639bf215546Sopenharmony_ci      args.attributes = AC_FUNC_ATTR_READONLY;
2640bf215546Sopenharmony_ci
2641bf215546Sopenharmony_ci      assert(instr->dest.is_ssa);
2642bf215546Sopenharmony_ci      args.d16 = instr->dest.ssa.bit_size == 16;
2643bf215546Sopenharmony_ci
2644bf215546Sopenharmony_ci      res = ac_build_image_opcode(&ctx->ac, &args);
2645bf215546Sopenharmony_ci   }
2646bf215546Sopenharmony_ci
2647bf215546Sopenharmony_ci   if (instr->dest.ssa.bit_size == 64) {
2648bf215546Sopenharmony_ci      LLVMValueRef code = NULL;
2649bf215546Sopenharmony_ci      if (args.tfe) {
2650bf215546Sopenharmony_ci         code = ac_llvm_extract_elem(&ctx->ac, res, 4);
2651bf215546Sopenharmony_ci         res = ac_trim_vector(&ctx->ac, res, 4);
2652bf215546Sopenharmony_ci      }
2653bf215546Sopenharmony_ci
2654bf215546Sopenharmony_ci      res = LLVMBuildBitCast(ctx->ac.builder, res, LLVMVectorType(ctx->ac.i64, 2), "");
2655bf215546Sopenharmony_ci      LLVMValueRef x = LLVMBuildExtractElement(ctx->ac.builder, res, ctx->ac.i32_0, "");
2656bf215546Sopenharmony_ci      LLVMValueRef w = LLVMBuildExtractElement(ctx->ac.builder, res, ctx->ac.i32_1, "");
2657bf215546Sopenharmony_ci
2658bf215546Sopenharmony_ci      if (code)
2659bf215546Sopenharmony_ci         code = LLVMBuildZExt(ctx->ac.builder, code, ctx->ac.i64, "");
2660bf215546Sopenharmony_ci      LLVMValueRef values[5] = {x, ctx->ac.i64_0, ctx->ac.i64_0, w, code};
2661bf215546Sopenharmony_ci      res = ac_build_gather_values(&ctx->ac, values, 4 + args.tfe);
2662bf215546Sopenharmony_ci   }
2663bf215546Sopenharmony_ci
2664bf215546Sopenharmony_ci   return exit_waterfall(ctx, &wctx, res);
2665bf215546Sopenharmony_ci}
2666bf215546Sopenharmony_ci
2667bf215546Sopenharmony_cistatic void visit_image_store(struct ac_nir_context *ctx, const nir_intrinsic_instr *instr,
2668bf215546Sopenharmony_ci                              bool bindless)
2669bf215546Sopenharmony_ci{
2670bf215546Sopenharmony_ci   if (ctx->ac.postponed_kill) {
2671bf215546Sopenharmony_ci      LLVMValueRef cond = LLVMBuildLoad2(ctx->ac.builder, ctx->ac.i1, ctx->ac.postponed_kill, "");
2672bf215546Sopenharmony_ci      ac_build_ifcc(&ctx->ac, cond, 7003);
2673bf215546Sopenharmony_ci   }
2674bf215546Sopenharmony_ci
2675bf215546Sopenharmony_ci   enum glsl_sampler_dim dim;
2676bf215546Sopenharmony_ci   enum gl_access_qualifier access = nir_intrinsic_access(instr);
2677bf215546Sopenharmony_ci   bool is_array;
2678bf215546Sopenharmony_ci
2679bf215546Sopenharmony_ci   if (bindless) {
2680bf215546Sopenharmony_ci      dim = nir_intrinsic_image_dim(instr);
2681bf215546Sopenharmony_ci      is_array = nir_intrinsic_image_array(instr);
2682bf215546Sopenharmony_ci   } else {
2683bf215546Sopenharmony_ci      const nir_deref_instr *image_deref = get_image_deref(instr);
2684bf215546Sopenharmony_ci      const struct glsl_type *type = image_deref->type;
2685bf215546Sopenharmony_ci      const nir_variable *var = nir_deref_instr_get_variable(image_deref);
2686bf215546Sopenharmony_ci      dim = glsl_get_sampler_dim(type);
2687bf215546Sopenharmony_ci      access |= var->data.access;
2688bf215546Sopenharmony_ci      is_array = glsl_sampler_type_is_array(type);
2689bf215546Sopenharmony_ci   }
2690bf215546Sopenharmony_ci
2691bf215546Sopenharmony_ci   struct waterfall_context wctx;
2692bf215546Sopenharmony_ci   LLVMValueRef dynamic_index = enter_waterfall_image(ctx, &wctx, instr);
2693bf215546Sopenharmony_ci
2694bf215546Sopenharmony_ci   bool writeonly_memory = access & ACCESS_NON_READABLE;
2695bf215546Sopenharmony_ci   struct ac_image_args args = {0};
2696bf215546Sopenharmony_ci
2697bf215546Sopenharmony_ci   args.cache_policy = get_cache_policy(ctx, access, true, writeonly_memory);
2698bf215546Sopenharmony_ci
2699bf215546Sopenharmony_ci   LLVMValueRef src = get_src(ctx, instr->src[3]);
2700bf215546Sopenharmony_ci   if (instr->src[3].ssa->bit_size == 64) {
2701bf215546Sopenharmony_ci      /* only R64_UINT and R64_SINT supported */
2702bf215546Sopenharmony_ci      src = ac_llvm_extract_elem(&ctx->ac, src, 0);
2703bf215546Sopenharmony_ci      src = LLVMBuildBitCast(ctx->ac.builder, src, ctx->ac.v2f32, "");
2704bf215546Sopenharmony_ci   } else {
2705bf215546Sopenharmony_ci      src = ac_to_float(&ctx->ac, src);
2706bf215546Sopenharmony_ci   }
2707bf215546Sopenharmony_ci
2708bf215546Sopenharmony_ci   if (dim == GLSL_SAMPLER_DIM_BUF) {
2709bf215546Sopenharmony_ci      LLVMValueRef rsrc = get_image_descriptor(ctx, instr, dynamic_index, AC_DESC_BUFFER, true);
2710bf215546Sopenharmony_ci      unsigned src_channels = ac_get_llvm_num_components(src);
2711bf215546Sopenharmony_ci      LLVMValueRef vindex;
2712bf215546Sopenharmony_ci
2713bf215546Sopenharmony_ci      if (src_channels == 3)
2714bf215546Sopenharmony_ci         src = ac_build_expand_to_vec4(&ctx->ac, src, 3);
2715bf215546Sopenharmony_ci
2716bf215546Sopenharmony_ci      vindex =
2717bf215546Sopenharmony_ci         LLVMBuildExtractElement(ctx->ac.builder, get_src(ctx, instr->src[1]), ctx->ac.i32_0, "");
2718bf215546Sopenharmony_ci
2719bf215546Sopenharmony_ci      ac_build_buffer_store_format(&ctx->ac, rsrc, src, vindex, ctx->ac.i32_0, args.cache_policy);
2720bf215546Sopenharmony_ci   } else {
2721bf215546Sopenharmony_ci      bool level_zero = nir_src_is_const(instr->src[4]) && nir_src_as_uint(instr->src[4]) == 0;
2722bf215546Sopenharmony_ci
2723bf215546Sopenharmony_ci      args.opcode = level_zero ? ac_image_store : ac_image_store_mip;
2724bf215546Sopenharmony_ci      args.data[0] = src;
2725bf215546Sopenharmony_ci      args.resource = get_image_descriptor(ctx, instr, dynamic_index, AC_DESC_IMAGE, true);
2726bf215546Sopenharmony_ci      get_image_coords(ctx, instr, dynamic_index, &args, dim, is_array);
2727bf215546Sopenharmony_ci      args.dim = ac_get_image_dim(ctx->ac.gfx_level, dim, is_array);
2728bf215546Sopenharmony_ci      if (!level_zero)
2729bf215546Sopenharmony_ci         args.lod = get_src(ctx, instr->src[4]);
2730bf215546Sopenharmony_ci      args.dmask = 15;
2731bf215546Sopenharmony_ci      args.d16 = ac_get_elem_bits(&ctx->ac, LLVMTypeOf(args.data[0])) == 16;
2732bf215546Sopenharmony_ci
2733bf215546Sopenharmony_ci      ac_build_image_opcode(&ctx->ac, &args);
2734bf215546Sopenharmony_ci   }
2735bf215546Sopenharmony_ci
2736bf215546Sopenharmony_ci   exit_waterfall(ctx, &wctx, NULL);
2737bf215546Sopenharmony_ci   if (ctx->ac.postponed_kill)
2738bf215546Sopenharmony_ci      ac_build_endif(&ctx->ac, 7003);
2739bf215546Sopenharmony_ci}
2740bf215546Sopenharmony_ci
2741bf215546Sopenharmony_cistatic LLVMValueRef visit_image_atomic(struct ac_nir_context *ctx, const nir_intrinsic_instr *instr,
2742bf215546Sopenharmony_ci                                       bool bindless)
2743bf215546Sopenharmony_ci{
2744bf215546Sopenharmony_ci   if (ctx->ac.postponed_kill) {
2745bf215546Sopenharmony_ci      LLVMValueRef cond = LLVMBuildLoad2(ctx->ac.builder, ctx->ac.i1, ctx->ac.postponed_kill, "");
2746bf215546Sopenharmony_ci      ac_build_ifcc(&ctx->ac, cond, 7004);
2747bf215546Sopenharmony_ci   }
2748bf215546Sopenharmony_ci
2749bf215546Sopenharmony_ci   LLVMValueRef params[7];
2750bf215546Sopenharmony_ci   int param_count = 0;
2751bf215546Sopenharmony_ci
2752bf215546Sopenharmony_ci   bool cmpswap = instr->intrinsic == nir_intrinsic_image_deref_atomic_comp_swap ||
2753bf215546Sopenharmony_ci                  instr->intrinsic == nir_intrinsic_bindless_image_atomic_comp_swap;
2754bf215546Sopenharmony_ci   const char *atomic_name;
2755bf215546Sopenharmony_ci   char intrinsic_name[64];
2756bf215546Sopenharmony_ci   enum ac_atomic_op atomic_subop;
2757bf215546Sopenharmony_ci   ASSERTED int length;
2758bf215546Sopenharmony_ci
2759bf215546Sopenharmony_ci   enum glsl_sampler_dim dim;
2760bf215546Sopenharmony_ci   bool is_array;
2761bf215546Sopenharmony_ci   if (bindless) {
2762bf215546Sopenharmony_ci      dim = nir_intrinsic_image_dim(instr);
2763bf215546Sopenharmony_ci      is_array = nir_intrinsic_image_array(instr);
2764bf215546Sopenharmony_ci   } else {
2765bf215546Sopenharmony_ci      const struct glsl_type *type = get_image_deref(instr)->type;
2766bf215546Sopenharmony_ci      dim = glsl_get_sampler_dim(type);
2767bf215546Sopenharmony_ci      is_array = glsl_sampler_type_is_array(type);
2768bf215546Sopenharmony_ci   }
2769bf215546Sopenharmony_ci
2770bf215546Sopenharmony_ci   struct waterfall_context wctx;
2771bf215546Sopenharmony_ci   LLVMValueRef dynamic_index = enter_waterfall_image(ctx, &wctx, instr);
2772bf215546Sopenharmony_ci
2773bf215546Sopenharmony_ci   switch (instr->intrinsic) {
2774bf215546Sopenharmony_ci   case nir_intrinsic_bindless_image_atomic_add:
2775bf215546Sopenharmony_ci   case nir_intrinsic_image_deref_atomic_add:
2776bf215546Sopenharmony_ci      atomic_name = "add";
2777bf215546Sopenharmony_ci      atomic_subop = ac_atomic_add;
2778bf215546Sopenharmony_ci      break;
2779bf215546Sopenharmony_ci   case nir_intrinsic_bindless_image_atomic_imin:
2780bf215546Sopenharmony_ci   case nir_intrinsic_image_deref_atomic_imin:
2781bf215546Sopenharmony_ci      atomic_name = "smin";
2782bf215546Sopenharmony_ci      atomic_subop = ac_atomic_smin;
2783bf215546Sopenharmony_ci      break;
2784bf215546Sopenharmony_ci   case nir_intrinsic_bindless_image_atomic_umin:
2785bf215546Sopenharmony_ci   case nir_intrinsic_image_deref_atomic_umin:
2786bf215546Sopenharmony_ci      atomic_name = "umin";
2787bf215546Sopenharmony_ci      atomic_subop = ac_atomic_umin;
2788bf215546Sopenharmony_ci      break;
2789bf215546Sopenharmony_ci   case nir_intrinsic_bindless_image_atomic_imax:
2790bf215546Sopenharmony_ci   case nir_intrinsic_image_deref_atomic_imax:
2791bf215546Sopenharmony_ci      atomic_name = "smax";
2792bf215546Sopenharmony_ci      atomic_subop = ac_atomic_smax;
2793bf215546Sopenharmony_ci      break;
2794bf215546Sopenharmony_ci   case nir_intrinsic_bindless_image_atomic_umax:
2795bf215546Sopenharmony_ci   case nir_intrinsic_image_deref_atomic_umax:
2796bf215546Sopenharmony_ci      atomic_name = "umax";
2797bf215546Sopenharmony_ci      atomic_subop = ac_atomic_umax;
2798bf215546Sopenharmony_ci      break;
2799bf215546Sopenharmony_ci   case nir_intrinsic_bindless_image_atomic_and:
2800bf215546Sopenharmony_ci   case nir_intrinsic_image_deref_atomic_and:
2801bf215546Sopenharmony_ci      atomic_name = "and";
2802bf215546Sopenharmony_ci      atomic_subop = ac_atomic_and;
2803bf215546Sopenharmony_ci      break;
2804bf215546Sopenharmony_ci   case nir_intrinsic_bindless_image_atomic_or:
2805bf215546Sopenharmony_ci   case nir_intrinsic_image_deref_atomic_or:
2806bf215546Sopenharmony_ci      atomic_name = "or";
2807bf215546Sopenharmony_ci      atomic_subop = ac_atomic_or;
2808bf215546Sopenharmony_ci      break;
2809bf215546Sopenharmony_ci   case nir_intrinsic_bindless_image_atomic_xor:
2810bf215546Sopenharmony_ci   case nir_intrinsic_image_deref_atomic_xor:
2811bf215546Sopenharmony_ci      atomic_name = "xor";
2812bf215546Sopenharmony_ci      atomic_subop = ac_atomic_xor;
2813bf215546Sopenharmony_ci      break;
2814bf215546Sopenharmony_ci   case nir_intrinsic_bindless_image_atomic_exchange:
2815bf215546Sopenharmony_ci   case nir_intrinsic_image_deref_atomic_exchange:
2816bf215546Sopenharmony_ci      atomic_name = "swap";
2817bf215546Sopenharmony_ci      atomic_subop = ac_atomic_swap;
2818bf215546Sopenharmony_ci      break;
2819bf215546Sopenharmony_ci   case nir_intrinsic_bindless_image_atomic_comp_swap:
2820bf215546Sopenharmony_ci   case nir_intrinsic_image_deref_atomic_comp_swap:
2821bf215546Sopenharmony_ci      atomic_name = "cmpswap";
2822bf215546Sopenharmony_ci      atomic_subop = 0; /* not used */
2823bf215546Sopenharmony_ci      break;
2824bf215546Sopenharmony_ci   case nir_intrinsic_bindless_image_atomic_inc_wrap:
2825bf215546Sopenharmony_ci   case nir_intrinsic_image_deref_atomic_inc_wrap: {
2826bf215546Sopenharmony_ci      atomic_name = "inc";
2827bf215546Sopenharmony_ci      atomic_subop = ac_atomic_inc_wrap;
2828bf215546Sopenharmony_ci      break;
2829bf215546Sopenharmony_ci   }
2830bf215546Sopenharmony_ci   case nir_intrinsic_bindless_image_atomic_dec_wrap:
2831bf215546Sopenharmony_ci   case nir_intrinsic_image_deref_atomic_dec_wrap:
2832bf215546Sopenharmony_ci      atomic_name = "dec";
2833bf215546Sopenharmony_ci      atomic_subop = ac_atomic_dec_wrap;
2834bf215546Sopenharmony_ci      break;
2835bf215546Sopenharmony_ci   case nir_intrinsic_image_deref_atomic_fmin:
2836bf215546Sopenharmony_ci      atomic_name = "fmin";
2837bf215546Sopenharmony_ci      atomic_subop = ac_atomic_fmin;
2838bf215546Sopenharmony_ci      break;
2839bf215546Sopenharmony_ci   case nir_intrinsic_image_deref_atomic_fmax:
2840bf215546Sopenharmony_ci      atomic_name = "fmax";
2841bf215546Sopenharmony_ci      atomic_subop = ac_atomic_fmax;
2842bf215546Sopenharmony_ci      break;
2843bf215546Sopenharmony_ci   default:
2844bf215546Sopenharmony_ci      abort();
2845bf215546Sopenharmony_ci   }
2846bf215546Sopenharmony_ci
2847bf215546Sopenharmony_ci   if (cmpswap)
2848bf215546Sopenharmony_ci      params[param_count++] = get_src(ctx, instr->src[4]);
2849bf215546Sopenharmony_ci   params[param_count++] = get_src(ctx, instr->src[3]);
2850bf215546Sopenharmony_ci
2851bf215546Sopenharmony_ci   if (atomic_subop == ac_atomic_fmin || atomic_subop == ac_atomic_fmax)
2852bf215546Sopenharmony_ci      params[0] = ac_to_float(&ctx->ac, params[0]);
2853bf215546Sopenharmony_ci
2854bf215546Sopenharmony_ci   LLVMValueRef result;
2855bf215546Sopenharmony_ci   if (dim == GLSL_SAMPLER_DIM_BUF) {
2856bf215546Sopenharmony_ci      params[param_count++] = get_image_descriptor(ctx, instr, dynamic_index, AC_DESC_BUFFER, true);
2857bf215546Sopenharmony_ci      params[param_count++] = LLVMBuildExtractElement(ctx->ac.builder, get_src(ctx, instr->src[1]),
2858bf215546Sopenharmony_ci                                                      ctx->ac.i32_0, ""); /* vindex */
2859bf215546Sopenharmony_ci      params[param_count++] = ctx->ac.i32_0;                              /* voffset */
2860bf215546Sopenharmony_ci      if (cmpswap && instr->dest.ssa.bit_size == 64) {
2861bf215546Sopenharmony_ci         result = emit_ssbo_comp_swap_64(ctx, params[2], params[3], params[1], params[0], true);
2862bf215546Sopenharmony_ci      } else {
2863bf215546Sopenharmony_ci         LLVMTypeRef data_type = LLVMTypeOf(params[0]);
2864bf215546Sopenharmony_ci         char type[8];
2865bf215546Sopenharmony_ci
2866bf215546Sopenharmony_ci         params[param_count++] = ctx->ac.i32_0; /* soffset */
2867bf215546Sopenharmony_ci         params[param_count++] = ctx->ac.i32_0; /* slc */
2868bf215546Sopenharmony_ci
2869bf215546Sopenharmony_ci         ac_build_type_name_for_intr(data_type, type, sizeof(type));
2870bf215546Sopenharmony_ci         length = snprintf(intrinsic_name, sizeof(intrinsic_name),
2871bf215546Sopenharmony_ci                           "llvm.amdgcn.struct.buffer.atomic.%s.%s",
2872bf215546Sopenharmony_ci                           atomic_name, type);
2873bf215546Sopenharmony_ci
2874bf215546Sopenharmony_ci         assert(length < sizeof(intrinsic_name));
2875bf215546Sopenharmony_ci         result = ac_build_intrinsic(&ctx->ac, intrinsic_name, LLVMTypeOf(params[0]), params, param_count, 0);
2876bf215546Sopenharmony_ci      }
2877bf215546Sopenharmony_ci   } else {
2878bf215546Sopenharmony_ci      struct ac_image_args args = {0};
2879bf215546Sopenharmony_ci      args.opcode = cmpswap ? ac_image_atomic_cmpswap : ac_image_atomic;
2880bf215546Sopenharmony_ci      args.atomic = atomic_subop;
2881bf215546Sopenharmony_ci      args.data[0] = params[0];
2882bf215546Sopenharmony_ci      if (cmpswap)
2883bf215546Sopenharmony_ci         args.data[1] = params[1];
2884bf215546Sopenharmony_ci      args.resource = get_image_descriptor(ctx, instr, dynamic_index, AC_DESC_IMAGE, true);
2885bf215546Sopenharmony_ci      get_image_coords(ctx, instr, dynamic_index, &args, dim, is_array);
2886bf215546Sopenharmony_ci      args.dim = ac_get_image_dim(ctx->ac.gfx_level, dim, is_array);
2887bf215546Sopenharmony_ci
2888bf215546Sopenharmony_ci      result = ac_build_image_opcode(&ctx->ac, &args);
2889bf215546Sopenharmony_ci   }
2890bf215546Sopenharmony_ci
2891bf215546Sopenharmony_ci   result = exit_waterfall(ctx, &wctx, result);
2892bf215546Sopenharmony_ci   if (ctx->ac.postponed_kill)
2893bf215546Sopenharmony_ci      ac_build_endif(&ctx->ac, 7004);
2894bf215546Sopenharmony_ci   return result;
2895bf215546Sopenharmony_ci}
2896bf215546Sopenharmony_ci
2897bf215546Sopenharmony_cistatic LLVMValueRef visit_image_samples(struct ac_nir_context *ctx, nir_intrinsic_instr *instr)
2898bf215546Sopenharmony_ci{
2899bf215546Sopenharmony_ci   struct waterfall_context wctx;
2900bf215546Sopenharmony_ci   LLVMValueRef dynamic_index = enter_waterfall_image(ctx, &wctx, instr);
2901bf215546Sopenharmony_ci   LLVMValueRef rsrc = get_image_descriptor(ctx, instr, dynamic_index, AC_DESC_IMAGE, false);
2902bf215546Sopenharmony_ci
2903bf215546Sopenharmony_ci   LLVMValueRef ret = ac_build_image_get_sample_count(&ctx->ac, rsrc);
2904bf215546Sopenharmony_ci   if (ctx->abi->robust_buffer_access) {
2905bf215546Sopenharmony_ci      LLVMValueRef dword1, is_null_descriptor;
2906bf215546Sopenharmony_ci
2907bf215546Sopenharmony_ci      /* Extract the second dword of the descriptor, if it's
2908bf215546Sopenharmony_ci       * all zero, then it's a null descriptor.
2909bf215546Sopenharmony_ci       */
2910bf215546Sopenharmony_ci      dword1 =
2911bf215546Sopenharmony_ci         LLVMBuildExtractElement(ctx->ac.builder, rsrc, LLVMConstInt(ctx->ac.i32, 1, false), "");
2912bf215546Sopenharmony_ci      is_null_descriptor = LLVMBuildICmp(ctx->ac.builder, LLVMIntEQ, dword1,
2913bf215546Sopenharmony_ci                                         LLVMConstInt(ctx->ac.i32, 0, false), "");
2914bf215546Sopenharmony_ci      ret = LLVMBuildSelect(ctx->ac.builder, is_null_descriptor, ctx->ac.i32_0, ret, "");
2915bf215546Sopenharmony_ci   }
2916bf215546Sopenharmony_ci
2917bf215546Sopenharmony_ci   return exit_waterfall(ctx, &wctx, ret);
2918bf215546Sopenharmony_ci}
2919bf215546Sopenharmony_ci
2920bf215546Sopenharmony_cistatic LLVMValueRef visit_image_size(struct ac_nir_context *ctx, const nir_intrinsic_instr *instr,
2921bf215546Sopenharmony_ci                                     bool bindless)
2922bf215546Sopenharmony_ci{
2923bf215546Sopenharmony_ci   LLVMValueRef res;
2924bf215546Sopenharmony_ci
2925bf215546Sopenharmony_ci   enum glsl_sampler_dim dim;
2926bf215546Sopenharmony_ci   bool is_array;
2927bf215546Sopenharmony_ci   if (bindless) {
2928bf215546Sopenharmony_ci      dim = nir_intrinsic_image_dim(instr);
2929bf215546Sopenharmony_ci      is_array = nir_intrinsic_image_array(instr);
2930bf215546Sopenharmony_ci   } else {
2931bf215546Sopenharmony_ci      const struct glsl_type *type = get_image_deref(instr)->type;
2932bf215546Sopenharmony_ci      dim = glsl_get_sampler_dim(type);
2933bf215546Sopenharmony_ci      is_array = glsl_sampler_type_is_array(type);
2934bf215546Sopenharmony_ci   }
2935bf215546Sopenharmony_ci
2936bf215546Sopenharmony_ci   struct waterfall_context wctx;
2937bf215546Sopenharmony_ci   LLVMValueRef dynamic_index = enter_waterfall_image(ctx, &wctx, instr);
2938bf215546Sopenharmony_ci
2939bf215546Sopenharmony_ci   if (dim == GLSL_SAMPLER_DIM_BUF) {
2940bf215546Sopenharmony_ci      res = get_buffer_size(
2941bf215546Sopenharmony_ci         ctx, get_image_descriptor(ctx, instr, dynamic_index, AC_DESC_BUFFER, false), true);
2942bf215546Sopenharmony_ci   } else {
2943bf215546Sopenharmony_ci
2944bf215546Sopenharmony_ci      struct ac_image_args args = {0};
2945bf215546Sopenharmony_ci
2946bf215546Sopenharmony_ci      args.dim = ac_get_image_dim(ctx->ac.gfx_level, dim, is_array);
2947bf215546Sopenharmony_ci      args.dmask = 0xf;
2948bf215546Sopenharmony_ci      args.resource = get_image_descriptor(ctx, instr, dynamic_index, AC_DESC_IMAGE, false);
2949bf215546Sopenharmony_ci      args.opcode = ac_image_get_resinfo;
2950bf215546Sopenharmony_ci      assert(nir_src_as_uint(instr->src[1]) == 0);
2951bf215546Sopenharmony_ci      args.lod = ctx->ac.i32_0;
2952bf215546Sopenharmony_ci      args.attributes = AC_FUNC_ATTR_READNONE;
2953bf215546Sopenharmony_ci
2954bf215546Sopenharmony_ci      res = ac_build_image_opcode(&ctx->ac, &args);
2955bf215546Sopenharmony_ci
2956bf215546Sopenharmony_ci      if (ctx->ac.gfx_level == GFX9 && dim == GLSL_SAMPLER_DIM_1D && is_array) {
2957bf215546Sopenharmony_ci         LLVMValueRef two = LLVMConstInt(ctx->ac.i32, 2, false);
2958bf215546Sopenharmony_ci         LLVMValueRef layers = LLVMBuildExtractElement(ctx->ac.builder, res, two, "");
2959bf215546Sopenharmony_ci         res = LLVMBuildInsertElement(ctx->ac.builder, res, layers, ctx->ac.i32_1, "");
2960bf215546Sopenharmony_ci      }
2961bf215546Sopenharmony_ci   }
2962bf215546Sopenharmony_ci   return exit_waterfall(ctx, &wctx, res);
2963bf215546Sopenharmony_ci}
2964bf215546Sopenharmony_ci
2965bf215546Sopenharmony_cistatic void emit_discard(struct ac_nir_context *ctx, const nir_intrinsic_instr *instr)
2966bf215546Sopenharmony_ci{
2967bf215546Sopenharmony_ci   LLVMValueRef cond;
2968bf215546Sopenharmony_ci
2969bf215546Sopenharmony_ci   if (instr->intrinsic == nir_intrinsic_discard_if ||
2970bf215546Sopenharmony_ci       instr->intrinsic == nir_intrinsic_terminate_if) {
2971bf215546Sopenharmony_ci      cond = LLVMBuildNot(ctx->ac.builder, get_src(ctx, instr->src[0]), "");
2972bf215546Sopenharmony_ci   } else {
2973bf215546Sopenharmony_ci      assert(instr->intrinsic == nir_intrinsic_discard ||
2974bf215546Sopenharmony_ci             instr->intrinsic == nir_intrinsic_terminate);
2975bf215546Sopenharmony_ci      cond = ctx->ac.i1false;
2976bf215546Sopenharmony_ci   }
2977bf215546Sopenharmony_ci
2978bf215546Sopenharmony_ci   ac_build_kill_if_false(&ctx->ac, cond);
2979bf215546Sopenharmony_ci}
2980bf215546Sopenharmony_ci
2981bf215546Sopenharmony_cistatic void emit_demote(struct ac_nir_context *ctx, const nir_intrinsic_instr *instr)
2982bf215546Sopenharmony_ci{
2983bf215546Sopenharmony_ci   LLVMValueRef cond;
2984bf215546Sopenharmony_ci
2985bf215546Sopenharmony_ci   if (instr->intrinsic == nir_intrinsic_demote_if) {
2986bf215546Sopenharmony_ci      cond = LLVMBuildNot(ctx->ac.builder, get_src(ctx, instr->src[0]), "");
2987bf215546Sopenharmony_ci   } else {
2988bf215546Sopenharmony_ci      assert(instr->intrinsic == nir_intrinsic_demote);
2989bf215546Sopenharmony_ci      cond = ctx->ac.i1false;
2990bf215546Sopenharmony_ci   }
2991bf215546Sopenharmony_ci
2992bf215546Sopenharmony_ci   if (LLVM_VERSION_MAJOR >= 13) {
2993bf215546Sopenharmony_ci      /* This demotes the pixel if the condition is false. */
2994bf215546Sopenharmony_ci      ac_build_intrinsic(&ctx->ac, "llvm.amdgcn.wqm.demote", ctx->ac.voidt, &cond, 1, 0);
2995bf215546Sopenharmony_ci      return;
2996bf215546Sopenharmony_ci   }
2997bf215546Sopenharmony_ci
2998bf215546Sopenharmony_ci   LLVMValueRef mask = LLVMBuildLoad2(ctx->ac.builder, ctx->ac.i1, ctx->ac.postponed_kill, "");
2999bf215546Sopenharmony_ci   mask = LLVMBuildAnd(ctx->ac.builder, mask, cond, "");
3000bf215546Sopenharmony_ci   LLVMBuildStore(ctx->ac.builder, mask, ctx->ac.postponed_kill);
3001bf215546Sopenharmony_ci
3002bf215546Sopenharmony_ci   if (!ctx->info->fs.needs_all_helper_invocations) {
3003bf215546Sopenharmony_ci      /* This is an optional optimization that only kills whole inactive quads.
3004bf215546Sopenharmony_ci       * It's not used when subgroup operations can possibly use all helper
3005bf215546Sopenharmony_ci       * invocations.
3006bf215546Sopenharmony_ci       */
3007bf215546Sopenharmony_ci      if (ctx->ac.flow->depth == 0) {
3008bf215546Sopenharmony_ci         ac_build_kill_if_false(&ctx->ac, ac_build_wqm_vote(&ctx->ac, cond));
3009bf215546Sopenharmony_ci      } else {
3010bf215546Sopenharmony_ci         /* amdgcn.wqm.vote doesn't work inside conditional blocks. Here's why.
3011bf215546Sopenharmony_ci          *
3012bf215546Sopenharmony_ci          * The problem is that kill(wqm.vote(0)) kills all active threads within
3013bf215546Sopenharmony_ci          * the block, which breaks the whole quad mode outside the block if
3014bf215546Sopenharmony_ci          * the conditional block has partially active quads (2x2 pixel blocks).
3015bf215546Sopenharmony_ci          * E.g. threads 0-3 are active outside the block, but only thread 0 is
3016bf215546Sopenharmony_ci          * active inside the block. Thread 0 shouldn't be killed by demote,
3017bf215546Sopenharmony_ci          * because threads 1-3 are still active outside the block.
3018bf215546Sopenharmony_ci          *
3019bf215546Sopenharmony_ci          * The fix for amdgcn.wqm.vote would be to return S_WQM((live & ~exec) | cond)
3020bf215546Sopenharmony_ci          * instead of S_WQM(cond).
3021bf215546Sopenharmony_ci          *
3022bf215546Sopenharmony_ci          * The less efficient workaround we do here is to save the kill condition
3023bf215546Sopenharmony_ci          * to a temporary (postponed_kill) and do kill(wqm.vote(cond)) after we
3024bf215546Sopenharmony_ci          * exit the conditional block.
3025bf215546Sopenharmony_ci          */
3026bf215546Sopenharmony_ci         ctx->ac.conditional_demote_seen = true;
3027bf215546Sopenharmony_ci      }
3028bf215546Sopenharmony_ci   }
3029bf215546Sopenharmony_ci}
3030bf215546Sopenharmony_ci
3031bf215546Sopenharmony_cistatic LLVMValueRef visit_load_local_invocation_index(struct ac_nir_context *ctx)
3032bf215546Sopenharmony_ci{
3033bf215546Sopenharmony_ci   if (ctx->args->tcs_wave_id.used) {
3034bf215546Sopenharmony_ci      return ac_build_imad(&ctx->ac,
3035bf215546Sopenharmony_ci                           ac_unpack_param(&ctx->ac, ac_get_arg(&ctx->ac, ctx->args->tcs_wave_id), 0, 3),
3036bf215546Sopenharmony_ci                           LLVMConstInt(ctx->ac.i32, ctx->ac.wave_size, 0),
3037bf215546Sopenharmony_ci                           ac_get_thread_id(&ctx->ac));
3038bf215546Sopenharmony_ci   } else if (ctx->args->vs_rel_patch_id.used) {
3039bf215546Sopenharmony_ci      return ac_get_arg(&ctx->ac, ctx->args->vs_rel_patch_id);
3040bf215546Sopenharmony_ci   } else if (ctx->args->merged_wave_info.used) {
3041bf215546Sopenharmony_ci      /* Thread ID in threadgroup in merged ESGS. */
3042bf215546Sopenharmony_ci      LLVMValueRef wave_id = ac_unpack_param(&ctx->ac, ac_get_arg(&ctx->ac, ctx->args->merged_wave_info), 24, 4);
3043bf215546Sopenharmony_ci      LLVMValueRef wave_size = LLVMConstInt(ctx->ac.i32, ctx->ac.wave_size, false);
3044bf215546Sopenharmony_ci      LLVMValueRef threads_before = LLVMBuildMul(ctx->ac.builder, wave_id, wave_size, "");
3045bf215546Sopenharmony_ci      return LLVMBuildAdd(ctx->ac.builder, threads_before, ac_get_thread_id(&ctx->ac), "");
3046bf215546Sopenharmony_ci   }
3047bf215546Sopenharmony_ci
3048bf215546Sopenharmony_ci   LLVMValueRef result;
3049bf215546Sopenharmony_ci   LLVMValueRef thread_id = ac_get_thread_id(&ctx->ac);
3050bf215546Sopenharmony_ci   result = LLVMBuildAnd(ctx->ac.builder, ac_get_arg(&ctx->ac, ctx->args->tg_size),
3051bf215546Sopenharmony_ci                         LLVMConstInt(ctx->ac.i32, 0xfc0, false), "");
3052bf215546Sopenharmony_ci
3053bf215546Sopenharmony_ci   if (ctx->ac.wave_size == 32)
3054bf215546Sopenharmony_ci      result = LLVMBuildLShr(ctx->ac.builder, result, LLVMConstInt(ctx->ac.i32, 1, false), "");
3055bf215546Sopenharmony_ci
3056bf215546Sopenharmony_ci   return LLVMBuildAdd(ctx->ac.builder, result, thread_id, "");
3057bf215546Sopenharmony_ci}
3058bf215546Sopenharmony_ci
3059bf215546Sopenharmony_cistatic LLVMValueRef visit_load_subgroup_id(struct ac_nir_context *ctx)
3060bf215546Sopenharmony_ci{
3061bf215546Sopenharmony_ci   if (ctx->stage == MESA_SHADER_COMPUTE) {
3062bf215546Sopenharmony_ci      LLVMValueRef result;
3063bf215546Sopenharmony_ci      result = LLVMBuildAnd(ctx->ac.builder, ac_get_arg(&ctx->ac, ctx->args->tg_size),
3064bf215546Sopenharmony_ci                            LLVMConstInt(ctx->ac.i32, 0xfc0, false), "");
3065bf215546Sopenharmony_ci      return LLVMBuildLShr(ctx->ac.builder, result, LLVMConstInt(ctx->ac.i32, 6, false), "");
3066bf215546Sopenharmony_ci   } else if (ctx->args->merged_wave_info.used) {
3067bf215546Sopenharmony_ci      return ac_unpack_param(&ctx->ac, ac_get_arg(&ctx->ac, ctx->args->merged_wave_info), 24, 4);
3068bf215546Sopenharmony_ci   } else {
3069bf215546Sopenharmony_ci      return LLVMConstInt(ctx->ac.i32, 0, false);
3070bf215546Sopenharmony_ci   }
3071bf215546Sopenharmony_ci}
3072bf215546Sopenharmony_ci
3073bf215546Sopenharmony_cistatic LLVMValueRef visit_load_num_subgroups(struct ac_nir_context *ctx)
3074bf215546Sopenharmony_ci{
3075bf215546Sopenharmony_ci   if (ctx->stage == MESA_SHADER_COMPUTE) {
3076bf215546Sopenharmony_ci      return LLVMBuildAnd(ctx->ac.builder, ac_get_arg(&ctx->ac, ctx->args->tg_size),
3077bf215546Sopenharmony_ci                          LLVMConstInt(ctx->ac.i32, 0x3f, false), "");
3078bf215546Sopenharmony_ci   } else if (ctx->args->merged_wave_info.used) {
3079bf215546Sopenharmony_ci      return ac_unpack_param(&ctx->ac, ac_get_arg(&ctx->ac, ctx->args->merged_wave_info), 28, 4);
3080bf215546Sopenharmony_ci   } else {
3081bf215546Sopenharmony_ci      return LLVMConstInt(ctx->ac.i32, 1, false);
3082bf215546Sopenharmony_ci   }
3083bf215546Sopenharmony_ci}
3084bf215546Sopenharmony_ci
3085bf215546Sopenharmony_cistatic LLVMValueRef visit_first_invocation(struct ac_nir_context *ctx)
3086bf215546Sopenharmony_ci{
3087bf215546Sopenharmony_ci   LLVMValueRef active_set = ac_build_ballot(&ctx->ac, ctx->ac.i32_1);
3088bf215546Sopenharmony_ci   const char *intr = ctx->ac.wave_size == 32 ? "llvm.cttz.i32" : "llvm.cttz.i64";
3089bf215546Sopenharmony_ci
3090bf215546Sopenharmony_ci   /* The second argument is whether cttz(0) should be defined, but we do not care. */
3091bf215546Sopenharmony_ci   LLVMValueRef args[] = {active_set, ctx->ac.i1false};
3092bf215546Sopenharmony_ci   LLVMValueRef result = ac_build_intrinsic(&ctx->ac, intr, ctx->ac.iN_wavemask, args, 2,
3093bf215546Sopenharmony_ci                                            AC_FUNC_ATTR_NOUNWIND | AC_FUNC_ATTR_READNONE);
3094bf215546Sopenharmony_ci
3095bf215546Sopenharmony_ci   return LLVMBuildTrunc(ctx->ac.builder, result, ctx->ac.i32, "");
3096bf215546Sopenharmony_ci}
3097bf215546Sopenharmony_ci
3098bf215546Sopenharmony_cistatic LLVMValueRef visit_load_shared(struct ac_nir_context *ctx, const nir_intrinsic_instr *instr)
3099bf215546Sopenharmony_ci{
3100bf215546Sopenharmony_ci   LLVMValueRef values[4], derived_ptr, index, ret;
3101bf215546Sopenharmony_ci   unsigned const_off = nir_intrinsic_base(instr);
3102bf215546Sopenharmony_ci
3103bf215546Sopenharmony_ci   LLVMTypeRef elem_type = LLVMIntTypeInContext(ctx->ac.context, instr->dest.ssa.bit_size);
3104bf215546Sopenharmony_ci   LLVMValueRef ptr = get_memory_ptr_t(ctx, instr->src[0], elem_type, const_off);
3105bf215546Sopenharmony_ci
3106bf215546Sopenharmony_ci   for (int chan = 0; chan < instr->num_components; chan++) {
3107bf215546Sopenharmony_ci      index = LLVMConstInt(ctx->ac.i32, chan, 0);
3108bf215546Sopenharmony_ci      derived_ptr = LLVMBuildGEP2(ctx->ac.builder, elem_type, ptr, &index, 1, "");
3109bf215546Sopenharmony_ci      values[chan] = LLVMBuildLoad2(ctx->ac.builder, elem_type, derived_ptr, "");
3110bf215546Sopenharmony_ci   }
3111bf215546Sopenharmony_ci
3112bf215546Sopenharmony_ci   ret = ac_build_gather_values(&ctx->ac, values, instr->num_components);
3113bf215546Sopenharmony_ci
3114bf215546Sopenharmony_ci   return LLVMBuildBitCast(ctx->ac.builder, ret, get_def_type(ctx, &instr->dest.ssa), "");
3115bf215546Sopenharmony_ci}
3116bf215546Sopenharmony_ci
3117bf215546Sopenharmony_cistatic void visit_store_shared(struct ac_nir_context *ctx, const nir_intrinsic_instr *instr)
3118bf215546Sopenharmony_ci{
3119bf215546Sopenharmony_ci   LLVMValueRef derived_ptr, data, index;
3120bf215546Sopenharmony_ci   LLVMBuilderRef builder = ctx->ac.builder;
3121bf215546Sopenharmony_ci
3122bf215546Sopenharmony_ci   unsigned const_off = nir_intrinsic_base(instr);
3123bf215546Sopenharmony_ci   LLVMTypeRef elem_type = LLVMIntTypeInContext(ctx->ac.context, instr->src[0].ssa->bit_size);
3124bf215546Sopenharmony_ci   LLVMValueRef ptr = get_memory_ptr_t(ctx, instr->src[1], elem_type, const_off);
3125bf215546Sopenharmony_ci   LLVMValueRef src = get_src(ctx, instr->src[0]);
3126bf215546Sopenharmony_ci
3127bf215546Sopenharmony_ci   int writemask = nir_intrinsic_write_mask(instr);
3128bf215546Sopenharmony_ci   for (int chan = 0; chan < 4; chan++) {
3129bf215546Sopenharmony_ci      if (!(writemask & (1 << chan))) {
3130bf215546Sopenharmony_ci         continue;
3131bf215546Sopenharmony_ci      }
3132bf215546Sopenharmony_ci      data = ac_llvm_extract_elem(&ctx->ac, src, chan);
3133bf215546Sopenharmony_ci      index = LLVMConstInt(ctx->ac.i32, chan, 0);
3134bf215546Sopenharmony_ci      derived_ptr = LLVMBuildGEP2(builder, elem_type, ptr, &index, 1, "");
3135bf215546Sopenharmony_ci      LLVMBuildStore(builder, data, derived_ptr);
3136bf215546Sopenharmony_ci   }
3137bf215546Sopenharmony_ci}
3138bf215546Sopenharmony_ci
3139bf215546Sopenharmony_cistatic LLVMValueRef visit_load_shared2_amd(struct ac_nir_context *ctx,
3140bf215546Sopenharmony_ci                                           const nir_intrinsic_instr *instr)
3141bf215546Sopenharmony_ci{
3142bf215546Sopenharmony_ci   LLVMValueRef ptr = get_memory_ptr(ctx, instr->src[0], instr->dest.ssa.bit_size, 0);
3143bf215546Sopenharmony_ci
3144bf215546Sopenharmony_ci   LLVMValueRef values[2];
3145bf215546Sopenharmony_ci   uint8_t offsets[] = {nir_intrinsic_offset0(instr), nir_intrinsic_offset1(instr)};
3146bf215546Sopenharmony_ci   unsigned stride = nir_intrinsic_st64(instr) ? 64 : 1;
3147bf215546Sopenharmony_ci   for (unsigned i = 0; i < 2; i++) {
3148bf215546Sopenharmony_ci      LLVMValueRef index = LLVMConstInt(ctx->ac.i32, offsets[i] * stride, 0);
3149bf215546Sopenharmony_ci      LLVMValueRef derived_ptr = LLVMBuildGEP(ctx->ac.builder, ptr, &index, 1, "");
3150bf215546Sopenharmony_ci      values[i] = LLVMBuildLoad(ctx->ac.builder, derived_ptr, "");
3151bf215546Sopenharmony_ci   }
3152bf215546Sopenharmony_ci
3153bf215546Sopenharmony_ci   LLVMValueRef ret = ac_build_gather_values(&ctx->ac, values, 2);
3154bf215546Sopenharmony_ci   return LLVMBuildBitCast(ctx->ac.builder, ret, get_def_type(ctx, &instr->dest.ssa), "");
3155bf215546Sopenharmony_ci}
3156bf215546Sopenharmony_ci
3157bf215546Sopenharmony_cistatic void visit_store_shared2_amd(struct ac_nir_context *ctx, const nir_intrinsic_instr *instr)
3158bf215546Sopenharmony_ci{
3159bf215546Sopenharmony_ci   LLVMValueRef ptr = get_memory_ptr(ctx, instr->src[1], instr->src[0].ssa->bit_size, 0);
3160bf215546Sopenharmony_ci   LLVMValueRef src = get_src(ctx, instr->src[0]);
3161bf215546Sopenharmony_ci
3162bf215546Sopenharmony_ci   uint8_t offsets[] = {nir_intrinsic_offset0(instr), nir_intrinsic_offset1(instr)};
3163bf215546Sopenharmony_ci   unsigned stride = nir_intrinsic_st64(instr) ? 64 : 1;
3164bf215546Sopenharmony_ci   for (unsigned i = 0; i < 2; i++) {
3165bf215546Sopenharmony_ci      LLVMValueRef index = LLVMConstInt(ctx->ac.i32, offsets[i] * stride, 0);
3166bf215546Sopenharmony_ci      LLVMValueRef derived_ptr = LLVMBuildGEP(ctx->ac.builder, ptr, &index, 1, "");
3167bf215546Sopenharmony_ci      LLVMBuildStore(ctx->ac.builder, ac_llvm_extract_elem(&ctx->ac, src, i), derived_ptr);
3168bf215546Sopenharmony_ci   }
3169bf215546Sopenharmony_ci}
3170bf215546Sopenharmony_ci
3171bf215546Sopenharmony_cistatic LLVMValueRef visit_var_atomic(struct ac_nir_context *ctx, const nir_intrinsic_instr *instr,
3172bf215546Sopenharmony_ci                                     LLVMValueRef ptr, int src_idx)
3173bf215546Sopenharmony_ci{
3174bf215546Sopenharmony_ci   if (ctx->ac.postponed_kill) {
3175bf215546Sopenharmony_ci      LLVMValueRef cond = LLVMBuildLoad2(ctx->ac.builder, ctx->ac.i1, ctx->ac.postponed_kill, "");
3176bf215546Sopenharmony_ci      ac_build_ifcc(&ctx->ac, cond, 7005);
3177bf215546Sopenharmony_ci   }
3178bf215546Sopenharmony_ci
3179bf215546Sopenharmony_ci   LLVMValueRef result;
3180bf215546Sopenharmony_ci   LLVMValueRef src = get_src(ctx, instr->src[src_idx]);
3181bf215546Sopenharmony_ci
3182bf215546Sopenharmony_ci   const char *sync_scope = "workgroup-one-as";
3183bf215546Sopenharmony_ci
3184bf215546Sopenharmony_ci   if (instr->intrinsic == nir_intrinsic_shared_atomic_comp_swap) {
3185bf215546Sopenharmony_ci      LLVMValueRef src1 = get_src(ctx, instr->src[src_idx + 1]);
3186bf215546Sopenharmony_ci      result = ac_build_atomic_cmp_xchg(&ctx->ac, ptr, src, src1, sync_scope);
3187bf215546Sopenharmony_ci      result = LLVMBuildExtractValue(ctx->ac.builder, result, 0, "");
3188bf215546Sopenharmony_ci   } else if (instr->intrinsic == nir_intrinsic_shared_atomic_fmin ||
3189bf215546Sopenharmony_ci              instr->intrinsic == nir_intrinsic_shared_atomic_fmax) {
3190bf215546Sopenharmony_ci      const char *op = instr->intrinsic == nir_intrinsic_shared_atomic_fmin ? "fmin" : "fmax";
3191bf215546Sopenharmony_ci      char name[64], type[8];
3192bf215546Sopenharmony_ci      LLVMValueRef params[5];
3193bf215546Sopenharmony_ci      LLVMTypeRef src_type;
3194bf215546Sopenharmony_ci      int arg_count = 0;
3195bf215546Sopenharmony_ci
3196bf215546Sopenharmony_ci      src = ac_to_float(&ctx->ac, src);
3197bf215546Sopenharmony_ci      src_type = LLVMTypeOf(src);
3198bf215546Sopenharmony_ci
3199bf215546Sopenharmony_ci      LLVMTypeRef ptr_type =
3200bf215546Sopenharmony_ci         LLVMPointerType(src_type, LLVMGetPointerAddressSpace(LLVMTypeOf(ptr)));
3201bf215546Sopenharmony_ci      ptr = LLVMBuildBitCast(ctx->ac.builder, ptr, ptr_type, "");
3202bf215546Sopenharmony_ci
3203bf215546Sopenharmony_ci      params[arg_count++] = ptr;
3204bf215546Sopenharmony_ci      params[arg_count++] = src;
3205bf215546Sopenharmony_ci      params[arg_count++] = ctx->ac.i32_0;
3206bf215546Sopenharmony_ci      params[arg_count++] = ctx->ac.i32_0;
3207bf215546Sopenharmony_ci      params[arg_count++] = ctx->ac.i1false;
3208bf215546Sopenharmony_ci
3209bf215546Sopenharmony_ci      ac_build_type_name_for_intr(src_type, type, sizeof(type));
3210bf215546Sopenharmony_ci      snprintf(name, sizeof(name), "llvm.amdgcn.ds.%s.%s", op, type);
3211bf215546Sopenharmony_ci
3212bf215546Sopenharmony_ci      result = ac_build_intrinsic(&ctx->ac, name, src_type, params, arg_count, 0);
3213bf215546Sopenharmony_ci      result = ac_to_integer(&ctx->ac, result);
3214bf215546Sopenharmony_ci   } else {
3215bf215546Sopenharmony_ci      LLVMAtomicRMWBinOp op;
3216bf215546Sopenharmony_ci      switch (instr->intrinsic) {
3217bf215546Sopenharmony_ci      case nir_intrinsic_shared_atomic_add:
3218bf215546Sopenharmony_ci         op = LLVMAtomicRMWBinOpAdd;
3219bf215546Sopenharmony_ci         break;
3220bf215546Sopenharmony_ci      case nir_intrinsic_shared_atomic_umin:
3221bf215546Sopenharmony_ci         op = LLVMAtomicRMWBinOpUMin;
3222bf215546Sopenharmony_ci         break;
3223bf215546Sopenharmony_ci      case nir_intrinsic_shared_atomic_umax:
3224bf215546Sopenharmony_ci         op = LLVMAtomicRMWBinOpUMax;
3225bf215546Sopenharmony_ci         break;
3226bf215546Sopenharmony_ci      case nir_intrinsic_shared_atomic_imin:
3227bf215546Sopenharmony_ci         op = LLVMAtomicRMWBinOpMin;
3228bf215546Sopenharmony_ci         break;
3229bf215546Sopenharmony_ci      case nir_intrinsic_shared_atomic_imax:
3230bf215546Sopenharmony_ci         op = LLVMAtomicRMWBinOpMax;
3231bf215546Sopenharmony_ci         break;
3232bf215546Sopenharmony_ci      case nir_intrinsic_shared_atomic_and:
3233bf215546Sopenharmony_ci         op = LLVMAtomicRMWBinOpAnd;
3234bf215546Sopenharmony_ci         break;
3235bf215546Sopenharmony_ci      case nir_intrinsic_shared_atomic_or:
3236bf215546Sopenharmony_ci         op = LLVMAtomicRMWBinOpOr;
3237bf215546Sopenharmony_ci         break;
3238bf215546Sopenharmony_ci      case nir_intrinsic_shared_atomic_xor:
3239bf215546Sopenharmony_ci         op = LLVMAtomicRMWBinOpXor;
3240bf215546Sopenharmony_ci         break;
3241bf215546Sopenharmony_ci      case nir_intrinsic_shared_atomic_exchange:
3242bf215546Sopenharmony_ci         op = LLVMAtomicRMWBinOpXchg;
3243bf215546Sopenharmony_ci         break;
3244bf215546Sopenharmony_ci      case nir_intrinsic_shared_atomic_fadd:
3245bf215546Sopenharmony_ci         op = LLVMAtomicRMWBinOpFAdd;
3246bf215546Sopenharmony_ci         break;
3247bf215546Sopenharmony_ci      default:
3248bf215546Sopenharmony_ci         return NULL;
3249bf215546Sopenharmony_ci      }
3250bf215546Sopenharmony_ci
3251bf215546Sopenharmony_ci      LLVMValueRef val;
3252bf215546Sopenharmony_ci
3253bf215546Sopenharmony_ci      if (instr->intrinsic == nir_intrinsic_shared_atomic_fadd) {
3254bf215546Sopenharmony_ci         val = ac_to_float(&ctx->ac, src);
3255bf215546Sopenharmony_ci
3256bf215546Sopenharmony_ci         LLVMTypeRef ptr_type =
3257bf215546Sopenharmony_ci            LLVMPointerType(LLVMTypeOf(val), LLVMGetPointerAddressSpace(LLVMTypeOf(ptr)));
3258bf215546Sopenharmony_ci         ptr = LLVMBuildBitCast(ctx->ac.builder, ptr, ptr_type, "");
3259bf215546Sopenharmony_ci      } else {
3260bf215546Sopenharmony_ci         val = ac_to_integer(&ctx->ac, src);
3261bf215546Sopenharmony_ci      }
3262bf215546Sopenharmony_ci
3263bf215546Sopenharmony_ci      result = ac_build_atomic_rmw(&ctx->ac, op, ptr, val, sync_scope);
3264bf215546Sopenharmony_ci
3265bf215546Sopenharmony_ci      if (instr->intrinsic == nir_intrinsic_shared_atomic_fadd ||
3266bf215546Sopenharmony_ci          instr->intrinsic == nir_intrinsic_deref_atomic_fadd) {
3267bf215546Sopenharmony_ci         result = ac_to_integer(&ctx->ac, result);
3268bf215546Sopenharmony_ci      }
3269bf215546Sopenharmony_ci   }
3270bf215546Sopenharmony_ci
3271bf215546Sopenharmony_ci   if (ctx->ac.postponed_kill)
3272bf215546Sopenharmony_ci      ac_build_endif(&ctx->ac, 7005);
3273bf215546Sopenharmony_ci   return result;
3274bf215546Sopenharmony_ci}
3275bf215546Sopenharmony_ci
3276bf215546Sopenharmony_cistatic LLVMValueRef load_sample_pos(struct ac_nir_context *ctx)
3277bf215546Sopenharmony_ci{
3278bf215546Sopenharmony_ci   LLVMValueRef values[2];
3279bf215546Sopenharmony_ci   LLVMValueRef pos[2];
3280bf215546Sopenharmony_ci
3281bf215546Sopenharmony_ci   pos[0] = ac_to_float(&ctx->ac, ac_get_arg(&ctx->ac, ctx->args->frag_pos[0]));
3282bf215546Sopenharmony_ci   pos[1] = ac_to_float(&ctx->ac, ac_get_arg(&ctx->ac, ctx->args->frag_pos[1]));
3283bf215546Sopenharmony_ci
3284bf215546Sopenharmony_ci   values[0] = ac_build_fract(&ctx->ac, pos[0], 32);
3285bf215546Sopenharmony_ci   values[1] = ac_build_fract(&ctx->ac, pos[1], 32);
3286bf215546Sopenharmony_ci   return ac_build_gather_values(&ctx->ac, values, 2);
3287bf215546Sopenharmony_ci}
3288bf215546Sopenharmony_ci
3289bf215546Sopenharmony_cistatic LLVMValueRef lookup_interp_param(struct ac_nir_context *ctx, enum glsl_interp_mode interp,
3290bf215546Sopenharmony_ci                                        unsigned location)
3291bf215546Sopenharmony_ci{
3292bf215546Sopenharmony_ci   switch (interp) {
3293bf215546Sopenharmony_ci   case INTERP_MODE_FLAT:
3294bf215546Sopenharmony_ci   default:
3295bf215546Sopenharmony_ci      return NULL;
3296bf215546Sopenharmony_ci   case INTERP_MODE_SMOOTH:
3297bf215546Sopenharmony_ci   case INTERP_MODE_NONE:
3298bf215546Sopenharmony_ci      if (location == INTERP_CENTER)
3299bf215546Sopenharmony_ci         return ac_get_arg(&ctx->ac, ctx->args->persp_center);
3300bf215546Sopenharmony_ci      else if (location == INTERP_CENTROID)
3301bf215546Sopenharmony_ci         return ctx->abi->persp_centroid;
3302bf215546Sopenharmony_ci      else if (location == INTERP_SAMPLE)
3303bf215546Sopenharmony_ci         return ac_get_arg(&ctx->ac, ctx->args->persp_sample);
3304bf215546Sopenharmony_ci      break;
3305bf215546Sopenharmony_ci   case INTERP_MODE_NOPERSPECTIVE:
3306bf215546Sopenharmony_ci      if (location == INTERP_CENTER)
3307bf215546Sopenharmony_ci         return ac_get_arg(&ctx->ac, ctx->args->linear_center);
3308bf215546Sopenharmony_ci      else if (location == INTERP_CENTROID)
3309bf215546Sopenharmony_ci         return ctx->abi->linear_centroid;
3310bf215546Sopenharmony_ci      else if (location == INTERP_SAMPLE)
3311bf215546Sopenharmony_ci         return ac_get_arg(&ctx->ac, ctx->args->linear_sample);
3312bf215546Sopenharmony_ci      break;
3313bf215546Sopenharmony_ci   }
3314bf215546Sopenharmony_ci   return NULL;
3315bf215546Sopenharmony_ci}
3316bf215546Sopenharmony_ci
3317bf215546Sopenharmony_cistatic LLVMValueRef barycentric_center(struct ac_nir_context *ctx, unsigned mode)
3318bf215546Sopenharmony_ci{
3319bf215546Sopenharmony_ci   LLVMValueRef interp_param = lookup_interp_param(ctx, mode, INTERP_CENTER);
3320bf215546Sopenharmony_ci   return LLVMBuildBitCast(ctx->ac.builder, interp_param, ctx->ac.v2i32, "");
3321bf215546Sopenharmony_ci}
3322bf215546Sopenharmony_ci
3323bf215546Sopenharmony_cistatic LLVMValueRef barycentric_offset(struct ac_nir_context *ctx, unsigned mode,
3324bf215546Sopenharmony_ci                                       LLVMValueRef offset)
3325bf215546Sopenharmony_ci{
3326bf215546Sopenharmony_ci   LLVMValueRef interp_param = lookup_interp_param(ctx, mode, INTERP_CENTER);
3327bf215546Sopenharmony_ci   LLVMValueRef src_c0 =
3328bf215546Sopenharmony_ci      ac_to_float(&ctx->ac, LLVMBuildExtractElement(ctx->ac.builder, offset, ctx->ac.i32_0, ""));
3329bf215546Sopenharmony_ci   LLVMValueRef src_c1 =
3330bf215546Sopenharmony_ci      ac_to_float(&ctx->ac, LLVMBuildExtractElement(ctx->ac.builder, offset, ctx->ac.i32_1, ""));
3331bf215546Sopenharmony_ci
3332bf215546Sopenharmony_ci   LLVMValueRef ij_out[2];
3333bf215546Sopenharmony_ci   LLVMValueRef ddxy_out = ac_build_ddxy_interp(&ctx->ac, interp_param);
3334bf215546Sopenharmony_ci
3335bf215546Sopenharmony_ci   /*
3336bf215546Sopenharmony_ci    * take the I then J parameters, and the DDX/Y for it, and
3337bf215546Sopenharmony_ci    * calculate the IJ inputs for the interpolator.
3338bf215546Sopenharmony_ci    * temp1 = ddx * offset/sample.x + I;
3339bf215546Sopenharmony_ci    * interp_param.I = ddy * offset/sample.y + temp1;
3340bf215546Sopenharmony_ci    * temp1 = ddx * offset/sample.x + J;
3341bf215546Sopenharmony_ci    * interp_param.J = ddy * offset/sample.y + temp1;
3342bf215546Sopenharmony_ci    */
3343bf215546Sopenharmony_ci   for (unsigned i = 0; i < 2; i++) {
3344bf215546Sopenharmony_ci      LLVMValueRef ix_ll = LLVMConstInt(ctx->ac.i32, i, false);
3345bf215546Sopenharmony_ci      LLVMValueRef iy_ll = LLVMConstInt(ctx->ac.i32, i + 2, false);
3346bf215546Sopenharmony_ci      LLVMValueRef ddx_el = LLVMBuildExtractElement(ctx->ac.builder, ddxy_out, ix_ll, "");
3347bf215546Sopenharmony_ci      LLVMValueRef ddy_el = LLVMBuildExtractElement(ctx->ac.builder, ddxy_out, iy_ll, "");
3348bf215546Sopenharmony_ci      LLVMValueRef interp_el = LLVMBuildExtractElement(ctx->ac.builder, interp_param, ix_ll, "");
3349bf215546Sopenharmony_ci      LLVMValueRef temp1, temp2;
3350bf215546Sopenharmony_ci
3351bf215546Sopenharmony_ci      interp_el = LLVMBuildBitCast(ctx->ac.builder, interp_el, ctx->ac.f32, "");
3352bf215546Sopenharmony_ci
3353bf215546Sopenharmony_ci      temp1 = ac_build_fmad(&ctx->ac, ddx_el, src_c0, interp_el);
3354bf215546Sopenharmony_ci      temp2 = ac_build_fmad(&ctx->ac, ddy_el, src_c1, temp1);
3355bf215546Sopenharmony_ci
3356bf215546Sopenharmony_ci      ij_out[i] = LLVMBuildBitCast(ctx->ac.builder, temp2, ctx->ac.i32, "");
3357bf215546Sopenharmony_ci   }
3358bf215546Sopenharmony_ci   interp_param = ac_build_gather_values(&ctx->ac, ij_out, 2);
3359bf215546Sopenharmony_ci   return LLVMBuildBitCast(ctx->ac.builder, interp_param, ctx->ac.v2i32, "");
3360bf215546Sopenharmony_ci}
3361bf215546Sopenharmony_ci
3362bf215546Sopenharmony_cistatic LLVMValueRef barycentric_centroid(struct ac_nir_context *ctx, unsigned mode)
3363bf215546Sopenharmony_ci{
3364bf215546Sopenharmony_ci   LLVMValueRef interp_param = lookup_interp_param(ctx, mode, INTERP_CENTROID);
3365bf215546Sopenharmony_ci   return LLVMBuildBitCast(ctx->ac.builder, interp_param, ctx->ac.v2i32, "");
3366bf215546Sopenharmony_ci}
3367bf215546Sopenharmony_ci
3368bf215546Sopenharmony_cistatic LLVMValueRef barycentric_at_sample(struct ac_nir_context *ctx, unsigned mode,
3369bf215546Sopenharmony_ci                                          LLVMValueRef sample_id)
3370bf215546Sopenharmony_ci{
3371bf215546Sopenharmony_ci   if (ctx->abi->interp_at_sample_force_center)
3372bf215546Sopenharmony_ci      return barycentric_center(ctx, mode);
3373bf215546Sopenharmony_ci
3374bf215546Sopenharmony_ci   LLVMValueRef halfval = LLVMConstReal(ctx->ac.f32, 0.5f);
3375bf215546Sopenharmony_ci
3376bf215546Sopenharmony_ci   /* fetch sample ID */
3377bf215546Sopenharmony_ci   LLVMValueRef sample_pos = ctx->abi->load_sample_position(ctx->abi, sample_id);
3378bf215546Sopenharmony_ci
3379bf215546Sopenharmony_ci   LLVMValueRef src_c0 = LLVMBuildExtractElement(ctx->ac.builder, sample_pos, ctx->ac.i32_0, "");
3380bf215546Sopenharmony_ci   src_c0 = LLVMBuildFSub(ctx->ac.builder, src_c0, halfval, "");
3381bf215546Sopenharmony_ci   LLVMValueRef src_c1 = LLVMBuildExtractElement(ctx->ac.builder, sample_pos, ctx->ac.i32_1, "");
3382bf215546Sopenharmony_ci   src_c1 = LLVMBuildFSub(ctx->ac.builder, src_c1, halfval, "");
3383bf215546Sopenharmony_ci   LLVMValueRef coords[] = {src_c0, src_c1};
3384bf215546Sopenharmony_ci   LLVMValueRef offset = ac_build_gather_values(&ctx->ac, coords, 2);
3385bf215546Sopenharmony_ci
3386bf215546Sopenharmony_ci   return barycentric_offset(ctx, mode, offset);
3387bf215546Sopenharmony_ci}
3388bf215546Sopenharmony_ci
3389bf215546Sopenharmony_cistatic LLVMValueRef barycentric_sample(struct ac_nir_context *ctx, unsigned mode)
3390bf215546Sopenharmony_ci{
3391bf215546Sopenharmony_ci   LLVMValueRef interp_param = lookup_interp_param(ctx, mode, INTERP_SAMPLE);
3392bf215546Sopenharmony_ci   return LLVMBuildBitCast(ctx->ac.builder, interp_param, ctx->ac.v2i32, "");
3393bf215546Sopenharmony_ci}
3394bf215546Sopenharmony_ci
3395bf215546Sopenharmony_cistatic LLVMValueRef barycentric_model(struct ac_nir_context *ctx)
3396bf215546Sopenharmony_ci{
3397bf215546Sopenharmony_ci   return LLVMBuildBitCast(ctx->ac.builder, ac_get_arg(&ctx->ac, ctx->args->pull_model),
3398bf215546Sopenharmony_ci                           ctx->ac.v3i32, "");
3399bf215546Sopenharmony_ci}
3400bf215546Sopenharmony_ci
3401bf215546Sopenharmony_cistatic LLVMValueRef load_interpolated_input(struct ac_nir_context *ctx, LLVMValueRef interp_param,
3402bf215546Sopenharmony_ci                                            unsigned index, unsigned comp_start,
3403bf215546Sopenharmony_ci                                            unsigned num_components, unsigned bitsize,
3404bf215546Sopenharmony_ci                                            bool high_16bits)
3405bf215546Sopenharmony_ci{
3406bf215546Sopenharmony_ci   LLVMValueRef attr_number = LLVMConstInt(ctx->ac.i32, index, false);
3407bf215546Sopenharmony_ci   LLVMValueRef interp_param_f;
3408bf215546Sopenharmony_ci
3409bf215546Sopenharmony_ci   interp_param_f = LLVMBuildBitCast(ctx->ac.builder, interp_param, ctx->ac.v2f32, "");
3410bf215546Sopenharmony_ci   LLVMValueRef i = LLVMBuildExtractElement(ctx->ac.builder, interp_param_f, ctx->ac.i32_0, "");
3411bf215546Sopenharmony_ci   LLVMValueRef j = LLVMBuildExtractElement(ctx->ac.builder, interp_param_f, ctx->ac.i32_1, "");
3412bf215546Sopenharmony_ci
3413bf215546Sopenharmony_ci   /* Workaround for issue 2647: kill threads with infinite interpolation coeffs */
3414bf215546Sopenharmony_ci   if (ctx->verified_interp && !_mesa_hash_table_search(ctx->verified_interp, interp_param)) {
3415bf215546Sopenharmony_ci      LLVMValueRef cond = ac_build_is_inf_or_nan(&ctx->ac, i);
3416bf215546Sopenharmony_ci      ac_build_kill_if_false(&ctx->ac, LLVMBuildNot(ctx->ac.builder, cond, ""));
3417bf215546Sopenharmony_ci      _mesa_hash_table_insert(ctx->verified_interp, interp_param, interp_param);
3418bf215546Sopenharmony_ci   }
3419bf215546Sopenharmony_ci
3420bf215546Sopenharmony_ci   LLVMValueRef values[4];
3421bf215546Sopenharmony_ci   assert(bitsize == 16 || bitsize == 32);
3422bf215546Sopenharmony_ci   for (unsigned comp = 0; comp < num_components; comp++) {
3423bf215546Sopenharmony_ci      LLVMValueRef llvm_chan = LLVMConstInt(ctx->ac.i32, comp_start + comp, false);
3424bf215546Sopenharmony_ci      if (bitsize == 16) {
3425bf215546Sopenharmony_ci         values[comp] = ac_build_fs_interp_f16(&ctx->ac, llvm_chan, attr_number,
3426bf215546Sopenharmony_ci                                               ac_get_arg(&ctx->ac, ctx->args->prim_mask), i, j,
3427bf215546Sopenharmony_ci                                               high_16bits);
3428bf215546Sopenharmony_ci      } else {
3429bf215546Sopenharmony_ci         values[comp] = ac_build_fs_interp(&ctx->ac, llvm_chan, attr_number,
3430bf215546Sopenharmony_ci                                           ac_get_arg(&ctx->ac, ctx->args->prim_mask), i, j);
3431bf215546Sopenharmony_ci      }
3432bf215546Sopenharmony_ci   }
3433bf215546Sopenharmony_ci
3434bf215546Sopenharmony_ci   return ac_to_integer(&ctx->ac, ac_build_gather_values(&ctx->ac, values, num_components));
3435bf215546Sopenharmony_ci}
3436bf215546Sopenharmony_ci
3437bf215546Sopenharmony_cistatic LLVMValueRef visit_load(struct ac_nir_context *ctx, nir_intrinsic_instr *instr,
3438bf215546Sopenharmony_ci                               bool is_output)
3439bf215546Sopenharmony_ci{
3440bf215546Sopenharmony_ci   LLVMValueRef values[8];
3441bf215546Sopenharmony_ci   LLVMTypeRef dest_type = get_def_type(ctx, &instr->dest.ssa);
3442bf215546Sopenharmony_ci   LLVMTypeRef component_type;
3443bf215546Sopenharmony_ci   unsigned base = nir_intrinsic_base(instr);
3444bf215546Sopenharmony_ci   unsigned component = nir_intrinsic_component(instr);
3445bf215546Sopenharmony_ci   unsigned count = instr->dest.ssa.num_components;
3446bf215546Sopenharmony_ci   nir_src *vertex_index_src = nir_get_io_arrayed_index_src(instr);
3447bf215546Sopenharmony_ci   LLVMValueRef vertex_index = vertex_index_src ? get_src(ctx, *vertex_index_src) : NULL;
3448bf215546Sopenharmony_ci   nir_src offset = *nir_get_io_offset_src(instr);
3449bf215546Sopenharmony_ci   LLVMValueRef indir_index = NULL;
3450bf215546Sopenharmony_ci
3451bf215546Sopenharmony_ci   switch (instr->dest.ssa.bit_size) {
3452bf215546Sopenharmony_ci   case 16:
3453bf215546Sopenharmony_ci   case 32:
3454bf215546Sopenharmony_ci      break;
3455bf215546Sopenharmony_ci   case 64:
3456bf215546Sopenharmony_ci      unreachable("64-bit IO should have been lowered");
3457bf215546Sopenharmony_ci      return NULL;
3458bf215546Sopenharmony_ci   default:
3459bf215546Sopenharmony_ci      unreachable("unhandled load type");
3460bf215546Sopenharmony_ci      return NULL;
3461bf215546Sopenharmony_ci   }
3462bf215546Sopenharmony_ci
3463bf215546Sopenharmony_ci   if (LLVMGetTypeKind(dest_type) == LLVMVectorTypeKind)
3464bf215546Sopenharmony_ci      component_type = LLVMGetElementType(dest_type);
3465bf215546Sopenharmony_ci   else
3466bf215546Sopenharmony_ci      component_type = dest_type;
3467bf215546Sopenharmony_ci
3468bf215546Sopenharmony_ci   if (nir_src_is_const(offset))
3469bf215546Sopenharmony_ci      assert(nir_src_as_uint(offset) == 0);
3470bf215546Sopenharmony_ci   else
3471bf215546Sopenharmony_ci      indir_index = get_src(ctx, offset);
3472bf215546Sopenharmony_ci
3473bf215546Sopenharmony_ci   if (ctx->stage == MESA_SHADER_TESS_CTRL) {
3474bf215546Sopenharmony_ci      LLVMValueRef result = ctx->abi->load_tess_varyings(ctx->abi, component_type,
3475bf215546Sopenharmony_ci                                                         vertex_index, indir_index,
3476bf215546Sopenharmony_ci                                                         base, component,
3477bf215546Sopenharmony_ci                                                         count, !is_output);
3478bf215546Sopenharmony_ci      if (instr->dest.ssa.bit_size == 16) {
3479bf215546Sopenharmony_ci         result = ac_to_integer(&ctx->ac, result);
3480bf215546Sopenharmony_ci         result = LLVMBuildTrunc(ctx->ac.builder, result, dest_type, "");
3481bf215546Sopenharmony_ci      }
3482bf215546Sopenharmony_ci      return LLVMBuildBitCast(ctx->ac.builder, result, dest_type, "");
3483bf215546Sopenharmony_ci   }
3484bf215546Sopenharmony_ci
3485bf215546Sopenharmony_ci   /* No indirect indexing is allowed after this point. */
3486bf215546Sopenharmony_ci   assert(!indir_index);
3487bf215546Sopenharmony_ci
3488bf215546Sopenharmony_ci   if (ctx->stage == MESA_SHADER_FRAGMENT && is_output &&
3489bf215546Sopenharmony_ci       nir_intrinsic_io_semantics(instr).fb_fetch_output)
3490bf215546Sopenharmony_ci      return ctx->abi->emit_fbfetch(ctx->abi);
3491bf215546Sopenharmony_ci
3492bf215546Sopenharmony_ci   if (ctx->stage == MESA_SHADER_VERTEX && !is_output)
3493bf215546Sopenharmony_ci      return ctx->abi->load_inputs(ctx->abi, base, component, count, 0, component_type);
3494bf215546Sopenharmony_ci
3495bf215546Sopenharmony_ci   /* Other non-fragment cases have outputs in temporaries. */
3496bf215546Sopenharmony_ci   if (is_output && (ctx->stage == MESA_SHADER_VERTEX || ctx->stage == MESA_SHADER_TESS_EVAL)) {
3497bf215546Sopenharmony_ci      assert(is_output);
3498bf215546Sopenharmony_ci
3499bf215546Sopenharmony_ci      for (unsigned chan = component; chan < count + component; chan++)
3500bf215546Sopenharmony_ci         values[chan] = LLVMBuildLoad2(ctx->ac.builder, ctx->ac.f32,
3501bf215546Sopenharmony_ci                                       ctx->abi->outputs[base * 4 + chan], "");
3502bf215546Sopenharmony_ci
3503bf215546Sopenharmony_ci      LLVMValueRef result = ac_build_varying_gather_values(&ctx->ac, values, count, component);
3504bf215546Sopenharmony_ci      return LLVMBuildBitCast(ctx->ac.builder, result, dest_type, "");
3505bf215546Sopenharmony_ci   }
3506bf215546Sopenharmony_ci
3507bf215546Sopenharmony_ci   /* Fragment shader inputs. */
3508bf215546Sopenharmony_ci   assert(ctx->stage == MESA_SHADER_FRAGMENT);
3509bf215546Sopenharmony_ci   unsigned vertex_id = 2; /* P0 */
3510bf215546Sopenharmony_ci
3511bf215546Sopenharmony_ci   if (instr->intrinsic == nir_intrinsic_load_input_vertex) {
3512bf215546Sopenharmony_ci      nir_const_value *src0 = nir_src_as_const_value(instr->src[0]);
3513bf215546Sopenharmony_ci
3514bf215546Sopenharmony_ci      switch (src0[0].i32) {
3515bf215546Sopenharmony_ci      case 0:
3516bf215546Sopenharmony_ci         vertex_id = 2;
3517bf215546Sopenharmony_ci         break;
3518bf215546Sopenharmony_ci      case 1:
3519bf215546Sopenharmony_ci         vertex_id = 0;
3520bf215546Sopenharmony_ci         break;
3521bf215546Sopenharmony_ci      case 2:
3522bf215546Sopenharmony_ci         vertex_id = 1;
3523bf215546Sopenharmony_ci         break;
3524bf215546Sopenharmony_ci      default:
3525bf215546Sopenharmony_ci         unreachable("Invalid vertex index");
3526bf215546Sopenharmony_ci      }
3527bf215546Sopenharmony_ci   }
3528bf215546Sopenharmony_ci
3529bf215546Sopenharmony_ci   LLVMValueRef attr_number = LLVMConstInt(ctx->ac.i32, base, false);
3530bf215546Sopenharmony_ci
3531bf215546Sopenharmony_ci   for (unsigned chan = 0; chan < count; chan++) {
3532bf215546Sopenharmony_ci      LLVMValueRef llvm_chan = LLVMConstInt(ctx->ac.i32, (component + chan) % 4, false);
3533bf215546Sopenharmony_ci      values[chan] =
3534bf215546Sopenharmony_ci         ac_build_fs_interp_mov(&ctx->ac, LLVMConstInt(ctx->ac.i32, vertex_id, false), llvm_chan,
3535bf215546Sopenharmony_ci                                attr_number, ac_get_arg(&ctx->ac, ctx->args->prim_mask));
3536bf215546Sopenharmony_ci      values[chan] = LLVMBuildBitCast(ctx->ac.builder, values[chan], ctx->ac.i32, "");
3537bf215546Sopenharmony_ci      if (instr->dest.ssa.bit_size == 16 &&
3538bf215546Sopenharmony_ci          nir_intrinsic_io_semantics(instr).high_16bits)
3539bf215546Sopenharmony_ci         values[chan] = LLVMBuildLShr(ctx->ac.builder, values[chan], LLVMConstInt(ctx->ac.i32, 16, 0), "");
3540bf215546Sopenharmony_ci      values[chan] =
3541bf215546Sopenharmony_ci         LLVMBuildTruncOrBitCast(ctx->ac.builder, values[chan],
3542bf215546Sopenharmony_ci                                 instr->dest.ssa.bit_size == 16 ? ctx->ac.i16 : ctx->ac.i32, "");
3543bf215546Sopenharmony_ci   }
3544bf215546Sopenharmony_ci
3545bf215546Sopenharmony_ci   LLVMValueRef result = ac_build_gather_values(&ctx->ac, values, count);
3546bf215546Sopenharmony_ci   return LLVMBuildBitCast(ctx->ac.builder, result, dest_type, "");
3547bf215546Sopenharmony_ci}
3548bf215546Sopenharmony_ci
3549bf215546Sopenharmony_cistatic LLVMValueRef
3550bf215546Sopenharmony_ciemit_load_frag_shading_rate(struct ac_nir_context *ctx)
3551bf215546Sopenharmony_ci{
3552bf215546Sopenharmony_ci   LLVMValueRef x_rate, y_rate, cond;
3553bf215546Sopenharmony_ci
3554bf215546Sopenharmony_ci   /* VRS Rate X = Ancillary[2:3]
3555bf215546Sopenharmony_ci    * VRS Rate Y = Ancillary[4:5]
3556bf215546Sopenharmony_ci    */
3557bf215546Sopenharmony_ci   x_rate = ac_unpack_param(&ctx->ac, ac_get_arg(&ctx->ac, ctx->args->ancillary), 2, 2);
3558bf215546Sopenharmony_ci   y_rate = ac_unpack_param(&ctx->ac, ac_get_arg(&ctx->ac, ctx->args->ancillary), 4, 2);
3559bf215546Sopenharmony_ci
3560bf215546Sopenharmony_ci   /* xRate = xRate == 0x1 ? Horizontal2Pixels : None. */
3561bf215546Sopenharmony_ci   cond = LLVMBuildICmp(ctx->ac.builder, LLVMIntEQ, x_rate, ctx->ac.i32_1, "");
3562bf215546Sopenharmony_ci   x_rate = LLVMBuildSelect(ctx->ac.builder, cond,
3563bf215546Sopenharmony_ci                            LLVMConstInt(ctx->ac.i32, 4, false), ctx->ac.i32_0, "");
3564bf215546Sopenharmony_ci
3565bf215546Sopenharmony_ci   /* yRate = yRate == 0x1 ? Vertical2Pixels : None. */
3566bf215546Sopenharmony_ci   cond = LLVMBuildICmp(ctx->ac.builder, LLVMIntEQ, y_rate, ctx->ac.i32_1, "");
3567bf215546Sopenharmony_ci   y_rate = LLVMBuildSelect(ctx->ac.builder, cond,
3568bf215546Sopenharmony_ci                            LLVMConstInt(ctx->ac.i32, 1, false), ctx->ac.i32_0, "");
3569bf215546Sopenharmony_ci
3570bf215546Sopenharmony_ci   return LLVMBuildOr(ctx->ac.builder, x_rate, y_rate, "");
3571bf215546Sopenharmony_ci}
3572bf215546Sopenharmony_ci
3573bf215546Sopenharmony_cistatic LLVMValueRef
3574bf215546Sopenharmony_ciemit_load_frag_coord(struct ac_nir_context *ctx)
3575bf215546Sopenharmony_ci{
3576bf215546Sopenharmony_ci   LLVMValueRef values[4] = {
3577bf215546Sopenharmony_ci      ac_get_arg(&ctx->ac, ctx->args->frag_pos[0]), ac_get_arg(&ctx->ac, ctx->args->frag_pos[1]),
3578bf215546Sopenharmony_ci      ac_get_arg(&ctx->ac, ctx->args->frag_pos[2]),
3579bf215546Sopenharmony_ci      ac_build_fdiv(&ctx->ac, ctx->ac.f32_1, ac_get_arg(&ctx->ac, ctx->args->frag_pos[3]))};
3580bf215546Sopenharmony_ci
3581bf215546Sopenharmony_ci   return ac_to_integer(&ctx->ac, ac_build_gather_values(&ctx->ac, values, 4));
3582bf215546Sopenharmony_ci}
3583bf215546Sopenharmony_ci
3584bf215546Sopenharmony_cistatic void visit_intrinsic(struct ac_nir_context *ctx, nir_intrinsic_instr *instr)
3585bf215546Sopenharmony_ci{
3586bf215546Sopenharmony_ci   LLVMValueRef result = NULL;
3587bf215546Sopenharmony_ci
3588bf215546Sopenharmony_ci   switch (instr->intrinsic) {
3589bf215546Sopenharmony_ci   case nir_intrinsic_ballot:
3590bf215546Sopenharmony_ci      result = ac_build_ballot(&ctx->ac, get_src(ctx, instr->src[0]));
3591bf215546Sopenharmony_ci      if (ctx->ac.ballot_mask_bits > ctx->ac.wave_size)
3592bf215546Sopenharmony_ci         result = LLVMBuildZExt(ctx->ac.builder, result, ctx->ac.iN_ballotmask, "");
3593bf215546Sopenharmony_ci      break;
3594bf215546Sopenharmony_ci   case nir_intrinsic_read_invocation:
3595bf215546Sopenharmony_ci      result =
3596bf215546Sopenharmony_ci         ac_build_readlane(&ctx->ac, get_src(ctx, instr->src[0]), get_src(ctx, instr->src[1]));
3597bf215546Sopenharmony_ci      break;
3598bf215546Sopenharmony_ci   case nir_intrinsic_read_first_invocation:
3599bf215546Sopenharmony_ci      result = ac_build_readlane(&ctx->ac, get_src(ctx, instr->src[0]), NULL);
3600bf215546Sopenharmony_ci      break;
3601bf215546Sopenharmony_ci   case nir_intrinsic_load_subgroup_invocation:
3602bf215546Sopenharmony_ci      result = ac_get_thread_id(&ctx->ac);
3603bf215546Sopenharmony_ci      break;
3604bf215546Sopenharmony_ci   case nir_intrinsic_load_workgroup_id: {
3605bf215546Sopenharmony_ci      LLVMValueRef values[3];
3606bf215546Sopenharmony_ci
3607bf215546Sopenharmony_ci      for (int i = 0; i < 3; i++) {
3608bf215546Sopenharmony_ci         values[i] = ctx->args->workgroup_ids[i].used
3609bf215546Sopenharmony_ci                        ? ac_get_arg(&ctx->ac, ctx->args->workgroup_ids[i])
3610bf215546Sopenharmony_ci                        : ctx->ac.i32_0;
3611bf215546Sopenharmony_ci      }
3612bf215546Sopenharmony_ci
3613bf215546Sopenharmony_ci      result = ac_build_gather_values(&ctx->ac, values, 3);
3614bf215546Sopenharmony_ci      break;
3615bf215546Sopenharmony_ci   }
3616bf215546Sopenharmony_ci   case nir_intrinsic_load_base_vertex:
3617bf215546Sopenharmony_ci   case nir_intrinsic_load_first_vertex:
3618bf215546Sopenharmony_ci   case nir_intrinsic_load_workgroup_size:
3619bf215546Sopenharmony_ci   case nir_intrinsic_load_tess_level_outer_default:
3620bf215546Sopenharmony_ci   case nir_intrinsic_load_tess_level_inner_default:
3621bf215546Sopenharmony_ci   case nir_intrinsic_load_tess_rel_patch_id_amd:
3622bf215546Sopenharmony_ci   case nir_intrinsic_load_patch_vertices_in:
3623bf215546Sopenharmony_ci   case nir_intrinsic_load_sample_mask_in:
3624bf215546Sopenharmony_ci   case nir_intrinsic_load_ring_tess_factors_amd:
3625bf215546Sopenharmony_ci   case nir_intrinsic_load_ring_tess_offchip_amd:
3626bf215546Sopenharmony_ci   case nir_intrinsic_load_ring_tess_offchip_offset_amd:
3627bf215546Sopenharmony_ci   case nir_intrinsic_load_ring_esgs_amd:
3628bf215546Sopenharmony_ci   case nir_intrinsic_load_ring_es2gs_offset_amd:
3629bf215546Sopenharmony_ci   case nir_intrinsic_load_lshs_vertex_stride_amd:
3630bf215546Sopenharmony_ci   case nir_intrinsic_load_tcs_num_patches_amd:
3631bf215546Sopenharmony_ci   case nir_intrinsic_load_hs_out_patch_data_offset_amd:
3632bf215546Sopenharmony_ci      result = ctx->abi->intrinsic_load(ctx->abi, instr->intrinsic);
3633bf215546Sopenharmony_ci      break;
3634bf215546Sopenharmony_ci   case nir_intrinsic_load_vertex_id_zero_base: {
3635bf215546Sopenharmony_ci      result = ctx->vertex_id_replaced ? ctx->vertex_id_replaced : ctx->abi->vertex_id;
3636bf215546Sopenharmony_ci      break;
3637bf215546Sopenharmony_ci   }
3638bf215546Sopenharmony_ci   case nir_intrinsic_load_local_invocation_id: {
3639bf215546Sopenharmony_ci      LLVMValueRef ids = ac_get_arg(&ctx->ac, ctx->args->local_invocation_ids);
3640bf215546Sopenharmony_ci
3641bf215546Sopenharmony_ci      if (LLVMGetTypeKind(LLVMTypeOf(ids)) == LLVMIntegerTypeKind) {
3642bf215546Sopenharmony_ci         /* Thread IDs are packed in VGPR0, 10 bits per component. */
3643bf215546Sopenharmony_ci         LLVMValueRef id[3];
3644bf215546Sopenharmony_ci
3645bf215546Sopenharmony_ci         for (unsigned i = 0; i < 3; i++)
3646bf215546Sopenharmony_ci            id[i] = ac_unpack_param(&ctx->ac, ids, i * 10, 10);
3647bf215546Sopenharmony_ci
3648bf215546Sopenharmony_ci         result = ac_build_gather_values(&ctx->ac, id, 3);
3649bf215546Sopenharmony_ci      } else {
3650bf215546Sopenharmony_ci         result = ids;
3651bf215546Sopenharmony_ci      }
3652bf215546Sopenharmony_ci      break;
3653bf215546Sopenharmony_ci   }
3654bf215546Sopenharmony_ci   case nir_intrinsic_load_base_instance:
3655bf215546Sopenharmony_ci      result = ac_get_arg(&ctx->ac, ctx->args->start_instance);
3656bf215546Sopenharmony_ci      break;
3657bf215546Sopenharmony_ci   case nir_intrinsic_load_draw_id:
3658bf215546Sopenharmony_ci      result = ac_get_arg(&ctx->ac, ctx->args->draw_id);
3659bf215546Sopenharmony_ci      break;
3660bf215546Sopenharmony_ci   case nir_intrinsic_load_view_index:
3661bf215546Sopenharmony_ci      result = ac_get_arg(&ctx->ac, ctx->args->view_index);
3662bf215546Sopenharmony_ci      break;
3663bf215546Sopenharmony_ci   case nir_intrinsic_load_invocation_id:
3664bf215546Sopenharmony_ci      if (ctx->stage == MESA_SHADER_TESS_CTRL) {
3665bf215546Sopenharmony_ci         result = ac_unpack_param(&ctx->ac, ac_get_arg(&ctx->ac, ctx->args->tcs_rel_ids), 8, 5);
3666bf215546Sopenharmony_ci      } else {
3667bf215546Sopenharmony_ci         if (ctx->ac.gfx_level >= GFX10) {
3668bf215546Sopenharmony_ci            result =
3669bf215546Sopenharmony_ci               LLVMBuildAnd(ctx->ac.builder, ac_get_arg(&ctx->ac, ctx->args->gs_invocation_id),
3670bf215546Sopenharmony_ci                            LLVMConstInt(ctx->ac.i32, 127, 0), "");
3671bf215546Sopenharmony_ci         } else {
3672bf215546Sopenharmony_ci            result = ac_get_arg(&ctx->ac, ctx->args->gs_invocation_id);
3673bf215546Sopenharmony_ci         }
3674bf215546Sopenharmony_ci      }
3675bf215546Sopenharmony_ci      break;
3676bf215546Sopenharmony_ci   case nir_intrinsic_load_primitive_id:
3677bf215546Sopenharmony_ci      if (ctx->stage == MESA_SHADER_GEOMETRY) {
3678bf215546Sopenharmony_ci         result = ac_get_arg(&ctx->ac, ctx->args->gs_prim_id);
3679bf215546Sopenharmony_ci      } else if (ctx->stage == MESA_SHADER_TESS_CTRL) {
3680bf215546Sopenharmony_ci         result = ac_get_arg(&ctx->ac, ctx->args->tcs_patch_id);
3681bf215546Sopenharmony_ci      } else if (ctx->stage == MESA_SHADER_TESS_EVAL) {
3682bf215546Sopenharmony_ci         result = ctx->tes_patch_id_replaced ? ctx->tes_patch_id_replaced
3683bf215546Sopenharmony_ci                                             : ac_get_arg(&ctx->ac, ctx->args->tes_patch_id);
3684bf215546Sopenharmony_ci      } else if (ctx->stage == MESA_SHADER_VERTEX) {
3685bf215546Sopenharmony_ci         if (ctx->args->vs_prim_id.used)
3686bf215546Sopenharmony_ci            result = ac_get_arg(&ctx->ac, ctx->args->vs_prim_id); /* legacy */
3687bf215546Sopenharmony_ci         else
3688bf215546Sopenharmony_ci            result = ac_get_arg(&ctx->ac, ctx->args->gs_prim_id); /* NGG */
3689bf215546Sopenharmony_ci      } else
3690bf215546Sopenharmony_ci         fprintf(stderr, "Unknown primitive id intrinsic: %d", ctx->stage);
3691bf215546Sopenharmony_ci      break;
3692bf215546Sopenharmony_ci   case nir_intrinsic_load_sample_id:
3693bf215546Sopenharmony_ci      result = ac_unpack_param(&ctx->ac, ac_get_arg(&ctx->ac, ctx->args->ancillary), 8, 4);
3694bf215546Sopenharmony_ci      break;
3695bf215546Sopenharmony_ci   case nir_intrinsic_load_sample_pos:
3696bf215546Sopenharmony_ci      result = load_sample_pos(ctx);
3697bf215546Sopenharmony_ci      break;
3698bf215546Sopenharmony_ci   case nir_intrinsic_load_frag_coord:
3699bf215546Sopenharmony_ci      result = emit_load_frag_coord(ctx);
3700bf215546Sopenharmony_ci      break;
3701bf215546Sopenharmony_ci   case nir_intrinsic_load_frag_shading_rate:
3702bf215546Sopenharmony_ci      result = emit_load_frag_shading_rate(ctx);
3703bf215546Sopenharmony_ci      break;
3704bf215546Sopenharmony_ci   case nir_intrinsic_load_front_face:
3705bf215546Sopenharmony_ci      result = emit_i2b(&ctx->ac, ac_get_arg(&ctx->ac, ctx->args->front_face));
3706bf215546Sopenharmony_ci      break;
3707bf215546Sopenharmony_ci   case nir_intrinsic_load_helper_invocation:
3708bf215546Sopenharmony_ci      result = ac_build_load_helper_invocation(&ctx->ac);
3709bf215546Sopenharmony_ci      break;
3710bf215546Sopenharmony_ci   case nir_intrinsic_is_helper_invocation:
3711bf215546Sopenharmony_ci      result = ac_build_is_helper_invocation(&ctx->ac);
3712bf215546Sopenharmony_ci      break;
3713bf215546Sopenharmony_ci   case nir_intrinsic_load_color0:
3714bf215546Sopenharmony_ci      result = ctx->abi->color0;
3715bf215546Sopenharmony_ci      break;
3716bf215546Sopenharmony_ci   case nir_intrinsic_load_color1:
3717bf215546Sopenharmony_ci      result = ctx->abi->color1;
3718bf215546Sopenharmony_ci      break;
3719bf215546Sopenharmony_ci   case nir_intrinsic_load_user_data_amd:
3720bf215546Sopenharmony_ci      assert(LLVMTypeOf(ctx->abi->user_data) == ctx->ac.v4i32);
3721bf215546Sopenharmony_ci      result = ctx->abi->user_data;
3722bf215546Sopenharmony_ci      break;
3723bf215546Sopenharmony_ci   case nir_intrinsic_load_instance_id:
3724bf215546Sopenharmony_ci      result = ctx->instance_id_replaced ? ctx->instance_id_replaced : ctx->abi->instance_id;
3725bf215546Sopenharmony_ci      break;
3726bf215546Sopenharmony_ci   case nir_intrinsic_load_num_workgroups:
3727bf215546Sopenharmony_ci      if (ctx->abi->load_grid_size_from_user_sgpr) {
3728bf215546Sopenharmony_ci         result = ac_get_arg(&ctx->ac, ctx->args->num_work_groups);
3729bf215546Sopenharmony_ci      } else {
3730bf215546Sopenharmony_ci         LLVMTypeRef ptr_type = ac_array_in_const_addr_space(ctx->ac.v3i32);
3731bf215546Sopenharmony_ci         LLVMValueRef ptr = ac_get_arg(&ctx->ac, ctx->args->num_work_groups);
3732bf215546Sopenharmony_ci         ptr = LLVMBuildBitCast(ctx->ac.builder, ptr, ptr_type, "");
3733bf215546Sopenharmony_ci         result = ac_build_load_invariant(&ctx->ac, ptr, ctx->ac.i32_0);
3734bf215546Sopenharmony_ci      }
3735bf215546Sopenharmony_ci      break;
3736bf215546Sopenharmony_ci   case nir_intrinsic_load_local_invocation_index:
3737bf215546Sopenharmony_ci      result = visit_load_local_invocation_index(ctx);
3738bf215546Sopenharmony_ci      break;
3739bf215546Sopenharmony_ci   case nir_intrinsic_load_subgroup_id:
3740bf215546Sopenharmony_ci      result = visit_load_subgroup_id(ctx);
3741bf215546Sopenharmony_ci      break;
3742bf215546Sopenharmony_ci   case nir_intrinsic_load_num_subgroups:
3743bf215546Sopenharmony_ci      result = visit_load_num_subgroups(ctx);
3744bf215546Sopenharmony_ci      break;
3745bf215546Sopenharmony_ci   case nir_intrinsic_first_invocation:
3746bf215546Sopenharmony_ci      result = visit_first_invocation(ctx);
3747bf215546Sopenharmony_ci      break;
3748bf215546Sopenharmony_ci   case nir_intrinsic_load_push_constant:
3749bf215546Sopenharmony_ci      result = visit_load_push_constant(ctx, instr);
3750bf215546Sopenharmony_ci      break;
3751bf215546Sopenharmony_ci   case nir_intrinsic_store_ssbo:
3752bf215546Sopenharmony_ci      visit_store_ssbo(ctx, instr);
3753bf215546Sopenharmony_ci      break;
3754bf215546Sopenharmony_ci   case nir_intrinsic_load_ssbo:
3755bf215546Sopenharmony_ci      result = visit_load_buffer(ctx, instr);
3756bf215546Sopenharmony_ci      break;
3757bf215546Sopenharmony_ci   case nir_intrinsic_load_global_constant:
3758bf215546Sopenharmony_ci   case nir_intrinsic_load_global:
3759bf215546Sopenharmony_ci   case nir_intrinsic_load_global_amd:
3760bf215546Sopenharmony_ci      result = visit_load_global(ctx, instr);
3761bf215546Sopenharmony_ci      break;
3762bf215546Sopenharmony_ci   case nir_intrinsic_store_global:
3763bf215546Sopenharmony_ci   case nir_intrinsic_store_global_amd:
3764bf215546Sopenharmony_ci      visit_store_global(ctx, instr);
3765bf215546Sopenharmony_ci      break;
3766bf215546Sopenharmony_ci   case nir_intrinsic_global_atomic_add:
3767bf215546Sopenharmony_ci   case nir_intrinsic_global_atomic_imin:
3768bf215546Sopenharmony_ci   case nir_intrinsic_global_atomic_umin:
3769bf215546Sopenharmony_ci   case nir_intrinsic_global_atomic_imax:
3770bf215546Sopenharmony_ci   case nir_intrinsic_global_atomic_umax:
3771bf215546Sopenharmony_ci   case nir_intrinsic_global_atomic_and:
3772bf215546Sopenharmony_ci   case nir_intrinsic_global_atomic_or:
3773bf215546Sopenharmony_ci   case nir_intrinsic_global_atomic_xor:
3774bf215546Sopenharmony_ci   case nir_intrinsic_global_atomic_exchange:
3775bf215546Sopenharmony_ci   case nir_intrinsic_global_atomic_comp_swap:
3776bf215546Sopenharmony_ci   case nir_intrinsic_global_atomic_fmin:
3777bf215546Sopenharmony_ci   case nir_intrinsic_global_atomic_fmax:
3778bf215546Sopenharmony_ci   case nir_intrinsic_global_atomic_add_amd:
3779bf215546Sopenharmony_ci   case nir_intrinsic_global_atomic_imin_amd:
3780bf215546Sopenharmony_ci   case nir_intrinsic_global_atomic_umin_amd:
3781bf215546Sopenharmony_ci   case nir_intrinsic_global_atomic_imax_amd:
3782bf215546Sopenharmony_ci   case nir_intrinsic_global_atomic_umax_amd:
3783bf215546Sopenharmony_ci   case nir_intrinsic_global_atomic_and_amd:
3784bf215546Sopenharmony_ci   case nir_intrinsic_global_atomic_or_amd:
3785bf215546Sopenharmony_ci   case nir_intrinsic_global_atomic_xor_amd:
3786bf215546Sopenharmony_ci   case nir_intrinsic_global_atomic_exchange_amd:
3787bf215546Sopenharmony_ci   case nir_intrinsic_global_atomic_comp_swap_amd:
3788bf215546Sopenharmony_ci   case nir_intrinsic_global_atomic_fmin_amd:
3789bf215546Sopenharmony_ci   case nir_intrinsic_global_atomic_fmax_amd:
3790bf215546Sopenharmony_ci      result = visit_global_atomic(ctx, instr);
3791bf215546Sopenharmony_ci      break;
3792bf215546Sopenharmony_ci   case nir_intrinsic_ssbo_atomic_add:
3793bf215546Sopenharmony_ci   case nir_intrinsic_ssbo_atomic_imin:
3794bf215546Sopenharmony_ci   case nir_intrinsic_ssbo_atomic_umin:
3795bf215546Sopenharmony_ci   case nir_intrinsic_ssbo_atomic_imax:
3796bf215546Sopenharmony_ci   case nir_intrinsic_ssbo_atomic_umax:
3797bf215546Sopenharmony_ci   case nir_intrinsic_ssbo_atomic_and:
3798bf215546Sopenharmony_ci   case nir_intrinsic_ssbo_atomic_or:
3799bf215546Sopenharmony_ci   case nir_intrinsic_ssbo_atomic_xor:
3800bf215546Sopenharmony_ci   case nir_intrinsic_ssbo_atomic_exchange:
3801bf215546Sopenharmony_ci   case nir_intrinsic_ssbo_atomic_comp_swap:
3802bf215546Sopenharmony_ci   case nir_intrinsic_ssbo_atomic_fmin:
3803bf215546Sopenharmony_ci   case nir_intrinsic_ssbo_atomic_fmax:
3804bf215546Sopenharmony_ci      result = visit_atomic_ssbo(ctx, instr);
3805bf215546Sopenharmony_ci      break;
3806bf215546Sopenharmony_ci   case nir_intrinsic_load_ubo:
3807bf215546Sopenharmony_ci      result = visit_load_ubo_buffer(ctx, instr);
3808bf215546Sopenharmony_ci      break;
3809bf215546Sopenharmony_ci   case nir_intrinsic_get_ssbo_size:
3810bf215546Sopenharmony_ci      result = visit_get_ssbo_size(ctx, instr);
3811bf215546Sopenharmony_ci      break;
3812bf215546Sopenharmony_ci   case nir_intrinsic_load_input:
3813bf215546Sopenharmony_ci   case nir_intrinsic_load_input_vertex:
3814bf215546Sopenharmony_ci   case nir_intrinsic_load_per_vertex_input:
3815bf215546Sopenharmony_ci      result = visit_load(ctx, instr, false);
3816bf215546Sopenharmony_ci      break;
3817bf215546Sopenharmony_ci   case nir_intrinsic_load_output:
3818bf215546Sopenharmony_ci   case nir_intrinsic_load_per_vertex_output:
3819bf215546Sopenharmony_ci      result = visit_load(ctx, instr, true);
3820bf215546Sopenharmony_ci      break;
3821bf215546Sopenharmony_ci   case nir_intrinsic_store_output:
3822bf215546Sopenharmony_ci   case nir_intrinsic_store_per_vertex_output:
3823bf215546Sopenharmony_ci      visit_store_output(ctx, instr);
3824bf215546Sopenharmony_ci      break;
3825bf215546Sopenharmony_ci   case nir_intrinsic_load_shared:
3826bf215546Sopenharmony_ci      result = visit_load_shared(ctx, instr);
3827bf215546Sopenharmony_ci      break;
3828bf215546Sopenharmony_ci   case nir_intrinsic_store_shared:
3829bf215546Sopenharmony_ci      visit_store_shared(ctx, instr);
3830bf215546Sopenharmony_ci      break;
3831bf215546Sopenharmony_ci   case nir_intrinsic_load_shared2_amd:
3832bf215546Sopenharmony_ci      result = visit_load_shared2_amd(ctx, instr);
3833bf215546Sopenharmony_ci      break;
3834bf215546Sopenharmony_ci   case nir_intrinsic_store_shared2_amd:
3835bf215546Sopenharmony_ci      visit_store_shared2_amd(ctx, instr);
3836bf215546Sopenharmony_ci      break;
3837bf215546Sopenharmony_ci   case nir_intrinsic_bindless_image_samples:
3838bf215546Sopenharmony_ci   case nir_intrinsic_image_deref_samples:
3839bf215546Sopenharmony_ci      result = visit_image_samples(ctx, instr);
3840bf215546Sopenharmony_ci      break;
3841bf215546Sopenharmony_ci   case nir_intrinsic_bindless_image_load:
3842bf215546Sopenharmony_ci   case nir_intrinsic_bindless_image_sparse_load:
3843bf215546Sopenharmony_ci      result = visit_image_load(ctx, instr, true);
3844bf215546Sopenharmony_ci      break;
3845bf215546Sopenharmony_ci   case nir_intrinsic_image_deref_load:
3846bf215546Sopenharmony_ci   case nir_intrinsic_image_deref_sparse_load:
3847bf215546Sopenharmony_ci      result = visit_image_load(ctx, instr, false);
3848bf215546Sopenharmony_ci      break;
3849bf215546Sopenharmony_ci   case nir_intrinsic_bindless_image_store:
3850bf215546Sopenharmony_ci      visit_image_store(ctx, instr, true);
3851bf215546Sopenharmony_ci      break;
3852bf215546Sopenharmony_ci   case nir_intrinsic_image_deref_store:
3853bf215546Sopenharmony_ci      visit_image_store(ctx, instr, false);
3854bf215546Sopenharmony_ci      break;
3855bf215546Sopenharmony_ci   case nir_intrinsic_bindless_image_atomic_add:
3856bf215546Sopenharmony_ci   case nir_intrinsic_bindless_image_atomic_imin:
3857bf215546Sopenharmony_ci   case nir_intrinsic_bindless_image_atomic_umin:
3858bf215546Sopenharmony_ci   case nir_intrinsic_bindless_image_atomic_imax:
3859bf215546Sopenharmony_ci   case nir_intrinsic_bindless_image_atomic_umax:
3860bf215546Sopenharmony_ci   case nir_intrinsic_bindless_image_atomic_and:
3861bf215546Sopenharmony_ci   case nir_intrinsic_bindless_image_atomic_or:
3862bf215546Sopenharmony_ci   case nir_intrinsic_bindless_image_atomic_xor:
3863bf215546Sopenharmony_ci   case nir_intrinsic_bindless_image_atomic_exchange:
3864bf215546Sopenharmony_ci   case nir_intrinsic_bindless_image_atomic_comp_swap:
3865bf215546Sopenharmony_ci   case nir_intrinsic_bindless_image_atomic_inc_wrap:
3866bf215546Sopenharmony_ci   case nir_intrinsic_bindless_image_atomic_dec_wrap:
3867bf215546Sopenharmony_ci      result = visit_image_atomic(ctx, instr, true);
3868bf215546Sopenharmony_ci      break;
3869bf215546Sopenharmony_ci   case nir_intrinsic_image_deref_atomic_add:
3870bf215546Sopenharmony_ci   case nir_intrinsic_image_deref_atomic_imin:
3871bf215546Sopenharmony_ci   case nir_intrinsic_image_deref_atomic_umin:
3872bf215546Sopenharmony_ci   case nir_intrinsic_image_deref_atomic_imax:
3873bf215546Sopenharmony_ci   case nir_intrinsic_image_deref_atomic_umax:
3874bf215546Sopenharmony_ci   case nir_intrinsic_image_deref_atomic_and:
3875bf215546Sopenharmony_ci   case nir_intrinsic_image_deref_atomic_or:
3876bf215546Sopenharmony_ci   case nir_intrinsic_image_deref_atomic_xor:
3877bf215546Sopenharmony_ci   case nir_intrinsic_image_deref_atomic_exchange:
3878bf215546Sopenharmony_ci   case nir_intrinsic_image_deref_atomic_comp_swap:
3879bf215546Sopenharmony_ci   case nir_intrinsic_image_deref_atomic_inc_wrap:
3880bf215546Sopenharmony_ci   case nir_intrinsic_image_deref_atomic_dec_wrap:
3881bf215546Sopenharmony_ci   case nir_intrinsic_image_deref_atomic_fmin:
3882bf215546Sopenharmony_ci   case nir_intrinsic_image_deref_atomic_fmax:
3883bf215546Sopenharmony_ci      result = visit_image_atomic(ctx, instr, false);
3884bf215546Sopenharmony_ci      break;
3885bf215546Sopenharmony_ci   case nir_intrinsic_bindless_image_size:
3886bf215546Sopenharmony_ci      result = visit_image_size(ctx, instr, true);
3887bf215546Sopenharmony_ci      break;
3888bf215546Sopenharmony_ci   case nir_intrinsic_image_deref_size:
3889bf215546Sopenharmony_ci      result = visit_image_size(ctx, instr, false);
3890bf215546Sopenharmony_ci      break;
3891bf215546Sopenharmony_ci   case nir_intrinsic_shader_clock:
3892bf215546Sopenharmony_ci      result = ac_build_shader_clock(&ctx->ac, nir_intrinsic_memory_scope(instr));
3893bf215546Sopenharmony_ci      break;
3894bf215546Sopenharmony_ci   case nir_intrinsic_discard:
3895bf215546Sopenharmony_ci   case nir_intrinsic_discard_if:
3896bf215546Sopenharmony_ci   case nir_intrinsic_terminate:
3897bf215546Sopenharmony_ci   case nir_intrinsic_terminate_if:
3898bf215546Sopenharmony_ci      emit_discard(ctx, instr);
3899bf215546Sopenharmony_ci      break;
3900bf215546Sopenharmony_ci   case nir_intrinsic_demote:
3901bf215546Sopenharmony_ci   case nir_intrinsic_demote_if:
3902bf215546Sopenharmony_ci      emit_demote(ctx, instr);
3903bf215546Sopenharmony_ci      break;
3904bf215546Sopenharmony_ci   case nir_intrinsic_memory_barrier:
3905bf215546Sopenharmony_ci   case nir_intrinsic_group_memory_barrier:
3906bf215546Sopenharmony_ci      ac_build_waitcnt(&ctx->ac, AC_WAIT_LGKM | AC_WAIT_VLOAD | AC_WAIT_VSTORE);
3907bf215546Sopenharmony_ci      break;
3908bf215546Sopenharmony_ci   case nir_intrinsic_memory_barrier_buffer:
3909bf215546Sopenharmony_ci   case nir_intrinsic_memory_barrier_image:
3910bf215546Sopenharmony_ci      ac_build_waitcnt(&ctx->ac, AC_WAIT_VLOAD | AC_WAIT_VSTORE);
3911bf215546Sopenharmony_ci      break;
3912bf215546Sopenharmony_ci   case nir_intrinsic_memory_barrier_shared:
3913bf215546Sopenharmony_ci   case nir_intrinsic_memory_barrier_tcs_patch:
3914bf215546Sopenharmony_ci      ac_build_waitcnt(&ctx->ac, AC_WAIT_LGKM);
3915bf215546Sopenharmony_ci      break;
3916bf215546Sopenharmony_ci   case nir_intrinsic_scoped_barrier: {
3917bf215546Sopenharmony_ci      assert(!(nir_intrinsic_memory_semantics(instr) &
3918bf215546Sopenharmony_ci               (NIR_MEMORY_MAKE_AVAILABLE | NIR_MEMORY_MAKE_VISIBLE)));
3919bf215546Sopenharmony_ci
3920bf215546Sopenharmony_ci      nir_variable_mode modes = nir_intrinsic_memory_modes(instr);
3921bf215546Sopenharmony_ci
3922bf215546Sopenharmony_ci      unsigned wait_flags = 0;
3923bf215546Sopenharmony_ci      if (modes & (nir_var_mem_global | nir_var_mem_ssbo | nir_var_image))
3924bf215546Sopenharmony_ci         wait_flags |= AC_WAIT_VLOAD | AC_WAIT_VSTORE;
3925bf215546Sopenharmony_ci      if (modes & nir_var_mem_shared)
3926bf215546Sopenharmony_ci         wait_flags |= AC_WAIT_LGKM;
3927bf215546Sopenharmony_ci
3928bf215546Sopenharmony_ci      if (wait_flags)
3929bf215546Sopenharmony_ci         ac_build_waitcnt(&ctx->ac, wait_flags);
3930bf215546Sopenharmony_ci
3931bf215546Sopenharmony_ci      if (nir_intrinsic_execution_scope(instr) == NIR_SCOPE_WORKGROUP)
3932bf215546Sopenharmony_ci         ac_build_s_barrier(&ctx->ac, ctx->stage);
3933bf215546Sopenharmony_ci      break;
3934bf215546Sopenharmony_ci   }
3935bf215546Sopenharmony_ci   case nir_intrinsic_control_barrier:
3936bf215546Sopenharmony_ci      /* If output patches are wholly in one wave, we don't need a barrier. */
3937bf215546Sopenharmony_ci      if (ctx->stage == MESA_SHADER_TESS_CTRL &&
3938bf215546Sopenharmony_ci          ctx->ac.wave_size % ctx->info->tess.tcs_vertices_out == 0)
3939bf215546Sopenharmony_ci         break;
3940bf215546Sopenharmony_ci
3941bf215546Sopenharmony_ci      ac_build_s_barrier(&ctx->ac, ctx->stage);
3942bf215546Sopenharmony_ci      break;
3943bf215546Sopenharmony_ci   case nir_intrinsic_shared_atomic_add:
3944bf215546Sopenharmony_ci   case nir_intrinsic_shared_atomic_imin:
3945bf215546Sopenharmony_ci   case nir_intrinsic_shared_atomic_umin:
3946bf215546Sopenharmony_ci   case nir_intrinsic_shared_atomic_imax:
3947bf215546Sopenharmony_ci   case nir_intrinsic_shared_atomic_umax:
3948bf215546Sopenharmony_ci   case nir_intrinsic_shared_atomic_and:
3949bf215546Sopenharmony_ci   case nir_intrinsic_shared_atomic_or:
3950bf215546Sopenharmony_ci   case nir_intrinsic_shared_atomic_xor:
3951bf215546Sopenharmony_ci   case nir_intrinsic_shared_atomic_exchange:
3952bf215546Sopenharmony_ci   case nir_intrinsic_shared_atomic_comp_swap:
3953bf215546Sopenharmony_ci   case nir_intrinsic_shared_atomic_fadd:
3954bf215546Sopenharmony_ci   case nir_intrinsic_shared_atomic_fmin:
3955bf215546Sopenharmony_ci   case nir_intrinsic_shared_atomic_fmax: {
3956bf215546Sopenharmony_ci      LLVMValueRef ptr = get_memory_ptr(ctx, instr->src[0], instr->src[1].ssa->bit_size, 0);
3957bf215546Sopenharmony_ci      result = visit_var_atomic(ctx, instr, ptr, 1);
3958bf215546Sopenharmony_ci      break;
3959bf215546Sopenharmony_ci   }
3960bf215546Sopenharmony_ci   case nir_intrinsic_deref_atomic_add:
3961bf215546Sopenharmony_ci   case nir_intrinsic_deref_atomic_imin:
3962bf215546Sopenharmony_ci   case nir_intrinsic_deref_atomic_umin:
3963bf215546Sopenharmony_ci   case nir_intrinsic_deref_atomic_imax:
3964bf215546Sopenharmony_ci   case nir_intrinsic_deref_atomic_umax:
3965bf215546Sopenharmony_ci   case nir_intrinsic_deref_atomic_and:
3966bf215546Sopenharmony_ci   case nir_intrinsic_deref_atomic_or:
3967bf215546Sopenharmony_ci   case nir_intrinsic_deref_atomic_xor:
3968bf215546Sopenharmony_ci   case nir_intrinsic_deref_atomic_exchange:
3969bf215546Sopenharmony_ci   case nir_intrinsic_deref_atomic_comp_swap:
3970bf215546Sopenharmony_ci   case nir_intrinsic_deref_atomic_fadd: {
3971bf215546Sopenharmony_ci      LLVMValueRef ptr = get_src(ctx, instr->src[0]);
3972bf215546Sopenharmony_ci      result = visit_var_atomic(ctx, instr, ptr, 1);
3973bf215546Sopenharmony_ci      break;
3974bf215546Sopenharmony_ci   }
3975bf215546Sopenharmony_ci   case nir_intrinsic_load_barycentric_pixel:
3976bf215546Sopenharmony_ci      result = barycentric_center(ctx, nir_intrinsic_interp_mode(instr));
3977bf215546Sopenharmony_ci      break;
3978bf215546Sopenharmony_ci   case nir_intrinsic_load_barycentric_centroid:
3979bf215546Sopenharmony_ci      result = barycentric_centroid(ctx, nir_intrinsic_interp_mode(instr));
3980bf215546Sopenharmony_ci      break;
3981bf215546Sopenharmony_ci   case nir_intrinsic_load_barycentric_sample:
3982bf215546Sopenharmony_ci      result = barycentric_sample(ctx, nir_intrinsic_interp_mode(instr));
3983bf215546Sopenharmony_ci      break;
3984bf215546Sopenharmony_ci   case nir_intrinsic_load_barycentric_model:
3985bf215546Sopenharmony_ci      result = barycentric_model(ctx);
3986bf215546Sopenharmony_ci      break;
3987bf215546Sopenharmony_ci   case nir_intrinsic_load_barycentric_at_offset: {
3988bf215546Sopenharmony_ci      LLVMValueRef offset = ac_to_float(&ctx->ac, get_src(ctx, instr->src[0]));
3989bf215546Sopenharmony_ci      result = barycentric_offset(ctx, nir_intrinsic_interp_mode(instr), offset);
3990bf215546Sopenharmony_ci      break;
3991bf215546Sopenharmony_ci   }
3992bf215546Sopenharmony_ci   case nir_intrinsic_load_barycentric_at_sample: {
3993bf215546Sopenharmony_ci      LLVMValueRef sample_id = get_src(ctx, instr->src[0]);
3994bf215546Sopenharmony_ci      result = barycentric_at_sample(ctx, nir_intrinsic_interp_mode(instr), sample_id);
3995bf215546Sopenharmony_ci      break;
3996bf215546Sopenharmony_ci   }
3997bf215546Sopenharmony_ci   case nir_intrinsic_load_interpolated_input: {
3998bf215546Sopenharmony_ci      /* We assume any indirect loads have been lowered away */
3999bf215546Sopenharmony_ci      ASSERTED nir_const_value *offset = nir_src_as_const_value(instr->src[1]);
4000bf215546Sopenharmony_ci      assert(offset);
4001bf215546Sopenharmony_ci      assert(offset[0].i32 == 0);
4002bf215546Sopenharmony_ci
4003bf215546Sopenharmony_ci      LLVMValueRef interp_param = get_src(ctx, instr->src[0]);
4004bf215546Sopenharmony_ci      unsigned index = nir_intrinsic_base(instr);
4005bf215546Sopenharmony_ci      unsigned component = nir_intrinsic_component(instr);
4006bf215546Sopenharmony_ci      result = load_interpolated_input(ctx, interp_param, index, component,
4007bf215546Sopenharmony_ci                                       instr->dest.ssa.num_components, instr->dest.ssa.bit_size,
4008bf215546Sopenharmony_ci                                       nir_intrinsic_io_semantics(instr).high_16bits);
4009bf215546Sopenharmony_ci      break;
4010bf215546Sopenharmony_ci   }
4011bf215546Sopenharmony_ci   case nir_intrinsic_load_point_coord_maybe_flipped: {
4012bf215546Sopenharmony_ci      LLVMValueRef interp_param = lookup_interp_param(ctx, INTERP_MODE_NONE, INTERP_CENTER);
4013bf215546Sopenharmony_ci      /* Load point coordinates (x, y) which are written by the hw after the interpolated inputs */
4014bf215546Sopenharmony_ci      result = load_interpolated_input(ctx, interp_param, ctx->abi->num_interp, 2,
4015bf215546Sopenharmony_ci                                       instr->dest.ssa.num_components, instr->dest.ssa.bit_size,
4016bf215546Sopenharmony_ci                                       false);
4017bf215546Sopenharmony_ci      break;
4018bf215546Sopenharmony_ci   }
4019bf215546Sopenharmony_ci   case nir_intrinsic_emit_vertex:
4020bf215546Sopenharmony_ci      ctx->abi->emit_vertex(ctx->abi, nir_intrinsic_stream_id(instr), ctx->abi->outputs);
4021bf215546Sopenharmony_ci      break;
4022bf215546Sopenharmony_ci   case nir_intrinsic_emit_vertex_with_counter: {
4023bf215546Sopenharmony_ci      unsigned stream = nir_intrinsic_stream_id(instr);
4024bf215546Sopenharmony_ci      LLVMValueRef next_vertex = get_src(ctx, instr->src[0]);
4025bf215546Sopenharmony_ci      ctx->abi->emit_vertex_with_counter(ctx->abi, stream, next_vertex, ctx->abi->outputs);
4026bf215546Sopenharmony_ci      break;
4027bf215546Sopenharmony_ci   }
4028bf215546Sopenharmony_ci   case nir_intrinsic_end_primitive:
4029bf215546Sopenharmony_ci   case nir_intrinsic_end_primitive_with_counter:
4030bf215546Sopenharmony_ci      ctx->abi->emit_primitive(ctx->abi, nir_intrinsic_stream_id(instr));
4031bf215546Sopenharmony_ci      break;
4032bf215546Sopenharmony_ci   case nir_intrinsic_load_tess_coord: {
4033bf215546Sopenharmony_ci      LLVMValueRef coord[] = {
4034bf215546Sopenharmony_ci         ctx->tes_u_replaced ? ctx->tes_u_replaced : ac_get_arg(&ctx->ac, ctx->args->tes_u),
4035bf215546Sopenharmony_ci         ctx->tes_v_replaced ? ctx->tes_v_replaced : ac_get_arg(&ctx->ac, ctx->args->tes_v),
4036bf215546Sopenharmony_ci         ctx->ac.f32_0,
4037bf215546Sopenharmony_ci      };
4038bf215546Sopenharmony_ci
4039bf215546Sopenharmony_ci      /* For triangles, the vector should be (u, v, 1-u-v). */
4040bf215546Sopenharmony_ci      if (ctx->info->tess._primitive_mode == TESS_PRIMITIVE_TRIANGLES) {
4041bf215546Sopenharmony_ci         coord[2] = LLVMBuildFSub(ctx->ac.builder, ctx->ac.f32_1,
4042bf215546Sopenharmony_ci                                  LLVMBuildFAdd(ctx->ac.builder, coord[0], coord[1], ""), "");
4043bf215546Sopenharmony_ci      }
4044bf215546Sopenharmony_ci      result = ac_build_gather_values(&ctx->ac, coord, 3);
4045bf215546Sopenharmony_ci      break;
4046bf215546Sopenharmony_ci   }
4047bf215546Sopenharmony_ci   case nir_intrinsic_load_gs_vertex_offset_amd:
4048bf215546Sopenharmony_ci      result = ac_get_arg(&ctx->ac, ctx->args->gs_vtx_offset[nir_intrinsic_base(instr)]);
4049bf215546Sopenharmony_ci      break;
4050bf215546Sopenharmony_ci   case nir_intrinsic_vote_all: {
4051bf215546Sopenharmony_ci      result = ac_build_vote_all(&ctx->ac, get_src(ctx, instr->src[0]));
4052bf215546Sopenharmony_ci      break;
4053bf215546Sopenharmony_ci   }
4054bf215546Sopenharmony_ci   case nir_intrinsic_vote_any: {
4055bf215546Sopenharmony_ci      result = ac_build_vote_any(&ctx->ac, get_src(ctx, instr->src[0]));
4056bf215546Sopenharmony_ci      break;
4057bf215546Sopenharmony_ci   }
4058bf215546Sopenharmony_ci   case nir_intrinsic_shuffle:
4059bf215546Sopenharmony_ci      if (ctx->ac.gfx_level == GFX8 || ctx->ac.gfx_level == GFX9 ||
4060bf215546Sopenharmony_ci          (ctx->ac.gfx_level >= GFX10 && ctx->ac.wave_size == 32)) {
4061bf215546Sopenharmony_ci         result =
4062bf215546Sopenharmony_ci            ac_build_shuffle(&ctx->ac, get_src(ctx, instr->src[0]), get_src(ctx, instr->src[1]));
4063bf215546Sopenharmony_ci      } else {
4064bf215546Sopenharmony_ci         LLVMValueRef src = get_src(ctx, instr->src[0]);
4065bf215546Sopenharmony_ci         LLVMValueRef index = get_src(ctx, instr->src[1]);
4066bf215546Sopenharmony_ci         LLVMTypeRef type = LLVMTypeOf(src);
4067bf215546Sopenharmony_ci         struct waterfall_context wctx;
4068bf215546Sopenharmony_ci         LLVMValueRef index_val;
4069bf215546Sopenharmony_ci
4070bf215546Sopenharmony_ci         index_val = enter_waterfall(ctx, &wctx, index, true);
4071bf215546Sopenharmony_ci
4072bf215546Sopenharmony_ci         src = LLVMBuildZExt(ctx->ac.builder, src, ctx->ac.i32, "");
4073bf215546Sopenharmony_ci
4074bf215546Sopenharmony_ci         result = ac_build_intrinsic(&ctx->ac, "llvm.amdgcn.readlane", ctx->ac.i32,
4075bf215546Sopenharmony_ci                                     (LLVMValueRef[]){src, index_val}, 2,
4076bf215546Sopenharmony_ci                                     AC_FUNC_ATTR_READNONE | AC_FUNC_ATTR_CONVERGENT);
4077bf215546Sopenharmony_ci
4078bf215546Sopenharmony_ci         result = LLVMBuildTrunc(ctx->ac.builder, result, type, "");
4079bf215546Sopenharmony_ci
4080bf215546Sopenharmony_ci         result = exit_waterfall(ctx, &wctx, result);
4081bf215546Sopenharmony_ci      }
4082bf215546Sopenharmony_ci      break;
4083bf215546Sopenharmony_ci   case nir_intrinsic_reduce:
4084bf215546Sopenharmony_ci      result = ac_build_reduce(&ctx->ac, get_src(ctx, instr->src[0]), instr->const_index[0],
4085bf215546Sopenharmony_ci                               instr->const_index[1]);
4086bf215546Sopenharmony_ci      break;
4087bf215546Sopenharmony_ci   case nir_intrinsic_inclusive_scan:
4088bf215546Sopenharmony_ci      result =
4089bf215546Sopenharmony_ci         ac_build_inclusive_scan(&ctx->ac, get_src(ctx, instr->src[0]), instr->const_index[0]);
4090bf215546Sopenharmony_ci      break;
4091bf215546Sopenharmony_ci   case nir_intrinsic_exclusive_scan:
4092bf215546Sopenharmony_ci      result =
4093bf215546Sopenharmony_ci         ac_build_exclusive_scan(&ctx->ac, get_src(ctx, instr->src[0]), instr->const_index[0]);
4094bf215546Sopenharmony_ci      break;
4095bf215546Sopenharmony_ci   case nir_intrinsic_quad_broadcast: {
4096bf215546Sopenharmony_ci      unsigned lane = nir_src_as_uint(instr->src[1]);
4097bf215546Sopenharmony_ci      result = ac_build_quad_swizzle(&ctx->ac, get_src(ctx, instr->src[0]), lane, lane, lane, lane);
4098bf215546Sopenharmony_ci      break;
4099bf215546Sopenharmony_ci   }
4100bf215546Sopenharmony_ci   case nir_intrinsic_quad_swap_horizontal:
4101bf215546Sopenharmony_ci      result = ac_build_quad_swizzle(&ctx->ac, get_src(ctx, instr->src[0]), 1, 0, 3, 2);
4102bf215546Sopenharmony_ci      break;
4103bf215546Sopenharmony_ci   case nir_intrinsic_quad_swap_vertical:
4104bf215546Sopenharmony_ci      result = ac_build_quad_swizzle(&ctx->ac, get_src(ctx, instr->src[0]), 2, 3, 0, 1);
4105bf215546Sopenharmony_ci      break;
4106bf215546Sopenharmony_ci   case nir_intrinsic_quad_swap_diagonal:
4107bf215546Sopenharmony_ci      result = ac_build_quad_swizzle(&ctx->ac, get_src(ctx, instr->src[0]), 3, 2, 1, 0);
4108bf215546Sopenharmony_ci      break;
4109bf215546Sopenharmony_ci   case nir_intrinsic_quad_swizzle_amd: {
4110bf215546Sopenharmony_ci      uint32_t mask = nir_intrinsic_swizzle_mask(instr);
4111bf215546Sopenharmony_ci      result = ac_build_quad_swizzle(&ctx->ac, get_src(ctx, instr->src[0]), mask & 0x3,
4112bf215546Sopenharmony_ci                                     (mask >> 2) & 0x3, (mask >> 4) & 0x3, (mask >> 6) & 0x3);
4113bf215546Sopenharmony_ci      break;
4114bf215546Sopenharmony_ci   }
4115bf215546Sopenharmony_ci   case nir_intrinsic_masked_swizzle_amd: {
4116bf215546Sopenharmony_ci      uint32_t mask = nir_intrinsic_swizzle_mask(instr);
4117bf215546Sopenharmony_ci      result = ac_build_ds_swizzle(&ctx->ac, get_src(ctx, instr->src[0]), mask);
4118bf215546Sopenharmony_ci      break;
4119bf215546Sopenharmony_ci   }
4120bf215546Sopenharmony_ci   case nir_intrinsic_write_invocation_amd:
4121bf215546Sopenharmony_ci      result = ac_build_writelane(&ctx->ac, get_src(ctx, instr->src[0]),
4122bf215546Sopenharmony_ci                                  get_src(ctx, instr->src[1]), get_src(ctx, instr->src[2]));
4123bf215546Sopenharmony_ci      break;
4124bf215546Sopenharmony_ci   case nir_intrinsic_mbcnt_amd:
4125bf215546Sopenharmony_ci      result = ac_build_mbcnt_add(&ctx->ac, get_src(ctx, instr->src[0]), get_src(ctx, instr->src[1]));
4126bf215546Sopenharmony_ci      break;
4127bf215546Sopenharmony_ci   case nir_intrinsic_load_scratch: {
4128bf215546Sopenharmony_ci      LLVMValueRef offset = get_src(ctx, instr->src[0]);
4129bf215546Sopenharmony_ci      LLVMValueRef ptr = ac_build_gep0(&ctx->ac, ctx->scratch, offset);
4130bf215546Sopenharmony_ci      LLVMTypeRef comp_type = LLVMIntTypeInContext(ctx->ac.context, instr->dest.ssa.bit_size);
4131bf215546Sopenharmony_ci      LLVMTypeRef vec_type = instr->dest.ssa.num_components == 1
4132bf215546Sopenharmony_ci                                ? comp_type
4133bf215546Sopenharmony_ci                                : LLVMVectorType(comp_type, instr->dest.ssa.num_components);
4134bf215546Sopenharmony_ci      unsigned addr_space = LLVMGetPointerAddressSpace(LLVMTypeOf(ptr));
4135bf215546Sopenharmony_ci      ptr = LLVMBuildBitCast(ctx->ac.builder, ptr, LLVMPointerType(vec_type, addr_space), "");
4136bf215546Sopenharmony_ci      result = LLVMBuildLoad2(ctx->ac.builder, vec_type, ptr, "");
4137bf215546Sopenharmony_ci      break;
4138bf215546Sopenharmony_ci   }
4139bf215546Sopenharmony_ci   case nir_intrinsic_store_scratch: {
4140bf215546Sopenharmony_ci      LLVMValueRef offset = get_src(ctx, instr->src[1]);
4141bf215546Sopenharmony_ci      LLVMValueRef ptr = ac_build_gep0(&ctx->ac, ctx->scratch, offset);
4142bf215546Sopenharmony_ci      LLVMTypeRef comp_type = LLVMIntTypeInContext(ctx->ac.context, instr->src[0].ssa->bit_size);
4143bf215546Sopenharmony_ci      unsigned addr_space = LLVMGetPointerAddressSpace(LLVMTypeOf(ptr));
4144bf215546Sopenharmony_ci      ptr = LLVMBuildBitCast(ctx->ac.builder, ptr, LLVMPointerType(comp_type, addr_space), "");
4145bf215546Sopenharmony_ci      LLVMValueRef src = get_src(ctx, instr->src[0]);
4146bf215546Sopenharmony_ci      unsigned wrmask = nir_intrinsic_write_mask(instr);
4147bf215546Sopenharmony_ci      while (wrmask) {
4148bf215546Sopenharmony_ci         int start, count;
4149bf215546Sopenharmony_ci         u_bit_scan_consecutive_range(&wrmask, &start, &count);
4150bf215546Sopenharmony_ci
4151bf215546Sopenharmony_ci         LLVMValueRef offset = LLVMConstInt(ctx->ac.i32, start, false);
4152bf215546Sopenharmony_ci         LLVMValueRef offset_ptr = LLVMBuildGEP2(ctx->ac.builder, comp_type, ptr, &offset, 1, "");
4153bf215546Sopenharmony_ci         LLVMTypeRef vec_type = count == 1 ? comp_type : LLVMVectorType(comp_type, count);
4154bf215546Sopenharmony_ci         offset_ptr = LLVMBuildBitCast(ctx->ac.builder, offset_ptr,
4155bf215546Sopenharmony_ci                                       LLVMPointerType(vec_type, addr_space), "");
4156bf215546Sopenharmony_ci         LLVMValueRef offset_src = ac_extract_components(&ctx->ac, src, start, count);
4157bf215546Sopenharmony_ci         LLVMBuildStore(ctx->ac.builder, offset_src, offset_ptr);
4158bf215546Sopenharmony_ci      }
4159bf215546Sopenharmony_ci      break;
4160bf215546Sopenharmony_ci   }
4161bf215546Sopenharmony_ci   case nir_intrinsic_load_constant: {
4162bf215546Sopenharmony_ci      unsigned base = nir_intrinsic_base(instr);
4163bf215546Sopenharmony_ci      unsigned range = nir_intrinsic_range(instr);
4164bf215546Sopenharmony_ci
4165bf215546Sopenharmony_ci      LLVMValueRef offset = get_src(ctx, instr->src[0]);
4166bf215546Sopenharmony_ci      offset = LLVMBuildAdd(ctx->ac.builder, offset, LLVMConstInt(ctx->ac.i32, base, false), "");
4167bf215546Sopenharmony_ci
4168bf215546Sopenharmony_ci      /* Clamp the offset to avoid out-of-bound access because global
4169bf215546Sopenharmony_ci       * instructions can't handle them.
4170bf215546Sopenharmony_ci       */
4171bf215546Sopenharmony_ci      LLVMValueRef size = LLVMConstInt(ctx->ac.i32, base + range, false);
4172bf215546Sopenharmony_ci      LLVMValueRef cond = LLVMBuildICmp(ctx->ac.builder, LLVMIntULT, offset, size, "");
4173bf215546Sopenharmony_ci      offset = LLVMBuildSelect(ctx->ac.builder, cond, offset, size, "");
4174bf215546Sopenharmony_ci
4175bf215546Sopenharmony_ci      LLVMValueRef ptr = ac_build_gep0(&ctx->ac, ctx->constant_data, offset);
4176bf215546Sopenharmony_ci      LLVMTypeRef comp_type = LLVMIntTypeInContext(ctx->ac.context, instr->dest.ssa.bit_size);
4177bf215546Sopenharmony_ci      LLVMTypeRef vec_type = instr->dest.ssa.num_components == 1
4178bf215546Sopenharmony_ci                                ? comp_type
4179bf215546Sopenharmony_ci                                : LLVMVectorType(comp_type, instr->dest.ssa.num_components);
4180bf215546Sopenharmony_ci      unsigned addr_space = LLVMGetPointerAddressSpace(LLVMTypeOf(ptr));
4181bf215546Sopenharmony_ci      ptr = LLVMBuildBitCast(ctx->ac.builder, ptr, LLVMPointerType(vec_type, addr_space), "");
4182bf215546Sopenharmony_ci      result = LLVMBuildLoad2(ctx->ac.builder, vec_type, ptr, "");
4183bf215546Sopenharmony_ci      break;
4184bf215546Sopenharmony_ci   }
4185bf215546Sopenharmony_ci   case nir_intrinsic_set_vertex_and_primitive_count:
4186bf215546Sopenharmony_ci      /* Currently ignored. */
4187bf215546Sopenharmony_ci      break;
4188bf215546Sopenharmony_ci   case nir_intrinsic_load_buffer_amd: {
4189bf215546Sopenharmony_ci      LLVMValueRef descriptor = get_src(ctx, instr->src[0]);
4190bf215546Sopenharmony_ci      LLVMValueRef addr_voffset = get_src(ctx, instr->src[1]);
4191bf215546Sopenharmony_ci      LLVMValueRef addr_soffset = get_src(ctx, instr->src[2]);
4192bf215546Sopenharmony_ci      unsigned num_components = instr->dest.ssa.num_components;
4193bf215546Sopenharmony_ci      unsigned const_offset = nir_intrinsic_base(instr);
4194bf215546Sopenharmony_ci      bool swizzled = nir_intrinsic_is_swizzled(instr);
4195bf215546Sopenharmony_ci      bool reorder = nir_intrinsic_can_reorder(instr);
4196bf215546Sopenharmony_ci      bool slc = nir_intrinsic_slc_amd(instr);
4197bf215546Sopenharmony_ci
4198bf215546Sopenharmony_ci      enum ac_image_cache_policy cache_policy = ac_glc;
4199bf215546Sopenharmony_ci      if (swizzled)
4200bf215546Sopenharmony_ci         cache_policy |= ac_swizzled;
4201bf215546Sopenharmony_ci      if (slc)
4202bf215546Sopenharmony_ci         cache_policy |= ac_slc;
4203bf215546Sopenharmony_ci
4204bf215546Sopenharmony_ci      LLVMTypeRef channel_type;
4205bf215546Sopenharmony_ci      if (instr->dest.ssa.bit_size == 8)
4206bf215546Sopenharmony_ci         channel_type = ctx->ac.i8;
4207bf215546Sopenharmony_ci      else if (instr->dest.ssa.bit_size == 16)
4208bf215546Sopenharmony_ci         channel_type = ctx->ac.i16;
4209bf215546Sopenharmony_ci      else if (instr->dest.ssa.bit_size == 32)
4210bf215546Sopenharmony_ci         channel_type = ctx->ac.i32;
4211bf215546Sopenharmony_ci      else if (instr->dest.ssa.bit_size == 64)
4212bf215546Sopenharmony_ci         channel_type = ctx->ac.i64;
4213bf215546Sopenharmony_ci      else if (instr->dest.ssa.bit_size == 128)
4214bf215546Sopenharmony_ci         channel_type = ctx->ac.i128;
4215bf215546Sopenharmony_ci      else
4216bf215546Sopenharmony_ci         unreachable("Unsupported channel type for load_buffer_amd");
4217bf215546Sopenharmony_ci
4218bf215546Sopenharmony_ci      LLVMValueRef voffset = LLVMBuildAdd(ctx->ac.builder, addr_voffset,
4219bf215546Sopenharmony_ci                                          LLVMConstInt(ctx->ac.i32, const_offset, 0), "");
4220bf215546Sopenharmony_ci      result = ac_build_buffer_load(&ctx->ac, descriptor, num_components, NULL, voffset,
4221bf215546Sopenharmony_ci                                    addr_soffset, channel_type, cache_policy, reorder, false);
4222bf215546Sopenharmony_ci      result = ac_to_integer(&ctx->ac, ac_trim_vector(&ctx->ac, result, num_components));
4223bf215546Sopenharmony_ci      break;
4224bf215546Sopenharmony_ci   }
4225bf215546Sopenharmony_ci   case nir_intrinsic_store_buffer_amd: {
4226bf215546Sopenharmony_ci      LLVMValueRef store_data = get_src(ctx, instr->src[0]);
4227bf215546Sopenharmony_ci      LLVMValueRef descriptor = get_src(ctx, instr->src[1]);
4228bf215546Sopenharmony_ci      LLVMValueRef addr_voffset = get_src(ctx, instr->src[2]);
4229bf215546Sopenharmony_ci      LLVMValueRef addr_soffset = get_src(ctx, instr->src[3]);
4230bf215546Sopenharmony_ci      unsigned const_offset = nir_intrinsic_base(instr);
4231bf215546Sopenharmony_ci      bool swizzled = nir_intrinsic_is_swizzled(instr);
4232bf215546Sopenharmony_ci      bool slc = nir_intrinsic_slc_amd(instr);
4233bf215546Sopenharmony_ci
4234bf215546Sopenharmony_ci      enum ac_image_cache_policy cache_policy = ac_glc;
4235bf215546Sopenharmony_ci      if (swizzled)
4236bf215546Sopenharmony_ci         cache_policy |= ac_swizzled;
4237bf215546Sopenharmony_ci      if (slc)
4238bf215546Sopenharmony_ci         cache_policy |= ac_slc;
4239bf215546Sopenharmony_ci
4240bf215546Sopenharmony_ci      unsigned writemask = nir_intrinsic_write_mask(instr);
4241bf215546Sopenharmony_ci      while (writemask) {
4242bf215546Sopenharmony_ci         int start, count;
4243bf215546Sopenharmony_ci         u_bit_scan_consecutive_range(&writemask, &start, &count);
4244bf215546Sopenharmony_ci
4245bf215546Sopenharmony_ci         LLVMValueRef voffset = LLVMBuildAdd(
4246bf215546Sopenharmony_ci            ctx->ac.builder, addr_voffset,
4247bf215546Sopenharmony_ci            LLVMConstInt(ctx->ac.i32, const_offset + start * 4, 0), "");
4248bf215546Sopenharmony_ci
4249bf215546Sopenharmony_ci         LLVMValueRef data = extract_vector_range(&ctx->ac, store_data, start, count);
4250bf215546Sopenharmony_ci         ac_build_buffer_store_dword(&ctx->ac, descriptor, data, NULL, voffset, addr_soffset,
4251bf215546Sopenharmony_ci                                     cache_policy);
4252bf215546Sopenharmony_ci      }
4253bf215546Sopenharmony_ci      break;
4254bf215546Sopenharmony_ci   }
4255bf215546Sopenharmony_ci   case nir_intrinsic_has_input_vertex_amd: {
4256bf215546Sopenharmony_ci      LLVMValueRef num =
4257bf215546Sopenharmony_ci         ac_unpack_param(&ctx->ac, ac_get_arg(&ctx->ac, ctx->args->merged_wave_info), 0, 8);
4258bf215546Sopenharmony_ci      result = LLVMBuildICmp(ctx->ac.builder, LLVMIntULT, ac_get_thread_id(&ctx->ac), num, "");
4259bf215546Sopenharmony_ci      break;
4260bf215546Sopenharmony_ci   }
4261bf215546Sopenharmony_ci   case nir_intrinsic_has_input_primitive_amd: {
4262bf215546Sopenharmony_ci      LLVMValueRef num =
4263bf215546Sopenharmony_ci         ac_unpack_param(&ctx->ac, ac_get_arg(&ctx->ac, ctx->args->merged_wave_info), 8, 8);
4264bf215546Sopenharmony_ci      result = LLVMBuildICmp(ctx->ac.builder, LLVMIntULT, ac_get_thread_id(&ctx->ac), num, "");
4265bf215546Sopenharmony_ci      break;
4266bf215546Sopenharmony_ci   }
4267bf215546Sopenharmony_ci   case nir_intrinsic_alloc_vertices_and_primitives_amd:
4268bf215546Sopenharmony_ci      /* The caller should only call this conditionally for wave 0, so pass NULL to disable
4269bf215546Sopenharmony_ci       * the wave 0 check inside this function.
4270bf215546Sopenharmony_ci       */
4271bf215546Sopenharmony_ci      ac_build_sendmsg_gs_alloc_req(&ctx->ac, NULL,
4272bf215546Sopenharmony_ci                                    get_src(ctx, instr->src[0]),
4273bf215546Sopenharmony_ci                                    get_src(ctx, instr->src[1]));
4274bf215546Sopenharmony_ci      break;
4275bf215546Sopenharmony_ci   case nir_intrinsic_overwrite_vs_arguments_amd:
4276bf215546Sopenharmony_ci      ctx->vertex_id_replaced = get_src(ctx, instr->src[0]);
4277bf215546Sopenharmony_ci      ctx->instance_id_replaced = get_src(ctx, instr->src[1]);
4278bf215546Sopenharmony_ci      break;
4279bf215546Sopenharmony_ci   case nir_intrinsic_overwrite_tes_arguments_amd:
4280bf215546Sopenharmony_ci      ctx->tes_u_replaced = get_src(ctx, instr->src[0]);
4281bf215546Sopenharmony_ci      ctx->tes_v_replaced = get_src(ctx, instr->src[1]);
4282bf215546Sopenharmony_ci      ctx->tes_rel_patch_id_replaced = get_src(ctx, instr->src[2]);
4283bf215546Sopenharmony_ci      ctx->tes_patch_id_replaced = get_src(ctx, instr->src[3]);
4284bf215546Sopenharmony_ci      break;
4285bf215546Sopenharmony_ci   case nir_intrinsic_export_primitive_amd: {
4286bf215546Sopenharmony_ci      struct ac_ngg_prim prim = {0};
4287bf215546Sopenharmony_ci      prim.passthrough = get_src(ctx, instr->src[0]);
4288bf215546Sopenharmony_ci      ac_build_export_prim(&ctx->ac, &prim);
4289bf215546Sopenharmony_ci      break;
4290bf215546Sopenharmony_ci   }
4291bf215546Sopenharmony_ci   case nir_intrinsic_gds_atomic_add_amd: {
4292bf215546Sopenharmony_ci      LLVMValueRef store_val = get_src(ctx, instr->src[0]);
4293bf215546Sopenharmony_ci      LLVMValueRef addr = get_src(ctx, instr->src[1]);
4294bf215546Sopenharmony_ci      LLVMTypeRef gds_ptr_type = LLVMPointerType(ctx->ac.i32, AC_ADDR_SPACE_GDS);
4295bf215546Sopenharmony_ci      LLVMValueRef gds_base = LLVMBuildIntToPtr(ctx->ac.builder, addr, gds_ptr_type, "");
4296bf215546Sopenharmony_ci      ac_build_atomic_rmw(&ctx->ac, LLVMAtomicRMWBinOpAdd, gds_base, store_val, "workgroup-one-as");
4297bf215546Sopenharmony_ci      break;
4298bf215546Sopenharmony_ci   }
4299bf215546Sopenharmony_ci   case nir_intrinsic_export_vertex_amd:
4300bf215546Sopenharmony_ci      ctx->abi->export_vertex(ctx->abi);
4301bf215546Sopenharmony_ci      break;
4302bf215546Sopenharmony_ci   case nir_intrinsic_elect:
4303bf215546Sopenharmony_ci      result = LLVMBuildICmp(ctx->ac.builder, LLVMIntEQ, visit_first_invocation(ctx),
4304bf215546Sopenharmony_ci                             ac_get_thread_id(&ctx->ac), "");
4305bf215546Sopenharmony_ci      break;
4306bf215546Sopenharmony_ci   case nir_intrinsic_byte_permute_amd:
4307bf215546Sopenharmony_ci      if (LLVM_VERSION_MAJOR < 13) {
4308bf215546Sopenharmony_ci         assert("unimplemented byte_permute, LLVM 12 doesn't have amdgcn.perm");
4309bf215546Sopenharmony_ci         break;
4310bf215546Sopenharmony_ci      }
4311bf215546Sopenharmony_ci      result = ac_build_intrinsic(&ctx->ac, "llvm.amdgcn.perm", ctx->ac.i32,
4312bf215546Sopenharmony_ci                                  (LLVMValueRef[]){get_src(ctx, instr->src[0]),
4313bf215546Sopenharmony_ci                                                   get_src(ctx, instr->src[1]),
4314bf215546Sopenharmony_ci                                                   get_src(ctx, instr->src[2])},
4315bf215546Sopenharmony_ci                                  3, AC_FUNC_ATTR_READNONE);
4316bf215546Sopenharmony_ci      break;
4317bf215546Sopenharmony_ci   case nir_intrinsic_lane_permute_16_amd:
4318bf215546Sopenharmony_ci      result = ac_build_intrinsic(&ctx->ac, "llvm.amdgcn.permlane16", ctx->ac.i32,
4319bf215546Sopenharmony_ci                                  (LLVMValueRef[]){get_src(ctx, instr->src[0]),
4320bf215546Sopenharmony_ci                                                   get_src(ctx, instr->src[0]),
4321bf215546Sopenharmony_ci                                                   get_src(ctx, instr->src[1]),
4322bf215546Sopenharmony_ci                                                   get_src(ctx, instr->src[2]),
4323bf215546Sopenharmony_ci                                                   ctx->ac.i1false,
4324bf215546Sopenharmony_ci                                                   ctx->ac.i1false},
4325bf215546Sopenharmony_ci                                  6, AC_FUNC_ATTR_READNONE | AC_FUNC_ATTR_CONVERGENT);
4326bf215546Sopenharmony_ci      break;
4327bf215546Sopenharmony_ci   case nir_intrinsic_load_force_vrs_rates_amd:
4328bf215546Sopenharmony_ci      result = ac_get_arg(&ctx->ac, ctx->args->force_vrs_rates);
4329bf215546Sopenharmony_ci      break;
4330bf215546Sopenharmony_ci   case nir_intrinsic_load_scalar_arg_amd:
4331bf215546Sopenharmony_ci   case nir_intrinsic_load_vector_arg_amd: {
4332bf215546Sopenharmony_ci      assert(nir_intrinsic_base(instr) < AC_MAX_ARGS);
4333bf215546Sopenharmony_ci      result = ac_to_integer(&ctx->ac, LLVMGetParam(ctx->main_function, nir_intrinsic_base(instr)));
4334bf215546Sopenharmony_ci      break;
4335bf215546Sopenharmony_ci   }
4336bf215546Sopenharmony_ci   case nir_intrinsic_load_smem_amd: {
4337bf215546Sopenharmony_ci      LLVMValueRef base = get_src(ctx, instr->src[0]);
4338bf215546Sopenharmony_ci      LLVMValueRef offset = get_src(ctx, instr->src[1]);
4339bf215546Sopenharmony_ci
4340bf215546Sopenharmony_ci      LLVMTypeRef result_type = get_def_type(ctx, &instr->dest.ssa);
4341bf215546Sopenharmony_ci      LLVMTypeRef ptr_type = LLVMPointerType(result_type, AC_ADDR_SPACE_CONST);
4342bf215546Sopenharmony_ci      LLVMTypeRef byte_ptr_type = LLVMPointerType(ctx->ac.i8, AC_ADDR_SPACE_CONST);
4343bf215546Sopenharmony_ci
4344bf215546Sopenharmony_ci      LLVMValueRef addr = LLVMBuildIntToPtr(ctx->ac.builder, base, byte_ptr_type, "");
4345bf215546Sopenharmony_ci      addr = LLVMBuildGEP2(ctx->ac.builder, ctx->ac.i8, addr, &offset, 1, "");
4346bf215546Sopenharmony_ci      addr = LLVMBuildBitCast(ctx->ac.builder, addr, ptr_type, "");
4347bf215546Sopenharmony_ci
4348bf215546Sopenharmony_ci      LLVMSetMetadata(addr, ctx->ac.uniform_md_kind, ctx->ac.empty_md);
4349bf215546Sopenharmony_ci      result = LLVMBuildLoad2(ctx->ac.builder, result_type, addr, "");
4350bf215546Sopenharmony_ci      LLVMSetMetadata(result, ctx->ac.invariant_load_md_kind, ctx->ac.empty_md);
4351bf215546Sopenharmony_ci      break;
4352bf215546Sopenharmony_ci   }
4353bf215546Sopenharmony_ci   default:
4354bf215546Sopenharmony_ci      fprintf(stderr, "Unknown intrinsic: ");
4355bf215546Sopenharmony_ci      nir_print_instr(&instr->instr, stderr);
4356bf215546Sopenharmony_ci      fprintf(stderr, "\n");
4357bf215546Sopenharmony_ci      abort();
4358bf215546Sopenharmony_ci      break;
4359bf215546Sopenharmony_ci   }
4360bf215546Sopenharmony_ci   if (result) {
4361bf215546Sopenharmony_ci      ctx->ssa_defs[instr->dest.ssa.index] = result;
4362bf215546Sopenharmony_ci   }
4363bf215546Sopenharmony_ci}
4364bf215546Sopenharmony_ci
4365bf215546Sopenharmony_cistatic LLVMValueRef get_bindless_index_from_uniform(struct ac_nir_context *ctx, unsigned base_index,
4366bf215546Sopenharmony_ci                                                    unsigned constant_index,
4367bf215546Sopenharmony_ci                                                    LLVMValueRef dynamic_index)
4368bf215546Sopenharmony_ci{
4369bf215546Sopenharmony_ci   LLVMValueRef offset = LLVMConstInt(ctx->ac.i32, base_index * 4, 0);
4370bf215546Sopenharmony_ci   LLVMValueRef index = LLVMBuildAdd(ctx->ac.builder, dynamic_index,
4371bf215546Sopenharmony_ci                                     LLVMConstInt(ctx->ac.i32, constant_index, 0), "");
4372bf215546Sopenharmony_ci
4373bf215546Sopenharmony_ci   /* Bindless uniforms are 64bit so multiple index by 8 */
4374bf215546Sopenharmony_ci   index = LLVMBuildMul(ctx->ac.builder, index, LLVMConstInt(ctx->ac.i32, 8, 0), "");
4375bf215546Sopenharmony_ci   offset = LLVMBuildAdd(ctx->ac.builder, offset, index, "");
4376bf215546Sopenharmony_ci
4377bf215546Sopenharmony_ci   LLVMValueRef ubo_index = ctx->abi->load_ubo(ctx->abi, ctx->ac.i32_0);
4378bf215546Sopenharmony_ci
4379bf215546Sopenharmony_ci   LLVMValueRef ret =
4380bf215546Sopenharmony_ci      ac_build_buffer_load(&ctx->ac, ubo_index, 1, NULL, offset, NULL, ctx->ac.f32, 0, true, true);
4381bf215546Sopenharmony_ci
4382bf215546Sopenharmony_ci   return LLVMBuildBitCast(ctx->ac.builder, ret, ctx->ac.i32, "");
4383bf215546Sopenharmony_ci}
4384bf215546Sopenharmony_ci
4385bf215546Sopenharmony_cistruct sampler_desc_address {
4386bf215546Sopenharmony_ci   unsigned descriptor_set;
4387bf215546Sopenharmony_ci   unsigned base_index; /* binding in vulkan */
4388bf215546Sopenharmony_ci   unsigned constant_index;
4389bf215546Sopenharmony_ci   LLVMValueRef dynamic_index;
4390bf215546Sopenharmony_ci   bool image;
4391bf215546Sopenharmony_ci   bool bindless;
4392bf215546Sopenharmony_ci};
4393bf215546Sopenharmony_ci
4394bf215546Sopenharmony_cistatic struct sampler_desc_address get_sampler_desc_internal(struct ac_nir_context *ctx,
4395bf215546Sopenharmony_ci                                                             nir_deref_instr *deref_instr,
4396bf215546Sopenharmony_ci                                                             const nir_instr *instr, bool image)
4397bf215546Sopenharmony_ci{
4398bf215546Sopenharmony_ci   LLVMValueRef index = NULL;
4399bf215546Sopenharmony_ci   unsigned constant_index = 0;
4400bf215546Sopenharmony_ci   unsigned descriptor_set;
4401bf215546Sopenharmony_ci   unsigned base_index;
4402bf215546Sopenharmony_ci   bool bindless = false;
4403bf215546Sopenharmony_ci
4404bf215546Sopenharmony_ci   if (!deref_instr) {
4405bf215546Sopenharmony_ci      descriptor_set = 0;
4406bf215546Sopenharmony_ci      if (image) {
4407bf215546Sopenharmony_ci         nir_intrinsic_instr *img_instr = nir_instr_as_intrinsic(instr);
4408bf215546Sopenharmony_ci         base_index = 0;
4409bf215546Sopenharmony_ci         bindless = true;
4410bf215546Sopenharmony_ci         index = get_src(ctx, img_instr->src[0]);
4411bf215546Sopenharmony_ci      } else {
4412bf215546Sopenharmony_ci         nir_tex_instr *tex_instr = nir_instr_as_tex(instr);
4413bf215546Sopenharmony_ci         int sampSrcIdx = nir_tex_instr_src_index(tex_instr, nir_tex_src_sampler_handle);
4414bf215546Sopenharmony_ci         if (sampSrcIdx != -1) {
4415bf215546Sopenharmony_ci            base_index = 0;
4416bf215546Sopenharmony_ci            bindless = true;
4417bf215546Sopenharmony_ci            index = get_src(ctx, tex_instr->src[sampSrcIdx].src);
4418bf215546Sopenharmony_ci         } else {
4419bf215546Sopenharmony_ci            assert(tex_instr && !image);
4420bf215546Sopenharmony_ci            base_index = tex_instr->sampler_index;
4421bf215546Sopenharmony_ci         }
4422bf215546Sopenharmony_ci      }
4423bf215546Sopenharmony_ci   } else {
4424bf215546Sopenharmony_ci      while (deref_instr->deref_type != nir_deref_type_var) {
4425bf215546Sopenharmony_ci         if (deref_instr->deref_type == nir_deref_type_array) {
4426bf215546Sopenharmony_ci            unsigned array_size = glsl_get_aoa_size(deref_instr->type);
4427bf215546Sopenharmony_ci            if (!array_size)
4428bf215546Sopenharmony_ci               array_size = 1;
4429bf215546Sopenharmony_ci
4430bf215546Sopenharmony_ci            if (nir_src_is_const(deref_instr->arr.index)) {
4431bf215546Sopenharmony_ci               constant_index += array_size * nir_src_as_uint(deref_instr->arr.index);
4432bf215546Sopenharmony_ci            } else {
4433bf215546Sopenharmony_ci               LLVMValueRef indirect = get_src(ctx, deref_instr->arr.index);
4434bf215546Sopenharmony_ci
4435bf215546Sopenharmony_ci               indirect = LLVMBuildMul(ctx->ac.builder, indirect,
4436bf215546Sopenharmony_ci                                       LLVMConstInt(ctx->ac.i32, array_size, false), "");
4437bf215546Sopenharmony_ci
4438bf215546Sopenharmony_ci               if (!index)
4439bf215546Sopenharmony_ci                  index = indirect;
4440bf215546Sopenharmony_ci               else
4441bf215546Sopenharmony_ci                  index = LLVMBuildAdd(ctx->ac.builder, index, indirect, "");
4442bf215546Sopenharmony_ci            }
4443bf215546Sopenharmony_ci
4444bf215546Sopenharmony_ci            deref_instr = nir_src_as_deref(deref_instr->parent);
4445bf215546Sopenharmony_ci         } else if (deref_instr->deref_type == nir_deref_type_struct) {
4446bf215546Sopenharmony_ci            unsigned sidx = deref_instr->strct.index;
4447bf215546Sopenharmony_ci            deref_instr = nir_src_as_deref(deref_instr->parent);
4448bf215546Sopenharmony_ci            constant_index += glsl_get_struct_location_offset(deref_instr->type, sidx);
4449bf215546Sopenharmony_ci         } else {
4450bf215546Sopenharmony_ci            unreachable("Unsupported deref type");
4451bf215546Sopenharmony_ci         }
4452bf215546Sopenharmony_ci      }
4453bf215546Sopenharmony_ci      descriptor_set = deref_instr->var->data.descriptor_set;
4454bf215546Sopenharmony_ci
4455bf215546Sopenharmony_ci      if (deref_instr->var->data.bindless) {
4456bf215546Sopenharmony_ci         /* For now just assert on unhandled variable types */
4457bf215546Sopenharmony_ci         assert(deref_instr->var->data.mode == nir_var_uniform);
4458bf215546Sopenharmony_ci
4459bf215546Sopenharmony_ci         base_index = deref_instr->var->data.driver_location;
4460bf215546Sopenharmony_ci         bindless = true;
4461bf215546Sopenharmony_ci
4462bf215546Sopenharmony_ci         index = index ? index : ctx->ac.i32_0;
4463bf215546Sopenharmony_ci         index = get_bindless_index_from_uniform(ctx, base_index, constant_index, index);
4464bf215546Sopenharmony_ci      } else
4465bf215546Sopenharmony_ci         base_index = deref_instr->var->data.binding;
4466bf215546Sopenharmony_ci   }
4467bf215546Sopenharmony_ci   return (struct sampler_desc_address){
4468bf215546Sopenharmony_ci      .descriptor_set = descriptor_set,
4469bf215546Sopenharmony_ci      .base_index = base_index,
4470bf215546Sopenharmony_ci      .constant_index = constant_index,
4471bf215546Sopenharmony_ci      .dynamic_index = index,
4472bf215546Sopenharmony_ci      .image = image,
4473bf215546Sopenharmony_ci      .bindless = bindless,
4474bf215546Sopenharmony_ci   };
4475bf215546Sopenharmony_ci}
4476bf215546Sopenharmony_ci
4477bf215546Sopenharmony_ci/* Extract any possibly divergent index into a separate value that can be fed
4478bf215546Sopenharmony_ci * into get_sampler_desc with the same arguments. */
4479bf215546Sopenharmony_cistatic LLVMValueRef get_sampler_desc_index(struct ac_nir_context *ctx, nir_deref_instr *deref_instr,
4480bf215546Sopenharmony_ci                                           const nir_instr *instr, bool image)
4481bf215546Sopenharmony_ci{
4482bf215546Sopenharmony_ci   struct sampler_desc_address addr = get_sampler_desc_internal(ctx, deref_instr, instr, image);
4483bf215546Sopenharmony_ci   return addr.dynamic_index;
4484bf215546Sopenharmony_ci}
4485bf215546Sopenharmony_ci
4486bf215546Sopenharmony_cistatic LLVMValueRef get_sampler_desc(struct ac_nir_context *ctx, nir_deref_instr *deref_instr,
4487bf215546Sopenharmony_ci                                     enum ac_descriptor_type desc_type, const nir_instr *instr,
4488bf215546Sopenharmony_ci                                     LLVMValueRef index, bool image, bool write)
4489bf215546Sopenharmony_ci{
4490bf215546Sopenharmony_ci   struct sampler_desc_address addr = get_sampler_desc_internal(ctx, deref_instr, instr, image);
4491bf215546Sopenharmony_ci   return ctx->abi->load_sampler_desc(ctx->abi, addr.descriptor_set, addr.base_index,
4492bf215546Sopenharmony_ci                                      addr.constant_index, index, desc_type, addr.image, write,
4493bf215546Sopenharmony_ci                                      addr.bindless);
4494bf215546Sopenharmony_ci}
4495bf215546Sopenharmony_ci
4496bf215546Sopenharmony_ci/* Disable anisotropic filtering if BASE_LEVEL == LAST_LEVEL.
4497bf215546Sopenharmony_ci *
4498bf215546Sopenharmony_ci * GFX6-GFX7:
4499bf215546Sopenharmony_ci *   If BASE_LEVEL == LAST_LEVEL, the shader must disable anisotropic
4500bf215546Sopenharmony_ci *   filtering manually. The driver sets img7 to a mask clearing
4501bf215546Sopenharmony_ci *   MAX_ANISO_RATIO if BASE_LEVEL == LAST_LEVEL. The shader must do:
4502bf215546Sopenharmony_ci *     s_and_b32 samp0, samp0, img7
4503bf215546Sopenharmony_ci *
4504bf215546Sopenharmony_ci * GFX8:
4505bf215546Sopenharmony_ci *   The ANISO_OVERRIDE sampler field enables this fix in TA.
4506bf215546Sopenharmony_ci */
4507bf215546Sopenharmony_cistatic LLVMValueRef sici_fix_sampler_aniso(struct ac_nir_context *ctx, LLVMValueRef res,
4508bf215546Sopenharmony_ci                                           LLVMValueRef samp)
4509bf215546Sopenharmony_ci{
4510bf215546Sopenharmony_ci   LLVMBuilderRef builder = ctx->ac.builder;
4511bf215546Sopenharmony_ci   LLVMValueRef img7, samp0;
4512bf215546Sopenharmony_ci
4513bf215546Sopenharmony_ci   if (ctx->ac.gfx_level >= GFX8)
4514bf215546Sopenharmony_ci      return samp;
4515bf215546Sopenharmony_ci
4516bf215546Sopenharmony_ci   img7 = LLVMBuildExtractElement(builder, res, LLVMConstInt(ctx->ac.i32, 7, 0), "");
4517bf215546Sopenharmony_ci   samp0 = LLVMBuildExtractElement(builder, samp, LLVMConstInt(ctx->ac.i32, 0, 0), "");
4518bf215546Sopenharmony_ci   samp0 = LLVMBuildAnd(builder, samp0, img7, "");
4519bf215546Sopenharmony_ci   return LLVMBuildInsertElement(builder, samp, samp0, LLVMConstInt(ctx->ac.i32, 0, 0), "");
4520bf215546Sopenharmony_ci}
4521bf215546Sopenharmony_ci
4522bf215546Sopenharmony_cistatic void tex_fetch_ptrs(struct ac_nir_context *ctx, nir_tex_instr *instr,
4523bf215546Sopenharmony_ci                           struct waterfall_context *wctx, LLVMValueRef *res_ptr,
4524bf215546Sopenharmony_ci                           LLVMValueRef *samp_ptr, LLVMValueRef *fmask_ptr)
4525bf215546Sopenharmony_ci{
4526bf215546Sopenharmony_ci   LLVMValueRef texture_dynamic_handle = NULL;
4527bf215546Sopenharmony_ci   LLVMValueRef sampler_dynamic_handle = NULL;
4528bf215546Sopenharmony_ci   nir_deref_instr *texture_deref_instr = NULL;
4529bf215546Sopenharmony_ci   nir_deref_instr *sampler_deref_instr = NULL;
4530bf215546Sopenharmony_ci   int plane = -1;
4531bf215546Sopenharmony_ci
4532bf215546Sopenharmony_ci   *res_ptr = NULL;
4533bf215546Sopenharmony_ci   *samp_ptr = NULL;
4534bf215546Sopenharmony_ci   *fmask_ptr = NULL;
4535bf215546Sopenharmony_ci   for (unsigned i = 0; i < instr->num_srcs; i++) {
4536bf215546Sopenharmony_ci      switch (instr->src[i].src_type) {
4537bf215546Sopenharmony_ci      case nir_tex_src_texture_deref:
4538bf215546Sopenharmony_ci         texture_deref_instr = nir_src_as_deref(instr->src[i].src);
4539bf215546Sopenharmony_ci         break;
4540bf215546Sopenharmony_ci      case nir_tex_src_sampler_deref:
4541bf215546Sopenharmony_ci         sampler_deref_instr = nir_src_as_deref(instr->src[i].src);
4542bf215546Sopenharmony_ci         break;
4543bf215546Sopenharmony_ci      case nir_tex_src_texture_handle:
4544bf215546Sopenharmony_ci      case nir_tex_src_sampler_handle: {
4545bf215546Sopenharmony_ci         LLVMValueRef val = get_src(ctx, instr->src[i].src);
4546bf215546Sopenharmony_ci         if (LLVMGetTypeKind(LLVMTypeOf(val)) == LLVMVectorTypeKind) {
4547bf215546Sopenharmony_ci            if (instr->src[i].src_type == nir_tex_src_texture_handle)
4548bf215546Sopenharmony_ci               *res_ptr = val;
4549bf215546Sopenharmony_ci            else
4550bf215546Sopenharmony_ci               *samp_ptr = val;
4551bf215546Sopenharmony_ci         } else {
4552bf215546Sopenharmony_ci            if (instr->src[i].src_type == nir_tex_src_texture_handle)
4553bf215546Sopenharmony_ci               texture_dynamic_handle = val;
4554bf215546Sopenharmony_ci            else
4555bf215546Sopenharmony_ci               sampler_dynamic_handle = val;
4556bf215546Sopenharmony_ci         }
4557bf215546Sopenharmony_ci         break;
4558bf215546Sopenharmony_ci      }
4559bf215546Sopenharmony_ci      case nir_tex_src_plane:
4560bf215546Sopenharmony_ci         plane = nir_src_as_int(instr->src[i].src);
4561bf215546Sopenharmony_ci         break;
4562bf215546Sopenharmony_ci      default:
4563bf215546Sopenharmony_ci         break;
4564bf215546Sopenharmony_ci      }
4565bf215546Sopenharmony_ci   }
4566bf215546Sopenharmony_ci
4567bf215546Sopenharmony_ci   if (*res_ptr) {
4568bf215546Sopenharmony_ci      /* descriptors given through nir_tex_src_{texture,sampler}_handle */
4569bf215546Sopenharmony_ci      return;
4570bf215546Sopenharmony_ci   }
4571bf215546Sopenharmony_ci
4572bf215546Sopenharmony_ci   enum ac_descriptor_type main_descriptor =
4573bf215546Sopenharmony_ci      instr->sampler_dim == GLSL_SAMPLER_DIM_BUF ? AC_DESC_BUFFER : AC_DESC_IMAGE;
4574bf215546Sopenharmony_ci
4575bf215546Sopenharmony_ci   if (plane >= 0) {
4576bf215546Sopenharmony_ci      assert(instr->op != nir_texop_txf_ms && instr->op != nir_texop_samples_identical);
4577bf215546Sopenharmony_ci      assert(instr->sampler_dim != GLSL_SAMPLER_DIM_BUF);
4578bf215546Sopenharmony_ci
4579bf215546Sopenharmony_ci      main_descriptor = AC_DESC_PLANE_0 + plane;
4580bf215546Sopenharmony_ci   }
4581bf215546Sopenharmony_ci
4582bf215546Sopenharmony_ci   if (instr->op == nir_texop_fragment_mask_fetch_amd || instr->op == nir_texop_samples_identical) {
4583bf215546Sopenharmony_ci      /* The fragment mask is fetched from the compressed
4584bf215546Sopenharmony_ci       * multisampled surface.
4585bf215546Sopenharmony_ci       */
4586bf215546Sopenharmony_ci      assert(ctx->ac.gfx_level < GFX11);
4587bf215546Sopenharmony_ci      main_descriptor = AC_DESC_FMASK;
4588bf215546Sopenharmony_ci   }
4589bf215546Sopenharmony_ci
4590bf215546Sopenharmony_ci   if (texture_dynamic_handle) {
4591bf215546Sopenharmony_ci      /* descriptor handles given through nir_tex_src_{texture,sampler}_handle */
4592bf215546Sopenharmony_ci      if (instr->texture_non_uniform)
4593bf215546Sopenharmony_ci         texture_dynamic_handle = enter_waterfall(ctx, &wctx[0], texture_dynamic_handle, true);
4594bf215546Sopenharmony_ci
4595bf215546Sopenharmony_ci      if (instr->sampler_non_uniform)
4596bf215546Sopenharmony_ci         sampler_dynamic_handle = enter_waterfall(ctx, &wctx[1], sampler_dynamic_handle, true);
4597bf215546Sopenharmony_ci
4598bf215546Sopenharmony_ci      *res_ptr = ctx->abi->load_sampler_desc(ctx->abi, 0, 0, 0, texture_dynamic_handle,
4599bf215546Sopenharmony_ci                                             main_descriptor, false, false, true);
4600bf215546Sopenharmony_ci
4601bf215546Sopenharmony_ci      if (samp_ptr)
4602bf215546Sopenharmony_ci         *samp_ptr = ctx->abi->load_sampler_desc(ctx->abi, 0, 0, 0, sampler_dynamic_handle,
4603bf215546Sopenharmony_ci                                                 AC_DESC_SAMPLER, false, false, true);
4604bf215546Sopenharmony_ci      return;
4605bf215546Sopenharmony_ci   }
4606bf215546Sopenharmony_ci
4607bf215546Sopenharmony_ci   LLVMValueRef texture_dynamic_index =
4608bf215546Sopenharmony_ci      get_sampler_desc_index(ctx, texture_deref_instr, &instr->instr, false);
4609bf215546Sopenharmony_ci   if (!sampler_deref_instr)
4610bf215546Sopenharmony_ci      sampler_deref_instr = texture_deref_instr;
4611bf215546Sopenharmony_ci
4612bf215546Sopenharmony_ci   LLVMValueRef sampler_dynamic_index =
4613bf215546Sopenharmony_ci      get_sampler_desc_index(ctx, sampler_deref_instr, &instr->instr, false);
4614bf215546Sopenharmony_ci
4615bf215546Sopenharmony_ci   /* instr->sampler_non_uniform and texture_non_uniform are always false in GLSL,
4616bf215546Sopenharmony_ci    * but this can lead to unexpected behavior if texture/sampler index come from
4617bf215546Sopenharmony_ci    * a vertex attribute.
4618bf215546Sopenharmony_ci    * For instance, 2 consecutive draws using 2 different index values,
4619bf215546Sopenharmony_ci    * could be squashed together by the hw - producing a single draw with
4620bf215546Sopenharmony_ci    * non-dynamically uniform index.
4621bf215546Sopenharmony_ci    * To avoid this, detect divergent indexing, and use enter_waterfall.
4622bf215546Sopenharmony_ci    * See https://gitlab.freedesktop.org/mesa/mesa/-/issues/2253.
4623bf215546Sopenharmony_ci    */
4624bf215546Sopenharmony_ci   if (instr->texture_non_uniform ||
4625bf215546Sopenharmony_ci       (ctx->abi->use_waterfall_for_divergent_tex_samplers && texture_deref_instr->dest.ssa.divergent))
4626bf215546Sopenharmony_ci      texture_dynamic_index = enter_waterfall(ctx, wctx + 0, texture_dynamic_index, true);
4627bf215546Sopenharmony_ci
4628bf215546Sopenharmony_ci   if (instr->sampler_non_uniform ||
4629bf215546Sopenharmony_ci       (ctx->abi->use_waterfall_for_divergent_tex_samplers && sampler_deref_instr->dest.ssa.divergent))
4630bf215546Sopenharmony_ci      sampler_dynamic_index = enter_waterfall(ctx, wctx + 1, sampler_dynamic_index, true);
4631bf215546Sopenharmony_ci
4632bf215546Sopenharmony_ci   *res_ptr = get_sampler_desc(ctx, texture_deref_instr, main_descriptor, &instr->instr,
4633bf215546Sopenharmony_ci                               texture_dynamic_index, false, false);
4634bf215546Sopenharmony_ci
4635bf215546Sopenharmony_ci   if (samp_ptr) {
4636bf215546Sopenharmony_ci      *samp_ptr = get_sampler_desc(ctx, sampler_deref_instr, AC_DESC_SAMPLER, &instr->instr,
4637bf215546Sopenharmony_ci                                   sampler_dynamic_index, false, false);
4638bf215546Sopenharmony_ci      if (instr->sampler_dim < GLSL_SAMPLER_DIM_RECT)
4639bf215546Sopenharmony_ci         *samp_ptr = sici_fix_sampler_aniso(ctx, *res_ptr, *samp_ptr);
4640bf215546Sopenharmony_ci   }
4641bf215546Sopenharmony_ci   if (ctx->ac.gfx_level < GFX11 &&
4642bf215546Sopenharmony_ci       fmask_ptr && (instr->op == nir_texop_txf_ms || instr->op == nir_texop_samples_identical))
4643bf215546Sopenharmony_ci      *fmask_ptr = get_sampler_desc(ctx, texture_deref_instr, AC_DESC_FMASK, &instr->instr,
4644bf215546Sopenharmony_ci                                    texture_dynamic_index, false, false);
4645bf215546Sopenharmony_ci}
4646bf215546Sopenharmony_ci
4647bf215546Sopenharmony_cistatic LLVMValueRef apply_round_slice(struct ac_llvm_context *ctx, LLVMValueRef coord)
4648bf215546Sopenharmony_ci{
4649bf215546Sopenharmony_ci   coord = ac_to_float(ctx, coord);
4650bf215546Sopenharmony_ci   coord = ac_build_round(ctx, coord);
4651bf215546Sopenharmony_ci   coord = ac_to_integer(ctx, coord);
4652bf215546Sopenharmony_ci   return coord;
4653bf215546Sopenharmony_ci}
4654bf215546Sopenharmony_ci
4655bf215546Sopenharmony_cistatic void visit_tex(struct ac_nir_context *ctx, nir_tex_instr *instr)
4656bf215546Sopenharmony_ci{
4657bf215546Sopenharmony_ci   LLVMValueRef result = NULL;
4658bf215546Sopenharmony_ci   struct ac_image_args args = {0};
4659bf215546Sopenharmony_ci   LLVMValueRef fmask_ptr = NULL, sample_index = NULL;
4660bf215546Sopenharmony_ci   LLVMValueRef ddx = NULL, ddy = NULL;
4661bf215546Sopenharmony_ci   unsigned offset_src = 0;
4662bf215546Sopenharmony_ci   struct waterfall_context wctx[2] = {{{0}}};
4663bf215546Sopenharmony_ci
4664bf215546Sopenharmony_ci   tex_fetch_ptrs(ctx, instr, wctx, &args.resource, &args.sampler, &fmask_ptr);
4665bf215546Sopenharmony_ci
4666bf215546Sopenharmony_ci   for (unsigned i = 0; i < instr->num_srcs; i++) {
4667bf215546Sopenharmony_ci      switch (instr->src[i].src_type) {
4668bf215546Sopenharmony_ci      case nir_tex_src_coord: {
4669bf215546Sopenharmony_ci         LLVMValueRef coord = get_src(ctx, instr->src[i].src);
4670bf215546Sopenharmony_ci         args.a16 = instr->src[i].src.ssa->bit_size == 16;
4671bf215546Sopenharmony_ci         for (unsigned chan = 0; chan < instr->coord_components; ++chan)
4672bf215546Sopenharmony_ci            args.coords[chan] = ac_llvm_extract_elem(&ctx->ac, coord, chan);
4673bf215546Sopenharmony_ci         break;
4674bf215546Sopenharmony_ci      }
4675bf215546Sopenharmony_ci      case nir_tex_src_projector:
4676bf215546Sopenharmony_ci         break;
4677bf215546Sopenharmony_ci      case nir_tex_src_comparator:
4678bf215546Sopenharmony_ci         if (instr->is_shadow) {
4679bf215546Sopenharmony_ci            args.compare = get_src(ctx, instr->src[i].src);
4680bf215546Sopenharmony_ci            args.compare = ac_to_float(&ctx->ac, args.compare);
4681bf215546Sopenharmony_ci            assert(instr->src[i].src.ssa->bit_size == 32);
4682bf215546Sopenharmony_ci         }
4683bf215546Sopenharmony_ci         break;
4684bf215546Sopenharmony_ci      case nir_tex_src_offset:
4685bf215546Sopenharmony_ci         args.offset = get_src(ctx, instr->src[i].src);
4686bf215546Sopenharmony_ci         offset_src = i;
4687bf215546Sopenharmony_ci         /* We pack it with bit shifts, so we need it to be 32-bit. */
4688bf215546Sopenharmony_ci         assert(ac_get_elem_bits(&ctx->ac, LLVMTypeOf(args.offset)) == 32);
4689bf215546Sopenharmony_ci         break;
4690bf215546Sopenharmony_ci      case nir_tex_src_bias:
4691bf215546Sopenharmony_ci         args.bias = get_src(ctx, instr->src[i].src);
4692bf215546Sopenharmony_ci         assert(ac_get_elem_bits(&ctx->ac, LLVMTypeOf(args.bias)) == 32);
4693bf215546Sopenharmony_ci         break;
4694bf215546Sopenharmony_ci      case nir_tex_src_lod:
4695bf215546Sopenharmony_ci         if (nir_src_is_const(instr->src[i].src) && nir_src_as_uint(instr->src[i].src) == 0)
4696bf215546Sopenharmony_ci            args.level_zero = true;
4697bf215546Sopenharmony_ci         else
4698bf215546Sopenharmony_ci            args.lod = get_src(ctx, instr->src[i].src);
4699bf215546Sopenharmony_ci         break;
4700bf215546Sopenharmony_ci      case nir_tex_src_ms_index:
4701bf215546Sopenharmony_ci         sample_index = get_src(ctx, instr->src[i].src);
4702bf215546Sopenharmony_ci         break;
4703bf215546Sopenharmony_ci      case nir_tex_src_ddx:
4704bf215546Sopenharmony_ci         ddx = get_src(ctx, instr->src[i].src);
4705bf215546Sopenharmony_ci         args.g16 = instr->src[i].src.ssa->bit_size == 16;
4706bf215546Sopenharmony_ci         break;
4707bf215546Sopenharmony_ci      case nir_tex_src_ddy:
4708bf215546Sopenharmony_ci         ddy = get_src(ctx, instr->src[i].src);
4709bf215546Sopenharmony_ci         assert(LLVMTypeOf(ddy) == LLVMTypeOf(ddx));
4710bf215546Sopenharmony_ci         break;
4711bf215546Sopenharmony_ci      case nir_tex_src_min_lod:
4712bf215546Sopenharmony_ci         args.min_lod = get_src(ctx, instr->src[i].src);
4713bf215546Sopenharmony_ci         break;
4714bf215546Sopenharmony_ci      case nir_tex_src_texture_offset:
4715bf215546Sopenharmony_ci      case nir_tex_src_sampler_offset:
4716bf215546Sopenharmony_ci      case nir_tex_src_plane:
4717bf215546Sopenharmony_ci      default:
4718bf215546Sopenharmony_ci         break;
4719bf215546Sopenharmony_ci      }
4720bf215546Sopenharmony_ci   }
4721bf215546Sopenharmony_ci
4722bf215546Sopenharmony_ci   if (instr->op == nir_texop_txs && instr->sampler_dim == GLSL_SAMPLER_DIM_BUF) {
4723bf215546Sopenharmony_ci      result = get_buffer_size(ctx, args.resource, true);
4724bf215546Sopenharmony_ci      goto write_result;
4725bf215546Sopenharmony_ci   }
4726bf215546Sopenharmony_ci
4727bf215546Sopenharmony_ci   if (instr->op == nir_texop_texture_samples) {
4728bf215546Sopenharmony_ci      LLVMValueRef res, samples, is_msaa;
4729bf215546Sopenharmony_ci      LLVMValueRef default_sample;
4730bf215546Sopenharmony_ci
4731bf215546Sopenharmony_ci      res = LLVMBuildBitCast(ctx->ac.builder, args.resource, ctx->ac.v8i32, "");
4732bf215546Sopenharmony_ci      samples =
4733bf215546Sopenharmony_ci         LLVMBuildExtractElement(ctx->ac.builder, res, LLVMConstInt(ctx->ac.i32, 3, false), "");
4734bf215546Sopenharmony_ci      is_msaa = LLVMBuildLShr(ctx->ac.builder, samples, LLVMConstInt(ctx->ac.i32, 28, false), "");
4735bf215546Sopenharmony_ci      is_msaa = LLVMBuildAnd(ctx->ac.builder, is_msaa, LLVMConstInt(ctx->ac.i32, 0xe, false), "");
4736bf215546Sopenharmony_ci      is_msaa = LLVMBuildICmp(ctx->ac.builder, LLVMIntEQ, is_msaa,
4737bf215546Sopenharmony_ci                              LLVMConstInt(ctx->ac.i32, 0xe, false), "");
4738bf215546Sopenharmony_ci
4739bf215546Sopenharmony_ci      samples = LLVMBuildLShr(ctx->ac.builder, samples, LLVMConstInt(ctx->ac.i32, 16, false), "");
4740bf215546Sopenharmony_ci      samples = LLVMBuildAnd(ctx->ac.builder, samples, LLVMConstInt(ctx->ac.i32, 0xf, false), "");
4741bf215546Sopenharmony_ci      samples = LLVMBuildShl(ctx->ac.builder, ctx->ac.i32_1, samples, "");
4742bf215546Sopenharmony_ci
4743bf215546Sopenharmony_ci      if (ctx->abi->robust_buffer_access) {
4744bf215546Sopenharmony_ci         LLVMValueRef dword1, is_null_descriptor;
4745bf215546Sopenharmony_ci
4746bf215546Sopenharmony_ci         /* Extract the second dword of the descriptor, if it's
4747bf215546Sopenharmony_ci          * all zero, then it's a null descriptor.
4748bf215546Sopenharmony_ci          */
4749bf215546Sopenharmony_ci         dword1 =
4750bf215546Sopenharmony_ci            LLVMBuildExtractElement(ctx->ac.builder, res, LLVMConstInt(ctx->ac.i32, 1, false), "");
4751bf215546Sopenharmony_ci         is_null_descriptor = LLVMBuildICmp(ctx->ac.builder, LLVMIntEQ, dword1,
4752bf215546Sopenharmony_ci                                            LLVMConstInt(ctx->ac.i32, 0, false), "");
4753bf215546Sopenharmony_ci         default_sample =
4754bf215546Sopenharmony_ci            LLVMBuildSelect(ctx->ac.builder, is_null_descriptor, ctx->ac.i32_0, ctx->ac.i32_1, "");
4755bf215546Sopenharmony_ci      } else {
4756bf215546Sopenharmony_ci         default_sample = ctx->ac.i32_1;
4757bf215546Sopenharmony_ci      }
4758bf215546Sopenharmony_ci
4759bf215546Sopenharmony_ci      samples = LLVMBuildSelect(ctx->ac.builder, is_msaa, samples, default_sample, "");
4760bf215546Sopenharmony_ci      result = samples;
4761bf215546Sopenharmony_ci      goto write_result;
4762bf215546Sopenharmony_ci   }
4763bf215546Sopenharmony_ci
4764bf215546Sopenharmony_ci   if (args.offset && instr->op != nir_texop_txf && instr->op != nir_texop_txf_ms) {
4765bf215546Sopenharmony_ci      LLVMValueRef offset[3], pack;
4766bf215546Sopenharmony_ci      for (unsigned chan = 0; chan < 3; ++chan)
4767bf215546Sopenharmony_ci         offset[chan] = ctx->ac.i32_0;
4768bf215546Sopenharmony_ci
4769bf215546Sopenharmony_ci      unsigned num_components = ac_get_llvm_num_components(args.offset);
4770bf215546Sopenharmony_ci      for (unsigned chan = 0; chan < num_components; chan++) {
4771bf215546Sopenharmony_ci         offset[chan] = ac_llvm_extract_elem(&ctx->ac, args.offset, chan);
4772bf215546Sopenharmony_ci         offset[chan] =
4773bf215546Sopenharmony_ci            LLVMBuildAnd(ctx->ac.builder, offset[chan], LLVMConstInt(ctx->ac.i32, 0x3f, false), "");
4774bf215546Sopenharmony_ci         if (chan)
4775bf215546Sopenharmony_ci            offset[chan] = LLVMBuildShl(ctx->ac.builder, offset[chan],
4776bf215546Sopenharmony_ci                                        LLVMConstInt(ctx->ac.i32, chan * 8, false), "");
4777bf215546Sopenharmony_ci      }
4778bf215546Sopenharmony_ci      pack = LLVMBuildOr(ctx->ac.builder, offset[0], offset[1], "");
4779bf215546Sopenharmony_ci      pack = LLVMBuildOr(ctx->ac.builder, pack, offset[2], "");
4780bf215546Sopenharmony_ci      args.offset = pack;
4781bf215546Sopenharmony_ci   }
4782bf215546Sopenharmony_ci
4783bf215546Sopenharmony_ci   /* Section 8.23.1 (Depth Texture Comparison Mode) of the
4784bf215546Sopenharmony_ci    * OpenGL 4.5 spec says:
4785bf215546Sopenharmony_ci    *
4786bf215546Sopenharmony_ci    *    "If the texture’s internal format indicates a fixed-point
4787bf215546Sopenharmony_ci    *     depth texture, then D_t and D_ref are clamped to the
4788bf215546Sopenharmony_ci    *     range [0, 1]; otherwise no clamping is performed."
4789bf215546Sopenharmony_ci    *
4790bf215546Sopenharmony_ci    * TC-compatible HTILE promotes Z16 and Z24 to Z32_FLOAT,
4791bf215546Sopenharmony_ci    * so the depth comparison value isn't clamped for Z16 and
4792bf215546Sopenharmony_ci    * Z24 anymore. Do it manually here for GFX8-9; GFX10 has
4793bf215546Sopenharmony_ci    * an explicitly clamped 32-bit float format.
4794bf215546Sopenharmony_ci    */
4795bf215546Sopenharmony_ci   if (args.compare && ctx->ac.gfx_level >= GFX8 && ctx->ac.gfx_level <= GFX9 &&
4796bf215546Sopenharmony_ci       ctx->abi->clamp_shadow_reference) {
4797bf215546Sopenharmony_ci      LLVMValueRef upgraded, clamped;
4798bf215546Sopenharmony_ci
4799bf215546Sopenharmony_ci      upgraded = LLVMBuildExtractElement(ctx->ac.builder, args.sampler,
4800bf215546Sopenharmony_ci                                         LLVMConstInt(ctx->ac.i32, 3, false), "");
4801bf215546Sopenharmony_ci      upgraded = LLVMBuildLShr(ctx->ac.builder, upgraded, LLVMConstInt(ctx->ac.i32, 29, false), "");
4802bf215546Sopenharmony_ci      upgraded = LLVMBuildTrunc(ctx->ac.builder, upgraded, ctx->ac.i1, "");
4803bf215546Sopenharmony_ci      clamped = ac_build_clamp(&ctx->ac, args.compare);
4804bf215546Sopenharmony_ci      args.compare = LLVMBuildSelect(ctx->ac.builder, upgraded, clamped, args.compare, "");
4805bf215546Sopenharmony_ci   }
4806bf215546Sopenharmony_ci
4807bf215546Sopenharmony_ci   /* pack derivatives */
4808bf215546Sopenharmony_ci   if (ddx || ddy) {
4809bf215546Sopenharmony_ci      int num_src_deriv_channels, num_dest_deriv_channels;
4810bf215546Sopenharmony_ci      switch (instr->sampler_dim) {
4811bf215546Sopenharmony_ci      case GLSL_SAMPLER_DIM_3D:
4812bf215546Sopenharmony_ci      case GLSL_SAMPLER_DIM_CUBE:
4813bf215546Sopenharmony_ci         num_src_deriv_channels = 3;
4814bf215546Sopenharmony_ci         num_dest_deriv_channels = 3;
4815bf215546Sopenharmony_ci         break;
4816bf215546Sopenharmony_ci      case GLSL_SAMPLER_DIM_2D:
4817bf215546Sopenharmony_ci      default:
4818bf215546Sopenharmony_ci         num_src_deriv_channels = 2;
4819bf215546Sopenharmony_ci         num_dest_deriv_channels = 2;
4820bf215546Sopenharmony_ci         break;
4821bf215546Sopenharmony_ci      case GLSL_SAMPLER_DIM_1D:
4822bf215546Sopenharmony_ci         num_src_deriv_channels = 1;
4823bf215546Sopenharmony_ci         if (ctx->ac.gfx_level == GFX9) {
4824bf215546Sopenharmony_ci            num_dest_deriv_channels = 2;
4825bf215546Sopenharmony_ci         } else {
4826bf215546Sopenharmony_ci            num_dest_deriv_channels = 1;
4827bf215546Sopenharmony_ci         }
4828bf215546Sopenharmony_ci         break;
4829bf215546Sopenharmony_ci      }
4830bf215546Sopenharmony_ci
4831bf215546Sopenharmony_ci      for (unsigned i = 0; i < num_src_deriv_channels; i++) {
4832bf215546Sopenharmony_ci         args.derivs[i] = ac_to_float(&ctx->ac, ac_llvm_extract_elem(&ctx->ac, ddx, i));
4833bf215546Sopenharmony_ci         args.derivs[num_dest_deriv_channels + i] =
4834bf215546Sopenharmony_ci            ac_to_float(&ctx->ac, ac_llvm_extract_elem(&ctx->ac, ddy, i));
4835bf215546Sopenharmony_ci      }
4836bf215546Sopenharmony_ci      for (unsigned i = num_src_deriv_channels; i < num_dest_deriv_channels; i++) {
4837bf215546Sopenharmony_ci         LLVMValueRef zero = args.g16 ? ctx->ac.f16_0 : ctx->ac.f32_0;
4838bf215546Sopenharmony_ci         args.derivs[i] = zero;
4839bf215546Sopenharmony_ci         args.derivs[num_dest_deriv_channels + i] = zero;
4840bf215546Sopenharmony_ci      }
4841bf215546Sopenharmony_ci   }
4842bf215546Sopenharmony_ci
4843bf215546Sopenharmony_ci   if (instr->sampler_dim == GLSL_SAMPLER_DIM_CUBE && args.coords[0]) {
4844bf215546Sopenharmony_ci      for (unsigned chan = 0; chan < instr->coord_components; chan++)
4845bf215546Sopenharmony_ci         args.coords[chan] = ac_to_float(&ctx->ac, args.coords[chan]);
4846bf215546Sopenharmony_ci      if (instr->coord_components == 3)
4847bf215546Sopenharmony_ci         args.coords[3] = LLVMGetUndef(args.a16 ? ctx->ac.f16 : ctx->ac.f32);
4848bf215546Sopenharmony_ci      ac_prepare_cube_coords(&ctx->ac, instr->op == nir_texop_txd, instr->is_array,
4849bf215546Sopenharmony_ci                             instr->op == nir_texop_lod, args.coords, args.derivs);
4850bf215546Sopenharmony_ci   }
4851bf215546Sopenharmony_ci
4852bf215546Sopenharmony_ci   /* Texture coordinates fixups */
4853bf215546Sopenharmony_ci   if (instr->coord_components > 1 && instr->sampler_dim == GLSL_SAMPLER_DIM_1D &&
4854bf215546Sopenharmony_ci       instr->is_array && instr->op != nir_texop_txf) {
4855bf215546Sopenharmony_ci      args.coords[1] = apply_round_slice(&ctx->ac, args.coords[1]);
4856bf215546Sopenharmony_ci   }
4857bf215546Sopenharmony_ci
4858bf215546Sopenharmony_ci   if (instr->coord_components > 2 &&
4859bf215546Sopenharmony_ci       (instr->sampler_dim == GLSL_SAMPLER_DIM_2D || instr->sampler_dim == GLSL_SAMPLER_DIM_MS ||
4860bf215546Sopenharmony_ci        instr->sampler_dim == GLSL_SAMPLER_DIM_SUBPASS ||
4861bf215546Sopenharmony_ci        instr->sampler_dim == GLSL_SAMPLER_DIM_SUBPASS_MS) &&
4862bf215546Sopenharmony_ci       instr->is_array && instr->op != nir_texop_txf && instr->op != nir_texop_txf_ms &&
4863bf215546Sopenharmony_ci       instr->op != nir_texop_fragment_fetch_amd && instr->op != nir_texop_fragment_mask_fetch_amd) {
4864bf215546Sopenharmony_ci      args.coords[2] = apply_round_slice(&ctx->ac, args.coords[2]);
4865bf215546Sopenharmony_ci   }
4866bf215546Sopenharmony_ci
4867bf215546Sopenharmony_ci   if (ctx->ac.gfx_level == GFX9 && instr->sampler_dim == GLSL_SAMPLER_DIM_1D &&
4868bf215546Sopenharmony_ci       instr->op != nir_texop_lod) {
4869bf215546Sopenharmony_ci      LLVMValueRef filler;
4870bf215546Sopenharmony_ci      if (instr->op == nir_texop_txf)
4871bf215546Sopenharmony_ci         filler = args.a16 ? ctx->ac.i16_0 : ctx->ac.i32_0;
4872bf215546Sopenharmony_ci      else
4873bf215546Sopenharmony_ci         filler = LLVMConstReal(args.a16 ? ctx->ac.f16 : ctx->ac.f32, 0.5);
4874bf215546Sopenharmony_ci
4875bf215546Sopenharmony_ci      if (instr->is_array)
4876bf215546Sopenharmony_ci         args.coords[2] = args.coords[1];
4877bf215546Sopenharmony_ci      args.coords[1] = filler;
4878bf215546Sopenharmony_ci   }
4879bf215546Sopenharmony_ci
4880bf215546Sopenharmony_ci   /* Pack sample index */
4881bf215546Sopenharmony_ci   if (sample_index && (instr->op == nir_texop_txf_ms || instr->op == nir_texop_fragment_fetch_amd))
4882bf215546Sopenharmony_ci      args.coords[instr->coord_components] = sample_index;
4883bf215546Sopenharmony_ci
4884bf215546Sopenharmony_ci   if (instr->op == nir_texop_samples_identical) {
4885bf215546Sopenharmony_ci      assert(ctx->ac.gfx_level < GFX11);
4886bf215546Sopenharmony_ci      struct ac_image_args txf_args = {0};
4887bf215546Sopenharmony_ci      memcpy(txf_args.coords, args.coords, sizeof(txf_args.coords));
4888bf215546Sopenharmony_ci
4889bf215546Sopenharmony_ci      txf_args.dmask = 0xf;
4890bf215546Sopenharmony_ci      txf_args.resource = args.resource;
4891bf215546Sopenharmony_ci      txf_args.dim = instr->is_array ? ac_image_2darray : ac_image_2d;
4892bf215546Sopenharmony_ci      result = build_tex_intrinsic(ctx, instr, &txf_args);
4893bf215546Sopenharmony_ci
4894bf215546Sopenharmony_ci      result = LLVMBuildExtractElement(ctx->ac.builder, result, ctx->ac.i32_0, "");
4895bf215546Sopenharmony_ci      result = emit_int_cmp(&ctx->ac, LLVMIntEQ, result, ctx->ac.i32_0);
4896bf215546Sopenharmony_ci      goto write_result;
4897bf215546Sopenharmony_ci   }
4898bf215546Sopenharmony_ci
4899bf215546Sopenharmony_ci   if (ctx->ac.gfx_level < GFX11 &&
4900bf215546Sopenharmony_ci       (instr->sampler_dim == GLSL_SAMPLER_DIM_SUBPASS_MS ||
4901bf215546Sopenharmony_ci        instr->sampler_dim == GLSL_SAMPLER_DIM_MS) &&
4902bf215546Sopenharmony_ci       instr->op != nir_texop_txs && instr->op != nir_texop_fragment_fetch_amd &&
4903bf215546Sopenharmony_ci       instr->op != nir_texop_fragment_mask_fetch_amd) {
4904bf215546Sopenharmony_ci      unsigned sample_chan = instr->is_array ? 3 : 2;
4905bf215546Sopenharmony_ci      args.coords[sample_chan] = adjust_sample_index_using_fmask(
4906bf215546Sopenharmony_ci         &ctx->ac, args.coords[0], args.coords[1], instr->is_array ? args.coords[2] : NULL,
4907bf215546Sopenharmony_ci         args.coords[sample_chan], fmask_ptr);
4908bf215546Sopenharmony_ci   }
4909bf215546Sopenharmony_ci
4910bf215546Sopenharmony_ci   if (args.offset && (instr->op == nir_texop_txf || instr->op == nir_texop_txf_ms)) {
4911bf215546Sopenharmony_ci      int num_offsets = instr->src[offset_src].src.ssa->num_components;
4912bf215546Sopenharmony_ci      num_offsets = MIN2(num_offsets, instr->coord_components);
4913bf215546Sopenharmony_ci      for (unsigned i = 0; i < num_offsets; ++i) {
4914bf215546Sopenharmony_ci         LLVMValueRef off = ac_llvm_extract_elem(&ctx->ac, args.offset, i);
4915bf215546Sopenharmony_ci         if (args.a16)
4916bf215546Sopenharmony_ci            off = LLVMBuildTrunc(ctx->ac.builder, off, ctx->ac.i16, "");
4917bf215546Sopenharmony_ci         args.coords[i] = LLVMBuildAdd(ctx->ac.builder, args.coords[i], off, "");
4918bf215546Sopenharmony_ci      }
4919bf215546Sopenharmony_ci      args.offset = NULL;
4920bf215546Sopenharmony_ci   }
4921bf215546Sopenharmony_ci
4922bf215546Sopenharmony_ci   /* DMASK was repurposed for GATHER4. 4 components are always
4923bf215546Sopenharmony_ci    * returned and DMASK works like a swizzle - it selects
4924bf215546Sopenharmony_ci    * the component to fetch. The only valid DMASK values are
4925bf215546Sopenharmony_ci    * 1=red, 2=green, 4=blue, 8=alpha. (e.g. 1 returns
4926bf215546Sopenharmony_ci    * (red,red,red,red) etc.) The ISA document doesn't mention
4927bf215546Sopenharmony_ci    * this.
4928bf215546Sopenharmony_ci    */
4929bf215546Sopenharmony_ci   args.dmask = 0xf;
4930bf215546Sopenharmony_ci   if (instr->op == nir_texop_tg4) {
4931bf215546Sopenharmony_ci      if (instr->is_shadow)
4932bf215546Sopenharmony_ci         args.dmask = 1;
4933bf215546Sopenharmony_ci      else
4934bf215546Sopenharmony_ci         args.dmask = 1 << instr->component;
4935bf215546Sopenharmony_ci   }
4936bf215546Sopenharmony_ci
4937bf215546Sopenharmony_ci   if (instr->sampler_dim != GLSL_SAMPLER_DIM_BUF) {
4938bf215546Sopenharmony_ci      args.dim = ac_get_sampler_dim(ctx->ac.gfx_level, instr->sampler_dim, instr->is_array);
4939bf215546Sopenharmony_ci      args.unorm = instr->sampler_dim == GLSL_SAMPLER_DIM_RECT;
4940bf215546Sopenharmony_ci   }
4941bf215546Sopenharmony_ci
4942bf215546Sopenharmony_ci   /* Adjust the number of coordinates because we only need (x,y) for 2D
4943bf215546Sopenharmony_ci    * multisampled images and (x,y,layer) for 2D multisampled layered
4944bf215546Sopenharmony_ci    * images or for multisampled input attachments.
4945bf215546Sopenharmony_ci    */
4946bf215546Sopenharmony_ci   if (instr->op == nir_texop_fragment_mask_fetch_amd) {
4947bf215546Sopenharmony_ci      if (args.dim == ac_image_2dmsaa) {
4948bf215546Sopenharmony_ci         args.dim = ac_image_2d;
4949bf215546Sopenharmony_ci      } else {
4950bf215546Sopenharmony_ci         assert(args.dim == ac_image_2darraymsaa);
4951bf215546Sopenharmony_ci         args.dim = ac_image_2darray;
4952bf215546Sopenharmony_ci      }
4953bf215546Sopenharmony_ci   }
4954bf215546Sopenharmony_ci
4955bf215546Sopenharmony_ci   /* Set TRUNC_COORD=0 for textureGather(). */
4956bf215546Sopenharmony_ci   if (instr->op == nir_texop_tg4) {
4957bf215546Sopenharmony_ci      LLVMValueRef dword0 = LLVMBuildExtractElement(ctx->ac.builder, args.sampler, ctx->ac.i32_0, "");
4958bf215546Sopenharmony_ci      dword0 = LLVMBuildAnd(ctx->ac.builder, dword0, LLVMConstInt(ctx->ac.i32, C_008F30_TRUNC_COORD, 0), "");
4959bf215546Sopenharmony_ci      args.sampler = LLVMBuildInsertElement(ctx->ac.builder, args.sampler, dword0, ctx->ac.i32_0, "");
4960bf215546Sopenharmony_ci   }
4961bf215546Sopenharmony_ci
4962bf215546Sopenharmony_ci   assert(instr->dest.is_ssa);
4963bf215546Sopenharmony_ci   args.d16 = instr->dest.ssa.bit_size == 16;
4964bf215546Sopenharmony_ci   args.tfe = instr->is_sparse;
4965bf215546Sopenharmony_ci
4966bf215546Sopenharmony_ci   result = build_tex_intrinsic(ctx, instr, &args);
4967bf215546Sopenharmony_ci
4968bf215546Sopenharmony_ci   LLVMValueRef code = NULL;
4969bf215546Sopenharmony_ci   if (instr->is_sparse) {
4970bf215546Sopenharmony_ci      code = ac_llvm_extract_elem(&ctx->ac, result, 4);
4971bf215546Sopenharmony_ci      result = ac_trim_vector(&ctx->ac, result, 4);
4972bf215546Sopenharmony_ci   }
4973bf215546Sopenharmony_ci
4974bf215546Sopenharmony_ci   if (instr->op == nir_texop_query_levels)
4975bf215546Sopenharmony_ci      result =
4976bf215546Sopenharmony_ci         LLVMBuildExtractElement(ctx->ac.builder, result, LLVMConstInt(ctx->ac.i32, 3, false), "");
4977bf215546Sopenharmony_ci   else if (instr->is_shadow && instr->is_new_style_shadow && instr->op != nir_texop_txs &&
4978bf215546Sopenharmony_ci            instr->op != nir_texop_lod && instr->op != nir_texop_tg4)
4979bf215546Sopenharmony_ci      result = LLVMBuildExtractElement(ctx->ac.builder, result, ctx->ac.i32_0, "");
4980bf215546Sopenharmony_ci   else if (ctx->ac.gfx_level == GFX9 && instr->op == nir_texop_txs &&
4981bf215546Sopenharmony_ci              instr->sampler_dim == GLSL_SAMPLER_DIM_1D && instr->is_array) {
4982bf215546Sopenharmony_ci      LLVMValueRef two = LLVMConstInt(ctx->ac.i32, 2, false);
4983bf215546Sopenharmony_ci      LLVMValueRef layers = LLVMBuildExtractElement(ctx->ac.builder, result, two, "");
4984bf215546Sopenharmony_ci      result = LLVMBuildInsertElement(ctx->ac.builder, result, layers, ctx->ac.i32_1, "");
4985bf215546Sopenharmony_ci   } else if (instr->op == nir_texop_fragment_mask_fetch_amd) {
4986bf215546Sopenharmony_ci      /* Use 0x76543210 if the image doesn't have FMASK. */
4987bf215546Sopenharmony_ci      LLVMValueRef tmp = LLVMBuildBitCast(ctx->ac.builder, args.resource, ctx->ac.v8i32, "");
4988bf215546Sopenharmony_ci      tmp = LLVMBuildExtractElement(ctx->ac.builder, tmp, ctx->ac.i32_1, "");
4989bf215546Sopenharmony_ci      tmp = LLVMBuildICmp(ctx->ac.builder, LLVMIntNE, tmp, ctx->ac.i32_0, "");
4990bf215546Sopenharmony_ci      result = LLVMBuildSelect(ctx->ac.builder, tmp,
4991bf215546Sopenharmony_ci                               LLVMBuildExtractElement(ctx->ac.builder, result, ctx->ac.i32_0, ""),
4992bf215546Sopenharmony_ci                               LLVMConstInt(ctx->ac.i32, 0x76543210, false), "");
4993bf215546Sopenharmony_ci   } else if (nir_tex_instr_result_size(instr) != 4)
4994bf215546Sopenharmony_ci      result = ac_trim_vector(&ctx->ac, result, instr->dest.ssa.num_components);
4995bf215546Sopenharmony_ci
4996bf215546Sopenharmony_ci   if (instr->is_sparse)
4997bf215546Sopenharmony_ci      result = ac_build_concat(&ctx->ac, result, code);
4998bf215546Sopenharmony_ci
4999bf215546Sopenharmony_ciwrite_result:
5000bf215546Sopenharmony_ci   if (result) {
5001bf215546Sopenharmony_ci      assert(instr->dest.is_ssa);
5002bf215546Sopenharmony_ci      result = ac_to_integer(&ctx->ac, result);
5003bf215546Sopenharmony_ci
5004bf215546Sopenharmony_ci      for (int i = ARRAY_SIZE(wctx); --i >= 0;) {
5005bf215546Sopenharmony_ci         result = exit_waterfall(ctx, wctx + i, result);
5006bf215546Sopenharmony_ci      }
5007bf215546Sopenharmony_ci
5008bf215546Sopenharmony_ci      ctx->ssa_defs[instr->dest.ssa.index] = result;
5009bf215546Sopenharmony_ci   }
5010bf215546Sopenharmony_ci}
5011bf215546Sopenharmony_ci
5012bf215546Sopenharmony_cistatic void visit_phi(struct ac_nir_context *ctx, nir_phi_instr *instr)
5013bf215546Sopenharmony_ci{
5014bf215546Sopenharmony_ci   LLVMTypeRef type = get_def_type(ctx, &instr->dest.ssa);
5015bf215546Sopenharmony_ci   LLVMValueRef result = LLVMBuildPhi(ctx->ac.builder, type, "");
5016bf215546Sopenharmony_ci
5017bf215546Sopenharmony_ci   ctx->ssa_defs[instr->dest.ssa.index] = result;
5018bf215546Sopenharmony_ci   _mesa_hash_table_insert(ctx->phis, instr, result);
5019bf215546Sopenharmony_ci}
5020bf215546Sopenharmony_ci
5021bf215546Sopenharmony_cistatic void visit_post_phi(struct ac_nir_context *ctx, nir_phi_instr *instr, LLVMValueRef llvm_phi)
5022bf215546Sopenharmony_ci{
5023bf215546Sopenharmony_ci   nir_foreach_phi_src (src, instr) {
5024bf215546Sopenharmony_ci      LLVMBasicBlockRef block = get_block(ctx, src->pred);
5025bf215546Sopenharmony_ci      LLVMValueRef llvm_src = get_src(ctx, src->src);
5026bf215546Sopenharmony_ci
5027bf215546Sopenharmony_ci      LLVMAddIncoming(llvm_phi, &llvm_src, &block, 1);
5028bf215546Sopenharmony_ci   }
5029bf215546Sopenharmony_ci}
5030bf215546Sopenharmony_ci
5031bf215546Sopenharmony_cistatic void phi_post_pass(struct ac_nir_context *ctx)
5032bf215546Sopenharmony_ci{
5033bf215546Sopenharmony_ci   hash_table_foreach(ctx->phis, entry)
5034bf215546Sopenharmony_ci   {
5035bf215546Sopenharmony_ci      visit_post_phi(ctx, (nir_phi_instr *)entry->key, (LLVMValueRef)entry->data);
5036bf215546Sopenharmony_ci   }
5037bf215546Sopenharmony_ci}
5038bf215546Sopenharmony_ci
5039bf215546Sopenharmony_cistatic bool is_def_used_in_an_export(const nir_ssa_def *def)
5040bf215546Sopenharmony_ci{
5041bf215546Sopenharmony_ci   nir_foreach_use (use_src, def) {
5042bf215546Sopenharmony_ci      if (use_src->parent_instr->type == nir_instr_type_intrinsic) {
5043bf215546Sopenharmony_ci         nir_intrinsic_instr *instr = nir_instr_as_intrinsic(use_src->parent_instr);
5044bf215546Sopenharmony_ci         if (instr->intrinsic == nir_intrinsic_store_deref)
5045bf215546Sopenharmony_ci            return true;
5046bf215546Sopenharmony_ci      } else if (use_src->parent_instr->type == nir_instr_type_alu) {
5047bf215546Sopenharmony_ci         nir_alu_instr *instr = nir_instr_as_alu(use_src->parent_instr);
5048bf215546Sopenharmony_ci         if (instr->op == nir_op_vec4 && is_def_used_in_an_export(&instr->dest.dest.ssa)) {
5049bf215546Sopenharmony_ci            return true;
5050bf215546Sopenharmony_ci         }
5051bf215546Sopenharmony_ci      }
5052bf215546Sopenharmony_ci   }
5053bf215546Sopenharmony_ci   return false;
5054bf215546Sopenharmony_ci}
5055bf215546Sopenharmony_ci
5056bf215546Sopenharmony_cistatic void visit_ssa_undef(struct ac_nir_context *ctx, const nir_ssa_undef_instr *instr)
5057bf215546Sopenharmony_ci{
5058bf215546Sopenharmony_ci   unsigned num_components = instr->def.num_components;
5059bf215546Sopenharmony_ci   LLVMTypeRef type = LLVMIntTypeInContext(ctx->ac.context, instr->def.bit_size);
5060bf215546Sopenharmony_ci
5061bf215546Sopenharmony_ci   if (!ctx->abi->convert_undef_to_zero || is_def_used_in_an_export(&instr->def)) {
5062bf215546Sopenharmony_ci      LLVMValueRef undef;
5063bf215546Sopenharmony_ci
5064bf215546Sopenharmony_ci      if (num_components == 1)
5065bf215546Sopenharmony_ci         undef = LLVMGetUndef(type);
5066bf215546Sopenharmony_ci      else {
5067bf215546Sopenharmony_ci         undef = LLVMGetUndef(LLVMVectorType(type, num_components));
5068bf215546Sopenharmony_ci      }
5069bf215546Sopenharmony_ci      ctx->ssa_defs[instr->def.index] = undef;
5070bf215546Sopenharmony_ci   } else {
5071bf215546Sopenharmony_ci      LLVMValueRef zero = LLVMConstInt(type, 0, false);
5072bf215546Sopenharmony_ci      if (num_components > 1) {
5073bf215546Sopenharmony_ci         zero = ac_build_gather_values_extended(&ctx->ac, &zero, num_components, 0, false);
5074bf215546Sopenharmony_ci      }
5075bf215546Sopenharmony_ci      ctx->ssa_defs[instr->def.index] = zero;
5076bf215546Sopenharmony_ci   }
5077bf215546Sopenharmony_ci}
5078bf215546Sopenharmony_ci
5079bf215546Sopenharmony_cistatic void visit_jump(struct ac_llvm_context *ctx, const nir_jump_instr *instr)
5080bf215546Sopenharmony_ci{
5081bf215546Sopenharmony_ci   switch (instr->type) {
5082bf215546Sopenharmony_ci   case nir_jump_break:
5083bf215546Sopenharmony_ci      ac_build_break(ctx);
5084bf215546Sopenharmony_ci      break;
5085bf215546Sopenharmony_ci   case nir_jump_continue:
5086bf215546Sopenharmony_ci      ac_build_continue(ctx);
5087bf215546Sopenharmony_ci      break;
5088bf215546Sopenharmony_ci   default:
5089bf215546Sopenharmony_ci      fprintf(stderr, "Unknown NIR jump instr: ");
5090bf215546Sopenharmony_ci      nir_print_instr(&instr->instr, stderr);
5091bf215546Sopenharmony_ci      fprintf(stderr, "\n");
5092bf215546Sopenharmony_ci      abort();
5093bf215546Sopenharmony_ci   }
5094bf215546Sopenharmony_ci}
5095bf215546Sopenharmony_ci
5096bf215546Sopenharmony_cistatic LLVMTypeRef glsl_base_to_llvm_type(struct ac_llvm_context *ac, enum glsl_base_type type)
5097bf215546Sopenharmony_ci{
5098bf215546Sopenharmony_ci   switch (type) {
5099bf215546Sopenharmony_ci   case GLSL_TYPE_INT:
5100bf215546Sopenharmony_ci   case GLSL_TYPE_UINT:
5101bf215546Sopenharmony_ci   case GLSL_TYPE_BOOL:
5102bf215546Sopenharmony_ci   case GLSL_TYPE_SUBROUTINE:
5103bf215546Sopenharmony_ci      return ac->i32;
5104bf215546Sopenharmony_ci   case GLSL_TYPE_INT8:
5105bf215546Sopenharmony_ci   case GLSL_TYPE_UINT8:
5106bf215546Sopenharmony_ci      return ac->i8;
5107bf215546Sopenharmony_ci   case GLSL_TYPE_INT16:
5108bf215546Sopenharmony_ci   case GLSL_TYPE_UINT16:
5109bf215546Sopenharmony_ci      return ac->i16;
5110bf215546Sopenharmony_ci   case GLSL_TYPE_FLOAT:
5111bf215546Sopenharmony_ci      return ac->f32;
5112bf215546Sopenharmony_ci   case GLSL_TYPE_FLOAT16:
5113bf215546Sopenharmony_ci      return ac->f16;
5114bf215546Sopenharmony_ci   case GLSL_TYPE_INT64:
5115bf215546Sopenharmony_ci   case GLSL_TYPE_UINT64:
5116bf215546Sopenharmony_ci      return ac->i64;
5117bf215546Sopenharmony_ci   case GLSL_TYPE_DOUBLE:
5118bf215546Sopenharmony_ci      return ac->f64;
5119bf215546Sopenharmony_ci   default:
5120bf215546Sopenharmony_ci      unreachable("unknown GLSL type");
5121bf215546Sopenharmony_ci   }
5122bf215546Sopenharmony_ci}
5123bf215546Sopenharmony_ci
5124bf215546Sopenharmony_cistatic LLVMTypeRef glsl_to_llvm_type(struct ac_llvm_context *ac, const struct glsl_type *type)
5125bf215546Sopenharmony_ci{
5126bf215546Sopenharmony_ci   if (glsl_type_is_scalar(type)) {
5127bf215546Sopenharmony_ci      return glsl_base_to_llvm_type(ac, glsl_get_base_type(type));
5128bf215546Sopenharmony_ci   }
5129bf215546Sopenharmony_ci
5130bf215546Sopenharmony_ci   if (glsl_type_is_vector(type)) {
5131bf215546Sopenharmony_ci      return LLVMVectorType(glsl_base_to_llvm_type(ac, glsl_get_base_type(type)),
5132bf215546Sopenharmony_ci                            glsl_get_vector_elements(type));
5133bf215546Sopenharmony_ci   }
5134bf215546Sopenharmony_ci
5135bf215546Sopenharmony_ci   if (glsl_type_is_matrix(type)) {
5136bf215546Sopenharmony_ci      return LLVMArrayType(glsl_to_llvm_type(ac, glsl_get_column_type(type)),
5137bf215546Sopenharmony_ci                           glsl_get_matrix_columns(type));
5138bf215546Sopenharmony_ci   }
5139bf215546Sopenharmony_ci
5140bf215546Sopenharmony_ci   if (glsl_type_is_array(type)) {
5141bf215546Sopenharmony_ci      return LLVMArrayType(glsl_to_llvm_type(ac, glsl_get_array_element(type)),
5142bf215546Sopenharmony_ci                           glsl_get_length(type));
5143bf215546Sopenharmony_ci   }
5144bf215546Sopenharmony_ci
5145bf215546Sopenharmony_ci   assert(glsl_type_is_struct_or_ifc(type));
5146bf215546Sopenharmony_ci
5147bf215546Sopenharmony_ci   LLVMTypeRef *const member_types = alloca(glsl_get_length(type) * sizeof(LLVMTypeRef));
5148bf215546Sopenharmony_ci
5149bf215546Sopenharmony_ci   for (unsigned i = 0; i < glsl_get_length(type); i++) {
5150bf215546Sopenharmony_ci      member_types[i] = glsl_to_llvm_type(ac, glsl_get_struct_field(type, i));
5151bf215546Sopenharmony_ci   }
5152bf215546Sopenharmony_ci
5153bf215546Sopenharmony_ci   return LLVMStructTypeInContext(ac->context, member_types, glsl_get_length(type), false);
5154bf215546Sopenharmony_ci}
5155bf215546Sopenharmony_ci
5156bf215546Sopenharmony_cistatic void visit_deref(struct ac_nir_context *ctx, nir_deref_instr *instr)
5157bf215546Sopenharmony_ci{
5158bf215546Sopenharmony_ci   if (!nir_deref_mode_is_one_of(instr, nir_var_mem_shared | nir_var_mem_global))
5159bf215546Sopenharmony_ci      return;
5160bf215546Sopenharmony_ci
5161bf215546Sopenharmony_ci   LLVMValueRef result = NULL;
5162bf215546Sopenharmony_ci   switch (instr->deref_type) {
5163bf215546Sopenharmony_ci   case nir_deref_type_var: {
5164bf215546Sopenharmony_ci      struct hash_entry *entry = _mesa_hash_table_search(ctx->vars, instr->var);
5165bf215546Sopenharmony_ci      result = entry->data;
5166bf215546Sopenharmony_ci      break;
5167bf215546Sopenharmony_ci   }
5168bf215546Sopenharmony_ci   case nir_deref_type_struct:
5169bf215546Sopenharmony_ci      if (nir_deref_mode_is(instr, nir_var_mem_global)) {
5170bf215546Sopenharmony_ci         nir_deref_instr *parent = nir_deref_instr_parent(instr);
5171bf215546Sopenharmony_ci         uint64_t offset = glsl_get_struct_field_offset(parent->type, instr->strct.index);
5172bf215546Sopenharmony_ci         result = ac_build_gep_ptr(&ctx->ac, get_src(ctx, instr->parent),
5173bf215546Sopenharmony_ci                                   LLVMConstInt(ctx->ac.i32, offset, 0));
5174bf215546Sopenharmony_ci      } else {
5175bf215546Sopenharmony_ci         result = ac_build_gep0(&ctx->ac, get_src(ctx, instr->parent),
5176bf215546Sopenharmony_ci                                LLVMConstInt(ctx->ac.i32, instr->strct.index, 0));
5177bf215546Sopenharmony_ci      }
5178bf215546Sopenharmony_ci      break;
5179bf215546Sopenharmony_ci   case nir_deref_type_array:
5180bf215546Sopenharmony_ci      if (nir_deref_mode_is(instr, nir_var_mem_global)) {
5181bf215546Sopenharmony_ci         nir_deref_instr *parent = nir_deref_instr_parent(instr);
5182bf215546Sopenharmony_ci         unsigned stride = glsl_get_explicit_stride(parent->type);
5183bf215546Sopenharmony_ci
5184bf215546Sopenharmony_ci         if ((glsl_type_is_matrix(parent->type) && glsl_matrix_type_is_row_major(parent->type)) ||
5185bf215546Sopenharmony_ci             (glsl_type_is_vector(parent->type) && stride == 0))
5186bf215546Sopenharmony_ci            stride = type_scalar_size_bytes(parent->type);
5187bf215546Sopenharmony_ci
5188bf215546Sopenharmony_ci         assert(stride > 0);
5189bf215546Sopenharmony_ci         LLVMValueRef index = get_src(ctx, instr->arr.index);
5190bf215546Sopenharmony_ci         if (LLVMTypeOf(index) != ctx->ac.i64)
5191bf215546Sopenharmony_ci            index = LLVMBuildZExt(ctx->ac.builder, index, ctx->ac.i64, "");
5192bf215546Sopenharmony_ci
5193bf215546Sopenharmony_ci         LLVMValueRef offset =
5194bf215546Sopenharmony_ci            LLVMBuildMul(ctx->ac.builder, index, LLVMConstInt(ctx->ac.i64, stride, 0), "");
5195bf215546Sopenharmony_ci
5196bf215546Sopenharmony_ci         result = ac_build_gep_ptr(&ctx->ac, get_src(ctx, instr->parent), offset);
5197bf215546Sopenharmony_ci      } else {
5198bf215546Sopenharmony_ci         result =
5199bf215546Sopenharmony_ci            ac_build_gep0(&ctx->ac, get_src(ctx, instr->parent), get_src(ctx, instr->arr.index));
5200bf215546Sopenharmony_ci      }
5201bf215546Sopenharmony_ci      break;
5202bf215546Sopenharmony_ci   case nir_deref_type_ptr_as_array:
5203bf215546Sopenharmony_ci      if (nir_deref_mode_is(instr, nir_var_mem_global)) {
5204bf215546Sopenharmony_ci         unsigned stride = nir_deref_instr_array_stride(instr);
5205bf215546Sopenharmony_ci
5206bf215546Sopenharmony_ci         LLVMValueRef index = get_src(ctx, instr->arr.index);
5207bf215546Sopenharmony_ci         if (LLVMTypeOf(index) != ctx->ac.i64)
5208bf215546Sopenharmony_ci            index = LLVMBuildZExt(ctx->ac.builder, index, ctx->ac.i64, "");
5209bf215546Sopenharmony_ci
5210bf215546Sopenharmony_ci         LLVMValueRef offset =
5211bf215546Sopenharmony_ci            LLVMBuildMul(ctx->ac.builder, index, LLVMConstInt(ctx->ac.i64, stride, 0), "");
5212bf215546Sopenharmony_ci
5213bf215546Sopenharmony_ci         result = ac_build_gep_ptr(&ctx->ac, get_src(ctx, instr->parent), offset);
5214bf215546Sopenharmony_ci      } else {
5215bf215546Sopenharmony_ci         result =
5216bf215546Sopenharmony_ci            ac_build_gep_ptr(&ctx->ac, get_src(ctx, instr->parent), get_src(ctx, instr->arr.index));
5217bf215546Sopenharmony_ci      }
5218bf215546Sopenharmony_ci      break;
5219bf215546Sopenharmony_ci   case nir_deref_type_cast: {
5220bf215546Sopenharmony_ci      result = get_src(ctx, instr->parent);
5221bf215546Sopenharmony_ci
5222bf215546Sopenharmony_ci      /* We can't use the structs from LLVM because the shader
5223bf215546Sopenharmony_ci       * specifies its own offsets. */
5224bf215546Sopenharmony_ci      LLVMTypeRef pointee_type = ctx->ac.i8;
5225bf215546Sopenharmony_ci      if (nir_deref_mode_is(instr, nir_var_mem_shared))
5226bf215546Sopenharmony_ci         pointee_type = glsl_to_llvm_type(&ctx->ac, instr->type);
5227bf215546Sopenharmony_ci
5228bf215546Sopenharmony_ci      unsigned address_space;
5229bf215546Sopenharmony_ci
5230bf215546Sopenharmony_ci      switch (instr->modes) {
5231bf215546Sopenharmony_ci      case nir_var_mem_shared:
5232bf215546Sopenharmony_ci         address_space = AC_ADDR_SPACE_LDS;
5233bf215546Sopenharmony_ci         break;
5234bf215546Sopenharmony_ci      case nir_var_mem_global:
5235bf215546Sopenharmony_ci         address_space = AC_ADDR_SPACE_GLOBAL;
5236bf215546Sopenharmony_ci         break;
5237bf215546Sopenharmony_ci      default:
5238bf215546Sopenharmony_ci         unreachable("Unhandled address space");
5239bf215546Sopenharmony_ci      }
5240bf215546Sopenharmony_ci
5241bf215546Sopenharmony_ci      LLVMTypeRef type = LLVMPointerType(pointee_type, address_space);
5242bf215546Sopenharmony_ci
5243bf215546Sopenharmony_ci      if (LLVMTypeOf(result) != type) {
5244bf215546Sopenharmony_ci         if (LLVMGetTypeKind(LLVMTypeOf(result)) == LLVMVectorTypeKind) {
5245bf215546Sopenharmony_ci            result = LLVMBuildBitCast(ctx->ac.builder, result, type, "");
5246bf215546Sopenharmony_ci         } else {
5247bf215546Sopenharmony_ci            result = LLVMBuildIntToPtr(ctx->ac.builder, result, type, "");
5248bf215546Sopenharmony_ci         }
5249bf215546Sopenharmony_ci      }
5250bf215546Sopenharmony_ci      break;
5251bf215546Sopenharmony_ci   }
5252bf215546Sopenharmony_ci   default:
5253bf215546Sopenharmony_ci      unreachable("Unhandled deref_instr deref type");
5254bf215546Sopenharmony_ci   }
5255bf215546Sopenharmony_ci
5256bf215546Sopenharmony_ci   ctx->ssa_defs[instr->dest.ssa.index] = result;
5257bf215546Sopenharmony_ci}
5258bf215546Sopenharmony_ci
5259bf215546Sopenharmony_cistatic void visit_cf_list(struct ac_nir_context *ctx, struct exec_list *list);
5260bf215546Sopenharmony_ci
5261bf215546Sopenharmony_cistatic void visit_block(struct ac_nir_context *ctx, nir_block *block)
5262bf215546Sopenharmony_ci{
5263bf215546Sopenharmony_ci   LLVMBasicBlockRef blockref = LLVMGetInsertBlock(ctx->ac.builder);
5264bf215546Sopenharmony_ci   LLVMValueRef first = LLVMGetFirstInstruction(blockref);
5265bf215546Sopenharmony_ci   if (first) {
5266bf215546Sopenharmony_ci      /* ac_branch_exited() might have already inserted non-phis */
5267bf215546Sopenharmony_ci      LLVMPositionBuilderBefore(ctx->ac.builder, LLVMGetFirstInstruction(blockref));
5268bf215546Sopenharmony_ci   }
5269bf215546Sopenharmony_ci
5270bf215546Sopenharmony_ci   nir_foreach_instr(instr, block) {
5271bf215546Sopenharmony_ci      if (instr->type != nir_instr_type_phi)
5272bf215546Sopenharmony_ci         break;
5273bf215546Sopenharmony_ci      visit_phi(ctx, nir_instr_as_phi(instr));
5274bf215546Sopenharmony_ci   }
5275bf215546Sopenharmony_ci
5276bf215546Sopenharmony_ci   LLVMPositionBuilderAtEnd(ctx->ac.builder, blockref);
5277bf215546Sopenharmony_ci
5278bf215546Sopenharmony_ci   nir_foreach_instr (instr, block) {
5279bf215546Sopenharmony_ci      switch (instr->type) {
5280bf215546Sopenharmony_ci      case nir_instr_type_alu:
5281bf215546Sopenharmony_ci         visit_alu(ctx, nir_instr_as_alu(instr));
5282bf215546Sopenharmony_ci         break;
5283bf215546Sopenharmony_ci      case nir_instr_type_load_const:
5284bf215546Sopenharmony_ci         visit_load_const(ctx, nir_instr_as_load_const(instr));
5285bf215546Sopenharmony_ci         break;
5286bf215546Sopenharmony_ci      case nir_instr_type_intrinsic:
5287bf215546Sopenharmony_ci         visit_intrinsic(ctx, nir_instr_as_intrinsic(instr));
5288bf215546Sopenharmony_ci         break;
5289bf215546Sopenharmony_ci      case nir_instr_type_tex:
5290bf215546Sopenharmony_ci         visit_tex(ctx, nir_instr_as_tex(instr));
5291bf215546Sopenharmony_ci         break;
5292bf215546Sopenharmony_ci      case nir_instr_type_phi:
5293bf215546Sopenharmony_ci         break;
5294bf215546Sopenharmony_ci      case nir_instr_type_ssa_undef:
5295bf215546Sopenharmony_ci         visit_ssa_undef(ctx, nir_instr_as_ssa_undef(instr));
5296bf215546Sopenharmony_ci         break;
5297bf215546Sopenharmony_ci      case nir_instr_type_jump:
5298bf215546Sopenharmony_ci         visit_jump(&ctx->ac, nir_instr_as_jump(instr));
5299bf215546Sopenharmony_ci         break;
5300bf215546Sopenharmony_ci      case nir_instr_type_deref:
5301bf215546Sopenharmony_ci         visit_deref(ctx, nir_instr_as_deref(instr));
5302bf215546Sopenharmony_ci         break;
5303bf215546Sopenharmony_ci      default:
5304bf215546Sopenharmony_ci         fprintf(stderr, "Unknown NIR instr type: ");
5305bf215546Sopenharmony_ci         nir_print_instr(instr, stderr);
5306bf215546Sopenharmony_ci         fprintf(stderr, "\n");
5307bf215546Sopenharmony_ci         abort();
5308bf215546Sopenharmony_ci      }
5309bf215546Sopenharmony_ci   }
5310bf215546Sopenharmony_ci
5311bf215546Sopenharmony_ci   _mesa_hash_table_insert(ctx->defs, block, LLVMGetInsertBlock(ctx->ac.builder));
5312bf215546Sopenharmony_ci}
5313bf215546Sopenharmony_ci
5314bf215546Sopenharmony_cistatic void visit_if(struct ac_nir_context *ctx, nir_if *if_stmt)
5315bf215546Sopenharmony_ci{
5316bf215546Sopenharmony_ci   LLVMValueRef value = get_src(ctx, if_stmt->condition);
5317bf215546Sopenharmony_ci
5318bf215546Sopenharmony_ci   nir_block *then_block = (nir_block *)exec_list_get_head(&if_stmt->then_list);
5319bf215546Sopenharmony_ci
5320bf215546Sopenharmony_ci   ac_build_ifcc(&ctx->ac, value, then_block->index);
5321bf215546Sopenharmony_ci
5322bf215546Sopenharmony_ci   visit_cf_list(ctx, &if_stmt->then_list);
5323bf215546Sopenharmony_ci
5324bf215546Sopenharmony_ci   if (!exec_list_is_empty(&if_stmt->else_list)) {
5325bf215546Sopenharmony_ci      nir_block *else_block = (nir_block *)exec_list_get_head(&if_stmt->else_list);
5326bf215546Sopenharmony_ci
5327bf215546Sopenharmony_ci      ac_build_else(&ctx->ac, else_block->index);
5328bf215546Sopenharmony_ci      visit_cf_list(ctx, &if_stmt->else_list);
5329bf215546Sopenharmony_ci   }
5330bf215546Sopenharmony_ci
5331bf215546Sopenharmony_ci   ac_build_endif(&ctx->ac, then_block->index);
5332bf215546Sopenharmony_ci}
5333bf215546Sopenharmony_ci
5334bf215546Sopenharmony_cistatic void visit_loop(struct ac_nir_context *ctx, nir_loop *loop)
5335bf215546Sopenharmony_ci{
5336bf215546Sopenharmony_ci   nir_block *first_loop_block = (nir_block *)exec_list_get_head(&loop->body);
5337bf215546Sopenharmony_ci
5338bf215546Sopenharmony_ci   ac_build_bgnloop(&ctx->ac, first_loop_block->index);
5339bf215546Sopenharmony_ci
5340bf215546Sopenharmony_ci   visit_cf_list(ctx, &loop->body);
5341bf215546Sopenharmony_ci
5342bf215546Sopenharmony_ci   ac_build_endloop(&ctx->ac, first_loop_block->index);
5343bf215546Sopenharmony_ci}
5344bf215546Sopenharmony_ci
5345bf215546Sopenharmony_cistatic void visit_cf_list(struct ac_nir_context *ctx, struct exec_list *list)
5346bf215546Sopenharmony_ci{
5347bf215546Sopenharmony_ci   foreach_list_typed(nir_cf_node, node, node, list)
5348bf215546Sopenharmony_ci   {
5349bf215546Sopenharmony_ci      switch (node->type) {
5350bf215546Sopenharmony_ci      case nir_cf_node_block:
5351bf215546Sopenharmony_ci         visit_block(ctx, nir_cf_node_as_block(node));
5352bf215546Sopenharmony_ci         break;
5353bf215546Sopenharmony_ci
5354bf215546Sopenharmony_ci      case nir_cf_node_if:
5355bf215546Sopenharmony_ci         visit_if(ctx, nir_cf_node_as_if(node));
5356bf215546Sopenharmony_ci         break;
5357bf215546Sopenharmony_ci
5358bf215546Sopenharmony_ci      case nir_cf_node_loop:
5359bf215546Sopenharmony_ci         visit_loop(ctx, nir_cf_node_as_loop(node));
5360bf215546Sopenharmony_ci         break;
5361bf215546Sopenharmony_ci
5362bf215546Sopenharmony_ci      default:
5363bf215546Sopenharmony_ci         assert(0);
5364bf215546Sopenharmony_ci      }
5365bf215546Sopenharmony_ci   }
5366bf215546Sopenharmony_ci}
5367bf215546Sopenharmony_ci
5368bf215546Sopenharmony_civoid ac_handle_shader_output_decl(struct ac_llvm_context *ctx, struct ac_shader_abi *abi,
5369bf215546Sopenharmony_ci                                  struct nir_shader *nir, struct nir_variable *variable,
5370bf215546Sopenharmony_ci                                  gl_shader_stage stage)
5371bf215546Sopenharmony_ci{
5372bf215546Sopenharmony_ci   unsigned output_loc = variable->data.driver_location;
5373bf215546Sopenharmony_ci   unsigned attrib_count = glsl_count_attribute_slots(variable->type, false);
5374bf215546Sopenharmony_ci
5375bf215546Sopenharmony_ci   /* tess ctrl has it's own load/store paths for outputs */
5376bf215546Sopenharmony_ci   if (stage == MESA_SHADER_TESS_CTRL)
5377bf215546Sopenharmony_ci      return;
5378bf215546Sopenharmony_ci
5379bf215546Sopenharmony_ci   if (stage == MESA_SHADER_VERTEX || stage == MESA_SHADER_TESS_EVAL ||
5380bf215546Sopenharmony_ci       stage == MESA_SHADER_GEOMETRY) {
5381bf215546Sopenharmony_ci      int idx = variable->data.location + variable->data.index;
5382bf215546Sopenharmony_ci      if (idx == VARYING_SLOT_CLIP_DIST0) {
5383bf215546Sopenharmony_ci         int length = nir->info.clip_distance_array_size + nir->info.cull_distance_array_size;
5384bf215546Sopenharmony_ci
5385bf215546Sopenharmony_ci         if (length > 4)
5386bf215546Sopenharmony_ci            attrib_count = 2;
5387bf215546Sopenharmony_ci         else
5388bf215546Sopenharmony_ci            attrib_count = 1;
5389bf215546Sopenharmony_ci      }
5390bf215546Sopenharmony_ci   }
5391bf215546Sopenharmony_ci
5392bf215546Sopenharmony_ci   bool is_16bit = glsl_type_is_16bit(glsl_without_array(variable->type));
5393bf215546Sopenharmony_ci   LLVMTypeRef type = is_16bit ? ctx->f16 : ctx->f32;
5394bf215546Sopenharmony_ci   for (unsigned i = 0; i < attrib_count; ++i) {
5395bf215546Sopenharmony_ci      for (unsigned chan = 0; chan < 4; chan++) {
5396bf215546Sopenharmony_ci         int idx = ac_llvm_reg_index_soa(output_loc + i, chan);
5397bf215546Sopenharmony_ci         abi->outputs[idx] = ac_build_alloca_undef(ctx, type, "");
5398bf215546Sopenharmony_ci         abi->is_16bit[idx] = is_16bit;
5399bf215546Sopenharmony_ci      }
5400bf215546Sopenharmony_ci   }
5401bf215546Sopenharmony_ci}
5402bf215546Sopenharmony_ci
5403bf215546Sopenharmony_cistatic void setup_scratch(struct ac_nir_context *ctx, struct nir_shader *shader)
5404bf215546Sopenharmony_ci{
5405bf215546Sopenharmony_ci   if (shader->scratch_size == 0)
5406bf215546Sopenharmony_ci      return;
5407bf215546Sopenharmony_ci
5408bf215546Sopenharmony_ci   ctx->scratch =
5409bf215546Sopenharmony_ci      ac_build_alloca_undef(&ctx->ac, LLVMArrayType(ctx->ac.i8, shader->scratch_size), "scratch");
5410bf215546Sopenharmony_ci}
5411bf215546Sopenharmony_ci
5412bf215546Sopenharmony_cistatic void setup_constant_data(struct ac_nir_context *ctx, struct nir_shader *shader)
5413bf215546Sopenharmony_ci{
5414bf215546Sopenharmony_ci   if (!shader->constant_data)
5415bf215546Sopenharmony_ci      return;
5416bf215546Sopenharmony_ci
5417bf215546Sopenharmony_ci   LLVMValueRef data = LLVMConstStringInContext(ctx->ac.context, shader->constant_data,
5418bf215546Sopenharmony_ci                                                shader->constant_data_size, true);
5419bf215546Sopenharmony_ci   LLVMTypeRef type = LLVMArrayType(ctx->ac.i8, shader->constant_data_size);
5420bf215546Sopenharmony_ci   LLVMValueRef global =
5421bf215546Sopenharmony_ci      LLVMAddGlobalInAddressSpace(ctx->ac.module, type, "const_data", AC_ADDR_SPACE_CONST);
5422bf215546Sopenharmony_ci
5423bf215546Sopenharmony_ci   LLVMSetInitializer(global, data);
5424bf215546Sopenharmony_ci   LLVMSetGlobalConstant(global, true);
5425bf215546Sopenharmony_ci   LLVMSetVisibility(global, LLVMHiddenVisibility);
5426bf215546Sopenharmony_ci   ctx->constant_data = global;
5427bf215546Sopenharmony_ci}
5428bf215546Sopenharmony_ci
5429bf215546Sopenharmony_cistatic void setup_shared(struct ac_nir_context *ctx, struct nir_shader *nir)
5430bf215546Sopenharmony_ci{
5431bf215546Sopenharmony_ci   if (ctx->ac.lds)
5432bf215546Sopenharmony_ci      return;
5433bf215546Sopenharmony_ci
5434bf215546Sopenharmony_ci   LLVMTypeRef type = LLVMArrayType(ctx->ac.i8, nir->info.shared_size);
5435bf215546Sopenharmony_ci
5436bf215546Sopenharmony_ci   LLVMValueRef lds =
5437bf215546Sopenharmony_ci      LLVMAddGlobalInAddressSpace(ctx->ac.module, type, "compute_lds", AC_ADDR_SPACE_LDS);
5438bf215546Sopenharmony_ci   LLVMSetAlignment(lds, 64 * 1024);
5439bf215546Sopenharmony_ci
5440bf215546Sopenharmony_ci   ctx->ac.lds =
5441bf215546Sopenharmony_ci      LLVMBuildBitCast(ctx->ac.builder, lds, LLVMPointerType(ctx->ac.i8, AC_ADDR_SPACE_LDS), "");
5442bf215546Sopenharmony_ci}
5443bf215546Sopenharmony_ci
5444bf215546Sopenharmony_cistatic void setup_gds(struct ac_nir_context *ctx, nir_function_impl *impl)
5445bf215546Sopenharmony_ci{
5446bf215546Sopenharmony_ci   bool has_gds_atomic = false;
5447bf215546Sopenharmony_ci
5448bf215546Sopenharmony_ci   if (ctx->ac.gfx_level >= GFX10 &&
5449bf215546Sopenharmony_ci       (ctx->stage == MESA_SHADER_VERTEX ||
5450bf215546Sopenharmony_ci        ctx->stage == MESA_SHADER_TESS_EVAL ||
5451bf215546Sopenharmony_ci        ctx->stage == MESA_SHADER_GEOMETRY)) {
5452bf215546Sopenharmony_ci
5453bf215546Sopenharmony_ci      nir_foreach_block(block, impl) {
5454bf215546Sopenharmony_ci         nir_foreach_instr(instr, block) {
5455bf215546Sopenharmony_ci            if (instr->type != nir_instr_type_intrinsic)
5456bf215546Sopenharmony_ci               continue;
5457bf215546Sopenharmony_ci
5458bf215546Sopenharmony_ci            nir_intrinsic_instr *intrin = nir_instr_as_intrinsic(instr);
5459bf215546Sopenharmony_ci            has_gds_atomic |= intrin->intrinsic == nir_intrinsic_gds_atomic_add_amd;
5460bf215546Sopenharmony_ci         }
5461bf215546Sopenharmony_ci      }
5462bf215546Sopenharmony_ci   }
5463bf215546Sopenharmony_ci
5464bf215546Sopenharmony_ci   unsigned gds_size = has_gds_atomic ? 0x100 : 0;
5465bf215546Sopenharmony_ci
5466bf215546Sopenharmony_ci   if (gds_size)
5467bf215546Sopenharmony_ci      ac_llvm_add_target_dep_function_attr(ctx->main_function, "amdgpu-gds-size", gds_size);
5468bf215546Sopenharmony_ci}
5469bf215546Sopenharmony_ci
5470bf215546Sopenharmony_civoid ac_nir_translate(struct ac_llvm_context *ac, struct ac_shader_abi *abi,
5471bf215546Sopenharmony_ci                      const struct ac_shader_args *args, struct nir_shader *nir)
5472bf215546Sopenharmony_ci{
5473bf215546Sopenharmony_ci   struct ac_nir_context ctx = {0};
5474bf215546Sopenharmony_ci   struct nir_function *func;
5475bf215546Sopenharmony_ci
5476bf215546Sopenharmony_ci   ctx.ac = *ac;
5477bf215546Sopenharmony_ci   ctx.abi = abi;
5478bf215546Sopenharmony_ci   ctx.args = args;
5479bf215546Sopenharmony_ci
5480bf215546Sopenharmony_ci   ctx.stage = nir->info.stage;
5481bf215546Sopenharmony_ci   ctx.info = &nir->info;
5482bf215546Sopenharmony_ci
5483bf215546Sopenharmony_ci   ctx.main_function = LLVMGetBasicBlockParent(LLVMGetInsertBlock(ctx.ac.builder));
5484bf215546Sopenharmony_ci
5485bf215546Sopenharmony_ci   /* TODO: remove this after RADV switches to lowered IO */
5486bf215546Sopenharmony_ci   if (!nir->info.io_lowered) {
5487bf215546Sopenharmony_ci      nir_foreach_shader_out_variable(variable, nir)
5488bf215546Sopenharmony_ci      {
5489bf215546Sopenharmony_ci         ac_handle_shader_output_decl(&ctx.ac, ctx.abi, nir, variable, ctx.stage);
5490bf215546Sopenharmony_ci      }
5491bf215546Sopenharmony_ci   }
5492bf215546Sopenharmony_ci
5493bf215546Sopenharmony_ci   ctx.defs = _mesa_hash_table_create(NULL, _mesa_hash_pointer, _mesa_key_pointer_equal);
5494bf215546Sopenharmony_ci   ctx.phis = _mesa_hash_table_create(NULL, _mesa_hash_pointer, _mesa_key_pointer_equal);
5495bf215546Sopenharmony_ci   ctx.vars = _mesa_hash_table_create(NULL, _mesa_hash_pointer, _mesa_key_pointer_equal);
5496bf215546Sopenharmony_ci
5497bf215546Sopenharmony_ci   if (ctx.abi->kill_ps_if_inf_interp)
5498bf215546Sopenharmony_ci      ctx.verified_interp =
5499bf215546Sopenharmony_ci         _mesa_hash_table_create(NULL, _mesa_hash_pointer, _mesa_key_pointer_equal);
5500bf215546Sopenharmony_ci
5501bf215546Sopenharmony_ci   func = (struct nir_function *)exec_list_get_head(&nir->functions);
5502bf215546Sopenharmony_ci
5503bf215546Sopenharmony_ci   nir_index_ssa_defs(func->impl);
5504bf215546Sopenharmony_ci   ctx.ssa_defs = calloc(func->impl->ssa_alloc, sizeof(LLVMValueRef));
5505bf215546Sopenharmony_ci
5506bf215546Sopenharmony_ci   setup_scratch(&ctx, nir);
5507bf215546Sopenharmony_ci   setup_constant_data(&ctx, nir);
5508bf215546Sopenharmony_ci   setup_gds(&ctx, func->impl);
5509bf215546Sopenharmony_ci
5510bf215546Sopenharmony_ci   if (gl_shader_stage_is_compute(nir->info.stage))
5511bf215546Sopenharmony_ci      setup_shared(&ctx, nir);
5512bf215546Sopenharmony_ci
5513bf215546Sopenharmony_ci   if (nir->info.stage == MESA_SHADER_FRAGMENT && nir->info.fs.uses_demote &&
5514bf215546Sopenharmony_ci       LLVM_VERSION_MAJOR < 13) {
5515bf215546Sopenharmony_ci      /* true = don't kill. */
5516bf215546Sopenharmony_ci      ctx.ac.postponed_kill = ac_build_alloca_init(&ctx.ac, ctx.ac.i1true, "");
5517bf215546Sopenharmony_ci   }
5518bf215546Sopenharmony_ci
5519bf215546Sopenharmony_ci   visit_cf_list(&ctx, &func->impl->body);
5520bf215546Sopenharmony_ci   phi_post_pass(&ctx);
5521bf215546Sopenharmony_ci
5522bf215546Sopenharmony_ci   if (ctx.ac.postponed_kill)
5523bf215546Sopenharmony_ci      ac_build_kill_if_false(&ctx.ac, LLVMBuildLoad2(ctx.ac.builder, ctx.ac.i1, ctx.ac.postponed_kill, ""));
5524bf215546Sopenharmony_ci
5525bf215546Sopenharmony_ci   free(ctx.ssa_defs);
5526bf215546Sopenharmony_ci   ralloc_free(ctx.defs);
5527bf215546Sopenharmony_ci   ralloc_free(ctx.phis);
5528bf215546Sopenharmony_ci   ralloc_free(ctx.vars);
5529bf215546Sopenharmony_ci   if (ctx.abi->kill_ps_if_inf_interp)
5530bf215546Sopenharmony_ci      ralloc_free(ctx.verified_interp);
5531bf215546Sopenharmony_ci}
5532