Commit Graph

1626 Commits

Author SHA1 Message Date
bors
fd9bf59436 Auto merge of #111999 - scottmcm:codegen-less-memcpy, r=compiler-errors
Use `load`+`store` instead of `memcpy` for small integer arrays

I was inspired by #98892 to see whether, rather than making `mem::swap` do something smart in the library, we could update MIR assignments like `*_1 = *_2` to do something smarter than `memcpy` for sufficiently-small types that doing it inline is going to be better than a `memcpy` call in assembly anyway.  After all, special code may help `mem::swap`, but if the "obvious" MIR can just result in the correct thing that helps everything -- other code like `mem::replace`, people doing it manually, and just passing around by value in general -- as well as makes MIR inlining happier since it doesn't need to deal with all the complicated library code if it just sees a couple assignments.

LLVM will turn the short, known-length `memcpy`s into direct instructions in the backend, but that's too late for it to be able to remove `alloca`s.  In general, replacing `memcpy`s with typed instructions is hard in the middle-end -- even for `memcpy.inline` where it knows it won't be a function call -- is hard [due to poison propagation issues](https://rust-lang.zulipchat.com/#narrow/stream/187780-t-compiler.2Fwg-llvm/topic/memcpy.20vs.20load-store.20for.20MIR.20assignments/near/360376712).  So because we know more about the type invariants -- these are typed copies -- rustc can emit something more specific, allowing LLVM to `mem2reg` away the `alloca`s in some situations.

#52051 previously did something like this in the library for `mem::swap`, but it ended up regressing during enabling mir inlining (cbbf06b0cd), so this has been suboptimal on stable for ≈5 releases now.

The code in this PR is narrowly targeted at just integer arrays in LLVM, but works via a new method on the [`LayoutTypeMethods`](https://doc.rust-lang.org/nightly/nightly-rustc/rustc_codegen_ssa/traits/trait.LayoutTypeMethods.html) trait, so specific backends based on cg_ssa can enable this for more situations over time, as we find them.  I don't want to try to bite off too much in this PR, though.  (Transparent newtypes and simple things like the 3×usize `String` would be obvious candidates for a follow-up.)

Codegen demonstrations: <https://llvm.godbolt.org/z/fK8hT9aqv>

Before:
```llvm
define void `@swap_rgb48_old(ptr` noalias nocapture noundef align 2 dereferenceable(6) %x, ptr noalias nocapture noundef align 2 dereferenceable(6) %y) unnamed_addr #1 {
  %a.i = alloca [3 x i16], align 2
  call void `@llvm.lifetime.start.p0(i64` 6, ptr nonnull %a.i)
  call void `@llvm.memcpy.p0.p0.i64(ptr` noundef nonnull align 2 dereferenceable(6) %a.i, ptr noundef nonnull align 2 dereferenceable(6) %x, i64 6, i1 false)
  tail call void `@llvm.memcpy.p0.p0.i64(ptr` noundef nonnull align 2 dereferenceable(6) %x, ptr noundef nonnull align 2 dereferenceable(6) %y, i64 6, i1 false)
  call void `@llvm.memcpy.p0.p0.i64(ptr` noundef nonnull align 2 dereferenceable(6) %y, ptr noundef nonnull align 2 dereferenceable(6) %a.i, i64 6, i1 false)
  call void `@llvm.lifetime.end.p0(i64` 6, ptr nonnull %a.i)
  ret void
}
```
Note it going to stack:
```nasm
swap_rgb48_old:                         # `@swap_rgb48_old`
        movzx   eax, word ptr [rdi + 4]
        mov     word ptr [rsp - 4], ax
        mov     eax, dword ptr [rdi]
        mov     dword ptr [rsp - 8], eax
        movzx   eax, word ptr [rsi + 4]
        mov     word ptr [rdi + 4], ax
        mov     eax, dword ptr [rsi]
        mov     dword ptr [rdi], eax
        movzx   eax, word ptr [rsp - 4]
        mov     word ptr [rsi + 4], ax
        mov     eax, dword ptr [rsp - 8]
        mov     dword ptr [rsi], eax
        ret
```

Now:
```llvm
define void `@swap_rgb48(ptr` noalias nocapture noundef align 2 dereferenceable(6) %x, ptr noalias nocapture noundef align 2 dereferenceable(6) %y) unnamed_addr #0 {
start:
  %0 = load <3 x i16>, ptr %x, align 2
  %1 = load <3 x i16>, ptr %y, align 2
  store <3 x i16> %1, ptr %x, align 2
  store <3 x i16> %0, ptr %y, align 2
  ret void
}
```
still lowers to `dword`+`word` operations, but has no stack traffic:
```nasm
swap_rgb48:                             # `@swap_rgb48`
        mov     eax, dword ptr [rdi]
        movzx   ecx, word ptr [rdi + 4]
        movzx   edx, word ptr [rsi + 4]
        mov     r8d, dword ptr [rsi]
        mov     dword ptr [rdi], r8d
        mov     word ptr [rdi + 4], dx
        mov     word ptr [rsi + 4], cx
        mov     dword ptr [rsi], eax
        ret
```

And as a demonstration that this isn't just `mem::swap`, a `mem::replace` on a small array (since replace doesn't use swap since #83022), which used to be `memcpy`s in LLVM changes in IR
```llvm
define void `@replace_short_array(ptr` noalias nocapture noundef sret([3 x i32]) dereferenceable(12) %0, ptr noalias noundef align 4 dereferenceable(12) %r, ptr noalias nocapture noundef readonly dereferenceable(12) %v) unnamed_addr #0 {
start:
  %1 = load <3 x i32>, ptr %r, align 4
  store <3 x i32> %1, ptr %0, align 4
  %2 = load <3 x i32>, ptr %v, align 4
  store <3 x i32> %2, ptr %r, align 4
  ret void
}
```
but that lowers to reasonable `dword`+`qword` instructions still
```nasm
replace_short_array:                    # `@replace_short_array`
        mov     rax, rdi
        mov     rcx, qword ptr [rsi]
        mov     edi, dword ptr [rsi + 8]
        mov     dword ptr [rax + 8], edi
        mov     qword ptr [rax], rcx
        mov     rcx, qword ptr [rdx]
        mov     edx, dword ptr [rdx + 8]
        mov     dword ptr [rsi + 8], edx
        mov     qword ptr [rsi], rcx
        ret
```
2023-06-06 01:50:28 +00:00
bors
7452822843 Auto merge of #107583 - EsmeYi:aix_xcoff_metadata, r=bjorn3
Support the rustc metadata for AIX

Support the rustc metadata for rlibs and dylibs on AIX.
XCOFF is the object file format on AIX.
2023-06-05 10:42:05 +00:00
Esme Yi
e31661cb9e Address @bjorn3's comments. 2023-06-05 15:15:09 +08:00
Andrew Xie
17412bae30 Removed use of iteration through a HashMap/HashSet in rustc_incremental and replaced with IndexMap/IndexSet 2023-06-04 21:54:28 -04:00
Scott McMurray
e1b020df9f Use load-store instead of memcpy for short integer arrays 2023-06-04 00:51:49 -07:00
Michael Goulet
5460f92a0f
Rollup merge of #112168 - scottmcm:lower-div-rem-unchecked-to-mir, r=oli-obk
Lower `unchecked_div`/`_rem` to MIR's `BinOp::Div`/`Rem`

As described in <https://doc.rust-lang.org/nightly/nightly-rustc/rustc_middle/mir/enum.BinOp.html#variant.Div>, the ordinary `BinOp`s for these are already UB for division by zero ([or overflow](https://llvm.org/docs/LangRef.html#sdiv-instruction), [demo](https://rust.godbolt.org/z/71e7P7Exh)), as MIR building is responsible for inserting code to panic for those cases regardless of whether the overflow checks are enabled.

So we can lower these in the same arm that lowers `wrapping_add` to MIR `BinOp::Add` and such, as all these cases turn into ordinary `Rvalue::BinaryOp`s.
2023-06-02 16:02:06 -07:00
Andrew Walbran
7269972f73 Add trustzone and virtualization target features for aarch32.
These are LLVM target features which allow the `smc` and `hvc`
instructions respectively to be used in inline assembly.
2023-06-02 13:50:18 +01:00
Tamir Duberstein
cd4354578a
Avoid passing --cpu-features when empty
Added in 12ac719b99, this logic always
passed --cpu-features, even when the value was the empty string.
2023-06-01 13:07:51 -04:00
Deadbeef
4f83717cf7 Use translatable diagnostics in rustc_const_eval 2023-06-01 14:45:18 +00:00
Scott McMurray
dc353f13b8 remove unchecked_div/_rem from cg_ssa 2023-06-01 00:05:55 -07:00
Scott McMurray
bf36193ef6 Add a distinct OperandValue::ZeroSized variant for ZSTs
These tend to have special handling in a bunch of places anyway, so the variant helps remember that.  And I think it's easier to grok than non-Scalar Aggregates sometimes being `Immediates` (like I got wrong and caused 109992).  As a minor bonus, it means we don't need to generate poison LLVM values for them to pass around in `OperandValue::Immediate`s.
2023-05-31 19:10:28 -07:00
bors
9af3865dec Auto merge of #110807 - petrochenkov:strictflavor, r=lqd,wesleywiser
linker: Report linker flavors incompatible with the current target

The linker flavor is checked for target compatibility even if linker is never used (e.g. we are producing a rlib).
If it causes trouble, we can move the check to `link.rs` so it will run if the linker (flavor) is actually used.

And also feature gate explicitly specifying linker flavors for tier 3 targets.

The next step is supporting all the internal linker flavors in user-visible interfaces (command line and json).
2023-05-31 22:40:25 +00:00
Matthias Krüger
f5894517ec
Rollup merge of #112069 - clubby789:offset-of-sized-fields, r=WaffleLapkin
offset_of: don't require type to be `Sized`

Fixes #112051

~~The RFC [explicitly forbids](https://rust-lang.github.io/rfcs/3308-offset_of.html#limitations) non-`Sized` types, but it looks like only the fields being recursed into were checked. The sized check also seemed to have been completely missing for tuples~~
2023-05-31 07:07:01 +02:00
bors
e6e4f7ed15 Auto merge of #112070 - lcnr:disjoint-closure-capture-ub, r=oli-obk
change `BorrowKind::Unique` to be a mutating `PlaceContext`

fixes #112056

I believe that `BorrowKind::Unique` is a footgun in general, so I added a FIXME and opened https://github.com/rust-lang/rust/issues/112072. This is a bit too involved for this PR though.
2023-05-31 00:24:39 +00:00
bors
a9251b6ce1 Auto merge of #112102 - Nilstrieb:rollup-ivu1hmc, r=Nilstrieb
Rollup of 7 pull requests

Successful merges:

 - #107916 (fix comment on Allocator trait)
 - #111543 (Uplift `clippy::invalid_utf8_in_unchecked` lint)
 - #111872 (fix: dedup `static_candidates` before report)
 - #111955 (bootstrap: Various Step refactors)
 - #112060 (`EarlyBinder::new` -> `EarlyBinder::bind`)
 - #112064 (Migrate GUI colors test to original CSS color format)
 - #112100 (Don't typecheck recovered method call from suggestion)

r? `@ghost`
`@rustbot` modify labels: rollup
2023-05-30 13:25:42 +00:00
bors
3266c36624 Auto merge of #111768 - oli-obk:pair_const_llvm, r=cjgillot
Optimize scalar and scalar pair representations loaded from ByRef in llvm

in https://github.com/rust-lang/rust/pull/105653 I noticed that we were generating suboptimal LLVM IR if we had a `ConstValue::ByRef` that could be represented by a `ScalarPair`. Before https://github.com/rust-lang/rust/pull/105653 this is probably rare, but after it, every slice will go down this suboptimal code path that requires LLVM to untangle a bunch of indirections and translate static allocations that are only used once to read a scalar pair from.
2023-05-30 10:31:10 +00:00
clubby789
6c18d1ecef offset_of: Don't require type to be sized 2023-05-29 21:56:10 +00:00
Vadim Petrochenkov
2013ccc218 rustc_target: Refactor linker flavor inference
Go through an intermediate pair of `cc`and `lld` hints instead of mapping CLI options to `LinkerFlavor` directly, and use the target's default linker flavor as a reference.
2023-05-29 19:58:11 +03:00
lcnr
cfd0623411 unique borrows are mutating uses 2023-05-29 17:15:48 +02:00
lcnr
08d149ca85 EarlyBinder::new -> EarlyBinder::bind 2023-05-29 13:46:10 +02:00
Nicholas Nethercote
781111ef35 Use Cow in {D,Subd}iagnosticMessage.
Each of `{D,Subd}iagnosticMessage::{Str,Eager}` has a comment:
```
// FIXME(davidtwco): can a `Cow<'static, str>` be used here?
```
This commit answers that question in the affirmative. It's not the most
compelling change ever, but it might be worth merging.

This requires changing the `impl<'a> From<&'a str>` impls to `impl
From<&'static str>`, which involves a bunch of knock-on changes that
require/result in call sites being a little more precise about exactly
what kind of string they use to create errors, and not just `&str`. This
will result in fewer unnecessary allocations, though this will not have
any notable perf effects given that these are error paths.

Note that I was lazy within Clippy, using `to_string` in a few places to
preserve the existing string imprecision. I could have used `impl
Into<{D,Subd}iagnosticMessage>` in various places as is done in the
compiler, but that would have required changes to *many* call sites
(mostly changing `&format("...")` to `format!("...")`) which didn't seem
worthwhile.
2023-05-29 09:23:43 +10:00
Kyle Matsuda
03534ac8b7 Replace EarlyBinder(x) with EarlyBinder::new(x) 2023-05-28 10:44:50 -06:00
bors
2560b80a08 Auto merge of #112000 - wesleywiser:safestack, r=Amanieu
Add support for LLVM SafeStack

Adds support for LLVM [SafeStack] which provides backward edge control
flow protection by separating the stack into two parts: data which is
only accessed in provable safe ways is allocated on the normal stack
(the "safe stack") and all other data is placed in a separate allocation
(the "unsafe stack").

SafeStack support is enabled by passing `-Zsanitizer=safestack`.

[SafeStack]: https://clang.llvm.org/docs/SafeStack.html

cc `@rcvalle` #39699
2023-05-28 04:41:13 +00:00
Guillaume Gomez
ddb5424569
Rollup merge of #111952 - cjgillot:drop-replace, r=WaffleLapkin
Remove DesugaringKind::Replace.

A simple boolean flag is enough.
2023-05-27 13:38:31 +02:00
Wesley Wiser
019d75b44e Add SafeStack support to rustc
Adds support for LLVM [SafeStack] which provides backward edge control
flow protection by separating the stack into two parts: data which is
only accessed in provable safe ways is allocated on the normal stack
(the "safe stack") and all other data is placed in a separate allocation
(the "unsafe stack").

SafeStack support is enabled by passing `-Zsanitizer=safestack`.

[SafeStack]: https://clang.llvm.org/docs/SafeStack.html
2023-05-26 15:18:54 -04:00
Oli Scherer
164d041e30 Stop creating intermediate places just to immediate convert them to operands 2023-05-26 15:01:29 +00:00
Matthias Krüger
42c7b8a7de
Rollup merge of #111384 - bmisiak:issue-106021-fix, r=petrochenkov
Fix linking Mac Catalyst by including LC_BUILD_VERSION in object files

Hello. My first rustc PR!

Issue #106021 prevents Rust code from being linked into Mac Catalyst applications. Apple's LD has started requiring object files to contain version information about the platform they were built for, such as:
* the "deployment target" (minimum supported OS version),
* the SDK version
* the type of the platform (macOS/iOS/catalyst/tvOS/watchOS all have a different number).

This is currently only enforced when building for Mac Catalyst.

Rust uses the `object` crate which added support for including this information starting with `0.31.0`. ~~I upgraded it along with `thorin-dwp` so that everything depends on 0.31.
Apparently 0.31 [pulls in](https://github.com/gimli-rs/object/issues/463) `ruzstd` due to a [new ELF standard](https://maskray.me/blog/2022-09-09-zstd-compressed-debug-sections) because its `compression` feature is enabled by thorin. If you find this objectionable, let me know what the best way to avoid pulling in those dependencies might be.~~

**(`object` upgraded in https://github.com/rust-lang/rust/pull/111413)**

I then added two commits:
* The first one adds very basic, hard-coded support for calling `set_macho_build_version` for `-macabi` (Catalyst) targets, where it claims deployment target of Catalyst 14.0 and SDK of 16.2.
* The second weaves the versioning through `rust_target::spec::TargetOptions`, so that we can stick to specifying all target-related info in one place.

Kudos to ``@ara4n`` for writing [this gist](https://gist.github.com/ara4n/320a53ea768aba51afad4c9ed2168536).
2023-05-26 08:24:07 +02:00
bors
c86212f9bc Auto merge of #111858 - clubby789:fluent-alphabetical, r=jyn514,compiler-errors
Ensure Fluent messages are in alphabetical order

Fixes #111847

This adds a tidy check to ensure Fluent messages are in alphabetical order, as well as sorting all existing messages. I think the error could be worded better, would appreciate suggestions.

<details>
<summary>Script used to sort files</summary>

```py
import sys
import re

fn = sys.argv[1]
with open(fn, 'r') as f:
    data = f.read().split("\n")

chunks = []
cur = ""
for line in data:
    if re.match(r"^([a-zA-Z0-9_]+)\s*=\s*", line):
        chunks.append(cur)
        cur = ""
    cur += line + "\n"
chunks.append(cur)
chunks.sort()

with open(fn, 'w') as f:
    f.write(''.join(chunks).strip("\n\n") + "\n")
```
</details>
2023-05-26 03:31:04 +00:00
clubby789
f97fddab91 Ensure Fluent messages are in alphabetical order 2023-05-25 23:49:35 +00:00
Michael Goulet
c2e3521bfb
Rollup merge of #111950 - cjgillot:expn-noinline, r=oli-obk
Remove ExpnKind::Inlined.

Suggested in https://github.com/rust-lang/rust/pull/111815#issuecomment-1561903339

r? ``@oli-obk``
2023-05-25 13:58:02 -07:00
Camille GILLOT
844c1cc5fe Remove DesugaringKind::Replace. 2023-05-25 17:40:46 +00:00
bors
a2b1646c59 Auto merge of #86844 - bjorn3:global_alloc_improvements, r=pnkfelix
Support #[global_allocator] without the allocator shim

This makes it possible to use liballoc/libstd in combination with `--emit obj` if you use `#[global_allocator]`. This is what rust-for-linux uses right now and systemd may use in the future. Currently they have to depend on the exact implementation of the allocator shim to create one themself as `--emit obj` doesn't create an allocator shim.

Note that currently the allocator shim also defines the oom error handler, which is normally required too. Once `#![feature(default_alloc_error_handler)]` becomes the only option, this can be avoided. In addition when using only fallible allocator methods and either `--cfg no_global_oom_handling` for liballoc (like rust-for-linux) or `--gc-sections` no references to the oom error handler will exist.

To avoid this feature being insta-stable, you will have to define `__rust_no_alloc_shim_is_unstable` to avoid linker errors.

(Labeling this with both T-compiler and T-lang as it originally involved both an implementation detail and had an insta-stable user facing change. As noted above, the `__rust_no_alloc_shim_is_unstable` symbol requirement should prevent unintended dependence on this unstable feature.)
2023-05-25 16:59:57 +00:00
Camille GILLOT
0919ec3ecc Remove ExpnKind::Inlined. 2023-05-25 16:43:14 +00:00
Brian M
d816b8b264 Fix Mac Catalyst linking by adding build version
Issue #106021
Apple LD requires build versions in object files for Catalyst
2023-05-25 00:02:13 -07:00
Manish Goregaokar
d0b3ebee66
Rollup merge of #111912 - WaffleLapkin:is_some_and_in_the_compiler, r=petrochenkov
Use `Option::is_some_and` and `Result::is_ok_and` in the compiler

`.is_some_and(..)`/`.is_ok_and(..)` replace `.map_or(false, ..)` and `.map(..).unwrap_or(false)`, making the code more readable.

This PR is a sibling of https://github.com/rust-lang/rust/pull/111873#issuecomment-1561316515
2023-05-24 15:05:05 -07:00
bors
97d328012b Auto merge of #111673 - cjgillot:dominator-preprocess, r=cjgillot,tmiasko
Preprocess and cache dominator tree

Preprocessing dominators has a very strong effect for https://github.com/rust-lang/rust/pull/111344.
That pass checks that assignments dominate their uses repeatedly. Using the unprocessed dominator tree caused a quadratic runtime (number of bbs x depth of the dominator tree).

This PR also caches the dominator tree and the pre-processed dominators in the MIR cfg cache.

Rebase of https://github.com/rust-lang/rust/pull/107157
cc `@tmiasko`
2023-05-24 16:18:21 +00:00
Maybe Waffle
fb0f74a8c9 Use Option::is_some_and and Result::is_ok_and in the compiler 2023-05-24 14:20:41 +00:00
bors
b3cbf7c835 Auto merge of #111694 - Amanieu:no-assume-bdynamic, r=petrochenkov
Don't assume that `-Bdynamic` is the default linker mode

In particular this is false when passing `-static` or `-static-pie` to the linker, which changes the default to `-Bstatic`. This PR ensures we explicitly initialize the correct mode when we first need it.
2023-05-24 13:35:30 +00:00
Esme Yi
18fdca37cf Support rust metadata for AIX. 2023-05-23 16:23:59 +08:00
Adam Gausmann
a7158ecfa9 rustc_codegen_ssa: Set e_flags for AVR architecture based on target CPU 2023-05-21 16:56:57 -05:00
bors
1b67f8b013 Auto merge of #111675 - Urgau:fix-local-libs-for-native-static-libs, r=bjorn3
Fix local libs not included when printing native static libs

This PR fixes https://github.com/rust-lang/rust/issues/111643 by adding the local used libs to the printed `--print=native-static-libs` output.

It seems that `--print=native-static-libs` doesn't have any test, so I added one. It's very simple and doesn't even tries to compile the result to a binary as I don't know how to handle external library linking in CI. (Note that https://github.com/rust-lang/rust/blob/master/tests/run-make/staticlib-dylib-linkage/Makefile does compile to a binary)

r? `@bjorn3`
2023-05-21 01:12:45 +00:00
bors
4eb5225cdf Auto merge of #111413 - workingjubilee:bump-object-0-31-1, r=MarkSimulacrum
Bump object and thorin-dwp

Required to fix watchOS breakage.
2023-05-20 13:19:37 +00:00
bors
17a681000b Auto merge of #111641 - michaelwoerister:debugger-visualizer-fixes, r=cjgillot
Fix dependency tracking for debugger visualizers

This PR fixes dependency tracking for debugger visualizer files by changing the `debugger_visualizers` query to an `eval_always` query that scans the AST while it is still available. This way the set of visualizer files is already available when dep-info is emitted. Since the query is turned into an `eval_always` query, dependency tracking will now reliably detect changes to the visualizer script files themselves.

TODO:
 - [x] perf.rlo
 - [x] Needs a bit more documentation in some places
 - [x] Needs regression test for the incr. comp. case

Fixes https://github.com/rust-lang/rust/issues/111226
Fixes https://github.com/rust-lang/rust/issues/111227
Fixes https://github.com/rust-lang/rust/issues/111295

r? `@wesleywiser`
cc `@gibbyfree`
2023-05-19 11:30:44 +00:00
bors
c9dc55d05c Auto merge of #111345 - jyn514:cfg-release-caching, r=cjgillot,est31
Only depend on CFG_VERSION in rustc_interface

This avoids having to rebuild the whole compiler on each commit when `omit-git-hash = false`.

cc https://github.com/rust-lang/rust/issues/76720 - this won't fix it, and I'm not suggesting we turn this on by default, but it will make it less painful for people who do have `omit-git-hash` on as a workaround.
2023-05-18 21:45:02 +00:00
Dylan DPC
677da1c4e5
Rollup merge of #110884 - korran:unaligned-scalar-mem, r=wesleywiser
Support RISC-V unaligned-scalar-mem target feature

This adds `unaligned-scalar-mem` as an allowed RISC-V target feature. Some RISC-V cores support unaligned access to memory without trapping. On such cores, the compiler could significantly improve code-size and performance when using functions like core::ptr::read_unaligned<u32> by emitting a single load or store instruction with an unaligned address, rather than a long sequence of byte load/store/bitmanip instructions.

Enabling the `unaligned-scalar-mem` target feature allows LLVM to do this optimization.

Fixes #110883
2023-05-18 10:52:33 +05:30
jyn
d5f2b8e5c6 Only depend on CFG_VERSION in rustc_interface
this avoids having to rebuild the whole compiler on each commit when
`omit-git-hash = false`.
2023-05-17 23:54:21 -05:00
Amanieu d'Antras
c2f0313d94 Don't assume that -Bdynamic is the default linker mode
In particular this is false when passing `-static` or `-static-pie` to
the linker, which changes the default to `-Bstatic`. This PR ensures we
explicitly initialize the correct mode when we first need it.
2023-05-17 22:52:54 +02:00
Camille GILLOT
6f271dc49c Cache dominators. 2023-05-17 09:36:12 +00:00
Urgau
701bf2d420 Fix issue #111643 by adding the local used libraries 2023-05-17 11:25:10 +02:00
Dylan DPC
2a5c4baf68
Rollup merge of #110145 - WaffleLapkin:share_slice_of_bytes, r=Nilstrieb
Share slice of bytes

r? `@Nilstrieb`
cc `@noamtashma`
2023-05-17 11:13:55 +05:30
Michael Woerister
d623668551 Move DebuggerVisualizerFile types from rustc_span to rustc_middle 2023-05-16 21:03:28 +02:00
Maybe Waffle
e39d61cbf3 Remove MetadataRef type alias 2023-05-16 11:55:47 +00:00
bors
ce5919fcef Auto merge of #107707 - calebzulawski:remove-features, r=Amanieu
Remove misleading target feature aliases

Fixes #100752.  This is a follow up to #103750. These aliases could not be completely removed until rust-lang/stdarch#1355 landed.

cc `@Amanieu`
2023-05-15 18:47:52 +00:00
John Kåre Alsaker
fff20a703d Move expansion of query macros in rustc_middle to rustc_middle::query 2023-05-15 08:49:13 +02:00
bors
bc888958c9 Auto merge of #111440 - cjgillot:refprop-debuginfo, r=oli-obk
Allow MIR debuginfo to point to a variable's address

MIR optimizations currently do not to operate on borrowed locals.

When enabling #106285, many borrows will be left as-is because they are used in debuginfo. This pass allows to replace this pattern directly in MIR debuginfo:
```rust
a => _1
_1 = &raw? mut? _2
```
becomes
```rust
a => &_2
// No statement to borrow _2.
```

This pass is implemented as a drive-by in ReferencePropagation MIR pass.

This transformation allows following following MIR opts to treat _2 as an unborrowed local, and optimize it as such, even in builds with debuginfo.

In codegen, when encountering `a => &..&_2`, we create a list of allocas:
```llvm
store ptr %_2.dbg.spill, ptr %a.ref0.dbg.spill
store ptr %a.ref0.dbg.spill, ptr %a.ref1.dbg.spill
...
call void `@llvm.dbg.declare(metadata` ptr %a.ref{n}.dbg.spill, /* ... */)
```

Caveat: this transformation looses the exact type, we do not differentiate `a` as a immutable, mutable reference or a raw pointer. Everything is declared to `*mut` to codegen. I'm not convinced this is a blocker.
2023-05-14 05:31:10 +00:00
bors
2c41369acc Auto merge of #111374 - tmiasko:align-unsized-locals, r=cjgillot
Align unsized locals

Allocate an extra space for unsized locals and manually align the storage, since alloca doesn't support dynamic alignment.

Fixes #71416.
Fixes #71695.
2023-05-13 19:03:33 +00:00
bors
dd8ec9c88d Auto merge of #107586 - SparrowLii:parallel-query, r=cjgillot
Introduce `DynSend` and `DynSync` auto trait for parallel compiler

part of parallel-rustc #101566

This PR introduces `DynSend / DynSync` trait and `FromDyn / IntoDyn` structure in rustc_data_structure::marker. `FromDyn` can dynamically check data structures for thread safety when switching to parallel environments (such as calling `par_for_each_in`). This happens only when `-Z threads > 1` so it doesn't affect single-threaded mode's compile efficiency.

r? `@cjgillot`
2023-05-13 13:47:53 +00:00
Camille GILLOT
7de9aac4fb Support ConstantIndex in debuginfo. 2023-05-13 10:12:15 +00:00
Camille GILLOT
2ec0071913 Implement references VarDebugInfo. 2023-05-13 10:12:14 +00:00
Camille GILLOT
1c36f50b3e Extract debug_introduce_local_as_var. 2023-05-13 10:04:17 +00:00
Matthias Krüger
d075b6c16d
Rollup merge of #111393 - klensy:win-0.48, r=oli-obk
bump windows crate 0.46 -> 0.48

This drops duped version of crate(0.46), reduces `rustc_driver.dll` ~800kb and reduces exported functions number from 26k to 22k.

Also while here, added `tidy-alphabetical` sorting to lists in tidy allowed lists.
2023-05-12 07:11:12 +02:00
bjorn3
66982a383b Prevent insta-stable no alloc shim support
You will need to add the following as replacement for the old __rust_*
definitions when not using the alloc shim.

    #[no_mangle]
    static __rust_no_alloc_shim_is_unstable: u8 = 0;
2023-05-11 14:35:09 +00:00
Matthias Krüger
39761b0cf9
Rollup merge of #111382 - Zalathar:ffi, r=cuviper
Isolate coverage FFI type layouts from their underlying LLVM C++ types

I noticed that several of the types used to send coverage information through FFI are not properly isolated from the layout of their corresponding C++ types in the LLVM API.

This PR adds more explicitly-defined FFI struct/enum types in `CoverageMappingWrapper.cpp`, so that Rust source files in `rustc_codegen_ssa` and `rustc_codegen_llvm` aren't directly exposed to LLVM C++ types.
2023-05-11 07:05:27 +02:00
Jubilee Young
7156ff67be Bump object and thorin-dwp
object -> 0.31.1
thorin-dwp -> 0.6.0

Required to fix watchOS breakage.
2023-05-10 21:36:22 -07:00
bors
63fc57b98e Auto merge of #106560 - bjorn3:support_staticlib_dylib_linking, r=pnkfelix
Support linking to rust dylib with --crate-type staticlib

This allows for example dynamically linking libstd, while statically linking the user crate into an executable or C dynamic library. For this two unstable flags (`-Z staticlib-allow-rdylib-deps` and `-Z staticlib-prefer-dynamic`) are introduced. Without the former you get an error. The latter is the equivalent to `-C prefer-dynamic` for the staticlib crate type to indicate that dynamically linking is preferred when both options are available, like for libstd. Care must be taken to ensure that no crate ends up being merged into two distinct staticlibs that are linked together. Doing so will cause a linker error at best and undefined behavior at worst. In addition two distinct staticlibs compiled by different rustc may not be combined under any circumstances due to some rustc private symbols not being mangled.

To successfully link a staticlib, `--print native-static-libs` can be used while compiling to ask rustc for the linker flags necessary when linking the staticlib. This is an existing flag which previously only listed native libraries. It has been extended to list rust dylibs too. Trying to locate libstd yourself to link against it is not supported and may break if for example the libstd of multiple rustc versions are put in the same directory.

For an example on how to use this see the `src/test/run-make-fulldeps/staticlib-dylib-linkage/` test.
2023-05-10 03:40:40 +00:00
klensy
3c03cce341 bump windows crate 0.46 -> 0.48 in workspace 2023-05-09 18:20:13 +03:00
Zalathar
7cab196e7c Isolate coverage FFI type layouts from their underlying LLVM C++ types 2023-05-09 18:08:32 +10:00
Dylan DPC
02a85bd038
Rollup merge of #110304 - cchiw:master, r=davidtwco
Add GNU Property Note

Fix #103001

Generates the missing property note:
```
Displaying notes found in: .note.gnu.property
  Owner                Data size 	Description
  GNU                  0x00000010	NT_GNU_PROPERTY_TYPE_0	      Properties: x86 feature: IBT
```
2023-05-09 12:33:44 +05:30
Tomasz Miąsko
83a5a69a4c Align unsized locals
Allocate an extra space for unsized locals and manually align the
storage, since alloca doesn't support dynamic alignment.
2023-05-08 23:40:51 +02:00
Michael Goulet
fcb275f85e
Rollup merge of #104070 - nbdd0121:unwind, r=Amanieu
Prevent aborting guard from aborting the process in a forced unwind

Fix #101469
2023-05-08 09:30:21 -07:00
Dylan DPC
71a1ac2c9a
Rollup merge of #110297 - kylematsuda:earlybinder_tcx_subst, r=BoxyUwU
Make `(try_)subst_and_normalize_erasing_regions` take `EarlyBinder`

Changes `subst_and_normalize_erasing_regions` and `try_subst_and_normalize_erasing_regions` to take  `EarlyBinder<T>` instead of `T`.

(related to #105779)

This was suggested by `@BoxyUwU` in https://github.com/rust-lang/rust/pull/107753#discussion_r1105828139. After changing `type_of` to return `EarlyBinder`, there were several places where the binder was immediately skipped to call `tcx.subst_and_normalize_erasing_regions`, only for the binder to be reconstructed inside of that method.

r? `@BoxyUwU`
2023-05-08 11:39:20 +05:30
Gary Guo
47171e0c50 Use landingpad filter to encode aborting landing pad 2023-05-07 12:35:54 +01:00
Yuki Okushi
58597717e2
Rollup merge of #105583 - luqmana:bitcast-immediates, r=oli-obk
Operand::extract_field: only cast llval if it's a pointer and replace bitcast w/ pointercast.

Fixes #105439.

Also cc `@erikdesjardins,` looks like another place to cleanup as part of #105545
2023-05-07 14:12:14 +09:00
Kyle Matsuda
82f57c16b7 use EarlyBinder in tcx.(try_)subst_mir_and_normalize_erasing_regions 2023-05-06 22:32:39 -06:00
Matthias Krüger
8172ada984
Rollup merge of #110985 - Amanieu:normalize_asm_spans, r=b-naber
Fix spans in LLVM-generated inline asm errors

Previously, incorrect spans were reported if inline assembly contained CRLF (Windows) line endings.

Fixes #110885
2023-05-06 13:30:04 +02:00
Amanieu d'Antras
bba2a1e071 Fix spans in LLVM-generated inline asm errors
Previously, incorrect spans were reported if inline assembly contained
CRLF (Windows) line endings.

Fixes #110885
2023-05-06 09:31:57 +01:00
SparrowLii
b9746ce039 introduce DynSend and DynSync auto trait 2023-05-06 09:34:18 +08:00
Yuki Okushi
923a5a2ca7
Rollup merge of #109677 - dpaoliello:rawdylib, r=michaelwoerister,wesleywiser
Stabilize raw-dylib, link_ordinal, import_name_type and -Cdlltool

This stabilizes the `raw-dylib` feature (#58713) for all architectures (i.e., `x86` as it is already stable for all other architectures).

Changes:
* Permit the use of the `raw-dylib` link kind for x86, the `link_ordinal` attribute and the `import_name_type` key for the `link` attribute.
* Mark the `raw_dylib` feature as stable.
* Stabilized the `-Zdlltool` argument as `-Cdlltool`.
* Note the path to `dlltool` if invoking it failed (we don't need to do this if `dlltool` returns an error since it prints its path in the error message).
* Adds tests for `-Cdlltool`.
* Adds tests for being unable to find the dlltool executable, and dlltool failing.
* Fixes a bug where we were checking the exit code of dlltool to see if it failed, but dlltool always returns 0 (indicating success), so instead we need to check if anything was written to `stderr`.

NOTE: As previously noted (https://github.com/rust-lang/rust/pull/104218#issuecomment-1315895618) using dlltool within rustc is temporary, but this is not the first time that Rust has added a temporary tool use and argument: https://github.com/rust-lang/rust/pull/104218#issuecomment-1318720482

Big thanks to ``````@tbu-`````` for the first version of this PR (#104218)
2023-05-06 09:09:30 +09:00
Luqman Aden
c7c042ad31 Address review comments.
Remove bitcasts in OperandRef::extract_field; only pointercasts should
be needed.
2023-05-05 15:13:18 -07:00
Luqman Aden
6a5ee11027 Don't bitcast aggregate field. 2023-05-05 14:25:56 -07:00
Luqman Aden
48af94c080 Operand::extract_field: only cast llval if it's a pointer and replace bitcast w/ pointercast. 2023-05-05 14:25:55 -07:00
Charisee
37f3e2f4b1 rewriting match on endianness 2023-05-05 19:47:00 +00:00
Charisee
1fc0442f7e rewriting match on endianness 2023-05-05 19:06:14 +00:00
Charisee
68a5bb4de1 Add GNU Property Note 2023-05-05 18:32:20 +00:00
Matthias Krüger
0ac8ebdf11
Rollup merge of #110826 - cjgillot:place-mention-use, r=JakobDegen,lcnr
Make PlaceMention a non-mutating use.

Fixes https://github.com/rust-lang/rust/issues/110781

r? `@JakobDegen`

I don't agree with your statement in https://github.com/rust-lang/rust/issues/110781#issuecomment-1520841434. I suggest that we start fixing `PlaceContext` to be accurate enough for optimizations to use it. This structure is very convenient to use in visitors, and we perhaps have an opportunity to make it less of a footgun.
2023-05-04 19:18:19 +02:00
Manish Goregaokar
38bbc39895
Rollup merge of #105452 - rcvalle:rust-cfi-3, r=bjorn3
Add cross-language LLVM CFI support to the Rust compiler

This PR adds cross-language LLVM Control Flow Integrity (CFI) support to the Rust compiler by adding the `-Zsanitizer-cfi-normalize-integers` option to be used with Clang `-fsanitize-cfi-icall-normalize-integers` for normalizing integer types (see https://reviews.llvm.org/D139395).

It provides forward-edge control flow protection for C or C++ and Rust -compiled code "mixed binaries" (i.e., for when C or C++ and Rust -compiled code share the same virtual address space). For more information about LLVM CFI and cross-language LLVM CFI support for the Rust compiler, see design document in the tracking issue #89653.

Cross-language LLVM CFI can be enabled with -Zsanitizer=cfi and -Zsanitizer-cfi-normalize-integers, and requires proper (i.e., non-rustc) LTO (i.e., -Clinker-plugin-lto).

Thank you again, ``@bjorn3,`` ``@nikic,`` ``@samitolvanen,`` and the Rust community for all the help!
2023-05-03 16:42:48 -07:00
Ramon de C Valle
004aa15b47 Add cross-language LLVM CFI support to the Rust compiler
This commit adds cross-language LLVM Control Flow Integrity (CFI)
support to the Rust compiler by adding the
`-Zsanitizer-cfi-normalize-integers` option to be used with Clang
`-fsanitize-cfi-icall-normalize-integers` for normalizing integer types
(see https://reviews.llvm.org/D139395).

It provides forward-edge control flow protection for C or C++ and Rust
-compiled code "mixed binaries" (i.e., for when C or C++ and Rust
-compiled code share the same virtual address space). For more
information about LLVM CFI and cross-language LLVM CFI support for the
Rust compiler, see design document in the tracking issue #89653.

Cross-language LLVM CFI can be enabled with -Zsanitizer=cfi and
-Zsanitizer-cfi-normalize-integers, and requires proper (i.e.,
non-rustc) LTO (i.e., -Clinker-plugin-lto).
2023-05-03 22:41:29 +00:00
Dylan DPC
887dffc447
Rollup merge of #111086 - nnethercote:rm-MemEncoder, r=cjgillot
Remove `MemEncoder`

`MemEncoder` only has one non-test use, and `FileEncoder` would be more appropriate there anyway.

r? ``@cjgillot``
2023-05-04 00:17:26 +05:30
Nicholas Nethercote
6b62f37402 Restrict From<S> for {D,Subd}iagnosticMessage.
Currently a `{D,Subd}iagnosticMessage` can be created from any type that
impls `Into<String>`. That includes `&str`, `String`, and `Cow<'static,
str>`, which are reasonable. It also includes `&String`, which is pretty
weird, and results in many places making unnecessary allocations for
patterns like this:
```
self.fatal(&format!(...))
```
This creates a string with `format!`, takes a reference, passes the
reference to `fatal`, which does an `into()`, which clones the
reference, doing a second allocation. Two allocations for a single
string, bleh.

This commit changes the `From` impls so that you can only create a
`{D,Subd}iagnosticMessage` from `&str`, `String`, or `Cow<'static,
str>`. This requires changing all the places that currently create one
from a `&String`. Most of these are of the `&format!(...)` form
described above; each one removes an unnecessary static `&`, plus an
allocation when executed. There are also a few places where the existing
use of `&String` was more reasonable; these now just use `clone()` at
the call site.

As well as making the code nicer and more efficient, this is a step
towards possibly using `Cow<'static, str>` in
`{D,Subd}iagnosticMessage::{Str,Eager}`. That would require changing
the `From<&'a str>` impls to `From<&'static str>`, which is doable, but
I'm not yet sure if it's worthwhile.
2023-05-03 08:44:39 +10:00
Dylan DPC
40c4ed4994
Rollup merge of #110955 - fee1-dead-contrib:sus-operation, r=compiler-errors
uplift `clippy::clone_double_ref` as `suspicious_double_ref_op`

Split from #109842.

r? ``@compiler-errors``
2023-05-02 11:44:52 +05:30
Nicholas Nethercote
b4ba2f0bf4 Change rlink serialization from MemEncoder to FileEncoder.
Because we're writing to a file, so `FileEncoder` is better because we
don't have to write all the data to memory first.
2023-05-01 17:09:59 +10:00
Matthias Krüger
734e866e63
Rollup merge of #110586 - ChrisDenton:msvc-oem-output, r=workingjubilee
Fix Unreadable non-UTF-8 output on localized MSVC

Fixes #35785 by converting non UTF-8 linker output to Unicode using the OEM code page.

Before:

```text
  = note: Non-UTF-8 output: LINK : fatal error LNK1181: cannot open input file \'m\x84rchenhaft.obj\'\r\n
```

After:

```text
   = note: LINK : fatal error LNK1181: cannot open input file 'märchenhaft.obj'
```

The difference is more dramatic if using a non-ascii language pack for Windows.
2023-04-30 01:14:55 +02:00
Camille GILLOT
9325a254f0 Make PlaceMention a non-mutating use. 2023-04-29 16:14:33 +00:00
Matthias Krüger
33f15e899d
Rollup merge of #110917 - Be-ing:whole_archive_with_staticlib_fix, r=petrochenkov
only error combining +whole-archive and +bundle for rlibs

Fixes https://github.com/rust-lang/rust/issues/110912

Checking `flavor == RlibFlavor::Normal` was accidentally lost in 601fc8b36b
https://github.com/rust-lang/rust/pull/105601

That caused combining +whole-archive and +bundle link modifiers on non-rlib crates to fail with a confusing error message saying that combination is unstable for rlibs. In particular, this caused the build to fail when +whole-archive was used on staticlib crates, even though +whole-archive effectively does nothing on non-bin crates because the final linker invocation is left to an external build system.

cc ``@petrochenkov``
2023-04-28 22:56:44 +02:00
Deadbeef
5c99175a9e uplift clippy::clone_double_ref as suspicious_double_ref_op 2023-04-28 17:24:48 +00:00
bors
43a78029b4 Auto merge of #110837 - scottmcm:offset-for-add, r=compiler-errors
Use MIR's `Offset` for pointer `add` too

~~Status: draft while waiting for #110822 to land, since this is built atop that.~~
~~r? `@ghost~~`

Canonical Rust code has mostly moved to `add`/`sub` on pointers, which take `usize`, instead of `offset` which takes `isize`.  (And, relatedly, when `sub_ptr` was added it turned out it replaced every single in-tree use of `offset_from`, because `usize` is just so much more useful than `isize` in Rust.)

Unfortunately, `intrinsics::offset` could only accept `*const` and `isize`, so there's a *huge* amount of type conversions back and forth being done.  They're identity conversions in the backend, but still end up producing quite a lot of unhelpful MIR.

This PR changes `intrinsics::offset` to accept `*const` *and* `*mut` along with `isize` *and* `usize`.  Conveniently, the backends and CTFE already handle this, since MIR's `BinOp::Offset` [already supports all four combinations](adaac6b166/compiler/rustc_const_eval/src/transform/validate.rs (L523-L528)).

To demonstrate the difference, I added some `mir-opt/pre-codegen/` tests around slice indexing.  Here's the difference to `[T]::get_mut`, since it uses `<*mut _>::add` internally:
```diff
`@@` -79,30 +70,21 `@@` fn slice_get_mut_usize(_1: &mut [u32], _2: usize) -> Option<&mut u32> {
         StorageLive(_12);                // scope 3 at $SRC_DIR/core/src/slice/index.rs:LL:COL
         StorageLive(_9);                 // scope 6 at $SRC_DIR/core/src/slice/index.rs:LL:COL
         _9 = _8 as *mut u32 (PtrToPtr);  // scope 11 at $SRC_DIR/core/src/ptr/mut_ptr.rs:LL:COL
-        StorageLive(_13);                // scope 13 at $SRC_DIR/core/src/ptr/mut_ptr.rs:LL:COL
-        _13 = _2 as isize (IntToInt);    // scope 13 at $SRC_DIR/core/src/ptr/mut_ptr.rs:LL:COL
-        StorageLive(_14);                // scope 15 at $SRC_DIR/core/src/ptr/mut_ptr.rs:LL:COL
-        StorageLive(_15);                // scope 15 at $SRC_DIR/core/src/ptr/mut_ptr.rs:LL:COL
-        _15 = _9 as *const u32 (Pointer(MutToConstPointer)); // scope 15 at $SRC_DIR/core/src/ptr/mut_ptr.rs:LL:COL
-        _14 = Offset(move _15, _13);     // scope 15 at $SRC_DIR/core/src/ptr/mut_ptr.rs:LL:COL
-        StorageDead(_15);                // scope 15 at $SRC_DIR/core/src/ptr/mut_ptr.rs:LL:COL
-        _7 = move _14 as *mut u32 (PtrToPtr); // scope 15 at $SRC_DIR/core/src/ptr/mut_ptr.rs:LL:COL
-        StorageDead(_14);                // scope 15 at $SRC_DIR/core/src/ptr/mut_ptr.rs:LL:COL
-        StorageDead(_13);                // scope 13 at $SRC_DIR/core/src/ptr/mut_ptr.rs:LL:COL
+        _7 = Offset(_9, _2);             // scope 13 at $SRC_DIR/core/src/ptr/mut_ptr.rs:LL:COL
         StorageDead(_9);                 // scope 6 at $SRC_DIR/core/src/slice/index.rs:LL:COL
         StorageDead(_12);                // scope 3 at $SRC_DIR/core/src/slice/index.rs:LL:COL
         StorageDead(_11);                // scope 3 at $SRC_DIR/core/src/slice/index.rs:LL:COL
```
1c1c8e442a (diff-a841b6a4538657add3f39bc895744331453d0625e7aace128b1f604f0b63c8fdR80)
2023-04-28 09:26:59 +00:00
Scott McMurray
e1da77c76d Also use mir::Offset for pointer add 2023-04-27 22:44:42 -07:00
Be Wilson
6a89e9451f only error with +whole-archive,+bundle for rlibs
Fixes https://github.com/rust-lang/rust/issues/110912

Checking `flavor == RlibFlavor::Normal` was accidentally lost in
601fc8b36b
https://github.com/rust-lang/rust/pull/105601

That caused combining +whole-archive and +bundle link modifiers on
non-rlib crates to fail with a confusing error message saying that
combination is unstable for rlibs. In particular, this caused the
build to fail when +whole-archive was used on staticlib crates, even
though +whole-archive effectively does nothing on non-bin crates because
the final linker invocation is left to an external build system.
2023-04-27 14:33:36 -05:00
Matthias Krüger
52d550b20e
Rollup merge of #110872 - Jules-Bertholet:err-67981, r=wesleywiser
Nicer ICE for #67981

Provides a slightly nicer ICE for #67981, documenting the problem. A proper fix will be necessary before `#![feature(unsized_fn_params)]` can be stabilized.

The problem is that the design of the `"rust-call"` ABI is fundamentally not compatible with `unsized_fn_params`. `"rust-call"` functions need to collect their arguments into a tuple, but if the arguments are not `Sized`, said tuple is potentially not even a valid type—and if it is, it requires `alloca` to create.

``@rustbot`` label +A-abi +A-codegen +F-unboxed_closures +F-unsized_fn_params
2023-04-27 15:10:56 +02:00
Chris Denton
9b9d39e43f
Abstract MultiByteToWideChar 2023-04-27 09:58:19 +01:00