2937 Commits

Author SHA1 Message Date
Camille Gillot
d58061e613 Restrict simple assignment condition. 2025-09-16 01:22:10 +00:00
Camille Gillot
8811344f22 Elaborate comment. 2025-09-14 13:27:46 +00:00
Camille Gillot
aee7d703c5 Mark reads in statements to avoid overlapping assingments. 2025-09-13 18:07:22 +00:00
bors
364da5d88d Auto merge of #145717 - BoxyUwU:erase_regions_rename, r=lcnr
rename erase_regions to erase_and_anonymize_regions

I find it consistently confusing that `erase_regions` does more than replacing regions with `'erased`. it also makes some code look real goofy to be writing manual folders to erase regions with a comment saying "we cant use erase regions" :> or code that re-calls erase_regions on types with regions already erased just to anonymize all the bound regions.

r? lcnr

idk how i feel about the name being almost twice as long now
2025-09-09 15:04:44 +00:00
Boxy
e379c77586 erase_regions to erase_and_anonymize_regions 2025-09-09 14:49:16 +02:00
Jana Dönszelmann
6087d89004
fixup limit handling code 2025-09-08 15:07:12 -07:00
Matthias Krüger
9a1feef5d8
Rollup merge of #146298 - cjgillot:gvn-derefer, r=nnethercote
GVN: Ensure indirect is first projection in try_as_place.

I haven't found any report for this bug on existing code, but managed to trigger it with rust-lang/rust#143333
2025-09-08 16:34:56 +02:00
bors
a09fbe2c83 Auto merge of #145910 - saethlin:ignore-intrinsic-calls, r=cjgillot
Ignore intrinsic calls in cross-crate-inlining cost model

I noticed in a side project that a function which just compares to `[u64; 2]` for equality is not cross-crate-inlinable. That was surprising to me because I didn't think that code contained a function call, but of course our array comparisons are lowered to an intrinsic. Intrinsic calls don't make a function no longer a leaf, so it makes sense to add this as an exception to the "only leaves" cross-crate-inline heuristic.

This is the useful compare link: https://perf.rust-lang.org/compare.html?start=7cb1a81145a739c4fd858abe3c624ce8e6e5f9cd&end=c3f0a64dbf9fba4722dacf8e39d2fe00069c995e&stat=instructions%3Au because it disables CGU merging in both commits, so effects that cause changes in the sysroot to perturb partitioning downstream are excluded. Perturbations to what is and isn't cross-crate-inlinable in the sysroot has chaotic effects on what items are in which CGUs after merging. It looks like before this PR by sheer luck some of the CGUs dirtied by the patch in eza incr-unchanged happened to be merged together, and with this PR they are not.

The perf runs on this PR point to a nice runtime performance improvement.
2025-09-08 03:03:21 +00:00
bors
2f3f27bf79 Auto merge of #145541 - cjgillot:dest-prop-live-range, r=Amanieu
Reimplement DestinationPropagation according to live ranges.

This PR reimplements DestinationPropagation as a problem of merging live-ranges of locals. We merge locals that have disjoint live-ranges. This allows merging several locals in the same round by updating live range information.

Live ranges are mainly computed using the `MaybeLiveLocals` analysis. The subtlety is that we split each statement and terminator in 2 positions. The first position is the regular statement. The second position is a shadow, which is always more live. It encodes partial writes and dead writes as a local being live for half a statement. This half statement ensures that writes conflict with another local's writes and regular liveness.

r? `@Amanieu`
2025-09-07 23:36:21 +00:00
Matthias Krüger
36557d1046
Rollup merge of #146297 - cjgillot:may-observe-address, r=saethlin
Introduce PlaceContext::may_observe_address.

A small utility method to avoid open-coding the logic in several MIR opts.
2025-09-07 20:02:29 +02:00
Camille Gillot
5d0e66d451 Use rustc_data_structures::union_find. 2025-09-07 16:46:34 +00:00
Camille Gillot
28ff5cf502 Simplify candidate collection. 2025-09-07 16:45:34 +00:00
Camille Gillot
99f6bcf380 Unify a source with all possible destinations. 2025-09-07 16:45:00 +00:00
Camille Gillot
4e9dd1b67b Do not use prepend to avoid quadratic behaviour. 2025-09-07 16:36:30 +00:00
Camille Gillot
de7c633ee5 Simplify VisitPlacesWith. 2025-09-07 16:36:22 +00:00
Camille Gillot
b9262bce67 Use regular MaybeLiveLocals. 2025-09-07 16:35:30 +00:00
Camille GILLOT
9b8a719ae4 Reimplement DestinationPropagation according to live ranges. 2025-09-07 16:24:46 +00:00
bors
55b9b4d1e1 Auto merge of #146289 - cjgillot:gvn-aggregate, r=dianqk
GVN: Allow reusing aggregates if LHS is not a simple local.

