diff options
Diffstat (limited to 'tests/codegen-llvm/issues')
76 files changed, 1989 insertions, 0 deletions
diff --git a/tests/codegen-llvm/issues/issue-101048.rs b/tests/codegen-llvm/issues/issue-101048.rs new file mode 100644 index 00000000000..cfe65e758fd --- /dev/null +++ b/tests/codegen-llvm/issues/issue-101048.rs @@ -0,0 +1,12 @@ +//@ compile-flags: -Copt-level=3 + +#![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-llvm/issues/issue-101082.rs b/tests/codegen-llvm/issues/issue-101082.rs new file mode 100644 index 00000000000..8d15921ddb4 --- /dev/null +++ b/tests/codegen-llvm/issues/issue-101082.rs @@ -0,0 +1,42 @@ +//@ compile-flags: -Copt-level=3 +//@ revisions: host x86-64 x86-64-v3 +//@ min-llvm-version: 20 + +//@[host] ignore-x86_64 + +// Set the base cpu explicitly, in case the default has been changed. +//@[x86-64] only-x86_64 +//@[x86-64] compile-flags: -Ctarget-cpu=x86-64 + +// FIXME(cuviper) x86-64-v3 in particular regressed in #131563, and the workaround +// at the time still sometimes fails, so only verify it for the power-of-two size +// - https://github.com/llvm/llvm-project/issues/134735 +//@[x86-64-v3] only-x86_64 +//@[x86-64-v3] min-llvm-version: 21 +//@[x86-64-v3] compile-flags: -Ctarget-cpu=x86-64-v3 + +#![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 +} + +#[no_mangle] +pub fn test_eight() -> usize { + // CHECK-LABEL: @test_eight( + // CHECK: ret {{i64|i32}} 220 + let values = [23, 16, 54, 3, 60, 9, 13, 42]; + let mut acc = 0; + for item in values { + acc += item; + } + acc +} diff --git a/tests/codegen-llvm/issues/issue-101814.rs b/tests/codegen-llvm/issues/issue-101814.rs new file mode 100644 index 00000000000..668ec8476e8 --- /dev/null +++ b/tests/codegen-llvm/issues/issue-101814.rs @@ -0,0 +1,18 @@ +//@ compile-flags: -Copt-level=3 + +#![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-llvm/issues/issue-103132.rs b/tests/codegen-llvm/issues/issue-103132.rs new file mode 100644 index 00000000000..623cab92806 --- /dev/null +++ b/tests/codegen-llvm/issues/issue-103132.rs @@ -0,0 +1,15 @@ +//@ compile-flags: -Copt-level=3 -C overflow-checks + +#![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-llvm/issues/issue-103285-ptr-addr-overflow-check.rs b/tests/codegen-llvm/issues/issue-103285-ptr-addr-overflow-check.rs new file mode 100644 index 00000000000..3ada5412e83 --- /dev/null +++ b/tests/codegen-llvm/issues/issue-103285-ptr-addr-overflow-check.rs @@ -0,0 +1,15 @@ +//@ compile-flags: -Copt-level=3 -C debug-assertions=yes + +#![crate_type = "lib"] + +#[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-llvm/issues/issue-103327.rs b/tests/codegen-llvm/issues/issue-103327.rs new file mode 100644 index 00000000000..4de3cfd12a0 --- /dev/null +++ b/tests/codegen-llvm/issues/issue-103327.rs @@ -0,0 +1,13 @@ +//@ compile-flags: -Copt-level=3 + +#![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-llvm/issues/issue-103840.rs b/tests/codegen-llvm/issues/issue-103840.rs new file mode 100644 index 00000000000..c6c5098bdd0 --- /dev/null +++ b/tests/codegen-llvm/issues/issue-103840.rs @@ -0,0 +1,9 @@ +//@ compile-flags: -Copt-level=3 +#![crate_type = "lib"] + +pub fn foo(t: &mut Vec<usize>) { + // CHECK-NOT: __rust_dealloc + let mut taken = std::mem::take(t); + taken.pop(); + *t = taken; +} diff --git a/tests/codegen-llvm/issues/issue-105386-ub-in-debuginfo.rs b/tests/codegen-llvm/issues/issue-105386-ub-in-debuginfo.rs new file mode 100644 index 00000000000..848aa910b58 --- /dev/null +++ b/tests/codegen-llvm/issues/issue-105386-ub-in-debuginfo.rs @@ -0,0 +1,24 @@ +//@ compile-flags: --crate-type=lib -Copt-level=3 -Cdebuginfo=2 -Cno-prepopulate-passes -Zmir-enable-passes=-ScalarReplacementOfAggregates +// MIR SROA will decompose the closure +#![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 +// CHECK-NOT: [[ptr_tmp:%.*]] = getelementptr inbounds i8, ptr [[spill]] +// CHECK-NOT: [[load:%.*]] = load ptr, ptr +// CHECK: call void @llvm.lifetime.start{{.*}}({{.*}}, ptr [[spill]]) +// CHECK: [[inner:%.*]] = getelementptr inbounds i8, ptr [[spill]] +// CHECK: call void @llvm.memcpy{{.*}}(ptr {{align .*}} [[inner]], ptr {{align .*}} %x diff --git a/tests/codegen-llvm/issues/issue-106369.rs b/tests/codegen-llvm/issues/issue-106369.rs new file mode 100644 index 00000000000..3583d20c9fa --- /dev/null +++ b/tests/codegen-llvm/issues/issue-106369.rs @@ -0,0 +1,14 @@ +//@ compile-flags: -Copt-level=3 + +#![crate_type = "lib"] + +// From <https://github.com/rust-lang/rust/issues/106369#issuecomment-1369095304> + +// 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-llvm/issues/issue-107681-unwrap_unchecked.rs b/tests/codegen-llvm/issues/issue-107681-unwrap_unchecked.rs new file mode 100644 index 00000000000..69aefc6b1fb --- /dev/null +++ b/tests/codegen-llvm/issues/issue-107681-unwrap_unchecked.rs @@ -0,0 +1,19 @@ +//@ compile-flags: -Copt-level=3 + +// Test for #107681. +// Make sure we don't create `br` or `select` instructions. + +#![crate_type = "lib"] + +use std::iter::Copied; +use std::slice::Iter; + +#[no_mangle] +pub unsafe fn foo(x: &mut Copied<Iter<'_, u32>>) -> u32 { + // CHECK-LABEL: @foo( + // CHECK-NOT: br + // CHECK-NOT: select + // CHECK: [[RET:%.*]] = load i32, ptr + // CHECK-NEXT: ret i32 [[RET]] + x.next().unwrap_unchecked() +} diff --git a/tests/codegen-llvm/issues/issue-108395-branchy-bool-match.rs b/tests/codegen-llvm/issues/issue-108395-branchy-bool-match.rs new file mode 100644 index 00000000000..96387e791b0 --- /dev/null +++ b/tests/codegen-llvm/issues/issue-108395-branchy-bool-match.rs @@ -0,0 +1,27 @@ +//@ compile-flags: -Copt-level=3 -Zmerge-functions=disabled +//! Test for <https://github.com/rust-lang/rust/issues/108395>. Check that +//! matching on two bools with wildcards does not produce branches. +#![crate_type = "lib"] + +// CHECK-LABEL: @wildcard( +#[no_mangle] +pub fn wildcard(a: u16, b: u16, v: u16) -> u16 { + // CHECK-NOT: br + match (a == v, b == v) { + (true, false) => 0, + (false, true) => u16::MAX, + _ => 1 << 15, // half + } +} + +// CHECK-LABEL: @exhaustive( +#[no_mangle] +pub fn exhaustive(a: u16, b: u16, v: u16) -> u16 { + // CHECK-NOT: br + match (a == v, b == v) { + (true, false) => 0, + (false, true) => u16::MAX, + (true, true) => 1 << 15, + (false, false) => 1 << 15, + } +} diff --git a/tests/codegen-llvm/issues/issue-109328-split_first.rs b/tests/codegen-llvm/issues/issue-109328-split_first.rs new file mode 100644 index 00000000000..26235edfc19 --- /dev/null +++ b/tests/codegen-llvm/issues/issue-109328-split_first.rs @@ -0,0 +1,16 @@ +//@ compile-flags: -Copt-level=3 + +#![crate_type = "lib"] + +// CHECK-LABEL: @foo +// CHECK-NEXT: {{.*}}: +// CHECK-NEXT: getelementptr inbounds +// CHECK-NEXT: load [[TYPE:i(32|64)]] +// CHECK-NEXT: icmp eq [[TYPE]] +// CHECK-NEXT: br i1 +#[no_mangle] +pub fn foo(input: &mut &[u64]) -> Option<u64> { + let (first, rest) = input.split_first()?; + *input = rest; + Some(*first) +} diff --git a/tests/codegen-llvm/issues/issue-110797-enum-jump-same.rs b/tests/codegen-llvm/issues/issue-110797-enum-jump-same.rs new file mode 100644 index 00000000000..b5f7c08795b --- /dev/null +++ b/tests/codegen-llvm/issues/issue-110797-enum-jump-same.rs @@ -0,0 +1,25 @@ +//@ compile-flags: -Copt-level=3 + +#![crate_type = "lib"] + +pub enum K { + A(Box<[i32]>), + B(Box<[u8]>), + C(Box<[String]>), + D(Box<[u16]>), +} + +// CHECK-LABEL: @get_len +// CHECK-NEXT: {{.*}}: +// CHECK-NEXT: getelementptr inbounds +// CHECK-NEXT: load [[TYPE:i(32|64)]] +// CHECK-NEXT: ret [[TYPE]] +#[no_mangle] +pub fn get_len(arg: &K) -> usize { + match arg { + K::A(ref lst) => lst.len(), + K::B(ref lst) => lst.len(), + K::C(ref lst) => lst.len(), + K::D(ref lst) => lst.len(), + } +} diff --git a/tests/codegen-llvm/issues/issue-111603.rs b/tests/codegen-llvm/issues/issue-111603.rs new file mode 100644 index 00000000000..2ba5a3f876a --- /dev/null +++ b/tests/codegen-llvm/issues/issue-111603.rs @@ -0,0 +1,40 @@ +//@ compile-flags: -Copt-level=3 + +#![crate_type = "lib"] +#![feature(get_mut_unchecked, new_uninit)] + +use std::sync::Arc; + +// CHECK-LABEL: @new_from_array +#[no_mangle] +pub fn new_from_array(x: u64) -> Arc<[u64]> { + // Ensure that we only generate one alloca for the array. + + // CHECK: alloca + // CHECK-SAME: [8000 x i8] + // CHECK-NOT: alloca + let array = [x; 1000]; + Arc::new(array) +} + +// CHECK-LABEL: @new_uninit +#[no_mangle] +pub fn new_uninit(x: u64) -> Arc<[u64; 1000]> { + // CHECK: call alloc::sync::arcinner_layout_for_value_layout + // CHECK-NOT: call alloc::sync::arcinner_layout_for_value_layout + let mut arc = Arc::new_uninit(); + unsafe { Arc::get_mut_unchecked(&mut arc) }.write([x; 1000]); + unsafe { arc.assume_init() } +} + +// CHECK-LABEL: @new_uninit_slice +#[no_mangle] +pub fn new_uninit_slice(x: u64) -> Arc<[u64]> { + // CHECK: call alloc::sync::arcinner_layout_for_value_layout + // CHECK-NOT: call alloc::sync::arcinner_layout_for_value_layout + let mut arc = Arc::new_uninit_slice(1000); + for elem in unsafe { Arc::get_mut_unchecked(&mut arc) } { + elem.write(x); + } + unsafe { arc.assume_init() } +} diff --git a/tests/codegen-llvm/issues/issue-112509-slice-get-andthen-get.rs b/tests/codegen-llvm/issues/issue-112509-slice-get-andthen-get.rs new file mode 100644 index 00000000000..3909b203d08 --- /dev/null +++ b/tests/codegen-llvm/issues/issue-112509-slice-get-andthen-get.rs @@ -0,0 +1,16 @@ +//@ compile-flags: -Copt-level=3 +#![crate_type = "lib"] + +// CHECK-LABEL: @write_u8_variant_a +// CHECK-NEXT: {{.*}}: +// CHECK-NEXT: icmp ugt +// CHECK-NEXT: getelementptr +// CHECK-NEXT: select i1 {{.+}} null +// CHECK-NEXT: insertvalue +// CHECK-NEXT: insertvalue +// CHECK-NEXT: ret +#[no_mangle] +pub fn write_u8_variant_a(bytes: &mut [u8], buf: u8, offset: usize) -> Option<&mut [u8]> { + let buf = buf.to_le_bytes(); + bytes.get_mut(offset..).and_then(|bytes| bytes.get_mut(..buf.len())) +} diff --git a/tests/codegen-llvm/issues/issue-113757-bounds-check-after-cmp-max.rs b/tests/codegen-llvm/issues/issue-113757-bounds-check-after-cmp-max.rs new file mode 100644 index 00000000000..d495adf9980 --- /dev/null +++ b/tests/codegen-llvm/issues/issue-113757-bounds-check-after-cmp-max.rs @@ -0,0 +1,18 @@ +// in Rust 1.73, -O and opt-level=3 optimizes differently +//@ compile-flags: -C opt-level=3 +#![crate_type = "lib"] + +use std::cmp::max; + +// CHECK-LABEL: @foo +// CHECK-NOT: slice_start_index_len_fail +// CHECK-NOT: unreachable +#[no_mangle] +pub fn foo(v: &mut Vec<u8>, size: usize) -> Option<&mut [u8]> { + if v.len() > max(1, size) { + let start = v.len() - size; + Some(&mut v[start..]) + } else { + None + } +} diff --git a/tests/codegen-llvm/issues/issue-114312.rs b/tests/codegen-llvm/issues/issue-114312.rs new file mode 100644 index 00000000000..61355dd5873 --- /dev/null +++ b/tests/codegen-llvm/issues/issue-114312.rs @@ -0,0 +1,25 @@ +//@ compile-flags: -Copt-level=3 +//@ only-x86_64-unknown-linux-gnu + +// We want to check that this function does not mis-optimize to loop jumping. + +#![crate_type = "lib"] + +#[repr(C)] +pub enum Expr { + Sum, + // must have more than usize data + Sub(usize, u8), +} + +#[no_mangle] +pub extern "C" fn issue_114312(expr: Expr) { + // CHECK-LABEL: @issue_114312( + // CHECK-SAME: byval + // CHECK-NEXT: start: + // CHECK-NEXT: ret void + match expr { + Expr::Sum => {} + Expr::Sub(_, _) => issue_114312(Expr::Sum), + } +} diff --git a/tests/codegen-llvm/issues/issue-115385-llvm-jump-threading.rs b/tests/codegen-llvm/issues/issue-115385-llvm-jump-threading.rs new file mode 100644 index 00000000000..8cabd94f202 --- /dev/null +++ b/tests/codegen-llvm/issues/issue-115385-llvm-jump-threading.rs @@ -0,0 +1,46 @@ +//@ compile-flags: -Copt-level=3 -Ccodegen-units=1 + +#![crate_type = "lib"] + +#[repr(i64)] +pub enum Boolean { + False = 0, + True = 1, +} + +impl Clone for Boolean { + fn clone(&self) -> Self { + *self + } +} + +impl Copy for Boolean {} + +extern "C" { + fn set_value(foo: *mut i64); + fn bar(); +} + +pub fn foo(x: bool) { + let mut foo = core::mem::MaybeUninit::<i64>::uninit(); + unsafe { + set_value(foo.as_mut_ptr()); + } + + if x { + let l1 = unsafe { *foo.as_mut_ptr().cast::<Boolean>() }; + if matches!(l1, Boolean::False) { + unsafe { + *foo.as_mut_ptr() = 0; + } + } + } + + let l2 = unsafe { *foo.as_mut_ptr() }; + if l2 == 2 { + // CHECK: call void @bar + unsafe { + bar(); + } + } +} diff --git a/tests/codegen-llvm/issues/issue-116878.rs b/tests/codegen-llvm/issues/issue-116878.rs new file mode 100644 index 00000000000..daf46c8bb55 --- /dev/null +++ b/tests/codegen-llvm/issues/issue-116878.rs @@ -0,0 +1,11 @@ +//@ compile-flags: -Copt-level=3 +#![crate_type = "lib"] + +/// Make sure no bounds checks are emitted after a `get_unchecked`. +// CHECK-LABEL: @unchecked_slice_no_bounds_check +#[no_mangle] +pub unsafe fn unchecked_slice_no_bounds_check(s: &[u8]) -> u8 { + let a = *s.get_unchecked(1); + // CHECK-NOT: panic_bounds_check + a + s[0] +} diff --git a/tests/codegen-llvm/issues/issue-118306.rs b/tests/codegen-llvm/issues/issue-118306.rs new file mode 100644 index 00000000000..f12dc7cdfe2 --- /dev/null +++ b/tests/codegen-llvm/issues/issue-118306.rs @@ -0,0 +1,22 @@ +//@ compile-flags: -Copt-level=3 +//@ only-x86_64 + +// Test for #118306. +// Make sure we don't create `br` or `select` instructions. + +#![crate_type = "lib"] + +#[no_mangle] +pub fn branchy(input: u64) -> u64 { + // CHECK-LABEL: @branchy( + // CHECK-NEXT: start: + // CHECK-NEXT: [[_2:%.*]] = and i64 [[INPUT:%.*]], 3 + // CHECK-NEXT: [[SWITCH_GEP:%.*]] = getelementptr inbounds{{( nuw)?}} [4 x i64], ptr @switch.table.branchy, i64 0, i64 [[_2]] + // CHECK-NEXT: [[SWITCH_LOAD:%.*]] = load i64, ptr [[SWITCH_GEP]] + // CHECK-NEXT: ret i64 [[SWITCH_LOAD]] + match input % 4 { + 1 | 2 => 1, + 3 => 2, + _ => 0, + } +} diff --git a/tests/codegen-llvm/issues/issue-118392.rs b/tests/codegen-llvm/issues/issue-118392.rs new file mode 100644 index 00000000000..07de8d9b237 --- /dev/null +++ b/tests/codegen-llvm/issues/issue-118392.rs @@ -0,0 +1,10 @@ +//@ compile-flags: -Copt-level=3 +#![crate_type = "lib"] + +// CHECK-LABEL: @div2 +// CHECK: ashr i32 %a, 1 +// CHECK-NEXT: ret i32 +#[no_mangle] +pub fn div2(a: i32) -> i32 { + a.div_euclid(2) +} diff --git a/tests/codegen-llvm/issues/issue-119422.rs b/tests/codegen-llvm/issues/issue-119422.rs new file mode 100644 index 00000000000..17ae71605b5 --- /dev/null +++ b/tests/codegen-llvm/issues/issue-119422.rs @@ -0,0 +1,83 @@ +//! This test checks that compiler don't generate useless compares to zeros +//! for `NonZero` integer types. +//! +//@ compile-flags: -Copt-level=3 -Zmerge-functions=disabled +//@ edition: 2021 +//@ only-64bit (because the LLVM type of i64 for usize shows up) +#![crate_type = "lib"] + +use core::num::NonZero; +use core::ptr::NonNull; + +// CHECK-LABEL: @check_non_null +#[no_mangle] +pub fn check_non_null(x: NonNull<u8>) -> bool { + // CHECK: ret i1 false + x.as_ptr().is_null() +} + +// CHECK-LABEL: @equals_zero_is_false_u8 +#[no_mangle] +pub fn equals_zero_is_false_u8(x: NonZero<u8>) -> bool { + // CHECK-NOT: br + // CHECK: ret i1 false + // CHECK-NOT: br + x.get() == 0 +} + +// CHECK-LABEL: @not_equals_zero_is_true_u8 +#[no_mangle] +pub fn not_equals_zero_is_true_u8(x: NonZero<u8>) -> bool { + // CHECK-NOT: br + // CHECK: ret i1 true + // CHECK-NOT: br + x.get() != 0 +} + +// CHECK-LABEL: @equals_zero_is_false_i8 +#[no_mangle] +pub fn equals_zero_is_false_i8(x: NonZero<i8>) -> bool { + // CHECK-NOT: br + // CHECK: ret i1 false + // CHECK-NOT: br + x.get() == 0 +} + +// CHECK-LABEL: @not_equals_zero_is_true_i8 +#[no_mangle] +pub fn not_equals_zero_is_true_i8(x: NonZero<i8>) -> bool { + // CHECK-NOT: br + // CHECK: ret i1 true + // CHECK-NOT: br + x.get() != 0 +} + +// CHECK-LABEL: @usize_try_from_u32 +#[no_mangle] +pub fn usize_try_from_u32(x: NonZero<u32>) -> NonZero<usize> { + // CHECK-NOT: br + // CHECK: zext i32 %{{.*}} to i64 + // CHECK-NOT: br + // CHECK: ret i64 + x.try_into().unwrap() +} + +// CHECK-LABEL: @isize_try_from_i32 +#[no_mangle] +pub fn isize_try_from_i32(x: NonZero<i32>) -> NonZero<isize> { + // CHECK-NOT: br + // CHECK: sext i32 %{{.*}} to i64 + // CHECK-NOT: br + // CHECK: ret i64 + x.try_into().unwrap() +} + +// CHECK-LABEL: @u64_from_nonzero_is_not_zero +#[no_mangle] +pub fn u64_from_nonzero_is_not_zero(x: NonZero<u64>) -> bool { + // CHECK-NOT: br + // CHECK: ret i1 false + // CHECK-NOT: br + let v: u64 = x.into(); + v == 0 +} diff --git a/tests/codegen-llvm/issues/issue-121719-common-field-offset.rs b/tests/codegen-llvm/issues/issue-121719-common-field-offset.rs new file mode 100644 index 00000000000..9f5f44e0375 --- /dev/null +++ b/tests/codegen-llvm/issues/issue-121719-common-field-offset.rs @@ -0,0 +1,44 @@ +//! This test checks that match branches which all access a field +//! at the same offset are merged together. +//! +//@ compile-flags: -Copt-level=3 +#![crate_type = "lib"] + +#[repr(C)] +pub struct A { + x: f64, + y: u64, +} +#[repr(C)] +pub struct B { + x: f64, + y: u32, +} +#[repr(C)] +pub struct C { + x: f64, + y: u16, +} +#[repr(C)] +pub struct D { + x: f64, + y: u8, +} + +pub enum E { + A(A), + B(B), + C(C), + D(D), +} + +// CHECK-LABEL: @match_on_e +#[no_mangle] +pub fn match_on_e(e: &E) -> &f64 { + // CHECK: start: + // CHECK-NEXT: getelementptr + // CHECK-NEXT: ret + match e { + E::A(A { x, .. }) | E::B(B { x, .. }) | E::C(C { x, .. }) | E::D(D { x, .. }) => x, + } +} diff --git a/tests/codegen-llvm/issues/issue-122600-ptr-discriminant-update.rs b/tests/codegen-llvm/issues/issue-122600-ptr-discriminant-update.rs new file mode 100644 index 00000000000..853a1ff36b1 --- /dev/null +++ b/tests/codegen-llvm/issues/issue-122600-ptr-discriminant-update.rs @@ -0,0 +1,43 @@ +//@ compile-flags: -Copt-level=3 + +#![crate_type = "lib"] + +// The bug here was that it was loading and storing the whole value. +// It's ok for it to load the discriminant, +// to preserve the UB from `unreachable_unchecked`, +// but it better only store the constant discriminant of `B`. + +pub enum State { + A([u8; 753]), + B([u8; 753]), +} + +// CHECK-LABEL: @update +#[no_mangle] +pub unsafe fn update(s: *mut State) { + // CHECK-NOT: alloca + + // CHECK-NOT: load + // CHECK-NOT: store + // CHECK-NOT: memcpy + // CHECK-NOT: 75{{3|4}} + + // CHECK: %[[TAG:.+]] = load i8, ptr %s, align 1 + // CHECK-NEXT: trunc nuw i8 %[[TAG]] to i1 + + // CHECK-NOT: load + // CHECK-NOT: store + // CHECK-NOT: memcpy + // CHECK-NOT: 75{{3|4}} + + // CHECK: store i8 1, ptr %s, align 1 + + // CHECK-NOT: load + // CHECK-NOT: store + // CHECK-NOT: memcpy + // CHECK-NOT: 75{{3|4}} + + // CHECK: ret + let State::A(v) = s.read() else { std::hint::unreachable_unchecked() }; + s.write(State::B(v)); +} diff --git a/tests/codegen-llvm/issues/issue-123712-str-to-lower-autovectorization.rs b/tests/codegen-llvm/issues/issue-123712-str-to-lower-autovectorization.rs new file mode 100644 index 00000000000..11ee10e8cc3 --- /dev/null +++ b/tests/codegen-llvm/issues/issue-123712-str-to-lower-autovectorization.rs @@ -0,0 +1,23 @@ +//@ only-x86_64 +//@ compile-flags: -C opt-level=3 +#![crate_type = "lib"] +#![no_std] +#![feature(str_internals)] + +extern crate alloc; + +/// Ensure that the ascii-prefix loop for `str::to_lowercase` and `str::to_uppercase` uses vector +/// instructions. +/// +/// The llvm ir should be the same for all targets that support some form of simd. Only targets +/// without any simd instructions would see scalarized ir. +/// Unfortunately, there is no `only-simd` directive to only run this test on only such platforms, +/// and using test revisions would still require the core libraries for all platforms. +// CHECK-LABEL: @lower_while_ascii +// CHECK: [[A:%[0-9]]] = load <16 x i8> +// CHECK-NEXT: [[B:%[0-9]]] = icmp slt <16 x i8> [[A]], zeroinitializer +// CHECK-NEXT: [[C:%[0-9]]] = bitcast <16 x i1> [[B]] to i16 +#[no_mangle] +pub fn lower_while_ascii(s: &str) -> (alloc::string::String, &str) { + alloc::str::convert_while_ascii(s, u8::to_ascii_lowercase) +} diff --git a/tests/codegen-llvm/issues/issue-126585.rs b/tests/codegen-llvm/issues/issue-126585.rs new file mode 100644 index 00000000000..466dab64cdc --- /dev/null +++ b/tests/codegen-llvm/issues/issue-126585.rs @@ -0,0 +1,23 @@ +//@ compile-flags: -Copt-level=s +//@ only-x86_64 + +// Test for #126585. +// Ensure that this IR doesn't have extra undef phi input, which also guarantees that this asm +// doesn't have subsequent labels and unnecessary `jmp` instructions. + +#![crate_type = "lib"] + +#[no_mangle] +fn checked_div_round(a: u64, b: u64) -> Option<u64> { + // CHECK-LABEL: @checked_div_round + // CHECK: phi + // CHECK-NOT: undef + // CHECK: phi + // CHECK-NOT: undef + match b { + 0 => None, + 1 => Some(a), + // `a / b` is computable and `(a % b) * 2` can not overflow since `b >= 2`. + b => Some(a / b + if (a % b) * 2 >= b { 1 } else { 0 }), + } +} diff --git a/tests/codegen-llvm/issues/issue-129795.rs b/tests/codegen-llvm/issues/issue-129795.rs new file mode 100644 index 00000000000..dc64ee35c97 --- /dev/null +++ b/tests/codegen-llvm/issues/issue-129795.rs @@ -0,0 +1,17 @@ +//@ compile-flags: -Copt-level=3 +//@ min-llvm-version: 20 +#![crate_type = "lib"] + +// Ensure that a modulo operation with an operand that is known to be +// a power-of-two is properly optimized. + +// CHECK-LABEL: @modulo_with_power_of_two_divisor +// CHECK: add i64 %divisor, -1 +// CHECK-NEXT: and i64 +// CHECK-NEXT: ret i64 +#[no_mangle] +pub fn modulo_with_power_of_two_divisor(dividend: u64, divisor: u64) -> u64 { + assert!(divisor.is_power_of_two()); + // should be optimized to (dividend & (divisor - 1)) + dividend % divisor +} diff --git a/tests/codegen-llvm/issues/issue-13018.rs b/tests/codegen-llvm/issues/issue-13018.rs new file mode 100644 index 00000000000..8040018b931 --- /dev/null +++ b/tests/codegen-llvm/issues/issue-13018.rs @@ -0,0 +1,14 @@ +//@ compile-flags: -Copt-level=3 + +// A drop([...].clone()) sequence on an Rc should be a no-op +// In particular, no call to __rust_dealloc should be emitted +// +// We use a cdylib since it's a leaf unit for Rust purposes, so doesn't codegen -Zshare-generics +// code. +#![crate_type = "cdylib"] +use std::rc::Rc; + +pub fn foo(t: &Rc<Vec<usize>>) { + // CHECK-NOT: __rust_dealloc + drop(t.clone()); +} diff --git a/tests/codegen-llvm/issues/issue-136329-optnone-noinline.rs b/tests/codegen-llvm/issues/issue-136329-optnone-noinline.rs new file mode 100644 index 00000000000..57c9e47a499 --- /dev/null +++ b/tests/codegen-llvm/issues/issue-136329-optnone-noinline.rs @@ -0,0 +1,21 @@ +//! Ensure that `#[optimize(none)]` functions are never inlined + +//@ compile-flags: -Copt-level=3 + +#![feature(optimize_attribute)] + +#[optimize(none)] +pub fn foo() { + let _x = 123; +} + +// CHECK-LABEL: define{{.*}}void @bar +// CHECK: start: +// CHECK: {{.*}}call {{.*}}void +// CHECK: ret void +#[no_mangle] +pub fn bar() { + foo(); +} + +fn main() {} diff --git a/tests/codegen-llvm/issues/issue-15953.rs b/tests/codegen-llvm/issues/issue-15953.rs new file mode 100644 index 00000000000..70e597ac1dd --- /dev/null +++ b/tests/codegen-llvm/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<i32>, +} + +#[inline(never)] +#[no_mangle] +// CHECK: memcpy +fn interior(x: Vec<i32>) -> Vec<i32> { + let Foo { x } = Foo { x }; + x +} + +#[inline(never)] +#[no_mangle] +// CHECK: memcpy +fn exterior(x: Vec<i32>) -> Vec<i32> { + x +} + +fn main() { + let x = interior(Vec::new()); + println!("{:?}", x); + + let x = exterior(Vec::new()); + println!("{:?}", x); +} diff --git a/tests/codegen-llvm/issues/issue-27130.rs b/tests/codegen-llvm/issues/issue-27130.rs new file mode 100644 index 00000000000..594e02af097 --- /dev/null +++ b/tests/codegen-llvm/issues/issue-27130.rs @@ -0,0 +1,21 @@ +//@ compile-flags: -Copt-level=3 + +#![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-llvm/issues/issue-32031.rs b/tests/codegen-llvm/issues/issue-32031.rs new file mode 100644 index 00000000000..559e8d947fb --- /dev/null +++ b/tests/codegen-llvm/issues/issue-32031.rs @@ -0,0 +1,29 @@ +//@ compile-flags: -C no-prepopulate-passes -Copt-level=0 +// 32-bit x86 returns `f32` and `f64` differently to avoid the x87 stack. +//@ revisions: x86 other +//@[x86] only-rustc_abi-x86-sse2 +//@[other] ignore-x86 + +#![crate_type = "lib"] + +#[no_mangle] +pub struct F32(f32); + +// other: define{{.*}}float @add_newtype_f32(float %a, float %b) +// x86: define{{.*}}<4 x i8> @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); + +// other: define{{.*}}double @add_newtype_f64(double %a, double %b) +// x86: define{{.*}}<8 x i8> @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-llvm/issues/issue-32364.rs b/tests/codegen-llvm/issues/issue-32364.rs new file mode 100644 index 00000000000..016981d1947 --- /dev/null +++ b/tests/codegen-llvm/issues/issue-32364.rs @@ -0,0 +1,17 @@ +// 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<T>() {} +} + +fn main() { + Foo::foo::<Foo>(); +} diff --git a/tests/codegen-llvm/issues/issue-34634.rs b/tests/codegen-llvm/issues/issue-34634.rs new file mode 100644 index 00000000000..d32fa97ec38 --- /dev/null +++ b/tests/codegen-llvm/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: -Copt-level=3 +#![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-llvm/issues/issue-34947-pow-i32.rs b/tests/codegen-llvm/issues/issue-34947-pow-i32.rs new file mode 100644 index 00000000000..b4750cd35bc --- /dev/null +++ b/tests/codegen-llvm/issues/issue-34947-pow-i32.rs @@ -0,0 +1,13 @@ +//@ compile-flags: -Copt-level=3 + +#![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-llvm/issues/issue-36010-some-box-is_some.rs b/tests/codegen-llvm/issues/issue-36010-some-box-is_some.rs new file mode 100644 index 00000000000..c9a8262162d --- /dev/null +++ b/tests/codegen-llvm/issues/issue-36010-some-box-is_some.rs @@ -0,0 +1,28 @@ +#![crate_type = "lib"] + +//@ compile-flags: -Copt-level=3 + +use std::mem; + +fn foo<T>(a: &mut T, b: T) -> bool { + let b = Some(mem::replace(a, b)); + let ret = b.is_some(); + mem::forget(b); + return ret; +} + +// CHECK-LABEL: @foo_u32 +// CHECK: store i32 +// CHECK-NEXT: ret i1 true +#[no_mangle] +pub fn foo_u32(a: &mut u32, b: u32) -> bool { + foo(a, b) +} + +// CHECK-LABEL: @foo_box +// CHECK: store ptr +// CHECK-NEXT: ret i1 true +#[no_mangle] +pub fn foo_box(a: &mut Box<u32>, b: Box<u32>) -> bool { + foo(a, b) +} diff --git a/tests/codegen-llvm/issues/issue-37945.rs b/tests/codegen-llvm/issues/issue-37945.rs new file mode 100644 index 00000000000..23d0eab8ae4 --- /dev/null +++ b/tests/codegen-llvm/issues/issue-37945.rs @@ -0,0 +1,34 @@ +//@ compile-flags: -Copt-level=3 -Zmerge-functions=disabled +//@ ignore-32bit 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<f32>) -> bool { + // CHECK-LABEL: @is_empty_1( + // CHECK-NEXT: start: + // CHECK-NEXT: [[A:%.*]] = icmp ne 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 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<f32>) -> bool { + // CHECK-LABEL: @is_empty_2 + // CHECK-NEXT: start: + // CHECK-NEXT: [[C:%.*]] = icmp ne 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 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-llvm/issues/issue-45222.rs b/tests/codegen-llvm/issues/issue-45222.rs new file mode 100644 index 00000000000..0201363c41a --- /dev/null +++ b/tests/codegen-llvm/issues/issue-45222.rs @@ -0,0 +1,62 @@ +//@ compile-flags: -Copt-level=3 + +#![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-llvm/issues/issue-45466.rs b/tests/codegen-llvm/issues/issue-45466.rs new file mode 100644 index 00000000000..164a27ef5d4 --- /dev/null +++ b/tests/codegen-llvm/issues/issue-45466.rs @@ -0,0 +1,14 @@ +//@ compile-flags: -Copt-level=3 + +#![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-llvm/issues/issue-45964-bounds-check-slice-pos.rs b/tests/codegen-llvm/issues/issue-45964-bounds-check-slice-pos.rs new file mode 100644 index 00000000000..a48bb2a1ccf --- /dev/null +++ b/tests/codegen-llvm/issues/issue-45964-bounds-check-slice-pos.rs @@ -0,0 +1,38 @@ +// This test case checks that slice::{r}position functions do not +// prevent optimizing away bounds checks + +//@ compile-flags: -Copt-level=3 + +#![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-llvm/issues/issue-47278.rs b/tests/codegen-llvm/issues/issue-47278.rs new file mode 100644 index 00000000000..4f0a5bdf36f --- /dev/null +++ b/tests/codegen-llvm/issues/issue-47278.rs @@ -0,0 +1,11 @@ +// -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-llvm/issues/issue-47442.rs b/tests/codegen-llvm/issues/issue-47442.rs new file mode 100644 index 00000000000..445234e55ad --- /dev/null +++ b/tests/codegen-llvm/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-llvm/issues/issue-56267-2.rs b/tests/codegen-llvm/issues/issue-56267-2.rs new file mode 100644 index 00000000000..98e3732777e --- /dev/null +++ b/tests/codegen-llvm/issues/issue-56267-2.rs @@ -0,0 +1,18 @@ +//@ compile-flags: -C no-prepopulate-passes + +#![crate_type = "rlib"] + +#[allow(dead_code)] +pub struct Foo<T> { + 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, ptr %{{.+}}, align 4 +#[no_mangle] +pub fn test(x: Foo<(i32, i32)>) -> (i32, i32) { + x.bar +} diff --git a/tests/codegen-llvm/issues/issue-56267.rs b/tests/codegen-llvm/issues/issue-56267.rs new file mode 100644 index 00000000000..cabcc298482 --- /dev/null +++ b/tests/codegen-llvm/issues/issue-56267.rs @@ -0,0 +1,18 @@ +//@ compile-flags: -C no-prepopulate-passes + +#![crate_type = "rlib"] + +#[allow(dead_code)] +pub struct Foo<T> { + 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:%.+]], 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-llvm/issues/issue-56927.rs b/tests/codegen-llvm/issues/issue-56927.rs new file mode 100644 index 00000000000..415ef073e03 --- /dev/null +++ b/tests/codegen-llvm/issues/issue-56927.rs @@ -0,0 +1,46 @@ +//@ compile-flags: -C no-prepopulate-passes +// 32bit MSVC does not align things properly so we suppress high alignment annotations (#112480) +//@ ignore-i686-pc-windows-msvc +//@ ignore-i686-pc-windows-gnu + +#![crate_type = "rlib"] + +#[repr(align(16))] +pub struct S { + arr: [u32; 4], +} + +// CHECK-LABEL: @test1 +// CHECK: store i32 0, ptr %{{.+}}, align 16 +// CHECK: store i32 1, ptr %{{.+}}, align 4 +// CHECK: store i32 2, ptr %{{.+}}, align 8 +// CHECK: store i32 3, 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, 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, ptr %{{.+}}, align 4 +#[no_mangle] +pub fn test3(s: &mut S, i: usize) { + s.arr[i] = 5; +} + +// CHECK-LABEL: @test4 +// CHECK: store i32 6, ptr %{{.+}}, align 4 +#[no_mangle] +pub fn test4(s: &mut S) { + s.arr = [6; 4]; +} diff --git a/tests/codegen-llvm/issues/issue-58881.rs b/tests/codegen-llvm/issues/issue-58881.rs new file mode 100644 index 00000000000..ba6285f3972 --- /dev/null +++ b/tests/codegen-llvm/issues/issue-58881.rs @@ -0,0 +1,20 @@ +//@ compile-flags: -C no-prepopulate-passes -Copt-level=0 +// +//@ only-x86_64 + +#![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 {{.*}}, ptr {{.*}}) + variadic_fn(0, Foo(0), Bar(0, 0, 0)) +} diff --git a/tests/codegen-llvm/issues/issue-59352.rs b/tests/codegen-llvm/issues/issue-59352.rs new file mode 100644 index 00000000000..cb4383d4a30 --- /dev/null +++ b/tests/codegen-llvm/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: -Copt-level=3 -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-llvm/issues/issue-64219-fn-ptr-call-returning-never-is-noreturn.rs b/tests/codegen-llvm/issues/issue-64219-fn-ptr-call-returning-never-is-noreturn.rs new file mode 100644 index 00000000000..86d020e1751 --- /dev/null +++ b/tests/codegen-llvm/issues/issue-64219-fn-ptr-call-returning-never-is-noreturn.rs @@ -0,0 +1,19 @@ +//! Test for https://github.com/rust-lang/rust/issues/64219 +//! Check if `noreturn` attribute is applied on calls to +//! function pointers returning `!` (never type). + +#![crate_type = "lib"] + +extern "C" { + static FOO: fn() -> !; +} + +// CHECK-LABEL: @foo +#[no_mangle] +pub unsafe fn foo() { + // CHECK: call + // CHECK-SAME: [[NUM:#[0-9]+$]] + FOO(); +} + +// CHECK: attributes [[NUM]] = {{{.*}} noreturn {{.*}}} diff --git a/tests/codegen-llvm/issues/issue-68667-unwrap-combinators.rs b/tests/codegen-llvm/issues/issue-68667-unwrap-combinators.rs new file mode 100644 index 00000000000..7f4a32109fe --- /dev/null +++ b/tests/codegen-llvm/issues/issue-68667-unwrap-combinators.rs @@ -0,0 +1,15 @@ +#![crate_type = "lib"] + +//@ compile-flags: -Copt-level=3 + +// MIR inlining now optimizes this code. + +// CHECK-LABEL: @unwrap_combinators +// CHECK: {{icmp|trunc}} +// CHECK-NEXT: icmp +// CHECK-NEXT: select i1 +// CHECK-NEXT: ret i1 +#[no_mangle] +pub fn unwrap_combinators(a: Option<i32>, b: i32) -> bool { + a.map(|t| t >= b).unwrap_or(false) +} diff --git a/tests/codegen-llvm/issues/issue-69101-bounds-check.rs b/tests/codegen-llvm/issues/issue-69101-bounds-check.rs new file mode 100644 index 00000000000..953b79aa263 --- /dev/null +++ b/tests/codegen-llvm/issues/issue-69101-bounds-check.rs @@ -0,0 +1,42 @@ +//@ compile-flags: -Copt-level=3 +#![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-llvm/issues/issue-73031.rs b/tests/codegen-llvm/issues/issue-73031.rs new file mode 100644 index 00000000000..80dea9b5bc2 --- /dev/null +++ b/tests/codegen-llvm/issues/issue-73031.rs @@ -0,0 +1,22 @@ +//@ compile-flags: -Copt-level=3 +#![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-llvm/issues/issue-73258.rs b/tests/codegen-llvm/issues/issue-73258.rs new file mode 100644 index 00000000000..936a7554496 --- /dev/null +++ b/tests/codegen-llvm/issues/issue-73258.rs @@ -0,0 +1,40 @@ +//@ compile-flags: -Copt-level=3 + +#![crate_type = "lib"] + +// Adapted from <https://github.com/rust-lang/rust/issues/73258#issue-637346014> + +#[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<Foo> = Some(ptr.read()); + return k.unwrap(); +} diff --git a/tests/codegen-llvm/issues/issue-73338-effecient-cmp.rs b/tests/codegen-llvm/issues/issue-73338-effecient-cmp.rs new file mode 100644 index 00000000000..71641a5457b --- /dev/null +++ b/tests/codegen-llvm/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-llvm/issues/issue-73396-bounds-check-after-position.rs b/tests/codegen-llvm/issues/issue-73396-bounds-check-after-position.rs new file mode 100644 index 00000000000..1e2c25babe0 --- /dev/null +++ b/tests/codegen-llvm/issues/issue-73396-bounds-check-after-position.rs @@ -0,0 +1,70 @@ +//@ compile-flags: -Copt-level=3 +#![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_start_index_len_fail + // CHECK-NOT: slice_end_index_len_fail + // CHECK-NOT: panic_bounds_check + // CHECK-NOT: unreachable + 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_start_index_len_fail + // CHECK-NOT: slice_end_index_len_fail + // CHECK-NOT: panic_bounds_check + // CHECK-NOT: unreachable + 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_start_index_len_fail + // CHECK-NOT: slice_end_index_len_fail + // CHECK-NOT: panic_bounds_check + // CHECK-NOT: unreachable + 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_start_index_len_fail + // CHECK-NOT: slice_end_index_len_fail + // CHECK-NOT: panic_bounds_check + // CHECK-NOT: unreachable + 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_start_index_len_fail + // CHECK-NOT: slice_end_index_len_fail + // CHECK-NOT: panic_bounds_check + // CHECK-NOT: unreachable + 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_start_index_len_fail + // CHECK-NOT: slice_end_index_len_fail + // CHECK-NOT: panic_bounds_check + // CHECK-NOT: unreachable + if let Some(idx) = s.iter().rposition(|b| *b == b'\\') { s[idx] } else { 42 } +} diff --git a/tests/codegen-llvm/issues/issue-73827-bounds-check-index-in-subexpr.rs b/tests/codegen-llvm/issues/issue-73827-bounds-check-index-in-subexpr.rs new file mode 100644 index 00000000000..e9dd0d1bf23 --- /dev/null +++ b/tests/codegen-llvm/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: -Copt-level=3 + +#![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-llvm/issues/issue-74938-array-split-at.rs b/tests/codegen-llvm/issues/issue-74938-array-split-at.rs new file mode 100644 index 00000000000..9d3e23d642b --- /dev/null +++ b/tests/codegen-llvm/issues/issue-74938-array-split-at.rs @@ -0,0 +1,14 @@ +//@ compile-flags: -Copt-level=3 + +#![crate_type = "lib"] + +const N: usize = 3; +pub type T = u8; + +// CHECK-LABEL: @split_multiple +// CHECK-NOT: unreachable +#[no_mangle] +pub fn split_multiple(slice: &[T]) -> (&[T], &[T]) { + let len = slice.len() / N; + slice.split_at(len * N) +} diff --git a/tests/codegen-llvm/issues/issue-75525-bounds-checks.rs b/tests/codegen-llvm/issues/issue-75525-bounds-checks.rs new file mode 100644 index 00000000000..5dfbd350010 --- /dev/null +++ b/tests/codegen-llvm/issues/issue-75525-bounds-checks.rs @@ -0,0 +1,26 @@ +// Regression test for #75525, verifies that no bounds checks are generated. + +//@ compile-flags: -Copt-level=3 + +#![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-llvm/issues/issue-75546.rs b/tests/codegen-llvm/issues/issue-75546.rs new file mode 100644 index 00000000000..1e1e6543a88 --- /dev/null +++ b/tests/codegen-llvm/issues/issue-75546.rs @@ -0,0 +1,17 @@ +//@ compile-flags: -Copt-level=3 +#![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-llvm/issues/issue-75659.rs b/tests/codegen-llvm/issues/issue-75659.rs new file mode 100644 index 00000000000..0960bfdb6b0 --- /dev/null +++ b/tests/codegen-llvm/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: -Copt-level=3 -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-llvm/issues/issue-75978.rs b/tests/codegen-llvm/issues/issue-75978.rs new file mode 100644 index 00000000000..f4b0bc36329 --- /dev/null +++ b/tests/codegen-llvm/issues/issue-75978.rs @@ -0,0 +1,18 @@ +//@ compile-flags: -Copt-level=3 + +#![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-llvm/issues/issue-77812.rs b/tests/codegen-llvm/issues/issue-77812.rs new file mode 100644 index 00000000000..09e2376c30d --- /dev/null +++ b/tests/codegen-llvm/issues/issue-77812.rs @@ -0,0 +1,32 @@ +//@ compile-flags: -Copt-level=3 +#![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 "C" { + 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-llvm/issues/issue-84268.rs b/tests/codegen-llvm/issues/issue-84268.rs new file mode 100644 index 00000000000..1dc55a909ad --- /dev/null +++ b/tests/codegen-llvm/issues/issue-84268.rs @@ -0,0 +1,18 @@ +//@ compile-flags: -Copt-level=3 --crate-type=rlib +#![feature(core_intrinsics, repr_simd)] + +use std::intrinsics::simd::{simd_eq, simd_fabs}; + +#[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-llvm/issues/issue-85872-multiple-reverse.rs b/tests/codegen-llvm/issues/issue-85872-multiple-reverse.rs new file mode 100644 index 00000000000..6f566ddee6b --- /dev/null +++ b/tests/codegen-llvm/issues/issue-85872-multiple-reverse.rs @@ -0,0 +1,19 @@ +//@ compile-flags: -Copt-level=3 + +#![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-llvm/issues/issue-86106.rs b/tests/codegen-llvm/issues/issue-86106.rs new file mode 100644 index 00000000000..8d1ce116d26 --- /dev/null +++ b/tests/codegen-llvm/issues/issue-86106.rs @@ -0,0 +1,63 @@ +//@ only-64bit llvm appears to use stores instead of memset on 32bit +//@ compile-flags: -C opt-level=3 -Z merge-functions=disabled +//@ needs-deterministic-layouts + +// The below two functions ensure that both `String::new()` and `"".to_string()` +// produce the identical code. + +#![crate_type = "lib"] + +// CHECK-LABEL: define {{(dso_local )?}}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 {{(dso_local )?}}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<u8> { + // 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<u8> { + // 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-llvm/issues/issue-86109-eliminate-div-by-zero-check.rs b/tests/codegen-llvm/issues/issue-86109-eliminate-div-by-zero-check.rs new file mode 100644 index 00000000000..345c09738b6 --- /dev/null +++ b/tests/codegen-llvm/issues/issue-86109-eliminate-div-by-zero-check.rs @@ -0,0 +1,26 @@ +//@ compile-flags: -Copt-level=3 +//! Test for https://github.com/rust-lang/rust/issues/86109 +//! Check LLVM can eliminate the impossible division by zero check by +//! ensuring there is no call (to panic) instruction. +//! +//! This has been fixed since `rustc 1.70.0`. + +#![crate_type = "lib"] + +type T = i16; + +// CHECK-LABEL: @foo +#[no_mangle] +pub fn foo(start: T) -> T { + // CHECK-NOT: panic + if start <= 0 { + return 0; + } + let mut count = 0; + for i in start..10_000 { + if 752 % i != 0 { + count += 1; + } + } + count +} diff --git a/tests/codegen-llvm/issues/issue-93036-assert-index.rs b/tests/codegen-llvm/issues/issue-93036-assert-index.rs new file mode 100644 index 00000000000..46f45c2f06e --- /dev/null +++ b/tests/codegen-llvm/issues/issue-93036-assert-index.rs @@ -0,0 +1,14 @@ +//@ compile-flags: -Copt-level=3 + +#![crate_type = "lib"] + +#[no_mangle] +// CHECK-LABEL: @foo +// CHECK-NOT: unreachable +pub fn foo(arr: &mut [u32]) { + for i in 0..arr.len() { + for j in 0..i { + assert!(j < arr.len()); + } + } +} diff --git a/tests/codegen-llvm/issues/issue-96274.rs b/tests/codegen-llvm/issues/issue-96274.rs new file mode 100644 index 00000000000..2425ec53e4e --- /dev/null +++ b/tests/codegen-llvm/issues/issue-96274.rs @@ -0,0 +1,15 @@ +//@ compile-flags: -Copt-level=3 + +#![crate_type = "lib"] + +use std::mem::MaybeUninit; + +pub fn maybe_uninit() -> [MaybeUninit<u8>; 3000] { + // CHECK-NOT: memset + [MaybeUninit::uninit(); 3000] +} + +pub fn maybe_uninit_const<T>() -> [MaybeUninit<T>; 8192] { + // CHECK-NOT: memset + [const { MaybeUninit::uninit() }; 8192] +} diff --git a/tests/codegen-llvm/issues/issue-96497-slice-size-nowrap.rs b/tests/codegen-llvm/issues/issue-96497-slice-size-nowrap.rs new file mode 100644 index 00000000000..7b3a20a295e --- /dev/null +++ b/tests/codegen-llvm/issues/issue-96497-slice-size-nowrap.rs @@ -0,0 +1,38 @@ +// 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: -Copt-level=3 + +#![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 nuw 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; +} + +// CHECK-LABEL: @slice_size_plus_2 +#[no_mangle] +pub fn slice_size_plus_2(x: &[u16]) -> usize { + // Before #136575 this didn't get the `nuw` in the add. + + // CHECK: [[BYTES:%.+]] = shl nuw nsw {{i16|i32|i64}} %x.1, 1 + // CHECK: = add nuw {{i16|i32|i64}} [[BYTES]], 2 + core::mem::size_of_val(x) + 2 +} diff --git a/tests/codegen-llvm/issues/issue-98294-get-mut-copy-from-slice-opt.rs b/tests/codegen-llvm/issues/issue-98294-get-mut-copy-from-slice-opt.rs new file mode 100644 index 00000000000..76adcf9fd45 --- /dev/null +++ b/tests/codegen-llvm/issues/issue-98294-get-mut-copy-from-slice-opt.rs @@ -0,0 +1,17 @@ +//@ compile-flags: -Copt-level=3 + +#![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-llvm/issues/issue-98678-async.rs b/tests/codegen-llvm/issues/issue-98678-async.rs new file mode 100644 index 00000000000..3dd06bb5194 --- /dev/null +++ b/tests/codegen-llvm/issues/issue-98678-async.rs @@ -0,0 +1,27 @@ +// ignore-tidy-linelength +//! This test verifies the accuracy of emitted file and line debuginfo metadata for async blocks and +//! async functions. + +//@ revisions: MSVC NONMSVC +//@[MSVC] only-msvc +//@[NONMSVC] ignore-msvc +//@ edition:2021 +//@ compile-flags: --crate-type=lib -Copt-level=0 -Cdebuginfo=2 -Zdebug-info-type-line-numbers=true + +// NONMSVC-DAG: ![[#FILE:]] = !DIFile({{.*}}filename:{{.*[/\\]}}issue-98678-async.rs{{".*}}) +// MSVC: ![[#FILE:]] = !DIFile({{.*}}filename:{{.*}}\\issue-98678-async.rs{{".*}}) + +// NONMSVC-DAG: !DISubprogram(name: "foo",{{.*}}file: ![[#FILE]]{{.*}}line: [[# @LINE + 2]], +// MSVC-DAG: !DISubprogram(name: "foo",{{.*}}file: ![[#FILE]]{{.*}}line: [[# @LINE + 1]], +pub async fn foo() -> u8 { + 5 +} + +pub fn bar() -> impl std::future::Future<Output = u8> { + // NONMSVC: !DICompositeType({{.*"}}{async_block_env#0}{{".*}}file: ![[#FILE]]{{.*}}line: [[# @LINE + 2]], + // MSVC-DAG: !DICompositeType({{.*"}}enum2$<issue_98678_async::bar::async_block_env$0>{{".*}}file: ![[#FILE]]{{.*}}line: [[# @LINE + 1]], + async { + let x: u8 = foo().await; + x + 5 + } +} diff --git a/tests/codegen-llvm/issues/issue-98678-closure-coroutine.rs b/tests/codegen-llvm/issues/issue-98678-closure-coroutine.rs new file mode 100644 index 00000000000..8763bcb799d --- /dev/null +++ b/tests/codegen-llvm/issues/issue-98678-closure-coroutine.rs @@ -0,0 +1,25 @@ +// ignore-tidy-linelength +//! This test verifies the accuracy of emitted file and line debuginfo metadata for closures and +//! coroutines. + +#![feature(coroutines, stmt_expr_attributes)] + +//@ revisions: MSVC NONMSVC +//@[MSVC] only-msvc +//@[NONMSVC] ignore-msvc +//@ compile-flags: --crate-type=lib -Copt-level=0 -Cdebuginfo=2 -Zdebug-info-type-line-numbers=true + +// NONMSVC-DAG: ![[#FILE:]] = !DIFile({{.*}}filename:{{.*[/\\]}}issue-98678-closure-coroutine.rs{{".*}}) +// MSVC-DAG: ![[#FILE:]] = !DIFile({{.*}}filename:{{.*}}\\issue-98678-closure-coroutine.rs{{".*}}) + +pub fn foo() { + // NONMSVC-DAG: !DICompositeType({{.*"}}{closure_env#0}{{".*}}file: ![[#FILE]]{{.*}}line: [[# @LINE + 2]], + // MSVC-DAG: !DICompositeType({{.*"}}closure_env$0{{".*}}file: ![[#FILE]]{{.*}}line: [[# @LINE + 1]], + let closure = |x| x; + closure(0); + + // NONMSVC-DAG: !DICompositeType({{.*"[{]}}coroutine_env#1{{[}]".*}}file: ![[#FILE]]{{.*}}line: [[# @LINE + 3]], + // MSVC-DAG: !DICompositeType({{.*".*foo::}}coroutine_env$1>{{".*}}file: ![[#FILE]]{{.*}}line: [[# @LINE + 2]], + let _coroutine = #[coroutine] + || yield 1; +} diff --git a/tests/codegen-llvm/issues/issue-98678-enum.rs b/tests/codegen-llvm/issues/issue-98678-enum.rs new file mode 100644 index 00000000000..87bf8797293 --- /dev/null +++ b/tests/codegen-llvm/issues/issue-98678-enum.rs @@ -0,0 +1,42 @@ +// ignore-tidy-linelength +//! This test verifies the accuracy of emitted file and line debuginfo metadata enums. + +//@ revisions: MSVC NONMSVC +//@[MSVC] only-msvc +//@[NONMSVC] ignore-msvc +//@ compile-flags: --crate-type=lib -Copt-level=0 -Cdebuginfo=2 -Zdebug-info-type-line-numbers=true + +// NONMSVC: ![[#FILE:]] = !DIFile({{.*}}filename:{{.*[/\\]}}issue-98678-enum.rs{{".*}}) +// MSVC: ![[#FILE:]] = !DIFile({{.*}}filename:{{.*}}\\issue-98678-enum.rs{{".*}}) + +// NONMSVC: !DICompositeType({{.*"}}SingleCase{{".*}}file: ![[#FILE]]{{.*}}line: [[# @LINE + 2]], +// MSVC: !DICompositeType({{.*"}}enum2$<issue_98678_enum::SingleCase>{{".*}}file: ![[#FILE]]{{.*}}line: [[# @LINE + 1]], +pub enum SingleCase { + // NONMSVC: !DIDerivedType(tag: DW_TAG_member, name: "One",{{.*}}file: ![[#FILE]]{{.*}}line: [[# @LINE + 2]], + // CHECK: !DICompositeType(tag: DW_TAG_structure_type, name: "One",{{.*}}file: ![[#FILE]]{{.*}}line: [[# @LINE + 1]], + One, +} + +// NONMSVC: !DICompositeType({{.*"}}MultipleDataCases{{".*}}file: ![[#FILE]]{{.*}}line: [[# @LINE + 2]], +// MSVC: !DICompositeType({{.*"}}enum2$<issue_98678_enum::MultipleDataCases>{{".*}}file: ![[#FILE]]{{.*}}line: [[# @LINE + 1]], +pub enum MultipleDataCases { + // NONMSVC: !DIDerivedType(tag: DW_TAG_member, name: "Case1",{{.*}}file: ![[#FILE]]{{.*}}line: [[# @LINE + 2]], + // CHECK: !DICompositeType(tag: DW_TAG_structure_type, name: "Case1",{{.*}}file: ![[#FILE]]{{.*}}line: [[# @LINE + 1]], + Case1(u32), + // NONMSVC: !DIDerivedType(tag: DW_TAG_member, name: "Case2",{{.*}}file: ![[#FILE]]{{.*}}line: [[# @LINE + 2]], + // CHECK: !DICompositeType(tag: DW_TAG_structure_type, name: "Case2",{{.*}}file: ![[#FILE]]{{.*}}line: [[# @LINE + 1]], + Case2(i64), +} + +// NONMSVC: !DICompositeType({{.*"}}NicheLayout{{".*}}file: ![[#FILE]]{{.*}}line: [[# @LINE + 2]], +// MSVC: !DICompositeType({{.*"}}enum2$<issue_98678_enum::NicheLayout>{{".*}}file: ![[#FILE]]{{.*}}line: [[# @LINE + 1]], +pub enum NicheLayout { + // NONMSVC: !DIDerivedType(tag: DW_TAG_member, name: "Something",{{.*}}file: ![[#FILE]]{{.*}}line: [[# @LINE + 2]], + // CHECK: !DICompositeType(tag: DW_TAG_structure_type, name: "Something",{{.*}}file: ![[#FILE]]{{.*}}line: [[# @LINE + 1]], + Something(&'static u32), + // NONMSVC: !DIDerivedType(tag: DW_TAG_member, name: "Nothing",{{.*}}file: ![[#FILE]]{{.*}}line: [[# @LINE + 2]], + // CHECK: !DICompositeType(tag: DW_TAG_structure_type, name: "Nothing",{{.*}}file: ![[#FILE]]{{.*}}line: [[# @LINE + 1]], + Nothing, +} + +pub fn foo(_: SingleCase, _: MultipleDataCases, _: NicheLayout) {} diff --git a/tests/codegen-llvm/issues/issue-98678-struct-union.rs b/tests/codegen-llvm/issues/issue-98678-struct-union.rs new file mode 100644 index 00000000000..a83a585a433 --- /dev/null +++ b/tests/codegen-llvm/issues/issue-98678-struct-union.rs @@ -0,0 +1,27 @@ +// ignore-tidy-linelength +//! This test verifies the accuracy of emitted file and line debuginfo metadata for structs and +//! unions. + +//@ revisions: MSVC NONMSVC +//@[MSVC] only-msvc +//@[NONMSVC] ignore-msvc +//@ compile-flags: --crate-type=lib -Copt-level=0 -Cdebuginfo=2 -Zdebug-info-type-line-numbers=true + +// NONMSVC: ![[#FILE:]] = !DIFile({{.*}}filename:{{.*[/\\]}}issue-98678-struct-union.rs{{".*}}) +// MSVC: ![[#FILE:]] = !DIFile({{.*}}filename:{{.*}}\\issue-98678-struct-union.rs{{".*}}) + +// CHECK: !DICompositeType({{.*"}}MyType{{".*}}file: ![[#FILE]]{{.*}}line: [[# @LINE + 1]], +pub struct MyType { + // CHECK: !DIDerivedType({{.*"}}i{{".*}}file: ![[#FILE]]{{.*}}line: [[# @LINE + 1]], + i: i32, +} + +// CHECK: !DICompositeType({{.*"}}MyUnion{{".*}}file: ![[#FILE]]{{.*}}line: [[# @LINE + 1]], +pub union MyUnion { + // CHECK: !DIDerivedType({{.*"}}i{{".*}}file: ![[#FILE]]{{.*}}line: [[# @LINE + 1]], + i: i32, + // CHECK: !DIDerivedType({{.*"}}f{{".*}}file: ![[#FILE]]{{.*}}line: [[# @LINE + 1]], + f: f32, +} + +pub fn foo(_: MyType, _: MyUnion) {} diff --git a/tests/codegen-llvm/issues/issue-99960.rs b/tests/codegen-llvm/issues/issue-99960.rs new file mode 100644 index 00000000000..571a9be967d --- /dev/null +++ b/tests/codegen-llvm/issues/issue-99960.rs @@ -0,0 +1,10 @@ +//@ compile-flags: -Copt-level=3 + +#![crate_type = "lib"] + +#[no_mangle] +pub fn test(dividend: i64, divisor: i64) -> Option<i64> { + // CHECK-LABEL: @test( + // CHECK-NOT: panic + if dividend > i64::min_value() && divisor != 0 { Some(dividend / divisor) } else { None } +} diff --git a/tests/codegen-llvm/issues/looping-over-ne-bytes-133528.rs b/tests/codegen-llvm/issues/looping-over-ne-bytes-133528.rs new file mode 100644 index 00000000000..35acf765d69 --- /dev/null +++ b/tests/codegen-llvm/issues/looping-over-ne-bytes-133528.rs @@ -0,0 +1,17 @@ +//@ compile-flags: -Copt-level=3 +//@ min-llvm-version: 20 +#![crate_type = "lib"] + +/// Ensure the function is properly optimized +/// In the issue #133528, the function was not getting optimized +/// whereas, a version with `bytes` wrapped into a `black_box` was optimized +/// It was probably a LLVM bug that was fixed in LLVM 20 + +// CHECK-LABEL: @looping_over_ne_bytes +// CHECK: icmp eq i64 %input, -1 +// CHECK-NEXT: ret i1 +#[no_mangle] +fn looping_over_ne_bytes(input: u64) -> bool { + let bytes = input.to_ne_bytes(); + bytes.iter().all(|x| *x == !0) +} diff --git a/tests/codegen-llvm/issues/str-to-string-128690.rs b/tests/codegen-llvm/issues/str-to-string-128690.rs new file mode 100644 index 00000000000..d9e69764be2 --- /dev/null +++ b/tests/codegen-llvm/issues/str-to-string-128690.rs @@ -0,0 +1,38 @@ +//@ compile-flags: -C opt-level=3 -Z merge-functions=disabled +#![crate_type = "lib"] + +//! Make sure str::to_string is specialized not to use fmt machinery. +//! +//! Note that the `CHECK-NOT`s here try to match on calls to functions under `core::fmt`. + +// CHECK-LABEL: define {{(dso_local )?}}void @one_ref +#[no_mangle] +pub fn one_ref(input: &str) -> String { + // CHECK-NOT: {{(call|invoke)}}{{.*}}@{{.*}}core{{.*}}fmt{{.*}} + input.to_string() +} + +// CHECK-LABEL: define {{(dso_local )?}}void @two_ref +#[no_mangle] +pub fn two_ref(input: &&str) -> String { + // CHECK-NOT: {{(call|invoke)}}{{.*}}@{{.*}}core{{.*}}fmt{{.*}} + input.to_string() +} + +// CHECK-LABEL: define {{(dso_local )?}}void @thirteen_ref +#[no_mangle] +pub fn thirteen_ref(input: &&&&&&&&&&&&&str) -> String { + // CHECK-NOT: {{(call|invoke)}}{{.*}}@{{.*}}core{{.*}}fmt{{.*}} + input.to_string() +} + +// This is a known performance cliff because of the macro-generated +// specialized impl. If this test suddenly starts failing, +// consider removing the `to_string_str!` macro in `alloc/str/string.rs`. +// +// CHECK-LABEL: define {{(dso_local )?}}void @fourteen_ref +#[no_mangle] +pub fn fourteen_ref(input: &&&&&&&&&&&&&&str) -> String { + // CHECK: {{(call|invoke)}}{{.*}}@{{.*}}core{{.*}}fmt{{.*}} + input.to_string() +} |
