-
Notifications
You must be signed in to change notification settings - Fork 13.3k
Lower matches against constant slices into better MIR #112370
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
Closed
Closed
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,10 +15,13 @@ | |
use crate::build::expr::as_place::PlaceBuilder; | ||
use crate::build::matches::{Ascription, Binding, Candidate, MatchPair}; | ||
use crate::build::Builder; | ||
use rustc_data_structures::intern::Interned; | ||
use rustc_hir::RangeEnd; | ||
use rustc_middle::mir::ConstantKind; | ||
use rustc_middle::thir::{self, *}; | ||
use rustc_middle::ty; | ||
use rustc_middle::ty::layout::IntegerExt; | ||
use rustc_middle::ty::Const; | ||
use rustc_middle::ty::{self, Ty, ValTree}; | ||
use rustc_target::abi::{Integer, Size}; | ||
|
||
use std::mem; | ||
|
@@ -120,6 +123,38 @@ impl<'a, 'tcx> Builder<'a, 'tcx> { | |
} | ||
} | ||
|
||
fn try_build_valtree(&self, elements: &Box<[Box<Pat<'tcx>>]>) -> Option<ValTree<'tcx>> { | ||
if elements.iter().all(|p| { | ||
matches!( | ||
p.kind, | ||
PatKind::Constant { | ||
value: ConstantKind::Ty(ty::Const(Interned( | ||
ty::ConstData { kind: ty::ConstKind::Value(ty::ValTree::Leaf(_)), .. }, | ||
_ | ||
))) | ||
} | ||
) | ||
}) { | ||
// This ValTree represents the content of the simple array. | ||
// We then create a new pattern that matches against this ValTree. | ||
// This reduces a match against `[1, 2, 3, 4]` from 4 basic-blocks in the MIR to just 1 | ||
Some(ValTree::from_scalars( | ||
self.tcx, | ||
&elements | ||
.iter() | ||
.map(|p| match p.kind { | ||
PatKind::Constant { value: ConstantKind::Ty(c) } => { | ||
c.to_valtree().unwrap_leaf() | ||
} | ||
_ => unreachable!(), | ||
}) | ||
.collect::<Vec<_>>(), | ||
)) | ||
} else { | ||
None | ||
} | ||
} | ||
|
||
/// Given `candidate` that has a single or-pattern for its match-pairs, | ||
/// creates a fresh candidate for each of its input subpatterns passed via | ||
/// `pats`. | ||
|
@@ -243,22 +278,6 @@ impl<'a, 'tcx> Builder<'a, 'tcx> { | |
Err(match_pair) | ||
} | ||
|
||
PatKind::Slice { ref prefix, ref slice, ref suffix } => { | ||
if prefix.is_empty() && slice.is_some() && suffix.is_empty() { | ||
// irrefutable | ||
self.prefix_slice_suffix( | ||
&mut candidate.match_pairs, | ||
&match_pair.place, | ||
prefix, | ||
slice, | ||
suffix, | ||
); | ||
Ok(()) | ||
} else { | ||
Err(match_pair) | ||
} | ||
} | ||
|
||
PatKind::Variant { adt_def, args, variant_index, ref subpatterns } => { | ||
let irrefutable = adt_def.variants().iter_enumerated().all(|(i, v)| { | ||
i == variant_index || { | ||
|
@@ -281,6 +300,51 @@ impl<'a, 'tcx> Builder<'a, 'tcx> { | |
} | ||
} | ||
|
||
// Looks for simple array patterns such as `[1, 2, 3]` | ||
// They cannot have slices (e.g `[1, .., 3, 4]` is not simple), and | ||
// all the elements of the array must be leaf constants (so no strings!) | ||
PatKind::Array { ref prefix, slice: None, ref suffix } | ||
| PatKind::Slice { ref prefix, slice: None, ref suffix } | ||
if let Some(_val) = self.try_build_valtree(prefix) | ||
&& !prefix.is_empty() | ||
&& suffix.is_empty() => | ||
{ | ||
// This ValTree represents the content of the simple array. | ||
// We then create a new pattern that matches against this ValTree. | ||
// This reduces a match against `[1, 2, 3, 4]` from 4 basic-blocks in the MIR to just 1 | ||
let val = self.try_build_valtree(prefix).unwrap(); // FIXME: false positive | ||
|
||
let el_ty = prefix.iter().next().unwrap().ty; | ||
let (place, cnst_ty, pat_ty) = | ||
if match_pair.pattern.ty.is_slice() { | ||
( | ||
PlaceBuilder::from(match_pair.place.base()), | ||
Ty::new_imm_ref(tcx, tcx.lifetimes.re_erased, Ty::new_array(tcx, el_ty, prefix.len() as u64)), | ||
Ty::new_imm_ref(tcx, tcx.lifetimes.re_erased, Ty::new_slice(tcx, el_ty)) | ||
) | ||
} else { | ||
let arr_ty = Ty::new_array(tcx, el_ty, prefix.len() as u64); | ||
( | ||
match_pair.place, | ||
arr_ty, | ||
arr_ty | ||
) | ||
}; | ||
|
||
let cnst = Const::new(tcx, ty::ConstKind::Value(val), cnst_ty); | ||
let new_pat = Pat { | ||
ty: pat_ty, | ||
span: match_pair.pattern.span, | ||
kind: PatKind::Constant { value: ConstantKind::Ty(cnst) }, | ||
}; | ||
|
||
let pat = tcx.arena.alloc(new_pat); | ||
|
||
candidate.match_pairs.push(MatchPair::new(place, pat, self)); | ||
|
||
Ok(()) | ||
} | ||
|
||
PatKind::Array { ref prefix, ref slice, ref suffix } => { | ||
self.prefix_slice_suffix( | ||
&mut candidate.match_pairs, | ||
|
@@ -292,6 +356,22 @@ impl<'a, 'tcx> Builder<'a, 'tcx> { | |
Ok(()) | ||
} | ||
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. Should this special case be moved to |
||
|
||
PatKind::Slice { ref prefix, ref slice, ref suffix } => { | ||
if prefix.is_empty() && slice.is_some() && suffix.is_empty() { | ||
// irrefutable | ||
self.prefix_slice_suffix( | ||
&mut candidate.match_pairs, | ||
&match_pair.place, | ||
prefix, | ||
slice, | ||
suffix, | ||
); | ||
Ok(()) | ||
} else { | ||
Err(match_pair) | ||
} | ||
} | ||
|
||
PatKind::Leaf { ref subpatterns } => { | ||
// tuple struct, match subpats (if any) | ||
candidate.match_pairs.extend(self.field_match_pairs(match_pair.place, subpatterns)); | ||
|
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
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.
Why?
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.
this has still not been addressed