Skip to content

Commit b750c89

Browse files
committed
Don't use reassociate(on) on older clang toolchains.
1 parent 7939016 commit b750c89

File tree

2 files changed

+24
-8
lines changed

2 files changed

+24
-8
lines changed

Sources/RealModule/RealFunctions.swift

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,10 @@ public protocol RealFunctions: ElementaryFunctions {
9898
/// Further improvement should be possible by improving LLVM optimizations
9999
/// or adding attributes to license more aggressive unrolling and taking
100100
/// advantage of vector ISA extensions for swift.
101+
///
102+
/// If a type or toolchain does not support reassociation for optimization
103+
/// purposes, this operation decays to a normal addition; it is a license
104+
/// for the compiler to optimize, not a guarantee that any change occurs.
101105
static func _relaxedAdd(_ a: Self, _ b: Self) -> Self
102106

103107
/// a * b, with the optimizer licensed to reassociate and form FMAs.
@@ -118,5 +122,10 @@ public protocol RealFunctions: ElementaryFunctions {
118122
///
119123
/// If you want to license FMA formation, but _not_ reassociation (desirable
120124
/// for some numerics tasks), use `_mulAdd(a, b, c)` instead.
125+
///
126+
/// If a type or toolchain does not support reassociation for optimization
127+
/// purposes, this operation decays to a normal multiplication; it is a
128+
/// license for the compiler to optimize, not a guarantee that any change
129+
/// occurs.
121130
static func _relaxedMul(_ a: Self, _ b: Self) -> Self
122131
}

Sources/_NumericsShims/include/_NumericsShims.h

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -383,6 +383,13 @@ HEADER_SHIM long double libm_lgammal(long double x, int *signp) {
383383
#endif
384384

385385
// MARK: - math inlines with relaxed semantics to support optimization.
386+
#if __swift__ >= 50400
387+
#define CLANG_RELAX_FP _Pragma("clang fp reassociate(on) contract(fast)")
388+
#else
389+
// reassociate(on) isn't supported by the clang in pre-swift-5.4 toolchains.
390+
#define CLANG_RELAX_FP _Pragma("clang fp contract(fast)")
391+
#endif
392+
386393
#if !(__i386__ || __x86_64__)
387394
/// a*b + c evaluated _either_ as two operations or fma, whichever is faster.
388395
HEADER_SHIM _Float16 _numerics_muladdf16(_Float16 a, _Float16 b, _Float16 c) {
@@ -393,14 +400,14 @@ HEADER_SHIM _Float16 _numerics_muladdf16(_Float16 a, _Float16 b, _Float16 c) {
393400
/// a + b with the "allow reassociation" and "allow FMA formation" flags
394401
/// set in the IR.
395402
HEADER_SHIM _Float16 _numerics_relaxed_addf16(_Float16 a, _Float16 b) {
396-
#pragma clang fp reassociate(on) contract(fast)
403+
CLANG_RELAX_FP
397404
return a + b;
398405
}
399406

400407
/// a * b with the "allow reassociation" and "allow FMA formation" flags
401408
/// set in the IR.
402409
HEADER_SHIM _Float16 _numerics_relaxed_mulf16(_Float16 a, _Float16 b) {
403-
#pragma clang fp reassociate(on) contract(fast)
410+
CLANG_RELAX_FP
404411
return a * b;
405412
}
406413
#endif
@@ -414,14 +421,14 @@ HEADER_SHIM float _numerics_muladdf(float a, float b, float c) {
414421
/// a + b with the "allow reassociation" and "allow FMA formation" flags
415422
/// set in the IR.
416423
HEADER_SHIM float _numerics_relaxed_addf(float a, float b) {
417-
#pragma clang fp reassociate(on) contract(fast)
424+
CLANG_RELAX_FP
418425
return a + b;
419426
}
420427

421428
/// a * b with the "allow reassociation" and "allow FMA formation" flags
422429
/// set in the IR.
423430
HEADER_SHIM float _numerics_relaxed_mulf(float a, float b) {
424-
#pragma clang fp reassociate(on) contract(fast)
431+
CLANG_RELAX_FP
425432
return a * b;
426433
}
427434

@@ -434,29 +441,29 @@ HEADER_SHIM double _numerics_muladd(double a, double b, double c) {
434441
/// a + b with the "allow reassociation" and "allow FMA formation" flags
435442
/// set in the IR.
436443
HEADER_SHIM double _numerics_relaxed_add(double a, double b) {
437-
#pragma clang fp reassociate(on) contract(fast)
444+
CLANG_RELAX_FP
438445
return a + b;
439446
}
440447

441448
/// a * b with the "allow reassociation" and "allow FMA formation" flags
442449
/// set in the IR.
443450
HEADER_SHIM double _numerics_relaxed_mul(double a, double b) {
444-
#pragma clang fp reassociate(on) contract(fast)
451+
CLANG_RELAX_FP
445452
return a * b;
446453
}
447454

448455
#if !defined _WIN32 && (defined __i386__ || defined __x86_64__)
449456
/// a + b with the "allow reassociation" and "allow FMA formation" flags
450457
/// set in the IR.
451458
HEADER_SHIM long double _numerics_relaxed_addl(long double a, long double b) {
452-
#pragma clang fp reassociate(on) contract(fast)
459+
CLANG_RELAX_FP
453460
return a + b;
454461
}
455462

456463
/// a * b with the "allow reassociation" and "allow FMA formation" flags
457464
/// set in the IR.
458465
HEADER_SHIM long double _numerics_relaxed_mull(long double a, long double b) {
459-
#pragma clang fp reassociate(on) contract(fast)
466+
CLANG_RELAX_FP
460467
return a * b;
461468
}
462469
#endif

0 commit comments

Comments
 (0)