Skip to content

Add Float16 to stdlib #30130

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 17 commits into from
Apr 6, 2020
Merged

Conversation

stephentyrone
Copy link
Contributor

Implement IEEE 754 binary16 as Float16 in the stdlib conforming to BinaryFloatingPoint and SIMDScalar.

Evolution proposal: https://github.com/apple/swift-evolution/blob/master/proposals/0277-float16.md
SE thread: https://forums.swift.org/t/se-0277-float16/33546

@stephentyrone stephentyrone added the swift evolution approved Flag → feature: A feature that was approved through the Swift evolution process label Feb 28, 2020
@stephentyrone
Copy link
Contributor Author

@swift-ci please test

@stephentyrone
Copy link
Contributor Author

@swift-ci please test

@swift-ci
Copy link
Contributor

Build failed
Swift Test Linux Platform
Git Sha - a5ffc9f

@swift-ci
Copy link
Contributor

Build failed
Swift Test OS X Platform
Git Sha - a5ffc9f

@stephentyrone
Copy link
Contributor Author

@swift-ci please test

@swift-ci
Copy link
Contributor

Build failed
Swift Test OS X Platform
Git Sha - 1d82e6c

@swift-ci
Copy link
Contributor

Build failed
Swift Test Linux Platform
Git Sha - 1d82e6c

Copy link
Member

@lorentey lorentey left a comment

Choose a reason for hiding this comment

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

Looks good! I caught one typo 🎣

@@ -58,6 +67,7 @@ else:
% end

${SelfDocComment}
${Availability(bits)}
Copy link
Member

Choose a reason for hiding this comment

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

The extra line here won't interfere with tools extracting doc comments, right?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I don't think so.

@stephentyrone
Copy link
Contributor Author

Update: ubuntu seems to be missing the float <-> float16 conversion functions in its libgcc, so we'll probably have to build them into the Swift runtime lib on Linux (and likely some other platforms, as well). I'll add a patch to do that sometime in the next few days.

@stephentyrone
Copy link
Contributor Author

@swift-ci please smoke test Linux

@stephentyrone
Copy link
Contributor Author

(Local build for Linux isn't working quite right, so rebuilding on CI to get a list of exactly the symbols required.)

@stephentyrone
Copy link
Contributor Author

Looks like we need to provide:

  • __gnu_h2f_ieee (half -> float)
  • __gnu_f2h_ieee (float -> half)
  • __truncdfhf2 (double -> half)

@stephentyrone
Copy link
Contributor Author

@swift-ci smoke test

@stephentyrone stephentyrone requested a review from tbkka March 11, 2020 21:28
@stephentyrone
Copy link
Contributor Author

@tbkka can you review the new runtime/Float16Support.cpp?

Copy link
Contributor

@tbkka tbkka left a comment

Choose a reason for hiding this comment

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

Minor suggestions.

@@ -0,0 +1,164 @@
//===------------- Float16Support.cpp - Swift Float16 Support -------------===//
Copy link
Contributor

Choose a reason for hiding this comment

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

This seems like the right place for this.

@stephentyrone
Copy link
Contributor Author

Getting back to this, let's run a test on Linux and see where we stand ...

@stephentyrone
Copy link
Contributor Author

@swift-ci test

@swift-ci
Copy link
Contributor

swift-ci commented Apr 1, 2020

Build failed
Swift Test Linux Platform
Git Sha - 2d11922faede69623256062ea4f542d30c5eb140

@xwu
Copy link
Collaborator

xwu commented Apr 2, 2020

@swift-ci test

@swift-ci
Copy link
Contributor

swift-ci commented Apr 2, 2020

Build failed
Swift Test OS X Platform
Git Sha - 2d11922faede69623256062ea4f542d30c5eb140

@swift-ci
Copy link
Contributor

swift-ci commented Apr 2, 2020

Build failed
Swift Test Linux Platform
Git Sha - 2d11922faede69623256062ea4f542d30c5eb140

Add an actual float16ToString operation, that we can give a real implemention for down the road.
@stephentyrone
Copy link
Contributor Author

@swift-ci test

@swift-ci
Copy link
Contributor

swift-ci commented Apr 2, 2020

Build failed
Swift Test OS X Platform
Git Sha - 1aefc71

@swift-ci
Copy link
Contributor

swift-ci commented Apr 2, 2020

Build failed
Swift Test Linux Platform
Git Sha - 1aefc71

@stephentyrone
Copy link
Contributor Author

@swift-ci test

@swift-ci
Copy link
Contributor

swift-ci commented Apr 3, 2020

Build failed
Swift Test OS X Platform
Git Sha - 6705057

@swift-ci
Copy link
Contributor

swift-ci commented Apr 3, 2020

Build failed
Swift Test Linux Platform
Git Sha - 6705057

@stephentyrone
Copy link
Contributor Author

@swift-ci please test

@swift-ci
Copy link
Contributor

swift-ci commented Apr 6, 2020

Build failed
Swift Test Linux Platform
Git Sha - 1188d20

@swift-ci
Copy link
Contributor

swift-ci commented Apr 6, 2020

Build failed
Swift Test OS X Platform
Git Sha - 1188d20

@stephentyrone
Copy link
Contributor Author

@swift-ci please test

@swift-ci
Copy link
Contributor

swift-ci commented Apr 6, 2020

Build failed
Swift Test Linux Platform
Git Sha - 0030880bd91ae41c4f42f7baeba743319008d58a

@swift-ci
Copy link
Contributor

swift-ci commented Apr 6, 2020

Build failed
Swift Test OS X Platform
Git Sha - 0030880bd91ae41c4f42f7baeba743319008d58a

@stephentyrone stephentyrone merged commit 248c554 into swiftlang:master Apr 6, 2020
@stephentyrone stephentyrone deleted the SE-0277-Float16 branch April 6, 2020 21:57
stephentyrone added a commit to swiftlang/swift-evolution that referenced this pull request Apr 7, 2020
Implementation was merged to Swift in swiftlang/swift#30130.
@compnerd
Copy link
Member

compnerd commented Apr 8, 2020

This caused a regression on the android builds :-(

Logs:
https://dev.azure.com/compnerd/3133d6ab-80a8-4996-ac4f-03df25cd3224/_apis/build/builds/29728/logs/289

The new Float16 support requires runtime support from the compiler runtime. In particular, the AEABI provides the runtime functions __aeabi_d2h to truncate a double to a half precision floating point which is missing in the compiler runtime. I think that the most expedient thing will be to provide an implementation in the standard library or in the shims :-(. Thoughts @stephentyrone?

@stephentyrone
Copy link
Contributor Author

stephentyrone commented Apr 8, 2020

@compnerd Yes, you'll need to update stdlib/public/runtime/Float16Support.cpp to expose __truncdfhf2 under that name when building for Android. Host platforms' compiler-rt or libgcc are supposed to provide this operation, but they clearly don't on a bunch of non-Apple targets.

The implementation should work essentially unchanged, though you may need to replace the call to __gnu_f2h_ieee with either the appropriate float-to-half conversion or a cast if the compiler knows how to handle that conversion when targeting Android.

@compnerd
Copy link
Member

compnerd commented Apr 8, 2020

Yeah, why would the system provide the (system) required symbols? sigh On the bright side, at least it is supposed to be there in future versions. I'll see if I can get the shims in place for ARMv7.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
swift evolution approved Flag → feature: A feature that was approved through the Swift evolution process
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants