[llvm] [CodeGen] Sort fixed frame objects by stack offset (PR #219669)
via llvm-commits
llvm-commits at lists.llvm.org
Sat Aug 29 04:13:37 PDT 2026
llvmorg-github-actions[bot] wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-backend-aarch64
Author: zhouguangyuan0718
<details>
<summary>Changes</summary>
## Summary
- sort fixed frame-index memory operands by their explicit stack offsets
- preserve the existing frame-index ordering for ordinary stack objects and as a tie-breaker
- add an AArch64 MIR regression with fixed objects created out of offset order
## Background
`BaseMemOpClusterMutation` currently orders frame-index bases by frame-index number, adjusted for the stack growth direction. Fixed frame objects are different from ordinary stack objects: they carry explicit offsets, and their creation order is not required to match address order.
As a result, a target clustering hook can receive two fixed objects in reverse address order. AArch64's `shouldClusterFI` expects the incoming fixed-object offsets to be ordered and aborts with `Object offsets are not ordered` in an assertions-enabled build.
When both operands refer to fixed frame objects, compare their `MachineFrameInfo` object offsets first. The existing frame-index comparison remains the fallback, so the ordering of ordinary stack objects is unchanged.
The regression deliberately creates two fixed objects in an order that disagrees with their explicit offsets. It covers both the legacy and new machine-scheduler pass forms.
## Testing
- `ninja check-llvm-codegen-aarch64`
- 4203 tests discovered
- 4198 passed
- 5 expected failures
- 0 unexpected failures
## AI assistance
OpenAI Codex was used to assist with the investigation, patch preparation, testing, and drafting this PR description.
---
Full diff: https://github.com/llvm/llvm-project/pull/219669.diff
2 Files Affected:
- (modified) llvm/lib/CodeGen/MachineScheduler.cpp (+13)
- (modified) llvm/test/CodeGen/AArch64/cluster-frame-index.mir (+23)
``````````diff
diff --git a/llvm/lib/CodeGen/MachineScheduler.cpp b/llvm/lib/CodeGen/MachineScheduler.cpp
index 814ea3f8eeb05..22b7d8ad5af5b 100644
--- a/llvm/lib/CodeGen/MachineScheduler.cpp
+++ b/llvm/lib/CodeGen/MachineScheduler.cpp
@@ -25,6 +25,7 @@
#include "llvm/CodeGen/LiveInterval.h"
#include "llvm/CodeGen/LiveIntervals.h"
#include "llvm/CodeGen/MachineBasicBlock.h"
+#include "llvm/CodeGen/MachineFrameInfo.h"
#include "llvm/CodeGen/MachineFunction.h"
#include "llvm/CodeGen/MachineFunctionPass.h"
#include "llvm/CodeGen/MachineInstr.h"
@@ -1982,6 +1983,18 @@ class BaseMemOpClusterMutation : public ScheduleDAGMutation {
return A->getReg() < B->getReg();
if (A->isFI()) {
const MachineFunction &MF = *A->getParent()->getParent()->getParent();
+ const MachineFrameInfo &MFI = MF.getFrameInfo();
+ if (MFI.isFixedObjectIndex(A->getIndex()) &&
+ MFI.isFixedObjectIndex(B->getIndex())) {
+ // Fixed objects have explicit offsets, and targets may create their
+ // frame indices in an order unrelated to those offsets. Sort by the
+ // actual object offsets so target clustering hooks see memory
+ // operations in address order.
+ int64_t AOffset = MFI.getObjectOffset(A->getIndex());
+ int64_t BOffset = MFI.getObjectOffset(B->getIndex());
+ if (AOffset != BOffset)
+ return AOffset < BOffset;
+ }
const TargetFrameLowering &TFI = *MF.getSubtarget().getFrameLowering();
bool StackGrowsDown = TFI.getStackGrowthDirection() ==
TargetFrameLowering::StackGrowsDown;
diff --git a/llvm/test/CodeGen/AArch64/cluster-frame-index.mir b/llvm/test/CodeGen/AArch64/cluster-frame-index.mir
index 5d761f10be3b2..ef82a5508128e 100644
--- a/llvm/test/CodeGen/AArch64/cluster-frame-index.mir
+++ b/llvm/test/CodeGen/AArch64/cluster-frame-index.mir
@@ -27,6 +27,29 @@ body: |
; CHECK-NEXT: RET
...
---
+name: merge_out_of_order_fixedstack
+# CHECK-LABEL: name: merge_out_of_order_fixedstack
+tracksRegLiveness: true
+fixedStack:
+ # Fixed object indices are allocated in reverse id order. Deliberately keep
+ # their explicit offsets in the opposite order: the scheduler must use the
+ # fixed object offsets, not frame-index creation order, when sorting memory
+ # operations for clustering.
+ - { id: 0, size: 8, alignment: 8, offset: -8 }
+ - { id: 1, size: 8, alignment: 8, offset: -16 }
+body: |
+ bb.0:
+ %0:gpr64 = LDRXui %fixed-stack.0, 0 :: (load (s64))
+ %1:gpr64 = LDRXui %fixed-stack.1, 0 :: (load (s64))
+ $x0 = ADDXrr %0, %1
+ RET_ReallyLR implicit $x0
+
+ ; CHECK: LDRXui %fixed-stack.1
+ ; CHECK-NEXT: LDRXui %fixed-stack.0
+ ; CHECK: ADDXrr
+ ; CHECK: RET
+...
+---
name: merge_fixedstack
# CHECK-LABEL: name: merge_fixedstack
tracksRegLiveness: true
``````````
</details>
https://github.com/llvm/llvm-project/pull/219669
More information about the llvm-commits
mailing list