Skip to content

Commit 527ef8c

Browse files
committed
Reland "[X86] Support _Float16 on SSE2 and up"
Enable `COMPILER_RT_HAS_FLOAT16` to solve the lit fail. This is split from D113107 to address #56204 and https://discourse.llvm.org/t/how-to-build-compiler-rt-for-new-x86-half-float-abi/63366 Reviewed By: zahiraam, rjmccall, bkramer Differential Revision: https://reviews.llvm.org/D128571
1 parent 7e86b13 commit 527ef8c

File tree

9 files changed

+51
-7
lines changed

9 files changed

+51
-7
lines changed

clang/docs/LanguageExtensions.rst

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -743,7 +743,13 @@ targets pending ABI standardization:
743743
* 64-bit ARM (AArch64)
744744
* AMDGPU
745745
* SPIR
746-
* X86 (Only available under feature AVX512-FP16)
746+
* X86 (see below)
747+
748+
On X86 targets, ``_Float16`` is supported as long as SSE2 is available, which
749+
includes all 64-bit and all recent 32-bit processors. When the target supports
750+
AVX512-FP16, ``_Float16`` arithmetic is performed using that native support.
751+
Otherwise, ``_Float16`` arithmetic is performed by promoting to ``float``,
752+
performing the operation, and then truncating to ``_Float16``.
747753

748754
``_Float16`` will be supported on more targets as they define ABIs for it.
749755

clang/docs/ReleaseNotes.rst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -514,6 +514,9 @@ X86 Support in Clang
514514

515515
- Support ``-mharden-sls=[none|all|return|indirect-jmp]`` for straight-line
516516
speculation hardening.
517+
- Support for the ``_Float16`` type has been added for all targets with SSE2.
518+
When AVX512-FP16 is not available, arithmetic on ``_Float16`` is emulated
519+
using ``float``.
517520

518521
DWARF Support in Clang
519522
----------------------