This resolves a FIXME in the code. I don't see a reason not to allow this.
2025-09-07 14:02:09 +00:00
Camille GILLOT
91241a1d25 Ensure indirect is first projection in try_as_place. 2025-09-07 13:55:01 +00:00
Camille GILLOT
4e7a068c9a Introduce PlaceContext::may_observe_address. 2025-09-07 13:51:53 +00:00
Matthias Krüger
cb8b5fa4e2
Rollup merge of #146254 - yotamofek:pr/itertools-all-equal-value, r=cjgillot
Use `Itertools::all_equal_value()` where applicable

Just a small cleanup.
We already have `itertools` as a dep in these crates, so might as well use another of its features.
Makes the code simpler IMHO :)
2025-09-07 08:18:59 +02:00
Camille GILLOT
52f74a5162 Allow simplifying aggregates if LHS is not a simple local. 2025-09-07 03:23:23 +00:00
Ben Kimock
ab91a63d40 Ignore intrinsic calls in cross-crate-inlining cost model 2025-09-05 20:44:49 -04:00
Yotam Ofek
f279ae1b05 Use Itertools::all_equal_value() where applicable 2025-09-05 18:43:43 +00:00
Matthias Krüger
81042523c6 remove couple of clones 2025-09-05 15:38:01 +02:00
Stuart Cook
f90cc353b8
Rollup merge of #145932 - JamieCunliffe:target-feature-inlining, r=jackh726
Allow `inline(always)` with a target feature behind a unstable feature `target_feature_inline_always`.

Rather than adding the inline always attribute to the function definition, we add it to the callsite. We can then check that the target features match and that the call would be safe to inline. If the function isn't inlined due to a mismatch, we emit a warning informing the user that the function can't be inlined due to the target feature mismatch.

See tracking issue rust-lang/rust#145574
2025-09-04 10:01:55 +10:00
Nicholas Nethercote
301655eafe Revert introduction of [workspace.dependencies].
This was done in #145740 and #145947. It is causing problems for people
using r-a on anything that uses the rustc-dev rustup package, e.g. Miri,
clippy.

This repository has lots of submodules and subtrees and various
different projects are carved out of pieces of it. It seems like
`[workspace.dependencies]` will just be more trouble than it's worth.
2025-09-02 19:12:54 +10:00
bors
84a1747022 Auto merge of #146077 - Zalathar:rollup-l7ip5yi, r=Zalathar
Rollup of 5 pull requests

Successful merges:

 - rust-lang/rust#145468 (dedup recip, powi, to_degrees, and to_radians float tests)
 - rust-lang/rust#145643 (coverage: Build an "expansion tree" and use it to unexpand raw spans)
 - rust-lang/rust#145754 (fix(lexer): Don't require frontmatters to be escaped with indented fences)
 - rust-lang/rust#146060 (fixup nix dev shell again)
 - rust-lang/rust#146068 (compiletest: Capture panic messages via a custom panic hook)

r? `@ghost`
`@rustbot` modify labels: rollup
2025-09-01 07:44:42 +00:00
Stuart Cook
88c254f8d4
Rollup merge of #145643 - Zalathar:tree, r=SparrowLii
coverage: Build an "expansion tree" and use it to unexpand raw spans

Historically and currently, coverage instrumentation assumes that all of a function's spans are in the same file and have the same syntax context. The spans extracted directly from MIR don't satisfy that assumption, so there is an “unexpansion” step that walks up each span's expansion-call-site tree to find a suitable span in the same context as the function's body span.

(That unexpansion step is what allows us to have somewhat reasonable coverage instrumentation for macros like `println!`, and for syntax like `for` and `?` that undergo desugaring expansion.)

The current unexpansion code mostly works fine in that “flat” single-file single-context world. But it's not suitable for incremental work towards proper expansion-aware coverage instrumentation, which would allow a function's coverage spans to encompass multiple expansion contexts and multiple files.

