| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 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 |
|
| 43 |
|
| 44 |
|
| 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 |
|
| 75 |
|
| 76 |
|
| 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 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
if (!aligned) { |
| 118 |
LLVMSetAlignment(res, 1); |
| 119 |
} else if (!util_is_power_of_two_or_zero(src_width)) { |
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 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 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
|
| 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 |
|
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
if (!aligned) { |
| 198 |
LLVMSetAlignment(res, 1); |
| 199 |
} else if (!util_is_power_of_two_or_zero(src_width)) { |
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
|
| 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 |
|
| 224 |
|
| 225 |
|
| 226 |
} else { |
| 227 |
LLVMTypeRef dst_elem_type = lp_build_vec_type(gallivm, dst_type); |
| 228 |
|
| 229 |
|
| 230 |
|
| 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 |
|
| 242 |
|
| 243 |
|
| 244 |
|
| 245 |
|
| 246 |
|
| 247 |
|
| 248 |
|
| 249 |
|
| 250 |
|
| 251 |
|
| 252 |
|
| 253 |
|
| 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 |
|
| 298 |
assert(LLVMTypeOf(base_ptr) == LLVMPointerType(LLVMInt8TypeInContext(gallivm->context), 0)); |
| 299 |
|
| 300 |
if (0) { |
| 301 |
|
| 302 |
|
| 303 |
|
| 304 |
|
| 305 |
|
| 306 |
|
| 307 |
|
| 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 |
|
| 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 |
|
| 384 |
|
| 385 |
|
| 386 |
|
| 387 |
|
| 388 |
|
| 389 |
|
| 390 |
|
| 391 |
|
| 392 |
|
| 393 |
|
| 394 |
|
| 395 |
|
| 396 |
|
| 397 |
|
| 398 |
|
| 399 |
|
| 400 |
|
| 401 |
|
| 402 |
|
| 403 |
|
| 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 |
|
| 425 |
|
| 426 |
|
| 427 |
|
| 428 |
|
| 429 |
|
| 430 |
|
| 431 |
|
| 432 |
|
| 433 |
|
| 434 |
|
| 435 |
|
| 436 |
|
| 437 |
|
| 438 |
|
| 439 |
|
| 440 |
|
| 441 |
|
| 442 |
|
| 443 |
|
| 444 |
|
| 445 |
|
| 446 |
|
| 447 |
|
| 448 |
if (((src_width % 32) == 0) && ((src_width % dst_type.width) == 0) && |
| 449 |
(dst_type.length > 1)) { |
| 450 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 485 |
|
| 486 |
|
| 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 |
|
| 494 |
|
| 495 |
|
| 496 |
|
| 497 |
|
| 498 |
|
| 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 |
|
| 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 |
|
| 520 |
|
| 521 |
|
| 522 |
|
| 523 |
|
| 524 |
|
| 525 |
|
| 526 |
|
| 527 |
|
| 528 |
|
| 529 |
|
| 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 |
|
| 563 |
|
| 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 |
|
| 603 |
|
| 604 |
|
| 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 |
} |