swift icon indicating copy to clipboard operation
swift copied to clipboard

[SR-203] Missed optimization opportunity (loop unrolling, SIMD)

Open jepers opened this issue 10 years ago • 4 comments

Previous ID SR-203
Radar None
Original Reporter @jepers
Type Bug
Status Closed
Resolution Done

Attachment: Download

Additional Detail from JIRA
Votes 0
Component/s Compiler
Labels Bug
Assignee @aschwaighofer
Priority Medium

md5: a3257dee833a6f8d992d3752f8dc6e04

Issue Description:

Swift's ability to produce highly optimized code from relatively high level abstractions is getting increasingly better, and the current open source master has improved hugely compared to Xcode 7.2 GM.

However, our (and supposedly many others') projects would benefit greatly from even further improvements in cases like the one exemplified by the attached stand-alone test program.

The test demonstrates how manually unrolling a simple for-loop (over a statically knowable int range) makes the code approximately 4 times faster (which is as fast as the SIMD equivalent of the custom type used).

I wish the optimizer could make sure this kind of manual unrolling wasn't necessary.

Filed this bug as requested here:
https://lists.swift.org/pipermail/swift-dev/Week-of-Mon-20151207/000266.html

jepers avatar Dec 12 '15 14:12 jepers

This should be addressed when my work on unrolling goes in.

(https://github.com/aschwaighofer/swift/tree/loop_unrolling)

$ ./unroll-test
Running test compiled with manuallyUnrolled = true:
float4 median time: 0.001071 s (DCE-prevention: 20283)
V4<Float> median time: 0.001127 s (DCE-prevention: 59705)
float4x4 median time: 0.004334 s (DCE-prevention: 43784)
V4<V4<Float>> median time: 0.004346 s (DCE-prevention: 30778)

$ ./unroll-test
Running test compiled with manuallyUnrolled = false:
float4 median time: 0.001070 s (DCE-prevention: 27745)
V4<Float> median time: 0.001121 s (DCE-prevention: 41516)
float4x4 median time: 0.004331 s (DCE-prevention: 28166)
V4<V4<Float>> median time: 0.004340 s (DCE-prevention: 64316)

aschwaighofer avatar Dec 12 '15 16:12 aschwaighofer

Wow, thanks!
I suppose it's not possible to say anything about ~ when these improvements will be included in an Xcode GM?

jepers avatar Dec 12 '15 16:12 jepers

This was fixed by
edf9ca0 Unroll loops with known short trip count

aschwaighofer avatar Dec 14 '15 20:12 aschwaighofer

This fix seems to be missing in Swift 3 (Xcode 8.0 beta), or something else is making this test run 200 times slower with Swift 3 than with Swift 2.2. Please see: https://bugs.swift.org/browse/SR-1824

jepers avatar Jun 19 '16 22:06 jepers