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