Skip to content

Commit e1f3a03

Browse files
authored
[CIR][CIRGen][Builtin][X86] Lower palignr intrinsics (#1897)
1 parent 5a94ee4 commit e1f3a03

File tree

5 files changed

+113
-2
lines changed

5 files changed

+113
-2
lines changed

clang/lib/CIR/CodeGen/CIRGenBuiltinX86.cpp

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1279,8 +1279,42 @@ mlir::Value CIRGenFunction::emitX86BuiltinExpr(unsigned BuiltinID,
12791279
llvm_unreachable("permdi NYI");
12801280
case X86::BI__builtin_ia32_palignr128:
12811281
case X86::BI__builtin_ia32_palignr256:
1282-
case X86::BI__builtin_ia32_palignr512:
1283-
llvm_unreachable("palignr NYI");
1282+
case X86::BI__builtin_ia32_palignr512: {
1283+
unsigned shiftVal =
1284+
Ops[2].getDefiningOp<cir::ConstantOp>().getIntValue().getZExtValue() &
1285+
0xff;
1286+
1287+
unsigned numElts = cast<cir::VectorType>(Ops[0].getType()).getSize();
1288+
assert(numElts % 16 == 0);
1289+
1290+
// If palignr is shifting the pair of vectors more than the size of two
1291+
// lanes, emit zero.
1292+
if (shiftVal >= 32)
1293+
return builder.getNullValue(convertType(E->getType()),
1294+
getLoc(E->getExprLoc()));
1295+
1296+
// If palignr is shifting the pair of input vectors more than one lane,
1297+
// but less than two lanes, convert to shifting in zeroes.
1298+
if (shiftVal > 16) {
1299+
shiftVal -= 16;
1300+
Ops[1] = Ops[0];
1301+
Ops[0] = builder.getNullValue(Ops[0].getType(), getLoc(E->getExprLoc()));
1302+
}
1303+
1304+
int64_t indices[64];
1305+
// 256-bit palignr operates on 128-bit lanes so we need to handle that
1306+
for (unsigned l = 0; l != numElts; l += 16) {
1307+
for (unsigned i = 0; i != 16; ++i) {
1308+
unsigned idx = shiftVal + i;
1309+
if (idx >= 16)
1310+
idx += numElts - 16; // End of lane, switch operand.
1311+
indices[l + i] = idx + l;
1312+
}
1313+
}
1314+
1315+
return builder.createVecShuffle(getLoc(E->getExprLoc()), Ops[1], Ops[0],
1316+
ArrayRef(indices, numElts));
1317+
}
12841318
case X86::BI__builtin_ia32_alignd128:
12851319
case X86::BI__builtin_ia32_alignd256:
12861320
case X86::BI__builtin_ia32_alignd512:

clang/test/CIR/CodeGen/X86/avx2-builtins.c

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,3 +131,15 @@ __m256i test_mm256_shufflehi_epi16(__m256i a) {
131131
// OGCG: shufflevector <16 x i16> %{{.*}}, <16 x i16> poison, <16 x i32> <i32 0, i32 1, i32 2, i32 3, i32 7, i32 6, i32 6, i32 5, i32 8, i32 9, i32 10, i32 11, i32 15, i32 14, i32 14, i32 13>
132132
return _mm256_shufflehi_epi16(a, 107);
133133
}
134+
135+
__m256i test_mm256_alignr_epi8(__m256i a, __m256i b) {
136+
// CIR-LABEL: test_mm256_alignr_epi8
137+
// CIR: %{{.*}} = cir.vec.shuffle(%{{.*}}, %{{.*}} : !cir.vector<{{!s8i|!u8i}} x 32>) [#cir.int<2> : !s32i, #cir.int<3> : !s32i, #cir.int<4> : !s32i, #cir.int<5> : !s32i, #cir.int<6> : !s32i, #cir.int<7> : !s32i, #cir.int<8> : !s32i, #cir.int<9> : !s32i, #cir.int<10> : !s32i, #cir.int<11> : !s32i, #cir.int<12> : !s32i, #cir.int<13> : !s32i, #cir.int<14> : !s32i, #cir.int<15> : !s32i, #cir.int<32> : !s32i, #cir.int<33> : !s32i, #cir.int<18> : !s32i, #cir.int<19> : !s32i, #cir.int<20> : !s32i, #cir.int<21> : !s32i, #cir.int<22> : !s32i, #cir.int<23> : !s32i, #cir.int<24> : !s32i, #cir.int<25> : !s32i, #cir.int<26> : !s32i, #cir.int<27> : !s32i, #cir.int<28> : !s32i, #cir.int<29> : !s32i, #cir.int<30> : !s32i, #cir.int<31> : !s32i, #cir.int<48> : !s32i, #cir.int<49> : !s32i] : !cir.vector<{{!s8i|!u8i}} x 32>
138+
139+
// LLVM-LABEL: test_mm256_alignr_epi8
140+
// LLVM: shufflevector <32 x i8> %{{.*}}, <32 x i8> %{{.*}}, <32 x i32> <i32 2, i32 3, i32 4, i32 5, i32 6, i32 7, i32 8, i32 9, i32 10, i32 11, i32 12, i32 13, i32 14, i32 15, i32 32, i32 33, i32 18, i32 19, i32 20, i32 21, i32 22, i32 23, i32 24, i32 25, i32 26, i32 27, i32 28, i32 29, i32 30, i32 31, i32 48, i32 49>
141+
142+
// OGCG-LABEL: test_mm256_alignr_epi8
143+
// OGCG: shufflevector <32 x i8> %{{.*}}, <32 x i8> %{{.*}}, <32 x i32> <i32 2, i32 3, i32 4, i32 5, i32 6, i32 7, i32 8, i32 9, i32 10, i32 11, i32 12, i32 13, i32 14, i32 15, i32 32, i32 33, i32 18, i32 19, i32 20, i32 21, i32 22, i32 23, i32 24, i32 25, i32 26, i32 27, i32 28, i32 29, i32 30, i32 31, i32 48, i32 49>
144+
return _mm256_alignr_epi8(a, b, 2);
145+
}

clang/test/CIR/CodeGen/X86/avx512bw-builtins.c

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,3 +131,15 @@ __m512i test_mm512_shufflehi_epi16(__m512i __A) {
131131
// OGCG: shufflevector <32 x i16> %{{.*}}, <32 x i16> poison, <32 x i32> <i32 0, i32 1, i32 2, i32 3, i32 5, i32 5, i32 4, i32 4, i32 8, i32 9, i32 10, i32 11, i32 13, i32 13, i32 12, i32 12, i32 16, i32 17, i32 18, i32 19, i32 21, i32 21, i32 20, i32 20, i32 24, i32 25, i32 26, i32 27, i32 29, i32 29, i32 28, i32 28>
132132
return _mm512_shufflehi_epi16(__A, 5);
133133
}
134+
135+
__m512i test_mm512_alignr_epi8(__m512i __A,__m512i __B){
136+
// CIR-LABEL: _mm512_alignr_epi8
137+
// CIR: %{{.*}} = cir.vec.shuffle(%{{.*}}, %{{.*}} : !cir.vector<{{!s8i|!u8i}} x 64>) [#cir.int<2> : !s32i, #cir.int<3> : !s32i, #cir.int<4> : !s32i, #cir.int<5> : !s32i, #cir.int<6> : !s32i, #cir.int<7> : !s32i, #cir.int<8> : !s32i, #cir.int<9> : !s32i, #cir.int<10> : !s32i, #cir.int<11> : !s32i, #cir.int<12> : !s32i, #cir.int<13> : !s32i, #cir.int<14> : !s32i, #cir.int<15> : !s32i, #cir.int<64> : !s32i, #cir.int<65> : !s32i, #cir.int<18> : !s32i, #cir.int<19> : !s32i, #cir.int<20> : !s32i, #cir.int<21> : !s32i, #cir.int<22> : !s32i, #cir.int<23> : !s32i, #cir.int<24> : !s32i, #cir.int<25> : !s32i, #cir.int<26> : !s32i, #cir.int<27> : !s32i, #cir.int<28> : !s32i, #cir.int<29> : !s32i, #cir.int<30> : !s32i, #cir.int<31> : !s32i, #cir.int<80> : !s32i, #cir.int<81> : !s32i, #cir.int<34> : !s32i, #cir.int<35> : !s32i, #cir.int<36> : !s32i, #cir.int<37> : !s32i, #cir.int<38> : !s32i, #cir.int<39> : !s32i, #cir.int<40> : !s32i, #cir.int<41> : !s32i, #cir.int<42> : !s32i, #cir.int<43> : !s32i, #cir.int<44> : !s32i, #cir.int<45> : !s32i, #cir.int<46> : !s32i, #cir.int<47> : !s32i, #cir.int<96> : !s32i, #cir.int<97> : !s32i, #cir.int<50> : !s32i, #cir.int<51> : !s32i, #cir.int<52> : !s32i, #cir.int<53> : !s32i, #cir.int<54> : !s32i, #cir.int<55> : !s32i, #cir.int<56> : !s32i, #cir.int<57> : !s32i, #cir.int<58> : !s32i, #cir.int<59> : !s32i, #cir.int<60> : !s32i, #cir.int<61> : !s32i, #cir.int<62> : !s32i, #cir.int<63> : !s32i, #cir.int<112> : !s32i, #cir.int<113> : !s32i] : !cir.vector<{{!s8i|!u8i}} x 64>
138+
139+
// LLVM-LABEL: @test_mm512_alignr_epi8
140+
// LLVM: shufflevector <64 x i8> %{{.*}}, <64 x i8> %{{.*}}, <64 x i32> <i32 2, i32 3, i32 4, i32 5, i32 6, i32 7, i32 8, i32 9, i32 10, i32 11, i32 12, i32 13, i32 14, i32 15, i32 64, i32 65, i32 18, i32 19, i32 20, i32 21, i32 22, i32 23, i32 24, i32 25, i32 26, i32 27, i32 28, i32 29, i32 30, i32 31, i32 80, i32 81, i32 34, i32 35, i32 36, i32 37, i32 38, i32 39, i32 40, i32 41, i32 42, i32 43, i32 44, i32 45, i32 46, i32 47, i32 96, i32 97, i32 50, i32 51, i32 52, i32 53, i32 54, i32 55, i32 56, i32 57, i32 58, i32 59, i32 60, i32 61, i32 62, i32 63, i32 112, i32 113>
141+
142+
// OGCG-LABEL: @test_mm512_alignr_epi8
143+
// OGCG: shufflevector <64 x i8> %{{.*}}, <64 x i8> %{{.*}}, <64 x i32> <i32 2, i32 3, i32 4, i32 5, i32 6, i32 7, i32 8, i32 9, i32 10, i32 11, i32 12, i32 13, i32 14, i32 15, i32 64, i32 65, i32 18, i32 19, i32 20, i32 21, i32 22, i32 23, i32 24, i32 25, i32 26, i32 27, i32 28, i32 29, i32 30, i32 31, i32 80, i32 81, i32 34, i32 35, i32 36, i32 37, i32 38, i32 39, i32 40, i32 41, i32 42, i32 43, i32 44, i32 45, i32 46, i32 47, i32 96, i32 97, i32 50, i32 51, i32 52, i32 53, i32 54, i32 55, i32 56, i32 57, i32 58, i32 59, i32 60, i32 61, i32 62, i32 63, i32 112, i32 113>
144+
return _mm512_alignr_epi8(__A, __B, 2);
145+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
// RUN: %clang_cc1 %s -triple=x86_64-unknown-linux -target-feature +ssse3 -fclangir -emit-llvm -o %t.ll
2+
// RUN: FileCheck --input-file=%t.ll %s --check-prefix=CIR
3+
4+
// RUN: %clang_cc1 %s -triple=x86_64-unknown-linux -target-feature +ssse3 -emit-llvm -o %t_og.ll
5+
// RUN: FileCheck --input-file=%t_og.ll %s --check-prefix=OGCG
6+
7+
#define _mm_alignr_epi8(a, b, n) (__builtin_ia32_palignr128((a), (b), (n)))
8+
typedef __attribute__((vector_size(16))) int int4;
9+
10+
// CIR-LABEL: @align1
11+
// CIR: shufflevector <16 x i8> %{{.*}}, <16 x i8> %{{.*}}, <16 x i32> <i32 15, i32 16, i32 17
12+
// OGCG-LABEL: @align1
13+
// OGCG: %palignr = shufflevector <16 x i8> %{{.*}}, <16 x i8> %{{.*}}, <16 x i32> <i32 15, i32 16, i32 17
14+
int4 align1(int4 a, int4 b) { return _mm_alignr_epi8(a, b, 15); }
15+
16+
// CIR-LABEL: @align2
17+
// CIR: shufflevector <16 x i8> %{{.*}}, <16 x i8> %{{.*}}, <16 x i32> <i32 16, i32 17, i32 18
18+
// OGCG-LABEL: @align2
19+
// OGCG: %palignr = shufflevector <16 x i8> %{{.*}}, <16 x i8> %{{.*}}, <16 x i32> <i32 16, i32 17, i32 18
20+
int4 align2(int4 a, int4 b) { return _mm_alignr_epi8(a, b, 16); }
21+
22+
// CIR-LABEL: @align3
23+
// CIR: shufflevector <16 x i8> %{{.*}}, <16 x i8> zeroinitializer, <16 x i32> <i32 1, i32 2, i32 3
24+
// OGCG-LABEL: @align3
25+
// OGCG: %palignr = shufflevector <16 x i8> %{{.*}}, <16 x i8> zeroinitializer, <16 x i32> <i32 1, i32 2, i32 3
26+
int4 align3(int4 a, int4 b) { return _mm_alignr_epi8(a, b, 17); }
27+
28+
// CIR-LABEL: @align4
29+
// CIR: store <4 x i32> zeroinitializer, ptr %{{.*}}, align 16
30+
// OGCG-LABEL: @align4
31+
// OGCG: ret <4 x i32> zeroinitializer
32+
int4 align4(int4 a, int4 b) { return _mm_alignr_epi8(a, b, 32); }
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// RUN: %clang_cc1 -x c -flax-vector-conversions=none -ffreestanding %s -triple=x86_64-unknown-linux -target-feature +ssse3 -fclangir -emit-cir -o %t.cir -Wall -Werror
2+
// RUN: FileCheck --check-prefixes=CIR --input-file=%t.cir %s
3+
// RUN: %clang_cc1 -x c -flax-vector-conversions=none -ffreestanding %s -triple=x86_64-unknown-linux -target-feature +ssse3 -fclangir -emit-llvm -o %t.ll -Wall -Werror
4+
// RUN: FileCheck --check-prefixes=LLVM --input-file=%t.ll %s
5+
6+
// RUN: %clang_cc1 -x c -flax-vector-conversions=none -ffreestanding %s -triple=x86_64-apple-darwin -target-feature +ssse3 -emit-llvm -o - -Wall -Werror | FileCheck %s --check-prefixes=OGCG
7+
// RUN: %clang_cc1 -x c++ -flax-vector-conversions=none -ffreestanding %s -triple=x86_64-apple-darwin -target-feature +ssse3 -emit-llvm -o - -Wall -Werror | FileCheck %s --check-prefixes=OGCG
8+
9+
#include <immintrin.h>
10+
11+
__m128i test_mm_alignr_epi8(__m128i a, __m128i b) {
12+
// CIR-LABEL: _mm_alignr_epi8
13+
// CIR: %{{.*}} = cir.vec.shuffle(%{{.*}}, %{{.*}} : !cir.vector<!s8i x 16>) [#cir.int<2> : !s32i, #cir.int<3> : !s32i, #cir.int<4> : !s32i, #cir.int<5> : !s32i, #cir.int<6> : !s32i, #cir.int<7> : !s32i, #cir.int<8> : !s32i, #cir.int<9> : !s32i, #cir.int<10> : !s32i, #cir.int<11> : !s32i, #cir.int<12> : !s32i, #cir.int<13> : !s32i, #cir.int<14> : !s32i, #cir.int<15> : !s32i, #cir.int<16> : !s32i, #cir.int<17> : !s32i] : !cir.vector<!s8i x 16>
14+
15+
// LLVM-LABEL: test_mm_alignr_epi8
16+
// LLVM: shufflevector <16 x i8> %{{.*}}, <16 x i8> %{{.*}}, <16 x i32> <i32 2, i32 3, i32 4, i32 5, i32 6, i32 7, i32 8, i32 9, i32 10, i32 11, i32 12, i32 13, i32 14, i32 15, i32 16, i32 17>
17+
18+
// OGCG-LABEL: test_mm_alignr_epi8
19+
// OGCG: shufflevector <16 x i8> %{{.*}}, <16 x i8> %{{.*}}, <16 x i32> <i32 2, i32 3, i32 4, i32 5, i32 6, i32 7, i32 8, i32 9, i32 10, i32 11, i32 12, i32 13, i32 14, i32 15, i32 16, i32 17>
20+
return _mm_alignr_epi8(a, b, 2);
21+
}

0 commit comments

Comments
 (0)