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