3a3aa72988e98c280da6c0e29e2c48f0d9933f89
[mesa.git] / src / amd / common / ac_nir_to_llvm.c
1 /*
2 * Copyright © 2016 Bas Nieuwenhuizen
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21 * IN THE SOFTWARE.
22 */
23
24 #include "ac_nir_to_llvm.h"
25 #include "ac_llvm_build.h"
26 #include "ac_llvm_util.h"
27 #include "ac_binary.h"
28 #include "sid.h"
29 #include "nir/nir.h"
30 #include "util/bitscan.h"
31 #include "util/u_math.h"
32 #include "ac_shader_abi.h"
33 #include "ac_shader_util.h"
34
35 struct ac_nir_context {
36 struct ac_llvm_context ac;
37 struct ac_shader_abi *abi;
38
39 gl_shader_stage stage;
40
41 LLVMValueRef *ssa_defs;
42
43 struct hash_table *defs;
44 struct hash_table *phis;
45 struct hash_table *vars;
46
47 LLVMValueRef main_function;
48 LLVMBasicBlockRef continue_block;
49 LLVMBasicBlockRef break_block;
50
51 int num_locals;
52 LLVMValueRef *locals;
53 };
54
55 static LLVMValueRef get_sampler_desc(struct ac_nir_context *ctx,
56 const nir_deref_var *deref,
57 enum ac_descriptor_type desc_type,
58 const nir_tex_instr *instr,
59 bool image, bool write);
60
61 static void
62 build_store_values_extended(struct ac_llvm_context *ac,
63 LLVMValueRef *values,
64 unsigned value_count,
65 unsigned value_stride,
66 LLVMValueRef vec)
67 {
68 LLVMBuilderRef builder = ac->builder;
69 unsigned i;
70
71 for (i = 0; i < value_count; i++) {
72 LLVMValueRef ptr = values[i * value_stride];
73 LLVMValueRef index = LLVMConstInt(ac->i32, i, false);
74 LLVMValueRef value = LLVMBuildExtractElement(builder, vec, index, "");
75 LLVMBuildStore(builder, value, ptr);
76 }
77 }
78
79 static LLVMTypeRef get_def_type(struct ac_nir_context *ctx,
80 const nir_ssa_def *def)
81 {
82 LLVMTypeRef type = LLVMIntTypeInContext(ctx->ac.context, def->bit_size);
83 if (def->num_components > 1) {
84 type = LLVMVectorType(type, def->num_components);
85 }
86 return type;
87 }
88
89 static LLVMValueRef get_src(struct ac_nir_context *nir, nir_src src)
90 {
91 assert(src.is_ssa);
92 return nir->ssa_defs[src.ssa->index];
93 }
94
95 static LLVMValueRef
96 get_memory_ptr(struct ac_nir_context *ctx, nir_src src)
97 {
98 LLVMValueRef ptr = get_src(ctx, src);
99 ptr = LLVMBuildGEP(ctx->ac.builder, ctx->ac.lds, &ptr, 1, "");
100 int addr_space = LLVMGetPointerAddressSpace(LLVMTypeOf(ptr));
101
102 return LLVMBuildBitCast(ctx->ac.builder, ptr,
103 LLVMPointerType(ctx->ac.i32, addr_space), "");
104 }
105
106 static LLVMBasicBlockRef get_block(struct ac_nir_context *nir,
107 const struct nir_block *b)
108 {
109 struct hash_entry *entry = _mesa_hash_table_search(nir->defs, b);
110 return (LLVMBasicBlockRef)entry->data;
111 }
112
113 static LLVMValueRef get_alu_src(struct ac_nir_context *ctx,
114 nir_alu_src src,
115 unsigned num_components)
116 {
117 LLVMValueRef value = get_src(ctx, src.src);
118 bool need_swizzle = false;
119
120 assert(value);
121 unsigned src_components = ac_get_llvm_num_components(value);
122 for (unsigned i = 0; i < num_components; ++i) {
123 assert(src.swizzle[i] < src_components);
124 if (src.swizzle[i] != i)
125 need_swizzle = true;
126 }
127
128 if (need_swizzle || num_components != src_components) {
129 LLVMValueRef masks[] = {
130 LLVMConstInt(ctx->ac.i32, src.swizzle[0], false),
131 LLVMConstInt(ctx->ac.i32, src.swizzle[1], false),
132 LLVMConstInt(ctx->ac.i32, src.swizzle[2], false),
133 LLVMConstInt(ctx->ac.i32, src.swizzle[3], false)};
134
135 if (src_components > 1 && num_components == 1) {
136 value = LLVMBuildExtractElement(ctx->ac.builder, value,
137 masks[0], "");
138 } else if (src_components == 1 && num_components > 1) {
139 LLVMValueRef values[] = {value, value, value, value};
140 value = ac_build_gather_values(&ctx->ac, values, num_components);
141 } else {
142 LLVMValueRef swizzle = LLVMConstVector(masks, num_components);
143 value = LLVMBuildShuffleVector(ctx->ac.builder, value, value,
144 swizzle, "");
145 }
146 }
147 assert(!src.negate);
148 assert(!src.abs);
149 return value;
150 }
151
152 static LLVMValueRef emit_int_cmp(struct ac_llvm_context *ctx,
153 LLVMIntPredicate pred, LLVMValueRef src0,
154 LLVMValueRef src1)
155 {
156 LLVMValueRef result = LLVMBuildICmp(ctx->builder, pred, src0, src1, "");
157 return LLVMBuildSelect(ctx->builder, result,
158 LLVMConstInt(ctx->i32, 0xFFFFFFFF, false),
159 ctx->i32_0, "");
160 }
161
162 static LLVMValueRef emit_float_cmp(struct ac_llvm_context *ctx,
163 LLVMRealPredicate pred, LLVMValueRef src0,
164 LLVMValueRef src1)
165 {
166 LLVMValueRef result;
167 src0 = ac_to_float(ctx, src0);
168 src1 = ac_to_float(ctx, src1);
169 result = LLVMBuildFCmp(ctx->builder, pred, src0, src1, "");
170 return LLVMBuildSelect(ctx->builder, result,
171 LLVMConstInt(ctx->i32, 0xFFFFFFFF, false),
172 ctx->i32_0, "");
173 }
174
175 static LLVMValueRef emit_intrin_1f_param(struct ac_llvm_context *ctx,
176 const char *intrin,
177 LLVMTypeRef result_type,
178 LLVMValueRef src0)
179 {
180 char name[64];
181 LLVMValueRef params[] = {
182 ac_to_float(ctx, src0),
183 };
184
185 MAYBE_UNUSED const int length = snprintf(name, sizeof(name), "%s.f%d", intrin,
186 ac_get_elem_bits(ctx, result_type));
187 assert(length < sizeof(name));
188 return ac_build_intrinsic(ctx, name, result_type, params, 1, AC_FUNC_ATTR_READNONE);
189 }
190
191 static LLVMValueRef emit_intrin_2f_param(struct ac_llvm_context *ctx,
192 const char *intrin,
193 LLVMTypeRef result_type,
194 LLVMValueRef src0, LLVMValueRef src1)
195 {
196 char name[64];
197 LLVMValueRef params[] = {
198 ac_to_float(ctx, src0),
199 ac_to_float(ctx, src1),
200 };
201
202 MAYBE_UNUSED const int length = snprintf(name, sizeof(name), "%s.f%d", intrin,
203 ac_get_elem_bits(ctx, result_type));
204 assert(length < sizeof(name));
205 return ac_build_intrinsic(ctx, name, result_type, params, 2, AC_FUNC_ATTR_READNONE);
206 }
207
208 static LLVMValueRef emit_intrin_3f_param(struct ac_llvm_context *ctx,
209 const char *intrin,
210 LLVMTypeRef result_type,
211 LLVMValueRef src0, LLVMValueRef src1, LLVMValueRef src2)
212 {
213 char name[64];
214 LLVMValueRef params[] = {
215 ac_to_float(ctx, src0),
216 ac_to_float(ctx, src1),
217 ac_to_float(ctx, src2),
218 };
219
220 MAYBE_UNUSED const int length = snprintf(name, sizeof(name), "%s.f%d", intrin,
221 ac_get_elem_bits(ctx, result_type));
222 assert(length < sizeof(name));
223 return ac_build_intrinsic(ctx, name, result_type, params, 3, AC_FUNC_ATTR_READNONE);
224 }
225
226 static LLVMValueRef emit_bcsel(struct ac_llvm_context *ctx,
227 LLVMValueRef src0, LLVMValueRef src1, LLVMValueRef src2)
228 {
229 LLVMValueRef v = LLVMBuildICmp(ctx->builder, LLVMIntNE, src0,
230 ctx->i32_0, "");
231 return LLVMBuildSelect(ctx->builder, v, ac_to_integer(ctx, src1),
232 ac_to_integer(ctx, src2), "");
233 }
234
235 static LLVMValueRef emit_minmax_int(struct ac_llvm_context *ctx,
236 LLVMIntPredicate pred,
237 LLVMValueRef src0, LLVMValueRef src1)
238 {
239 return LLVMBuildSelect(ctx->builder,
240 LLVMBuildICmp(ctx->builder, pred, src0, src1, ""),
241 src0,
242 src1, "");
243
244 }
245 static LLVMValueRef emit_iabs(struct ac_llvm_context *ctx,
246 LLVMValueRef src0)
247 {
248 return emit_minmax_int(ctx, LLVMIntSGT, src0,
249 LLVMBuildNeg(ctx->builder, src0, ""));
250 }
251
252 static LLVMValueRef emit_uint_carry(struct ac_llvm_context *ctx,
253 const char *intrin,
254 LLVMValueRef src0, LLVMValueRef src1)
255 {
256 LLVMTypeRef ret_type;
257 LLVMTypeRef types[] = { ctx->i32, ctx->i1 };
258 LLVMValueRef res;
259 LLVMValueRef params[] = { src0, src1 };
260 ret_type = LLVMStructTypeInContext(ctx->context, types,
261 2, true);
262
263 res = ac_build_intrinsic(ctx, intrin, ret_type,
264 params, 2, AC_FUNC_ATTR_READNONE);
265
266 res = LLVMBuildExtractValue(ctx->builder, res, 1, "");
267 res = LLVMBuildZExt(ctx->builder, res, ctx->i32, "");
268 return res;
269 }
270
271 static LLVMValueRef emit_b2f(struct ac_llvm_context *ctx,
272 LLVMValueRef src0)
273 {
274 return LLVMBuildAnd(ctx->builder, src0, LLVMBuildBitCast(ctx->builder, LLVMConstReal(ctx->f32, 1.0), ctx->i32, ""), "");
275 }
276
277 static LLVMValueRef emit_f2b(struct ac_llvm_context *ctx,
278 LLVMValueRef src0)
279 {
280 src0 = ac_to_float(ctx, src0);
281 LLVMValueRef zero = LLVMConstNull(LLVMTypeOf(src0));
282 return LLVMBuildSExt(ctx->builder,
283 LLVMBuildFCmp(ctx->builder, LLVMRealUNE, src0, zero, ""),
284 ctx->i32, "");
285 }
286
287 static LLVMValueRef emit_b2i(struct ac_llvm_context *ctx,
288 LLVMValueRef src0,
289 unsigned bitsize)
290 {
291 LLVMValueRef result = LLVMBuildAnd(ctx->builder, src0, ctx->i32_1, "");
292
293 if (bitsize == 32)
294 return result;
295
296 return LLVMBuildZExt(ctx->builder, result, ctx->i64, "");
297 }
298
299 static LLVMValueRef emit_i2b(struct ac_llvm_context *ctx,
300 LLVMValueRef src0)
301 {
302 LLVMValueRef zero = LLVMConstNull(LLVMTypeOf(src0));
303 return LLVMBuildSExt(ctx->builder,
304 LLVMBuildICmp(ctx->builder, LLVMIntNE, src0, zero, ""),
305 ctx->i32, "");
306 }
307
308 static LLVMValueRef emit_f2f16(struct ac_llvm_context *ctx,
309 LLVMValueRef src0)
310 {
311 LLVMValueRef result;
312 LLVMValueRef cond = NULL;
313
314 src0 = ac_to_float(ctx, src0);
315 result = LLVMBuildFPTrunc(ctx->builder, src0, ctx->f16, "");
316
317 if (ctx->chip_class >= VI) {
318 LLVMValueRef args[2];
319 /* Check if the result is a denormal - and flush to 0 if so. */
320 args[0] = result;
321 args[1] = LLVMConstInt(ctx->i32, N_SUBNORMAL | P_SUBNORMAL, false);
322 cond = ac_build_intrinsic(ctx, "llvm.amdgcn.class.f16", ctx->i1, args, 2, AC_FUNC_ATTR_READNONE);
323 }
324
325 /* need to convert back up to f32 */
326 result = LLVMBuildFPExt(ctx->builder, result, ctx->f32, "");
327
328 if (ctx->chip_class >= VI)
329 result = LLVMBuildSelect(ctx->builder, cond, ctx->f32_0, result, "");
330 else {
331 /* for SI/CIK */
332 /* 0x38800000 is smallest half float value (2^-14) in 32-bit float,
333 * so compare the result and flush to 0 if it's smaller.
334 */
335 LLVMValueRef temp, cond2;
336 temp = emit_intrin_1f_param(ctx, "llvm.fabs", ctx->f32, result);
337 cond = LLVMBuildFCmp(ctx->builder, LLVMRealUGT,
338 LLVMBuildBitCast(ctx->builder, LLVMConstInt(ctx->i32, 0x38800000, false), ctx->f32, ""),
339 temp, "");
340 cond2 = LLVMBuildFCmp(ctx->builder, LLVMRealUNE,
341 temp, ctx->f32_0, "");
342 cond = LLVMBuildAnd(ctx->builder, cond, cond2, "");
343 result = LLVMBuildSelect(ctx->builder, cond, ctx->f32_0, result, "");
344 }
345 return result;
346 }
347
348 static LLVMValueRef emit_umul_high(struct ac_llvm_context *ctx,
349 LLVMValueRef src0, LLVMValueRef src1)
350 {
351 LLVMValueRef dst64, result;
352 src0 = LLVMBuildZExt(ctx->builder, src0, ctx->i64, "");
353 src1 = LLVMBuildZExt(ctx->builder, src1, ctx->i64, "");
354
355 dst64 = LLVMBuildMul(ctx->builder, src0, src1, "");
356 dst64 = LLVMBuildLShr(ctx->builder, dst64, LLVMConstInt(ctx->i64, 32, false), "");
357 result = LLVMBuildTrunc(ctx->builder, dst64, ctx->i32, "");
358 return result;
359 }
360
361 static LLVMValueRef emit_imul_high(struct ac_llvm_context *ctx,
362 LLVMValueRef src0, LLVMValueRef src1)
363 {
364 LLVMValueRef dst64, result;
365 src0 = LLVMBuildSExt(ctx->builder, src0, ctx->i64, "");
366 src1 = LLVMBuildSExt(ctx->builder, src1, ctx->i64, "");
367
368 dst64 = LLVMBuildMul(ctx->builder, src0, src1, "");
369 dst64 = LLVMBuildAShr(ctx->builder, dst64, LLVMConstInt(ctx->i64, 32, false), "");
370 result = LLVMBuildTrunc(ctx->builder, dst64, ctx->i32, "");
371 return result;
372 }
373
374 static LLVMValueRef emit_bitfield_extract(struct ac_llvm_context *ctx,
375 bool is_signed,
376 const LLVMValueRef srcs[3])
377 {
378 LLVMValueRef result;
379 LLVMValueRef icond = LLVMBuildICmp(ctx->builder, LLVMIntEQ, srcs[2], LLVMConstInt(ctx->i32, 32, false), "");
380
381 result = ac_build_bfe(ctx, srcs[0], srcs[1], srcs[2], is_signed);
382 result = LLVMBuildSelect(ctx->builder, icond, srcs[0], result, "");
383 return result;
384 }
385
386 static LLVMValueRef emit_bitfield_insert(struct ac_llvm_context *ctx,
387 LLVMValueRef src0, LLVMValueRef src1,
388 LLVMValueRef src2, LLVMValueRef src3)
389 {
390 LLVMValueRef bfi_args[3], result;
391
392 bfi_args[0] = LLVMBuildShl(ctx->builder,
393 LLVMBuildSub(ctx->builder,
394 LLVMBuildShl(ctx->builder,
395 ctx->i32_1,
396 src3, ""),
397 ctx->i32_1, ""),
398 src2, "");
399 bfi_args[1] = LLVMBuildShl(ctx->builder, src1, src2, "");
400 bfi_args[2] = src0;
401
402 LLVMValueRef icond = LLVMBuildICmp(ctx->builder, LLVMIntEQ, src3, LLVMConstInt(ctx->i32, 32, false), "");
403
404 /* Calculate:
405 * (arg0 & arg1) | (~arg0 & arg2) = arg2 ^ (arg0 & (arg1 ^ arg2)
406 * Use the right-hand side, which the LLVM backend can convert to V_BFI.
407 */
408 result = LLVMBuildXor(ctx->builder, bfi_args[2],
409 LLVMBuildAnd(ctx->builder, bfi_args[0],
410 LLVMBuildXor(ctx->builder, bfi_args[1], bfi_args[2], ""), ""), "");
411
412 result = LLVMBuildSelect(ctx->builder, icond, src1, result, "");
413 return result;
414 }
415
416 static LLVMValueRef emit_pack_half_2x16(struct ac_llvm_context *ctx,
417 LLVMValueRef src0)
418 {
419 LLVMValueRef comp[2];
420
421 src0 = ac_to_float(ctx, src0);
422 comp[0] = LLVMBuildExtractElement(ctx->builder, src0, ctx->i32_0, "");
423 comp[1] = LLVMBuildExtractElement(ctx->builder, src0, ctx->i32_1, "");
424
425 return ac_build_cvt_pkrtz_f16(ctx, comp);
426 }
427
428 static LLVMValueRef emit_unpack_half_2x16(struct ac_llvm_context *ctx,
429 LLVMValueRef src0)
430 {
431 LLVMValueRef const16 = LLVMConstInt(ctx->i32, 16, false);
432 LLVMValueRef temps[2], result, val;
433 int i;
434
435 for (i = 0; i < 2; i++) {
436 val = i == 1 ? LLVMBuildLShr(ctx->builder, src0, const16, "") : src0;
437 val = LLVMBuildTrunc(ctx->builder, val, ctx->i16, "");
438 val = LLVMBuildBitCast(ctx->builder, val, ctx->f16, "");
439 temps[i] = LLVMBuildFPExt(ctx->builder, val, ctx->f32, "");
440 }
441
442 result = LLVMBuildInsertElement(ctx->builder, LLVMGetUndef(ctx->v2f32), temps[0],
443 ctx->i32_0, "");
444 result = LLVMBuildInsertElement(ctx->builder, result, temps[1],
445 ctx->i32_1, "");
446 return result;
447 }
448
449 static LLVMValueRef emit_ddxy(struct ac_nir_context *ctx,
450 nir_op op,
451 LLVMValueRef src0)
452 {
453 unsigned mask;
454 int idx;
455 LLVMValueRef result;
456
457 if (op == nir_op_fddx_fine)
458 mask = AC_TID_MASK_LEFT;
459 else if (op == nir_op_fddy_fine)
460 mask = AC_TID_MASK_TOP;
461 else
462 mask = AC_TID_MASK_TOP_LEFT;
463
464 /* for DDX we want to next X pixel, DDY next Y pixel. */
465 if (op == nir_op_fddx_fine ||
466 op == nir_op_fddx_coarse ||
467 op == nir_op_fddx)
468 idx = 1;
469 else
470 idx = 2;
471
472 result = ac_build_ddxy(&ctx->ac, mask, idx, src0);
473 return result;
474 }
475
476 /*
477 * this takes an I,J coordinate pair,
478 * and works out the X and Y derivatives.
479 * it returns DDX(I), DDX(J), DDY(I), DDY(J).
480 */
481 static LLVMValueRef emit_ddxy_interp(
482 struct ac_nir_context *ctx,
483 LLVMValueRef interp_ij)
484 {
485 LLVMValueRef result[4], a;
486 unsigned i;
487
488 for (i = 0; i < 2; i++) {
489 a = LLVMBuildExtractElement(ctx->ac.builder, interp_ij,
490 LLVMConstInt(ctx->ac.i32, i, false), "");
491 result[i] = emit_ddxy(ctx, nir_op_fddx, a);
492 result[2+i] = emit_ddxy(ctx, nir_op_fddy, a);
493 }
494 return ac_build_gather_values(&ctx->ac, result, 4);
495 }
496
497 static void visit_alu(struct ac_nir_context *ctx, const nir_alu_instr *instr)
498 {
499 LLVMValueRef src[4], result = NULL;
500 unsigned num_components = instr->dest.dest.ssa.num_components;
501 unsigned src_components;
502 LLVMTypeRef def_type = get_def_type(ctx, &instr->dest.dest.ssa);
503
504 assert(nir_op_infos[instr->op].num_inputs <= ARRAY_SIZE(src));
505 switch (instr->op) {
506 case nir_op_vec2:
507 case nir_op_vec3:
508 case nir_op_vec4:
509 src_components = 1;
510 break;
511 case nir_op_pack_half_2x16:
512 src_components = 2;
513 break;
514 case nir_op_unpack_half_2x16:
515 src_components = 1;
516 break;
517 case nir_op_cube_face_coord:
518 case nir_op_cube_face_index:
519 src_components = 3;
520 break;
521 default:
522 src_components = num_components;
523 break;
524 }
525 for (unsigned i = 0; i < nir_op_infos[instr->op].num_inputs; i++)
526 src[i] = get_alu_src(ctx, instr->src[i], src_components);
527
528 switch (instr->op) {
529 case nir_op_fmov:
530 case nir_op_imov:
531 result = src[0];
532 break;
533 case nir_op_fneg:
534 src[0] = ac_to_float(&ctx->ac, src[0]);
535 result = LLVMBuildFNeg(ctx->ac.builder, src[0], "");
536 break;
537 case nir_op_ineg:
538 result = LLVMBuildNeg(ctx->ac.builder, src[0], "");
539 break;
540 case nir_op_inot:
541 result = LLVMBuildNot(ctx->ac.builder, src[0], "");
542 break;
543 case nir_op_iadd:
544 result = LLVMBuildAdd(ctx->ac.builder, src[0], src[1], "");
545 break;
546 case nir_op_fadd:
547 src[0] = ac_to_float(&ctx->ac, src[0]);
548 src[1] = ac_to_float(&ctx->ac, src[1]);
549 result = LLVMBuildFAdd(ctx->ac.builder, src[0], src[1], "");
550 break;
551 case nir_op_fsub:
552 src[0] = ac_to_float(&ctx->ac, src[0]);
553 src[1] = ac_to_float(&ctx->ac, src[1]);
554 result = LLVMBuildFSub(ctx->ac.builder, src[0], src[1], "");
555 break;
556 case nir_op_isub:
557 result = LLVMBuildSub(ctx->ac.builder, src[0], src[1], "");
558 break;
559 case nir_op_imul:
560 result = LLVMBuildMul(ctx->ac.builder, src[0], src[1], "");
561 break;
562 case nir_op_imod:
563 result = LLVMBuildSRem(ctx->ac.builder, src[0], src[1], "");
564 break;
565 case nir_op_umod:
566 result = LLVMBuildURem(ctx->ac.builder, src[0], src[1], "");
567 break;
568 case nir_op_fmod:
569 src[0] = ac_to_float(&ctx->ac, src[0]);
570 src[1] = ac_to_float(&ctx->ac, src[1]);
571 result = ac_build_fdiv(&ctx->ac, src[0], src[1]);
572 result = emit_intrin_1f_param(&ctx->ac, "llvm.floor",
573 ac_to_float_type(&ctx->ac, def_type), result);
574 result = LLVMBuildFMul(ctx->ac.builder, src[1] , result, "");
575 result = LLVMBuildFSub(ctx->ac.builder, src[0], result, "");
576 break;
577 case nir_op_frem:
578 src[0] = ac_to_float(&ctx->ac, src[0]);
579 src[1] = ac_to_float(&ctx->ac, src[1]);
580 result = LLVMBuildFRem(ctx->ac.builder, src[0], src[1], "");
581 break;
582 case nir_op_irem:
583 result = LLVMBuildSRem(ctx->ac.builder, src[0], src[1], "");
584 break;
585 case nir_op_idiv:
586 result = LLVMBuildSDiv(ctx->ac.builder, src[0], src[1], "");
587 break;
588 case nir_op_udiv:
589 result = LLVMBuildUDiv(ctx->ac.builder, src[0], src[1], "");
590 break;
591 case nir_op_fmul:
592 src[0] = ac_to_float(&ctx->ac, src[0]);
593 src[1] = ac_to_float(&ctx->ac, src[1]);
594 result = LLVMBuildFMul(ctx->ac.builder, src[0], src[1], "");
595 break;
596 case nir_op_frcp:
597 src[0] = ac_to_float(&ctx->ac, src[0]);
598 result = ac_build_fdiv(&ctx->ac, instr->dest.dest.ssa.bit_size == 32 ? ctx->ac.f32_1 : ctx->ac.f64_1,
599 src[0]);
600 break;
601 case nir_op_iand:
602 result = LLVMBuildAnd(ctx->ac.builder, src[0], src[1], "");
603 break;
604 case nir_op_ior:
605 result = LLVMBuildOr(ctx->ac.builder, src[0], src[1], "");
606 break;
607 case nir_op_ixor:
608 result = LLVMBuildXor(ctx->ac.builder, src[0], src[1], "");
609 break;
610 case nir_op_ishl:
611 result = LLVMBuildShl(ctx->ac.builder, src[0],
612 LLVMBuildZExt(ctx->ac.builder, src[1],
613 LLVMTypeOf(src[0]), ""),
614 "");
615 break;
616 case nir_op_ishr:
617 result = LLVMBuildAShr(ctx->ac.builder, src[0],
618 LLVMBuildZExt(ctx->ac.builder, src[1],
619 LLVMTypeOf(src[0]), ""),
620 "");
621 break;
622 case nir_op_ushr:
623 result = LLVMBuildLShr(ctx->ac.builder, src[0],
624 LLVMBuildZExt(ctx->ac.builder, src[1],
625 LLVMTypeOf(src[0]), ""),
626 "");
627 break;
628 case nir_op_ilt:
629 result = emit_int_cmp(&ctx->ac, LLVMIntSLT, src[0], src[1]);
630 break;
631 case nir_op_ine:
632 result = emit_int_cmp(&ctx->ac, LLVMIntNE, src[0], src[1]);
633 break;
634 case nir_op_ieq:
635 result = emit_int_cmp(&ctx->ac, LLVMIntEQ, src[0], src[1]);
636 break;
637 case nir_op_ige:
638 result = emit_int_cmp(&ctx->ac, LLVMIntSGE, src[0], src[1]);
639 break;
640 case nir_op_ult:
641 result = emit_int_cmp(&ctx->ac, LLVMIntULT, src[0], src[1]);
642 break;
643 case nir_op_uge:
644 result = emit_int_cmp(&ctx->ac, LLVMIntUGE, src[0], src[1]);
645 break;
646 case nir_op_feq:
647 result = emit_float_cmp(&ctx->ac, LLVMRealOEQ, src[0], src[1]);
648 break;
649 case nir_op_fne:
650 result = emit_float_cmp(&ctx->ac, LLVMRealUNE, src[0], src[1]);
651 break;
652 case nir_op_flt:
653 result = emit_float_cmp(&ctx->ac, LLVMRealOLT, src[0], src[1]);
654 break;
655 case nir_op_fge:
656 result = emit_float_cmp(&ctx->ac, LLVMRealOGE, src[0], src[1]);
657 break;
658 case nir_op_fabs:
659 result = emit_intrin_1f_param(&ctx->ac, "llvm.fabs",
660 ac_to_float_type(&ctx->ac, def_type), src[0]);
661 break;
662 case nir_op_iabs:
663 result = emit_iabs(&ctx->ac, src[0]);
664 break;
665 case nir_op_imax:
666 result = emit_minmax_int(&ctx->ac, LLVMIntSGT, src[0], src[1]);
667 break;
668 case nir_op_imin:
669 result = emit_minmax_int(&ctx->ac, LLVMIntSLT, src[0], src[1]);
670 break;
671 case nir_op_umax:
672 result = emit_minmax_int(&ctx->ac, LLVMIntUGT, src[0], src[1]);
673 break;
674 case nir_op_umin:
675 result = emit_minmax_int(&ctx->ac, LLVMIntULT, src[0], src[1]);
676 break;
677 case nir_op_isign:
678 result = ac_build_isign(&ctx->ac, src[0],
679 instr->dest.dest.ssa.bit_size);
680 break;
681 case nir_op_fsign:
682 src[0] = ac_to_float(&ctx->ac, src[0]);
683 result = ac_build_fsign(&ctx->ac, src[0],
684 instr->dest.dest.ssa.bit_size);
685 break;
686 case nir_op_ffloor:
687 result = emit_intrin_1f_param(&ctx->ac, "llvm.floor",
688 ac_to_float_type(&ctx->ac, def_type), src[0]);
689 break;
690 case nir_op_ftrunc:
691 result = emit_intrin_1f_param(&ctx->ac, "llvm.trunc",
692 ac_to_float_type(&ctx->ac, def_type), src[0]);
693 break;
694 case nir_op_fceil:
695 result = emit_intrin_1f_param(&ctx->ac, "llvm.ceil",
696 ac_to_float_type(&ctx->ac, def_type), src[0]);
697 break;
698 case nir_op_fround_even:
699 result = emit_intrin_1f_param(&ctx->ac, "llvm.rint",
700 ac_to_float_type(&ctx->ac, def_type),src[0]);
701 break;
702 case nir_op_ffract:
703 src[0] = ac_to_float(&ctx->ac, src[0]);
704 result = ac_build_fract(&ctx->ac, src[0],
705 instr->dest.dest.ssa.bit_size);
706 break;
707 case nir_op_fsin:
708 result = emit_intrin_1f_param(&ctx->ac, "llvm.sin",
709 ac_to_float_type(&ctx->ac, def_type), src[0]);
710 break;
711 case nir_op_fcos:
712 result = emit_intrin_1f_param(&ctx->ac, "llvm.cos",
713 ac_to_float_type(&ctx->ac, def_type), src[0]);
714 break;
715 case nir_op_fsqrt:
716 result = emit_intrin_1f_param(&ctx->ac, "llvm.sqrt",
717 ac_to_float_type(&ctx->ac, def_type), src[0]);
718 break;
719 case nir_op_fexp2:
720 result = emit_intrin_1f_param(&ctx->ac, "llvm.exp2",
721 ac_to_float_type(&ctx->ac, def_type), src[0]);
722 break;
723 case nir_op_flog2:
724 result = emit_intrin_1f_param(&ctx->ac, "llvm.log2",
725 ac_to_float_type(&ctx->ac, def_type), src[0]);
726 break;
727 case nir_op_frsq:
728 result = emit_intrin_1f_param(&ctx->ac, "llvm.sqrt",
729 ac_to_float_type(&ctx->ac, def_type), src[0]);
730 result = ac_build_fdiv(&ctx->ac, instr->dest.dest.ssa.bit_size == 32 ? ctx->ac.f32_1 : ctx->ac.f64_1,
731 result);
732 break;
733 case nir_op_frexp_exp:
734 src[0] = ac_to_float(&ctx->ac, src[0]);
735 result = ac_build_intrinsic(&ctx->ac, "llvm.amdgcn.frexp.exp.i32.f64",
736 ctx->ac.i32, src, 1, AC_FUNC_ATTR_READNONE);
737
738 break;
739 case nir_op_frexp_sig:
740 src[0] = ac_to_float(&ctx->ac, src[0]);
741 result = ac_build_intrinsic(&ctx->ac, "llvm.amdgcn.frexp.mant.f64",
742 ctx->ac.f64, src, 1, AC_FUNC_ATTR_READNONE);
743 break;
744 case nir_op_fmax:
745 result = emit_intrin_2f_param(&ctx->ac, "llvm.maxnum",
746 ac_to_float_type(&ctx->ac, def_type), src[0], src[1]);
747 if (ctx->ac.chip_class < GFX9 &&
748 instr->dest.dest.ssa.bit_size == 32) {
749 /* Only pre-GFX9 chips do not flush denorms. */
750 result = emit_intrin_1f_param(&ctx->ac, "llvm.canonicalize",
751 ac_to_float_type(&ctx->ac, def_type),
752 result);
753 }
754 break;
755 case nir_op_fmin:
756 result = emit_intrin_2f_param(&ctx->ac, "llvm.minnum",
757 ac_to_float_type(&ctx->ac, def_type), src[0], src[1]);
758 if (ctx->ac.chip_class < GFX9 &&
759 instr->dest.dest.ssa.bit_size == 32) {
760 /* Only pre-GFX9 chips do not flush denorms. */
761 result = emit_intrin_1f_param(&ctx->ac, "llvm.canonicalize",
762 ac_to_float_type(&ctx->ac, def_type),
763 result);
764 }
765 break;
766 case nir_op_ffma:
767 result = emit_intrin_3f_param(&ctx->ac, "llvm.fmuladd",
768 ac_to_float_type(&ctx->ac, def_type), src[0], src[1], src[2]);
769 break;
770 case nir_op_ldexp:
771 src[0] = ac_to_float(&ctx->ac, src[0]);
772 if (ac_get_elem_bits(&ctx->ac, LLVMTypeOf(src[0])) == 32)
773 result = ac_build_intrinsic(&ctx->ac, "llvm.amdgcn.ldexp.f32", ctx->ac.f32, src, 2, AC_FUNC_ATTR_READNONE);
774 else
775 result = ac_build_intrinsic(&ctx->ac, "llvm.amdgcn.ldexp.f64", ctx->ac.f64, src, 2, AC_FUNC_ATTR_READNONE);
776 break;
777 case nir_op_ibitfield_extract:
778 result = emit_bitfield_extract(&ctx->ac, true, src);
779 break;
780 case nir_op_ubitfield_extract:
781 result = emit_bitfield_extract(&ctx->ac, false, src);
782 break;
783 case nir_op_bitfield_insert:
784 result = emit_bitfield_insert(&ctx->ac, src[0], src[1], src[2], src[3]);
785 break;
786 case nir_op_bitfield_reverse:
787 result = ac_build_intrinsic(&ctx->ac, "llvm.bitreverse.i32", ctx->ac.i32, src, 1, AC_FUNC_ATTR_READNONE);
788 break;
789 case nir_op_bit_count:
790 if (ac_get_elem_bits(&ctx->ac, LLVMTypeOf(src[0])) == 32)
791 result = ac_build_intrinsic(&ctx->ac, "llvm.ctpop.i32", ctx->ac.i32, src, 1, AC_FUNC_ATTR_READNONE);
792 else {
793 result = ac_build_intrinsic(&ctx->ac, "llvm.ctpop.i64", ctx->ac.i64, src, 1, AC_FUNC_ATTR_READNONE);
794 result = LLVMBuildTrunc(ctx->ac.builder, result, ctx->ac.i32, "");
795 }
796 break;
797 case nir_op_vec2:
798 case nir_op_vec3:
799 case nir_op_vec4:
800 for (unsigned i = 0; i < nir_op_infos[instr->op].num_inputs; i++)
801 src[i] = ac_to_integer(&ctx->ac, src[i]);
802 result = ac_build_gather_values(&ctx->ac, src, num_components);
803 break;
804 case nir_op_f2i32:
805 case nir_op_f2i64:
806 src[0] = ac_to_float(&ctx->ac, src[0]);
807 result = LLVMBuildFPToSI(ctx->ac.builder, src[0], def_type, "");
808 break;
809 case nir_op_f2u32:
810 case nir_op_f2u64:
811 src[0] = ac_to_float(&ctx->ac, src[0]);
812 result = LLVMBuildFPToUI(ctx->ac.builder, src[0], def_type, "");
813 break;
814 case nir_op_i2f32:
815 case nir_op_i2f64:
816 src[0] = ac_to_integer(&ctx->ac, src[0]);
817 result = LLVMBuildSIToFP(ctx->ac.builder, src[0], ac_to_float_type(&ctx->ac, def_type), "");
818 break;
819 case nir_op_u2f32:
820 case nir_op_u2f64:
821 src[0] = ac_to_integer(&ctx->ac, src[0]);
822 result = LLVMBuildUIToFP(ctx->ac.builder, src[0], ac_to_float_type(&ctx->ac, def_type), "");
823 break;
824 case nir_op_f2f64:
825 src[0] = ac_to_float(&ctx->ac, src[0]);
826 result = LLVMBuildFPExt(ctx->ac.builder, src[0], ac_to_float_type(&ctx->ac, def_type), "");
827 break;
828 case nir_op_f2f32:
829 src[0] = ac_to_float(&ctx->ac, src[0]);
830 result = LLVMBuildFPTrunc(ctx->ac.builder, src[0], ac_to_float_type(&ctx->ac, def_type), "");
831 break;
832 case nir_op_u2u32:
833 case nir_op_u2u64:
834 src[0] = ac_to_integer(&ctx->ac, src[0]);
835 if (ac_get_elem_bits(&ctx->ac, LLVMTypeOf(src[0])) < ac_get_elem_bits(&ctx->ac, def_type))
836 result = LLVMBuildZExt(ctx->ac.builder, src[0], def_type, "");
837 else
838 result = LLVMBuildTrunc(ctx->ac.builder, src[0], def_type, "");
839 break;
840 case nir_op_i2i32:
841 case nir_op_i2i64:
842 src[0] = ac_to_integer(&ctx->ac, src[0]);
843 if (ac_get_elem_bits(&ctx->ac, LLVMTypeOf(src[0])) < ac_get_elem_bits(&ctx->ac, def_type))
844 result = LLVMBuildSExt(ctx->ac.builder, src[0], def_type, "");
845 else
846 result = LLVMBuildTrunc(ctx->ac.builder, src[0], def_type, "");
847 break;
848 case nir_op_bcsel:
849 result = emit_bcsel(&ctx->ac, src[0], src[1], src[2]);
850 break;
851 case nir_op_find_lsb:
852 src[0] = ac_to_integer(&ctx->ac, src[0]);
853 result = ac_find_lsb(&ctx->ac, ctx->ac.i32, src[0]);
854 break;
855 case nir_op_ufind_msb:
856 src[0] = ac_to_integer(&ctx->ac, src[0]);
857 result = ac_build_umsb(&ctx->ac, src[0], ctx->ac.i32);
858 break;
859 case nir_op_ifind_msb:
860 src[0] = ac_to_integer(&ctx->ac, src[0]);
861 result = ac_build_imsb(&ctx->ac, src[0], ctx->ac.i32);
862 break;
863 case nir_op_uadd_carry:
864 src[0] = ac_to_integer(&ctx->ac, src[0]);
865 src[1] = ac_to_integer(&ctx->ac, src[1]);
866 result = emit_uint_carry(&ctx->ac, "llvm.uadd.with.overflow.i32", src[0], src[1]);
867 break;
868 case nir_op_usub_borrow:
869 src[0] = ac_to_integer(&ctx->ac, src[0]);
870 src[1] = ac_to_integer(&ctx->ac, src[1]);
871 result = emit_uint_carry(&ctx->ac, "llvm.usub.with.overflow.i32", src[0], src[1]);
872 break;
873 case nir_op_b2f:
874 result = emit_b2f(&ctx->ac, src[0]);
875 break;
876 case nir_op_f2b:
877 result = emit_f2b(&ctx->ac, src[0]);
878 break;
879 case nir_op_b2i:
880 result = emit_b2i(&ctx->ac, src[0], instr->dest.dest.ssa.bit_size);
881 break;
882 case nir_op_i2b:
883 src[0] = ac_to_integer(&ctx->ac, src[0]);
884 result = emit_i2b(&ctx->ac, src[0]);
885 break;
886 case nir_op_fquantize2f16:
887 result = emit_f2f16(&ctx->ac, src[0]);
888 break;
889 case nir_op_umul_high:
890 src[0] = ac_to_integer(&ctx->ac, src[0]);
891 src[1] = ac_to_integer(&ctx->ac, src[1]);
892 result = emit_umul_high(&ctx->ac, src[0], src[1]);
893 break;
894 case nir_op_imul_high:
895 src[0] = ac_to_integer(&ctx->ac, src[0]);
896 src[1] = ac_to_integer(&ctx->ac, src[1]);
897 result = emit_imul_high(&ctx->ac, src[0], src[1]);
898 break;
899 case nir_op_pack_half_2x16:
900 result = emit_pack_half_2x16(&ctx->ac, src[0]);
901 break;
902 case nir_op_unpack_half_2x16:
903 result = emit_unpack_half_2x16(&ctx->ac, src[0]);
904 break;
905 case nir_op_fddx:
906 case nir_op_fddy:
907 case nir_op_fddx_fine:
908 case nir_op_fddy_fine:
909 case nir_op_fddx_coarse:
910 case nir_op_fddy_coarse:
911 result = emit_ddxy(ctx, instr->op, src[0]);
912 break;
913
914 case nir_op_unpack_64_2x32_split_x: {
915 assert(ac_get_llvm_num_components(src[0]) == 1);
916 LLVMValueRef tmp = LLVMBuildBitCast(ctx->ac.builder, src[0],
917 ctx->ac.v2i32,
918 "");
919 result = LLVMBuildExtractElement(ctx->ac.builder, tmp,
920 ctx->ac.i32_0, "");
921 break;
922 }
923
924 case nir_op_unpack_64_2x32_split_y: {
925 assert(ac_get_llvm_num_components(src[0]) == 1);
926 LLVMValueRef tmp = LLVMBuildBitCast(ctx->ac.builder, src[0],
927 ctx->ac.v2i32,
928 "");
929 result = LLVMBuildExtractElement(ctx->ac.builder, tmp,
930 ctx->ac.i32_1, "");
931 break;
932 }
933
934 case nir_op_pack_64_2x32_split: {
935 LLVMValueRef tmp = LLVMGetUndef(ctx->ac.v2i32);
936 tmp = LLVMBuildInsertElement(ctx->ac.builder, tmp,
937 src[0], ctx->ac.i32_0, "");
938 tmp = LLVMBuildInsertElement(ctx->ac.builder, tmp,
939 src[1], ctx->ac.i32_1, "");
940 result = LLVMBuildBitCast(ctx->ac.builder, tmp, ctx->ac.i64, "");
941 break;
942 }
943
944 case nir_op_cube_face_coord: {
945 src[0] = ac_to_float(&ctx->ac, src[0]);
946 LLVMValueRef results[2];
947 LLVMValueRef in[3];
948 for (unsigned chan = 0; chan < 3; chan++)
949 in[chan] = ac_llvm_extract_elem(&ctx->ac, src[0], chan);
950 results[0] = ac_build_intrinsic(&ctx->ac, "llvm.amdgcn.cubetc",
951 ctx->ac.f32, in, 3, AC_FUNC_ATTR_READNONE);
952 results[1] = ac_build_intrinsic(&ctx->ac, "llvm.amdgcn.cubesc",
953 ctx->ac.f32, in, 3, AC_FUNC_ATTR_READNONE);
954 result = ac_build_gather_values(&ctx->ac, results, 2);
955 break;
956 }
957
958 case nir_op_cube_face_index: {
959 src[0] = ac_to_float(&ctx->ac, src[0]);
960 LLVMValueRef in[3];
961 for (unsigned chan = 0; chan < 3; chan++)
962 in[chan] = ac_llvm_extract_elem(&ctx->ac, src[0], chan);
963 result = ac_build_intrinsic(&ctx->ac, "llvm.amdgcn.cubeid",
964 ctx->ac.f32, in, 3, AC_FUNC_ATTR_READNONE);
965 break;
966 }
967
968 case nir_op_fmin3:
969 result = emit_intrin_2f_param(&ctx->ac, "llvm.minnum",
970 ac_to_float_type(&ctx->ac, def_type), src[0], src[1]);
971 result = emit_intrin_2f_param(&ctx->ac, "llvm.minnum",
972 ac_to_float_type(&ctx->ac, def_type), result, src[2]);
973 break;
974 case nir_op_umin3:
975 result = emit_minmax_int(&ctx->ac, LLVMIntULT, src[0], src[1]);
976 result = emit_minmax_int(&ctx->ac, LLVMIntULT, result, src[2]);
977 break;
978 case nir_op_imin3:
979 result = emit_minmax_int(&ctx->ac, LLVMIntSLT, src[0], src[1]);
980 result = emit_minmax_int(&ctx->ac, LLVMIntSLT, result, src[2]);
981 break;
982 case nir_op_fmax3:
983 result = emit_intrin_2f_param(&ctx->ac, "llvm.maxnum",
984 ac_to_float_type(&ctx->ac, def_type), src[0], src[1]);
985 result = emit_intrin_2f_param(&ctx->ac, "llvm.maxnum",
986 ac_to_float_type(&ctx->ac, def_type), result, src[2]);
987 break;
988 case nir_op_umax3:
989 result = emit_minmax_int(&ctx->ac, LLVMIntUGT, src[0], src[1]);
990 result = emit_minmax_int(&ctx->ac, LLVMIntUGT, result, src[2]);
991 break;
992 case nir_op_imax3:
993 result = emit_minmax_int(&ctx->ac, LLVMIntSGT, src[0], src[1]);
994 result = emit_minmax_int(&ctx->ac, LLVMIntSGT, result, src[2]);
995 break;
996 case nir_op_fmed3: {
997 LLVMValueRef tmp1 = emit_intrin_2f_param(&ctx->ac, "llvm.minnum",
998 ac_to_float_type(&ctx->ac, def_type), src[0], src[1]);
999 LLVMValueRef tmp2 = emit_intrin_2f_param(&ctx->ac, "llvm.maxnum",
1000 ac_to_float_type(&ctx->ac, def_type), src[0], src[1]);
1001 tmp2 = emit_intrin_2f_param(&ctx->ac, "llvm.minnum",
1002 ac_to_float_type(&ctx->ac, def_type), tmp2, src[2]);
1003 result = emit_intrin_2f_param(&ctx->ac, "llvm.maxnum",
1004 ac_to_float_type(&ctx->ac, def_type), tmp1, tmp2);
1005 break;
1006 }
1007 case nir_op_imed3: {
1008 LLVMValueRef tmp1 = emit_minmax_int(&ctx->ac, LLVMIntSLT, src[0], src[1]);
1009 LLVMValueRef tmp2 = emit_minmax_int(&ctx->ac, LLVMIntSGT, src[0], src[1]);
1010 tmp2 = emit_minmax_int(&ctx->ac, LLVMIntSLT, tmp2, src[2]);
1011 result = emit_minmax_int(&ctx->ac, LLVMIntSGT, tmp1, tmp2);
1012 break;
1013 }
1014 case nir_op_umed3: {
1015 LLVMValueRef tmp1 = emit_minmax_int(&ctx->ac, LLVMIntULT, src[0], src[1]);
1016 LLVMValueRef tmp2 = emit_minmax_int(&ctx->ac, LLVMIntUGT, src[0], src[1]);
1017 tmp2 = emit_minmax_int(&ctx->ac, LLVMIntULT, tmp2, src[2]);
1018 result = emit_minmax_int(&ctx->ac, LLVMIntUGT, tmp1, tmp2);
1019 break;
1020 }
1021
1022 default:
1023 fprintf(stderr, "Unknown NIR alu instr: ");
1024 nir_print_instr(&instr->instr, stderr);
1025 fprintf(stderr, "\n");
1026 abort();
1027 }
1028
1029 if (result) {
1030 assert(instr->dest.dest.is_ssa);
1031 result = ac_to_integer(&ctx->ac, result);
1032 ctx->ssa_defs[instr->dest.dest.ssa.index] = result;
1033 }
1034 }
1035
1036 static void visit_load_const(struct ac_nir_context *ctx,
1037 const nir_load_const_instr *instr)
1038 {
1039 LLVMValueRef values[4], value = NULL;
1040 LLVMTypeRef element_type =
1041 LLVMIntTypeInContext(ctx->ac.context, instr->def.bit_size);
1042
1043 for (unsigned i = 0; i < instr->def.num_components; ++i) {
1044 switch (instr->def.bit_size) {
1045 case 32:
1046 values[i] = LLVMConstInt(element_type,
1047 instr->value.u32[i], false);
1048 break;
1049 case 64:
1050 values[i] = LLVMConstInt(element_type,
1051 instr->value.u64[i], false);
1052 break;
1053 default:
1054 fprintf(stderr,
1055 "unsupported nir load_const bit_size: %d\n",
1056 instr->def.bit_size);
1057 abort();
1058 }
1059 }
1060 if (instr->def.num_components > 1) {
1061 value = LLVMConstVector(values, instr->def.num_components);
1062 } else
1063 value = values[0];
1064
1065 ctx->ssa_defs[instr->def.index] = value;
1066 }
1067
1068 static LLVMValueRef
1069 get_buffer_size(struct ac_nir_context *ctx, LLVMValueRef descriptor, bool in_elements)
1070 {
1071 LLVMValueRef size =
1072 LLVMBuildExtractElement(ctx->ac.builder, descriptor,
1073 LLVMConstInt(ctx->ac.i32, 2, false), "");
1074
1075 /* VI only */
1076 if (ctx->ac.chip_class == VI && in_elements) {
1077 /* On VI, the descriptor contains the size in bytes,
1078 * but TXQ must return the size in elements.
1079 * The stride is always non-zero for resources using TXQ.
1080 */
1081 LLVMValueRef stride =
1082 LLVMBuildExtractElement(ctx->ac.builder, descriptor,
1083 ctx->ac.i32_1, "");
1084 stride = LLVMBuildLShr(ctx->ac.builder, stride,
1085 LLVMConstInt(ctx->ac.i32, 16, false), "");
1086 stride = LLVMBuildAnd(ctx->ac.builder, stride,
1087 LLVMConstInt(ctx->ac.i32, 0x3fff, false), "");
1088
1089 size = LLVMBuildUDiv(ctx->ac.builder, size, stride, "");
1090 }
1091 return size;
1092 }
1093
1094 /**
1095 * Given the i32 or vNi32 \p type, generate the textual name (e.g. for use with
1096 * intrinsic names).
1097 */
1098 static void build_int_type_name(
1099 LLVMTypeRef type,
1100 char *buf, unsigned bufsize)
1101 {
1102 assert(bufsize >= 6);
1103
1104 if (LLVMGetTypeKind(type) == LLVMVectorTypeKind)
1105 snprintf(buf, bufsize, "v%ui32",
1106 LLVMGetVectorSize(type));
1107 else
1108 strcpy(buf, "i32");
1109 }
1110
1111 static LLVMValueRef lower_gather4_integer(struct ac_llvm_context *ctx,
1112 struct ac_image_args *args,
1113 const nir_tex_instr *instr)
1114 {
1115 enum glsl_base_type stype = glsl_get_sampler_result_type(instr->texture->var->type);
1116 LLVMValueRef coord = args->addr;
1117 LLVMValueRef half_texel[2];
1118 LLVMValueRef compare_cube_wa = NULL;
1119 LLVMValueRef result;
1120 int c;
1121 unsigned coord_vgpr_index = (unsigned)args->offset + (unsigned)args->compare;
1122
1123 //TODO Rect
1124 {
1125 struct ac_image_args txq_args = { 0 };
1126
1127 txq_args.da = instr->is_array || instr->sampler_dim == GLSL_SAMPLER_DIM_CUBE;
1128 txq_args.opcode = ac_image_get_resinfo;
1129 txq_args.dmask = 0xf;
1130 txq_args.addr = ctx->i32_0;
1131 txq_args.resource = args->resource;
1132 LLVMValueRef size = ac_build_image_opcode(ctx, &txq_args);
1133
1134 for (c = 0; c < 2; c++) {
1135 half_texel[c] = LLVMBuildExtractElement(ctx->builder, size,
1136 LLVMConstInt(ctx->i32, c, false), "");
1137 half_texel[c] = LLVMBuildUIToFP(ctx->builder, half_texel[c], ctx->f32, "");
1138 half_texel[c] = ac_build_fdiv(ctx, ctx->f32_1, half_texel[c]);
1139 half_texel[c] = LLVMBuildFMul(ctx->builder, half_texel[c],
1140 LLVMConstReal(ctx->f32, -0.5), "");
1141 }
1142 }
1143
1144 LLVMValueRef orig_coords = args->addr;
1145
1146 for (c = 0; c < 2; c++) {
1147 LLVMValueRef tmp;
1148 LLVMValueRef index = LLVMConstInt(ctx->i32, coord_vgpr_index + c, 0);
1149 tmp = LLVMBuildExtractElement(ctx->builder, coord, index, "");
1150 tmp = LLVMBuildBitCast(ctx->builder, tmp, ctx->f32, "");
1151 tmp = LLVMBuildFAdd(ctx->builder, tmp, half_texel[c], "");
1152 tmp = LLVMBuildBitCast(ctx->builder, tmp, ctx->i32, "");
1153 coord = LLVMBuildInsertElement(ctx->builder, coord, tmp, index, "");
1154 }
1155
1156
1157 /*
1158 * Apparantly cube has issue with integer types that the workaround doesn't solve,
1159 * so this tests if the format is 8_8_8_8 and an integer type do an alternate
1160 * workaround by sampling using a scaled type and converting.
1161 * This is taken from amdgpu-pro shaders.
1162 */
1163 /* NOTE this produces some ugly code compared to amdgpu-pro,
1164 * LLVM ends up dumping SGPRs into VGPRs to deal with the compare/select,
1165 * and then reads them back. -pro generates two selects,
1166 * one s_cmp for the descriptor rewriting
1167 * one v_cmp for the coordinate and result changes.
1168 */
1169 if (instr->sampler_dim == GLSL_SAMPLER_DIM_CUBE) {
1170 LLVMValueRef tmp, tmp2;
1171
1172 /* workaround 8/8/8/8 uint/sint cube gather bug */
1173 /* first detect it then change to a scaled read and f2i */
1174 tmp = LLVMBuildExtractElement(ctx->builder, args->resource, ctx->i32_1, "");
1175 tmp2 = tmp;
1176
1177 /* extract the DATA_FORMAT */
1178 tmp = ac_build_bfe(ctx, tmp, LLVMConstInt(ctx->i32, 20, false),
1179 LLVMConstInt(ctx->i32, 6, false), false);
1180
1181 /* is the DATA_FORMAT == 8_8_8_8 */
1182 compare_cube_wa = LLVMBuildICmp(ctx->builder, LLVMIntEQ, tmp, LLVMConstInt(ctx->i32, V_008F14_IMG_DATA_FORMAT_8_8_8_8, false), "");
1183
1184 if (stype == GLSL_TYPE_UINT)
1185 /* Create a NUM FORMAT - 0x2 or 0x4 - USCALED or UINT */
1186 tmp = LLVMBuildSelect(ctx->builder, compare_cube_wa, LLVMConstInt(ctx->i32, 0x8000000, false),
1187 LLVMConstInt(ctx->i32, 0x10000000, false), "");
1188 else
1189 /* Create a NUM FORMAT - 0x3 or 0x5 - SSCALED or SINT */
1190 tmp = LLVMBuildSelect(ctx->builder, compare_cube_wa, LLVMConstInt(ctx->i32, 0xc000000, false),
1191 LLVMConstInt(ctx->i32, 0x14000000, false), "");
1192
1193 /* replace the NUM FORMAT in the descriptor */
1194 tmp2 = LLVMBuildAnd(ctx->builder, tmp2, LLVMConstInt(ctx->i32, C_008F14_NUM_FORMAT_GFX6, false), "");
1195 tmp2 = LLVMBuildOr(ctx->builder, tmp2, tmp, "");
1196
1197 args->resource = LLVMBuildInsertElement(ctx->builder, args->resource, tmp2, ctx->i32_1, "");
1198
1199 /* don't modify the coordinates for this case */
1200 coord = LLVMBuildSelect(ctx->builder, compare_cube_wa, orig_coords, coord, "");
1201 }
1202 args->addr = coord;
1203 result = ac_build_image_opcode(ctx, args);
1204
1205 if (instr->sampler_dim == GLSL_SAMPLER_DIM_CUBE) {
1206 LLVMValueRef tmp, tmp2;
1207
1208 /* if the cube workaround is in place, f2i the result. */
1209 for (c = 0; c < 4; c++) {
1210 tmp = LLVMBuildExtractElement(ctx->builder, result, LLVMConstInt(ctx->i32, c, false), "");
1211 if (stype == GLSL_TYPE_UINT)
1212 tmp2 = LLVMBuildFPToUI(ctx->builder, tmp, ctx->i32, "");
1213 else
1214 tmp2 = LLVMBuildFPToSI(ctx->builder, tmp, ctx->i32, "");
1215 tmp = LLVMBuildBitCast(ctx->builder, tmp, ctx->i32, "");
1216 tmp2 = LLVMBuildBitCast(ctx->builder, tmp2, ctx->i32, "");
1217 tmp = LLVMBuildSelect(ctx->builder, compare_cube_wa, tmp2, tmp, "");
1218 tmp = LLVMBuildBitCast(ctx->builder, tmp, ctx->f32, "");
1219 result = LLVMBuildInsertElement(ctx->builder, result, tmp, LLVMConstInt(ctx->i32, c, false), "");
1220 }
1221 }
1222 return result;
1223 }
1224
1225 static LLVMValueRef build_tex_intrinsic(struct ac_nir_context *ctx,
1226 const nir_tex_instr *instr,
1227 bool lod_is_zero,
1228 struct ac_image_args *args)
1229 {
1230 if (instr->sampler_dim == GLSL_SAMPLER_DIM_BUF) {
1231 unsigned mask = nir_ssa_def_components_read(&instr->dest.ssa);
1232
1233 if (ctx->abi->gfx9_stride_size_workaround) {
1234 return ac_build_buffer_load_format_gfx9_safe(&ctx->ac,
1235 args->resource,
1236 args->addr,
1237 ctx->ac.i32_0,
1238 util_last_bit(mask),
1239 false, true);
1240 } else {
1241 return ac_build_buffer_load_format(&ctx->ac,
1242 args->resource,
1243 args->addr,
1244 ctx->ac.i32_0,
1245 util_last_bit(mask),
1246 false, true);
1247 }
1248 }
1249
1250 args->opcode = ac_image_sample;
1251 args->compare = instr->is_shadow;
1252
1253 switch (instr->op) {
1254 case nir_texop_txf:
1255 case nir_texop_txf_ms:
1256 case nir_texop_samples_identical:
1257 args->opcode = lod_is_zero ||
1258 instr->sampler_dim == GLSL_SAMPLER_DIM_MS ?
1259 ac_image_load : ac_image_load_mip;
1260 args->compare = false;
1261 args->offset = false;
1262 break;
1263 case nir_texop_txb:
1264 args->bias = true;
1265 break;
1266 case nir_texop_txl:
1267 if (lod_is_zero)
1268 args->level_zero = true;
1269 else
1270 args->lod = true;
1271 break;
1272 case nir_texop_txs:
1273 case nir_texop_query_levels:
1274 args->opcode = ac_image_get_resinfo;
1275 break;
1276 case nir_texop_tex:
1277 if (ctx->stage != MESA_SHADER_FRAGMENT)
1278 args->level_zero = true;
1279 break;
1280 case nir_texop_txd:
1281 args->deriv = true;
1282 break;
1283 case nir_texop_tg4:
1284 args->opcode = ac_image_gather4;
1285 args->level_zero = true;
1286 break;
1287 case nir_texop_lod:
1288 args->opcode = ac_image_get_lod;
1289 args->compare = false;
1290 args->offset = false;
1291 break;
1292 default:
1293 break;
1294 }
1295
1296 if (instr->op == nir_texop_tg4 && ctx->ac.chip_class <= VI) {
1297 enum glsl_base_type stype = glsl_get_sampler_result_type(instr->texture->var->type);
1298 if (stype == GLSL_TYPE_UINT || stype == GLSL_TYPE_INT) {
1299 return lower_gather4_integer(&ctx->ac, args, instr);
1300 }
1301 }
1302 return ac_build_image_opcode(&ctx->ac, args);
1303 }
1304
1305 static LLVMValueRef visit_vulkan_resource_reindex(struct ac_nir_context *ctx,
1306 nir_intrinsic_instr *instr)
1307 {
1308 LLVMValueRef ptr = get_src(ctx, instr->src[0]);
1309 LLVMValueRef index = get_src(ctx, instr->src[1]);
1310
1311 LLVMValueRef result = LLVMBuildGEP(ctx->ac.builder, ptr, &index, 1, "");
1312 LLVMSetMetadata(result, ctx->ac.uniform_md_kind, ctx->ac.empty_md);
1313 return result;
1314 }
1315
1316 static LLVMValueRef visit_load_push_constant(struct ac_nir_context *ctx,
1317 nir_intrinsic_instr *instr)
1318 {
1319 LLVMValueRef ptr, addr;
1320
1321 addr = LLVMConstInt(ctx->ac.i32, nir_intrinsic_base(instr), 0);
1322 addr = LLVMBuildAdd(ctx->ac.builder, addr,
1323 get_src(ctx, instr->src[0]), "");
1324
1325 ptr = ac_build_gep0(&ctx->ac, ctx->abi->push_constants, addr);
1326 ptr = ac_cast_ptr(&ctx->ac, ptr, get_def_type(ctx, &instr->dest.ssa));
1327
1328 return LLVMBuildLoad(ctx->ac.builder, ptr, "");
1329 }
1330
1331 static LLVMValueRef visit_get_buffer_size(struct ac_nir_context *ctx,
1332 const nir_intrinsic_instr *instr)
1333 {
1334 LLVMValueRef index = get_src(ctx, instr->src[0]);
1335
1336 return get_buffer_size(ctx, ctx->abi->load_ssbo(ctx->abi, index, false), false);
1337 }
1338
1339 static uint32_t widen_mask(uint32_t mask, unsigned multiplier)
1340 {
1341 uint32_t new_mask = 0;
1342 for(unsigned i = 0; i < 32 && (1u << i) <= mask; ++i)
1343 if (mask & (1u << i))
1344 new_mask |= ((1u << multiplier) - 1u) << (i * multiplier);
1345 return new_mask;
1346 }
1347
1348 static LLVMValueRef extract_vector_range(struct ac_llvm_context *ctx, LLVMValueRef src,
1349 unsigned start, unsigned count)
1350 {
1351 LLVMTypeRef type = LLVMTypeOf(src);
1352
1353 if (LLVMGetTypeKind(type) != LLVMVectorTypeKind) {
1354 assert(start == 0);
1355 assert(count == 1);
1356 return src;
1357 }
1358
1359 unsigned src_elements = LLVMGetVectorSize(type);
1360 assert(start < src_elements);
1361 assert(start + count <= src_elements);
1362
1363 if (start == 0 && count == src_elements)
1364 return src;
1365
1366 if (count == 1)
1367 return LLVMBuildExtractElement(ctx->builder, src, LLVMConstInt(ctx->i32, start, false), "");
1368
1369 assert(count <= 8);
1370 LLVMValueRef indices[8];
1371 for (unsigned i = 0; i < count; ++i)
1372 indices[i] = LLVMConstInt(ctx->i32, start + i, false);
1373
1374 LLVMValueRef swizzle = LLVMConstVector(indices, count);
1375 return LLVMBuildShuffleVector(ctx->builder, src, src, swizzle, "");
1376 }
1377
1378 static void visit_store_ssbo(struct ac_nir_context *ctx,
1379 nir_intrinsic_instr *instr)
1380 {
1381 const char *store_name;
1382 LLVMValueRef src_data = get_src(ctx, instr->src[0]);
1383 LLVMTypeRef data_type = ctx->ac.f32;
1384 int elem_size_mult = ac_get_elem_bits(&ctx->ac, LLVMTypeOf(src_data)) / 32;
1385 int components_32bit = elem_size_mult * instr->num_components;
1386 unsigned writemask = nir_intrinsic_write_mask(instr);
1387 LLVMValueRef base_data, base_offset;
1388 LLVMValueRef params[6];
1389
1390 params[1] = ctx->abi->load_ssbo(ctx->abi,
1391 get_src(ctx, instr->src[1]), true);
1392 params[2] = ctx->ac.i32_0; /* vindex */
1393 params[4] = ctx->ac.i1false; /* glc */
1394 params[5] = ctx->ac.i1false; /* slc */
1395
1396 if (components_32bit > 1)
1397 data_type = LLVMVectorType(ctx->ac.f32, components_32bit);
1398
1399 writemask = widen_mask(writemask, elem_size_mult);
1400
1401 base_data = ac_to_float(&ctx->ac, src_data);
1402 base_data = ac_trim_vector(&ctx->ac, base_data, instr->num_components);
1403 base_data = LLVMBuildBitCast(ctx->ac.builder, base_data,
1404 data_type, "");
1405 base_offset = get_src(ctx, instr->src[2]); /* voffset */
1406 while (writemask) {
1407 int start, count;
1408 LLVMValueRef data;
1409 LLVMValueRef offset;
1410
1411 u_bit_scan_consecutive_range(&writemask, &start, &count);
1412
1413 /* Due to an LLVM limitation, split 3-element writes
1414 * into a 2-element and a 1-element write. */
1415 if (count == 3) {
1416 writemask |= 1 << (start + 2);
1417 count = 2;
1418 }
1419
1420 if (count > 4) {
1421 writemask |= ((1u << (count - 4)) - 1u) << (start + 4);
1422 count = 4;
1423 }
1424
1425 if (count == 4) {
1426 store_name = "llvm.amdgcn.buffer.store.v4f32";
1427 } else if (count == 2) {
1428 store_name = "llvm.amdgcn.buffer.store.v2f32";
1429
1430 } else {
1431 assert(count == 1);
1432 store_name = "llvm.amdgcn.buffer.store.f32";
1433 }
1434 data = extract_vector_range(&ctx->ac, base_data, start, count);
1435
1436 offset = base_offset;
1437 if (start != 0) {
1438 offset = LLVMBuildAdd(ctx->ac.builder, offset, LLVMConstInt(ctx->ac.i32, start * 4, false), "");
1439 }
1440 params[0] = data;
1441 params[3] = offset;
1442 ac_build_intrinsic(&ctx->ac, store_name,
1443 ctx->ac.voidt, params, 6, 0);
1444 }
1445 }
1446
1447 static LLVMValueRef visit_atomic_ssbo(struct ac_nir_context *ctx,
1448 const nir_intrinsic_instr *instr)
1449 {
1450 const char *name;
1451 LLVMValueRef params[6];
1452 int arg_count = 0;
1453
1454 if (instr->intrinsic == nir_intrinsic_ssbo_atomic_comp_swap) {
1455 params[arg_count++] = ac_llvm_extract_elem(&ctx->ac, get_src(ctx, instr->src[3]), 0);
1456 }
1457 params[arg_count++] = ac_llvm_extract_elem(&ctx->ac, get_src(ctx, instr->src[2]), 0);
1458 params[arg_count++] = ctx->abi->load_ssbo(ctx->abi,
1459 get_src(ctx, instr->src[0]),
1460 true);
1461 params[arg_count++] = ctx->ac.i32_0; /* vindex */
1462 params[arg_count++] = get_src(ctx, instr->src[1]); /* voffset */
1463 params[arg_count++] = LLVMConstInt(ctx->ac.i1, 0, false); /* slc */
1464
1465 switch (instr->intrinsic) {
1466 case nir_intrinsic_ssbo_atomic_add:
1467 name = "llvm.amdgcn.buffer.atomic.add";
1468 break;
1469 case nir_intrinsic_ssbo_atomic_imin:
1470 name = "llvm.amdgcn.buffer.atomic.smin";
1471 break;
1472 case nir_intrinsic_ssbo_atomic_umin:
1473 name = "llvm.amdgcn.buffer.atomic.umin";
1474 break;
1475 case nir_intrinsic_ssbo_atomic_imax:
1476 name = "llvm.amdgcn.buffer.atomic.smax";
1477 break;
1478 case nir_intrinsic_ssbo_atomic_umax:
1479 name = "llvm.amdgcn.buffer.atomic.umax";
1480 break;
1481 case nir_intrinsic_ssbo_atomic_and:
1482 name = "llvm.amdgcn.buffer.atomic.and";
1483 break;
1484 case nir_intrinsic_ssbo_atomic_or:
1485 name = "llvm.amdgcn.buffer.atomic.or";
1486 break;
1487 case nir_intrinsic_ssbo_atomic_xor:
1488 name = "llvm.amdgcn.buffer.atomic.xor";
1489 break;
1490 case nir_intrinsic_ssbo_atomic_exchange:
1491 name = "llvm.amdgcn.buffer.atomic.swap";
1492 break;
1493 case nir_intrinsic_ssbo_atomic_comp_swap:
1494 name = "llvm.amdgcn.buffer.atomic.cmpswap";
1495 break;
1496 default:
1497 abort();
1498 }
1499
1500 return ac_build_intrinsic(&ctx->ac, name, ctx->ac.i32, params, arg_count, 0);
1501 }
1502
1503 static LLVMValueRef visit_load_buffer(struct ac_nir_context *ctx,
1504 const nir_intrinsic_instr *instr)
1505 {
1506 LLVMValueRef results[2];
1507 int load_components;
1508 int num_components = instr->num_components;
1509 if (instr->dest.ssa.bit_size == 64)
1510 num_components *= 2;
1511
1512 for (int i = 0; i < num_components; i += load_components) {
1513 load_components = MIN2(num_components - i, 4);
1514 const char *load_name;
1515 LLVMTypeRef data_type = ctx->ac.f32;
1516 LLVMValueRef offset = LLVMConstInt(ctx->ac.i32, i * 4, false);
1517 offset = LLVMBuildAdd(ctx->ac.builder, get_src(ctx, instr->src[1]), offset, "");
1518
1519 if (load_components == 3)
1520 data_type = LLVMVectorType(ctx->ac.f32, 4);
1521 else if (load_components > 1)
1522 data_type = LLVMVectorType(ctx->ac.f32, load_components);
1523
1524 if (load_components >= 3)
1525 load_name = "llvm.amdgcn.buffer.load.v4f32";
1526 else if (load_components == 2)
1527 load_name = "llvm.amdgcn.buffer.load.v2f32";
1528 else if (load_components == 1)
1529 load_name = "llvm.amdgcn.buffer.load.f32";
1530 else
1531 unreachable("unhandled number of components");
1532
1533 LLVMValueRef params[] = {
1534 ctx->abi->load_ssbo(ctx->abi,
1535 get_src(ctx, instr->src[0]),
1536 false),
1537 ctx->ac.i32_0,
1538 offset,
1539 ctx->ac.i1false,
1540 ctx->ac.i1false,
1541 };
1542
1543 results[i > 0 ? 1 : 0] = ac_build_intrinsic(&ctx->ac, load_name, data_type, params, 5, 0);
1544 }
1545
1546 assume(results[0]);
1547 LLVMValueRef ret = results[0];
1548 if (num_components > 4 || num_components == 3) {
1549 LLVMValueRef masks[] = {
1550 LLVMConstInt(ctx->ac.i32, 0, false), LLVMConstInt(ctx->ac.i32, 1, false),
1551 LLVMConstInt(ctx->ac.i32, 2, false), LLVMConstInt(ctx->ac.i32, 3, false),
1552 LLVMConstInt(ctx->ac.i32, 4, false), LLVMConstInt(ctx->ac.i32, 5, false),
1553 LLVMConstInt(ctx->ac.i32, 6, false), LLVMConstInt(ctx->ac.i32, 7, false)
1554 };
1555
1556 LLVMValueRef swizzle = LLVMConstVector(masks, num_components);
1557 ret = LLVMBuildShuffleVector(ctx->ac.builder, results[0],
1558 results[num_components > 4 ? 1 : 0], swizzle, "");
1559 }
1560
1561 return LLVMBuildBitCast(ctx->ac.builder, ret,
1562 get_def_type(ctx, &instr->dest.ssa), "");
1563 }
1564
1565 static LLVMValueRef visit_load_ubo_buffer(struct ac_nir_context *ctx,
1566 const nir_intrinsic_instr *instr)
1567 {
1568 LLVMValueRef ret;
1569 LLVMValueRef rsrc = get_src(ctx, instr->src[0]);
1570 LLVMValueRef offset = get_src(ctx, instr->src[1]);
1571 int num_components = instr->num_components;
1572
1573 if (ctx->abi->load_ubo)
1574 rsrc = ctx->abi->load_ubo(ctx->abi, rsrc);
1575
1576 if (instr->dest.ssa.bit_size == 64)
1577 num_components *= 2;
1578
1579 ret = ac_build_buffer_load(&ctx->ac, rsrc, num_components, NULL, offset,
1580 NULL, 0, false, false, true, true);
1581 ret = ac_trim_vector(&ctx->ac, ret, num_components);
1582 return LLVMBuildBitCast(ctx->ac.builder, ret,
1583 get_def_type(ctx, &instr->dest.ssa), "");
1584 }
1585
1586 static void
1587 get_deref_offset(struct ac_nir_context *ctx, nir_deref_var *deref,
1588 bool vs_in, unsigned *vertex_index_out,
1589 LLVMValueRef *vertex_index_ref,
1590 unsigned *const_out, LLVMValueRef *indir_out)
1591 {
1592 unsigned const_offset = 0;
1593 nir_deref *tail = &deref->deref;
1594 LLVMValueRef offset = NULL;
1595
1596 if (vertex_index_out != NULL || vertex_index_ref != NULL) {
1597 tail = tail->child;
1598 nir_deref_array *deref_array = nir_deref_as_array(tail);
1599 if (vertex_index_out)
1600 *vertex_index_out = deref_array->base_offset;
1601
1602 if (vertex_index_ref) {
1603 LLVMValueRef vtx = LLVMConstInt(ctx->ac.i32, deref_array->base_offset, false);
1604 if (deref_array->deref_array_type == nir_deref_array_type_indirect) {
1605 vtx = LLVMBuildAdd(ctx->ac.builder, vtx, get_src(ctx, deref_array->indirect), "");
1606 }
1607 *vertex_index_ref = vtx;
1608 }
1609 }
1610
1611 if (deref->var->data.compact) {
1612 assert(tail->child->deref_type == nir_deref_type_array);
1613 assert(glsl_type_is_scalar(glsl_without_array(deref->var->type)));
1614 nir_deref_array *deref_array = nir_deref_as_array(tail->child);
1615 /* We always lower indirect dereferences for "compact" array vars. */
1616 assert(deref_array->deref_array_type == nir_deref_array_type_direct);
1617
1618 const_offset = deref_array->base_offset;
1619 goto out;
1620 }
1621
1622 while (tail->child != NULL) {
1623 const struct glsl_type *parent_type = tail->type;
1624 tail = tail->child;
1625
1626 if (tail->deref_type == nir_deref_type_array) {
1627 nir_deref_array *deref_array = nir_deref_as_array(tail);
1628 LLVMValueRef index, stride, local_offset;
1629 unsigned size = glsl_count_attribute_slots(tail->type, vs_in);
1630
1631 const_offset += size * deref_array->base_offset;
1632 if (deref_array->deref_array_type == nir_deref_array_type_direct)
1633 continue;
1634
1635 assert(deref_array->deref_array_type == nir_deref_array_type_indirect);
1636 index = get_src(ctx, deref_array->indirect);
1637 stride = LLVMConstInt(ctx->ac.i32, size, 0);
1638 local_offset = LLVMBuildMul(ctx->ac.builder, stride, index, "");
1639
1640 if (offset)
1641 offset = LLVMBuildAdd(ctx->ac.builder, offset, local_offset, "");
1642 else
1643 offset = local_offset;
1644 } else if (tail->deref_type == nir_deref_type_struct) {
1645 nir_deref_struct *deref_struct = nir_deref_as_struct(tail);
1646
1647 for (unsigned i = 0; i < deref_struct->index; i++) {
1648 const struct glsl_type *ft = glsl_get_struct_field(parent_type, i);
1649 const_offset += glsl_count_attribute_slots(ft, vs_in);
1650 }
1651 } else
1652 unreachable("unsupported deref type");
1653
1654 }
1655 out:
1656 if (const_offset && offset)
1657 offset = LLVMBuildAdd(ctx->ac.builder, offset,
1658 LLVMConstInt(ctx->ac.i32, const_offset, 0),
1659 "");
1660
1661 *const_out = const_offset;
1662 *indir_out = offset;
1663 }
1664
1665 static LLVMValueRef
1666 build_gep_for_deref(struct ac_nir_context *ctx,
1667 nir_deref_var *deref)
1668 {
1669 struct hash_entry *entry = _mesa_hash_table_search(ctx->vars, deref->var);
1670 assert(entry->data);
1671 LLVMValueRef val = entry->data;
1672 nir_deref *tail = deref->deref.child;
1673 while (tail != NULL) {
1674 LLVMValueRef offset;
1675 switch (tail->deref_type) {
1676 case nir_deref_type_array: {
1677 nir_deref_array *array = nir_deref_as_array(tail);
1678 offset = LLVMConstInt(ctx->ac.i32, array->base_offset, 0);
1679 if (array->deref_array_type ==
1680 nir_deref_array_type_indirect) {
1681 offset = LLVMBuildAdd(ctx->ac.builder, offset,
1682 get_src(ctx,
1683 array->indirect),
1684 "");
1685 }
1686 break;
1687 }
1688 case nir_deref_type_struct: {
1689 nir_deref_struct *deref_struct =
1690 nir_deref_as_struct(tail);
1691 offset = LLVMConstInt(ctx->ac.i32,
1692 deref_struct->index, 0);
1693 break;
1694 }
1695 default:
1696 unreachable("bad deref type");
1697 }
1698 val = ac_build_gep0(&ctx->ac, val, offset);
1699 tail = tail->child;
1700 }
1701 return val;
1702 }
1703
1704 static LLVMValueRef load_tess_varyings(struct ac_nir_context *ctx,
1705 nir_intrinsic_instr *instr,
1706 bool load_inputs)
1707 {
1708 LLVMValueRef result;
1709 LLVMValueRef vertex_index = NULL;
1710 LLVMValueRef indir_index = NULL;
1711 unsigned const_index = 0;
1712 unsigned location = instr->variables[0]->var->data.location;
1713 unsigned driver_location = instr->variables[0]->var->data.driver_location;
1714 const bool is_patch = instr->variables[0]->var->data.patch;
1715 const bool is_compact = instr->variables[0]->var->data.compact;
1716
1717 get_deref_offset(ctx, instr->variables[0],
1718 false, NULL, is_patch ? NULL : &vertex_index,
1719 &const_index, &indir_index);
1720
1721 LLVMTypeRef dest_type = get_def_type(ctx, &instr->dest.ssa);
1722
1723 LLVMTypeRef src_component_type;
1724 if (LLVMGetTypeKind(dest_type) == LLVMVectorTypeKind)
1725 src_component_type = LLVMGetElementType(dest_type);
1726 else
1727 src_component_type = dest_type;
1728
1729 result = ctx->abi->load_tess_varyings(ctx->abi, src_component_type,
1730 vertex_index, indir_index,
1731 const_index, location, driver_location,
1732 instr->variables[0]->var->data.location_frac,
1733 instr->num_components,
1734 is_patch, is_compact, load_inputs);
1735 return LLVMBuildBitCast(ctx->ac.builder, result, dest_type, "");
1736 }
1737
1738 static LLVMValueRef visit_load_var(struct ac_nir_context *ctx,
1739 nir_intrinsic_instr *instr)
1740 {
1741 LLVMValueRef values[8];
1742 int idx = instr->variables[0]->var->data.driver_location;
1743 int ve = instr->dest.ssa.num_components;
1744 unsigned comp = instr->variables[0]->var->data.location_frac;
1745 LLVMValueRef indir_index;
1746 LLVMValueRef ret;
1747 unsigned const_index;
1748 unsigned stride = instr->variables[0]->var->data.compact ? 1 : 4;
1749 bool vs_in = ctx->stage == MESA_SHADER_VERTEX &&
1750 instr->variables[0]->var->data.mode == nir_var_shader_in;
1751 get_deref_offset(ctx, instr->variables[0], vs_in, NULL, NULL,
1752 &const_index, &indir_index);
1753
1754 if (instr->dest.ssa.bit_size == 64)
1755 ve *= 2;
1756
1757 switch (instr->variables[0]->var->data.mode) {
1758 case nir_var_shader_in:
1759 if (ctx->stage == MESA_SHADER_TESS_CTRL ||
1760 ctx->stage == MESA_SHADER_TESS_EVAL) {
1761 return load_tess_varyings(ctx, instr, true);
1762 }
1763
1764 if (ctx->stage == MESA_SHADER_GEOMETRY) {
1765 LLVMTypeRef type = LLVMIntTypeInContext(ctx->ac.context, instr->dest.ssa.bit_size);
1766 LLVMValueRef indir_index;
1767 unsigned const_index, vertex_index;
1768 get_deref_offset(ctx, instr->variables[0],
1769 false, &vertex_index, NULL,
1770 &const_index, &indir_index);
1771
1772 return ctx->abi->load_inputs(ctx->abi, instr->variables[0]->var->data.location,
1773 instr->variables[0]->var->data.driver_location,
1774 instr->variables[0]->var->data.location_frac,
1775 instr->num_components, vertex_index, const_index, type);
1776 }
1777
1778 for (unsigned chan = comp; chan < ve + comp; chan++) {
1779 if (indir_index) {
1780 unsigned count = glsl_count_attribute_slots(
1781 instr->variables[0]->var->type,
1782 ctx->stage == MESA_SHADER_VERTEX);
1783 count -= chan / 4;
1784 LLVMValueRef tmp_vec = ac_build_gather_values_extended(
1785 &ctx->ac, ctx->abi->inputs + idx + chan, count,
1786 stride, false, true);
1787
1788 values[chan] = LLVMBuildExtractElement(ctx->ac.builder,
1789 tmp_vec,
1790 indir_index, "");
1791 } else
1792 values[chan] = ctx->abi->inputs[idx + chan + const_index * stride];
1793 }
1794 break;
1795 case nir_var_local:
1796 for (unsigned chan = 0; chan < ve; chan++) {
1797 if (indir_index) {
1798 unsigned count = glsl_count_attribute_slots(
1799 instr->variables[0]->var->type, false);
1800 count -= chan / 4;
1801 LLVMValueRef tmp_vec = ac_build_gather_values_extended(
1802 &ctx->ac, ctx->locals + idx + chan, count,
1803 stride, true, true);
1804
1805 values[chan] = LLVMBuildExtractElement(ctx->ac.builder,
1806 tmp_vec,
1807 indir_index, "");
1808 } else {
1809 values[chan] = LLVMBuildLoad(ctx->ac.builder, ctx->locals[idx + chan + const_index * stride], "");
1810 }
1811 }
1812 break;
1813 case nir_var_shared: {
1814 LLVMValueRef address = build_gep_for_deref(ctx,
1815 instr->variables[0]);
1816 LLVMValueRef val = LLVMBuildLoad(ctx->ac.builder, address, "");
1817 return LLVMBuildBitCast(ctx->ac.builder, val,
1818 get_def_type(ctx, &instr->dest.ssa),
1819 "");
1820 }
1821 case nir_var_shader_out:
1822 if (ctx->stage == MESA_SHADER_TESS_CTRL) {
1823 return load_tess_varyings(ctx, instr, false);
1824 }
1825
1826 for (unsigned chan = comp; chan < ve + comp; chan++) {
1827 if (indir_index) {
1828 unsigned count = glsl_count_attribute_slots(
1829 instr->variables[0]->var->type, false);
1830 count -= chan / 4;
1831 LLVMValueRef tmp_vec = ac_build_gather_values_extended(
1832 &ctx->ac, ctx->abi->outputs + idx + chan, count,
1833 stride, true, true);
1834
1835 values[chan] = LLVMBuildExtractElement(ctx->ac.builder,
1836 tmp_vec,
1837 indir_index, "");
1838 } else {
1839 values[chan] = LLVMBuildLoad(ctx->ac.builder,
1840 ctx->abi->outputs[idx + chan + const_index * stride],
1841 "");
1842 }
1843 }
1844 break;
1845 default:
1846 unreachable("unhandle variable mode");
1847 }
1848 ret = ac_build_varying_gather_values(&ctx->ac, values, ve, comp);
1849 return LLVMBuildBitCast(ctx->ac.builder, ret, get_def_type(ctx, &instr->dest.ssa), "");
1850 }
1851
1852 static void
1853 visit_store_var(struct ac_nir_context *ctx,
1854 nir_intrinsic_instr *instr)
1855 {
1856 LLVMValueRef temp_ptr, value;
1857 int idx = instr->variables[0]->var->data.driver_location;
1858 unsigned comp = instr->variables[0]->var->data.location_frac;
1859 LLVMValueRef src = ac_to_float(&ctx->ac, get_src(ctx, instr->src[0]));
1860 int writemask = instr->const_index[0];
1861 LLVMValueRef indir_index;
1862 unsigned const_index;
1863 get_deref_offset(ctx, instr->variables[0], false,
1864 NULL, NULL, &const_index, &indir_index);
1865
1866 if (ac_get_elem_bits(&ctx->ac, LLVMTypeOf(src)) == 64) {
1867
1868 src = LLVMBuildBitCast(ctx->ac.builder, src,
1869 LLVMVectorType(ctx->ac.f32, ac_get_llvm_num_components(src) * 2),
1870 "");
1871
1872 writemask = widen_mask(writemask, 2);
1873 }
1874
1875 writemask = writemask << comp;
1876
1877 switch (instr->variables[0]->var->data.mode) {
1878 case nir_var_shader_out:
1879
1880 if (ctx->stage == MESA_SHADER_TESS_CTRL) {
1881 LLVMValueRef vertex_index = NULL;
1882 LLVMValueRef indir_index = NULL;
1883 unsigned const_index = 0;
1884 const bool is_patch = instr->variables[0]->var->data.patch;
1885
1886 get_deref_offset(ctx, instr->variables[0],
1887 false, NULL, is_patch ? NULL : &vertex_index,
1888 &const_index, &indir_index);
1889
1890 ctx->abi->store_tcs_outputs(ctx->abi, instr->variables[0]->var,
1891 vertex_index, indir_index,
1892 const_index, src, writemask);
1893 return;
1894 }
1895
1896 for (unsigned chan = 0; chan < 8; chan++) {
1897 int stride = 4;
1898 if (!(writemask & (1 << chan)))
1899 continue;
1900
1901 value = ac_llvm_extract_elem(&ctx->ac, src, chan - comp);
1902
1903 if (instr->variables[0]->var->data.compact)
1904 stride = 1;
1905 if (indir_index) {
1906 unsigned count = glsl_count_attribute_slots(
1907 instr->variables[0]->var->type, false);
1908 count -= chan / 4;
1909 LLVMValueRef tmp_vec = ac_build_gather_values_extended(
1910 &ctx->ac, ctx->abi->outputs + idx + chan, count,
1911 stride, true, true);
1912
1913 tmp_vec = LLVMBuildInsertElement(ctx->ac.builder, tmp_vec,
1914 value, indir_index, "");
1915 build_store_values_extended(&ctx->ac, ctx->abi->outputs + idx + chan,
1916 count, stride, tmp_vec);
1917
1918 } else {
1919 temp_ptr = ctx->abi->outputs[idx + chan + const_index * stride];
1920
1921 LLVMBuildStore(ctx->ac.builder, value, temp_ptr);
1922 }
1923 }
1924 break;
1925 case nir_var_local:
1926 for (unsigned chan = 0; chan < 8; chan++) {
1927 if (!(writemask & (1 << chan)))
1928 continue;
1929
1930 value = ac_llvm_extract_elem(&ctx->ac, src, chan);
1931 if (indir_index) {
1932 unsigned count = glsl_count_attribute_slots(
1933 instr->variables[0]->var->type, false);
1934 count -= chan / 4;
1935 LLVMValueRef tmp_vec = ac_build_gather_values_extended(
1936 &ctx->ac, ctx->locals + idx + chan, count,
1937 4, true, true);
1938
1939 tmp_vec = LLVMBuildInsertElement(ctx->ac.builder, tmp_vec,
1940 value, indir_index, "");
1941 build_store_values_extended(&ctx->ac, ctx->locals + idx + chan,
1942 count, 4, tmp_vec);
1943 } else {
1944 temp_ptr = ctx->locals[idx + chan + const_index * 4];
1945
1946 LLVMBuildStore(ctx->ac.builder, value, temp_ptr);
1947 }
1948 }
1949 break;
1950 case nir_var_shared: {
1951 int writemask = instr->const_index[0];
1952 LLVMValueRef address = build_gep_for_deref(ctx,
1953 instr->variables[0]);
1954 LLVMValueRef val = get_src(ctx, instr->src[0]);
1955 unsigned components =
1956 glsl_get_vector_elements(
1957 nir_deref_tail(&instr->variables[0]->deref)->type);
1958 if (writemask == (1 << components) - 1) {
1959 val = LLVMBuildBitCast(
1960 ctx->ac.builder, val,
1961 LLVMGetElementType(LLVMTypeOf(address)), "");
1962 LLVMBuildStore(ctx->ac.builder, val, address);
1963 } else {
1964 for (unsigned chan = 0; chan < 4; chan++) {
1965 if (!(writemask & (1 << chan)))
1966 continue;
1967 LLVMValueRef ptr =
1968 LLVMBuildStructGEP(ctx->ac.builder,
1969 address, chan, "");
1970 LLVMValueRef src = ac_llvm_extract_elem(&ctx->ac, val,
1971 chan);
1972 src = LLVMBuildBitCast(
1973 ctx->ac.builder, src,
1974 LLVMGetElementType(LLVMTypeOf(ptr)), "");
1975 LLVMBuildStore(ctx->ac.builder, src, ptr);
1976 }
1977 }
1978 break;
1979 }
1980 default:
1981 break;
1982 }
1983 }
1984
1985 static int image_type_to_components_count(enum glsl_sampler_dim dim, bool array)
1986 {
1987 switch (dim) {
1988 case GLSL_SAMPLER_DIM_BUF:
1989 return 1;
1990 case GLSL_SAMPLER_DIM_1D:
1991 return array ? 2 : 1;
1992 case GLSL_SAMPLER_DIM_2D:
1993 return array ? 3 : 2;
1994 case GLSL_SAMPLER_DIM_MS:
1995 return array ? 4 : 3;
1996 case GLSL_SAMPLER_DIM_3D:
1997 case GLSL_SAMPLER_DIM_CUBE:
1998 return 3;
1999 case GLSL_SAMPLER_DIM_RECT:
2000 case GLSL_SAMPLER_DIM_SUBPASS:
2001 return 2;
2002 case GLSL_SAMPLER_DIM_SUBPASS_MS:
2003 return 3;
2004 default:
2005 break;
2006 }
2007 return 0;
2008 }
2009
2010 static bool
2011 glsl_is_array_image(const struct glsl_type *type)
2012 {
2013 const enum glsl_sampler_dim dim = glsl_get_sampler_dim(type);
2014
2015 if (glsl_sampler_type_is_array(type))
2016 return true;
2017
2018 return dim == GLSL_SAMPLER_DIM_CUBE ||
2019 dim == GLSL_SAMPLER_DIM_3D ||
2020 dim == GLSL_SAMPLER_DIM_SUBPASS ||
2021 dim == GLSL_SAMPLER_DIM_SUBPASS_MS;
2022 }
2023
2024
2025 /* Adjust the sample index according to FMASK.
2026 *
2027 * For uncompressed MSAA surfaces, FMASK should return 0x76543210,
2028 * which is the identity mapping. Each nibble says which physical sample
2029 * should be fetched to get that sample.
2030 *
2031 * For example, 0x11111100 means there are only 2 samples stored and
2032 * the second sample covers 3/4 of the pixel. When reading samples 0
2033 * and 1, return physical sample 0 (determined by the first two 0s
2034 * in FMASK), otherwise return physical sample 1.
2035 *
2036 * The sample index should be adjusted as follows:
2037 * sample_index = (fmask >> (sample_index * 4)) & 0xF;
2038 */
2039 static LLVMValueRef adjust_sample_index_using_fmask(struct ac_llvm_context *ctx,
2040 LLVMValueRef coord_x, LLVMValueRef coord_y,
2041 LLVMValueRef coord_z,
2042 LLVMValueRef sample_index,
2043 LLVMValueRef fmask_desc_ptr)
2044 {
2045 LLVMValueRef fmask_load_address[4];
2046 LLVMValueRef res;
2047
2048 fmask_load_address[0] = coord_x;
2049 fmask_load_address[1] = coord_y;
2050 if (coord_z) {
2051 fmask_load_address[2] = coord_z;
2052 fmask_load_address[3] = LLVMGetUndef(ctx->i32);
2053 }
2054
2055 struct ac_image_args args = {0};
2056
2057 args.opcode = ac_image_load;
2058 args.da = coord_z ? true : false;
2059 args.resource = fmask_desc_ptr;
2060 args.dmask = 0xf;
2061 args.addr = ac_build_gather_values(ctx, fmask_load_address, coord_z ? 4 : 2);
2062
2063 res = ac_build_image_opcode(ctx, &args);
2064
2065 res = ac_to_integer(ctx, res);
2066 LLVMValueRef four = LLVMConstInt(ctx->i32, 4, false);
2067 LLVMValueRef F = LLVMConstInt(ctx->i32, 0xf, false);
2068
2069 LLVMValueRef fmask = LLVMBuildExtractElement(ctx->builder,
2070 res,
2071 ctx->i32_0, "");
2072
2073 LLVMValueRef sample_index4 =
2074 LLVMBuildMul(ctx->builder, sample_index, four, "");
2075 LLVMValueRef shifted_fmask =
2076 LLVMBuildLShr(ctx->builder, fmask, sample_index4, "");
2077 LLVMValueRef final_sample =
2078 LLVMBuildAnd(ctx->builder, shifted_fmask, F, "");
2079
2080 /* Don't rewrite the sample index if WORD1.DATA_FORMAT of the FMASK
2081 * resource descriptor is 0 (invalid),
2082 */
2083 LLVMValueRef fmask_desc =
2084 LLVMBuildBitCast(ctx->builder, fmask_desc_ptr,
2085 ctx->v8i32, "");
2086
2087 LLVMValueRef fmask_word1 =
2088 LLVMBuildExtractElement(ctx->builder, fmask_desc,
2089 ctx->i32_1, "");
2090
2091 LLVMValueRef word1_is_nonzero =
2092 LLVMBuildICmp(ctx->builder, LLVMIntNE,
2093 fmask_word1, ctx->i32_0, "");
2094
2095 /* Replace the MSAA sample index. */
2096 sample_index =
2097 LLVMBuildSelect(ctx->builder, word1_is_nonzero,
2098 final_sample, sample_index, "");
2099 return sample_index;
2100 }
2101
2102 static LLVMValueRef get_image_coords(struct ac_nir_context *ctx,
2103 const nir_intrinsic_instr *instr)
2104 {
2105 const struct glsl_type *type = glsl_without_array(instr->variables[0]->var->type);
2106
2107 LLVMValueRef src0 = get_src(ctx, instr->src[0]);
2108 LLVMValueRef coords[4];
2109 LLVMValueRef masks[] = {
2110 LLVMConstInt(ctx->ac.i32, 0, false), LLVMConstInt(ctx->ac.i32, 1, false),
2111 LLVMConstInt(ctx->ac.i32, 2, false), LLVMConstInt(ctx->ac.i32, 3, false),
2112 };
2113 LLVMValueRef res;
2114 LLVMValueRef sample_index = ac_llvm_extract_elem(&ctx->ac, get_src(ctx, instr->src[1]), 0);
2115
2116 int count;
2117 enum glsl_sampler_dim dim = glsl_get_sampler_dim(type);
2118 bool is_array = glsl_sampler_type_is_array(type);
2119 bool add_frag_pos = (dim == GLSL_SAMPLER_DIM_SUBPASS ||
2120 dim == GLSL_SAMPLER_DIM_SUBPASS_MS);
2121 bool is_ms = (dim == GLSL_SAMPLER_DIM_MS ||
2122 dim == GLSL_SAMPLER_DIM_SUBPASS_MS);
2123 bool gfx9_1d = ctx->ac.chip_class >= GFX9 && dim == GLSL_SAMPLER_DIM_1D;
2124 count = image_type_to_components_count(dim, is_array);
2125
2126 if (is_ms) {
2127 LLVMValueRef fmask_load_address[3];
2128 int chan;
2129
2130 fmask_load_address[0] = LLVMBuildExtractElement(ctx->ac.builder, src0, masks[0], "");
2131 fmask_load_address[1] = LLVMBuildExtractElement(ctx->ac.builder, src0, masks[1], "");
2132 if (is_array)
2133 fmask_load_address[2] = LLVMBuildExtractElement(ctx->ac.builder, src0, masks[2], "");
2134 else
2135 fmask_load_address[2] = NULL;
2136 if (add_frag_pos) {
2137 for (chan = 0; chan < 2; ++chan)
2138 fmask_load_address[chan] =
2139 LLVMBuildAdd(ctx->ac.builder, fmask_load_address[chan],
2140 LLVMBuildFPToUI(ctx->ac.builder, ctx->abi->frag_pos[chan],
2141 ctx->ac.i32, ""), "");
2142 fmask_load_address[2] = ac_to_integer(&ctx->ac, ctx->abi->inputs[ac_llvm_reg_index_soa(VARYING_SLOT_LAYER, 0)]);
2143 }
2144 sample_index = adjust_sample_index_using_fmask(&ctx->ac,
2145 fmask_load_address[0],
2146 fmask_load_address[1],
2147 fmask_load_address[2],
2148 sample_index,
2149 get_sampler_desc(ctx, instr->variables[0], AC_DESC_FMASK, NULL, true, false));
2150 }
2151 if (count == 1 && !gfx9_1d) {
2152 if (instr->src[0].ssa->num_components)
2153 res = LLVMBuildExtractElement(ctx->ac.builder, src0, masks[0], "");
2154 else
2155 res = src0;
2156 } else {
2157 int chan;
2158 if (is_ms)
2159 count--;
2160 for (chan = 0; chan < count; ++chan) {
2161 coords[chan] = ac_llvm_extract_elem(&ctx->ac, src0, chan);
2162 }
2163 if (add_frag_pos) {
2164 for (chan = 0; chan < 2; ++chan)
2165 coords[chan] = LLVMBuildAdd(ctx->ac.builder, coords[chan], LLVMBuildFPToUI(ctx->ac.builder, ctx->abi->frag_pos[chan],
2166 ctx->ac.i32, ""), "");
2167 coords[2] = ac_to_integer(&ctx->ac, ctx->abi->inputs[ac_llvm_reg_index_soa(VARYING_SLOT_LAYER, 0)]);
2168 count++;
2169 }
2170
2171 if (gfx9_1d) {
2172 if (is_array) {
2173 coords[2] = coords[1];
2174 coords[1] = ctx->ac.i32_0;
2175 } else
2176 coords[1] = ctx->ac.i32_0;
2177 count++;
2178 }
2179
2180 if (is_ms) {
2181 coords[count] = sample_index;
2182 count++;
2183 }
2184
2185 if (count == 3) {
2186 coords[3] = LLVMGetUndef(ctx->ac.i32);
2187 count = 4;
2188 }
2189 res = ac_build_gather_values(&ctx->ac, coords, count);
2190 }
2191 return res;
2192 }
2193
2194 static LLVMValueRef visit_image_load(struct ac_nir_context *ctx,
2195 const nir_intrinsic_instr *instr)
2196 {
2197 LLVMValueRef params[7];
2198 LLVMValueRef res;
2199 char intrinsic_name[64];
2200 const nir_variable *var = instr->variables[0]->var;
2201 const struct glsl_type *type = var->type;
2202
2203 if(instr->variables[0]->deref.child)
2204 type = instr->variables[0]->deref.child->type;
2205
2206 type = glsl_without_array(type);
2207
2208 const enum glsl_sampler_dim dim = glsl_get_sampler_dim(type);
2209 if (dim == GLSL_SAMPLER_DIM_BUF) {
2210 unsigned mask = nir_ssa_def_components_read(&instr->dest.ssa);
2211 unsigned num_channels = util_last_bit(mask);
2212 LLVMValueRef rsrc, vindex;
2213
2214 rsrc = get_sampler_desc(ctx, instr->variables[0], AC_DESC_BUFFER, NULL, true, false);
2215 vindex = LLVMBuildExtractElement(ctx->ac.builder, get_src(ctx, instr->src[0]),
2216 ctx->ac.i32_0, "");
2217
2218 /* TODO: set "glc" and "can_speculate" when OpenGL needs it. */
2219 res = ac_build_buffer_load_format(&ctx->ac, rsrc, vindex,
2220 ctx->ac.i32_0, num_channels,
2221 false, false);
2222 res = ac_build_expand_to_vec4(&ctx->ac, res, num_channels);
2223
2224 res = ac_trim_vector(&ctx->ac, res, instr->dest.ssa.num_components);
2225 res = ac_to_integer(&ctx->ac, res);
2226 } else {
2227 LLVMValueRef da = glsl_is_array_image(type) ? ctx->ac.i1true : ctx->ac.i1false;
2228 LLVMValueRef slc = ctx->ac.i1false;
2229
2230 params[0] = get_image_coords(ctx, instr);
2231 params[1] = get_sampler_desc(ctx, instr->variables[0], AC_DESC_IMAGE, NULL, true, false);
2232 params[2] = LLVMConstInt(ctx->ac.i32, 15, false); /* dmask */
2233 params[3] = (var->data.image._volatile || var->data.image.coherent) ?
2234 ctx->ac.i1true : ctx->ac.i1false;
2235 params[4] = slc;
2236 params[5] = ctx->ac.i1false;
2237 params[6] = da;
2238
2239 ac_get_image_intr_name("llvm.amdgcn.image.load",
2240 ctx->ac.v4f32, /* vdata */
2241 LLVMTypeOf(params[0]), /* coords */
2242 LLVMTypeOf(params[1]), /* rsrc */
2243 intrinsic_name, sizeof(intrinsic_name));
2244
2245 res = ac_build_intrinsic(&ctx->ac, intrinsic_name, ctx->ac.v4f32,
2246 params, 7, AC_FUNC_ATTR_READONLY);
2247 }
2248 return ac_to_integer(&ctx->ac, res);
2249 }
2250
2251 static void visit_image_store(struct ac_nir_context *ctx,
2252 nir_intrinsic_instr *instr)
2253 {
2254 LLVMValueRef params[8];
2255 char intrinsic_name[64];
2256 const nir_variable *var = instr->variables[0]->var;
2257 const struct glsl_type *type = glsl_without_array(var->type);
2258 const enum glsl_sampler_dim dim = glsl_get_sampler_dim(type);
2259 LLVMValueRef glc = ctx->ac.i1false;
2260 bool force_glc = ctx->ac.chip_class == SI;
2261 if (force_glc)
2262 glc = ctx->ac.i1true;
2263
2264 if (dim == GLSL_SAMPLER_DIM_BUF) {
2265 LLVMValueRef rsrc = get_sampler_desc(ctx, instr->variables[0], AC_DESC_BUFFER, NULL, true, true);
2266
2267 if (ctx->abi->gfx9_stride_size_workaround) {
2268 LLVMValueRef elem_count = LLVMBuildExtractElement(ctx->ac.builder, rsrc, LLVMConstInt(ctx->ac.i32, 2, 0), "");
2269 LLVMValueRef stride = LLVMBuildExtractElement(ctx->ac.builder, rsrc, LLVMConstInt(ctx->ac.i32, 1, 0), "");
2270 stride = LLVMBuildLShr(ctx->ac.builder, stride, LLVMConstInt(ctx->ac.i32, 16, 0), "");
2271
2272 LLVMValueRef new_elem_count = LLVMBuildSelect(ctx->ac.builder,
2273 LLVMBuildICmp(ctx->ac.builder, LLVMIntUGT, elem_count, stride, ""),
2274 elem_count, stride, "");
2275
2276 rsrc = LLVMBuildInsertElement(ctx->ac.builder, rsrc, new_elem_count,
2277 LLVMConstInt(ctx->ac.i32, 2, 0), "");
2278 }
2279
2280 params[0] = ac_to_float(&ctx->ac, get_src(ctx, instr->src[2])); /* data */
2281 params[1] = rsrc;
2282 params[2] = LLVMBuildExtractElement(ctx->ac.builder, get_src(ctx, instr->src[0]),
2283 ctx->ac.i32_0, ""); /* vindex */
2284 params[3] = ctx->ac.i32_0; /* voffset */
2285 params[4] = glc; /* glc */
2286 params[5] = ctx->ac.i1false; /* slc */
2287 ac_build_intrinsic(&ctx->ac, "llvm.amdgcn.buffer.store.format.v4f32", ctx->ac.voidt,
2288 params, 6, 0);
2289 } else {
2290 LLVMValueRef da = glsl_is_array_image(type) ? ctx->ac.i1true : ctx->ac.i1false;
2291 LLVMValueRef slc = ctx->ac.i1false;
2292
2293 params[0] = ac_to_float(&ctx->ac, get_src(ctx, instr->src[2]));
2294 params[1] = get_image_coords(ctx, instr); /* coords */
2295 params[2] = get_sampler_desc(ctx, instr->variables[0], AC_DESC_IMAGE, NULL, true, true);
2296 params[3] = LLVMConstInt(ctx->ac.i32, 15, false); /* dmask */
2297 params[4] = (force_glc || var->data.image._volatile || var->data.image.coherent) ?
2298 ctx->ac.i1true : ctx->ac.i1false;
2299 params[5] = slc;
2300 params[6] = ctx->ac.i1false;
2301 params[7] = da;
2302
2303 ac_get_image_intr_name("llvm.amdgcn.image.store",
2304 LLVMTypeOf(params[0]), /* vdata */
2305 LLVMTypeOf(params[1]), /* coords */
2306 LLVMTypeOf(params[2]), /* rsrc */
2307 intrinsic_name, sizeof(intrinsic_name));
2308
2309 ac_build_intrinsic(&ctx->ac, intrinsic_name, ctx->ac.voidt,
2310 params, 8, 0);
2311 }
2312
2313 }
2314
2315 static LLVMValueRef visit_image_atomic(struct ac_nir_context *ctx,
2316 const nir_intrinsic_instr *instr)
2317 {
2318 LLVMValueRef params[7];
2319 int param_count = 0;
2320 const nir_variable *var = instr->variables[0]->var;
2321
2322 const char *atomic_name;
2323 char intrinsic_name[41];
2324 const struct glsl_type *type = glsl_without_array(var->type);
2325 MAYBE_UNUSED int length;
2326
2327 bool is_unsigned = glsl_get_sampler_result_type(type) == GLSL_TYPE_UINT;
2328
2329 switch (instr->intrinsic) {
2330 case nir_intrinsic_image_var_atomic_add:
2331 atomic_name = "add";
2332 break;
2333 case nir_intrinsic_image_var_atomic_min:
2334 atomic_name = is_unsigned ? "umin" : "smin";
2335 break;
2336 case nir_intrinsic_image_var_atomic_max:
2337 atomic_name = is_unsigned ? "umax" : "smax";
2338 break;
2339 case nir_intrinsic_image_var_atomic_and:
2340 atomic_name = "and";
2341 break;
2342 case nir_intrinsic_image_var_atomic_or:
2343 atomic_name = "or";
2344 break;
2345 case nir_intrinsic_image_var_atomic_xor:
2346 atomic_name = "xor";
2347 break;
2348 case nir_intrinsic_image_var_atomic_exchange:
2349 atomic_name = "swap";
2350 break;
2351 case nir_intrinsic_image_var_atomic_comp_swap:
2352 atomic_name = "cmpswap";
2353 break;
2354 default:
2355 abort();
2356 }
2357
2358 if (instr->intrinsic == nir_intrinsic_image_var_atomic_comp_swap)
2359 params[param_count++] = get_src(ctx, instr->src[3]);
2360 params[param_count++] = get_src(ctx, instr->src[2]);
2361
2362 if (glsl_get_sampler_dim(type) == GLSL_SAMPLER_DIM_BUF) {
2363 params[param_count++] = get_sampler_desc(ctx, instr->variables[0], AC_DESC_BUFFER,
2364 NULL, true, true);
2365 params[param_count++] = LLVMBuildExtractElement(ctx->ac.builder, get_src(ctx, instr->src[0]),
2366 ctx->ac.i32_0, ""); /* vindex */
2367 params[param_count++] = ctx->ac.i32_0; /* voffset */
2368 params[param_count++] = ctx->ac.i1false; /* slc */
2369
2370 length = snprintf(intrinsic_name, sizeof(intrinsic_name),
2371 "llvm.amdgcn.buffer.atomic.%s", atomic_name);
2372 } else {
2373 char coords_type[8];
2374
2375 LLVMValueRef coords = params[param_count++] = get_image_coords(ctx, instr);
2376 params[param_count++] = get_sampler_desc(ctx, instr->variables[0], AC_DESC_IMAGE,
2377 NULL, true, true);
2378 params[param_count++] = ctx->ac.i1false; /* r128 */
2379 params[param_count++] = glsl_is_array_image(type) ? ctx->ac.i1true : ctx->ac.i1false; /* da */
2380 params[param_count++] = ctx->ac.i1false; /* slc */
2381
2382 build_int_type_name(LLVMTypeOf(coords),
2383 coords_type, sizeof(coords_type));
2384
2385 length = snprintf(intrinsic_name, sizeof(intrinsic_name),
2386 "llvm.amdgcn.image.atomic.%s.%s", atomic_name, coords_type);
2387 }
2388
2389 assert(length < sizeof(intrinsic_name));
2390 return ac_build_intrinsic(&ctx->ac, intrinsic_name, ctx->ac.i32, params, param_count, 0);
2391 }
2392
2393 static LLVMValueRef visit_image_samples(struct ac_nir_context *ctx,
2394 const nir_intrinsic_instr *instr)
2395 {
2396 const nir_variable *var = instr->variables[0]->var;
2397 const struct glsl_type *type = glsl_without_array(var->type);
2398
2399 struct ac_image_args args = { 0 };
2400 args.da = glsl_is_array_image(type);
2401 args.dmask = 0xf;
2402 args.resource = get_sampler_desc(ctx, instr->variables[0],
2403 AC_DESC_IMAGE, NULL, true, false);
2404 args.opcode = ac_image_get_resinfo;
2405 args.addr = ctx->ac.i32_0;
2406
2407 return ac_build_image_opcode(&ctx->ac, &args);
2408 }
2409
2410 static LLVMValueRef visit_image_size(struct ac_nir_context *ctx,
2411 const nir_intrinsic_instr *instr)
2412 {
2413 LLVMValueRef res;
2414 const nir_variable *var = instr->variables[0]->var;
2415 const struct glsl_type *type = glsl_without_array(var->type);
2416
2417 if (glsl_get_sampler_dim(type) == GLSL_SAMPLER_DIM_BUF)
2418 return get_buffer_size(ctx,
2419 get_sampler_desc(ctx, instr->variables[0],
2420 AC_DESC_BUFFER, NULL, true, false), true);
2421
2422 struct ac_image_args args = { 0 };
2423
2424 args.da = glsl_is_array_image(type);
2425 args.dmask = 0xf;
2426 args.resource = get_sampler_desc(ctx, instr->variables[0], AC_DESC_IMAGE, NULL, true, false);
2427 args.opcode = ac_image_get_resinfo;
2428 args.addr = ctx->ac.i32_0;
2429
2430 res = ac_build_image_opcode(&ctx->ac, &args);
2431
2432 LLVMValueRef two = LLVMConstInt(ctx->ac.i32, 2, false);
2433
2434 if (glsl_get_sampler_dim(type) == GLSL_SAMPLER_DIM_CUBE &&
2435 glsl_sampler_type_is_array(type)) {
2436 LLVMValueRef six = LLVMConstInt(ctx->ac.i32, 6, false);
2437 LLVMValueRef z = LLVMBuildExtractElement(ctx->ac.builder, res, two, "");
2438 z = LLVMBuildSDiv(ctx->ac.builder, z, six, "");
2439 res = LLVMBuildInsertElement(ctx->ac.builder, res, z, two, "");
2440 }
2441 if (ctx->ac.chip_class >= GFX9 &&
2442 glsl_get_sampler_dim(type) == GLSL_SAMPLER_DIM_1D &&
2443 glsl_sampler_type_is_array(type)) {
2444 LLVMValueRef layers = LLVMBuildExtractElement(ctx->ac.builder, res, two, "");
2445 res = LLVMBuildInsertElement(ctx->ac.builder, res, layers,
2446 ctx->ac.i32_1, "");
2447
2448 }
2449 return res;
2450 }
2451
2452 #define NOOP_WAITCNT 0xf7f
2453 #define LGKM_CNT 0x07f
2454 #define VM_CNT 0xf70
2455
2456 static void emit_membar(struct ac_llvm_context *ac,
2457 const nir_intrinsic_instr *instr)
2458 {
2459 unsigned waitcnt = NOOP_WAITCNT;
2460
2461 switch (instr->intrinsic) {
2462 case nir_intrinsic_memory_barrier:
2463 case nir_intrinsic_group_memory_barrier:
2464 waitcnt &= VM_CNT & LGKM_CNT;
2465 break;
2466 case nir_intrinsic_memory_barrier_atomic_counter:
2467 case nir_intrinsic_memory_barrier_buffer:
2468 case nir_intrinsic_memory_barrier_image:
2469 waitcnt &= VM_CNT;
2470 break;
2471 case nir_intrinsic_memory_barrier_shared:
2472 waitcnt &= LGKM_CNT;
2473 break;
2474 default:
2475 break;
2476 }
2477 if (waitcnt != NOOP_WAITCNT)
2478 ac_build_waitcnt(ac, waitcnt);
2479 }
2480
2481 void ac_emit_barrier(struct ac_llvm_context *ac, gl_shader_stage stage)
2482 {
2483 /* SI only (thanks to a hw bug workaround):
2484 * The real barrier instruction isn’t needed, because an entire patch
2485 * always fits into a single wave.
2486 */
2487 if (ac->chip_class == SI && stage == MESA_SHADER_TESS_CTRL) {
2488 ac_build_waitcnt(ac, LGKM_CNT & VM_CNT);
2489 return;
2490 }
2491 ac_build_intrinsic(ac, "llvm.amdgcn.s.barrier",
2492 ac->voidt, NULL, 0, AC_FUNC_ATTR_CONVERGENT);
2493 }
2494
2495 static void emit_discard(struct ac_nir_context *ctx,
2496 const nir_intrinsic_instr *instr)
2497 {
2498 LLVMValueRef cond;
2499
2500 if (instr->intrinsic == nir_intrinsic_discard_if) {
2501 cond = LLVMBuildICmp(ctx->ac.builder, LLVMIntEQ,
2502 get_src(ctx, instr->src[0]),
2503 ctx->ac.i32_0, "");
2504 } else {
2505 assert(instr->intrinsic == nir_intrinsic_discard);
2506 cond = LLVMConstInt(ctx->ac.i1, false, 0);
2507 }
2508
2509 ctx->abi->emit_kill(ctx->abi, cond);
2510 }
2511
2512 static LLVMValueRef
2513 visit_load_helper_invocation(struct ac_nir_context *ctx)
2514 {
2515 LLVMValueRef result = ac_build_intrinsic(&ctx->ac,
2516 "llvm.amdgcn.ps.live",
2517 ctx->ac.i1, NULL, 0,
2518 AC_FUNC_ATTR_READNONE);
2519 result = LLVMBuildNot(ctx->ac.builder, result, "");
2520 return LLVMBuildSExt(ctx->ac.builder, result, ctx->ac.i32, "");
2521 }
2522
2523 static LLVMValueRef
2524 visit_load_local_invocation_index(struct ac_nir_context *ctx)
2525 {
2526 LLVMValueRef result;
2527 LLVMValueRef thread_id = ac_get_thread_id(&ctx->ac);
2528 result = LLVMBuildAnd(ctx->ac.builder, ctx->abi->tg_size,
2529 LLVMConstInt(ctx->ac.i32, 0xfc0, false), "");
2530
2531 return LLVMBuildAdd(ctx->ac.builder, result, thread_id, "");
2532 }
2533
2534 static LLVMValueRef
2535 visit_load_subgroup_id(struct ac_nir_context *ctx)
2536 {
2537 if (ctx->stage == MESA_SHADER_COMPUTE) {
2538 LLVMValueRef result;
2539 result = LLVMBuildAnd(ctx->ac.builder, ctx->abi->tg_size,
2540 LLVMConstInt(ctx->ac.i32, 0xfc0, false), "");
2541 return LLVMBuildLShr(ctx->ac.builder, result, LLVMConstInt(ctx->ac.i32, 6, false), "");
2542 } else {
2543 return LLVMConstInt(ctx->ac.i32, 0, false);
2544 }
2545 }
2546
2547 static LLVMValueRef
2548 visit_load_num_subgroups(struct ac_nir_context *ctx)
2549 {
2550 if (ctx->stage == MESA_SHADER_COMPUTE) {
2551 return LLVMBuildAnd(ctx->ac.builder, ctx->abi->tg_size,
2552 LLVMConstInt(ctx->ac.i32, 0x3f, false), "");
2553 } else {
2554 return LLVMConstInt(ctx->ac.i32, 1, false);
2555 }
2556 }
2557
2558 static LLVMValueRef
2559 visit_first_invocation(struct ac_nir_context *ctx)
2560 {
2561 LLVMValueRef active_set = ac_build_ballot(&ctx->ac, ctx->ac.i32_1);
2562
2563 /* The second argument is whether cttz(0) should be defined, but we do not care. */
2564 LLVMValueRef args[] = {active_set, LLVMConstInt(ctx->ac.i1, 0, false)};
2565 LLVMValueRef result = ac_build_intrinsic(&ctx->ac,
2566 "llvm.cttz.i64",
2567 ctx->ac.i64, args, 2,
2568 AC_FUNC_ATTR_NOUNWIND |
2569 AC_FUNC_ATTR_READNONE);
2570
2571 return LLVMBuildTrunc(ctx->ac.builder, result, ctx->ac.i32, "");
2572 }
2573
2574 static LLVMValueRef
2575 visit_load_shared(struct ac_nir_context *ctx,
2576 const nir_intrinsic_instr *instr)
2577 {
2578 LLVMValueRef values[4], derived_ptr, index, ret;
2579
2580 LLVMValueRef ptr = get_memory_ptr(ctx, instr->src[0]);
2581
2582 for (int chan = 0; chan < instr->num_components; chan++) {
2583 index = LLVMConstInt(ctx->ac.i32, chan, 0);
2584 derived_ptr = LLVMBuildGEP(ctx->ac.builder, ptr, &index, 1, "");
2585 values[chan] = LLVMBuildLoad(ctx->ac.builder, derived_ptr, "");
2586 }
2587
2588 ret = ac_build_gather_values(&ctx->ac, values, instr->num_components);
2589 return LLVMBuildBitCast(ctx->ac.builder, ret, get_def_type(ctx, &instr->dest.ssa), "");
2590 }
2591
2592 static void
2593 visit_store_shared(struct ac_nir_context *ctx,
2594 const nir_intrinsic_instr *instr)
2595 {
2596 LLVMValueRef derived_ptr, data,index;
2597 LLVMBuilderRef builder = ctx->ac.builder;
2598
2599 LLVMValueRef ptr = get_memory_ptr(ctx, instr->src[1]);
2600 LLVMValueRef src = get_src(ctx, instr->src[0]);
2601
2602 int writemask = nir_intrinsic_write_mask(instr);
2603 for (int chan = 0; chan < 4; chan++) {
2604 if (!(writemask & (1 << chan))) {
2605 continue;
2606 }
2607 data = ac_llvm_extract_elem(&ctx->ac, src, chan);
2608 index = LLVMConstInt(ctx->ac.i32, chan, 0);
2609 derived_ptr = LLVMBuildGEP(builder, ptr, &index, 1, "");
2610 LLVMBuildStore(builder, data, derived_ptr);
2611 }
2612 }
2613
2614 static LLVMValueRef visit_var_atomic(struct ac_nir_context *ctx,
2615 const nir_intrinsic_instr *instr,
2616 LLVMValueRef ptr, int src_idx)
2617 {
2618 LLVMValueRef result;
2619 LLVMValueRef src = get_src(ctx, instr->src[src_idx]);
2620
2621 if (instr->intrinsic == nir_intrinsic_var_atomic_comp_swap ||
2622 instr->intrinsic == nir_intrinsic_shared_atomic_comp_swap) {
2623 LLVMValueRef src1 = get_src(ctx, instr->src[src_idx + 1]);
2624 result = LLVMBuildAtomicCmpXchg(ctx->ac.builder,
2625 ptr, src, src1,
2626 LLVMAtomicOrderingSequentiallyConsistent,
2627 LLVMAtomicOrderingSequentiallyConsistent,
2628 false);
2629 } else {
2630 LLVMAtomicRMWBinOp op;
2631 switch (instr->intrinsic) {
2632 case nir_intrinsic_var_atomic_add:
2633 case nir_intrinsic_shared_atomic_add:
2634 op = LLVMAtomicRMWBinOpAdd;
2635 break;
2636 case nir_intrinsic_var_atomic_umin:
2637 case nir_intrinsic_shared_atomic_umin:
2638 op = LLVMAtomicRMWBinOpUMin;
2639 break;
2640 case nir_intrinsic_var_atomic_umax:
2641 case nir_intrinsic_shared_atomic_umax:
2642 op = LLVMAtomicRMWBinOpUMax;
2643 break;
2644 case nir_intrinsic_var_atomic_imin:
2645 case nir_intrinsic_shared_atomic_imin:
2646 op = LLVMAtomicRMWBinOpMin;
2647 break;
2648 case nir_intrinsic_var_atomic_imax:
2649 case nir_intrinsic_shared_atomic_imax:
2650 op = LLVMAtomicRMWBinOpMax;
2651 break;
2652 case nir_intrinsic_var_atomic_and:
2653 case nir_intrinsic_shared_atomic_and:
2654 op = LLVMAtomicRMWBinOpAnd;
2655 break;
2656 case nir_intrinsic_var_atomic_or:
2657 case nir_intrinsic_shared_atomic_or:
2658 op = LLVMAtomicRMWBinOpOr;
2659 break;
2660 case nir_intrinsic_var_atomic_xor:
2661 case nir_intrinsic_shared_atomic_xor:
2662 op = LLVMAtomicRMWBinOpXor;
2663 break;
2664 case nir_intrinsic_var_atomic_exchange:
2665 case nir_intrinsic_shared_atomic_exchange:
2666 op = LLVMAtomicRMWBinOpXchg;
2667 break;
2668 default:
2669 return NULL;
2670 }
2671
2672 result = LLVMBuildAtomicRMW(ctx->ac.builder, op, ptr, ac_to_integer(&ctx->ac, src),
2673 LLVMAtomicOrderingSequentiallyConsistent,
2674 false);
2675 }
2676 return result;
2677 }
2678
2679 static LLVMValueRef load_sample_pos(struct ac_nir_context *ctx)
2680 {
2681 LLVMValueRef values[2];
2682 LLVMValueRef pos[2];
2683
2684 pos[0] = ac_to_float(&ctx->ac, ctx->abi->frag_pos[0]);
2685 pos[1] = ac_to_float(&ctx->ac, ctx->abi->frag_pos[1]);
2686
2687 values[0] = ac_build_fract(&ctx->ac, pos[0], 32);
2688 values[1] = ac_build_fract(&ctx->ac, pos[1], 32);
2689 return ac_build_gather_values(&ctx->ac, values, 2);
2690 }
2691
2692 static LLVMValueRef visit_interp(struct ac_nir_context *ctx,
2693 const nir_intrinsic_instr *instr)
2694 {
2695 LLVMValueRef result[4];
2696 LLVMValueRef interp_param, attr_number;
2697 unsigned location;
2698 unsigned chan;
2699 LLVMValueRef src_c0 = NULL;
2700 LLVMValueRef src_c1 = NULL;
2701 LLVMValueRef src0 = NULL;
2702 int input_index = instr->variables[0]->var->data.location - VARYING_SLOT_VAR0;
2703 switch (instr->intrinsic) {
2704 case nir_intrinsic_interp_var_at_centroid:
2705 location = INTERP_CENTROID;
2706 break;
2707 case nir_intrinsic_interp_var_at_sample:
2708 case nir_intrinsic_interp_var_at_offset:
2709 location = INTERP_CENTER;
2710 src0 = get_src(ctx, instr->src[0]);
2711 break;
2712 default:
2713 break;
2714 }
2715
2716 if (instr->intrinsic == nir_intrinsic_interp_var_at_offset) {
2717 src_c0 = ac_to_float(&ctx->ac, LLVMBuildExtractElement(ctx->ac.builder, src0, ctx->ac.i32_0, ""));
2718 src_c1 = ac_to_float(&ctx->ac, LLVMBuildExtractElement(ctx->ac.builder, src0, ctx->ac.i32_1, ""));
2719 } else if (instr->intrinsic == nir_intrinsic_interp_var_at_sample) {
2720 LLVMValueRef sample_position;
2721 LLVMValueRef halfval = LLVMConstReal(ctx->ac.f32, 0.5f);
2722
2723 /* fetch sample ID */
2724 sample_position = ctx->abi->load_sample_position(ctx->abi, src0);
2725
2726 src_c0 = LLVMBuildExtractElement(ctx->ac.builder, sample_position, ctx->ac.i32_0, "");
2727 src_c0 = LLVMBuildFSub(ctx->ac.builder, src_c0, halfval, "");
2728 src_c1 = LLVMBuildExtractElement(ctx->ac.builder, sample_position, ctx->ac.i32_1, "");
2729 src_c1 = LLVMBuildFSub(ctx->ac.builder, src_c1, halfval, "");
2730 }
2731 interp_param = ctx->abi->lookup_interp_param(ctx->abi, instr->variables[0]->var->data.interpolation, location);
2732 attr_number = LLVMConstInt(ctx->ac.i32, input_index, false);
2733
2734 if (location == INTERP_CENTER) {
2735 LLVMValueRef ij_out[2];
2736 LLVMValueRef ddxy_out = emit_ddxy_interp(ctx, interp_param);
2737
2738 /*
2739 * take the I then J parameters, and the DDX/Y for it, and
2740 * calculate the IJ inputs for the interpolator.
2741 * temp1 = ddx * offset/sample.x + I;
2742 * interp_param.I = ddy * offset/sample.y + temp1;
2743 * temp1 = ddx * offset/sample.x + J;
2744 * interp_param.J = ddy * offset/sample.y + temp1;
2745 */
2746 for (unsigned i = 0; i < 2; i++) {
2747 LLVMValueRef ix_ll = LLVMConstInt(ctx->ac.i32, i, false);
2748 LLVMValueRef iy_ll = LLVMConstInt(ctx->ac.i32, i + 2, false);
2749 LLVMValueRef ddx_el = LLVMBuildExtractElement(ctx->ac.builder,
2750 ddxy_out, ix_ll, "");
2751 LLVMValueRef ddy_el = LLVMBuildExtractElement(ctx->ac.builder,
2752 ddxy_out, iy_ll, "");
2753 LLVMValueRef interp_el = LLVMBuildExtractElement(ctx->ac.builder,
2754 interp_param, ix_ll, "");
2755 LLVMValueRef temp1, temp2;
2756
2757 interp_el = LLVMBuildBitCast(ctx->ac.builder, interp_el,
2758 ctx->ac.f32, "");
2759
2760 temp1 = LLVMBuildFMul(ctx->ac.builder, ddx_el, src_c0, "");
2761 temp1 = LLVMBuildFAdd(ctx->ac.builder, temp1, interp_el, "");
2762
2763 temp2 = LLVMBuildFMul(ctx->ac.builder, ddy_el, src_c1, "");
2764 temp2 = LLVMBuildFAdd(ctx->ac.builder, temp2, temp1, "");
2765
2766 ij_out[i] = LLVMBuildBitCast(ctx->ac.builder,
2767 temp2, ctx->ac.i32, "");
2768 }
2769 interp_param = ac_build_gather_values(&ctx->ac, ij_out, 2);
2770
2771 }
2772
2773 for (chan = 0; chan < 4; chan++) {
2774 LLVMValueRef llvm_chan = LLVMConstInt(ctx->ac.i32, chan, false);
2775
2776 if (interp_param) {
2777 interp_param = LLVMBuildBitCast(ctx->ac.builder,
2778 interp_param, ctx->ac.v2f32, "");
2779 LLVMValueRef i = LLVMBuildExtractElement(
2780 ctx->ac.builder, interp_param, ctx->ac.i32_0, "");
2781 LLVMValueRef j = LLVMBuildExtractElement(
2782 ctx->ac.builder, interp_param, ctx->ac.i32_1, "");
2783
2784 result[chan] = ac_build_fs_interp(&ctx->ac,
2785 llvm_chan, attr_number,
2786 ctx->abi->prim_mask, i, j);
2787 } else {
2788 result[chan] = ac_build_fs_interp_mov(&ctx->ac,
2789 LLVMConstInt(ctx->ac.i32, 2, false),
2790 llvm_chan, attr_number,
2791 ctx->abi->prim_mask);
2792 }
2793 }
2794 return ac_build_varying_gather_values(&ctx->ac, result, instr->num_components,
2795 instr->variables[0]->var->data.location_frac);
2796 }
2797
2798 static void visit_intrinsic(struct ac_nir_context *ctx,
2799 nir_intrinsic_instr *instr)
2800 {
2801 LLVMValueRef result = NULL;
2802
2803 switch (instr->intrinsic) {
2804 case nir_intrinsic_ballot:
2805 result = ac_build_ballot(&ctx->ac, get_src(ctx, instr->src[0]));
2806 break;
2807 case nir_intrinsic_read_invocation:
2808 result = ac_build_readlane(&ctx->ac, get_src(ctx, instr->src[0]),
2809 get_src(ctx, instr->src[1]));
2810 break;
2811 case nir_intrinsic_read_first_invocation:
2812 result = ac_build_readlane(&ctx->ac, get_src(ctx, instr->src[0]), NULL);
2813 break;
2814 case nir_intrinsic_load_subgroup_invocation:
2815 result = ac_get_thread_id(&ctx->ac);
2816 break;
2817 case nir_intrinsic_load_work_group_id: {
2818 LLVMValueRef values[3];
2819
2820 for (int i = 0; i < 3; i++) {
2821 values[i] = ctx->abi->workgroup_ids[i] ?
2822 ctx->abi->workgroup_ids[i] : ctx->ac.i32_0;
2823 }
2824
2825 result = ac_build_gather_values(&ctx->ac, values, 3);
2826 break;
2827 }
2828 case nir_intrinsic_load_base_vertex: {
2829 result = ctx->abi->load_base_vertex(ctx->abi);
2830 break;
2831 }
2832 case nir_intrinsic_load_local_group_size:
2833 result = ctx->abi->load_local_group_size(ctx->abi);
2834 break;
2835 case nir_intrinsic_load_vertex_id:
2836 result = LLVMBuildAdd(ctx->ac.builder, ctx->abi->vertex_id,
2837 ctx->abi->base_vertex, "");
2838 break;
2839 case nir_intrinsic_load_vertex_id_zero_base: {
2840 result = ctx->abi->vertex_id;
2841 break;
2842 }
2843 case nir_intrinsic_load_local_invocation_id: {
2844 result = ctx->abi->local_invocation_ids;
2845 break;
2846 }
2847 case nir_intrinsic_load_base_instance:
2848 result = ctx->abi->start_instance;
2849 break;
2850 case nir_intrinsic_load_draw_id:
2851 result = ctx->abi->draw_id;
2852 break;
2853 case nir_intrinsic_load_view_index:
2854 result = ctx->abi->view_index;
2855 break;
2856 case nir_intrinsic_load_invocation_id:
2857 if (ctx->stage == MESA_SHADER_TESS_CTRL)
2858 result = ac_unpack_param(&ctx->ac, ctx->abi->tcs_rel_ids, 8, 5);
2859 else
2860 result = ctx->abi->gs_invocation_id;
2861 break;
2862 case nir_intrinsic_load_primitive_id:
2863 if (ctx->stage == MESA_SHADER_GEOMETRY) {
2864 result = ctx->abi->gs_prim_id;
2865 } else if (ctx->stage == MESA_SHADER_TESS_CTRL) {
2866 result = ctx->abi->tcs_patch_id;
2867 } else if (ctx->stage == MESA_SHADER_TESS_EVAL) {
2868 result = ctx->abi->tes_patch_id;
2869 } else
2870 fprintf(stderr, "Unknown primitive id intrinsic: %d", ctx->stage);
2871 break;
2872 case nir_intrinsic_load_sample_id:
2873 result = ac_unpack_param(&ctx->ac, ctx->abi->ancillary, 8, 4);
2874 break;
2875 case nir_intrinsic_load_sample_pos:
2876 result = load_sample_pos(ctx);
2877 break;
2878 case nir_intrinsic_load_sample_mask_in:
2879 result = ctx->abi->load_sample_mask_in(ctx->abi);
2880 break;
2881 case nir_intrinsic_load_frag_coord: {
2882 LLVMValueRef values[4] = {
2883 ctx->abi->frag_pos[0],
2884 ctx->abi->frag_pos[1],
2885 ctx->abi->frag_pos[2],
2886 ac_build_fdiv(&ctx->ac, ctx->ac.f32_1, ctx->abi->frag_pos[3])
2887 };
2888 result = ac_build_gather_values(&ctx->ac, values, 4);
2889 break;
2890 }
2891 case nir_intrinsic_load_front_face:
2892 result = ctx->abi->front_face;
2893 break;
2894 case nir_intrinsic_load_helper_invocation:
2895 result = visit_load_helper_invocation(ctx);
2896 break;
2897 case nir_intrinsic_load_instance_id:
2898 result = ctx->abi->instance_id;
2899 break;
2900 case nir_intrinsic_load_num_work_groups:
2901 result = ctx->abi->num_work_groups;
2902 break;
2903 case nir_intrinsic_load_local_invocation_index:
2904 result = visit_load_local_invocation_index(ctx);
2905 break;
2906 case nir_intrinsic_load_subgroup_id:
2907 result = visit_load_subgroup_id(ctx);
2908 break;
2909 case nir_intrinsic_load_num_subgroups:
2910 result = visit_load_num_subgroups(ctx);
2911 break;
2912 case nir_intrinsic_first_invocation:
2913 result = visit_first_invocation(ctx);
2914 break;
2915 case nir_intrinsic_load_push_constant:
2916 result = visit_load_push_constant(ctx, instr);
2917 break;
2918 case nir_intrinsic_vulkan_resource_index: {
2919 LLVMValueRef index = get_src(ctx, instr->src[0]);
2920 unsigned desc_set = nir_intrinsic_desc_set(instr);
2921 unsigned binding = nir_intrinsic_binding(instr);
2922
2923 result = ctx->abi->load_resource(ctx->abi, index, desc_set,
2924 binding);
2925 break;
2926 }
2927 case nir_intrinsic_vulkan_resource_reindex:
2928 result = visit_vulkan_resource_reindex(ctx, instr);
2929 break;
2930 case nir_intrinsic_store_ssbo:
2931 visit_store_ssbo(ctx, instr);
2932 break;
2933 case nir_intrinsic_load_ssbo:
2934 result = visit_load_buffer(ctx, instr);
2935 break;
2936 case nir_intrinsic_ssbo_atomic_add:
2937 case nir_intrinsic_ssbo_atomic_imin:
2938 case nir_intrinsic_ssbo_atomic_umin:
2939 case nir_intrinsic_ssbo_atomic_imax:
2940 case nir_intrinsic_ssbo_atomic_umax:
2941 case nir_intrinsic_ssbo_atomic_and:
2942 case nir_intrinsic_ssbo_atomic_or:
2943 case nir_intrinsic_ssbo_atomic_xor:
2944 case nir_intrinsic_ssbo_atomic_exchange:
2945 case nir_intrinsic_ssbo_atomic_comp_swap:
2946 result = visit_atomic_ssbo(ctx, instr);
2947 break;
2948 case nir_intrinsic_load_ubo:
2949 result = visit_load_ubo_buffer(ctx, instr);
2950 break;
2951 case nir_intrinsic_get_buffer_size:
2952 result = visit_get_buffer_size(ctx, instr);
2953 break;
2954 case nir_intrinsic_load_var:
2955 result = visit_load_var(ctx, instr);
2956 break;
2957 case nir_intrinsic_store_var:
2958 visit_store_var(ctx, instr);
2959 break;
2960 case nir_intrinsic_load_shared:
2961 result = visit_load_shared(ctx, instr);
2962 break;
2963 case nir_intrinsic_store_shared:
2964 visit_store_shared(ctx, instr);
2965 break;
2966 case nir_intrinsic_image_var_samples:
2967 result = visit_image_samples(ctx, instr);
2968 break;
2969 case nir_intrinsic_image_var_load:
2970 result = visit_image_load(ctx, instr);
2971 break;
2972 case nir_intrinsic_image_var_store:
2973 visit_image_store(ctx, instr);
2974 break;
2975 case nir_intrinsic_image_var_atomic_add:
2976 case nir_intrinsic_image_var_atomic_min:
2977 case nir_intrinsic_image_var_atomic_max:
2978 case nir_intrinsic_image_var_atomic_and:
2979 case nir_intrinsic_image_var_atomic_or:
2980 case nir_intrinsic_image_var_atomic_xor:
2981 case nir_intrinsic_image_var_atomic_exchange:
2982 case nir_intrinsic_image_var_atomic_comp_swap:
2983 result = visit_image_atomic(ctx, instr);
2984 break;
2985 case nir_intrinsic_image_var_size:
2986 result = visit_image_size(ctx, instr);
2987 break;
2988 case nir_intrinsic_shader_clock:
2989 result = ac_build_shader_clock(&ctx->ac);
2990 break;
2991 case nir_intrinsic_discard:
2992 case nir_intrinsic_discard_if:
2993 emit_discard(ctx, instr);
2994 break;
2995 case nir_intrinsic_memory_barrier:
2996 case nir_intrinsic_group_memory_barrier:
2997 case nir_intrinsic_memory_barrier_atomic_counter:
2998 case nir_intrinsic_memory_barrier_buffer:
2999 case nir_intrinsic_memory_barrier_image:
3000 case nir_intrinsic_memory_barrier_shared:
3001 emit_membar(&ctx->ac, instr);
3002 break;
3003 case nir_intrinsic_barrier:
3004 ac_emit_barrier(&ctx->ac, ctx->stage);
3005 break;
3006 case nir_intrinsic_shared_atomic_add:
3007 case nir_intrinsic_shared_atomic_imin:
3008 case nir_intrinsic_shared_atomic_umin:
3009 case nir_intrinsic_shared_atomic_imax:
3010 case nir_intrinsic_shared_atomic_umax:
3011 case nir_intrinsic_shared_atomic_and:
3012 case nir_intrinsic_shared_atomic_or:
3013 case nir_intrinsic_shared_atomic_xor:
3014 case nir_intrinsic_shared_atomic_exchange:
3015 case nir_intrinsic_shared_atomic_comp_swap: {
3016 LLVMValueRef ptr = get_memory_ptr(ctx, instr->src[0]);
3017 result = visit_var_atomic(ctx, instr, ptr, 1);
3018 break;
3019 }
3020 case nir_intrinsic_var_atomic_add:
3021 case nir_intrinsic_var_atomic_imin:
3022 case nir_intrinsic_var_atomic_umin:
3023 case nir_intrinsic_var_atomic_imax:
3024 case nir_intrinsic_var_atomic_umax:
3025 case nir_intrinsic_var_atomic_and:
3026 case nir_intrinsic_var_atomic_or:
3027 case nir_intrinsic_var_atomic_xor:
3028 case nir_intrinsic_var_atomic_exchange:
3029 case nir_intrinsic_var_atomic_comp_swap: {
3030 LLVMValueRef ptr = build_gep_for_deref(ctx, instr->variables[0]);
3031 result = visit_var_atomic(ctx, instr, ptr, 0);
3032 break;
3033 }
3034 case nir_intrinsic_interp_var_at_centroid:
3035 case nir_intrinsic_interp_var_at_sample:
3036 case nir_intrinsic_interp_var_at_offset:
3037 result = visit_interp(ctx, instr);
3038 break;
3039 case nir_intrinsic_emit_vertex:
3040 ctx->abi->emit_vertex(ctx->abi, nir_intrinsic_stream_id(instr), ctx->abi->outputs);
3041 break;
3042 case nir_intrinsic_end_primitive:
3043 ctx->abi->emit_primitive(ctx->abi, nir_intrinsic_stream_id(instr));
3044 break;
3045 case nir_intrinsic_load_tess_coord:
3046 result = ctx->abi->load_tess_coord(ctx->abi);
3047 break;
3048 case nir_intrinsic_load_tess_level_outer:
3049 result = ctx->abi->load_tess_level(ctx->abi, VARYING_SLOT_TESS_LEVEL_OUTER);
3050 break;
3051 case nir_intrinsic_load_tess_level_inner:
3052 result = ctx->abi->load_tess_level(ctx->abi, VARYING_SLOT_TESS_LEVEL_INNER);
3053 break;
3054 case nir_intrinsic_load_patch_vertices_in:
3055 result = ctx->abi->load_patch_vertices_in(ctx->abi);
3056 break;
3057 case nir_intrinsic_vote_all: {
3058 LLVMValueRef tmp = ac_build_vote_all(&ctx->ac, get_src(ctx, instr->src[0]));
3059 result = LLVMBuildSExt(ctx->ac.builder, tmp, ctx->ac.i32, "");
3060 break;
3061 }
3062 case nir_intrinsic_vote_any: {
3063 LLVMValueRef tmp = ac_build_vote_any(&ctx->ac, get_src(ctx, instr->src[0]));
3064 result = LLVMBuildSExt(ctx->ac.builder, tmp, ctx->ac.i32, "");
3065 break;
3066 }
3067 case nir_intrinsic_shuffle:
3068 result = ac_build_shuffle(&ctx->ac, get_src(ctx, instr->src[0]),
3069 get_src(ctx, instr->src[1]));
3070 break;
3071 case nir_intrinsic_reduce:
3072 result = ac_build_reduce(&ctx->ac,
3073 get_src(ctx, instr->src[0]),
3074 instr->const_index[0],
3075 instr->const_index[1]);
3076 break;
3077 case nir_intrinsic_inclusive_scan:
3078 result = ac_build_inclusive_scan(&ctx->ac,
3079 get_src(ctx, instr->src[0]),
3080 instr->const_index[0]);
3081 break;
3082 case nir_intrinsic_exclusive_scan:
3083 result = ac_build_exclusive_scan(&ctx->ac,
3084 get_src(ctx, instr->src[0]),
3085 instr->const_index[0]);
3086 break;
3087 case nir_intrinsic_quad_broadcast: {
3088 unsigned lane = nir_src_as_const_value(instr->src[1])->u32[0];
3089 result = ac_build_quad_swizzle(&ctx->ac, get_src(ctx, instr->src[0]),
3090 lane, lane, lane, lane);
3091 break;
3092 }
3093 case nir_intrinsic_quad_swap_horizontal:
3094 result = ac_build_quad_swizzle(&ctx->ac, get_src(ctx, instr->src[0]), 1, 0, 3 ,2);
3095 break;
3096 case nir_intrinsic_quad_swap_vertical:
3097 result = ac_build_quad_swizzle(&ctx->ac, get_src(ctx, instr->src[0]), 2, 3, 0 ,1);
3098 break;
3099 case nir_intrinsic_quad_swap_diagonal:
3100 result = ac_build_quad_swizzle(&ctx->ac, get_src(ctx, instr->src[0]), 3, 2, 1 ,0);
3101 break;
3102 default:
3103 fprintf(stderr, "Unknown intrinsic: ");
3104 nir_print_instr(&instr->instr, stderr);
3105 fprintf(stderr, "\n");
3106 break;
3107 }
3108 if (result) {
3109 ctx->ssa_defs[instr->dest.ssa.index] = result;
3110 }
3111 }
3112
3113 static LLVMValueRef get_sampler_desc(struct ac_nir_context *ctx,
3114 const nir_deref_var *deref,
3115 enum ac_descriptor_type desc_type,
3116 const nir_tex_instr *tex_instr,
3117 bool image, bool write)
3118 {
3119 LLVMValueRef index = NULL;
3120 unsigned constant_index = 0;
3121 unsigned descriptor_set;
3122 unsigned base_index;
3123 bool bindless = false;
3124
3125 if (!deref) {
3126 assert(tex_instr && !image);
3127 descriptor_set = 0;
3128 base_index = tex_instr->sampler_index;
3129 } else {
3130 const nir_deref *tail = &deref->deref;
3131 while (tail->child) {
3132 const nir_deref_array *child = nir_deref_as_array(tail->child);
3133 unsigned array_size = glsl_get_aoa_size(tail->child->type);
3134
3135 if (!array_size)
3136 array_size = 1;
3137
3138 assert(child->deref_array_type != nir_deref_array_type_wildcard);
3139
3140 if (child->deref_array_type == nir_deref_array_type_indirect) {
3141 LLVMValueRef indirect = get_src(ctx, child->indirect);
3142
3143 indirect = LLVMBuildMul(ctx->ac.builder, indirect,
3144 LLVMConstInt(ctx->ac.i32, array_size, false), "");
3145
3146 if (!index)
3147 index = indirect;
3148 else
3149 index = LLVMBuildAdd(ctx->ac.builder, index, indirect, "");
3150 }
3151
3152 constant_index += child->base_offset * array_size;
3153
3154 tail = &child->deref;
3155 }
3156 descriptor_set = deref->var->data.descriptor_set;
3157
3158 if (deref->var->data.bindless) {
3159 bindless = deref->var->data.bindless;
3160 base_index = deref->var->data.driver_location;
3161 } else {
3162 base_index = deref->var->data.binding;
3163 }
3164 }
3165
3166 return ctx->abi->load_sampler_desc(ctx->abi,
3167 descriptor_set,
3168 base_index,
3169 constant_index, index,
3170 desc_type, image, write, bindless);
3171 }
3172
3173 static void set_tex_fetch_args(struct ac_llvm_context *ctx,
3174 struct ac_image_args *args,
3175 const nir_tex_instr *instr,
3176 nir_texop op,
3177 LLVMValueRef res_ptr, LLVMValueRef samp_ptr,
3178 LLVMValueRef *param, unsigned count,
3179 unsigned dmask)
3180 {
3181 unsigned is_rect = 0;
3182 bool da = instr->is_array || instr->sampler_dim == GLSL_SAMPLER_DIM_CUBE;
3183
3184 if (op == nir_texop_lod)
3185 da = false;
3186 /* Pad to power of two vector */
3187 while (count < util_next_power_of_two(count))
3188 param[count++] = LLVMGetUndef(ctx->i32);
3189
3190 if (count > 1)
3191 args->addr = ac_build_gather_values(ctx, param, count);
3192 else
3193 args->addr = param[0];
3194
3195 args->resource = res_ptr;
3196 args->sampler = samp_ptr;
3197
3198 if (instr->sampler_dim == GLSL_SAMPLER_DIM_BUF && op == nir_texop_txf) {
3199 args->addr = param[0];
3200 return;
3201 }
3202
3203 args->dmask = dmask;
3204 args->unorm = is_rect;
3205 args->da = da;
3206 }
3207
3208 /* Disable anisotropic filtering if BASE_LEVEL == LAST_LEVEL.
3209 *
3210 * SI-CI:
3211 * If BASE_LEVEL == LAST_LEVEL, the shader must disable anisotropic
3212 * filtering manually. The driver sets img7 to a mask clearing
3213 * MAX_ANISO_RATIO if BASE_LEVEL == LAST_LEVEL. The shader must do:
3214 * s_and_b32 samp0, samp0, img7
3215 *
3216 * VI:
3217 * The ANISO_OVERRIDE sampler field enables this fix in TA.
3218 */
3219 static LLVMValueRef sici_fix_sampler_aniso(struct ac_nir_context *ctx,
3220 LLVMValueRef res, LLVMValueRef samp)
3221 {
3222 LLVMBuilderRef builder = ctx->ac.builder;
3223 LLVMValueRef img7, samp0;
3224
3225 if (ctx->ac.chip_class >= VI)
3226 return samp;
3227
3228 img7 = LLVMBuildExtractElement(builder, res,
3229 LLVMConstInt(ctx->ac.i32, 7, 0), "");
3230 samp0 = LLVMBuildExtractElement(builder, samp,
3231 LLVMConstInt(ctx->ac.i32, 0, 0), "");
3232 samp0 = LLVMBuildAnd(builder, samp0, img7, "");
3233 return LLVMBuildInsertElement(builder, samp, samp0,
3234 LLVMConstInt(ctx->ac.i32, 0, 0), "");
3235 }
3236
3237 static void tex_fetch_ptrs(struct ac_nir_context *ctx,
3238 nir_tex_instr *instr,
3239 LLVMValueRef *res_ptr, LLVMValueRef *samp_ptr,
3240 LLVMValueRef *fmask_ptr)
3241 {
3242 if (instr->sampler_dim == GLSL_SAMPLER_DIM_BUF)
3243 *res_ptr = get_sampler_desc(ctx, instr->texture, AC_DESC_BUFFER, instr, false, false);
3244 else
3245 *res_ptr = get_sampler_desc(ctx, instr->texture, AC_DESC_IMAGE, instr, false, false);
3246 if (samp_ptr) {
3247 if (instr->sampler)
3248 *samp_ptr = get_sampler_desc(ctx, instr->sampler, AC_DESC_SAMPLER, instr, false, false);
3249 else
3250 *samp_ptr = get_sampler_desc(ctx, instr->texture, AC_DESC_SAMPLER, instr, false, false);
3251 if (instr->sampler_dim < GLSL_SAMPLER_DIM_RECT)
3252 *samp_ptr = sici_fix_sampler_aniso(ctx, *res_ptr, *samp_ptr);
3253 }
3254 if (fmask_ptr && !instr->sampler && (instr->op == nir_texop_txf_ms ||
3255 instr->op == nir_texop_samples_identical))
3256 *fmask_ptr = get_sampler_desc(ctx, instr->texture, AC_DESC_FMASK, instr, false, false);
3257 }
3258
3259 static LLVMValueRef apply_round_slice(struct ac_llvm_context *ctx,
3260 LLVMValueRef coord)
3261 {
3262 coord = ac_to_float(ctx, coord);
3263 coord = ac_build_intrinsic(ctx, "llvm.rint.f32", ctx->f32, &coord, 1, 0);
3264 coord = ac_to_integer(ctx, coord);
3265 return coord;
3266 }
3267
3268 static void visit_tex(struct ac_nir_context *ctx, nir_tex_instr *instr)
3269 {
3270 LLVMValueRef result = NULL;
3271 struct ac_image_args args = { 0 };
3272 unsigned dmask = 0xf;
3273 LLVMValueRef address[16];
3274 LLVMValueRef coords[5];
3275 LLVMValueRef coord = NULL, lod = NULL, comparator = NULL;
3276 LLVMValueRef bias = NULL, offsets = NULL;
3277 LLVMValueRef res_ptr, samp_ptr, fmask_ptr = NULL, sample_index = NULL;
3278 LLVMValueRef ddx = NULL, ddy = NULL;
3279 LLVMValueRef derivs[6];
3280 unsigned chan, count = 0;
3281 unsigned const_src = 0, num_deriv_comp = 0;
3282 bool lod_is_zero = false;
3283
3284 tex_fetch_ptrs(ctx, instr, &res_ptr, &samp_ptr, &fmask_ptr);
3285
3286 for (unsigned i = 0; i < instr->num_srcs; i++) {
3287 switch (instr->src[i].src_type) {
3288 case nir_tex_src_coord:
3289 coord = get_src(ctx, instr->src[i].src);
3290 break;
3291 case nir_tex_src_projector:
3292 break;
3293 case nir_tex_src_comparator:
3294 comparator = get_src(ctx, instr->src[i].src);
3295 break;
3296 case nir_tex_src_offset:
3297 offsets = get_src(ctx, instr->src[i].src);
3298 const_src = i;
3299 break;
3300 case nir_tex_src_bias:
3301 bias = get_src(ctx, instr->src[i].src);
3302 break;
3303 case nir_tex_src_lod: {
3304 nir_const_value *val = nir_src_as_const_value(instr->src[i].src);
3305
3306 if (val && val->i32[0] == 0)
3307 lod_is_zero = true;
3308 lod = get_src(ctx, instr->src[i].src);
3309 break;
3310 }
3311 case nir_tex_src_ms_index:
3312 sample_index = get_src(ctx, instr->src[i].src);
3313 break;
3314 case nir_tex_src_ms_mcs:
3315 break;
3316 case nir_tex_src_ddx:
3317 ddx = get_src(ctx, instr->src[i].src);
3318 num_deriv_comp = instr->src[i].src.ssa->num_components;
3319 break;
3320 case nir_tex_src_ddy:
3321 ddy = get_src(ctx, instr->src[i].src);
3322 break;
3323 case nir_tex_src_texture_offset:
3324 case nir_tex_src_sampler_offset:
3325 case nir_tex_src_plane:
3326 default:
3327 break;
3328 }
3329 }
3330
3331 if (instr->op == nir_texop_txs && instr->sampler_dim == GLSL_SAMPLER_DIM_BUF) {
3332 result = get_buffer_size(ctx, res_ptr, true);
3333 goto write_result;
3334 }
3335
3336 if (instr->op == nir_texop_texture_samples) {
3337 LLVMValueRef res, samples, is_msaa;
3338 res = LLVMBuildBitCast(ctx->ac.builder, res_ptr, ctx->ac.v8i32, "");
3339 samples = LLVMBuildExtractElement(ctx->ac.builder, res,
3340 LLVMConstInt(ctx->ac.i32, 3, false), "");
3341 is_msaa = LLVMBuildLShr(ctx->ac.builder, samples,
3342 LLVMConstInt(ctx->ac.i32, 28, false), "");
3343 is_msaa = LLVMBuildAnd(ctx->ac.builder, is_msaa,
3344 LLVMConstInt(ctx->ac.i32, 0xe, false), "");
3345 is_msaa = LLVMBuildICmp(ctx->ac.builder, LLVMIntEQ, is_msaa,
3346 LLVMConstInt(ctx->ac.i32, 0xe, false), "");
3347
3348 samples = LLVMBuildLShr(ctx->ac.builder, samples,
3349 LLVMConstInt(ctx->ac.i32, 16, false), "");
3350 samples = LLVMBuildAnd(ctx->ac.builder, samples,
3351 LLVMConstInt(ctx->ac.i32, 0xf, false), "");
3352 samples = LLVMBuildShl(ctx->ac.builder, ctx->ac.i32_1,
3353 samples, "");
3354 samples = LLVMBuildSelect(ctx->ac.builder, is_msaa, samples,
3355 ctx->ac.i32_1, "");
3356 result = samples;
3357 goto write_result;
3358 }
3359
3360 if (coord)
3361 for (chan = 0; chan < instr->coord_components; chan++)
3362 coords[chan] = ac_llvm_extract_elem(&ctx->ac, coord, chan);
3363
3364 if (offsets && instr->op != nir_texop_txf) {
3365 LLVMValueRef offset[3], pack;
3366 for (chan = 0; chan < 3; ++chan)
3367 offset[chan] = ctx->ac.i32_0;
3368
3369 args.offset = true;
3370 for (chan = 0; chan < ac_get_llvm_num_components(offsets); chan++) {
3371 offset[chan] = ac_llvm_extract_elem(&ctx->ac, offsets, chan);
3372 offset[chan] = LLVMBuildAnd(ctx->ac.builder, offset[chan],
3373 LLVMConstInt(ctx->ac.i32, 0x3f, false), "");
3374 if (chan)
3375 offset[chan] = LLVMBuildShl(ctx->ac.builder, offset[chan],
3376 LLVMConstInt(ctx->ac.i32, chan * 8, false), "");
3377 }
3378 pack = LLVMBuildOr(ctx->ac.builder, offset[0], offset[1], "");
3379 pack = LLVMBuildOr(ctx->ac.builder, pack, offset[2], "");
3380 address[count++] = pack;
3381
3382 }
3383 /* pack LOD bias value */
3384 if (instr->op == nir_texop_txb && bias) {
3385 address[count++] = bias;
3386 }
3387
3388 /* Pack depth comparison value */
3389 if (instr->is_shadow && comparator) {
3390 LLVMValueRef z = ac_to_float(&ctx->ac,
3391 ac_llvm_extract_elem(&ctx->ac, comparator, 0));
3392
3393 /* TC-compatible HTILE on radeonsi promotes Z16 and Z24 to Z32_FLOAT,
3394 * so the depth comparison value isn't clamped for Z16 and
3395 * Z24 anymore. Do it manually here.
3396 *
3397 * It's unnecessary if the original texture format was
3398 * Z32_FLOAT, but we don't know that here.
3399 */
3400 if (ctx->ac.chip_class == VI && ctx->abi->clamp_shadow_reference)
3401 z = ac_build_clamp(&ctx->ac, z);
3402
3403 address[count++] = z;
3404 }
3405
3406 /* pack derivatives */
3407 if (ddx || ddy) {
3408 int num_src_deriv_channels, num_dest_deriv_channels;
3409 switch (instr->sampler_dim) {
3410 case GLSL_SAMPLER_DIM_3D:
3411 case GLSL_SAMPLER_DIM_CUBE:
3412 num_deriv_comp = 3;
3413 num_src_deriv_channels = 3;
3414 num_dest_deriv_channels = 3;
3415 break;
3416 case GLSL_SAMPLER_DIM_2D:
3417 default:
3418 num_src_deriv_channels = 2;
3419 num_dest_deriv_channels = 2;
3420 num_deriv_comp = 2;
3421 break;
3422 case GLSL_SAMPLER_DIM_1D:
3423 num_src_deriv_channels = 1;
3424 if (ctx->ac.chip_class >= GFX9) {
3425 num_dest_deriv_channels = 2;
3426 num_deriv_comp = 2;
3427 } else {
3428 num_dest_deriv_channels = 1;
3429 num_deriv_comp = 1;
3430 }
3431 break;
3432 }
3433
3434 for (unsigned i = 0; i < num_src_deriv_channels; i++) {
3435 derivs[i] = ac_to_float(&ctx->ac, ac_llvm_extract_elem(&ctx->ac, ddx, i));
3436 derivs[num_dest_deriv_channels + i] = ac_to_float(&ctx->ac, ac_llvm_extract_elem(&ctx->ac, ddy, i));
3437 }
3438 for (unsigned i = num_src_deriv_channels; i < num_dest_deriv_channels; i++) {
3439 derivs[i] = ctx->ac.f32_0;
3440 derivs[num_dest_deriv_channels + i] = ctx->ac.f32_0;
3441 }
3442 }
3443
3444 if (instr->sampler_dim == GLSL_SAMPLER_DIM_CUBE && coord) {
3445 for (chan = 0; chan < instr->coord_components; chan++)
3446 coords[chan] = ac_to_float(&ctx->ac, coords[chan]);
3447 if (instr->coord_components == 3)
3448 coords[3] = LLVMGetUndef(ctx->ac.f32);
3449 ac_prepare_cube_coords(&ctx->ac,
3450 instr->op == nir_texop_txd, instr->is_array,
3451 instr->op == nir_texop_lod, coords, derivs);
3452 if (num_deriv_comp)
3453 num_deriv_comp--;
3454 }
3455
3456 if (ddx || ddy) {
3457 for (unsigned i = 0; i < num_deriv_comp * 2; i++)
3458 address[count++] = derivs[i];
3459 }
3460
3461 /* Pack texture coordinates */
3462 if (coord) {
3463 address[count++] = coords[0];
3464 if (instr->coord_components > 1) {
3465 if (instr->sampler_dim == GLSL_SAMPLER_DIM_1D && instr->is_array && instr->op != nir_texop_txf) {
3466 coords[1] = apply_round_slice(&ctx->ac, coords[1]);
3467 }
3468 address[count++] = coords[1];
3469 }
3470 if (instr->coord_components > 2) {
3471 if ((instr->sampler_dim == GLSL_SAMPLER_DIM_2D ||
3472 instr->sampler_dim == GLSL_SAMPLER_DIM_MS ||
3473 instr->sampler_dim == GLSL_SAMPLER_DIM_SUBPASS ||
3474 instr->sampler_dim == GLSL_SAMPLER_DIM_SUBPASS_MS) &&
3475 instr->is_array &&
3476 instr->op != nir_texop_txf && instr->op != nir_texop_txf_ms) {
3477 coords[2] = apply_round_slice(&ctx->ac, coords[2]);
3478 }
3479 address[count++] = coords[2];
3480 }
3481
3482 if (ctx->ac.chip_class >= GFX9) {
3483 LLVMValueRef filler;
3484 if (instr->op == nir_texop_txf)
3485 filler = ctx->ac.i32_0;
3486 else
3487 filler = LLVMConstReal(ctx->ac.f32, 0.5);
3488
3489 if (instr->sampler_dim == GLSL_SAMPLER_DIM_1D) {
3490 /* No nir_texop_lod, because it does not take a slice
3491 * even with array textures. */
3492 if (instr->is_array && instr->op != nir_texop_lod ) {
3493 address[count] = address[count - 1];
3494 address[count - 1] = filler;
3495 count++;
3496 } else
3497 address[count++] = filler;
3498 }
3499 }
3500 }
3501
3502 /* Pack LOD */
3503 if (lod && ((instr->op == nir_texop_txl || instr->op == nir_texop_txf) && !lod_is_zero)) {
3504 address[count++] = lod;
3505 } else if (instr->op == nir_texop_txf_ms && sample_index) {
3506 address[count++] = sample_index;
3507 } else if(instr->op == nir_texop_txs) {
3508 count = 0;
3509 if (lod)
3510 address[count++] = lod;
3511 else
3512 address[count++] = ctx->ac.i32_0;
3513 }
3514
3515 for (chan = 0; chan < count; chan++) {
3516 address[chan] = LLVMBuildBitCast(ctx->ac.builder,
3517 address[chan], ctx->ac.i32, "");
3518 }
3519
3520 if (instr->op == nir_texop_samples_identical) {
3521 LLVMValueRef txf_address[4];
3522 struct ac_image_args txf_args = { 0 };
3523 unsigned txf_count = count;
3524 memcpy(txf_address, address, sizeof(txf_address));
3525
3526 if (!instr->is_array)
3527 txf_address[2] = ctx->ac.i32_0;
3528 txf_address[3] = ctx->ac.i32_0;
3529
3530 set_tex_fetch_args(&ctx->ac, &txf_args, instr, nir_texop_txf,
3531 fmask_ptr, NULL,
3532 txf_address, txf_count, 0xf);
3533
3534 result = build_tex_intrinsic(ctx, instr, false, &txf_args);
3535
3536 result = LLVMBuildExtractElement(ctx->ac.builder, result, ctx->ac.i32_0, "");
3537 result = emit_int_cmp(&ctx->ac, LLVMIntEQ, result, ctx->ac.i32_0);
3538 goto write_result;
3539 }
3540
3541 if (instr->sampler_dim == GLSL_SAMPLER_DIM_MS &&
3542 instr->op != nir_texop_txs) {
3543 unsigned sample_chan = instr->is_array ? 3 : 2;
3544 address[sample_chan] = adjust_sample_index_using_fmask(&ctx->ac,
3545 address[0],
3546 address[1],
3547 instr->is_array ? address[2] : NULL,
3548 address[sample_chan],
3549 fmask_ptr);
3550 }
3551
3552 if (offsets && instr->op == nir_texop_txf) {
3553 nir_const_value *const_offset =
3554 nir_src_as_const_value(instr->src[const_src].src);
3555 int num_offsets = instr->src[const_src].src.ssa->num_components;
3556 assert(const_offset);
3557 num_offsets = MIN2(num_offsets, instr->coord_components);
3558 if (num_offsets > 2)
3559 address[2] = LLVMBuildAdd(ctx->ac.builder,
3560 address[2], LLVMConstInt(ctx->ac.i32, const_offset->i32[2], false), "");
3561 if (num_offsets > 1)
3562 address[1] = LLVMBuildAdd(ctx->ac.builder,
3563 address[1], LLVMConstInt(ctx->ac.i32, const_offset->i32[1], false), "");
3564 address[0] = LLVMBuildAdd(ctx->ac.builder,
3565 address[0], LLVMConstInt(ctx->ac.i32, const_offset->i32[0], false), "");
3566
3567 }
3568
3569 /* TODO TG4 support */
3570 if (instr->op == nir_texop_tg4) {
3571 if (instr->is_shadow)
3572 dmask = 1;
3573 else
3574 dmask = 1 << instr->component;
3575 }
3576 set_tex_fetch_args(&ctx->ac, &args, instr, instr->op,
3577 res_ptr, samp_ptr, address, count, dmask);
3578
3579 result = build_tex_intrinsic(ctx, instr, lod_is_zero, &args);
3580
3581 if (instr->op == nir_texop_query_levels)
3582 result = LLVMBuildExtractElement(ctx->ac.builder, result, LLVMConstInt(ctx->ac.i32, 3, false), "");
3583 else if (instr->is_shadow && instr->is_new_style_shadow &&
3584 instr->op != nir_texop_txs && instr->op != nir_texop_lod &&
3585 instr->op != nir_texop_tg4)
3586 result = LLVMBuildExtractElement(ctx->ac.builder, result, ctx->ac.i32_0, "");
3587 else if (instr->op == nir_texop_txs &&
3588 instr->sampler_dim == GLSL_SAMPLER_DIM_CUBE &&
3589 instr->is_array) {
3590 LLVMValueRef two = LLVMConstInt(ctx->ac.i32, 2, false);
3591 LLVMValueRef six = LLVMConstInt(ctx->ac.i32, 6, false);
3592 LLVMValueRef z = LLVMBuildExtractElement(ctx->ac.builder, result, two, "");
3593 z = LLVMBuildSDiv(ctx->ac.builder, z, six, "");
3594 result = LLVMBuildInsertElement(ctx->ac.builder, result, z, two, "");
3595 } else if (ctx->ac.chip_class >= GFX9 &&
3596 instr->op == nir_texop_txs &&
3597 instr->sampler_dim == GLSL_SAMPLER_DIM_1D &&
3598 instr->is_array) {
3599 LLVMValueRef two = LLVMConstInt(ctx->ac.i32, 2, false);
3600 LLVMValueRef layers = LLVMBuildExtractElement(ctx->ac.builder, result, two, "");
3601 result = LLVMBuildInsertElement(ctx->ac.builder, result, layers,
3602 ctx->ac.i32_1, "");
3603 } else if (instr->dest.ssa.num_components != 4)
3604 result = ac_trim_vector(&ctx->ac, result, instr->dest.ssa.num_components);
3605
3606 write_result:
3607 if (result) {
3608 assert(instr->dest.is_ssa);
3609 result = ac_to_integer(&ctx->ac, result);
3610 ctx->ssa_defs[instr->dest.ssa.index] = result;
3611 }
3612 }
3613
3614
3615 static void visit_phi(struct ac_nir_context *ctx, nir_phi_instr *instr)
3616 {
3617 LLVMTypeRef type = get_def_type(ctx, &instr->dest.ssa);
3618 LLVMValueRef result = LLVMBuildPhi(ctx->ac.builder, type, "");
3619
3620 ctx->ssa_defs[instr->dest.ssa.index] = result;
3621 _mesa_hash_table_insert(ctx->phis, instr, result);
3622 }
3623
3624 static void visit_post_phi(struct ac_nir_context *ctx,
3625 nir_phi_instr *instr,
3626 LLVMValueRef llvm_phi)
3627 {
3628 nir_foreach_phi_src(src, instr) {
3629 LLVMBasicBlockRef block = get_block(ctx, src->pred);
3630 LLVMValueRef llvm_src = get_src(ctx, src->src);
3631
3632 LLVMAddIncoming(llvm_phi, &llvm_src, &block, 1);
3633 }
3634 }
3635
3636 static void phi_post_pass(struct ac_nir_context *ctx)
3637 {
3638 struct hash_entry *entry;
3639 hash_table_foreach(ctx->phis, entry) {
3640 visit_post_phi(ctx, (nir_phi_instr*)entry->key,
3641 (LLVMValueRef)entry->data);
3642 }
3643 }
3644
3645
3646 static void visit_ssa_undef(struct ac_nir_context *ctx,
3647 const nir_ssa_undef_instr *instr)
3648 {
3649 unsigned num_components = instr->def.num_components;
3650 LLVMTypeRef type = LLVMIntTypeInContext(ctx->ac.context, instr->def.bit_size);
3651 LLVMValueRef undef;
3652
3653 if (num_components == 1)
3654 undef = LLVMGetUndef(type);
3655 else {
3656 undef = LLVMGetUndef(LLVMVectorType(type, num_components));
3657 }
3658 ctx->ssa_defs[instr->def.index] = undef;
3659 }
3660
3661 static void visit_jump(struct ac_llvm_context *ctx,
3662 const nir_jump_instr *instr)
3663 {
3664 switch (instr->type) {
3665 case nir_jump_break:
3666 ac_build_break(ctx);
3667 break;
3668 case nir_jump_continue:
3669 ac_build_continue(ctx);
3670 break;
3671 default:
3672 fprintf(stderr, "Unknown NIR jump instr: ");
3673 nir_print_instr(&instr->instr, stderr);
3674 fprintf(stderr, "\n");
3675 abort();
3676 }
3677 }
3678
3679 static void visit_cf_list(struct ac_nir_context *ctx,
3680 struct exec_list *list);
3681
3682 static void visit_block(struct ac_nir_context *ctx, nir_block *block)
3683 {
3684 LLVMBasicBlockRef llvm_block = LLVMGetInsertBlock(ctx->ac.builder);
3685 nir_foreach_instr(instr, block)
3686 {
3687 switch (instr->type) {
3688 case nir_instr_type_alu:
3689 visit_alu(ctx, nir_instr_as_alu(instr));
3690 break;
3691 case nir_instr_type_load_const:
3692 visit_load_const(ctx, nir_instr_as_load_const(instr));
3693 break;
3694 case nir_instr_type_intrinsic:
3695 visit_intrinsic(ctx, nir_instr_as_intrinsic(instr));
3696 break;
3697 case nir_instr_type_tex:
3698 visit_tex(ctx, nir_instr_as_tex(instr));
3699 break;
3700 case nir_instr_type_phi:
3701 visit_phi(ctx, nir_instr_as_phi(instr));
3702 break;
3703 case nir_instr_type_ssa_undef:
3704 visit_ssa_undef(ctx, nir_instr_as_ssa_undef(instr));
3705 break;
3706 case nir_instr_type_jump:
3707 visit_jump(&ctx->ac, nir_instr_as_jump(instr));
3708 break;
3709 default:
3710 fprintf(stderr, "Unknown NIR instr type: ");
3711 nir_print_instr(instr, stderr);
3712 fprintf(stderr, "\n");
3713 abort();
3714 }
3715 }
3716
3717 _mesa_hash_table_insert(ctx->defs, block, llvm_block);
3718 }
3719
3720 static void visit_if(struct ac_nir_context *ctx, nir_if *if_stmt)
3721 {
3722 LLVMValueRef value = get_src(ctx, if_stmt->condition);
3723
3724 nir_block *then_block =
3725 (nir_block *) exec_list_get_head(&if_stmt->then_list);
3726
3727 ac_build_uif(&ctx->ac, value, then_block->index);
3728
3729 visit_cf_list(ctx, &if_stmt->then_list);
3730
3731 if (!exec_list_is_empty(&if_stmt->else_list)) {
3732 nir_block *else_block =
3733 (nir_block *) exec_list_get_head(&if_stmt->else_list);
3734
3735 ac_build_else(&ctx->ac, else_block->index);
3736 visit_cf_list(ctx, &if_stmt->else_list);
3737 }
3738
3739 ac_build_endif(&ctx->ac, then_block->index);
3740 }
3741
3742 static void visit_loop(struct ac_nir_context *ctx, nir_loop *loop)
3743 {
3744 nir_block *first_loop_block =
3745 (nir_block *) exec_list_get_head(&loop->body);
3746
3747 ac_build_bgnloop(&ctx->ac, first_loop_block->index);
3748
3749 visit_cf_list(ctx, &loop->body);
3750
3751 ac_build_endloop(&ctx->ac, first_loop_block->index);
3752 }
3753
3754 static void visit_cf_list(struct ac_nir_context *ctx,
3755 struct exec_list *list)
3756 {
3757 foreach_list_typed(nir_cf_node, node, node, list)
3758 {
3759 switch (node->type) {
3760 case nir_cf_node_block:
3761 visit_block(ctx, nir_cf_node_as_block(node));
3762 break;
3763
3764 case nir_cf_node_if:
3765 visit_if(ctx, nir_cf_node_as_if(node));
3766 break;
3767
3768 case nir_cf_node_loop:
3769 visit_loop(ctx, nir_cf_node_as_loop(node));
3770 break;
3771
3772 default:
3773 assert(0);
3774 }
3775 }
3776 }
3777
3778 void
3779 ac_handle_shader_output_decl(struct ac_llvm_context *ctx,
3780 struct ac_shader_abi *abi,
3781 struct nir_shader *nir,
3782 struct nir_variable *variable,
3783 gl_shader_stage stage)
3784 {
3785 unsigned output_loc = variable->data.driver_location / 4;
3786 unsigned attrib_count = glsl_count_attribute_slots(variable->type, false);
3787
3788 /* tess ctrl has it's own load/store paths for outputs */
3789 if (stage == MESA_SHADER_TESS_CTRL)
3790 return;
3791
3792 if (stage == MESA_SHADER_VERTEX ||
3793 stage == MESA_SHADER_TESS_EVAL ||
3794 stage == MESA_SHADER_GEOMETRY) {
3795 int idx = variable->data.location + variable->data.index;
3796 if (idx == VARYING_SLOT_CLIP_DIST0) {
3797 int length = nir->info.clip_distance_array_size +
3798 nir->info.cull_distance_array_size;
3799
3800 if (length > 4)
3801 attrib_count = 2;
3802 else
3803 attrib_count = 1;
3804 }
3805 }
3806
3807 for (unsigned i = 0; i < attrib_count; ++i) {
3808 for (unsigned chan = 0; chan < 4; chan++) {
3809 abi->outputs[ac_llvm_reg_index_soa(output_loc + i, chan)] =
3810 ac_build_alloca_undef(ctx, ctx->f32, "");
3811 }
3812 }
3813 }
3814
3815 static LLVMTypeRef
3816 glsl_base_to_llvm_type(struct ac_llvm_context *ac,
3817 enum glsl_base_type type)
3818 {
3819 switch (type) {
3820 case GLSL_TYPE_INT:
3821 case GLSL_TYPE_UINT:
3822 case GLSL_TYPE_BOOL:
3823 case GLSL_TYPE_SUBROUTINE:
3824 return ac->i32;
3825 case GLSL_TYPE_FLOAT: /* TODO handle mediump */
3826 return ac->f32;
3827 case GLSL_TYPE_INT64:
3828 case GLSL_TYPE_UINT64:
3829 return ac->i64;
3830 case GLSL_TYPE_DOUBLE:
3831 return ac->f64;
3832 default:
3833 unreachable("unknown GLSL type");
3834 }
3835 }
3836
3837 static LLVMTypeRef
3838 glsl_to_llvm_type(struct ac_llvm_context *ac,
3839 const struct glsl_type *type)
3840 {
3841 if (glsl_type_is_scalar(type)) {
3842 return glsl_base_to_llvm_type(ac, glsl_get_base_type(type));
3843 }
3844
3845 if (glsl_type_is_vector(type)) {
3846 return LLVMVectorType(
3847 glsl_base_to_llvm_type(ac, glsl_get_base_type(type)),
3848 glsl_get_vector_elements(type));
3849 }
3850
3851 if (glsl_type_is_matrix(type)) {
3852 return LLVMArrayType(
3853 glsl_to_llvm_type(ac, glsl_get_column_type(type)),
3854 glsl_get_matrix_columns(type));
3855 }
3856
3857 if (glsl_type_is_array(type)) {
3858 return LLVMArrayType(
3859 glsl_to_llvm_type(ac, glsl_get_array_element(type)),
3860 glsl_get_length(type));
3861 }
3862
3863 assert(glsl_type_is_struct(type));
3864
3865 LLVMTypeRef member_types[glsl_get_length(type)];
3866
3867 for (unsigned i = 0; i < glsl_get_length(type); i++) {
3868 member_types[i] =
3869 glsl_to_llvm_type(ac,
3870 glsl_get_struct_field(type, i));
3871 }
3872
3873 return LLVMStructTypeInContext(ac->context, member_types,
3874 glsl_get_length(type), false);
3875 }
3876
3877 static void
3878 setup_locals(struct ac_nir_context *ctx,
3879 struct nir_function *func)
3880 {
3881 int i, j;
3882 ctx->num_locals = 0;
3883 nir_foreach_variable(variable, &func->impl->locals) {
3884 unsigned attrib_count = glsl_count_attribute_slots(variable->type, false);
3885 variable->data.driver_location = ctx->num_locals * 4;
3886 variable->data.location_frac = 0;
3887 ctx->num_locals += attrib_count;
3888 }
3889 ctx->locals = malloc(4 * ctx->num_locals * sizeof(LLVMValueRef));
3890 if (!ctx->locals)
3891 return;
3892
3893 for (i = 0; i < ctx->num_locals; i++) {
3894 for (j = 0; j < 4; j++) {
3895 ctx->locals[i * 4 + j] =
3896 ac_build_alloca_undef(&ctx->ac, ctx->ac.f32, "temp");
3897 }
3898 }
3899 }
3900
3901 static void
3902 setup_shared(struct ac_nir_context *ctx,
3903 struct nir_shader *nir)
3904 {
3905 nir_foreach_variable(variable, &nir->shared) {
3906 LLVMValueRef shared =
3907 LLVMAddGlobalInAddressSpace(
3908 ctx->ac.module, glsl_to_llvm_type(&ctx->ac, variable->type),
3909 variable->name ? variable->name : "",
3910 AC_LOCAL_ADDR_SPACE);
3911 _mesa_hash_table_insert(ctx->vars, variable, shared);
3912 }
3913 }
3914
3915 void ac_nir_translate(struct ac_llvm_context *ac, struct ac_shader_abi *abi,
3916 struct nir_shader *nir)
3917 {
3918 struct ac_nir_context ctx = {};
3919 struct nir_function *func;
3920
3921 ctx.ac = *ac;
3922 ctx.abi = abi;
3923
3924 ctx.stage = nir->info.stage;
3925
3926 ctx.main_function = LLVMGetBasicBlockParent(LLVMGetInsertBlock(ctx.ac.builder));
3927
3928 nir_foreach_variable(variable, &nir->outputs)
3929 ac_handle_shader_output_decl(&ctx.ac, ctx.abi, nir, variable,
3930 ctx.stage);
3931
3932 ctx.defs = _mesa_hash_table_create(NULL, _mesa_hash_pointer,
3933 _mesa_key_pointer_equal);
3934 ctx.phis = _mesa_hash_table_create(NULL, _mesa_hash_pointer,
3935 _mesa_key_pointer_equal);
3936 ctx.vars = _mesa_hash_table_create(NULL, _mesa_hash_pointer,
3937 _mesa_key_pointer_equal);
3938
3939 func = (struct nir_function *)exec_list_get_head(&nir->functions);
3940
3941 nir_index_ssa_defs(func->impl);
3942 ctx.ssa_defs = calloc(func->impl->ssa_alloc, sizeof(LLVMValueRef));
3943
3944 setup_locals(&ctx, func);
3945
3946 if (nir->info.stage == MESA_SHADER_COMPUTE)
3947 setup_shared(&ctx, nir);
3948
3949 visit_cf_list(&ctx, &func->impl->body);
3950 phi_post_pass(&ctx);
3951
3952 if (nir->info.stage != MESA_SHADER_COMPUTE)
3953 ctx.abi->emit_outputs(ctx.abi, AC_LLVM_MAX_OUTPUTS,
3954 ctx.abi->outputs);
3955
3956 free(ctx.locals);
3957 free(ctx.ssa_defs);
3958 ralloc_free(ctx.defs);
3959 ralloc_free(ctx.phis);
3960 ralloc_free(ctx.vars);
3961 }
3962
3963 void
3964 ac_lower_indirect_derefs(struct nir_shader *nir, enum chip_class chip_class)
3965 {
3966 /* While it would be nice not to have this flag, we are constrained
3967 * by the reality that LLVM 5.0 doesn't have working VGPR indexing
3968 * on GFX9.
3969 */
3970 bool llvm_has_working_vgpr_indexing = chip_class <= VI;
3971
3972 /* TODO: Indirect indexing of GS inputs is unimplemented.
3973 *
3974 * TCS and TES load inputs directly from LDS or offchip memory, so
3975 * indirect indexing is trivial.
3976 */
3977 nir_variable_mode indirect_mask = 0;
3978 if (nir->info.stage == MESA_SHADER_GEOMETRY ||
3979 (nir->info.stage != MESA_SHADER_TESS_CTRL &&
3980 nir->info.stage != MESA_SHADER_TESS_EVAL &&
3981 !llvm_has_working_vgpr_indexing)) {
3982 indirect_mask |= nir_var_shader_in;
3983 }
3984 if (!llvm_has_working_vgpr_indexing &&
3985 nir->info.stage != MESA_SHADER_TESS_CTRL)
3986 indirect_mask |= nir_var_shader_out;
3987
3988 /* TODO: We shouldn't need to do this, however LLVM isn't currently
3989 * smart enough to handle indirects without causing excess spilling
3990 * causing the gpu to hang.
3991 *
3992 * See the following thread for more details of the problem:
3993 * https://lists.freedesktop.org/archives/mesa-dev/2017-July/162106.html
3994 */
3995 indirect_mask |= nir_var_local;
3996
3997 nir_lower_indirect_derefs(nir, indirect_mask);
3998 }