Skip to content

Replace two reduces with explicit loops in SIMD implementations. #72423

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Mar 20, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 12 additions & 2 deletions stdlib/public/core/SIMDVector.swift
Original file line number Diff line number Diff line change
Expand Up @@ -850,7 +850,11 @@ extension SIMD where Scalar: FixedWidthInteger {
/// Equivalent to `indices.reduce(into: 0) { $0 &+= self[$1] }`.
@_alwaysEmitIntoClient
public func wrappedSum() -> Scalar {
return indices.reduce(into: 0) { $0 &+= self[$1] }
var result: Scalar = 0
for i in indices {
result &+= self[i]
}
return result
}
}

Expand Down Expand Up @@ -927,7 +931,13 @@ extension SIMD where Scalar: FloatingPoint {
// llvm.experimental.vector.reduce.fadd or an explicit tree-sum. Open-
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit to past-you: The first line of the implementation note has an elided word, "this eventually" probably needs a "will" or "should" somewhere in there.

// coding the tree sum is problematic, we probably need to define a
// Swift Builtin to support it.
return indices.reduce(into: 0) { $0 += self[$1] }
//
// Use -0 so that LLVM can optimize away initial value + self[0].
var result = -Scalar.zero
for i in indices {
result += self[i]
}
return result
}
}

Expand Down