Debian Codegraph @

src/gallium/auxiliary/gallivm/lp_bld_gather.c

raw symbols SCIP
1 /**************************************************************************
2 *
3 * Copyright 2010 VMware, Inc.
4 * All Rights Reserved.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the
8 * "Software"), to deal in the Software without restriction, including
9 * without limitation the rights to use, copy, modify, merge, publish,
10 * distribute, sub license, and/or sell copies of the Software, and to
11 * permit persons to whom the Software is furnished to do so, subject to
12 * the following conditions:
13 *
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
17 * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM,
18 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
19 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
20 * USE OR OTHER DEALINGS IN THE SOFTWARE.
21 *
22 * The above copyright notice and this permission notice (including the
23 * next paragraph) shall be included in all copies or substantial portions
24 * of the Software.
25 *
26 **************************************************************************/
27
28
29 #include "util/u_cpu_detect.h"
30 #include "util/bitscan.h"
31 #include "lp_bld_const.h"
32 #include "lp_bld_format.h"
33 #include "lp_bld_gather.h"
34 #include "lp_bld_swizzle.h"
35 #include "lp_bld_type.h"
36 #include "lp_bld_init.h"
37 #include "lp_bld_intr.h"
38 #include "lp_bld_pack.h"
39
40
41 /**
42 * Get the pointer to one element from scatter positions in memory.
43 *
44 * @sa lp_build_gather()
45 */
46 LLVMValueRef
47 lp_build_gather_elem_ptr(struct gallivm_state *gallivm,
48 unsigned length,
49 LLVMValueRef base_ptr,
50 LLVMValueRef offsets,
51 unsigned i)
52 {
53 LLVMValueRef offset;
54 LLVMValueRef ptr;
55
56 ASSERTED LLVMTypeRef element_type = LLVMInt8TypeInContext(gallivm->context);
57 assert(LLVMTypeOf(base_ptr) == LLVMPointerType(element_type, 0));
58
59 if (length == 1) {
60 assert(i == 0);
61 offset = offsets;
62 } else {
63 LLVMValueRef index = lp_build_const_int32(gallivm, i);
64 offset = LLVMBuildExtractElement(gallivm->builder, offsets, index, "");
65 }
66
67 ptr = LLVMBuildGEP2(gallivm->builder, element_type, base_ptr, &offset, 1, "");
68
69 return ptr;
70 }
71
72
73 /**
74 * Gather one element from scatter positions in memory.
75 *
76 * @sa lp_build_gather()
77 */
78 LLVMValueRef
79 lp_build_gather_elem(struct gallivm_state *gallivm,
80 unsigned length,
81 unsigned src_width,
82 unsigned dst_width,
83 bool aligned,
84 LLVMValueRef base_ptr,
85 LLVMValueRef offsets,
86 unsigned i,
87 bool vector_justify)
88 {
89 LLVMTypeRef src_type = LLVMIntTypeInContext(gallivm->context, src_width);
90 LLVMTypeRef dst_elem_type = LLVMIntTypeInContext(gallivm->context, dst_width);
91 LLVMValueRef ptr;
92 LLVMValueRef res;
93
94 assert(LLVMTypeOf(base_ptr) == LLVMPointerType(LLVMInt8TypeInContext(gallivm->context), 0));
95
96 ptr = lp_build_gather_elem_ptr(gallivm, length, base_ptr, offsets, i);
97 ptr = LLVMBuildBitCast(gallivm->builder, ptr, LLVMPointerType(src_type, 0), "");
98 res = LLVMBuildLoad2(gallivm->builder, src_type, ptr, "");
99
100 /* XXX
101 * On some archs we probably really want to avoid having to deal
102 * with alignments lower than 4 bytes (if fetch size is a power of
103 * two >= 32). On x86 it doesn't matter, however.
104 * We should be able to guarantee full alignment for any kind of texture
105 * fetch (except ARB_texture_buffer_range, oops), but not vertex fetch
106 * (there's pipe_caps.vertex_input_alignment
107 * but I don't think that's quite what we wanted).
108 * For ARB_texture_buffer_range, pipe_caps.texture_buffer_offset_alignment
109 * looks like a good fit, but it seems this cap bit (and OpenGL) aren't
110 * enforcing what we want (which is what d3d10 does, the offset needs to
111 * be aligned to element size, but GL has bytes regardless of element
112 * size which would only leave us with minimum alignment restriction of 16
113 * which doesn't make much sense if the type isn't 4x32bit). Due to
114 * translation of offsets to first_elem in sampler_views it actually seems
115 * gallium could not do anything else except 16 no matter what...
116 */
117 if (!aligned) {
118 LLVMSetAlignment(res, 1);
119 } else if (!util_is_power_of_two_or_zero(src_width)) {
120 /*
121 * Full alignment is impossible, assume the caller really meant
122 * the individual elements were aligned (e.g. 3x32bit format).
123 * And yes the generated code may otherwise crash, llvm will
124 * really assume 128bit alignment with a 96bit fetch (I suppose
125 * that makes sense as it can just assume the upper 32bit to be
126 * whatever).
127 * Maybe the caller should be able to explicitly set this, but
128 * this should cover all the 3-channel formats.
129 */
130 if (((src_width / 24) * 24 == src_width) &&
131 util_is_power_of_two_or_zero(src_width / 24)) {
132 LLVMSetAlignment(res, src_width / 24);
133 } else {
134 LLVMSetAlignment(res, 1);
135 }
136 }
137
138 assert(src_width <= dst_width);
139 if (src_width < dst_width) {
140 res = LLVMBuildZExt(gallivm->builder, res, dst_elem_type, "");
141 if (vector_justify) {
142 #if UTIL_ARCH_BIG_ENDIAN
143 res = LLVMBuildShl(gallivm->builder, res,
144 LLVMConstInt(dst_elem_type, dst_width - src_width, 0), "");
145 #endif
146 }
147 }
148
149 return res;
150 }
151
152
153 /**
154 * Gather one element from scatter positions in memory.
155 * Nearly the same as above, however the individual elements
156 * may be vectors themselves, and fetches may be float type.
157 * Can also do pad vector instead of ZExt.
158 *
159 * @sa lp_build_gather()
160 */
161 static LLVMValueRef
162 lp_build_gather_elem_vec(struct gallivm_state *gallivm,
163 unsigned length,
164 unsigned src_width,
165 LLVMTypeRef src_type,
166 struct lp_type dst_type,
167 bool aligned,
168 LLVMValueRef base_ptr,
169 LLVMValueRef offsets,
170 unsigned i,
171 bool vector_justify)
172 {
173 LLVMValueRef ptr, res;
174 assert(LLVMTypeOf(base_ptr) == LLVMPointerType(LLVMInt8TypeInContext(gallivm->context), 0));
175
176 ptr = lp_build_gather_elem_ptr(gallivm, length, base_ptr, offsets, i);
177 ptr = LLVMBuildBitCast(gallivm->builder, ptr, LLVMPointerType(src_type, 0), "");
178 res = LLVMBuildLoad2(gallivm->builder, src_type, ptr, "");
179
180 /* XXX
181 * On some archs we probably really want to avoid having to deal
182 * with alignments lower than 4 bytes (if fetch size is a power of
183 * two >= 32). On x86 it doesn't matter, however.
184 * We should be able to guarantee full alignment for any kind of texture
185 * fetch (except ARB_texture_buffer_range, oops), but not vertex fetch
186 * (there's pipe_caps.vertex_input_alignment
187 * but I don't think that's quite what we wanted).
188 * For ARB_texture_buffer_range, pipe_caps.texture_buffer_offset_alignment
189 * looks like a good fit, but it seems this cap bit (and OpenGL) aren't
190 * enforcing what we want (which is what d3d10 does, the offset needs to
191 * be aligned to element size, but GL has bytes regardless of element
192 * size which would only leave us with minimum alignment restriction of 16
193 * which doesn't make much sense if the type isn't 4x32bit). Due to
194 * translation of offsets to first_elem in sampler_views it actually seems
195 * gallium could not do anything else except 16 no matter what...
196 */
197 if (!aligned) {
198 LLVMSetAlignment(res, 1);
199 } else if (!util_is_power_of_two_or_zero(src_width)) {
200 /*
201 * Full alignment is impossible, assume the caller really meant
202 * the individual elements were aligned (e.g. 3x32bit format).
203 * And yes the generated code may otherwise crash, llvm will
204 * really assume 128bit alignment with a 96bit fetch (I suppose
205 * that makes sense as it can just assume the upper 32bit to be
206 * whatever).
207 * Maybe the caller should be able to explicitly set this, but
208 * this should cover all the 3-channel formats.
209 */
210 if (((src_width / 24) * 24 == src_width) &&
211 util_is_power_of_two_or_zero(src_width / 24)) {
212 LLVMSetAlignment(res, src_width / 24);
213 } else {
214 LLVMSetAlignment(res, 1);
215 }
216 }
217
218 assert(src_width <= dst_type.width * dst_type.length);
219 if (src_width < dst_type.width * dst_type.length) {
220 if (dst_type.length > 1) {
221 res = lp_build_pad_vector(gallivm, res, dst_type.length);
222 /*
223 * vector_justify hopefully a non-issue since we only deal
224 * with src_width >= 32 here?
225 */
226 } else {
227 LLVMTypeRef dst_elem_type = lp_build_vec_type(gallivm, dst_type);
228
229 /*
230 * Only valid if src_ptr_type is int type...
231 */
232 res = LLVMBuildZExt(gallivm->builder, res, dst_elem_type, "");
233
234 #if UTIL_ARCH_BIG_ENDIAN
235 if (vector_justify) {
236 res = LLVMBuildShl(gallivm->builder, res,
237 LLVMConstInt(dst_elem_type,
238 dst_type.width - src_width, 0), "");
239 }
240 if (src_width == 48) {
241 /* Load 3x16 bit vector.
242 * The sequence of loads on big-endian hardware proceeds as follows.
243 * 16-bit fields are denoted by X, Y, Z, and 0. In memory, the sequence
244 * of three fields appears in the order X, Y, Z.
245 *
246 * Load 32-bit word: 0.0.X.Y
247 * Load 16-bit halfword: 0.0.0.Z
248 * Rotate left: 0.X.Y.0
249 * Bitwise OR: 0.X.Y.Z
250 *
251 * The order in which we need the fields in the result is 0.Z.Y.X,
252 * the same as on little-endian; permute 16-bit fields accordingly
253 * within 64-bit register:
254 */
255 LLVMValueRef shuffles[4] = {
256 lp_build_const_int32(gallivm, 2),
257 lp_build_const_int32(gallivm, 1),
258 lp_build_const_int32(gallivm, 0),
259 lp_build_const_int32(gallivm, 3),
260 };
261 res = LLVMBuildBitCast(gallivm->builder, res,
262 lp_build_vec_type(gallivm, lp_type_uint_vec(16, 4*16)), "");
263 res = LLVMBuildShuffleVector(gallivm->builder, res, res, LLVMConstVector(shuffles, 4), "");
264 res = LLVMBuildBitCast(gallivm->builder, res, dst_elem_type, "");
265 }
266 #endif
267 }
268 }
269 return res;
270 }
271
272
273
274
275 static LLVMValueRef
276 lp_build_gather_avx2(struct gallivm_state *gallivm,
277 unsigned length,
278 unsigned src_width,
279 struct lp_type dst_type,
280 LLVMValueRef base_ptr,
281 LLVMValueRef offsets)
282 {
283 LLVMBuilderRef builder = gallivm->builder;
284 LLVMTypeRef src_type, src_vec_type;
285 LLVMValueRef res;
286 struct lp_type res_type = dst_type;
287 res_type.length *= length;
288
289 if (dst_type.floating) {
290 src_type = src_width == 64 ? LLVMDoubleTypeInContext(gallivm->context) :
291 LLVMFloatTypeInContext(gallivm->context);
292 } else {
293 src_type = LLVMIntTypeInContext(gallivm->context, src_width);
294 }
295 src_vec_type = LLVMVectorType(src_type, length);
296
297 /* XXX should allow hw scaling (can handle i8, i16, i32, i64 for x86) */
298 assert(LLVMTypeOf(base_ptr) == LLVMPointerType(LLVMInt8TypeInContext(gallivm->context), 0));
299
300 if (0) {
301 /*
302 * XXX: This will cause LLVM pre 3.7 to hang; it works on LLVM 3.8 but
303 * will not use the AVX2 gather instrinsics (even with llvm 4.0), at
304 * least with Haswell. See
305 * http://lists.llvm.org/pipermail/llvm-dev/2016-January/094448.html
306 * And the generated code doing the emulation is quite a bit worse
307 * than what we get by doing it ourselves too.
308 */
309 LLVMTypeRef i32_type = LLVMIntTypeInContext(gallivm->context, 32);
310 LLVMTypeRef i32_vec_type = LLVMVectorType(i32_type, length);
311 LLVMTypeRef i1_type = LLVMIntTypeInContext(gallivm->context, 1);
312 LLVMTypeRef i1_vec_type = LLVMVectorType(i1_type, length);
313 LLVMTypeRef src_ptr_type = LLVMPointerType(src_type, 0);
314 LLVMValueRef src_ptr;
315
316 base_ptr = LLVMBuildBitCast(builder, base_ptr, src_ptr_type, "");
317
318 /* Rescale offsets from bytes to elements */
319 LLVMValueRef scale = LLVMConstInt(i32_type, src_width/8, 0);
320 scale = lp_build_broadcast(gallivm, i32_vec_type, scale);
321 assert(LLVMTypeOf(offsets) == i32_vec_type);
322 offsets = LLVMBuildSDiv(builder, offsets, scale, "");
323
324 src_ptr = LLVMBuildGEP2(builder, src_type, base_ptr, &offsets, 1, "vector-gep");
325
326 char intrinsic[64];
327 snprintf(intrinsic, sizeof intrinsic, "llvm.masked.gather.v%u%s%u",
328 length, dst_type.floating ? "f" : "i", src_width);
329 LLVMValueRef alignment = LLVMConstInt(i32_type, src_width/8, 0);
330 LLVMValueRef mask = LLVMConstAllOnes(i1_vec_type);
331 LLVMValueRef passthru = LLVMGetUndef(src_vec_type);
332
333 LLVMValueRef args[] = { src_ptr, alignment, mask, passthru };
334
335 res = lp_build_intrinsic(builder, intrinsic, src_vec_type, args, 4, 0);
336 } else {
337 LLVMTypeRef i8_type = LLVMIntTypeInContext(gallivm->context, 8);
338 const char *intrinsic = NULL;
339 unsigned l_idx = 0;
340
341 assert(src_width == 32 || src_width == 64);
342 if (src_width == 32) {
343 assert(length == 4 || length == 8);
344 } else {
345 assert(length == 2 || length == 4);
346 }
347
348 static const char *intrinsics[2][2][2] = {
349
350 {{"llvm.x86.avx2.gather.d.d",
351 "llvm.x86.avx2.gather.d.d.256"},
352 {"llvm.x86.avx2.gather.d.q",
353 "llvm.x86.avx2.gather.d.q.256"}},
354
355 {{"llvm.x86.avx2.gather.d.ps",
356 "llvm.x86.avx2.gather.d.ps.256"},
357 {"llvm.x86.avx2.gather.d.pd",
358 "llvm.x86.avx2.gather.d.pd.256"}},
359 };
360
361 if ((src_width == 32 && length == 8) ||
362 (src_width == 64 && length == 4)) {
363 l_idx = 1;
364 }
365 intrinsic = intrinsics[dst_type.floating][src_width == 64][l_idx];
366
367 LLVMValueRef passthru = LLVMGetUndef(src_vec_type);
368 LLVMValueRef mask = LLVMConstAllOnes(src_vec_type);
369 mask = LLVMConstBitCast(mask, src_vec_type);
370 LLVMValueRef scale = LLVMConstInt(i8_type, 1, 0);
371
372 LLVMValueRef args[] = { passthru, base_ptr, offsets, mask, scale };
373
374 res = lp_build_intrinsic(builder, intrinsic, src_vec_type, args, 5, 0);
375 }
376 res = LLVMBuildBitCast(builder, res, lp_build_vec_type(gallivm, res_type), "");
377
378 return res;
379 }
380
381
382 /**
383 * Gather elements from scatter positions in memory into a single vector.
384 * Use for fetching texels from a texture.
385 * For SSE, typical values are length=4, src_width=32, dst_width=32.
386 *
387 * When src_width < dst_width, the return value can be justified in
388 * one of two ways:
389 * "integer justification" is used when the caller treats the destination
390 * as a packed integer bitmask, as described by the channels' "shift" and
391 * "width" fields;
392 * "vector justification" is used when the caller casts the destination
393 * to a vector and needs channel X to be in vector element 0.
394 *
395 * @param length length of the offsets
396 * @param src_width src element width in bits
397 * @param dst_type result element type (src will be expanded to fit,
398 * but truncation is not allowed)
399 * (this may be a vector, must be pot sized)
400 * @param aligned whether the data is guaranteed to be aligned (to src_width)
401 * @param base_ptr base pointer, needs to be a i8 pointer type.
402 * @param offsets vector with offsets
403 * @param vector_justify select vector rather than integer justification
404 */
405 LLVMValueRef
406 lp_build_gather(struct gallivm_state *gallivm,
407 unsigned length,
408 unsigned src_width,
409 struct lp_type dst_type,
410 bool aligned,
411 LLVMValueRef base_ptr,
412 LLVMValueRef offsets,
413 bool vector_justify)
414 {
415 LLVMValueRef res;
416 bool need_expansion = src_width < dst_type.width * dst_type.length;
417 bool vec_fetch;
418 struct lp_type fetch_type, fetch_dst_type;
419 LLVMTypeRef src_type;
420
421 assert(src_width <= dst_type.width * dst_type.length);
422
423 /*
424 * This is quite a mess...
425 * Figure out if the fetch should be done as:
426 * a) scalar or vector
427 * b) float or int
428 *
429 * As an example, for a 96bit fetch expanded into 4x32bit, it is better
430 * to use (3x32bit) vector type (then pad the vector). Otherwise, the
431 * zext will cause extra instructions.
432 * However, the same isn't true for 3x16bit (the codegen for that is
433 * completely worthless on x86 simd, and for 3x8bit is is way worse
434 * still, don't try that... (To get really good code out of llvm for
435 * these cases, the only way is to decompose the fetches manually
436 * into 1x32bit/1x16bit, or 1x16/1x8bit respectively, although the latter
437 * case requires sse41, otherwise simple scalar zext is way better.
438 * But probably not important enough, so don't bother.)
439 * Also, we try to honor the floating bit of destination (but isn't
440 * possible if caller asks for instance for 2x32bit dst_type with
441 * 48bit fetch - the idea would be to use 3x16bit fetch, pad and
442 * cast to 2x32f type, so the fetch is always int and on top of that
443 * we avoid the vec pad and use scalar zext due the above mentioned
444 * issue).
445 * Note this is optimized for x86 sse2 and up backend. Could be tweaked
446 * for other archs if necessary...
447 */
448 if (((src_width % 32) == 0) && ((src_width % dst_type.width) == 0) &&
449 (dst_type.length > 1)) {
450 /* use vector fetch (if dst_type is vector) */
451 vec_fetch = true;
452 if (dst_type.floating) {
453 fetch_type = lp_type_float_vec(dst_type.width, src_width);
454 } else {
455 fetch_type = lp_type_int_vec(dst_type.width, src_width);
456 }
457 /* intentionally not using lp_build_vec_type here */
458 src_type = LLVMVectorType(lp_build_elem_type(gallivm, fetch_type),
459 fetch_type.length);
460 fetch_dst_type = fetch_type;
461 fetch_dst_type.length = dst_type.length;
462 } else {
463 /* use scalar fetch */
464 vec_fetch = false;
465 if (dst_type.floating && ((src_width == 32) || (src_width == 64))) {
466 fetch_type = lp_type_float(src_width);
467 } else {
468 fetch_type = lp_type_int(src_width);
469 }
470 src_type = lp_build_vec_type(gallivm, fetch_type);
471 fetch_dst_type = fetch_type;
472 fetch_dst_type.width = dst_type.width * dst_type.length;
473 }
474
475 if (length == 1) {
476 /* Scalar */
477 res = lp_build_gather_elem_vec(gallivm, length,
478 src_width, src_type, fetch_dst_type,
479 aligned, base_ptr, offsets, 0,
480 vector_justify);
481 return LLVMBuildBitCast(gallivm->builder, res,
482 lp_build_vec_type(gallivm, dst_type), "");
483 /*
484 * Excluding expansion from these paths because if you need it for
485 * 32bit/64bit fetches you're doing it wrong (this is gather, not
486 * conversion) and it would be awkward for floats.
487 */
488 } else if (util_get_cpu_caps()->has_avx2 && !need_expansion &&
489 src_width == 32 && (length == 4 || length == 8)) {
490 return lp_build_gather_avx2(gallivm, length, src_width, dst_type,
491 base_ptr, offsets);
492 /*
493 * This looks bad on paper wrt throughtput/latency on Haswell.
494 * Even on Broadwell it doesn't look stellar.
495 * Albeit no measurements were done (but tested to work).
496 * Should definitely enable on Skylake.
497 * (In general, should be more of a win if the fetch is 256bit wide -
498 * this is true for the 32bit case above too.)
499 */
500 } else if (0 && util_get_cpu_caps()->has_avx2 && !need_expansion &&
501 src_width == 64 && (length == 2 || length == 4)) {
502 return lp_build_gather_avx2(gallivm, length, src_width, dst_type,
503 base_ptr, offsets);
504 } else {
505 /* Vector */
506
507 LLVMValueRef elems[LP_MAX_VECTOR_WIDTH / 8];
508 unsigned i;
509 bool vec_zext = false;
510 struct lp_type res_type, gather_res_type;
511 LLVMTypeRef res_t, gather_res_t;
512
513 res_type = fetch_dst_type;
514 res_type.length *= length;
515 gather_res_type = res_type;
516
517 if (src_width == 16 && dst_type.width == 32 && dst_type.length == 1) {
518 /*
519 * Note that llvm is never able to optimize zext/insert combos
520 * directly (i.e. zero the simd reg, then place the elements into
521 * the appropriate place directly). (I think this has to do with
522 * scalar/vector transition.) And scalar 16->32bit zext simd loads
523 * aren't possible (instead loading to scalar reg first).
524 * No idea about other archs...
525 * We could do this manually, but instead we just use a vector
526 * zext, which is simple enough (and, in fact, llvm might optimize
527 * this away).
528 * (We're not trying that with other bit widths as that might not be
529 * easier, in particular with 8 bit values at least with only sse2.)
530 */
531 assert(vec_fetch == false);
532 gather_res_type.width /= 2;
533 fetch_dst_type = fetch_type;
534 src_type = lp_build_vec_type(gallivm, fetch_type);
535 vec_zext = true;
536 }
537 res_t = lp_build_vec_type(gallivm, res_type);
538 gather_res_t = lp_build_vec_type(gallivm, gather_res_type);
539 res = LLVMGetUndef(gather_res_t);
540 for (i = 0; i < length; ++i) {
541 LLVMValueRef index = lp_build_const_int32(gallivm, i);
542 elems[i] = lp_build_gather_elem_vec(gallivm, length,
543 src_width, src_type, fetch_dst_type,
544 aligned, base_ptr, offsets, i,
545 vector_justify);
546 if (!vec_fetch) {
547 res = LLVMBuildInsertElement(gallivm->builder, res, elems[i], index, "");
548 }
549 }
550 if (vec_zext) {
551 res = LLVMBuildZExt(gallivm->builder, res, res_t, "");
552 if (vector_justify) {
553 #if UTIL_ARCH_BIG_ENDIAN
554 unsigned sv = dst_type.width - src_width;
555 res = LLVMBuildShl(gallivm->builder, res,
556 lp_build_const_int_vec(gallivm, res_type, sv), "");
557 #endif
558 }
559 }
560 if (vec_fetch) {
561 /*
562 * Do bitcast now otherwise llvm might get some funny ideas wrt
563 * float/int types...
564 */
565 for (i = 0; i < length; i++) {
566 elems[i] = LLVMBuildBitCast(gallivm->builder, elems[i],
567 lp_build_vec_type(gallivm, dst_type), "");
568 }
569 res = lp_build_concat(gallivm, elems, dst_type, length);
570 } else {
571 struct lp_type really_final_type = dst_type;
572 assert(res_type.length * res_type.width ==
573 dst_type.length * dst_type.width * length);
574 really_final_type.length *= length;
575 res = LLVMBuildBitCast(gallivm->builder, res,
576 lp_build_vec_type(gallivm, really_final_type), "");
577 }
578 }
579
580 return res;
581 }
582
583 LLVMValueRef
584 lp_build_gather_values(struct gallivm_state * gallivm,
585 LLVMValueRef * values,
586 unsigned value_count)
587 {
588 LLVMTypeRef vec_type = LLVMVectorType(LLVMTypeOf(values[0]), value_count);
589 LLVMBuilderRef builder = gallivm->builder;
590 LLVMValueRef vec = LLVMGetUndef(vec_type);
591 unsigned i;
592
593 for (i = 0; i < value_count; i++) {
594 LLVMValueRef index = lp_build_const_int32(gallivm, i);
595 vec = LLVMBuildInsertElement(builder, vec, values[i], index, "");
596 }
597 return vec;
598 }
599
600
601 /**
602 * `lp_build_gather_array` has the same function as `lp_build_gather_values`,
603 * but gathers values into an Array instead of a Vector. This function can be
604 * used for values that are not valid Vector element types.
605 */
606 LLVMValueRef
607 lp_build_gather_array(struct gallivm_state * gallivm,
608 LLVMValueRef * values,
609 unsigned value_count)
610 {
611 LLVMTypeRef arr_type = LLVMArrayType(LLVMTypeOf(values[0]), value_count);
612 LLVMBuilderRef builder = gallivm->builder;
613 LLVMValueRef arr = LLVMGetUndef(arr_type);
614 unsigned i;
615
616 for (i = 0; i < value_count; i++) {
617 arr = LLVMBuildInsertValue(builder, arr, values[i], i, "");
618 }
619 return arr;
620 }
621
622 static LLVMAttributeRef
623 create_align_attr(LLVMContextRef context, uint64_t align_bytes) {
624 unsigned kind_id = LLVMGetEnumAttributeKindForName("align", 5);
625 return LLVMCreateEnumAttribute(context, kind_id, align_bytes);
626 }
627
628 LLVMValueRef
629 lp_build_masked_gather(struct gallivm_state *gallivm,
630 unsigned length,
631 unsigned bit_size,
632 LLVMTypeRef vec_type,
633 LLVMValueRef offset_ptr,
634 LLVMValueRef exec_mask)
635 {
636 LLVMBuilderRef builder = gallivm->builder;
637 LLVMValueRef args[4];
638 char intrin_name[64];
639 int arg_count = 0;
640 #if LLVM_VERSION_MAJOR >= 16
641 snprintf(intrin_name, 64, "llvm.masked.gather.v%ui%u.v%up0",
642 length, bit_size, length);
643 #else
644 snprintf(intrin_name, 64, "llvm.masked.gather.v%ui%u.v%up0i%u",
645 length, bit_size, length, bit_size);
646 #endif
647
648 args[arg_count++] = offset_ptr;
649 #if LLVM_VERSION_MAJOR < 22
650 args[arg_count++] = lp_build_const_int32(gallivm, bit_size / 8);
651 #endif
652 args[arg_count++] = LLVMBuildICmp(builder, LLVMIntNE, exec_mask,
653 LLVMConstNull(LLVMTypeOf(exec_mask)), "");
654 args[arg_count++] = LLVMConstNull(vec_type);
655
656 LLVMValueRef func = lp_build_intrinsic(builder, intrin_name, vec_type,
657 args, arg_count, 0);
658
659 #if LLVM_VERSION_MAJOR >= 22
660 LLVMAttributeRef AlignAttr = create_align_attr(gallivm->context, bit_size / 8);
661 LLVMAddCallSiteAttribute(func, 1, AlignAttr);
662 #endif
663 return func;
664
665 }
666
667 void
668 lp_build_masked_scatter(struct gallivm_state *gallivm,
669 unsigned length,
670 unsigned bit_size,
671 LLVMValueRef offset_ptr,
672 LLVMValueRef value_vec,
673 LLVMValueRef exec_mask)
674 {
675 LLVMBuilderRef builder = gallivm->builder;
676 LLVMValueRef args[4];
677 char intrin_name[64];
678 int arg_count = 0;
679 #if LLVM_VERSION_MAJOR >= 16
680 snprintf(intrin_name, 64, "llvm.masked.scatter.v%ui%u.v%up0",
681 length, bit_size, length);
682 #else
683 snprintf(intrin_name, 64, "llvm.masked.scatter.v%ui%u.v%up0i%u",
684 length, bit_size, length, bit_size);
685 #endif
686
687 args[arg_count++] = value_vec;
688 args[arg_count++] = offset_ptr;
689 #if LLVM_VERSION_MAJOR < 22
690 args[arg_count++] = lp_build_const_int32(gallivm, bit_size / 8);
691 #endif
692
693 args[arg_count++] = LLVMBuildICmp(builder, LLVMIntNE, exec_mask,
694 LLVMConstNull(LLVMTypeOf(exec_mask)), "");
695 LLVMValueRef func = lp_build_intrinsic(builder, intrin_name, LLVMVoidTypeInContext(gallivm->context),
696 args, arg_count, 0);
697 #if LLVM_VERSION_MAJOR >= 22
698 LLVMAttributeRef AlignAttr = create_align_attr(gallivm->context, bit_size / 8);
699 LLVMAddCallSiteAttribute(func, 2, AlignAttr);
700 #else
701 (void)func;
702 #endif
703 }