Skip to content

Commit 523e178

Browse files
committed
X86: bump the minimum required CPU to IvyBridge
Bump the minimum supported CPU to IvyBridge so that we can take advantage of the F16C/CVT16 extensions to support half floating point rounding. This avoids the undefined reference to `__extendsfhf2` in the FP16 support.
1 parent e7561e1 commit 523e178

File tree

4 files changed

+39
-3
lines changed

4 files changed

+39
-3
lines changed

lib/ClangImporter/ClangImporter.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -780,6 +780,16 @@ importer::addCommonInvocationArguments(
780780
invocationArgStrs.push_back("-mcx16");
781781
}
782782

783+
if (triple.isX86()) {
784+
// Enable the FC16/CVT16 extensions which provide half prevision floating
785+
// point support on x86_64. This bumps the minimum requirement of the CPU
786+
// to Ivy Bridge, however, Windows 10 (at least as of 1709) requires
787+
// ~Broadwell. At this point, the complexity of supporting an older release
788+
// no longer is justified. For uniformity, bump the minimum x86 CPU on all
789+
// the targets.
790+
invocationArgStrs.push_back("-mf16c");
791+
}
792+
783793
if (!importerOpts.Optimization.empty()) {
784794
invocationArgStrs.push_back(importerOpts.Optimization);
785795
}

stdlib/cmake/modules/AddSwiftStdlib.cmake

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -288,6 +288,18 @@ function(_add_target_variant_c_compile_flags)
288288
endif()
289289
endif()
290290

291+
# Avoid the need for the FP16 truncation and extension routines from
292+
# compiler-rt by assuming that we have hardware capable of performing
293+
# half-precision floating point conversions. This effectively pins the
294+
# minimum CPU requirements to Ivy Bridge (~2013).
295+
if(CFLAGS_ARCH STREQUAL x86_64 OR CFLAGS_ARCH STREQUAL i686)
296+
if(SWIFT_COMPILER_IS_MSVC_LIKE)
297+
list(APPEND result /clang:-mf16c)
298+
else()
299+
list(APPEND result -mf16c)
300+
endif()
301+
endif()
302+
291303
if(CFLAGS_ENABLE_ASSERTIONS)
292304
list(APPEND result "-UNDEBUG")
293305
else()

stdlib/public/runtime/Float16Support.cpp

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
// __gnu_h2f_ieee
1616
// __gnu_f2h_ieee
1717
// __truncdfhf2
18+
// __extendhfxf2
1819
//
1920
// On Darwin platforms, these are provided by the host compiler-rt, but we
2021
// can't depend on that everywhere, so we have to provide them in the Swift
@@ -30,7 +31,7 @@
3031
// Android NDK <r21 do not provide `__aeabi_d2h` in the compiler runtime,
3132
// provide shims in that case.
3233
#if (defined(__ANDROID__) && defined(__ARM_ARCH_7A__) && defined(__ARM_EABI__)) || \
33-
((defined(__i386__) || defined(__i686__) || defined(__x86_64__)) && !defined(__APPLE__) && !defined(__linux__))
34+
(!defined(__APPLE__) && (defined(__i386__) || defined(__x86_64__)))
3435

3536
#include "swift/shims/Visibility.h"
3637

@@ -150,10 +151,23 @@ SWIFT_RUNTIME_EXPORT unsigned short __truncdfhf2(double d) {
150151
return __gnu_f2h_ieee(f);
151152
}
152153

154+
// F16C does not cover FP80 conversions, so we still need an implementation
155+
// here.
156+
#if (defined(__i386__) || defined(__x86_64__)) && \
157+
!(defined(__ANDROID__) || defined(__APPLE__) || defined(_WIN32))
158+
159+
SWIFT_RUNTIME_EXPORT long double __extendhfxf2(_Float16 h) {
160+
__builtin_trap();
161+
}
162+
163+
#endif // (defined(__i386__) || defined(__x86_64__)) &&
164+
// !(defined(__ANDROID__) || defined(__APPLE__) || defined(_WIN32))
165+
153166
#if defined(__ARM_EABI__)
154167
SWIFT_RUNTIME_EXPORT unsigned short __aeabi_d2h(double d) {
155168
return __truncdfhf2(d);
156169
}
157170
#endif
158171

159-
#endif // defined(__x86_64__) && !defined(__APPLE__)
172+
#endif // (defined(__ANDROID__) && defined(__ARM_ARCH_7A__) && defined(__ARM_EABI__)) ||
173+
// (!defined(__APPLE__) && (defined(__i386__) || defined(__x86_64__)))

test/IRGen/ordering_x86.sil

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,4 +41,4 @@ bb0:
4141
// the order of features differs.
4242

4343
// X86_64: define{{( protected)?}} swiftcc void @baz{{.*}}#0
44-
// X86_64: #0 = {{.*}}"target-features"="+cx16,+cx8,+fxsr,+mmx,+sahf,+sse,+sse2,+sse3,+ssse3,+x87"
44+
// X86_64: #0 = {{.*}}"target-features"="+avx,+crc32,+cx16,+cx8,+f16c,+fxsr,+mmx,+popcnt,+sahf,+sse,+sse2,+sse3,+sse4.1,+sse4.2,+ssse3,+x87,+xsave"

0 commit comments

Comments
 (0)