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

memset not being optimized out when initializing array contents #38613

Open
learnopengles opened this issue Dec 26, 2016 · 8 comments
Open

memset not being optimized out when initializing array contents #38613

learnopengles opened this issue Dec 26, 2016 · 8 comments
Labels
A-LLVM Area: Code generation parts specific to LLVM. Both correctness bugs and optimization-related issues. A-mir-opt Area: MIR optimizations C-enhancement Category: An issue proposing an enhancement or a PR with one. C-optimization Category: An issue highlighting optimization opportunities or PRs implementing such I-slow Issue: Problems and improvements with respect to performance of generated code. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@learnopengles
Copy link

Apologies as there's most likely already a tracking issue for this, but I wasn't able to find it. When initializing an array's contents, it seems that the initial memset is not optimized out:

https://godbolt.org/g/pbgnlq

let mut arr = [0; 1000];
  for i in 0..arr.len() {
    arr[i] = i;
  }
  
  println!("{}", arr[5])

Clang has the same issue actually, except there memset would have to be called explicitly.

@steveklabnik
Copy link
Member

If clang has the same issue, it's likely an LLVM bug, not our bug, I'd guess.

@steveklabnik steveklabnik added the I-slow Issue: Problems and improvements with respect to performance of generated code. label Dec 26, 2016
@learnopengles
Copy link
Author

Quite possibly yes, the difference is that everything starts out uninitialized so you wouldn't be running into this case by default. Could MIR omit the check to prevent it from cascading down to the IR level?

@bluss
Copy link
Member

bluss commented Dec 26, 2016

Sounds like it's related to #35662

@learnopengles
Copy link
Author

It seems to be present in older versions of Rust too (at least when comparing with rust.godbolt.org). I tried switching to enumerate and that didn't cause the memset to go away.

@nagisa
Copy link
Member

nagisa commented Dec 26, 2016

Quite possibly yes, the difference is that everything starts out uninitialized so you wouldn't be running into this case by default.

Use std::mem::uninitialized() if you want the default behaviour of C.

@learnopengles
Copy link
Author

learnopengles commented Dec 26, 2016

Hi, I don't believe I said I wanted the behaviour of C? I only wanted to clarify that we wouldn't run into this bug as often there because arrays start out uninitialized. I'd prefer the behavior of Rust but without the redundant zero fill. :)

@RReverser
Copy link
Contributor

Is it even a good idea to allocate 4 KB off the stack? And, when allocating off the heap, you can just do (0..1000).collect() to a vector and issue won't appear as internal array will be allocated and filled just once.

@Mark-Simulacrum Mark-Simulacrum added the C-enhancement Category: An issue proposing an enhancement or a PR with one. label Jul 26, 2017
@Enselic
Copy link
Member

Enselic commented Sep 14, 2023

Triage: Problem remains in Rust 1.72. Ideally this would be fixed by LLVM, but the incentives for LLVM to do so might be weak since this is mostly a problem in safe languages. So to get this fixed a Rust specific MIR optimization pass is probably needed.

@Enselic Enselic added A-mir-opt Area: MIR optimizations T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Sep 14, 2023
@nikic nikic added the A-LLVM Area: Code generation parts specific to LLVM. Both correctness bugs and optimization-related issues. label Sep 14, 2023
@workingjubilee workingjubilee added the C-optimization Category: An issue highlighting optimization opportunities or PRs implementing such label Oct 8, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-LLVM Area: Code generation parts specific to LLVM. Both correctness bugs and optimization-related issues. A-mir-opt Area: MIR optimizations C-enhancement Category: An issue proposing an enhancement or a PR with one. C-optimization Category: An issue highlighting optimization opportunities or PRs implementing such I-slow Issue: Problems and improvements with respect to performance of generated code. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

9 participants