Skip to content

[LoongArch] Strengthen stack size estimation for LSX/LASX extension #146455

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Jul 18, 2025

Conversation

tangaac
Copy link
Contributor

@tangaac tangaac commented Jul 1, 2025

This patch adds an emergency spill slot when ran out of registers.
PR #139201 introduces vstelm instructions with only 8-bit imm offset,
it causes no spill slot to store the spill registers.

@llvmbot
Copy link
Member

llvmbot commented Jul 1, 2025

@llvm/pr-subscribers-backend-loongarch

Author: None (tangaac)

Changes

Full diff: https://github.com/llvm/llvm-project/pull/146455.diff

2 Files Affected:

  • (modified) llvm/lib/Target/LoongArch/LoongArchRegisterInfo.cpp (+12-1)
  • (modified) llvm/lib/Target/LoongArch/LoongArchRegisterInfo.h (+5)
diff --git a/llvm/lib/Target/LoongArch/LoongArchRegisterInfo.cpp b/llvm/lib/Target/LoongArch/LoongArchRegisterInfo.cpp
index 47fce37ce59f6..b3a6ca9acad63 100644
--- a/llvm/lib/Target/LoongArch/LoongArchRegisterInfo.cpp
+++ b/llvm/lib/Target/LoongArch/LoongArchRegisterInfo.cpp
@@ -29,7 +29,6 @@ using namespace llvm;
 
 #define GET_REGINFO_TARGET_DESC
 #include "LoongArchGenRegisterInfo.inc"
-
 LoongArchRegisterInfo::LoongArchRegisterInfo(unsigned HwMode)
     : LoongArchGenRegisterInfo(LoongArch::R1, /*DwarfFlavour*/ 0,
                                /*EHFlavor*/ 0,
@@ -254,3 +253,15 @@ bool LoongArchRegisterInfo::canRealignStack(const MachineFunction &MF) const {
   // reserve it.
   return MRI->canReserveReg(LoongArchABI::getBPReg());
 }
+
+bool LoongArchRegisterInfo::saveScavengerRegister(
+    MachineBasicBlock &MBB, MachineBasicBlock::iterator I,
+    MachineBasicBlock::iterator &UseMI, const TargetRegisterClass *RC,
+    Register Reg) const {
+
+  DebugLoc DL;
+  const TargetInstrInfo &TII = *MBB.getParent()->getSubtarget().getInstrInfo();
+  TII.copyPhysReg(MBB, I, DL, LoongArch::R12, Reg, true);
+  TII.copyPhysReg(MBB, UseMI, DL, Reg, LoongArch::R12, true);
+  return true;
+}
\ No newline at end of file
diff --git a/llvm/lib/Target/LoongArch/LoongArchRegisterInfo.h b/llvm/lib/Target/LoongArch/LoongArchRegisterInfo.h
index d1e40254c2972..e9e7ee930f7f6 100644
--- a/llvm/lib/Target/LoongArch/LoongArchRegisterInfo.h
+++ b/llvm/lib/Target/LoongArch/LoongArchRegisterInfo.h
@@ -52,6 +52,11 @@ struct LoongArchRegisterInfo : public LoongArchGenRegisterInfo {
     return true;
   }
   bool canRealignStack(const MachineFunction &MF) const override;
+  bool saveScavengerRegister(MachineBasicBlock &MBB,
+                             MachineBasicBlock::iterator I,
+                             MachineBasicBlock::iterator &UseMI,
+                             const TargetRegisterClass *RC,
+                             Register Reg) const override;
 };
 } // end namespace llvm
 

@tangaac tangaac changed the title [LoongArch] use register scavenger to spill the register [LoongArch] Strengthen stack size estimation for LSX/LASX extension Jul 1, 2025
@tangaac
Copy link
Contributor Author

tangaac commented Jul 15, 2025

reduced.ll.txt
This file needs to be simplified.

@@ -47,7 +47,7 @@ define i64 @va1(ptr %fmt, ...) {
; LA64-WITHFP-NEXT: st.d $a2, $fp, 16
; LA64-WITHFP-NEXT: st.d $a1, $fp, 8
; LA64-WITHFP-NEXT: addi.d $a1, $fp, 16
; LA64-WITHFP-NEXT: st.d $a1, $fp, -24
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why this is changed?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

With this PR or not, there is no difference between debugging outputs.
So it's ok to ignore this change.

@tangaac tangaac requested a review from heiher July 18, 2025 06:45
Copy link
Contributor

@SixWeining SixWeining left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM. @heiher What do you think?

Copy link
Member

@heiher heiher left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM.

@tangaac tangaac merged commit 64a0478 into llvm:main Jul 18, 2025
9 checks passed
@tangaac tangaac deleted the spill-register branch July 18, 2025 09:09
@tangaac tangaac added this to the LLVM 21.x Release milestone Jul 21, 2025
@github-project-automation github-project-automation bot moved this to Needs Triage in LLVM Release Status Jul 21, 2025
@tangaac
Copy link
Contributor Author

tangaac commented Jul 21, 2025

/cherry-pick 64a0478

@llvmbot
Copy link
Member

llvmbot commented Jul 21, 2025

/pull-request #149777

@llvmbot llvmbot moved this from Needs Triage to Done in LLVM Release Status Jul 21, 2025
heiher pushed a commit to llvmbot/llvm-project that referenced this pull request Jul 23, 2025
…lvm#146455)

This patch adds an emergency spill slot when ran out of registers.
PR llvm#139201 introduces `vstelm` instructions with only 8-bit imm offset,
it causes no spill slot to store the spill registers.

(cherry picked from commit 64a0478)
tru pushed a commit to llvmbot/llvm-project that referenced this pull request Jul 24, 2025
…lvm#146455)

This patch adds an emergency spill slot when ran out of registers.
PR llvm#139201 introduces `vstelm` instructions with only 8-bit imm offset,
it causes no spill slot to store the spill registers.

(cherry picked from commit 64a0478)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Development

Successfully merging this pull request may close these issues.

4 participants