Skip to content

Add a workaround for 128 bit switches#668

Merged
antoyo merged 1 commit into
rust-lang:masterfrom
FractalFir:int128_fix
May 10, 2025
Merged

Add a workaround for 128 bit switches#668
antoyo merged 1 commit into
rust-lang:masterfrom
FractalFir:int128_fix

Conversation

@FractalFir

@FractalFir FractalFir commented May 9, 2025

Copy link
Copy Markdown
Contributor

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

@antoyo antoyo left a comment

Copy link
Copy Markdown
Contributor

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.

Comment thread src/builder.rs Outdated
Comment thread src/builder.rs Outdated
Comment thread src/builder.rs Outdated
Comment thread src/builder.rs Outdated
Comment thread src/builder.rs Outdated
Comment thread src/builder.rs Outdated
Comment thread tests/run/switchint_128bit.rs
Comment thread example/mini_core.rs
@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