about summary refs log tree commit diff
path: root/tests/codegen
diff options
context:
space:
mode:
authorbors <bors@rust-lang.org>2023-04-14 21:54:56 +0000
committerbors <bors@rust-lang.org>2023-04-14 21:54:56 +0000
commit158c30951383afb9cee7eba14d152eb7b198a910 (patch)
treeb06561d7258ce152a9fe560a6fa6ee3c0ad3d05e /tests/codegen
parent84dd17b56a931a631a23dfd5ef2018fd3ef49108 (diff)
parenta8857d2fe153717e428742f6a8537abbe6793c50 (diff)
Auto merge of #110197 - cjgillot:codegen-discr, r=pnkfelix
Do not attempt to commute comparison and cast to codegen discriminants

The general algorithm to compute a discriminant is:
```
relative_tag = tag - niche_start
is_niche = relative_tag <= (ule) relative_max
discr = if is_niche {
    cast(relative_tag) + niche_variants.start()
} else {
    untagged_variant
}
```

We have an optimization branch which attempts to merge the addition and the subtraction by commuting them with the cast. We currently get this optimization wrong.

This PR takes the easiest and safest way: remove the optimization, and let LLVM handle it. (Perf may not agree with that course of action :sweat_smile:)

There may be a less invasive solution, but I don't have the necessary knowledge of LLVM semantics to find it. Cranelift has the same optimization, which should be handled similarly.
cc `@nikic` and `@bjorn3` if you have a better solution.

Fixes https://github.com/rust-lang/rust/issues/110128
Diffstat (limited to 'tests/codegen')
-rw-r--r--tests/codegen/enum-match.rs7
1 files changed, 5 insertions, 2 deletions
diff --git a/tests/codegen/enum-match.rs b/tests/codegen/enum-match.rs
index 5f8063a27f7..36c6be19012 100644
--- a/tests/codegen/enum-match.rs
+++ b/tests/codegen/enum-match.rs
@@ -34,8 +34,11 @@ pub enum Enum1 {
 
 // CHECK: define noundef i8 @match1{{.*}}
 // CHECK-NEXT: start:
-// CHECK-NEXT: [[DISCR:%.*]] = {{.*}}call i8 @llvm.usub.sat.i8(i8 %0, i8 1)
-// CHECK-NEXT: switch i8 [[DISCR]], label {{.*}} [
+// CHECK-NEXT: %1 = add i8 %0, -2
+// CHECK-NEXT: %2 = zext i8 %1 to i64
+// CHECK-NEXT: %3 = icmp ult i8 %1, 2
+// CHECK-NEXT: %4 = add nuw nsw i64 %2, 1
+// CHECK-NEXT: %_2 = select i1 %3, i64 %4, i64 0
 #[no_mangle]
 pub fn match1(e: Enum1) -> u8 {
     use Enum1::*;