Skip to content

Commit ca8eef7

Browse files
dpenrydavemgreen
authored andcommitted
[CodeGen] Use ProcResGroup information in SchedBoundary
When the ProcResGroup has BufferSize=0, 1. if there is a subunit in the list of write resources for the scheduling class, do not attempt to schedule the ProcResGroup. 2. if there is not a subunit in the list of write resources for the scheduling class, choose a subunit to use instead of the ProcResGroup. 3. having both the ProcResGroup and any of its subunits in the resources implied by a InstRW is not supported. Used to model parallel uses from a pool of resources. Differential Revision: https://reviews.llvm.org/D98976
1 parent 78a871a commit ca8eef7

File tree

3 files changed

+61
-10
lines changed

3 files changed

+61
-10
lines changed

llvm/include/llvm/CodeGen/MachineScheduler.h

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@
7474
#ifndef LLVM_CODEGEN_MACHINESCHEDULER_H
7575
#define LLVM_CODEGEN_MACHINESCHEDULER_H
7676

77+
#include "llvm/ADT/APInt.h"
7778
#include "llvm/ADT/ArrayRef.h"
7879
#include "llvm/ADT/BitVector.h"
7980
#include "llvm/ADT/STLExtras.h"
@@ -674,6 +675,9 @@ class SchedBoundary {
674675
// it.
675676
SmallVector<unsigned, 16> ReservedCyclesIndex;
676677

678+
// For each PIdx, stores the resource group IDs of its subunits
679+
SmallVector<APInt, 16> ResourceGroupSubUnitMasks;
680+
677681
#ifndef NDEBUG
678682
// Remember the greatest possible stall as an upper bound on the number of
679683
// times we should retry the pending queue because of a hazard.
@@ -751,9 +755,15 @@ class SchedBoundary {
751755
unsigned getNextResourceCycleByInstance(unsigned InstanceIndex,
752756
unsigned Cycles);
753757

754-
std::pair<unsigned, unsigned> getNextResourceCycle(unsigned PIdx,
758+
std::pair<unsigned, unsigned> getNextResourceCycle(const MCSchedClassDesc *SC,
759+
unsigned PIdx,
755760
unsigned Cycles);
756761

762+
bool isUnbufferedGroup(unsigned PIdx) const {
763+
return SchedModel->getProcResource(PIdx)->SubUnitsIdxBegin &&
764+
!SchedModel->getProcResource(PIdx)->BufferSize;
765+
}
766+
757767
bool checkHazard(SUnit *SU);
758768

759769
unsigned findMaxLatency(ArrayRef<SUnit*> ReadySUs);
@@ -775,7 +785,8 @@ class SchedBoundary {
775785

776786
void incExecutedResources(unsigned PIdx, unsigned Count);
777787

778-
unsigned countResource(unsigned PIdx, unsigned Cycles, unsigned ReadyCycle);
788+
unsigned countResource(const MCSchedClassDesc *SC, unsigned PIdx,
789+
unsigned Cycles, unsigned ReadyCycle);
779790

780791
void bumpNode(SUnit *SU);
781792

llvm/lib/CodeGen/MachineScheduler.cpp

Lines changed: 47 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2004,6 +2004,7 @@ void SchedBoundary::reset() {
20042004
IsResourceLimited = false;
20052005
ReservedCycles.clear();
20062006
ReservedCyclesIndex.clear();
2007+
ResourceGroupSubUnitMasks.clear();
20072008
#ifndef NDEBUG
20082009
// Track the maximum number of stall cycles that could arise either from the
20092010
// latency of a DAG edge or the number of cycles that a processor resource is
@@ -2045,11 +2046,18 @@ init(ScheduleDAGMI *dag, const TargetSchedModel *smodel, SchedRemainder *rem) {
20452046
unsigned ResourceCount = SchedModel->getNumProcResourceKinds();
20462047
ReservedCyclesIndex.resize(ResourceCount);
20472048
ExecutedResCounts.resize(ResourceCount);
2049+
ResourceGroupSubUnitMasks.resize(ResourceCount, APInt(ResourceCount, 0));
20482050
unsigned NumUnits = 0;
20492051

20502052
for (unsigned i = 0; i < ResourceCount; ++i) {
20512053
ReservedCyclesIndex[i] = NumUnits;
20522054
NumUnits += SchedModel->getProcResource(i)->NumUnits;
2055+
if (isUnbufferedGroup(i)) {
2056+
auto SubUnits = SchedModel->getProcResource(i)->SubUnitsIdxBegin;
2057+
for (unsigned U = 0, UE = SchedModel->getProcResource(i)->NumUnits;
2058+
U != UE; ++U)
2059+
ResourceGroupSubUnitMasks[i].setBit(SubUnits[U]);
2060+
}
20532061
}
20542062

20552063
ReservedCycles.resize(NumUnits, InvalidCycle);
@@ -2091,14 +2099,45 @@ unsigned SchedBoundary::getNextResourceCycleByInstance(unsigned InstanceIdx,
20912099
/// scheduled. Returns the next cycle and the index of the processor resource
20922100
/// instance in the reserved cycles vector.
20932101
std::pair<unsigned, unsigned>
2094-
SchedBoundary::getNextResourceCycle(unsigned PIdx, unsigned Cycles) {
2102+
SchedBoundary::getNextResourceCycle(const MCSchedClassDesc *SC, unsigned PIdx,
2103+
unsigned Cycles) {
2104+
20952105
unsigned MinNextUnreserved = InvalidCycle;
20962106
unsigned InstanceIdx = 0;
20972107
unsigned StartIndex = ReservedCyclesIndex[PIdx];
20982108
unsigned NumberOfInstances = SchedModel->getProcResource(PIdx)->NumUnits;
20992109
assert(NumberOfInstances > 0 &&
21002110
"Cannot have zero instances of a ProcResource");
21012111

2112+
if (isUnbufferedGroup(PIdx)) {
2113+
// If any subunits are used by the instruction, report that the resource
2114+
// group is available at 0, effectively removing the group record from
2115+
// hazarding and basing the hazarding decisions on the subunit records.
2116+
// Otherwise, choose the first available instance from among the subunits.
2117+
// Specifications which assign cycles to both the subunits and the group or
2118+
// which use an unbuffered group with buffered subunits will appear to
2119+
// schedule strangely. In the first case, the additional cycles for the
2120+
// group will be ignored. In the second, the group will be ignored
2121+
// entirely.
2122+
for (const MCWriteProcResEntry &PE :
2123+
make_range(SchedModel->getWriteProcResBegin(SC),
2124+
SchedModel->getWriteProcResEnd(SC)))
2125+
if (ResourceGroupSubUnitMasks[PIdx][PE.ProcResourceIdx])
2126+
return std::make_pair(0u, StartIndex);
2127+
2128+
auto SubUnits = SchedModel->getProcResource(PIdx)->SubUnitsIdxBegin;
2129+
for (unsigned I = 0, End = NumberOfInstances; I < End; ++I) {
2130+
unsigned NextUnreserved, NextInstanceIdx;
2131+
std::tie(NextUnreserved, NextInstanceIdx) =
2132+
getNextResourceCycle(SC, SubUnits[I], Cycles);
2133+
if (MinNextUnreserved > NextUnreserved) {
2134+
InstanceIdx = NextInstanceIdx;
2135+
MinNextUnreserved = NextUnreserved;
2136+
}
2137+
}
2138+
return std::make_pair(MinNextUnreserved, InstanceIdx);
2139+
}
2140+
21022141
for (unsigned I = StartIndex, End = StartIndex + NumberOfInstances; I < End;
21032142
++I) {
21042143
unsigned NextUnreserved = getNextResourceCycleByInstance(I, Cycles);
@@ -2152,7 +2191,7 @@ bool SchedBoundary::checkHazard(SUnit *SU) {
21522191
unsigned ResIdx = PE.ProcResourceIdx;
21532192
unsigned Cycles = PE.Cycles;
21542193
unsigned NRCycle, InstanceIdx;
2155-
std::tie(NRCycle, InstanceIdx) = getNextResourceCycle(ResIdx, Cycles);
2194+
std::tie(NRCycle, InstanceIdx) = getNextResourceCycle(SC, ResIdx, Cycles);
21562195
if (NRCycle > CurrCycle) {
21572196
#ifndef NDEBUG
21582197
MaxObservedStall = std::max(Cycles, MaxObservedStall);
@@ -2302,8 +2341,8 @@ void SchedBoundary::incExecutedResources(unsigned PIdx, unsigned Count) {
23022341
///
23032342
/// \return the next cycle at which the instruction may execute without
23042343
/// oversubscribing resources.
2305-
unsigned SchedBoundary::
2306-
countResource(unsigned PIdx, unsigned Cycles, unsigned NextCycle) {
2344+
unsigned SchedBoundary::countResource(const MCSchedClassDesc *SC, unsigned PIdx,
2345+
unsigned Cycles, unsigned NextCycle) {
23072346
unsigned Factor = SchedModel->getResourceFactor(PIdx);
23082347
unsigned Count = Factor * Cycles;
23092348
LLVM_DEBUG(dbgs() << " " << SchedModel->getResourceName(PIdx) << " +"
@@ -2325,7 +2364,7 @@ countResource(unsigned PIdx, unsigned Cycles, unsigned NextCycle) {
23252364
}
23262365
// For reserved resources, record the highest cycle using the resource.
23272366
unsigned NextAvailable, InstanceIdx;
2328-
std::tie(NextAvailable, InstanceIdx) = getNextResourceCycle(PIdx, Cycles);
2367+
std::tie(NextAvailable, InstanceIdx) = getNextResourceCycle(SC, PIdx, Cycles);
23292368
if (NextAvailable > CurrCycle) {
23302369
LLVM_DEBUG(dbgs() << " Resource conflict: "
23312370
<< SchedModel->getResourceName(PIdx)
@@ -2405,7 +2444,7 @@ void SchedBoundary::bumpNode(SUnit *SU) {
24052444
PI = SchedModel->getWriteProcResBegin(SC),
24062445
PE = SchedModel->getWriteProcResEnd(SC); PI != PE; ++PI) {
24072446
unsigned RCycle =
2408-
countResource(PI->ProcResourceIdx, PI->Cycles, NextCycle);
2447+
countResource(SC, PI->ProcResourceIdx, PI->Cycles, NextCycle);
24092448
if (RCycle > NextCycle)
24102449
NextCycle = RCycle;
24112450
}
@@ -2420,7 +2459,8 @@ void SchedBoundary::bumpNode(SUnit *SU) {
24202459
unsigned PIdx = PI->ProcResourceIdx;
24212460
if (SchedModel->getProcResource(PIdx)->BufferSize == 0) {
24222461
unsigned ReservedUntil, InstanceIdx;
2423-
std::tie(ReservedUntil, InstanceIdx) = getNextResourceCycle(PIdx, 0);
2462+
std::tie(ReservedUntil, InstanceIdx) =
2463+
getNextResourceCycle(SC, PIdx, 0);
24242464
if (isTop()) {
24252465
ReservedCycles[InstanceIdx] =
24262466
std::max(ReservedUntil, NextCycle + PI->Cycles);

llvm/test/CodeGen/ARM/cortex-m7-wideops.mir

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,9 @@ body: |
2222
; CHECK-LABEL: name: test_groups
2323
; CHECK: liveins: $d0, $r0, $r1, $r2, $r3, $r4
2424
; CHECK: renamable $d0 = VADDD killed renamable $d0, renamable $d0, 14 /* CC::al */, $noreg
25+
; CHECK: renamable $r3 = t2ADDrr killed renamable $r3, renamable $r3, 14 /* CC::al */, $noreg, $noreg
2526
; CHECK: renamable $s2 = VLDRS killed renamable $r0, 0, 14 /* CC::al */, $noreg
2627
; CHECK: VSTRS killed renamable $s2, killed renamable $r1, 0, 14 /* CC::al */, $noreg
27-
; CHECK: renamable $r3 = t2ADDrr killed renamable $r3, renamable $r3, 14 /* CC::al */, $noreg, $noreg
2828
; CHECK: t2STRi12 killed renamable $r3, killed renamable $r2, 0, 14 /* CC::al */, $noreg
2929
; CHECK: renamable $r4 = t2ADDrr killed renamable $r4, renamable $r4, 14 /* CC::al */, $noreg, $noreg
3030
; CHECK: tBX_RET 14 /* CC::al */, $noreg, implicit killed $d0

0 commit comments

Comments
 (0)