about summary refs log tree commit diff
path: root/tests/codegen/skip-mono-inside-if-false.rs
diff options
context:
space:
mode:
authorbors <bors@rust-lang.org>2024-03-13 05:07:26 +0000
committerbors <bors@rust-lang.org>2024-03-13 05:07:26 +0000
commit5a6c1aa2bccfcbfa42f486a54c09bd698378faef (patch)
treedb71d79324c8bf1e111842bc3d2634488fe69c5e /tests/codegen/skip-mono-inside-if-false.rs
parentd3555f3d8e555ce488bbf8eee5eccdb66a464e14 (diff)
parent81d630453b08253dc1d6bcc4139dde16530887d9 (diff)
downloadrust-5a6c1aa2bccfcbfa42f486a54c09bd698378faef.tar.gz
rust-5a6c1aa2bccfcbfa42f486a54c09bd698378faef.zip
Auto merge of #121421 - saethlin:smarter-mono, r=oli-obk
Avoid lowering code under dead SwitchInt targets

The objective of this PR is to detect and eliminate code which is guarded by an `if false`, even if that `false` is a constant which is not known until monomorphization, or is `intrinsics::debug_assertions()`.

The effect of this is that we generate no LLVM IR the standard library's unsafe preconditions, when they are compiled in a build where they should be immediately optimized out. This mono-time optimization ensures that builds which disable debug assertions do not grow a linkage requirement against `core`, which compiler-builtins currently needs: https://github.com/rust-lang/rust/issues/121552

This revives the codegen side of https://github.com/rust-lang/rust/pull/91222 as planned in https://github.com/rust-lang/rust/issues/120848.
Diffstat (limited to 'tests/codegen/skip-mono-inside-if-false.rs')
-rw-r--r--tests/codegen/skip-mono-inside-if-false.rs41
1 files changed, 41 insertions, 0 deletions
diff --git a/tests/codegen/skip-mono-inside-if-false.rs b/tests/codegen/skip-mono-inside-if-false.rs
new file mode 100644
index 00000000000..8b95de99dd3
--- /dev/null
+++ b/tests/codegen/skip-mono-inside-if-false.rs
@@ -0,0 +1,41 @@
+//@ compile-flags: -Cno-prepopulate-passes -Copt-level=0
+
+#![crate_type = "lib"]
+
+#[no_mangle]
+pub fn demo_for_i32() {
+    generic_impl::<i32>();
+}
+
+// Two important things here:
+// - We replace the "then" block with `unreachable` to avoid linking problems
+// - We neither declare nor define the `big_impl` that said block "calls".
+
+// CHECK-LABEL: ; skip_mono_inside_if_false::generic_impl
+// CHECK: start:
+// CHECK-NEXT: br label %[[ELSE_BRANCH:bb[0-9]+]]
+// CHECK: [[ELSE_BRANCH]]:
+// CHECK-NEXT: call skip_mono_inside_if_false::small_impl
+// CHECK: bb{{[0-9]+}}:
+// CHECK-NEXT: ret void
+// CHECK: bb{{[0-9+]}}:
+// CHECK-NEXT: unreachable
+
+fn generic_impl<T>() {
+    trait MagicTrait {
+        const IS_BIG: bool;
+    }
+    impl<T> MagicTrait for T {
+        const IS_BIG: bool = std::mem::size_of::<T>() > 10;
+    }
+    if T::IS_BIG {
+        big_impl::<T>();
+    } else {
+        small_impl::<T>();
+    }
+}
+
+#[inline(never)]
+fn small_impl<T>() {}
+#[inline(never)]
+fn big_impl<T>() {}