Skip to content

Add a workaround for 128 bit switches #668

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
May 10, 2025

Conversation

FractalFir
Copy link
Contributor

@FractalFir FractalFir commented May 9, 2025

Due to a libgccjit limitation, 128 bit switches are currently not supported.

As a workaround, this PR:

  1. Detects switches on 16 byte(128 bit) values.
  2. Replaces such switches with an "if ladder".

Effectively, we turn this:

let val = black_box(64_u128);
match val {
    0 => return 1,
    1 => return 2,
    64 => (),
    _ => return 3,
}

Into this:

let val = black_box(64_u128);
if val == 0{
  return 1;
}
else if val == 1{
   return 2;
}
else if val ==   64 {
} else {
   return 3; 
}

This is less efficient than a switch, but it only applies to previously unsupported edge case: 128 bit switches. So, the small perf penalty is acceptable in this case.

Besides that, this PR contains a regression test for this issue, and a few tiny changes to minicore.

  1. Added support for the black_box intrinsic - it is strictly necessary for correct tests. Even now, MIR optimizations sometimes optimize the crash I test for away. Using black_box guarantees we will not miss any issues in the future.

  2. Added copy impls for i64, u128, and i128. The u128 impl was needed to properly test 128 bit switches. Adding one for i128 too seemed reasonable.

@FractalFir FractalFir force-pushed the int128_fix branch 2 times, most recently from 056b3ae to 9137de1 Compare May 9, 2025 21:14
Copy link
Contributor

@antoyo antoyo left a comment

Choose a reason for hiding this comment

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

Thanks a lot!
Nice work as always.

A few nitpicks.

@antoyo antoyo merged commit 3cffea7 into rust-lang:master May 10, 2025
37 checks passed
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.

2 participants