-
Notifications
You must be signed in to change notification settings - Fork 12.8k
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
Lower assume(false) to an unreachable terminator #122610
Closed
Closed
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 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 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 |
---|---|---|
|
@@ -6,9 +6,22 @@ use super::FunctionCx; | |
use super::LocalRef; | ||
use crate::traits::*; | ||
|
||
use std::ops::ControlFlow; | ||
|
||
impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> { | ||
/// Lower a single MIR statement, returning [`ControlFlow::Break`] if we must not continue lowering | ||
/// the rest of the statements in the block. | ||
/// | ||
/// Since we are lowering a polymorphic MIR body, we can discover only at this point that we | ||
/// are lowering `assume(false)`. If we encounter such a statement, there is no reason to lower | ||
/// the rest of the block; we just emit an unreachable terminator and return | ||
/// [`ControlFlow::Break`]. | ||
#[instrument(level = "debug", skip(self, bx))] | ||
pub fn codegen_statement(&mut self, bx: &mut Bx, statement: &mir::Statement<'tcx>) { | ||
pub fn codegen_statement( | ||
&mut self, | ||
bx: &mut Bx, | ||
statement: &mir::Statement<'tcx>, | ||
) -> ControlFlow<()> { | ||
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. A brief comment about the meaning of the return value would be helpful. |
||
self.set_debug_loc(bx, statement.source_info); | ||
match statement.kind { | ||
mir::StatementKind::Assign(box (ref place, ref rvalue)) => { | ||
|
@@ -70,7 +83,17 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> { | |
mir::StatementKind::Intrinsic(box NonDivergingIntrinsic::Assume(ref op)) => { | ||
if !matches!(bx.tcx().sess.opts.optimize, OptLevel::No | OptLevel::Less) { | ||
let op_val = self.codegen_operand(bx, op); | ||
bx.assume(op_val.immediate()); | ||
let imm = op_val.immediate(); | ||
if let Some(value) = bx.const_to_opt_uint(imm) { | ||
// If we are lowering assume(false), just produce an unreachable | ||
// terminator. We don't emit anything for assume(true). | ||
if value == 0 { | ||
bx.unreachable(); | ||
return ControlFlow::Break(()); | ||
} | ||
} else { | ||
bx.assume(imm); | ||
} | ||
} | ||
} | ||
mir::StatementKind::Intrinsic(box NonDivergingIntrinsic::CopyNonOverlapping( | ||
|
@@ -97,5 +120,6 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> { | |
| mir::StatementKind::PlaceMention(..) | ||
| mir::StatementKind::Nop => {} | ||
} | ||
ControlFlow::Continue(()) | ||
} | ||
} |
This file contains 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,28 @@ | ||
// The lowering of the function below initially reads and writes to the entire pointee, even | ||
// though it only needs to do a store to the discriminant. | ||
// This test ensures that std::hint::unreachable_unchecked does not prevent the desired | ||
// optimization. | ||
|
||
//@ compile-flags: -O | ||
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. A brief comment about the point of this test would be helpful. |
||
|
||
#![crate_type = "lib"] | ||
|
||
use std::hint::unreachable_unchecked; | ||
use std::ptr::{read, write}; | ||
|
||
type T = [u8; 753]; | ||
|
||
pub enum State { | ||
A(T), | ||
B(T), | ||
} | ||
|
||
// CHECK-LABEL: @init(ptr {{.*}}s) | ||
// CHECK-NEXT: start | ||
// CHECK-NEXT: store i8 1, ptr %s, align 1 | ||
// CHECK-NEXT: ret void | ||
#[no_mangle] | ||
unsafe fn init(s: *mut State) { | ||
let State::A(v) = read(s) else { unreachable_unchecked() }; | ||
write(s, State::B(v)); | ||
} |
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.
Hmm, can you use a loop label and use it to break out of the outer loop? Avoiding the need for
replaced_terminator
entirely.