Skip to content

Commit 04c46ba

Browse files
authored
[CIR] Backport fixsing ShuffleDynamicOp maskbits logic (#1649)
Backport the calculation of maskbits in the lowering from `N - 1` to `NextPowerOf2(numElements - 1) - 1`, similar to Clang CG. Backport from [#141411](llvm/llvm-project#141411)
1 parent 868a945 commit 04c46ba

File tree

2 files changed

+22
-7
lines changed

2 files changed

+22
-7
lines changed

clang/lib/CIR/Lowering/DirectToLLVM/LowerToLLVM.cpp

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2021,9 +2021,12 @@ mlir::LogicalResult CIRToLLVMVecShuffleDynamicOpLowering::matchAndRewrite(
20212021
// __builtin_shufflevector(V, I)
20222022
// is implemented as this pseudocode, where the for loop is unrolled
20232023
// and N is the number of elements:
2024-
// masked = I & (N-1)
2025-
// for (i in 0 <= i < N)
2026-
// result[i] = V[masked[i]]
2024+
//
2025+
// result = undef
2026+
// maskbits = NextPowerOf2(N - 1)
2027+
// masked = I & maskbits
2028+
// for (i in 0 <= i < N)
2029+
// result[i] = V[masked[i]]
20272030
auto loc = op.getLoc();
20282031
mlir::Value input = adaptor.getVec();
20292032
mlir::Type llvmIndexVecType =
@@ -2032,8 +2035,9 @@ mlir::LogicalResult CIRToLLVMVecShuffleDynamicOpLowering::matchAndRewrite(
20322035
elementTypeIfVector(op.getIndices().getType()));
20332036
uint64_t numElements =
20342037
mlir::cast<cir::VectorType>(op.getVec().getType()).getSize();
2035-
mlir::Value maskValue = rewriter.create<mlir::LLVM::ConstantOp>(
2036-
loc, llvmIndexType, numElements - 1);
2038+
uint64_t maskBits = llvm::NextPowerOf2(numElements - 1) - 1;
2039+
mlir::Value maskValue =
2040+
rewriter.create<mlir::LLVM::ConstantOp>(loc, llvmIndexType, maskBits);
20372041
mlir::Value maskVector =
20382042
rewriter.create<mlir::LLVM::UndefOp>(loc, llvmIndexVecType);
20392043
for (uint64_t i = 0; i < numElements; ++i) {

clang/test/CIR/CodeGen/vectype-ext.cpp

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
// RUN: FileCheck --input-file=%t.ll %s -check-prefix=LLVM
55

66
typedef int vi4 __attribute__((ext_vector_type(4)));
7+
typedef int vi6 __attribute__((ext_vector_type(6)));
78
typedef unsigned int uvi4 __attribute__((ext_vector_type(4)));
89
typedef int vi3 __attribute__((ext_vector_type(3)));
910
typedef int vi2 __attribute__((ext_vector_type(2)));
@@ -542,7 +543,17 @@ void vector_integers_shifts_test() {
542543
uvi4 b = {5u, 6u, 7u, 8u};
543544

544545
vi4 shl = a << b;
545-
// CHECK: %{{[0-9]+}} = cir.shift(left, %{{[0-9]+}} : !cir.vector<!s32i x 4>, %{{[0-9]+}} : !cir.vector<!u32i x 4>) -> !cir.vector<!s32i x 4>
546+
// CIR: %{{[0-9]+}} = cir.shift(left, %{{[0-9]+}} : !cir.vector<!s32i x 4>, %{{[0-9]+}} : !cir.vector<!u32i x 4>) -> !cir.vector<!s32i x 4>
546547
uvi4 shr = b >> a;
547-
// CHECK: %{{[0-9]+}} = cir.shift(right, %{{[0-9]+}} : !cir.vector<!u32i x 4>, %{{[0-9]+}} : !cir.vector<!s32i x 4>) -> !cir.vector<!u32i x 4>
548+
// CIR: %{{[0-9]+}} = cir.shift(right, %{{[0-9]+}} : !cir.vector<!u32i x 4>, %{{[0-9]+}} : !cir.vector<!s32i x 4>) -> !cir.vector<!u32i x 4>
549+
}
550+
551+
void vector_shuffle_dynamic_mask_test() {
552+
vi6 a;
553+
vi6 b;
554+
vi6 r = __builtin_shufflevector(a, b);
555+
556+
// CIR: %{{[0-9]+}} = cir.vec.shuffle.dynamic %{{[0-9]+}} : !cir.vector<!s32i x 6>, %{{[0-9]+}} : !cir.vector<!s32i x 6>
557+
558+
// LLVM: {{.*}} = and <6 x i32> {{.*}}, splat (i32 7)
548559
}

0 commit comments

Comments
 (0)