Skip to content
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

[builtins] Start to refactor int to fp conversion functions to use a common implementation #66903

Merged
merged 1 commit into from Oct 15, 2023

Conversation

asb
Copy link
Contributor

@asb asb commented Sep 20, 2023

After this patch, the softfp implementations of floatdidf and floatundidf use a common implementation (int_to_fp.h and int_to_fp_impl.inc). This roughly follows the pattern used for a wide range of other builtins, e.g. fp_trunc_impl.inc.

Currently there is substantial copy and paste for the various int to fp conversion functions, with just a few constants being changed. This is a barrier to maintainability, and it's also not attractive to copy this approach as we introduce additional int to fp conversion functions for bf16 and half (which we currently lack, but need - see https://reviews.llvm.org/D157509).

I welcome feedback on the approach taken here, as well as the best way to move forward to land it. I've opted to conservatively start by replacing just two functions, leaving a follow-up patch to replace others that follow the same pattern. Also, for better or worse I've left the logic in float[un]didf largely unchanged other than using a similar approach to fp_trunc_impl.inc to remove the constants that are tied to a specific output floating point format.

For something like this I really miss the stacked reviews we had on Phabricator, but getting some feedback on this rough first patch is probably sensible before pushing out a longer series.

@asb asb changed the title [compiler-rt] Start to refactor int to fp conversion functions to use a common implementation [builtins] Start to refactor int to fp conversion functions to use a common implementation Sep 20, 2023
@asb asb force-pushed the 2023q3-compiler-rt-refactor-int-to-fp-1 branch 2 times, most recently from 9730c2e to 324ecfb Compare September 27, 2023 11:07
@asb
Copy link
Contributor Author

asb commented Sep 27, 2023

Ping. See #67540 for the next patch in the series.

@asb
Copy link
Contributor Author

asb commented Oct 2, 2023

EDIT: I'd erroneously said that clang generates identical IR - this isn't true. However, alive2 does seem to show that the functions are equivalent (not totally clear to me this is valid usage).

floatdidf old vs new: https://alive2.llvm.org/ce/z/XTadv9

floatundidf old vs new: https://alive2.llvm.org/ce/z/zY7Txo

@asb
Copy link
Contributor Author

asb commented Oct 9, 2023

Ping? See above comment for Alive's successful comparison of old vs new IR.

Copy link
Collaborator

@preames preames left a comment

Choose a reason for hiding this comment

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

LGTM - Note that I'm only LGTMing this because of the alive2 proof Alex mentioned above. I wouldn't be comfortable approving this (correctness wise) otherwise. Given we know it's correct, all that's left is the stylistic question and this looks entirely reasonable to me. I'm definitely out of area though, so if someone more active in this area has an opinion, please speak up.

…common implementation

After this patch, the softfp implementations of floatdidf and floatundidf
use a common implementation (int_to_fp.h and int_to_fp_impl.inc). This
roughly follows the pattern used for a wide range of other builtins,
e.g. fp_trunc_impl.inc.

Currently there is substantial copy and paste for the various int to fp
conversion functions, with just a few constants being changed. This is a
barrier to maintainability, and it's also not attractive to copy this
approach as we introduce additional int to fp conversion functions for
bf16 and half (which we currently lack, but need - see
<https://reviews.llvm.org/D157509>).

I've opted to conservatively start by replacing just two functions,
leaving a follow-up patch to replace others that follow the same
pattern. Also, for better or worse I've left the logic in float[un]didf
largely unchanged other than using a similar approach to
fp_trunc_impl.inc to remove the constants that are tied to a specific
output floating point format.
@asb asb force-pushed the 2023q3-compiler-rt-refactor-int-to-fp-1 branch from 324ecfb to 475a67d Compare October 15, 2023 15:11
@asb asb merged commit 6dfea56 into llvm:main Oct 15, 2023
1 of 3 checks passed
asb added a commit to asb/llvm-project that referenced this pull request Oct 17, 2023
Builds in llvm#66903, converting the rest of the low-hanging fruit to use
the common implementation.
asb added a commit that referenced this pull request Oct 18, 2023
…ion (#67540)

Builds on #66903, converting the rest of the low-hanging fruit to use
the common implementation.

See #67540 (comment) for links to Alive2 comparisons of before/after.
@strega-nil
Copy link
Contributor

This seems to have broken the build under MSVC:

C:\src\llvm-project\compiler-rt\lib\builtins\int_to_fp_impl.inc(36): error C2051: case expression not constant
C:\src\llvm-project\compiler-rt\lib\builtins\int_to_fp_impl.inc(39): error C2051: case expression not constant

strega-nil added a commit that referenced this pull request Oct 20, 2023
MSVC in C mode apparently doesn't consider `const int` to be
sufficiently constant expression

This was broken in #66903.
@itf
Copy link
Contributor

itf commented Oct 25, 2023

@asb This seems to have broken some things for -target arm64-apple-ios11.0-simulator
#69779

/int_to_fp_impl.inc:60:29: error: shift count is negative [-Werror,-Wshift-count-negative]
60 | const int dstExpBias = (1 << (dstExpBits - 1)) - 1;
| ^ ~~~~~~~~~~~~~~~~
1 error generated.

@asb
Copy link
Contributor Author

asb commented Oct 26, 2023

This seems to have broken the build under MSVC:

C:\src\llvm-project\compiler-rt\lib\builtins\int_to_fp_impl.inc(36): error C2051: case expression not constant
C:\src\llvm-project\compiler-rt\lib\builtins\int_to_fp_impl.inc(39): error C2051: case expression not constant

Thanks for fixing, I'm sorry I'd missed this - seems I had an issue with my email filter rules.

I'm really surprised the code in int_to_fp.h was problematic but fp_trunc_impl.inc isn't.

@asb
Copy link
Contributor Author

asb commented Oct 26, 2023

@asb This seems to have broken some things for -target arm64-apple-ios11.0-simulator #69779

/int_to_fp_impl.inc:60:29: error: shift count is negative [-Werror,-Wshift-count-negative] 60 | const int dstExpBias = (1 << (dstExpBits - 1)) - 1; | ^ ~~~~~~~~~~~~~~~~ 1 error generated.

Thank you and apologies for breaking your build, commented there with what I think the problem is.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants