-
-
Notifications
You must be signed in to change notification settings - Fork 14.8k
Add should_skip_bounds_check to MIR Builder
#153602
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
Open
Bryntet
wants to merge
1
commit into
rust-lang:main
Choose a base branch
from
Bryntet:opt
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -91,7 +91,9 @@ use std::hash::{Hash, Hasher}; | |
|
|
||
| use either::Either; | ||
| use itertools::Itertools as _; | ||
| use rustc_abi::{self as abi, BackendRepr, FIRST_VARIANT, FieldIdx, Primitive, Size, VariantIdx}; | ||
| use rustc_abi::{ | ||
| self as abi, BackendRepr, FIRST_VARIANT, FieldIdx, Integer, Primitive, Size, VariantIdx, | ||
| }; | ||
| use rustc_arena::DroplessArena; | ||
| use rustc_const_eval::const_eval::DummyMachine; | ||
| use rustc_const_eval::interpret::{ | ||
|
|
@@ -108,7 +110,7 @@ use rustc_middle::bug; | |
| use rustc_middle::mir::interpret::{AllocRange, GlobalAlloc}; | ||
| use rustc_middle::mir::visit::*; | ||
| use rustc_middle::mir::*; | ||
| use rustc_middle::ty::layout::HasTypingEnv; | ||
| use rustc_middle::ty::layout::{HasTypingEnv, IntegerExt}; | ||
| use rustc_middle::ty::{self, Ty, TyCtxt}; | ||
| use rustc_span::DUMMY_SP; | ||
| use smallvec::SmallVec; | ||
|
|
@@ -1482,6 +1484,33 @@ impl<'body, 'a, 'tcx> VnState<'body, 'a, 'tcx> { | |
| (BinOp::Eq, a, b) if a == b => self.insert_bool(true), | ||
| (BinOp::Ne, Left(a), Left(b)) => self.insert_bool(a != b), | ||
| (BinOp::Ne, a, b) if a == b => self.insert_bool(false), | ||
| // When casting from a value, and comparing with a literal | ||
| // compare the maximum value with this literal | ||
| // to see if it's possible omit the runtime check | ||
| (BinOp::Lt, Right(a), Left(b)) if self.max_value_of_cast(a).is_some_and(|a| a < b) => { | ||
| self.insert_bool(true) | ||
| } | ||
| (BinOp::Lt, Left(a), Right(b)) if self.max_value_of_cast(b).is_some_and(|b| a >= b) => { | ||
| self.insert_bool(false) | ||
| } | ||
| (BinOp::Le, Right(a), Left(b)) if self.max_value_of_cast(a).is_some_and(|a| a <= b) => { | ||
| self.insert_bool(true) | ||
| } | ||
| (BinOp::Le, Left(a), Right(b)) if self.max_value_of_cast(b).is_some_and(|b| a > b) => { | ||
| self.insert_bool(false) | ||
| } | ||
| (BinOp::Gt, Left(a), Right(b)) if self.max_value_of_cast(b).is_some_and(|b| a > b) => { | ||
| self.insert_bool(true) | ||
| } | ||
| (BinOp::Gt, Right(a), Left(b)) if self.max_value_of_cast(a).is_some_and(|a| a <= b) => { | ||
| self.insert_bool(false) | ||
| } | ||
| (BinOp::Ge, Left(a), Right(b)) if self.max_value_of_cast(b).is_some_and(|b| a >= b) => { | ||
| self.insert_bool(true) | ||
| } | ||
| (BinOp::Ge, Right(a), Left(b)) if self.max_value_of_cast(a).is_some_and(|a| a < b) => { | ||
| self.insert_bool(false) | ||
| } | ||
| _ => return None, | ||
| }; | ||
|
|
||
|
|
@@ -1494,6 +1523,16 @@ impl<'body, 'a, 'tcx> VnState<'body, 'a, 'tcx> { | |
| } | ||
| } | ||
|
|
||
| fn max_value_of_cast(&self, value: VnIndex) -> Option<u128> { | ||
| if let Value::Cast { kind: CastKind::IntToInt, value } = self.get(value) { | ||
| self.max_value_of_cast(value) | ||
| } else if let ty::Uint(uty) = self.ty(value).kind() { | ||
| Some(Integer::from_uint_ty(&self.tcx, *uty).size().unsigned_int_max()) | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Here is the actual Size::unsigned_int_max() call
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
| } else { | ||
| None | ||
| } | ||
| } | ||
|
|
||
| fn simplify_cast( | ||
| &mut self, | ||
| initial_kind: &mut CastKind, | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,40 @@ | ||
| - // MIR for `array` before GVN | ||
| + // MIR for `array` after GVN | ||
|
|
||
| fn array(_1: [i32; 256], _2: u8) -> () { | ||
| debug input => _1; | ||
| debug lit => _2; | ||
| let mut _0: (); | ||
| let _3: i32; | ||
| let _4: usize; | ||
| let mut _5: u8; | ||
| let mut _6: bool; | ||
| scope 1 { | ||
| debug x => _3; | ||
| } | ||
|
|
||
| bb0: { | ||
| StorageLive(_3); | ||
| - StorageLive(_4); | ||
| + nop; | ||
| StorageLive(_5); | ||
| _5 = copy _2; | ||
| - _4 = move _5 as usize (IntToInt); | ||
| + _4 = copy _2 as usize (IntToInt); | ||
| StorageDead(_5); | ||
| - _6 = Lt(copy _4, const 256_usize); | ||
| - assert(move _6, "index out of bounds: the length is {} but the index is {}", const 256_usize, copy _4) -> [success: bb1, unwind continue]; | ||
| + _6 = const true; | ||
| + assert(const true, "index out of bounds: the length is {} but the index is {}", const 256_usize, copy _4) -> [success: bb1, unwind continue]; | ||
| } | ||
|
|
||
| bb1: { | ||
| _3 = copy _1[_4]; | ||
| - StorageDead(_4); | ||
| + nop; | ||
| _0 = const (); | ||
| StorageDead(_3); | ||
| return; | ||
| } | ||
| } | ||
|
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,40 @@ | ||
| - // MIR for `array` before GVN | ||
| + // MIR for `array` after GVN | ||
|
|
||
| fn array(_1: [i32; 256], _2: u8) -> () { | ||
| debug input => _1; | ||
| debug lit => _2; | ||
| let mut _0: (); | ||
| let _3: i32; | ||
| let _4: usize; | ||
| let mut _5: u8; | ||
| let mut _6: bool; | ||
| scope 1 { | ||
| debug x => _3; | ||
| } | ||
|
|
||
| bb0: { | ||
| StorageLive(_3); | ||
| - StorageLive(_4); | ||
| + nop; | ||
| StorageLive(_5); | ||
| _5 = copy _2; | ||
| - _4 = move _5 as usize (IntToInt); | ||
| + _4 = copy _2 as usize (IntToInt); | ||
| StorageDead(_5); | ||
| - _6 = Lt(copy _4, const 256_usize); | ||
| - assert(move _6, "index out of bounds: the length is {} but the index is {}", const 256_usize, copy _4) -> [success: bb1, unwind continue]; | ||
| + _6 = const true; | ||
| + assert(const true, "index out of bounds: the length is {} but the index is {}", const 256_usize, copy _4) -> [success: bb1, unwind continue]; | ||
| } | ||
|
|
||
| bb1: { | ||
| _3 = copy _1[_4]; | ||
| - StorageDead(_4); | ||
| + nop; | ||
| _0 = const (); | ||
| StorageDead(_3); | ||
| return; | ||
| } | ||
| } | ||
|
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,71 @@ | ||
| - // MIR for `ge` before GVN | ||
| + // MIR for `ge` after GVN | ||
|
|
||
| fn ge(_1: u32) -> () { | ||
| debug input => _1; | ||
| let mut _0: (); | ||
| let _2: bool; | ||
| let mut _3: u64; | ||
| let mut _4: u32; | ||
| let mut _6: u64; | ||
| let mut _7: u32; | ||
| let mut _8: u64; | ||
| let mut _10: u64; | ||
| let mut _11: u32; | ||
| scope 1 { | ||
| debug yes => _2; | ||
| let _5: bool; | ||
| scope 2 { | ||
| debug runtime => _5; | ||
| let _9: bool; | ||
| scope 3 { | ||
| debug no => _9; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| bb0: { | ||
| StorageLive(_2); | ||
| - StorageLive(_3); | ||
| + nop; | ||
| StorageLive(_4); | ||
| _4 = copy _1; | ||
| - _3 = move _4 as u64 (IntToInt); | ||
| + _3 = copy _1 as u64 (IntToInt); | ||
| StorageDead(_4); | ||
| - _2 = Ge(const U32_MAX_PLUS_ONE, move _3); | ||
| - StorageDead(_3); | ||
| + _2 = const true; | ||
| + nop; | ||
| StorageLive(_5); | ||
| StorageLive(_6); | ||
| StorageLive(_7); | ||
| _7 = copy _1; | ||
| - _6 = move _7 as u64 (IntToInt); | ||
| + _6 = copy _3; | ||
| StorageDead(_7); | ||
| StorageLive(_8); | ||
| - _8 = const core::num::<impl u32>::MAX as u64 (IntToInt); | ||
| - _5 = Ge(move _6, move _8); | ||
| + _8 = const 4294967295_u64; | ||
| + _5 = Ge(copy _3, const 4294967295_u64); | ||
| StorageDead(_8); | ||
| StorageDead(_6); | ||
| StorageLive(_9); | ||
| StorageLive(_10); | ||
| StorageLive(_11); | ||
| _11 = copy _1; | ||
| - _10 = move _11 as u64 (IntToInt); | ||
| + _10 = copy _3; | ||
| StorageDead(_11); | ||
| - _9 = Ge(move _10, const U32_MAX_PLUS_ONE); | ||
| + _9 = const false; | ||
| StorageDead(_10); | ||
| _0 = const (); | ||
| StorageDead(_9); | ||
| StorageDead(_5); | ||
| StorageDead(_2); | ||
| return; | ||
| } | ||
| } | ||
|
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,71 @@ | ||
| - // MIR for `ge` before GVN | ||
| + // MIR for `ge` after GVN | ||
|
|
||
| fn ge(_1: u32) -> () { | ||
| debug input => _1; | ||
| let mut _0: (); | ||
| let _2: bool; | ||
| let mut _3: u64; | ||
| let mut _4: u32; | ||
| let mut _6: u64; | ||
| let mut _7: u32; | ||
| let mut _8: u64; | ||
| let mut _10: u64; | ||
| let mut _11: u32; | ||
| scope 1 { | ||
| debug yes => _2; | ||
| let _5: bool; | ||
| scope 2 { | ||
| debug runtime => _5; | ||
| let _9: bool; | ||
| scope 3 { | ||
| debug no => _9; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| bb0: { | ||
| StorageLive(_2); | ||
| - StorageLive(_3); | ||
| + nop; | ||
| StorageLive(_4); | ||
| _4 = copy _1; | ||
| - _3 = move _4 as u64 (IntToInt); | ||
| + _3 = copy _1 as u64 (IntToInt); | ||
| StorageDead(_4); | ||
| - _2 = Ge(const U32_MAX_PLUS_ONE, move _3); | ||
| - StorageDead(_3); | ||
| + _2 = const true; | ||
| + nop; | ||
| StorageLive(_5); | ||
| StorageLive(_6); | ||
| StorageLive(_7); | ||
| _7 = copy _1; | ||
| - _6 = move _7 as u64 (IntToInt); | ||
| + _6 = copy _3; | ||
| StorageDead(_7); | ||
| StorageLive(_8); | ||
| - _8 = const core::num::<impl u32>::MAX as u64 (IntToInt); | ||
| - _5 = Ge(move _6, move _8); | ||
| + _8 = const 4294967295_u64; | ||
| + _5 = Ge(copy _3, const 4294967295_u64); | ||
| StorageDead(_8); | ||
| StorageDead(_6); | ||
| StorageLive(_9); | ||
| StorageLive(_10); | ||
| StorageLive(_11); | ||
| _11 = copy _1; | ||
| - _10 = move _11 as u64 (IntToInt); | ||
| + _10 = copy _3; | ||
| StorageDead(_11); | ||
| - _9 = Ge(move _10, const U32_MAX_PLUS_ONE); | ||
| + _9 = const false; | ||
| StorageDead(_10); | ||
| _0 = const (); | ||
| StorageDead(_9); | ||
| StorageDead(_5); | ||
| StorageDead(_2); | ||
| return; | ||
| } | ||
| } | ||
|
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,49 @@ | ||
| - // MIR for `gt` before GVN | ||
| + // MIR for `gt` after GVN | ||
|
|
||
| fn gt(_1: u8) -> () { | ||
| debug input => _1; | ||
| let mut _0: (); | ||
| let _2: bool; | ||
| let mut _3: u16; | ||
| let mut _4: u8; | ||
| let mut _6: u16; | ||
| let mut _7: u8; | ||
| scope 1 { | ||
| debug yes => _2; | ||
| let _5: bool; | ||
| scope 2 { | ||
| debug no => _5; | ||
| } | ||
| } | ||
|
|
||
| bb0: { | ||
| StorageLive(_2); | ||
| - StorageLive(_3); | ||
| + nop; | ||
| StorageLive(_4); | ||
| _4 = copy _1; | ||
| - _3 = move _4 as u16 (IntToInt); | ||
| + _3 = copy _1 as u16 (IntToInt); | ||
| StorageDead(_4); | ||
| - _2 = Gt(const 256_u16, move _3); | ||
| - StorageDead(_3); | ||
| + _2 = const true; | ||
| + nop; | ||
| StorageLive(_5); | ||
| StorageLive(_6); | ||
| StorageLive(_7); | ||
| _7 = copy _1; | ||
| - _6 = move _7 as u16 (IntToInt); | ||
| + _6 = copy _3; | ||
| StorageDead(_7); | ||
| - _5 = Gt(move _6, const 256_u16); | ||
| + _5 = const false; | ||
| StorageDead(_6); | ||
| _0 = const (); | ||
| StorageDead(_5); | ||
| StorageDead(_2); | ||
| return; | ||
| } | ||
| } | ||
|
|
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I still think you shouldn't need this match, by finding some method that'll do the right thing. Like if you can get to
Integerthe match would be replaced with https://doc.rust-lang.org/nightly/nightly-rustc/rustc_abi/enum.Integer.html#method.signed_max.Or
Size::from_bytes(int_ty.num_bytes()).signed_int_max()or something.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Whoops, this is actually an unrelated change to the PR that snuck in, this isn't the actual match I added but one I noticed existed while searching for the good way to get Size, like you said, I'll ping you in a comment on the cofe where I actually changed it for this PR