Skip to content

[SimplifyCFG] handle monotonic wrapped case for D150943 #65882

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 1 commit into from
Sep 14, 2023

Conversation

khei4
Copy link
Contributor

@khei4 khei4 commented Sep 10, 2023

This is a follow-up on the miscompile for wrapping the flag for the switch to the lookup table on SimplifyCFG. Checking the switch results' monotonicity isn't sufficient to say linear map calculation has no wrapping. Monotonicity is only necessary, and further checking whether wrapping happens on the maximum index is required to be sufficient.
For example( @mikaelholmen gave ), https://alive2.llvm.org/ce/z/So_fzw
Total linear map (-2, -1, 1, 2) |→ (-4, -2, 0, 2) is monotonic, but actual calculation contains wrapping (-2, -1, 1, 2) |→ (0, 1, 2, 3) (*2) |→ (0, 2, 4(wrapped), 6(wrapped)) (-4)|→ (-4, -2, 0, 2).

Also on the Linear map calculation, checking the multiplication wrapping with monotonicity is enough.(If the addition is only wrapped not with multiplication, contradicts with monotonicity. )

@khei4 khei4 requested review from nikic and mikaelholmen September 10, 2023 08:38
@khei4 khei4 requested a review from a team as a code owner September 10, 2023 08:38
@mikaelholmen
Copy link
Collaborator

Thanks @khei4 !
This looks good to me and I've verified that it solves the original miscompile problem I saw but I'd prefer if @nikic has a look too.

@djkoloski
Copy link

We ran into this on Fuchsia, which builds using tip-of-tree rustc. Once this is merged, can we get it added to the llvmorg-17.0.0-rc4 tag, or a new release candidate tag that I can pull the fix back up into rust?

@dianqk
Copy link
Member

dianqk commented Sep 11, 2023

We ran into this on Fuchsia, which builds using tip-of-tree rustc. Once this is merged, can we get it added to the llvmorg-17.0.0-rc4 tag, or a new release candidate tag that I can pull the fix back up into rust?

Bug fixes are always welcome. If you want to understand the process, refer to https://m.youtube.com/watch?v=onaNb2U1Od8.

@nikic
Copy link
Contributor

nikic commented Sep 12, 2023

Can you please update the PR description with more details about what the problematic case is an how this fixes it?

My understanding is that in this case we are mapping (after index adjustment) the indices (0, 1, 2, 3) to (-4, -2, 0, 2) so we have a monotonic sequence, but this is done by doing a multiply first, so we go through (0, 2, -4, -2) first, which wraps.

@khei4
Copy link
Contributor Author

khei4 commented Sep 12, 2023

Thank you for the review!

Can you please update the PR description with more details about what the problematic case is an how this fixes it?

Sure!

My understanding is that in this case we are mapping (after index adjustment) the indices (0, 1, 2, 3) to (-4, -2, 0, 2) so we have a monotonic sequence, but this is done by doing a multiply first, so we go through (0, 2, -4, -2) first, which wraps.

I think you are right.

@khei4 khei4 requested a review from nikic September 12, 2023 06:31
Copy link
Contributor

@nikic nikic left a comment

Choose a reason for hiding this comment

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

LGTM

Remove: explanation comments residues

Remove: redundant checking and clarify the variable

Rename: variable
@khei4 khei4 merged commit fef8249 into llvm:main Sep 14, 2023
@djkoloski
Copy link

djkoloski commented Sep 14, 2023

I'm having trouble backporting this to 17.x. I opened #66373 to backport it but it got closed automatically because the 17.x branch does not have the latest changes which remove the repo lockdown. So I opened #66375 to disable the repo lockdown on the 17.x branch but that was also closed automatically (I didn't know that GHA would run using the in-tree actions instead of the ones in the PR). Any suggestions? Should I go through Phabricator for this?

Sorry for posting here, I can't comment on any of the PRs I opened because they're locked.

@nikic
Copy link
Contributor

nikic commented Sep 14, 2023

@dianqk
Copy link
Member

dianqk commented Sep 14, 2023

@djkoloski You need to create an issue first. The presentation above has more details.
I can do this on the weekends if needed.

@djkoloski
Copy link

Thanks for the help everyone, sorry for the confusion. I've created issue #66396 so a release manager can decide whether this change should be added to the LLVM 17.0.X milestone.

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.

6 participants