https://github.com/heiher created 
https://github.com/llvm/llvm-project/pull/239516

eliminateFrameIndex did not check that the final offset is a multiple of the 
access size for [X]VLDREPL_{H,W,D}, so a byte-aligned stack object could 
produce an unencodable immediate. Materialize the address in a scratch register 
in that case, as is already done for [X]VSTELM_*.

Assisted-by: LLM

Fixes #239508

>From 47d998aafd7d1d51b86e65282dc2105aded6a2e1 Mon Sep 17 00:00:00 2001
From: WANG Rui <[email protected]>
Date: Fri, 6 Nov 2026 16:03:36 +0800
Subject: [PATCH] [LoongArch] Fix misaligned frame-index offset for
 vldrepl.{h,w,d}

eliminateFrameIndex did not check that the final offset is a multiple of
the access size for [X]VLDREPL_{H,W,D}, so a byte-aligned stack object
could produce an unencodable immediate. Materialize the address in a
scratch register in that case, as is already done for [X]VSTELM_*.

Assisted-by: LLM
---
 llvm/lib/Target/LoongArch/LoongArchRegisterInfo.cpp  | 12 ++++++++++++
 .../CodeGen/LoongArch/lasx/xvldrepl-frame-index.ll   |  9 ++++++---
 .../CodeGen/LoongArch/lsx/vldrepl-frame-index.ll     |  9 ++++++---
 3 files changed, 24 insertions(+), 6 deletions(-)

