Skip to content

Commit d24843e

Browse files
Replace two reduces with explicit loops in SIMD implementations. (#72423)
* Replace two `reduce`s with explicit loops in SIMD implementations. `reduce` has enough machinery that it pushes us over some inlining thresholds before optimizations happen, resulting in much worse codegen for String breadcrumbs. So a nice pickup there, but also generally applicable to other code as well.
1 parent f3441c0 commit d24843e

File tree

1 file changed

+12
-2
lines changed

1 file changed

+12
-2
lines changed

stdlib/public/core/SIMDVector.swift

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -850,7 +850,11 @@ extension SIMD where Scalar: FixedWidthInteger {
850850
/// Equivalent to `indices.reduce(into: 0) { $0 &+= self[$1] }`.
851851
@_alwaysEmitIntoClient
852852
public func wrappedSum() -> Scalar {
853-
return indices.reduce(into: 0) { $0 &+= self[$1] }
853+
var result: Scalar = 0
854+
for i in indices {
855+
result &+= self[i]
856+
}
857+
return result
854858
}
855859
}
856860

@@ -927,7 +931,13 @@ extension SIMD where Scalar: FloatingPoint {
927931
// llvm.experimental.vector.reduce.fadd or an explicit tree-sum. Open-
928932
// coding the tree sum is problematic, we probably need to define a
929933
// Swift Builtin to support it.
930-
return indices.reduce(into: 0) { $0 += self[$1] }
934+
//
935+
// Use -0 so that LLVM can optimize away initial value + self[0].
936+
var result = -Scalar.zero
937+
for i in indices {
938+
result += self[i]
939+
}
940+
return result
931941
}
932942
}
933943

0 commit comments

Comments
 (0)