Skip to content

Commit 6201e3b

Browse files
committed
[MachinePipeliner] Improve loop carried dependence analysis
The previous implementation had false positive/negative cases in analysis of loop carried dependence. A case of missed dependencies is caused by incorrect analysis of address increments. It is fixed by a strict analysis of recursive definitions. See added test swp-carried-dep4.mir. Excessive detection of dependence is corrected by improving the formula for determining overlap of address ranges to be accessed. See added test swp-carried-dep5.mir.
1 parent f023da1 commit 6201e3b

File tree

8 files changed

+397
-82
lines changed

8 files changed

+397
-82
lines changed

llvm/include/llvm/CodeGen/MachinePipeliner.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -390,6 +390,9 @@ class SwingSchedulerDAG : public ScheduleDAGInstrs {
390390

391391
const SwingSchedulerDDG *getDDG() const { return DDG.get(); }
392392

393+
bool mayOverlapInLaterIter(const MachineInstr *BaseMI,
394+
const MachineInstr *OtherMI) const;
395+
393396
private:
394397
void addLoopCarriedDependences(AAResults *AA);
395398
void updatePhiDependences();
@@ -409,7 +412,7 @@ class SwingSchedulerDAG : public ScheduleDAGInstrs {
409412
void computeNodeOrder(NodeSetType &NodeSets);
410413
void checkValidNodeOrder(const NodeSetType &Circuits) const;
411414
bool schedulePipeline(SMSchedule &Schedule);
412-
bool computeDelta(MachineInstr &MI, unsigned &Delta) const;
415+
bool computeDelta(const MachineInstr &MI, int &Delta) const;
413416
MachineInstr *findDefInLoop(Register Reg);
414417
bool canUseLastOffsetValue(MachineInstr *MI, unsigned &BasePos,
415418
unsigned &OffsetPos, unsigned &NewBase,

llvm/lib/CodeGen/MachinePipeliner.cpp

Lines changed: 188 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -2523,9 +2523,104 @@ bool SwingSchedulerDAG::schedulePipeline(SMSchedule &Schedule) {
25232523
return scheduleFound && Schedule.getMaxStageCount() > 0;
25242524
}
25252525

2526+
static Register findUniqueOperandDefinedInLoop(const MachineInstr &MI) {
2527+
const MachineRegisterInfo &MRI = MI.getParent()->getParent()->getRegInfo();
2528+
Register Result;
2529+
for (const MachineOperand &Use : MI.all_uses()) {
2530+
Register Reg = Use.getReg();
2531+
if (!Reg.isVirtual())
2532+
return Register();
2533+
if (MRI.getVRegDef(Reg)->getParent() != MI.getParent())
2534+
continue;
2535+
if (Result)
2536+
return Register();
2537+
Result = Reg;
2538+
}
2539+
return Result;
2540+
}
2541+
2542+
/// When Op is a value that is incremented recursively in a loop and there is a
2543+
/// unique instruction that increments it, returns true and sets Value.
2544+
static bool findLoopIncrementValue(const MachineOperand &Op, int &Value) {
2545+
if (!Op.isReg() || !Op.getReg().isVirtual())
2546+
return false;
2547+
2548+
Register OrgReg = Op.getReg();
2549+
Register CurReg = OrgReg;
2550+
const MachineBasicBlock *LoopBB = Op.getParent()->getParent();
2551+
const MachineRegisterInfo &MRI = LoopBB->getParent()->getRegInfo();
2552+
2553+
const TargetInstrInfo *TII =
2554+
LoopBB->getParent()->getSubtarget().getInstrInfo();
2555+
const TargetRegisterInfo *TRI =
2556+
LoopBB->getParent()->getSubtarget().getRegisterInfo();
2557+
2558+
MachineInstr *Phi = nullptr;
2559+
MachineInstr *Increment = nullptr;
2560+
2561+
// Traverse definitions until it reaches Op or an instruction that does not
2562+
// satisfy the condition.
2563+
// Acceptable example:
2564+
// bb.0:
2565+
// %0 = PHI %3, %bb.0, ...
2566+
// %2 = ADD %0, Value
2567+
// ... = LOAD %2(Op)
2568+
// %3 = COPY %2
2569+
while (true) {
2570+
if (!CurReg.isValid() || !CurReg.isVirtual())
2571+
return false;
2572+
MachineInstr *Def = MRI.getVRegDef(CurReg);
2573+
if (Def->getParent() != LoopBB)
2574+
return false;
2575+
2576+
if (Def->isCopy()) {
2577+
// Ignore copy instructions unless they contain subregisters
2578+
if (Def->getOperand(0).getSubReg() || Def->getOperand(1).getSubReg())
2579+
return false;
2580+
CurReg = Def->getOperand(1).getReg();
2581+
} else if (Def->isPHI()) {
2582+
// There must be just one Phi
2583+
if (Phi)
2584+
return false;
2585+
Phi = Def;
2586+
CurReg = getLoopPhiReg(*Def, LoopBB);
2587+
} else if (TII->getIncrementValue(*Def, Value)) {
2588+
// Potentially a unique increment
2589+
if (Increment)
2590+
// Multiple increments exist
2591+
return false;
2592+
2593+
const MachineOperand *BaseOp;
2594+
int64_t Offset;
2595+
bool OffsetIsScalable;
2596+
if (TII->getMemOperandWithOffset(*Def, BaseOp, Offset, OffsetIsScalable,
2597+
TRI)) {
2598+
// Pre/post increment instruction
2599+
CurReg = BaseOp->getReg();
2600+
} else {
2601+
// If only one of the operands is defined within the loop, it is assumed
2602+
// to be an incremented value.
2603+
CurReg = findUniqueOperandDefinedInLoop(*Def);
2604+
if (!CurReg.isValid())
2605+
return false;
2606+
}
2607+
Increment = Def;
2608+
} else {
2609+
return false;
2610+
}
2611+
if (CurReg == OrgReg)
2612+
break;
2613+
}
2614+
2615+
if (!Phi || !Increment)
2616+
return false;
2617+
2618+
return true;
2619+
}
2620+
25262621
/// Return true if we can compute the amount the instruction changes
25272622
/// during each iteration. Set Delta to the amount of the change.
2528-
bool SwingSchedulerDAG::computeDelta(MachineInstr &MI, unsigned &Delta) const {
2623+
bool SwingSchedulerDAG::computeDelta(const MachineInstr &MI, int &Delta) const {
25292624
const TargetRegisterInfo *TRI = MF.getSubtarget().getRegisterInfo();
25302625
const MachineOperand *BaseOp;
25312626
int64_t Offset;
@@ -2540,24 +2635,7 @@ bool SwingSchedulerDAG::computeDelta(MachineInstr &MI, unsigned &Delta) const {
25402635
if (!BaseOp->isReg())
25412636
return false;
25422637

2543-
Register BaseReg = BaseOp->getReg();
2544-
2545-
MachineRegisterInfo &MRI = MF.getRegInfo();
2546-
// Check if there is a Phi. If so, get the definition in the loop.
2547-
MachineInstr *BaseDef = MRI.getVRegDef(BaseReg);
2548-
if (BaseDef && BaseDef->isPHI()) {
2549-
BaseReg = getLoopPhiReg(*BaseDef, MI.getParent());
2550-
BaseDef = MRI.getVRegDef(BaseReg);
2551-
}
2552-
if (!BaseDef)
2553-
return false;
2554-
2555-
int D = 0;
2556-
if (!TII->getIncrementValue(*BaseDef, D) && D >= 0)
2557-
return false;
2558-
2559-
Delta = D;
2560-
return true;
2638+
return findLoopIncrementValue(*BaseOp, Delta);
25612639
}
25622640

25632641
/// Check if we can change the instruction to use an offset value from the
@@ -2675,6 +2753,96 @@ MachineInstr *SwingSchedulerDAG::findDefInLoop(Register Reg) {
26752753
return Def;
26762754
}
26772755

2756+
/// Return false if there is no overlap between the region accessed by BaseMI in
2757+
/// an iteration and the region accessed by OtherMI in subsequent iterations.
2758+
bool SwingSchedulerDAG::mayOverlapInLaterIter(
2759+
const MachineInstr *BaseMI, const MachineInstr *OtherMI) const {
2760+
int DeltaB, DeltaO, Delta;
2761+
if (!computeDelta(*BaseMI, DeltaB) || !computeDelta(*OtherMI, DeltaO) ||
2762+
DeltaB != DeltaO)
2763+
return true;
2764+
Delta = DeltaB;
2765+
2766+
const MachineOperand *BaseOpB, *BaseOpO;
2767+
int64_t OffsetB, OffsetO;
2768+
bool OffsetBIsScalable, OffsetOIsScalable;
2769+
const TargetRegisterInfo *TRI = MF.getSubtarget().getRegisterInfo();
2770+
if (!TII->getMemOperandWithOffset(*BaseMI, BaseOpB, OffsetB,
2771+
OffsetBIsScalable, TRI) ||
2772+
!TII->getMemOperandWithOffset(*OtherMI, BaseOpO, OffsetO,
2773+
OffsetOIsScalable, TRI))
2774+
return true;
2775+
2776+
if (OffsetBIsScalable || OffsetOIsScalable)
2777+
return true;
2778+
2779+
if (!BaseOpB->isIdenticalTo(*BaseOpO)) {
2780+
// Pass cases with different base operands but same initial values.
2781+
// Typically for when pre/post increment is used.
2782+
2783+
if (!BaseOpB->isReg() || !BaseOpO->isReg())
2784+
return true;
2785+
Register RegB = BaseOpB->getReg(), RegO = BaseOpO->getReg();
2786+
if (!RegB.isVirtual() || !RegO.isVirtual())
2787+
return true;
2788+
2789+
MachineInstr *DefB = MRI.getVRegDef(BaseOpB->getReg());
2790+
MachineInstr *DefO = MRI.getVRegDef(BaseOpO->getReg());
2791+
if (!DefB || !DefO || !DefB->isPHI() || !DefO->isPHI())
2792+
return true;
2793+
2794+
unsigned InitValB = 0;
2795+
unsigned LoopValB = 0;
2796+
unsigned InitValO = 0;
2797+
unsigned LoopValO = 0;
2798+
getPhiRegs(*DefB, BB, InitValB, LoopValB);
2799+
getPhiRegs(*DefO, BB, InitValO, LoopValO);
2800+
MachineInstr *InitDefB = MRI.getVRegDef(InitValB);
2801+
MachineInstr *InitDefO = MRI.getVRegDef(InitValO);
2802+
2803+
if (!InitDefB->isIdenticalTo(*InitDefO))
2804+
return true;
2805+
}
2806+
2807+
LocationSize AccessSizeB = (*BaseMI->memoperands_begin())->getSize();
2808+
LocationSize AccessSizeO = (*OtherMI->memoperands_begin())->getSize();
2809+
2810+
// This is the main test, which checks the offset values and the loop
2811+
// increment value to determine if the accesses may be loop carried.
2812+
if (!AccessSizeB.hasValue() || !AccessSizeO.hasValue())
2813+
return true;
2814+
2815+
LLVM_DEBUG({
2816+
dbgs() << "Overlap check:\n";
2817+
dbgs() << " BaseMI: ";
2818+
BaseMI->dump();
2819+
dbgs() << " Base + " << OffsetB << " + I * " << Delta
2820+
<< ", Len: " << AccessSizeB.getValue() << "\n";
2821+
dbgs() << " OtherMI: ";
2822+
OtherMI->dump();
2823+
dbgs() << " Base + " << OffsetO << " + I * " << Delta
2824+
<< ", Len: " << AccessSizeO.getValue() << "\n";
2825+
});
2826+
2827+
if (Delta < 0) {
2828+
int64_t BaseMinAddr = OffsetB;
2829+
int64_t OhterNextIterMaxAddr = OffsetO + Delta + AccessSizeO.getValue() - 1;
2830+
if (BaseMinAddr > OhterNextIterMaxAddr) {
2831+
LLVM_DEBUG(dbgs() << " Result: No overlap\n");
2832+
return false;
2833+
}
2834+
} else {
2835+
int64_t BaseMaxAddr = OffsetB + AccessSizeB.getValue() - 1;
2836+
int64_t OtherNextIterMinAddr = OffsetO + Delta;
2837+
if (BaseMaxAddr < OtherNextIterMinAddr) {
2838+
LLVM_DEBUG(dbgs() << " Result: No overlap\n");
2839+
return false;
2840+
}
2841+
}
2842+
LLVM_DEBUG(dbgs() << " Result: Overlap\n");
2843+
return true;
2844+
}
2845+
26782846
/// Return true for an order or output dependence that is loop carried
26792847
/// potentially. A dependence is loop carried if the destination defines a value
26802848
/// that may be used or defined by the source in a subsequent iteration.
@@ -2706,61 +2874,7 @@ bool SwingSchedulerDAG::isLoopCarriedDep(
27062874
// The conservative assumption is that a dependence between memory operations
27072875
// may be loop carried. The following code checks when it can be proved that
27082876
// there is no loop carried dependence.
2709-
unsigned DeltaS, DeltaD;
2710-
if (!computeDelta(*SI, DeltaS) || !computeDelta(*DI, DeltaD))
2711-
return true;
2712-
2713-
const MachineOperand *BaseOpS, *BaseOpD;
2714-
int64_t OffsetS, OffsetD;
2715-
bool OffsetSIsScalable, OffsetDIsScalable;
2716-
const TargetRegisterInfo *TRI = MF.getSubtarget().getRegisterInfo();
2717-
if (!TII->getMemOperandWithOffset(*SI, BaseOpS, OffsetS, OffsetSIsScalable,
2718-
TRI) ||
2719-
!TII->getMemOperandWithOffset(*DI, BaseOpD, OffsetD, OffsetDIsScalable,
2720-
TRI))
2721-
return true;
2722-
2723-
assert(!OffsetSIsScalable && !OffsetDIsScalable &&
2724-
"Expected offsets to be byte offsets");
2725-
2726-
MachineInstr *DefS = MRI.getVRegDef(BaseOpS->getReg());
2727-
MachineInstr *DefD = MRI.getVRegDef(BaseOpD->getReg());
2728-
if (!DefS || !DefD || !DefS->isPHI() || !DefD->isPHI())
2729-
return true;
2730-
2731-
unsigned InitValS = 0;
2732-
unsigned LoopValS = 0;
2733-
unsigned InitValD = 0;
2734-
unsigned LoopValD = 0;
2735-
getPhiRegs(*DefS, BB, InitValS, LoopValS);
2736-
getPhiRegs(*DefD, BB, InitValD, LoopValD);
2737-
MachineInstr *InitDefS = MRI.getVRegDef(InitValS);
2738-
MachineInstr *InitDefD = MRI.getVRegDef(InitValD);
2739-
2740-
if (!InitDefS->isIdenticalTo(*InitDefD))
2741-
return true;
2742-
2743-
// Check that the base register is incremented by a constant value for each
2744-
// iteration.
2745-
MachineInstr *LoopDefS = MRI.getVRegDef(LoopValS);
2746-
int D = 0;
2747-
if (!LoopDefS || !TII->getIncrementValue(*LoopDefS, D))
2748-
return true;
2749-
2750-
LocationSize AccessSizeS = (*SI->memoperands_begin())->getSize();
2751-
LocationSize AccessSizeD = (*DI->memoperands_begin())->getSize();
2752-
2753-
// This is the main test, which checks the offset values and the loop
2754-
// increment value to determine if the accesses may be loop carried.
2755-
if (!AccessSizeS.hasValue() || !AccessSizeD.hasValue())
2756-
return true;
2757-
2758-
if (DeltaS != DeltaD || DeltaS < AccessSizeS.getValue() ||
2759-
DeltaD < AccessSizeD.getValue())
2760-
return true;
2761-
2762-
return (OffsetS + (int64_t)AccessSizeS.getValue() <
2763-
OffsetD + (int64_t)AccessSizeD.getValue());
2877+
return mayOverlapInLaterIter(DI, SI);
27642878
}
27652879

27662880
void SwingSchedulerDAG::postProcessDAG() {

llvm/test/CodeGen/Hexagon/swp-carried-dep1.mir

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,13 @@
33

44
# Test that the loop carried dependence check correctly identifies a recurrence.
55

6+
# CHECK: Overlap check:
7+
# CHECK-NEXT: BaseMI: S2_storerh_io %{{[0-9]+}}:intregs, 0, %{{[0-9]+}}:intregs :: (store (s16) into %ir.lsr.iv24)
8+
# CHECK-NEXT: Base + 0 + I * 4, Len: 2
9+
# CHECK-NEXT: OtherMI: %{{[0-9]+}}:intregs = L2_loadrh_io %{{[0-9]+}}:intregs, -8 :: (load (s16) from %ir.cgep10)
10+
# CHECK-NEXT: Base + -8 + I * 4, Len: 2
11+
# CHECK-NEXT: Result: Overlap
12+
613
# CHECK: Rec NodeSet
714
# CHECK: Rec NodeSet
815
# CHECK: Rec NodeSet

llvm/test/CodeGen/Hexagon/swp-carried-dep2.mir

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
# RUN: llc -mtriple=hexagon -run-pass pipeliner -debug-only=pipeliner %s -o /dev/null 2>&1 -pipeliner-experimental-cg=true | FileCheck %s
22
# REQUIRES: asserts
33

4-
# Test that the loop carried dependence check correctly identifies a recurrence
4+
# Test that it correctly recognizes that there is no loop carried dependence
55
# when the loop variable decreases and the array index offset is negative.
66

7-
# CHECK: Rec NodeSet
8-
# CHECK: Rec NodeSet
9-
# CHECK: SU(3)
10-
# CHECK: SU(4)
11-
# CHECK: SU(5)
7+
# CHECK: Overlap check:
8+
# CHECK-NEXT: BaseMI: S2_storeri_io %{{[0-9]+}}:intregs, 0, %{{[0-9]+}}:intregs :: (store (s32) into %ir.lsr.iv1)
9+
# CHECK-NEXT: Base + 0 + I * -4, Len: 4
10+
# CHECK-NEXT: OtherMI: %{{[0-9]+}}:intregs = L2_loadri_io %{{[0-9]+}}:intregs, -8 :: (load (s32) from %ir.cgep)
11+
# CHECK-NEXT: Base + -8 + I * -4, Len: 4
12+
# CHECK-NEXT: Result: No overlap
1213

1314
--- |
1415

llvm/test/CodeGen/Hexagon/swp-carried-dep3.mir

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,13 @@
77
# requires to use a single CHECK-NOT to match such a Rec NodeSet. Fortunately
88
# the atom '.' does not match a newline but anything else on a line.
99

10+
# CHECK: Overlap check:
11+
# CHECK-NEXT: BaseMI: %13:intregs = S2_storerh_pi %12:intregs(tied-def 0), 2, %20:intregs :: (store (s16))
12+
# CHECK-NEXT: Base + 0 + I * 2, Len: 2
13+
# CHECK-NEXT: OtherMI: %19:intregs, %15:intregs = L2_loadrh_pi %14:intregs(tied-def 1), 2 :: (load (s16))
14+
# CHECK-NEXT: Base + 0 + I * 2, Len: 2
15+
# CHECK-NEXT: Result: No overlap
16+
1017
# CHECK-NOT: Rec NodeSet{{.+[[:space:]]}} SU(5){{.+[[:space:]]}} SU(7)
1118

1219
...
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
# RUN: llc -mtriple=hexagon -run-pass pipeliner -debug-only=pipeliner %s -o /dev/null 2>&1 | FileCheck %s
2+
# REQUIRES: asserts
3+
4+
# Loop carried dependence is assumed in cases where increment value cannot be recognized
5+
# (Not supported for multiple increment instruction)
6+
7+
# CHECK: Rec NodeSet
8+
# CHECK: Rec NodeSet
9+
# CHECK-NEXT: SU(1)
10+
# CHECK-NEXT: SU(2)
11+
12+
---
13+
name: test
14+
tracksRegLiveness: true
15+
16+
body: |
17+
bb.0:
18+
successors: %bb.1
19+
20+
%10:intregs = IMPLICIT_DEF
21+
%11:intregs = IMPLICIT_DEF
22+
J2_loop0i %bb.1, 6, implicit-def $lc0, implicit-def $sa0, implicit-def $usr
23+
24+
bb.1 (machine-block-address-taken):
25+
successors: %bb.1, %bb.2
26+
27+
%0:intregs = PHI %11, %bb.0, %6, %bb.1
28+
%4:intregs = L2_loadri_io %0, 0 :: (load (s32))
29+
S2_storeri_io %0, 0, %10 :: (store (s32))
30+
%7:intregs = A2_addi %0, -8
31+
%6:intregs = A2_addi %7, 4
32+
ENDLOOP0 %bb.1, implicit-def $pc, implicit-def $lc0, implicit $sa0, implicit $lc0
33+
J2_jump %bb.2, implicit-def dead $pc
34+
35+
bb.2:
36+
37+
...

0 commit comments

Comments
 (0)