Skip to content

Commit db31493

Browse files
committed
Merge remote-tracking branch 'origin/main' into cxx-ctor-dtor
2 parents d6c0f3a + 33c739f commit db31493

File tree

5 files changed

+146
-2
lines changed

5 files changed

+146
-2
lines changed

clang/lib/CIR/CodeGen/CIRGenBuiltinX86.cpp

Lines changed: 42 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,14 @@ translateX86ToMsvcIntrin(unsigned BuiltinID) {
6666
llvm_unreachable("must return from switch");
6767
}
6868

69+
/// Get integer from a mlir::Value that is an int constant or a constant op.
70+
static int64_t getIntValueFromConstOp(mlir::Value val) {
71+
auto constOp = mlir::cast<cir::ConstantOp>(val.getDefiningOp());
72+
return (mlir::cast<cir::IntAttr>(constOp.getValue()))
73+
.getValue()
74+
.getSExtValue();
75+
}
76+
6977
mlir::Value CIRGenFunction::emitX86BuiltinExpr(unsigned BuiltinID,
7078
const CallExpr *E) {
7179
if (BuiltinID == Builtin::BI__builtin_cpu_is)
@@ -96,7 +104,23 @@ mlir::Value CIRGenFunction::emitX86BuiltinExpr(unsigned BuiltinID,
96104
default:
97105
return nullptr;
98106
case X86::BI_mm_prefetch: {
99-
llvm_unreachable("_mm_prefetch NYI");
107+
mlir::Value Address = builder.createPtrBitcast(Ops[0], VoidTy);
108+
109+
int64_t Hint = getIntValueFromConstOp(Ops[1]);
110+
mlir::Value RW = builder.create<cir::ConstantOp>(
111+
getLoc(E->getExprLoc()),
112+
cir::IntAttr::get(SInt32Ty, (Hint >> 2) & 0x1));
113+
mlir::Value Locality = builder.create<cir::ConstantOp>(
114+
getLoc(E->getExprLoc()), cir::IntAttr::get(SInt32Ty, Hint & 0x3));
115+
mlir::Value Data = builder.create<cir::ConstantOp>(
116+
getLoc(E->getExprLoc()), cir::IntAttr::get(SInt32Ty, 1));
117+
mlir::Type voidTy = cir::VoidType::get(&getMLIRContext());
118+
119+
return builder
120+
.create<cir::LLVMIntrinsicCallOp>(
121+
getLoc(E->getExprLoc()), builder.getStringAttr("prefetch"), voidTy,
122+
mlir::ValueRange{Address, RW, Locality, Data})
123+
.getResult();
100124
}
101125
case X86::BI_mm_clflush: {
102126
mlir::Type voidTy = cir::VoidType::get(&getMLIRContext());
@@ -233,7 +257,23 @@ mlir::Value CIRGenFunction::emitX86BuiltinExpr(unsigned BuiltinID,
233257
case X86::BI__builtin_ia32_vec_set_v16hi:
234258
case X86::BI__builtin_ia32_vec_set_v8si:
235259
case X86::BI__builtin_ia32_vec_set_v4di: {
236-
llvm_unreachable("__builtin_ia32_vec_set_vXX NYI");
260+
unsigned NumElts = cast<cir::VectorType>(Ops[0].getType()).getSize();
261+
262+
auto constOp = cast<cir::ConstantOp>(Ops[2].getDefiningOp());
263+
auto intAttr = cast<cir::IntAttr>(constOp.getValue());
264+
uint64_t index = intAttr.getValue().getZExtValue();
265+
266+
index &= NumElts - 1;
267+
268+
auto indexAttr = cir::IntAttr::get(
269+
cir::IntType::get(&getMLIRContext(), 64, false), index);
270+
auto indexVal =
271+
builder.create<cir::ConstantOp>(getLoc(E->getExprLoc()), indexAttr);
272+
273+
// These builtins exist so we can ensure the index is an ICE and in range.
274+
// Otherwise we could just do this in the header file.
275+
return builder.create<cir::VecInsertOp>(getLoc(E->getExprLoc()), Ops[0],
276+
Ops[1], indexVal);
237277
}
238278
case X86::BI_mm_setcsr:
239279
case X86::BI__builtin_ia32_ldmxcsr: {

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

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,3 +58,45 @@ long long test_mm256_extract_epi64(__m256i A) {
5858
return _mm256_extract_epi64(A, 3);
5959
}
6060
#endif
61+
62+
__m256i test_mm256_insert_epi8(__m256i x, char b) {
63+
64+
// CIR-CHECK-LABEL: test_mm256_insert_epi8
65+
// CIR-CHECK-LABEL: {{%.*}} = cir.vec.insert {{%.*}}, {{%.*}}[{{%.*}} : {{!u32i|!u64i}}] : !cir.vector<{{!s8i|!u8i}} x 32>
66+
67+
// LLVM-CHECK-LABEL: test_mm256_insert_epi8
68+
// LLVM-CHECK: insertelement <32 x i8> %{{.*}}, i8 %{{.*}}, {{i32|i64}} 14
69+
return _mm256_insert_epi8(x, b, 14);
70+
}
71+
72+
__m256i test_mm256_insert_epi16(__m256i x, int b) {
73+
74+
// CIR-CHECK-LABEL: test_mm256_insert_epi16
75+
// CIR-CHECK: {{%.*}} = cir.vec.insert {{%.*}}, {{%.*}}[{{%.*}} : {{!u32i|!u64i}}] : !cir.vector<!s16i x 16>
76+
77+
// LLVM-CHECK-LABEL: test_mm256_insert_epi16
78+
// LLVM-CHECK: insertelement <16 x i16> %{{.*}}, i16 %{{.*}}, {{i32|i64}} 4
79+
return _mm256_insert_epi16(x, b, 4);
80+
}
81+
82+
__m256i test_mm256_insert_epi32(__m256i x, int b) {
83+
84+
// CIR-CHECK-LABEL: test_mm256_insert_epi32
85+
// CIR-CHECK: {{%.*}} = cir.vec.insert {{%.*}}, {{%.*}}[{{%.*}} : {{!u32i|!u64i}}] : !cir.vector<!s32i x 8>
86+
87+
// LLVM-CHECK-LABEL: test_mm256_insert_epi32
88+
// LLVM-CHECK: insertelement <8 x i32> %{{.*}}, i32 %{{.*}}, {{i32|i64}} 5
89+
return _mm256_insert_epi32(x, b, 5);
90+
}
91+
92+
#ifdef __x86_64__
93+
__m256i test_mm256_insert_epi64(__m256i x, long long b) {
94+
95+
// CIR-X64-LABEL: test_mm256_insert_epi64
96+
// CIR-X64: {{%.*}} = cir.vec.insert {{%.*}}, {{%.*}}[{{%.*}} : {{!u32i|!u64i}}] : !cir.vector<!s64i x 4>
97+
98+
// LLVM-X64-LABEL: test_mm256_insert_epi64
99+
// LLVM-X64: insertelement <4 x i64> %{{.*}}, i64 %{{.*}}, {{i32|i64}} 2
100+
return _mm256_insert_epi64(x, b, 2);
101+
}
102+
#endif

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,13 @@ int test_mm_extract_pi16(__m64 a) {
1919
// LLVM-CHECK: extractelement <4 x i16> %{{.*}}, i64 2
2020
return _mm_extract_pi16(a, 2);
2121
}
22+
23+
__m64 test_mm_insert_pi16(__m64 a, int d) {
24+
25+
// CIR-CHECK-LABEL: test_mm_insert_pi16
26+
// CIR-CHECK-LABEL: {{%.*}} = cir.vec.insert {{%.*}}, {{%.*}}[{{%.*}} : !u64i] : !cir.vector<!s16i x 4>
27+
28+
// LLVM-CHECK-LABEL: test_mm_insert_pi16
29+
// LLVM-CHECK: insertelement <4 x i16>
30+
return _mm_insert_pi16(a, d, 2);
31+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// RUN: %clang_cc1 -x c -flax-vector-conversions=none -ffreestanding %s -triple=x86_64-unknown-linux -target-feature +sse -fclangir -emit-cir -o %t.cir -Wall -Werror
2+
// RUN: FileCheck --check-prefix=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 +sse -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-unknown-linux -target-feature +sse -fno-signed-char -fclangir -emit-cir -o %t.cir -Wall -Werror
7+
// RUN: FileCheck --check-prefix=CIR --input-file=%t.cir %s
8+
// RUN: %clang_cc1 -x c++ -flax-vector-conversions=none -ffreestanding %s -triple=x86_64-unknown-linux -target-feature +sse -fclangir -emit-llvm -o %t.ll -Wall -Werror
9+
// RUN: FileCheck --check-prefixes=LLVM --input-file=%t.ll %s
10+
11+
#include <immintrin.h>
12+
13+
14+
void test_mm_prefetch(char const* p) {
15+
// CIR-LABEL: test_mm_prefetch
16+
// LLVM-LABEL: test_mm_prefetch
17+
_mm_prefetch(p, 0);
18+
// CIR: cir.prefetch(%{{.*}} : !cir.ptr<!void>) locality(0) read
19+
// LLVM: call void @llvm.prefetch.p0(ptr {{.*}}, i32 0, i32 0, i32 1)
20+
}

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

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,3 +48,35 @@ int test_mm_extract_ps(__m128 x) {
4848
// LLVM-CHECK: extractelement <4 x float> %{{.*}}, {{i32|i64}} 1
4949
return _mm_extract_ps(x, 1);
5050
}
51+
52+
__m128i test_mm_insert_epi8(__m128i x, char b) {
53+
54+
// CIR-CHECK-LABEL: test_mm_insert_epi8
55+
// CIR-CHECK-LABEL: {{%.*}} = cir.vec.insert {{%.*}}, {{%.*}}[{{%.*}} : {{!u32i|!u64i}}] : !cir.vector<{{!s8i|!u8i}} x 16>
56+
57+
// LLVM-CHECK-LABEL: test_mm_insert_epi8
58+
// LLVM-CHECK: insertelement <16 x i8> %{{.*}}, i8 %{{.*}}, {{i32|i64}} 1
59+
return _mm_insert_epi8(x, b, 1);
60+
}
61+
62+
__m128i test_mm_insert_epi32(__m128i x, int b) {
63+
64+
// CIR-CHECK-LABEL: test_mm_insert_epi32
65+
// CIR-CHECK-LABEL: {{%.*}} = cir.vec.insert {{%.*}}, {{%.*}}[{{%.*}} : {{!u32i|!u64i}}] : !cir.vector<!s32i x 4>
66+
67+
// LLVM-CHECK-LABEL: test_mm_insert_epi32
68+
// LLVM-CHECK: insertelement <4 x i32> %{{.*}}, i32 %{{.*}}, {{i32|i64}} 1
69+
return _mm_insert_epi32(x, b, 1);
70+
}
71+
72+
#ifdef __x86_64__
73+
__m128i test_mm_insert_epi64(__m128i x, long long b) {
74+
75+
// CIR-X64-LABEL: test_mm_insert_epi64
76+
// CIR-X64: {{%.*}} = cir.vec.insert {{%.*}}, {{%.*}}[{{%.*}} : {{!u32i|!u64i}}] : !cir.vector<!s64i x 2>
77+
78+
// LLVM-X64-LABEL: test_mm_insert_epi64
79+
// LLVM-X64: insertelement <2 x i64> %{{.*}}, i64 %{{.*}}, {{i32|i64}} 1
80+
return _mm_insert_epi64(x, b, 1);
81+
}
82+
#endif

0 commit comments

Comments
 (0)