-
Notifications
You must be signed in to change notification settings - Fork 13.5k
Make codegen choose whether to emit overflow checks #107921
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
+250
−217
Merged
Changes from all commits
Commits
Show all changes
19 commits
Select commit
Hold shift + click to select a range
4bd2ebc
Do not codegen overflow check when not required.
cjgillot e34caaf
Remove overflow checks from ConstProp.
cjgillot 86dbcb5
Add codegen test.
cjgillot d0cc00f
Add comment.
cjgillot 1480b1c
Correct comment.
cjgillot 7567f1f
Adapt interpreter.
cjgillot 4c93145
Remove outdated comment.
cjgillot 7f36a3f
Fix CTFE interpreter.
cjgillot 085eaa7
Adapt interpreter.
cjgillot fb1f7f7
Remove exception from MIR doc.
cjgillot 328696c
Update compiler/rustc_middle/src/mir/syntax.rs
cjgillot 4f13aa7
Comment codegen optimization.
cjgillot e9c73ea
Make name more explicit.
cjgillot a576919
Remove special case in rvalue codegen.
cjgillot c1a0f82
Rename checked_binop_checks_overflow.
cjgillot c107e0e
Fix codegen test.
cjgillot f79db59
Adapt cg_clif.
cjgillot 7e795bd
Replace _with_overflow instrinsics in LowerIntrinsics.
cjgillot 9f6c1df
Stop implementing _with_overflow intrinsics in codegen backends.
cjgillot 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
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
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
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
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
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
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
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 |
---|---|---|
@@ -0,0 +1,14 @@ | ||
// compile-flags: -Zmir-enable-passes=+Inline,+ConstProp --crate-type lib | ||
// revisions: ASSERT NOASSERT | ||
//[ASSERT] compile-flags: -Coverflow-checks=on | ||
//[NOASSERT] compile-flags: -Coverflow-checks=off | ||
|
||
// CHECK-LABEL: define{{.*}} @assertion | ||
// ASSERT: call void @_ZN4core9panicking5panic17h | ||
// NOASSERT: ret i8 0 | ||
#[no_mangle] | ||
pub fn assertion() -> u8 { | ||
// Optimized MIR will replace this `CheckedBinaryOp` by `const (0, true)`. | ||
// Verify that codegen does or does not emit the panic. | ||
<u8 as std::ops::Add>::add(255, 1) | ||
} |
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
39 changes: 39 additions & 0 deletions
39
tests/mir-opt/const_prop/inherit_overflow.main.ConstProp.diff
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,39 @@ | ||
- // MIR for `main` before ConstProp | ||
+ // MIR for `main` after ConstProp | ||
|
||
fn main() -> () { | ||
let mut _0: (); // return place in scope 0 at $DIR/inherit_overflow.rs:+0:11: +0:11 | ||
let mut _1: u8; // in scope 0 at $DIR/inherit_overflow.rs:+3:13: +3:47 | ||
let mut _2: u8; // in scope 0 at $DIR/inherit_overflow.rs:+3:13: +3:47 | ||
let mut _3: u8; // in scope 0 at $DIR/inherit_overflow.rs:+3:13: +3:47 | ||
scope 1 { | ||
} | ||
scope 2 (inlined <u8 as Add>::add) { // at $DIR/inherit_overflow.rs:8:13: 8:47 | ||
debug self => _2; // in scope 2 at $SRC_DIR/core/src/ops/arith.rs:LL:COL | ||
debug other => _3; // in scope 2 at $SRC_DIR/core/src/ops/arith.rs:LL:COL | ||
let mut _4: (u8, bool); // in scope 2 at $SRC_DIR/core/src/ops/arith.rs:LL:COL | ||
} | ||
|
||
bb0: { | ||
StorageLive(_1); // scope 0 at $DIR/inherit_overflow.rs:+3:13: +3:47 | ||
StorageLive(_2); // scope 0 at $DIR/inherit_overflow.rs:+3:13: +3:47 | ||
_2 = const u8::MAX; // scope 0 at $DIR/inherit_overflow.rs:+3:13: +3:47 | ||
StorageLive(_3); // scope 0 at $DIR/inherit_overflow.rs:+3:13: +3:47 | ||
_3 = const 1_u8; // scope 0 at $DIR/inherit_overflow.rs:+3:13: +3:47 | ||
- _4 = CheckedAdd(_2, _3); // scope 2 at $SRC_DIR/core/src/ops/arith.rs:LL:COL | ||
- assert(!move (_4.1: bool), "attempt to compute `{} + {}`, which would overflow", _2, _3) -> bb1; // scope 2 at $SRC_DIR/core/src/ops/arith.rs:LL:COL | ||
+ _4 = const (0_u8, true); // scope 2 at $SRC_DIR/core/src/ops/arith.rs:LL:COL | ||
+ assert(!const true, "attempt to compute `{} + {}`, which would overflow", _2, _3) -> bb1; // scope 2 at $SRC_DIR/core/src/ops/arith.rs:LL:COL | ||
} | ||
|
||
bb1: { | ||
- _1 = move (_4.0: u8); // scope 2 at $SRC_DIR/core/src/ops/arith.rs:LL:COL | ||
+ _1 = const 0_u8; // scope 2 at $SRC_DIR/core/src/ops/arith.rs:LL:COL | ||
StorageDead(_3); // scope 0 at $DIR/inherit_overflow.rs:+3:13: +3:47 | ||
StorageDead(_2); // scope 0 at $DIR/inherit_overflow.rs:+3:13: +3:47 | ||
StorageDead(_1); // scope 0 at $DIR/inherit_overflow.rs:+3:47: +3:48 | ||
_0 = const (); // scope 0 at $DIR/inherit_overflow.rs:+0:11: +4:2 | ||
return; // scope 0 at $DIR/inherit_overflow.rs:+4:2: +4:2 | ||
} | ||
} | ||
|
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,9 @@ | ||
// unit-test: ConstProp | ||
// compile-flags: -Zmir-enable-passes=+Inline | ||
|
||
// EMIT_MIR inherit_overflow.main.ConstProp.diff | ||
fn main() { | ||
// After inlining, this will contain a `CheckedBinaryOp`. | ||
// Propagating the overflow is ok as codegen will just skip emitting the panic. | ||
let _ = <u8 as std::ops::Add>::add(255, 1); | ||
} |
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
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 |
---|---|---|
@@ -1,8 +1,9 @@ | ||
// compile-flags: -Zunsound-mir-opts | ||
// unit-test: DataflowConstProp | ||
// compile-flags: -Zmir-enable-passes=+Inline | ||
|
||
// EMIT_MIR inherit_overflow.main.DataflowConstProp.diff | ||
fn main() { | ||
// After inlining, this will contain a `CheckedBinaryOp`. The overflow | ||
// must be ignored by the constant propagation to avoid triggering a panic. | ||
// After inlining, this will contain a `CheckedBinaryOp`. | ||
// Propagating the overflow is ok as codegen will just skip emitting the panic. | ||
let _ = <u8 as std::ops::Add>::add(255, 1); | ||
} |
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
83 changes: 83 additions & 0 deletions
83
tests/mir-opt/lower_intrinsics.with_overflow.LowerIntrinsics.diff
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,83 @@ | ||
- // MIR for `with_overflow` before LowerIntrinsics | ||
+ // MIR for `with_overflow` after LowerIntrinsics | ||
|
||
fn with_overflow(_1: i32, _2: i32) -> () { | ||
debug a => _1; // in scope 0 at $DIR/lower_intrinsics.rs:+0:22: +0:23 | ||
debug b => _2; // in scope 0 at $DIR/lower_intrinsics.rs:+0:30: +0:31 | ||
let mut _0: (); // return place in scope 0 at $DIR/lower_intrinsics.rs:+0:38: +0:38 | ||
let _3: (i32, bool); // in scope 0 at $DIR/lower_intrinsics.rs:+1:9: +1:11 | ||
let mut _4: i32; // in scope 0 at $DIR/lower_intrinsics.rs:+1:50: +1:51 | ||
let mut _5: i32; // in scope 0 at $DIR/lower_intrinsics.rs:+1:53: +1:54 | ||
let mut _7: i32; // in scope 0 at $DIR/lower_intrinsics.rs:+2:50: +2:51 | ||
let mut _8: i32; // in scope 0 at $DIR/lower_intrinsics.rs:+2:53: +2:54 | ||
let mut _10: i32; // in scope 0 at $DIR/lower_intrinsics.rs:+3:50: +3:51 | ||
let mut _11: i32; // in scope 0 at $DIR/lower_intrinsics.rs:+3:53: +3:54 | ||
scope 1 { | ||
debug _x => _3; // in scope 1 at $DIR/lower_intrinsics.rs:+1:9: +1:11 | ||
let _6: (i32, bool); // in scope 1 at $DIR/lower_intrinsics.rs:+2:9: +2:11 | ||
scope 2 { | ||
debug _y => _6; // in scope 2 at $DIR/lower_intrinsics.rs:+2:9: +2:11 | ||
let _9: (i32, bool); // in scope 2 at $DIR/lower_intrinsics.rs:+3:9: +3:11 | ||
scope 3 { | ||
debug _z => _9; // in scope 3 at $DIR/lower_intrinsics.rs:+3:9: +3:11 | ||
} | ||
} | ||
} | ||
|
||
bb0: { | ||
StorageLive(_3); // scope 0 at $DIR/lower_intrinsics.rs:+1:9: +1:11 | ||
StorageLive(_4); // scope 0 at $DIR/lower_intrinsics.rs:+1:50: +1:51 | ||
_4 = _1; // scope 0 at $DIR/lower_intrinsics.rs:+1:50: +1:51 | ||
StorageLive(_5); // scope 0 at $DIR/lower_intrinsics.rs:+1:53: +1:54 | ||
_5 = _2; // scope 0 at $DIR/lower_intrinsics.rs:+1:53: +1:54 | ||
- _3 = add_with_overflow::<i32>(move _4, move _5) -> bb1; // scope 0 at $DIR/lower_intrinsics.rs:+1:14: +1:55 | ||
- // mir::Constant | ||
- // + span: $DIR/lower_intrinsics.rs:78:14: 78:49 | ||
- // + literal: Const { ty: extern "rust-intrinsic" fn(i32, i32) -> (i32, bool) {add_with_overflow::<i32>}, val: Value(<ZST>) } | ||
+ _3 = CheckedAdd(move _4, move _5); // scope 0 at $DIR/lower_intrinsics.rs:+1:14: +1:55 | ||
+ goto -> bb1; // scope 0 at $DIR/lower_intrinsics.rs:+1:14: +1:55 | ||
} | ||
|
||
bb1: { | ||
StorageDead(_5); // scope 0 at $DIR/lower_intrinsics.rs:+1:54: +1:55 | ||
StorageDead(_4); // scope 0 at $DIR/lower_intrinsics.rs:+1:54: +1:55 | ||
StorageLive(_6); // scope 1 at $DIR/lower_intrinsics.rs:+2:9: +2:11 | ||
StorageLive(_7); // scope 1 at $DIR/lower_intrinsics.rs:+2:50: +2:51 | ||
_7 = _1; // scope 1 at $DIR/lower_intrinsics.rs:+2:50: +2:51 | ||
StorageLive(_8); // scope 1 at $DIR/lower_intrinsics.rs:+2:53: +2:54 | ||
_8 = _2; // scope 1 at $DIR/lower_intrinsics.rs:+2:53: +2:54 | ||
- _6 = sub_with_overflow::<i32>(move _7, move _8) -> bb2; // scope 1 at $DIR/lower_intrinsics.rs:+2:14: +2:55 | ||
- // mir::Constant | ||
- // + span: $DIR/lower_intrinsics.rs:79:14: 79:49 | ||
- // + literal: Const { ty: extern "rust-intrinsic" fn(i32, i32) -> (i32, bool) {sub_with_overflow::<i32>}, val: Value(<ZST>) } | ||
+ _6 = CheckedSub(move _7, move _8); // scope 1 at $DIR/lower_intrinsics.rs:+2:14: +2:55 | ||
+ goto -> bb2; // scope 1 at $DIR/lower_intrinsics.rs:+2:14: +2:55 | ||
} | ||
|
||
bb2: { | ||
StorageDead(_8); // scope 1 at $DIR/lower_intrinsics.rs:+2:54: +2:55 | ||
StorageDead(_7); // scope 1 at $DIR/lower_intrinsics.rs:+2:54: +2:55 | ||
StorageLive(_9); // scope 2 at $DIR/lower_intrinsics.rs:+3:9: +3:11 | ||
StorageLive(_10); // scope 2 at $DIR/lower_intrinsics.rs:+3:50: +3:51 | ||
_10 = _1; // scope 2 at $DIR/lower_intrinsics.rs:+3:50: +3:51 | ||
StorageLive(_11); // scope 2 at $DIR/lower_intrinsics.rs:+3:53: +3:54 | ||
_11 = _2; // scope 2 at $DIR/lower_intrinsics.rs:+3:53: +3:54 | ||
- _9 = mul_with_overflow::<i32>(move _10, move _11) -> bb3; // scope 2 at $DIR/lower_intrinsics.rs:+3:14: +3:55 | ||
- // mir::Constant | ||
- // + span: $DIR/lower_intrinsics.rs:80:14: 80:49 | ||
- // + literal: Const { ty: extern "rust-intrinsic" fn(i32, i32) -> (i32, bool) {mul_with_overflow::<i32>}, val: Value(<ZST>) } | ||
+ _9 = CheckedMul(move _10, move _11); // scope 2 at $DIR/lower_intrinsics.rs:+3:14: +3:55 | ||
+ goto -> bb3; // scope 2 at $DIR/lower_intrinsics.rs:+3:14: +3:55 | ||
} | ||
|
||
bb3: { | ||
StorageDead(_11); // scope 2 at $DIR/lower_intrinsics.rs:+3:54: +3:55 | ||
StorageDead(_10); // scope 2 at $DIR/lower_intrinsics.rs:+3:54: +3:55 | ||
_0 = const (); // scope 0 at $DIR/lower_intrinsics.rs:+0:38: +4:2 | ||
StorageDead(_9); // scope 2 at $DIR/lower_intrinsics.rs:+4:1: +4:2 | ||
StorageDead(_6); // scope 1 at $DIR/lower_intrinsics.rs:+4:1: +4:2 | ||
StorageDead(_3); // scope 0 at $DIR/lower_intrinsics.rs:+4:1: +4:2 | ||
return; // scope 0 at $DIR/lower_intrinsics.rs:+4:2: +4:2 | ||
} | ||
} | ||
|
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.
Uh oh!
There was an error while loading. Please reload this page.