[llvm-branch-commits] [llvm] [MachinePipeliner] Fix PHIs with loop-invariant backedge (PR #238601)
via llvm-branch-commits
llvm-branch-commits at lists.llvm.org
Wed Nov 4 12:47:58 PST 2026
llvmorg-github-actions[bot] wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-backend-hexagon
Author: Florian Mayer (fmayer)
<details>
<summary>Changes</summary>
Assisted-By: Gemini
---
<sub>Stack created with <a href="https://github.com/github/gh-stack">GitHub Stacks CLI</a> • <a href="https://gh.io/stacks-feedback">Give Feedback 💬</a></sub>
---
Full diff: https://github.com/llvm/llvm-project/pull/238601.diff
2 Files Affected:
- (modified) llvm/lib/CodeGen/ModuloSchedule.cpp (+12-6)
- (modified) llvm/test/CodeGen/Hexagon/swp-phi-loop-invariant-backedge.mir (+5-4)
``````````diff
diff --git a/llvm/lib/CodeGen/ModuloSchedule.cpp b/llvm/lib/CodeGen/ModuloSchedule.cpp
index e0a33c73f052c..0ac825b819d6d 100644
--- a/llvm/lib/CodeGen/ModuloSchedule.cpp
+++ b/llvm/lib/CodeGen/ModuloSchedule.cpp
@@ -1406,13 +1406,14 @@ Register KernelRewriter::remapUse(Register Reg, MachineInstr &MI) {
if (!Producer)
return Reg;
+ if (Producer->getParent() != BB)
+ // Producer was not inside the loop. Use the register as-is.
+ return Reg;
+
int ConsumerStage = S.getStage(&MI);
if (!Producer->isPHI()) {
// Non-phi producers are simple to remap. Insert as many phis as the
// difference between the consumer and producer stages.
- if (Producer->getParent() != BB)
- // Producer was not inside the loop. Use the register as-is.
- return Reg;
int ProducerStage = S.getStage(Producer);
assert(ConsumerStage != -1 &&
"In-loop consumer should always be scheduled!");
@@ -1436,12 +1437,17 @@ Register KernelRewriter::remapUse(Register Reg, MachineInstr &MI) {
assert(LoopProducer);
}
int LoopProducerStage = S.getStage(LoopProducer);
+ if (LoopProducerStage == -1) {
+ assert(LoopProducer->getParent() != BB &&
+ "In-loop producer should always be scheduled!");
+ // The loop-carried value of the in-loop phi chain is defined outside the
+ // loop, so it is available at stage 0 of every iteration.
+ LoopProducerStage = 0;
+ }
std::optional<Register> IllegalPhiDefault;
- if (LoopProducerStage == -1) {
- // Do nothing.
- } else if (LoopProducerStage > ConsumerStage) {
+ if (LoopProducerStage > ConsumerStage) {
// This schedule is only representable if ProducerStage == ConsumerStage+1.
// In addition, Consumer's cycle must be scheduled after Producer in the
// rescheduled loop. This is enforced by the pipeliner's ASAP and ALAP
diff --git a/llvm/test/CodeGen/Hexagon/swp-phi-loop-invariant-backedge.mir b/llvm/test/CodeGen/Hexagon/swp-phi-loop-invariant-backedge.mir
index 82e730de4d34b..2440c63697927 100644
--- a/llvm/test/CodeGen/Hexagon/swp-phi-loop-invariant-backedge.mir
+++ b/llvm/test/CodeGen/Hexagon/swp-phi-loop-invariant-backedge.mir
@@ -29,7 +29,8 @@ body: |
; CHECK-NEXT: {{ $}}
; CHECK-NEXT: [[PHI:%[0-9]+]]:intregs = PHI [[A2_addi]], %bb.3, %3, %bb.1
; CHECK-NEXT: [[PHI1:%[0-9]+]]:intregs = PHI [[A2_tfrsi1]], %bb.3, [[A2_tfrsi1]], %bb.1
- ; CHECK-NEXT: [[A2_add:%[0-9]+]]:intregs = A2_add [[PHI]], [[PHI1]]
+ ; CHECK-NEXT: [[PHI2:%[0-9]+]]:intregs = PHI [[A2_tfrsi]], %bb.3, [[PHI1]], %bb.1
+ ; CHECK-NEXT: [[A2_add:%[0-9]+]]:intregs = A2_add [[PHI]], [[PHI2]]
; CHECK-NEXT: [[A2_addi1:%[0-9]+]]:intregs = A2_addi [[A2_tfrsi]], 1
; CHECK-NEXT: ENDLOOP0 %bb.1, implicit-def $pc, implicit-def $lc0, implicit $sa0, implicit $lc0
; CHECK-NEXT: J2_jump %bb.5, implicit-def $pc
@@ -37,9 +38,9 @@ body: |
; CHECK-NEXT: bb.5:
; CHECK-NEXT: successors: %bb.4(0x80000000)
; CHECK-NEXT: {{ $}}
- ; CHECK-NEXT: [[PHI2:%[0-9]+]]:intregs = PHI [[A2_addi1]], %bb.1
- ; CHECK-NEXT: [[PHI3:%[0-9]+]]:intregs = PHI [[A2_tfrsi1]], %bb.1
- ; CHECK-NEXT: [[A2_add1:%[0-9]+]]:intregs = A2_add [[PHI2]], [[PHI3]]
+ ; CHECK-NEXT: [[PHI3:%[0-9]+]]:intregs = PHI [[A2_addi1]], %bb.1
+ ; CHECK-NEXT: [[PHI4:%[0-9]+]]:intregs = PHI [[PHI1]], %bb.1
+ ; CHECK-NEXT: [[A2_add1:%[0-9]+]]:intregs = A2_add [[PHI3]], [[PHI4]]
; CHECK-NEXT: J2_jump %bb.4, implicit-def $pc
; CHECK-NEXT: {{ $}}
; CHECK-NEXT: bb.4:
``````````
</details>
https://github.com/llvm/llvm-project/pull/238601
More information about the llvm-branch-commits
mailing list