Skip to content

Optimizer: add some floating point optimizations #80404

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 5 commits into from
Apr 2, 2025

Conversation

eeckstein
Copy link
Contributor

@eeckstein eeckstein commented Mar 31, 2025

The goal is to completely constant fold literals of custom floating point types which use the generic initializers of the stdlib's BinaryFloatingPoint protocol.

Two changes were necessary to make this happen:

  • add constant folding for some floating point intrinsics
  • force inlining of some floating point functions in the stdlib, if their arguments are constant

It turned out that constant folding the bitcast intrinsic broke diagnosing floating point literals which end up as "infinity".
So, I had to fix this as well. For details see the commit messages.

rdar://147278006

@eeckstein
Copy link
Contributor Author

@swift-ci test

@eeckstein
Copy link
Contributor Author

@swift-ci apple silicon benchmark

@eeckstein eeckstein force-pushed the optimize-floating-point branch from c89ffa6 to 072ed8a Compare March 31, 2025 12:34
@eeckstein
Copy link
Contributor Author

@swift-ci test

@eeckstein eeckstein force-pushed the optimize-floating-point branch from 072ed8a to fc2d86f Compare March 31, 2025 14:41
@eeckstein
Copy link
Contributor Author

@swift-ci test

@eeckstein eeckstein force-pushed the optimize-floating-point branch from fc2d86f to ae2607c Compare March 31, 2025 17:21
@eeckstein
Copy link
Contributor Author

@swift-ci test

@eeckstein eeckstein force-pushed the optimize-floating-point branch from ae2607c to 08f01de Compare April 1, 2025 11:16
@eeckstein
Copy link
Contributor Author

@swift-ci test

…erals if it has an AST node

This avoids false alarms for floating point literals which are imported from the `var infinity` getter of floating point types.
This forces inlining the annotated function if its arguments are constant.
* `sitofp` signed integer to floating point
* `rint` round floating point to integral
* `bitcast` between integer and floating point

Constant folding `bitcast`s also made it necessary to rewrite constant folding for Nan and inf values, because the old code explicitly checked for `bitcast` intrinsics.
Relying on constant folded `bitcast`s makes the new version much simpler.

It is important to constant fold these intrinsics already in SIL because it enables other optimizations.
@eeckstein eeckstein force-pushed the optimize-floating-point branch from 08f01de to 3ec0570 Compare April 1, 2025 16:13
@eeckstein
Copy link
Contributor Author

@swift-ci test

@eeckstein
Copy link
Contributor Author

@swift-ci test windows

@eeckstein
Copy link
Contributor Author

@swift-ci smoke test macos

@eeckstein eeckstein merged commit 6c0b778 into swiftlang:main Apr 2, 2025
4 of 5 checks passed
@eeckstein eeckstein deleted the optimize-floating-point branch April 2, 2025 16:23
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants