rust/tests/assembly
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
..
asm [AUTO_GENERATED] Migrate compiletest to use ui_test-style //@ directives 2024-02-22 16:04:04 +00:00
auxiliary Move /src/test to /tests 2023-01-11 09:32:08 +00:00
libs [AUTO_GENERATED] Migrate compiletest to use ui_test-style //@ directives 2024-02-22 16:04:04 +00:00
nvptx-kernel-abi NVPTX: Enable previously disabled tests 2024-03-11 13:35:58 +01:00
stack-protector Rename wasm32-wasi-preview1-threads to wasm32-wasip1-threads 2024-03-11 09:31:41 -07:00
targets Add bare metal riscv32 target. 2024-03-20 16:02:10 +01:00
aarch64-naked-fn-no-bti-prolog.rs [AUTO_GENERATED] Migrate compiletest to use ui_test-style //@ directives 2024-02-22 16:04:04 +00:00
aarch64-pointer-auth.rs [AUTO_GENERATED] Migrate compiletest to use ui_test-style //@ directives 2024-02-22 16:04:04 +00:00
align_offset.rs [AUTO_GENERATED] Migrate compiletest to use ui_test-style //@ directives 2024-02-22 16:04:04 +00:00
closure-inherit-target-feature.rs [AUTO_GENERATED] Migrate compiletest to use ui_test-style //@ directives 2024-02-22 16:04:04 +00:00
dwarf4.rs [AUTO_GENERATED] Migrate compiletest to use ui_test-style //@ directives 2024-02-22 16:04:04 +00:00
dwarf5.rs [AUTO_GENERATED] Migrate compiletest to use ui_test-style //@ directives 2024-02-22 16:04:04 +00:00
is_aligned.rs stabilize ptr.is_aligned, move ptr.is_aligned_to to a new feature gate 2024-03-29 19:59:46 -04:00
niche-prefer-zero.rs [AUTO_GENERATED] Migrate compiletest to use ui_test-style //@ directives 2024-02-22 16:04:04 +00:00
nvptx-arch-default.rs NVPTX: Enable previously disabled tests 2024-03-11 13:35:58 +01:00
nvptx-arch-emit-asm.rs NVPTX: Enable previously disabled tests 2024-03-11 13:35:58 +01:00
nvptx-arch-link-arg.rs [AUTO_GENERATED] Migrate compiletest to use ui_test-style //@ directives 2024-02-22 16:04:04 +00:00
nvptx-arch-target-cpu.rs NVPTX: Enable previously disabled tests 2024-03-11 13:35:58 +01:00
nvptx-atomics.rs [AUTO_GENERATED] Migrate compiletest to use ui_test-style //@ directives 2024-02-22 16:04:04 +00:00
nvptx-internalizing.rs [AUTO_GENERATED] Migrate compiletest to use ui_test-style //@ directives 2024-02-22 16:04:04 +00:00
nvptx-linking-binary.rs [AUTO_GENERATED] Migrate compiletest to use ui_test-style //@ directives 2024-02-22 16:04:04 +00:00
nvptx-linking-cdylib.rs [AUTO_GENERATED] Migrate compiletest to use ui_test-style //@ directives 2024-02-22 16:04:04 +00:00
nvptx-safe-naming.rs NVPTX: Enable previously disabled tests 2024-03-11 13:35:58 +01:00
panic-no-unwind-no-uwtable.rs [AUTO_GENERATED] Migrate compiletest to use ui_test-style //@ directives 2024-02-22 16:04:04 +00:00
panic-unwind-no-uwtable.rs [AUTO_GENERATED] Migrate compiletest to use ui_test-style //@ directives 2024-02-22 16:04:04 +00:00
pic-relocation-model.rs [AUTO_GENERATED] Migrate compiletest to use ui_test-style //@ directives 2024-02-22 16:04:04 +00:00
pie-relocation-model.rs [AUTO_GENERATED] Migrate compiletest to use ui_test-style //@ directives 2024-02-22 16:04:04 +00:00
simd-bitmask.rs Add tests for the generated assembly of mask related simd instructions. 2024-03-12 08:52:54 +01:00
simd-intrinsic-gather.rs Add tests for the generated assembly of mask related simd instructions. 2024-03-12 08:52:54 +01:00
simd-intrinsic-mask-load.rs Add tests for the generated assembly of mask related simd instructions. 2024-03-12 08:52:54 +01:00
simd-intrinsic-mask-reduce.rs Add tests for the generated assembly of mask related simd instructions. 2024-03-12 08:52:54 +01:00
simd-intrinsic-mask-store.rs Add tests for the generated assembly of mask related simd instructions. 2024-03-12 08:52:54 +01:00
simd-intrinsic-scatter.rs Add tests for the generated assembly of mask related simd instructions. 2024-03-12 08:52:54 +01:00
simd-intrinsic-select.rs Add tests for the generated assembly of mask related simd instructions. 2024-03-12 08:52:54 +01:00
slice-is_ascii.rs Ignore less tests in debug builds 2024-02-23 18:04:01 -05:00
sparc-struct-abi.rs [AUTO_GENERATED] Migrate compiletest to use ui_test-style //@ directives 2024-02-22 16:04:04 +00:00
stack-probes.rs [AUTO_GENERATED] Migrate compiletest to use ui_test-style //@ directives 2024-02-22 16:04:04 +00:00
static-relocation-model.rs Ignore less tests in debug builds 2024-02-23 18:04:01 -05:00
strict_provenance.rs [AUTO_GENERATED] Migrate compiletest to use ui_test-style //@ directives 2024-02-22 16:04:04 +00:00
target-feature-multiple.rs [AUTO_GENERATED] Migrate compiletest to use ui_test-style //@ directives 2024-02-22 16:04:04 +00:00
thin-lto.rs [AUTO_GENERATED] Migrate compiletest to use ui_test-style //@ directives 2024-02-22 16:04:04 +00:00
wasm_exceptions.rs Update test directives for wasm32-wasip1 2024-03-11 09:36:35 -07:00
x86_64-array-pair-load-store-merge.rs [AUTO_GENERATED] Migrate compiletest to use ui_test-style //@ directives 2024-02-22 16:04:04 +00:00
x86_64-cmp.rs Add+Use mir::BinOp::Cmp 2024-03-23 23:23:41 -07:00
x86_64-floating-point-clamp.rs [AUTO_GENERATED] Migrate compiletest to use ui_test-style //@ directives 2024-02-22 16:04:04 +00:00
x86_64-fortanix-unknown-sgx-lvi-generic-load.rs [AUTO_GENERATED] Migrate compiletest to use ui_test-style //@ directives 2024-02-22 16:04:04 +00:00
x86_64-fortanix-unknown-sgx-lvi-generic-ret.rs [AUTO_GENERATED] Migrate compiletest to use ui_test-style //@ directives 2024-02-22 16:04:04 +00:00
x86_64-fortanix-unknown-sgx-lvi-inline-assembly.rs [AUTO_GENERATED] Migrate compiletest to use ui_test-style //@ directives 2024-02-22 16:04:04 +00:00
x86_64-function-return.rs [AUTO_GENERATED] Migrate compiletest to use ui_test-style //@ directives 2024-02-22 16:04:04 +00:00
x86_64-naked-fn-no-cet-prolog.rs [AUTO_GENERATED] Migrate compiletest to use ui_test-style //@ directives 2024-02-22 16:04:04 +00:00
x86_64-no-jump-tables.rs [AUTO_GENERATED] Migrate compiletest to use ui_test-style //@ directives 2024-02-22 16:04:04 +00:00
x86_64-sse_crc.rs [AUTO_GENERATED] Migrate compiletest to use ui_test-style //@ directives 2024-02-22 16:04:04 +00:00
x86_64-typed-swap.rs Avoid non-windows non-linux in assembly x64 test 2024-03-23 00:02:53 -07:00