Skip to content

[ConstantRange] Fix miscompile caused by off by 1 bugs in UIToFP and SIToFP handling #8452

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 2 commits into from
Mar 22, 2024

Conversation

AZero13
Copy link

@AZero13 AZero13 commented Mar 21, 2024

We were passing the min and max values of the range to the ConstantRange
constructor, but the constructor expects the upper bound to 1
more than the max value so we need to add 1.

We also need to use getNonEmpty so that passing 0, 0 to the constructor
creates a full range rather than an empty range. And passing smin, smax+1
doesn't cause an assertion.

This is significant because this fixes a miscompile regarding Float2Int when
the float converts to a very large integer.

(cherry picked from commit llvm@1283646)

@AZero13 AZero13 requested a review from JDevlieghere as a code owner March 21, 2024 02:53
@AZero13 AZero13 changed the base branch from stable/20240123 to stable/20230725 March 21, 2024 02:54
@AZero13 AZero13 changed the title Fix off by 1 bugs in UIToFP and SIToFP handling [ConstantRange] Fix off by 1 bugs in UIToFP and SIToFP handling Mar 21, 2024
@AZero13 AZero13 changed the title [ConstantRange] Fix off by 1 bugs in UIToFP and SIToFP handling [ConstantRange] Fix miscompile caused by 1 bugs in UIToFP and SIToFP handling Mar 21, 2024
@AZero13
Copy link
Author

AZero13 commented Mar 21, 2024

See: https://godbolt.org/z/Kbsza5K63

@AZero13
Copy link
Author

AZero13 commented Mar 21, 2024

@adrian-prantl

@AZero13 AZero13 changed the title [ConstantRange] Fix miscompile caused by 1 bugs in UIToFP and SIToFP handling [ConstantRange] Fix miscompile caused by off by 1 bugs in UIToFP and SIToFP handling Mar 21, 2024
@JDevlieghere
Copy link

It looks like we have the same bug upstream. Any reason not to fix it there and then cherrypick the fix to the relevant Apple branches?

@AZero13
Copy link
Author

AZero13 commented Mar 21, 2024

It looks like we have the same bug upstream. Any reason not to fix it there and then cherrypick the fix to the relevant Apple branches?

It's merged upstream now.

@AZero13
Copy link
Author

AZero13 commented Mar 21, 2024

@JDevlieghere Now that this code is merged upstream and is being backported to the release branch.

@JDevlieghere
Copy link

Can you please cherrypick the upstream commit with -x? This helps engineers, our qualification team and automation trace this back to the corresponding upstream commit if they encounter any issues. Besides that this LGTM.

@JDevlieghere
Copy link

I meant cherrypicking the upstream commit to the branch for this PR with:

git cherry-pick -x 12836467b76c56872b4c22a6fd44bcda696ea720

The -x adds a little trailer at the bottom saying "cherry picked from commit 1283646". Here's an example of one of my PRs that showcases this: #8414

topperc added 2 commits March 21, 2024 13:40
The range for these operations is being constructed without the
maximum value for the range due to an incorrect usage of the
ConstantRange constructor.

This causes Float2Int to think the range for 'uitofp i1' only
contains 0 instead of 0 and 1.
…m#86041)

We were passing the min and max values of the range to the ConstantRange
constructor, but the constructor expects the upper bound to 1 more than
the max value so we need to add 1.

We also need to use getNonEmpty so that passing 0, 0 to the constructor
creates a full range rather than an empty range. And passing smin,
smax+1 doesn't cause an assertion.

I believe this fixes at least some of the reason llvm#79158 was reverted.
@AZero13
Copy link
Author

AZero13 commented Mar 21, 2024

I meant cherrypicking the upstream commit to the branch for this PR with:

git cherry-pick -x 12836467b76c56872b4c22a6fd44bcda696ea720

The -x adds a little trailer at the bottom saying "cherry picked from commit 1283646". Here's an example of one of my PRs that showcases this: #8414

Thank you!

@AZero13
Copy link
Author

AZero13 commented Mar 21, 2024

@JDevlieghere Is this good?

@AZero13
Copy link
Author

AZero13 commented Mar 21, 2024

I don't have perms to do swift ci tests or whatever by the way that's why I ask.

@adrian-prantl
Copy link

Could you please rebase this on swift/release/6.0 which automerges into stable/20230725 and change the target branch accordingly?

@AZero13 AZero13 closed this Mar 22, 2024
@AZero13
Copy link
Author

AZero13 commented Mar 22, 2024

Could you please rebase this on swift/release/6.0 which automerges into stable/20230725 and change the target branch accordingly?

Done!

@AZero13 AZero13 deleted the miscompile2 branch March 22, 2024 00:24
@AZero13 AZero13 restored the miscompile2 branch March 22, 2024 14:06
@AZero13 AZero13 reopened this Mar 22, 2024
@AZero13 AZero13 changed the base branch from stable/20230725 to swift/release/6.0 March 22, 2024 14:06
Copy link

@JDevlieghere JDevlieghere left a comment

Choose a reason for hiding this comment

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

Thanks for bearing with us!

@JDevlieghere
Copy link

@swift-ci test

@adrian-prantl
Copy link

@swift-ci test llvm

@adrian-prantl
Copy link

@swift-ci test windows

@AZero13
Copy link
Author

AZero13 commented Mar 22, 2024

@adrian-prantl @JDevlieghere Are these results okay?

@adrian-prantl adrian-prantl merged commit 6d5203d into swiftlang:swift/release/6.0 Mar 22, 2024
@AZero13 AZero13 deleted the miscompile2 branch March 22, 2024 22:08
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.

4 participants