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