Issue 203921
Summary sveorv_s8 intrinsic call optimised out incorrectly
Labels
Assignees
Reporter davidtwco
    LLVM is optimising away a call to `sveorv_s8` (more specifically, the Rust wrapper around it) and replacing it with a constant zero. This is happening in our testing of Rust's SVE intrinsics where we compare the output with the same inputs against a C FFI call to a Clang-compiled wrapper for the same intrinsic, which was not returning zero, so I'm pretty sure that this optimisation isn't correct. 

```llvm
; call core::core_arch::aarch64::sve::generated::sveorv_s8
%2 = call i8 @_RNvNtNtNtNtCsf377yN02RLm_4core9core_arch7aarch643sve9generated9sveorv_s8CsexjTo5JmmeX_3foo(<vscale x 16 x i1> %_10, <vscale x 16
store i8 %2, ptr %__rust_return_value, align 1
```

```llvm
  store i8 0, ptr %__rust_return_value, align 4
```

When I was running `rustc`, I was able to use `-opt-bisect-limit` to narrow it down to `early-cse`, but couldn't work out how to reproduce it with that specific pass when using `opt`. 

<details><summary>Original Rust source</summary><p>

Compiled using `rustc +nightly foo.rs -Ctarget-feature=+sve --emit llvm-ir -Copt-level=3 -o -`:

```rust
#![feature(stdarch_aarch64_sve)]
use std::arch::aarch64::*;

static I8_275: [u8; 275] = [
    0x0, 0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7, 0x8, 0x9, 0xa, 0xb, 0xc, 0xd, 0xe, 0xf, 0x10, 0x11,
    0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, 0x20, 0x21,
    0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f, 0x30, 0x31,
    0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x3a, 0x3b, 0x3c, 0x3d, 0x3e, 0x3f, 0x40, 0x41,
    0x42, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 0x49, 0x4a, 0x4b, 0x4c, 0x4d, 0x4e, 0x4f, 0x50, 0x51,
    0x52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0x7, 0x8, 0x9, 0xa, 0xb, 0xc, 0xd, 0xe, 0xf, 0x10, 0x11, 0x12,
];

extern "C" {
    fn sveorv_s8_wrapper(__dst: *mut i8, pg: *const svbool_t, op: *const svint8_t);
}

fn main() {
    unsafe {
        let __pred = svptrue_b8();
        let op_val = svld1_s8(__pred, I8_275.as_ptr().add(0) as _);
        let __rust_return_value = sveorv_s8(__pred, op_val);

        let mut __c_return_value = std::mem::MaybeUninit::uninit();
        sveorv_s8_wrapper(
 __c_return_value.as_mut_ptr(),
            &raw const __pred,
 &raw const op_val,
        );
        let __c_return_value = __c_return_value.assume_init();

        assert_eq!(__rust_return_value, __c_return_value);
    }
}
```
</p></details> 

<details><summary>Unoptimised LLVM IR</summary><p>

