Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions compiler/rustc_ast_lowering/src/format.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use std::borrow::Cow;
use rustc_ast::*;
use rustc_data_structures::fx::FxIndexMap;
use rustc_hir as hir;
use rustc_middle::mir::interpret::PointerArithmetic;
use rustc_session::config::FmtDebug;
use rustc_span::{ByteSymbol, DesugaringKind, Ident, Span, Symbol, sym};

Expand Down Expand Up @@ -56,7 +57,7 @@ impl<'hir, R: ResolverAstLoweringExt<'hir>> LoweringContext<'_, 'hir, R> {
/// Get the maximum value of int_ty. It is platform-dependent due to the byte size of isize
fn int_ty_max(&self, int_ty: IntTy) -> u128 {
match int_ty {
IntTy::Isize => self.tcx.data_layout.pointer_size().signed_int_max() as u128,
IntTy::Isize => self.tcx.target_isize_max() as u128,
IntTy::I8 => i8::MAX as u128,
IntTy::I16 => i16::MAX as u128,
Copy link
Copy Markdown
Member

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 Integer the 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.

Copy link
Copy Markdown
Contributor Author

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

IntTy::I32 => i32::MAX as u128,
Expand All @@ -68,7 +69,7 @@ impl<'hir, R: ResolverAstLoweringExt<'hir>> LoweringContext<'_, 'hir, R> {
/// Get the maximum value of uint_ty. It is platform-dependent due to the byte size of usize
fn uint_ty_max(&self, uint_ty: UintTy) -> u128 {
match uint_ty {
UintTy::Usize => self.tcx.data_layout.pointer_size().unsigned_int_max(),
UintTy::Usize => self.tcx.target_usize_max() as u128,
UintTy::U8 => u8::MAX as u128,
UintTy::U16 => u16::MAX as u128,
UintTy::U32 => u32::MAX as u128,
Expand Down
43 changes: 41 additions & 2 deletions compiler/rustc_mir_transform/src/gvn.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::{
Expand All @@ -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;
Expand Down Expand Up @@ -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,
};

Expand All @@ -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())
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Here is the actual Size::unsigned_int_max() call

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

} else {
None
}
}

fn simplify_cast(
&mut self,
initial_kind: &mut CastKind,
Expand Down
40 changes: 40 additions & 0 deletions tests/mir-opt/const_prop/index_cast.array.GVN.32bit.diff
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;
}
}

40 changes: 40 additions & 0 deletions tests/mir-opt/const_prop/index_cast.array.GVN.64bit.diff
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;
}
}

71 changes: 71 additions & 0 deletions tests/mir-opt/const_prop/index_cast.ge.GVN.32bit.diff
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;
}
}

71 changes: 71 additions & 0 deletions tests/mir-opt/const_prop/index_cast.ge.GVN.64bit.diff
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;
}
}

49 changes: 49 additions & 0 deletions tests/mir-opt/const_prop/index_cast.gt.GVN.32bit.diff
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;
}
}

Loading
Loading