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

Emit unnamed_addr on statics #18297

Closed
arielb1 opened this issue Oct 24, 2014 · 7 comments
Closed

Emit unnamed_addr on statics #18297

arielb1 opened this issue Oct 24, 2014 · 7 comments
Labels
A-codegen Area: Code generation A-LLVM Area: Code generation parts specific to LLVM. Both correctness bugs and optimization-related issues. C-enhancement Category: An issue proposing an enhancement or a PR with one. 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

@arielb1
Copy link
Contributor

arielb1 commented Oct 24, 2014

Mutable globals still work with unnamed_addr, and Rust code shouldn't rely on the significance of addresses.

@thestinger thestinger added I-slow Issue: Problems and improvements with respect to performance of generated code. A-codegen Area: Code generation labels Oct 24, 2014
@huonw
Copy link
Member

huonw commented Oct 25, 2014

cc the discussion on rust-lang/rfcs#362

@emberian emberian self-assigned this Mar 25, 2015
@emberian emberian removed their assignment Jan 5, 2016
@Mark-Simulacrum Mark-Simulacrum added the C-enhancement Category: An issue proposing an enhancement or a PR with one. label Jul 22, 2017
@steveklabnik
Copy link
Member

I know next to nothing about llvm attributes, but

pub static mut X: i32 = 5;

gives

@_ZN10playground1X17hacd7d7b63e8c9b94E = local_unnamed_addr global <{ [4 x i8] }> <{ [4 x i8] c"\05\00\00\00" }>, align 4

llvm says https://llvm.org/docs/LangRef.html

Global variables can be marked with unnamed_addr which indicates that the address is not significant, only the content. Constants marked like this can be merged with other constants if they have the same initializer. Note that a constant with significant address can be merged with a unnamed_addr constant, the result being a constant whose address is significant.

If the local_unnamed_addr attribute is given, the address is known to not be significant within the module.

I'm not sure if the local_ version is correct or not...

@workingjubilee
Copy link
Member

This is -Copt-level dependent.

; in debug
@_ZN10playground12RUSTY_STATIC17h7d2abb51a94403c7E =
    global <{ [4 x i8] }> <{ [4 x i8] c"\05\00\00\00" }>, align 4, !dbg !0
; in --release
@_ZN10playground12RUSTY_STATIC17hd7ab9ec18434d12fE = 
    local_unnamed_addr constant <{ [4 x i8] }> <{ [4 x i8] c"\05\00\00\00" }>, align 4

So we do not appear to emit any such annotation on statics. Instead, it seems that LLVM is capable of inferring local_unnamed_addr via analysis.

@workingjubilee workingjubilee added the A-LLVM Area: Code generation parts specific to LLVM. Both correctness bugs and optimization-related issues. label Jul 11, 2022
@Noratrieb Noratrieb added the T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. label Apr 5, 2023
lnicola pushed a commit to lnicola/rust that referenced this issue Oct 17, 2024
…=davidbarsky

minor: `ra-salsa` in `profile.dev.package`

Since `ra-salsa`'s package name is actually `salsa` it makes the following warning in `cargo` commands;

```
warning: profile package spec `ra-salsa` in profile `dev` did not match any packages
```

and the opt level isn't applied to it.
bors added a commit to rust-lang-ci/rust that referenced this issue Oct 19, 2024
Always emit `unnamed_addr` for statics

Fixes rust-lang#18297

Mostly to see if anything breaks/perf

r? `@ghost`
@RalfJung
Copy link
Member

Rust code shouldn't rely on the significance of addresses.

Rust code should definitely be able to rely on the address of a static. That's one of their defining features.
Cc @rust-lang/opsem

So, closing this issue.

@workingjubilee
Copy link
Member

@RalfJung

A static item is similar to a constant, except that it represents a precise memory location in the program. All references to the static refer to the same memory location. Static items have the static lifetime, which outlives all other lifetimes in a Rust program. Static items do not call drop at the end of the program.

The reference there says that a static has a precise memory location, I cannot see where it says that the following must hold:

static ZERO: u8 = 0;
static ZER0: u8 = 0;
assert_ne!(&raw const ZERO, &raw const ZER0);

@RalfJung
Copy link
Member

RalfJung commented Oct 20, 2024 via email

@digama0
Copy link
Contributor

digama0 commented Oct 20, 2024

Note, those two statics are not the same, one of them is ZERO and the other is ZER0. Not sure why it was written like that... A slightly less confusing alpha-equivalent formulation is:

static A: u8 = 0;
static B: u8 = 0;
assert_ne!(&raw const A, raw const B);

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-enhancement Category: An issue proposing an enhancement or a PR with one. 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

Successfully merging a pull request may close this issue.

10 participants