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

Set dso_local for hidden, private and local items #83592

Merged
merged 1 commit into from
Apr 6, 2021

Conversation

nagisa
Copy link
Member

@nagisa nagisa commented Mar 27, 2021

This should probably have no real effect in most cases, as e.g. hidden
visibility already implies dso_local (or at least LLVM IR does not
preserve the dso_local setting if the item is already hidden), but
it should fix -Crelocation-model=static and improve codegen in
executables.

Note that this PR does not exhaustively port the logic in clang, only the
portion that is necessary to fix a regression from LLVM 12 that relates to
-Crelocation_model=static.

Fixes #83335

@rust-highfive
Copy link
Collaborator

r? @davidtwco

(rust-highfive has picked a reviewer for you, use r? to override)

@rust-highfive rust-highfive added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Mar 27, 2021
@nagisa
Copy link
Member Author

nagisa commented Mar 27, 2021

@bors try @rust-timer queue

@rust-timer
Copy link
Collaborator

Awaiting bors try build completion.

@rustbot label: +S-waiting-on-perf

@rustbot rustbot added the S-waiting-on-perf Status: Waiting on a perf run to be completed. label Mar 27, 2021
@bors
Copy link
Contributor

bors commented Mar 27, 2021

⌛ Trying commit e5184ab339d2ee4fc502dae690892fbff8bfe99a with merge 67a145d7d663cd212cb8c2bef6a90df8d6f652c9...

@rust-log-analyzer

This comment has been minimized.

@bors
Copy link
Contributor

bors commented Mar 27, 2021

💔 Test failed - checks-actions

@bors bors added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Mar 27, 2021
@rust-log-analyzer

This comment has been minimized.

@rust-log-analyzer

This comment has been minimized.

@rust-log-analyzer

This comment has been minimized.

@nagisa nagisa force-pushed the nagisa/dso_local branch 2 times, most recently from e431f03 to ec3ef2a Compare March 29, 2021 00:26
@nagisa
Copy link
Member Author

nagisa commented Mar 29, 2021

@bors try @rust-timer queue

@rust-timer
Copy link
Collaborator

Awaiting bors try build completion.

@rustbot label: +S-waiting-on-perf

@bors
Copy link
Contributor

bors commented Mar 29, 2021

⌛ Trying commit ec3ef2a5f48bc553788b66e07137ea5e14ca1dde with merge 01bf9eb60ecc1bf23dd897173c7330b0fc7a1348...

@rust-log-analyzer

This comment has been minimized.

@bors
Copy link
Contributor

bors commented Mar 29, 2021

☀️ Try build successful - checks-actions
Build commit: 01bf9eb60ecc1bf23dd897173c7330b0fc7a1348 (01bf9eb60ecc1bf23dd897173c7330b0fc7a1348)

@rust-timer
Copy link
Collaborator

Queued 01bf9eb60ecc1bf23dd897173c7330b0fc7a1348 with parent cb0e0db, future comparison URL.

@nagisa
Copy link
Member Author

nagisa commented Apr 2, 2021

@davidtwco the number of test changes may warrant a second look.

@davidtwco
Copy link
Member

@bors r+

@bors
Copy link
Contributor

bors commented Apr 5, 2021

📌 Commit 2f000a7 has been approved by davidtwco

@bors bors added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Apr 5, 2021
@bors
Copy link
Contributor

bors commented Apr 6, 2021

⌛ Testing commit 2f000a7 with merge 0c7d4ef...

@bors
Copy link
Contributor

bors commented Apr 6, 2021

☀️ Test successful - checks-actions
Approved by: davidtwco
Pushing 0c7d4ef to master...

@bors bors added the merged-by-bors This PR was explicitly merged by bors. label Apr 6, 2021
@bors bors merged commit 0c7d4ef into rust-lang:master Apr 6, 2021
@rustbot rustbot added this to the 1.53.0 milestone Apr 6, 2021
Bobo1239 added a commit to Bobo1239/rust that referenced this pull request May 18, 2021
The test case wasn't actually checked for x64 due to a small difference in the name.
bors added a commit to rust-lang-ci/rust that referenced this pull request May 19, 2021
Set dso_local for more items

