Issue 239508
Summary [LoongArch] Misaligned frame-index offset on `vldrepl.{h,w,d}` loads the wrong address
Labels miscompilation, backend:loongarch
Assignees
Reporter heiher
    ## Summary 
 
When `vldrepl.h`, `vldrepl.w` or `vldrepl.d` is selected with a frame-index base, `LoongArchRegisterInfo::eliminateFrameIndex` does not check that the final offset is a multiple of the access size. A byte-aligned stack object can produce a misaligned offset, which these instructions cannot encode.
 
## Reproducer

`repro.ll`:

```llvm
declare void @use(ptr)

define <8 x i16> @repl_h() noinline "frame-pointer"="all" nounwind {
  %a = alloca [65 x i8], align 1
  call void @use(ptr %a)
  %p = getelementptr i8, ptr %a, i64 16
  %v = load i16, ptr %p, align 1
  %i = insertelement <8 x i16> poison, i16 %v, i64 0
  %s = shufflevector <8 x i16> %i, <8 x i16> poison, <8 x i32> zeroinitializer
  ret <8 x i16> %s
}

define <4 x i32> @repl_w() noinline "frame-pointer"="all" nounwind {
  %a = alloca [66 x i8], align 1
  call void @use(ptr %a)
  %p = getelementptr i8, ptr %a, i64 16
  %v = load i32, ptr %p, align 1
  %i = insertelement <4 x i32> poison, i32 %v, i64 0
  %s = shufflevector <4 x i32> %i, <4 x i32> poison, <4 x i32> zeroinitializer
  ret <4 x i32> %s
}

define <2 x i64> @repl_d() noinline "frame-pointer"="all" nounwind {
  %a = alloca [67 x i8], align 1
  call void @use(ptr %a)
  %p = getelementptr i8, ptr %a, i64 16
  %v = load i64, ptr %p, align 1
  %i = insertelement <2 x i64> poison, i64 %v, i64 0
  %s = shufflevector <2 x i64> %i, <2 x i64> poison, <2 x i32> zeroinitializer
  ret <2 x i64> %s
}
```

### Invalid assembly

```console
$ llc -mtriple=loongarch64 -mattr=+lsx,+ual -O2 repro.ll -o repro.s
$ grep vldrepl repro.s
	vldrepl.h	$vr0, $fp, -73
	vldrepl.w	$vr0, $fp, -74
	vldrepl.d	$vr0, $fp, -75
$ llvm-mc -triple=loongarch64 -mattr=+lsx -filetype=null repro.s
error: immediate must be a multiple of 2 in the range [-2048, 2046]
error: immediate must be a multiple of 4 in the range [-2048, 2044]
error: immediate must be a multiple of 8 in the range [-2048, 2040]
```
_______________________________________________
llvm-bugs mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs

Reply via email to