This PR therefore replaces the current unexpansion code with a more sophisticated system that uses the raw MIR spans to reconstruct an “expansion tree”, and then uses that tree to help perform most of the unexpansion work.

Building the tree is “overkill” for current unexpansion needs (though it does give some minor edge-case improvements), but my hope is that having the explicit tree available will be a big help when taking the next steps towards proper expansion-region support.
2025-09-01 17:35:03 +10:00
Nicholas Nethercote
5ce3797073 Introduce MirDumper and MirWriter.
MIR dumping is a mess. There are lots of functions and entry points,
e.g. `dump_mir`, `dump_mir_with_options`, `dump_polonius_mir`,
`dump_mir_to_writer`. Also, it's crucial that `create_dump_file` is
never called without `dump_enabled` first being checked, but there is no
mechanism for ensuring this and it's hard to tell if it is satisfied on
all paths. (`dump_enabled` is checked twice on some paths, however!)

This commit introduces `MirWriter`, which controls the MIR writing, and
encapsulates the `extra_data` closure and `options`. Two existing
functions are now methods of this type. It sets reasonable defaults,
allowing the removal of many `|_, _| Ok(())` closures.

The commit also introduces `MirDumper`, which is layered on top of
`MirWriter`, and which manages the creation of the dump files,
encapsulating pass names, disambiguators, etc. Four existing functions
are now methods of this type.
- `MirDumper::new` will only succeed if dumps are enabled, and will
  return `None` otherwise, which makes it impossible to dump when you
  shouldn't.
- It also sets reasonable defaults for various things like
  disambiguators, which means you no longer need to specify them in many
  cases. When they do need to be specified, it's now done via setter
  methods.
- It avoids some repetition. E.g. `dump_nll_mir` previously specifed the
  pass name `"nll"` four times and the disambiguator `&0` three times;
  now it specifies them just once, to put them in the `MirDumper`.
- For Polonius, the `extra_data` closure can now be specified earlier,
  which avoids having to pass some arguments through some functions.
2025-09-01 09:19:03 +10:00
Nicholas Nethercote
d3e2c93498 Use trait object references for closures.
The dynamic dispatch cost doesn't matter for MIR dumping, which is
perf-insensitive. And it's necessary for the next commit, which will
store some `extra_data` closures in a struct.
2025-09-01 08:52:34 +10:00
Nicholas Nethercote
3a0d0be586 Inline and remove dump_mir_for_pass.
The code is more readable without it.
2025-09-01 08:52:33 +10:00
Nicholas Nethercote
d263d3a88c Add either to [workspace.dependencies]. 2025-08-28 20:10:55 +10:00
James Barford-Evans
bcfc9b5073 inline at the callsite & warn when target features mismatch
Co-authored-by: Jamie Cunliffe <Jamie.Cunliffe@arm.com>
2025-08-27 14:45:01 +01:00
Nicholas Nethercote
200f56d605 Add itertools to [workspace.dependencies]. 2025-08-27 14:21:21 +10:00
Nicholas Nethercote
c50d2cc807 Add tracing to [workspace.dependencies]. 2025-08-27 14:21:19 +10:00
Jana Dönszelmann
d66ca53000
add a flag to codegen fn attrs for foreign items 2025-08-26 13:05:51 +02:00
Zalathar
c2eb45b4a1 coverage: Build an "expansion tree" and use it to unexpand raw spans 2025-08-24 20:29:34 +10:00
Samuel Tardieu
323e23005a Fix ICE when validating transmuting ZST to inhabited enum
MIR validation attempts to determine the number of bytes needed to
represent the size of the source type to compute the discriminant for
the inhabited target enum. For a ZST source, there is no source data to
use as a discriminant so no proper runtime check can be generated.

Since that should never be possible, insert a delayed bug to ensure the
problem has been properly reported to the user by the type checker.
2025-08-23 19:25:58 +02:00
Jacob Pratt
ca6415d692
Rollup merge of #145738 - cjgillot:union-find-uplift, r=Zalathar
Uplift rustc_mir_transform::coverage::counters::union_find to rustc_data_structures.

I was wondering if we had some union-find implementation in the compiler. We do. So put it where we can find it.
2025-08-22 22:00:56 -04:00
Jacob Pratt
c80e77fa21
Rollup merge of #145695 - cjgillot:place-elem-map, r=oli-obk,lcnr
Introduce ProjectionElem::try_map.