clang/lib/Basic/Targets/X86.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -239,7 +239,6 @@ bool X86TargetInfo::handleTargetFeatures(std::vector<std::string> &Features,
239239
HasAVX512ER = true;
240240
} else if (Feature == "+avx512fp16") {
241241
HasAVX512FP16 = true;
242-
HasFloat16 = true;
243242
} else if (Feature == "+avx512pf") {
244243
HasAVX512PF = true;
245244
} else if (Feature == "+avx512dq") {
@@ -355,6 +354,9 @@ bool X86TargetInfo::handleTargetFeatures(std::vector<std::string> &Features,
355354
.Default(NoSSE);
356355
SSELevel = std::max(SSELevel, Level);
357356

357+
// Turn on _float16 for x86 (feature sse2)
358+
HasFloat16 = SSELevel >= SSE2;
359+
358360
MMX3DNowEnum ThreeDNowLevel = llvm::StringSwitch<MMX3DNowEnum>(Feature)
359361
.Case("+3dnowa", AMD3DNowAthlon)
360362
.Case("+3dnow", AMD3DNow)
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
// RUN: %clang_cc1 -triple x86_64-unknown-unknown \
2+
// RUN: -emit-llvm -o - %s | FileCheck %s --check-prefixes=CHECK
3+
4+
// CHECK-NOT: fpext
5+
// CHECK-NOT: fptrunc
6+
7+
_Float16 add1(_Float16 a, _Float16 b) {
8+
return a + b;
9+
}
10+
11+
_Float16 add2(_Float16 a, _Float16 b, _Float16 c) {
12+
return a + b + c;
13+
}
14+
15+
_Float16 div(_Float16 a, _Float16 b) {
16+
return a / b;
17+
}
18+
19+
_Float16 mul(_Float16 a, _Float16 b) {
20+
return a * b;
21+
}
22+
23+
_Float16 add_and_mul1(_Float16 a, _Float16 b, _Float16 c, _Float16 d) {
24+
return a * b + c * d;
25+
}
26+
27+
_Float16 add_and_mul2(_Float16 a, _Float16 b, _Float16 c, _Float16 d) {
28+
return (a - 6 * b) + c;
29+
}

clang/test/CodeGen/X86/avx512fp16-complex.c renamed to clang/test/CodeGen/X86/Float16-complex.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
// RUN: %clang_cc1 %s -O0 -emit-llvm -triple x86_64-unknown-unknown -target-feature +avx512fp16 -o - | FileCheck %s --check-prefix=X86
2+
// RUN: %clang_cc1 %s -O0 -emit-llvm -triple x86_64-unknown-unknown -o - | FileCheck %s --check-prefix=X86
23

34
_Float16 _Complex add_half_rr(_Float16 a, _Float16 b) {
45
// X86-LABEL: @add_half_rr(

clang/test/Sema/Float16.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
// RUN: %clang_cc1 -fsyntax-only -verify -triple x86_64-linux-pc %s
2-
// RUN: %clang_cc1 -fsyntax-only -verify -triple x86_64-linux-pc -target-feature +avx512fp16 %s -DHAVE
1+
// RUN: %clang_cc1 -fsyntax-only -verify -triple i686-linux-pc %s
2+
// RUN: %clang_cc1 -fsyntax-only -verify -triple i686-linux-pc -target-feature +sse2 %s -DHAVE
3+
// RUN: %clang_cc1 -fsyntax-only -verify -triple x86_64-linux-pc %s -DHAVE
34
// RUN: %clang_cc1 -fsyntax-only -verify -triple spir-unknown-unknown %s -DHAVE
45
// RUN: %clang_cc1 -fsyntax-only -verify -triple armv7a-linux-gnu %s -DHAVE
56
// RUN: %clang_cc1 -fsyntax-only -verify -triple aarch64-linux-gnu %s -DHAVE

clang/test/Sema/conversion-target-dep.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
long double ld;
88
double d;
9-
_Float16 f16; // x86-error {{_Float16 is not supported on this target}}
9+
_Float16 f16;
1010

1111
int main(void) {
1212
ld = d; // x86-warning {{implicit conversion increases floating-point precision: 'double' to 'long double'}}

clang/test/SemaCXX/Float16.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
// RUN: %clang_cc1 -fsyntax-only -verify -triple x86_64-linux-pc %s
1+
// RUN: %clang_cc1 -fsyntax-only -verify -triple i686-linux-pc %s
2+
// RUN: %clang_cc1 -fsyntax-only -verify -triple i686-linux-pc -target-feature +sse2 %s -DHAVE
3+
// RUN: %clang_cc1 -fsyntax-only -verify -triple x86_64-linux-pc %s -DHAVE
24
// RUN: %clang_cc1 -fsyntax-only -verify -triple spir-unknown-unknown %s -DHAVE
35
// RUN: %clang_cc1 -fsyntax-only -verify -triple armv7a-linux-gnu %s -DHAVE
46
// RUN: %clang_cc1 -fsyntax-only -verify -triple aarch64-linux-gnu %s -DHAVE

compiler-rt/test/builtins/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ foreach(arch ${BUILTIN_TEST_ARCH})
4444
string(REPLACE ";" " " BUILTINS_TEST_TARGET_CFLAGS "${BUILTINS_TEST_TARGET_CFLAGS}")
4545
endif()
4646

47-
if (${arch} MATCHES "arm|aarch64|arm64" AND COMPILER_RT_HAS_FLOAT16)
47+
if (${arch} MATCHES "arm|aarch64|arm64|i?86|x86_64|AMD64" AND COMPILER_RT_HAS_FLOAT16)
4848
list(APPEND BUILTINS_TEST_TARGET_CFLAGS -DCOMPILER_RT_HAS_FLOAT16)
4949
string(REPLACE ";" " " BUILTINS_TEST_TARGET_CFLAGS "${BUILTINS_TEST_TARGET_CFLAGS}")
5050
endif()

0 commit comments

Comments
 (0)