```llvm
; ModuleID = 'foo.a95393f5593190f9-cgu.0'
source_filename = "foo.a95393f5593190f9-cgu.0"
target datalayout = "e-m:e-p270:32:32-p271:32:32-p272:64:64-i8:8:32-i16:16:32-i64:64-i128:128-n32:64-S128-Fn32"
target triple = "aarch64-unknown-linux-gnu"

@_RNvCsexjTo5JmmeX_3foo6I8_275 = internal constant [275 x i8] c"\00\01\02\03\04\05\06\07\08\09\0A\0B\0C\0D\0E\0F\10\11\12\13\14\15\16\17\18\19\1A
@vtable.0 = private unnamed_addr constant <{ [24 x i8], ptr }> <{ [24 x i8] c"\00\00\00\00\00\00\00\00\08\00\00\00\00\00\00\00\08\00\00\00\00\00\
@vtable.1 = private unnamed_addr constant <{ [24 x i8], ptr, ptr, ptr }> <{ [24 x i8] c"\00\00\00\00\00\00\00\00\08\00\00\00\00\00\00\00\08\00\00
@alloc_9d77294945a8bc75524d9a9f296b847d = private unnamed_addr constant [7 x i8] c"foo.rs\00", align 1
@alloc_3a32ff7c63a7be90aee43350758d408d = private unnamed_addr constant <{ ptr, [16 x i8] }> <{ ptr @alloc_9d77294945a8bc75524d9a9f296b847d, [16
@alloc_1f3d73bee937d603c824f5da25820532 = private unnamed_addr constant <{ ptr, [16 x i8] }> <{ ptr @alloc_9d77294945a8bc75524d9a9f296b847d, [16
@alloc_d0b1ec05466ad28efe31e29bc53d3848 = private unnamed_addr constant <{ ptr, [16 x i8] }> <{ ptr @alloc_9d77294945a8bc75524d9a9f296b847d, [16
@alloc_fad0cd83b7d1858a846a172eb260e593 = private unnamed_addr constant [42 x i8] c"is_aligned_to: align is not a power-of-two", align 1
@alloc_462d48c71ecbcc11b125140bcb84ed12 = private unnamed_addr constant [34 x i8] c"library/core/src/ptr/const_ptr.rs\00", align 1
@alloc_8279c84e0b07e9461fc64e474bce4e46 = private unnamed_addr constant <{ ptr, [16 x i8] }> <{ ptr @alloc_462d48c71ecbcc11b125140bcb84ed12, [16
@alloc_4fb4eca1f8b9d0ded0407faa6b2654bb = private unnamed_addr constant [214 x i8] c"unsafe precondition(s) violated: ptr::add requires that the
@alloc_ed8641ebea8e5515740d4eb49a916ff5 = private unnamed_addr constant [218 x i8] c"unsafe precondition(s) violated: ptr::read requires that the

; core::panicking::assert_failed::<i8, i8>
; Function Attrs: cold minsize noinline noreturn optsize uwtable
define internal void @_RINvNtCsf377yN02RLm_4core9panicking13assert_failedaaECsexjTo5JmmeX_3foo(i8 %kind, ptr %0, ptr %1, ptr %args.0, ptr %args.1
start:
  %right = alloca [8 x i8], align 8
  %left = alloca [8 x i8], align 8
  store ptr %0, ptr %left, align 8
  store ptr %1, ptr %right, align 8
; call core::panicking::assert_failed_inner
  call void @_RNvNtCsf377yN02RLm_4core9panicking19assert_failed_inner(i8 %kind, ptr %left, ptr align 8 @vtable.0, ptr %right, ptr align 8 @vtable
 unreachable
}

; std::rt::lang_start::<()>
; Function Attrs: uwtable
define hidden i64 @_RINvNtCsiWfa7QTmrCY_3std2rt10lang_startuECsexjTo5JmmeX_3foo(ptr %main, i64 %argc, ptr %argv, i8 %sigpipe) unnamed_addr #1 {
start:
  %_7 = alloca [8 x i8], align 8
  store ptr %main, ptr %_7, align 8
; call std::rt::lang_start_internal
  %_0 = call i64 @_RNvNtCsiWfa7QTmrCY_3std2rt19lang_start_internal(ptr %_7, ptr align 8 @vtable.1, i64 %argc, ptr %argv, i8 %sigpipe)
  ret i64 %_0
}

; std::sys::backtrace::__rust_begin_short_backtrace::<fn(), ()>
; Function Attrs: noinline uwtable
define internal void @_RINvNtNtCsiWfa7QTmrCY_3std3sys9backtrace28___rust_begin_short_backtraceFEuuECsexjTo5JmmeX_3foo(ptr %f) unnamed_addr #2 {
start:
  %_2 = alloca [0 x i8], align 1
; call <fn() as core::ops::function::FnOnce<()>>::call_once
  call void @_RNvYFEuINtNtNtCsf377yN02RLm_4core3ops8function6FnOnceuE9call_onceCsexjTo5JmmeX_3foo(ptr %f) #17
  call void asm sideeffect "", "~{memory}"(), !srcloc !5
  ret void
}

; std::rt::lang_start::<()>::{closure#0}
; Function Attrs: inlinehint uwtable
define internal i32 @_RNCINvNtCsiWfa7QTmrCY_3std2rt10lang_startuE0CsexjTo5JmmeX_3foo(ptr align 8 %_1) unnamed_addr #3 {
start:
  %_4 = load ptr, ptr %_1, align 8
; call std::sys::backtrace::__rust_begin_short_backtrace::<fn(), ()>
  call void @_RINvNtNtCsiWfa7QTmrCY_3std3sys9backtrace28___rust_begin_short_backtraceFEuuECsexjTo5JmmeX_3foo(ptr %_4) #18
; call <() as std::process::Termination>::report
  %self = call i8 @_RNvXsU_NtCsiWfa7QTmrCY_3std7processuNtB5_11Termination6reportCsexjTo5JmmeX_3foo() #17
  %_0 = zext i8 %self to i32
  ret i32 %_0
}

; <std::rt::lang_start<()>::{closure#0} as core::ops::function::FnOnce<()>>::call_once::{shim:vtable#0}
; Function Attrs: inlinehint uwtable
define internal i32 @_RNSNvYNCINvNtCsiWfa7QTmrCY_3std2rt10lang_startuE0INtNtNtCsf377yN02RLm_4core3ops8function6FnOnceuE9call_once6vtableCsexjTo5J
start:
 %_2 = alloca [0 x i8], align 1
  %0 = load ptr, ptr %_1, align 8
; call <std::rt::lang_start<()>::{closure#0} as core::ops::function::FnOnce<()>>::call_once
  %_0 = call i32 @_RNvYNCINvNtCsiWfa7QTmrCY_3std2rt10lang_startuE0INtNtNtCsf377yN02RLm_4core3ops8function6FnOnceuE9call_onceCsexjTo5JmmeX_3foo(pt
 ret i32 %_0
}

; foo::main
; Function Attrs: uwtable
define hidden void @_RNvCsexjTo5JmmeX_3foo4main() unnamed_addr #1 {
start:
  %self.i = alloca [1 x i8], align 1
  %__c_return_value1 = alloca [1 x i8], align 1
 %__c_return_value = alloca [1 x i8], align 1
  %__rust_return_value = alloca [1 x i8], align 1
  %op_val = alloca <vscale x 16 x i8>, align 16
  %__pred = alloca <vscale x 16 x i1>, align 16
; call core::core_arch::aarch64::sve::generated::svptrue_b8
  %0 = call <vscale x 16 x i1> @_RNvNtNtNtNtCsf377yN02RLm_4core9core_arch7aarch643sve9generated10svptrue_b8CsexjTo5JmmeX_3foo() #17
  store <vscale x 16 x i1> %0, ptr %__pred, align 16
  %_3 = load <vscale x 16 x i1>, ptr %__pred, align 16
; call <*const _>::add::precondition_check
  call void @_RNvNvMNtNtCsf377yN02RLm_4core3ptr9const_ptrPp3add18precondition_checkCsexjTo5JmmeX_3foo(ptr @_RNvCsexjTo5JmmeX_3foo6I8_275, i64 0,
; call core::core_arch::aarch64::sve::generated::svld1_s8
  %1 = call <vscale x 16 x i8> @_RNvNtNtNtNtCsf377yN02RLm_4core9core_arch7aarch643sve9generated8svld1_s8CsexjTo5JmmeX_3foo(<vscale x 16 x i1> %_3
  store <vscale x 16 x i8> %1, ptr %op_val, align 16
  %_10 = load <vscale x 16 x i1>, ptr %__pred, align 16
  %_11 = load <vscale x 16 x i8>, ptr %op_val, align 16
; call core::core_arch::aarch64::sve::generated::sveorv_s8
  %2 = call i8 @_RNvNtNtNtNtCsf377yN02RLm_4core9core_arch7aarch643sve9generated9sveorv_s8CsexjTo5JmmeX_3foo(<vscale x 16 x i1> %_10, <vscale x 16
  store i8 %2, ptr %__rust_return_value, align 1
  store i8 undef, ptr %__c_return_value, align 1
  call void @sveorv_s8_wrapper(ptr %__c_return_value, ptr %__pred, ptr %op_val) #20
 %_19 = load i8, ptr %__c_return_value, align 1
  store i8 %_19, ptr %self.i, align 1
; call core::ptr::read::precondition_check
  call void @_RNvNvNtCsf377yN02RLm_4core3ptr4read18precondition_checkCsexjTo5JmmeX_3foo(ptr %self.i, i64 1, i1 zeroext false, ptr align 8 @alloc_
  %_0.i = load i8, ptr %self.i, align 1
  store i8 %_0.i, ptr %__c_return_value1, align 1
  %_26 = load i8, ptr %__rust_return_value, align 1
  %_27 = load i8, ptr %__c_return_value1, align 1
  %_25 = icmp eq i8 %_26, %_27
  br i1 %_25, label %bb10, label %bb11

bb11: ; preds = %start
; call core::panicking::assert_failed::<i8, i8>
  call void @_RINvNtCsf377yN02RLm_4core9panicking13assert_failedaaECsexjTo5JmmeX_3foo(i8 0, ptr %__rust_return_value, ptr %__c_return_value1, ptr
 unreachable

bb10:                                             ; preds = %start
  ret void
}

; <*const ()>::is_aligned_to
; Function Attrs: inlinehint uwtable
define internal zeroext i1 @_RNvMNtNtCsf377yN02RLm_4core3ptr9const_ptrPu13is_aligned_toCsexjTo5JmmeX_3foo(ptr %self, i64 %align) unnamed_addr #3
start:
  %0 = call i64 @llvm.ctpop.i64(i64 %align)
  %_8 = trunc i64 %0 to i32
  %1 = icmp eq i32 %_8, 1
  br i1 %1, label %bb1, label %bb2

bb1: ; preds = %start
  %_6 = ptrtoint ptr %self to i64
  %_7 = sub i64 %align, 1
  %_5 = and i64 %_6, %_7
  %_0 = icmp eq i64 %_5, 0
 ret i1 %_0

bb2:                                              ; preds = %start
; call core::panicking::panic_fmt
  call void @_RNvNtCsf377yN02RLm_4core9panicking9panic_fmt(ptr @alloc_fad0cd83b7d1858a846a172eb260e593, ptr inttoptr (i64 85 to ptr), ptr align 8
  unreachable
}

; core::intrinsics::cold_path
; Function Attrs: cold nounwind uwtable
define internal void @_RNvNtCsf377yN02RLm_4core10intrinsics9cold_pathCsexjTo5JmmeX_3foo() unnamed_addr #4 {
start:
  ret void
}

; core::core_arch::aarch64::sve::generated::svptrue_b8
; Function Attrs: inlinehint uwtable
define internal <vscale x 16 x i1> @_RNvNtNtNtNtCsf377yN02RLm_4core9core_arch7aarch643sve9generated10svptrue_b8CsexjTo5JmmeX_3foo() unnamed_addr
start:
  %_0 = call <vscale x 16 x i1> @llvm.aarch64.sve.ptrue.nxv16i1(i32 31)
  ret <vscale x 16 x i1> %_0
}

; core::core_arch::aarch64::sve::generated::svld1_s8
; Function Attrs: inlinehint uwtable
define internal <vscale x 16 x i8> @_RNvNtNtNtNtCsf377yN02RLm_4core9core_arch7aarch643sve9generated8svld1_s8CsexjTo5JmmeX_3foo(<vscale x 16 x i1>
start:
  %_0 = call <vscale x 16 x i8> @llvm.aarch64.sve.ld1.nxv16i8(<vscale x 16 x i1> %pg, ptr %base)
  ret <vscale x 16 x i8> %_0
}

; core::core_arch::aarch64::sve::generated::sveorv_s8
; Function Attrs: inlinehint uwtable
define internal i8 @_RNvNtNtNtNtCsf377yN02RLm_4core9core_arch7aarch643sve9generated9sveorv_s8CsexjTo5JmmeX_3foo(<vscale x 16 x i1> %pg, <vscale x
start:
  %_0 = call i8 @llvm.aarch64.sve.eorv.nxv16i8(<vscale x 16 x i1> %pg, <vscale x 16 x i8> %op)
  ret i8 %_0
}

; <*const _>::add::precondition_check
; Function Attrs: inlinehint nounwind uwtable
define internal void @_RNvNvMNtNtCsf377yN02RLm_4core3ptr9const_ptrPp3add18precondition_checkCsexjTo5JmmeX_3foo(ptr %this, i64 %count, i64 %size,
start:
  %1 = call { i64, i1 } @llvm.umul.with.overflow.i64(i64 %count, i64 %size)
  %_12.0 = extractvalue { i64, i1 } %1, 0
  %_12.1 = extractvalue { i64, i1 } %1, 1
  br i1 %_12.1, label %bb5, label %bb7

bb7:                                              ; preds = %start
  %self = ptrtoint ptr %this to i64
  %_18.0 = add i64 %self, %_12.0
  %_18.1 = icmp ult i64 %_18.0, %self
  %_9 = icmp ule i64 %_12.0, 9223372036854775807
  br i1 %_9, label %bb3, label %bb4

bb5: ; preds = %start
  br label %bb8

bb4:                                              ; preds = %bb7
 br label %bb8

bb3:                                              ; preds = %bb7
  %_4 = xor i1 %_18.1, true
  br i1 %_4, label %bb1, label %bb2

bb8:                                              ; preds = %bb5, %bb4
  br label %bb2

bb2:                                              ; preds = %bb8, %bb3
; call core::panicking::panic_nounwind_fmt
  call void @_RNvNtCsf377yN02RLm_4core9panicking18panic_nounwind_fmt(ptr @alloc_4fb4eca1f8b9d0ded0407faa6b2654bb, ptr inttoptr (i64 429 to ptr),
 unreachable

bb1:                                              ; preds = %bb3
  ret void
}

; core::ptr::read::precondition_check
; Function Attrs: inlinehint nounwind uwtable
define internal void @_RNvNvNtCsf377yN02RLm_4core3ptr4read18precondition_checkCsexjTo5JmmeX_3foo(ptr %addr, i64 %align, i1 zeroext %is_zst, ptr a
start:
; invoke <*const ()>::is_aligned_to
  %_8 = invoke zeroext i1 @_RNvMNtNtCsf377yN02RLm_4core3ptr9const_ptrPu13is_aligned_toCsexjTo5JmmeX_3foo(ptr %addr, i64 %align)
          to label %bb7 unwind label %terminate

terminate:                                        ; preds = %start
  %1 = landingpad { ptr, i32 }
          filter [0 x ptr] zeroinitializer
; call core::panicking::panic<truncated>Please see the issue for the entire body.
_______________________________________________
llvm-bugs mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs

Reply via email to