Skip to content

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

Merged
tangaac merged 5 commits into
llvm:mainfrom
tangaac:spill-register
Jul 18, 2025
Merged

[LoongArch] Strengthen stack size estimation for LSX/LASX extension#146455
tangaac merged 5 commits into
llvm:mainfrom
tangaac:spill-register

Conversation

@tangaac

@tangaac tangaac commented Jul 1, 2025

Copy link
Copy Markdown
Member

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

llvmbot commented Jul 1, 2025

Copy link
Copy Markdown
Member

@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
Comment thread llvm/lib/Target/LoongArch/LoongArchFrameLowering.cpp Outdated
@tangaac

tangaac commented Jul 15, 2025

Copy link
Copy Markdown
Member Author

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

Comment thread llvm/test/CodeGen/LoongArch/vararg.ll Outdated

Copy link
Copy Markdown
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
Copy Markdown
Member 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.

Comment thread llvm/test/CodeGen/LoongArch/lsx/pr146455.ll Outdated
Comment thread llvm/lib/Target/LoongArch/LoongArchFrameLowering.cpp Outdated
Comment thread llvm/test/CodeGen/LoongArch/lasx/pr146455.ll Outdated
@tangaac tangaac requested a review from heiher July 18, 2025 06:45

@SixWeining SixWeining left a comment

Copy link
Copy Markdown
Contributor

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?

@heiher heiher left a comment

Copy link
Copy Markdown
Member

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

tangaac commented Jul 21, 2025

Copy link
Copy Markdown
Member Author

/cherry-pick 64a0478

@llvmbot

llvmbot commented Jul 21, 2025

Copy link
Copy Markdown
Member

/pull-request #149777

@llvmbot llvmbot moved this from Needs Triage to Done in LLVM Release Status Jul 21, 2025
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