diff --git a/llvm/lib/Target/LoongArch/LoongArchRegisterInfo.cpp 
b/llvm/lib/Target/LoongArch/LoongArchRegisterInfo.cpp
index 682a147d946783..392cbd0d0f0abb 100644
--- a/llvm/lib/Target/LoongArch/LoongArchRegisterInfo.cpp
+++ b/llvm/lib/Target/LoongArch/LoongArchRegisterInfo.cpp
@@ -202,6 +202,18 @@ bool 
LoongArchRegisterInfo::eliminateFrameIndex(MachineBasicBlock::iterator II,
 
   // Handle offsets that exceed the immediate range of the instruction.
   switch (MIOpc) {
+  case LoongArch::VLDREPL_H:
+  case LoongArch::XVLDREPL_H:
+    OffsetLegal = isShiftedInt<11, 1>(FixedOffset);
+    break;
+  case LoongArch::VLDREPL_W:
+  case LoongArch::XVLDREPL_W:
+    OffsetLegal = isShiftedInt<10, 2>(FixedOffset);
+    break;
+  case LoongArch::VLDREPL_D:
+  case LoongArch::XVLDREPL_D:
+    OffsetLegal = isShiftedInt<9, 3>(FixedOffset);
+    break;
   case LoongArch::VSTELM_B:
   case LoongArch::XVSTELM_B:
     OffsetLegal = isInt<8>(FixedOffset);
diff --git a/llvm/test/CodeGen/LoongArch/lasx/xvldrepl-frame-index.ll 
b/llvm/test/CodeGen/LoongArch/lasx/xvldrepl-frame-index.ll
index c8bbb89d8cb5be..efbc7a495708bd 100644
--- a/llvm/test/CodeGen/LoongArch/lasx/xvldrepl-frame-index.ll
+++ b/llvm/test/CodeGen/LoongArch/lasx/xvldrepl-frame-index.ll
@@ -13,7 +13,8 @@ define <16 x i16> @repl_h() noinline nounwind 
"frame-pointer"="all" {
 ; CHECK-NEXT:    addi.d $a0, $fp, -89
 ; CHECK-NEXT:    pcaddu18i $ra, %call36(use)
 ; CHECK-NEXT:    jirl $ra, $ra, 0
-; CHECK-NEXT:    xvldrepl.h $xr0, $fp, -73
+; CHECK-NEXT:    addi.d $a0, $fp, -73
+; CHECK-NEXT:    xvldrepl.h $xr0, $a0, 0
 ; CHECK-NEXT:    ld.d $fp, $sp, 80 # 8-byte Folded Reload
 ; CHECK-NEXT:    ld.d $ra, $sp, 88 # 8-byte Folded Reload
 ; CHECK-NEXT:    addi.d $sp, $sp, 96
@@ -37,7 +38,8 @@ define <8 x i32> @repl_w() noinline nounwind 
"frame-pointer"="all" {
 ; CHECK-NEXT:    addi.d $a0, $fp, -90
 ; CHECK-NEXT:    pcaddu18i $ra, %call36(use)
 ; CHECK-NEXT:    jirl $ra, $ra, 0
-; CHECK-NEXT:    xvldrepl.w $xr0, $fp, -74
+; CHECK-NEXT:    addi.d $a0, $fp, -74
+; CHECK-NEXT:    xvldrepl.w $xr0, $a0, 0
 ; CHECK-NEXT:    ld.d $fp, $sp, 80 # 8-byte Folded Reload
 ; CHECK-NEXT:    ld.d $ra, $sp, 88 # 8-byte Folded Reload
 ; CHECK-NEXT:    addi.d $sp, $sp, 96
@@ -61,7 +63,8 @@ define <4 x i64> @repl_d() noinline nounwind 
"frame-pointer"="all" {
 ; CHECK-NEXT:    addi.d $a0, $fp, -91
 ; CHECK-NEXT:    pcaddu18i $ra, %call36(use)
 ; CHECK-NEXT:    jirl $ra, $ra, 0
-; CHECK-NEXT:    xvldrepl.d $xr0, $fp, -75
+; CHECK-NEXT:    addi.d $a0, $fp, -75
+; CHECK-NEXT:    xvldrepl.d $xr0, $a0, 0
 ; CHECK-NEXT:    ld.d $fp, $sp, 80 # 8-byte Folded Reload
 ; CHECK-NEXT:    ld.d $ra, $sp, 88 # 8-byte Folded Reload
 ; CHECK-NEXT:    addi.d $sp, $sp, 96
diff --git a/llvm/test/CodeGen/LoongArch/lsx/vldrepl-frame-index.ll 
b/llvm/test/CodeGen/LoongArch/lsx/vldrepl-frame-index.ll
index cc49fb96550be6..d4aecb0ffc95d0 100644
--- a/llvm/test/CodeGen/LoongArch/lsx/vldrepl-frame-index.ll
+++ b/llvm/test/CodeGen/LoongArch/lsx/vldrepl-frame-index.ll
@@ -13,7 +13,8 @@ define <8 x i16> @repl_h() noinline nounwind 
"frame-pointer"="all" {
 ; CHECK-NEXT:    addi.d $a0, $fp, -89
 ; CHECK-NEXT:    pcaddu18i $ra, %call36(use)
 ; CHECK-NEXT:    jirl $ra, $ra, 0
-; CHECK-NEXT:    vldrepl.h $vr0, $fp, -73
+; CHECK-NEXT:    addi.d $a0, $fp, -73
+; CHECK-NEXT:    vldrepl.h $vr0, $a0, 0
 ; CHECK-NEXT:    ld.d $fp, $sp, 80 # 8-byte Folded Reload
 ; CHECK-NEXT:    ld.d $ra, $sp, 88 # 8-byte Folded Reload
 ; CHECK-NEXT:    addi.d $sp, $sp, 96
@@ -37,7 +38,8 @@ define <4 x i32> @repl_w() noinline nounwind 
"frame-pointer"="all" {
 ; CHECK-NEXT:    addi.d $a0, $fp, -90
 ; CHECK-NEXT:    pcaddu18i $ra, %call36(use)
 ; CHECK-NEXT:    jirl $ra, $ra, 0
-; CHECK-NEXT:    vldrepl.w $vr0, $fp, -74
+; CHECK-NEXT:    addi.d $a0, $fp, -74
+; CHECK-NEXT:    vldrepl.w $vr0, $a0, 0
 ; CHECK-NEXT:    ld.d $fp, $sp, 80 # 8-byte Folded Reload
 ; CHECK-NEXT:    ld.d $ra, $sp, 88 # 8-byte Folded Reload
 ; CHECK-NEXT:    addi.d $sp, $sp, 96
@@ -61,7 +63,8 @@ define <2 x i64> @repl_d() noinline nounwind 
"frame-pointer"="all" {
 ; CHECK-NEXT:    addi.d $a0, $fp, -91
 ; CHECK-NEXT:    pcaddu18i $ra, %call36(use)
 ; CHECK-NEXT:    jirl $ra, $ra, 0
-; CHECK-NEXT:    vldrepl.d $vr0, $fp, -75
+; CHECK-NEXT:    addi.d $a0, $fp, -75
+; CHECK-NEXT:    vldrepl.d $vr0, $a0, 0
 ; CHECK-NEXT:    ld.d $fp, $sp, 80 # 8-byte Folded Reload
 ; CHECK-NEXT:    ld.d $ra, $sp, 88 # 8-byte Folded Reload
 ; CHECK-NEXT:    addi.d $sp, $sp, 96

_______________________________________________
llvm-branch-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-branch-commits

Reply via email to