From 631cd5845e8de329d0e227aaa707d7ea228b8f8f Mon Sep 17 00:00:00 2001 From: Daniel Baumann Date: Wed, 17 Apr 2024 14:20:29 +0200 Subject: Merging upstream version 1.70.0+dfsg1. Signed-off-by: Daniel Baumann --- tests/codegen/abi-main-signature-16bit-c-int.rs | 1 - tests/codegen/adjustments.rs | 2 +- tests/codegen/array-map.rs | 6 +- tests/codegen/auxiliary/static_dllimport_aux.rs | 13 - tests/codegen/comparison-operators-2-tuple.rs | 121 ++++++ tests/codegen/consts.rs | 2 +- tests/codegen/debug-limited.rs | 27 ++ tests/codegen/debug-line-directives-only.rs | 27 ++ tests/codegen/debug-line-tables-only.rs | 27 ++ tests/codegen/enum-match.rs | 7 +- tests/codegen/fewer-names.rs | 4 +- tests/codegen/global_asm.rs | 27 +- tests/codegen/global_asm_include.rs | 27 +- tests/codegen/global_asm_x2.rs | 27 +- tests/codegen/inherit_overflow.rs | 2 +- tests/codegen/inline-function-args-debug-info.rs | 20 + tests/codegen/intrinsics/transmute-x64.rs | 35 ++ tests/codegen/intrinsics/transmute.rs | 431 +++++++++++++++++++++ .../issue-103285-ptr-addr-overflow-check.rs | 16 - tests/codegen/issue-103840.rs | 9 - tests/codegen/issue-105386-ub-in-debuginfo.rs | 22 -- tests/codegen/issue-13018.rs | 11 - tests/codegen/issue-15953.rs | 29 -- tests/codegen/issue-27130.rs | 21 - tests/codegen/issue-32031.rs | 23 -- tests/codegen/issue-32364.rs | 18 - tests/codegen/issue-34634.rs | 16 - tests/codegen/issue-34947-pow-i32.rs | 13 - tests/codegen/issue-37945.rs | 38 -- tests/codegen/issue-44056-macos-tls-align.rs | 28 -- tests/codegen/issue-45222.rs | 63 --- tests/codegen/issue-45466.rs | 15 - .../codegen/issue-45964-bounds-check-slice-pos.rs | 39 -- tests/codegen/issue-47278.rs | 9 - tests/codegen/issue-47442.rs | 22 -- tests/codegen/issue-56267-2.rs | 18 - tests/codegen/issue-56267.rs | 18 - tests/codegen/issue-56927.rs | 43 -- tests/codegen/issue-58881.rs | 21 - tests/codegen/issue-59352.rs | 18 - tests/codegen/issue-69101-bounds-check.rs | 44 --- tests/codegen/issue-73031.rs | 26 -- tests/codegen/issue-73338-effecient-cmp.rs | 39 -- .../issue-73396-bounds-check-after-position.rs | 77 ---- .../issue-73827-bounds-check-index-in-subexpr.rs | 17 - tests/codegen/issue-75525-bounds-checks.rs | 26 -- tests/codegen/issue-75546.rs | 15 - tests/codegen/issue-75659.rs | 63 --- tests/codegen/issue-77812.rs | 32 -- .../issue-81408-dllimport-thinlto-windows.rs | 15 - tests/codegen/issue-84268.rs | 23 -- tests/codegen/issue-85872-multiple-reverse.rs | 20 - tests/codegen/issue-86106.rs | 62 --- tests/codegen/issue-96274.rs | 17 - tests/codegen/issue-96497-slice-size-nowrap.rs | 29 -- .../codegen/issue-98156-const-arg-temp-lifetime.rs | 27 -- .../issue-98294-get-mut-copy-from-slice-opt.rs | 19 - .../issues/auxiliary/static_dllimport_aux.rs | 13 + tests/codegen/issues/issue-101048.rs | 13 + tests/codegen/issues/issue-101082.rs | 17 + tests/codegen/issues/issue-101814.rs | 20 + tests/codegen/issues/issue-103132.rs | 16 + .../issues/issue-103285-ptr-addr-overflow-check.rs | 16 + tests/codegen/issues/issue-103327.rs | 18 + tests/codegen/issues/issue-103840.rs | 9 + .../codegen/issues/issue-105386-ub-in-debuginfo.rs | 22 ++ tests/codegen/issues/issue-106369.rs | 15 + tests/codegen/issues/issue-13018.rs | 11 + tests/codegen/issues/issue-15953.rs | 29 ++ tests/codegen/issues/issue-27130.rs | 21 + tests/codegen/issues/issue-32031.rs | 23 ++ tests/codegen/issues/issue-32364.rs | 18 + tests/codegen/issues/issue-34634.rs | 16 + tests/codegen/issues/issue-34947-pow-i32.rs | 13 + tests/codegen/issues/issue-37945.rs | 35 ++ .../codegen/issues/issue-44056-macos-tls-align.rs | 28 ++ tests/codegen/issues/issue-45222.rs | 63 +++ tests/codegen/issues/issue-45466.rs | 15 + .../issues/issue-45964-bounds-check-slice-pos.rs | 39 ++ tests/codegen/issues/issue-47278.rs | 9 + tests/codegen/issues/issue-47442.rs | 22 ++ tests/codegen/issues/issue-56267-2.rs | 18 + tests/codegen/issues/issue-56267.rs | 18 + tests/codegen/issues/issue-56927.rs | 43 ++ tests/codegen/issues/issue-58881.rs | 21 + tests/codegen/issues/issue-59352.rs | 18 + tests/codegen/issues/issue-69101-bounds-check.rs | 44 +++ tests/codegen/issues/issue-73031.rs | 26 ++ tests/codegen/issues/issue-73258.rs | 38 ++ tests/codegen/issues/issue-73338-effecient-cmp.rs | 39 ++ .../issue-73396-bounds-check-after-position.rs | 77 ++++ .../issue-73827-bounds-check-index-in-subexpr.rs | 17 + tests/codegen/issues/issue-75525-bounds-checks.rs | 26 ++ tests/codegen/issues/issue-75546.rs | 15 + tests/codegen/issues/issue-75659.rs | 63 +++ tests/codegen/issues/issue-75978.rs | 19 + tests/codegen/issues/issue-77812.rs | 32 ++ .../issue-81408-dllimport-thinlto-windows.rs | 15 + tests/codegen/issues/issue-84268.rs | 23 ++ .../codegen/issues/issue-85872-multiple-reverse.rs | 20 + tests/codegen/issues/issue-86106.rs | 62 +++ tests/codegen/issues/issue-96274.rs | 17 + .../issues/issue-96497-slice-size-nowrap.rs | 29 ++ .../issues/issue-98156-const-arg-temp-lifetime.rs | 27 ++ .../issue-98294-get-mut-copy-from-slice-opt.rs | 19 + tests/codegen/issues/issue-99960.rs | 15 + tests/codegen/mem-replace-big-type.rs | 36 ++ tests/codegen/mem-replace-direct-memcpy.rs | 21 +- tests/codegen/optimize-attr-1.rs | 3 - tests/codegen/option-as-slice.rs | 18 +- tests/codegen/ptr-read-metadata.rs | 96 +++++ tests/codegen/remap_path_prefix/main.rs | 2 +- tests/codegen/repr-transparent-aggregates-2.rs | 1 - .../simd-intrinsic-transmute-array.rs | 51 ++- tests/codegen/simd-wide-sum.rs | 2 +- tests/codegen/slice-indexing.rs | 35 ++ tests/codegen/transmute-scalar.rs | 65 +--- tests/codegen/var-names.rs | 4 +- tests/codegen/vec-as-ptr.rs | 19 + tests/codegen/vec-in-place.rs | 42 +- tests/codegen/vec-shrink-panik.rs | 23 +- tests/codegen/virtual-function-elimination.rs | 5 +- 122 files changed, 2289 insertions(+), 1243 deletions(-) delete mode 100644 tests/codegen/auxiliary/static_dllimport_aux.rs create mode 100644 tests/codegen/comparison-operators-2-tuple.rs create mode 100644 tests/codegen/debug-limited.rs create mode 100644 tests/codegen/debug-line-directives-only.rs create mode 100644 tests/codegen/debug-line-tables-only.rs create mode 100644 tests/codegen/inline-function-args-debug-info.rs create mode 100644 tests/codegen/intrinsics/transmute-x64.rs create mode 100644 tests/codegen/intrinsics/transmute.rs delete mode 100644 tests/codegen/issue-103285-ptr-addr-overflow-check.rs delete mode 100644 tests/codegen/issue-103840.rs delete mode 100644 tests/codegen/issue-105386-ub-in-debuginfo.rs delete mode 100644 tests/codegen/issue-13018.rs delete mode 100644 tests/codegen/issue-15953.rs delete mode 100644 tests/codegen/issue-27130.rs delete mode 100644 tests/codegen/issue-32031.rs delete mode 100644 tests/codegen/issue-32364.rs delete mode 100644 tests/codegen/issue-34634.rs delete mode 100644 tests/codegen/issue-34947-pow-i32.rs delete mode 100644 tests/codegen/issue-37945.rs delete mode 100644 tests/codegen/issue-44056-macos-tls-align.rs delete mode 100644 tests/codegen/issue-45222.rs delete mode 100644 tests/codegen/issue-45466.rs delete mode 100644 tests/codegen/issue-45964-bounds-check-slice-pos.rs delete mode 100644 tests/codegen/issue-47278.rs delete mode 100644 tests/codegen/issue-47442.rs delete mode 100644 tests/codegen/issue-56267-2.rs delete mode 100644 tests/codegen/issue-56267.rs delete mode 100644 tests/codegen/issue-56927.rs delete mode 100644 tests/codegen/issue-58881.rs delete mode 100644 tests/codegen/issue-59352.rs delete mode 100644 tests/codegen/issue-69101-bounds-check.rs delete mode 100644 tests/codegen/issue-73031.rs delete mode 100644 tests/codegen/issue-73338-effecient-cmp.rs delete mode 100644 tests/codegen/issue-73396-bounds-check-after-position.rs delete mode 100644 tests/codegen/issue-73827-bounds-check-index-in-subexpr.rs delete mode 100644 tests/codegen/issue-75525-bounds-checks.rs delete mode 100644 tests/codegen/issue-75546.rs delete mode 100644 tests/codegen/issue-75659.rs delete mode 100644 tests/codegen/issue-77812.rs delete mode 100644 tests/codegen/issue-81408-dllimport-thinlto-windows.rs delete mode 100644 tests/codegen/issue-84268.rs delete mode 100644 tests/codegen/issue-85872-multiple-reverse.rs delete mode 100644 tests/codegen/issue-86106.rs delete mode 100644 tests/codegen/issue-96274.rs delete mode 100644 tests/codegen/issue-96497-slice-size-nowrap.rs delete mode 100644 tests/codegen/issue-98156-const-arg-temp-lifetime.rs delete mode 100644 tests/codegen/issue-98294-get-mut-copy-from-slice-opt.rs create mode 100644 tests/codegen/issues/auxiliary/static_dllimport_aux.rs create mode 100644 tests/codegen/issues/issue-101048.rs create mode 100644 tests/codegen/issues/issue-101082.rs create mode 100644 tests/codegen/issues/issue-101814.rs create mode 100644 tests/codegen/issues/issue-103132.rs create mode 100644 tests/codegen/issues/issue-103285-ptr-addr-overflow-check.rs create mode 100644 tests/codegen/issues/issue-103327.rs create mode 100644 tests/codegen/issues/issue-103840.rs create mode 100644 tests/codegen/issues/issue-105386-ub-in-debuginfo.rs create mode 100644 tests/codegen/issues/issue-106369.rs create mode 100644 tests/codegen/issues/issue-13018.rs create mode 100644 tests/codegen/issues/issue-15953.rs create mode 100644 tests/codegen/issues/issue-27130.rs create mode 100644 tests/codegen/issues/issue-32031.rs create mode 100644 tests/codegen/issues/issue-32364.rs create mode 100644 tests/codegen/issues/issue-34634.rs create mode 100644 tests/codegen/issues/issue-34947-pow-i32.rs create mode 100644 tests/codegen/issues/issue-37945.rs create mode 100644 tests/codegen/issues/issue-44056-macos-tls-align.rs create mode 100644 tests/codegen/issues/issue-45222.rs create mode 100644 tests/codegen/issues/issue-45466.rs create mode 100644 tests/codegen/issues/issue-45964-bounds-check-slice-pos.rs create mode 100644 tests/codegen/issues/issue-47278.rs create mode 100644 tests/codegen/issues/issue-47442.rs create mode 100644 tests/codegen/issues/issue-56267-2.rs create mode 100644 tests/codegen/issues/issue-56267.rs create mode 100644 tests/codegen/issues/issue-56927.rs create mode 100644 tests/codegen/issues/issue-58881.rs create mode 100644 tests/codegen/issues/issue-59352.rs create mode 100644 tests/codegen/issues/issue-69101-bounds-check.rs create mode 100644 tests/codegen/issues/issue-73031.rs create mode 100644 tests/codegen/issues/issue-73258.rs create mode 100644 tests/codegen/issues/issue-73338-effecient-cmp.rs create mode 100644 tests/codegen/issues/issue-73396-bounds-check-after-position.rs create mode 100644 tests/codegen/issues/issue-73827-bounds-check-index-in-subexpr.rs create mode 100644 tests/codegen/issues/issue-75525-bounds-checks.rs create mode 100644 tests/codegen/issues/issue-75546.rs create mode 100644 tests/codegen/issues/issue-75659.rs create mode 100644 tests/codegen/issues/issue-75978.rs create mode 100644 tests/codegen/issues/issue-77812.rs create mode 100644 tests/codegen/issues/issue-81408-dllimport-thinlto-windows.rs create mode 100644 tests/codegen/issues/issue-84268.rs create mode 100644 tests/codegen/issues/issue-85872-multiple-reverse.rs create mode 100644 tests/codegen/issues/issue-86106.rs create mode 100644 tests/codegen/issues/issue-96274.rs create mode 100644 tests/codegen/issues/issue-96497-slice-size-nowrap.rs create mode 100644 tests/codegen/issues/issue-98156-const-arg-temp-lifetime.rs create mode 100644 tests/codegen/issues/issue-98294-get-mut-copy-from-slice-opt.rs create mode 100644 tests/codegen/issues/issue-99960.rs create mode 100644 tests/codegen/mem-replace-big-type.rs create mode 100644 tests/codegen/ptr-read-metadata.rs create mode 100644 tests/codegen/slice-indexing.rs create mode 100644 tests/codegen/vec-as-ptr.rs (limited to 'tests/codegen') diff --git a/tests/codegen/abi-main-signature-16bit-c-int.rs b/tests/codegen/abi-main-signature-16bit-c-int.rs index 4ed491dfb..3548cc06a 100644 --- a/tests/codegen/abi-main-signature-16bit-c-int.rs +++ b/tests/codegen/abi-main-signature-16bit-c-int.rs @@ -17,7 +17,6 @@ // ignore-wasm32 // ignore-x86 // ignore-x86_64 -// ignore-xcore fn main() { } diff --git a/tests/codegen/adjustments.rs b/tests/codegen/adjustments.rs index 6d2247517..b53a68a55 100644 --- a/tests/codegen/adjustments.rs +++ b/tests/codegen/adjustments.rs @@ -13,7 +13,7 @@ pub fn helper(_: usize) { pub fn no_op_slice_adjustment(x: &[u8]) -> &[u8] { // We used to generate an extra alloca and memcpy for the block's trailing expression value, so // check that we copy directly to the return value slot -// CHECK: %0 = insertvalue { {{\[0 x i8\]\*|ptr}}, [[USIZE]] } undef, {{\[0 x i8\]\*|ptr}} %x.0, 0 +// CHECK: %0 = insertvalue { {{\[0 x i8\]\*|ptr}}, [[USIZE]] } poison, {{\[0 x i8\]\*|ptr}} %x.0, 0 // CHECK: %1 = insertvalue { {{\[0 x i8\]\*|ptr}}, [[USIZE]] } %0, [[USIZE]] %x.1, 1 // CHECK: ret { {{\[0 x i8\]\*|ptr}}, [[USIZE]] } %1 { x } diff --git a/tests/codegen/array-map.rs b/tests/codegen/array-map.rs index 9298e89e3..7b8ab2c79 100644 --- a/tests/codegen/array-map.rs +++ b/tests/codegen/array-map.rs @@ -38,10 +38,10 @@ pub fn short_integer_zip_map(x: [u32; 8], y: [u32; 8]) -> [u32; 8] { // // CHECK-LABEL: @long_integer_map #[no_mangle] -pub fn long_integer_map(x: [u32; 64]) -> [u32; 64] { +pub fn long_integer_map(x: [u32; 512]) -> [u32; 512] { // CHECK: start: - // CHECK-NEXT: alloca [64 x i32] - // CHECK-NEXT: alloca %"core::mem::manually_drop::ManuallyDrop<[u32; 64]>" + // CHECK-NEXT: alloca [512 x i32] + // CHECK-NEXT: alloca %"core::mem::manually_drop::ManuallyDrop<[u32; 512]>" // CHECK-NOT: alloca // CHECK: mul <{{[0-9]+}} x i32> // CHECK: add <{{[0-9]+}} x i32> diff --git a/tests/codegen/auxiliary/static_dllimport_aux.rs b/tests/codegen/auxiliary/static_dllimport_aux.rs deleted file mode 100644 index afb0dc42f..000000000 --- a/tests/codegen/auxiliary/static_dllimport_aux.rs +++ /dev/null @@ -1,13 +0,0 @@ -use std::sync::atomic::{AtomicPtr, Ordering}; - -#[inline(always)] -pub fn memrchr() { - fn detect() {} - - static CROSS_CRATE_STATIC_ITEM: AtomicPtr<()> = AtomicPtr::new(detect as *mut ()); - - unsafe { - let fun = CROSS_CRATE_STATIC_ITEM.load(Ordering::SeqCst); - std::mem::transmute::<*mut (), fn()>(fun)() - } -} diff --git a/tests/codegen/comparison-operators-2-tuple.rs b/tests/codegen/comparison-operators-2-tuple.rs new file mode 100644 index 000000000..a9d25e3b5 --- /dev/null +++ b/tests/codegen/comparison-operators-2-tuple.rs @@ -0,0 +1,121 @@ +// compile-flags: -C opt-level=1 -Z merge-functions=disabled +// min-llvm-version: 15.0 +// only-x86_64 + +#![crate_type = "lib"] + +use std::cmp::Ordering; + +type TwoTuple = (i16, u16); + +// +// The operators are all overridden directly, so should optimize easily. +// +// Yes, the `s[lg]t` is correct for the `[lg]e` version because it's only used +// in the side of the select where we know the values are *not* equal. +// + +// CHECK-LABEL: @check_lt_direct +// CHECK-SAME: (i16 noundef %[[A0:.+]], i16 noundef %[[A1:.+]], i16 noundef %[[B0:.+]], i16 noundef %[[B1:.+]]) +#[no_mangle] +pub fn check_lt_direct(a: TwoTuple, b: TwoTuple) -> bool { + // CHECK-DAG: %[[EQ:.+]] = icmp eq i16 %[[A0]], %[[B0]] + // CHECK-DAG: %[[CMP0:.+]] = icmp slt i16 %[[A0]], %[[B0]] + // CHECK-DAG: %[[CMP1:.+]] = icmp ult i16 %[[A1]], %[[B1]] + // CHECK: %[[R:.+]] = select i1 %[[EQ]], i1 %[[CMP1]], i1 %[[CMP0]] + // CHECK: ret i1 %[[R]] + a < b +} + +// CHECK-LABEL: @check_le_direct +// CHECK-SAME: (i16 noundef %[[A0:.+]], i16 noundef %[[A1:.+]], i16 noundef %[[B0:.+]], i16 noundef %[[B1:.+]]) +#[no_mangle] +pub fn check_le_direct(a: TwoTuple, b: TwoTuple) -> bool { + // CHECK-DAG: %[[EQ:.+]] = icmp eq i16 %[[A0]], %[[B0]] + // CHECK-DAG: %[[CMP0:.+]] = icmp slt i16 %[[A0]], %[[B0]] + // CHECK-DAG: %[[CMP1:.+]] = icmp ule i16 %[[A1]], %[[B1]] + // CHECK: %[[R:.+]] = select i1 %[[EQ]], i1 %[[CMP1]], i1 %[[CMP0]] + // CHECK: ret i1 %[[R]] + a <= b +} + +// CHECK-LABEL: @check_gt_direct +// CHECK-SAME: (i16 noundef %[[A0:.+]], i16 noundef %[[A1:.+]], i16 noundef %[[B0:.+]], i16 noundef %[[B1:.+]]) +#[no_mangle] +pub fn check_gt_direct(a: TwoTuple, b: TwoTuple) -> bool { + // CHECK-DAG: %[[EQ:.+]] = icmp eq i16 %[[A0]], %[[B0]] + // CHECK-DAG: %[[CMP0:.+]] = icmp sgt i16 %[[A0]], %[[B0]] + // CHECK-DAG: %[[CMP1:.+]] = icmp ugt i16 %[[A1]], %[[B1]] + // CHECK: %[[R:.+]] = select i1 %[[EQ]], i1 %[[CMP1]], i1 %[[CMP0]] + // CHECK: ret i1 %[[R]] + a > b +} + +// CHECK-LABEL: @check_ge_direct +// CHECK-SAME: (i16 noundef %[[A0:.+]], i16 noundef %[[A1:.+]], i16 noundef %[[B0:.+]], i16 noundef %[[B1:.+]]) +#[no_mangle] +pub fn check_ge_direct(a: TwoTuple, b: TwoTuple) -> bool { + // CHECK-DAG: %[[EQ:.+]] = icmp eq i16 %[[A0]], %[[B0]] + // CHECK-DAG: %[[CMP0:.+]] = icmp sgt i16 %[[A0]], %[[B0]] + // CHECK-DAG: %[[CMP1:.+]] = icmp uge i16 %[[A1]], %[[B1]] + // CHECK: %[[R:.+]] = select i1 %[[EQ]], i1 %[[CMP1]], i1 %[[CMP0]] + // CHECK: ret i1 %[[R]] + a >= b +} + +// +// These ones are harder, since there are more intermediate values to remove. +// +// `<` seems to be getting lucky right now, so test that doesn't regress. +// +// The others, however, aren't managing to optimize away the extra `select`s yet. +// See for more about this. +// + +// CHECK-LABEL: @check_lt_via_cmp +// CHECK-SAME: (i16 noundef %[[A0:.+]], i16 noundef %[[A1:.+]], i16 noundef %[[B0:.+]], i16 noundef %[[B1:.+]]) +#[no_mangle] +pub fn check_lt_via_cmp(a: TwoTuple, b: TwoTuple) -> bool { + // CHECK-DAG: %[[EQ:.+]] = icmp eq i16 %[[A0]], %[[B0]] + // CHECK-DAG: %[[CMP0:.+]] = icmp slt i16 %[[A0]], %[[B0]] + // CHECK-DAG: %[[CMP1:.+]] = icmp ult i16 %[[A1]], %[[B1]] + // CHECK: %[[R:.+]] = select i1 %[[EQ]], i1 %[[CMP1]], i1 %[[CMP0]] + // CHECK: ret i1 %[[R]] + Ord::cmp(&a, &b).is_lt() +} + +// CHECK-LABEL: @check_le_via_cmp +// CHECK-SAME: (i16 noundef %[[A0:.+]], i16 noundef %[[A1:.+]], i16 noundef %[[B0:.+]], i16 noundef %[[B1:.+]]) +#[no_mangle] +pub fn check_le_via_cmp(a: TwoTuple, b: TwoTuple) -> bool { + // FIXME-CHECK-DAG: %[[EQ:.+]] = icmp eq i16 %[[A0]], %[[B0]] + // FIXME-CHECK-DAG: %[[CMP0:.+]] = icmp sle i16 %[[A0]], %[[B0]] + // FIXME-CHECK-DAG: %[[CMP1:.+]] = icmp ule i16 %[[A1]], %[[B1]] + // FIXME-CHECK: %[[R:.+]] = select i1 %[[EQ]], i1 %[[CMP1]], i1 %[[CMP0]] + // FIXME-CHECK: ret i1 %[[R]] + Ord::cmp(&a, &b).is_le() +} + +// CHECK-LABEL: @check_gt_via_cmp +// CHECK-SAME: (i16 noundef %[[A0:.+]], i16 noundef %[[A1:.+]], i16 noundef %[[B0:.+]], i16 noundef %[[B1:.+]]) +#[no_mangle] +pub fn check_gt_via_cmp(a: TwoTuple, b: TwoTuple) -> bool { + // FIXME-CHECK-DAG: %[[EQ:.+]] = icmp eq i16 %[[A0]], %[[B0]] + // FIXME-CHECK-DAG: %[[CMP0:.+]] = icmp sgt i16 %[[A0]], %[[B0]] + // FIXME-CHECK-DAG: %[[CMP1:.+]] = icmp ugt i16 %[[A1]], %[[B1]] + // FIXME-CHECK: %[[R:.+]] = select i1 %[[EQ]], i1 %[[CMP1]], i1 %[[CMP0]] + // FIXME-CHECK: ret i1 %[[R]] + Ord::cmp(&a, &b).is_gt() +} + +// CHECK-LABEL: @check_ge_via_cmp +// CHECK-SAME: (i16 noundef %[[A0:.+]], i16 noundef %[[A1:.+]], i16 noundef %[[B0:.+]], i16 noundef %[[B1:.+]]) +#[no_mangle] +pub fn check_ge_via_cmp(a: TwoTuple, b: TwoTuple) -> bool { + // FIXME-CHECK-DAG: %[[EQ:.+]] = icmp eq i16 %[[A0]], %[[B0]] + // FIXME-CHECK-DAG: %[[CMP0:.+]] = icmp sge i16 %[[A0]], %[[B0]] + // FIXME-CHECK-DAG: %[[CMP1:.+]] = icmp uge i16 %[[A1]], %[[B1]] + // FIXME-CHECK: %[[R:.+]] = select i1 %[[EQ]], i1 %[[CMP1]], i1 %[[CMP0]] + // FIXME-CHECK: ret i1 %[[R]] + Ord::cmp(&a, &b).is_ge() +} diff --git a/tests/codegen/consts.rs b/tests/codegen/consts.rs index dd22fd0f7..fc2badc41 100644 --- a/tests/codegen/consts.rs +++ b/tests/codegen/consts.rs @@ -9,7 +9,7 @@ // CHECK: @STATIC = {{.*}}, align 4 // This checks the constants from inline_enum_const -// CHECK: @alloc_76bfe2f13a3e3b01074971d122eac57e = {{.*}}, align 2 +// CHECK: @alloc_af1f8e8e6f4b341431a1d405e652df2d = {{.*}}, align 2 // This checks the constants from {low,high}_align_const, they share the same // constant, but the alignment differs, so the higher one should be used diff --git a/tests/codegen/debug-limited.rs b/tests/codegen/debug-limited.rs new file mode 100644 index 000000000..48d676887 --- /dev/null +++ b/tests/codegen/debug-limited.rs @@ -0,0 +1,27 @@ +// Verify that the limited debuginfo option emits llvm's FullDebugInfo, but no type info. +// +// compile-flags: -C debuginfo=limited + +#[repr(C)] +struct StructType { + a: i64, + b: i32, +} + +extern "C" { + fn creator() -> *mut StructType; + fn save(p: *const StructType); +} + +fn main() { + unsafe { + let value: &mut StructType = &mut *creator(); + value.a = 7; + save(value as *const StructType) + } +} + +// CHECK: !DICompileUnit +// CHECK: emissionKind: FullDebug +// CHECK: !DILocation +// CHECK-NOT: !DIBasicType diff --git a/tests/codegen/debug-line-directives-only.rs b/tests/codegen/debug-line-directives-only.rs new file mode 100644 index 000000000..750bdd49d --- /dev/null +++ b/tests/codegen/debug-line-directives-only.rs @@ -0,0 +1,27 @@ +// Verify that the only debuginfo generated are the line directives. +// +// compile-flags: -C debuginfo=line-directives-only + +#[repr(C)] +struct StructType { + a: i64, + b: i32, +} + +extern "C" { + fn creator() -> *mut StructType; + fn save(p: *const StructType); +} + +fn main() { + unsafe { + let value: &mut StructType = &mut *creator(); + value.a = 7; + save(value as *const StructType) + } +} + +// CHECK: !DICompileUnit +// CHECK: emissionKind: DebugDirectivesOnly +// CHECK: !DILocation +// CHECK-NOT: !DIBasicType diff --git a/tests/codegen/debug-line-tables-only.rs b/tests/codegen/debug-line-tables-only.rs new file mode 100644 index 000000000..3ed165a6f --- /dev/null +++ b/tests/codegen/debug-line-tables-only.rs @@ -0,0 +1,27 @@ +// Verify that the only debuginfo generated are the line tables. +// +// compile-flags: -C debuginfo=line-tables-only + +#[repr(C)] +struct StructType { + a: i64, + b: i32, +} + +extern "C" { + fn creator() -> *mut StructType; + fn save(p: *const StructType); +} + +fn main() { + unsafe { + let value: &mut StructType = &mut *creator(); + value.a = 7; + save(value as *const StructType) + } +} + +// CHECK: !DICompileUnit +// CHECK: emissionKind: LineTablesOnly +// CHECK: !DILocation +// CHECK-NOT: !DIBasicType diff --git a/tests/codegen/enum-match.rs b/tests/codegen/enum-match.rs index 5f8063a27..36c6be190 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::*; diff --git a/tests/codegen/fewer-names.rs b/tests/codegen/fewer-names.rs index ac8cba06b..7f383a5c1 100644 --- a/tests/codegen/fewer-names.rs +++ b/tests/codegen/fewer-names.rs @@ -13,8 +13,8 @@ pub fn sum(x: u32, y: u32) -> u32 { // NO-LABEL: define{{.*}}i32 @sum(i32 noundef %x, i32 noundef %y) // NO-NEXT: start: -// NO-NEXT: %z = add i32 %y, %x -// NO-NEXT: ret i32 %z +// NO-NEXT: %0 = add i32 %y, %x +// NO-NEXT: ret i32 %0 let z = x + y; z } diff --git a/tests/codegen/global_asm.rs b/tests/codegen/global_asm.rs index fab84868f..9912b1e75 100644 --- a/tests/codegen/global_asm.rs +++ b/tests/codegen/global_asm.rs @@ -1,39 +1,20 @@ // ignore-aarch64 -// ignore-aarch64_be // ignore-arm -// ignore-armeb // ignore-avr -// ignore-bpfel -// ignore-bpfeb +// ignore-bpf +// ignore-bpf // ignore-hexagon // ignore-mips // ignore-mips64 // ignore-msp430 // ignore-powerpc64 -// ignore-powerpc64le // ignore-powerpc -// ignore-r600 -// ignore-amdgcn // ignore-sparc -// ignore-sparcv9 -// ignore-sparcel +// ignore-sparc64 // ignore-s390x -// ignore-tce // ignore-thumb -// ignore-thumbeb -// ignore-xcore -// ignore-nvptx // ignore-nvptx64 -// ignore-le32 -// ignore-le64 -// ignore-amdil -// ignore-amdil64 -// ignore-hsail -// ignore-hsail64 -// ignore-spir -// ignore-spir64 -// ignore-kalimba -// ignore-shave +// ignore-spirv // ignore-wasm32 // ignore-wasm64 // ignore-emscripten diff --git a/tests/codegen/global_asm_include.rs b/tests/codegen/global_asm_include.rs index 02ee91645..b68c5ad3b 100644 --- a/tests/codegen/global_asm_include.rs +++ b/tests/codegen/global_asm_include.rs @@ -1,39 +1,20 @@ // ignore-aarch64 -// ignore-aarch64_be // ignore-arm -// ignore-armeb // ignore-avr -// ignore-bpfel -// ignore-bpfeb +// ignore-bpf +// ignore-bpf // ignore-hexagon // ignore-mips // ignore-mips64 // ignore-msp430 // ignore-powerpc64 -// ignore-powerpc64le // ignore-powerpc -// ignore-r600 -// ignore-amdgcn // ignore-sparc -// ignore-sparcv9 -// ignore-sparcel +// ignore-sparc64 // ignore-s390x -// ignore-tce // ignore-thumb -// ignore-thumbeb -// ignore-xcore -// ignore-nvptx // ignore-nvptx64 -// ignore-le32 -// ignore-le64 -// ignore-amdil -// ignore-amdil64 -// ignore-hsail -// ignore-hsail64 -// ignore-spir -// ignore-spir64 -// ignore-kalimba -// ignore-shave +// ignore-spirv // ignore-wasm32 // ignore-wasm64 // ignore-emscripten diff --git a/tests/codegen/global_asm_x2.rs b/tests/codegen/global_asm_x2.rs index bdcf0ea84..d87e02bef 100644 --- a/tests/codegen/global_asm_x2.rs +++ b/tests/codegen/global_asm_x2.rs @@ -1,39 +1,20 @@ // ignore-aarch64 -// ignore-aarch64_be // ignore-arm -// ignore-armeb // ignore-avr -// ignore-bpfel -// ignore-bpfeb +// ignore-bpf +// ignore-bpf // ignore-hexagon // ignore-mips // ignore-mips64 // ignore-msp430 // ignore-powerpc64 -// ignore-powerpc64le // ignore-powerpc -// ignore-r600 -// ignore-amdgcn // ignore-sparc -// ignore-sparcv9 -// ignore-sparcel +// ignore-sparc64 // ignore-s390x -// ignore-tce // ignore-thumb -// ignore-thumbeb -// ignore-xcore -// ignore-nvptx // ignore-nvptx64 -// ignore-le32 -// ignore-le64 -// ignore-amdil -// ignore-amdil64 -// ignore-hsail -// ignore-hsail64 -// ignore-spir -// ignore-spir64 -// ignore-kalimba -// ignore-shave +// ignore-spirv // ignore-wasm32 // ignore-wasm64 // ignore-emscripten diff --git a/tests/codegen/inherit_overflow.rs b/tests/codegen/inherit_overflow.rs index 0b0b890b2..39909d7ab 100644 --- a/tests/codegen/inherit_overflow.rs +++ b/tests/codegen/inherit_overflow.rs @@ -4,7 +4,7 @@ //[NOASSERT] compile-flags: -Coverflow-checks=off // CHECK-LABEL: define{{.*}} @assertion -// ASSERT: call void @_ZN4core9panicking5panic17h +// ASSERT: call void @{{.*4core9panicking5panic}} // NOASSERT: ret i8 0 #[no_mangle] pub fn assertion() -> u8 { diff --git a/tests/codegen/inline-function-args-debug-info.rs b/tests/codegen/inline-function-args-debug-info.rs new file mode 100644 index 000000000..e3d8caa49 --- /dev/null +++ b/tests/codegen/inline-function-args-debug-info.rs @@ -0,0 +1,20 @@ +// This test checks that debug information includes function argument indexes even if the function +// gets inlined by MIR inlining. Without function argument indexes, `info args` in gdb won't show +// arguments and their values for the current function. + +// compile-flags: -Zinline-mir=yes -Cdebuginfo=2 --edition=2021 + +#![crate_type = "lib"] + +pub fn outer_function(x: usize, y: usize) -> usize { + inner_function(x, y) + 1 +} + +#[inline] +fn inner_function(aaaa: usize, bbbb: usize) -> usize { + // CHECK: !DILocalVariable(name: "aaaa", arg: 1 + // CHECK-SAME: line: 14 + // CHECK: !DILocalVariable(name: "bbbb", arg: 2 + // CHECK-SAME: line: 14 + aaaa + bbbb +} diff --git a/tests/codegen/intrinsics/transmute-x64.rs b/tests/codegen/intrinsics/transmute-x64.rs new file mode 100644 index 000000000..99d258c62 --- /dev/null +++ b/tests/codegen/intrinsics/transmute-x64.rs @@ -0,0 +1,35 @@ +// compile-flags: -O -C no-prepopulate-passes +// only-x86_64 (it's using arch-specific types) +// min-llvm-version: 15.0 # this test assumes `ptr`s + +#![crate_type = "lib"] + +use std::arch::x86_64::{__m128, __m128i, __m256i}; +use std::mem::transmute; + +// CHECK-LABEL: @check_sse_float_to_int( +#[no_mangle] +pub unsafe fn check_sse_float_to_int(x: __m128) -> __m128i { + // CHECK-NOT: alloca + // CHECK: %1 = load <4 x float>, ptr %x, align 16 + // CHECK: store <4 x float> %1, ptr %0, align 16 + transmute(x) +} + +// CHECK-LABEL: @check_sse_pair_to_avx( +#[no_mangle] +pub unsafe fn check_sse_pair_to_avx(x: (__m128i, __m128i)) -> __m256i { + // CHECK-NOT: alloca + // CHECK: %1 = load <4 x i64>, ptr %x, align 16 + // CHECK: store <4 x i64> %1, ptr %0, align 32 + transmute(x) +} + +// CHECK-LABEL: @check_sse_pair_from_avx( +#[no_mangle] +pub unsafe fn check_sse_pair_from_avx(x: __m256i) -> (__m128i, __m128i) { + // CHECK-NOT: alloca + // CHECK: %1 = load <4 x i64>, ptr %x, align 32 + // CHECK: store <4 x i64> %1, ptr %0, align 16 + transmute(x) +} diff --git a/tests/codegen/intrinsics/transmute.rs b/tests/codegen/intrinsics/transmute.rs new file mode 100644 index 000000000..57f901c67 --- /dev/null +++ b/tests/codegen/intrinsics/transmute.rs @@ -0,0 +1,431 @@ +// compile-flags: -O -C no-prepopulate-passes +// only-64bit (so I don't need to worry about usize) +// min-llvm-version: 15.0 # this test assumes `ptr`s + +#![crate_type = "lib"] +#![feature(core_intrinsics)] +#![feature(custom_mir)] +#![feature(inline_const)] +#![allow(unreachable_code)] + +use std::mem::{transmute, MaybeUninit}; + +// Some of the cases here are statically rejected by `mem::transmute`, so +// we need to generate custom MIR for those cases to get to codegen. +use std::intrinsics::mir::*; + +enum Never {} + +#[repr(align(2))] +pub struct BigNever(Never, u16, Never); + +#[repr(align(8))] +pub struct Scalar64(i64); + +#[repr(C, align(4))] +pub struct Aggregate64(u16, u8, i8, f32); + +#[repr(C)] +pub struct Aggregate8(u8); + +// CHECK-LABEL: @check_bigger_size( +#[no_mangle] +#[custom_mir(dialect = "runtime", phase = "initial")] +pub unsafe fn check_bigger_size(x: u16) -> u32 { + // CHECK: call void @llvm.trap + mir!{ + { + RET = CastTransmute(x); + Return() + } + } +} + +// CHECK-LABEL: @check_smaller_size( +#[no_mangle] +#[custom_mir(dialect = "runtime", phase = "initial")] +pub unsafe fn check_smaller_size(x: u32) -> u16 { + // CHECK: call void @llvm.trap + mir!{ + { + RET = CastTransmute(x); + Return() + } + } +} + +// CHECK-LABEL: @check_smaller_array( +#[no_mangle] +#[custom_mir(dialect = "runtime", phase = "initial")] +pub unsafe fn check_smaller_array(x: [u32; 7]) -> [u32; 3] { + // CHECK: call void @llvm.trap + mir!{ + { + RET = CastTransmute(x); + Return() + } + } +} + +// CHECK-LABEL: @check_bigger_array( +#[no_mangle] +#[custom_mir(dialect = "runtime", phase = "initial")] +pub unsafe fn check_bigger_array(x: [u32; 3]) -> [u32; 7] { + // CHECK: call void @llvm.trap + mir!{ + { + RET = CastTransmute(x); + Return() + } + } +} + +// CHECK-LABEL: @check_to_uninhabited( +#[no_mangle] +#[custom_mir(dialect = "runtime", phase = "initial")] +pub unsafe fn check_to_uninhabited(x: u16) -> BigNever { + // CHECK: call void @llvm.trap + mir!{ + { + RET = CastTransmute(x); + Return() + } + } +} + +// CHECK-LABEL: @check_from_uninhabited( +#[no_mangle] +#[custom_mir(dialect = "runtime", phase = "initial")] +pub unsafe fn check_from_uninhabited(x: BigNever) -> u16 { + // CHECK: ret i16 poison + mir!{ + { + RET = CastTransmute(x); + Return() + } + } +} + +// CHECK-LABEL: @check_intermediate_passthrough( +#[no_mangle] +pub unsafe fn check_intermediate_passthrough(x: u32) -> i32 { + // CHECK: start + // CHECK: %[[TMP:.+]] = add i32 1, %x + // CHECK: %[[RET:.+]] = add i32 %[[TMP]], 1 + // CHECK: ret i32 %[[RET]] + unsafe { + transmute::(1 + x) + 1 + } +} + +// CHECK-LABEL: @check_nop_pair( +#[no_mangle] +pub unsafe fn check_nop_pair(x: (u8, i8)) -> (i8, u8) { + // CHECK-NOT: alloca + // CHECK: %0 = insertvalue { i8, i8 } poison, i8 %x.0, 0 + // CHECK: %1 = insertvalue { i8, i8 } %0, i8 %x.1, 1 + // CHECK: ret { i8, i8 } %1 + unsafe { + transmute(x) + } +} + +// CHECK-LABEL: @check_to_newtype( +#[no_mangle] +pub unsafe fn check_to_newtype(x: u64) -> Scalar64 { + // CHECK-NOT: alloca + // CHECK: ret i64 %x + transmute(x) +} + +// CHECK-LABEL: @check_from_newtype( +#[no_mangle] +pub unsafe fn check_from_newtype(x: Scalar64) -> u64 { + // CHECK-NOT: alloca + // CHECK: ret i64 %x + transmute(x) +} + +// CHECK-LABEL: @check_aggregate_to_bool( +#[no_mangle] +pub unsafe fn check_aggregate_to_bool(x: Aggregate8) -> bool { + // CHECK: %x = alloca %Aggregate8, align 1 + // CHECK: %[[BYTE:.+]] = load i8, ptr %x, align 1 + // CHECK: %[[BOOL:.+]] = trunc i8 %[[BYTE]] to i1 + // CHECK: ret i1 %[[BOOL]] + transmute(x) +} + +// CHECK-LABEL: @check_aggregate_from_bool( +#[no_mangle] +pub unsafe fn check_aggregate_from_bool(x: bool) -> Aggregate8 { + // CHECK: %0 = alloca %Aggregate8, align 1 + // CHECK: %[[BYTE:.+]] = zext i1 %x to i8 + // CHECK: store i8 %[[BYTE]], ptr %0, align 1 + transmute(x) +} + +// CHECK-LABEL: @check_byte_to_bool( +#[no_mangle] +pub unsafe fn check_byte_to_bool(x: u8) -> bool { + // CHECK-NOT: alloca + // CHECK: %0 = trunc i8 %x to i1 + // CHECK: ret i1 %0 + transmute(x) +} + +// CHECK-LABEL: @check_byte_from_bool( +#[no_mangle] +pub unsafe fn check_byte_from_bool(x: bool) -> u8 { + // CHECK-NOT: alloca + // CHECK: %0 = zext i1 %x to i8 + // CHECK: ret i8 %0 + transmute(x) +} + +// CHECK-LABEL: @check_to_pair( +#[no_mangle] +pub unsafe fn check_to_pair(x: u64) -> Option { + // CHECK: %0 = alloca { i32, i32 }, align 4 + // CHECK: store i64 %x, ptr %0, align 4 + transmute(x) +} + +// CHECK-LABEL: @check_from_pair( +#[no_mangle] +pub unsafe fn check_from_pair(x: Option) -> u64 { + // The two arguments are of types that are only 4-aligned, but they're + // immediates so we can write using the destination alloca's alignment. + const { assert!(std::mem::align_of::>() == 4) }; + + // CHECK: %0 = alloca i64, align 8 + // CHECK: store i32 %x.0, ptr %1, align 8 + // CHECK: store i32 %x.1, ptr %2, align 4 + // CHECK: %3 = load i64, ptr %0, align 8 + // CHECK: ret i64 %3 + transmute(x) +} + +// CHECK-LABEL: @check_to_float( +#[no_mangle] +pub unsafe fn check_to_float(x: u32) -> f32 { + // CHECK-NOT: alloca + // CHECK: %0 = bitcast i32 %x to float + // CHECK: ret float %0 + transmute(x) +} + +// CHECK-LABEL: @check_from_float( +#[no_mangle] +pub unsafe fn check_from_float(x: f32) -> u32 { + // CHECK-NOT: alloca + // CHECK: %0 = bitcast float %x to i32 + // CHECK: ret i32 %0 + transmute(x) +} + +// CHECK-LABEL: @check_to_bytes( +#[no_mangle] +pub unsafe fn check_to_bytes(x: u32) -> [u8; 4] { + // CHECK: %0 = alloca [4 x i8], align 1 + // CHECK: store i32 %x, ptr %0, align 1 + transmute(x) +} + +// CHECK-LABEL: @check_from_bytes( +#[no_mangle] +pub unsafe fn check_from_bytes(x: [u8; 4]) -> u32 { + // CHECK: %x = alloca [4 x i8], align 1 + // CHECK: %[[VAL:.+]] = load i32, ptr %x, align 1 + // CHECK: ret i32 %[[VAL]] + transmute(x) +} + +// CHECK-LABEL: @check_to_aggregate( +#[no_mangle] +pub unsafe fn check_to_aggregate(x: u64) -> Aggregate64 { + // CHECK: %0 = alloca %Aggregate64, align 4 + // CHECK: store i64 %x, ptr %0, align 4 + // CHECK: %1 = load i64, ptr %0, align 4 + // CHECK: ret i64 %1 + transmute(x) +} + +// CHECK-LABEL: @check_from_aggregate( +#[no_mangle] +pub unsafe fn check_from_aggregate(x: Aggregate64) -> u64 { + // CHECK: %x = alloca %Aggregate64, align 4 + // CHECK: %[[VAL:.+]] = load i64, ptr %x, align 4 + // CHECK: ret i64 %[[VAL]] + transmute(x) +} + +// CHECK-LABEL: @check_long_array_less_aligned( +#[no_mangle] +pub unsafe fn check_long_array_less_aligned(x: [u64; 100]) -> [u16; 400] { + // CHECK-NEXT: start + // CHECK-NEXT: call void @llvm.memcpy.p0.p0.i64(ptr align 2 %0, ptr align 8 %x, i64 800, i1 false) + // CHECK-NEXT: ret void + transmute(x) +} + +// CHECK-LABEL: @check_long_array_more_aligned( +#[no_mangle] +pub unsafe fn check_long_array_more_aligned(x: [u8; 100]) -> [u32; 25] { + // CHECK-NEXT: start + // CHECK-NEXT: call void @llvm.memcpy.p0.p0.i64(ptr align 4 %0, ptr align 1 %x, i64 100, i1 false) + // CHECK-NEXT: ret void + transmute(x) +} + +// CHECK-LABEL: @check_pair_with_bool( +#[no_mangle] +pub unsafe fn check_pair_with_bool(x: (u8, bool)) -> (bool, i8) { + // CHECK-NOT: alloca + // CHECK: trunc i8 %x.0 to i1 + // CHECK: zext i1 %x.1 to i8 + transmute(x) +} + +// CHECK-LABEL: @check_float_to_pointer( +#[no_mangle] +pub unsafe fn check_float_to_pointer(x: f64) -> *const () { + // CHECK-NOT: alloca + // CHECK: %0 = bitcast double %x to i64 + // CHECK: %1 = inttoptr i64 %0 to ptr + // CHECK: ret ptr %1 + transmute(x) +} + +// CHECK-LABEL: @check_float_from_pointer( +#[no_mangle] +pub unsafe fn check_float_from_pointer(x: *const ()) -> f64 { + // CHECK-NOT: alloca + // CHECK: %0 = ptrtoint ptr %x to i64 + // CHECK: %1 = bitcast i64 %0 to double + // CHECK: ret double %1 + transmute(x) +} + +// CHECK-LABEL: @check_array_to_pair( +#[no_mangle] +pub unsafe fn check_array_to_pair(x: [u8; 16]) -> (i64, u64) { + // CHECK-NOT: alloca + // CHECK: %[[FST:.+]] = load i64, ptr %{{.+}}, align 1, !noundef ! + // CHECK: %[[SND:.+]] = load i64, ptr %{{.+}}, align 1, !noundef ! + // CHECK: %[[PAIR0:.+]] = insertvalue { i64, i64 } poison, i64 %[[FST]], 0 + // CHECK: %[[PAIR01:.+]] = insertvalue { i64, i64 } %[[PAIR0]], i64 %[[SND]], 1 + // CHECK: ret { i64, i64 } %[[PAIR01]] + transmute(x) +} + +// CHECK-LABEL: @check_pair_to_array( +#[no_mangle] +pub unsafe fn check_pair_to_array(x: (i64, u64)) -> [u8; 16] { + // CHECK-NOT: alloca + // CHECK: store i64 %x.0, ptr %{{.+}}, align 1 + // CHECK: store i64 %x.1, ptr %{{.+}}, align 1 + transmute(x) +} + +// CHECK-LABEL: @check_heterogeneous_integer_pair( +#[no_mangle] +pub unsafe fn check_heterogeneous_integer_pair(x: (i32, bool)) -> (bool, u32) { + // CHECK: store i32 %x.0 + // CHECK: %[[WIDER:.+]] = zext i1 %x.1 to i8 + // CHECK: store i8 %[[WIDER]] + + // CHECK: %[[BYTE:.+]] = load i8 + // CHECK: trunc i8 %[[BYTE:.+]] to i1 + // CHECK: load i32 + transmute(x) +} + +// CHECK-LABEL: @check_heterogeneous_float_pair( +#[no_mangle] +pub unsafe fn check_heterogeneous_float_pair(x: (f64, f32)) -> (f32, f64) { + // CHECK: store double %x.0 + // CHECK: store float %x.1 + // CHECK: %[[A:.+]] = load float + // CHECK: %[[B:.+]] = load double + // CHECK: %[[P:.+]] = insertvalue { float, double } poison, float %[[A]], 0 + // CHECK: insertvalue { float, double } %[[P]], double %[[B]], 1 + transmute(x) +} + +// CHECK-LABEL: @check_issue_110005( +#[no_mangle] +pub unsafe fn check_issue_110005(x: (usize, bool)) -> Option> { + // CHECK: store i64 %x.0 + // CHECK: %[[WIDER:.+]] = zext i1 %x.1 to i8 + // CHECK: store i8 %[[WIDER]] + // CHECK: load ptr + // CHECK: load i64 + transmute(x) +} + +// CHECK-LABEL: @check_pair_to_dst_ref( +#[no_mangle] +pub unsafe fn check_pair_to_dst_ref<'a>(x: (usize, usize)) -> &'a [u8] { + // CHECK: %0 = inttoptr i64 %x.0 to ptr + // CHECK: %1 = insertvalue { ptr, i64 } poison, ptr %0, 0 + // CHECK: %2 = insertvalue { ptr, i64 } %1, i64 %x.1, 1 + // CHECK: ret { ptr, i64 } %2 + transmute(x) +} + +// CHECK-LABEL: @check_issue_109992( +#[no_mangle] +#[custom_mir(dialect = "runtime", phase = "optimized")] +pub unsafe fn check_issue_109992(x: ()) -> [(); 1] { + // This uses custom MIR to avoid MIR optimizations having removed ZST ops. + + // CHECK: start + // CHECK-NEXT: ret void + mir!{ + { + RET = CastTransmute(x); + Return() + } + } +} + +// CHECK-LABEL: @check_maybe_uninit_pair(i16 %x.0, i64 %x.1) +#[no_mangle] +pub unsafe fn check_maybe_uninit_pair( + x: (MaybeUninit, MaybeUninit), +) -> (MaybeUninit, MaybeUninit) { + // Thanks to `MaybeUninit` this is actually defined behaviour, + // unlike the examples above with pairs of primitives. + + // CHECK: store i16 %x.0 + // CHECK: store i64 %x.1 + // CHECK: load i64 + // CHECK-NOT: noundef + // CHECK: load i16 + // CHECK-NOT: noundef + // CHECK: ret { i64, i16 } + transmute(x) +} + +#[repr(align(8))] +pub struct HighAlignScalar(u8); + +// CHECK-LABEL: @check_to_overalign( +#[no_mangle] +pub unsafe fn check_to_overalign(x: u64) -> HighAlignScalar { + // CHECK: %0 = alloca %HighAlignScalar, align 8 + // CHECK: store i64 %x, ptr %0, align 8 + // CHECK: %1 = load i64, ptr %0, align 8 + // CHECK: ret i64 %1 + transmute(x) +} + +// CHECK-LABEL: @check_from_overalign( +#[no_mangle] +pub unsafe fn check_from_overalign(x: HighAlignScalar) -> u64 { + // CHECK: %x = alloca %HighAlignScalar, align 8 + // CHECK: %[[VAL:.+]] = load i64, ptr %x, align 8 + // CHECK: ret i64 %[[VAL]] + transmute(x) +} diff --git a/tests/codegen/issue-103285-ptr-addr-overflow-check.rs b/tests/codegen/issue-103285-ptr-addr-overflow-check.rs deleted file mode 100644 index a3499babe..000000000 --- a/tests/codegen/issue-103285-ptr-addr-overflow-check.rs +++ /dev/null @@ -1,16 +0,0 @@ -// compile-flags: -O -C debug-assertions=yes - -#![crate_type = "lib"] -#![feature(strict_provenance)] - -#[no_mangle] -pub fn test(src: *const u8, dst: *const u8) -> usize { - // CHECK-LABEL: @test( - // CHECK-NOT: panic - let src_usize = src.addr(); - let dst_usize = dst.addr(); - if src_usize > dst_usize { - return src_usize - dst_usize; - } - return 0; -} diff --git a/tests/codegen/issue-103840.rs b/tests/codegen/issue-103840.rs deleted file mode 100644 index f19d7031b..000000000 --- a/tests/codegen/issue-103840.rs +++ /dev/null @@ -1,9 +0,0 @@ -// compile-flags: -O -#![crate_type = "lib"] - -pub fn foo(t: &mut Vec) { - // CHECK-NOT: __rust_dealloc - let mut taken = std::mem::take(t); - taken.pop(); - *t = taken; -} diff --git a/tests/codegen/issue-105386-ub-in-debuginfo.rs b/tests/codegen/issue-105386-ub-in-debuginfo.rs deleted file mode 100644 index d54ac9e33..000000000 --- a/tests/codegen/issue-105386-ub-in-debuginfo.rs +++ /dev/null @@ -1,22 +0,0 @@ -// compile-flags: --crate-type=lib -O -Cdebuginfo=2 -Cno-prepopulate-passes -// min-llvm-version: 15.0 # this test uses opaque pointer notation -#![feature(stmt_expr_attributes)] - -pub struct S([usize; 8]); - -#[no_mangle] -pub fn outer_function(x: S, y: S) -> usize { - (#[inline(always)]|| { - let _z = x; - y.0[0] - })() -} - -// Check that we do not attempt to load from the spilled arg before it is assigned to -// when generating debuginfo. -// CHECK-LABEL: @outer_function -// CHECK: [[spill:%.*]] = alloca %"[closure@{{.*.rs}}:9:23: 9:25]" -// CHECK-NOT: [[ptr_tmp:%.*]] = getelementptr inbounds %"[closure@{{.*.rs}}:9:23: 9:25]", ptr [[spill]] -// CHECK-NOT: [[load:%.*]] = load ptr, ptr -// CHECK: call void @llvm.lifetime.start{{.*}}({{.*}}, ptr [[spill]]) -// CHECK: call void @llvm.memcpy{{.*}}(ptr {{align .*}} [[spill]], ptr {{align .*}} %x diff --git a/tests/codegen/issue-13018.rs b/tests/codegen/issue-13018.rs deleted file mode 100644 index b70ea1f48..000000000 --- a/tests/codegen/issue-13018.rs +++ /dev/null @@ -1,11 +0,0 @@ -// compile-flags: -O - -// A drop([...].clone()) sequence on an Rc should be a no-op -// In particular, no call to __rust_dealloc should be emitted -#![crate_type = "lib"] -use std::rc::Rc; - -pub fn foo(t: &Rc>) { -// CHECK-NOT: __rust_dealloc - drop(t.clone()); -} diff --git a/tests/codegen/issue-15953.rs b/tests/codegen/issue-15953.rs deleted file mode 100644 index 28d284289..000000000 --- a/tests/codegen/issue-15953.rs +++ /dev/null @@ -1,29 +0,0 @@ -// Test that llvm generates `memcpy` for moving a value -// inside a function and moving an argument. - -struct Foo { - x: Vec, -} - -#[inline(never)] -#[no_mangle] -// CHECK: memcpy -fn interior(x: Vec) -> Vec { - let Foo { x } = Foo { x: x }; - x -} - -#[inline(never)] -#[no_mangle] -// CHECK: memcpy -fn exterior(x: Vec) -> Vec { - x -} - -fn main() { - let x = interior(Vec::new()); - println!("{:?}", x); - - let x = exterior(Vec::new()); - println!("{:?}", x); -} diff --git a/tests/codegen/issue-27130.rs b/tests/codegen/issue-27130.rs deleted file mode 100644 index e5ee94e1f..000000000 --- a/tests/codegen/issue-27130.rs +++ /dev/null @@ -1,21 +0,0 @@ -// compile-flags: -O - -#![crate_type = "lib"] - -// CHECK-LABEL: @trim_in_place -#[no_mangle] -pub fn trim_in_place(a: &mut &[u8]) { - while a.first() == Some(&42) { - // CHECK-NOT: slice_index_order_fail - *a = &a[1..]; - } -} - -// CHECK-LABEL: @trim_in_place2 -#[no_mangle] -pub fn trim_in_place2(a: &mut &[u8]) { - while let Some(&42) = a.first() { - // CHECK-NOT: slice_index_order_fail - *a = &a[2..]; - } -} diff --git a/tests/codegen/issue-32031.rs b/tests/codegen/issue-32031.rs deleted file mode 100644 index abef92c19..000000000 --- a/tests/codegen/issue-32031.rs +++ /dev/null @@ -1,23 +0,0 @@ -// compile-flags: -C no-prepopulate-passes -Copt-level=0 - -#![crate_type = "lib"] - -#[no_mangle] -pub struct F32(f32); - -// CHECK: define{{.*}}float @add_newtype_f32(float %a, float %b) -#[inline(never)] -#[no_mangle] -pub fn add_newtype_f32(a: F32, b: F32) -> F32 { - F32(a.0 + b.0) -} - -#[no_mangle] -pub struct F64(f64); - -// CHECK: define{{.*}}double @add_newtype_f64(double %a, double %b) -#[inline(never)] -#[no_mangle] -pub fn add_newtype_f64(a: F64, b: F64) -> F64 { - F64(a.0 + b.0) -} diff --git a/tests/codegen/issue-32364.rs b/tests/codegen/issue-32364.rs deleted file mode 100644 index 85493a4bb..000000000 --- a/tests/codegen/issue-32364.rs +++ /dev/null @@ -1,18 +0,0 @@ -// Test that `extern "stdcall"` is properly translated. - -// only-x86 - -// compile-flags: -C no-prepopulate-passes - -struct Foo; - -impl Foo { -// CHECK: define internal x86_stdcallcc void @{{.*}}foo{{.*}}() - #[inline(never)] - pub extern "stdcall" fn foo() { - } -} - -fn main() { - Foo::foo::(); -} diff --git a/tests/codegen/issue-34634.rs b/tests/codegen/issue-34634.rs deleted file mode 100644 index f53fa240c..000000000 --- a/tests/codegen/issue-34634.rs +++ /dev/null @@ -1,16 +0,0 @@ -// Test that `wrapping_div` only checks divisor once. -// This test checks that there is only a single compare against -1 and -1 is not present as a -// switch case (the second check present until rustc 1.12). -// This test also verifies that a single panic call is generated (for the division by zero case). - -// compile-flags: -O -#![crate_type = "lib"] - -// CHECK-LABEL: @f -#[no_mangle] -pub fn f(x: i32, y: i32) -> i32 { - // CHECK-COUNT-1: icmp eq i32 %y, -1 - // CHECK-COUNT-1: panic - // CHECK-NOT: i32 -1, label - x.wrapping_div(y) -} diff --git a/tests/codegen/issue-34947-pow-i32.rs b/tests/codegen/issue-34947-pow-i32.rs deleted file mode 100644 index 653da8e8b..000000000 --- a/tests/codegen/issue-34947-pow-i32.rs +++ /dev/null @@ -1,13 +0,0 @@ -// compile-flags: -O - -#![crate_type = "lib"] - -// CHECK-LABEL: @issue_34947 -#[no_mangle] -pub fn issue_34947(x: i32) -> i32 { - // CHECK: mul - // CHECK-NEXT: mul - // CHECK-NEXT: mul - // CHECK-NEXT: ret - x.pow(5) -} diff --git a/tests/codegen/issue-37945.rs b/tests/codegen/issue-37945.rs deleted file mode 100644 index fe54375bb..000000000 --- a/tests/codegen/issue-37945.rs +++ /dev/null @@ -1,38 +0,0 @@ -// compile-flags: -O -Zmerge-functions=disabled -// ignore-x86 -// ignore-arm -// ignore-emscripten -// ignore-gnux32 -// ignore 32-bit platforms (LLVM has a bug with them) - -// Check that LLVM understands that `Iter` pointer is not null. Issue #37945. - -#![crate_type = "lib"] - -use std::slice::Iter; - -#[no_mangle] -pub fn is_empty_1(xs: Iter) -> bool { -// CHECK-LABEL: @is_empty_1( -// CHECK-NEXT: start: -// CHECK-NEXT: [[A:%.*]] = icmp ne {{i32\*|ptr}} {{%xs.0|%xs.1}}, null -// CHECK-NEXT: tail call void @llvm.assume(i1 [[A]]) -// The order between %xs.0 and %xs.1 on the next line doesn't matter -// and different LLVM versions produce different order. -// CHECK-NEXT: [[B:%.*]] = icmp eq {{i32\*|ptr}} {{%xs.0, %xs.1|%xs.1, %xs.0}} -// CHECK-NEXT: ret i1 [[B:%.*]] - {xs}.next().is_none() -} - -#[no_mangle] -pub fn is_empty_2(xs: Iter) -> bool { -// CHECK-LABEL: @is_empty_2 -// CHECK-NEXT: start: -// CHECK-NEXT: [[C:%.*]] = icmp ne {{i32\*|ptr}} {{%xs.0|%xs.1}}, null -// CHECK-NEXT: tail call void @llvm.assume(i1 [[C]]) -// The order between %xs.0 and %xs.1 on the next line doesn't matter -// and different LLVM versions produce different order. -// CHECK-NEXT: [[D:%.*]] = icmp eq {{i32\*|ptr}} {{%xs.0, %xs.1|%xs.1, %xs.0}} -// CHECK-NEXT: ret i1 [[D:%.*]] - xs.map(|&x| x).next().is_none() -} diff --git a/tests/codegen/issue-44056-macos-tls-align.rs b/tests/codegen/issue-44056-macos-tls-align.rs deleted file mode 100644 index 1a3923f1b..000000000 --- a/tests/codegen/issue-44056-macos-tls-align.rs +++ /dev/null @@ -1,28 +0,0 @@ -// -// only-macos -// no-system-llvm -// compile-flags: -O - -#![crate_type = "rlib"] -#![feature(thread_local)] - -// local_unnamed_addr does not appear when std is built with debug assertions. -// CHECK: @STATIC_VAR_1 = thread_local {{(local_unnamed_addr )?}}global <{ [32 x i8] }> zeroinitializer, section "__DATA,__thread_bss", align 4 -#[no_mangle] -#[thread_local] -static mut STATIC_VAR_1: [u32; 8] = [0; 8]; - -// CHECK: @STATIC_VAR_2 = thread_local {{(local_unnamed_addr )?}}global <{ [32 x i8] }> <{{[^>]*}}>, section "__DATA,__thread_data", align 4 -#[no_mangle] -#[thread_local] -static mut STATIC_VAR_2: [u32; 8] = [4; 8]; - -#[no_mangle] -pub unsafe fn f(x: &mut [u32; 8]) { - std::mem::swap(x, &mut STATIC_VAR_1) -} - -#[no_mangle] -pub unsafe fn g(x: &mut [u32; 8]) { - std::mem::swap(x, &mut STATIC_VAR_2) -} diff --git a/tests/codegen/issue-45222.rs b/tests/codegen/issue-45222.rs deleted file mode 100644 index e9b05e648..000000000 --- a/tests/codegen/issue-45222.rs +++ /dev/null @@ -1,63 +0,0 @@ -// compile-flags: -O -// ignore-debug: the debug assertions get in the way - -#![crate_type = "lib"] - -// verify that LLVM recognizes a loop involving 0..=n and will const-fold it. - -// Example from original issue #45222 - -fn foo2(n: u64) -> u64 { - let mut count = 0; - for _ in 0..n { - for j in (0..=n).rev() { - count += j; - } - } - count -} - -// CHECK-LABEL: @check_foo2 -#[no_mangle] -pub fn check_foo2() -> u64 { - // CHECK: ret i64 500005000000000 - foo2(100000) -} - -// Simplified example of #45222 -// -// Temporarily disabled in #68835 to fix a soundness hole. -// -// fn triangle_inc(n: u64) -> u64 { -// let mut count = 0; -// for j in 0 ..= n { -// count += j; -// } -// count -// } -// -// // COMMENTEDCHECK-LABEL: @check_triangle_inc -// #[no_mangle] -// pub fn check_triangle_inc() -> u64 { -// // COMMENTEDCHECK: ret i64 5000050000 -// triangle_inc(100000) -// } - -// Demo in #48012 - -fn foo3r(n: u64) -> u64 { - let mut count = 0; - (0..n).for_each(|_| { - (0..=n).rev().for_each(|j| { - count += j; - }) - }); - count -} - -// CHECK-LABEL: @check_foo3r -#[no_mangle] -pub fn check_foo3r() -> u64 { - // CHECK: ret i64 500050000000 - foo3r(10000) -} diff --git a/tests/codegen/issue-45466.rs b/tests/codegen/issue-45466.rs deleted file mode 100644 index c79542767..000000000 --- a/tests/codegen/issue-45466.rs +++ /dev/null @@ -1,15 +0,0 @@ -// compile-flags: -O -// ignore-debug: the debug assertions get in the way - -#![crate_type="rlib"] - -// CHECK-LABEL: @memzero -// CHECK-NOT: store -// CHECK: call void @llvm.memset -// CHECK-NOT: store -#[no_mangle] -pub fn memzero(data: &mut [u8]) { - for i in 0..data.len() { - data[i] = 0; - } -} diff --git a/tests/codegen/issue-45964-bounds-check-slice-pos.rs b/tests/codegen/issue-45964-bounds-check-slice-pos.rs deleted file mode 100644 index 1daa213fc..000000000 --- a/tests/codegen/issue-45964-bounds-check-slice-pos.rs +++ /dev/null @@ -1,39 +0,0 @@ -// This test case checks that slice::{r}position functions do not -// prevent optimizing away bounds checks - -// compile-flags: -O -// ignore-debug: the debug assertions get in the way - -#![crate_type="rlib"] - -// CHECK-LABEL: @test -#[no_mangle] -pub fn test(y: &[u32], x: &u32, z: &u32) -> bool { - let result = match y.iter().position(|a| a == x) { - Some(p) => Ok(p), - None => Err(()), - }; - - if let Ok(p) = result { - // CHECK-NOT: panic - y[p] == *z - } else { - false - } -} - -// CHECK-LABEL: @rtest -#[no_mangle] -pub fn rtest(y: &[u32], x: &u32, z: &u32) -> bool { - let result = match y.iter().rposition(|a| a == x) { - Some(p) => Ok(p), - None => Err(()), - }; - - if let Ok(p) = result { - // CHECK-NOT: panic - y[p] == *z - } else { - false - } -} diff --git a/tests/codegen/issue-47278.rs b/tests/codegen/issue-47278.rs deleted file mode 100644 index 9076274f4..000000000 --- a/tests/codegen/issue-47278.rs +++ /dev/null @@ -1,9 +0,0 @@ -// -C no-prepopulate-passes -#![crate_type="staticlib"] - -#[repr(C)] -pub struct Foo(u64); - -// CHECK: define {{.*}} @foo( -#[no_mangle] -pub extern "C" fn foo(_: Foo) -> Foo { loop {} } diff --git a/tests/codegen/issue-47442.rs b/tests/codegen/issue-47442.rs deleted file mode 100644 index 6944336d3..000000000 --- a/tests/codegen/issue-47442.rs +++ /dev/null @@ -1,22 +0,0 @@ -// check that we don't emit unneeded `resume` cleanup blocks for every -// destructor. - -// CHECK-NOT: Unwind - -#![feature(test)] -#![crate_type="rlib"] - -extern crate test; - -struct Foo {} - -impl Drop for Foo { - fn drop(&mut self) { - test::black_box(()); - } -} - -#[no_mangle] -pub fn foo() { - let _foo = Foo {}; -} diff --git a/tests/codegen/issue-56267-2.rs b/tests/codegen/issue-56267-2.rs deleted file mode 100644 index 4dc9ebfeb..000000000 --- a/tests/codegen/issue-56267-2.rs +++ /dev/null @@ -1,18 +0,0 @@ -// compile-flags: -C no-prepopulate-passes - -#![crate_type="rlib"] - -#[allow(dead_code)] -pub struct Foo { - foo: u64, - bar: T, -} - -// The load from bar.1 should have alignment 4. Not checking -// other loads here, as the alignment will be platform-dependent. - -// CHECK: %{{.+}} = load i32, {{i32\*|ptr}} %{{.+}}, align 4 -#[no_mangle] -pub fn test(x: Foo<(i32, i32)>) -> (i32, i32) { - x.bar -} diff --git a/tests/codegen/issue-56267.rs b/tests/codegen/issue-56267.rs deleted file mode 100644 index 7bdd25779..000000000 --- a/tests/codegen/issue-56267.rs +++ /dev/null @@ -1,18 +0,0 @@ -// compile-flags: -C no-prepopulate-passes - -#![crate_type="rlib"] - -#[allow(dead_code)] -pub struct Foo { - foo: u64, - bar: T, -} - -// The store writing to bar.1 should have alignment 4. Not checking -// other stores here, as the alignment will be platform-dependent. - -// CHECK: store i32 [[TMP1:%.+]], {{i32\*|ptr}} [[TMP2:%.+]], align 4 -#[no_mangle] -pub fn test(x: (i32, i32)) -> Foo<(i32, i32)> { - Foo { foo: 0, bar: x } -} diff --git a/tests/codegen/issue-56927.rs b/tests/codegen/issue-56927.rs deleted file mode 100644 index 044d72181..000000000 --- a/tests/codegen/issue-56927.rs +++ /dev/null @@ -1,43 +0,0 @@ -// compile-flags: -C no-prepopulate-passes - -#![crate_type="rlib"] - -#[repr(align(16))] -pub struct S { - arr: [u32; 4], -} - -// CHECK-LABEL: @test1 -// CHECK: store i32 0, {{i32\*|ptr}} %{{.+}}, align 16 -// CHECK: store i32 1, {{i32\*|ptr}} %{{.+}}, align 4 -// CHECK: store i32 2, {{i32\*|ptr}} %{{.+}}, align 8 -// CHECK: store i32 3, {{i32\*|ptr}} %{{.+}}, align 4 -#[no_mangle] -pub fn test1(s: &mut S) { - s.arr[0] = 0; - s.arr[1] = 1; - s.arr[2] = 2; - s.arr[3] = 3; -} - -// CHECK-LABEL: @test2 -// CHECK: store i32 4, {{i32\*|ptr}} %{{.+}}, align 4 -#[allow(unconditional_panic)] -#[no_mangle] -pub fn test2(s: &mut S) { - s.arr[usize::MAX / 4 + 1] = 4; -} - -// CHECK-LABEL: @test3 -// CHECK: store i32 5, {{i32\*|ptr}} %{{.+}}, align 4 -#[no_mangle] -pub fn test3(s: &mut S, i: usize) { - s.arr[i] = 5; -} - -// CHECK-LABEL: @test4 -// CHECK: store i32 6, {{i32\*|ptr}} %{{.+}}, align 4 -#[no_mangle] -pub fn test4(s: &mut S) { - s.arr = [6; 4]; -} diff --git a/tests/codegen/issue-58881.rs b/tests/codegen/issue-58881.rs deleted file mode 100644 index 00f8953d9..000000000 --- a/tests/codegen/issue-58881.rs +++ /dev/null @@ -1,21 +0,0 @@ -// compile-flags: -C no-prepopulate-passes -Copt-level=0 -// -// only-x86_64 -// ignore-windows - -#![crate_type = "lib"] - -extern "C" { - fn variadic_fn(_: i32, ...); -} - -#[repr(C)] -struct Foo(u8); -#[repr(C)] -struct Bar(u64, u64, u64); - -// Ensure that emit arguments of the correct type. -pub unsafe fn test_call_variadic() { - // CHECK: call void (i32, ...) @variadic_fn(i32 0, i8 {{.*}}, {{%Bar\*|ptr}} {{.*}}) - variadic_fn(0, Foo(0), Bar(0, 0, 0)) -} diff --git a/tests/codegen/issue-59352.rs b/tests/codegen/issue-59352.rs deleted file mode 100644 index d271fe027..000000000 --- a/tests/codegen/issue-59352.rs +++ /dev/null @@ -1,18 +0,0 @@ -// This test is a mirror of mir-opt/issues/issue-59352.rs. The LLVM inliner doesn't inline -// `char::method::is_digit()` and `char::method::to_digit()`, probably because of their size. -// -// Currently, the MIR optimizer isn't capable of removing the unreachable panic in this test case. -// Once the optimizer can do that, mir-opt/issues/issue-59352.rs will need to be updated and this -// test case should be removed as it will become redundant. - -// mir-opt-level=3 enables inlining and enables LLVM to optimize away the unreachable panic call. -// compile-flags: -O -Z mir-opt-level=3 - -#![crate_type = "rlib"] - -// CHECK-LABEL: @num_to_digit -#[no_mangle] -pub fn num_to_digit(num: char) -> u32 { - // CHECK-NOT: panic - if num.is_digit(8) { num.to_digit(8).unwrap() } else { 0 } -} diff --git a/tests/codegen/issue-69101-bounds-check.rs b/tests/codegen/issue-69101-bounds-check.rs deleted file mode 100644 index a3aca3a29..000000000 --- a/tests/codegen/issue-69101-bounds-check.rs +++ /dev/null @@ -1,44 +0,0 @@ -// no-system-llvm -// compile-flags: -O -// ignore-debug: the debug assertions get in the way -#![crate_type = "lib"] - -// Make sure no bounds checks are emitted in the loop when upfront slicing -// ensures that the slices are big enough. -// In particular, bounds checks were not always optimized out if the upfront -// check was for a greater len than the loop requires. -// (i.e. `already_sliced_no_bounds_check` was not always optimized even when -// `already_sliced_no_bounds_check_exact` was) -// CHECK-LABEL: @already_sliced_no_bounds_check -#[no_mangle] -pub fn already_sliced_no_bounds_check(a: &[u8], b: &[u8], c: &mut [u8]) { - // CHECK: slice_end_index_len_fail - // CHECK-NOT: panic_bounds_check - let _ = (&a[..2048], &b[..2048], &mut c[..2048]); - for i in 0..1024 { - c[i] = a[i] ^ b[i]; - } -} - -// CHECK-LABEL: @already_sliced_no_bounds_check_exact -#[no_mangle] -pub fn already_sliced_no_bounds_check_exact(a: &[u8], b: &[u8], c: &mut [u8]) { - // CHECK: slice_end_index_len_fail - // CHECK-NOT: panic_bounds_check - let _ = (&a[..1024], &b[..1024], &mut c[..1024]); - for i in 0..1024 { - c[i] = a[i] ^ b[i]; - } -} - -// Make sure we're checking for the right thing: there can be a panic if the slice is too small. -// CHECK-LABEL: @already_sliced_bounds_check -#[no_mangle] -pub fn already_sliced_bounds_check(a: &[u8], b: &[u8], c: &mut [u8]) { - // CHECK: slice_end_index_len_fail - // CHECK: panic_bounds_check - let _ = (&a[..1023], &b[..2048], &mut c[..2048]); - for i in 0..1024 { - c[i] = a[i] ^ b[i]; - } -} diff --git a/tests/codegen/issue-73031.rs b/tests/codegen/issue-73031.rs deleted file mode 100644 index a09c4bcfb..000000000 --- a/tests/codegen/issue-73031.rs +++ /dev/null @@ -1,26 +0,0 @@ -// compile-flags: -O -#![crate_type = "lib"] - -// Test that LLVM can eliminate the unreachable `All::None` branch. - -pub enum All { - None, - Foo, - Bar, -} - -// CHECK-LABEL: @issue_73031 -#[no_mangle] -pub fn issue_73031(a: &mut All, q: i32) -> i32 { - *a = if q == 5 { - All::Foo - } else { - All::Bar - }; - match *a { - // CHECK-NOT: panic - All::None => panic!(), - All::Foo => 1, - All::Bar => 2, - } -} diff --git a/tests/codegen/issue-73338-effecient-cmp.rs b/tests/codegen/issue-73338-effecient-cmp.rs deleted file mode 100644 index 85c2bbfd0..000000000 --- a/tests/codegen/issue-73338-effecient-cmp.rs +++ /dev/null @@ -1,39 +0,0 @@ -// This test checks that comparison operation -// generated by #[derive(PartialOrd)] -// doesn't contain jumps for C enums - -// compile-flags: -Copt-level=3 - -#![crate_type="lib"] - -#[repr(u32)] -#[derive(Copy, Clone, Eq, PartialEq, PartialOrd)] -pub enum Foo { - Zero, - One, - Two, -} - -#[no_mangle] -pub fn compare_less(a: Foo, b: Foo)->bool{ - // CHECK-NOT: br {{.*}} - a < b -} - -#[no_mangle] -pub fn compare_le(a: Foo, b: Foo)->bool{ - // CHECK-NOT: br {{.*}} - a <= b -} - -#[no_mangle] -pub fn compare_ge(a: Foo, b: Foo)->bool{ - // CHECK-NOT: br {{.*}} - a >= b -} - -#[no_mangle] -pub fn compare_greater(a: Foo, b: Foo)->bool{ - // CHECK-NOT: br {{.*}} - a > b -} diff --git a/tests/codegen/issue-73396-bounds-check-after-position.rs b/tests/codegen/issue-73396-bounds-check-after-position.rs deleted file mode 100644 index 8d07a67a1..000000000 --- a/tests/codegen/issue-73396-bounds-check-after-position.rs +++ /dev/null @@ -1,77 +0,0 @@ -// compile-flags: -O -// ignore-debug: the debug assertions get in the way -#![crate_type = "lib"] - -// Make sure no bounds checks are emitted when slicing or indexing -// with an index from `position()` or `rposition()`. - -// CHECK-LABEL: @position_slice_to_no_bounds_check -#[no_mangle] -pub fn position_slice_to_no_bounds_check(s: &[u8]) -> &[u8] { - // CHECK-NOT: panic - // CHECK-NOT: slice_index_len_fail - if let Some(idx) = s.iter().position(|b| *b == b'\\') { - &s[..idx] - } else { - s - } -} - -// CHECK-LABEL: @position_slice_from_no_bounds_check -#[no_mangle] -pub fn position_slice_from_no_bounds_check(s: &[u8]) -> &[u8] { - // CHECK-NOT: panic - // CHECK-NOT: slice_index_len_fail - if let Some(idx) = s.iter().position(|b| *b == b'\\') { - &s[idx..] - } else { - s - } -} - -// CHECK-LABEL: @position_index_no_bounds_check -#[no_mangle] -pub fn position_index_no_bounds_check(s: &[u8]) -> u8 { - // CHECK-NOT: panic - // CHECK-NOT: slice_index_len_fail - if let Some(idx) = s.iter().position(|b| *b == b'\\') { - s[idx] - } else { - 42 - } -} -// CHECK-LABEL: @rposition_slice_to_no_bounds_check -#[no_mangle] -pub fn rposition_slice_to_no_bounds_check(s: &[u8]) -> &[u8] { - // CHECK-NOT: panic - // CHECK-NOT: slice_index_len_fail - if let Some(idx) = s.iter().rposition(|b| *b == b'\\') { - &s[..idx] - } else { - s - } -} - -// CHECK-LABEL: @rposition_slice_from_no_bounds_check -#[no_mangle] -pub fn rposition_slice_from_no_bounds_check(s: &[u8]) -> &[u8] { - // CHECK-NOT: panic - // CHECK-NOT: slice_index_len_fail - if let Some(idx) = s.iter().rposition(|b| *b == b'\\') { - &s[idx..] - } else { - s - } -} - -// CHECK-LABEL: @rposition_index_no_bounds_check -#[no_mangle] -pub fn rposition_index_no_bounds_check(s: &[u8]) -> u8 { - // CHECK-NOT: panic - // CHECK-NOT: slice_index_len_fail - if let Some(idx) = s.iter().rposition(|b| *b == b'\\') { - s[idx] - } else { - 42 - } -} diff --git a/tests/codegen/issue-73827-bounds-check-index-in-subexpr.rs b/tests/codegen/issue-73827-bounds-check-index-in-subexpr.rs deleted file mode 100644 index 1ad05906e..000000000 --- a/tests/codegen/issue-73827-bounds-check-index-in-subexpr.rs +++ /dev/null @@ -1,17 +0,0 @@ -// This test checks that bounds checks are elided when -// index is part of a (x | y) < C style condition - -// compile-flags: -O - -#![crate_type = "lib"] - -// CHECK-LABEL: @get -#[no_mangle] -pub fn get(array: &[u8; 8], x: usize, y: usize) -> u8 { - if x > 7 || y > 7 { - 0 - } else { - // CHECK-NOT: panic_bounds_check - array[y] - } -} diff --git a/tests/codegen/issue-75525-bounds-checks.rs b/tests/codegen/issue-75525-bounds-checks.rs deleted file mode 100644 index 2d363d8f7..000000000 --- a/tests/codegen/issue-75525-bounds-checks.rs +++ /dev/null @@ -1,26 +0,0 @@ -// Regression test for #75525, verifies that no bounds checks are generated. - -// compile-flags: -O - -#![crate_type = "lib"] - -// CHECK-LABEL: @f0 -// CHECK-NOT: panic -#[no_mangle] -pub fn f0(idx: usize, buf: &[u8; 10]) -> u8 { - if idx < 8 { buf[idx + 1] } else { 0 } -} - -// CHECK-LABEL: @f1 -// CHECK-NOT: panic -#[no_mangle] -pub fn f1(idx: usize, buf: &[u8; 10]) -> u8 { - if idx > 5 && idx < 8 { buf[idx - 1] } else { 0 } -} - -// CHECK-LABEL: @f2 -// CHECK-NOT: panic -#[no_mangle] -pub fn f2(idx: usize, buf: &[u8; 10]) -> u8 { - if idx > 5 && idx < 8 { buf[idx] } else { 0 } -} diff --git a/tests/codegen/issue-75546.rs b/tests/codegen/issue-75546.rs deleted file mode 100644 index 470a9e040..000000000 --- a/tests/codegen/issue-75546.rs +++ /dev/null @@ -1,15 +0,0 @@ -// compile-flags: -O -#![crate_type = "lib"] - -// Test that LLVM can eliminate the impossible `i == 0` check. - -// CHECK-LABEL: @issue_75546 -#[no_mangle] -pub fn issue_75546() { - let mut i = 1u32; - while i < u32::MAX { - // CHECK-NOT: panic - if i == 0 { panic!(); } - i += 1; - } -} diff --git a/tests/codegen/issue-75659.rs b/tests/codegen/issue-75659.rs deleted file mode 100644 index 9394868c0..000000000 --- a/tests/codegen/issue-75659.rs +++ /dev/null @@ -1,63 +0,0 @@ -// This test checks that the call to memchr/slice_contains is optimized away -// when searching in small slices. - -// compile-flags: -O -Zinline-mir=false -// only-x86_64 - -#![crate_type = "lib"] - -// CHECK-LABEL: @foo1 -#[no_mangle] -pub fn foo1(x: u8, data: &[u8; 1]) -> bool { - // CHECK-NOT: memchr - // CHECK-NOT: slice_contains - data.contains(&x) -} - -// CHECK-LABEL: @foo2 -#[no_mangle] -pub fn foo2(x: u8, data: &[u8; 2]) -> bool { - // CHECK-NOT: memchr - // CHECK-NOT: slice_contains - data.contains(&x) -} - -// CHECK-LABEL: @foo3 -#[no_mangle] -pub fn foo3(x: u8, data: &[u8; 3]) -> bool { - // CHECK-NOT: memchr - // CHECK-NOT: slice_contains - data.contains(&x) -} - -// CHECK-LABEL: @foo4 -#[no_mangle] -pub fn foo4(x: u8, data: &[u8; 4]) -> bool { - // CHECK-NOT: memchr - // CHECK-NOT: slice_contains - data.contains(&x) -} - -// CHECK-LABEL: @foo8 -#[no_mangle] -pub fn foo8(x: u8, data: &[u8; 8]) -> bool { - // CHECK-NOT: memchr - // CHECK-NOT: slice_contains - data.contains(&x) -} - -// CHECK-LABEL: @foo8_i8 -#[no_mangle] -pub fn foo8_i8(x: i8, data: &[i8; 8]) -> bool { - // CHECK-NOT: memchr - // CHECK-NOT: slice_contains - !data.contains(&x) -} - -// Check that the general case isn't inlined -// CHECK-LABEL: @foo80 -#[no_mangle] -pub fn foo80(x: u8, data: &[u8; 80]) -> bool { - // CHECK: call core::slice::memchr - data.contains(&x) -} diff --git a/tests/codegen/issue-77812.rs b/tests/codegen/issue-77812.rs deleted file mode 100644 index 4cc824145..000000000 --- a/tests/codegen/issue-77812.rs +++ /dev/null @@ -1,32 +0,0 @@ -// compile-flags: -O -#![crate_type = "lib"] - -// Test that LLVM can eliminate the unreachable `Variant::Zero` branch. - -#[derive(Copy, Clone, Eq, PartialEq)] -pub enum Variant { - Zero, - One, - Two, -} - -extern { - fn exf1(); - fn exf2(); -} - -pub static mut GLOBAL: Variant = Variant::Zero; - -// CHECK-LABEL: @issue_77812 -#[no_mangle] -pub unsafe fn issue_77812() { - let g = GLOBAL; - if g != Variant::Zero { - match g { - Variant::One => exf1(), - Variant::Two => exf2(), - // CHECK-NOT: panic - Variant::Zero => panic!(), - } - } -} diff --git a/tests/codegen/issue-81408-dllimport-thinlto-windows.rs b/tests/codegen/issue-81408-dllimport-thinlto-windows.rs deleted file mode 100644 index 0b6ab4f7e..000000000 --- a/tests/codegen/issue-81408-dllimport-thinlto-windows.rs +++ /dev/null @@ -1,15 +0,0 @@ -// compile-flags: -O -C lto=thin -C prefer-dynamic=no -// only-windows -// aux-build:static_dllimport_aux.rs - -// Test that on Windows, when performing ThinLTO, we do not mark cross-crate static items with -// dllimport because lld does not fix the symbol names for us. - -extern crate static_dllimport_aux; - -// CHECK-LABEL: @{{.+}}CROSS_CRATE_STATIC_ITEM{{.+}} = -// CHECK-SAME: external local_unnamed_addr global %"{{.+}}AtomicPtr - -pub fn main() { - static_dllimport_aux::memrchr(); -} diff --git a/tests/codegen/issue-84268.rs b/tests/codegen/issue-84268.rs deleted file mode 100644 index 7ca195447..000000000 --- a/tests/codegen/issue-84268.rs +++ /dev/null @@ -1,23 +0,0 @@ -// compile-flags: -O --crate-type=rlib -#![feature(platform_intrinsics, repr_simd)] - -extern "platform-intrinsic" { - fn simd_fabs(x: T) -> T; - fn simd_eq(x: T, y: T) -> U; -} - -#[repr(simd)] -pub struct V([f32; 4]); - -#[repr(simd)] -pub struct M([i32; 4]); - -#[no_mangle] -// CHECK-LABEL: @is_infinite -pub fn is_infinite(v: V) -> M { - // CHECK: fabs - // CHECK: cmp oeq - unsafe { - simd_eq(simd_fabs(v), V([f32::INFINITY; 4])) - } -} diff --git a/tests/codegen/issue-85872-multiple-reverse.rs b/tests/codegen/issue-85872-multiple-reverse.rs deleted file mode 100644 index 591a1aca7..000000000 --- a/tests/codegen/issue-85872-multiple-reverse.rs +++ /dev/null @@ -1,20 +0,0 @@ -// min-llvm-version: 15.0.0 -// compile-flags: -O - -#![crate_type = "lib"] - -#[no_mangle] -pub fn u16_be_to_arch(mut data: [u8; 2]) -> [u8; 2] { - // CHECK-LABEL: @u16_be_to_arch - // CHECK: @llvm.bswap.i16 - data.reverse(); - data -} - -#[no_mangle] -pub fn u32_be_to_arch(mut data: [u8; 4]) -> [u8; 4] { - // CHECK-LABEL: @u32_be_to_arch - // CHECK: @llvm.bswap.i32 - data.reverse(); - data -} diff --git a/tests/codegen/issue-86106.rs b/tests/codegen/issue-86106.rs deleted file mode 100644 index 9ccbcb24f..000000000 --- a/tests/codegen/issue-86106.rs +++ /dev/null @@ -1,62 +0,0 @@ -// min-llvm-version: 15.0 -// compile-flags: -C opt-level=3 -Z merge-functions=disabled - -// The below two functions ensure that both `String::new()` and `"".to_string()` -// produce the identical code. - -#![crate_type = "lib"] - -// CHECK-LABEL: define void @string_new -#[no_mangle] -pub fn string_new() -> String { - // CHECK-NOT: load i8 - // CHECK: store i{{32|64}} - // CHECK-NEXT: getelementptr - // CHECK-NEXT: store ptr - // CHECK-NEXT: getelementptr - // CHECK-NEXT: store i{{32|64}} - // CHECK-NEXT: ret void - String::new() -} - -// CHECK-LABEL: define void @empty_to_string -#[no_mangle] -pub fn empty_to_string() -> String { - // CHECK-NOT: load i8 - // CHECK: store i{{32|64}} - // CHECK-NEXT: getelementptr - // CHECK-NEXT: store ptr - // CHECK-NEXT: getelementptr - // CHECK-NEXT: store i{{32|64}} - // CHECK-NEXT: ret void - "".to_string() -} - -// The below two functions ensure that both `vec![]` and `vec![].clone()` -// produce the identical code. - -// CHECK-LABEL: @empty_vec -#[no_mangle] -pub fn empty_vec() -> Vec { - // CHECK: store i{{32|64}} - // CHECK-NOT: load i8 - // CHECK-NEXT: getelementptr - // CHECK-NEXT: store ptr - // CHECK-NEXT: getelementptr - // CHECK-NEXT: store i{{32|64}} - // CHECK-NEXT: ret void - vec![] -} - -// CHECK-LABEL: @empty_vec_clone -#[no_mangle] -pub fn empty_vec_clone() -> Vec { - // CHECK: store i{{32|64}} - // CHECK-NOT: load i8 - // CHECK-NEXT: getelementptr - // CHECK-NEXT: store ptr - // CHECK-NEXT: getelementptr - // CHECK-NEXT: store i{{32|64}} - // CHECK-NEXT: ret void - vec![].clone() -} diff --git a/tests/codegen/issue-96274.rs b/tests/codegen/issue-96274.rs deleted file mode 100644 index 28bfcce0d..000000000 --- a/tests/codegen/issue-96274.rs +++ /dev/null @@ -1,17 +0,0 @@ -// min-llvm-version: 15.0 -// compile-flags: -O - -#![crate_type = "lib"] -#![feature(inline_const)] - -use std::mem::MaybeUninit; - -pub fn maybe_uninit() -> [MaybeUninit; 3000] { - // CHECK-NOT: memset - [MaybeUninit::uninit(); 3000] -} - -pub fn maybe_uninit_const() -> [MaybeUninit; 8192] { - // CHECK-NOT: memset - [const { MaybeUninit::uninit() }; 8192] -} diff --git a/tests/codegen/issue-96497-slice-size-nowrap.rs b/tests/codegen/issue-96497-slice-size-nowrap.rs deleted file mode 100644 index 0413ed6b2..000000000 --- a/tests/codegen/issue-96497-slice-size-nowrap.rs +++ /dev/null @@ -1,29 +0,0 @@ -// This test case checks that LLVM is aware that computing the size of a slice cannot wrap. -// The possibility of wrapping results in an additional branch when dropping boxed slices -// in some situations, see https://github.com/rust-lang/rust/issues/96497#issuecomment-1112865218 - -// compile-flags: -O -// min-llvm-version: 15.0 - -#![crate_type="lib"] - -// CHECK-LABEL: @simple_size_of_nowrap -#[no_mangle] -pub fn simple_size_of_nowrap(x: &[u32]) -> usize { - // Make sure the shift used to compute the size has a nowrap flag. - - // CHECK: [[A:%.*]] = shl nsw {{.*}}, 2 - // CHECK-NEXT: ret {{.*}} [[A]] - core::mem::size_of_val(x) -} - -// CHECK-LABEL: @drop_write -#[no_mangle] -pub fn drop_write(mut x: Box<[u32]>) { - // Check that this write is optimized out. - // This depends on the size calculation not wrapping, - // since otherwise LLVM can't tell that the memory is always deallocated if the slice len > 0. - - // CHECK-NOT: store i32 42 - x[1] = 42; -} diff --git a/tests/codegen/issue-98156-const-arg-temp-lifetime.rs b/tests/codegen/issue-98156-const-arg-temp-lifetime.rs deleted file mode 100644 index 12ace5fff..000000000 --- a/tests/codegen/issue-98156-const-arg-temp-lifetime.rs +++ /dev/null @@ -1,27 +0,0 @@ -// This test checks that temporaries for indirectly-passed arguments get lifetime markers. - -// compile-flags: -O -C no-prepopulate-passes -Zmir-opt-level=0 - -#![crate_type = "lib"] - -extern "Rust" { - fn f(x: [u8; 1024]); -} - -const A: [u8; 1024] = [0; 1024]; - -// CHECK-LABEL: @const_arg_indirect -#[no_mangle] -pub unsafe fn const_arg_indirect() { - // Ensure that the live ranges for the two argument temporaries don't overlap. - - // CHECK: call void @llvm.lifetime.start - // CHECK: call void @f - // CHECK: call void @llvm.lifetime.end - // CHECK: call void @llvm.lifetime.start - // CHECK: call void @f - // CHECK: call void @llvm.lifetime.end - - f(A); - f(A); -} diff --git a/tests/codegen/issue-98294-get-mut-copy-from-slice-opt.rs b/tests/codegen/issue-98294-get-mut-copy-from-slice-opt.rs deleted file mode 100644 index 7da29cd79..000000000 --- a/tests/codegen/issue-98294-get-mut-copy-from-slice-opt.rs +++ /dev/null @@ -1,19 +0,0 @@ -// min-llvm-version: 15.0.0 -// ignore-debug: The debug assertions get in the way -// compile-flags: -O - -#![crate_type = "lib"] - -// There should be no calls to panic / len_mismatch_fail. - -#[no_mangle] -pub fn test(a: &mut [u8], offset: usize, bytes: &[u8]) { - // CHECK-LABEL: @test( - // CHECK-NOT: call - // CHECK: call void @llvm.memcpy - // CHECK-NOT: call - // CHECK: } - if let Some(dst) = a.get_mut(offset..offset + bytes.len()) { - dst.copy_from_slice(bytes); - } -} diff --git a/tests/codegen/issues/auxiliary/static_dllimport_aux.rs b/tests/codegen/issues/auxiliary/static_dllimport_aux.rs new file mode 100644 index 000000000..afb0dc42f --- /dev/null +++ b/tests/codegen/issues/auxiliary/static_dllimport_aux.rs @@ -0,0 +1,13 @@ +use std::sync::atomic::{AtomicPtr, Ordering}; + +#[inline(always)] +pub fn memrchr() { + fn detect() {} + + static CROSS_CRATE_STATIC_ITEM: AtomicPtr<()> = AtomicPtr::new(detect as *mut ()); + + unsafe { + let fun = CROSS_CRATE_STATIC_ITEM.load(Ordering::SeqCst); + std::mem::transmute::<*mut (), fn()>(fun)() + } +} diff --git a/tests/codegen/issues/issue-101048.rs b/tests/codegen/issues/issue-101048.rs new file mode 100644 index 000000000..efa4db93e --- /dev/null +++ b/tests/codegen/issues/issue-101048.rs @@ -0,0 +1,13 @@ +// compile-flags: -O +// min-llvm-version: 16 + +#![crate_type = "lib"] + +#[no_mangle] +pub fn all_zero(data: &[u64]) -> bool { + // CHECK-LABEL: @all_zero( + // CHECK: [[PHI:%.*]] = phi i1 + // CHECK-NOT: phi i8 + // CHECK-NOT: zext + data.iter().copied().fold(true, |acc, x| acc & (x == 0)) +} diff --git a/tests/codegen/issues/issue-101082.rs b/tests/codegen/issues/issue-101082.rs new file mode 100644 index 000000000..2cbe99942 --- /dev/null +++ b/tests/codegen/issues/issue-101082.rs @@ -0,0 +1,17 @@ +// compile-flags: -O +// min-llvm-version: 16 +// ignore-debug: the debug assertions get in the way + +#![crate_type = "lib"] + +#[no_mangle] +pub fn test() -> usize { + // CHECK-LABEL: @test( + // CHECK: ret {{i64|i32}} 165 + let values = [23, 16, 54, 3, 60, 9]; + let mut acc = 0; + for item in values { + acc += item; + } + acc +} diff --git a/tests/codegen/issues/issue-101814.rs b/tests/codegen/issues/issue-101814.rs new file mode 100644 index 000000000..13796352c --- /dev/null +++ b/tests/codegen/issues/issue-101814.rs @@ -0,0 +1,20 @@ +// compile-flags: -O +// min-llvm-version: 16 +// ignore-debug: the debug assertions get in the way + +#![crate_type = "lib"] + +#[no_mangle] +pub fn test(a: [i32; 10]) -> i32 { + // CHECK-LABEL: @test( + // CHECK: [[L1:%.+]] = load i32 + // CHECK: [[L2:%.+]] = load i32 + // CHECK: [[R:%.+]] = add i32 [[L1]], [[L2]] + // CHECK: ret i32 [[R]] + let mut sum = 0; + for v in a.iter().skip(8) { + sum += v; + } + + sum +} diff --git a/tests/codegen/issues/issue-103132.rs b/tests/codegen/issues/issue-103132.rs new file mode 100644 index 000000000..cc87d7cd2 --- /dev/null +++ b/tests/codegen/issues/issue-103132.rs @@ -0,0 +1,16 @@ +// compile-flags: -O -C overflow-checks +// min-llvm-version: 16 + +#![crate_type = "lib"] + +#[no_mangle] +pub fn test(arr: &[u8], weight: u32) { + // CHECK-LABEL: @test( + // CHECK-NOT: panic + let weight = weight.min(256 * 256 * 256); + + for x in arr { + assert!(weight <= 256 * 256 * 256); + let result = *x as u32 * weight; + } +} diff --git a/tests/codegen/issues/issue-103285-ptr-addr-overflow-check.rs b/tests/codegen/issues/issue-103285-ptr-addr-overflow-check.rs new file mode 100644 index 000000000..a3499babe --- /dev/null +++ b/tests/codegen/issues/issue-103285-ptr-addr-overflow-check.rs @@ -0,0 +1,16 @@ +// compile-flags: -O -C debug-assertions=yes + +#![crate_type = "lib"] +#![feature(strict_provenance)] + +#[no_mangle] +pub fn test(src: *const u8, dst: *const u8) -> usize { + // CHECK-LABEL: @test( + // CHECK-NOT: panic + let src_usize = src.addr(); + let dst_usize = dst.addr(); + if src_usize > dst_usize { + return src_usize - dst_usize; + } + return 0; +} diff --git a/tests/codegen/issues/issue-103327.rs b/tests/codegen/issues/issue-103327.rs new file mode 100644 index 000000000..cee00facc --- /dev/null +++ b/tests/codegen/issues/issue-103327.rs @@ -0,0 +1,18 @@ +// compile-flags: -O +// min-llvm-version: 16 + +#![crate_type = "lib"] + +#[no_mangle] +pub fn test(a: i32, b: i32) -> bool { + // CHECK-LABEL: @test( + // CHECK: ret i1 true + let c1 = (a >= 0) && (a <= 10); + let c2 = (b >= 0) && (b <= 20); + + if c1 & c2 { + a + 100 != b + } else { + true + } +} diff --git a/tests/codegen/issues/issue-103840.rs b/tests/codegen/issues/issue-103840.rs new file mode 100644 index 000000000..f19d7031b --- /dev/null +++ b/tests/codegen/issues/issue-103840.rs @@ -0,0 +1,9 @@ +// compile-flags: -O +#![crate_type = "lib"] + +pub fn foo(t: &mut Vec) { + // CHECK-NOT: __rust_dealloc + let mut taken = std::mem::take(t); + taken.pop(); + *t = taken; +} diff --git a/tests/codegen/issues/issue-105386-ub-in-debuginfo.rs b/tests/codegen/issues/issue-105386-ub-in-debuginfo.rs new file mode 100644 index 000000000..d54ac9e33 --- /dev/null +++ b/tests/codegen/issues/issue-105386-ub-in-debuginfo.rs @@ -0,0 +1,22 @@ +// compile-flags: --crate-type=lib -O -Cdebuginfo=2 -Cno-prepopulate-passes +// min-llvm-version: 15.0 # this test uses opaque pointer notation +#![feature(stmt_expr_attributes)] + +pub struct S([usize; 8]); + +#[no_mangle] +pub fn outer_function(x: S, y: S) -> usize { + (#[inline(always)]|| { + let _z = x; + y.0[0] + })() +} + +// Check that we do not attempt to load from the spilled arg before it is assigned to +// when generating debuginfo. +// CHECK-LABEL: @outer_function +// CHECK: [[spill:%.*]] = alloca %"[closure@{{.*.rs}}:9:23: 9:25]" +// CHECK-NOT: [[ptr_tmp:%.*]] = getelementptr inbounds %"[closure@{{.*.rs}}:9:23: 9:25]", ptr [[spill]] +// CHECK-NOT: [[load:%.*]] = load ptr, ptr +// CHECK: call void @llvm.lifetime.start{{.*}}({{.*}}, ptr [[spill]]) +// CHECK: call void @llvm.memcpy{{.*}}(ptr {{align .*}} [[spill]], ptr {{align .*}} %x diff --git a/tests/codegen/issues/issue-106369.rs b/tests/codegen/issues/issue-106369.rs new file mode 100644 index 000000000..3fe7be4f1 --- /dev/null +++ b/tests/codegen/issues/issue-106369.rs @@ -0,0 +1,15 @@ +// compile-flags: -O +// ignore-debug (the extra assertions get in the way) + +#![crate_type = "lib"] + +// From + +// CHECK-LABEL: @issue_106369( +#[no_mangle] +pub unsafe fn issue_106369(ptr: *const &i32) -> bool { + // CHECK-NOT: icmp + // CHECK: ret i1 true + // CHECK-NOT: icmp + Some(std::ptr::read(ptr)).is_some() +} diff --git a/tests/codegen/issues/issue-13018.rs b/tests/codegen/issues/issue-13018.rs new file mode 100644 index 000000000..b70ea1f48 --- /dev/null +++ b/tests/codegen/issues/issue-13018.rs @@ -0,0 +1,11 @@ +// compile-flags: -O + +// A drop([...].clone()) sequence on an Rc should be a no-op +// In particular, no call to __rust_dealloc should be emitted +#![crate_type = "lib"] +use std::rc::Rc; + +pub fn foo(t: &Rc>) { +// CHECK-NOT: __rust_dealloc + drop(t.clone()); +} diff --git a/tests/codegen/issues/issue-15953.rs b/tests/codegen/issues/issue-15953.rs new file mode 100644 index 000000000..28d284289 --- /dev/null +++ b/tests/codegen/issues/issue-15953.rs @@ -0,0 +1,29 @@ +// Test that llvm generates `memcpy` for moving a value +// inside a function and moving an argument. + +struct Foo { + x: Vec, +} + +#[inline(never)] +#[no_mangle] +// CHECK: memcpy +fn interior(x: Vec) -> Vec { + let Foo { x } = Foo { x: x }; + x +} + +#[inline(never)] +#[no_mangle] +// CHECK: memcpy +fn exterior(x: Vec) -> Vec { + x +} + +fn main() { + let x = interior(Vec::new()); + println!("{:?}", x); + + let x = exterior(Vec::new()); + println!("{:?}", x); +} diff --git a/tests/codegen/issues/issue-27130.rs b/tests/codegen/issues/issue-27130.rs new file mode 100644 index 000000000..e5ee94e1f --- /dev/null +++ b/tests/codegen/issues/issue-27130.rs @@ -0,0 +1,21 @@ +// compile-flags: -O + +#![crate_type = "lib"] + +// CHECK-LABEL: @trim_in_place +#[no_mangle] +pub fn trim_in_place(a: &mut &[u8]) { + while a.first() == Some(&42) { + // CHECK-NOT: slice_index_order_fail + *a = &a[1..]; + } +} + +// CHECK-LABEL: @trim_in_place2 +#[no_mangle] +pub fn trim_in_place2(a: &mut &[u8]) { + while let Some(&42) = a.first() { + // CHECK-NOT: slice_index_order_fail + *a = &a[2..]; + } +} diff --git a/tests/codegen/issues/issue-32031.rs b/tests/codegen/issues/issue-32031.rs new file mode 100644 index 000000000..abef92c19 --- /dev/null +++ b/tests/codegen/issues/issue-32031.rs @@ -0,0 +1,23 @@ +// compile-flags: -C no-prepopulate-passes -Copt-level=0 + +#![crate_type = "lib"] + +#[no_mangle] +pub struct F32(f32); + +// CHECK: define{{.*}}float @add_newtype_f32(float %a, float %b) +#[inline(never)] +#[no_mangle] +pub fn add_newtype_f32(a: F32, b: F32) -> F32 { + F32(a.0 + b.0) +} + +#[no_mangle] +pub struct F64(f64); + +// CHECK: define{{.*}}double @add_newtype_f64(double %a, double %b) +#[inline(never)] +#[no_mangle] +pub fn add_newtype_f64(a: F64, b: F64) -> F64 { + F64(a.0 + b.0) +} diff --git a/tests/codegen/issues/issue-32364.rs b/tests/codegen/issues/issue-32364.rs new file mode 100644 index 000000000..85493a4bb --- /dev/null +++ b/tests/codegen/issues/issue-32364.rs @@ -0,0 +1,18 @@ +// Test that `extern "stdcall"` is properly translated. + +// only-x86 + +// compile-flags: -C no-prepopulate-passes + +struct Foo; + +impl Foo { +// CHECK: define internal x86_stdcallcc void @{{.*}}foo{{.*}}() + #[inline(never)] + pub extern "stdcall" fn foo() { + } +} + +fn main() { + Foo::foo::(); +} diff --git a/tests/codegen/issues/issue-34634.rs b/tests/codegen/issues/issue-34634.rs new file mode 100644 index 000000000..f53fa240c --- /dev/null +++ b/tests/codegen/issues/issue-34634.rs @@ -0,0 +1,16 @@ +// Test that `wrapping_div` only checks divisor once. +// This test checks that there is only a single compare against -1 and -1 is not present as a +// switch case (the second check present until rustc 1.12). +// This test also verifies that a single panic call is generated (for the division by zero case). + +// compile-flags: -O +#![crate_type = "lib"] + +// CHECK-LABEL: @f +#[no_mangle] +pub fn f(x: i32, y: i32) -> i32 { + // CHECK-COUNT-1: icmp eq i32 %y, -1 + // CHECK-COUNT-1: panic + // CHECK-NOT: i32 -1, label + x.wrapping_div(y) +} diff --git a/tests/codegen/issues/issue-34947-pow-i32.rs b/tests/codegen/issues/issue-34947-pow-i32.rs new file mode 100644 index 000000000..653da8e8b --- /dev/null +++ b/tests/codegen/issues/issue-34947-pow-i32.rs @@ -0,0 +1,13 @@ +// compile-flags: -O + +#![crate_type = "lib"] + +// CHECK-LABEL: @issue_34947 +#[no_mangle] +pub fn issue_34947(x: i32) -> i32 { + // CHECK: mul + // CHECK-NEXT: mul + // CHECK-NEXT: mul + // CHECK-NEXT: ret + x.pow(5) +} diff --git a/tests/codegen/issues/issue-37945.rs b/tests/codegen/issues/issue-37945.rs new file mode 100644 index 000000000..4f386d335 --- /dev/null +++ b/tests/codegen/issues/issue-37945.rs @@ -0,0 +1,35 @@ +// compile-flags: -O -Zmerge-functions=disabled +// ignore-32bit LLVM has a bug with them +// ignore-debug + +// Check that LLVM understands that `Iter` pointer is not null. Issue #37945. + +#![crate_type = "lib"] + +use std::slice::Iter; + +#[no_mangle] +pub fn is_empty_1(xs: Iter) -> bool { +// CHECK-LABEL: @is_empty_1( +// CHECK-NEXT: start: +// CHECK-NEXT: [[A:%.*]] = icmp ne {{i32\*|ptr}} {{%xs.0|%xs.1}}, null +// CHECK-NEXT: tail call void @llvm.assume(i1 [[A]]) +// The order between %xs.0 and %xs.1 on the next line doesn't matter +// and different LLVM versions produce different order. +// CHECK-NEXT: [[B:%.*]] = icmp eq {{i32\*|ptr}} {{%xs.0, %xs.1|%xs.1, %xs.0}} +// CHECK-NEXT: ret i1 [[B:%.*]] + {xs}.next().is_none() +} + +#[no_mangle] +pub fn is_empty_2(xs: Iter) -> bool { +// CHECK-LABEL: @is_empty_2 +// CHECK-NEXT: start: +// CHECK-NEXT: [[C:%.*]] = icmp ne {{i32\*|ptr}} {{%xs.0|%xs.1}}, null +// CHECK-NEXT: tail call void @llvm.assume(i1 [[C]]) +// The order between %xs.0 and %xs.1 on the next line doesn't matter +// and different LLVM versions produce different order. +// CHECK-NEXT: [[D:%.*]] = icmp eq {{i32\*|ptr}} {{%xs.0, %xs.1|%xs.1, %xs.0}} +// CHECK-NEXT: ret i1 [[D:%.*]] + xs.map(|&x| x).next().is_none() +} diff --git a/tests/codegen/issues/issue-44056-macos-tls-align.rs b/tests/codegen/issues/issue-44056-macos-tls-align.rs new file mode 100644 index 000000000..1a3923f1b --- /dev/null +++ b/tests/codegen/issues/issue-44056-macos-tls-align.rs @@ -0,0 +1,28 @@ +// +// only-macos +// no-system-llvm +// compile-flags: -O + +#![crate_type = "rlib"] +#![feature(thread_local)] + +// local_unnamed_addr does not appear when std is built with debug assertions. +// CHECK: @STATIC_VAR_1 = thread_local {{(local_unnamed_addr )?}}global <{ [32 x i8] }> zeroinitializer, section "__DATA,__thread_bss", align 4 +#[no_mangle] +#[thread_local] +static mut STATIC_VAR_1: [u32; 8] = [0; 8]; + +// CHECK: @STATIC_VAR_2 = thread_local {{(local_unnamed_addr )?}}global <{ [32 x i8] }> <{{[^>]*}}>, section "__DATA,__thread_data", align 4 +#[no_mangle] +#[thread_local] +static mut STATIC_VAR_2: [u32; 8] = [4; 8]; + +#[no_mangle] +pub unsafe fn f(x: &mut [u32; 8]) { + std::mem::swap(x, &mut STATIC_VAR_1) +} + +#[no_mangle] +pub unsafe fn g(x: &mut [u32; 8]) { + std::mem::swap(x, &mut STATIC_VAR_2) +} diff --git a/tests/codegen/issues/issue-45222.rs b/tests/codegen/issues/issue-45222.rs new file mode 100644 index 000000000..e9b05e648 --- /dev/null +++ b/tests/codegen/issues/issue-45222.rs @@ -0,0 +1,63 @@ +// compile-flags: -O +// ignore-debug: the debug assertions get in the way + +#![crate_type = "lib"] + +// verify that LLVM recognizes a loop involving 0..=n and will const-fold it. + +// Example from original issue #45222 + +fn foo2(n: u64) -> u64 { + let mut count = 0; + for _ in 0..n { + for j in (0..=n).rev() { + count += j; + } + } + count +} + +// CHECK-LABEL: @check_foo2 +#[no_mangle] +pub fn check_foo2() -> u64 { + // CHECK: ret i64 500005000000000 + foo2(100000) +} + +// Simplified example of #45222 +// +// Temporarily disabled in #68835 to fix a soundness hole. +// +// fn triangle_inc(n: u64) -> u64 { +// let mut count = 0; +// for j in 0 ..= n { +// count += j; +// } +// count +// } +// +// // COMMENTEDCHECK-LABEL: @check_triangle_inc +// #[no_mangle] +// pub fn check_triangle_inc() -> u64 { +// // COMMENTEDCHECK: ret i64 5000050000 +// triangle_inc(100000) +// } + +// Demo in #48012 + +fn foo3r(n: u64) -> u64 { + let mut count = 0; + (0..n).for_each(|_| { + (0..=n).rev().for_each(|j| { + count += j; + }) + }); + count +} + +// CHECK-LABEL: @check_foo3r +#[no_mangle] +pub fn check_foo3r() -> u64 { + // CHECK: ret i64 500050000000 + foo3r(10000) +} diff --git a/tests/codegen/issues/issue-45466.rs b/tests/codegen/issues/issue-45466.rs new file mode 100644 index 000000000..c79542767 --- /dev/null +++ b/tests/codegen/issues/issue-45466.rs @@ -0,0 +1,15 @@ +// compile-flags: -O +// ignore-debug: the debug assertions get in the way + +#![crate_type="rlib"] + +// CHECK-LABEL: @memzero +// CHECK-NOT: store +// CHECK: call void @llvm.memset +// CHECK-NOT: store +#[no_mangle] +pub fn memzero(data: &mut [u8]) { + for i in 0..data.len() { + data[i] = 0; + } +} diff --git a/tests/codegen/issues/issue-45964-bounds-check-slice-pos.rs b/tests/codegen/issues/issue-45964-bounds-check-slice-pos.rs new file mode 100644 index 000000000..1daa213fc --- /dev/null +++ b/tests/codegen/issues/issue-45964-bounds-check-slice-pos.rs @@ -0,0 +1,39 @@ +// This test case checks that slice::{r}position functions do not +// prevent optimizing away bounds checks + +// compile-flags: -O +// ignore-debug: the debug assertions get in the way + +#![crate_type="rlib"] + +// CHECK-LABEL: @test +#[no_mangle] +pub fn test(y: &[u32], x: &u32, z: &u32) -> bool { + let result = match y.iter().position(|a| a == x) { + Some(p) => Ok(p), + None => Err(()), + }; + + if let Ok(p) = result { + // CHECK-NOT: panic + y[p] == *z + } else { + false + } +} + +// CHECK-LABEL: @rtest +#[no_mangle] +pub fn rtest(y: &[u32], x: &u32, z: &u32) -> bool { + let result = match y.iter().rposition(|a| a == x) { + Some(p) => Ok(p), + None => Err(()), + }; + + if let Ok(p) = result { + // CHECK-NOT: panic + y[p] == *z + } else { + false + } +} diff --git a/tests/codegen/issues/issue-47278.rs b/tests/codegen/issues/issue-47278.rs new file mode 100644 index 000000000..9076274f4 --- /dev/null +++ b/tests/codegen/issues/issue-47278.rs @@ -0,0 +1,9 @@ +// -C no-prepopulate-passes +#![crate_type="staticlib"] + +#[repr(C)] +pub struct Foo(u64); + +// CHECK: define {{.*}} @foo( +#[no_mangle] +pub extern "C" fn foo(_: Foo) -> Foo { loop {} } diff --git a/tests/codegen/issues/issue-47442.rs b/tests/codegen/issues/issue-47442.rs new file mode 100644 index 000000000..6944336d3 --- /dev/null +++ b/tests/codegen/issues/issue-47442.rs @@ -0,0 +1,22 @@ +// check that we don't emit unneeded `resume` cleanup blocks for every +// destructor. + +// CHECK-NOT: Unwind + +#![feature(test)] +#![crate_type="rlib"] + +extern crate test; + +struct Foo {} + +impl Drop for Foo { + fn drop(&mut self) { + test::black_box(()); + } +} + +#[no_mangle] +pub fn foo() { + let _foo = Foo {}; +} diff --git a/tests/codegen/issues/issue-56267-2.rs b/tests/codegen/issues/issue-56267-2.rs new file mode 100644 index 000000000..4dc9ebfeb --- /dev/null +++ b/tests/codegen/issues/issue-56267-2.rs @@ -0,0 +1,18 @@ +// compile-flags: -C no-prepopulate-passes + +#![crate_type="rlib"] + +#[allow(dead_code)] +pub struct Foo { + foo: u64, + bar: T, +} + +// The load from bar.1 should have alignment 4. Not checking +// other loads here, as the alignment will be platform-dependent. + +// CHECK: %{{.+}} = load i32, {{i32\*|ptr}} %{{.+}}, align 4 +#[no_mangle] +pub fn test(x: Foo<(i32, i32)>) -> (i32, i32) { + x.bar +} diff --git a/tests/codegen/issues/issue-56267.rs b/tests/codegen/issues/issue-56267.rs new file mode 100644 index 000000000..7bdd25779 --- /dev/null +++ b/tests/codegen/issues/issue-56267.rs @@ -0,0 +1,18 @@ +// compile-flags: -C no-prepopulate-passes + +#![crate_type="rlib"] + +#[allow(dead_code)] +pub struct Foo { + foo: u64, + bar: T, +} + +// The store writing to bar.1 should have alignment 4. Not checking +// other stores here, as the alignment will be platform-dependent. + +// CHECK: store i32 [[TMP1:%.+]], {{i32\*|ptr}} [[TMP2:%.+]], align 4 +#[no_mangle] +pub fn test(x: (i32, i32)) -> Foo<(i32, i32)> { + Foo { foo: 0, bar: x } +} diff --git a/tests/codegen/issues/issue-56927.rs b/tests/codegen/issues/issue-56927.rs new file mode 100644 index 000000000..044d72181 --- /dev/null +++ b/tests/codegen/issues/issue-56927.rs @@ -0,0 +1,43 @@ +// compile-flags: -C no-prepopulate-passes + +#![crate_type="rlib"] + +#[repr(align(16))] +pub struct S { + arr: [u32; 4], +} + +// CHECK-LABEL: @test1 +// CHECK: store i32 0, {{i32\*|ptr}} %{{.+}}, align 16 +// CHECK: store i32 1, {{i32\*|ptr}} %{{.+}}, align 4 +// CHECK: store i32 2, {{i32\*|ptr}} %{{.+}}, align 8 +// CHECK: store i32 3, {{i32\*|ptr}} %{{.+}}, align 4 +#[no_mangle] +pub fn test1(s: &mut S) { + s.arr[0] = 0; + s.arr[1] = 1; + s.arr[2] = 2; + s.arr[3] = 3; +} + +// CHECK-LABEL: @test2 +// CHECK: store i32 4, {{i32\*|ptr}} %{{.+}}, align 4 +#[allow(unconditional_panic)] +#[no_mangle] +pub fn test2(s: &mut S) { + s.arr[usize::MAX / 4 + 1] = 4; +} + +// CHECK-LABEL: @test3 +// CHECK: store i32 5, {{i32\*|ptr}} %{{.+}}, align 4 +#[no_mangle] +pub fn test3(s: &mut S, i: usize) { + s.arr[i] = 5; +} + +// CHECK-LABEL: @test4 +// CHECK: store i32 6, {{i32\*|ptr}} %{{.+}}, align 4 +#[no_mangle] +pub fn test4(s: &mut S) { + s.arr = [6; 4]; +} diff --git a/tests/codegen/issues/issue-58881.rs b/tests/codegen/issues/issue-58881.rs new file mode 100644 index 000000000..00f8953d9 --- /dev/null +++ b/tests/codegen/issues/issue-58881.rs @@ -0,0 +1,21 @@ +// compile-flags: -C no-prepopulate-passes -Copt-level=0 +// +// only-x86_64 +// ignore-windows + +#![crate_type = "lib"] + +extern "C" { + fn variadic_fn(_: i32, ...); +} + +#[repr(C)] +struct Foo(u8); +#[repr(C)] +struct Bar(u64, u64, u64); + +// Ensure that emit arguments of the correct type. +pub unsafe fn test_call_variadic() { + // CHECK: call void (i32, ...) @variadic_fn(i32 0, i8 {{.*}}, {{%Bar\*|ptr}} {{.*}}) + variadic_fn(0, Foo(0), Bar(0, 0, 0)) +} diff --git a/tests/codegen/issues/issue-59352.rs b/tests/codegen/issues/issue-59352.rs new file mode 100644 index 000000000..d271fe027 --- /dev/null +++ b/tests/codegen/issues/issue-59352.rs @@ -0,0 +1,18 @@ +// This test is a mirror of mir-opt/issues/issue-59352.rs. The LLVM inliner doesn't inline +// `char::method::is_digit()` and `char::method::to_digit()`, probably because of their size. +// +// Currently, the MIR optimizer isn't capable of removing the unreachable panic in this test case. +// Once the optimizer can do that, mir-opt/issues/issue-59352.rs will need to be updated and this +// test case should be removed as it will become redundant. + +// mir-opt-level=3 enables inlining and enables LLVM to optimize away the unreachable panic call. +// compile-flags: -O -Z mir-opt-level=3 + +#![crate_type = "rlib"] + +// CHECK-LABEL: @num_to_digit +#[no_mangle] +pub fn num_to_digit(num: char) -> u32 { + // CHECK-NOT: panic + if num.is_digit(8) { num.to_digit(8).unwrap() } else { 0 } +} diff --git a/tests/codegen/issues/issue-69101-bounds-check.rs b/tests/codegen/issues/issue-69101-bounds-check.rs new file mode 100644 index 000000000..a3aca3a29 --- /dev/null +++ b/tests/codegen/issues/issue-69101-bounds-check.rs @@ -0,0 +1,44 @@ +// no-system-llvm +// compile-flags: -O +// ignore-debug: the debug assertions get in the way +#![crate_type = "lib"] + +// Make sure no bounds checks are emitted in the loop when upfront slicing +// ensures that the slices are big enough. +// In particular, bounds checks were not always optimized out if the upfront +// check was for a greater len than the loop requires. +// (i.e. `already_sliced_no_bounds_check` was not always optimized even when +// `already_sliced_no_bounds_check_exact` was) +// CHECK-LABEL: @already_sliced_no_bounds_check +#[no_mangle] +pub fn already_sliced_no_bounds_check(a: &[u8], b: &[u8], c: &mut [u8]) { + // CHECK: slice_end_index_len_fail + // CHECK-NOT: panic_bounds_check + let _ = (&a[..2048], &b[..2048], &mut c[..2048]); + for i in 0..1024 { + c[i] = a[i] ^ b[i]; + } +} + +// CHECK-LABEL: @already_sliced_no_bounds_check_exact +#[no_mangle] +pub fn already_sliced_no_bounds_check_exact(a: &[u8], b: &[u8], c: &mut [u8]) { + // CHECK: slice_end_index_len_fail + // CHECK-NOT: panic_bounds_check + let _ = (&a[..1024], &b[..1024], &mut c[..1024]); + for i in 0..1024 { + c[i] = a[i] ^ b[i]; + } +} + +// Make sure we're checking for the right thing: there can be a panic if the slice is too small. +// CHECK-LABEL: @already_sliced_bounds_check +#[no_mangle] +pub fn already_sliced_bounds_check(a: &[u8], b: &[u8], c: &mut [u8]) { + // CHECK: slice_end_index_len_fail + // CHECK: panic_bounds_check + let _ = (&a[..1023], &b[..2048], &mut c[..2048]); + for i in 0..1024 { + c[i] = a[i] ^ b[i]; + } +} diff --git a/tests/codegen/issues/issue-73031.rs b/tests/codegen/issues/issue-73031.rs new file mode 100644 index 000000000..a09c4bcfb --- /dev/null +++ b/tests/codegen/issues/issue-73031.rs @@ -0,0 +1,26 @@ +// compile-flags: -O +#![crate_type = "lib"] + +// Test that LLVM can eliminate the unreachable `All::None` branch. + +pub enum All { + None, + Foo, + Bar, +} + +// CHECK-LABEL: @issue_73031 +#[no_mangle] +pub fn issue_73031(a: &mut All, q: i32) -> i32 { + *a = if q == 5 { + All::Foo + } else { + All::Bar + }; + match *a { + // CHECK-NOT: panic + All::None => panic!(), + All::Foo => 1, + All::Bar => 2, + } +} diff --git a/tests/codegen/issues/issue-73258.rs b/tests/codegen/issues/issue-73258.rs new file mode 100644 index 000000000..0134f929b --- /dev/null +++ b/tests/codegen/issues/issue-73258.rs @@ -0,0 +1,38 @@ +// compile-flags: -O +// ignore-debug (the extra assertions get in the way) + +#![crate_type = "lib"] + +// Adapted from + +#[derive(Clone, Copy)] +#[repr(u8)] +pub enum Foo { + A, B, C, D, +} + +// CHECK-LABEL: @issue_73258( +#[no_mangle] +pub unsafe fn issue_73258(ptr: *const Foo) -> Foo { + // CHECK-NOT: icmp + // CHECK-NOT: call + // CHECK-NOT: br + // CHECK-NOT: select + + // CHECK: %[[R:.+]] = load i8 + // CHECK-SAME: !range ! + + // CHECK-NOT: icmp + // CHECK-NOT: call + // CHECK-NOT: br + // CHECK-NOT: select + + // CHECK: ret i8 %[[R]] + + // CHECK-NOT: icmp + // CHECK-NOT: call + // CHECK-NOT: br + // CHECK-NOT: select + let k: Option = Some(ptr.read()); + return k.unwrap(); +} diff --git a/tests/codegen/issues/issue-73338-effecient-cmp.rs b/tests/codegen/issues/issue-73338-effecient-cmp.rs new file mode 100644 index 000000000..85c2bbfd0 --- /dev/null +++ b/tests/codegen/issues/issue-73338-effecient-cmp.rs @@ -0,0 +1,39 @@ +// This test checks that comparison operation +// generated by #[derive(PartialOrd)] +// doesn't contain jumps for C enums + +// compile-flags: -Copt-level=3 + +#![crate_type="lib"] + +#[repr(u32)] +#[derive(Copy, Clone, Eq, PartialEq, PartialOrd)] +pub enum Foo { + Zero, + One, + Two, +} + +#[no_mangle] +pub fn compare_less(a: Foo, b: Foo)->bool{ + // CHECK-NOT: br {{.*}} + a < b +} + +#[no_mangle] +pub fn compare_le(a: Foo, b: Foo)->bool{ + // CHECK-NOT: br {{.*}} + a <= b +} + +#[no_mangle] +pub fn compare_ge(a: Foo, b: Foo)->bool{ + // CHECK-NOT: br {{.*}} + a >= b +} + +#[no_mangle] +pub fn compare_greater(a: Foo, b: Foo)->bool{ + // CHECK-NOT: br {{.*}} + a > b +} diff --git a/tests/codegen/issues/issue-73396-bounds-check-after-position.rs b/tests/codegen/issues/issue-73396-bounds-check-after-position.rs new file mode 100644 index 000000000..8d07a67a1 --- /dev/null +++ b/tests/codegen/issues/issue-73396-bounds-check-after-position.rs @@ -0,0 +1,77 @@ +// compile-flags: -O +// ignore-debug: the debug assertions get in the way +#![crate_type = "lib"] + +// Make sure no bounds checks are emitted when slicing or indexing +// with an index from `position()` or `rposition()`. + +// CHECK-LABEL: @position_slice_to_no_bounds_check +#[no_mangle] +pub fn position_slice_to_no_bounds_check(s: &[u8]) -> &[u8] { + // CHECK-NOT: panic + // CHECK-NOT: slice_index_len_fail + if let Some(idx) = s.iter().position(|b| *b == b'\\') { + &s[..idx] + } else { + s + } +} + +// CHECK-LABEL: @position_slice_from_no_bounds_check +#[no_mangle] +pub fn position_slice_from_no_bounds_check(s: &[u8]) -> &[u8] { + // CHECK-NOT: panic + // CHECK-NOT: slice_index_len_fail + if let Some(idx) = s.iter().position(|b| *b == b'\\') { + &s[idx..] + } else { + s + } +} + +// CHECK-LABEL: @position_index_no_bounds_check +#[no_mangle] +pub fn position_index_no_bounds_check(s: &[u8]) -> u8 { + // CHECK-NOT: panic + // CHECK-NOT: slice_index_len_fail + if let Some(idx) = s.iter().position(|b| *b == b'\\') { + s[idx] + } else { + 42 + } +} +// CHECK-LABEL: @rposition_slice_to_no_bounds_check +#[no_mangle] +pub fn rposition_slice_to_no_bounds_check(s: &[u8]) -> &[u8] { + // CHECK-NOT: panic + // CHECK-NOT: slice_index_len_fail + if let Some(idx) = s.iter().rposition(|b| *b == b'\\') { + &s[..idx] + } else { + s + } +} + +// CHECK-LABEL: @rposition_slice_from_no_bounds_check +#[no_mangle] +pub fn rposition_slice_from_no_bounds_check(s: &[u8]) -> &[u8] { + // CHECK-NOT: panic + // CHECK-NOT: slice_index_len_fail + if let Some(idx) = s.iter().rposition(|b| *b == b'\\') { + &s[idx..] + } else { + s + } +} + +// CHECK-LABEL: @rposition_index_no_bounds_check +#[no_mangle] +pub fn rposition_index_no_bounds_check(s: &[u8]) -> u8 { + // CHECK-NOT: panic + // CHECK-NOT: slice_index_len_fail + if let Some(idx) = s.iter().rposition(|b| *b == b'\\') { + s[idx] + } else { + 42 + } +} diff --git a/tests/codegen/issues/issue-73827-bounds-check-index-in-subexpr.rs b/tests/codegen/issues/issue-73827-bounds-check-index-in-subexpr.rs new file mode 100644 index 000000000..1ad05906e --- /dev/null +++ b/tests/codegen/issues/issue-73827-bounds-check-index-in-subexpr.rs @@ -0,0 +1,17 @@ +// This test checks that bounds checks are elided when +// index is part of a (x | y) < C style condition + +// compile-flags: -O + +#![crate_type = "lib"] + +// CHECK-LABEL: @get +#[no_mangle] +pub fn get(array: &[u8; 8], x: usize, y: usize) -> u8 { + if x > 7 || y > 7 { + 0 + } else { + // CHECK-NOT: panic_bounds_check + array[y] + } +} diff --git a/tests/codegen/issues/issue-75525-bounds-checks.rs b/tests/codegen/issues/issue-75525-bounds-checks.rs new file mode 100644 index 000000000..2d363d8f7 --- /dev/null +++ b/tests/codegen/issues/issue-75525-bounds-checks.rs @@ -0,0 +1,26 @@ +// Regression test for #75525, verifies that no bounds checks are generated. + +// compile-flags: -O + +#![crate_type = "lib"] + +// CHECK-LABEL: @f0 +// CHECK-NOT: panic +#[no_mangle] +pub fn f0(idx: usize, buf: &[u8; 10]) -> u8 { + if idx < 8 { buf[idx + 1] } else { 0 } +} + +// CHECK-LABEL: @f1 +// CHECK-NOT: panic +#[no_mangle] +pub fn f1(idx: usize, buf: &[u8; 10]) -> u8 { + if idx > 5 && idx < 8 { buf[idx - 1] } else { 0 } +} + +// CHECK-LABEL: @f2 +// CHECK-NOT: panic +#[no_mangle] +pub fn f2(idx: usize, buf: &[u8; 10]) -> u8 { + if idx > 5 && idx < 8 { buf[idx] } else { 0 } +} diff --git a/tests/codegen/issues/issue-75546.rs b/tests/codegen/issues/issue-75546.rs new file mode 100644 index 000000000..470a9e040 --- /dev/null +++ b/tests/codegen/issues/issue-75546.rs @@ -0,0 +1,15 @@ +// compile-flags: -O +#![crate_type = "lib"] + +// Test that LLVM can eliminate the impossible `i == 0` check. + +// CHECK-LABEL: @issue_75546 +#[no_mangle] +pub fn issue_75546() { + let mut i = 1u32; + while i < u32::MAX { + // CHECK-NOT: panic + if i == 0 { panic!(); } + i += 1; + } +} diff --git a/tests/codegen/issues/issue-75659.rs b/tests/codegen/issues/issue-75659.rs new file mode 100644 index 000000000..9394868c0 --- /dev/null +++ b/tests/codegen/issues/issue-75659.rs @@ -0,0 +1,63 @@ +// This test checks that the call to memchr/slice_contains is optimized away +// when searching in small slices. + +// compile-flags: -O -Zinline-mir=false +// only-x86_64 + +#![crate_type = "lib"] + +// CHECK-LABEL: @foo1 +#[no_mangle] +pub fn foo1(x: u8, data: &[u8; 1]) -> bool { + // CHECK-NOT: memchr + // CHECK-NOT: slice_contains + data.contains(&x) +} + +// CHECK-LABEL: @foo2 +#[no_mangle] +pub fn foo2(x: u8, data: &[u8; 2]) -> bool { + // CHECK-NOT: memchr + // CHECK-NOT: slice_contains + data.contains(&x) +} + +// CHECK-LABEL: @foo3 +#[no_mangle] +pub fn foo3(x: u8, data: &[u8; 3]) -> bool { + // CHECK-NOT: memchr + // CHECK-NOT: slice_contains + data.contains(&x) +} + +// CHECK-LABEL: @foo4 +#[no_mangle] +pub fn foo4(x: u8, data: &[u8; 4]) -> bool { + // CHECK-NOT: memchr + // CHECK-NOT: slice_contains + data.contains(&x) +} + +// CHECK-LABEL: @foo8 +#[no_mangle] +pub fn foo8(x: u8, data: &[u8; 8]) -> bool { + // CHECK-NOT: memchr + // CHECK-NOT: slice_contains + data.contains(&x) +} + +// CHECK-LABEL: @foo8_i8 +#[no_mangle] +pub fn foo8_i8(x: i8, data: &[i8; 8]) -> bool { + // CHECK-NOT: memchr + // CHECK-NOT: slice_contains + !data.contains(&x) +} + +// Check that the general case isn't inlined +// CHECK-LABEL: @foo80 +#[no_mangle] +pub fn foo80(x: u8, data: &[u8; 80]) -> bool { + // CHECK: call core::slice::memchr + data.contains(&x) +} diff --git a/tests/codegen/issues/issue-75978.rs b/tests/codegen/issues/issue-75978.rs new file mode 100644 index 000000000..f335e92c3 --- /dev/null +++ b/tests/codegen/issues/issue-75978.rs @@ -0,0 +1,19 @@ +// compile-flags: -O +// min-llvm-version: 16 + +#![crate_type = "lib"] + +#[no_mangle] +pub fn test() -> u32 { + // CHECK-LABEL: @test( + // CHECK: ret i32 13 + let s = [1, 2, 3, 4, 5, 6, 7]; + + let mut iter = s.iter(); + let mut sum = 0; + while let Some(_) = iter.next() { + sum += iter.next().map_or(1, |&x| x) + } + + sum +} diff --git a/tests/codegen/issues/issue-77812.rs b/tests/codegen/issues/issue-77812.rs new file mode 100644 index 000000000..4cc824145 --- /dev/null +++ b/tests/codegen/issues/issue-77812.rs @@ -0,0 +1,32 @@ +// compile-flags: -O +#![crate_type = "lib"] + +// Test that LLVM can eliminate the unreachable `Variant::Zero` branch. + +#[derive(Copy, Clone, Eq, PartialEq)] +pub enum Variant { + Zero, + One, + Two, +} + +extern { + fn exf1(); + fn exf2(); +} + +pub static mut GLOBAL: Variant = Variant::Zero; + +// CHECK-LABEL: @issue_77812 +#[no_mangle] +pub unsafe fn issue_77812() { + let g = GLOBAL; + if g != Variant::Zero { + match g { + Variant::One => exf1(), + Variant::Two => exf2(), + // CHECK-NOT: panic + Variant::Zero => panic!(), + } + } +} diff --git a/tests/codegen/issues/issue-81408-dllimport-thinlto-windows.rs b/tests/codegen/issues/issue-81408-dllimport-thinlto-windows.rs new file mode 100644 index 000000000..0b6ab4f7e --- /dev/null +++ b/tests/codegen/issues/issue-81408-dllimport-thinlto-windows.rs @@ -0,0 +1,15 @@ +// compile-flags: -O -C lto=thin -C prefer-dynamic=no +// only-windows +// aux-build:static_dllimport_aux.rs + +// Test that on Windows, when performing ThinLTO, we do not mark cross-crate static items with +// dllimport because lld does not fix the symbol names for us. + +extern crate static_dllimport_aux; + +// CHECK-LABEL: @{{.+}}CROSS_CRATE_STATIC_ITEM{{.+}} = +// CHECK-SAME: external local_unnamed_addr global %"{{.+}}AtomicPtr + +pub fn main() { + static_dllimport_aux::memrchr(); +} diff --git a/tests/codegen/issues/issue-84268.rs b/tests/codegen/issues/issue-84268.rs new file mode 100644 index 000000000..7ca195447 --- /dev/null +++ b/tests/codegen/issues/issue-84268.rs @@ -0,0 +1,23 @@ +// compile-flags: -O --crate-type=rlib +#![feature(platform_intrinsics, repr_simd)] + +extern "platform-intrinsic" { + fn simd_fabs(x: T) -> T; + fn simd_eq(x: T, y: T) -> U; +} + +#[repr(simd)] +pub struct V([f32; 4]); + +#[repr(simd)] +pub struct M([i32; 4]); + +#[no_mangle] +// CHECK-LABEL: @is_infinite +pub fn is_infinite(v: V) -> M { + // CHECK: fabs + // CHECK: cmp oeq + unsafe { + simd_eq(simd_fabs(v), V([f32::INFINITY; 4])) + } +} diff --git a/tests/codegen/issues/issue-85872-multiple-reverse.rs b/tests/codegen/issues/issue-85872-multiple-reverse.rs new file mode 100644 index 000000000..591a1aca7 --- /dev/null +++ b/tests/codegen/issues/issue-85872-multiple-reverse.rs @@ -0,0 +1,20 @@ +// min-llvm-version: 15.0.0 +// compile-flags: -O + +#![crate_type = "lib"] + +#[no_mangle] +pub fn u16_be_to_arch(mut data: [u8; 2]) -> [u8; 2] { + // CHECK-LABEL: @u16_be_to_arch + // CHECK: @llvm.bswap.i16 + data.reverse(); + data +} + +#[no_mangle] +pub fn u32_be_to_arch(mut data: [u8; 4]) -> [u8; 4] { + // CHECK-LABEL: @u32_be_to_arch + // CHECK: @llvm.bswap.i32 + data.reverse(); + data +} diff --git a/tests/codegen/issues/issue-86106.rs b/tests/codegen/issues/issue-86106.rs new file mode 100644 index 000000000..9ccbcb24f --- /dev/null +++ b/tests/codegen/issues/issue-86106.rs @@ -0,0 +1,62 @@ +// min-llvm-version: 15.0 +// compile-flags: -C opt-level=3 -Z merge-functions=disabled + +// The below two functions ensure that both `String::new()` and `"".to_string()` +// produce the identical code. + +#![crate_type = "lib"] + +// CHECK-LABEL: define void @string_new +#[no_mangle] +pub fn string_new() -> String { + // CHECK-NOT: load i8 + // CHECK: store i{{32|64}} + // CHECK-NEXT: getelementptr + // CHECK-NEXT: store ptr + // CHECK-NEXT: getelementptr + // CHECK-NEXT: store i{{32|64}} + // CHECK-NEXT: ret void + String::new() +} + +// CHECK-LABEL: define void @empty_to_string +#[no_mangle] +pub fn empty_to_string() -> String { + // CHECK-NOT: load i8 + // CHECK: store i{{32|64}} + // CHECK-NEXT: getelementptr + // CHECK-NEXT: store ptr + // CHECK-NEXT: getelementptr + // CHECK-NEXT: store i{{32|64}} + // CHECK-NEXT: ret void + "".to_string() +} + +// The below two functions ensure that both `vec![]` and `vec![].clone()` +// produce the identical code. + +// CHECK-LABEL: @empty_vec +#[no_mangle] +pub fn empty_vec() -> Vec { + // CHECK: store i{{32|64}} + // CHECK-NOT: load i8 + // CHECK-NEXT: getelementptr + // CHECK-NEXT: store ptr + // CHECK-NEXT: getelementptr + // CHECK-NEXT: store i{{32|64}} + // CHECK-NEXT: ret void + vec![] +} + +// CHECK-LABEL: @empty_vec_clone +#[no_mangle] +pub fn empty_vec_clone() -> Vec { + // CHECK: store i{{32|64}} + // CHECK-NOT: load i8 + // CHECK-NEXT: getelementptr + // CHECK-NEXT: store ptr + // CHECK-NEXT: getelementptr + // CHECK-NEXT: store i{{32|64}} + // CHECK-NEXT: ret void + vec![].clone() +} diff --git a/tests/codegen/issues/issue-96274.rs b/tests/codegen/issues/issue-96274.rs new file mode 100644 index 000000000..28bfcce0d --- /dev/null +++ b/tests/codegen/issues/issue-96274.rs @@ -0,0 +1,17 @@ +// min-llvm-version: 15.0 +// compile-flags: -O + +#![crate_type = "lib"] +#![feature(inline_const)] + +use std::mem::MaybeUninit; + +pub fn maybe_uninit() -> [MaybeUninit; 3000] { + // CHECK-NOT: memset + [MaybeUninit::uninit(); 3000] +} + +pub fn maybe_uninit_const() -> [MaybeUninit; 8192] { + // CHECK-NOT: memset + [const { MaybeUninit::uninit() }; 8192] +} diff --git a/tests/codegen/issues/issue-96497-slice-size-nowrap.rs b/tests/codegen/issues/issue-96497-slice-size-nowrap.rs new file mode 100644 index 000000000..0413ed6b2 --- /dev/null +++ b/tests/codegen/issues/issue-96497-slice-size-nowrap.rs @@ -0,0 +1,29 @@ +// This test case checks that LLVM is aware that computing the size of a slice cannot wrap. +// The possibility of wrapping results in an additional branch when dropping boxed slices +// in some situations, see https://github.com/rust-lang/rust/issues/96497#issuecomment-1112865218 + +// compile-flags: -O +// min-llvm-version: 15.0 + +#![crate_type="lib"] + +// CHECK-LABEL: @simple_size_of_nowrap +#[no_mangle] +pub fn simple_size_of_nowrap(x: &[u32]) -> usize { + // Make sure the shift used to compute the size has a nowrap flag. + + // CHECK: [[A:%.*]] = shl nsw {{.*}}, 2 + // CHECK-NEXT: ret {{.*}} [[A]] + core::mem::size_of_val(x) +} + +// CHECK-LABEL: @drop_write +#[no_mangle] +pub fn drop_write(mut x: Box<[u32]>) { + // Check that this write is optimized out. + // This depends on the size calculation not wrapping, + // since otherwise LLVM can't tell that the memory is always deallocated if the slice len > 0. + + // CHECK-NOT: store i32 42 + x[1] = 42; +} diff --git a/tests/codegen/issues/issue-98156-const-arg-temp-lifetime.rs b/tests/codegen/issues/issue-98156-const-arg-temp-lifetime.rs new file mode 100644 index 000000000..12ace5fff --- /dev/null +++ b/tests/codegen/issues/issue-98156-const-arg-temp-lifetime.rs @@ -0,0 +1,27 @@ +// This test checks that temporaries for indirectly-passed arguments get lifetime markers. + +// compile-flags: -O -C no-prepopulate-passes -Zmir-opt-level=0 + +#![crate_type = "lib"] + +extern "Rust" { + fn f(x: [u8; 1024]); +} + +const A: [u8; 1024] = [0; 1024]; + +// CHECK-LABEL: @const_arg_indirect +#[no_mangle] +pub unsafe fn const_arg_indirect() { + // Ensure that the live ranges for the two argument temporaries don't overlap. + + // CHECK: call void @llvm.lifetime.start + // CHECK: call void @f + // CHECK: call void @llvm.lifetime.end + // CHECK: call void @llvm.lifetime.start + // CHECK: call void @f + // CHECK: call void @llvm.lifetime.end + + f(A); + f(A); +} diff --git a/tests/codegen/issues/issue-98294-get-mut-copy-from-slice-opt.rs b/tests/codegen/issues/issue-98294-get-mut-copy-from-slice-opt.rs new file mode 100644 index 000000000..7da29cd79 --- /dev/null +++ b/tests/codegen/issues/issue-98294-get-mut-copy-from-slice-opt.rs @@ -0,0 +1,19 @@ +// min-llvm-version: 15.0.0 +// ignore-debug: The debug assertions get in the way +// compile-flags: -O + +#![crate_type = "lib"] + +// There should be no calls to panic / len_mismatch_fail. + +#[no_mangle] +pub fn test(a: &mut [u8], offset: usize, bytes: &[u8]) { + // CHECK-LABEL: @test( + // CHECK-NOT: call + // CHECK: call void @llvm.memcpy + // CHECK-NOT: call + // CHECK: } + if let Some(dst) = a.get_mut(offset..offset + bytes.len()) { + dst.copy_from_slice(bytes); + } +} diff --git a/tests/codegen/issues/issue-99960.rs b/tests/codegen/issues/issue-99960.rs new file mode 100644 index 000000000..e9c9367fa --- /dev/null +++ b/tests/codegen/issues/issue-99960.rs @@ -0,0 +1,15 @@ +// compile-flags: -O +// min-llvm-version: 16 + +#![crate_type = "lib"] + +#[no_mangle] +pub fn test(dividend: i64, divisor: i64) -> Option { + // CHECK-LABEL: @test( + // CHECK-NOT: panic + if dividend > i64::min_value() && divisor != 0 { + Some(dividend / divisor) + } else { + None + } +} diff --git a/tests/codegen/mem-replace-big-type.rs b/tests/codegen/mem-replace-big-type.rs new file mode 100644 index 000000000..f6898e2f7 --- /dev/null +++ b/tests/codegen/mem-replace-big-type.rs @@ -0,0 +1,36 @@ +// This test ensures that `mem::replace::` only ever calls `@llvm.memcpy` +// with `size_of::()` as the size, and never goes through any wrapper that +// may e.g. multiply `size_of::()` with a variable "count" (which is only +// known to be `1` after inlining). + +// compile-flags: -C no-prepopulate-passes -Zinline-mir=no +// ignore-debug: the debug assertions get in the way + +#![crate_type = "lib"] + +#[repr(C, align(8))] +pub struct Big([u64; 7]); +pub fn replace_big(dst: &mut Big, src: Big) -> Big { + // Before the `read_via_copy` intrinsic, this emitted six `memcpy`s. + std::mem::replace(dst, src) +} + +// NOTE(eddyb) the `CHECK-NOT`s ensure that the only calls of `@llvm.memcpy` in +// the entire output, are the direct calls we want, from `ptr::replace`. + +// CHECK-NOT: call void @llvm.memcpy + +// For a large type, we expect exactly three `memcpy`s +// CHECK-LABEL: define internal void @{{.+}}mem{{.+}}replace{{.+}}sret(%Big) + // CHECK-NOT: alloca + // CHECK: alloca %Big + // CHECK-NOT: alloca + // CHECK-NOT: call void @llvm.memcpy + // CHECK: call void @llvm.memcpy.{{.+}}({{i8\*|ptr}} align 8 %{{.*}}, {{i8\*|ptr}} align 8 %{{.*}}, i{{.*}} 56, i1 false) + // CHECK-NOT: call void @llvm.memcpy + // CHECK: call void @llvm.memcpy.{{.+}}({{i8\*|ptr}} align 8 %{{.*}}, {{i8\*|ptr}} align 8 %{{.*}}, i{{.*}} 56, i1 false) + // CHECK-NOT: call void @llvm.memcpy + // CHECK: call void @llvm.memcpy.{{.+}}({{i8\*|ptr}} align 8 %{{.*}}, {{i8\*|ptr}} align 8 %{{.*}}, i{{.*}} 56, i1 false) + // CHECK-NOT: call void @llvm.memcpy + +// CHECK-NOT: call void @llvm.memcpy diff --git a/tests/codegen/mem-replace-direct-memcpy.rs b/tests/codegen/mem-replace-direct-memcpy.rs index e8bbf0e1b..83babab4f 100644 --- a/tests/codegen/mem-replace-direct-memcpy.rs +++ b/tests/codegen/mem-replace-direct-memcpy.rs @@ -13,12 +13,21 @@ pub fn replace_byte(dst: &mut u8, src: u8) -> u8 { } // NOTE(eddyb) the `CHECK-NOT`s ensure that the only calls of `@llvm.memcpy` in -// the entire output, are the two direct calls we want, from `ptr::replace`. +// the entire output, are the direct calls we want, from `ptr::replace`. // CHECK-NOT: call void @llvm.memcpy -// CHECK: ; core::mem::replace -// CHECK-NOT: call void @llvm.memcpy -// CHECK: call void @llvm.memcpy.{{.+}}({{i8\*|ptr}} align 1 %{{.*}}, {{i8\*|ptr}} align 1 %{{.*}}, i{{.*}} 1, i1 false) -// CHECK-NOT: call void @llvm.memcpy -// CHECK: call void @llvm.memcpy.{{.+}}({{i8\*|ptr}} align 1 %{{.*}}, {{i8\*|ptr}} align 1 %{{.*}}, i{{.*}} 1, i1 false) + +// For a small type, we expect one each of `load`/`store`/`memcpy` instead +// CHECK-LABEL: define internal noundef i8 @{{.+}}mem{{.+}}replace + // CHECK-NOT: alloca + // CHECK: alloca i8 + // CHECK-NOT: alloca + // CHECK-NOT: call void @llvm.memcpy + // CHECK: load i8 + // CHECK-NOT: call void @llvm.memcpy + // CHECK: store i8 + // CHECK-NOT: call void @llvm.memcpy + // CHECK: call void @llvm.memcpy.{{.+}}({{i8\*|ptr}} align 1 %{{.*}}, {{i8\*|ptr}} align 1 %{{.*}}, i{{.*}} 1, i1 false) + // CHECK-NOT: call void @llvm.memcpy + // CHECK-NOT: call void @llvm.memcpy diff --git a/tests/codegen/optimize-attr-1.rs b/tests/codegen/optimize-attr-1.rs index 22abe06e7..1d1f0a386 100644 --- a/tests/codegen/optimize-attr-1.rs +++ b/tests/codegen/optimize-attr-1.rs @@ -8,7 +8,6 @@ // CHECK-LABEL: define{{.*}}i32 @nothing // CHECK-SAME: [[NOTHING_ATTRS:#[0-9]+]] -// NO-OPT: ret i32 4 // SIZE-OPT: ret i32 4 // SPEEC-OPT: ret i32 4 #[no_mangle] @@ -18,7 +17,6 @@ pub fn nothing() -> i32 { // CHECK-LABEL: define{{.*}}i32 @size // CHECK-SAME: [[SIZE_ATTRS:#[0-9]+]] -// NO-OPT: ret i32 6 // SIZE-OPT: ret i32 6 // SPEED-OPT: ret i32 6 #[optimize(size)] @@ -31,7 +29,6 @@ pub fn size() -> i32 { // NO-OPT-SAME: [[NOTHING_ATTRS]] // SPEED-OPT-SAME: [[NOTHING_ATTRS]] // SIZE-OPT-SAME: [[SPEED_ATTRS:#[0-9]+]] -// NO-OPT: ret i32 8 // SIZE-OPT: ret i32 8 // SPEED-OPT: ret i32 8 #[optimize(speed)] diff --git a/tests/codegen/option-as-slice.rs b/tests/codegen/option-as-slice.rs index d5077dbf6..d735d5583 100644 --- a/tests/codegen/option-as-slice.rs +++ b/tests/codegen/option-as-slice.rs @@ -1,4 +1,4 @@ -// compile-flags: -O +// compile-flags: -O -Z randomize-layout=no // only-x86_64 #![crate_type = "lib"] @@ -12,17 +12,25 @@ use core::option::Option; // CHECK-LABEL: @u64_opt_as_slice #[no_mangle] pub fn u64_opt_as_slice(o: &Option) -> &[u64] { - // CHECK: start: // CHECK-NOT: select - // CHECK: ret + // CHECK-NOT: br + // CHECK-NOT: switch + // CHECK-NOT: icmp o.as_slice() } // CHECK-LABEL: @nonzero_u64_opt_as_slice #[no_mangle] pub fn nonzero_u64_opt_as_slice(o: &Option) -> &[NonZeroU64] { - // CHECK: start: // CHECK-NOT: select - // CHECK: ret + // CHECK-NOT: br + // CHECK-NOT: switch + // CHECK-NOT: icmp + // CHECK: %[[NZ:.+]] = icmp ne i64 %{{.+}}, 0 + // CHECK-NEXT: zext i1 %[[NZ]] to i64 + // CHECK-NOT: select + // CHECK-NOT: br + // CHECK-NOT: switch + // CHECK-NOT: icmp o.as_slice() } diff --git a/tests/codegen/ptr-read-metadata.rs b/tests/codegen/ptr-read-metadata.rs new file mode 100644 index 000000000..e1e327266 --- /dev/null +++ b/tests/codegen/ptr-read-metadata.rs @@ -0,0 +1,96 @@ +// compile-flags: -O -Z merge-functions=disabled +// no-system-llvm +// ignore-debug (the extra assertions get in the way) + +#![crate_type = "lib"] + +// Ensure that various forms of reading pointers correctly annotate the `load`s +// with `!noundef` and `!range` metadata to enable extra optimization. + +use std::mem::MaybeUninit; + +// CHECK-LABEL: define noundef i8 @copy_byte( +#[no_mangle] +pub unsafe fn copy_byte(p: *const u8) -> u8 { + // CHECK-NOT: load + // CHECK: load i8, ptr %p, align 1 + // CHECK-SAME: !noundef ! + // CHECK-NOT: load + *p +} + +// CHECK-LABEL: define noundef i8 @read_byte( +#[no_mangle] +pub unsafe fn read_byte(p: *const u8) -> u8 { + // CHECK-NOT: load + // CHECK: load i8, ptr %p, align 1 + // CHECK-SAME: !noundef ! + // CHECK-NOT: load + p.read() +} + +// CHECK-LABEL: define i8 @read_byte_maybe_uninit( +#[no_mangle] +pub unsafe fn read_byte_maybe_uninit(p: *const MaybeUninit) -> MaybeUninit { + // CHECK-NOT: load + // CHECK: load i8, ptr %p, align 1 + // CHECK-NOT: noundef + // CHECK-NOT: load + p.read() +} + +// CHECK-LABEL: define noundef i8 @read_byte_assume_init( +#[no_mangle] +pub unsafe fn read_byte_assume_init(p: &MaybeUninit) -> u8 { + // CHECK-NOT: load + // CHECK: load i8, ptr %p, align 1 + // CHECK-SAME: !noundef ! + // CHECK-NOT: load + p.assume_init_read() +} + +// CHECK-LABEL: define noundef i32 @copy_char( +#[no_mangle] +pub unsafe fn copy_char(p: *const char) -> char { + // CHECK-NOT: load + // CHECK: load i32, ptr %p + // CHECK-SAME: !range ![[RANGE:[0-9]+]] + // CHECK-SAME: !noundef ! + // CHECK-NOT: load + *p +} + +// CHECK-LABEL: define noundef i32 @read_char( +#[no_mangle] +pub unsafe fn read_char(p: *const char) -> char { + // CHECK-NOT: load + // CHECK: load i32, ptr %p + // CHECK-SAME: !range ![[RANGE]] + // CHECK-SAME: !noundef ! + // CHECK-NOT: load + p.read() +} + +// CHECK-LABEL: define i32 @read_char_maybe_uninit( +#[no_mangle] +pub unsafe fn read_char_maybe_uninit(p: *const MaybeUninit) -> MaybeUninit { + // CHECK-NOT: load + // CHECK: load i32, ptr %p + // CHECK-NOT: range + // CHECK-NOT: noundef + // CHECK-NOT: load + p.read() +} + +// CHECK-LABEL: define noundef i32 @read_char_assume_init( +#[no_mangle] +pub unsafe fn read_char_assume_init(p: &MaybeUninit) -> char { + // CHECK-NOT: load + // CHECK: load i32, ptr %p + // CHECK-SAME: !range ![[RANGE]] + // CHECK-SAME: !noundef ! + // CHECK-NOT: load + p.assume_init_read() +} + +// CHECK: ![[RANGE]] = !{i32 0, i32 1114112} diff --git a/tests/codegen/remap_path_prefix/main.rs b/tests/codegen/remap_path_prefix/main.rs index 6c0cd6997..f1e1dd69b 100644 --- a/tests/codegen/remap_path_prefix/main.rs +++ b/tests/codegen/remap_path_prefix/main.rs @@ -12,7 +12,7 @@ mod aux_mod; include!("aux_mod.rs"); // Here we check that the expansion of the file!() macro is mapped. -// CHECK: @alloc_92a59126a55aa3c0019b6c8a007fe001 = private unnamed_addr constant <{ [34 x i8] }> <{ [34 x i8] c"/the/src/remap_path_prefix/main.rs" }> +// CHECK: @alloc_5761061597a97f66e13ef2ff92712c4b = private unnamed_addr constant <{ [34 x i8] }> <{ [34 x i8] c"/the/src/remap_path_prefix/main.rs" }> pub static FILE_PATH: &'static str = file!(); fn main() { diff --git a/tests/codegen/repr-transparent-aggregates-2.rs b/tests/codegen/repr-transparent-aggregates-2.rs index df7e88f08..e9fa5143b 100644 --- a/tests/codegen/repr-transparent-aggregates-2.rs +++ b/tests/codegen/repr-transparent-aggregates-2.rs @@ -6,7 +6,6 @@ // ignore-mips64 // ignore-powerpc // ignore-powerpc64 -// ignore-powerpc64le // ignore-riscv64 see codegen/riscv-abi // ignore-s390x // ignore-sparc diff --git a/tests/codegen/simd-intrinsic/simd-intrinsic-transmute-array.rs b/tests/codegen/simd-intrinsic/simd-intrinsic-transmute-array.rs index 7c77398df..fd488a14b 100644 --- a/tests/codegen/simd-intrinsic/simd-intrinsic-transmute-array.rs +++ b/tests/codegen/simd-intrinsic/simd-intrinsic-transmute-array.rs @@ -4,7 +4,8 @@ #![crate_type = "lib"] #![allow(non_camel_case_types)] -#![feature(repr_simd, platform_intrinsics, min_const_generics)] +#![feature(repr_simd, platform_intrinsics)] +#![feature(inline_const)] #[repr(simd)] #[derive(Copy, Clone)] @@ -18,23 +19,65 @@ pub struct T([f32; 4]); #[derive(Copy, Clone)] pub struct U(f32, f32, f32, f32); +// CHECK-LABEL: @array_align( +#[no_mangle] +pub fn array_align() -> usize { + // CHECK: ret [[USIZE:i[0-9]+]] [[ARRAY_ALIGN:[0-9]+]] + const { std::mem::align_of::() } +} + +// CHECK-LABEL: @vector_align( +#[no_mangle] +pub fn vector_align() -> usize { + // CHECK: ret [[USIZE]] [[VECTOR_ALIGN:[0-9]+]] + const { std::mem::align_of::() } +} + // CHECK-LABEL: @build_array_s #[no_mangle] pub fn build_array_s(x: [f32; 4]) -> S<4> { - // CHECK: call void @llvm.memcpy.{{.+}}({{.*}}, i{{[0-9]+}} 16, i1 false) + // CHECK: call void @llvm.memcpy.{{.+}}({{.*}} align [[VECTOR_ALIGN]] {{.*}} align [[ARRAY_ALIGN]] {{.*}}, [[USIZE]] 16, i1 false) S::<4>(x) } +// CHECK-LABEL: @build_array_transmute_s +#[no_mangle] +pub fn build_array_transmute_s(x: [f32; 4]) -> S<4> { + // CHECK: %[[VAL:.+]] = load <4 x float>, {{ptr %x|.+>\* %.+}}, align [[ARRAY_ALIGN]] + // CHECK: store <4 x float> %[[VAL:.+]], {{ptr %0|.+>\* %.+}}, align [[VECTOR_ALIGN]] + unsafe { std::mem::transmute(x) } +} + // CHECK-LABEL: @build_array_t #[no_mangle] pub fn build_array_t(x: [f32; 4]) -> T { - // CHECK: call void @llvm.memcpy.{{.+}}({{.*}}, i{{[0-9]+}} 16, i1 false) + // CHECK: call void @llvm.memcpy.{{.+}}({{.*}} align [[VECTOR_ALIGN]] {{.*}} align [[ARRAY_ALIGN]] {{.*}}, [[USIZE]] 16, i1 false) T(x) } +// CHECK-LABEL: @build_array_transmute_t +#[no_mangle] +pub fn build_array_transmute_t(x: [f32; 4]) -> T { + // CHECK: %[[VAL:.+]] = load <4 x float>, {{ptr %x|.+>\* %.+}}, align [[ARRAY_ALIGN]] + // CHECK: store <4 x float> %[[VAL:.+]], {{ptr %0|.+>\* %.+}}, align [[VECTOR_ALIGN]] + unsafe { std::mem::transmute(x) } +} + // CHECK-LABEL: @build_array_u #[no_mangle] pub fn build_array_u(x: [f32; 4]) -> U { - // CHECK: call void @llvm.memcpy.{{.+}}({{.*}}, i{{[0-9]+}} 16, i1 false) + // CHECK: store float %a, {{.+}}, align [[VECTOR_ALIGN]] + // CHECK: store float %b, {{.+}}, align [[ARRAY_ALIGN]] + // CHECK: store float %c, {{.+}}, align + // CHECK: store float %d, {{.+}}, align [[ARRAY_ALIGN]] + let [a, b, c, d] = x; + U(a, b, c, d) +} + +// CHECK-LABEL: @build_array_transmute_u +#[no_mangle] +pub fn build_array_transmute_u(x: [f32; 4]) -> U { + // CHECK: %[[VAL:.+]] = load <4 x float>, {{ptr %x|.+>\* %.+}}, align [[ARRAY_ALIGN]] + // CHECK: store <4 x float> %[[VAL:.+]], {{ptr %0|.+>\* %.+}}, align [[VECTOR_ALIGN]] unsafe { std::mem::transmute(x) } } diff --git a/tests/codegen/simd-wide-sum.rs b/tests/codegen/simd-wide-sum.rs index 04314dc29..db2aa20bd 100644 --- a/tests/codegen/simd-wide-sum.rs +++ b/tests/codegen/simd-wide-sum.rs @@ -1,4 +1,4 @@ -// compile-flags: -C opt-level=3 --edition=2021 +// compile-flags: -C opt-level=3 -Z merge-functions=disabled --edition=2021 // only-x86_64 // ignore-debug: the debug assertions get in the way diff --git a/tests/codegen/slice-indexing.rs b/tests/codegen/slice-indexing.rs new file mode 100644 index 000000000..c40d59fb0 --- /dev/null +++ b/tests/codegen/slice-indexing.rs @@ -0,0 +1,35 @@ +// compile-flags: -O +// only-64bit (because the LLVM type of i64 for usize shows up) +// ignore-debug: the debug assertions get in the way + +#![crate_type = "lib"] + +use std::ops::Range; + +// CHECK-LABEL: @index_by_range( +#[no_mangle] +pub fn index_by_range(x: &[u16], r: Range) -> &[u16] { + // CHECK: sub nuw i64 + &x[r] +} + +// CHECK-LABEL: @get_unchecked_by_range( +#[no_mangle] +pub unsafe fn get_unchecked_by_range(x: &[u16], r: Range) -> &[u16] { + // CHECK: sub nuw i64 + x.get_unchecked(r) +} + +// CHECK-LABEL: @index_mut_by_range( +#[no_mangle] +pub fn index_mut_by_range(x: &mut [i32], r: Range) -> &mut [i32] { + // CHECK: sub nuw i64 + &mut x[r] +} + +// CHECK-LABEL: @get_unchecked_mut_by_range( +#[no_mangle] +pub unsafe fn get_unchecked_mut_by_range(x: &mut [i32], r: Range) -> &mut [i32] { + // CHECK: sub nuw i64 + x.get_unchecked_mut(r) +} diff --git a/tests/codegen/transmute-scalar.rs b/tests/codegen/transmute-scalar.rs index 260dcbac0..af2cef472 100644 --- a/tests/codegen/transmute-scalar.rs +++ b/tests/codegen/transmute-scalar.rs @@ -1,80 +1,57 @@ // compile-flags: -O -C no-prepopulate-passes +// min-llvm-version: 15.0 # this test assumes `ptr`s and thus no `pointercast`s #![crate_type = "lib"] -// FIXME(eddyb) all of these tests show memory stores and loads, even after a -// scalar `bitcast`, more special-casing is required to remove `alloca` usage. +// With opaque ptrs in LLVM, `transmute` can load/store any `alloca` as any type, +// without needing to pointercast, and SRoA will turn that into a `bitcast`. +// Thus memory-to-memory transmutes don't need to generate them ourselves. + +// However, `bitcast`s and `ptrtoint`s and `inttoptr`s are still worth doing when +// that allows us to avoid the `alloca`s entirely; see `rvalue_creates_operand`. // CHECK-LABEL: define{{.*}}i32 @f32_to_bits(float noundef %x) -// CHECK: store i32 %{{.*}}, {{.*}} %0 -// CHECK-NEXT: %[[RES:.*]] = load i32, {{.*}} %0 -// CHECK: ret i32 %[[RES]] +// CHECK: %0 = bitcast float %x to i32 +// CHECK-NEXT: ret i32 %0 #[no_mangle] pub fn f32_to_bits(x: f32) -> u32 { unsafe { std::mem::transmute(x) } } // CHECK-LABEL: define{{.*}}i8 @bool_to_byte(i1 noundef zeroext %b) -// CHECK: %1 = zext i1 %b to i8 -// CHECK-NEXT: store i8 %1, {{.*}} %0 -// CHECK-NEXT: %2 = load i8, {{.*}} %0 -// CHECK: ret i8 %2 +// CHECK: %0 = zext i1 %b to i8 +// CHECK-NEXT: ret i8 %0 #[no_mangle] pub fn bool_to_byte(b: bool) -> u8 { unsafe { std::mem::transmute(b) } } // CHECK-LABEL: define{{.*}}noundef zeroext i1 @byte_to_bool(i8 noundef %byte) -// CHECK: %1 = trunc i8 %byte to i1 -// CHECK-NEXT: %2 = zext i1 %1 to i8 -// CHECK-NEXT: store i8 %2, {{.*}} %0 -// CHECK-NEXT: %3 = load i8, {{.*}} %0 -// CHECK-NEXT: %4 = trunc i8 %3 to i1 -// CHECK: ret i1 %4 +// CHECK: %0 = trunc i8 %byte to i1 +// CHECK-NEXT: ret i1 %0 #[no_mangle] pub unsafe fn byte_to_bool(byte: u8) -> bool { std::mem::transmute(byte) } -// CHECK-LABEL: define{{.*}}{{i8\*|ptr}} @ptr_to_ptr({{i16\*|ptr}} noundef %p) -// CHECK: store {{i8\*|ptr}} %{{.*}}, {{.*}} %0 -// CHECK-NEXT: %[[RES:.*]] = load {{i8\*|ptr}}, {{.*}} %0 -// CHECK: ret {{i8\*|ptr}} %[[RES]] +// CHECK-LABEL: define{{.*}}ptr @ptr_to_ptr(ptr noundef %p) +// CHECK: ret ptr %p #[no_mangle] pub fn ptr_to_ptr(p: *mut u16) -> *mut u8 { unsafe { std::mem::transmute(p) } } -// HACK(eddyb) scalar `transmute`s between pointers and non-pointers are -// currently not special-cased like other scalar `transmute`s, because -// LLVM requires specifically `ptrtoint`/`inttoptr` instead of `bitcast`. -// -// Tests below show the non-special-cased behavior (with the possible -// future special-cased instructions in the "NOTE(eddyb)" comments). - -// CHECK: define{{.*}}[[USIZE:i[0-9]+]] @ptr_to_int({{i16\*|ptr}} noundef %p) - -// NOTE(eddyb) see above, the following two CHECK lines should ideally be this: -// %2 = ptrtoint i16* %p to [[USIZE]] -// store [[USIZE]] %2, [[USIZE]]* %0 -// CHECK: store {{i16\*|ptr}} %p, {{.*}} - -// CHECK-NEXT: %[[RES:.*]] = load [[USIZE]], {{.*}} %0 -// CHECK: ret [[USIZE]] %[[RES]] +// CHECK: define{{.*}}[[USIZE:i[0-9]+]] @ptr_to_int(ptr noundef %p) +// CHECK: %0 = ptrtoint ptr %p to [[USIZE]] +// CHECK-NEXT: ret [[USIZE]] %0 #[no_mangle] pub fn ptr_to_int(p: *mut u16) -> usize { unsafe { std::mem::transmute(p) } } -// CHECK: define{{.*}}{{i16\*|ptr}} @int_to_ptr([[USIZE]] noundef %i) - -// NOTE(eddyb) see above, the following two CHECK lines should ideally be this: -// %2 = inttoptr [[USIZE]] %i to i16* -// store i16* %2, i16** %0 -// CHECK: store [[USIZE]] %i, {{.*}} - -// CHECK-NEXT: %[[RES:.*]] = load {{i16\*|ptr}}, {{.*}} %0 -// CHECK: ret {{i16\*|ptr}} %[[RES]] +// CHECK: define{{.*}}ptr @int_to_ptr([[USIZE]] noundef %i) +// CHECK: %0 = inttoptr [[USIZE]] %i to ptr +// CHECK-NEXT: ret ptr %0 #[no_mangle] pub fn int_to_ptr(i: usize) -> *mut u16 { unsafe { std::mem::transmute(i) } diff --git a/tests/codegen/var-names.rs b/tests/codegen/var-names.rs index d4715efad..53841df32 100644 --- a/tests/codegen/var-names.rs +++ b/tests/codegen/var-names.rs @@ -9,7 +9,7 @@ pub fn test(a: u32, b: u32) -> u32 { // CHECK: %c = add i32 %a, %b let d = c; let e = d * a; - // CHECK-NEXT: %e = mul i32 %c, %a + // CHECK-NEXT: %0 = mul i32 %c, %a e - // CHECK-NEXT: ret i32 %e + // CHECK-NEXT: ret i32 %0 } diff --git a/tests/codegen/vec-as-ptr.rs b/tests/codegen/vec-as-ptr.rs new file mode 100644 index 000000000..8ff7ba9cb --- /dev/null +++ b/tests/codegen/vec-as-ptr.rs @@ -0,0 +1,19 @@ +// compile-flags: -O -Zmerge-functions=disabled + +#![crate_type = "lib"] + +// Test that even though we return a *const u8 not a &[u8] or a NonNull, LLVM knows that this +// pointer is nonnull. +// CHECK: nonnull {{i8\*|ptr}} @vec_as_ptr +#[no_mangle] +pub fn vec_as_ptr(v: &Vec) -> *const u8 { + v.as_ptr() +} + +// Test that even though we return a *const u8 not a &[u8] or a NonNull, LLVM knows that this +// pointer is nonnull. +// CHECK: nonnull {{i8\*|ptr}} @vec_as_mut_ptr +#[no_mangle] +pub fn vec_as_mut_ptr(v: &mut Vec) -> *mut u8 { + v.as_mut_ptr() +} diff --git a/tests/codegen/vec-in-place.rs b/tests/codegen/vec-in-place.rs index 999260422..d68067ceb 100644 --- a/tests/codegen/vec-in-place.rs +++ b/tests/codegen/vec-in-place.rs @@ -1,11 +1,13 @@ // ignore-debug: the debug assertions get in the way // compile-flags: -O -Z merge-functions=disabled +// min-llvm-version: 16 #![crate_type = "lib"] // Ensure that trivial casts of vec elements are O(1) pub struct Wrapper(T); +// previously repr(C) caused the optimization to fail #[repr(C)] pub struct Foo { a: u64, @@ -14,9 +16,8 @@ pub struct Foo { d: u64, } -// Going from an aggregate struct to another type currently requires Copy to -// enable the TrustedRandomAccess specialization. Without it optimizations do not yet -// reliably recognize the loops as noop for repr(C) or non-Copy structs. +// implementing Copy exercises the TrustedRandomAccess specialization inside the in-place +// specialization #[derive(Copy, Clone)] pub struct Bar { a: u64, @@ -25,6 +26,14 @@ pub struct Bar { d: u64, } +// this exercises the try-fold codepath +pub struct Baz { + a: u64, + b: u64, + c: u64, + d: u64, +} + // CHECK-LABEL: @vec_iterator_cast_primitive #[no_mangle] pub fn vec_iterator_cast_primitive(vec: Vec) -> Vec { @@ -52,18 +61,29 @@ pub fn vec_iterator_cast_unwrap(vec: Vec>) -> Vec { // CHECK-LABEL: @vec_iterator_cast_aggregate #[no_mangle] pub fn vec_iterator_cast_aggregate(vec: Vec<[u64; 4]>) -> Vec { - // FIXME These checks should be the same as other functions. - // CHECK-NOT: @__rust_alloc - // CHECK-NOT: @__rust_alloc + // CHECK-NOT: loop + // CHECK-NOT: call vec.into_iter().map(|e| unsafe { std::mem::transmute(e) }).collect() } -// CHECK-LABEL: @vec_iterator_cast_deaggregate +// CHECK-LABEL: @vec_iterator_cast_deaggregate_tra #[no_mangle] -pub fn vec_iterator_cast_deaggregate(vec: Vec) -> Vec<[u64; 4]> { - // FIXME These checks should be the same as other functions. - // CHECK-NOT: @__rust_alloc - // CHECK-NOT: @__rust_alloc +pub fn vec_iterator_cast_deaggregate_tra(vec: Vec) -> Vec<[u64; 4]> { + // CHECK-NOT: loop + // CHECK-NOT: call + + // Safety: For the purpose of this test we assume that Bar layout matches [u64; 4]. + // This currently is not guaranteed for repr(Rust) types, but it happens to work here and + // the UCG may add additional guarantees for homogenous types in the future that would make this + // correct. + vec.into_iter().map(|e| unsafe { std::mem::transmute(e) }).collect() +} + +// CHECK-LABEL: @vec_iterator_cast_deaggregate_fold +#[no_mangle] +pub fn vec_iterator_cast_deaggregate_fold(vec: Vec) -> Vec<[u64; 4]> { + // CHECK-NOT: loop + // CHECK-NOT: call // Safety: For the purpose of this test we assume that Bar layout matches [u64; 4]. // This currently is not guaranteed for repr(Rust) types, but it happens to work here and diff --git a/tests/codegen/vec-shrink-panik.rs b/tests/codegen/vec-shrink-panik.rs index aa6589dc3..b3c3483fe 100644 --- a/tests/codegen/vec-shrink-panik.rs +++ b/tests/codegen/vec-shrink-panik.rs @@ -1,3 +1,8 @@ +// revisions: old new +// LLVM 17 realizes double panic is not possible and doesn't generate calls +// to panic_cannot_unwind. +// [old]ignore-llvm-version: 17 - 99 +// [new]min-llvm-version: 17 // compile-flags: -O // ignore-debug: the debug assertions get in the way #![crate_type = "lib"] @@ -18,11 +23,11 @@ pub fn shrink_to_fit(vec: &mut Vec) { pub fn issue71861(vec: Vec) -> Box<[u32]> { // CHECK-NOT: panic - // Call to panic_cannot_unwind in case of double-panic is expected, - // but other panics are not. + // Call to panic_cannot_unwind in case of double-panic is expected + // on LLVM 16 and older, but other panics are not. // CHECK: cleanup - // CHECK-NEXT: ; call core::panicking::panic_cannot_unwind - // CHECK-NEXT: panic_cannot_unwind + // old-NEXT: ; call core::panicking::panic_cannot_unwind + // old-NEXT: panic_cannot_unwind // CHECK-NOT: panic vec.into_boxed_slice() @@ -34,14 +39,14 @@ pub fn issue75636<'a>(iter: &[&'a str]) -> Box<[&'a str]> { // CHECK-NOT: panic // Call to panic_cannot_unwind in case of double-panic is expected, - // but other panics are not. + // on LLVM 16 and older, but other panics are not. // CHECK: cleanup - // CHECK-NEXT: ; call core::panicking::panic_cannot_unwind - // CHECK-NEXT: panic_cannot_unwind + // old-NEXT: ; call core::panicking::panic_cannot_unwind + // old-NEXT: panic_cannot_unwind // CHECK-NOT: panic iter.iter().copied().collect() } -// CHECK: ; core::panicking::panic_cannot_unwind -// CHECK: declare void @{{.*}}panic_cannot_unwind +// old: ; core::panicking::panic_cannot_unwind +// old: declare void @{{.*}}panic_cannot_unwind diff --git a/tests/codegen/virtual-function-elimination.rs b/tests/codegen/virtual-function-elimination.rs index 4cf7e12fe..f22176a02 100644 --- a/tests/codegen/virtual-function-elimination.rs +++ b/tests/codegen/virtual-function-elimination.rs @@ -1,5 +1,6 @@ // compile-flags: -Zvirtual-function-elimination -Clto -O -Csymbol-mangling-version=v0 // ignore-32bit +// ignore-debug // CHECK: @vtable.0 = {{.*}}, !type ![[TYPE0:[0-9]+]], !vcall_visibility ![[VCALL_VIS0:[0-9]+]] // CHECK: @vtable.1 = {{.*}}, !type ![[TYPE1:[0-9]+]], !vcall_visibility ![[VCALL_VIS0:[0-9]+]] @@ -81,7 +82,7 @@ fn taking_u(u: &dyn U) -> i32 { } pub fn taking_v(v: &dyn V) -> i32 { - // CHECK: @llvm.type.checked.load({{.*}}, i32 24, metadata !"NtCsfRpWlKdQPZn_28virtual_function_elimination1V") + // CHECK: @llvm.type.checked.load({{.*}}, i32 24, metadata !"NtCs64ITQYi9761_28virtual_function_elimination1V") v.public_function() } @@ -96,5 +97,5 @@ pub fn main() { // CHECK: ![[TYPE0]] = !{i64 0, !"[[MANGLED_TYPE0]]"} // CHECK: ![[VCALL_VIS0]] = !{i64 2} // CHECK: ![[TYPE1]] = !{i64 0, !"[[MANGLED_TYPE1]]"} -// CHECK: ![[TYPE2]] = !{i64 0, !"NtCsfRpWlKdQPZn_28virtual_function_elimination1V"} +// CHECK: ![[TYPE2]] = !{i64 0, !"NtCs64ITQYi9761_28virtual_function_elimination1V"} // CHECK: ![[VCALL_VIS2]] = !{i64 1} -- cgit v1.2.3