-
Notifications
You must be signed in to change notification settings - Fork 13.3k
Do some simple constant propagation in the ConstProp pass #60597
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
Changes from all commits
317daf7
b1c4fb2
45214ed
b17066d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
fn main() { | ||
let x: u32 = [0, 1, 2, 3][2]; | ||
} | ||
|
||
// END RUST SOURCE | ||
// START rustc.main.ConstProp.before.mir | ||
// bb0: { | ||
// ... | ||
// _2 = [const 0u32, const 1u32, const 2u32, const 3u32]; | ||
// ... | ||
// _3 = const 2usize; | ||
// _4 = const 4usize; | ||
// _5 = Lt(_3, _4); | ||
// assert(move _5, "index out of bounds: the len is move _4 but the index is _3") -> bb1; | ||
// } | ||
// bb1: { | ||
// _1 = _2[_3]; | ||
// ... | ||
// return; | ||
// } | ||
// END rustc.main.ConstProp.before.mir | ||
// START rustc.main.ConstProp.after.mir | ||
// bb0: { | ||
// ... | ||
// _5 = const true; | ||
// assert(move _5, "index out of bounds: the len is move _4 but the index is _3") -> bb1; | ||
// } | ||
// bb1: { | ||
// _1 = _2[_3]; | ||
// ... | ||
// return; | ||
// } | ||
// END rustc.main.ConstProp.after.mir |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
// compile-flags: -C overflow-checks=on | ||
|
||
fn main() { | ||
let x: u32 = 1 + 1; | ||
} | ||
|
||
// END RUST SOURCE | ||
// START rustc.main.ConstProp.before.mir | ||
// bb0: { | ||
// ... | ||
// _2 = CheckedAdd(const 1u32, const 1u32); | ||
// assert(!move (_2.1: bool), "attempt to add with overflow") -> bb1; | ||
// } | ||
// END rustc.main.ConstProp.before.mir | ||
// START rustc.main.ConstProp.after.mir | ||
// bb0: { | ||
// ... | ||
// _2 = (const 2u32, const false); | ||
// assert(!move (_2.1: bool), "attempt to add with overflow") -> bb1; | ||
// } | ||
// END rustc.main.ConstProp.after.mir |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
#[inline(never)] | ||
fn read(_: usize) { } | ||
|
||
fn main() { | ||
const FOO: &i32 = &1; | ||
let x = FOO as *const i32 as usize; | ||
read(x); | ||
} | ||
|
||
// END RUST SOURCE | ||
// START rustc.main.ConstProp.before.mir | ||
// bb0: { | ||
// ... | ||
// _3 = _4; | ||
// _2 = move _3 as *const i32 (Misc); | ||
// ... | ||
// _1 = move _2 as usize (Misc); | ||
// ... | ||
// _6 = _1; | ||
// _5 = const read(move _6) -> bb1; | ||
// } | ||
// END rustc.main.ConstProp.before.mir | ||
// START rustc.main.ConstProp.after.mir | ||
// bb0: { | ||
// ... | ||
// _3 = _4; | ||
// _2 = move _3 as *const i32 (Misc); | ||
// ... | ||
// _1 = move _2 as usize (Misc); | ||
// ... | ||
// _6 = _1; | ||
// _5 = const read(move _6) -> bb1; | ||
// } | ||
// END rustc.main.ConstProp.after.mir |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
fn test() -> &'static [u32] { | ||
&[1, 2] | ||
} | ||
|
||
fn main() { | ||
let x = test()[0]; | ||
} | ||
|
||
// END RUST SOURCE | ||
// START rustc.main.ConstProp.before.mir | ||
// bb1: { | ||
// ... | ||
// _3 = const 0usize; | ||
// _4 = Len((*_2)); | ||
// _5 = Lt(_3, _4); | ||
// assert(move _5, "index out of bounds: the len is move _4 but the index is _3") -> bb2; | ||
// } | ||
// bb2: { | ||
// _1 = (*_2)[_3]; | ||
// ... | ||
// return; | ||
// } | ||
// END rustc.main.ConstProp.before.mir | ||
// START rustc.main.ConstProp.after.mir | ||
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. Mir after const prop doesn't seem to be different from before. 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. That's correct. I just added this test to try to "lock in" more of the behavior. I have additional changes to improve the constant propagation code incoming but per @oli-obk's feedback, I wanted to keep this PR small. |
||
// bb0: { | ||
// ... | ||
// _3 = const 0usize; | ||
// _4 = Len((*_2)); | ||
// _5 = Lt(_3, _4); | ||
// assert(move _5, "index out of bounds: the len is move _4 but the index is _3") -> bb2; | ||
// } | ||
// bb2: { | ||
// _1 = (*_2)[_3]; | ||
// ... | ||
// return; | ||
// } | ||
// END rustc.main.ConstProp.after.mir |
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 don't see why this is needed, you should be able to create a
ty::Const
from any constant.