Repository navigation
Closed
Labels
A-MIRArea: Mid-level IR (MIR) - https://blog.rust-lang.org/2016/04/19/MIR.htmlArea: Mid-level IR (MIR) - https://blog.rust-lang.org/2016/04/19/MIR.htmlA-codegenArea: Code generationArea: Code generationI-slowIssue: Problems and improvements with respect to performance of generated code.Issue: Problems and improvements with respect to performance of generated code.P-mediumMedium priorityMedium priorityT-compilerRelevant to the compiler team, which will review and decide on the PR/issue.Relevant to the compiler team, which will review and decide on the PR/issue.regression-from-stable-to-stablePerformance or correctness regression from one stable version to another.Performance or correctness regression from one stable version to another.
Description
Meta
Checked on rustc 1.12.0 with MIR on, 1.15.1, 1.16 and 1.18. Does not affect 1.12.0 with MIR off.
STR
#![crate_type="rlib"]
pub struct Big {
drop_me: [Option<Box<u8>>; 64],
}
pub fn supersize_me(meal: fn() -> Big, out: &mut Vec<Big>) {
out.push(meal());
out.push(meal());
out.push(meal());
out.push(meal());
out.push(meal());
out.push(meal());
out.push(meal());
out.push(meal());
out.push(meal());
out.push(meal());
out.push(meal());
out.push(meal());
out.push(meal());
out.push(meal());
out.push(meal());
out.push(meal()); // 16 calls to `push`
}Expected Result
Function should use a small amount of stack space, definitely less than 2 kilobytes (Big is 512 bytes per copy); 1.12.0 with -Z orbit=off uses 1088 bytes of stack.
Actual Result
When compiled, the function uses more than 16384 = 8*64*16*2 bytes of stack space, as is evident from subq $16384, %rsp in the assembly - 2 copies of Big for every call to push.
Notes
This is the root cause for #40573. It is not new, however - it was probably always present in MIR.
Metadata
Metadata
Assignees
Labels
A-MIRArea: Mid-level IR (MIR) - https://blog.rust-lang.org/2016/04/19/MIR.htmlArea: Mid-level IR (MIR) - https://blog.rust-lang.org/2016/04/19/MIR.htmlA-codegenArea: Code generationArea: Code generationI-slowIssue: Problems and improvements with respect to performance of generated code.Issue: Problems and improvements with respect to performance of generated code.P-mediumMedium priorityMedium priorityT-compilerRelevant to the compiler team, which will review and decide on the PR/issue.Relevant to the compiler team, which will review and decide on the PR/issue.regression-from-stable-to-stablePerformance or correctness regression from one stable version to another.Performance or correctness regression from one stable version to another.
Activity
arielb1 commentedon Mar 29, 2017
Another example:
arielb1 commentedon Mar 29, 2017
And then there's this case, which is probably the big granddaddy of them all, and which fixing probably requires help on the LLVM side:
The LLVM IR ends up like this:
And the problem is that either alloca can be alive at
bb1, so LLVM thinks they can be simultaneously alive. And we can't solve this without splittingbb1- which creates lots of landing pads that also degrade performance/stack usage.ranma42 commentedon Mar 30, 2017
@arielb1 Since the lifetimes of the
allocas are disjoint, wouldn't it be possible to just use one alloca?arielb1 commentedon Mar 30, 2017
@ranma42
As a MIR optimization? Certainly. However, that won't help with LLVM inlining (LLVM has the alloca-merging-on-inlining thing, which sometimes improves matters, but not always).
michaelwoerister commentedon Mar 30, 2017
According to @arielb1, this is another example of #35408. Will need LLVM changes to really be fixed. He'll look into it some more.
michaelwoerister commentedon Mar 30, 2017
Possibly related LLVM bug: https://bugs.llvm.org//show_bug.cgi?id=25776
arielb1 commentedon Apr 2, 2017
https://bugs.llvm.org//show_bug.cgi?id=32488 & https://reviews.llvm.org/D31583
brson commentedon Apr 4, 2017
Thanks @arielb1.
nikomatsakis commentedon Apr 11, 2017
@arielb1 can you say a bit more about this "granddaddy example". The problem seems to be (iiuc) that we unwind to bb1 so we can drop the
something_random_with_a_dtorvariable, and at that point LLVM considers the allocas alive. Would it be possible for us to issue calls tollvm.lifetime.endintrinsics on the unwind path? Is that not allowed in LLVM for some reason? (It seems like it'd be valid for us to do so, no?)dotdash commentedon Jun 13, 2017
Patch has landed upstream! Thanks for keeping it on track @arielb1 🥇
arielb1 commentedon Jun 13, 2017
Yay!
brson commentedon Jun 15, 2017
Does the pending LLVM upgrade fix?
Auto merge of #42750 - arielb1:unwind-stack, r=eddyb
Rollup merge of rust-lang#136536 - DuskyElf:master, r=jieyouxu
Rollup merge of rust-lang#136536 - DuskyElf:master, r=jieyouxu
Rollup merge of rust-lang#136536 - DuskyElf:master, r=jieyouxu
Unrolled build for rust-lang#136536