Skip to content
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

librustc_mir: Propagate constants during copy propagation. #36639

Merged
merged 1 commit into from Sep 24, 2016

Conversation

pcwalton
Copy link
Contributor

This optimization kicks in a lot when bootstrapping the compiler.

r? @eddyb

@pcwalton pcwalton added I-slow Issue: Problems and improvements with respect to performance of generated code. A-optimization A-mir Area: Mid-level IR (MIR) - https://blog.rust-lang.org/2016/04/19/MIR.html labels Sep 22, 2016
Copy link
Member

@eddyb eddyb left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

r=me with minor nits

MirSource::Const(_) | MirSource::Static(..) | MirSource::Promoted(..) => return,
MirSource::Fn(function_node_id) => {
// Don't run on const functions, because constant qualification might reject the
// optimized IR.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Comment nit: not really a problem for const qualification (as that only looks at non-generic consts only, already handled by the previous match arm), but rather rustc_trans, which in a pre-miri world cannot evaluate MIR where mutation is used to construct ADT.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@eddyb But when I tried to bootstrap without doing this then qualify_consts started emitting errors. So I think it is a problem with that pass, no?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The previous match arm (MirSource::Const(_) specifically) prevents qualify_consts from freaking out. Everything else is also needed, but for a different reason (trans::mir::constant).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added more comments.

}
}

impl<'tcx> MutVisitor<'tcx> for ConstantPropagationVisitor<'tcx> {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder if this could be replaced with replace_all_defs_and_uses_with taking an Operand.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

At first I wanted to do that, but I realized it's not possible. That's because defs are not Operands if they appear on e.g. the LHS of an Assign.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right, but in that case, it'd be a bug! - or you could have a separate replace_all_uses_with.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems like premature abstraction. What other passes will need that function? Note that it's mostly just useful for consts, because operands are either lvalues or consts.

@@ -22,12 +22,12 @@ fn main() {
// START rustc.node4.PreTrans.after.mir
// bb0: {
// nop; // scope 0 at storage_ranges.rs:14:9: 14:10
// var0 = const 0i32; // scope 0 at storage_ranges.rs:14:13: 14:14
// nop; // scope 0 at storage_ranges.rs:14:13: 14:14
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This test should probably use the MIR before any optimization passes, just to check the way storage ranges are generated.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Want to change this to test pre-optimization MIR? If not, r=me as-is.

@pcwalton pcwalton force-pushed the const-prop branch 2 times, most recently from bdff21a to cbf41b0 Compare September 22, 2016 07:00
@pcwalton
Copy link
Contributor Author

@bors: r=eddyb

@bors
Copy link
Contributor

bors commented Sep 22, 2016

📌 Commit cbf41b0 has been approved by eddyb

@eddyb
Copy link
Member

eddyb commented Sep 22, 2016

Wait, Travis failed, @bors r-


failures:
    [debuginfo-gdb] debuginfo-gdb/lexical-scope-in-if.rs
    [debuginfo-gdb] debuginfo-gdb/lexical-scopes-in-block-expression.rs
    [debuginfo-gdb] debuginfo-gdb/no-debug-attribute.rs

@pcwalton
Copy link
Contributor Author

Argh, those don't run on the Mac!

@pcwalton
Copy link
Contributor Author

@bors: r=eddyb

I think restricting the pass to MIR opt level >= 1 will fix the debug info problems.

@bors
Copy link
Contributor

bors commented Sep 22, 2016

📌 Commit f9b73aa has been approved by eddyb

// There must be exactly one use of the source used in a statement (not in a terminator).
let src_use_info = def_use_analysis.local_info(src_local);
let src_use_count = src_use_info.use_count();
if src_use_count == 0 {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't you have a use of src by definition?

debug!(" Can't copy-propagate local: no uses");
return None
}
if src_use_count != 1 {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is this check needed?

@arielb1
Copy link
Contributor

arielb1 commented Sep 22, 2016

error: this file contains an un-closed delimiter

@bors r-

@pcwalton
Copy link
Contributor Author

@bors: r=eddyb

@bors
Copy link
Contributor

bors commented Sep 23, 2016

📌 Commit a1108d1 has been approved by eddyb

@bors
Copy link
Contributor

bors commented Sep 23, 2016

⌛ Testing commit a1108d1 with merge 1cd17db...

@bors
Copy link
Contributor

bors commented Sep 23, 2016

💔 Test failed - auto-win-gnu-32-opt-rustbuild

@pcwalton
Copy link
Contributor Author

@bors: r=eddyb

@bors
Copy link
Contributor

bors commented Sep 24, 2016

📌 Commit d1366c3 has been approved by eddyb

@bors
Copy link
Contributor

bors commented Sep 24, 2016

⌛ Testing commit d1366c3 with merge f1278ae...

@bors
Copy link
Contributor

bors commented Sep 24, 2016

💔 Test failed - auto-win-gnu-32-opt-rustbuild

This optimization kicks in a lot when bootstrapping the compiler.
@pcwalton
Copy link
Contributor Author

@bors: r=eddyb

@bors
Copy link
Contributor

bors commented Sep 24, 2016

📌 Commit 79cb2db has been approved by eddyb

@bors
Copy link
Contributor

bors commented Sep 24, 2016

⌛ Testing commit 79cb2db with merge 05c2fdd...

bors added a commit that referenced this pull request Sep 24, 2016
librustc_mir: Propagate constants during copy propagation.

This optimization kicks in a lot when bootstrapping the compiler.

r? @eddyb
@bors bors merged commit 79cb2db into rust-lang:master Sep 24, 2016
pmatos pushed a commit to LinkiTools/rust that referenced this pull request Sep 27, 2016
librustc_mir: Propagate constants during copy propagation.

This optimization kicks in a lot when bootstrapping the compiler.

r? @eddyb
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-mir Area: Mid-level IR (MIR) - https://blog.rust-lang.org/2016/04/19/MIR.html I-slow Issue: Problems and improvements with respect to performance of generated code.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants