[SR-11499] Bounds checks inhibiting vectorisation of array operations. #53900
Labels
bug
A deviation from expected or documented behavior. Also: expected but undesirable behavior.
compiler
The Swift compiler itself
performance
Environment
Apple Swift version 5.1.1 (swiftlang-1100.0.273 clang-1100.0.33.9)
Target: x86_64-apple-darwin19.0.0
Additional Detail from JIRA
md5: 845b8aa38251f79df6cb70314a1ed141
Issue Description:
The following "benchmark" program shows a simple constant-time (ish) comparison function implemented on top of
[UInt8]
:When compiled with
-O
we obtain the following assembly forconstantTimeCompare
:The central part of the loop uses
xorb
andorb
, performing the comparison one byte at a time. An equivalent C function would be vectorised and usepxor
for the body of the loop.It seems likely that the bounds checks are what is inhibiting the vectorisation optimisation here. I came to this conclusion by rewriting the loop to avoid using
zip
orreduce
, and while that new loop has drastically smaller code size it still fails to vectorise.I am not sure whether this even can be fixed in the case of a general
Collection
, as it's not trivially clear to me that the bounds check can be hoisted, but in the case of certain kinds ofCollection
object (those with linear integral indices) it would seem to be possible to perform a transformation that allows vectorisation of this code.The text was updated successfully, but these errors were encountered: