Skip to content

Commit d1a4feb

Browse files
pbo-linarodpaoliello
authored andcommitted
[COFF][Aarch64] Add _InterlockedAdd64 intrinsic (#81849)
Found when compiling openssl master branch using clang-cl. This commit introduces usage of InterlockedAdd64: openssl/openssl@d0e1a0a https://learn.microsoft.com/en-us/cpp/intrinsics/interlockedadd-intrinsic-functions
1 parent 6dbaa89 commit d1a4feb

File tree

6 files changed

+33
-1
lines changed

6 files changed

+33
-1
lines changed

clang/include/clang/Basic/BuiltinsAArch64.def

+1
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,7 @@ TARGET_HEADER_BUILTIN(_BitScanForward64, "UcUNi*ULLi", "nh", INTRIN_H, ALL_MS_LA
139139
TARGET_HEADER_BUILTIN(_BitScanReverse64, "UcUNi*ULLi", "nh", INTRIN_H, ALL_MS_LANGUAGES, "")
140140

141141
TARGET_HEADER_BUILTIN(_InterlockedAdd, "NiNiD*Ni", "nh", INTRIN_H, ALL_MS_LANGUAGES, "")
142+
TARGET_HEADER_BUILTIN(_InterlockedAdd64, "LLiLLiD*LLi", "nh", INTRIN_H, ALL_MS_LANGUAGES, "")
142143
TARGET_HEADER_BUILTIN(_InterlockedAnd64, "LLiLLiD*LLi", "nh", INTRIN_H, ALL_MS_LANGUAGES, "")
143144
TARGET_HEADER_BUILTIN(_InterlockedDecrement64, "LLiLLiD*", "nh", INTRIN_H, ALL_MS_LANGUAGES, "")
144145
TARGET_HEADER_BUILTIN(_InterlockedExchange64, "LLiLLiD*LLi", "nh", INTRIN_H, ALL_MS_LANGUAGES, "")

clang/lib/CodeGen/CGBuiltin.cpp

+2-1
Original file line numberDiff line numberDiff line change
@@ -12044,7 +12044,8 @@ Value *CodeGenFunction::EmitAArch64BuiltinExpr(unsigned BuiltinID,
1204412044
"vgetq_lane");
1204512045
}
1204612046

12047-
case clang::AArch64::BI_InterlockedAdd: {
12047+
case clang::AArch64::BI_InterlockedAdd:
12048+
case clang::AArch64::BI_InterlockedAdd64: {
1204812049
Address DestAddr = CheckAtomicAlignment(*this, E);
1204912050
Value *Val = EmitScalarExpr(E->getArg(1));
1205012051
AtomicRMWInst *RMWI =

clang/lib/Headers/intrin.h

+1
Original file line numberDiff line numberDiff line change
@@ -551,6 +551,7 @@ static __inline__ void __DEFAULT_FN_ATTRS __nop(void) {
551551
#if defined(__aarch64__)
552552
unsigned __int64 __getReg(int);
553553
long _InterlockedAdd(long volatile *Addend, long Value);
554+
__int64 _InterlockedAdd64(__int64 volatile *Addend, __int64 Value);
554555
__int64 _ReadStatusReg(int);
555556
void _WriteStatusReg(int, __int64);
556557

clang/test/CodeGen/arm64-microsoft-intrinsics.c

+14
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,20 @@ long test_InterlockedAdd_constant(long volatile *Addend) {
2121
// CHECK-MSVC: ret i32 %[[NEWVAL:[0-9]+]]
2222
// CHECK-LINUX: error: call to undeclared function '_InterlockedAdd'
2323

24+
__int64 test_InterlockedAdd64(__int64 volatile *Addend, __int64 Value) {
25+
return _InterlockedAdd64(Addend, Value);
26+
}
27+
28+
__int64 test_InterlockedAdd64_constant(__int64 volatile *Addend) {
29+
return _InterlockedAdd64(Addend, -1);
30+
}
31+
32+
// CHECK-LABEL: define {{.*}} i64 @test_InterlockedAdd64(ptr %Addend, i64 %Value) {{.*}} {
33+
// CHECK-MSVC: %[[OLDVAL:[0-9]+]] = atomicrmw add ptr %1, i64 %2 seq_cst, align 8
34+
// CHECK-MSVC: %[[NEWVAL:[0-9]+]] = add i64 %[[OLDVAL:[0-9]+]], %2
35+
// CHECK-MSVC: ret i64 %[[NEWVAL:[0-9]+]]
36+
// CHECK-LINUX: error: call to undeclared function '_InterlockedAdd64'
37+
2438
void check__dmb(void) {
2539
__dmb(0);
2640
}

clang/test/CodeGen/ms-intrinsics-other.c

+9
Original file line numberDiff line numberDiff line change
@@ -240,6 +240,15 @@ LONG test_InterlockedAdd(LONG volatile *Addend, LONG Value) {
240240
// CHECK-ARM-ARM64: %[[OLDVAL:[0-9]+]] = atomicrmw add ptr %Addend, i32 %Value seq_cst, align 4
241241
// CHECK-ARM-ARM64: %[[NEWVAL:[0-9]+]] = add i32 %[[OLDVAL:[0-9]+]], %Value
242242
// CHECK-ARM-ARM64: ret i32 %[[NEWVAL:[0-9]+]]
243+
244+
__int64 test_InterlockedAdd64(__int64 volatile *Addend, __int64 Value) {
245+
return _InterlockedAdd64(Addend, Value);
246+
}
247+
248+
// CHECK-ARM-ARM64: define{{.*}}i64 @test_InterlockedAdd64(ptr{{[a-z_ ]*}}%Addend, i64 noundef %Value) {{.*}} {
249+
// CHECK-ARM-ARM64: %[[OLDVAL:[0-9]+]] = atomicrmw add ptr %Addend, i64 %Value seq_cst, align 8
250+
// CHECK-ARM-ARM64: %[[NEWVAL:[0-9]+]] = add i64 %[[OLDVAL:[0-9]+]], %Value
251+
// CHECK-ARM-ARM64: ret i64 %[[NEWVAL:[0-9]+]]
243252
#endif
244253

245254
#if defined(__arm__) || defined(__aarch64__)

clang/test/CodeGen/ms-intrinsics-underaligned.c

+6
Original file line numberDiff line numberDiff line change
@@ -107,4 +107,10 @@ long long test_InterlockedCompareExchange64(X *x) {
107107
long test_InterlockedAdd(X *x) {
108108
return _InterlockedAdd(&x->c, 4);
109109
}
110+
111+
// CHECK-AARCH64-LABEL: @test_InterlockedAdd64(
112+
// CHECK-AARCH64: atomicrmw {{.*}} align 8
113+
long test_InterlockedAdd64(X *x) {
114+
return _InterlockedAdd64(&x->c, 4);
115+
}
110116
#endif

0 commit comments

Comments
 (0)