Related to rust-lang#83592. (cc `@nagisa)`

Noticed that on x86_64 with `relocation-model: static` `R_X86_64_GOTPCREL` relocations were still generated in some cases. (related: Rust-for-Linux/linux#135; Rust-for-Linux needs these fixes to successfully build)

First time doing anything with LLVM so not sure whether this is correct but the following are some of the things I've tried to convince myself.

## C equivalent

Example from clang which also sets `dso_local` in these cases:
`clang-12 -fno-PIC -S -emit-llvm test.c`
```C
extern int A;

int* a() {
    return &A;
}

int B;

int* b() {
    return &B;
}
```
```
; ModuleID = 'test.c'
source_filename = "test.c"
target datalayout = "e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-f80:128-n8:16:32:64-S128"
target triple = "x86_64-unknown-linux-gnu"

`@A` = external dso_local global i32, align 4
`@B` = dso_local global i32 0, align 4

; Function Attrs: noinline nounwind optnone uwtable
define dso_local i32* `@a()` #0 {
  ret i32* `@A`
}

; Function Attrs: noinline nounwind optnone uwtable
define dso_local i32* `@b()` #0 {
  ret i32* `@B`
}

attributes #0 = { noinline nounwind optnone uwtable "disable-tail-calls"="false" "frame-pointer"="all" "less-precise-fpmad"="false" "min-legal-vector-width"="0" "no-infs-fp-math"="false" "no-jump-tables"="false" "no-nans-fp-math"="false" "no-signed-zeros-fp-math"="false" "no-trapping-math"="true" "stack-protector-buffer-size"="8" "target-cpu"="x86-64" "target-features"="+cx8,+fxsr,+mmx,+sse,+sse2,+x87" "tune-cpu"="generic" "unsafe-fp-math"="false" "use-soft-float"="false" }

!llvm.module.flags = !{!0}
!llvm.ident = !{!1}

!0 = !{i32 1, !"wchar_size", i32 4}
!1 = !{!"clang version 12.0.0 (https://github.com/llvm/llvm-project/ b978a93)"}
```
`clang-12 -fno-PIC -c test.c`
`objdump test.o -r`:
```
test.o:     file format elf64-x86-64

RELOCATION RECORDS FOR [.text]:
OFFSET           TYPE              VALUE
0000000000000006 R_X86_64_64       A
0000000000000016 R_X86_64_64       B

RELOCATION RECORDS FOR [.eh_frame]:
OFFSET           TYPE              VALUE
0000000000000020 R_X86_64_PC32     .text
0000000000000040 R_X86_64_PC32     .text+0x0000000000000010
```

## Comparison to pre-LLVM 12 output

`rustc --emit=obj,llvm-ir --target=x86_64-unknown-none-linuxkernel --crate-type rlib test.rs`
```Rust
#![feature(no_core, lang_items)]
#![no_core]

#[lang="sized"]
trait Sized {}

#[lang="sync"]
trait Sync {}

#[lang = "drop_in_place"]
pub unsafe fn drop_in_place<T: ?Sized>(_: *mut T) {}

impl Sync for i32 {}

pub static STATIC: i32 = 32;

extern {
    pub static EXT_STATIC: i32;
}

pub fn a() -> &'static i32 {
    &STATIC
}
pub fn b() -> &'static i32 {
    unsafe {&EXT_STATIC}
}
```
`objdump test.o -r`
nightly-2021-02-20 (rustc target is `x86_64-linux-kernel`):
```
RELOCATION RECORDS FOR [.text._ZN4test1a17h1024ba65f3424175E]:
OFFSET           TYPE              VALUE
0000000000000007 R_X86_64_32S      _ZN4test6STATIC17h3adc41a83746c9ffE

RELOCATION RECORDS FOR [.text._ZN4test1b17h86a6a80c1190ac8dE]:
OFFSET           TYPE              VALUE
0000000000000007 R_X86_64_32S      EXT_STATIC
```
nightly-2021-05-10:
```
RELOCATION RECORDS FOR [.text._ZN4test1a17he846f03bf37b2d20E]:
OFFSET           TYPE              VALUE
0000000000000007 R_X86_64_GOTPCREL  _ZN4test6STATIC17h5a059515bf3d4968E-0x0000000000000004

RELOCATION RECORDS FOR [.text._ZN4test1b17h7e0f7f80fbd91125E]:
OFFSET           TYPE              VALUE
0000000000000007 R_X86_64_GOTPCREL  EXT_STATIC-0x0000000000000004
```
This PR:
```
RELOCATION RECORDS FOR [.text._ZN4test1a17he846f03bf37b2d20E]:
OFFSET           TYPE              VALUE
0000000000000007 R_X86_64_32S      _ZN4test6STATIC17h5a059515bf3d4968E

RELOCATION RECORDS FOR [.text._ZN4test1b17h7e0f7f80fbd91125E]:
OFFSET           TYPE              VALUE
0000000000000007 R_X86_64_32S      EXT_STATIC
```
Bobo1239 added a commit to Bobo1239/linux that referenced this pull request May 22, 2021
Includes rust-lang/rust#83592 and rust-lang/rust#85276 which were needed
to build correctly again after rustc's upgrade to LLVM 12.

Signed-off-by: Boris-Chengbiao Zhou <bobo1239@web.de>
Bobo1239 added a commit to Bobo1239/linux that referenced this pull request May 22, 2021
Includes rust-lang/rust#83592 and rust-lang/rust#85276 which are needed
to build correctly again after rustc's upgrade to LLVM 12.

Signed-off-by: Boris-Chengbiao Zhou <bobo1239@web.de>
Bobo1239 added a commit to Bobo1239/linux that referenced this pull request May 29, 2021
Includes rust-lang/rust#83592 and rust-lang/rust#85276 which are needed
to build correctly again after rustc's upgrade to LLVM 12.

Signed-off-by: Boris-Chengbiao Zhou <bobo1239@web.de>
Bobo1239 added a commit to Bobo1239/linux that referenced this pull request May 29, 2021
Includes rust-lang/rust#83592, rust-lang/rust#85276 and rust-lang/rust#85700
which are needed to build correctly again after rustc's upgrade to LLVM 12.

Signed-off-by: Boris-Chengbiao Zhou <bobo1239@web.de>
Bobo1239 added a commit to Bobo1239/linux that referenced this pull request May 29, 2021
Includes rust-lang/rust#83592, rust-lang/rust#85276 and
rust-lang/rust#85700 which are needed to build correctly again after
rustc's upgrade to LLVM 12.

Signed-off-by: Boris-Chengbiao Zhou <bobo1239@web.de>
Bobo1239 added a commit to Bobo1239/linux that referenced this pull request May 29, 2021
Includes rust-lang/rust#83592, rust-lang/rust#85276 and
rust-lang/rust#85700 which are needed to build correctly again after
rustc's upgrade to LLVM 12.

Also fixes a newly introduced clippy warning.

Signed-off-by: Boris-Chengbiao Zhou <bobo1239@web.de>
Bobo1239 added a commit to Bobo1239/linux that referenced this pull request May 29, 2021
Includes rust-lang/rust#83592, rust-lang/rust#85276 and
rust-lang/rust#85700 which are needed to build correctly again after
rustc's upgrade to LLVM 12.

Signed-off-by: Boris-Chengbiao Zhou <bobo1239@web.de>
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. merged-by-bors This PR was explicitly merged by bors. S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Upgrade to LLVM 12 caused AArch64 bare metal program run incorrectly
8 participants