[llvm] [AVR] Support stack realignment (PR #187129)

Patryk Wychowaniec via llvm-commits llvm-commits at lists.llvm.org
Sat Aug 22 05:30:54 PDT 2026


Patryk27 wrote:

> Ok, with this flag active I can see two tests failing - thanks, investigating 🔍

Now that avr-isel allocates a virtual register, it needs to clear the "no vregs" flag the function might have - I've added the following:

```patch
diff --git a/llvm/lib/Target/AVR/AVRISelDAGToDAG.cpp b/llvm/lib/Target/AVR/AVRISelDAGToDAG.cpp
index 1c2ae2ed99ff..2afbe6da8d5e 100644
--- a/llvm/lib/Target/AVR/AVRISelDAGToDAG.cpp
+++ b/llvm/lib/Target/AVR/AVRISelDAGToDAG.cpp
@@ -105,6 +105,10 @@ bool AVRDAGToDAGISel::runOnMachineFunction(MachineFunction &MF) {
   AFI->AlignedStackObjectIdx =
       MF.getFrameInfo().CreateStackObject(1, Align(1), false);
 
+  // We've just allocated a virtual register, so let's drop the "NoVRegs" flag
+  // in case it was active.
+  MF.getProperties().resetNoVRegs();
+
   return SelectionDAGISel::runOnMachineFunction(MF);
 }
```

... and no tests are crashing anymore!

> Your patch's assembly has two difference against avr-gcc-7.3's:

I see - I think this is harmless :eyes:

After avr-isel, LLVM sees the following stack objects to allocate:

```
object 0: the "aligned stack", as allocated here:
    AFI->AlignedStackObjectIdx =
        MF.getFrameInfo().CreateStackObject(1, Align(1), false);

object 1: int a
```

LLVM then allocates them last-to-first, so that:

- `int a` lands in `Y+1 : Y+2`,
- aligned stack lands in `Y+3 : Y+11`.

... while GCC does it the other way around, allocating first-to-last (or possibly employing some smarter strategy, like smallest-to-bigest, I'm not 100% sure):

- aligned stack lands in `Y+1 : Y+9`,
- `int a` lands in `Y+10 : Y+11`.

Both allocation schemes are correct - in fact, we match avr-gcc behavior with `-O`, it's just `-O3` that produces different offsets.

Playing around with avr-gcc, I _suspect_ that they always put the aligned stack object at the end of the frame - we can match this behavior by implementing `TargetFrameLowering.orderFrameObjects`:

```patch
diff --git a/llvm/lib/Target/AVR/AVRFrameLowering.h b/llvm/lib/Target/AVR/AVRFrameLowering.h
index 2d6b56579b3d..6e39e85f549a 100644
--- a/llvm/lib/Target/AVR/AVRFrameLowering.h
+++ b/llvm/lib/Target/AVR/AVRFrameLowering.h
@@ -9,6 +9,7 @@
 #ifndef LLVM_AVR_FRAME_LOWERING_H
 #define LLVM_AVR_FRAME_LOWERING_H
 
+#include "AVRMachineFunctionInfo.h"
 #include "llvm/CodeGen/TargetFrameLowering.h"
 
 namespace llvm {
@@ -42,6 +43,25 @@ public:
   eliminateCallFramePseudoInstr(MachineFunction &MF, MachineBasicBlock &MBB,
                                 MachineBasicBlock::iterator MI) const override;
 
+  void orderFrameObjects(const MachineFunction &MF,
+                         SmallVectorImpl<int> &Objects) const override {
+    if (Objects.empty()) {
+      return;
+    }
+
+    const auto *AFI = MF.getInfo<AVRMachineFunctionInfo>();
+
+    auto *It =
+        std::find(Objects.begin(), Objects.end(), AFI->AlignedStackObjectIdx);
+
+    if (It == Objects.end()) {
+      return;
+    }
+
+    Objects.erase(It);
+    Objects.push_back(AFI->AlignedStackObjectIdx);
+  }
+
 protected:
   bool hasFPImpl(const MachineFunction &MF) const override;
 };
```

What this method does is that it always puts the `AlignedStackObject` at the _end_ of the `Objects` list, forcing LLVM to allocate it at the end of the frame.

I don't mind applying this patch if you want to match `avr-gcc -O3`'s behavior, but I thiiink it's not worth it - it introduces a bit more complexity for essentially the same outcome (stack objects just get shuffled around, which doesn't meaningfully change anything IMO).

> Is it possible to use R3:R2 on AVR and R19:R18 on AVRTiny as SPReg ? [...]

Unfortunately, R19:R18 doesn't belong to the PTRDISPREGS category and thus cannot be directly used with STD and LDD - so if we used R19:R18 as SPReg, in order to legalize this `a = 10` write in here:

```cpp
void foo() {
  _Alignas(4) uint16_t a = 10;
}
```
... we would have to copy SPReg into a register of the PTRDISPREGS class, which brings us back to allocating virtual registers (same thing we do now).

In my older version, instead of allocating a register I've hard-coded the copy to go through R31:R30:

https://github.com/Patryk27/llvm-project/blob/2d61aaa3623b64e2c1c0ac6c4f000aa91b37958f/llvm/lib/Target/AVR/AVRRegisterInfo.cpp#L336

... but this quickly breaks, because R31:R30 might be busy and thus using it as a temporary is illegal (it will cause us to overwrite whatever value R31:R30 held before we copied SPReg into it).

So, to be very specific, under the previous approach this problem cannot be solved:

https://github.com/Patryk27/llvm-project/blob/2d61aaa3623b64e2c1c0ac6c4f000aa91b37958f/llvm/lib/Target/AVR/AVRRegisterInfo.cpp#L333

... and that's why my current approach takes a different direction - I just tell LLVM I need a register and LLVM takes care of everything, I don't have to worry about spills and whatnot.

Let me know if I'm talking sense 😄 

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


More information about the llvm-commits mailing list