-
-
Notifications
You must be signed in to change notification settings - Fork 14.4k
[MIR] Deaggregate structs to enable further optimizations #35168
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
Merged
Changes from 1 commit
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
62cdbea
deaggregate structs to enable further optimization
scottcarr bda46c2
reduce rightward drift, add fixme
scottcarr e8bfba7
fix field type, add test
scottcarr d918c99
add hashtag to emphasis its a gh issue
scottcarr d5908a3
run mir opt test with mir-opt-level=3 so they fire
scottcarr 06acf16
reduce rightward drift, add precondition comment
scottcarr 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -31,55 +31,55 @@ impl<'tcx> MirPass<'tcx> for Deaggregator { | |
| None => { return; }, | ||
| _ => {} | ||
| }; | ||
|
|
||
| // Do not trigger on constants. Could be revised in future | ||
| if let MirSource::Fn(_) = source {} else { return; } | ||
|
|
||
| let mut curr: usize = 0; | ||
| for bb in mir.basic_blocks_mut() { | ||
| while let Some(idx) = get_aggregate_statement(curr, &bb.statements) { | ||
| // do the replacement | ||
| debug!("removing statement {:?}", idx); | ||
| let src_info = bb.statements[idx].source_info; | ||
| let mut suffix_stmts = bb.statements.split_off(idx); | ||
| let orig_stmt = suffix_stmts.remove(0); | ||
| let StatementKind::Assign(ref lhs, ref rhs) = orig_stmt.kind; | ||
| if let &Rvalue::Aggregate(ref agg_kind, ref operands) = rhs { | ||
| if let &AggregateKind::Adt(adt_def, variant, substs) = agg_kind { | ||
| let n = bb.statements.len(); | ||
| bb.statements.reserve(n + operands.len() + suffix_stmts.len()); | ||
| for (i, op) in operands.iter().enumerate() { | ||
| let ref variant_def = adt_def.variants[variant]; | ||
| let ty = variant_def.fields[variant].ty(tcx, substs); | ||
| let rhs = Rvalue::Use(op.clone()); | ||
| let idx = match get_aggregate_statement(curr, &bb.statements) { | ||
| Some(idx) => idx, | ||
| None => continue, | ||
| }; | ||
| // do the replacement | ||
| debug!("removing statement {:?}", idx); | ||
| let src_info = bb.statements[idx].source_info; | ||
| let suffix_stmts = bb.statements.split_off(idx+1); | ||
| let orig_stmt = bb.statements.pop().unwrap(); | ||
| let StatementKind::Assign(ref lhs, ref rhs) = orig_stmt.kind; | ||
| let (agg_kind, operands) = match rhs { | ||
| &Rvalue::Aggregate(ref agg_kind, ref operands) => (agg_kind, operands), | ||
| _ => span_bug!(src_info.span, "expected aggregate, not {:?}", rhs), | ||
| }; | ||
| let (adt_def, variant, substs) = match agg_kind { | ||
| &AggregateKind::Adt(adt_def, variant, substs) => (adt_def, variant, substs), | ||
| _ => span_bug!(src_info.span, "expected struct, not {:?}", rhs), | ||
| }; | ||
| let n = bb.statements.len(); | ||
| bb.statements.reserve(n + operands.len() + suffix_stmts.len()); | ||
| for (i, op) in operands.iter().enumerate() { | ||
| let ref variant_def = adt_def.variants[variant]; | ||
| let ty = variant_def.fields[variant].ty(tcx, substs); | ||
| let rhs = Rvalue::Use(op.clone()); | ||
|
|
||
| // since we don't handle enums, we don't need a cast | ||
| let lhs_cast = lhs.clone(); | ||
| // since we don't handle enums, we don't need a cast | ||
| let lhs_cast = lhs.clone(); | ||
|
|
||
| // if we handled enums: | ||
| // let lhs_cast = if adt_def.variants.len() > 1 { | ||
| // Lvalue::Projection(Box::new(LvalueProjection { | ||
| // base: ai.lhs.clone(), | ||
| // elem: ProjectionElem::Downcast(ai.adt_def, ai.variant), | ||
| // })) | ||
| // } else { | ||
| // lhs_cast | ||
| // }; | ||
| // FIXME we cannot deaggregate enums issue: 35186 | ||
|
||
|
|
||
| let lhs_proj = Lvalue::Projection(Box::new(LvalueProjection { | ||
| base: lhs_cast, | ||
| elem: ProjectionElem::Field(Field::new(i), ty), | ||
| })); | ||
| let new_statement = Statement { | ||
| source_info: src_info, | ||
| kind: StatementKind::Assign(lhs_proj, rhs), | ||
| }; | ||
| debug!("inserting: {:?} @ {:?}", new_statement, idx + i); | ||
| bb.statements.push(new_statement); | ||
| } | ||
| curr = bb.statements.len(); | ||
| bb.statements.extend(suffix_stmts); | ||
| } | ||
| } | ||
| let lhs_proj = Lvalue::Projection(Box::new(LvalueProjection { | ||
| base: lhs_cast, | ||
| elem: ProjectionElem::Field(Field::new(i), ty), | ||
| })); | ||
| let new_statement = Statement { | ||
| source_info: src_info, | ||
| kind: StatementKind::Assign(lhs_proj, rhs), | ||
| }; | ||
| debug!("inserting: {:?} @ {:?}", new_statement, idx + i); | ||
| bb.statements.push(new_statement); | ||
| } | ||
| curr = bb.statements.len(); | ||
| bb.statements.extend(suffix_stmts); | ||
| } | ||
| } | ||
| } | ||
|
|
||
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.
can you leave a comment as to why we do this
if-- iirc, otherwise this triggers in constants. Seems like this may be just a temporary limitation, if we improve our constant handling a la miri.