rust/compiler
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
..
rustc
rustc_abi Use the Align type when parsing alignment attributes 2024-04-01 03:05:55 +01:00
rustc_arena
rustc_ast Implement mut ref/mut ref mut 2024-03-27 09:53:23 -04:00
rustc_ast_ir
rustc_ast_lowering Implement mut ref/mut ref mut 2024-03-27 09:53:23 -04:00
rustc_ast_passes Rollup merge of #123188 - klensy:clippy-me2, r=Nilstrieb 2024-03-29 15:17:11 +01:00
rustc_ast_pretty Auto merge of #123080 - Jules-Bertholet:mut-ref-mut, r=Nadrieril 2024-03-29 11:08:11 +00:00
rustc_attr Use the Align type when parsing alignment attributes 2024-04-01 03:05:55 +01:00
rustc_baked_icu_data
rustc_borrowck Stop calling visitors V 2024-03-30 11:13:33 -04:00
rustc_builtin_macros Fix error message for env! when env var is not valid Unicode 2024-04-01 05:44:45 +01:00
rustc_codegen_cranelift Auto merge of #118310 - scottmcm:three-way-compare, r=davidtwco 2024-04-02 19:21:44 +00:00
rustc_codegen_gcc Auto merge of #118310 - scottmcm:three-way-compare, r=davidtwco 2024-04-02 19:21:44 +00:00
rustc_codegen_llvm Auto merge of #118310 - scottmcm:three-way-compare, r=davidtwco 2024-04-02 19:21:44 +00:00
rustc_codegen_ssa Auto merge of #118310 - scottmcm:three-way-compare, r=davidtwco 2024-04-02 19:21:44 +00:00
rustc_const_eval Auto merge of #118310 - scottmcm:three-way-compare, r=davidtwco 2024-04-02 19:21:44 +00:00
rustc_data_structures Auto merge of #121851 - michaelwoerister:mcp-533-effective-vis, r=cjgillot 2024-03-31 16:22:38 +00:00
rustc_driver
rustc_driver_impl Auto merge of #111769 - saethlin:ctfe-backtrace-ctrlc, r=RalfJung 2024-03-26 00:04:03 +00:00
rustc_error_codes
rustc_error_messages
rustc_errors Rollup merge of #122737 - ytmimi:conditionally_ignore_fatal_diagnostic, r=davidtwco 2024-03-24 17:08:15 +01:00
rustc_expand compiler: fix few needless_pass_by_ref_mut clippy lints 2024-03-28 11:37:52 +03:00
rustc_feature Auto merge of #123080 - Jules-Bertholet:mut-ref-mut, r=Nadrieril 2024-03-29 11:08:11 +00:00
rustc_fluent_macro
rustc_fs_util
rustc_graphviz
rustc_hir Auto merge of #118310 - scottmcm:three-way-compare, r=davidtwco 2024-04-02 19:21:44 +00:00
rustc_hir_analysis Auto merge of #118310 - scottmcm:three-way-compare, r=davidtwco 2024-04-02 19:21:44 +00:00
rustc_hir_pretty Implement mut ref/mut ref mut 2024-03-27 09:53:23 -04:00
rustc_hir_typeck Fixup parsing of rustc_never_type_options attribute 2024-04-01 10:56:33 +00:00
rustc_incremental Auto merge of #122721 - oli-obk:merge_queries, r=davidtwco 2024-03-25 01:33:46 +00:00
rustc_index
rustc_index_macros
rustc_infer Rollup merge of #123188 - klensy:clippy-me2, r=Nilstrieb 2024-03-29 15:17:11 +01:00
rustc_interface Auto merge of #115220 - Zoxc:revive-gcx-ptr, r=oli-obk 2024-03-28 14:00:08 +00:00
rustc_lexer
rustc_lint Add support for NonNull in ambiguous_wide_ptr_comparisions 2024-03-29 22:02:07 +01:00
rustc_lint_defs
rustc_llvm RustWrapper: update call for llvm/llvm-project@44d037cc25 2024-03-26 14:10:25 -04:00
rustc_log bump tracing-tree to 0.3 2024-03-30 17:39:43 +03:00
rustc_macros
rustc_metadata Replace RemapFileNameExt::for_codegen with explicit calls 2024-03-28 18:47:26 +01:00
rustc_middle Auto merge of #118310 - scottmcm:three-way-compare, r=davidtwco 2024-04-02 19:21:44 +00:00
rustc_mir_build Auto merge of #122046 - Nadrieril:integrate-or-pats2, r=matthewjasper 2024-04-01 12:31:27 +00:00
rustc_mir_dataflow refactor check_{lang,library}_ub: use a single intrinsic, put policy into library 2024-03-23 18:45:05 +01:00
rustc_mir_transform Auto merge of #118310 - scottmcm:three-way-compare, r=davidtwco 2024-04-02 19:21:44 +00:00
rustc_monomorphize Only allow upstream calls to LLVM intrinsics, not any link_name function 2024-04-01 20:31:19 -04:00
rustc_next_trait_solver Require foldability part of interner item bounds, remove redundant where clauses 2024-03-28 12:30:52 -04:00
rustc_parse Auto merge of #123080 - Jules-Bertholet:mut-ref-mut, r=Nadrieril 2024-03-29 11:08:11 +00:00
rustc_parse_format
rustc_passes Auto merge of #122450 - Urgau:simplify-trim-paths-feature, r=michaelwoerister 2024-03-29 14:00:21 +00:00
rustc_pattern_analysis Rollup merge of #123242 - Nadrieril:require-contiguous-enum-indices, r=compiler-errors 2024-03-31 11:50:41 +02:00
rustc_privacy
rustc_query_impl
rustc_query_system Remove CacheSelector trait now that we can use GATs 2024-03-26 11:03:23 +00:00
rustc_resolve remove def_id_to_node_id in ast lowering 2024-03-28 16:58:03 +08:00
rustc_serialize
rustc_session Rollup merge of #123200 - maurer:kcfi-abort, r=compiler-errors 2024-03-30 14:30:50 +01:00
rustc_smir Auto merge of #118310 - scottmcm:three-way-compare, r=davidtwco 2024-04-02 19:21:44 +00:00
rustc_span Auto merge of #118310 - scottmcm:three-way-compare, r=davidtwco 2024-04-02 19:21:44 +00:00
rustc_symbol_mangling CFI: Rewrite closure and coroutine instances to their trait method 2024-03-30 16:40:38 +00:00
rustc_target CFI: Enable KCFI testing of run-pass tests 2024-03-26 03:16:41 +00:00
rustc_trait_selection Auto merge of #123340 - fmease:rustdoc-simplify-auto-trait-impl-synth, r=GuillaumeGomez 2024-04-02 12:13:44 +00:00
rustc_traits
rustc_transmute Remove unnecessary Partial/Ord derive 2024-03-27 14:02:15 +00:00
rustc_ty_utils Auto merge of #118310 - scottmcm:three-way-compare, r=davidtwco 2024-04-02 19:21:44 +00:00
rustc_type_ir Require foldability part of interner item bounds, remove redundant where clauses 2024-03-28 12:30:52 -04:00
stable_mir Auto merge of #118310 - scottmcm:three-way-compare, r=davidtwco 2024-04-02 19:21:44 +00:00