[llvm] [WebAssembly] Enable a limited amount of stackification for debug code (PR #136510)

via llvm-commits llvm-commits at lists.llvm.org
Sun Apr 20 14:45:18 PDT 2025


github-actions[bot] wrote:

<!--LLVM CODE FORMAT COMMENT: {clang-format}-->


:warning: C/C++ code formatter, clang-format found issues in your code. :warning:

<details>
<summary>
You can test this locally with the following command:
</summary>

``````````bash
git-clang-format --diff HEAD~1 HEAD --extensions h,cpp -- llvm/lib/Target/WebAssembly/WebAssembly.h llvm/lib/Target/WebAssembly/WebAssemblyRegStackify.cpp llvm/lib/Target/WebAssembly/WebAssemblyTargetMachine.cpp
``````````

</details>

<details>
<summary>
View the diff from clang-format here.
</summary>

``````````diff
diff --git a/llvm/lib/Target/WebAssembly/WebAssemblyRegStackify.cpp b/llvm/lib/Target/WebAssembly/WebAssemblyRegStackify.cpp
index 6f47d9c05..05c89bd0e 100644
--- a/llvm/lib/Target/WebAssembly/WebAssemblyRegStackify.cpp
+++ b/llvm/lib/Target/WebAssembly/WebAssemblyRegStackify.cpp
@@ -275,7 +275,7 @@ static MachineInstr *getVRegDef(unsigned Reg, const MachineInstr *Insert,
   // MRI doesn't know what the Def is. Try asking LIS.
   if (LIS != nullptr) {
     SlotIndex InstIndex = LIS->getInstructionIndex(*Insert);
-    if (const VNInfo* ValNo = LIS->getInterval(Reg).getVNInfoBefore(InstIndex))
+    if (const VNInfo *ValNo = LIS->getInterval(Reg).getVNInfoBefore(InstIndex))
       return LIS->getInstructionFromIndex(ValNo->def);
   }
 
@@ -285,9 +285,9 @@ static MachineInstr *getVRegDef(unsigned Reg, const MachineInstr *Insert,
 // Test whether Reg, as defined at Def, has exactly one use. This is a
 // generalization of MachineRegisterInfo::hasOneNonDBGUse that uses
 // LiveIntervals to handle complex cases in optimized code.
-static bool hasSingleUse(unsigned Reg, MachineRegisterInfo& MRI,
-                         WebAssemblyFunctionInfo& MFI, bool Optimize,
-                         MachineInstr* Def, LiveIntervals *LIS) {
+static bool hasSingleUse(unsigned Reg, MachineRegisterInfo &MRI,
+                         WebAssemblyFunctionInfo &MFI, bool Optimize,
+                         MachineInstr *Def, LiveIntervals *LIS) {
   if (!Optimize) {
     // We don't want to stackify DBG_VALUE operands since WASM stack locations
     // are less useful and less widely supported than WASM local locations.
@@ -332,8 +332,7 @@ static bool hasSingleUse(unsigned Reg, MachineRegisterInfo& MRI,
 static bool isSafeToMove(const MachineOperand *Def, const MachineOperand *Use,
                          const MachineInstr *Insert,
                          const WebAssemblyFunctionInfo &MFI,
-                         const MachineRegisterInfo &MRI,
-                         bool Optimize) {
+                         const MachineRegisterInfo &MRI, bool Optimize) {
   const MachineInstr *DefI = Def->getParent();
   const MachineInstr *UseI = Use->getParent();
   assert(DefI->getParent() == Insert->getParent());
@@ -597,11 +596,12 @@ static MachineInstr *getPrevNonDebugInst(MachineInstr *MI) {
 
 /// A trivially cloneable instruction; clone it and nest the new copy with the
 /// current instruction.
-static MachineInstr *rematerializeCheapDef(
-    unsigned Reg, MachineOperand &Op, MachineInstr &Def,
-    MachineBasicBlock::instr_iterator Insert, LiveIntervals &LIS,
-    WebAssemblyFunctionInfo &MFI, MachineRegisterInfo &MRI,
-    const WebAssemblyInstrInfo *TII) {
+static MachineInstr *
+rematerializeCheapDef(unsigned Reg, MachineOperand &Op, MachineInstr &Def,
+                      MachineBasicBlock::instr_iterator Insert,
+                      LiveIntervals &LIS, WebAssemblyFunctionInfo &MFI,
+                      MachineRegisterInfo &MRI,
+                      const WebAssemblyInstrInfo *TII) {
   LLVM_DEBUG(dbgs() << "Rematerializing cheap def: "; Def.dump());
   LLVM_DEBUG(dbgs() << " - for use in "; Op.getParent()->dump());
 
@@ -841,8 +841,8 @@ bool WebAssemblyRegStackify::runOnMachineFunction(MachineFunction &MF) {
   MachineRegisterInfo &MRI = MF.getRegInfo();
   WebAssemblyFunctionInfo &MFI = *MF.getInfo<WebAssemblyFunctionInfo>();
   const auto *TII = MF.getSubtarget<WebAssemblySubtarget>().getInstrInfo();
-  MachineDominatorTree* MDT = nullptr;
-  LiveIntervals* LIS = nullptr;
+  MachineDominatorTree *MDT = nullptr;
+  LiveIntervals *LIS = nullptr;
   if (Optimize) {
     MDT = &getAnalysis<MachineDominatorTreeWrapperPass>().getDomTree();
     LIS = &getAnalysis<LiveIntervalsWrapperPass>().getLIS();
@@ -909,7 +909,8 @@ bool WebAssemblyRegStackify::runOnMachineFunction(MachineFunction &MF) {
         // supports intra-block moves) and it's MachineSink's job to catch all
         // the sinking opportunities anyway.
         bool SameBlock = DefI->getParent() == &MBB;
-        bool CanMove = SameBlock && isSafeToMove(Def, &Use, Insert, MFI, MRI, Optimize) &&
+        bool CanMove = SameBlock &&
+                       isSafeToMove(Def, &Use, Insert, MFI, MRI, Optimize) &&
                        !TreeWalker.isOnStack(Reg);
         if (CanMove && hasSingleUse(Reg, MRI, MFI, Optimize, DefI, LIS)) {
           Insert = moveForSingleUse(Reg, Use, DefI, MBB, Insert, LIS, MFI, MRI);
@@ -918,15 +919,17 @@ bool WebAssemblyRegStackify::runOnMachineFunction(MachineFunction &MF) {
           // info as well.
           // TODO: Encode this properly as a stackified value.
           if (MFI.isFrameBaseVirtual() && MFI.getFrameBaseVreg() == Reg) {
-            assert(Optimize && "Stackifying away frame base in unoptimized code not expected");
+            assert(
+                Optimize &&
+                "Stackifying away frame base in unoptimized code not expected");
             MFI.clearFrameBaseVreg();
           }
         } else if (Optimize && shouldRematerialize(*DefI, TII)) {
-          Insert =
-              rematerializeCheapDef(Reg, Use, *DefI, Insert->getIterator(),
-                                    *LIS, MFI, MRI, TII);
-        } else if (Optimize && CanMove && oneUseDominatesOtherUses(Reg, Use, MBB, MRI, *MDT,
-                                                                   *LIS, MFI)) {
+          Insert = rematerializeCheapDef(Reg, Use, *DefI, Insert->getIterator(),
+                                         *LIS, MFI, MRI, TII);
+        } else if (Optimize && CanMove &&
+                   oneUseDominatesOtherUses(Reg, Use, MBB, MRI, *MDT, *LIS,
+                                            MFI)) {
           Insert = moveAndTeeForMultiUse(Reg, Use, DefI, MBB, Insert, *LIS, MFI,
                                          MRI, TII);
         } else {
@@ -950,7 +953,8 @@ bool WebAssemblyRegStackify::runOnMachineFunction(MachineFunction &MF) {
           Register DefReg = SubsequentDef->getReg();
           Register UseReg = SubsequentUse->getReg();
           // TODO: This single-use restriction could be relaxed by using tees
-          if (DefReg != UseReg || !hasSingleUse(DefReg, MRI, MFI, Optimize, nullptr, nullptr))
+          if (DefReg != UseReg ||
+              !hasSingleUse(DefReg, MRI, MFI, Optimize, nullptr, nullptr))
             break;
           MFI.stackifyVReg(MRI, DefReg);
           ++SubsequentDef;

``````````

</details>


https://github.com/llvm/llvm-project/pull/136510


More information about the llvm-commits mailing list