-
Notifications
You must be signed in to change notification settings - Fork 13.1k
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 matches against constant slices into better MIR #112370
Conversation
r? @eholk (rustbot has picked a reviewer for you, use r? to override) |
This comment has been minimized.
This comment has been minimized.
|
Okay, I have somehow managed to get the basic test case for this working with the expected MIR generation. The basic idea is (this still may be the entirely wrong way to do this):
|
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
r? mir-opt |
Some changes occurred in compiler/rustc_codegen_gcc cc @antoyo |
This comment has been minimized.
This comment has been minimized.
@@ -35,7 +35,7 @@ extern crate rustc_middle; | |||
extern crate rustc_session; | |||
extern crate rustc_span; | |||
extern crate rustc_target; | |||
extern crate tempfile; | |||
|
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.
tempfile
is used at line 89 so it should not be removed.
Not sure why there's a warning emitted about this, but removing this line will result in a compilation error.
☔ The latest upstream changes (presumably #113134) made this pull request unmergeable. Please resolve the merge conflicts. |
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 looks like the right place to put this change. Could you add a MIR building test, in the tests/mir-opt/build directory, so we can see the results of this change?
@@ -18,7 +18,7 @@ pub use valtree::*; | |||
/// Use this rather than `ConstData`, whenever possible. | |||
#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, HashStable)] | |||
#[rustc_pass_by_value] | |||
pub struct Const<'tcx>(pub(super) Interned<'tcx, ConstData<'tcx>>); | |||
pub struct Const<'tcx>(pub Interned<'tcx, ConstData<'tcx>>); |
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
)); | ||
|
||
Ok(()) | ||
} |
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.
Should this special case be moved to prefix_slice_suffix
? Once for the prefix and once for the suffix?
|
||
candidate.match_pairs.push(MatchPair::new( | ||
place, | ||
Box::leak(Box::new(new_pat)), // FIXME: 'tcx lifetime for pat |
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.
You should add rustc_middle::thir::Pat
to the list in rustc_middle::arena
, and use tcx.arena.alloc
.
cc @oli-obk as you manipulated that code recently. |
@john-h-k any updates on this? |
Sorry have been super busy the last few weeks - I am currently working on this yes |
63581fb
to
0c77221
Compare
Some changes occurred in src/tools/cargo cc @ehuss |
46be8c1
to
a8a195d
Compare
#[inline] | ||
pub fn new(tcx: TyCtxt<'tcx>, kind: ty::ConstKind<'tcx>, ty: Ty<'tcx>) -> Const<'tcx> { | ||
tcx.mk_ct_from_kind(kind, ty) | ||
} | ||
|
||
#[inline] | ||
pub fn new_param(tcx: TyCtxt<'tcx>, param: ty::ParamConst, ty: Ty<'tcx>) -> Const<'tcx> { | ||
Const::new(tcx, ty::ConstKind::Param(param), ty) | ||
} | ||
|
||
#[inline] | ||
pub fn new_var(tcx: TyCtxt<'tcx>, infer: ty::ConstVid<'tcx>, ty: Ty<'tcx>) -> Const<'tcx> { | ||
Const::new(tcx, ty::ConstKind::Infer(ty::InferConst::Var(infer)), ty) | ||
} | ||
|
||
#[inline] | ||
pub fn new_fresh(tcx: TyCtxt<'tcx>, fresh: u32, ty: Ty<'tcx>) -> Const<'tcx> { | ||
Const::new(tcx, ty::ConstKind::Infer(ty::InferConst::Fresh(fresh)), ty) | ||
} | ||
|
||
#[inline] | ||
pub fn new_infer(tcx: TyCtxt<'tcx>, infer: ty::InferConst<'tcx>, ty: Ty<'tcx>) -> Const<'tcx> { | ||
Const::new(tcx, ty::ConstKind::Infer(infer), ty) | ||
} | ||
|
||
#[inline] | ||
pub fn new_bound( | ||
tcx: TyCtxt<'tcx>, | ||
debruijn: ty::DebruijnIndex, | ||
var: ty::BoundVar, | ||
ty: Ty<'tcx>, | ||
) -> Const<'tcx> { | ||
Const::new(tcx, ty::ConstKind::Bound(debruijn, var), ty) | ||
} | ||
|
||
#[inline] | ||
pub fn new_placeholder( | ||
tcx: TyCtxt<'tcx>, | ||
placeholder: ty::PlaceholderConst<'tcx>, | ||
ty: Ty<'tcx>, | ||
) -> Const<'tcx> { | ||
Const::new(tcx, ty::ConstKind::Placeholder(placeholder), ty) | ||
} | ||
|
||
#[inline] | ||
pub fn new_unevaluated( | ||
tcx: TyCtxt<'tcx>, | ||
uv: ty::UnevaluatedConst<'tcx>, | ||
ty: Ty<'tcx>, | ||
) -> Const<'tcx> { | ||
Const::new(tcx, ty::ConstKind::Unevaluated(uv), ty) | ||
} | ||
|
||
#[inline] | ||
pub fn new_value(tcx: TyCtxt<'tcx>, val: ty::ValTree<'tcx>, ty: Ty<'tcx>) -> Const<'tcx> { | ||
Const::new(tcx, ty::ConstKind::Value(val), ty) | ||
} | ||
|
||
#[inline] | ||
pub fn new_expr(tcx: TyCtxt<'tcx>, expr: ty::Expr<'tcx>, ty: Ty<'tcx>) -> Const<'tcx> { | ||
Const::new(tcx, ty::ConstKind::Expr(expr), ty) | ||
} | ||
|
||
#[inline] | ||
pub fn new_error(tcx: TyCtxt<'tcx>, e: ty::ErrorGuaranteed, ty: Ty<'tcx>) -> Const<'tcx> { | ||
Const::new(tcx, ty::ConstKind::Error(e), ty) | ||
} | ||
|
||
/// Like [Ty::new_error] but for constants. | ||
#[track_caller] | ||
pub fn new_misc_error(tcx: TyCtxt<'tcx>, ty: Ty<'tcx>) -> Const<'tcx> { | ||
Const::new_error_with_message( | ||
tcx, | ||
ty, | ||
DUMMY_SP, | ||
"ty::ConstKind::Error constructed but no error reported", | ||
) | ||
} |
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 should probably be its own PR, so we aren't mixing a restructuring with a behaviour change
69dbd6b
to
1cdaa59
Compare
This comment has been minimized.
This comment has been minimized.
50a2655
to
89ff2b2
Compare
This comment has been minimized.
This comment has been minimized.
@rustbot review |
Please add a mir-opt test that shows the bad MIR in a commit before your main commit (which should also get a meaningful commit message). Then your commit will change the MIR of the new test, and thus demonstrate how your PR changes things. |
@rustbot author |
☔ The latest upstream changes (presumably #116027) made this pull request unmergeable. Please resolve the merge conflicts. |
Co-authored-by: Oli Scherer <github35764891676564198441@oli-obk.de>
@john-h-k FYI: when a PR is ready for review, send a message containing |
Ping from triage: I'm closing this due to inactivity, Please reopen when you are ready to continue with this. @rustbot label: +S-inactive |
Fixes #110870 (eventually)
The approach I've been trying so far is lowering
PatKind::Array
(when it is a fixed array with only constants in it) to a constant pattern inmir_build
. Currently there are two issues here:Pat
into thetcx
to get the lifetimes right (hence theBox::leak
hack)I will continue toying around with this but wanted to open the PR to see if this is the wrong way of doing it. Should I be doing it in a different MIR stage/a MIR opt pass?