Issue 136658
Summary [RISCV] Miscompile when casting tvv type to pointer
Labels new issue
Assignees
Reporter zhangjunphy
    commit: 5e834b9ec7fc85072ec12de780d3c793c03b7d00
godbolt: https://godbolt.org/z/cr6445nqh

This code, after compiled with `-O2 -march=rv64gv`, the `__riscv_vle32_v_f32m2` call in function `f` is gone.
```c++
#include <riscv_vector.h>
#include <stdio.h>

void print(uint16_t* data, int vl) {
  for (int i = 0; i < vl; ++i) {
 printf("%d\n", data[i]);
  }
}

void f(const float *p) {
 vfloat32m2_t v = __riscv_vle32_v_f32m2(p, 1024);
  print((uint16_t*)(&v), 1024);
}
```

produced ir:
```
@.str = private unnamed_addr constant [4 x i8] c"%d\0A\00", align 1

define dso_local void @print(unsigned short*, int)(ptr noundef readonly captures(none) %data, i32 noundef signext %vl) local_unnamed_addr {
entry:
  %cmp3 = icmp sgt i32 %vl, 0
  br i1 %cmp3, label %for.body.preheader, label %for.cond.cleanup

for.body.preheader:
  %wide.trip.count = zext nneg i32 %vl to i64
  br label %for.body

for.cond.cleanup:
  ret void

for.body:
  %indvars.iv = phi i64 [ 0, %for.body.preheader ], [ %indvars.iv.next, %for.body ]
  %arrayidx = getelementptr inbounds nuw i16, ptr %data, i64 %indvars.iv
  %0 = load i16, ptr %arrayidx, align 2
  %conv = zext i16 %0 to i32
  %call = tail call signext i32 (ptr, ...) @printf(ptr noundef nonnull dereferenceable(1) @.str, i32 noundef signext %conv)
 %indvars.iv.next = add nuw nsw i64 %indvars.iv, 1
  %exitcond.not = icmp eq i64 %indvars.iv.next, %wide.trip.count
  br i1 %exitcond.not, label %for.cond.cleanup, label %for.body
}

declare void @llvm.lifetime.start.p0(i64 immarg, ptr captures(none)) #1

declare noundef signext i32 @printf(ptr noundef readonly captures(none), ...) local_unnamed_addr #2

declare void @llvm.lifetime.end.p0(i64 immarg, ptr captures(none)) #1

define dso_local void @f(float const*)(ptr noundef readonly captures(none) %p) local_unnamed_addr {
entry:
  %v = alloca <vscale x 4 x float>, align 4
  call void @llvm.lifetime.start.p0(i64 -1, ptr nonnull %v) #3
  br label %for.body.i

for.body.i:
  %indvars.iv.i = phi i64 [ 0, %entry ], [ %indvars.iv.next.i, %for.body.i ]
  %arrayidx.i = getelementptr inbounds nuw i16, ptr %v, i64 %indvars.iv.i
  %0 = load i16, ptr %arrayidx.i, align 2
  %conv.i = zext i16 %0 to i32
  %call.i = tail call signext i32 (ptr, ...) @printf(ptr noundef nonnull dereferenceable(1) @.str, i32 noundef signext %conv.i)
  %indvars.iv.next.i = add nuw nsw i64 %indvars.iv.i, 1
  %exitcond.not.i = icmp eq i64 %indvars.iv.next.i, 1024
 br i1 %exitcond.not.i, label %print(unsigned short*, int).exit, label %for.body.i

print(unsigned short*, int).exit:
  call void @llvm.lifetime.end.p0(i64 -1, ptr nonnull %v) #3
  ret void
}
```
_______________________________________________
llvm-bugs mailing list
llvm-bugs@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs

Reply via email to