ac/nir,radeonsi: add and use ac_shader_abi::frag_pos
[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 "../vulkan/radv_descriptor_set.h"
31 #include "util/bitscan.h"
32 #include <llvm-c/Transforms/Scalar.h>
33 #include "ac_shader_abi.h"
34 #include "ac_shader_info.h"
35 #include "ac_exp_param.h"
36
37 enum radeon_llvm_calling_convention {
38 RADEON_LLVM_AMDGPU_VS = 87,
39 RADEON_LLVM_AMDGPU_GS = 88,
40 RADEON_LLVM_AMDGPU_PS = 89,
41 RADEON_LLVM_AMDGPU_CS = 90,
42 };
43
44 #define CONST_ADDR_SPACE 2
45 #define LOCAL_ADDR_SPACE 3
46
47 #define RADEON_LLVM_MAX_INPUTS (VARYING_SLOT_VAR31 + 1)
48 #define RADEON_LLVM_MAX_OUTPUTS (VARYING_SLOT_VAR31 + 1)
49
50 struct nir_to_llvm_context;
51
52 struct ac_nir_context {
53 struct ac_llvm_context ac;
54 struct ac_shader_abi *abi;
55
56 gl_shader_stage stage;
57
58 struct hash_table *defs;
59 struct hash_table *phis;
60 struct hash_table *vars;
61
62 LLVMValueRef main_function;
63 LLVMBasicBlockRef continue_block;
64 LLVMBasicBlockRef break_block;
65
66 LLVMValueRef outputs[RADEON_LLVM_MAX_OUTPUTS * 4];
67
68 int num_locals;
69 LLVMValueRef *locals;
70
71 LLVMValueRef ddxy_lds;
72
73 struct nir_to_llvm_context *nctx; /* TODO get rid of this */
74 };
75
76 struct nir_to_llvm_context {
77 struct ac_llvm_context ac;
78 const struct ac_nir_compiler_options *options;
79 struct ac_shader_variant_info *shader_info;
80 struct ac_shader_abi abi;
81 struct ac_nir_context *nir;
82
83 unsigned max_workgroup_size;
84 LLVMContextRef context;
85 LLVMModuleRef module;
86 LLVMBuilderRef builder;
87 LLVMValueRef main_function;
88
89 struct hash_table *defs;
90 struct hash_table *phis;
91
92 LLVMValueRef descriptor_sets[AC_UD_MAX_SETS];
93 LLVMValueRef ring_offsets;
94 LLVMValueRef push_constants;
95 LLVMValueRef num_work_groups;
96 LLVMValueRef workgroup_ids;
97 LLVMValueRef local_invocation_ids;
98 LLVMValueRef tg_size;
99
100 LLVMValueRef vertex_buffers;
101 LLVMValueRef rel_auto_id;
102 LLVMValueRef vs_prim_id;
103 LLVMValueRef ls_out_layout;
104 LLVMValueRef es2gs_offset;
105
106 LLVMValueRef tcs_offchip_layout;
107 LLVMValueRef tcs_out_offsets;
108 LLVMValueRef tcs_out_layout;
109 LLVMValueRef tcs_in_layout;
110 LLVMValueRef oc_lds;
111 LLVMValueRef tess_factor_offset;
112 LLVMValueRef tcs_patch_id;
113 LLVMValueRef tcs_rel_ids;
114 LLVMValueRef tes_rel_patch_id;
115 LLVMValueRef tes_patch_id;
116 LLVMValueRef tes_u;
117 LLVMValueRef tes_v;
118
119 LLVMValueRef gsvs_ring_stride;
120 LLVMValueRef gsvs_num_entries;
121 LLVMValueRef gs2vs_offset;
122 LLVMValueRef gs_wave_id;
123 LLVMValueRef gs_vtx_offset[6];
124 LLVMValueRef gs_prim_id, gs_invocation_id;
125
126 LLVMValueRef esgs_ring;
127 LLVMValueRef gsvs_ring;
128 LLVMValueRef hs_ring_tess_offchip;
129 LLVMValueRef hs_ring_tess_factor;
130
131 LLVMValueRef prim_mask;
132 LLVMValueRef sample_pos_offset;
133 LLVMValueRef persp_sample, persp_center, persp_centroid;
134 LLVMValueRef linear_sample, linear_center, linear_centroid;
135
136 LLVMTypeRef i1;
137 LLVMTypeRef i8;
138 LLVMTypeRef i16;
139 LLVMTypeRef i32;
140 LLVMTypeRef i64;
141 LLVMTypeRef v2i32;
142 LLVMTypeRef v3i32;
143 LLVMTypeRef v4i32;
144 LLVMTypeRef v8i32;
145 LLVMTypeRef f64;
146 LLVMTypeRef f32;
147 LLVMTypeRef f16;
148 LLVMTypeRef v2f32;
149 LLVMTypeRef v4f32;
150 LLVMTypeRef voidt;
151
152 LLVMValueRef i1true;
153 LLVMValueRef i1false;
154 LLVMValueRef i32zero;
155 LLVMValueRef i32one;
156 LLVMValueRef f32zero;
157 LLVMValueRef f32one;
158 LLVMValueRef v4f32empty;
159
160 unsigned uniform_md_kind;
161 LLVMValueRef empty_md;
162 gl_shader_stage stage;
163
164 LLVMValueRef lds;
165 LLVMValueRef inputs[RADEON_LLVM_MAX_INPUTS * 4];
166
167 uint64_t input_mask;
168 uint64_t output_mask;
169 uint8_t num_output_clips;
170 uint8_t num_output_culls;
171
172 bool is_gs_copy_shader;
173 LLVMValueRef gs_next_vertex;
174 unsigned gs_max_out_vertices;
175
176 unsigned tes_primitive_mode;
177 uint64_t tess_outputs_written;
178 uint64_t tess_patch_outputs_written;
179 };
180
181 static inline struct nir_to_llvm_context *
182 nir_to_llvm_context_from_abi(struct ac_shader_abi *abi)
183 {
184 struct nir_to_llvm_context *ctx = NULL;
185 return container_of(abi, ctx, abi);
186 }
187
188 static LLVMValueRef get_sampler_desc(struct ac_nir_context *ctx,
189 const nir_deref_var *deref,
190 enum ac_descriptor_type desc_type,
191 bool image, bool write);
192
193 static unsigned radeon_llvm_reg_index_soa(unsigned index, unsigned chan)
194 {
195 return (index * 4) + chan;
196 }
197
198 static unsigned shader_io_get_unique_index(gl_varying_slot slot)
199 {
200 /* handle patch indices separate */
201 if (slot == VARYING_SLOT_TESS_LEVEL_OUTER)
202 return 0;
203 if (slot == VARYING_SLOT_TESS_LEVEL_INNER)
204 return 1;
205 if (slot >= VARYING_SLOT_PATCH0 && slot <= VARYING_SLOT_TESS_MAX)
206 return 2 + (slot - VARYING_SLOT_PATCH0);
207
208 if (slot == VARYING_SLOT_POS)
209 return 0;
210 if (slot == VARYING_SLOT_PSIZ)
211 return 1;
212 if (slot == VARYING_SLOT_CLIP_DIST0)
213 return 2;
214 /* 3 is reserved for clip dist as well */
215 if (slot >= VARYING_SLOT_VAR0 && slot <= VARYING_SLOT_VAR31)
216 return 4 + (slot - VARYING_SLOT_VAR0);
217 unreachable("illegal slot in get unique index\n");
218 }
219
220 static unsigned llvm_get_type_size(LLVMTypeRef type)
221 {
222 LLVMTypeKind kind = LLVMGetTypeKind(type);
223
224 switch (kind) {
225 case LLVMIntegerTypeKind:
226 return LLVMGetIntTypeWidth(type) / 8;
227 case LLVMFloatTypeKind:
228 return 4;
229 case LLVMPointerTypeKind:
230 return 8;
231 case LLVMVectorTypeKind:
232 return LLVMGetVectorSize(type) *
233 llvm_get_type_size(LLVMGetElementType(type));
234 default:
235 assert(0);
236 return 0;
237 }
238 }
239
240 static void set_llvm_calling_convention(LLVMValueRef func,
241 gl_shader_stage stage)
242 {
243 enum radeon_llvm_calling_convention calling_conv;
244
245 switch (stage) {
246 case MESA_SHADER_VERTEX:
247 case MESA_SHADER_TESS_CTRL:
248 case MESA_SHADER_TESS_EVAL:
249 calling_conv = RADEON_LLVM_AMDGPU_VS;
250 break;
251 case MESA_SHADER_GEOMETRY:
252 calling_conv = RADEON_LLVM_AMDGPU_GS;
253 break;
254 case MESA_SHADER_FRAGMENT:
255 calling_conv = RADEON_LLVM_AMDGPU_PS;
256 break;
257 case MESA_SHADER_COMPUTE:
258 calling_conv = RADEON_LLVM_AMDGPU_CS;
259 break;
260 default:
261 unreachable("Unhandle shader type");
262 }
263
264 LLVMSetFunctionCallConv(func, calling_conv);
265 }
266
267 #define MAX_ARGS 23
268 struct arg_info {
269 LLVMTypeRef types[MAX_ARGS];
270 LLVMValueRef *assign[MAX_ARGS];
271 unsigned array_params_mask;
272 uint8_t count;
273 uint8_t user_sgpr_count;
274 uint8_t sgpr_count;
275 uint8_t num_user_sgprs_used;
276 uint8_t num_sgprs_used;
277 uint8_t num_vgprs_used;
278 };
279
280 static inline void
281 add_argument(struct arg_info *info,
282 LLVMTypeRef type, LLVMValueRef *param_ptr)
283 {
284 assert(info->count < MAX_ARGS);
285 info->assign[info->count] = param_ptr;
286 info->types[info->count] = type;
287 info->count++;
288 }
289
290 static inline void
291 add_sgpr_argument(struct arg_info *info,
292 LLVMTypeRef type, LLVMValueRef *param_ptr)
293 {
294 add_argument(info, type, param_ptr);
295 info->num_sgprs_used += llvm_get_type_size(type) / 4;
296 info->sgpr_count++;
297 }
298
299 static inline void
300 add_user_sgpr_argument(struct arg_info *info,
301 LLVMTypeRef type,
302 LLVMValueRef *param_ptr)
303 {
304 add_sgpr_argument(info, type, param_ptr);
305 info->num_user_sgprs_used += llvm_get_type_size(type) / 4;
306 info->user_sgpr_count++;
307 }
308
309 static inline void
310 add_vgpr_argument(struct arg_info *info,
311 LLVMTypeRef type,
312 LLVMValueRef *param_ptr)
313 {
314 add_argument(info, type, param_ptr);
315 info->num_vgprs_used += llvm_get_type_size(type) / 4;
316 }
317
318 static inline void
319 add_user_sgpr_array_argument(struct arg_info *info,
320 LLVMTypeRef type,
321 LLVMValueRef *param_ptr)
322 {
323 info->array_params_mask |= (1 << info->count);
324 add_user_sgpr_argument(info, type, param_ptr);
325 }
326
327 static void assign_arguments(LLVMValueRef main_function,
328 struct arg_info *info)
329 {
330 unsigned i;
331 for (i = 0; i < info->count; i++) {
332 if (info->assign[i])
333 *info->assign[i] = LLVMGetParam(main_function, i);
334 }
335 }
336
337 static LLVMValueRef
338 create_llvm_function(LLVMContextRef ctx, LLVMModuleRef module,
339 LLVMBuilderRef builder, LLVMTypeRef *return_types,
340 unsigned num_return_elems,
341 struct arg_info *args,
342 unsigned max_workgroup_size,
343 bool unsafe_math)
344 {
345 LLVMTypeRef main_function_type, ret_type;
346 LLVMBasicBlockRef main_function_body;
347
348 if (num_return_elems)
349 ret_type = LLVMStructTypeInContext(ctx, return_types,
350 num_return_elems, true);
351 else
352 ret_type = LLVMVoidTypeInContext(ctx);
353
354 /* Setup the function */
355 main_function_type =
356 LLVMFunctionType(ret_type, args->types, args->count, 0);
357 LLVMValueRef main_function =
358 LLVMAddFunction(module, "main", main_function_type);
359 main_function_body =
360 LLVMAppendBasicBlockInContext(ctx, main_function, "main_body");
361 LLVMPositionBuilderAtEnd(builder, main_function_body);
362
363 LLVMSetFunctionCallConv(main_function, RADEON_LLVM_AMDGPU_CS);
364 for (unsigned i = 0; i < args->sgpr_count; ++i) {
365 if (args->array_params_mask & (1 << i)) {
366 LLVMValueRef P = LLVMGetParam(main_function, i);
367 ac_add_function_attr(ctx, main_function, i + 1, AC_FUNC_ATTR_BYVAL);
368 ac_add_attr_dereferenceable(P, UINT64_MAX);
369 }
370 else {
371 ac_add_function_attr(ctx, main_function, i + 1, AC_FUNC_ATTR_INREG);
372 }
373 }
374
375 if (max_workgroup_size) {
376 ac_llvm_add_target_dep_function_attr(main_function,
377 "amdgpu-max-work-group-size",
378 max_workgroup_size);
379 }
380 if (unsafe_math) {
381 /* These were copied from some LLVM test. */
382 LLVMAddTargetDependentFunctionAttr(main_function,
383 "less-precise-fpmad",
384 "true");
385 LLVMAddTargetDependentFunctionAttr(main_function,
386 "no-infs-fp-math",
387 "true");
388 LLVMAddTargetDependentFunctionAttr(main_function,
389 "no-nans-fp-math",
390 "true");
391 LLVMAddTargetDependentFunctionAttr(main_function,
392 "unsafe-fp-math",
393 "true");
394 }
395 return main_function;
396 }
397
398 static LLVMTypeRef const_array(LLVMTypeRef elem_type, int num_elements)
399 {
400 return LLVMPointerType(LLVMArrayType(elem_type, num_elements),
401 CONST_ADDR_SPACE);
402 }
403
404 static LLVMTypeRef to_integer_type_scalar(struct ac_llvm_context *ctx, LLVMTypeRef t)
405 {
406 if (t == ctx->f16 || t == ctx->i16)
407 return ctx->i16;
408 else if (t == ctx->f32 || t == ctx->i32)
409 return ctx->i32;
410 else if (t == ctx->f64 || t == ctx->i64)
411 return ctx->i64;
412 else
413 unreachable("Unhandled integer size");
414 }
415
416 static LLVMTypeRef to_integer_type(struct ac_llvm_context *ctx, LLVMTypeRef t)
417 {
418 if (LLVMGetTypeKind(t) == LLVMVectorTypeKind) {
419 LLVMTypeRef elem_type = LLVMGetElementType(t);
420 return LLVMVectorType(to_integer_type_scalar(ctx, elem_type),
421 LLVMGetVectorSize(t));
422 }
423 return to_integer_type_scalar(ctx, t);
424 }
425
426 static LLVMValueRef to_integer(struct ac_llvm_context *ctx, LLVMValueRef v)
427 {
428 LLVMTypeRef type = LLVMTypeOf(v);
429 return LLVMBuildBitCast(ctx->builder, v, to_integer_type(ctx, type), "");
430 }
431
432 static LLVMTypeRef to_float_type_scalar(struct ac_llvm_context *ctx, LLVMTypeRef t)
433 {
434 if (t == ctx->i16 || t == ctx->f16)
435 return ctx->f16;
436 else if (t == ctx->i32 || t == ctx->f32)
437 return ctx->f32;
438 else if (t == ctx->i64 || t == ctx->f64)
439 return ctx->f64;
440 else
441 unreachable("Unhandled float size");
442 }
443
444 static LLVMTypeRef to_float_type(struct ac_llvm_context *ctx, LLVMTypeRef t)
445 {
446 if (LLVMGetTypeKind(t) == LLVMVectorTypeKind) {
447 LLVMTypeRef elem_type = LLVMGetElementType(t);
448 return LLVMVectorType(to_float_type_scalar(ctx, elem_type),
449 LLVMGetVectorSize(t));
450 }
451 return to_float_type_scalar(ctx, t);
452 }
453
454 static LLVMValueRef to_float(struct ac_llvm_context *ctx, LLVMValueRef v)
455 {
456 LLVMTypeRef type = LLVMTypeOf(v);
457 return LLVMBuildBitCast(ctx->builder, v, to_float_type(ctx, type), "");
458 }
459
460 static int get_elem_bits(struct ac_llvm_context *ctx, LLVMTypeRef type)
461 {
462 if (LLVMGetTypeKind(type) == LLVMVectorTypeKind)
463 type = LLVMGetElementType(type);
464
465 if (LLVMGetTypeKind(type) == LLVMIntegerTypeKind)
466 return LLVMGetIntTypeWidth(type);
467
468 if (type == ctx->f16)
469 return 16;
470 if (type == ctx->f32)
471 return 32;
472 if (type == ctx->f64)
473 return 64;
474
475 unreachable("Unhandled type kind in get_elem_bits");
476 }
477
478 static LLVMValueRef unpack_param(struct nir_to_llvm_context *ctx,
479 LLVMValueRef param, unsigned rshift,
480 unsigned bitwidth)
481 {
482 LLVMValueRef value = param;
483 if (rshift)
484 value = LLVMBuildLShr(ctx->builder, value,
485 LLVMConstInt(ctx->i32, rshift, false), "");
486
487 if (rshift + bitwidth < 32) {
488 unsigned mask = (1 << bitwidth) - 1;
489 value = LLVMBuildAnd(ctx->builder, value,
490 LLVMConstInt(ctx->i32, mask, false), "");
491 }
492 return value;
493 }
494
495 static LLVMValueRef get_rel_patch_id(struct nir_to_llvm_context *ctx)
496 {
497 switch (ctx->stage) {
498 case MESA_SHADER_TESS_CTRL:
499 return unpack_param(ctx, ctx->tcs_rel_ids, 0, 8);
500 case MESA_SHADER_TESS_EVAL:
501 return ctx->tes_rel_patch_id;
502 break;
503 default:
504 unreachable("Illegal stage");
505 }
506 }
507
508 /* Tessellation shaders pass outputs to the next shader using LDS.
509 *
510 * LS outputs = TCS inputs
511 * TCS outputs = TES inputs
512 *
513 * The LDS layout is:
514 * - TCS inputs for patch 0
515 * - TCS inputs for patch 1
516 * - TCS inputs for patch 2 = get_tcs_in_current_patch_offset (if RelPatchID==2)
517 * - ...
518 * - TCS outputs for patch 0 = get_tcs_out_patch0_offset
519 * - Per-patch TCS outputs for patch 0 = get_tcs_out_patch0_patch_data_offset
520 * - TCS outputs for patch 1
521 * - Per-patch TCS outputs for patch 1
522 * - TCS outputs for patch 2 = get_tcs_out_current_patch_offset (if RelPatchID==2)
523 * - Per-patch TCS outputs for patch 2 = get_tcs_out_current_patch_data_offset (if RelPatchID==2)
524 * - ...
525 *
526 * All three shaders VS(LS), TCS, TES share the same LDS space.
527 */
528 static LLVMValueRef
529 get_tcs_in_patch_stride(struct nir_to_llvm_context *ctx)
530 {
531 if (ctx->stage == MESA_SHADER_VERTEX)
532 return unpack_param(ctx, ctx->ls_out_layout, 0, 13);
533 else if (ctx->stage == MESA_SHADER_TESS_CTRL)
534 return unpack_param(ctx, ctx->tcs_in_layout, 0, 13);
535 else {
536 assert(0);
537 return NULL;
538 }
539 }
540
541 static LLVMValueRef
542 get_tcs_out_patch_stride(struct nir_to_llvm_context *ctx)
543 {
544 return unpack_param(ctx, ctx->tcs_out_layout, 0, 13);
545 }
546
547 static LLVMValueRef
548 get_tcs_out_patch0_offset(struct nir_to_llvm_context *ctx)
549 {
550 return LLVMBuildMul(ctx->builder,
551 unpack_param(ctx, ctx->tcs_out_offsets, 0, 16),
552 LLVMConstInt(ctx->i32, 4, false), "");
553 }
554
555 static LLVMValueRef
556 get_tcs_out_patch0_patch_data_offset(struct nir_to_llvm_context *ctx)
557 {
558 return LLVMBuildMul(ctx->builder,
559 unpack_param(ctx, ctx->tcs_out_offsets, 16, 16),
560 LLVMConstInt(ctx->i32, 4, false), "");
561 }
562
563 static LLVMValueRef
564 get_tcs_in_current_patch_offset(struct nir_to_llvm_context *ctx)
565 {
566 LLVMValueRef patch_stride = get_tcs_in_patch_stride(ctx);
567 LLVMValueRef rel_patch_id = get_rel_patch_id(ctx);
568
569 return LLVMBuildMul(ctx->builder, patch_stride, rel_patch_id, "");
570 }
571
572 static LLVMValueRef
573 get_tcs_out_current_patch_offset(struct nir_to_llvm_context *ctx)
574 {
575 LLVMValueRef patch0_offset = get_tcs_out_patch0_offset(ctx);
576 LLVMValueRef patch_stride = get_tcs_out_patch_stride(ctx);
577 LLVMValueRef rel_patch_id = get_rel_patch_id(ctx);
578
579 return LLVMBuildAdd(ctx->builder, patch0_offset,
580 LLVMBuildMul(ctx->builder, patch_stride,
581 rel_patch_id, ""),
582 "");
583 }
584
585 static LLVMValueRef
586 get_tcs_out_current_patch_data_offset(struct nir_to_llvm_context *ctx)
587 {
588 LLVMValueRef patch0_patch_data_offset =
589 get_tcs_out_patch0_patch_data_offset(ctx);
590 LLVMValueRef patch_stride = get_tcs_out_patch_stride(ctx);
591 LLVMValueRef rel_patch_id = get_rel_patch_id(ctx);
592
593 return LLVMBuildAdd(ctx->builder, patch0_patch_data_offset,
594 LLVMBuildMul(ctx->builder, patch_stride,
595 rel_patch_id, ""),
596 "");
597 }
598
599 static void set_userdata_location(struct ac_userdata_info *ud_info, uint8_t *sgpr_idx, uint8_t num_sgprs)
600 {
601 ud_info->sgpr_idx = *sgpr_idx;
602 ud_info->num_sgprs = num_sgprs;
603 ud_info->indirect = false;
604 ud_info->indirect_offset = 0;
605 *sgpr_idx += num_sgprs;
606 }
607
608 static void set_userdata_location_shader(struct nir_to_llvm_context *ctx,
609 int idx, uint8_t *sgpr_idx, uint8_t num_sgprs)
610 {
611 set_userdata_location(&ctx->shader_info->user_sgprs_locs.shader_data[idx], sgpr_idx, num_sgprs);
612 }
613
614
615 static void set_userdata_location_indirect(struct ac_userdata_info *ud_info, uint8_t sgpr_idx, uint8_t num_sgprs,
616 uint32_t indirect_offset)
617 {
618 ud_info->sgpr_idx = sgpr_idx;
619 ud_info->num_sgprs = num_sgprs;
620 ud_info->indirect = true;
621 ud_info->indirect_offset = indirect_offset;
622 }
623
624 static void declare_tess_lds(struct nir_to_llvm_context *ctx)
625 {
626 unsigned lds_size = ctx->options->chip_class >= CIK ? 65536 : 32768;
627 ctx->lds = LLVMBuildIntToPtr(ctx->builder, ctx->i32zero,
628 LLVMPointerType(LLVMArrayType(ctx->i32, lds_size / 4), LOCAL_ADDR_SPACE),
629 "tess_lds");
630 }
631
632 struct user_sgpr_info {
633 bool need_ring_offsets;
634 uint8_t sgpr_count;
635 bool indirect_all_descriptor_sets;
636 };
637
638 static void allocate_user_sgprs(struct nir_to_llvm_context *ctx,
639 struct user_sgpr_info *user_sgpr_info)
640 {
641 memset(user_sgpr_info, 0, sizeof(struct user_sgpr_info));
642
643 /* until we sort out scratch/global buffers always assign ring offsets for gs/vs/es */
644 if (ctx->stage == MESA_SHADER_GEOMETRY ||
645 ctx->stage == MESA_SHADER_VERTEX ||
646 ctx->stage == MESA_SHADER_TESS_CTRL ||
647 ctx->stage == MESA_SHADER_TESS_EVAL ||
648 ctx->is_gs_copy_shader)
649 user_sgpr_info->need_ring_offsets = true;
650
651 if (ctx->stage == MESA_SHADER_FRAGMENT &&
652 ctx->shader_info->info.ps.needs_sample_positions)
653 user_sgpr_info->need_ring_offsets = true;
654
655 /* 2 user sgprs will nearly always be allocated for scratch/rings */
656 if (ctx->options->supports_spill || user_sgpr_info->need_ring_offsets) {
657 user_sgpr_info->sgpr_count += 2;
658 }
659
660 switch (ctx->stage) {
661 case MESA_SHADER_COMPUTE:
662 user_sgpr_info->sgpr_count += ctx->shader_info->info.cs.grid_components_used;
663 break;
664 case MESA_SHADER_FRAGMENT:
665 user_sgpr_info->sgpr_count += ctx->shader_info->info.ps.needs_sample_positions;
666 break;
667 case MESA_SHADER_VERTEX:
668 if (!ctx->is_gs_copy_shader) {
669 user_sgpr_info->sgpr_count += ctx->shader_info->info.vs.has_vertex_buffers ? 2 : 0;
670 if (ctx->shader_info->info.vs.needs_draw_id) {
671 user_sgpr_info->sgpr_count += 3;
672 } else {
673 user_sgpr_info->sgpr_count += 2;
674 }
675 }
676 if (ctx->options->key.vs.as_ls)
677 user_sgpr_info->sgpr_count++;
678 break;
679 case MESA_SHADER_TESS_CTRL:
680 user_sgpr_info->sgpr_count += 4;
681 break;
682 case MESA_SHADER_TESS_EVAL:
683 user_sgpr_info->sgpr_count += 1;
684 break;
685 case MESA_SHADER_GEOMETRY:
686 user_sgpr_info->sgpr_count += 2;
687 break;
688 default:
689 break;
690 }
691
692 if (ctx->shader_info->info.needs_push_constants)
693 user_sgpr_info->sgpr_count += 2;
694
695 uint32_t remaining_sgprs = 16 - user_sgpr_info->sgpr_count;
696 if (remaining_sgprs / 2 < util_bitcount(ctx->shader_info->info.desc_set_used_mask)) {
697 user_sgpr_info->sgpr_count += 2;
698 user_sgpr_info->indirect_all_descriptor_sets = true;
699 } else {
700 user_sgpr_info->sgpr_count += util_bitcount(ctx->shader_info->info.desc_set_used_mask) * 2;
701 }
702 }
703
704 static void create_function(struct nir_to_llvm_context *ctx)
705 {
706 unsigned num_sets = ctx->options->layout ? ctx->options->layout->num_sets : 0;
707 uint8_t user_sgpr_idx;
708 struct user_sgpr_info user_sgpr_info;
709 struct arg_info args = {};
710 LLVMValueRef desc_sets;
711
712 allocate_user_sgprs(ctx, &user_sgpr_info);
713 if (user_sgpr_info.need_ring_offsets && !ctx->options->supports_spill) {
714 add_user_sgpr_argument(&args, const_array(ctx->v4i32, 16), &ctx->ring_offsets); /* address of rings */
715 }
716
717 /* 1 for each descriptor set */
718 if (!user_sgpr_info.indirect_all_descriptor_sets) {
719 for (unsigned i = 0; i < num_sets; ++i) {
720 if (ctx->options->layout->set[i].layout->shader_stages & (1 << ctx->stage)) {
721 add_user_sgpr_array_argument(&args, const_array(ctx->i8, 1024 * 1024), &ctx->descriptor_sets[i]);
722 }
723 }
724 } else
725 add_user_sgpr_array_argument(&args, const_array(const_array(ctx->i8, 1024 * 1024), 32), &desc_sets);
726
727 if (ctx->shader_info->info.needs_push_constants) {
728 /* 1 for push constants and dynamic descriptors */
729 add_user_sgpr_array_argument(&args, const_array(ctx->i8, 1024 * 1024), &ctx->push_constants);
730 }
731
732 switch (ctx->stage) {
733 case MESA_SHADER_COMPUTE:
734 if (ctx->shader_info->info.cs.grid_components_used)
735 add_user_sgpr_argument(&args, LLVMVectorType(ctx->i32, ctx->shader_info->info.cs.grid_components_used), &ctx->num_work_groups); /* grid size */
736 add_sgpr_argument(&args, LLVMVectorType(ctx->i32, 3), &ctx->workgroup_ids);
737 add_sgpr_argument(&args, ctx->i32, &ctx->tg_size);
738 add_vgpr_argument(&args, LLVMVectorType(ctx->i32, 3), &ctx->local_invocation_ids);
739 break;
740 case MESA_SHADER_VERTEX:
741 if (!ctx->is_gs_copy_shader) {
742 if (ctx->shader_info->info.vs.has_vertex_buffers)
743 add_user_sgpr_argument(&args, const_array(ctx->v4i32, 16), &ctx->vertex_buffers); /* vertex buffers */
744 add_user_sgpr_argument(&args, ctx->i32, &ctx->abi.base_vertex); // base vertex
745 add_user_sgpr_argument(&args, ctx->i32, &ctx->abi.start_instance);// start instance
746 if (ctx->shader_info->info.vs.needs_draw_id)
747 add_user_sgpr_argument(&args, ctx->i32, &ctx->abi.draw_id); // draw id
748 }
749 if (ctx->options->key.vs.as_es)
750 add_sgpr_argument(&args, ctx->i32, &ctx->es2gs_offset); // es2gs offset
751 else if (ctx->options->key.vs.as_ls)
752 add_user_sgpr_argument(&args, ctx->i32, &ctx->ls_out_layout); // ls out layout
753 add_vgpr_argument(&args, ctx->i32, &ctx->abi.vertex_id); // vertex id
754 if (!ctx->is_gs_copy_shader) {
755 add_vgpr_argument(&args, ctx->i32, &ctx->rel_auto_id); // rel auto id
756 add_vgpr_argument(&args, ctx->i32, &ctx->vs_prim_id); // vs prim id
757 add_vgpr_argument(&args, ctx->i32, &ctx->abi.instance_id); // instance id
758 }
759 break;
760 case MESA_SHADER_TESS_CTRL:
761 add_user_sgpr_argument(&args, ctx->i32, &ctx->tcs_offchip_layout); // tcs offchip layout
762 add_user_sgpr_argument(&args, ctx->i32, &ctx->tcs_out_offsets); // tcs out offsets
763 add_user_sgpr_argument(&args, ctx->i32, &ctx->tcs_out_layout); // tcs out layout
764 add_user_sgpr_argument(&args, ctx->i32, &ctx->tcs_in_layout); // tcs in layout
765 add_sgpr_argument(&args, ctx->i32, &ctx->oc_lds); // param oc lds
766 add_sgpr_argument(&args, ctx->i32, &ctx->tess_factor_offset); // tess factor offset
767 add_vgpr_argument(&args, ctx->i32, &ctx->tcs_patch_id); // patch id
768 add_vgpr_argument(&args, ctx->i32, &ctx->tcs_rel_ids); // rel ids;
769 break;
770 case MESA_SHADER_TESS_EVAL:
771 add_user_sgpr_argument(&args, ctx->i32, &ctx->tcs_offchip_layout); // tcs offchip layout
772 if (ctx->options->key.tes.as_es) {
773 add_sgpr_argument(&args, ctx->i32, &ctx->oc_lds); // OC LDS
774 add_sgpr_argument(&args, ctx->i32, NULL); //
775 add_sgpr_argument(&args, ctx->i32, &ctx->es2gs_offset); // es2gs offset
776 } else {
777 add_sgpr_argument(&args, ctx->i32, NULL); //
778 add_sgpr_argument(&args, ctx->i32, &ctx->oc_lds); // OC LDS
779 }
780 add_vgpr_argument(&args, ctx->f32, &ctx->tes_u); // tes_u
781 add_vgpr_argument(&args, ctx->f32, &ctx->tes_v); // tes_v
782 add_vgpr_argument(&args, ctx->i32, &ctx->tes_rel_patch_id); // tes rel patch id
783 add_vgpr_argument(&args, ctx->i32, &ctx->tes_patch_id); // tes patch id
784 break;
785 case MESA_SHADER_GEOMETRY:
786 add_user_sgpr_argument(&args, ctx->i32, &ctx->gsvs_ring_stride); // gsvs stride
787 add_user_sgpr_argument(&args, ctx->i32, &ctx->gsvs_num_entries); // gsvs num entires
788 add_sgpr_argument(&args, ctx->i32, &ctx->gs2vs_offset); // gs2vs offset
789 add_sgpr_argument(&args, ctx->i32, &ctx->gs_wave_id); // wave id
790 add_vgpr_argument(&args, ctx->i32, &ctx->gs_vtx_offset[0]); // vtx0
791 add_vgpr_argument(&args, ctx->i32, &ctx->gs_vtx_offset[1]); // vtx1
792 add_vgpr_argument(&args, ctx->i32, &ctx->gs_prim_id); // prim id
793 add_vgpr_argument(&args, ctx->i32, &ctx->gs_vtx_offset[2]);
794 add_vgpr_argument(&args, ctx->i32, &ctx->gs_vtx_offset[3]);
795 add_vgpr_argument(&args, ctx->i32, &ctx->gs_vtx_offset[4]);
796 add_vgpr_argument(&args, ctx->i32, &ctx->gs_vtx_offset[5]);
797 add_vgpr_argument(&args, ctx->i32, &ctx->gs_invocation_id);
798 break;
799 case MESA_SHADER_FRAGMENT:
800 if (ctx->shader_info->info.ps.needs_sample_positions)
801 add_user_sgpr_argument(&args, ctx->i32, &ctx->sample_pos_offset); /* sample position offset */
802 add_sgpr_argument(&args, ctx->i32, &ctx->prim_mask); /* prim mask */
803 add_vgpr_argument(&args, ctx->v2i32, &ctx->persp_sample); /* persp sample */
804 add_vgpr_argument(&args, ctx->v2i32, &ctx->persp_center); /* persp center */
805 add_vgpr_argument(&args, ctx->v2i32, &ctx->persp_centroid); /* persp centroid */
806 add_vgpr_argument(&args, ctx->v3i32, NULL); /* persp pull model */
807 add_vgpr_argument(&args, ctx->v2i32, &ctx->linear_sample); /* linear sample */
808 add_vgpr_argument(&args, ctx->v2i32, &ctx->linear_center); /* linear center */
809 add_vgpr_argument(&args, ctx->v2i32, &ctx->linear_centroid); /* linear centroid */
810 add_vgpr_argument(&args, ctx->f32, NULL); /* line stipple tex */
811 add_vgpr_argument(&args, ctx->f32, &ctx->abi.frag_pos[0]); /* pos x float */
812 add_vgpr_argument(&args, ctx->f32, &ctx->abi.frag_pos[1]); /* pos y float */
813 add_vgpr_argument(&args, ctx->f32, &ctx->abi.frag_pos[2]); /* pos z float */
814 add_vgpr_argument(&args, ctx->f32, &ctx->abi.frag_pos[3]); /* pos w float */
815 add_vgpr_argument(&args, ctx->i32, &ctx->abi.front_face); /* front face */
816 add_vgpr_argument(&args, ctx->i32, &ctx->abi.ancillary); /* ancillary */
817 add_vgpr_argument(&args, ctx->i32, &ctx->abi.sample_coverage); /* sample coverage */
818 add_vgpr_argument(&args, ctx->i32, NULL); /* fixed pt */
819 break;
820 default:
821 unreachable("Shader stage not implemented");
822 }
823
824 ctx->main_function = create_llvm_function(
825 ctx->context, ctx->module, ctx->builder, NULL, 0, &args,
826 ctx->max_workgroup_size,
827 ctx->options->unsafe_math);
828 set_llvm_calling_convention(ctx->main_function, ctx->stage);
829
830
831 ctx->shader_info->num_input_vgprs = 0;
832 ctx->shader_info->num_input_sgprs = ctx->shader_info->num_user_sgprs =
833 ctx->options->supports_spill ? 2 : 0;
834
835 ctx->shader_info->num_user_sgprs += args.num_user_sgprs_used;
836 ctx->shader_info->num_input_sgprs += args.num_sgprs_used;
837
838 if (ctx->stage != MESA_SHADER_FRAGMENT)
839 ctx->shader_info->num_input_vgprs = args.num_vgprs_used;
840
841 assign_arguments(ctx->main_function, &args);
842
843 user_sgpr_idx = 0;
844
845 if (ctx->options->supports_spill || user_sgpr_info.need_ring_offsets) {
846 set_userdata_location_shader(ctx, AC_UD_SCRATCH_RING_OFFSETS, &user_sgpr_idx, 2);
847 if (ctx->options->supports_spill) {
848 ctx->ring_offsets = ac_build_intrinsic(&ctx->ac, "llvm.amdgcn.implicit.buffer.ptr",
849 LLVMPointerType(ctx->i8, CONST_ADDR_SPACE),
850 NULL, 0, AC_FUNC_ATTR_READNONE);
851 ctx->ring_offsets = LLVMBuildBitCast(ctx->builder, ctx->ring_offsets,
852 const_array(ctx->v4i32, 16), "");
853 }
854 }
855
856 if (!user_sgpr_info.indirect_all_descriptor_sets) {
857 for (unsigned i = 0; i < num_sets; ++i) {
858 if (ctx->options->layout->set[i].layout->shader_stages & (1 << ctx->stage)) {
859 set_userdata_location(&ctx->shader_info->user_sgprs_locs.descriptor_sets[i], &user_sgpr_idx, 2);
860 } else
861 ctx->descriptor_sets[i] = NULL;
862 }
863 } else {
864 uint32_t desc_sgpr_idx = user_sgpr_idx;
865 set_userdata_location_shader(ctx, AC_UD_INDIRECT_DESCRIPTOR_SETS, &user_sgpr_idx, 2);
866
867 for (unsigned i = 0; i < num_sets; ++i) {
868 if (ctx->options->layout->set[i].layout->shader_stages & (1 << ctx->stage)) {
869 set_userdata_location_indirect(&ctx->shader_info->user_sgprs_locs.descriptor_sets[i], desc_sgpr_idx, 2, i * 8);
870 ctx->descriptor_sets[i] = ac_build_indexed_load_const(&ctx->ac, desc_sets, LLVMConstInt(ctx->i32, i, false));
871
872 } else
873 ctx->descriptor_sets[i] = NULL;
874 }
875 ctx->shader_info->need_indirect_descriptor_sets = true;
876 }
877
878 if (ctx->shader_info->info.needs_push_constants) {
879 set_userdata_location_shader(ctx, AC_UD_PUSH_CONSTANTS, &user_sgpr_idx, 2);
880 }
881
882 switch (ctx->stage) {
883 case MESA_SHADER_COMPUTE:
884 if (ctx->shader_info->info.cs.grid_components_used) {
885 set_userdata_location_shader(ctx, AC_UD_CS_GRID_SIZE, &user_sgpr_idx, ctx->shader_info->info.cs.grid_components_used);
886 }
887 break;
888 case MESA_SHADER_VERTEX:
889 if (!ctx->is_gs_copy_shader) {
890 if (ctx->shader_info->info.vs.has_vertex_buffers) {
891 set_userdata_location_shader(ctx, AC_UD_VS_VERTEX_BUFFERS, &user_sgpr_idx, 2);
892 }
893 unsigned vs_num = 2;
894 if (ctx->shader_info->info.vs.needs_draw_id)
895 vs_num++;
896
897 set_userdata_location_shader(ctx, AC_UD_VS_BASE_VERTEX_START_INSTANCE, &user_sgpr_idx, vs_num);
898 }
899 if (ctx->options->key.vs.as_ls) {
900 set_userdata_location_shader(ctx, AC_UD_VS_LS_TCS_IN_LAYOUT, &user_sgpr_idx, 1);
901 }
902 if (ctx->options->key.vs.as_ls)
903 declare_tess_lds(ctx);
904 break;
905 case MESA_SHADER_TESS_CTRL:
906 set_userdata_location_shader(ctx, AC_UD_TCS_OFFCHIP_LAYOUT, &user_sgpr_idx, 4);
907 declare_tess_lds(ctx);
908 break;
909 case MESA_SHADER_TESS_EVAL:
910 set_userdata_location_shader(ctx, AC_UD_TES_OFFCHIP_LAYOUT, &user_sgpr_idx, 1);
911 break;
912 case MESA_SHADER_GEOMETRY:
913 set_userdata_location_shader(ctx, AC_UD_GS_VS_RING_STRIDE_ENTRIES, &user_sgpr_idx, 2);
914 break;
915 case MESA_SHADER_FRAGMENT:
916 if (ctx->shader_info->info.ps.needs_sample_positions) {
917 set_userdata_location_shader(ctx, AC_UD_PS_SAMPLE_POS_OFFSET, &user_sgpr_idx, 1);
918 }
919 break;
920 default:
921 unreachable("Shader stage not implemented");
922 }
923 }
924
925 static void setup_types(struct nir_to_llvm_context *ctx)
926 {
927 LLVMValueRef args[4];
928
929 ctx->voidt = LLVMVoidTypeInContext(ctx->context);
930 ctx->i1 = LLVMIntTypeInContext(ctx->context, 1);
931 ctx->i8 = LLVMIntTypeInContext(ctx->context, 8);
932 ctx->i16 = LLVMIntTypeInContext(ctx->context, 16);
933 ctx->i32 = LLVMIntTypeInContext(ctx->context, 32);
934 ctx->i64 = LLVMIntTypeInContext(ctx->context, 64);
935 ctx->v2i32 = LLVMVectorType(ctx->i32, 2);
936 ctx->v3i32 = LLVMVectorType(ctx->i32, 3);
937 ctx->v4i32 = LLVMVectorType(ctx->i32, 4);
938 ctx->v8i32 = LLVMVectorType(ctx->i32, 8);
939 ctx->f32 = LLVMFloatTypeInContext(ctx->context);
940 ctx->f16 = LLVMHalfTypeInContext(ctx->context);
941 ctx->f64 = LLVMDoubleTypeInContext(ctx->context);
942 ctx->v2f32 = LLVMVectorType(ctx->f32, 2);
943 ctx->v4f32 = LLVMVectorType(ctx->f32, 4);
944
945 ctx->i1false = LLVMConstInt(ctx->i1, 0, false);
946 ctx->i1true = LLVMConstInt(ctx->i1, 1, false);
947 ctx->i32zero = LLVMConstInt(ctx->i32, 0, false);
948 ctx->i32one = LLVMConstInt(ctx->i32, 1, false);
949 ctx->f32zero = LLVMConstReal(ctx->f32, 0.0);
950 ctx->f32one = LLVMConstReal(ctx->f32, 1.0);
951
952 args[0] = ctx->f32zero;
953 args[1] = ctx->f32zero;
954 args[2] = ctx->f32zero;
955 args[3] = ctx->f32one;
956 ctx->v4f32empty = LLVMConstVector(args, 4);
957
958 ctx->uniform_md_kind =
959 LLVMGetMDKindIDInContext(ctx->context, "amdgpu.uniform", 14);
960 ctx->empty_md = LLVMMDNodeInContext(ctx->context, NULL, 0);
961
962 args[0] = LLVMConstReal(ctx->f32, 2.5);
963 }
964
965 static int get_llvm_num_components(LLVMValueRef value)
966 {
967 LLVMTypeRef type = LLVMTypeOf(value);
968 unsigned num_components = LLVMGetTypeKind(type) == LLVMVectorTypeKind
969 ? LLVMGetVectorSize(type)
970 : 1;
971 return num_components;
972 }
973
974 static LLVMValueRef llvm_extract_elem(struct ac_llvm_context *ac,
975 LLVMValueRef value,
976 int index)
977 {
978 int count = get_llvm_num_components(value);
979
980 assert(index < count);
981 if (count == 1)
982 return value;
983
984 return LLVMBuildExtractElement(ac->builder, value,
985 LLVMConstInt(ac->i32, index, false), "");
986 }
987
988 static LLVMValueRef trim_vector(struct ac_llvm_context *ctx,
989 LLVMValueRef value, unsigned count)
990 {
991 unsigned num_components = get_llvm_num_components(value);
992 if (count == num_components)
993 return value;
994
995 LLVMValueRef masks[] = {
996 LLVMConstInt(ctx->i32, 0, false), LLVMConstInt(ctx->i32, 1, false),
997 LLVMConstInt(ctx->i32, 2, false), LLVMConstInt(ctx->i32, 3, false)};
998
999 if (count == 1)
1000 return LLVMBuildExtractElement(ctx->builder, value, masks[0],
1001 "");
1002
1003 LLVMValueRef swizzle = LLVMConstVector(masks, count);
1004 return LLVMBuildShuffleVector(ctx->builder, value, value, swizzle, "");
1005 }
1006
1007 static void
1008 build_store_values_extended(struct ac_llvm_context *ac,
1009 LLVMValueRef *values,
1010 unsigned value_count,
1011 unsigned value_stride,
1012 LLVMValueRef vec)
1013 {
1014 LLVMBuilderRef builder = ac->builder;
1015 unsigned i;
1016
1017 for (i = 0; i < value_count; i++) {
1018 LLVMValueRef ptr = values[i * value_stride];
1019 LLVMValueRef index = LLVMConstInt(ac->i32, i, false);
1020 LLVMValueRef value = LLVMBuildExtractElement(builder, vec, index, "");
1021 LLVMBuildStore(builder, value, ptr);
1022 }
1023 }
1024
1025 static LLVMTypeRef get_def_type(struct ac_nir_context *ctx,
1026 const nir_ssa_def *def)
1027 {
1028 LLVMTypeRef type = LLVMIntTypeInContext(ctx->ac.context, def->bit_size);
1029 if (def->num_components > 1) {
1030 type = LLVMVectorType(type, def->num_components);
1031 }
1032 return type;
1033 }
1034
1035 static LLVMValueRef get_src(struct ac_nir_context *nir, nir_src src)
1036 {
1037 assert(src.is_ssa);
1038 struct hash_entry *entry = _mesa_hash_table_search(nir->defs, src.ssa);
1039 return (LLVMValueRef)entry->data;
1040 }
1041
1042
1043 static LLVMBasicBlockRef get_block(struct ac_nir_context *nir,
1044 const struct nir_block *b)
1045 {
1046 struct hash_entry *entry = _mesa_hash_table_search(nir->defs, b);
1047 return (LLVMBasicBlockRef)entry->data;
1048 }
1049
1050 static LLVMValueRef get_alu_src(struct ac_nir_context *ctx,
1051 nir_alu_src src,
1052 unsigned num_components)
1053 {
1054 LLVMValueRef value = get_src(ctx, src.src);
1055 bool need_swizzle = false;
1056
1057 assert(value);
1058 LLVMTypeRef type = LLVMTypeOf(value);
1059 unsigned src_components = LLVMGetTypeKind(type) == LLVMVectorTypeKind
1060 ? LLVMGetVectorSize(type)
1061 : 1;
1062
1063 for (unsigned i = 0; i < num_components; ++i) {
1064 assert(src.swizzle[i] < src_components);
1065 if (src.swizzle[i] != i)
1066 need_swizzle = true;
1067 }
1068
1069 if (need_swizzle || num_components != src_components) {
1070 LLVMValueRef masks[] = {
1071 LLVMConstInt(ctx->ac.i32, src.swizzle[0], false),
1072 LLVMConstInt(ctx->ac.i32, src.swizzle[1], false),
1073 LLVMConstInt(ctx->ac.i32, src.swizzle[2], false),
1074 LLVMConstInt(ctx->ac.i32, src.swizzle[3], false)};
1075
1076 if (src_components > 1 && num_components == 1) {
1077 value = LLVMBuildExtractElement(ctx->ac.builder, value,
1078 masks[0], "");
1079 } else if (src_components == 1 && num_components > 1) {
1080 LLVMValueRef values[] = {value, value, value, value};
1081 value = ac_build_gather_values(&ctx->ac, values, num_components);
1082 } else {
1083 LLVMValueRef swizzle = LLVMConstVector(masks, num_components);
1084 value = LLVMBuildShuffleVector(ctx->ac.builder, value, value,
1085 swizzle, "");
1086 }
1087 }
1088 assert(!src.negate);
1089 assert(!src.abs);
1090 return value;
1091 }
1092
1093 static LLVMValueRef emit_int_cmp(struct ac_llvm_context *ctx,
1094 LLVMIntPredicate pred, LLVMValueRef src0,
1095 LLVMValueRef src1)
1096 {
1097 LLVMValueRef result = LLVMBuildICmp(ctx->builder, pred, src0, src1, "");
1098 return LLVMBuildSelect(ctx->builder, result,
1099 LLVMConstInt(ctx->i32, 0xFFFFFFFF, false),
1100 LLVMConstInt(ctx->i32, 0, false), "");
1101 }
1102
1103 static LLVMValueRef emit_float_cmp(struct ac_llvm_context *ctx,
1104 LLVMRealPredicate pred, LLVMValueRef src0,
1105 LLVMValueRef src1)
1106 {
1107 LLVMValueRef result;
1108 src0 = to_float(ctx, src0);
1109 src1 = to_float(ctx, src1);
1110 result = LLVMBuildFCmp(ctx->builder, pred, src0, src1, "");
1111 return LLVMBuildSelect(ctx->builder, result,
1112 LLVMConstInt(ctx->i32, 0xFFFFFFFF, false),
1113 LLVMConstInt(ctx->i32, 0, false), "");
1114 }
1115
1116 static LLVMValueRef emit_intrin_1f_param(struct ac_llvm_context *ctx,
1117 const char *intrin,
1118 LLVMTypeRef result_type,
1119 LLVMValueRef src0)
1120 {
1121 char name[64];
1122 LLVMValueRef params[] = {
1123 to_float(ctx, src0),
1124 };
1125
1126 MAYBE_UNUSED const int length = snprintf(name, sizeof(name), "%s.f%d", intrin,
1127 get_elem_bits(ctx, result_type));
1128 assert(length < sizeof(name));
1129 return ac_build_intrinsic(ctx, name, result_type, params, 1, AC_FUNC_ATTR_READNONE);
1130 }
1131
1132 static LLVMValueRef emit_intrin_2f_param(struct ac_llvm_context *ctx,
1133 const char *intrin,
1134 LLVMTypeRef result_type,
1135 LLVMValueRef src0, LLVMValueRef src1)
1136 {
1137 char name[64];
1138 LLVMValueRef params[] = {
1139 to_float(ctx, src0),
1140 to_float(ctx, src1),
1141 };
1142
1143 MAYBE_UNUSED const int length = snprintf(name, sizeof(name), "%s.f%d", intrin,
1144 get_elem_bits(ctx, result_type));
1145 assert(length < sizeof(name));
1146 return ac_build_intrinsic(ctx, name, result_type, params, 2, AC_FUNC_ATTR_READNONE);
1147 }
1148
1149 static LLVMValueRef emit_intrin_3f_param(struct ac_llvm_context *ctx,
1150 const char *intrin,
1151 LLVMTypeRef result_type,
1152 LLVMValueRef src0, LLVMValueRef src1, LLVMValueRef src2)
1153 {
1154 char name[64];
1155 LLVMValueRef params[] = {
1156 to_float(ctx, src0),
1157 to_float(ctx, src1),
1158 to_float(ctx, src2),
1159 };
1160
1161 MAYBE_UNUSED const int length = snprintf(name, sizeof(name), "%s.f%d", intrin,
1162 get_elem_bits(ctx, result_type));
1163 assert(length < sizeof(name));
1164 return ac_build_intrinsic(ctx, name, result_type, params, 3, AC_FUNC_ATTR_READNONE);
1165 }
1166
1167 static LLVMValueRef emit_bcsel(struct ac_llvm_context *ctx,
1168 LLVMValueRef src0, LLVMValueRef src1, LLVMValueRef src2)
1169 {
1170 LLVMValueRef v = LLVMBuildICmp(ctx->builder, LLVMIntNE, src0,
1171 ctx->i32_0, "");
1172 return LLVMBuildSelect(ctx->builder, v, src1, src2, "");
1173 }
1174
1175 static LLVMValueRef emit_find_lsb(struct ac_llvm_context *ctx,
1176 LLVMValueRef src0)
1177 {
1178 LLVMValueRef params[2] = {
1179 src0,
1180
1181 /* The value of 1 means that ffs(x=0) = undef, so LLVM won't
1182 * add special code to check for x=0. The reason is that
1183 * the LLVM behavior for x=0 is different from what we
1184 * need here.
1185 *
1186 * The hardware already implements the correct behavior.
1187 */
1188 LLVMConstInt(ctx->i1, 1, false),
1189 };
1190 return ac_build_intrinsic(ctx, "llvm.cttz.i32", ctx->i32, params, 2, AC_FUNC_ATTR_READNONE);
1191 }
1192
1193 static LLVMValueRef emit_ifind_msb(struct ac_llvm_context *ctx,
1194 LLVMValueRef src0)
1195 {
1196 return ac_build_imsb(ctx, src0, ctx->i32);
1197 }
1198
1199 static LLVMValueRef emit_ufind_msb(struct ac_llvm_context *ctx,
1200 LLVMValueRef src0)
1201 {
1202 return ac_build_umsb(ctx, src0, ctx->i32);
1203 }
1204
1205 static LLVMValueRef emit_minmax_int(struct ac_llvm_context *ctx,
1206 LLVMIntPredicate pred,
1207 LLVMValueRef src0, LLVMValueRef src1)
1208 {
1209 return LLVMBuildSelect(ctx->builder,
1210 LLVMBuildICmp(ctx->builder, pred, src0, src1, ""),
1211 src0,
1212 src1, "");
1213
1214 }
1215 static LLVMValueRef emit_iabs(struct ac_llvm_context *ctx,
1216 LLVMValueRef src0)
1217 {
1218 return emit_minmax_int(ctx, LLVMIntSGT, src0,
1219 LLVMBuildNeg(ctx->builder, src0, ""));
1220 }
1221
1222 static LLVMValueRef emit_fsign(struct ac_llvm_context *ctx,
1223 LLVMValueRef src0)
1224 {
1225 LLVMValueRef cmp, val;
1226
1227 cmp = LLVMBuildFCmp(ctx->builder, LLVMRealOGT, src0, ctx->f32_0, "");
1228 val = LLVMBuildSelect(ctx->builder, cmp, ctx->f32_1, src0, "");
1229 cmp = LLVMBuildFCmp(ctx->builder, LLVMRealOGE, val, ctx->f32_0, "");
1230 val = LLVMBuildSelect(ctx->builder, cmp, val, LLVMConstReal(ctx->f32, -1.0), "");
1231 return val;
1232 }
1233
1234 static LLVMValueRef emit_isign(struct ac_llvm_context *ctx,
1235 LLVMValueRef src0)
1236 {
1237 LLVMValueRef cmp, val;
1238
1239 cmp = LLVMBuildICmp(ctx->builder, LLVMIntSGT, src0, ctx->i32_0, "");
1240 val = LLVMBuildSelect(ctx->builder, cmp, ctx->i32_1, src0, "");
1241 cmp = LLVMBuildICmp(ctx->builder, LLVMIntSGE, val, ctx->i32_0, "");
1242 val = LLVMBuildSelect(ctx->builder, cmp, val, LLVMConstInt(ctx->i32, -1, true), "");
1243 return val;
1244 }
1245
1246 static LLVMValueRef emit_ffract(struct ac_llvm_context *ctx,
1247 LLVMValueRef src0)
1248 {
1249 const char *intr = "llvm.floor.f32";
1250 LLVMValueRef fsrc0 = to_float(ctx, src0);
1251 LLVMValueRef params[] = {
1252 fsrc0,
1253 };
1254 LLVMValueRef floor = ac_build_intrinsic(ctx, intr,
1255 ctx->f32, params, 1,
1256 AC_FUNC_ATTR_READNONE);
1257 return LLVMBuildFSub(ctx->builder, fsrc0, floor, "");
1258 }
1259
1260 static LLVMValueRef emit_uint_carry(struct ac_llvm_context *ctx,
1261 const char *intrin,
1262 LLVMValueRef src0, LLVMValueRef src1)
1263 {
1264 LLVMTypeRef ret_type;
1265 LLVMTypeRef types[] = { ctx->i32, ctx->i1 };
1266 LLVMValueRef res;
1267 LLVMValueRef params[] = { src0, src1 };
1268 ret_type = LLVMStructTypeInContext(ctx->context, types,
1269 2, true);
1270
1271 res = ac_build_intrinsic(ctx, intrin, ret_type,
1272 params, 2, AC_FUNC_ATTR_READNONE);
1273
1274 res = LLVMBuildExtractValue(ctx->builder, res, 1, "");
1275 res = LLVMBuildZExt(ctx->builder, res, ctx->i32, "");
1276 return res;
1277 }
1278
1279 static LLVMValueRef emit_b2f(struct ac_llvm_context *ctx,
1280 LLVMValueRef src0)
1281 {
1282 return LLVMBuildAnd(ctx->builder, src0, LLVMBuildBitCast(ctx->builder, LLVMConstReal(ctx->f32, 1.0), ctx->i32, ""), "");
1283 }
1284
1285 static LLVMValueRef emit_f2b(struct ac_llvm_context *ctx,
1286 LLVMValueRef src0)
1287 {
1288 src0 = to_float(ctx, src0);
1289 return LLVMBuildSExt(ctx->builder,
1290 LLVMBuildFCmp(ctx->builder, LLVMRealUNE, src0, ctx->f32_0, ""),
1291 ctx->i32, "");
1292 }
1293
1294 static LLVMValueRef emit_b2i(struct ac_llvm_context *ctx,
1295 LLVMValueRef src0)
1296 {
1297 return LLVMBuildAnd(ctx->builder, src0, ctx->i32_1, "");
1298 }
1299
1300 static LLVMValueRef emit_i2b(struct ac_llvm_context *ctx,
1301 LLVMValueRef src0)
1302 {
1303 return LLVMBuildSExt(ctx->builder,
1304 LLVMBuildICmp(ctx->builder, LLVMIntNE, src0, ctx->i32_0, ""),
1305 ctx->i32, "");
1306 }
1307
1308 static LLVMValueRef emit_f2f16(struct nir_to_llvm_context *ctx,
1309 LLVMValueRef src0)
1310 {
1311 LLVMValueRef result;
1312 LLVMValueRef cond;
1313
1314 src0 = to_float(&ctx->ac, src0);
1315 result = LLVMBuildFPTrunc(ctx->builder, src0, ctx->f16, "");
1316
1317 /* TODO SI/CIK options here */
1318 if (ctx->options->chip_class >= VI) {
1319 LLVMValueRef args[2];
1320 /* Check if the result is a denormal - and flush to 0 if so. */
1321 args[0] = result;
1322 args[1] = LLVMConstInt(ctx->i32, N_SUBNORMAL | P_SUBNORMAL, false);
1323 cond = ac_build_intrinsic(&ctx->ac, "llvm.amdgcn.class.f16", ctx->i1, args, 2, AC_FUNC_ATTR_READNONE);
1324 }
1325
1326 /* need to convert back up to f32 */
1327 result = LLVMBuildFPExt(ctx->builder, result, ctx->f32, "");
1328
1329 if (ctx->options->chip_class >= VI)
1330 result = LLVMBuildSelect(ctx->builder, cond, ctx->f32zero, result, "");
1331
1332 return result;
1333 }
1334
1335 static LLVMValueRef emit_umul_high(struct ac_llvm_context *ctx,
1336 LLVMValueRef src0, LLVMValueRef src1)
1337 {
1338 LLVMValueRef dst64, result;
1339 src0 = LLVMBuildZExt(ctx->builder, src0, ctx->i64, "");
1340 src1 = LLVMBuildZExt(ctx->builder, src1, ctx->i64, "");
1341
1342 dst64 = LLVMBuildMul(ctx->builder, src0, src1, "");
1343 dst64 = LLVMBuildLShr(ctx->builder, dst64, LLVMConstInt(ctx->i64, 32, false), "");
1344 result = LLVMBuildTrunc(ctx->builder, dst64, ctx->i32, "");
1345 return result;
1346 }
1347
1348 static LLVMValueRef emit_imul_high(struct ac_llvm_context *ctx,
1349 LLVMValueRef src0, LLVMValueRef src1)
1350 {
1351 LLVMValueRef dst64, result;
1352 src0 = LLVMBuildSExt(ctx->builder, src0, ctx->i64, "");
1353 src1 = LLVMBuildSExt(ctx->builder, src1, ctx->i64, "");
1354
1355 dst64 = LLVMBuildMul(ctx->builder, src0, src1, "");
1356 dst64 = LLVMBuildAShr(ctx->builder, dst64, LLVMConstInt(ctx->i64, 32, false), "");
1357 result = LLVMBuildTrunc(ctx->builder, dst64, ctx->i32, "");
1358 return result;
1359 }
1360
1361 static LLVMValueRef emit_bitfield_extract(struct ac_llvm_context *ctx,
1362 bool is_signed,
1363 const LLVMValueRef srcs[3])
1364 {
1365 LLVMValueRef result;
1366 LLVMValueRef icond = LLVMBuildICmp(ctx->builder, LLVMIntEQ, srcs[2], LLVMConstInt(ctx->i32, 32, false), "");
1367
1368 result = ac_build_bfe(ctx, srcs[0], srcs[1], srcs[2], is_signed);
1369 result = LLVMBuildSelect(ctx->builder, icond, srcs[0], result, "");
1370 return result;
1371 }
1372
1373 static LLVMValueRef emit_bitfield_insert(struct ac_llvm_context *ctx,
1374 LLVMValueRef src0, LLVMValueRef src1,
1375 LLVMValueRef src2, LLVMValueRef src3)
1376 {
1377 LLVMValueRef bfi_args[3], result;
1378
1379 bfi_args[0] = LLVMBuildShl(ctx->builder,
1380 LLVMBuildSub(ctx->builder,
1381 LLVMBuildShl(ctx->builder,
1382 ctx->i32_1,
1383 src3, ""),
1384 ctx->i32_1, ""),
1385 src2, "");
1386 bfi_args[1] = LLVMBuildShl(ctx->builder, src1, src2, "");
1387 bfi_args[2] = src0;
1388
1389 LLVMValueRef icond = LLVMBuildICmp(ctx->builder, LLVMIntEQ, src3, LLVMConstInt(ctx->i32, 32, false), "");
1390
1391 /* Calculate:
1392 * (arg0 & arg1) | (~arg0 & arg2) = arg2 ^ (arg0 & (arg1 ^ arg2)
1393 * Use the right-hand side, which the LLVM backend can convert to V_BFI.
1394 */
1395 result = LLVMBuildXor(ctx->builder, bfi_args[2],
1396 LLVMBuildAnd(ctx->builder, bfi_args[0],
1397 LLVMBuildXor(ctx->builder, bfi_args[1], bfi_args[2], ""), ""), "");
1398
1399 result = LLVMBuildSelect(ctx->builder, icond, src1, result, "");
1400 return result;
1401 }
1402
1403 static LLVMValueRef emit_pack_half_2x16(struct ac_llvm_context *ctx,
1404 LLVMValueRef src0)
1405 {
1406 LLVMValueRef const16 = LLVMConstInt(ctx->i32, 16, false);
1407 int i;
1408 LLVMValueRef comp[2];
1409
1410 src0 = to_float(ctx, src0);
1411 comp[0] = LLVMBuildExtractElement(ctx->builder, src0, ctx->i32_0, "");
1412 comp[1] = LLVMBuildExtractElement(ctx->builder, src0, ctx->i32_1, "");
1413 for (i = 0; i < 2; i++) {
1414 comp[i] = LLVMBuildFPTrunc(ctx->builder, comp[i], ctx->f16, "");
1415 comp[i] = LLVMBuildBitCast(ctx->builder, comp[i], ctx->i16, "");
1416 comp[i] = LLVMBuildZExt(ctx->builder, comp[i], ctx->i32, "");
1417 }
1418
1419 comp[1] = LLVMBuildShl(ctx->builder, comp[1], const16, "");
1420 comp[0] = LLVMBuildOr(ctx->builder, comp[0], comp[1], "");
1421
1422 return comp[0];
1423 }
1424
1425 static LLVMValueRef emit_unpack_half_2x16(struct ac_llvm_context *ctx,
1426 LLVMValueRef src0)
1427 {
1428 LLVMValueRef const16 = LLVMConstInt(ctx->i32, 16, false);
1429 LLVMValueRef temps[2], result, val;
1430 int i;
1431
1432 for (i = 0; i < 2; i++) {
1433 val = i == 1 ? LLVMBuildLShr(ctx->builder, src0, const16, "") : src0;
1434 val = LLVMBuildTrunc(ctx->builder, val, ctx->i16, "");
1435 val = LLVMBuildBitCast(ctx->builder, val, ctx->f16, "");
1436 temps[i] = LLVMBuildFPExt(ctx->builder, val, ctx->f32, "");
1437 }
1438
1439 LLVMTypeRef v2f32 = LLVMVectorType(ctx->f32, 2);
1440 result = LLVMBuildInsertElement(ctx->builder, LLVMGetUndef(v2f32), temps[0],
1441 ctx->i32_0, "");
1442 result = LLVMBuildInsertElement(ctx->builder, result, temps[1],
1443 ctx->i32_1, "");
1444 return result;
1445 }
1446
1447 static LLVMValueRef emit_ddxy(struct ac_nir_context *ctx,
1448 nir_op op,
1449 LLVMValueRef src0)
1450 {
1451 unsigned mask;
1452 int idx;
1453 LLVMValueRef result;
1454 bool has_ds_bpermute = ctx->abi->chip_class >= VI;
1455
1456 if (!ctx->ddxy_lds && !has_ds_bpermute)
1457 ctx->ddxy_lds = LLVMAddGlobalInAddressSpace(ctx->ac.module,
1458 LLVMArrayType(ctx->ac.i32, 64),
1459 "ddxy_lds", LOCAL_ADDR_SPACE);
1460
1461 if (op == nir_op_fddx_fine || op == nir_op_fddx)
1462 mask = AC_TID_MASK_LEFT;
1463 else if (op == nir_op_fddy_fine || op == nir_op_fddy)
1464 mask = AC_TID_MASK_TOP;
1465 else
1466 mask = AC_TID_MASK_TOP_LEFT;
1467
1468 /* for DDX we want to next X pixel, DDY next Y pixel. */
1469 if (op == nir_op_fddx_fine ||
1470 op == nir_op_fddx_coarse ||
1471 op == nir_op_fddx)
1472 idx = 1;
1473 else
1474 idx = 2;
1475
1476 result = ac_build_ddxy(&ctx->ac, has_ds_bpermute,
1477 mask, idx, ctx->ddxy_lds,
1478 src0);
1479 return result;
1480 }
1481
1482 /*
1483 * this takes an I,J coordinate pair,
1484 * and works out the X and Y derivatives.
1485 * it returns DDX(I), DDX(J), DDY(I), DDY(J).
1486 */
1487 static LLVMValueRef emit_ddxy_interp(
1488 struct ac_nir_context *ctx,
1489 LLVMValueRef interp_ij)
1490 {
1491 LLVMValueRef result[4], a;
1492 unsigned i;
1493
1494 for (i = 0; i < 2; i++) {
1495 a = LLVMBuildExtractElement(ctx->ac.builder, interp_ij,
1496 LLVMConstInt(ctx->ac.i32, i, false), "");
1497 result[i] = emit_ddxy(ctx, nir_op_fddx, a);
1498 result[2+i] = emit_ddxy(ctx, nir_op_fddy, a);
1499 }
1500 return ac_build_gather_values(&ctx->ac, result, 4);
1501 }
1502
1503 static void visit_alu(struct ac_nir_context *ctx, const nir_alu_instr *instr)
1504 {
1505 LLVMValueRef src[4], result = NULL;
1506 unsigned num_components = instr->dest.dest.ssa.num_components;
1507 unsigned src_components;
1508 LLVMTypeRef def_type = get_def_type(ctx, &instr->dest.dest.ssa);
1509
1510 assert(nir_op_infos[instr->op].num_inputs <= ARRAY_SIZE(src));
1511 switch (instr->op) {
1512 case nir_op_vec2:
1513 case nir_op_vec3:
1514 case nir_op_vec4:
1515 src_components = 1;
1516 break;
1517 case nir_op_pack_half_2x16:
1518 src_components = 2;
1519 break;
1520 case nir_op_unpack_half_2x16:
1521 src_components = 1;
1522 break;
1523 default:
1524 src_components = num_components;
1525 break;
1526 }
1527 for (unsigned i = 0; i < nir_op_infos[instr->op].num_inputs; i++)
1528 src[i] = get_alu_src(ctx, instr->src[i], src_components);
1529
1530 switch (instr->op) {
1531 case nir_op_fmov:
1532 case nir_op_imov:
1533 result = src[0];
1534 break;
1535 case nir_op_fneg:
1536 src[0] = to_float(&ctx->ac, src[0]);
1537 result = LLVMBuildFNeg(ctx->ac.builder, src[0], "");
1538 break;
1539 case nir_op_ineg:
1540 result = LLVMBuildNeg(ctx->ac.builder, src[0], "");
1541 break;
1542 case nir_op_inot:
1543 result = LLVMBuildNot(ctx->ac.builder, src[0], "");
1544 break;
1545 case nir_op_iadd:
1546 result = LLVMBuildAdd(ctx->ac.builder, src[0], src[1], "");
1547 break;
1548 case nir_op_fadd:
1549 src[0] = to_float(&ctx->ac, src[0]);
1550 src[1] = to_float(&ctx->ac, src[1]);
1551 result = LLVMBuildFAdd(ctx->ac.builder, src[0], src[1], "");
1552 break;
1553 case nir_op_fsub:
1554 src[0] = to_float(&ctx->ac, src[0]);
1555 src[1] = to_float(&ctx->ac, src[1]);
1556 result = LLVMBuildFSub(ctx->ac.builder, src[0], src[1], "");
1557 break;
1558 case nir_op_isub:
1559 result = LLVMBuildSub(ctx->ac.builder, src[0], src[1], "");
1560 break;
1561 case nir_op_imul:
1562 result = LLVMBuildMul(ctx->ac.builder, src[0], src[1], "");
1563 break;
1564 case nir_op_imod:
1565 result = LLVMBuildSRem(ctx->ac.builder, src[0], src[1], "");
1566 break;
1567 case nir_op_umod:
1568 result = LLVMBuildURem(ctx->ac.builder, src[0], src[1], "");
1569 break;
1570 case nir_op_fmod:
1571 src[0] = to_float(&ctx->ac, src[0]);
1572 src[1] = to_float(&ctx->ac, src[1]);
1573 result = ac_build_fdiv(&ctx->ac, src[0], src[1]);
1574 result = emit_intrin_1f_param(&ctx->ac, "llvm.floor",
1575 to_float_type(&ctx->ac, def_type), result);
1576 result = LLVMBuildFMul(ctx->ac.builder, src[1] , result, "");
1577 result = LLVMBuildFSub(ctx->ac.builder, src[0], result, "");
1578 break;
1579 case nir_op_frem:
1580 src[0] = to_float(&ctx->ac, src[0]);
1581 src[1] = to_float(&ctx->ac, src[1]);
1582 result = LLVMBuildFRem(ctx->ac.builder, src[0], src[1], "");
1583 break;
1584 case nir_op_irem:
1585 result = LLVMBuildSRem(ctx->ac.builder, src[0], src[1], "");
1586 break;
1587 case nir_op_idiv:
1588 result = LLVMBuildSDiv(ctx->ac.builder, src[0], src[1], "");
1589 break;
1590 case nir_op_udiv:
1591 result = LLVMBuildUDiv(ctx->ac.builder, src[0], src[1], "");
1592 break;
1593 case nir_op_fmul:
1594 src[0] = to_float(&ctx->ac, src[0]);
1595 src[1] = to_float(&ctx->ac, src[1]);
1596 result = LLVMBuildFMul(ctx->ac.builder, src[0], src[1], "");
1597 break;
1598 case nir_op_fdiv:
1599 src[0] = to_float(&ctx->ac, src[0]);
1600 src[1] = to_float(&ctx->ac, src[1]);
1601 result = ac_build_fdiv(&ctx->ac, src[0], src[1]);
1602 break;
1603 case nir_op_frcp:
1604 src[0] = to_float(&ctx->ac, src[0]);
1605 result = ac_build_fdiv(&ctx->ac, ctx->ac.f32_1, src[0]);
1606 break;
1607 case nir_op_iand:
1608 result = LLVMBuildAnd(ctx->ac.builder, src[0], src[1], "");
1609 break;
1610 case nir_op_ior:
1611 result = LLVMBuildOr(ctx->ac.builder, src[0], src[1], "");
1612 break;
1613 case nir_op_ixor:
1614 result = LLVMBuildXor(ctx->ac.builder, src[0], src[1], "");
1615 break;
1616 case nir_op_ishl:
1617 result = LLVMBuildShl(ctx->ac.builder, src[0],
1618 LLVMBuildZExt(ctx->ac.builder, src[1],
1619 LLVMTypeOf(src[0]), ""),
1620 "");
1621 break;
1622 case nir_op_ishr:
1623 result = LLVMBuildAShr(ctx->ac.builder, src[0],
1624 LLVMBuildZExt(ctx->ac.builder, src[1],
1625 LLVMTypeOf(src[0]), ""),
1626 "");
1627 break;
1628 case nir_op_ushr:
1629 result = LLVMBuildLShr(ctx->ac.builder, src[0],
1630 LLVMBuildZExt(ctx->ac.builder, src[1],
1631 LLVMTypeOf(src[0]), ""),
1632 "");
1633 break;
1634 case nir_op_ilt:
1635 result = emit_int_cmp(&ctx->ac, LLVMIntSLT, src[0], src[1]);
1636 break;
1637 case nir_op_ine:
1638 result = emit_int_cmp(&ctx->ac, LLVMIntNE, src[0], src[1]);
1639 break;
1640 case nir_op_ieq:
1641 result = emit_int_cmp(&ctx->ac, LLVMIntEQ, src[0], src[1]);
1642 break;
1643 case nir_op_ige:
1644 result = emit_int_cmp(&ctx->ac, LLVMIntSGE, src[0], src[1]);
1645 break;
1646 case nir_op_ult:
1647 result = emit_int_cmp(&ctx->ac, LLVMIntULT, src[0], src[1]);
1648 break;
1649 case nir_op_uge:
1650 result = emit_int_cmp(&ctx->ac, LLVMIntUGE, src[0], src[1]);
1651 break;
1652 case nir_op_feq:
1653 result = emit_float_cmp(&ctx->ac, LLVMRealUEQ, src[0], src[1]);
1654 break;
1655 case nir_op_fne:
1656 result = emit_float_cmp(&ctx->ac, LLVMRealUNE, src[0], src[1]);
1657 break;
1658 case nir_op_flt:
1659 result = emit_float_cmp(&ctx->ac, LLVMRealULT, src[0], src[1]);
1660 break;
1661 case nir_op_fge:
1662 result = emit_float_cmp(&ctx->ac, LLVMRealUGE, src[0], src[1]);
1663 break;
1664 case nir_op_fabs:
1665 result = emit_intrin_1f_param(&ctx->ac, "llvm.fabs",
1666 to_float_type(&ctx->ac, def_type), src[0]);
1667 break;
1668 case nir_op_iabs:
1669 result = emit_iabs(&ctx->ac, src[0]);
1670 break;
1671 case nir_op_imax:
1672 result = emit_minmax_int(&ctx->ac, LLVMIntSGT, src[0], src[1]);
1673 break;
1674 case nir_op_imin:
1675 result = emit_minmax_int(&ctx->ac, LLVMIntSLT, src[0], src[1]);
1676 break;
1677 case nir_op_umax:
1678 result = emit_minmax_int(&ctx->ac, LLVMIntUGT, src[0], src[1]);
1679 break;
1680 case nir_op_umin:
1681 result = emit_minmax_int(&ctx->ac, LLVMIntULT, src[0], src[1]);
1682 break;
1683 case nir_op_isign:
1684 result = emit_isign(&ctx->ac, src[0]);
1685 break;
1686 case nir_op_fsign:
1687 src[0] = to_float(&ctx->ac, src[0]);
1688 result = emit_fsign(&ctx->ac, src[0]);
1689 break;
1690 case nir_op_ffloor:
1691 result = emit_intrin_1f_param(&ctx->ac, "llvm.floor",
1692 to_float_type(&ctx->ac, def_type), src[0]);
1693 break;
1694 case nir_op_ftrunc:
1695 result = emit_intrin_1f_param(&ctx->ac, "llvm.trunc",
1696 to_float_type(&ctx->ac, def_type), src[0]);
1697 break;
1698 case nir_op_fceil:
1699 result = emit_intrin_1f_param(&ctx->ac, "llvm.ceil",
1700 to_float_type(&ctx->ac, def_type), src[0]);
1701 break;
1702 case nir_op_fround_even:
1703 result = emit_intrin_1f_param(&ctx->ac, "llvm.rint",
1704 to_float_type(&ctx->ac, def_type),src[0]);
1705 break;
1706 case nir_op_ffract:
1707 result = emit_ffract(&ctx->ac, src[0]);
1708 break;
1709 case nir_op_fsin:
1710 result = emit_intrin_1f_param(&ctx->ac, "llvm.sin",
1711 to_float_type(&ctx->ac, def_type), src[0]);
1712 break;
1713 case nir_op_fcos:
1714 result = emit_intrin_1f_param(&ctx->ac, "llvm.cos",
1715 to_float_type(&ctx->ac, def_type), src[0]);
1716 break;
1717 case nir_op_fsqrt:
1718 result = emit_intrin_1f_param(&ctx->ac, "llvm.sqrt",
1719 to_float_type(&ctx->ac, def_type), src[0]);
1720 break;
1721 case nir_op_fexp2:
1722 result = emit_intrin_1f_param(&ctx->ac, "llvm.exp2",
1723 to_float_type(&ctx->ac, def_type), src[0]);
1724 break;
1725 case nir_op_flog2:
1726 result = emit_intrin_1f_param(&ctx->ac, "llvm.log2",
1727 to_float_type(&ctx->ac, def_type), src[0]);
1728 break;
1729 case nir_op_frsq:
1730 result = emit_intrin_1f_param(&ctx->ac, "llvm.sqrt",
1731 to_float_type(&ctx->ac, def_type), src[0]);
1732 result = ac_build_fdiv(&ctx->ac, ctx->ac.f32_1, result);
1733 break;
1734 case nir_op_fpow:
1735 result = emit_intrin_2f_param(&ctx->ac, "llvm.pow",
1736 to_float_type(&ctx->ac, def_type), src[0], src[1]);
1737 break;
1738 case nir_op_fmax:
1739 result = emit_intrin_2f_param(&ctx->ac, "llvm.maxnum",
1740 to_float_type(&ctx->ac, def_type), src[0], src[1]);
1741 if (instr->dest.dest.ssa.bit_size == 32)
1742 result = emit_intrin_1f_param(&ctx->ac, "llvm.canonicalize",
1743 to_float_type(&ctx->ac, def_type),
1744 result);
1745 break;
1746 case nir_op_fmin:
1747 result = emit_intrin_2f_param(&ctx->ac, "llvm.minnum",
1748 to_float_type(&ctx->ac, def_type), src[0], src[1]);
1749 if (instr->dest.dest.ssa.bit_size == 32)
1750 result = emit_intrin_1f_param(&ctx->ac, "llvm.canonicalize",
1751 to_float_type(&ctx->ac, def_type),
1752 result);
1753 break;
1754 case nir_op_ffma:
1755 result = emit_intrin_3f_param(&ctx->ac, "llvm.fma",
1756 to_float_type(&ctx->ac, def_type), src[0], src[1], src[2]);
1757 break;
1758 case nir_op_ibitfield_extract:
1759 result = emit_bitfield_extract(&ctx->ac, true, src);
1760 break;
1761 case nir_op_ubitfield_extract:
1762 result = emit_bitfield_extract(&ctx->ac, false, src);
1763 break;
1764 case nir_op_bitfield_insert:
1765 result = emit_bitfield_insert(&ctx->ac, src[0], src[1], src[2], src[3]);
1766 break;
1767 case nir_op_bitfield_reverse:
1768 result = ac_build_intrinsic(&ctx->ac, "llvm.bitreverse.i32", ctx->ac.i32, src, 1, AC_FUNC_ATTR_READNONE);
1769 break;
1770 case nir_op_bit_count:
1771 result = ac_build_intrinsic(&ctx->ac, "llvm.ctpop.i32", ctx->ac.i32, src, 1, AC_FUNC_ATTR_READNONE);
1772 break;
1773 case nir_op_vec2:
1774 case nir_op_vec3:
1775 case nir_op_vec4:
1776 for (unsigned i = 0; i < nir_op_infos[instr->op].num_inputs; i++)
1777 src[i] = to_integer(&ctx->ac, src[i]);
1778 result = ac_build_gather_values(&ctx->ac, src, num_components);
1779 break;
1780 case nir_op_f2i32:
1781 case nir_op_f2i64:
1782 src[0] = to_float(&ctx->ac, src[0]);
1783 result = LLVMBuildFPToSI(ctx->ac.builder, src[0], def_type, "");
1784 break;
1785 case nir_op_f2u32:
1786 case nir_op_f2u64:
1787 src[0] = to_float(&ctx->ac, src[0]);
1788 result = LLVMBuildFPToUI(ctx->ac.builder, src[0], def_type, "");
1789 break;
1790 case nir_op_i2f32:
1791 case nir_op_i2f64:
1792 result = LLVMBuildSIToFP(ctx->ac.builder, src[0], to_float_type(&ctx->ac, def_type), "");
1793 break;
1794 case nir_op_u2f32:
1795 case nir_op_u2f64:
1796 result = LLVMBuildUIToFP(ctx->ac.builder, src[0], to_float_type(&ctx->ac, def_type), "");
1797 break;
1798 case nir_op_f2f64:
1799 result = LLVMBuildFPExt(ctx->ac.builder, src[0], to_float_type(&ctx->ac, def_type), "");
1800 break;
1801 case nir_op_f2f32:
1802 result = LLVMBuildFPTrunc(ctx->ac.builder, src[0], to_float_type(&ctx->ac, def_type), "");
1803 break;
1804 case nir_op_u2u32:
1805 case nir_op_u2u64:
1806 if (get_elem_bits(&ctx->ac, LLVMTypeOf(src[0])) < get_elem_bits(&ctx->ac, def_type))
1807 result = LLVMBuildZExt(ctx->ac.builder, src[0], def_type, "");
1808 else
1809 result = LLVMBuildTrunc(ctx->ac.builder, src[0], def_type, "");
1810 break;
1811 case nir_op_i2i32:
1812 case nir_op_i2i64:
1813 if (get_elem_bits(&ctx->ac, LLVMTypeOf(src[0])) < get_elem_bits(&ctx->ac, def_type))
1814 result = LLVMBuildSExt(ctx->ac.builder, src[0], def_type, "");
1815 else
1816 result = LLVMBuildTrunc(ctx->ac.builder, src[0], def_type, "");
1817 break;
1818 case nir_op_bcsel:
1819 result = emit_bcsel(&ctx->ac, src[0], src[1], src[2]);
1820 break;
1821 case nir_op_find_lsb:
1822 result = emit_find_lsb(&ctx->ac, src[0]);
1823 break;
1824 case nir_op_ufind_msb:
1825 result = emit_ufind_msb(&ctx->ac, src[0]);
1826 break;
1827 case nir_op_ifind_msb:
1828 result = emit_ifind_msb(&ctx->ac, src[0]);
1829 break;
1830 case nir_op_uadd_carry:
1831 result = emit_uint_carry(&ctx->ac, "llvm.uadd.with.overflow.i32", src[0], src[1]);
1832 break;
1833 case nir_op_usub_borrow:
1834 result = emit_uint_carry(&ctx->ac, "llvm.usub.with.overflow.i32", src[0], src[1]);
1835 break;
1836 case nir_op_b2f:
1837 result = emit_b2f(&ctx->ac, src[0]);
1838 break;
1839 case nir_op_f2b:
1840 result = emit_f2b(&ctx->ac, src[0]);
1841 break;
1842 case nir_op_b2i:
1843 result = emit_b2i(&ctx->ac, src[0]);
1844 break;
1845 case nir_op_i2b:
1846 result = emit_i2b(&ctx->ac, src[0]);
1847 break;
1848 case nir_op_fquantize2f16:
1849 result = emit_f2f16(ctx->nctx, src[0]);
1850 break;
1851 case nir_op_umul_high:
1852 result = emit_umul_high(&ctx->ac, src[0], src[1]);
1853 break;
1854 case nir_op_imul_high:
1855 result = emit_imul_high(&ctx->ac, src[0], src[1]);
1856 break;
1857 case nir_op_pack_half_2x16:
1858 result = emit_pack_half_2x16(&ctx->ac, src[0]);
1859 break;
1860 case nir_op_unpack_half_2x16:
1861 result = emit_unpack_half_2x16(&ctx->ac, src[0]);
1862 break;
1863 case nir_op_fddx:
1864 case nir_op_fddy:
1865 case nir_op_fddx_fine:
1866 case nir_op_fddy_fine:
1867 case nir_op_fddx_coarse:
1868 case nir_op_fddy_coarse:
1869 result = emit_ddxy(ctx, instr->op, src[0]);
1870 break;
1871
1872 case nir_op_unpack_64_2x32_split_x: {
1873 assert(instr->src[0].src.ssa->num_components == 1);
1874 LLVMValueRef tmp = LLVMBuildBitCast(ctx->ac.builder, src[0],
1875 LLVMVectorType(ctx->ac.i32, 2),
1876 "");
1877 result = LLVMBuildExtractElement(ctx->ac.builder, tmp,
1878 ctx->ac.i32_0, "");
1879 break;
1880 }
1881
1882 case nir_op_unpack_64_2x32_split_y: {
1883 assert(instr->src[0].src.ssa->num_components == 1);
1884 LLVMValueRef tmp = LLVMBuildBitCast(ctx->ac.builder, src[0],
1885 LLVMVectorType(ctx->ac.i32, 2),
1886 "");
1887 result = LLVMBuildExtractElement(ctx->ac.builder, tmp,
1888 ctx->ac.i32_0, "");
1889 break;
1890 }
1891
1892 case nir_op_pack_64_2x32_split: {
1893 LLVMValueRef tmp = LLVMGetUndef(LLVMVectorType(ctx->ac.i32, 2));
1894 tmp = LLVMBuildInsertElement(ctx->ac.builder, tmp,
1895 src[0], ctx->ac.i32_0, "");
1896 tmp = LLVMBuildInsertElement(ctx->ac.builder, tmp,
1897 src[1], ctx->ac.i32_1, "");
1898 result = LLVMBuildBitCast(ctx->ac.builder, tmp, ctx->ac.i64, "");
1899 break;
1900 }
1901
1902 default:
1903 fprintf(stderr, "Unknown NIR alu instr: ");
1904 nir_print_instr(&instr->instr, stderr);
1905 fprintf(stderr, "\n");
1906 abort();
1907 }
1908
1909 if (result) {
1910 assert(instr->dest.dest.is_ssa);
1911 result = to_integer(&ctx->ac, result);
1912 _mesa_hash_table_insert(ctx->defs, &instr->dest.dest.ssa,
1913 result);
1914 }
1915 }
1916
1917 static void visit_load_const(struct ac_nir_context *ctx,
1918 const nir_load_const_instr *instr)
1919 {
1920 LLVMValueRef values[4], value = NULL;
1921 LLVMTypeRef element_type =
1922 LLVMIntTypeInContext(ctx->ac.context, instr->def.bit_size);
1923
1924 for (unsigned i = 0; i < instr->def.num_components; ++i) {
1925 switch (instr->def.bit_size) {
1926 case 32:
1927 values[i] = LLVMConstInt(element_type,
1928 instr->value.u32[i], false);
1929 break;
1930 case 64:
1931 values[i] = LLVMConstInt(element_type,
1932 instr->value.u64[i], false);
1933 break;
1934 default:
1935 fprintf(stderr,
1936 "unsupported nir load_const bit_size: %d\n",
1937 instr->def.bit_size);
1938 abort();
1939 }
1940 }
1941 if (instr->def.num_components > 1) {
1942 value = LLVMConstVector(values, instr->def.num_components);
1943 } else
1944 value = values[0];
1945
1946 _mesa_hash_table_insert(ctx->defs, &instr->def, value);
1947 }
1948
1949 static LLVMValueRef cast_ptr(struct nir_to_llvm_context *ctx, LLVMValueRef ptr,
1950 LLVMTypeRef type)
1951 {
1952 int addr_space = LLVMGetPointerAddressSpace(LLVMTypeOf(ptr));
1953 return LLVMBuildBitCast(ctx->builder, ptr,
1954 LLVMPointerType(type, addr_space), "");
1955 }
1956
1957 static LLVMValueRef
1958 get_buffer_size(struct ac_nir_context *ctx, LLVMValueRef descriptor, bool in_elements)
1959 {
1960 LLVMValueRef size =
1961 LLVMBuildExtractElement(ctx->ac.builder, descriptor,
1962 LLVMConstInt(ctx->ac.i32, 2, false), "");
1963
1964 /* VI only */
1965 if (ctx->abi->chip_class >= VI && in_elements) {
1966 /* On VI, the descriptor contains the size in bytes,
1967 * but TXQ must return the size in elements.
1968 * The stride is always non-zero for resources using TXQ.
1969 */
1970 LLVMValueRef stride =
1971 LLVMBuildExtractElement(ctx->ac.builder, descriptor,
1972 LLVMConstInt(ctx->ac.i32, 1, false), "");
1973 stride = LLVMBuildLShr(ctx->ac.builder, stride,
1974 LLVMConstInt(ctx->ac.i32, 16, false), "");
1975 stride = LLVMBuildAnd(ctx->ac.builder, stride,
1976 LLVMConstInt(ctx->ac.i32, 0x3fff, false), "");
1977
1978 size = LLVMBuildUDiv(ctx->ac.builder, size, stride, "");
1979 }
1980 return size;
1981 }
1982
1983 /**
1984 * Given the i32 or vNi32 \p type, generate the textual name (e.g. for use with
1985 * intrinsic names).
1986 */
1987 static void build_int_type_name(
1988 LLVMTypeRef type,
1989 char *buf, unsigned bufsize)
1990 {
1991 assert(bufsize >= 6);
1992
1993 if (LLVMGetTypeKind(type) == LLVMVectorTypeKind)
1994 snprintf(buf, bufsize, "v%ui32",
1995 LLVMGetVectorSize(type));
1996 else
1997 strcpy(buf, "i32");
1998 }
1999
2000 static LLVMValueRef radv_lower_gather4_integer(struct ac_llvm_context *ctx,
2001 struct ac_image_args *args,
2002 const nir_tex_instr *instr)
2003 {
2004 enum glsl_base_type stype = glsl_get_sampler_result_type(instr->texture->var->type);
2005 LLVMValueRef coord = args->addr;
2006 LLVMValueRef half_texel[2];
2007 LLVMValueRef compare_cube_wa;
2008 LLVMValueRef result;
2009 int c;
2010 unsigned coord_vgpr_index = (unsigned)args->offset + (unsigned)args->compare;
2011
2012 //TODO Rect
2013 {
2014 struct ac_image_args txq_args = { 0 };
2015
2016 txq_args.da = instr->is_array || instr->sampler_dim == GLSL_SAMPLER_DIM_CUBE;
2017 txq_args.opcode = ac_image_get_resinfo;
2018 txq_args.dmask = 0xf;
2019 txq_args.addr = ctx->i32_0;
2020 txq_args.resource = args->resource;
2021 LLVMValueRef size = ac_build_image_opcode(ctx, &txq_args);
2022
2023 for (c = 0; c < 2; c++) {
2024 half_texel[c] = LLVMBuildExtractElement(ctx->builder, size,
2025 LLVMConstInt(ctx->i32, c, false), "");
2026 half_texel[c] = LLVMBuildUIToFP(ctx->builder, half_texel[c], ctx->f32, "");
2027 half_texel[c] = ac_build_fdiv(ctx, ctx->f32_1, half_texel[c]);
2028 half_texel[c] = LLVMBuildFMul(ctx->builder, half_texel[c],
2029 LLVMConstReal(ctx->f32, -0.5), "");
2030 }
2031 }
2032
2033 LLVMValueRef orig_coords = args->addr;
2034
2035 for (c = 0; c < 2; c++) {
2036 LLVMValueRef tmp;
2037 LLVMValueRef index = LLVMConstInt(ctx->i32, coord_vgpr_index + c, 0);
2038 tmp = LLVMBuildExtractElement(ctx->builder, coord, index, "");
2039 tmp = LLVMBuildBitCast(ctx->builder, tmp, ctx->f32, "");
2040 tmp = LLVMBuildFAdd(ctx->builder, tmp, half_texel[c], "");
2041 tmp = LLVMBuildBitCast(ctx->builder, tmp, ctx->i32, "");
2042 coord = LLVMBuildInsertElement(ctx->builder, coord, tmp, index, "");
2043 }
2044
2045
2046 /*
2047 * Apparantly cube has issue with integer types that the workaround doesn't solve,
2048 * so this tests if the format is 8_8_8_8 and an integer type do an alternate
2049 * workaround by sampling using a scaled type and converting.
2050 * This is taken from amdgpu-pro shaders.
2051 */
2052 /* NOTE this produces some ugly code compared to amdgpu-pro,
2053 * LLVM ends up dumping SGPRs into VGPRs to deal with the compare/select,
2054 * and then reads them back. -pro generates two selects,
2055 * one s_cmp for the descriptor rewriting
2056 * one v_cmp for the coordinate and result changes.
2057 */
2058 if (instr->sampler_dim == GLSL_SAMPLER_DIM_CUBE) {
2059 LLVMValueRef tmp, tmp2;
2060
2061 /* workaround 8/8/8/8 uint/sint cube gather bug */
2062 /* first detect it then change to a scaled read and f2i */
2063 tmp = LLVMBuildExtractElement(ctx->builder, args->resource, ctx->i32_1, "");
2064 tmp2 = tmp;
2065
2066 /* extract the DATA_FORMAT */
2067 tmp = ac_build_bfe(ctx, tmp, LLVMConstInt(ctx->i32, 20, false),
2068 LLVMConstInt(ctx->i32, 6, false), false);
2069
2070 /* is the DATA_FORMAT == 8_8_8_8 */
2071 compare_cube_wa = LLVMBuildICmp(ctx->builder, LLVMIntEQ, tmp, LLVMConstInt(ctx->i32, V_008F14_IMG_DATA_FORMAT_8_8_8_8, false), "");
2072
2073 if (stype == GLSL_TYPE_UINT)
2074 /* Create a NUM FORMAT - 0x2 or 0x4 - USCALED or UINT */
2075 tmp = LLVMBuildSelect(ctx->builder, compare_cube_wa, LLVMConstInt(ctx->i32, 0x8000000, false),
2076 LLVMConstInt(ctx->i32, 0x10000000, false), "");
2077 else
2078 /* Create a NUM FORMAT - 0x3 or 0x5 - SSCALED or SINT */
2079 tmp = LLVMBuildSelect(ctx->builder, compare_cube_wa, LLVMConstInt(ctx->i32, 0xc000000, false),
2080 LLVMConstInt(ctx->i32, 0x14000000, false), "");
2081
2082 /* replace the NUM FORMAT in the descriptor */
2083 tmp2 = LLVMBuildAnd(ctx->builder, tmp2, LLVMConstInt(ctx->i32, C_008F14_NUM_FORMAT_GFX6, false), "");
2084 tmp2 = LLVMBuildOr(ctx->builder, tmp2, tmp, "");
2085
2086 args->resource = LLVMBuildInsertElement(ctx->builder, args->resource, tmp2, ctx->i32_1, "");
2087
2088 /* don't modify the coordinates for this case */
2089 coord = LLVMBuildSelect(ctx->builder, compare_cube_wa, orig_coords, coord, "");
2090 }
2091 args->addr = coord;
2092 result = ac_build_image_opcode(ctx, args);
2093
2094 if (instr->sampler_dim == GLSL_SAMPLER_DIM_CUBE) {
2095 LLVMValueRef tmp, tmp2;
2096
2097 /* if the cube workaround is in place, f2i the result. */
2098 for (c = 0; c < 4; c++) {
2099 tmp = LLVMBuildExtractElement(ctx->builder, result, LLVMConstInt(ctx->i32, c, false), "");
2100 if (stype == GLSL_TYPE_UINT)
2101 tmp2 = LLVMBuildFPToUI(ctx->builder, tmp, ctx->i32, "");
2102 else
2103 tmp2 = LLVMBuildFPToSI(ctx->builder, tmp, ctx->i32, "");
2104 tmp = LLVMBuildBitCast(ctx->builder, tmp, ctx->i32, "");
2105 tmp2 = LLVMBuildBitCast(ctx->builder, tmp2, ctx->i32, "");
2106 tmp = LLVMBuildSelect(ctx->builder, compare_cube_wa, tmp2, tmp, "");
2107 tmp = LLVMBuildBitCast(ctx->builder, tmp, ctx->f32, "");
2108 result = LLVMBuildInsertElement(ctx->builder, result, tmp, LLVMConstInt(ctx->i32, c, false), "");
2109 }
2110 }
2111 return result;
2112 }
2113
2114 static LLVMValueRef build_tex_intrinsic(struct ac_nir_context *ctx,
2115 const nir_tex_instr *instr,
2116 bool lod_is_zero,
2117 struct ac_image_args *args)
2118 {
2119 if (instr->sampler_dim == GLSL_SAMPLER_DIM_BUF) {
2120 return ac_build_buffer_load_format(&ctx->ac,
2121 args->resource,
2122 args->addr,
2123 LLVMConstInt(ctx->ac.i32, 0, false),
2124 true);
2125 }
2126
2127 args->opcode = ac_image_sample;
2128 args->compare = instr->is_shadow;
2129
2130 switch (instr->op) {
2131 case nir_texop_txf:
2132 case nir_texop_txf_ms:
2133 case nir_texop_samples_identical:
2134 args->opcode = instr->sampler_dim == GLSL_SAMPLER_DIM_MS ? ac_image_load : ac_image_load_mip;
2135 args->compare = false;
2136 args->offset = false;
2137 break;
2138 case nir_texop_txb:
2139 args->bias = true;
2140 break;
2141 case nir_texop_txl:
2142 if (lod_is_zero)
2143 args->level_zero = true;
2144 else
2145 args->lod = true;
2146 break;
2147 case nir_texop_txs:
2148 case nir_texop_query_levels:
2149 args->opcode = ac_image_get_resinfo;
2150 break;
2151 case nir_texop_tex:
2152 if (ctx->stage != MESA_SHADER_FRAGMENT)
2153 args->level_zero = true;
2154 break;
2155 case nir_texop_txd:
2156 args->deriv = true;
2157 break;
2158 case nir_texop_tg4:
2159 args->opcode = ac_image_gather4;
2160 args->level_zero = true;
2161 break;
2162 case nir_texop_lod:
2163 args->opcode = ac_image_get_lod;
2164 args->compare = false;
2165 args->offset = false;
2166 break;
2167 default:
2168 break;
2169 }
2170
2171 if (instr->op == nir_texop_tg4) {
2172 enum glsl_base_type stype = glsl_get_sampler_result_type(instr->texture->var->type);
2173 if (stype == GLSL_TYPE_UINT || stype == GLSL_TYPE_INT) {
2174 return radv_lower_gather4_integer(&ctx->ac, args, instr);
2175 }
2176 }
2177 return ac_build_image_opcode(&ctx->ac, args);
2178 }
2179
2180 static LLVMValueRef visit_vulkan_resource_index(struct nir_to_llvm_context *ctx,
2181 nir_intrinsic_instr *instr)
2182 {
2183 LLVMValueRef index = get_src(ctx->nir, instr->src[0]);
2184 unsigned desc_set = nir_intrinsic_desc_set(instr);
2185 unsigned binding = nir_intrinsic_binding(instr);
2186 LLVMValueRef desc_ptr = ctx->descriptor_sets[desc_set];
2187 struct radv_pipeline_layout *pipeline_layout = ctx->options->layout;
2188 struct radv_descriptor_set_layout *layout = pipeline_layout->set[desc_set].layout;
2189 unsigned base_offset = layout->binding[binding].offset;
2190 LLVMValueRef offset, stride;
2191
2192 if (layout->binding[binding].type == VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC ||
2193 layout->binding[binding].type == VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC) {
2194 unsigned idx = pipeline_layout->set[desc_set].dynamic_offset_start +
2195 layout->binding[binding].dynamic_offset_offset;
2196 desc_ptr = ctx->push_constants;
2197 base_offset = pipeline_layout->push_constant_size + 16 * idx;
2198 stride = LLVMConstInt(ctx->i32, 16, false);
2199 } else
2200 stride = LLVMConstInt(ctx->i32, layout->binding[binding].size, false);
2201
2202 offset = LLVMConstInt(ctx->i32, base_offset, false);
2203 index = LLVMBuildMul(ctx->builder, index, stride, "");
2204 offset = LLVMBuildAdd(ctx->builder, offset, index, "");
2205
2206 desc_ptr = ac_build_gep0(&ctx->ac, desc_ptr, offset);
2207 desc_ptr = cast_ptr(ctx, desc_ptr, ctx->v4i32);
2208 LLVMSetMetadata(desc_ptr, ctx->uniform_md_kind, ctx->empty_md);
2209
2210 return LLVMBuildLoad(ctx->builder, desc_ptr, "");
2211 }
2212
2213 static LLVMValueRef visit_load_push_constant(struct nir_to_llvm_context *ctx,
2214 nir_intrinsic_instr *instr)
2215 {
2216 LLVMValueRef ptr, addr;
2217
2218 addr = LLVMConstInt(ctx->i32, nir_intrinsic_base(instr), 0);
2219 addr = LLVMBuildAdd(ctx->builder, addr, get_src(ctx->nir, instr->src[0]), "");
2220
2221 ptr = ac_build_gep0(&ctx->ac, ctx->push_constants, addr);
2222 ptr = cast_ptr(ctx, ptr, get_def_type(ctx->nir, &instr->dest.ssa));
2223
2224 return LLVMBuildLoad(ctx->builder, ptr, "");
2225 }
2226
2227 static LLVMValueRef visit_get_buffer_size(struct ac_nir_context *ctx,
2228 const nir_intrinsic_instr *instr)
2229 {
2230 LLVMValueRef desc = get_src(ctx, instr->src[0]);
2231
2232 return get_buffer_size(ctx, desc, false);
2233 }
2234 static void visit_store_ssbo(struct ac_nir_context *ctx,
2235 nir_intrinsic_instr *instr)
2236 {
2237 const char *store_name;
2238 LLVMValueRef src_data = get_src(ctx, instr->src[0]);
2239 LLVMTypeRef data_type = ctx->ac.f32;
2240 int elem_size_mult = get_elem_bits(&ctx->ac, LLVMTypeOf(src_data)) / 32;
2241 int components_32bit = elem_size_mult * instr->num_components;
2242 unsigned writemask = nir_intrinsic_write_mask(instr);
2243 LLVMValueRef base_data, base_offset;
2244 LLVMValueRef params[6];
2245 LLVMValueRef i1false = LLVMConstInt(ctx->ac.i1, 0, false);
2246
2247 params[1] = ctx->abi->load_ssbo(ctx->abi,
2248 get_src(ctx, instr->src[1]), true);
2249 params[2] = LLVMConstInt(ctx->ac.i32, 0, false); /* vindex */
2250 params[4] = i1false; /* glc */
2251 params[5] = i1false; /* slc */
2252
2253 if (components_32bit > 1)
2254 data_type = LLVMVectorType(ctx->ac.f32, components_32bit);
2255
2256 base_data = to_float(&ctx->ac, src_data);
2257 base_data = trim_vector(&ctx->ac, base_data, instr->num_components);
2258 base_data = LLVMBuildBitCast(ctx->ac.builder, base_data,
2259 data_type, "");
2260 base_offset = get_src(ctx, instr->src[2]); /* voffset */
2261 while (writemask) {
2262 int start, count;
2263 LLVMValueRef data;
2264 LLVMValueRef offset;
2265 LLVMValueRef tmp;
2266 u_bit_scan_consecutive_range(&writemask, &start, &count);
2267
2268 /* Due to an LLVM limitation, split 3-element writes
2269 * into a 2-element and a 1-element write. */
2270 if (count == 3) {
2271 writemask |= 1 << (start + 2);
2272 count = 2;
2273 }
2274
2275 start *= elem_size_mult;
2276 count *= elem_size_mult;
2277
2278 if (count > 4) {
2279 writemask |= ((1u << (count - 4)) - 1u) << (start + 4);
2280 count = 4;
2281 }
2282
2283 if (count == 4) {
2284 store_name = "llvm.amdgcn.buffer.store.v4f32";
2285 data = base_data;
2286 } else if (count == 2) {
2287 LLVMTypeRef v2f32 = LLVMVectorType(ctx->ac.f32, 2);
2288
2289 tmp = LLVMBuildExtractElement(ctx->ac.builder,
2290 base_data, LLVMConstInt(ctx->ac.i32, start, false), "");
2291 data = LLVMBuildInsertElement(ctx->ac.builder, LLVMGetUndef(v2f32), tmp,
2292 ctx->ac.i32_0, "");
2293
2294 tmp = LLVMBuildExtractElement(ctx->ac.builder,
2295 base_data, LLVMConstInt(ctx->ac.i32, start + 1, false), "");
2296 data = LLVMBuildInsertElement(ctx->ac.builder, data, tmp,
2297 ctx->ac.i32_1, "");
2298 store_name = "llvm.amdgcn.buffer.store.v2f32";
2299
2300 } else {
2301 assert(count == 1);
2302 if (get_llvm_num_components(base_data) > 1)
2303 data = LLVMBuildExtractElement(ctx->ac.builder, base_data,
2304 LLVMConstInt(ctx->ac.i32, start, false), "");
2305 else
2306 data = base_data;
2307 store_name = "llvm.amdgcn.buffer.store.f32";
2308 }
2309
2310 offset = base_offset;
2311 if (start != 0) {
2312 offset = LLVMBuildAdd(ctx->ac.builder, offset, LLVMConstInt(ctx->ac.i32, start * 4, false), "");
2313 }
2314 params[0] = data;
2315 params[3] = offset;
2316 ac_build_intrinsic(&ctx->ac, store_name,
2317 ctx->ac.voidt, params, 6, 0);
2318 }
2319 }
2320
2321 static LLVMValueRef visit_atomic_ssbo(struct ac_nir_context *ctx,
2322 const nir_intrinsic_instr *instr)
2323 {
2324 const char *name;
2325 LLVMValueRef params[6];
2326 int arg_count = 0;
2327
2328 if (instr->intrinsic == nir_intrinsic_ssbo_atomic_comp_swap) {
2329 params[arg_count++] = llvm_extract_elem(&ctx->ac, get_src(ctx, instr->src[3]), 0);
2330 }
2331 params[arg_count++] = llvm_extract_elem(&ctx->ac, get_src(ctx, instr->src[2]), 0);
2332 params[arg_count++] = ctx->abi->load_ssbo(ctx->abi,
2333 get_src(ctx, instr->src[0]),
2334 true);
2335 params[arg_count++] = LLVMConstInt(ctx->ac.i32, 0, false); /* vindex */
2336 params[arg_count++] = get_src(ctx, instr->src[1]); /* voffset */
2337 params[arg_count++] = LLVMConstInt(ctx->ac.i1, 0, false); /* slc */
2338
2339 switch (instr->intrinsic) {
2340 case nir_intrinsic_ssbo_atomic_add:
2341 name = "llvm.amdgcn.buffer.atomic.add";
2342 break;
2343 case nir_intrinsic_ssbo_atomic_imin:
2344 name = "llvm.amdgcn.buffer.atomic.smin";
2345 break;
2346 case nir_intrinsic_ssbo_atomic_umin:
2347 name = "llvm.amdgcn.buffer.atomic.umin";
2348 break;
2349 case nir_intrinsic_ssbo_atomic_imax:
2350 name = "llvm.amdgcn.buffer.atomic.smax";
2351 break;
2352 case nir_intrinsic_ssbo_atomic_umax:
2353 name = "llvm.amdgcn.buffer.atomic.umax";
2354 break;
2355 case nir_intrinsic_ssbo_atomic_and:
2356 name = "llvm.amdgcn.buffer.atomic.and";
2357 break;
2358 case nir_intrinsic_ssbo_atomic_or:
2359 name = "llvm.amdgcn.buffer.atomic.or";
2360 break;
2361 case nir_intrinsic_ssbo_atomic_xor:
2362 name = "llvm.amdgcn.buffer.atomic.xor";
2363 break;
2364 case nir_intrinsic_ssbo_atomic_exchange:
2365 name = "llvm.amdgcn.buffer.atomic.swap";
2366 break;
2367 case nir_intrinsic_ssbo_atomic_comp_swap:
2368 name = "llvm.amdgcn.buffer.atomic.cmpswap";
2369 break;
2370 default:
2371 abort();
2372 }
2373
2374 return ac_build_intrinsic(&ctx->ac, name, ctx->ac.i32, params, arg_count, 0);
2375 }
2376
2377 static LLVMValueRef visit_load_buffer(struct ac_nir_context *ctx,
2378 const nir_intrinsic_instr *instr)
2379 {
2380 LLVMValueRef results[2];
2381 int load_components;
2382 int num_components = instr->num_components;
2383 if (instr->dest.ssa.bit_size == 64)
2384 num_components *= 2;
2385
2386 for (int i = 0; i < num_components; i += load_components) {
2387 load_components = MIN2(num_components - i, 4);
2388 const char *load_name;
2389 LLVMTypeRef data_type = ctx->ac.f32;
2390 LLVMValueRef offset = LLVMConstInt(ctx->ac.i32, i * 4, false);
2391 offset = LLVMBuildAdd(ctx->ac.builder, get_src(ctx, instr->src[1]), offset, "");
2392
2393 if (load_components == 3)
2394 data_type = LLVMVectorType(ctx->ac.f32, 4);
2395 else if (load_components > 1)
2396 data_type = LLVMVectorType(ctx->ac.f32, load_components);
2397
2398 if (load_components >= 3)
2399 load_name = "llvm.amdgcn.buffer.load.v4f32";
2400 else if (load_components == 2)
2401 load_name = "llvm.amdgcn.buffer.load.v2f32";
2402 else if (load_components == 1)
2403 load_name = "llvm.amdgcn.buffer.load.f32";
2404 else
2405 unreachable("unhandled number of components");
2406
2407 LLVMValueRef i1false = LLVMConstInt(ctx->ac.i1, 0, false);
2408 LLVMValueRef params[] = {
2409 ctx->abi->load_ssbo(ctx->abi,
2410 get_src(ctx, instr->src[0]),
2411 false),
2412 LLVMConstInt(ctx->ac.i32, 0, false),
2413 offset,
2414 i1false,
2415 i1false,
2416 };
2417
2418 results[i] = ac_build_intrinsic(&ctx->ac, load_name, data_type, params, 5, 0);
2419
2420 }
2421
2422 LLVMValueRef ret = results[0];
2423 if (num_components > 4 || num_components == 3) {
2424 LLVMValueRef masks[] = {
2425 LLVMConstInt(ctx->ac.i32, 0, false), LLVMConstInt(ctx->ac.i32, 1, false),
2426 LLVMConstInt(ctx->ac.i32, 2, false), LLVMConstInt(ctx->ac.i32, 3, false),
2427 LLVMConstInt(ctx->ac.i32, 4, false), LLVMConstInt(ctx->ac.i32, 5, false),
2428 LLVMConstInt(ctx->ac.i32, 6, false), LLVMConstInt(ctx->ac.i32, 7, false)
2429 };
2430
2431 LLVMValueRef swizzle = LLVMConstVector(masks, num_components);
2432 ret = LLVMBuildShuffleVector(ctx->ac.builder, results[0],
2433 results[num_components > 4 ? 1 : 0], swizzle, "");
2434 }
2435
2436 return LLVMBuildBitCast(ctx->ac.builder, ret,
2437 get_def_type(ctx, &instr->dest.ssa), "");
2438 }
2439
2440 static LLVMValueRef visit_load_ubo_buffer(struct ac_nir_context *ctx,
2441 const nir_intrinsic_instr *instr)
2442 {
2443 LLVMValueRef results[8], ret;
2444 LLVMValueRef rsrc = get_src(ctx, instr->src[0]);
2445 LLVMValueRef offset = get_src(ctx, instr->src[1]);
2446 int num_components = instr->num_components;
2447
2448 if (ctx->abi->load_ubo)
2449 rsrc = ctx->abi->load_ubo(ctx->abi, rsrc);
2450
2451 if (instr->dest.ssa.bit_size == 64)
2452 num_components *= 2;
2453
2454 for (unsigned i = 0; i < num_components; ++i) {
2455 LLVMValueRef params[] = {
2456 rsrc,
2457 LLVMBuildAdd(ctx->ac.builder, LLVMConstInt(ctx->ac.i32, 4 * i, 0),
2458 offset, "")
2459 };
2460 results[i] = ac_build_intrinsic(&ctx->ac, "llvm.SI.load.const.v4i32", ctx->ac.f32,
2461 params, 2,
2462 AC_FUNC_ATTR_READNONE |
2463 AC_FUNC_ATTR_LEGACY);
2464 }
2465
2466
2467 ret = ac_build_gather_values(&ctx->ac, results, instr->num_components);
2468 return LLVMBuildBitCast(ctx->ac.builder, ret,
2469 get_def_type(ctx, &instr->dest.ssa), "");
2470 }
2471
2472 static void
2473 get_deref_offset(struct ac_nir_context *ctx, nir_deref_var *deref,
2474 bool vs_in, unsigned *vertex_index_out,
2475 LLVMValueRef *vertex_index_ref,
2476 unsigned *const_out, LLVMValueRef *indir_out)
2477 {
2478 unsigned const_offset = 0;
2479 nir_deref *tail = &deref->deref;
2480 LLVMValueRef offset = NULL;
2481
2482 if (vertex_index_out != NULL || vertex_index_ref != NULL) {
2483 tail = tail->child;
2484 nir_deref_array *deref_array = nir_deref_as_array(tail);
2485 if (vertex_index_out)
2486 *vertex_index_out = deref_array->base_offset;
2487
2488 if (vertex_index_ref) {
2489 LLVMValueRef vtx = LLVMConstInt(ctx->ac.i32, deref_array->base_offset, false);
2490 if (deref_array->deref_array_type == nir_deref_array_type_indirect) {
2491 vtx = LLVMBuildAdd(ctx->ac.builder, vtx, get_src(ctx, deref_array->indirect), "");
2492 }
2493 *vertex_index_ref = vtx;
2494 }
2495 }
2496
2497 if (deref->var->data.compact) {
2498 assert(tail->child->deref_type == nir_deref_type_array);
2499 assert(glsl_type_is_scalar(glsl_without_array(deref->var->type)));
2500 nir_deref_array *deref_array = nir_deref_as_array(tail->child);
2501 /* We always lower indirect dereferences for "compact" array vars. */
2502 assert(deref_array->deref_array_type == nir_deref_array_type_direct);
2503
2504 const_offset = deref_array->base_offset;
2505 goto out;
2506 }
2507
2508 while (tail->child != NULL) {
2509 const struct glsl_type *parent_type = tail->type;
2510 tail = tail->child;
2511
2512 if (tail->deref_type == nir_deref_type_array) {
2513 nir_deref_array *deref_array = nir_deref_as_array(tail);
2514 LLVMValueRef index, stride, local_offset;
2515 unsigned size = glsl_count_attribute_slots(tail->type, vs_in);
2516
2517 const_offset += size * deref_array->base_offset;
2518 if (deref_array->deref_array_type == nir_deref_array_type_direct)
2519 continue;
2520
2521 assert(deref_array->deref_array_type == nir_deref_array_type_indirect);
2522 index = get_src(ctx, deref_array->indirect);
2523 stride = LLVMConstInt(ctx->ac.i32, size, 0);
2524 local_offset = LLVMBuildMul(ctx->ac.builder, stride, index, "");
2525
2526 if (offset)
2527 offset = LLVMBuildAdd(ctx->ac.builder, offset, local_offset, "");
2528 else
2529 offset = local_offset;
2530 } else if (tail->deref_type == nir_deref_type_struct) {
2531 nir_deref_struct *deref_struct = nir_deref_as_struct(tail);
2532
2533 for (unsigned i = 0; i < deref_struct->index; i++) {
2534 const struct glsl_type *ft = glsl_get_struct_field(parent_type, i);
2535 const_offset += glsl_count_attribute_slots(ft, vs_in);
2536 }
2537 } else
2538 unreachable("unsupported deref type");
2539
2540 }
2541 out:
2542 if (const_offset && offset)
2543 offset = LLVMBuildAdd(ctx->ac.builder, offset,
2544 LLVMConstInt(ctx->ac.i32, const_offset, 0),
2545 "");
2546
2547 *const_out = const_offset;
2548 *indir_out = offset;
2549 }
2550
2551 static LLVMValueRef
2552 lds_load(struct nir_to_llvm_context *ctx,
2553 LLVMValueRef dw_addr)
2554 {
2555 LLVMValueRef value;
2556 value = ac_build_indexed_load(&ctx->ac, ctx->lds, dw_addr, false);
2557 return value;
2558 }
2559
2560 static void
2561 lds_store(struct nir_to_llvm_context *ctx,
2562 LLVMValueRef dw_addr, LLVMValueRef value)
2563 {
2564 value = LLVMBuildBitCast(ctx->builder, value, ctx->i32, "");
2565 ac_build_indexed_store(&ctx->ac, ctx->lds,
2566 dw_addr, value);
2567 }
2568
2569 /* The offchip buffer layout for TCS->TES is
2570 *
2571 * - attribute 0 of patch 0 vertex 0
2572 * - attribute 0 of patch 0 vertex 1
2573 * - attribute 0 of patch 0 vertex 2
2574 * ...
2575 * - attribute 0 of patch 1 vertex 0
2576 * - attribute 0 of patch 1 vertex 1
2577 * ...
2578 * - attribute 1 of patch 0 vertex 0
2579 * - attribute 1 of patch 0 vertex 1
2580 * ...
2581 * - per patch attribute 0 of patch 0
2582 * - per patch attribute 0 of patch 1
2583 * ...
2584 *
2585 * Note that every attribute has 4 components.
2586 */
2587 static LLVMValueRef get_tcs_tes_buffer_address(struct nir_to_llvm_context *ctx,
2588 LLVMValueRef vertex_index,
2589 LLVMValueRef param_index)
2590 {
2591 LLVMValueRef base_addr, vertices_per_patch, num_patches, total_vertices;
2592 LLVMValueRef param_stride, constant16;
2593 LLVMValueRef rel_patch_id = get_rel_patch_id(ctx);
2594
2595 vertices_per_patch = unpack_param(ctx, ctx->tcs_offchip_layout, 9, 6);
2596 num_patches = unpack_param(ctx, ctx->tcs_offchip_layout, 0, 9);
2597 total_vertices = LLVMBuildMul(ctx->builder, vertices_per_patch,
2598 num_patches, "");
2599
2600 constant16 = LLVMConstInt(ctx->i32, 16, false);
2601 if (vertex_index) {
2602 base_addr = LLVMBuildMul(ctx->builder, rel_patch_id,
2603 vertices_per_patch, "");
2604
2605 base_addr = LLVMBuildAdd(ctx->builder, base_addr,
2606 vertex_index, "");
2607
2608 param_stride = total_vertices;
2609 } else {
2610 base_addr = rel_patch_id;
2611 param_stride = num_patches;
2612 }
2613
2614 base_addr = LLVMBuildAdd(ctx->builder, base_addr,
2615 LLVMBuildMul(ctx->builder, param_index,
2616 param_stride, ""), "");
2617
2618 base_addr = LLVMBuildMul(ctx->builder, base_addr, constant16, "");
2619
2620 if (!vertex_index) {
2621 LLVMValueRef patch_data_offset =
2622 unpack_param(ctx, ctx->tcs_offchip_layout, 16, 16);
2623
2624 base_addr = LLVMBuildAdd(ctx->builder, base_addr,
2625 patch_data_offset, "");
2626 }
2627 return base_addr;
2628 }
2629
2630 static LLVMValueRef get_tcs_tes_buffer_address_params(struct nir_to_llvm_context *ctx,
2631 unsigned param,
2632 unsigned const_index,
2633 bool is_compact,
2634 LLVMValueRef vertex_index,
2635 LLVMValueRef indir_index)
2636 {
2637 LLVMValueRef param_index;
2638
2639 if (indir_index)
2640 param_index = LLVMBuildAdd(ctx->builder, LLVMConstInt(ctx->i32, param, false),
2641 indir_index, "");
2642 else {
2643 if (const_index && !is_compact)
2644 param += const_index;
2645 param_index = LLVMConstInt(ctx->i32, param, false);
2646 }
2647 return get_tcs_tes_buffer_address(ctx, vertex_index, param_index);
2648 }
2649
2650 static void
2651 mark_tess_output(struct nir_to_llvm_context *ctx,
2652 bool is_patch, uint32_t param)
2653
2654 {
2655 if (is_patch) {
2656 ctx->tess_patch_outputs_written |= (1ull << param);
2657 } else
2658 ctx->tess_outputs_written |= (1ull << param);
2659 }
2660
2661 static LLVMValueRef
2662 get_dw_address(struct nir_to_llvm_context *ctx,
2663 LLVMValueRef dw_addr,
2664 unsigned param,
2665 unsigned const_index,
2666 bool compact_const_index,
2667 LLVMValueRef vertex_index,
2668 LLVMValueRef stride,
2669 LLVMValueRef indir_index)
2670
2671 {
2672
2673 if (vertex_index) {
2674 dw_addr = LLVMBuildAdd(ctx->builder, dw_addr,
2675 LLVMBuildMul(ctx->builder,
2676 vertex_index,
2677 stride, ""), "");
2678 }
2679
2680 if (indir_index)
2681 dw_addr = LLVMBuildAdd(ctx->builder, dw_addr,
2682 LLVMBuildMul(ctx->builder, indir_index,
2683 LLVMConstInt(ctx->i32, 4, false), ""), "");
2684 else if (const_index && !compact_const_index)
2685 dw_addr = LLVMBuildAdd(ctx->builder, dw_addr,
2686 LLVMConstInt(ctx->i32, const_index, false), "");
2687
2688 dw_addr = LLVMBuildAdd(ctx->builder, dw_addr,
2689 LLVMConstInt(ctx->i32, param * 4, false), "");
2690
2691 if (const_index && compact_const_index)
2692 dw_addr = LLVMBuildAdd(ctx->builder, dw_addr,
2693 LLVMConstInt(ctx->i32, const_index, false), "");
2694 return dw_addr;
2695 }
2696
2697 static LLVMValueRef
2698 load_tcs_input(struct nir_to_llvm_context *ctx,
2699 nir_intrinsic_instr *instr)
2700 {
2701 LLVMValueRef dw_addr, stride;
2702 unsigned const_index;
2703 LLVMValueRef vertex_index;
2704 LLVMValueRef indir_index;
2705 unsigned param;
2706 LLVMValueRef value[4], result;
2707 const bool per_vertex = nir_is_per_vertex_io(instr->variables[0]->var, ctx->stage);
2708 const bool is_compact = instr->variables[0]->var->data.compact;
2709 param = shader_io_get_unique_index(instr->variables[0]->var->data.location);
2710 get_deref_offset(ctx->nir, instr->variables[0],
2711 false, NULL, per_vertex ? &vertex_index : NULL,
2712 &const_index, &indir_index);
2713
2714 stride = unpack_param(ctx, ctx->tcs_in_layout, 13, 8);
2715 dw_addr = get_tcs_in_current_patch_offset(ctx);
2716 dw_addr = get_dw_address(ctx, dw_addr, param, const_index, is_compact, vertex_index, stride,
2717 indir_index);
2718
2719 for (unsigned i = 0; i < instr->num_components; i++) {
2720 value[i] = lds_load(ctx, dw_addr);
2721 dw_addr = LLVMBuildAdd(ctx->builder, dw_addr,
2722 ctx->i32one, "");
2723 }
2724 result = ac_build_gather_values(&ctx->ac, value, instr->num_components);
2725 result = LLVMBuildBitCast(ctx->builder, result, get_def_type(ctx->nir, &instr->dest.ssa), "");
2726 return result;
2727 }
2728
2729 static LLVMValueRef
2730 load_tcs_output(struct nir_to_llvm_context *ctx,
2731 nir_intrinsic_instr *instr)
2732 {
2733 LLVMValueRef dw_addr, stride;
2734 LLVMValueRef value[4], result;
2735 LLVMValueRef vertex_index = NULL;
2736 LLVMValueRef indir_index = NULL;
2737 unsigned const_index = 0;
2738 unsigned param;
2739 const bool per_vertex = nir_is_per_vertex_io(instr->variables[0]->var, ctx->stage);
2740 const bool is_compact = instr->variables[0]->var->data.compact;
2741 param = shader_io_get_unique_index(instr->variables[0]->var->data.location);
2742 get_deref_offset(ctx->nir, instr->variables[0],
2743 false, NULL, per_vertex ? &vertex_index : NULL,
2744 &const_index, &indir_index);
2745
2746 if (!instr->variables[0]->var->data.patch) {
2747 stride = unpack_param(ctx, ctx->tcs_out_layout, 13, 8);
2748 dw_addr = get_tcs_out_current_patch_offset(ctx);
2749 } else {
2750 dw_addr = get_tcs_out_current_patch_data_offset(ctx);
2751 }
2752
2753 dw_addr = get_dw_address(ctx, dw_addr, param, const_index, is_compact, vertex_index, stride,
2754 indir_index);
2755
2756 for (unsigned i = 0; i < instr->num_components; i++) {
2757 value[i] = lds_load(ctx, dw_addr);
2758 dw_addr = LLVMBuildAdd(ctx->builder, dw_addr,
2759 ctx->i32one, "");
2760 }
2761 result = ac_build_gather_values(&ctx->ac, value, instr->num_components);
2762 result = LLVMBuildBitCast(ctx->builder, result, get_def_type(ctx->nir, &instr->dest.ssa), "");
2763 return result;
2764 }
2765
2766 static void
2767 store_tcs_output(struct nir_to_llvm_context *ctx,
2768 nir_intrinsic_instr *instr,
2769 LLVMValueRef src,
2770 unsigned writemask)
2771 {
2772 LLVMValueRef stride, dw_addr;
2773 LLVMValueRef buf_addr = NULL;
2774 LLVMValueRef vertex_index = NULL;
2775 LLVMValueRef indir_index = NULL;
2776 unsigned const_index = 0;
2777 unsigned param;
2778 const bool per_vertex = nir_is_per_vertex_io(instr->variables[0]->var, ctx->stage);
2779 const bool is_compact = instr->variables[0]->var->data.compact;
2780
2781 get_deref_offset(ctx->nir, instr->variables[0],
2782 false, NULL, per_vertex ? &vertex_index : NULL,
2783 &const_index, &indir_index);
2784
2785 param = shader_io_get_unique_index(instr->variables[0]->var->data.location);
2786 if (instr->variables[0]->var->data.location == VARYING_SLOT_CLIP_DIST0 &&
2787 is_compact && const_index > 3) {
2788 const_index -= 3;
2789 param++;
2790 }
2791
2792 if (!instr->variables[0]->var->data.patch) {
2793 stride = unpack_param(ctx, ctx->tcs_out_layout, 13, 8);
2794 dw_addr = get_tcs_out_current_patch_offset(ctx);
2795 } else {
2796 dw_addr = get_tcs_out_current_patch_data_offset(ctx);
2797 }
2798
2799 mark_tess_output(ctx, instr->variables[0]->var->data.patch, param);
2800
2801 dw_addr = get_dw_address(ctx, dw_addr, param, const_index, is_compact, vertex_index, stride,
2802 indir_index);
2803 buf_addr = get_tcs_tes_buffer_address_params(ctx, param, const_index, is_compact,
2804 vertex_index, indir_index);
2805
2806 unsigned base = is_compact ? const_index : 0;
2807 for (unsigned chan = 0; chan < 8; chan++) {
2808 bool is_tess_factor = false;
2809 if (!(writemask & (1 << chan)))
2810 continue;
2811 LLVMValueRef value = llvm_extract_elem(&ctx->ac, src, chan);
2812
2813 lds_store(ctx, dw_addr, value);
2814
2815 if (instr->variables[0]->var->data.location == VARYING_SLOT_TESS_LEVEL_INNER ||
2816 instr->variables[0]->var->data.location == VARYING_SLOT_TESS_LEVEL_OUTER)
2817 is_tess_factor = true;
2818
2819 if (!is_tess_factor && writemask != 0xF)
2820 ac_build_buffer_store_dword(&ctx->ac, ctx->hs_ring_tess_offchip, value, 1,
2821 buf_addr, ctx->oc_lds,
2822 4 * (base + chan), 1, 0, true, false);
2823
2824 dw_addr = LLVMBuildAdd(ctx->builder, dw_addr,
2825 ctx->i32one, "");
2826 }
2827
2828 if (writemask == 0xF) {
2829 ac_build_buffer_store_dword(&ctx->ac, ctx->hs_ring_tess_offchip, src, 4,
2830 buf_addr, ctx->oc_lds,
2831 (base * 4), 1, 0, true, false);
2832 }
2833 }
2834
2835 static LLVMValueRef
2836 load_tes_input(struct nir_to_llvm_context *ctx,
2837 const nir_intrinsic_instr *instr)
2838 {
2839 LLVMValueRef buf_addr;
2840 LLVMValueRef result;
2841 LLVMValueRef vertex_index = NULL;
2842 LLVMValueRef indir_index = NULL;
2843 unsigned const_index = 0;
2844 unsigned param;
2845 const bool per_vertex = nir_is_per_vertex_io(instr->variables[0]->var, ctx->stage);
2846 const bool is_compact = instr->variables[0]->var->data.compact;
2847
2848 get_deref_offset(ctx->nir, instr->variables[0],
2849 false, NULL, per_vertex ? &vertex_index : NULL,
2850 &const_index, &indir_index);
2851 param = shader_io_get_unique_index(instr->variables[0]->var->data.location);
2852 if (instr->variables[0]->var->data.location == VARYING_SLOT_CLIP_DIST0 &&
2853 is_compact && const_index > 3) {
2854 const_index -= 3;
2855 param++;
2856 }
2857 buf_addr = get_tcs_tes_buffer_address_params(ctx, param, const_index,
2858 is_compact, vertex_index, indir_index);
2859
2860 result = ac_build_buffer_load(&ctx->ac, ctx->hs_ring_tess_offchip, instr->num_components, NULL,
2861 buf_addr, ctx->oc_lds, is_compact ? (4 * const_index) : 0, 1, 0, true, false);
2862 result = trim_vector(&ctx->ac, result, instr->num_components);
2863 result = LLVMBuildBitCast(ctx->builder, result, get_def_type(ctx->nir, &instr->dest.ssa), "");
2864 return result;
2865 }
2866
2867 static LLVMValueRef
2868 load_gs_input(struct nir_to_llvm_context *ctx,
2869 nir_intrinsic_instr *instr)
2870 {
2871 LLVMValueRef indir_index, vtx_offset;
2872 unsigned const_index;
2873 LLVMValueRef args[9];
2874 unsigned param, vtx_offset_param;
2875 LLVMValueRef value[4], result;
2876 unsigned vertex_index;
2877 get_deref_offset(ctx->nir, instr->variables[0],
2878 false, &vertex_index, NULL,
2879 &const_index, &indir_index);
2880 vtx_offset_param = vertex_index;
2881 assert(vtx_offset_param < 6);
2882 vtx_offset = LLVMBuildMul(ctx->builder, ctx->gs_vtx_offset[vtx_offset_param],
2883 LLVMConstInt(ctx->i32, 4, false), "");
2884
2885 param = shader_io_get_unique_index(instr->variables[0]->var->data.location);
2886 for (unsigned i = 0; i < instr->num_components; i++) {
2887
2888 args[0] = ctx->esgs_ring;
2889 args[1] = vtx_offset;
2890 args[2] = LLVMConstInt(ctx->i32, (param * 4 + i + const_index) * 256, false);
2891 args[3] = ctx->i32zero;
2892 args[4] = ctx->i32one; /* OFFEN */
2893 args[5] = ctx->i32zero; /* IDXEN */
2894 args[6] = ctx->i32one; /* GLC */
2895 args[7] = ctx->i32zero; /* SLC */
2896 args[8] = ctx->i32zero; /* TFE */
2897
2898 value[i] = ac_build_intrinsic(&ctx->ac, "llvm.SI.buffer.load.dword.i32.i32",
2899 ctx->i32, args, 9,
2900 AC_FUNC_ATTR_READONLY |
2901 AC_FUNC_ATTR_LEGACY);
2902 }
2903 result = ac_build_gather_values(&ctx->ac, value, instr->num_components);
2904
2905 return result;
2906 }
2907
2908 static LLVMValueRef
2909 build_gep_for_deref(struct ac_nir_context *ctx,
2910 nir_deref_var *deref)
2911 {
2912 struct hash_entry *entry = _mesa_hash_table_search(ctx->vars, deref->var);
2913 assert(entry->data);
2914 LLVMValueRef val = entry->data;
2915 nir_deref *tail = deref->deref.child;
2916 while (tail != NULL) {
2917 LLVMValueRef offset;
2918 switch (tail->deref_type) {
2919 case nir_deref_type_array: {
2920 nir_deref_array *array = nir_deref_as_array(tail);
2921 offset = LLVMConstInt(ctx->ac.i32, array->base_offset, 0);
2922 if (array->deref_array_type ==
2923 nir_deref_array_type_indirect) {
2924 offset = LLVMBuildAdd(ctx->ac.builder, offset,
2925 get_src(ctx,
2926 array->indirect),
2927 "");
2928 }
2929 break;
2930 }
2931 case nir_deref_type_struct: {
2932 nir_deref_struct *deref_struct =
2933 nir_deref_as_struct(tail);
2934 offset = LLVMConstInt(ctx->ac.i32,
2935 deref_struct->index, 0);
2936 break;
2937 }
2938 default:
2939 unreachable("bad deref type");
2940 }
2941 val = ac_build_gep0(&ctx->ac, val, offset);
2942 tail = tail->child;
2943 }
2944 return val;
2945 }
2946
2947 static LLVMValueRef visit_load_var(struct ac_nir_context *ctx,
2948 nir_intrinsic_instr *instr)
2949 {
2950 LLVMValueRef values[8];
2951 int idx = instr->variables[0]->var->data.driver_location;
2952 int ve = instr->dest.ssa.num_components;
2953 LLVMValueRef indir_index;
2954 LLVMValueRef ret;
2955 unsigned const_index;
2956 bool vs_in = ctx->stage == MESA_SHADER_VERTEX &&
2957 instr->variables[0]->var->data.mode == nir_var_shader_in;
2958 get_deref_offset(ctx, instr->variables[0], vs_in, NULL, NULL,
2959 &const_index, &indir_index);
2960
2961 if (instr->dest.ssa.bit_size == 64)
2962 ve *= 2;
2963
2964 switch (instr->variables[0]->var->data.mode) {
2965 case nir_var_shader_in:
2966 if (ctx->stage == MESA_SHADER_TESS_CTRL)
2967 return load_tcs_input(ctx->nctx, instr);
2968 if (ctx->stage == MESA_SHADER_TESS_EVAL)
2969 return load_tes_input(ctx->nctx, instr);
2970 if (ctx->stage == MESA_SHADER_GEOMETRY) {
2971 return load_gs_input(ctx->nctx, instr);
2972 }
2973 for (unsigned chan = 0; chan < ve; chan++) {
2974 if (indir_index) {
2975 unsigned count = glsl_count_attribute_slots(
2976 instr->variables[0]->var->type,
2977 ctx->stage == MESA_SHADER_VERTEX);
2978 count -= chan / 4;
2979 LLVMValueRef tmp_vec = ac_build_gather_values_extended(
2980 &ctx->ac, ctx->abi->inputs + idx + chan, count,
2981 4, false, true);
2982
2983 values[chan] = LLVMBuildExtractElement(ctx->ac.builder,
2984 tmp_vec,
2985 indir_index, "");
2986 } else
2987 values[chan] = ctx->abi->inputs[idx + chan + const_index * 4];
2988 }
2989 break;
2990 case nir_var_local:
2991 for (unsigned chan = 0; chan < ve; chan++) {
2992 if (indir_index) {
2993 unsigned count = glsl_count_attribute_slots(
2994 instr->variables[0]->var->type, false);
2995 count -= chan / 4;
2996 LLVMValueRef tmp_vec = ac_build_gather_values_extended(
2997 &ctx->ac, ctx->locals + idx + chan, count,
2998 4, true, true);
2999
3000 values[chan] = LLVMBuildExtractElement(ctx->ac.builder,
3001 tmp_vec,
3002 indir_index, "");
3003 } else {
3004 values[chan] = LLVMBuildLoad(ctx->ac.builder, ctx->locals[idx + chan + const_index * 4], "");
3005 }
3006 }
3007 break;
3008 case nir_var_shared: {
3009 LLVMValueRef address = build_gep_for_deref(ctx,
3010 instr->variables[0]);
3011 LLVMValueRef val = LLVMBuildLoad(ctx->ac.builder, address, "");
3012 return LLVMBuildBitCast(ctx->ac.builder, val,
3013 get_def_type(ctx, &instr->dest.ssa),
3014 "");
3015 }
3016 case nir_var_shader_out:
3017 if (ctx->stage == MESA_SHADER_TESS_CTRL)
3018 return load_tcs_output(ctx->nctx, instr);
3019 for (unsigned chan = 0; chan < ve; chan++) {
3020 if (indir_index) {
3021 unsigned count = glsl_count_attribute_slots(
3022 instr->variables[0]->var->type, false);
3023 count -= chan / 4;
3024 LLVMValueRef tmp_vec = ac_build_gather_values_extended(
3025 &ctx->ac, ctx->outputs + idx + chan, count,
3026 4, true, true);
3027
3028 values[chan] = LLVMBuildExtractElement(ctx->ac.builder,
3029 tmp_vec,
3030 indir_index, "");
3031 } else {
3032 values[chan] = LLVMBuildLoad(ctx->ac.builder,
3033 ctx->outputs[idx + chan + const_index * 4],
3034 "");
3035 }
3036 }
3037 break;
3038 default:
3039 unreachable("unhandle variable mode");
3040 }
3041 ret = ac_build_gather_values(&ctx->ac, values, ve);
3042 return LLVMBuildBitCast(ctx->ac.builder, ret, get_def_type(ctx, &instr->dest.ssa), "");
3043 }
3044
3045 static void
3046 visit_store_var(struct ac_nir_context *ctx,
3047 nir_intrinsic_instr *instr)
3048 {
3049 LLVMValueRef temp_ptr, value;
3050 int idx = instr->variables[0]->var->data.driver_location;
3051 LLVMValueRef src = to_float(&ctx->ac, get_src(ctx, instr->src[0]));
3052 int writemask = instr->const_index[0];
3053 LLVMValueRef indir_index;
3054 unsigned const_index;
3055 get_deref_offset(ctx, instr->variables[0], false,
3056 NULL, NULL, &const_index, &indir_index);
3057
3058 if (get_elem_bits(&ctx->ac, LLVMTypeOf(src)) == 64) {
3059 int old_writemask = writemask;
3060
3061 src = LLVMBuildBitCast(ctx->ac.builder, src,
3062 LLVMVectorType(ctx->ac.f32, get_llvm_num_components(src) * 2),
3063 "");
3064
3065 writemask = 0;
3066 for (unsigned chan = 0; chan < 4; chan++) {
3067 if (old_writemask & (1 << chan))
3068 writemask |= 3u << (2 * chan);
3069 }
3070 }
3071
3072 switch (instr->variables[0]->var->data.mode) {
3073 case nir_var_shader_out:
3074
3075 if (ctx->stage == MESA_SHADER_TESS_CTRL) {
3076 store_tcs_output(ctx->nctx, instr, src, writemask);
3077 return;
3078 }
3079
3080 for (unsigned chan = 0; chan < 8; chan++) {
3081 int stride = 4;
3082 if (!(writemask & (1 << chan)))
3083 continue;
3084
3085 value = llvm_extract_elem(&ctx->ac, src, chan);
3086
3087 if (instr->variables[0]->var->data.compact)
3088 stride = 1;
3089 if (indir_index) {
3090 unsigned count = glsl_count_attribute_slots(
3091 instr->variables[0]->var->type, false);
3092 count -= chan / 4;
3093 LLVMValueRef tmp_vec = ac_build_gather_values_extended(
3094 &ctx->ac, ctx->outputs + idx + chan, count,
3095 stride, true, true);
3096
3097 tmp_vec = LLVMBuildInsertElement(ctx->ac.builder, tmp_vec,
3098 value, indir_index, "");
3099 build_store_values_extended(&ctx->ac, ctx->outputs + idx + chan,
3100 count, stride, tmp_vec);
3101
3102 } else {
3103 temp_ptr = ctx->outputs[idx + chan + const_index * stride];
3104
3105 LLVMBuildStore(ctx->ac.builder, value, temp_ptr);
3106 }
3107 }
3108 break;
3109 case nir_var_local:
3110 for (unsigned chan = 0; chan < 8; chan++) {
3111 if (!(writemask & (1 << chan)))
3112 continue;
3113
3114 value = llvm_extract_elem(&ctx->ac, src, chan);
3115 if (indir_index) {
3116 unsigned count = glsl_count_attribute_slots(
3117 instr->variables[0]->var->type, false);
3118 count -= chan / 4;
3119 LLVMValueRef tmp_vec = ac_build_gather_values_extended(
3120 &ctx->ac, ctx->locals + idx + chan, count,
3121 4, true, true);
3122
3123 tmp_vec = LLVMBuildInsertElement(ctx->ac.builder, tmp_vec,
3124 value, indir_index, "");
3125 build_store_values_extended(&ctx->ac, ctx->locals + idx + chan,
3126 count, 4, tmp_vec);
3127 } else {
3128 temp_ptr = ctx->locals[idx + chan + const_index * 4];
3129
3130 LLVMBuildStore(ctx->ac.builder, value, temp_ptr);
3131 }
3132 }
3133 break;
3134 case nir_var_shared: {
3135 int writemask = instr->const_index[0];
3136 LLVMValueRef address = build_gep_for_deref(ctx,
3137 instr->variables[0]);
3138 LLVMValueRef val = get_src(ctx, instr->src[0]);
3139 unsigned components =
3140 glsl_get_vector_elements(
3141 nir_deref_tail(&instr->variables[0]->deref)->type);
3142 if (writemask == (1 << components) - 1) {
3143 val = LLVMBuildBitCast(
3144 ctx->ac.builder, val,
3145 LLVMGetElementType(LLVMTypeOf(address)), "");
3146 LLVMBuildStore(ctx->ac.builder, val, address);
3147 } else {
3148 for (unsigned chan = 0; chan < 4; chan++) {
3149 if (!(writemask & (1 << chan)))
3150 continue;
3151 LLVMValueRef ptr =
3152 LLVMBuildStructGEP(ctx->ac.builder,
3153 address, chan, "");
3154 LLVMValueRef src = llvm_extract_elem(&ctx->ac, val,
3155 chan);
3156 src = LLVMBuildBitCast(
3157 ctx->ac.builder, src,
3158 LLVMGetElementType(LLVMTypeOf(ptr)), "");
3159 LLVMBuildStore(ctx->ac.builder, src, ptr);
3160 }
3161 }
3162 break;
3163 }
3164 default:
3165 break;
3166 }
3167 }
3168
3169 static int image_type_to_components_count(enum glsl_sampler_dim dim, bool array)
3170 {
3171 switch (dim) {
3172 case GLSL_SAMPLER_DIM_BUF:
3173 return 1;
3174 case GLSL_SAMPLER_DIM_1D:
3175 return array ? 2 : 1;
3176 case GLSL_SAMPLER_DIM_2D:
3177 return array ? 3 : 2;
3178 case GLSL_SAMPLER_DIM_MS:
3179 return array ? 4 : 3;
3180 case GLSL_SAMPLER_DIM_3D:
3181 case GLSL_SAMPLER_DIM_CUBE:
3182 return 3;
3183 case GLSL_SAMPLER_DIM_RECT:
3184 case GLSL_SAMPLER_DIM_SUBPASS:
3185 return 2;
3186 case GLSL_SAMPLER_DIM_SUBPASS_MS:
3187 return 3;
3188 default:
3189 break;
3190 }
3191 return 0;
3192 }
3193
3194
3195
3196 /* Adjust the sample index according to FMASK.
3197 *
3198 * For uncompressed MSAA surfaces, FMASK should return 0x76543210,
3199 * which is the identity mapping. Each nibble says which physical sample
3200 * should be fetched to get that sample.
3201 *
3202 * For example, 0x11111100 means there are only 2 samples stored and
3203 * the second sample covers 3/4 of the pixel. When reading samples 0
3204 * and 1, return physical sample 0 (determined by the first two 0s
3205 * in FMASK), otherwise return physical sample 1.
3206 *
3207 * The sample index should be adjusted as follows:
3208 * sample_index = (fmask >> (sample_index * 4)) & 0xF;
3209 */
3210 static LLVMValueRef adjust_sample_index_using_fmask(struct ac_llvm_context *ctx,
3211 LLVMValueRef coord_x, LLVMValueRef coord_y,
3212 LLVMValueRef coord_z,
3213 LLVMValueRef sample_index,
3214 LLVMValueRef fmask_desc_ptr)
3215 {
3216 LLVMValueRef fmask_load_address[4];
3217 LLVMValueRef res;
3218
3219 fmask_load_address[0] = coord_x;
3220 fmask_load_address[1] = coord_y;
3221 if (coord_z) {
3222 fmask_load_address[2] = coord_z;
3223 fmask_load_address[3] = LLVMGetUndef(ctx->i32);
3224 }
3225
3226 struct ac_image_args args = {0};
3227
3228 args.opcode = ac_image_load;
3229 args.da = coord_z ? true : false;
3230 args.resource = fmask_desc_ptr;
3231 args.dmask = 0xf;
3232 args.addr = ac_build_gather_values(ctx, fmask_load_address, coord_z ? 4 : 2);
3233
3234 res = ac_build_image_opcode(ctx, &args);
3235
3236 res = to_integer(ctx, res);
3237 LLVMValueRef four = LLVMConstInt(ctx->i32, 4, false);
3238 LLVMValueRef F = LLVMConstInt(ctx->i32, 0xf, false);
3239
3240 LLVMValueRef fmask = LLVMBuildExtractElement(ctx->builder,
3241 res,
3242 ctx->i32_0, "");
3243
3244 LLVMValueRef sample_index4 =
3245 LLVMBuildMul(ctx->builder, sample_index, four, "");
3246 LLVMValueRef shifted_fmask =
3247 LLVMBuildLShr(ctx->builder, fmask, sample_index4, "");
3248 LLVMValueRef final_sample =
3249 LLVMBuildAnd(ctx->builder, shifted_fmask, F, "");
3250
3251 /* Don't rewrite the sample index if WORD1.DATA_FORMAT of the FMASK
3252 * resource descriptor is 0 (invalid),
3253 */
3254 LLVMValueRef fmask_desc =
3255 LLVMBuildBitCast(ctx->builder, fmask_desc_ptr,
3256 ctx->v8i32, "");
3257
3258 LLVMValueRef fmask_word1 =
3259 LLVMBuildExtractElement(ctx->builder, fmask_desc,
3260 ctx->i32_1, "");
3261
3262 LLVMValueRef word1_is_nonzero =
3263 LLVMBuildICmp(ctx->builder, LLVMIntNE,
3264 fmask_word1, ctx->i32_0, "");
3265
3266 /* Replace the MSAA sample index. */
3267 sample_index =
3268 LLVMBuildSelect(ctx->builder, word1_is_nonzero,
3269 final_sample, sample_index, "");
3270 return sample_index;
3271 }
3272
3273 static LLVMValueRef get_image_coords(struct ac_nir_context *ctx,
3274 const nir_intrinsic_instr *instr)
3275 {
3276 const struct glsl_type *type = instr->variables[0]->var->type;
3277 if(instr->variables[0]->deref.child)
3278 type = instr->variables[0]->deref.child->type;
3279
3280 LLVMValueRef src0 = get_src(ctx, instr->src[0]);
3281 LLVMValueRef coords[4];
3282 LLVMValueRef masks[] = {
3283 LLVMConstInt(ctx->ac.i32, 0, false), LLVMConstInt(ctx->ac.i32, 1, false),
3284 LLVMConstInt(ctx->ac.i32, 2, false), LLVMConstInt(ctx->ac.i32, 3, false),
3285 };
3286 LLVMValueRef res;
3287 LLVMValueRef sample_index = llvm_extract_elem(&ctx->ac, get_src(ctx, instr->src[1]), 0);
3288
3289 int count;
3290 enum glsl_sampler_dim dim = glsl_get_sampler_dim(type);
3291 bool add_frag_pos = (dim == GLSL_SAMPLER_DIM_SUBPASS ||
3292 dim == GLSL_SAMPLER_DIM_SUBPASS_MS);
3293 bool is_ms = (dim == GLSL_SAMPLER_DIM_MS ||
3294 dim == GLSL_SAMPLER_DIM_SUBPASS_MS);
3295
3296 count = image_type_to_components_count(dim,
3297 glsl_sampler_type_is_array(type));
3298
3299 if (is_ms) {
3300 LLVMValueRef fmask_load_address[3];
3301 int chan;
3302
3303 fmask_load_address[0] = LLVMBuildExtractElement(ctx->ac.builder, src0, masks[0], "");
3304 fmask_load_address[1] = LLVMBuildExtractElement(ctx->ac.builder, src0, masks[1], "");
3305 if (glsl_sampler_type_is_array(type))
3306 fmask_load_address[2] = LLVMBuildExtractElement(ctx->ac.builder, src0, masks[2], "");
3307 else
3308 fmask_load_address[2] = NULL;
3309 if (add_frag_pos) {
3310 for (chan = 0; chan < 2; ++chan)
3311 fmask_load_address[chan] =
3312 LLVMBuildAdd(ctx->ac.builder, fmask_load_address[chan],
3313 LLVMBuildFPToUI(ctx->ac.builder, ctx->abi->frag_pos[chan],
3314 ctx->ac.i32, ""), "");
3315 }
3316 sample_index = adjust_sample_index_using_fmask(&ctx->ac,
3317 fmask_load_address[0],
3318 fmask_load_address[1],
3319 fmask_load_address[2],
3320 sample_index,
3321 get_sampler_desc(ctx, instr->variables[0], AC_DESC_FMASK, true, false));
3322 }
3323 if (count == 1) {
3324 if (instr->src[0].ssa->num_components)
3325 res = LLVMBuildExtractElement(ctx->ac.builder, src0, masks[0], "");
3326 else
3327 res = src0;
3328 } else {
3329 int chan;
3330 if (is_ms)
3331 count--;
3332 for (chan = 0; chan < count; ++chan) {
3333 coords[chan] = LLVMBuildExtractElement(ctx->ac.builder, src0, masks[chan], "");
3334 }
3335
3336 if (add_frag_pos) {
3337 for (chan = 0; chan < count; ++chan)
3338 coords[chan] = LLVMBuildAdd(ctx->ac.builder, coords[chan], LLVMBuildFPToUI(ctx->ac.builder, ctx->abi->frag_pos[chan],
3339 ctx->ac.i32, ""), "");
3340 }
3341 if (is_ms) {
3342 coords[count] = sample_index;
3343 count++;
3344 }
3345
3346 if (count == 3) {
3347 coords[3] = LLVMGetUndef(ctx->ac.i32);
3348 count = 4;
3349 }
3350 res = ac_build_gather_values(&ctx->ac, coords, count);
3351 }
3352 return res;
3353 }
3354
3355 static LLVMValueRef visit_image_load(struct ac_nir_context *ctx,
3356 const nir_intrinsic_instr *instr)
3357 {
3358 LLVMValueRef params[7];
3359 LLVMValueRef res;
3360 char intrinsic_name[64];
3361 const nir_variable *var = instr->variables[0]->var;
3362 const struct glsl_type *type = var->type;
3363 LLVMValueRef i1false = LLVMConstInt(ctx->ac.i1, 0, false);
3364 LLVMValueRef i1true = LLVMConstInt(ctx->ac.i1, 1, false);
3365
3366 if(instr->variables[0]->deref.child)
3367 type = instr->variables[0]->deref.child->type;
3368
3369 type = glsl_without_array(type);
3370 if (glsl_get_sampler_dim(type) == GLSL_SAMPLER_DIM_BUF) {
3371 params[0] = get_sampler_desc(ctx, instr->variables[0], AC_DESC_BUFFER, true, false);
3372 params[1] = LLVMBuildExtractElement(ctx->ac.builder, get_src(ctx, instr->src[0]),
3373 ctx->ac.i32_0, ""); /* vindex */
3374 params[2] = ctx->ac.i32_0; /* voffset */
3375 params[3] = i1false; /* glc */
3376 params[4] = i1false; /* slc */
3377 res = ac_build_intrinsic(&ctx->ac, "llvm.amdgcn.buffer.load.format.v4f32", ctx->ac.v4f32,
3378 params, 5, 0);
3379
3380 res = trim_vector(&ctx->ac, res, instr->dest.ssa.num_components);
3381 res = to_integer(&ctx->ac, res);
3382 } else {
3383 bool is_da = glsl_sampler_type_is_array(type) ||
3384 glsl_get_sampler_dim(type) == GLSL_SAMPLER_DIM_CUBE;
3385 LLVMValueRef da = is_da ? i1true : i1false;
3386 LLVMValueRef glc = i1false;
3387 LLVMValueRef slc = i1false;
3388
3389 params[0] = get_image_coords(ctx, instr);
3390 params[1] = get_sampler_desc(ctx, instr->variables[0], AC_DESC_IMAGE, true, false);
3391 params[2] = LLVMConstInt(ctx->ac.i32, 15, false); /* dmask */
3392 if (HAVE_LLVM <= 0x0309) {
3393 params[3] = i1false; /* r128 */
3394 params[4] = da;
3395 params[5] = glc;
3396 params[6] = slc;
3397 } else {
3398 LLVMValueRef lwe = i1false;
3399 params[3] = glc;
3400 params[4] = slc;
3401 params[5] = lwe;
3402 params[6] = da;
3403 }
3404
3405 ac_get_image_intr_name("llvm.amdgcn.image.load",
3406 ctx->ac.v4f32, /* vdata */
3407 LLVMTypeOf(params[0]), /* coords */
3408 LLVMTypeOf(params[1]), /* rsrc */
3409 intrinsic_name, sizeof(intrinsic_name));
3410
3411 res = ac_build_intrinsic(&ctx->ac, intrinsic_name, ctx->ac.v4f32,
3412 params, 7, AC_FUNC_ATTR_READONLY);
3413 }
3414 return to_integer(&ctx->ac, res);
3415 }
3416
3417 static void visit_image_store(struct ac_nir_context *ctx,
3418 nir_intrinsic_instr *instr)
3419 {
3420 LLVMValueRef params[8];
3421 char intrinsic_name[64];
3422 const nir_variable *var = instr->variables[0]->var;
3423 const struct glsl_type *type = glsl_without_array(var->type);
3424 LLVMValueRef i1false = LLVMConstInt(ctx->ac.i1, 0, false);
3425 LLVMValueRef i1true = LLVMConstInt(ctx->ac.i1, 1, false);
3426 LLVMValueRef glc = i1false;
3427 bool force_glc = ctx->abi->chip_class == SI;
3428 if (force_glc)
3429 glc = i1true;
3430
3431 if (glsl_get_sampler_dim(type) == GLSL_SAMPLER_DIM_BUF) {
3432 params[0] = to_float(&ctx->ac, get_src(ctx, instr->src[2])); /* data */
3433 params[1] = get_sampler_desc(ctx, instr->variables[0], AC_DESC_BUFFER, true, true);
3434 params[2] = LLVMBuildExtractElement(ctx->ac.builder, get_src(ctx, instr->src[0]),
3435 ctx->ac.i32_0, ""); /* vindex */
3436 params[3] = ctx->ac.i32_0; /* voffset */
3437 params[4] = glc; /* glc */
3438 params[5] = i1false; /* slc */
3439 ac_build_intrinsic(&ctx->ac, "llvm.amdgcn.buffer.store.format.v4f32", ctx->ac.voidt,
3440 params, 6, 0);
3441 } else {
3442 bool is_da = glsl_sampler_type_is_array(type) ||
3443 glsl_get_sampler_dim(type) == GLSL_SAMPLER_DIM_CUBE;
3444 LLVMValueRef da = is_da ? i1true : i1false;
3445 LLVMValueRef slc = i1false;
3446
3447 params[0] = to_float(&ctx->ac, get_src(ctx, instr->src[2]));
3448 params[1] = get_image_coords(ctx, instr); /* coords */
3449 params[2] = get_sampler_desc(ctx, instr->variables[0], AC_DESC_IMAGE, true, true);
3450 params[3] = LLVMConstInt(ctx->ac.i32, 15, false); /* dmask */
3451 if (HAVE_LLVM <= 0x0309) {
3452 params[4] = i1false; /* r128 */
3453 params[5] = da;
3454 params[6] = glc;
3455 params[7] = slc;
3456 } else {
3457 LLVMValueRef lwe = i1false;
3458 params[4] = glc;
3459 params[5] = slc;
3460 params[6] = lwe;
3461 params[7] = da;
3462 }
3463
3464 ac_get_image_intr_name("llvm.amdgcn.image.store",
3465 LLVMTypeOf(params[0]), /* vdata */
3466 LLVMTypeOf(params[1]), /* coords */
3467 LLVMTypeOf(params[2]), /* rsrc */
3468 intrinsic_name, sizeof(intrinsic_name));
3469
3470 ac_build_intrinsic(&ctx->ac, intrinsic_name, ctx->ac.voidt,
3471 params, 8, 0);
3472 }
3473
3474 }
3475
3476 static LLVMValueRef visit_image_atomic(struct ac_nir_context *ctx,
3477 const nir_intrinsic_instr *instr)
3478 {
3479 LLVMValueRef params[6];
3480 int param_count = 0;
3481 const nir_variable *var = instr->variables[0]->var;
3482
3483 const char *atomic_name;
3484 char intrinsic_name[41];
3485 const struct glsl_type *type = glsl_without_array(var->type);
3486 LLVMValueRef i1false = LLVMConstInt(ctx->ac.i1, 0, false);
3487 LLVMValueRef i1true = LLVMConstInt(ctx->ac.i1, 1, false);
3488 MAYBE_UNUSED int length;
3489
3490 switch (instr->intrinsic) {
3491 case nir_intrinsic_image_atomic_add:
3492 atomic_name = "add";
3493 break;
3494 case nir_intrinsic_image_atomic_min:
3495 atomic_name = "smin";
3496 break;
3497 case nir_intrinsic_image_atomic_max:
3498 atomic_name = "smax";
3499 break;
3500 case nir_intrinsic_image_atomic_and:
3501 atomic_name = "and";
3502 break;
3503 case nir_intrinsic_image_atomic_or:
3504 atomic_name = "or";
3505 break;
3506 case nir_intrinsic_image_atomic_xor:
3507 atomic_name = "xor";
3508 break;
3509 case nir_intrinsic_image_atomic_exchange:
3510 atomic_name = "swap";
3511 break;
3512 case nir_intrinsic_image_atomic_comp_swap:
3513 atomic_name = "cmpswap";
3514 break;
3515 default:
3516 abort();
3517 }
3518
3519 if (instr->intrinsic == nir_intrinsic_image_atomic_comp_swap)
3520 params[param_count++] = get_src(ctx, instr->src[3]);
3521 params[param_count++] = get_src(ctx, instr->src[2]);
3522
3523 if (glsl_get_sampler_dim(type) == GLSL_SAMPLER_DIM_BUF) {
3524 params[param_count++] = get_sampler_desc(ctx, instr->variables[0], AC_DESC_BUFFER,
3525 true, true);
3526 params[param_count++] = LLVMBuildExtractElement(ctx->ac.builder, get_src(ctx, instr->src[0]),
3527 ctx->ac.i32_0, ""); /* vindex */
3528 params[param_count++] = ctx->ac.i32_0; /* voffset */
3529 params[param_count++] = i1false; /* slc */
3530
3531 length = snprintf(intrinsic_name, sizeof(intrinsic_name),
3532 "llvm.amdgcn.buffer.atomic.%s", atomic_name);
3533 } else {
3534 char coords_type[8];
3535
3536 bool da = glsl_sampler_type_is_array(type) ||
3537 glsl_get_sampler_dim(type) == GLSL_SAMPLER_DIM_CUBE;
3538
3539 LLVMValueRef coords = params[param_count++] = get_image_coords(ctx, instr);
3540 params[param_count++] = get_sampler_desc(ctx, instr->variables[0], AC_DESC_IMAGE,
3541 true, true);
3542 params[param_count++] = i1false; /* r128 */
3543 params[param_count++] = da ? i1true : i1false; /* da */
3544 params[param_count++] = i1false; /* slc */
3545
3546 build_int_type_name(LLVMTypeOf(coords),
3547 coords_type, sizeof(coords_type));
3548
3549 length = snprintf(intrinsic_name, sizeof(intrinsic_name),
3550 "llvm.amdgcn.image.atomic.%s.%s", atomic_name, coords_type);
3551 }
3552
3553 assert(length < sizeof(intrinsic_name));
3554 return ac_build_intrinsic(&ctx->ac, intrinsic_name, ctx->ac.i32, params, param_count, 0);
3555 }
3556
3557 static LLVMValueRef visit_image_size(struct ac_nir_context *ctx,
3558 const nir_intrinsic_instr *instr)
3559 {
3560 LLVMValueRef res;
3561 const nir_variable *var = instr->variables[0]->var;
3562 const struct glsl_type *type = instr->variables[0]->var->type;
3563 bool da = glsl_sampler_type_is_array(var->type) ||
3564 glsl_get_sampler_dim(var->type) == GLSL_SAMPLER_DIM_CUBE;
3565 if(instr->variables[0]->deref.child)
3566 type = instr->variables[0]->deref.child->type;
3567
3568 if (glsl_get_sampler_dim(type) == GLSL_SAMPLER_DIM_BUF)
3569 return get_buffer_size(ctx,
3570 get_sampler_desc(ctx, instr->variables[0],
3571 AC_DESC_BUFFER, true, false), true);
3572
3573 struct ac_image_args args = { 0 };
3574
3575 args.da = da;
3576 args.dmask = 0xf;
3577 args.resource = get_sampler_desc(ctx, instr->variables[0], AC_DESC_IMAGE, true, false);
3578 args.opcode = ac_image_get_resinfo;
3579 args.addr = ctx->ac.i32_0;
3580
3581 res = ac_build_image_opcode(&ctx->ac, &args);
3582
3583 if (glsl_get_sampler_dim(type) == GLSL_SAMPLER_DIM_CUBE &&
3584 glsl_sampler_type_is_array(type)) {
3585 LLVMValueRef two = LLVMConstInt(ctx->ac.i32, 2, false);
3586 LLVMValueRef six = LLVMConstInt(ctx->ac.i32, 6, false);
3587 LLVMValueRef z = LLVMBuildExtractElement(ctx->ac.builder, res, two, "");
3588 z = LLVMBuildSDiv(ctx->ac.builder, z, six, "");
3589 res = LLVMBuildInsertElement(ctx->ac.builder, res, z, two, "");
3590 }
3591 return res;
3592 }
3593
3594 #define NOOP_WAITCNT 0xf7f
3595 #define LGKM_CNT 0x07f
3596 #define VM_CNT 0xf70
3597
3598 static void emit_waitcnt(struct nir_to_llvm_context *ctx,
3599 unsigned simm16)
3600 {
3601 LLVMValueRef args[1] = {
3602 LLVMConstInt(ctx->i32, simm16, false),
3603 };
3604 ac_build_intrinsic(&ctx->ac, "llvm.amdgcn.s.waitcnt",
3605 ctx->voidt, args, 1, 0);
3606 }
3607
3608 static void emit_barrier(struct nir_to_llvm_context *ctx)
3609 {
3610 /* SI only (thanks to a hw bug workaround):
3611 * The real barrier instruction isn’t needed, because an entire patch
3612 * always fits into a single wave.
3613 */
3614 if (ctx->options->chip_class == SI &&
3615 ctx->stage == MESA_SHADER_TESS_CTRL) {
3616 emit_waitcnt(ctx, LGKM_CNT & VM_CNT);
3617 return;
3618 }
3619 ac_build_intrinsic(&ctx->ac, "llvm.amdgcn.s.barrier",
3620 ctx->voidt, NULL, 0, AC_FUNC_ATTR_CONVERGENT);
3621 }
3622
3623 static void emit_discard_if(struct ac_nir_context *ctx,
3624 const nir_intrinsic_instr *instr)
3625 {
3626 LLVMValueRef cond;
3627
3628 cond = LLVMBuildICmp(ctx->ac.builder, LLVMIntNE,
3629 get_src(ctx, instr->src[0]),
3630 ctx->ac.i32_0, "");
3631
3632 cond = LLVMBuildSelect(ctx->ac.builder, cond,
3633 LLVMConstReal(ctx->ac.f32, -1.0f),
3634 ctx->ac.f32_0, "");
3635 ac_build_kill(&ctx->ac, cond);
3636 }
3637
3638 static LLVMValueRef
3639 visit_load_local_invocation_index(struct nir_to_llvm_context *ctx)
3640 {
3641 LLVMValueRef result;
3642 LLVMValueRef thread_id = ac_get_thread_id(&ctx->ac);
3643 result = LLVMBuildAnd(ctx->builder, ctx->tg_size,
3644 LLVMConstInt(ctx->i32, 0xfc0, false), "");
3645
3646 return LLVMBuildAdd(ctx->builder, result, thread_id, "");
3647 }
3648
3649 static LLVMValueRef visit_var_atomic(struct nir_to_llvm_context *ctx,
3650 const nir_intrinsic_instr *instr)
3651 {
3652 LLVMValueRef ptr, result;
3653 LLVMValueRef src = get_src(ctx->nir, instr->src[0]);
3654 ptr = build_gep_for_deref(ctx->nir, instr->variables[0]);
3655
3656 if (instr->intrinsic == nir_intrinsic_var_atomic_comp_swap) {
3657 LLVMValueRef src1 = get_src(ctx->nir, instr->src[1]);
3658 result = LLVMBuildAtomicCmpXchg(ctx->builder,
3659 ptr, src, src1,
3660 LLVMAtomicOrderingSequentiallyConsistent,
3661 LLVMAtomicOrderingSequentiallyConsistent,
3662 false);
3663 } else {
3664 LLVMAtomicRMWBinOp op;
3665 switch (instr->intrinsic) {
3666 case nir_intrinsic_var_atomic_add:
3667 op = LLVMAtomicRMWBinOpAdd;
3668 break;
3669 case nir_intrinsic_var_atomic_umin:
3670 op = LLVMAtomicRMWBinOpUMin;
3671 break;
3672 case nir_intrinsic_var_atomic_umax:
3673 op = LLVMAtomicRMWBinOpUMax;
3674 break;
3675 case nir_intrinsic_var_atomic_imin:
3676 op = LLVMAtomicRMWBinOpMin;
3677 break;
3678 case nir_intrinsic_var_atomic_imax:
3679 op = LLVMAtomicRMWBinOpMax;
3680 break;
3681 case nir_intrinsic_var_atomic_and:
3682 op = LLVMAtomicRMWBinOpAnd;
3683 break;
3684 case nir_intrinsic_var_atomic_or:
3685 op = LLVMAtomicRMWBinOpOr;
3686 break;
3687 case nir_intrinsic_var_atomic_xor:
3688 op = LLVMAtomicRMWBinOpXor;
3689 break;
3690 case nir_intrinsic_var_atomic_exchange:
3691 op = LLVMAtomicRMWBinOpXchg;
3692 break;
3693 default:
3694 return NULL;
3695 }
3696
3697 result = LLVMBuildAtomicRMW(ctx->builder, op, ptr, to_integer(&ctx->ac, src),
3698 LLVMAtomicOrderingSequentiallyConsistent,
3699 false);
3700 }
3701 return result;
3702 }
3703
3704 #define INTERP_CENTER 0
3705 #define INTERP_CENTROID 1
3706 #define INTERP_SAMPLE 2
3707
3708 static LLVMValueRef lookup_interp_param(struct nir_to_llvm_context *ctx,
3709 enum glsl_interp_mode interp, unsigned location)
3710 {
3711 switch (interp) {
3712 case INTERP_MODE_FLAT:
3713 default:
3714 return NULL;
3715 case INTERP_MODE_SMOOTH:
3716 case INTERP_MODE_NONE:
3717 if (location == INTERP_CENTER)
3718 return ctx->persp_center;
3719 else if (location == INTERP_CENTROID)
3720 return ctx->persp_centroid;
3721 else if (location == INTERP_SAMPLE)
3722 return ctx->persp_sample;
3723 break;
3724 case INTERP_MODE_NOPERSPECTIVE:
3725 if (location == INTERP_CENTER)
3726 return ctx->linear_center;
3727 else if (location == INTERP_CENTROID)
3728 return ctx->linear_centroid;
3729 else if (location == INTERP_SAMPLE)
3730 return ctx->linear_sample;
3731 break;
3732 }
3733 return NULL;
3734 }
3735
3736 static LLVMValueRef load_sample_position(struct nir_to_llvm_context *ctx,
3737 LLVMValueRef sample_id)
3738 {
3739 LLVMValueRef result;
3740 LLVMValueRef ptr = ac_build_gep0(&ctx->ac, ctx->ring_offsets, LLVMConstInt(ctx->i32, RING_PS_SAMPLE_POSITIONS, false));
3741
3742 ptr = LLVMBuildBitCast(ctx->builder, ptr,
3743 const_array(ctx->v2f32, 64), "");
3744
3745 sample_id = LLVMBuildAdd(ctx->builder, sample_id, ctx->sample_pos_offset, "");
3746 result = ac_build_indexed_load(&ctx->ac, ptr, sample_id, false);
3747
3748 return result;
3749 }
3750
3751 static LLVMValueRef load_sample_pos(struct ac_nir_context *ctx)
3752 {
3753 LLVMValueRef values[2];
3754
3755 values[0] = emit_ffract(&ctx->ac, ctx->abi->frag_pos[0]);
3756 values[1] = emit_ffract(&ctx->ac, ctx->abi->frag_pos[1]);
3757 return ac_build_gather_values(&ctx->ac, values, 2);
3758 }
3759
3760 static LLVMValueRef visit_interp(struct nir_to_llvm_context *ctx,
3761 const nir_intrinsic_instr *instr)
3762 {
3763 LLVMValueRef result[2];
3764 LLVMValueRef interp_param, attr_number;
3765 unsigned location;
3766 unsigned chan;
3767 LLVMValueRef src_c0, src_c1;
3768 LLVMValueRef src0;
3769 int input_index = instr->variables[0]->var->data.location - VARYING_SLOT_VAR0;
3770 switch (instr->intrinsic) {
3771 case nir_intrinsic_interp_var_at_centroid:
3772 location = INTERP_CENTROID;
3773 break;
3774 case nir_intrinsic_interp_var_at_sample:
3775 case nir_intrinsic_interp_var_at_offset:
3776 location = INTERP_CENTER;
3777 src0 = get_src(ctx->nir, instr->src[0]);
3778 break;
3779 default:
3780 break;
3781 }
3782
3783 if (instr->intrinsic == nir_intrinsic_interp_var_at_offset) {
3784 src_c0 = to_float(&ctx->ac, LLVMBuildExtractElement(ctx->builder, src0, ctx->i32zero, ""));
3785 src_c1 = to_float(&ctx->ac, LLVMBuildExtractElement(ctx->builder, src0, ctx->i32one, ""));
3786 } else if (instr->intrinsic == nir_intrinsic_interp_var_at_sample) {
3787 LLVMValueRef sample_position;
3788 LLVMValueRef halfval = LLVMConstReal(ctx->f32, 0.5f);
3789
3790 /* fetch sample ID */
3791 sample_position = load_sample_position(ctx, src0);
3792
3793 src_c0 = LLVMBuildExtractElement(ctx->builder, sample_position, ctx->i32zero, "");
3794 src_c0 = LLVMBuildFSub(ctx->builder, src_c0, halfval, "");
3795 src_c1 = LLVMBuildExtractElement(ctx->builder, sample_position, ctx->i32one, "");
3796 src_c1 = LLVMBuildFSub(ctx->builder, src_c1, halfval, "");
3797 }
3798 interp_param = lookup_interp_param(ctx, instr->variables[0]->var->data.interpolation, location);
3799 attr_number = LLVMConstInt(ctx->i32, input_index, false);
3800
3801 if (location == INTERP_SAMPLE || location == INTERP_CENTER) {
3802 LLVMValueRef ij_out[2];
3803 LLVMValueRef ddxy_out = emit_ddxy_interp(ctx->nir, interp_param);
3804
3805 /*
3806 * take the I then J parameters, and the DDX/Y for it, and
3807 * calculate the IJ inputs for the interpolator.
3808 * temp1 = ddx * offset/sample.x + I;
3809 * interp_param.I = ddy * offset/sample.y + temp1;
3810 * temp1 = ddx * offset/sample.x + J;
3811 * interp_param.J = ddy * offset/sample.y + temp1;
3812 */
3813 for (unsigned i = 0; i < 2; i++) {
3814 LLVMValueRef ix_ll = LLVMConstInt(ctx->i32, i, false);
3815 LLVMValueRef iy_ll = LLVMConstInt(ctx->i32, i + 2, false);
3816 LLVMValueRef ddx_el = LLVMBuildExtractElement(ctx->builder,
3817 ddxy_out, ix_ll, "");
3818 LLVMValueRef ddy_el = LLVMBuildExtractElement(ctx->builder,
3819 ddxy_out, iy_ll, "");
3820 LLVMValueRef interp_el = LLVMBuildExtractElement(ctx->builder,
3821 interp_param, ix_ll, "");
3822 LLVMValueRef temp1, temp2;
3823
3824 interp_el = LLVMBuildBitCast(ctx->builder, interp_el,
3825 ctx->f32, "");
3826
3827 temp1 = LLVMBuildFMul(ctx->builder, ddx_el, src_c0, "");
3828 temp1 = LLVMBuildFAdd(ctx->builder, temp1, interp_el, "");
3829
3830 temp2 = LLVMBuildFMul(ctx->builder, ddy_el, src_c1, "");
3831 temp2 = LLVMBuildFAdd(ctx->builder, temp2, temp1, "");
3832
3833 ij_out[i] = LLVMBuildBitCast(ctx->builder,
3834 temp2, ctx->i32, "");
3835 }
3836 interp_param = ac_build_gather_values(&ctx->ac, ij_out, 2);
3837
3838 }
3839
3840 for (chan = 0; chan < 2; chan++) {
3841 LLVMValueRef llvm_chan = LLVMConstInt(ctx->i32, chan, false);
3842
3843 if (interp_param) {
3844 interp_param = LLVMBuildBitCast(ctx->builder,
3845 interp_param, LLVMVectorType(ctx->f32, 2), "");
3846 LLVMValueRef i = LLVMBuildExtractElement(
3847 ctx->builder, interp_param, ctx->i32zero, "");
3848 LLVMValueRef j = LLVMBuildExtractElement(
3849 ctx->builder, interp_param, ctx->i32one, "");
3850
3851 result[chan] = ac_build_fs_interp(&ctx->ac,
3852 llvm_chan, attr_number,
3853 ctx->prim_mask, i, j);
3854 } else {
3855 result[chan] = ac_build_fs_interp_mov(&ctx->ac,
3856 LLVMConstInt(ctx->i32, 2, false),
3857 llvm_chan, attr_number,
3858 ctx->prim_mask);
3859 }
3860 }
3861 return ac_build_gather_values(&ctx->ac, result, 2);
3862 }
3863
3864 static void
3865 visit_emit_vertex(struct nir_to_llvm_context *ctx,
3866 const nir_intrinsic_instr *instr)
3867 {
3868 LLVMValueRef gs_next_vertex;
3869 LLVMValueRef can_emit, kill;
3870 int idx;
3871
3872 assert(instr->const_index[0] == 0);
3873 /* Write vertex attribute values to GSVS ring */
3874 gs_next_vertex = LLVMBuildLoad(ctx->builder,
3875 ctx->gs_next_vertex,
3876 "");
3877
3878 /* If this thread has already emitted the declared maximum number of
3879 * vertices, kill it: excessive vertex emissions are not supposed to
3880 * have any effect, and GS threads have no externally observable
3881 * effects other than emitting vertices.
3882 */
3883 can_emit = LLVMBuildICmp(ctx->builder, LLVMIntULT, gs_next_vertex,
3884 LLVMConstInt(ctx->i32, ctx->gs_max_out_vertices, false), "");
3885
3886 kill = LLVMBuildSelect(ctx->builder, can_emit,
3887 LLVMConstReal(ctx->f32, 1.0f),
3888 LLVMConstReal(ctx->f32, -1.0f), "");
3889 ac_build_kill(&ctx->ac, kill);
3890
3891 /* loop num outputs */
3892 idx = 0;
3893 for (unsigned i = 0; i < RADEON_LLVM_MAX_OUTPUTS; ++i) {
3894 LLVMValueRef *out_ptr = &ctx->nir->outputs[i * 4];
3895 int length = 4;
3896 int slot = idx;
3897 int slot_inc = 1;
3898
3899 if (!(ctx->output_mask & (1ull << i)))
3900 continue;
3901
3902 if (i == VARYING_SLOT_CLIP_DIST0) {
3903 /* pack clip and cull into a single set of slots */
3904 length = ctx->num_output_clips + ctx->num_output_culls;
3905 if (length > 4)
3906 slot_inc = 2;
3907 }
3908 for (unsigned j = 0; j < length; j++) {
3909 LLVMValueRef out_val = LLVMBuildLoad(ctx->builder,
3910 out_ptr[j], "");
3911 LLVMValueRef voffset = LLVMConstInt(ctx->i32, (slot * 4 + j) * ctx->gs_max_out_vertices, false);
3912 voffset = LLVMBuildAdd(ctx->builder, voffset, gs_next_vertex, "");
3913 voffset = LLVMBuildMul(ctx->builder, voffset, LLVMConstInt(ctx->i32, 4, false), "");
3914
3915 out_val = LLVMBuildBitCast(ctx->builder, out_val, ctx->i32, "");
3916
3917 ac_build_buffer_store_dword(&ctx->ac, ctx->gsvs_ring,
3918 out_val, 1,
3919 voffset, ctx->gs2vs_offset, 0,
3920 1, 1, true, true);
3921 }
3922 idx += slot_inc;
3923 }
3924
3925 gs_next_vertex = LLVMBuildAdd(ctx->builder, gs_next_vertex,
3926 ctx->i32one, "");
3927 LLVMBuildStore(ctx->builder, gs_next_vertex, ctx->gs_next_vertex);
3928
3929 ac_build_sendmsg(&ctx->ac, AC_SENDMSG_GS_OP_EMIT | AC_SENDMSG_GS | (0 << 8), ctx->gs_wave_id);
3930 }
3931
3932 static void
3933 visit_end_primitive(struct nir_to_llvm_context *ctx,
3934 const nir_intrinsic_instr *instr)
3935 {
3936 ac_build_sendmsg(&ctx->ac, AC_SENDMSG_GS_OP_CUT | AC_SENDMSG_GS | (0 << 8), ctx->gs_wave_id);
3937 }
3938
3939 static LLVMValueRef
3940 visit_load_tess_coord(struct nir_to_llvm_context *ctx,
3941 const nir_intrinsic_instr *instr)
3942 {
3943 LLVMValueRef coord[4] = {
3944 ctx->tes_u,
3945 ctx->tes_v,
3946 ctx->f32zero,
3947 ctx->f32zero,
3948 };
3949
3950 if (ctx->tes_primitive_mode == GL_TRIANGLES)
3951 coord[2] = LLVMBuildFSub(ctx->builder, ctx->f32one,
3952 LLVMBuildFAdd(ctx->builder, coord[0], coord[1], ""), "");
3953
3954 LLVMValueRef result = ac_build_gather_values(&ctx->ac, coord, instr->num_components);
3955 return LLVMBuildBitCast(ctx->builder, result,
3956 get_def_type(ctx->nir, &instr->dest.ssa), "");
3957 }
3958
3959 static void visit_intrinsic(struct ac_nir_context *ctx,
3960 nir_intrinsic_instr *instr)
3961 {
3962 LLVMValueRef result = NULL;
3963
3964 switch (instr->intrinsic) {
3965 case nir_intrinsic_load_work_group_id: {
3966 result = ctx->nctx->workgroup_ids;
3967 break;
3968 }
3969 case nir_intrinsic_load_base_vertex: {
3970 result = ctx->abi->base_vertex;
3971 break;
3972 }
3973 case nir_intrinsic_load_vertex_id_zero_base: {
3974 result = ctx->abi->vertex_id;
3975 break;
3976 }
3977 case nir_intrinsic_load_local_invocation_id: {
3978 result = ctx->nctx->local_invocation_ids;
3979 break;
3980 }
3981 case nir_intrinsic_load_base_instance:
3982 result = ctx->abi->start_instance;
3983 break;
3984 case nir_intrinsic_load_draw_id:
3985 result = ctx->abi->draw_id;
3986 break;
3987 case nir_intrinsic_load_invocation_id:
3988 if (ctx->stage == MESA_SHADER_TESS_CTRL)
3989 result = unpack_param(ctx->nctx, ctx->nctx->tcs_rel_ids, 8, 5);
3990 else
3991 result = ctx->nctx->gs_invocation_id;
3992 break;
3993 case nir_intrinsic_load_primitive_id:
3994 if (ctx->stage == MESA_SHADER_GEOMETRY) {
3995 ctx->nctx->shader_info->gs.uses_prim_id = true;
3996 result = ctx->nctx->gs_prim_id;
3997 } else if (ctx->stage == MESA_SHADER_TESS_CTRL) {
3998 ctx->nctx->shader_info->tcs.uses_prim_id = true;
3999 result = ctx->nctx->tcs_patch_id;
4000 } else if (ctx->stage == MESA_SHADER_TESS_EVAL) {
4001 ctx->nctx->shader_info->tcs.uses_prim_id = true;
4002 result = ctx->nctx->tes_patch_id;
4003 } else
4004 fprintf(stderr, "Unknown primitive id intrinsic: %d", ctx->stage);
4005 break;
4006 case nir_intrinsic_load_sample_id:
4007 result = unpack_param(ctx->nctx, ctx->abi->ancillary, 8, 4);
4008 break;
4009 case nir_intrinsic_load_sample_pos:
4010 result = load_sample_pos(ctx);
4011 break;
4012 case nir_intrinsic_load_sample_mask_in:
4013 result = ctx->abi->sample_coverage;
4014 break;
4015 case nir_intrinsic_load_front_face:
4016 result = ctx->abi->front_face;
4017 break;
4018 case nir_intrinsic_load_instance_id:
4019 result = ctx->abi->instance_id;
4020 break;
4021 case nir_intrinsic_load_num_work_groups:
4022 result = ctx->nctx->num_work_groups;
4023 break;
4024 case nir_intrinsic_load_local_invocation_index:
4025 result = visit_load_local_invocation_index(ctx->nctx);
4026 break;
4027 case nir_intrinsic_load_push_constant:
4028 result = visit_load_push_constant(ctx->nctx, instr);
4029 break;
4030 case nir_intrinsic_vulkan_resource_index:
4031 result = visit_vulkan_resource_index(ctx->nctx, instr);
4032 break;
4033 case nir_intrinsic_store_ssbo:
4034 visit_store_ssbo(ctx, instr);
4035 break;
4036 case nir_intrinsic_load_ssbo:
4037 result = visit_load_buffer(ctx, instr);
4038 break;
4039 case nir_intrinsic_ssbo_atomic_add:
4040 case nir_intrinsic_ssbo_atomic_imin:
4041 case nir_intrinsic_ssbo_atomic_umin:
4042 case nir_intrinsic_ssbo_atomic_imax:
4043 case nir_intrinsic_ssbo_atomic_umax:
4044 case nir_intrinsic_ssbo_atomic_and:
4045 case nir_intrinsic_ssbo_atomic_or:
4046 case nir_intrinsic_ssbo_atomic_xor:
4047 case nir_intrinsic_ssbo_atomic_exchange:
4048 case nir_intrinsic_ssbo_atomic_comp_swap:
4049 result = visit_atomic_ssbo(ctx, instr);
4050 break;
4051 case nir_intrinsic_load_ubo:
4052 result = visit_load_ubo_buffer(ctx, instr);
4053 break;
4054 case nir_intrinsic_get_buffer_size:
4055 result = visit_get_buffer_size(ctx, instr);
4056 break;
4057 case nir_intrinsic_load_var:
4058 result = visit_load_var(ctx, instr);
4059 break;
4060 case nir_intrinsic_store_var:
4061 visit_store_var(ctx, instr);
4062 break;
4063 case nir_intrinsic_image_load:
4064 result = visit_image_load(ctx, instr);
4065 break;
4066 case nir_intrinsic_image_store:
4067 visit_image_store(ctx, instr);
4068 break;
4069 case nir_intrinsic_image_atomic_add:
4070 case nir_intrinsic_image_atomic_min:
4071 case nir_intrinsic_image_atomic_max:
4072 case nir_intrinsic_image_atomic_and:
4073 case nir_intrinsic_image_atomic_or:
4074 case nir_intrinsic_image_atomic_xor:
4075 case nir_intrinsic_image_atomic_exchange:
4076 case nir_intrinsic_image_atomic_comp_swap:
4077 result = visit_image_atomic(ctx, instr);
4078 break;
4079 case nir_intrinsic_image_size:
4080 result = visit_image_size(ctx, instr);
4081 break;
4082 case nir_intrinsic_discard:
4083 ac_build_intrinsic(&ctx->ac, "llvm.AMDGPU.kilp",
4084 LLVMVoidTypeInContext(ctx->ac.context),
4085 NULL, 0, AC_FUNC_ATTR_LEGACY);
4086 break;
4087 case nir_intrinsic_discard_if:
4088 emit_discard_if(ctx, instr);
4089 break;
4090 case nir_intrinsic_memory_barrier:
4091 emit_waitcnt(ctx->nctx, VM_CNT);
4092 break;
4093 case nir_intrinsic_barrier:
4094 emit_barrier(ctx->nctx);
4095 break;
4096 case nir_intrinsic_var_atomic_add:
4097 case nir_intrinsic_var_atomic_imin:
4098 case nir_intrinsic_var_atomic_umin:
4099 case nir_intrinsic_var_atomic_imax:
4100 case nir_intrinsic_var_atomic_umax:
4101 case nir_intrinsic_var_atomic_and:
4102 case nir_intrinsic_var_atomic_or:
4103 case nir_intrinsic_var_atomic_xor:
4104 case nir_intrinsic_var_atomic_exchange:
4105 case nir_intrinsic_var_atomic_comp_swap:
4106 result = visit_var_atomic(ctx->nctx, instr);
4107 break;
4108 case nir_intrinsic_interp_var_at_centroid:
4109 case nir_intrinsic_interp_var_at_sample:
4110 case nir_intrinsic_interp_var_at_offset:
4111 result = visit_interp(ctx->nctx, instr);
4112 break;
4113 case nir_intrinsic_emit_vertex:
4114 visit_emit_vertex(ctx->nctx, instr);
4115 break;
4116 case nir_intrinsic_end_primitive:
4117 visit_end_primitive(ctx->nctx, instr);
4118 break;
4119 case nir_intrinsic_load_tess_coord:
4120 result = visit_load_tess_coord(ctx->nctx, instr);
4121 break;
4122 case nir_intrinsic_load_patch_vertices_in:
4123 result = LLVMConstInt(ctx->ac.i32, ctx->nctx->options->key.tcs.input_vertices, false);
4124 break;
4125 default:
4126 fprintf(stderr, "Unknown intrinsic: ");
4127 nir_print_instr(&instr->instr, stderr);
4128 fprintf(stderr, "\n");
4129 break;
4130 }
4131 if (result) {
4132 _mesa_hash_table_insert(ctx->defs, &instr->dest.ssa, result);
4133 }
4134 }
4135
4136 static LLVMValueRef radv_load_ssbo(struct ac_shader_abi *abi,
4137 LLVMValueRef buffer, bool write)
4138 {
4139 struct nir_to_llvm_context *ctx = nir_to_llvm_context_from_abi(abi);
4140
4141 if (write && ctx->stage == MESA_SHADER_FRAGMENT)
4142 ctx->shader_info->fs.writes_memory = true;
4143
4144 return buffer;
4145 }
4146
4147 static LLVMValueRef radv_get_sampler_desc(struct ac_shader_abi *abi,
4148 unsigned descriptor_set,
4149 unsigned base_index,
4150 unsigned constant_index,
4151 LLVMValueRef index,
4152 enum ac_descriptor_type desc_type,
4153 bool image, bool write)
4154 {
4155 struct nir_to_llvm_context *ctx = nir_to_llvm_context_from_abi(abi);
4156 LLVMValueRef list = ctx->descriptor_sets[descriptor_set];
4157 struct radv_descriptor_set_layout *layout = ctx->options->layout->set[descriptor_set].layout;
4158 struct radv_descriptor_set_binding_layout *binding = layout->binding + base_index;
4159 unsigned offset = binding->offset;
4160 unsigned stride = binding->size;
4161 unsigned type_size;
4162 LLVMBuilderRef builder = ctx->builder;
4163 LLVMTypeRef type;
4164
4165 assert(base_index < layout->binding_count);
4166
4167 if (write && ctx->stage == MESA_SHADER_FRAGMENT)
4168 ctx->shader_info->fs.writes_memory = true;
4169
4170 switch (desc_type) {
4171 case AC_DESC_IMAGE:
4172 type = ctx->v8i32;
4173 type_size = 32;
4174 break;
4175 case AC_DESC_FMASK:
4176 type = ctx->v8i32;
4177 offset += 32;
4178 type_size = 32;
4179 break;
4180 case AC_DESC_SAMPLER:
4181 type = ctx->v4i32;
4182 if (binding->type == VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER)
4183 offset += 64;
4184
4185 type_size = 16;
4186 break;
4187 case AC_DESC_BUFFER:
4188 type = ctx->v4i32;
4189 type_size = 16;
4190 break;
4191 default:
4192 unreachable("invalid desc_type\n");
4193 }
4194
4195 offset += constant_index * stride;
4196
4197 if (desc_type == AC_DESC_SAMPLER && binding->immutable_samplers_offset &&
4198 (!index || binding->immutable_samplers_equal)) {
4199 if (binding->immutable_samplers_equal)
4200 constant_index = 0;
4201
4202 const uint32_t *samplers = radv_immutable_samplers(layout, binding);
4203
4204 LLVMValueRef constants[] = {
4205 LLVMConstInt(ctx->i32, samplers[constant_index * 4 + 0], 0),
4206 LLVMConstInt(ctx->i32, samplers[constant_index * 4 + 1], 0),
4207 LLVMConstInt(ctx->i32, samplers[constant_index * 4 + 2], 0),
4208 LLVMConstInt(ctx->i32, samplers[constant_index * 4 + 3], 0),
4209 };
4210 return ac_build_gather_values(&ctx->ac, constants, 4);
4211 }
4212
4213 assert(stride % type_size == 0);
4214
4215 if (!index)
4216 index = ctx->i32zero;
4217
4218 index = LLVMBuildMul(builder, index, LLVMConstInt(ctx->i32, stride / type_size, 0), "");
4219
4220 list = ac_build_gep0(&ctx->ac, list, LLVMConstInt(ctx->i32, offset, 0));
4221 list = LLVMBuildPointerCast(builder, list, const_array(type, 0), "");
4222
4223 return ac_build_indexed_load_const(&ctx->ac, list, index);
4224 }
4225
4226 static LLVMValueRef get_sampler_desc(struct ac_nir_context *ctx,
4227 const nir_deref_var *deref,
4228 enum ac_descriptor_type desc_type,
4229 bool image, bool write)
4230 {
4231 LLVMValueRef index = NULL;
4232 unsigned constant_index = 0;
4233 const nir_deref *tail = &deref->deref;
4234
4235 while (tail->child) {
4236 const nir_deref_array *child = nir_deref_as_array(tail->child);
4237 unsigned array_size = glsl_get_aoa_size(tail->child->type);
4238
4239 if (!array_size)
4240 array_size = 1;
4241
4242 assert(child->deref_array_type != nir_deref_array_type_wildcard);
4243
4244 if (child->deref_array_type == nir_deref_array_type_indirect) {
4245 LLVMValueRef indirect = get_src(ctx, child->indirect);
4246
4247 indirect = LLVMBuildMul(ctx->ac.builder, indirect,
4248 LLVMConstInt(ctx->ac.i32, array_size, false), "");
4249
4250 if (!index)
4251 index = indirect;
4252 else
4253 index = LLVMBuildAdd(ctx->ac.builder, index, indirect, "");
4254 }
4255
4256 constant_index += child->base_offset * array_size;
4257
4258 tail = &child->deref;
4259 }
4260
4261 return ctx->abi->load_sampler_desc(ctx->abi,
4262 deref->var->data.descriptor_set,
4263 deref->var->data.binding,
4264 constant_index, index,
4265 desc_type, image, write);
4266 }
4267
4268 static void set_tex_fetch_args(struct ac_llvm_context *ctx,
4269 struct ac_image_args *args,
4270 const nir_tex_instr *instr,
4271 nir_texop op,
4272 LLVMValueRef res_ptr, LLVMValueRef samp_ptr,
4273 LLVMValueRef *param, unsigned count,
4274 unsigned dmask)
4275 {
4276 unsigned is_rect = 0;
4277 bool da = instr->is_array || instr->sampler_dim == GLSL_SAMPLER_DIM_CUBE;
4278
4279 if (op == nir_texop_lod)
4280 da = false;
4281 /* Pad to power of two vector */
4282 while (count < util_next_power_of_two(count))
4283 param[count++] = LLVMGetUndef(ctx->i32);
4284
4285 if (count > 1)
4286 args->addr = ac_build_gather_values(ctx, param, count);
4287 else
4288 args->addr = param[0];
4289
4290 args->resource = res_ptr;
4291 args->sampler = samp_ptr;
4292
4293 if (instr->sampler_dim == GLSL_SAMPLER_DIM_BUF && op == nir_texop_txf) {
4294 args->addr = param[0];
4295 return;
4296 }
4297
4298 args->dmask = dmask;
4299 args->unorm = is_rect;
4300 args->da = da;
4301 }
4302
4303 /* Disable anisotropic filtering if BASE_LEVEL == LAST_LEVEL.
4304 *
4305 * SI-CI:
4306 * If BASE_LEVEL == LAST_LEVEL, the shader must disable anisotropic
4307 * filtering manually. The driver sets img7 to a mask clearing
4308 * MAX_ANISO_RATIO if BASE_LEVEL == LAST_LEVEL. The shader must do:
4309 * s_and_b32 samp0, samp0, img7
4310 *
4311 * VI:
4312 * The ANISO_OVERRIDE sampler field enables this fix in TA.
4313 */
4314 static LLVMValueRef sici_fix_sampler_aniso(struct ac_nir_context *ctx,
4315 LLVMValueRef res, LLVMValueRef samp)
4316 {
4317 LLVMBuilderRef builder = ctx->ac.builder;
4318 LLVMValueRef img7, samp0;
4319
4320 if (ctx->abi->chip_class >= VI)
4321 return samp;
4322
4323 img7 = LLVMBuildExtractElement(builder, res,
4324 LLVMConstInt(ctx->ac.i32, 7, 0), "");
4325 samp0 = LLVMBuildExtractElement(builder, samp,
4326 LLVMConstInt(ctx->ac.i32, 0, 0), "");
4327 samp0 = LLVMBuildAnd(builder, samp0, img7, "");
4328 return LLVMBuildInsertElement(builder, samp, samp0,
4329 LLVMConstInt(ctx->ac.i32, 0, 0), "");
4330 }
4331
4332 static void tex_fetch_ptrs(struct ac_nir_context *ctx,
4333 nir_tex_instr *instr,
4334 LLVMValueRef *res_ptr, LLVMValueRef *samp_ptr,
4335 LLVMValueRef *fmask_ptr)
4336 {
4337 if (instr->sampler_dim == GLSL_SAMPLER_DIM_BUF)
4338 *res_ptr = get_sampler_desc(ctx, instr->texture, AC_DESC_BUFFER, false, false);
4339 else
4340 *res_ptr = get_sampler_desc(ctx, instr->texture, AC_DESC_IMAGE, false, false);
4341 if (samp_ptr) {
4342 if (instr->sampler)
4343 *samp_ptr = get_sampler_desc(ctx, instr->sampler, AC_DESC_SAMPLER, false, false);
4344 else
4345 *samp_ptr = get_sampler_desc(ctx, instr->texture, AC_DESC_SAMPLER, false, false);
4346 if (instr->sampler_dim < GLSL_SAMPLER_DIM_RECT)
4347 *samp_ptr = sici_fix_sampler_aniso(ctx, *res_ptr, *samp_ptr);
4348 }
4349 if (fmask_ptr && !instr->sampler && (instr->op == nir_texop_txf_ms ||
4350 instr->op == nir_texop_samples_identical))
4351 *fmask_ptr = get_sampler_desc(ctx, instr->texture, AC_DESC_FMASK, false, false);
4352 }
4353
4354 static LLVMValueRef apply_round_slice(struct ac_llvm_context *ctx,
4355 LLVMValueRef coord)
4356 {
4357 coord = to_float(ctx, coord);
4358 coord = ac_build_intrinsic(ctx, "llvm.rint.f32", ctx->f32, &coord, 1, 0);
4359 coord = to_integer(ctx, coord);
4360 return coord;
4361 }
4362
4363 static void visit_tex(struct ac_nir_context *ctx, nir_tex_instr *instr)
4364 {
4365 LLVMValueRef result = NULL;
4366 struct ac_image_args args = { 0 };
4367 unsigned dmask = 0xf;
4368 LLVMValueRef address[16];
4369 LLVMValueRef coords[5];
4370 LLVMValueRef coord = NULL, lod = NULL, comparator = NULL;
4371 LLVMValueRef bias = NULL, offsets = NULL;
4372 LLVMValueRef res_ptr, samp_ptr, fmask_ptr = NULL, sample_index = NULL;
4373 LLVMValueRef ddx = NULL, ddy = NULL;
4374 LLVMValueRef derivs[6];
4375 unsigned chan, count = 0;
4376 unsigned const_src = 0, num_deriv_comp = 0;
4377 bool lod_is_zero = false;
4378
4379 tex_fetch_ptrs(ctx, instr, &res_ptr, &samp_ptr, &fmask_ptr);
4380
4381 for (unsigned i = 0; i < instr->num_srcs; i++) {
4382 switch (instr->src[i].src_type) {
4383 case nir_tex_src_coord:
4384 coord = get_src(ctx, instr->src[i].src);
4385 break;
4386 case nir_tex_src_projector:
4387 break;
4388 case nir_tex_src_comparator:
4389 comparator = get_src(ctx, instr->src[i].src);
4390 break;
4391 case nir_tex_src_offset:
4392 offsets = get_src(ctx, instr->src[i].src);
4393 const_src = i;
4394 break;
4395 case nir_tex_src_bias:
4396 bias = get_src(ctx, instr->src[i].src);
4397 break;
4398 case nir_tex_src_lod: {
4399 nir_const_value *val = nir_src_as_const_value(instr->src[i].src);
4400
4401 if (val && val->i32[0] == 0)
4402 lod_is_zero = true;
4403 lod = get_src(ctx, instr->src[i].src);
4404 break;
4405 }
4406 case nir_tex_src_ms_index:
4407 sample_index = get_src(ctx, instr->src[i].src);
4408 break;
4409 case nir_tex_src_ms_mcs:
4410 break;
4411 case nir_tex_src_ddx:
4412 ddx = get_src(ctx, instr->src[i].src);
4413 num_deriv_comp = instr->src[i].src.ssa->num_components;
4414 break;
4415 case nir_tex_src_ddy:
4416 ddy = get_src(ctx, instr->src[i].src);
4417 break;
4418 case nir_tex_src_texture_offset:
4419 case nir_tex_src_sampler_offset:
4420 case nir_tex_src_plane:
4421 default:
4422 break;
4423 }
4424 }
4425
4426 if (instr->op == nir_texop_txs && instr->sampler_dim == GLSL_SAMPLER_DIM_BUF) {
4427 result = get_buffer_size(ctx, res_ptr, true);
4428 goto write_result;
4429 }
4430
4431 if (instr->op == nir_texop_texture_samples) {
4432 LLVMValueRef res, samples, is_msaa;
4433 res = LLVMBuildBitCast(ctx->ac.builder, res_ptr, ctx->ac.v8i32, "");
4434 samples = LLVMBuildExtractElement(ctx->ac.builder, res,
4435 LLVMConstInt(ctx->ac.i32, 3, false), "");
4436 is_msaa = LLVMBuildLShr(ctx->ac.builder, samples,
4437 LLVMConstInt(ctx->ac.i32, 28, false), "");
4438 is_msaa = LLVMBuildAnd(ctx->ac.builder, is_msaa,
4439 LLVMConstInt(ctx->ac.i32, 0xe, false), "");
4440 is_msaa = LLVMBuildICmp(ctx->ac.builder, LLVMIntEQ, is_msaa,
4441 LLVMConstInt(ctx->ac.i32, 0xe, false), "");
4442
4443 samples = LLVMBuildLShr(ctx->ac.builder, samples,
4444 LLVMConstInt(ctx->ac.i32, 16, false), "");
4445 samples = LLVMBuildAnd(ctx->ac.builder, samples,
4446 LLVMConstInt(ctx->ac.i32, 0xf, false), "");
4447 samples = LLVMBuildShl(ctx->ac.builder, ctx->ac.i32_1,
4448 samples, "");
4449 samples = LLVMBuildSelect(ctx->ac.builder, is_msaa, samples,
4450 ctx->ac.i32_1, "");
4451 result = samples;
4452 goto write_result;
4453 }
4454
4455 if (coord)
4456 for (chan = 0; chan < instr->coord_components; chan++)
4457 coords[chan] = llvm_extract_elem(&ctx->ac, coord, chan);
4458
4459 if (offsets && instr->op != nir_texop_txf) {
4460 LLVMValueRef offset[3], pack;
4461 for (chan = 0; chan < 3; ++chan)
4462 offset[chan] = ctx->ac.i32_0;
4463
4464 args.offset = true;
4465 for (chan = 0; chan < get_llvm_num_components(offsets); chan++) {
4466 offset[chan] = llvm_extract_elem(&ctx->ac, offsets, chan);
4467 offset[chan] = LLVMBuildAnd(ctx->ac.builder, offset[chan],
4468 LLVMConstInt(ctx->ac.i32, 0x3f, false), "");
4469 if (chan)
4470 offset[chan] = LLVMBuildShl(ctx->ac.builder, offset[chan],
4471 LLVMConstInt(ctx->ac.i32, chan * 8, false), "");
4472 }
4473 pack = LLVMBuildOr(ctx->ac.builder, offset[0], offset[1], "");
4474 pack = LLVMBuildOr(ctx->ac.builder, pack, offset[2], "");
4475 address[count++] = pack;
4476
4477 }
4478 /* pack LOD bias value */
4479 if (instr->op == nir_texop_txb && bias) {
4480 address[count++] = bias;
4481 }
4482
4483 /* Pack depth comparison value */
4484 if (instr->is_shadow && comparator) {
4485 LLVMValueRef z = llvm_extract_elem(&ctx->ac, comparator, 0);
4486
4487 /* TC-compatible HTILE promotes Z16 and Z24 to Z32_FLOAT,
4488 * so the depth comparison value isn't clamped for Z16 and
4489 * Z24 anymore. Do it manually here.
4490 *
4491 * It's unnecessary if the original texture format was
4492 * Z32_FLOAT, but we don't know that here.
4493 */
4494 if (ctx->abi->chip_class == VI)
4495 z = ac_build_clamp(&ctx->ac, z);
4496
4497 address[count++] = z;
4498 }
4499
4500 /* pack derivatives */
4501 if (ddx || ddy) {
4502 switch (instr->sampler_dim) {
4503 case GLSL_SAMPLER_DIM_3D:
4504 case GLSL_SAMPLER_DIM_CUBE:
4505 num_deriv_comp = 3;
4506 break;
4507 case GLSL_SAMPLER_DIM_2D:
4508 default:
4509 num_deriv_comp = 2;
4510 break;
4511 case GLSL_SAMPLER_DIM_1D:
4512 num_deriv_comp = 1;
4513 break;
4514 }
4515
4516 for (unsigned i = 0; i < num_deriv_comp; i++) {
4517 derivs[i] = to_float(&ctx->ac, llvm_extract_elem(&ctx->ac, ddx, i));
4518 derivs[num_deriv_comp + i] = to_float(&ctx->ac, llvm_extract_elem(&ctx->ac, ddy, i));
4519 }
4520 }
4521
4522 if (instr->sampler_dim == GLSL_SAMPLER_DIM_CUBE && coord) {
4523 if (instr->is_array && instr->op != nir_texop_lod)
4524 coords[3] = apply_round_slice(&ctx->ac, coords[3]);
4525 for (chan = 0; chan < instr->coord_components; chan++)
4526 coords[chan] = to_float(&ctx->ac, coords[chan]);
4527 if (instr->coord_components == 3)
4528 coords[3] = LLVMGetUndef(ctx->ac.f32);
4529 ac_prepare_cube_coords(&ctx->ac,
4530 instr->op == nir_texop_txd, instr->is_array,
4531 coords, derivs);
4532 if (num_deriv_comp)
4533 num_deriv_comp--;
4534 }
4535
4536 if (ddx || ddy) {
4537 for (unsigned i = 0; i < num_deriv_comp * 2; i++)
4538 address[count++] = derivs[i];
4539 }
4540
4541 /* Pack texture coordinates */
4542 if (coord) {
4543 address[count++] = coords[0];
4544 if (instr->coord_components > 1) {
4545 if (instr->sampler_dim == GLSL_SAMPLER_DIM_1D && instr->is_array && instr->op != nir_texop_txf) {
4546 coords[1] = apply_round_slice(&ctx->ac, coords[1]);
4547 }
4548 address[count++] = coords[1];
4549 }
4550 if (instr->coord_components > 2) {
4551 /* This seems like a bit of a hack - but it passes Vulkan CTS with it */
4552 if (instr->sampler_dim != GLSL_SAMPLER_DIM_3D &&
4553 instr->sampler_dim != GLSL_SAMPLER_DIM_CUBE &&
4554 instr->op != nir_texop_txf) {
4555 coords[2] = apply_round_slice(&ctx->ac, coords[2]);
4556 }
4557 address[count++] = coords[2];
4558 }
4559 }
4560
4561 /* Pack LOD */
4562 if (lod && ((instr->op == nir_texop_txl && !lod_is_zero) ||
4563 instr->op == nir_texop_txf)) {
4564 address[count++] = lod;
4565 } else if (instr->op == nir_texop_txf_ms && sample_index) {
4566 address[count++] = sample_index;
4567 } else if(instr->op == nir_texop_txs) {
4568 count = 0;
4569 if (lod)
4570 address[count++] = lod;
4571 else
4572 address[count++] = ctx->ac.i32_0;
4573 }
4574
4575 for (chan = 0; chan < count; chan++) {
4576 address[chan] = LLVMBuildBitCast(ctx->ac.builder,
4577 address[chan], ctx->ac.i32, "");
4578 }
4579
4580 if (instr->op == nir_texop_samples_identical) {
4581 LLVMValueRef txf_address[4];
4582 struct ac_image_args txf_args = { 0 };
4583 unsigned txf_count = count;
4584 memcpy(txf_address, address, sizeof(txf_address));
4585
4586 if (!instr->is_array)
4587 txf_address[2] = ctx->ac.i32_0;
4588 txf_address[3] = ctx->ac.i32_0;
4589
4590 set_tex_fetch_args(&ctx->ac, &txf_args, instr, nir_texop_txf,
4591 fmask_ptr, NULL,
4592 txf_address, txf_count, 0xf);
4593
4594 result = build_tex_intrinsic(ctx, instr, false, &txf_args);
4595
4596 result = LLVMBuildExtractElement(ctx->ac.builder, result, ctx->ac.i32_0, "");
4597 result = emit_int_cmp(&ctx->ac, LLVMIntEQ, result, ctx->ac.i32_0);
4598 goto write_result;
4599 }
4600
4601 if (instr->sampler_dim == GLSL_SAMPLER_DIM_MS &&
4602 instr->op != nir_texop_txs) {
4603 unsigned sample_chan = instr->is_array ? 3 : 2;
4604 address[sample_chan] = adjust_sample_index_using_fmask(&ctx->ac,
4605 address[0],
4606 address[1],
4607 instr->is_array ? address[2] : NULL,
4608 address[sample_chan],
4609 fmask_ptr);
4610 }
4611
4612 if (offsets && instr->op == nir_texop_txf) {
4613 nir_const_value *const_offset =
4614 nir_src_as_const_value(instr->src[const_src].src);
4615 int num_offsets = instr->src[const_src].src.ssa->num_components;
4616 assert(const_offset);
4617 num_offsets = MIN2(num_offsets, instr->coord_components);
4618 if (num_offsets > 2)
4619 address[2] = LLVMBuildAdd(ctx->ac.builder,
4620 address[2], LLVMConstInt(ctx->ac.i32, const_offset->i32[2], false), "");
4621 if (num_offsets > 1)
4622 address[1] = LLVMBuildAdd(ctx->ac.builder,
4623 address[1], LLVMConstInt(ctx->ac.i32, const_offset->i32[1], false), "");
4624 address[0] = LLVMBuildAdd(ctx->ac.builder,
4625 address[0], LLVMConstInt(ctx->ac.i32, const_offset->i32[0], false), "");
4626
4627 }
4628
4629 /* TODO TG4 support */
4630 if (instr->op == nir_texop_tg4) {
4631 if (instr->is_shadow)
4632 dmask = 1;
4633 else
4634 dmask = 1 << instr->component;
4635 }
4636 set_tex_fetch_args(&ctx->ac, &args, instr, instr->op,
4637 res_ptr, samp_ptr, address, count, dmask);
4638
4639 result = build_tex_intrinsic(ctx, instr, lod_is_zero, &args);
4640
4641 if (instr->op == nir_texop_query_levels)
4642 result = LLVMBuildExtractElement(ctx->ac.builder, result, LLVMConstInt(ctx->ac.i32, 3, false), "");
4643 else if (instr->is_shadow && instr->is_new_style_shadow &&
4644 instr->op != nir_texop_txs && instr->op != nir_texop_lod &&
4645 instr->op != nir_texop_tg4)
4646 result = LLVMBuildExtractElement(ctx->ac.builder, result, ctx->ac.i32_0, "");
4647 else if (instr->op == nir_texop_txs &&
4648 instr->sampler_dim == GLSL_SAMPLER_DIM_CUBE &&
4649 instr->is_array) {
4650 LLVMValueRef two = LLVMConstInt(ctx->ac.i32, 2, false);
4651 LLVMValueRef six = LLVMConstInt(ctx->ac.i32, 6, false);
4652 LLVMValueRef z = LLVMBuildExtractElement(ctx->ac.builder, result, two, "");
4653 z = LLVMBuildSDiv(ctx->ac.builder, z, six, "");
4654 result = LLVMBuildInsertElement(ctx->ac.builder, result, z, two, "");
4655 } else if (instr->dest.ssa.num_components != 4)
4656 result = trim_vector(&ctx->ac, result, instr->dest.ssa.num_components);
4657
4658 write_result:
4659 if (result) {
4660 assert(instr->dest.is_ssa);
4661 result = to_integer(&ctx->ac, result);
4662 _mesa_hash_table_insert(ctx->defs, &instr->dest.ssa, result);
4663 }
4664 }
4665
4666
4667 static void visit_phi(struct ac_nir_context *ctx, nir_phi_instr *instr)
4668 {
4669 LLVMTypeRef type = get_def_type(ctx, &instr->dest.ssa);
4670 LLVMValueRef result = LLVMBuildPhi(ctx->ac.builder, type, "");
4671
4672 _mesa_hash_table_insert(ctx->defs, &instr->dest.ssa, result);
4673 _mesa_hash_table_insert(ctx->phis, instr, result);
4674 }
4675
4676 static void visit_post_phi(struct ac_nir_context *ctx,
4677 nir_phi_instr *instr,
4678 LLVMValueRef llvm_phi)
4679 {
4680 nir_foreach_phi_src(src, instr) {
4681 LLVMBasicBlockRef block = get_block(ctx, src->pred);
4682 LLVMValueRef llvm_src = get_src(ctx, src->src);
4683
4684 LLVMAddIncoming(llvm_phi, &llvm_src, &block, 1);
4685 }
4686 }
4687
4688 static void phi_post_pass(struct ac_nir_context *ctx)
4689 {
4690 struct hash_entry *entry;
4691 hash_table_foreach(ctx->phis, entry) {
4692 visit_post_phi(ctx, (nir_phi_instr*)entry->key,
4693 (LLVMValueRef)entry->data);
4694 }
4695 }
4696
4697
4698 static void visit_ssa_undef(struct ac_nir_context *ctx,
4699 const nir_ssa_undef_instr *instr)
4700 {
4701 unsigned num_components = instr->def.num_components;
4702 LLVMValueRef undef;
4703
4704 if (num_components == 1)
4705 undef = LLVMGetUndef(ctx->ac.i32);
4706 else {
4707 undef = LLVMGetUndef(LLVMVectorType(ctx->ac.i32, num_components));
4708 }
4709 _mesa_hash_table_insert(ctx->defs, &instr->def, undef);
4710 }
4711
4712 static void visit_jump(struct ac_nir_context *ctx,
4713 const nir_jump_instr *instr)
4714 {
4715 switch (instr->type) {
4716 case nir_jump_break:
4717 LLVMBuildBr(ctx->ac.builder, ctx->break_block);
4718 LLVMClearInsertionPosition(ctx->ac.builder);
4719 break;
4720 case nir_jump_continue:
4721 LLVMBuildBr(ctx->ac.builder, ctx->continue_block);
4722 LLVMClearInsertionPosition(ctx->ac.builder);
4723 break;
4724 default:
4725 fprintf(stderr, "Unknown NIR jump instr: ");
4726 nir_print_instr(&instr->instr, stderr);
4727 fprintf(stderr, "\n");
4728 abort();
4729 }
4730 }
4731
4732 static void visit_cf_list(struct ac_nir_context *ctx,
4733 struct exec_list *list);
4734
4735 static void visit_block(struct ac_nir_context *ctx, nir_block *block)
4736 {
4737 LLVMBasicBlockRef llvm_block = LLVMGetInsertBlock(ctx->ac.builder);
4738 nir_foreach_instr(instr, block)
4739 {
4740 switch (instr->type) {
4741 case nir_instr_type_alu:
4742 visit_alu(ctx, nir_instr_as_alu(instr));
4743 break;
4744 case nir_instr_type_load_const:
4745 visit_load_const(ctx, nir_instr_as_load_const(instr));
4746 break;
4747 case nir_instr_type_intrinsic:
4748 visit_intrinsic(ctx, nir_instr_as_intrinsic(instr));
4749 break;
4750 case nir_instr_type_tex:
4751 visit_tex(ctx, nir_instr_as_tex(instr));
4752 break;
4753 case nir_instr_type_phi:
4754 visit_phi(ctx, nir_instr_as_phi(instr));
4755 break;
4756 case nir_instr_type_ssa_undef:
4757 visit_ssa_undef(ctx, nir_instr_as_ssa_undef(instr));
4758 break;
4759 case nir_instr_type_jump:
4760 visit_jump(ctx, nir_instr_as_jump(instr));
4761 break;
4762 default:
4763 fprintf(stderr, "Unknown NIR instr type: ");
4764 nir_print_instr(instr, stderr);
4765 fprintf(stderr, "\n");
4766 abort();
4767 }
4768 }
4769
4770 _mesa_hash_table_insert(ctx->defs, block, llvm_block);
4771 }
4772
4773 static void visit_if(struct ac_nir_context *ctx, nir_if *if_stmt)
4774 {
4775 LLVMValueRef value = get_src(ctx, if_stmt->condition);
4776
4777 LLVMValueRef fn = LLVMGetBasicBlockParent(LLVMGetInsertBlock(ctx->ac.builder));
4778 LLVMBasicBlockRef merge_block =
4779 LLVMAppendBasicBlockInContext(ctx->ac.context, fn, "");
4780 LLVMBasicBlockRef if_block =
4781 LLVMAppendBasicBlockInContext(ctx->ac.context, fn, "");
4782 LLVMBasicBlockRef else_block = merge_block;
4783 if (!exec_list_is_empty(&if_stmt->else_list))
4784 else_block = LLVMAppendBasicBlockInContext(
4785 ctx->ac.context, fn, "");
4786
4787 LLVMValueRef cond = LLVMBuildICmp(ctx->ac.builder, LLVMIntNE, value,
4788 LLVMConstInt(ctx->ac.i32, 0, false), "");
4789 LLVMBuildCondBr(ctx->ac.builder, cond, if_block, else_block);
4790
4791 LLVMPositionBuilderAtEnd(ctx->ac.builder, if_block);
4792 visit_cf_list(ctx, &if_stmt->then_list);
4793 if (LLVMGetInsertBlock(ctx->ac.builder))
4794 LLVMBuildBr(ctx->ac.builder, merge_block);
4795
4796 if (!exec_list_is_empty(&if_stmt->else_list)) {
4797 LLVMPositionBuilderAtEnd(ctx->ac.builder, else_block);
4798 visit_cf_list(ctx, &if_stmt->else_list);
4799 if (LLVMGetInsertBlock(ctx->ac.builder))
4800 LLVMBuildBr(ctx->ac.builder, merge_block);
4801 }
4802
4803 LLVMPositionBuilderAtEnd(ctx->ac.builder, merge_block);
4804 }
4805
4806 static void visit_loop(struct ac_nir_context *ctx, nir_loop *loop)
4807 {
4808 LLVMValueRef fn = LLVMGetBasicBlockParent(LLVMGetInsertBlock(ctx->ac.builder));
4809 LLVMBasicBlockRef continue_parent = ctx->continue_block;
4810 LLVMBasicBlockRef break_parent = ctx->break_block;
4811
4812 ctx->continue_block =
4813 LLVMAppendBasicBlockInContext(ctx->ac.context, fn, "");
4814 ctx->break_block =
4815 LLVMAppendBasicBlockInContext(ctx->ac.context, fn, "");
4816
4817 LLVMBuildBr(ctx->ac.builder, ctx->continue_block);
4818 LLVMPositionBuilderAtEnd(ctx->ac.builder, ctx->continue_block);
4819 visit_cf_list(ctx, &loop->body);
4820
4821 if (LLVMGetInsertBlock(ctx->ac.builder))
4822 LLVMBuildBr(ctx->ac.builder, ctx->continue_block);
4823 LLVMPositionBuilderAtEnd(ctx->ac.builder, ctx->break_block);
4824
4825 ctx->continue_block = continue_parent;
4826 ctx->break_block = break_parent;
4827 }
4828
4829 static void visit_cf_list(struct ac_nir_context *ctx,
4830 struct exec_list *list)
4831 {
4832 foreach_list_typed(nir_cf_node, node, node, list)
4833 {
4834 switch (node->type) {
4835 case nir_cf_node_block:
4836 visit_block(ctx, nir_cf_node_as_block(node));
4837 break;
4838
4839 case nir_cf_node_if:
4840 visit_if(ctx, nir_cf_node_as_if(node));
4841 break;
4842
4843 case nir_cf_node_loop:
4844 visit_loop(ctx, nir_cf_node_as_loop(node));
4845 break;
4846
4847 default:
4848 assert(0);
4849 }
4850 }
4851 }
4852
4853 static void
4854 handle_vs_input_decl(struct nir_to_llvm_context *ctx,
4855 struct nir_variable *variable)
4856 {
4857 LLVMValueRef t_list_ptr = ctx->vertex_buffers;
4858 LLVMValueRef t_offset;
4859 LLVMValueRef t_list;
4860 LLVMValueRef input;
4861 LLVMValueRef buffer_index;
4862 int index = variable->data.location - VERT_ATTRIB_GENERIC0;
4863 int idx = variable->data.location;
4864 unsigned attrib_count = glsl_count_attribute_slots(variable->type, true);
4865
4866 variable->data.driver_location = idx * 4;
4867
4868 if (ctx->options->key.vs.instance_rate_inputs & (1u << index)) {
4869 buffer_index = LLVMBuildAdd(ctx->builder, ctx->abi.instance_id,
4870 ctx->abi.start_instance, "");
4871 ctx->shader_info->vs.vgpr_comp_cnt = MAX2(3,
4872 ctx->shader_info->vs.vgpr_comp_cnt);
4873 } else
4874 buffer_index = LLVMBuildAdd(ctx->builder, ctx->abi.vertex_id,
4875 ctx->abi.base_vertex, "");
4876
4877 for (unsigned i = 0; i < attrib_count; ++i, ++idx) {
4878 t_offset = LLVMConstInt(ctx->i32, index + i, false);
4879
4880 t_list = ac_build_indexed_load_const(&ctx->ac, t_list_ptr, t_offset);
4881
4882 input = ac_build_buffer_load_format(&ctx->ac, t_list,
4883 buffer_index,
4884 LLVMConstInt(ctx->i32, 0, false),
4885 true);
4886
4887 for (unsigned chan = 0; chan < 4; chan++) {
4888 LLVMValueRef llvm_chan = LLVMConstInt(ctx->i32, chan, false);
4889 ctx->inputs[radeon_llvm_reg_index_soa(idx, chan)] =
4890 to_integer(&ctx->ac, LLVMBuildExtractElement(ctx->builder,
4891 input, llvm_chan, ""));
4892 }
4893 }
4894 }
4895
4896 static void interp_fs_input(struct nir_to_llvm_context *ctx,
4897 unsigned attr,
4898 LLVMValueRef interp_param,
4899 LLVMValueRef prim_mask,
4900 LLVMValueRef result[4])
4901 {
4902 LLVMValueRef attr_number;
4903 unsigned chan;
4904 LLVMValueRef i, j;
4905 bool interp = interp_param != NULL;
4906
4907 attr_number = LLVMConstInt(ctx->i32, attr, false);
4908
4909 /* fs.constant returns the param from the middle vertex, so it's not
4910 * really useful for flat shading. It's meant to be used for custom
4911 * interpolation (but the intrinsic can't fetch from the other two
4912 * vertices).
4913 *
4914 * Luckily, it doesn't matter, because we rely on the FLAT_SHADE state
4915 * to do the right thing. The only reason we use fs.constant is that
4916 * fs.interp cannot be used on integers, because they can be equal
4917 * to NaN.
4918 */
4919 if (interp) {
4920 interp_param = LLVMBuildBitCast(ctx->builder, interp_param,
4921 LLVMVectorType(ctx->f32, 2), "");
4922
4923 i = LLVMBuildExtractElement(ctx->builder, interp_param,
4924 ctx->i32zero, "");
4925 j = LLVMBuildExtractElement(ctx->builder, interp_param,
4926 ctx->i32one, "");
4927 }
4928
4929 for (chan = 0; chan < 4; chan++) {
4930 LLVMValueRef llvm_chan = LLVMConstInt(ctx->i32, chan, false);
4931
4932 if (interp) {
4933 result[chan] = ac_build_fs_interp(&ctx->ac,
4934 llvm_chan,
4935 attr_number,
4936 prim_mask, i, j);
4937 } else {
4938 result[chan] = ac_build_fs_interp_mov(&ctx->ac,
4939 LLVMConstInt(ctx->i32, 2, false),
4940 llvm_chan,
4941 attr_number,
4942 prim_mask);
4943 }
4944 }
4945 }
4946
4947 static void
4948 handle_fs_input_decl(struct nir_to_llvm_context *ctx,
4949 struct nir_variable *variable)
4950 {
4951 int idx = variable->data.location;
4952 unsigned attrib_count = glsl_count_attribute_slots(variable->type, false);
4953 LLVMValueRef interp;
4954
4955 variable->data.driver_location = idx * 4;
4956 ctx->input_mask |= ((1ull << attrib_count) - 1) << variable->data.location;
4957
4958 if (glsl_get_base_type(glsl_without_array(variable->type)) == GLSL_TYPE_FLOAT) {
4959 unsigned interp_type;
4960 if (variable->data.sample) {
4961 interp_type = INTERP_SAMPLE;
4962 ctx->shader_info->info.ps.force_persample = true;
4963 } else if (variable->data.centroid)
4964 interp_type = INTERP_CENTROID;
4965 else
4966 interp_type = INTERP_CENTER;
4967
4968 interp = lookup_interp_param(ctx, variable->data.interpolation, interp_type);
4969 } else
4970 interp = NULL;
4971
4972 for (unsigned i = 0; i < attrib_count; ++i)
4973 ctx->inputs[radeon_llvm_reg_index_soa(idx + i, 0)] = interp;
4974
4975 }
4976
4977 static void
4978 handle_shader_input_decl(struct nir_to_llvm_context *ctx,
4979 struct nir_variable *variable)
4980 {
4981 switch (ctx->stage) {
4982 case MESA_SHADER_VERTEX:
4983 handle_vs_input_decl(ctx, variable);
4984 break;
4985 case MESA_SHADER_FRAGMENT:
4986 handle_fs_input_decl(ctx, variable);
4987 break;
4988 default:
4989 break;
4990 }
4991
4992 }
4993
4994 static void
4995 handle_fs_inputs_pre(struct nir_to_llvm_context *ctx,
4996 struct nir_shader *nir)
4997 {
4998 unsigned index = 0;
4999 for (unsigned i = 0; i < RADEON_LLVM_MAX_INPUTS; ++i) {
5000 LLVMValueRef interp_param;
5001 LLVMValueRef *inputs = ctx->inputs +radeon_llvm_reg_index_soa(i, 0);
5002
5003 if (!(ctx->input_mask & (1ull << i)))
5004 continue;
5005
5006 if (i >= VARYING_SLOT_VAR0 || i == VARYING_SLOT_PNTC ||
5007 i == VARYING_SLOT_PRIMITIVE_ID || i == VARYING_SLOT_LAYER) {
5008 interp_param = *inputs;
5009 interp_fs_input(ctx, index, interp_param, ctx->prim_mask,
5010 inputs);
5011
5012 if (!interp_param)
5013 ctx->shader_info->fs.flat_shaded_mask |= 1u << index;
5014 ++index;
5015 } else if (i == VARYING_SLOT_POS) {
5016 for(int i = 0; i < 3; ++i)
5017 inputs[i] = ctx->abi.frag_pos[i];
5018
5019 inputs[3] = ac_build_fdiv(&ctx->ac, ctx->f32one,
5020 ctx->abi.frag_pos[3]);
5021 }
5022 }
5023 ctx->shader_info->fs.num_interp = index;
5024 if (ctx->input_mask & (1 << VARYING_SLOT_PNTC))
5025 ctx->shader_info->fs.has_pcoord = true;
5026 if (ctx->input_mask & (1 << VARYING_SLOT_PRIMITIVE_ID))
5027 ctx->shader_info->fs.prim_id_input = true;
5028 if (ctx->input_mask & (1 << VARYING_SLOT_LAYER))
5029 ctx->shader_info->fs.layer_input = true;
5030 ctx->shader_info->fs.input_mask = ctx->input_mask >> VARYING_SLOT_VAR0;
5031 }
5032
5033 static LLVMValueRef
5034 ac_build_alloca(struct ac_llvm_context *ac,
5035 LLVMTypeRef type,
5036 const char *name)
5037 {
5038 LLVMBuilderRef builder = ac->builder;
5039 LLVMBasicBlockRef current_block = LLVMGetInsertBlock(builder);
5040 LLVMValueRef function = LLVMGetBasicBlockParent(current_block);
5041 LLVMBasicBlockRef first_block = LLVMGetEntryBasicBlock(function);
5042 LLVMValueRef first_instr = LLVMGetFirstInstruction(first_block);
5043 LLVMBuilderRef first_builder = LLVMCreateBuilderInContext(ac->context);
5044 LLVMValueRef res;
5045
5046 if (first_instr) {
5047 LLVMPositionBuilderBefore(first_builder, first_instr);
5048 } else {
5049 LLVMPositionBuilderAtEnd(first_builder, first_block);
5050 }
5051
5052 res = LLVMBuildAlloca(first_builder, type, name);
5053 LLVMBuildStore(builder, LLVMConstNull(type), res);
5054
5055 LLVMDisposeBuilder(first_builder);
5056
5057 return res;
5058 }
5059
5060 static LLVMValueRef si_build_alloca_undef(struct ac_llvm_context *ac,
5061 LLVMTypeRef type,
5062 const char *name)
5063 {
5064 LLVMValueRef ptr = ac_build_alloca(ac, type, name);
5065 LLVMBuildStore(ac->builder, LLVMGetUndef(type), ptr);
5066 return ptr;
5067 }
5068
5069 static void
5070 scan_shader_output_decl(struct nir_to_llvm_context *ctx,
5071 struct nir_variable *variable)
5072 {
5073 int idx = variable->data.location + variable->data.index;
5074 unsigned attrib_count = glsl_count_attribute_slots(variable->type, false);
5075 uint64_t mask_attribs;
5076
5077 variable->data.driver_location = idx * 4;
5078
5079 /* tess ctrl has it's own load/store paths for outputs */
5080 if (ctx->stage == MESA_SHADER_TESS_CTRL)
5081 return;
5082
5083 mask_attribs = ((1ull << attrib_count) - 1) << idx;
5084 if (ctx->stage == MESA_SHADER_VERTEX ||
5085 ctx->stage == MESA_SHADER_TESS_EVAL ||
5086 ctx->stage == MESA_SHADER_GEOMETRY) {
5087 if (idx == VARYING_SLOT_CLIP_DIST0) {
5088 int length = ctx->num_output_clips + ctx->num_output_culls;
5089 if (ctx->stage == MESA_SHADER_VERTEX) {
5090 ctx->shader_info->vs.outinfo.clip_dist_mask = (1 << ctx->num_output_clips) - 1;
5091 ctx->shader_info->vs.outinfo.cull_dist_mask = (1 << ctx->num_output_culls) - 1;
5092 }
5093 if (ctx->stage == MESA_SHADER_TESS_EVAL) {
5094 ctx->shader_info->tes.outinfo.clip_dist_mask = (1 << ctx->num_output_clips) - 1;
5095 ctx->shader_info->tes.outinfo.cull_dist_mask = (1 << ctx->num_output_culls) - 1;
5096 }
5097
5098 if (length > 4)
5099 attrib_count = 2;
5100 else
5101 attrib_count = 1;
5102 mask_attribs = 1ull << idx;
5103 }
5104 }
5105
5106 ctx->output_mask |= mask_attribs;
5107 }
5108
5109 static void
5110 handle_shader_output_decl(struct ac_nir_context *ctx,
5111 struct nir_shader *nir,
5112 struct nir_variable *variable)
5113 {
5114 unsigned output_loc = variable->data.driver_location / 4;
5115 unsigned attrib_count = glsl_count_attribute_slots(variable->type, false);
5116
5117 /* tess ctrl has it's own load/store paths for outputs */
5118 if (ctx->stage == MESA_SHADER_TESS_CTRL)
5119 return;
5120
5121 if (ctx->stage == MESA_SHADER_VERTEX ||
5122 ctx->stage == MESA_SHADER_TESS_EVAL ||
5123 ctx->stage == MESA_SHADER_GEOMETRY) {
5124 int idx = variable->data.location + variable->data.index;
5125 if (idx == VARYING_SLOT_CLIP_DIST0) {
5126 int length = nir->info.clip_distance_array_size +
5127 nir->info.cull_distance_array_size;
5128
5129 if (length > 4)
5130 attrib_count = 2;
5131 else
5132 attrib_count = 1;
5133 }
5134 }
5135
5136 for (unsigned i = 0; i < attrib_count; ++i) {
5137 for (unsigned chan = 0; chan < 4; chan++) {
5138 ctx->outputs[radeon_llvm_reg_index_soa(output_loc + i, chan)] =
5139 si_build_alloca_undef(&ctx->ac, ctx->ac.f32, "");
5140 }
5141 }
5142 }
5143
5144 static LLVMTypeRef
5145 glsl_base_to_llvm_type(struct nir_to_llvm_context *ctx,
5146 enum glsl_base_type type)
5147 {
5148 switch (type) {
5149 case GLSL_TYPE_INT:
5150 case GLSL_TYPE_UINT:
5151 case GLSL_TYPE_BOOL:
5152 case GLSL_TYPE_SUBROUTINE:
5153 return ctx->i32;
5154 case GLSL_TYPE_FLOAT: /* TODO handle mediump */
5155 return ctx->f32;
5156 case GLSL_TYPE_INT64:
5157 case GLSL_TYPE_UINT64:
5158 return ctx->i64;
5159 case GLSL_TYPE_DOUBLE:
5160 return ctx->f64;
5161 default:
5162 unreachable("unknown GLSL type");
5163 }
5164 }
5165
5166 static LLVMTypeRef
5167 glsl_to_llvm_type(struct nir_to_llvm_context *ctx,
5168 const struct glsl_type *type)
5169 {
5170 if (glsl_type_is_scalar(type)) {
5171 return glsl_base_to_llvm_type(ctx, glsl_get_base_type(type));
5172 }
5173
5174 if (glsl_type_is_vector(type)) {
5175 return LLVMVectorType(
5176 glsl_base_to_llvm_type(ctx, glsl_get_base_type(type)),
5177 glsl_get_vector_elements(type));
5178 }
5179
5180 if (glsl_type_is_matrix(type)) {
5181 return LLVMArrayType(
5182 glsl_to_llvm_type(ctx, glsl_get_column_type(type)),
5183 glsl_get_matrix_columns(type));
5184 }
5185
5186 if (glsl_type_is_array(type)) {
5187 return LLVMArrayType(
5188 glsl_to_llvm_type(ctx, glsl_get_array_element(type)),
5189 glsl_get_length(type));
5190 }
5191
5192 assert(glsl_type_is_struct(type));
5193
5194 LLVMTypeRef member_types[glsl_get_length(type)];
5195
5196 for (unsigned i = 0; i < glsl_get_length(type); i++) {
5197 member_types[i] =
5198 glsl_to_llvm_type(ctx,
5199 glsl_get_struct_field(type, i));
5200 }
5201
5202 return LLVMStructTypeInContext(ctx->context, member_types,
5203 glsl_get_length(type), false);
5204 }
5205
5206 static void
5207 setup_locals(struct ac_nir_context *ctx,
5208 struct nir_function *func)
5209 {
5210 int i, j;
5211 ctx->num_locals = 0;
5212 nir_foreach_variable(variable, &func->impl->locals) {
5213 unsigned attrib_count = glsl_count_attribute_slots(variable->type, false);
5214 variable->data.driver_location = ctx->num_locals * 4;
5215 ctx->num_locals += attrib_count;
5216 }
5217 ctx->locals = malloc(4 * ctx->num_locals * sizeof(LLVMValueRef));
5218 if (!ctx->locals)
5219 return;
5220
5221 for (i = 0; i < ctx->num_locals; i++) {
5222 for (j = 0; j < 4; j++) {
5223 ctx->locals[i * 4 + j] =
5224 si_build_alloca_undef(&ctx->ac, ctx->ac.f32, "temp");
5225 }
5226 }
5227 }
5228
5229 static void
5230 setup_shared(struct ac_nir_context *ctx,
5231 struct nir_shader *nir)
5232 {
5233 nir_foreach_variable(variable, &nir->shared) {
5234 LLVMValueRef shared =
5235 LLVMAddGlobalInAddressSpace(
5236 ctx->ac.module, glsl_to_llvm_type(ctx->nctx, variable->type),
5237 variable->name ? variable->name : "",
5238 LOCAL_ADDR_SPACE);
5239 _mesa_hash_table_insert(ctx->vars, variable, shared);
5240 }
5241 }
5242
5243 static LLVMValueRef
5244 emit_float_saturate(struct ac_llvm_context *ctx, LLVMValueRef v, float lo, float hi)
5245 {
5246 v = to_float(ctx, v);
5247 v = emit_intrin_2f_param(ctx, "llvm.maxnum.f32", ctx->f32, v, LLVMConstReal(ctx->f32, lo));
5248 return emit_intrin_2f_param(ctx, "llvm.minnum.f32", ctx->f32, v, LLVMConstReal(ctx->f32, hi));
5249 }
5250
5251
5252 static LLVMValueRef emit_pack_int16(struct nir_to_llvm_context *ctx,
5253 LLVMValueRef src0, LLVMValueRef src1)
5254 {
5255 LLVMValueRef const16 = LLVMConstInt(ctx->i32, 16, false);
5256 LLVMValueRef comp[2];
5257
5258 comp[0] = LLVMBuildAnd(ctx->builder, src0, LLVMConstInt(ctx-> i32, 65535, 0), "");
5259 comp[1] = LLVMBuildAnd(ctx->builder, src1, LLVMConstInt(ctx-> i32, 65535, 0), "");
5260 comp[1] = LLVMBuildShl(ctx->builder, comp[1], const16, "");
5261 return LLVMBuildOr(ctx->builder, comp[0], comp[1], "");
5262 }
5263
5264 /* Initialize arguments for the shader export intrinsic */
5265 static void
5266 si_llvm_init_export_args(struct nir_to_llvm_context *ctx,
5267 LLVMValueRef *values,
5268 unsigned target,
5269 struct ac_export_args *args)
5270 {
5271 /* Default is 0xf. Adjusted below depending on the format. */
5272 args->enabled_channels = 0xf;
5273
5274 /* Specify whether the EXEC mask represents the valid mask */
5275 args->valid_mask = 0;
5276
5277 /* Specify whether this is the last export */
5278 args->done = 0;
5279
5280 /* Specify the target we are exporting */
5281 args->target = target;
5282
5283 args->compr = false;
5284 args->out[0] = LLVMGetUndef(ctx->f32);
5285 args->out[1] = LLVMGetUndef(ctx->f32);
5286 args->out[2] = LLVMGetUndef(ctx->f32);
5287 args->out[3] = LLVMGetUndef(ctx->f32);
5288
5289 if (!values)
5290 return;
5291
5292 if (ctx->stage == MESA_SHADER_FRAGMENT && target >= V_008DFC_SQ_EXP_MRT) {
5293 LLVMValueRef val[4];
5294 unsigned index = target - V_008DFC_SQ_EXP_MRT;
5295 unsigned col_format = (ctx->options->key.fs.col_format >> (4 * index)) & 0xf;
5296 bool is_int8 = (ctx->options->key.fs.is_int8 >> index) & 1;
5297
5298 switch(col_format) {
5299 case V_028714_SPI_SHADER_ZERO:
5300 args->enabled_channels = 0; /* writemask */
5301 args->target = V_008DFC_SQ_EXP_NULL;
5302 break;
5303
5304 case V_028714_SPI_SHADER_32_R:
5305 args->enabled_channels = 1;
5306 args->out[0] = values[0];
5307 break;
5308
5309 case V_028714_SPI_SHADER_32_GR:
5310 args->enabled_channels = 0x3;
5311 args->out[0] = values[0];
5312 args->out[1] = values[1];
5313 break;
5314
5315 case V_028714_SPI_SHADER_32_AR:
5316 args->enabled_channels = 0x9;
5317 args->out[0] = values[0];
5318 args->out[3] = values[3];
5319 break;
5320
5321 case V_028714_SPI_SHADER_FP16_ABGR:
5322 args->compr = 1;
5323
5324 for (unsigned chan = 0; chan < 2; chan++) {
5325 LLVMValueRef pack_args[2] = {
5326 values[2 * chan],
5327 values[2 * chan + 1]
5328 };
5329 LLVMValueRef packed;
5330
5331 packed = ac_build_cvt_pkrtz_f16(&ctx->ac, pack_args);
5332 args->out[chan] = packed;
5333 }
5334 break;
5335
5336 case V_028714_SPI_SHADER_UNORM16_ABGR:
5337 for (unsigned chan = 0; chan < 4; chan++) {
5338 val[chan] = ac_build_clamp(&ctx->ac, values[chan]);
5339 val[chan] = LLVMBuildFMul(ctx->builder, val[chan],
5340 LLVMConstReal(ctx->f32, 65535), "");
5341 val[chan] = LLVMBuildFAdd(ctx->builder, val[chan],
5342 LLVMConstReal(ctx->f32, 0.5), "");
5343 val[chan] = LLVMBuildFPToUI(ctx->builder, val[chan],
5344 ctx->i32, "");
5345 }
5346
5347 args->compr = 1;
5348 args->out[0] = emit_pack_int16(ctx, val[0], val[1]);
5349 args->out[1] = emit_pack_int16(ctx, val[2], val[3]);
5350 break;
5351
5352 case V_028714_SPI_SHADER_SNORM16_ABGR:
5353 for (unsigned chan = 0; chan < 4; chan++) {
5354 val[chan] = emit_float_saturate(&ctx->ac, values[chan], -1, 1);
5355 val[chan] = LLVMBuildFMul(ctx->builder, val[chan],
5356 LLVMConstReal(ctx->f32, 32767), "");
5357
5358 /* If positive, add 0.5, else add -0.5. */
5359 val[chan] = LLVMBuildFAdd(ctx->builder, val[chan],
5360 LLVMBuildSelect(ctx->builder,
5361 LLVMBuildFCmp(ctx->builder, LLVMRealOGE,
5362 val[chan], ctx->f32zero, ""),
5363 LLVMConstReal(ctx->f32, 0.5),
5364 LLVMConstReal(ctx->f32, -0.5), ""), "");
5365 val[chan] = LLVMBuildFPToSI(ctx->builder, val[chan], ctx->i32, "");
5366 }
5367
5368 args->compr = 1;
5369 args->out[0] = emit_pack_int16(ctx, val[0], val[1]);
5370 args->out[1] = emit_pack_int16(ctx, val[2], val[3]);
5371 break;
5372
5373 case V_028714_SPI_SHADER_UINT16_ABGR: {
5374 LLVMValueRef max = LLVMConstInt(ctx->i32, is_int8 ? 255 : 65535, 0);
5375
5376 for (unsigned chan = 0; chan < 4; chan++) {
5377 val[chan] = to_integer(&ctx->ac, values[chan]);
5378 val[chan] = emit_minmax_int(&ctx->ac, LLVMIntULT, val[chan], max);
5379 }
5380
5381 args->compr = 1;
5382 args->out[0] = emit_pack_int16(ctx, val[0], val[1]);
5383 args->out[1] = emit_pack_int16(ctx, val[2], val[3]);
5384 break;
5385 }
5386
5387 case V_028714_SPI_SHADER_SINT16_ABGR: {
5388 LLVMValueRef max = LLVMConstInt(ctx->i32, is_int8 ? 127 : 32767, 0);
5389 LLVMValueRef min = LLVMConstInt(ctx->i32, is_int8 ? -128 : -32768, 0);
5390
5391 /* Clamp. */
5392 for (unsigned chan = 0; chan < 4; chan++) {
5393 val[chan] = to_integer(&ctx->ac, values[chan]);
5394 val[chan] = emit_minmax_int(&ctx->ac, LLVMIntSLT, val[chan], max);
5395 val[chan] = emit_minmax_int(&ctx->ac, LLVMIntSGT, val[chan], min);
5396 }
5397
5398 args->compr = 1;
5399 args->out[0] = emit_pack_int16(ctx, val[0], val[1]);
5400 args->out[1] = emit_pack_int16(ctx, val[2], val[3]);
5401 break;
5402 }
5403
5404 default:
5405 case V_028714_SPI_SHADER_32_ABGR:
5406 memcpy(&args->out[0], values, sizeof(values[0]) * 4);
5407 break;
5408 }
5409 } else
5410 memcpy(&args->out[0], values, sizeof(values[0]) * 4);
5411
5412 for (unsigned i = 0; i < 4; ++i)
5413 args->out[i] = to_float(&ctx->ac, args->out[i]);
5414 }
5415
5416 static void
5417 handle_vs_outputs_post(struct nir_to_llvm_context *ctx,
5418 bool export_prim_id,
5419 struct ac_vs_output_info *outinfo)
5420 {
5421 uint32_t param_count = 0;
5422 unsigned target;
5423 unsigned pos_idx, num_pos_exports = 0;
5424 struct ac_export_args args, pos_args[4] = {};
5425 LLVMValueRef psize_value = NULL, layer_value = NULL, viewport_index_value = NULL;
5426 int i;
5427
5428 memset(outinfo->vs_output_param_offset, AC_EXP_PARAM_UNDEFINED,
5429 sizeof(outinfo->vs_output_param_offset));
5430
5431 if (ctx->output_mask & (1ull << VARYING_SLOT_CLIP_DIST0)) {
5432 LLVMValueRef slots[8];
5433 unsigned j;
5434
5435 if (outinfo->cull_dist_mask)
5436 outinfo->cull_dist_mask <<= ctx->num_output_clips;
5437
5438 i = VARYING_SLOT_CLIP_DIST0;
5439 for (j = 0; j < ctx->num_output_clips + ctx->num_output_culls; j++)
5440 slots[j] = to_float(&ctx->ac, LLVMBuildLoad(ctx->builder,
5441 ctx->nir->outputs[radeon_llvm_reg_index_soa(i, j)], ""));
5442
5443 for (i = ctx->num_output_clips + ctx->num_output_culls; i < 8; i++)
5444 slots[i] = LLVMGetUndef(ctx->f32);
5445
5446 if (ctx->num_output_clips + ctx->num_output_culls > 4) {
5447 target = V_008DFC_SQ_EXP_POS + 3;
5448 si_llvm_init_export_args(ctx, &slots[4], target, &args);
5449 memcpy(&pos_args[target - V_008DFC_SQ_EXP_POS],
5450 &args, sizeof(args));
5451 }
5452
5453 target = V_008DFC_SQ_EXP_POS + 2;
5454 si_llvm_init_export_args(ctx, &slots[0], target, &args);
5455 memcpy(&pos_args[target - V_008DFC_SQ_EXP_POS],
5456 &args, sizeof(args));
5457
5458 }
5459
5460 LLVMValueRef pos_values[4] = {ctx->f32zero, ctx->f32zero, ctx->f32zero, ctx->f32one};
5461 if (ctx->output_mask & (1ull << VARYING_SLOT_POS)) {
5462 for (unsigned j = 0; j < 4; j++)
5463 pos_values[j] = LLVMBuildLoad(ctx->builder,
5464 ctx->nir->outputs[radeon_llvm_reg_index_soa(VARYING_SLOT_POS, j)], "");
5465 }
5466 si_llvm_init_export_args(ctx, pos_values, V_008DFC_SQ_EXP_POS, &pos_args[0]);
5467
5468 if (ctx->output_mask & (1ull << VARYING_SLOT_PSIZ)) {
5469 outinfo->writes_pointsize = true;
5470 psize_value = LLVMBuildLoad(ctx->builder,
5471 ctx->nir->outputs[radeon_llvm_reg_index_soa(VARYING_SLOT_PSIZ, 0)], "");
5472 }
5473
5474 if (ctx->output_mask & (1ull << VARYING_SLOT_LAYER)) {
5475 outinfo->writes_layer = true;
5476 layer_value = LLVMBuildLoad(ctx->builder,
5477 ctx->nir->outputs[radeon_llvm_reg_index_soa(VARYING_SLOT_LAYER, 0)], "");
5478 }
5479
5480 if (ctx->output_mask & (1ull << VARYING_SLOT_VIEWPORT)) {
5481 outinfo->writes_viewport_index = true;
5482 viewport_index_value = LLVMBuildLoad(ctx->builder,
5483 ctx->nir->outputs[radeon_llvm_reg_index_soa(VARYING_SLOT_VIEWPORT, 0)], "");
5484 }
5485
5486 uint32_t mask = ((outinfo->writes_pointsize == true ? 1 : 0) |
5487 (outinfo->writes_layer == true ? 4 : 0) |
5488 (outinfo->writes_viewport_index == true ? 8 : 0));
5489 if (mask) {
5490 pos_args[1].enabled_channels = mask;
5491 pos_args[1].valid_mask = 0;
5492 pos_args[1].done = 0;
5493 pos_args[1].target = V_008DFC_SQ_EXP_POS + 1;
5494 pos_args[1].compr = 0;
5495 pos_args[1].out[0] = ctx->f32zero; /* X */
5496 pos_args[1].out[1] = ctx->f32zero; /* Y */
5497 pos_args[1].out[2] = ctx->f32zero; /* Z */
5498 pos_args[1].out[3] = ctx->f32zero; /* W */
5499
5500 if (outinfo->writes_pointsize == true)
5501 pos_args[1].out[0] = psize_value;
5502 if (outinfo->writes_layer == true)
5503 pos_args[1].out[2] = layer_value;
5504 if (outinfo->writes_viewport_index == true)
5505 pos_args[1].out[3] = viewport_index_value;
5506 }
5507 for (i = 0; i < 4; i++) {
5508 if (pos_args[i].out[0])
5509 num_pos_exports++;
5510 }
5511
5512 pos_idx = 0;
5513 for (i = 0; i < 4; i++) {
5514 if (!pos_args[i].out[0])
5515 continue;
5516
5517 /* Specify the target we are exporting */
5518 pos_args[i].target = V_008DFC_SQ_EXP_POS + pos_idx++;
5519 if (pos_idx == num_pos_exports)
5520 pos_args[i].done = 1;
5521 ac_build_export(&ctx->ac, &pos_args[i]);
5522 }
5523
5524 for (unsigned i = 0; i < RADEON_LLVM_MAX_OUTPUTS; ++i) {
5525 LLVMValueRef values[4];
5526 if (!(ctx->output_mask & (1ull << i)))
5527 continue;
5528
5529 for (unsigned j = 0; j < 4; j++)
5530 values[j] = to_float(&ctx->ac, LLVMBuildLoad(ctx->builder,
5531 ctx->nir->outputs[radeon_llvm_reg_index_soa(i, j)], ""));
5532
5533 if (i == VARYING_SLOT_LAYER) {
5534 target = V_008DFC_SQ_EXP_PARAM + param_count;
5535 outinfo->vs_output_param_offset[VARYING_SLOT_LAYER] = param_count;
5536 param_count++;
5537 } else if (i == VARYING_SLOT_PRIMITIVE_ID) {
5538 target = V_008DFC_SQ_EXP_PARAM + param_count;
5539 outinfo->vs_output_param_offset[VARYING_SLOT_PRIMITIVE_ID] = param_count;
5540 param_count++;
5541 } else if (i >= VARYING_SLOT_VAR0) {
5542 outinfo->export_mask |= 1u << (i - VARYING_SLOT_VAR0);
5543 target = V_008DFC_SQ_EXP_PARAM + param_count;
5544 outinfo->vs_output_param_offset[i] = param_count;
5545 param_count++;
5546 } else
5547 continue;
5548
5549 si_llvm_init_export_args(ctx, values, target, &args);
5550
5551 if (target >= V_008DFC_SQ_EXP_POS &&
5552 target <= (V_008DFC_SQ_EXP_POS + 3)) {
5553 memcpy(&pos_args[target - V_008DFC_SQ_EXP_POS],
5554 &args, sizeof(args));
5555 } else {
5556 ac_build_export(&ctx->ac, &args);
5557 }
5558 }
5559
5560 if (export_prim_id) {
5561 LLVMValueRef values[4];
5562 target = V_008DFC_SQ_EXP_PARAM + param_count;
5563 outinfo->vs_output_param_offset[VARYING_SLOT_PRIMITIVE_ID] = param_count;
5564 param_count++;
5565
5566 values[0] = ctx->vs_prim_id;
5567 ctx->shader_info->vs.vgpr_comp_cnt = MAX2(2,
5568 ctx->shader_info->vs.vgpr_comp_cnt);
5569 for (unsigned j = 1; j < 4; j++)
5570 values[j] = ctx->f32zero;
5571 si_llvm_init_export_args(ctx, values, target, &args);
5572 ac_build_export(&ctx->ac, &args);
5573 outinfo->export_prim_id = true;
5574 }
5575
5576 outinfo->pos_exports = num_pos_exports;
5577 outinfo->param_exports = param_count;
5578 }
5579
5580 static void
5581 handle_es_outputs_post(struct nir_to_llvm_context *ctx,
5582 struct ac_es_output_info *outinfo)
5583 {
5584 int j;
5585 uint64_t max_output_written = 0;
5586 for (unsigned i = 0; i < RADEON_LLVM_MAX_OUTPUTS; ++i) {
5587 LLVMValueRef *out_ptr = &ctx->nir->outputs[i * 4];
5588 int param_index;
5589 int length = 4;
5590
5591 if (!(ctx->output_mask & (1ull << i)))
5592 continue;
5593
5594 if (i == VARYING_SLOT_CLIP_DIST0)
5595 length = ctx->num_output_clips + ctx->num_output_culls;
5596
5597 param_index = shader_io_get_unique_index(i);
5598
5599 max_output_written = MAX2(param_index + (length > 4), max_output_written);
5600
5601 for (j = 0; j < length; j++) {
5602 LLVMValueRef out_val = LLVMBuildLoad(ctx->builder, out_ptr[j], "");
5603 out_val = LLVMBuildBitCast(ctx->builder, out_val, ctx->i32, "");
5604
5605 ac_build_buffer_store_dword(&ctx->ac,
5606 ctx->esgs_ring,
5607 out_val, 1,
5608 NULL, ctx->es2gs_offset,
5609 (4 * param_index + j) * 4,
5610 1, 1, true, true);
5611 }
5612 }
5613 outinfo->esgs_itemsize = (max_output_written + 1) * 16;
5614 }
5615
5616 static void
5617 handle_ls_outputs_post(struct nir_to_llvm_context *ctx)
5618 {
5619 LLVMValueRef vertex_id = ctx->rel_auto_id;
5620 LLVMValueRef vertex_dw_stride = unpack_param(ctx, ctx->ls_out_layout, 13, 8);
5621 LLVMValueRef base_dw_addr = LLVMBuildMul(ctx->builder, vertex_id,
5622 vertex_dw_stride, "");
5623
5624 for (unsigned i = 0; i < RADEON_LLVM_MAX_OUTPUTS; ++i) {
5625 LLVMValueRef *out_ptr = &ctx->nir->outputs[i * 4];
5626 int length = 4;
5627
5628 if (!(ctx->output_mask & (1ull << i)))
5629 continue;
5630
5631 if (i == VARYING_SLOT_CLIP_DIST0)
5632 length = ctx->num_output_clips + ctx->num_output_culls;
5633 int param = shader_io_get_unique_index(i);
5634 mark_tess_output(ctx, false, param);
5635 if (length > 4)
5636 mark_tess_output(ctx, false, param + 1);
5637 LLVMValueRef dw_addr = LLVMBuildAdd(ctx->builder, base_dw_addr,
5638 LLVMConstInt(ctx->i32, param * 4, false),
5639 "");
5640 for (unsigned j = 0; j < length; j++) {
5641 lds_store(ctx, dw_addr,
5642 LLVMBuildLoad(ctx->builder, out_ptr[j], ""));
5643 dw_addr = LLVMBuildAdd(ctx->builder, dw_addr, ctx->i32one, "");
5644 }
5645 }
5646 }
5647
5648 struct ac_build_if_state
5649 {
5650 struct nir_to_llvm_context *ctx;
5651 LLVMValueRef condition;
5652 LLVMBasicBlockRef entry_block;
5653 LLVMBasicBlockRef true_block;
5654 LLVMBasicBlockRef false_block;
5655 LLVMBasicBlockRef merge_block;
5656 };
5657
5658 static LLVMBasicBlockRef
5659 ac_build_insert_new_block(struct nir_to_llvm_context *ctx, const char *name)
5660 {
5661 LLVMBasicBlockRef current_block;
5662 LLVMBasicBlockRef next_block;
5663 LLVMBasicBlockRef new_block;
5664
5665 /* get current basic block */
5666 current_block = LLVMGetInsertBlock(ctx->builder);
5667
5668 /* chqeck if there's another block after this one */
5669 next_block = LLVMGetNextBasicBlock(current_block);
5670 if (next_block) {
5671 /* insert the new block before the next block */
5672 new_block = LLVMInsertBasicBlockInContext(ctx->context, next_block, name);
5673 }
5674 else {
5675 /* append new block after current block */
5676 LLVMValueRef function = LLVMGetBasicBlockParent(current_block);
5677 new_block = LLVMAppendBasicBlockInContext(ctx->context, function, name);
5678 }
5679 return new_block;
5680 }
5681
5682 static void
5683 ac_nir_build_if(struct ac_build_if_state *ifthen,
5684 struct nir_to_llvm_context *ctx,
5685 LLVMValueRef condition)
5686 {
5687 LLVMBasicBlockRef block = LLVMGetInsertBlock(ctx->builder);
5688
5689 memset(ifthen, 0, sizeof *ifthen);
5690 ifthen->ctx = ctx;
5691 ifthen->condition = condition;
5692 ifthen->entry_block = block;
5693
5694 /* create endif/merge basic block for the phi functions */
5695 ifthen->merge_block = ac_build_insert_new_block(ctx, "endif-block");
5696
5697 /* create/insert true_block before merge_block */
5698 ifthen->true_block =
5699 LLVMInsertBasicBlockInContext(ctx->context,
5700 ifthen->merge_block,
5701 "if-true-block");
5702
5703 /* successive code goes into the true block */
5704 LLVMPositionBuilderAtEnd(ctx->builder, ifthen->true_block);
5705 }
5706
5707 /**
5708 * End a conditional.
5709 */
5710 static void
5711 ac_nir_build_endif(struct ac_build_if_state *ifthen)
5712 {
5713 LLVMBuilderRef builder = ifthen->ctx->builder;
5714
5715 /* Insert branch to the merge block from current block */
5716 LLVMBuildBr(builder, ifthen->merge_block);
5717
5718 /*
5719 * Now patch in the various branch instructions.
5720 */
5721
5722 /* Insert the conditional branch instruction at the end of entry_block */
5723 LLVMPositionBuilderAtEnd(builder, ifthen->entry_block);
5724 if (ifthen->false_block) {
5725 /* we have an else clause */
5726 LLVMBuildCondBr(builder, ifthen->condition,
5727 ifthen->true_block, ifthen->false_block);
5728 }
5729 else {
5730 /* no else clause */
5731 LLVMBuildCondBr(builder, ifthen->condition,
5732 ifthen->true_block, ifthen->merge_block);
5733 }
5734
5735 /* Resume building code at end of the ifthen->merge_block */
5736 LLVMPositionBuilderAtEnd(builder, ifthen->merge_block);
5737 }
5738
5739 static void
5740 write_tess_factors(struct nir_to_llvm_context *ctx)
5741 {
5742 unsigned stride, outer_comps, inner_comps;
5743 struct ac_build_if_state if_ctx, inner_if_ctx;
5744 LLVMValueRef invocation_id = unpack_param(ctx, ctx->tcs_rel_ids, 8, 5);
5745 LLVMValueRef rel_patch_id = unpack_param(ctx, ctx->tcs_rel_ids, 0, 8);
5746 unsigned tess_inner_index, tess_outer_index;
5747 LLVMValueRef lds_base, lds_inner, lds_outer, byteoffset, buffer;
5748 LLVMValueRef out[6], vec0, vec1, tf_base, inner[4], outer[4];
5749 int i;
5750 emit_barrier(ctx);
5751
5752 switch (ctx->options->key.tcs.primitive_mode) {
5753 case GL_ISOLINES:
5754 stride = 2;
5755 outer_comps = 2;
5756 inner_comps = 0;
5757 break;
5758 case GL_TRIANGLES:
5759 stride = 4;
5760 outer_comps = 3;
5761 inner_comps = 1;
5762 break;
5763 case GL_QUADS:
5764 stride = 6;
5765 outer_comps = 4;
5766 inner_comps = 2;
5767 break;
5768 default:
5769 return;
5770 }
5771
5772 ac_nir_build_if(&if_ctx, ctx,
5773 LLVMBuildICmp(ctx->builder, LLVMIntEQ,
5774 invocation_id, ctx->i32zero, ""));
5775
5776 tess_inner_index = shader_io_get_unique_index(VARYING_SLOT_TESS_LEVEL_INNER);
5777 tess_outer_index = shader_io_get_unique_index(VARYING_SLOT_TESS_LEVEL_OUTER);
5778
5779 mark_tess_output(ctx, true, tess_inner_index);
5780 mark_tess_output(ctx, true, tess_outer_index);
5781 lds_base = get_tcs_out_current_patch_data_offset(ctx);
5782 lds_inner = LLVMBuildAdd(ctx->builder, lds_base,
5783 LLVMConstInt(ctx->i32, tess_inner_index * 4, false), "");
5784 lds_outer = LLVMBuildAdd(ctx->builder, lds_base,
5785 LLVMConstInt(ctx->i32, tess_outer_index * 4, false), "");
5786
5787 for (i = 0; i < 4; i++) {
5788 inner[i] = LLVMGetUndef(ctx->i32);
5789 outer[i] = LLVMGetUndef(ctx->i32);
5790 }
5791
5792 // LINES reverseal
5793 if (ctx->options->key.tcs.primitive_mode == GL_ISOLINES) {
5794 outer[0] = out[1] = lds_load(ctx, lds_outer);
5795 lds_outer = LLVMBuildAdd(ctx->builder, lds_outer,
5796 LLVMConstInt(ctx->i32, 1, false), "");
5797 outer[1] = out[0] = lds_load(ctx, lds_outer);
5798 } else {
5799 for (i = 0; i < outer_comps; i++) {
5800 outer[i] = out[i] =
5801 lds_load(ctx, lds_outer);
5802 lds_outer = LLVMBuildAdd(ctx->builder, lds_outer,
5803 LLVMConstInt(ctx->i32, 1, false), "");
5804 }
5805 for (i = 0; i < inner_comps; i++) {
5806 inner[i] = out[outer_comps+i] =
5807 lds_load(ctx, lds_inner);
5808 lds_inner = LLVMBuildAdd(ctx->builder, lds_inner,
5809 LLVMConstInt(ctx->i32, 1, false), "");
5810 }
5811 }
5812
5813 /* Convert the outputs to vectors for stores. */
5814 vec0 = ac_build_gather_values(&ctx->ac, out, MIN2(stride, 4));
5815 vec1 = NULL;
5816
5817 if (stride > 4)
5818 vec1 = ac_build_gather_values(&ctx->ac, out + 4, stride - 4);
5819
5820
5821 buffer = ctx->hs_ring_tess_factor;
5822 tf_base = ctx->tess_factor_offset;
5823 byteoffset = LLVMBuildMul(ctx->builder, rel_patch_id,
5824 LLVMConstInt(ctx->i32, 4 * stride, false), "");
5825
5826 ac_nir_build_if(&inner_if_ctx, ctx,
5827 LLVMBuildICmp(ctx->builder, LLVMIntEQ,
5828 rel_patch_id, ctx->i32zero, ""));
5829
5830 /* Store the dynamic HS control word. */
5831 ac_build_buffer_store_dword(&ctx->ac, buffer,
5832 LLVMConstInt(ctx->i32, 0x80000000, false),
5833 1, ctx->i32zero, tf_base,
5834 0, 1, 0, true, false);
5835 ac_nir_build_endif(&inner_if_ctx);
5836
5837 /* Store the tessellation factors. */
5838 ac_build_buffer_store_dword(&ctx->ac, buffer, vec0,
5839 MIN2(stride, 4), byteoffset, tf_base,
5840 4, 1, 0, true, false);
5841 if (vec1)
5842 ac_build_buffer_store_dword(&ctx->ac, buffer, vec1,
5843 stride - 4, byteoffset, tf_base,
5844 20, 1, 0, true, false);
5845
5846 //TODO store to offchip for TES to read - only if TES reads them
5847 if (1) {
5848 LLVMValueRef inner_vec, outer_vec, tf_outer_offset;
5849 LLVMValueRef tf_inner_offset;
5850 unsigned param_outer, param_inner;
5851
5852 param_outer = shader_io_get_unique_index(VARYING_SLOT_TESS_LEVEL_OUTER);
5853 tf_outer_offset = get_tcs_tes_buffer_address(ctx, NULL,
5854 LLVMConstInt(ctx->i32, param_outer, 0));
5855
5856 outer_vec = ac_build_gather_values(&ctx->ac, outer,
5857 util_next_power_of_two(outer_comps));
5858
5859 ac_build_buffer_store_dword(&ctx->ac, ctx->hs_ring_tess_offchip, outer_vec,
5860 outer_comps, tf_outer_offset,
5861 ctx->oc_lds, 0, 1, 0, true, false);
5862 if (inner_comps) {
5863 param_inner = shader_io_get_unique_index(VARYING_SLOT_TESS_LEVEL_INNER);
5864 tf_inner_offset = get_tcs_tes_buffer_address(ctx, NULL,
5865 LLVMConstInt(ctx->i32, param_inner, 0));
5866
5867 inner_vec = inner_comps == 1 ? inner[0] :
5868 ac_build_gather_values(&ctx->ac, inner, inner_comps);
5869 ac_build_buffer_store_dword(&ctx->ac, ctx->hs_ring_tess_offchip, inner_vec,
5870 inner_comps, tf_inner_offset,
5871 ctx->oc_lds, 0, 1, 0, true, false);
5872 }
5873 }
5874 ac_nir_build_endif(&if_ctx);
5875 }
5876
5877 static void
5878 handle_tcs_outputs_post(struct nir_to_llvm_context *ctx)
5879 {
5880 write_tess_factors(ctx);
5881 }
5882
5883 static bool
5884 si_export_mrt_color(struct nir_to_llvm_context *ctx,
5885 LLVMValueRef *color, unsigned param, bool is_last,
5886 struct ac_export_args *args)
5887 {
5888 /* Export */
5889 si_llvm_init_export_args(ctx, color, param,
5890 args);
5891
5892 if (is_last) {
5893 args->valid_mask = 1; /* whether the EXEC mask is valid */
5894 args->done = 1; /* DONE bit */
5895 } else if (!args->enabled_channels)
5896 return false; /* unnecessary NULL export */
5897
5898 return true;
5899 }
5900
5901 static void
5902 si_export_mrt_z(struct nir_to_llvm_context *ctx,
5903 LLVMValueRef depth, LLVMValueRef stencil,
5904 LLVMValueRef samplemask)
5905 {
5906 struct ac_export_args args;
5907
5908 args.enabled_channels = 0;
5909 args.valid_mask = 1;
5910 args.done = 1;
5911 args.target = V_008DFC_SQ_EXP_MRTZ;
5912 args.compr = false;
5913
5914 args.out[0] = LLVMGetUndef(ctx->f32); /* R, depth */
5915 args.out[1] = LLVMGetUndef(ctx->f32); /* G, stencil test val[0:7], stencil op val[8:15] */
5916 args.out[2] = LLVMGetUndef(ctx->f32); /* B, sample mask */
5917 args.out[3] = LLVMGetUndef(ctx->f32); /* A, alpha to mask */
5918
5919 if (depth) {
5920 args.out[0] = depth;
5921 args.enabled_channels |= 0x1;
5922 }
5923
5924 if (stencil) {
5925 args.out[1] = stencil;
5926 args.enabled_channels |= 0x2;
5927 }
5928
5929 if (samplemask) {
5930 args.out[2] = samplemask;
5931 args.enabled_channels |= 0x4;
5932 }
5933
5934 /* SI (except OLAND and HAINAN) has a bug that it only looks
5935 * at the X writemask component. */
5936 if (ctx->options->chip_class == SI &&
5937 ctx->options->family != CHIP_OLAND &&
5938 ctx->options->family != CHIP_HAINAN)
5939 args.enabled_channels |= 0x1;
5940
5941 ac_build_export(&ctx->ac, &args);
5942 }
5943
5944 static void
5945 handle_fs_outputs_post(struct nir_to_llvm_context *ctx)
5946 {
5947 unsigned index = 0;
5948 LLVMValueRef depth = NULL, stencil = NULL, samplemask = NULL;
5949 struct ac_export_args color_args[8];
5950
5951 for (unsigned i = 0; i < RADEON_LLVM_MAX_OUTPUTS; ++i) {
5952 LLVMValueRef values[4];
5953
5954 if (!(ctx->output_mask & (1ull << i)))
5955 continue;
5956
5957 if (i == FRAG_RESULT_DEPTH) {
5958 ctx->shader_info->fs.writes_z = true;
5959 depth = to_float(&ctx->ac, LLVMBuildLoad(ctx->builder,
5960 ctx->nir->outputs[radeon_llvm_reg_index_soa(i, 0)], ""));
5961 } else if (i == FRAG_RESULT_STENCIL) {
5962 ctx->shader_info->fs.writes_stencil = true;
5963 stencil = to_float(&ctx->ac, LLVMBuildLoad(ctx->builder,
5964 ctx->nir->outputs[radeon_llvm_reg_index_soa(i, 0)], ""));
5965 } else if (i == FRAG_RESULT_SAMPLE_MASK) {
5966 ctx->shader_info->fs.writes_sample_mask = true;
5967 samplemask = to_float(&ctx->ac, LLVMBuildLoad(ctx->builder,
5968 ctx->nir->outputs[radeon_llvm_reg_index_soa(i, 0)], ""));
5969 } else {
5970 bool last = false;
5971 for (unsigned j = 0; j < 4; j++)
5972 values[j] = to_float(&ctx->ac, LLVMBuildLoad(ctx->builder,
5973 ctx->nir->outputs[radeon_llvm_reg_index_soa(i, j)], ""));
5974
5975 if (!ctx->shader_info->fs.writes_z && !ctx->shader_info->fs.writes_stencil && !ctx->shader_info->fs.writes_sample_mask)
5976 last = ctx->output_mask <= ((1ull << (i + 1)) - 1);
5977
5978 bool ret = si_export_mrt_color(ctx, values, V_008DFC_SQ_EXP_MRT + (i - FRAG_RESULT_DATA0), last, &color_args[index]);
5979 if (ret)
5980 index++;
5981 }
5982 }
5983
5984 for (unsigned i = 0; i < index; i++)
5985 ac_build_export(&ctx->ac, &color_args[i]);
5986 if (depth || stencil || samplemask)
5987 si_export_mrt_z(ctx, depth, stencil, samplemask);
5988 else if (!index) {
5989 si_export_mrt_color(ctx, NULL, V_008DFC_SQ_EXP_NULL, true, &color_args[0]);
5990 ac_build_export(&ctx->ac, &color_args[0]);
5991 }
5992
5993 ctx->shader_info->fs.output_mask = index ? ((1ull << index) - 1) : 0;
5994 }
5995
5996 static void
5997 emit_gs_epilogue(struct nir_to_llvm_context *ctx)
5998 {
5999 ac_build_sendmsg(&ctx->ac, AC_SENDMSG_GS_OP_NOP | AC_SENDMSG_GS_DONE, ctx->gs_wave_id);
6000 }
6001
6002 static void
6003 handle_shader_outputs_post(struct ac_shader_abi *abi, unsigned max_outputs,
6004 LLVMValueRef *addrs)
6005 {
6006 struct nir_to_llvm_context *ctx = nir_to_llvm_context_from_abi(abi);
6007
6008 switch (ctx->stage) {
6009 case MESA_SHADER_VERTEX:
6010 if (ctx->options->key.vs.as_ls)
6011 handle_ls_outputs_post(ctx);
6012 else if (ctx->options->key.vs.as_es)
6013 handle_es_outputs_post(ctx, &ctx->shader_info->vs.es_info);
6014 else
6015 handle_vs_outputs_post(ctx, ctx->options->key.vs.export_prim_id,
6016 &ctx->shader_info->vs.outinfo);
6017 break;
6018 case MESA_SHADER_FRAGMENT:
6019 handle_fs_outputs_post(ctx);
6020 break;
6021 case MESA_SHADER_GEOMETRY:
6022 emit_gs_epilogue(ctx);
6023 break;
6024 case MESA_SHADER_TESS_CTRL:
6025 handle_tcs_outputs_post(ctx);
6026 break;
6027 case MESA_SHADER_TESS_EVAL:
6028 if (ctx->options->key.tes.as_es)
6029 handle_es_outputs_post(ctx, &ctx->shader_info->tes.es_info);
6030 else
6031 handle_vs_outputs_post(ctx, ctx->options->key.tes.export_prim_id,
6032 &ctx->shader_info->tes.outinfo);
6033 break;
6034 default:
6035 break;
6036 }
6037 }
6038
6039 static void ac_llvm_finalize_module(struct nir_to_llvm_context * ctx)
6040 {
6041 LLVMPassManagerRef passmgr;
6042 /* Create the pass manager */
6043 passmgr = LLVMCreateFunctionPassManagerForModule(
6044 ctx->module);
6045
6046 /* This pass should eliminate all the load and store instructions */
6047 LLVMAddPromoteMemoryToRegisterPass(passmgr);
6048
6049 /* Add some optimization passes */
6050 LLVMAddScalarReplAggregatesPass(passmgr);
6051 LLVMAddLICMPass(passmgr);
6052 LLVMAddAggressiveDCEPass(passmgr);
6053 LLVMAddCFGSimplificationPass(passmgr);
6054 LLVMAddInstructionCombiningPass(passmgr);
6055
6056 /* Run the pass */
6057 LLVMInitializeFunctionPassManager(passmgr);
6058 LLVMRunFunctionPassManager(passmgr, ctx->main_function);
6059 LLVMFinalizeFunctionPassManager(passmgr);
6060
6061 LLVMDisposeBuilder(ctx->builder);
6062 LLVMDisposePassManager(passmgr);
6063 }
6064
6065 static void
6066 ac_nir_eliminate_const_vs_outputs(struct nir_to_llvm_context *ctx)
6067 {
6068 struct ac_vs_output_info *outinfo;
6069
6070 switch (ctx->stage) {
6071 case MESA_SHADER_FRAGMENT:
6072 case MESA_SHADER_COMPUTE:
6073 case MESA_SHADER_TESS_CTRL:
6074 case MESA_SHADER_GEOMETRY:
6075 return;
6076 case MESA_SHADER_VERTEX:
6077 if (ctx->options->key.vs.as_ls ||
6078 ctx->options->key.vs.as_es)
6079 return;
6080 outinfo = &ctx->shader_info->vs.outinfo;
6081 break;
6082 case MESA_SHADER_TESS_EVAL:
6083 if (ctx->options->key.vs.as_es)
6084 return;
6085 outinfo = &ctx->shader_info->tes.outinfo;
6086 break;
6087 default:
6088 unreachable("Unhandled shader type");
6089 }
6090
6091 ac_optimize_vs_outputs(&ctx->ac,
6092 ctx->main_function,
6093 outinfo->vs_output_param_offset,
6094 VARYING_SLOT_MAX,
6095 &outinfo->param_exports);
6096 }
6097
6098 static void
6099 ac_setup_rings(struct nir_to_llvm_context *ctx)
6100 {
6101 if ((ctx->stage == MESA_SHADER_VERTEX && ctx->options->key.vs.as_es) ||
6102 (ctx->stage == MESA_SHADER_TESS_EVAL && ctx->options->key.tes.as_es)) {
6103 ctx->esgs_ring = ac_build_indexed_load_const(&ctx->ac, ctx->ring_offsets, LLVMConstInt(ctx->i32, RING_ESGS_VS, false));
6104 }
6105
6106 if (ctx->is_gs_copy_shader) {
6107 ctx->gsvs_ring = ac_build_indexed_load_const(&ctx->ac, ctx->ring_offsets, LLVMConstInt(ctx->i32, RING_GSVS_VS, false));
6108 }
6109 if (ctx->stage == MESA_SHADER_GEOMETRY) {
6110 LLVMValueRef tmp;
6111 ctx->esgs_ring = ac_build_indexed_load_const(&ctx->ac, ctx->ring_offsets, LLVMConstInt(ctx->i32, RING_ESGS_GS, false));
6112 ctx->gsvs_ring = ac_build_indexed_load_const(&ctx->ac, ctx->ring_offsets, LLVMConstInt(ctx->i32, RING_GSVS_GS, false));
6113
6114 ctx->gsvs_ring = LLVMBuildBitCast(ctx->builder, ctx->gsvs_ring, ctx->v4i32, "");
6115
6116 ctx->gsvs_ring = LLVMBuildInsertElement(ctx->builder, ctx->gsvs_ring, ctx->gsvs_num_entries, LLVMConstInt(ctx->i32, 2, false), "");
6117 tmp = LLVMBuildExtractElement(ctx->builder, ctx->gsvs_ring, ctx->i32one, "");
6118 tmp = LLVMBuildOr(ctx->builder, tmp, ctx->gsvs_ring_stride, "");
6119 ctx->gsvs_ring = LLVMBuildInsertElement(ctx->builder, ctx->gsvs_ring, tmp, ctx->i32one, "");
6120 }
6121
6122 if (ctx->stage == MESA_SHADER_TESS_CTRL ||
6123 ctx->stage == MESA_SHADER_TESS_EVAL) {
6124 ctx->hs_ring_tess_offchip = ac_build_indexed_load_const(&ctx->ac, ctx->ring_offsets, LLVMConstInt(ctx->i32, RING_HS_TESS_OFFCHIP, false));
6125 ctx->hs_ring_tess_factor = ac_build_indexed_load_const(&ctx->ac, ctx->ring_offsets, LLVMConstInt(ctx->i32, RING_HS_TESS_FACTOR, false));
6126 }
6127 }
6128
6129 static unsigned
6130 ac_nir_get_max_workgroup_size(enum chip_class chip_class,
6131 const struct nir_shader *nir)
6132 {
6133 switch (nir->stage) {
6134 case MESA_SHADER_TESS_CTRL:
6135 return chip_class >= CIK ? 128 : 64;
6136 case MESA_SHADER_GEOMETRY:
6137 return 64;
6138 case MESA_SHADER_COMPUTE:
6139 break;
6140 default:
6141 return 0;
6142 }
6143
6144 unsigned max_workgroup_size = nir->info.cs.local_size[0] *
6145 nir->info.cs.local_size[1] *
6146 nir->info.cs.local_size[2];
6147 return max_workgroup_size;
6148 }
6149
6150 void ac_nir_translate(struct ac_llvm_context *ac, struct ac_shader_abi *abi,
6151 struct nir_shader *nir, struct nir_to_llvm_context *nctx)
6152 {
6153 struct ac_nir_context ctx = {};
6154 struct nir_function *func;
6155
6156 ctx.ac = *ac;
6157 ctx.abi = abi;
6158
6159 ctx.nctx = nctx;
6160 if (nctx)
6161 nctx->nir = &ctx;
6162
6163 ctx.stage = nir->stage;
6164
6165 ctx.main_function = LLVMGetBasicBlockParent(LLVMGetInsertBlock(ctx.ac.builder));
6166
6167 nir_foreach_variable(variable, &nir->outputs)
6168 handle_shader_output_decl(&ctx, nir, variable);
6169
6170 ctx.defs = _mesa_hash_table_create(NULL, _mesa_hash_pointer,
6171 _mesa_key_pointer_equal);
6172 ctx.phis = _mesa_hash_table_create(NULL, _mesa_hash_pointer,
6173 _mesa_key_pointer_equal);
6174 ctx.vars = _mesa_hash_table_create(NULL, _mesa_hash_pointer,
6175 _mesa_key_pointer_equal);
6176
6177 func = (struct nir_function *)exec_list_get_head(&nir->functions);
6178
6179 setup_locals(&ctx, func);
6180
6181 if (nir->stage == MESA_SHADER_COMPUTE)
6182 setup_shared(&ctx, nir);
6183
6184 visit_cf_list(&ctx, &func->impl->body);
6185 phi_post_pass(&ctx);
6186
6187 ctx.abi->emit_outputs(ctx.abi, RADEON_LLVM_MAX_OUTPUTS,
6188 ctx.outputs);
6189
6190 free(ctx.locals);
6191 ralloc_free(ctx.defs);
6192 ralloc_free(ctx.phis);
6193 ralloc_free(ctx.vars);
6194
6195 if (nctx)
6196 nctx->nir = NULL;
6197 }
6198
6199 static
6200 LLVMModuleRef ac_translate_nir_to_llvm(LLVMTargetMachineRef tm,
6201 struct nir_shader *nir,
6202 struct ac_shader_variant_info *shader_info,
6203 const struct ac_nir_compiler_options *options)
6204 {
6205 struct nir_to_llvm_context ctx = {0};
6206 unsigned i;
6207 ctx.options = options;
6208 ctx.shader_info = shader_info;
6209 ctx.context = LLVMContextCreate();
6210 ctx.module = LLVMModuleCreateWithNameInContext("shader", ctx.context);
6211
6212 ac_llvm_context_init(&ctx.ac, ctx.context);
6213 ctx.ac.module = ctx.module;
6214
6215 memset(shader_info, 0, sizeof(*shader_info));
6216
6217 ac_nir_shader_info_pass(nir, options, &shader_info->info);
6218
6219 LLVMSetTarget(ctx.module, options->supports_spill ? "amdgcn-mesa-mesa3d" : "amdgcn--");
6220
6221 LLVMTargetDataRef data_layout = LLVMCreateTargetDataLayout(tm);
6222 char *data_layout_str = LLVMCopyStringRepOfTargetData(data_layout);
6223 LLVMSetDataLayout(ctx.module, data_layout_str);
6224 LLVMDisposeTargetData(data_layout);
6225 LLVMDisposeMessage(data_layout_str);
6226
6227 setup_types(&ctx);
6228
6229 ctx.builder = LLVMCreateBuilderInContext(ctx.context);
6230 ctx.ac.builder = ctx.builder;
6231 ctx.stage = nir->stage;
6232 ctx.max_workgroup_size = ac_nir_get_max_workgroup_size(ctx.options->chip_class, nir);
6233
6234 for (i = 0; i < AC_UD_MAX_SETS; i++)
6235 shader_info->user_sgprs_locs.descriptor_sets[i].sgpr_idx = -1;
6236 for (i = 0; i < AC_UD_MAX_UD; i++)
6237 shader_info->user_sgprs_locs.shader_data[i].sgpr_idx = -1;
6238
6239 create_function(&ctx);
6240
6241 if (nir->stage == MESA_SHADER_GEOMETRY) {
6242 ctx.gs_next_vertex = ac_build_alloca(&ctx.ac, ctx.i32, "gs_next_vertex");
6243
6244 ctx.gs_max_out_vertices = nir->info.gs.vertices_out;
6245 } else if (nir->stage == MESA_SHADER_TESS_EVAL) {
6246 ctx.tes_primitive_mode = nir->info.tess.primitive_mode;
6247 } else if (nir->stage == MESA_SHADER_VERTEX) {
6248 if (shader_info->info.vs.needs_instance_id) {
6249 ctx.shader_info->vs.vgpr_comp_cnt =
6250 MAX2(3, ctx.shader_info->vs.vgpr_comp_cnt);
6251 }
6252 } else if (nir->stage == MESA_SHADER_FRAGMENT) {
6253 shader_info->fs.can_discard = nir->info.fs.uses_discard;
6254 }
6255
6256 ac_setup_rings(&ctx);
6257
6258 ctx.num_output_clips = nir->info.clip_distance_array_size;
6259 ctx.num_output_culls = nir->info.cull_distance_array_size;
6260
6261 nir_foreach_variable(variable, &nir->inputs)
6262 handle_shader_input_decl(&ctx, variable);
6263
6264 if (nir->stage == MESA_SHADER_FRAGMENT)
6265 handle_fs_inputs_pre(&ctx, nir);
6266
6267 ctx.abi.chip_class = options->chip_class;
6268 ctx.abi.inputs = &ctx.inputs[0];
6269 ctx.abi.emit_outputs = handle_shader_outputs_post;
6270 ctx.abi.load_ssbo = radv_load_ssbo;
6271 ctx.abi.load_sampler_desc = radv_get_sampler_desc;
6272
6273 nir_foreach_variable(variable, &nir->outputs)
6274 scan_shader_output_decl(&ctx, variable);
6275
6276 ac_nir_translate(&ctx.ac, &ctx.abi, nir, &ctx);
6277
6278 LLVMBuildRetVoid(ctx.builder);
6279
6280 ac_llvm_finalize_module(&ctx);
6281
6282 ac_nir_eliminate_const_vs_outputs(&ctx);
6283
6284 if (nir->stage == MESA_SHADER_GEOMETRY) {
6285 unsigned addclip = ctx.num_output_clips + ctx.num_output_culls > 4;
6286 shader_info->gs.gsvs_vertex_size = (util_bitcount64(ctx.output_mask) + addclip) * 16;
6287 shader_info->gs.max_gsvs_emit_size = shader_info->gs.gsvs_vertex_size *
6288 nir->info.gs.vertices_out;
6289 } else if (nir->stage == MESA_SHADER_TESS_CTRL) {
6290 shader_info->tcs.outputs_written = ctx.tess_outputs_written;
6291 shader_info->tcs.patch_outputs_written = ctx.tess_patch_outputs_written;
6292 } else if (nir->stage == MESA_SHADER_VERTEX && ctx.options->key.vs.as_ls) {
6293 shader_info->vs.outputs_written = ctx.tess_outputs_written;
6294 }
6295
6296 return ctx.module;
6297 }
6298
6299 static void ac_diagnostic_handler(LLVMDiagnosticInfoRef di, void *context)
6300 {
6301 unsigned *retval = (unsigned *)context;
6302 LLVMDiagnosticSeverity severity = LLVMGetDiagInfoSeverity(di);
6303 char *description = LLVMGetDiagInfoDescription(di);
6304
6305 if (severity == LLVMDSError) {
6306 *retval = 1;
6307 fprintf(stderr, "LLVM triggered Diagnostic Handler: %s\n",
6308 description);
6309 }
6310
6311 LLVMDisposeMessage(description);
6312 }
6313
6314 static unsigned ac_llvm_compile(LLVMModuleRef M,
6315 struct ac_shader_binary *binary,
6316 LLVMTargetMachineRef tm)
6317 {
6318 unsigned retval = 0;
6319 char *err;
6320 LLVMContextRef llvm_ctx;
6321 LLVMMemoryBufferRef out_buffer;
6322 unsigned buffer_size;
6323 const char *buffer_data;
6324 LLVMBool mem_err;
6325
6326 /* Setup Diagnostic Handler*/
6327 llvm_ctx = LLVMGetModuleContext(M);
6328
6329 LLVMContextSetDiagnosticHandler(llvm_ctx, ac_diagnostic_handler,
6330 &retval);
6331
6332 /* Compile IR*/
6333 mem_err = LLVMTargetMachineEmitToMemoryBuffer(tm, M, LLVMObjectFile,
6334 &err, &out_buffer);
6335
6336 /* Process Errors/Warnings */
6337 if (mem_err) {
6338 fprintf(stderr, "%s: %s", __FUNCTION__, err);
6339 free(err);
6340 retval = 1;
6341 goto out;
6342 }
6343
6344 /* Extract Shader Code*/
6345 buffer_size = LLVMGetBufferSize(out_buffer);
6346 buffer_data = LLVMGetBufferStart(out_buffer);
6347
6348 ac_elf_read(buffer_data, buffer_size, binary);
6349
6350 /* Clean up */
6351 LLVMDisposeMemoryBuffer(out_buffer);
6352
6353 out:
6354 return retval;
6355 }
6356
6357 static void ac_compile_llvm_module(LLVMTargetMachineRef tm,
6358 LLVMModuleRef llvm_module,
6359 struct ac_shader_binary *binary,
6360 struct ac_shader_config *config,
6361 struct ac_shader_variant_info *shader_info,
6362 gl_shader_stage stage,
6363 bool dump_shader, bool supports_spill)
6364 {
6365 if (dump_shader)
6366 ac_dump_module(llvm_module);
6367
6368 memset(binary, 0, sizeof(*binary));
6369 int v = ac_llvm_compile(llvm_module, binary, tm);
6370 if (v) {
6371 fprintf(stderr, "compile failed\n");
6372 }
6373
6374 if (dump_shader)
6375 fprintf(stderr, "disasm:\n%s\n", binary->disasm_string);
6376
6377 ac_shader_binary_read_config(binary, config, 0, supports_spill);
6378
6379 LLVMContextRef ctx = LLVMGetModuleContext(llvm_module);
6380 LLVMDisposeModule(llvm_module);
6381 LLVMContextDispose(ctx);
6382
6383 if (stage == MESA_SHADER_FRAGMENT) {
6384 shader_info->num_input_vgprs = 0;
6385 if (G_0286CC_PERSP_SAMPLE_ENA(config->spi_ps_input_addr))
6386 shader_info->num_input_vgprs += 2;
6387 if (G_0286CC_PERSP_CENTER_ENA(config->spi_ps_input_addr))
6388 shader_info->num_input_vgprs += 2;
6389 if (G_0286CC_PERSP_CENTROID_ENA(config->spi_ps_input_addr))
6390 shader_info->num_input_vgprs += 2;
6391 if (G_0286CC_PERSP_PULL_MODEL_ENA(config->spi_ps_input_addr))
6392 shader_info->num_input_vgprs += 3;
6393 if (G_0286CC_LINEAR_SAMPLE_ENA(config->spi_ps_input_addr))
6394 shader_info->num_input_vgprs += 2;
6395 if (G_0286CC_LINEAR_CENTER_ENA(config->spi_ps_input_addr))
6396 shader_info->num_input_vgprs += 2;
6397 if (G_0286CC_LINEAR_CENTROID_ENA(config->spi_ps_input_addr))
6398 shader_info->num_input_vgprs += 2;
6399 if (G_0286CC_LINE_STIPPLE_TEX_ENA(config->spi_ps_input_addr))
6400 shader_info->num_input_vgprs += 1;
6401 if (G_0286CC_POS_X_FLOAT_ENA(config->spi_ps_input_addr))
6402 shader_info->num_input_vgprs += 1;
6403 if (G_0286CC_POS_Y_FLOAT_ENA(config->spi_ps_input_addr))
6404 shader_info->num_input_vgprs += 1;
6405 if (G_0286CC_POS_Z_FLOAT_ENA(config->spi_ps_input_addr))
6406 shader_info->num_input_vgprs += 1;
6407 if (G_0286CC_POS_W_FLOAT_ENA(config->spi_ps_input_addr))
6408 shader_info->num_input_vgprs += 1;
6409 if (G_0286CC_FRONT_FACE_ENA(config->spi_ps_input_addr))
6410 shader_info->num_input_vgprs += 1;
6411 if (G_0286CC_ANCILLARY_ENA(config->spi_ps_input_addr))
6412 shader_info->num_input_vgprs += 1;
6413 if (G_0286CC_SAMPLE_COVERAGE_ENA(config->spi_ps_input_addr))
6414 shader_info->num_input_vgprs += 1;
6415 if (G_0286CC_POS_FIXED_PT_ENA(config->spi_ps_input_addr))
6416 shader_info->num_input_vgprs += 1;
6417 }
6418 config->num_vgprs = MAX2(config->num_vgprs, shader_info->num_input_vgprs);
6419
6420 /* +3 for scratch wave offset and VCC */
6421 config->num_sgprs = MAX2(config->num_sgprs,
6422 shader_info->num_input_sgprs + 3);
6423 }
6424
6425 void ac_compile_nir_shader(LLVMTargetMachineRef tm,
6426 struct ac_shader_binary *binary,
6427 struct ac_shader_config *config,
6428 struct ac_shader_variant_info *shader_info,
6429 struct nir_shader *nir,
6430 const struct ac_nir_compiler_options *options,
6431 bool dump_shader)
6432 {
6433
6434 LLVMModuleRef llvm_module = ac_translate_nir_to_llvm(tm, nir, shader_info,
6435 options);
6436
6437 ac_compile_llvm_module(tm, llvm_module, binary, config, shader_info, nir->stage, dump_shader, options->supports_spill);
6438 switch (nir->stage) {
6439 case MESA_SHADER_COMPUTE:
6440 for (int i = 0; i < 3; ++i)
6441 shader_info->cs.block_size[i] = nir->info.cs.local_size[i];
6442 break;
6443 case MESA_SHADER_FRAGMENT:
6444 shader_info->fs.early_fragment_test = nir->info.fs.early_fragment_tests;
6445 break;
6446 case MESA_SHADER_GEOMETRY:
6447 shader_info->gs.vertices_in = nir->info.gs.vertices_in;
6448 shader_info->gs.vertices_out = nir->info.gs.vertices_out;
6449 shader_info->gs.output_prim = nir->info.gs.output_primitive;
6450 shader_info->gs.invocations = nir->info.gs.invocations;
6451 break;
6452 case MESA_SHADER_TESS_EVAL:
6453 shader_info->tes.primitive_mode = nir->info.tess.primitive_mode;
6454 shader_info->tes.spacing = nir->info.tess.spacing;
6455 shader_info->tes.ccw = nir->info.tess.ccw;
6456 shader_info->tes.point_mode = nir->info.tess.point_mode;
6457 shader_info->tes.as_es = options->key.tes.as_es;
6458 break;
6459 case MESA_SHADER_TESS_CTRL:
6460 shader_info->tcs.tcs_vertices_out = nir->info.tess.tcs_vertices_out;
6461 break;
6462 case MESA_SHADER_VERTEX:
6463 shader_info->vs.as_es = options->key.vs.as_es;
6464 shader_info->vs.as_ls = options->key.vs.as_ls;
6465 /* in LS mode we need at least 1, invocation id needs 3, handled elsewhere */
6466 if (options->key.vs.as_ls)
6467 shader_info->vs.vgpr_comp_cnt = MAX2(1, shader_info->vs.vgpr_comp_cnt);
6468 break;
6469 default:
6470 break;
6471 }
6472 }
6473
6474 static void
6475 ac_gs_copy_shader_emit(struct nir_to_llvm_context *ctx)
6476 {
6477 LLVMValueRef args[9];
6478 args[0] = ctx->gsvs_ring;
6479 args[1] = LLVMBuildMul(ctx->builder, ctx->abi.vertex_id, LLVMConstInt(ctx->i32, 4, false), "");
6480 args[3] = ctx->i32zero;
6481 args[4] = ctx->i32one; /* OFFEN */
6482 args[5] = ctx->i32zero; /* IDXEN */
6483 args[6] = ctx->i32one; /* GLC */
6484 args[7] = ctx->i32one; /* SLC */
6485 args[8] = ctx->i32zero; /* TFE */
6486
6487 int idx = 0;
6488
6489 for (unsigned i = 0; i < RADEON_LLVM_MAX_OUTPUTS; ++i) {
6490 int length = 4;
6491 int slot = idx;
6492 int slot_inc = 1;
6493 if (!(ctx->output_mask & (1ull << i)))
6494 continue;
6495
6496 if (i == VARYING_SLOT_CLIP_DIST0) {
6497 /* unpack clip and cull from a single set of slots */
6498 length = ctx->num_output_clips + ctx->num_output_culls;
6499 if (length > 4)
6500 slot_inc = 2;
6501 }
6502
6503 for (unsigned j = 0; j < length; j++) {
6504 LLVMValueRef value;
6505 args[2] = LLVMConstInt(ctx->i32,
6506 (slot * 4 + j) *
6507 ctx->gs_max_out_vertices * 16 * 4, false);
6508
6509 value = ac_build_intrinsic(&ctx->ac,
6510 "llvm.SI.buffer.load.dword.i32.i32",
6511 ctx->i32, args, 9,
6512 AC_FUNC_ATTR_READONLY |
6513 AC_FUNC_ATTR_LEGACY);
6514
6515 LLVMBuildStore(ctx->builder,
6516 to_float(&ctx->ac, value), ctx->nir->outputs[radeon_llvm_reg_index_soa(i, j)]);
6517 }
6518 idx += slot_inc;
6519 }
6520 handle_vs_outputs_post(ctx, false, &ctx->shader_info->vs.outinfo);
6521 }
6522
6523 void ac_create_gs_copy_shader(LLVMTargetMachineRef tm,
6524 struct nir_shader *geom_shader,
6525 struct ac_shader_binary *binary,
6526 struct ac_shader_config *config,
6527 struct ac_shader_variant_info *shader_info,
6528 const struct ac_nir_compiler_options *options,
6529 bool dump_shader)
6530 {
6531 struct nir_to_llvm_context ctx = {0};
6532 ctx.context = LLVMContextCreate();
6533 ctx.module = LLVMModuleCreateWithNameInContext("shader", ctx.context);
6534 ctx.options = options;
6535 ctx.shader_info = shader_info;
6536
6537 ac_llvm_context_init(&ctx.ac, ctx.context);
6538 ctx.ac.module = ctx.module;
6539
6540 ctx.is_gs_copy_shader = true;
6541 LLVMSetTarget(ctx.module, "amdgcn--");
6542 setup_types(&ctx);
6543
6544 ctx.builder = LLVMCreateBuilderInContext(ctx.context);
6545 ctx.ac.builder = ctx.builder;
6546 ctx.stage = MESA_SHADER_VERTEX;
6547
6548 create_function(&ctx);
6549
6550 ctx.gs_max_out_vertices = geom_shader->info.gs.vertices_out;
6551 ac_setup_rings(&ctx);
6552
6553 ctx.num_output_clips = geom_shader->info.clip_distance_array_size;
6554 ctx.num_output_culls = geom_shader->info.cull_distance_array_size;
6555
6556 struct ac_nir_context nir_ctx = {};
6557 nir_ctx.ac = ctx.ac;
6558 nir_ctx.abi = &ctx.abi;
6559
6560 nir_ctx.nctx = &ctx;
6561 ctx.nir = &nir_ctx;
6562
6563 nir_foreach_variable(variable, &geom_shader->outputs) {
6564 scan_shader_output_decl(&ctx, variable);
6565 handle_shader_output_decl(&nir_ctx, geom_shader, variable);
6566 }
6567
6568 ac_gs_copy_shader_emit(&ctx);
6569
6570 ctx.nir = NULL;
6571
6572 LLVMBuildRetVoid(ctx.builder);
6573
6574 ac_llvm_finalize_module(&ctx);
6575
6576 ac_compile_llvm_module(tm, ctx.module, binary, config, shader_info,
6577 MESA_SHADER_VERTEX,
6578 dump_shader, options->supports_spill);
6579 }