ac: add glc parameter to ac_build_buffer_load_format
[mesa.git] / src / amd / common / ac_llvm_build.c
1 /*
2 * Copyright 2014 Advanced Micro Devices, Inc.
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the
6 * "Software"), to deal in the Software without restriction, including
7 * without limitation the rights to use, copy, modify, merge, publish,
8 * distribute, sub license, and/or sell copies of the Software, and to
9 * permit persons to whom the Software is furnished to do so, subject to
10 * the following conditions:
11 *
12 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
13 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
14 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
15 * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM,
16 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
17 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
18 * USE OR OTHER DEALINGS IN THE SOFTWARE.
19 *
20 * The above copyright notice and this permission notice (including the
21 * next paragraph) shall be included in all copies or substantial portions
22 * of the Software.
23 *
24 */
25 /* based on pieces from si_pipe.c and radeon_llvm_emit.c */
26 #include "ac_llvm_build.h"
27
28 #include <llvm-c/Core.h>
29
30 #include "c11/threads.h"
31
32 #include <assert.h>
33 #include <stdio.h>
34
35 #include "ac_llvm_util.h"
36 #include "ac_exp_param.h"
37 #include "util/bitscan.h"
38 #include "util/macros.h"
39 #include "util/u_atomic.h"
40 #include "sid.h"
41
42 #include "shader_enums.h"
43
44 /* Initialize module-independent parts of the context.
45 *
46 * The caller is responsible for initializing ctx::module and ctx::builder.
47 */
48 void
49 ac_llvm_context_init(struct ac_llvm_context *ctx, LLVMContextRef context,
50 enum chip_class chip_class, enum radeon_family family)
51 {
52 LLVMValueRef args[1];
53
54 ctx->chip_class = chip_class;
55 ctx->family = family;
56
57 ctx->context = context;
58 ctx->module = NULL;
59 ctx->builder = NULL;
60
61 ctx->voidt = LLVMVoidTypeInContext(ctx->context);
62 ctx->i1 = LLVMInt1TypeInContext(ctx->context);
63 ctx->i8 = LLVMInt8TypeInContext(ctx->context);
64 ctx->i16 = LLVMIntTypeInContext(ctx->context, 16);
65 ctx->i32 = LLVMIntTypeInContext(ctx->context, 32);
66 ctx->i64 = LLVMIntTypeInContext(ctx->context, 64);
67 ctx->f16 = LLVMHalfTypeInContext(ctx->context);
68 ctx->f32 = LLVMFloatTypeInContext(ctx->context);
69 ctx->f64 = LLVMDoubleTypeInContext(ctx->context);
70 ctx->v2i32 = LLVMVectorType(ctx->i32, 2);
71 ctx->v3i32 = LLVMVectorType(ctx->i32, 3);
72 ctx->v4i32 = LLVMVectorType(ctx->i32, 4);
73 ctx->v2f32 = LLVMVectorType(ctx->f32, 2);
74 ctx->v4f32 = LLVMVectorType(ctx->f32, 4);
75 ctx->v8i32 = LLVMVectorType(ctx->i32, 8);
76
77 ctx->i32_0 = LLVMConstInt(ctx->i32, 0, false);
78 ctx->i32_1 = LLVMConstInt(ctx->i32, 1, false);
79 ctx->i64_0 = LLVMConstInt(ctx->i64, 0, false);
80 ctx->i64_1 = LLVMConstInt(ctx->i64, 1, false);
81 ctx->f32_0 = LLVMConstReal(ctx->f32, 0.0);
82 ctx->f32_1 = LLVMConstReal(ctx->f32, 1.0);
83 ctx->f64_0 = LLVMConstReal(ctx->f64, 0.0);
84 ctx->f64_1 = LLVMConstReal(ctx->f64, 1.0);
85
86 ctx->i1false = LLVMConstInt(ctx->i1, 0, false);
87 ctx->i1true = LLVMConstInt(ctx->i1, 1, false);
88
89 ctx->range_md_kind = LLVMGetMDKindIDInContext(ctx->context,
90 "range", 5);
91
92 ctx->invariant_load_md_kind = LLVMGetMDKindIDInContext(ctx->context,
93 "invariant.load", 14);
94
95 ctx->fpmath_md_kind = LLVMGetMDKindIDInContext(ctx->context, "fpmath", 6);
96
97 args[0] = LLVMConstReal(ctx->f32, 2.5);
98 ctx->fpmath_md_2p5_ulp = LLVMMDNodeInContext(ctx->context, args, 1);
99
100 ctx->uniform_md_kind = LLVMGetMDKindIDInContext(ctx->context,
101 "amdgpu.uniform", 14);
102
103 ctx->empty_md = LLVMMDNodeInContext(ctx->context, NULL, 0);
104 }
105
106 int
107 ac_get_llvm_num_components(LLVMValueRef value)
108 {
109 LLVMTypeRef type = LLVMTypeOf(value);
110 unsigned num_components = LLVMGetTypeKind(type) == LLVMVectorTypeKind
111 ? LLVMGetVectorSize(type)
112 : 1;
113 return num_components;
114 }
115
116 LLVMValueRef
117 ac_llvm_extract_elem(struct ac_llvm_context *ac,
118 LLVMValueRef value,
119 int index)
120 {
121 if (LLVMGetTypeKind(LLVMTypeOf(value)) != LLVMVectorTypeKind) {
122 assert(index == 0);
123 return value;
124 }
125
126 return LLVMBuildExtractElement(ac->builder, value,
127 LLVMConstInt(ac->i32, index, false), "");
128 }
129
130 unsigned
131 ac_get_type_size(LLVMTypeRef type)
132 {
133 LLVMTypeKind kind = LLVMGetTypeKind(type);
134
135 switch (kind) {
136 case LLVMIntegerTypeKind:
137 return LLVMGetIntTypeWidth(type) / 8;
138 case LLVMFloatTypeKind:
139 return 4;
140 case LLVMDoubleTypeKind:
141 case LLVMPointerTypeKind:
142 return 8;
143 case LLVMVectorTypeKind:
144 return LLVMGetVectorSize(type) *
145 ac_get_type_size(LLVMGetElementType(type));
146 case LLVMArrayTypeKind:
147 return LLVMGetArrayLength(type) *
148 ac_get_type_size(LLVMGetElementType(type));
149 default:
150 assert(0);
151 return 0;
152 }
153 }
154
155 static LLVMTypeRef to_integer_type_scalar(struct ac_llvm_context *ctx, LLVMTypeRef t)
156 {
157 if (t == ctx->f16 || t == ctx->i16)
158 return ctx->i16;
159 else if (t == ctx->f32 || t == ctx->i32)
160 return ctx->i32;
161 else if (t == ctx->f64 || t == ctx->i64)
162 return ctx->i64;
163 else
164 unreachable("Unhandled integer size");
165 }
166
167 LLVMTypeRef
168 ac_to_integer_type(struct ac_llvm_context *ctx, LLVMTypeRef t)
169 {
170 if (LLVMGetTypeKind(t) == LLVMVectorTypeKind) {
171 LLVMTypeRef elem_type = LLVMGetElementType(t);
172 return LLVMVectorType(to_integer_type_scalar(ctx, elem_type),
173 LLVMGetVectorSize(t));
174 }
175 return to_integer_type_scalar(ctx, t);
176 }
177
178 LLVMValueRef
179 ac_to_integer(struct ac_llvm_context *ctx, LLVMValueRef v)
180 {
181 LLVMTypeRef type = LLVMTypeOf(v);
182 return LLVMBuildBitCast(ctx->builder, v, ac_to_integer_type(ctx, type), "");
183 }
184
185 static LLVMTypeRef to_float_type_scalar(struct ac_llvm_context *ctx, LLVMTypeRef t)
186 {
187 if (t == ctx->i16 || t == ctx->f16)
188 return ctx->f16;
189 else if (t == ctx->i32 || t == ctx->f32)
190 return ctx->f32;
191 else if (t == ctx->i64 || t == ctx->f64)
192 return ctx->f64;
193 else
194 unreachable("Unhandled float size");
195 }
196
197 LLVMTypeRef
198 ac_to_float_type(struct ac_llvm_context *ctx, LLVMTypeRef t)
199 {
200 if (LLVMGetTypeKind(t) == LLVMVectorTypeKind) {
201 LLVMTypeRef elem_type = LLVMGetElementType(t);
202 return LLVMVectorType(to_float_type_scalar(ctx, elem_type),
203 LLVMGetVectorSize(t));
204 }
205 return to_float_type_scalar(ctx, t);
206 }
207
208 LLVMValueRef
209 ac_to_float(struct ac_llvm_context *ctx, LLVMValueRef v)
210 {
211 LLVMTypeRef type = LLVMTypeOf(v);
212 return LLVMBuildBitCast(ctx->builder, v, ac_to_float_type(ctx, type), "");
213 }
214
215
216 LLVMValueRef
217 ac_build_intrinsic(struct ac_llvm_context *ctx, const char *name,
218 LLVMTypeRef return_type, LLVMValueRef *params,
219 unsigned param_count, unsigned attrib_mask)
220 {
221 LLVMValueRef function, call;
222 bool set_callsite_attrs = HAVE_LLVM >= 0x0400 &&
223 !(attrib_mask & AC_FUNC_ATTR_LEGACY);
224
225 function = LLVMGetNamedFunction(ctx->module, name);
226 if (!function) {
227 LLVMTypeRef param_types[32], function_type;
228 unsigned i;
229
230 assert(param_count <= 32);
231
232 for (i = 0; i < param_count; ++i) {
233 assert(params[i]);
234 param_types[i] = LLVMTypeOf(params[i]);
235 }
236 function_type =
237 LLVMFunctionType(return_type, param_types, param_count, 0);
238 function = LLVMAddFunction(ctx->module, name, function_type);
239
240 LLVMSetFunctionCallConv(function, LLVMCCallConv);
241 LLVMSetLinkage(function, LLVMExternalLinkage);
242
243 if (!set_callsite_attrs)
244 ac_add_func_attributes(ctx->context, function, attrib_mask);
245 }
246
247 call = LLVMBuildCall(ctx->builder, function, params, param_count, "");
248 if (set_callsite_attrs)
249 ac_add_func_attributes(ctx->context, call, attrib_mask);
250 return call;
251 }
252
253 /**
254 * Given the i32 or vNi32 \p type, generate the textual name (e.g. for use with
255 * intrinsic names).
256 */
257 void ac_build_type_name_for_intr(LLVMTypeRef type, char *buf, unsigned bufsize)
258 {
259 LLVMTypeRef elem_type = type;
260
261 assert(bufsize >= 8);
262
263 if (LLVMGetTypeKind(type) == LLVMVectorTypeKind) {
264 int ret = snprintf(buf, bufsize, "v%u",
265 LLVMGetVectorSize(type));
266 if (ret < 0) {
267 char *type_name = LLVMPrintTypeToString(type);
268 fprintf(stderr, "Error building type name for: %s\n",
269 type_name);
270 return;
271 }
272 elem_type = LLVMGetElementType(type);
273 buf += ret;
274 bufsize -= ret;
275 }
276 switch (LLVMGetTypeKind(elem_type)) {
277 default: break;
278 case LLVMIntegerTypeKind:
279 snprintf(buf, bufsize, "i%d", LLVMGetIntTypeWidth(elem_type));
280 break;
281 case LLVMFloatTypeKind:
282 snprintf(buf, bufsize, "f32");
283 break;
284 case LLVMDoubleTypeKind:
285 snprintf(buf, bufsize, "f64");
286 break;
287 }
288 }
289
290 /**
291 * Helper function that builds an LLVM IR PHI node and immediately adds
292 * incoming edges.
293 */
294 LLVMValueRef
295 ac_build_phi(struct ac_llvm_context *ctx, LLVMTypeRef type,
296 unsigned count_incoming, LLVMValueRef *values,
297 LLVMBasicBlockRef *blocks)
298 {
299 LLVMValueRef phi = LLVMBuildPhi(ctx->builder, type, "");
300 LLVMAddIncoming(phi, values, blocks, count_incoming);
301 return phi;
302 }
303
304 /* Prevent optimizations (at least of memory accesses) across the current
305 * point in the program by emitting empty inline assembly that is marked as
306 * having side effects.
307 *
308 * Optionally, a value can be passed through the inline assembly to prevent
309 * LLVM from hoisting calls to ReadNone functions.
310 */
311 void
312 ac_build_optimization_barrier(struct ac_llvm_context *ctx,
313 LLVMValueRef *pvgpr)
314 {
315 static int counter = 0;
316
317 LLVMBuilderRef builder = ctx->builder;
318 char code[16];
319
320 snprintf(code, sizeof(code), "; %d", p_atomic_inc_return(&counter));
321
322 if (!pvgpr) {
323 LLVMTypeRef ftype = LLVMFunctionType(ctx->voidt, NULL, 0, false);
324 LLVMValueRef inlineasm = LLVMConstInlineAsm(ftype, code, "", true, false);
325 LLVMBuildCall(builder, inlineasm, NULL, 0, "");
326 } else {
327 LLVMTypeRef ftype = LLVMFunctionType(ctx->i32, &ctx->i32, 1, false);
328 LLVMValueRef inlineasm = LLVMConstInlineAsm(ftype, code, "=v,0", true, false);
329 LLVMValueRef vgpr = *pvgpr;
330 LLVMTypeRef vgpr_type = LLVMTypeOf(vgpr);
331 unsigned vgpr_size = ac_get_type_size(vgpr_type);
332 LLVMValueRef vgpr0;
333
334 assert(vgpr_size % 4 == 0);
335
336 vgpr = LLVMBuildBitCast(builder, vgpr, LLVMVectorType(ctx->i32, vgpr_size / 4), "");
337 vgpr0 = LLVMBuildExtractElement(builder, vgpr, ctx->i32_0, "");
338 vgpr0 = LLVMBuildCall(builder, inlineasm, &vgpr0, 1, "");
339 vgpr = LLVMBuildInsertElement(builder, vgpr, vgpr0, ctx->i32_0, "");
340 vgpr = LLVMBuildBitCast(builder, vgpr, vgpr_type, "");
341
342 *pvgpr = vgpr;
343 }
344 }
345
346 LLVMValueRef
347 ac_build_ballot(struct ac_llvm_context *ctx,
348 LLVMValueRef value)
349 {
350 LLVMValueRef args[3] = {
351 value,
352 ctx->i32_0,
353 LLVMConstInt(ctx->i32, LLVMIntNE, 0)
354 };
355
356 /* We currently have no other way to prevent LLVM from lifting the icmp
357 * calls to a dominating basic block.
358 */
359 ac_build_optimization_barrier(ctx, &args[0]);
360
361 if (LLVMTypeOf(args[0]) != ctx->i32)
362 args[0] = LLVMBuildBitCast(ctx->builder, args[0], ctx->i32, "");
363
364 return ac_build_intrinsic(ctx,
365 "llvm.amdgcn.icmp.i32",
366 ctx->i64, args, 3,
367 AC_FUNC_ATTR_NOUNWIND |
368 AC_FUNC_ATTR_READNONE |
369 AC_FUNC_ATTR_CONVERGENT);
370 }
371
372 LLVMValueRef
373 ac_build_vote_all(struct ac_llvm_context *ctx, LLVMValueRef value)
374 {
375 LLVMValueRef active_set = ac_build_ballot(ctx, ctx->i32_1);
376 LLVMValueRef vote_set = ac_build_ballot(ctx, value);
377 return LLVMBuildICmp(ctx->builder, LLVMIntEQ, vote_set, active_set, "");
378 }
379
380 LLVMValueRef
381 ac_build_vote_any(struct ac_llvm_context *ctx, LLVMValueRef value)
382 {
383 LLVMValueRef vote_set = ac_build_ballot(ctx, value);
384 return LLVMBuildICmp(ctx->builder, LLVMIntNE, vote_set,
385 LLVMConstInt(ctx->i64, 0, 0), "");
386 }
387
388 LLVMValueRef
389 ac_build_vote_eq(struct ac_llvm_context *ctx, LLVMValueRef value)
390 {
391 LLVMValueRef active_set = ac_build_ballot(ctx, ctx->i32_1);
392 LLVMValueRef vote_set = ac_build_ballot(ctx, value);
393
394 LLVMValueRef all = LLVMBuildICmp(ctx->builder, LLVMIntEQ,
395 vote_set, active_set, "");
396 LLVMValueRef none = LLVMBuildICmp(ctx->builder, LLVMIntEQ,
397 vote_set,
398 LLVMConstInt(ctx->i64, 0, 0), "");
399 return LLVMBuildOr(ctx->builder, all, none, "");
400 }
401
402 LLVMValueRef
403 ac_build_varying_gather_values(struct ac_llvm_context *ctx, LLVMValueRef *values,
404 unsigned value_count, unsigned component)
405 {
406 LLVMValueRef vec = NULL;
407
408 if (value_count == 1) {
409 return values[component];
410 } else if (!value_count)
411 unreachable("value_count is 0");
412
413 for (unsigned i = component; i < value_count + component; i++) {
414 LLVMValueRef value = values[i];
415
416 if (i == component)
417 vec = LLVMGetUndef( LLVMVectorType(LLVMTypeOf(value), value_count));
418 LLVMValueRef index = LLVMConstInt(ctx->i32, i - component, false);
419 vec = LLVMBuildInsertElement(ctx->builder, vec, value, index, "");
420 }
421 return vec;
422 }
423
424 LLVMValueRef
425 ac_build_gather_values_extended(struct ac_llvm_context *ctx,
426 LLVMValueRef *values,
427 unsigned value_count,
428 unsigned value_stride,
429 bool load,
430 bool always_vector)
431 {
432 LLVMBuilderRef builder = ctx->builder;
433 LLVMValueRef vec = NULL;
434 unsigned i;
435
436 if (value_count == 1 && !always_vector) {
437 if (load)
438 return LLVMBuildLoad(builder, values[0], "");
439 return values[0];
440 } else if (!value_count)
441 unreachable("value_count is 0");
442
443 for (i = 0; i < value_count; i++) {
444 LLVMValueRef value = values[i * value_stride];
445 if (load)
446 value = LLVMBuildLoad(builder, value, "");
447
448 if (!i)
449 vec = LLVMGetUndef( LLVMVectorType(LLVMTypeOf(value), value_count));
450 LLVMValueRef index = LLVMConstInt(ctx->i32, i, false);
451 vec = LLVMBuildInsertElement(builder, vec, value, index, "");
452 }
453 return vec;
454 }
455
456 LLVMValueRef
457 ac_build_gather_values(struct ac_llvm_context *ctx,
458 LLVMValueRef *values,
459 unsigned value_count)
460 {
461 return ac_build_gather_values_extended(ctx, values, value_count, 1, false, false);
462 }
463
464 /* Expand a scalar or vector to <4 x type> by filling the remaining channels
465 * with undef. Extract at most num_channels components from the input.
466 */
467 LLVMValueRef ac_build_expand_to_vec4(struct ac_llvm_context *ctx,
468 LLVMValueRef value,
469 unsigned num_channels)
470 {
471 LLVMTypeRef elemtype;
472 LLVMValueRef chan[4];
473
474 if (LLVMGetTypeKind(LLVMTypeOf(value)) == LLVMVectorTypeKind) {
475 unsigned vec_size = LLVMGetVectorSize(LLVMTypeOf(value));
476 num_channels = MIN2(num_channels, vec_size);
477
478 if (num_channels >= 4)
479 return value;
480
481 for (unsigned i = 0; i < num_channels; i++)
482 chan[i] = ac_llvm_extract_elem(ctx, value, i);
483
484 elemtype = LLVMGetElementType(LLVMTypeOf(value));
485 } else {
486 if (num_channels) {
487 assert(num_channels == 1);
488 chan[0] = value;
489 }
490 elemtype = LLVMTypeOf(value);
491 }
492
493 while (num_channels < 4)
494 chan[num_channels++] = LLVMGetUndef(elemtype);
495
496 return ac_build_gather_values(ctx, chan, 4);
497 }
498
499 LLVMValueRef
500 ac_build_fdiv(struct ac_llvm_context *ctx,
501 LLVMValueRef num,
502 LLVMValueRef den)
503 {
504 LLVMValueRef ret = LLVMBuildFDiv(ctx->builder, num, den, "");
505
506 /* Use v_rcp_f32 instead of precise division. */
507 if (!LLVMIsConstant(ret))
508 LLVMSetMetadata(ret, ctx->fpmath_md_kind, ctx->fpmath_md_2p5_ulp);
509 return ret;
510 }
511
512 /* Coordinates for cube map selection. sc, tc, and ma are as in Table 8.27
513 * of the OpenGL 4.5 (Compatibility Profile) specification, except ma is
514 * already multiplied by two. id is the cube face number.
515 */
516 struct cube_selection_coords {
517 LLVMValueRef stc[2];
518 LLVMValueRef ma;
519 LLVMValueRef id;
520 };
521
522 static void
523 build_cube_intrinsic(struct ac_llvm_context *ctx,
524 LLVMValueRef in[3],
525 struct cube_selection_coords *out)
526 {
527 LLVMTypeRef f32 = ctx->f32;
528
529 out->stc[1] = ac_build_intrinsic(ctx, "llvm.amdgcn.cubetc",
530 f32, in, 3, AC_FUNC_ATTR_READNONE);
531 out->stc[0] = ac_build_intrinsic(ctx, "llvm.amdgcn.cubesc",
532 f32, in, 3, AC_FUNC_ATTR_READNONE);
533 out->ma = ac_build_intrinsic(ctx, "llvm.amdgcn.cubema",
534 f32, in, 3, AC_FUNC_ATTR_READNONE);
535 out->id = ac_build_intrinsic(ctx, "llvm.amdgcn.cubeid",
536 f32, in, 3, AC_FUNC_ATTR_READNONE);
537 }
538
539 /**
540 * Build a manual selection sequence for cube face sc/tc coordinates and
541 * major axis vector (multiplied by 2 for consistency) for the given
542 * vec3 \p coords, for the face implied by \p selcoords.
543 *
544 * For the major axis, we always adjust the sign to be in the direction of
545 * selcoords.ma; i.e., a positive out_ma means that coords is pointed towards
546 * the selcoords major axis.
547 */
548 static void build_cube_select(struct ac_llvm_context *ctx,
549 const struct cube_selection_coords *selcoords,
550 const LLVMValueRef *coords,
551 LLVMValueRef *out_st,
552 LLVMValueRef *out_ma)
553 {
554 LLVMBuilderRef builder = ctx->builder;
555 LLVMTypeRef f32 = LLVMTypeOf(coords[0]);
556 LLVMValueRef is_ma_positive;
557 LLVMValueRef sgn_ma;
558 LLVMValueRef is_ma_z, is_not_ma_z;
559 LLVMValueRef is_ma_y;
560 LLVMValueRef is_ma_x;
561 LLVMValueRef sgn;
562 LLVMValueRef tmp;
563
564 is_ma_positive = LLVMBuildFCmp(builder, LLVMRealUGE,
565 selcoords->ma, LLVMConstReal(f32, 0.0), "");
566 sgn_ma = LLVMBuildSelect(builder, is_ma_positive,
567 LLVMConstReal(f32, 1.0), LLVMConstReal(f32, -1.0), "");
568
569 is_ma_z = LLVMBuildFCmp(builder, LLVMRealUGE, selcoords->id, LLVMConstReal(f32, 4.0), "");
570 is_not_ma_z = LLVMBuildNot(builder, is_ma_z, "");
571 is_ma_y = LLVMBuildAnd(builder, is_not_ma_z,
572 LLVMBuildFCmp(builder, LLVMRealUGE, selcoords->id, LLVMConstReal(f32, 2.0), ""), "");
573 is_ma_x = LLVMBuildAnd(builder, is_not_ma_z, LLVMBuildNot(builder, is_ma_y, ""), "");
574
575 /* Select sc */
576 tmp = LLVMBuildSelect(builder, is_ma_x, coords[2], coords[0], "");
577 sgn = LLVMBuildSelect(builder, is_ma_y, LLVMConstReal(f32, 1.0),
578 LLVMBuildSelect(builder, is_ma_z, sgn_ma,
579 LLVMBuildFNeg(builder, sgn_ma, ""), ""), "");
580 out_st[0] = LLVMBuildFMul(builder, tmp, sgn, "");
581
582 /* Select tc */
583 tmp = LLVMBuildSelect(builder, is_ma_y, coords[2], coords[1], "");
584 sgn = LLVMBuildSelect(builder, is_ma_y, sgn_ma,
585 LLVMConstReal(f32, -1.0), "");
586 out_st[1] = LLVMBuildFMul(builder, tmp, sgn, "");
587
588 /* Select ma */
589 tmp = LLVMBuildSelect(builder, is_ma_z, coords[2],
590 LLVMBuildSelect(builder, is_ma_y, coords[1], coords[0], ""), "");
591 tmp = ac_build_intrinsic(ctx, "llvm.fabs.f32",
592 ctx->f32, &tmp, 1, AC_FUNC_ATTR_READNONE);
593 *out_ma = LLVMBuildFMul(builder, tmp, LLVMConstReal(f32, 2.0), "");
594 }
595
596 void
597 ac_prepare_cube_coords(struct ac_llvm_context *ctx,
598 bool is_deriv, bool is_array, bool is_lod,
599 LLVMValueRef *coords_arg,
600 LLVMValueRef *derivs_arg)
601 {
602
603 LLVMBuilderRef builder = ctx->builder;
604 struct cube_selection_coords selcoords;
605 LLVMValueRef coords[3];
606 LLVMValueRef invma;
607
608 if (is_array && !is_lod) {
609 LLVMValueRef tmp = coords_arg[3];
610 tmp = ac_build_intrinsic(ctx, "llvm.rint.f32", ctx->f32, &tmp, 1, 0);
611
612 /* Section 8.9 (Texture Functions) of the GLSL 4.50 spec says:
613 *
614 * "For Array forms, the array layer used will be
615 *
616 * max(0, min(d−1, floor(layer+0.5)))
617 *
618 * where d is the depth of the texture array and layer
619 * comes from the component indicated in the tables below.
620 * Workaroudn for an issue where the layer is taken from a
621 * helper invocation which happens to fall on a different
622 * layer due to extrapolation."
623 *
624 * VI and earlier attempt to implement this in hardware by
625 * clamping the value of coords[2] = (8 * layer) + face.
626 * Unfortunately, this means that the we end up with the wrong
627 * face when clamping occurs.
628 *
629 * Clamp the layer earlier to work around the issue.
630 */
631 if (ctx->chip_class <= VI) {
632 LLVMValueRef ge0;
633 ge0 = LLVMBuildFCmp(builder, LLVMRealOGE, tmp, ctx->f32_0, "");
634 tmp = LLVMBuildSelect(builder, ge0, tmp, ctx->f32_0, "");
635 }
636
637 coords_arg[3] = tmp;
638 }
639
640 build_cube_intrinsic(ctx, coords_arg, &selcoords);
641
642 invma = ac_build_intrinsic(ctx, "llvm.fabs.f32",
643 ctx->f32, &selcoords.ma, 1, AC_FUNC_ATTR_READNONE);
644 invma = ac_build_fdiv(ctx, LLVMConstReal(ctx->f32, 1.0), invma);
645
646 for (int i = 0; i < 2; ++i)
647 coords[i] = LLVMBuildFMul(builder, selcoords.stc[i], invma, "");
648
649 coords[2] = selcoords.id;
650
651 if (is_deriv && derivs_arg) {
652 LLVMValueRef derivs[4];
653 int axis;
654
655 /* Convert cube derivatives to 2D derivatives. */
656 for (axis = 0; axis < 2; axis++) {
657 LLVMValueRef deriv_st[2];
658 LLVMValueRef deriv_ma;
659
660 /* Transform the derivative alongside the texture
661 * coordinate. Mathematically, the correct formula is
662 * as follows. Assume we're projecting onto the +Z face
663 * and denote by dx/dh the derivative of the (original)
664 * X texture coordinate with respect to horizontal
665 * window coordinates. The projection onto the +Z face
666 * plane is:
667 *
668 * f(x,z) = x/z
669 *
670 * Then df/dh = df/dx * dx/dh + df/dz * dz/dh
671 * = 1/z * dx/dh - x/z * 1/z * dz/dh.
672 *
673 * This motivatives the implementation below.
674 *
675 * Whether this actually gives the expected results for
676 * apps that might feed in derivatives obtained via
677 * finite differences is anyone's guess. The OpenGL spec
678 * seems awfully quiet about how textureGrad for cube
679 * maps should be handled.
680 */
681 build_cube_select(ctx, &selcoords, &derivs_arg[axis * 3],
682 deriv_st, &deriv_ma);
683
684 deriv_ma = LLVMBuildFMul(builder, deriv_ma, invma, "");
685
686 for (int i = 0; i < 2; ++i)
687 derivs[axis * 2 + i] =
688 LLVMBuildFSub(builder,
689 LLVMBuildFMul(builder, deriv_st[i], invma, ""),
690 LLVMBuildFMul(builder, deriv_ma, coords[i], ""), "");
691 }
692
693 memcpy(derivs_arg, derivs, sizeof(derivs));
694 }
695
696 /* Shift the texture coordinate. This must be applied after the
697 * derivative calculation.
698 */
699 for (int i = 0; i < 2; ++i)
700 coords[i] = LLVMBuildFAdd(builder, coords[i], LLVMConstReal(ctx->f32, 1.5), "");
701
702 if (is_array) {
703 /* for cube arrays coord.z = coord.w(array_index) * 8 + face */
704 /* coords_arg.w component - array_index for cube arrays */
705 LLVMValueRef tmp = LLVMBuildFMul(ctx->builder, coords_arg[3], LLVMConstReal(ctx->f32, 8.0), "");
706 coords[2] = LLVMBuildFAdd(ctx->builder, tmp, coords[2], "");
707 }
708
709 memcpy(coords_arg, coords, sizeof(coords));
710 }
711
712
713 LLVMValueRef
714 ac_build_fs_interp(struct ac_llvm_context *ctx,
715 LLVMValueRef llvm_chan,
716 LLVMValueRef attr_number,
717 LLVMValueRef params,
718 LLVMValueRef i,
719 LLVMValueRef j)
720 {
721 LLVMValueRef args[5];
722 LLVMValueRef p1;
723
724 if (HAVE_LLVM < 0x0400) {
725 LLVMValueRef ij[2];
726 ij[0] = LLVMBuildBitCast(ctx->builder, i, ctx->i32, "");
727 ij[1] = LLVMBuildBitCast(ctx->builder, j, ctx->i32, "");
728
729 args[0] = llvm_chan;
730 args[1] = attr_number;
731 args[2] = params;
732 args[3] = ac_build_gather_values(ctx, ij, 2);
733 return ac_build_intrinsic(ctx, "llvm.SI.fs.interp",
734 ctx->f32, args, 4,
735 AC_FUNC_ATTR_READNONE);
736 }
737
738 args[0] = i;
739 args[1] = llvm_chan;
740 args[2] = attr_number;
741 args[3] = params;
742
743 p1 = ac_build_intrinsic(ctx, "llvm.amdgcn.interp.p1",
744 ctx->f32, args, 4, AC_FUNC_ATTR_READNONE);
745
746 args[0] = p1;
747 args[1] = j;
748 args[2] = llvm_chan;
749 args[3] = attr_number;
750 args[4] = params;
751
752 return ac_build_intrinsic(ctx, "llvm.amdgcn.interp.p2",
753 ctx->f32, args, 5, AC_FUNC_ATTR_READNONE);
754 }
755
756 LLVMValueRef
757 ac_build_fs_interp_mov(struct ac_llvm_context *ctx,
758 LLVMValueRef parameter,
759 LLVMValueRef llvm_chan,
760 LLVMValueRef attr_number,
761 LLVMValueRef params)
762 {
763 LLVMValueRef args[4];
764 if (HAVE_LLVM < 0x0400) {
765 args[0] = llvm_chan;
766 args[1] = attr_number;
767 args[2] = params;
768
769 return ac_build_intrinsic(ctx,
770 "llvm.SI.fs.constant",
771 ctx->f32, args, 3,
772 AC_FUNC_ATTR_READNONE);
773 }
774
775 args[0] = parameter;
776 args[1] = llvm_chan;
777 args[2] = attr_number;
778 args[3] = params;
779
780 return ac_build_intrinsic(ctx, "llvm.amdgcn.interp.mov",
781 ctx->f32, args, 4, AC_FUNC_ATTR_READNONE);
782 }
783
784 LLVMValueRef
785 ac_build_gep0(struct ac_llvm_context *ctx,
786 LLVMValueRef base_ptr,
787 LLVMValueRef index)
788 {
789 LLVMValueRef indices[2] = {
790 LLVMConstInt(ctx->i32, 0, 0),
791 index,
792 };
793 return LLVMBuildGEP(ctx->builder, base_ptr,
794 indices, 2, "");
795 }
796
797 void
798 ac_build_indexed_store(struct ac_llvm_context *ctx,
799 LLVMValueRef base_ptr, LLVMValueRef index,
800 LLVMValueRef value)
801 {
802 LLVMBuildStore(ctx->builder, value,
803 ac_build_gep0(ctx, base_ptr, index));
804 }
805
806 /**
807 * Build an LLVM bytecode indexed load using LLVMBuildGEP + LLVMBuildLoad.
808 * It's equivalent to doing a load from &base_ptr[index].
809 *
810 * \param base_ptr Where the array starts.
811 * \param index The element index into the array.
812 * \param uniform Whether the base_ptr and index can be assumed to be
813 * dynamically uniform (i.e. load to an SGPR)
814 * \param invariant Whether the load is invariant (no other opcodes affect it)
815 */
816 static LLVMValueRef
817 ac_build_load_custom(struct ac_llvm_context *ctx, LLVMValueRef base_ptr,
818 LLVMValueRef index, bool uniform, bool invariant)
819 {
820 LLVMValueRef pointer, result;
821
822 pointer = ac_build_gep0(ctx, base_ptr, index);
823 if (uniform)
824 LLVMSetMetadata(pointer, ctx->uniform_md_kind, ctx->empty_md);
825 result = LLVMBuildLoad(ctx->builder, pointer, "");
826 if (invariant)
827 LLVMSetMetadata(result, ctx->invariant_load_md_kind, ctx->empty_md);
828 return result;
829 }
830
831 LLVMValueRef ac_build_load(struct ac_llvm_context *ctx, LLVMValueRef base_ptr,
832 LLVMValueRef index)
833 {
834 return ac_build_load_custom(ctx, base_ptr, index, false, false);
835 }
836
837 LLVMValueRef ac_build_load_invariant(struct ac_llvm_context *ctx,
838 LLVMValueRef base_ptr, LLVMValueRef index)
839 {
840 return ac_build_load_custom(ctx, base_ptr, index, false, true);
841 }
842
843 LLVMValueRef ac_build_load_to_sgpr(struct ac_llvm_context *ctx,
844 LLVMValueRef base_ptr, LLVMValueRef index)
845 {
846 return ac_build_load_custom(ctx, base_ptr, index, true, true);
847 }
848
849 /* TBUFFER_STORE_FORMAT_{X,XY,XYZ,XYZW} <- the suffix is selected by num_channels=1..4.
850 * The type of vdata must be one of i32 (num_channels=1), v2i32 (num_channels=2),
851 * or v4i32 (num_channels=3,4).
852 */
853 void
854 ac_build_buffer_store_dword(struct ac_llvm_context *ctx,
855 LLVMValueRef rsrc,
856 LLVMValueRef vdata,
857 unsigned num_channels,
858 LLVMValueRef voffset,
859 LLVMValueRef soffset,
860 unsigned inst_offset,
861 bool glc,
862 bool slc,
863 bool writeonly_memory,
864 bool swizzle_enable_hint)
865 {
866 /* SWIZZLE_ENABLE requires that soffset isn't folded into voffset
867 * (voffset is swizzled, but soffset isn't swizzled).
868 * llvm.amdgcn.buffer.store doesn't have a separate soffset parameter.
869 */
870 if (!swizzle_enable_hint) {
871 /* Split 3 channel stores, becase LLVM doesn't support 3-channel
872 * intrinsics. */
873 if (num_channels == 3) {
874 LLVMValueRef v[3], v01;
875
876 for (int i = 0; i < 3; i++) {
877 v[i] = LLVMBuildExtractElement(ctx->builder, vdata,
878 LLVMConstInt(ctx->i32, i, 0), "");
879 }
880 v01 = ac_build_gather_values(ctx, v, 2);
881
882 ac_build_buffer_store_dword(ctx, rsrc, v01, 2, voffset,
883 soffset, inst_offset, glc, slc,
884 writeonly_memory, swizzle_enable_hint);
885 ac_build_buffer_store_dword(ctx, rsrc, v[2], 1, voffset,
886 soffset, inst_offset + 8,
887 glc, slc,
888 writeonly_memory, swizzle_enable_hint);
889 return;
890 }
891
892 unsigned func = CLAMP(num_channels, 1, 3) - 1;
893 static const char *types[] = {"f32", "v2f32", "v4f32"};
894 char name[256];
895 LLVMValueRef offset = soffset;
896
897 if (inst_offset)
898 offset = LLVMBuildAdd(ctx->builder, offset,
899 LLVMConstInt(ctx->i32, inst_offset, 0), "");
900 if (voffset)
901 offset = LLVMBuildAdd(ctx->builder, offset, voffset, "");
902
903 LLVMValueRef args[] = {
904 ac_to_float(ctx, vdata),
905 LLVMBuildBitCast(ctx->builder, rsrc, ctx->v4i32, ""),
906 LLVMConstInt(ctx->i32, 0, 0),
907 offset,
908 LLVMConstInt(ctx->i1, glc, 0),
909 LLVMConstInt(ctx->i1, slc, 0),
910 };
911
912 snprintf(name, sizeof(name), "llvm.amdgcn.buffer.store.%s",
913 types[func]);
914
915 ac_build_intrinsic(ctx, name, ctx->voidt,
916 args, ARRAY_SIZE(args),
917 writeonly_memory ?
918 AC_FUNC_ATTR_INACCESSIBLE_MEM_ONLY :
919 AC_FUNC_ATTR_WRITEONLY);
920 return;
921 }
922
923 static unsigned dfmt[] = {
924 V_008F0C_BUF_DATA_FORMAT_32,
925 V_008F0C_BUF_DATA_FORMAT_32_32,
926 V_008F0C_BUF_DATA_FORMAT_32_32_32,
927 V_008F0C_BUF_DATA_FORMAT_32_32_32_32
928 };
929 assert(num_channels >= 1 && num_channels <= 4);
930
931 LLVMValueRef args[] = {
932 rsrc,
933 vdata,
934 LLVMConstInt(ctx->i32, num_channels, 0),
935 voffset ? voffset : LLVMGetUndef(ctx->i32),
936 soffset,
937 LLVMConstInt(ctx->i32, inst_offset, 0),
938 LLVMConstInt(ctx->i32, dfmt[num_channels - 1], 0),
939 LLVMConstInt(ctx->i32, V_008F0C_BUF_NUM_FORMAT_UINT, 0),
940 LLVMConstInt(ctx->i32, voffset != NULL, 0),
941 LLVMConstInt(ctx->i32, 0, 0), /* idxen */
942 LLVMConstInt(ctx->i32, glc, 0),
943 LLVMConstInt(ctx->i32, slc, 0),
944 LLVMConstInt(ctx->i32, 0, 0), /* tfe*/
945 };
946
947 /* The instruction offset field has 12 bits */
948 assert(voffset || inst_offset < (1 << 12));
949
950 /* The intrinsic is overloaded, we need to add a type suffix for overloading to work. */
951 unsigned func = CLAMP(num_channels, 1, 3) - 1;
952 const char *types[] = {"i32", "v2i32", "v4i32"};
953 char name[256];
954 snprintf(name, sizeof(name), "llvm.SI.tbuffer.store.%s", types[func]);
955
956 ac_build_intrinsic(ctx, name, ctx->voidt,
957 args, ARRAY_SIZE(args),
958 AC_FUNC_ATTR_LEGACY);
959 }
960
961 static LLVMValueRef
962 ac_build_buffer_load_common(struct ac_llvm_context *ctx,
963 LLVMValueRef rsrc,
964 LLVMValueRef vindex,
965 LLVMValueRef voffset,
966 unsigned num_channels,
967 bool glc,
968 bool slc,
969 bool can_speculate,
970 bool use_format)
971 {
972 LLVMValueRef args[] = {
973 LLVMBuildBitCast(ctx->builder, rsrc, ctx->v4i32, ""),
974 vindex ? vindex : LLVMConstInt(ctx->i32, 0, 0),
975 voffset,
976 LLVMConstInt(ctx->i1, glc, 0),
977 LLVMConstInt(ctx->i1, slc, 0)
978 };
979 unsigned func = CLAMP(num_channels, 1, 3) - 1;
980
981 LLVMTypeRef types[] = {ctx->f32, ctx->v2f32, ctx->v4f32};
982 const char *type_names[] = {"f32", "v2f32", "v4f32"};
983 char name[256];
984
985 if (use_format) {
986 snprintf(name, sizeof(name), "llvm.amdgcn.buffer.load.format.%s",
987 type_names[func]);
988 } else {
989 snprintf(name, sizeof(name), "llvm.amdgcn.buffer.load.%s",
990 type_names[func]);
991 }
992
993 return ac_build_intrinsic(ctx, name, types[func], args,
994 ARRAY_SIZE(args),
995 ac_get_load_intr_attribs(can_speculate));
996 }
997
998 LLVMValueRef
999 ac_build_buffer_load(struct ac_llvm_context *ctx,
1000 LLVMValueRef rsrc,
1001 int num_channels,
1002 LLVMValueRef vindex,
1003 LLVMValueRef voffset,
1004 LLVMValueRef soffset,
1005 unsigned inst_offset,
1006 unsigned glc,
1007 unsigned slc,
1008 bool can_speculate,
1009 bool allow_smem)
1010 {
1011 LLVMValueRef offset = LLVMConstInt(ctx->i32, inst_offset, 0);
1012 if (voffset)
1013 offset = LLVMBuildAdd(ctx->builder, offset, voffset, "");
1014 if (soffset)
1015 offset = LLVMBuildAdd(ctx->builder, offset, soffset, "");
1016
1017 /* TODO: VI and later generations can use SMEM with GLC=1.*/
1018 if (allow_smem && !glc && !slc) {
1019 assert(vindex == NULL);
1020
1021 LLVMValueRef result[8];
1022
1023 for (int i = 0; i < num_channels; i++) {
1024 if (i) {
1025 offset = LLVMBuildAdd(ctx->builder, offset,
1026 LLVMConstInt(ctx->i32, 4, 0), "");
1027 }
1028 LLVMValueRef args[2] = {rsrc, offset};
1029 result[i] = ac_build_intrinsic(ctx, "llvm.SI.load.const.v4i32",
1030 ctx->f32, args, 2,
1031 AC_FUNC_ATTR_READNONE |
1032 AC_FUNC_ATTR_LEGACY);
1033 }
1034 if (num_channels == 1)
1035 return result[0];
1036
1037 if (num_channels == 3)
1038 result[num_channels++] = LLVMGetUndef(ctx->f32);
1039 return ac_build_gather_values(ctx, result, num_channels);
1040 }
1041
1042 return ac_build_buffer_load_common(ctx, rsrc, vindex, offset,
1043 num_channels, glc, slc,
1044 can_speculate, false);
1045 }
1046
1047 LLVMValueRef ac_build_buffer_load_format(struct ac_llvm_context *ctx,
1048 LLVMValueRef rsrc,
1049 LLVMValueRef vindex,
1050 LLVMValueRef voffset,
1051 unsigned num_channels,
1052 bool glc,
1053 bool can_speculate)
1054 {
1055 return ac_build_buffer_load_common(ctx, rsrc, vindex, voffset,
1056 num_channels, glc, false,
1057 can_speculate, true);
1058 }
1059
1060 /**
1061 * Set range metadata on an instruction. This can only be used on load and
1062 * call instructions. If you know an instruction can only produce the values
1063 * 0, 1, 2, you would do set_range_metadata(value, 0, 3);
1064 * \p lo is the minimum value inclusive.
1065 * \p hi is the maximum value exclusive.
1066 */
1067 static void set_range_metadata(struct ac_llvm_context *ctx,
1068 LLVMValueRef value, unsigned lo, unsigned hi)
1069 {
1070 LLVMValueRef range_md, md_args[2];
1071 LLVMTypeRef type = LLVMTypeOf(value);
1072 LLVMContextRef context = LLVMGetTypeContext(type);
1073
1074 md_args[0] = LLVMConstInt(type, lo, false);
1075 md_args[1] = LLVMConstInt(type, hi, false);
1076 range_md = LLVMMDNodeInContext(context, md_args, 2);
1077 LLVMSetMetadata(value, ctx->range_md_kind, range_md);
1078 }
1079
1080 LLVMValueRef
1081 ac_get_thread_id(struct ac_llvm_context *ctx)
1082 {
1083 LLVMValueRef tid;
1084
1085 LLVMValueRef tid_args[2];
1086 tid_args[0] = LLVMConstInt(ctx->i32, 0xffffffff, false);
1087 tid_args[1] = LLVMConstInt(ctx->i32, 0, false);
1088 tid_args[1] = ac_build_intrinsic(ctx,
1089 "llvm.amdgcn.mbcnt.lo", ctx->i32,
1090 tid_args, 2, AC_FUNC_ATTR_READNONE);
1091
1092 tid = ac_build_intrinsic(ctx, "llvm.amdgcn.mbcnt.hi",
1093 ctx->i32, tid_args,
1094 2, AC_FUNC_ATTR_READNONE);
1095 set_range_metadata(ctx, tid, 0, 64);
1096 return tid;
1097 }
1098
1099 /*
1100 * SI implements derivatives using the local data store (LDS)
1101 * All writes to the LDS happen in all executing threads at
1102 * the same time. TID is the Thread ID for the current
1103 * thread and is a value between 0 and 63, representing
1104 * the thread's position in the wavefront.
1105 *
1106 * For the pixel shader threads are grouped into quads of four pixels.
1107 * The TIDs of the pixels of a quad are:
1108 *
1109 * +------+------+
1110 * |4n + 0|4n + 1|
1111 * +------+------+
1112 * |4n + 2|4n + 3|
1113 * +------+------+
1114 *
1115 * So, masking the TID with 0xfffffffc yields the TID of the top left pixel
1116 * of the quad, masking with 0xfffffffd yields the TID of the top pixel of
1117 * the current pixel's column, and masking with 0xfffffffe yields the TID
1118 * of the left pixel of the current pixel's row.
1119 *
1120 * Adding 1 yields the TID of the pixel to the right of the left pixel, and
1121 * adding 2 yields the TID of the pixel below the top pixel.
1122 */
1123 LLVMValueRef
1124 ac_build_ddxy(struct ac_llvm_context *ctx,
1125 uint32_t mask,
1126 int idx,
1127 LLVMValueRef val)
1128 {
1129 LLVMValueRef tl, trbl, args[2];
1130 LLVMValueRef result;
1131
1132 if (ctx->chip_class >= VI) {
1133 LLVMValueRef thread_id, tl_tid, trbl_tid;
1134 thread_id = ac_get_thread_id(ctx);
1135
1136 tl_tid = LLVMBuildAnd(ctx->builder, thread_id,
1137 LLVMConstInt(ctx->i32, mask, false), "");
1138
1139 trbl_tid = LLVMBuildAdd(ctx->builder, tl_tid,
1140 LLVMConstInt(ctx->i32, idx, false), "");
1141
1142 args[0] = LLVMBuildMul(ctx->builder, tl_tid,
1143 LLVMConstInt(ctx->i32, 4, false), "");
1144 args[1] = val;
1145 tl = ac_build_intrinsic(ctx,
1146 "llvm.amdgcn.ds.bpermute", ctx->i32,
1147 args, 2,
1148 AC_FUNC_ATTR_READNONE |
1149 AC_FUNC_ATTR_CONVERGENT);
1150
1151 args[0] = LLVMBuildMul(ctx->builder, trbl_tid,
1152 LLVMConstInt(ctx->i32, 4, false), "");
1153 trbl = ac_build_intrinsic(ctx,
1154 "llvm.amdgcn.ds.bpermute", ctx->i32,
1155 args, 2,
1156 AC_FUNC_ATTR_READNONE |
1157 AC_FUNC_ATTR_CONVERGENT);
1158 } else {
1159 uint32_t masks[2] = {};
1160
1161 switch (mask) {
1162 case AC_TID_MASK_TOP_LEFT:
1163 masks[0] = 0x8000;
1164 if (idx == 1)
1165 masks[1] = 0x8055;
1166 else
1167 masks[1] = 0x80aa;
1168
1169 break;
1170 case AC_TID_MASK_TOP:
1171 masks[0] = 0x8044;
1172 masks[1] = 0x80ee;
1173 break;
1174 case AC_TID_MASK_LEFT:
1175 masks[0] = 0x80a0;
1176 masks[1] = 0x80f5;
1177 break;
1178 default:
1179 assert(0);
1180 }
1181
1182 args[0] = val;
1183 args[1] = LLVMConstInt(ctx->i32, masks[0], false);
1184
1185 tl = ac_build_intrinsic(ctx,
1186 "llvm.amdgcn.ds.swizzle", ctx->i32,
1187 args, 2,
1188 AC_FUNC_ATTR_READNONE |
1189 AC_FUNC_ATTR_CONVERGENT);
1190
1191 args[1] = LLVMConstInt(ctx->i32, masks[1], false);
1192 trbl = ac_build_intrinsic(ctx,
1193 "llvm.amdgcn.ds.swizzle", ctx->i32,
1194 args, 2,
1195 AC_FUNC_ATTR_READNONE |
1196 AC_FUNC_ATTR_CONVERGENT);
1197 }
1198
1199 tl = LLVMBuildBitCast(ctx->builder, tl, ctx->f32, "");
1200 trbl = LLVMBuildBitCast(ctx->builder, trbl, ctx->f32, "");
1201 result = LLVMBuildFSub(ctx->builder, trbl, tl, "");
1202 return result;
1203 }
1204
1205 void
1206 ac_build_sendmsg(struct ac_llvm_context *ctx,
1207 uint32_t msg,
1208 LLVMValueRef wave_id)
1209 {
1210 LLVMValueRef args[2];
1211 const char *intr_name = (HAVE_LLVM < 0x0400) ? "llvm.SI.sendmsg" : "llvm.amdgcn.s.sendmsg";
1212 args[0] = LLVMConstInt(ctx->i32, msg, false);
1213 args[1] = wave_id;
1214 ac_build_intrinsic(ctx, intr_name, ctx->voidt, args, 2, 0);
1215 }
1216
1217 LLVMValueRef
1218 ac_build_imsb(struct ac_llvm_context *ctx,
1219 LLVMValueRef arg,
1220 LLVMTypeRef dst_type)
1221 {
1222 const char *intr_name = (HAVE_LLVM < 0x0400) ? "llvm.AMDGPU.flbit.i32" :
1223 "llvm.amdgcn.sffbh.i32";
1224 LLVMValueRef msb = ac_build_intrinsic(ctx, intr_name,
1225 dst_type, &arg, 1,
1226 AC_FUNC_ATTR_READNONE);
1227
1228 /* The HW returns the last bit index from MSB, but NIR/TGSI wants
1229 * the index from LSB. Invert it by doing "31 - msb". */
1230 msb = LLVMBuildSub(ctx->builder, LLVMConstInt(ctx->i32, 31, false),
1231 msb, "");
1232
1233 LLVMValueRef all_ones = LLVMConstInt(ctx->i32, -1, true);
1234 LLVMValueRef cond = LLVMBuildOr(ctx->builder,
1235 LLVMBuildICmp(ctx->builder, LLVMIntEQ,
1236 arg, LLVMConstInt(ctx->i32, 0, 0), ""),
1237 LLVMBuildICmp(ctx->builder, LLVMIntEQ,
1238 arg, all_ones, ""), "");
1239
1240 return LLVMBuildSelect(ctx->builder, cond, all_ones, msb, "");
1241 }
1242
1243 LLVMValueRef
1244 ac_build_umsb(struct ac_llvm_context *ctx,
1245 LLVMValueRef arg,
1246 LLVMTypeRef dst_type)
1247 {
1248 LLVMValueRef args[2] = {
1249 arg,
1250 ctx->i1true,
1251 };
1252 LLVMValueRef msb = ac_build_intrinsic(ctx, "llvm.ctlz.i32",
1253 dst_type, args, ARRAY_SIZE(args),
1254 AC_FUNC_ATTR_READNONE);
1255
1256 /* The HW returns the last bit index from MSB, but TGSI/NIR wants
1257 * the index from LSB. Invert it by doing "31 - msb". */
1258 msb = LLVMBuildSub(ctx->builder, LLVMConstInt(ctx->i32, 31, false),
1259 msb, "");
1260
1261 /* check for zero */
1262 return LLVMBuildSelect(ctx->builder,
1263 LLVMBuildICmp(ctx->builder, LLVMIntEQ, arg,
1264 LLVMConstInt(ctx->i32, 0, 0), ""),
1265 LLVMConstInt(ctx->i32, -1, true), msb, "");
1266 }
1267
1268 LLVMValueRef ac_build_fmin(struct ac_llvm_context *ctx, LLVMValueRef a,
1269 LLVMValueRef b)
1270 {
1271 LLVMValueRef args[2] = {a, b};
1272 return ac_build_intrinsic(ctx, "llvm.minnum.f32", ctx->f32, args, 2,
1273 AC_FUNC_ATTR_READNONE);
1274 }
1275
1276 LLVMValueRef ac_build_fmax(struct ac_llvm_context *ctx, LLVMValueRef a,
1277 LLVMValueRef b)
1278 {
1279 LLVMValueRef args[2] = {a, b};
1280 return ac_build_intrinsic(ctx, "llvm.maxnum.f32", ctx->f32, args, 2,
1281 AC_FUNC_ATTR_READNONE);
1282 }
1283
1284 LLVMValueRef ac_build_umin(struct ac_llvm_context *ctx, LLVMValueRef a,
1285 LLVMValueRef b)
1286 {
1287 LLVMValueRef cmp = LLVMBuildICmp(ctx->builder, LLVMIntULE, a, b, "");
1288 return LLVMBuildSelect(ctx->builder, cmp, a, b, "");
1289 }
1290
1291 LLVMValueRef ac_build_clamp(struct ac_llvm_context *ctx, LLVMValueRef value)
1292 {
1293 if (HAVE_LLVM >= 0x0500) {
1294 return ac_build_fmin(ctx, ac_build_fmax(ctx, value, ctx->f32_0),
1295 ctx->f32_1);
1296 }
1297
1298 LLVMValueRef args[3] = {
1299 value,
1300 LLVMConstReal(ctx->f32, 0),
1301 LLVMConstReal(ctx->f32, 1),
1302 };
1303
1304 return ac_build_intrinsic(ctx, "llvm.AMDGPU.clamp.", ctx->f32, args, 3,
1305 AC_FUNC_ATTR_READNONE |
1306 AC_FUNC_ATTR_LEGACY);
1307 }
1308
1309 void ac_build_export(struct ac_llvm_context *ctx, struct ac_export_args *a)
1310 {
1311 LLVMValueRef args[9];
1312
1313 if (HAVE_LLVM >= 0x0500) {
1314 args[0] = LLVMConstInt(ctx->i32, a->target, 0);
1315 args[1] = LLVMConstInt(ctx->i32, a->enabled_channels, 0);
1316
1317 if (a->compr) {
1318 LLVMTypeRef i16 = LLVMInt16TypeInContext(ctx->context);
1319 LLVMTypeRef v2i16 = LLVMVectorType(i16, 2);
1320
1321 args[2] = LLVMBuildBitCast(ctx->builder, a->out[0],
1322 v2i16, "");
1323 args[3] = LLVMBuildBitCast(ctx->builder, a->out[1],
1324 v2i16, "");
1325 args[4] = LLVMConstInt(ctx->i1, a->done, 0);
1326 args[5] = LLVMConstInt(ctx->i1, a->valid_mask, 0);
1327
1328 ac_build_intrinsic(ctx, "llvm.amdgcn.exp.compr.v2i16",
1329 ctx->voidt, args, 6, 0);
1330 } else {
1331 args[2] = a->out[0];
1332 args[3] = a->out[1];
1333 args[4] = a->out[2];
1334 args[5] = a->out[3];
1335 args[6] = LLVMConstInt(ctx->i1, a->done, 0);
1336 args[7] = LLVMConstInt(ctx->i1, a->valid_mask, 0);
1337
1338 ac_build_intrinsic(ctx, "llvm.amdgcn.exp.f32",
1339 ctx->voidt, args, 8, 0);
1340 }
1341 return;
1342 }
1343
1344 args[0] = LLVMConstInt(ctx->i32, a->enabled_channels, 0);
1345 args[1] = LLVMConstInt(ctx->i32, a->valid_mask, 0);
1346 args[2] = LLVMConstInt(ctx->i32, a->done, 0);
1347 args[3] = LLVMConstInt(ctx->i32, a->target, 0);
1348 args[4] = LLVMConstInt(ctx->i32, a->compr, 0);
1349 memcpy(args + 5, a->out, sizeof(a->out[0]) * 4);
1350
1351 ac_build_intrinsic(ctx, "llvm.SI.export", ctx->voidt, args, 9,
1352 AC_FUNC_ATTR_LEGACY);
1353 }
1354
1355 LLVMValueRef ac_build_image_opcode(struct ac_llvm_context *ctx,
1356 struct ac_image_args *a)
1357 {
1358 LLVMTypeRef dst_type;
1359 LLVMValueRef args[11];
1360 unsigned num_args = 0;
1361 const char *name = NULL;
1362 char intr_name[128], type[64];
1363
1364 if (HAVE_LLVM >= 0x0400) {
1365 bool sample = a->opcode == ac_image_sample ||
1366 a->opcode == ac_image_gather4 ||
1367 a->opcode == ac_image_get_lod;
1368
1369 if (sample)
1370 args[num_args++] = ac_to_float(ctx, a->addr);
1371 else
1372 args[num_args++] = a->addr;
1373
1374 args[num_args++] = a->resource;
1375 if (sample)
1376 args[num_args++] = a->sampler;
1377 args[num_args++] = LLVMConstInt(ctx->i32, a->dmask, 0);
1378 if (sample)
1379 args[num_args++] = LLVMConstInt(ctx->i1, a->unorm, 0);
1380 args[num_args++] = ctx->i1false; /* glc */
1381 args[num_args++] = ctx->i1false; /* slc */
1382 args[num_args++] = ctx->i1false; /* lwe */
1383 args[num_args++] = LLVMConstInt(ctx->i1, a->da, 0);
1384
1385 switch (a->opcode) {
1386 case ac_image_sample:
1387 name = "llvm.amdgcn.image.sample";
1388 break;
1389 case ac_image_gather4:
1390 name = "llvm.amdgcn.image.gather4";
1391 break;
1392 case ac_image_load:
1393 name = "llvm.amdgcn.image.load";
1394 break;
1395 case ac_image_load_mip:
1396 name = "llvm.amdgcn.image.load.mip";
1397 break;
1398 case ac_image_get_lod:
1399 name = "llvm.amdgcn.image.getlod";
1400 break;
1401 case ac_image_get_resinfo:
1402 name = "llvm.amdgcn.image.getresinfo";
1403 break;
1404 default:
1405 unreachable("invalid image opcode");
1406 }
1407
1408 ac_build_type_name_for_intr(LLVMTypeOf(args[0]), type,
1409 sizeof(type));
1410
1411 snprintf(intr_name, sizeof(intr_name), "%s%s%s%s.v4f32.%s.v8i32",
1412 name,
1413 a->compare ? ".c" : "",
1414 a->bias ? ".b" :
1415 a->lod ? ".l" :
1416 a->deriv ? ".d" :
1417 a->level_zero ? ".lz" : "",
1418 a->offset ? ".o" : "",
1419 type);
1420
1421 LLVMValueRef result =
1422 ac_build_intrinsic(ctx, intr_name,
1423 ctx->v4f32, args, num_args,
1424 AC_FUNC_ATTR_READNONE);
1425 if (!sample) {
1426 result = LLVMBuildBitCast(ctx->builder, result,
1427 ctx->v4i32, "");
1428 }
1429 return result;
1430 }
1431
1432 args[num_args++] = a->addr;
1433 args[num_args++] = a->resource;
1434
1435 if (a->opcode == ac_image_load ||
1436 a->opcode == ac_image_load_mip ||
1437 a->opcode == ac_image_get_resinfo) {
1438 dst_type = ctx->v4i32;
1439 } else {
1440 dst_type = ctx->v4f32;
1441 args[num_args++] = a->sampler;
1442 }
1443
1444 args[num_args++] = LLVMConstInt(ctx->i32, a->dmask, 0);
1445 args[num_args++] = LLVMConstInt(ctx->i32, a->unorm, 0);
1446 args[num_args++] = LLVMConstInt(ctx->i32, 0, 0); /* r128 */
1447 args[num_args++] = LLVMConstInt(ctx->i32, a->da, 0);
1448 args[num_args++] = LLVMConstInt(ctx->i32, 0, 0); /* glc */
1449 args[num_args++] = LLVMConstInt(ctx->i32, 0, 0); /* slc */
1450 args[num_args++] = LLVMConstInt(ctx->i32, 0, 0); /* tfe */
1451 args[num_args++] = LLVMConstInt(ctx->i32, 0, 0); /* lwe */
1452
1453 switch (a->opcode) {
1454 case ac_image_sample:
1455 name = "llvm.SI.image.sample";
1456 break;
1457 case ac_image_gather4:
1458 name = "llvm.SI.gather4";
1459 break;
1460 case ac_image_load:
1461 name = "llvm.SI.image.load";
1462 break;
1463 case ac_image_load_mip:
1464 name = "llvm.SI.image.load.mip";
1465 break;
1466 case ac_image_get_lod:
1467 name = "llvm.SI.getlod";
1468 break;
1469 case ac_image_get_resinfo:
1470 name = "llvm.SI.getresinfo";
1471 break;
1472 }
1473
1474 ac_build_type_name_for_intr(LLVMTypeOf(a->addr), type, sizeof(type));
1475 snprintf(intr_name, sizeof(intr_name), "%s%s%s%s.%s",
1476 name,
1477 a->compare ? ".c" : "",
1478 a->bias ? ".b" :
1479 a->lod ? ".l" :
1480 a->deriv ? ".d" :
1481 a->level_zero ? ".lz" : "",
1482 a->offset ? ".o" : "",
1483 type);
1484
1485 return ac_build_intrinsic(ctx, intr_name,
1486 dst_type, args, num_args,
1487 AC_FUNC_ATTR_READNONE |
1488 AC_FUNC_ATTR_LEGACY);
1489 }
1490
1491 LLVMValueRef ac_build_cvt_pkrtz_f16(struct ac_llvm_context *ctx,
1492 LLVMValueRef args[2])
1493 {
1494 if (HAVE_LLVM >= 0x0500) {
1495 LLVMTypeRef v2f16 =
1496 LLVMVectorType(LLVMHalfTypeInContext(ctx->context), 2);
1497 LLVMValueRef res =
1498 ac_build_intrinsic(ctx, "llvm.amdgcn.cvt.pkrtz",
1499 v2f16, args, 2,
1500 AC_FUNC_ATTR_READNONE);
1501 return LLVMBuildBitCast(ctx->builder, res, ctx->i32, "");
1502 }
1503
1504 return ac_build_intrinsic(ctx, "llvm.SI.packf16", ctx->i32, args, 2,
1505 AC_FUNC_ATTR_READNONE |
1506 AC_FUNC_ATTR_LEGACY);
1507 }
1508
1509 LLVMValueRef ac_build_wqm_vote(struct ac_llvm_context *ctx, LLVMValueRef i1)
1510 {
1511 assert(HAVE_LLVM >= 0x0600);
1512 return ac_build_intrinsic(ctx, "llvm.amdgcn.wqm.vote", ctx->i1,
1513 &i1, 1, AC_FUNC_ATTR_READNONE);
1514 }
1515
1516 void ac_build_kill_if_false(struct ac_llvm_context *ctx, LLVMValueRef i1)
1517 {
1518 if (HAVE_LLVM >= 0x0600) {
1519 ac_build_intrinsic(ctx, "llvm.amdgcn.kill", ctx->voidt,
1520 &i1, 1, 0);
1521 return;
1522 }
1523
1524 LLVMValueRef value = LLVMBuildSelect(ctx->builder, i1,
1525 LLVMConstReal(ctx->f32, 1),
1526 LLVMConstReal(ctx->f32, -1), "");
1527 ac_build_intrinsic(ctx, "llvm.AMDGPU.kill", ctx->voidt,
1528 &value, 1, AC_FUNC_ATTR_LEGACY);
1529 }
1530
1531 LLVMValueRef ac_build_bfe(struct ac_llvm_context *ctx, LLVMValueRef input,
1532 LLVMValueRef offset, LLVMValueRef width,
1533 bool is_signed)
1534 {
1535 LLVMValueRef args[] = {
1536 input,
1537 offset,
1538 width,
1539 };
1540
1541 if (HAVE_LLVM >= 0x0500) {
1542 return ac_build_intrinsic(ctx,
1543 is_signed ? "llvm.amdgcn.sbfe.i32" :
1544 "llvm.amdgcn.ubfe.i32",
1545 ctx->i32, args, 3,
1546 AC_FUNC_ATTR_READNONE);
1547 }
1548
1549 return ac_build_intrinsic(ctx,
1550 is_signed ? "llvm.AMDGPU.bfe.i32" :
1551 "llvm.AMDGPU.bfe.u32",
1552 ctx->i32, args, 3,
1553 AC_FUNC_ATTR_READNONE |
1554 AC_FUNC_ATTR_LEGACY);
1555 }
1556
1557 void ac_build_waitcnt(struct ac_llvm_context *ctx, unsigned simm16)
1558 {
1559 LLVMValueRef args[1] = {
1560 LLVMConstInt(ctx->i32, simm16, false),
1561 };
1562 ac_build_intrinsic(ctx, "llvm.amdgcn.s.waitcnt",
1563 ctx->voidt, args, 1, 0);
1564 }
1565
1566 void ac_get_image_intr_name(const char *base_name,
1567 LLVMTypeRef data_type,
1568 LLVMTypeRef coords_type,
1569 LLVMTypeRef rsrc_type,
1570 char *out_name, unsigned out_len)
1571 {
1572 char coords_type_name[8];
1573
1574 ac_build_type_name_for_intr(coords_type, coords_type_name,
1575 sizeof(coords_type_name));
1576
1577 if (HAVE_LLVM <= 0x0309) {
1578 snprintf(out_name, out_len, "%s.%s", base_name, coords_type_name);
1579 } else {
1580 char data_type_name[8];
1581 char rsrc_type_name[8];
1582
1583 ac_build_type_name_for_intr(data_type, data_type_name,
1584 sizeof(data_type_name));
1585 ac_build_type_name_for_intr(rsrc_type, rsrc_type_name,
1586 sizeof(rsrc_type_name));
1587 snprintf(out_name, out_len, "%s.%s.%s.%s", base_name,
1588 data_type_name, coords_type_name, rsrc_type_name);
1589 }
1590 }
1591
1592 #define AC_EXP_TARGET (HAVE_LLVM >= 0x0500 ? 0 : 3)
1593 #define AC_EXP_OUT0 (HAVE_LLVM >= 0x0500 ? 2 : 5)
1594
1595 enum ac_ir_type {
1596 AC_IR_UNDEF,
1597 AC_IR_CONST,
1598 AC_IR_VALUE,
1599 };
1600
1601 struct ac_vs_exp_chan
1602 {
1603 LLVMValueRef value;
1604 float const_float;
1605 enum ac_ir_type type;
1606 };
1607
1608 struct ac_vs_exp_inst {
1609 unsigned offset;
1610 LLVMValueRef inst;
1611 struct ac_vs_exp_chan chan[4];
1612 };
1613
1614 struct ac_vs_exports {
1615 unsigned num;
1616 struct ac_vs_exp_inst exp[VARYING_SLOT_MAX];
1617 };
1618
1619 /* Return true if the PARAM export has been eliminated. */
1620 static bool ac_eliminate_const_output(uint8_t *vs_output_param_offset,
1621 uint32_t num_outputs,
1622 struct ac_vs_exp_inst *exp)
1623 {
1624 unsigned i, default_val; /* SPI_PS_INPUT_CNTL_i.DEFAULT_VAL */
1625 bool is_zero[4] = {}, is_one[4] = {};
1626
1627 for (i = 0; i < 4; i++) {
1628 /* It's a constant expression. Undef outputs are eliminated too. */
1629 if (exp->chan[i].type == AC_IR_UNDEF) {
1630 is_zero[i] = true;
1631 is_one[i] = true;
1632 } else if (exp->chan[i].type == AC_IR_CONST) {
1633 if (exp->chan[i].const_float == 0)
1634 is_zero[i] = true;
1635 else if (exp->chan[i].const_float == 1)
1636 is_one[i] = true;
1637 else
1638 return false; /* other constant */
1639 } else
1640 return false;
1641 }
1642
1643 /* Only certain combinations of 0 and 1 can be eliminated. */
1644 if (is_zero[0] && is_zero[1] && is_zero[2])
1645 default_val = is_zero[3] ? 0 : 1;
1646 else if (is_one[0] && is_one[1] && is_one[2])
1647 default_val = is_zero[3] ? 2 : 3;
1648 else
1649 return false;
1650
1651 /* The PARAM export can be represented as DEFAULT_VAL. Kill it. */
1652 LLVMInstructionEraseFromParent(exp->inst);
1653
1654 /* Change OFFSET to DEFAULT_VAL. */
1655 for (i = 0; i < num_outputs; i++) {
1656 if (vs_output_param_offset[i] == exp->offset) {
1657 vs_output_param_offset[i] =
1658 AC_EXP_PARAM_DEFAULT_VAL_0000 + default_val;
1659 break;
1660 }
1661 }
1662 return true;
1663 }
1664
1665 static bool ac_eliminate_duplicated_output(uint8_t *vs_output_param_offset,
1666 uint32_t num_outputs,
1667 struct ac_vs_exports *processed,
1668 struct ac_vs_exp_inst *exp)
1669 {
1670 unsigned p, copy_back_channels = 0;
1671
1672 /* See if the output is already in the list of processed outputs.
1673 * The LLVMValueRef comparison relies on SSA.
1674 */
1675 for (p = 0; p < processed->num; p++) {
1676 bool different = false;
1677
1678 for (unsigned j = 0; j < 4; j++) {
1679 struct ac_vs_exp_chan *c1 = &processed->exp[p].chan[j];
1680 struct ac_vs_exp_chan *c2 = &exp->chan[j];
1681
1682 /* Treat undef as a match. */
1683 if (c2->type == AC_IR_UNDEF)
1684 continue;
1685
1686 /* If c1 is undef but c2 isn't, we can copy c2 to c1
1687 * and consider the instruction duplicated.
1688 */
1689 if (c1->type == AC_IR_UNDEF) {
1690 copy_back_channels |= 1 << j;
1691 continue;
1692 }
1693
1694 /* Test whether the channels are not equal. */
1695 if (c1->type != c2->type ||
1696 (c1->type == AC_IR_CONST &&
1697 c1->const_float != c2->const_float) ||
1698 (c1->type == AC_IR_VALUE &&
1699 c1->value != c2->value)) {
1700 different = true;
1701 break;
1702 }
1703 }
1704 if (!different)
1705 break;
1706
1707 copy_back_channels = 0;
1708 }
1709 if (p == processed->num)
1710 return false;
1711
1712 /* If a match was found, but the matching export has undef where the new
1713 * one has a normal value, copy the normal value to the undef channel.
1714 */
1715 struct ac_vs_exp_inst *match = &processed->exp[p];
1716
1717 while (copy_back_channels) {
1718 unsigned chan = u_bit_scan(&copy_back_channels);
1719
1720 assert(match->chan[chan].type == AC_IR_UNDEF);
1721 LLVMSetOperand(match->inst, AC_EXP_OUT0 + chan,
1722 exp->chan[chan].value);
1723 match->chan[chan] = exp->chan[chan];
1724 }
1725
1726 /* The PARAM export is duplicated. Kill it. */
1727 LLVMInstructionEraseFromParent(exp->inst);
1728
1729 /* Change OFFSET to the matching export. */
1730 for (unsigned i = 0; i < num_outputs; i++) {
1731 if (vs_output_param_offset[i] == exp->offset) {
1732 vs_output_param_offset[i] = match->offset;
1733 break;
1734 }
1735 }
1736 return true;
1737 }
1738
1739 void ac_optimize_vs_outputs(struct ac_llvm_context *ctx,
1740 LLVMValueRef main_fn,
1741 uint8_t *vs_output_param_offset,
1742 uint32_t num_outputs,
1743 uint8_t *num_param_exports)
1744 {
1745 LLVMBasicBlockRef bb;
1746 bool removed_any = false;
1747 struct ac_vs_exports exports;
1748
1749 exports.num = 0;
1750
1751 /* Process all LLVM instructions. */
1752 bb = LLVMGetFirstBasicBlock(main_fn);
1753 while (bb) {
1754 LLVMValueRef inst = LLVMGetFirstInstruction(bb);
1755
1756 while (inst) {
1757 LLVMValueRef cur = inst;
1758 inst = LLVMGetNextInstruction(inst);
1759 struct ac_vs_exp_inst exp;
1760
1761 if (LLVMGetInstructionOpcode(cur) != LLVMCall)
1762 continue;
1763
1764 LLVMValueRef callee = ac_llvm_get_called_value(cur);
1765
1766 if (!ac_llvm_is_function(callee))
1767 continue;
1768
1769 const char *name = LLVMGetValueName(callee);
1770 unsigned num_args = LLVMCountParams(callee);
1771
1772 /* Check if this is an export instruction. */
1773 if ((num_args != 9 && num_args != 8) ||
1774 (strcmp(name, "llvm.SI.export") &&
1775 strcmp(name, "llvm.amdgcn.exp.f32")))
1776 continue;
1777
1778 LLVMValueRef arg = LLVMGetOperand(cur, AC_EXP_TARGET);
1779 unsigned target = LLVMConstIntGetZExtValue(arg);
1780
1781 if (target < V_008DFC_SQ_EXP_PARAM)
1782 continue;
1783
1784 target -= V_008DFC_SQ_EXP_PARAM;
1785
1786 /* Parse the instruction. */
1787 memset(&exp, 0, sizeof(exp));
1788 exp.offset = target;
1789 exp.inst = cur;
1790
1791 for (unsigned i = 0; i < 4; i++) {
1792 LLVMValueRef v = LLVMGetOperand(cur, AC_EXP_OUT0 + i);
1793
1794 exp.chan[i].value = v;
1795
1796 if (LLVMIsUndef(v)) {
1797 exp.chan[i].type = AC_IR_UNDEF;
1798 } else if (LLVMIsAConstantFP(v)) {
1799 LLVMBool loses_info;
1800 exp.chan[i].type = AC_IR_CONST;
1801 exp.chan[i].const_float =
1802 LLVMConstRealGetDouble(v, &loses_info);
1803 } else {
1804 exp.chan[i].type = AC_IR_VALUE;
1805 }
1806 }
1807
1808 /* Eliminate constant and duplicated PARAM exports. */
1809 if (ac_eliminate_const_output(vs_output_param_offset,
1810 num_outputs, &exp) ||
1811 ac_eliminate_duplicated_output(vs_output_param_offset,
1812 num_outputs, &exports,
1813 &exp)) {
1814 removed_any = true;
1815 } else {
1816 exports.exp[exports.num++] = exp;
1817 }
1818 }
1819 bb = LLVMGetNextBasicBlock(bb);
1820 }
1821
1822 /* Remove holes in export memory due to removed PARAM exports.
1823 * This is done by renumbering all PARAM exports.
1824 */
1825 if (removed_any) {
1826 uint8_t old_offset[VARYING_SLOT_MAX];
1827 unsigned out, i;
1828
1829 /* Make a copy of the offsets. We need the old version while
1830 * we are modifying some of them. */
1831 memcpy(old_offset, vs_output_param_offset,
1832 sizeof(old_offset));
1833
1834 for (i = 0; i < exports.num; i++) {
1835 unsigned offset = exports.exp[i].offset;
1836
1837 /* Update vs_output_param_offset. Multiple outputs can
1838 * have the same offset.
1839 */
1840 for (out = 0; out < num_outputs; out++) {
1841 if (old_offset[out] == offset)
1842 vs_output_param_offset[out] = i;
1843 }
1844
1845 /* Change the PARAM offset in the instruction. */
1846 LLVMSetOperand(exports.exp[i].inst, AC_EXP_TARGET,
1847 LLVMConstInt(ctx->i32,
1848 V_008DFC_SQ_EXP_PARAM + i, 0));
1849 }
1850 *num_param_exports = exports.num;
1851 }
1852 }
1853
1854 void ac_init_exec_full_mask(struct ac_llvm_context *ctx)
1855 {
1856 LLVMValueRef full_mask = LLVMConstInt(ctx->i64, ~0ull, 0);
1857 ac_build_intrinsic(ctx,
1858 "llvm.amdgcn.init.exec", ctx->voidt,
1859 &full_mask, 1, AC_FUNC_ATTR_CONVERGENT);
1860 }
1861
1862 void ac_declare_lds_as_pointer(struct ac_llvm_context *ctx)
1863 {
1864 unsigned lds_size = ctx->chip_class >= CIK ? 65536 : 32768;
1865 ctx->lds = LLVMBuildIntToPtr(ctx->builder, ctx->i32_0,
1866 LLVMPointerType(LLVMArrayType(ctx->i32, lds_size / 4), AC_LOCAL_ADDR_SPACE),
1867 "lds");
1868 }
1869
1870 LLVMValueRef ac_lds_load(struct ac_llvm_context *ctx,
1871 LLVMValueRef dw_addr)
1872 {
1873 return ac_build_load(ctx, ctx->lds, dw_addr);
1874 }
1875
1876 void ac_lds_store(struct ac_llvm_context *ctx,
1877 LLVMValueRef dw_addr,
1878 LLVMValueRef value)
1879 {
1880 value = ac_to_integer(ctx, value);
1881 ac_build_indexed_store(ctx, ctx->lds,
1882 dw_addr, value);
1883 }
1884
1885 LLVMValueRef ac_find_lsb(struct ac_llvm_context *ctx,
1886 LLVMTypeRef dst_type,
1887 LLVMValueRef src0)
1888 {
1889 LLVMValueRef params[2] = {
1890 src0,
1891
1892 /* The value of 1 means that ffs(x=0) = undef, so LLVM won't
1893 * add special code to check for x=0. The reason is that
1894 * the LLVM behavior for x=0 is different from what we
1895 * need here. However, LLVM also assumes that ffs(x) is
1896 * in [0, 31], but GLSL expects that ffs(0) = -1, so
1897 * a conditional assignment to handle 0 is still required.
1898 *
1899 * The hardware already implements the correct behavior.
1900 */
1901 LLVMConstInt(ctx->i1, 1, false),
1902 };
1903
1904 LLVMValueRef lsb = ac_build_intrinsic(ctx, "llvm.cttz.i32", ctx->i32,
1905 params, 2,
1906 AC_FUNC_ATTR_READNONE);
1907
1908 /* TODO: We need an intrinsic to skip this conditional. */
1909 /* Check for zero: */
1910 return LLVMBuildSelect(ctx->builder, LLVMBuildICmp(ctx->builder,
1911 LLVMIntEQ, src0,
1912 ctx->i32_0, ""),
1913 LLVMConstInt(ctx->i32, -1, 0), lsb, "");
1914 }
1915
1916 LLVMTypeRef ac_array_in_const_addr_space(LLVMTypeRef elem_type)
1917 {
1918 return LLVMPointerType(LLVMArrayType(elem_type, 0),
1919 AC_CONST_ADDR_SPACE);
1920 }