Small utility function useful to manipulate MIR place projections.
2025-08-22 22:00:54 -04:00
Jacob Pratt
bc4a6431eb
Rollup merge of #142185 - saethlin:refprop-moves, r=cjgillot
Convert moves of references to copies in ReferencePropagation

This is a fix for https://github.com/rust-lang/rust/issues/141101.

The root cause of this miscompile is that the SsaLocals analysis that MIR transforms use is supposed to detect locals that are only written to once, in their single assignment. But that analysis is subtly wrong; it does not consider `Operand::Move` to be a write even though the meaning ascribed to `Operand::Move` (at least as a function parameter) by Miri is that the callee may have done arbitrary writes to the caller's Local that the Operand wraps (because `Move` is pass-by-pointer). So Miri conwiders `Operand::Move` to be a write but both the MIR visitor system considers it a read, and so does SsaLocals.

I have tried fixing this by changing the `PlaceContext` that is ascribed to an `Operand::Move` to a `MutatingUseContext` but that seems to have borrow checker implications, and changing SsaLocals seems to have wide-ranging regressions in MIR optimizations.

So instead of doing those, this PR adds a new kludge to ReferencePropagation, which follows the same line of thinking as the kludge in CopyProp that solves this same problem inside that pass: a5584a8fe1/compiler/rustc_mir_transform/src/copy_prop.rs (L65-L98)
2025-08-22 22:00:46 -04:00
Camille Gillot
689171d38e Uplift rustc_mir_transform::coverage::counters::union_find to rustc_data_structures. 2025-08-22 02:22:51 +00:00
Camille Gillot
e5bd01b533 Correct comments. 2025-08-21 23:59:20 +00:00
Camille GILLOT
37e7f52876 Introduce ProjectionElem::try_map. 2025-08-21 02:06:21 +00:00
bors
425a9c0a0e Auto merge of #145284 - nnethercote:type_name-print-regions, r=lcnr
Print regions in `type_name`.

Currently they are skipped, which is a bit weird, and it sometimes causes malformed output like `Foo<>` and `dyn Bar<, A = u32>`.

Most regions are erased by the time `type_name` does its work. So all regions are now printed as `'_` in non-optional places. Not perfect, but better than the status quo.

`c_name` is updated to trim lifetimes from MIR pass names, so that the `PASS_NAMES` sanity check still works. It is also renamed as `simplify_pass_type_name` and made non-const, because it doesn't need to be const and the non-const implementation is much shorter.

The commit also renames `should_print_region` as `should_print_optional_region`, which makes it clearer that it only applies to some regions.

Fixes rust-lang/rust#145168.

r? `@lcnr`
2025-08-17 10:24:20 +00:00
Stuart Cook
02efea51af
Rollup merge of #145392 - Zalathar:create-mappings, r=petrochenkov
coverage: Remove intermediate data structures from mapping creation

The data structures in `coverage::mappings` were historically very useful for isolating the details of mapping-extraction from the details of how coverage mappings are stored in MIR.

But because of various changes that have taken place over time, they now provide little value, and cause difficulty for the coordinated changes that will be needed for introducing expansion mapping support.

In the future, the pendulum might eventually swing back towards these being useful again, but we can always reintroduce suitable intermediate data structures if and when that happens. For now, the simplicity of not having this intermediate layer is a higher priority.

There should be no changes to compiler output.
2025-08-15 16:16:43 +10:00
Zalathar
ecce90b3ef coverage: Remove intermediate data structures from mapping creation 2025-08-14 21:35:23 +10:00
Nicholas Nethercote
8296ad0456 Print regions in type_name.
Currently they are skipped, which is a bit weird, and it sometimes
causes malformed output like `Foo<>` and `dyn Bar<, A = u32>`.

Most regions are erased by the time `type_name` does its work. So all
regions are now printed as `'_` in non-optional places. Not perfect, but
better than the status quo.

`c_name` is updated to trim lifetimes from MIR pass names, so that the
`PASS_NAMES` sanity check still works. It is also renamed as
`simplify_pass_type_name` and made non-const, because it doesn't need
to be const and the non-const implementation is much shorter.

The commit also renames `should_print_region` as
`should_print_optional_region`, which makes it clearer that it only
applies to some regions.

Fixes #145168.
2025-08-14 21:13:06 +10:00
Cameron Steffen
d4eb0947f1 Cleanup assoc parent utils 2025-08-13 09:33:09 -05:00