Skip to content

Unmerged stack slots under Windows #132014

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
xTachyon opened this issue Oct 21, 2024 · 5 comments · Fixed by #138489
Closed

Unmerged stack slots under Windows #132014

xTachyon opened this issue Oct 21, 2024 · 5 comments · Fixed by #138489
Labels
A-codegen Area: Code generation A-LLVM Area: Code generation parts specific to LLVM. Both correctness bugs and optimization-related issues. C-optimization Category: An issue highlighting optimization opportunities or PRs implementing such I-heavy Issue: Problems and improvements with respect to binary size of generated code. O-windows-msvc Toolchain: MSVC, Operating system: Windows T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@xTachyon
Copy link
Contributor

https://godbolt.org/z/1foPhW5PT

Relevant bits:

example::write_characteristics::hd20ef966b954cd90:
        sub     rsp, 200
define void @example::write_characteristics::hd20ef966b954cd90(i16 noundef %c) unnamed_addr {
start:
  %0 = alloca [16 x i8], align 8
  %1 = alloca [16 x i8], align 8
  %2 = alloca [16 x i8], align 8
  %3 = alloca [16 x i8], align 8
  %4 = alloca [16 x i8], align 8
  %5 = alloca [16 x i8], align 8
  %6 = alloca [16 x i8], align 8
  %7 = alloca [16 x i8], align 8
  %8 = alloca [16 x i8], align 8
  %9 = alloca [16 x i8], align 8

I think all the allocas should've been merged in one, or be able to pass a pointer to a global const with the slice.

@rustbot rustbot added the needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. label Oct 21, 2024
@workingjubilee workingjubilee added A-codegen Area: Code generation O-windows-msvc Toolchain: MSVC, Operating system: Windows I-heavy Issue: Problems and improvements with respect to binary size of generated code. C-optimization Category: An issue highlighting optimization opportunities or PRs implementing such labels Oct 21, 2024
@xTachyon
Copy link
Contributor Author

xTachyon commented Oct 22, 2024

For the first idea, I'd expect codegen similar to this: https://godbolt.org/z/5M8jG5orf

@bjorn3
Copy link
Member

bjorn3 commented Oct 22, 2024

I think what happens is that at the MIR level each call argument is a const operand and never stored into a temporary variable. As such no StorageLive and StorageDead MIR statements are emitted, which then results in LLVM never being told that the lifetime of the stack slots doesn't overlap.

@tmiasko tmiasko added A-LLVM Area: Code generation parts specific to LLVM. Both correctness bugs and optimization-related issues. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. and removed needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. labels Oct 22, 2024
@xTachyon
Copy link
Contributor Author

Rustc emiting lifetime start/end would be great, but even without, I don't see why LLVM can't figure out by itself it can merge them. The function argument is nocapture, so the alloca should be dead after each call. At least that's my understanding of it.

@bjorn3
Copy link
Member

bjorn3 commented Oct 24, 2024

Turns out LLVM does actually infer lifetime start/end intrinsics, yet still fails to overlap the stack slots.

@dianqk
Copy link
Member

dianqk commented Oct 24, 2024

I’m not sure if this is what you’re looking for: https://godbolt.org/z/qh8ov63hb. I don't know if this is an optimization.

bors added a commit to rust-lang-ci/rust that referenced this issue Mar 24, 2025
Describe lifetime of call argument temporaries passed indirectly

Fixes rust-lang#132014.
bors added a commit to rust-lang-ci/rust that referenced this issue May 17, 2025
…gjubilee

Describe lifetime of call argument temporaries passed indirectly

Fixes rust-lang#132014.
@bors bors closed this as completed in 46264e6 May 26, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-codegen Area: Code generation A-LLVM Area: Code generation parts specific to LLVM. Both correctness bugs and optimization-related issues. C-optimization Category: An issue highlighting optimization opportunities or PRs implementing such I-heavy Issue: Problems and improvements with respect to binary size of generated code. O-windows-msvc Toolchain: MSVC, Operating system: Windows T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants