From 2f00e86cb5cbb4d4cfe17abc6136aacadbe02382 Mon Sep 17 00:00:00 2001 From: Mazdak Farrokhzad Date: Sun, 10 Nov 2019 10:26:33 +0100 Subject: Introduce MIR optimizations for simplifying `x?` on `Result`s. This optimization depends on inlining for the identity conversions introduced by the lowering of the `?`. To take advantage of `SimplifyArmIdentity`, `-Z mir-opt-level=2` is required because that triggers the inlining MIR optimization. --- src/test/codegen/match.rs | 8 +++++--- src/test/codegen/try_identity.rs | 17 +++++++++++++++++ 2 files changed, 22 insertions(+), 3 deletions(-) create mode 100644 src/test/codegen/try_identity.rs (limited to 'src/test/codegen') diff --git a/src/test/codegen/match.rs b/src/test/codegen/match.rs index 145d4ba6b4c..b5ee5ba2394 100644 --- a/src/test/codegen/match.rs +++ b/src/test/codegen/match.rs @@ -9,19 +9,21 @@ pub enum E { // CHECK-LABEL: @exhaustive_match #[no_mangle] -pub fn exhaustive_match(e: E, unit: ()) { +pub fn exhaustive_match(e: E) -> u8 { // CHECK: switch{{.*}}, label %[[OTHERWISE:[a-zA-Z0-9_]+]] [ // CHECK-NEXT: i[[TY:[0-9]+]] [[DISCR:[0-9]+]], label %[[A:[a-zA-Z0-9_]+]] // CHECK-NEXT: i[[TY:[0-9]+]] [[DISCR:[0-9]+]], label %[[B:[a-zA-Z0-9_]+]] // CHECK-NEXT: ] // CHECK: [[B]]: +// CHECK-NEXT: store i8 1, i8* %1, align 1 // CHECK-NEXT: br label %[[EXIT:[a-zA-Z0-9_]+]] // CHECK: [[OTHERWISE]]: // CHECK-NEXT: unreachable // CHECK: [[A]]: +// CHECK-NEXT: store i8 0, i8* %1, align 1 // CHECK-NEXT: br label %[[EXIT:[a-zA-Z0-9_]+]] match e { - E::A => unit, - E::B => unit, + E::A => 0, + E::B => 1, } } diff --git a/src/test/codegen/try_identity.rs b/src/test/codegen/try_identity.rs new file mode 100644 index 00000000000..30e7adfddf7 --- /dev/null +++ b/src/test/codegen/try_identity.rs @@ -0,0 +1,17 @@ +// compile-flags: -C no-prepopulate-passes -Z mir-opt-level=2 + +// Ensure that `x?` has no overhead on `Result` due to identity `match`es in lowering. +// This requires inlining to trigger the MIR optimizations in `SimplifyArmIdentity`. + +#![crate_type = "lib"] + +type R = Result; + +#[no_mangle] +fn try_identity(x: R) -> R { +// CHECK: start: +// CHECK-NOT: br {{.*}} +// CHECK ret void + let y = x?; + Ok(y) +} -- cgit 1.4.1-3-g733a5