Commit Graph

101 Commits

Author SHA1 Message Date
Matthew Jasper
a277c901d9 Remove MIR unsafe check
This also remove safety information from MIR.
2024-04-03 08:50:12 +00:00
bors
a77322c16f Auto merge of #118310 - scottmcm:three-way-compare, r=davidtwco
Add `Ord::cmp` for primitives as a `BinOp` in MIR

Update: most of this OP was written months ago.  See https://github.com/rust-lang/rust/pull/118310#issuecomment-2016940014 below for where we got to recently that made it ready for review.

---

There are dozens of reasonable ways to implement `Ord::cmp` for integers using comparison, bit-ops, and branches.  Those differences are irrelevant at the rust level, however, so we can make things better by adding `BinOp::Cmp` at the MIR level:

1. Exactly how to implement it is left up to the backends, so LLVM can use whatever pattern its optimizer best recognizes and cranelift can use whichever pattern codegens the fastest.
2. By not inlining those details for every use of `cmp`, we drastically reduce the amount of MIR generated for `derive`d `PartialOrd`, while also making it more amenable to MIR-level optimizations.

Having extremely careful `if` ordering to μoptimize resource usage on broadwell (#63767) is great, but it really feels to me like libcore is the wrong place to put that logic.  Similarly, using subtraction [tricks](https://graphics.stanford.edu/~seander/bithacks.html#CopyIntegerSign) (#105840) is arguably even nicer, but depends on the optimizer understanding it (https://github.com/llvm/llvm-project/issues/73417) to be practical.  Or maybe [bitor is better than add](https://discourse.llvm.org/t/representing-in-ir/67369/2?u=scottmcm)?  But maybe only on a future version that [has `or disjoint` support](https://discourse.llvm.org/t/rfc-add-or-disjoint-flag/75036?u=scottmcm)?  And just because one of those forms happens to be good for LLVM, there's no guarantee that it'd be the same form that GCC or Cranelift would rather see -- especially given their very different optimizers.  Not to mention that if LLVM gets a spaceship intrinsic -- [which it should](https://rust-lang.zulipchat.com/#narrow/stream/131828-t-compiler/topic/Suboptimal.20inlining.20in.20std.20function.20.60binary_search.60/near/404250586) -- we'll need at least a rustc intrinsic to be able to call it.

As for simplifying it in Rust, we now regularly inline `{integer}::partial_cmp`, but it's quite a large amount of IR.  The best way to see that is with 8811efa88b (diff-d134c32d028fbe2bf835fef2df9aca9d13332dd82284ff21ee7ebf717bfa4765R113) -- I added a new pre-codegen MIR test for a simple 3-tuple struct, and this PR change it from 36 locals and 26 basic blocks down to 24 locals and 8 basic blocks.  Even better, as soon as the construct-`Some`-then-match-it-in-same-BB noise is cleaned up, this'll expose the `Cmp == 0` branches clearly in MIR, so that an InstCombine (#105808) can simplify that to just a `BinOp::Eq` and thus fix some of our generated code perf issues.  (Tracking that through today's `if a < b { Less } else if a == b { Equal } else { Greater }` would be *much* harder.)

---

r? `@ghost`
But first I should check that perf is ok with this
~~...and my true nemesis, tidy.~~
2024-04-02 19:21:44 +00:00
DianQK
47ed73a7b5
Eliminate UbCheck for non-standard libraries 2024-03-27 21:02:40 +08:00
Scott McMurray
8d5977d6af Slightly simplify the iN::partial_cmp MIR
This saves some debug and scope metadata in every single function that calls it.

Normally wouldn't be worth it, but with the derives there's *so* many of these.
2024-03-24 10:01:37 -07:00
Scott McMurray
3da115a93b Add+Use mir::BinOp::Cmp 2024-03-23 23:23:41 -07:00
Scott McMurray
744c664ba2 Add a MIR pre-codegen test for derived PartialOrd 2024-03-23 23:06:44 -07:00
bors
2f090c30dd Auto merge of #122629 - RalfJung:assert-unsafe-precondition, r=saethlin
refactor check_{lang,library}_ub: use a single intrinsic

This enacts the plan I laid out [here](https://github.com/rust-lang/rust/pull/122282#issuecomment-1996917998): use a single intrinsic, called `ub_checks` (in aniticpation of https://github.com/rust-lang/compiler-team/issues/725), that just exposes the value of `debug_assertions` (consistently implemented in both codegen and the interpreter). Put the language vs library UB logic into the library.

This makes it easier to do something like https://github.com/rust-lang/rust/pull/122282 in the future: that just slightly alters the semantics of `ub_checks` (making it more approximating when crates built with different flags are mixed), but it no longer affects whether these checks can happen in Miri or compile-time.

The first commit just moves things around; I don't think these macros and functions belong into `intrinsics.rs` as they are not intrinsics.

r? `@saethlin`
2024-03-23 21:11:00 +00:00
Ralf Jung
6177530420 refactor check_{lang,library}_ub: use a single intrinsic, put policy into library 2024-03-23 18:45:05 +01:00
Ben Kimock
07994c9310 Enable more mir-opt tests in debug builds 2024-03-22 20:14:39 -04:00
Ben Kimock
68f284f337 Remove some only- clauses from mir-opt tests 2024-03-18 10:07:43 -04:00
Ralf Jung
7d99e80c55 MIR printing: print the path of uneval'd const; refer to promoteds in a consistent way 2024-03-10 14:59:41 +01:00
Ben Kimock
5a93a59fd5 Distinguish between library and lang UB in assert_unsafe_precondition 2024-03-08 18:53:58 -05:00
DianQK
b5bd98d540
Update MIR with MirPatch in UninhabitedEnumBranching 2024-03-08 08:15:14 +08:00
r0cky
88b9e7a602 Bless test 2024-02-29 10:55:05 +08:00
Ben Kimock
2f3c0b9859 Ignore less tests in debug builds 2024-02-23 18:04:01 -05:00
许杰友 Jieyou Xu (Joe)
6e48b96692
[AUTO_GENERATED] Migrate compiletest to use ui_test-style //@ directives 2024-02-22 16:04:04 +00:00
Ralf Jung
b58f647d54 rename ptr::invalid -> ptr::without_provenance
also introduce ptr::dangling matching NonNull::dangling
2024-02-21 20:15:52 +01:00
Ben Kimock
581e171773 Convert debug_assert_nounwind to intrinsics::debug_assertions 2024-02-19 20:38:09 -05:00
bors
9aa232ecc7 Auto merge of #120405 - cjgillot:gvn-pointer, r=oli-obk
Fold pointer operations in GVN

This PR proposes 2 combinations of cast operations in MIR GVN:
- a chain of `PtrToPtr` or `MutToConstPointer` casts can be folded together into a single `PtrToPtr` cast;
- we attempt to evaluate more ptr ops when there is no provenance.

In particular, this allows to read from static slices.

This is not yet sufficient to see through slice operations that use `PtrComponents` (because that's a union), but still a step forward.

r? `@ghost`
2024-02-11 04:24:40 +00:00
Camille GILLOT
014b29eecf Remove ConstGoto and SeparateConstSwitch. 2024-02-09 21:13:53 +00:00
Camille GILLOT
e132cac3c4 Enable by default. 2024-02-09 21:13:51 +00:00
Camille GILLOT
1f544ca0cc Fold consecutive PtrToPtr casts. 2024-02-09 21:01:56 +00:00
Ben Kimock
611c3cb561 Bless/fix tests 2024-02-08 19:56:30 -05:00
Ralf Jung
b4e1c569fe raw pointer metadata API: data address -> data pointer 2024-01-29 07:56:38 +01:00
bors
bf2637f4e8 Auto merge of #119954 - scottmcm:option-unwrap-failed, r=WaffleLapkin
Split out `option::unwrap_failed` like we have `result::unwrap_failed`

...and like `option::expect_failed`
2024-01-16 15:32:39 +00:00
bors
fa0dc208d0 Auto merge of #119672 - cjgillot:dse-sandwich, r=oli-obk
Sandwich MIR optimizations between DSE.

This PR reorders MIR optimization passes in an attempt to increase their efficiency.

- Stop running CopyProp before GVN, it's useless as GVN will do the same thing anyway. Instead, we perform CopyProp at the end of the pipeline, to ensure we do not emit copy/move chains.
- Run DSE before GVN, as it increases the probability to have single-assignment locals.
- Run DSE after the final CopyProp to turn copies into moves.

r? `@ghost`
2024-01-16 11:34:16 +00:00
bors
f9c2421a2a Auto merge of #119439 - cjgillot:gvn-faster, r=oli-obk
Avoid some redundant work in GVN

The first 2 commits are about reducing the perf effect.

Third commit avoids doing redundant work: is a local is SSA, it already has been simplified, and the resulting value is in `self.locals`. No need to call any code on it.

The last commit avoids removing some storage statements.

r? wg-mir-opt
2024-01-16 05:17:49 +00:00
Scott McMurray
23483664a2 Split out option::unwrap_failed like we have result::unwrap_failed
...and like `option::expect_failed`
2024-01-14 12:45:01 -08:00
Camille GILLOT
0aedd6e86f Sandwich MIR optimizations between DSE. 2024-01-11 09:58:19 +00:00
bors
3a6bf351a3 Auto merge of #119677 - cjgillot:early-cfg-opt, r=oli-obk
Reorder early post-inlining passes.

`RemoveZsts`, `RemoveUnneededDrops` and `UninhabitedEnumBranching` only depend on types, so they should be executed together early after MIR inlining introduces those types.

This does not change the end-result, but this makes the pipeline a bit more consistent.
2024-01-11 04:09:07 +00:00
Camille GILLOT
4071572cb4 Merge dead bb pruning and unreachable bb deduplication. 2024-01-07 15:12:10 +00:00
Camille GILLOT
7e39100586 Avoid recording no-op replacements. 2024-01-07 13:54:05 +00:00
Camille GILLOT
a8c4d43cb1 Reorder early post-inlining passes. 2024-01-07 01:42:57 +00:00
Camille GILLOT
2837727471 Replace legacy ConstProp by GVN. 2023-12-24 20:08:57 +00:00
Camille GILLOT
a03c972816 Enable GVN by default. 2023-12-24 20:08:57 +00:00
Ralf Jung
29c95e98e3 also print 'immutable' flag 2023-12-07 17:46:36 +01:00
Caleb Zulawski
4d9607869a Update std::simd usage and test outputs 2023-11-26 09:02:25 -05:00
Gary Guo
2c03f21d1f Bless MIR tests 2023-11-26 00:14:18 +00:00
Camille GILLOT
0f8f77fcc2 Rebase fallout. 2023-11-01 16:55:18 +00:00
Camille GILLOT
a2e151ca4b Make ui into mir-opt test. 2023-11-01 16:49:18 +00:00
Camille GILLOT
cb918904fe Only emit != assumptions if the otherwise target is reachable. 2023-10-31 11:44:23 +00:00
Camille GILLOT
0b13e636f5 Simplify assume of a constant. 2023-10-31 11:44:23 +00:00
Camille GILLOT
c748ac1f11 Replace SwitchInt to unreachable by an assumption. 2023-10-31 11:44:23 +00:00
Camille GILLOT
4ff03cd1a4 Allow to run filecheck in mir-opt tests. 2023-10-19 15:51:52 +00:00
Ben Kimock
33b0e4be06 Automatically enable cross-crate inlining for small functions 2023-10-17 19:53:51 -04:00
Camille GILLOT
02424e4bc5 Normalize alloc-id in tests. 2023-10-16 16:29:35 +00:00
Camille GILLOT
37f080edbc Also consider call and yield as MIR SSA. 2023-10-08 16:05:26 +00:00
Camille GILLOT
27d6a57e58 Preserve DebugInfo in DeadStoreElimination. 2023-10-06 15:46:11 +00:00
Camille GILLOT
e63d19c4dd Remove mir::LocalDecl::internal. 2023-10-04 17:55:15 +00:00
bors
959b2c703d Auto merge of #115696 - RalfJung:closure-ty-print, r=oli-obk
adjust how closure/generator types are printed

I saw `&[closure@$DIR/issue-20862.rs:2:5]` and I thought it is a slice type, because that's usually what `&[_]` is... it took me a while to realize that this is just a confusing printer and actually there's no slice. Let's use something that cannot be mistaken for a regular type.
2023-09-22 15:19:38 +00:00