Skip to content

Commit fc9b9e8

Browse files
authored
Revert "MTM: fix issues after cursory reading" (#100559)
Reverts #100404 This broke the gcc7 build here: https://lab.llvm.org/buildbot/#/builders/116/builds/1724
1 parent f6431f0 commit fc9b9e8

File tree

1 file changed

+55
-46
lines changed

1 file changed

+55
-46
lines changed

llvm/lib/CodeGen/MachineTraceMetrics.cpp

Lines changed: 55 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,17 @@
2424
#include "llvm/CodeGen/TargetSchedule.h"
2525
#include "llvm/CodeGen/TargetSubtargetInfo.h"
2626
#include "llvm/InitializePasses.h"
27+
#include "llvm/MC/MCRegisterInfo.h"
2728
#include "llvm/Pass.h"
2829
#include "llvm/Support/Debug.h"
2930
#include "llvm/Support/ErrorHandling.h"
3031
#include "llvm/Support/Format.h"
3132
#include "llvm/Support/raw_ostream.h"
33+
#include <algorithm>
34+
#include <cassert>
35+
#include <iterator>
36+
#include <tuple>
37+
#include <utility>
3238

3339
using namespace llvm;
3440

@@ -127,7 +133,7 @@ MachineTraceMetrics::getResources(const MachineBasicBlock *MBB) {
127133

128134
// Scale the resource cycles so they are comparable.
129135
unsigned PROffset = MBB->getNumber() * PRKinds;
130-
for (unsigned K = 0; K < PRKinds; ++K)
136+
for (unsigned K = 0; K != PRKinds; ++K)
131137
ProcReleaseAtCycles[PROffset + K] =
132138
PRCycles[K] * SchedModel.getResourceFactor(K);
133139

@@ -140,14 +146,15 @@ MachineTraceMetrics::getProcReleaseAtCycles(unsigned MBBNum) const {
140146
"getResources() must be called before getProcReleaseAtCycles()");
141147
unsigned PRKinds = SchedModel.getNumProcResourceKinds();
142148
assert((MBBNum+1) * PRKinds <= ProcReleaseAtCycles.size());
143-
return ArrayRef{ProcReleaseAtCycles.data() + MBBNum * PRKinds, PRKinds};
149+
return ArrayRef(ProcReleaseAtCycles.data() + MBBNum * PRKinds, PRKinds);
144150
}
145151

146152
//===----------------------------------------------------------------------===//
147153
// Ensemble utility functions
148154
//===----------------------------------------------------------------------===//
149155

150-
MachineTraceMetrics::Ensemble::Ensemble(MachineTraceMetrics *CT) : MTM(*CT) {
156+
MachineTraceMetrics::Ensemble::Ensemble(MachineTraceMetrics *ct)
157+
: MTM(*ct) {
151158
BlockInfo.resize(MTM.BlockInfo.size());
152159
unsigned PRKinds = MTM.SchedModel.getNumProcResourceKinds();
153160
ProcResourceDepths.resize(MTM.BlockInfo.size() * PRKinds);
@@ -191,7 +198,7 @@ computeDepthResources(const MachineBasicBlock *MBB) {
191198
// Compute per-resource depths.
192199
ArrayRef<unsigned> PredPRDepths = getProcResourceDepths(PredNum);
193200
ArrayRef<unsigned> PredPRCycles = MTM.getProcReleaseAtCycles(PredNum);
194-
for (unsigned K = 0; K < PRKinds; ++K)
201+
for (unsigned K = 0; K != PRKinds; ++K)
195202
ProcResourceDepths[PROffset + K] = PredPRDepths[K] + PredPRCycles[K];
196203
}
197204

@@ -224,7 +231,7 @@ computeHeightResources(const MachineBasicBlock *MBB) {
224231

225232
// Compute per-resource heights.
226233
ArrayRef<unsigned> SuccPRHeights = getProcResourceHeights(SuccNum);
227-
for (unsigned K = 0; K < PRKinds; ++K)
234+
for (unsigned K = 0; K != PRKinds; ++K)
228235
ProcResourceHeights[PROffset + K] = SuccPRHeights[K] + PRCycles[K];
229236
}
230237

@@ -257,7 +264,7 @@ MachineTraceMetrics::Ensemble::
257264
getProcResourceDepths(unsigned MBBNum) const {
258265
unsigned PRKinds = MTM.SchedModel.getNumProcResourceKinds();
259266
assert((MBBNum+1) * PRKinds <= ProcResourceDepths.size());
260-
return ArrayRef{ProcResourceDepths.data() + MBBNum * PRKinds, PRKinds};
267+
return ArrayRef(ProcResourceDepths.data() + MBBNum * PRKinds, PRKinds);
261268
}
262269

263270
/// Get an array of processor resource heights for MBB. Indexed by processor
@@ -270,7 +277,7 @@ MachineTraceMetrics::Ensemble::
270277
getProcResourceHeights(unsigned MBBNum) const {
271278
unsigned PRKinds = MTM.SchedModel.getNumProcResourceKinds();
272279
assert((MBBNum+1) * PRKinds <= ProcResourceHeights.size());
273-
return ArrayRef{ProcResourceHeights.data() + MBBNum * PRKinds, PRKinds};
280+
return ArrayRef(ProcResourceHeights.data() + MBBNum * PRKinds, PRKinds);
274281
}
275282

276283
//===----------------------------------------------------------------------===//
@@ -307,8 +314,8 @@ class MinInstrCountEnsemble : public MachineTraceMetrics::Ensemble {
307314
const MachineBasicBlock *pickTraceSucc(const MachineBasicBlock*) override;
308315

309316
public:
310-
MinInstrCountEnsemble(MachineTraceMetrics *MTM)
311-
: MachineTraceMetrics::Ensemble(MTM) {}
317+
MinInstrCountEnsemble(MachineTraceMetrics *mtm)
318+
: MachineTraceMetrics::Ensemble(mtm) {}
312319
};
313320

314321
/// Pick only the current basic block for the trace and do not choose any
@@ -388,15 +395,15 @@ MinInstrCountEnsemble::pickTraceSucc(const MachineBasicBlock *MBB) {
388395

389396
// Get an Ensemble sub-class for the requested trace strategy.
390397
MachineTraceMetrics::Ensemble *
391-
MachineTraceMetrics::getEnsemble(MachineTraceStrategy Strategy) {
392-
assert(Strategy < MachineTraceStrategy::TS_NumStrategies &&
398+
MachineTraceMetrics::getEnsemble(MachineTraceStrategy strategy) {
399+
assert(strategy < MachineTraceStrategy::TS_NumStrategies &&
393400
"Invalid trace strategy enum");
394-
Ensemble *&E = Ensembles[static_cast<size_t>(Strategy)];
401+
Ensemble *&E = Ensembles[static_cast<size_t>(strategy)];
395402
if (E)
396403
return E;
397404

398405
// Allocate new Ensemble on demand.
399-
switch (Strategy) {
406+
switch (strategy) {
400407
case MachineTraceStrategy::TS_MinInstrCount:
401408
return (E = new MinInstrCountEnsemble(this));
402409
case MachineTraceStrategy::TS_Local:
@@ -441,9 +448,8 @@ struct LoopBounds {
441448
const MachineLoopInfo *Loops;
442449
bool Downward = false;
443450

444-
LoopBounds(MutableArrayRef<MachineTraceMetrics::TraceBlockInfo> Blocks,
445-
const MachineLoopInfo *Loops)
446-
: Blocks(Blocks), Loops(Loops) {}
451+
LoopBounds(MutableArrayRef<MachineTraceMetrics::TraceBlockInfo> blocks,
452+
const MachineLoopInfo *loops) : Blocks(blocks), Loops(loops) {}
447453
};
448454

449455
} // end anonymous namespace
@@ -457,7 +463,7 @@ class po_iterator_storage<LoopBounds, true> {
457463
LoopBounds &LB;
458464

459465
public:
460-
po_iterator_storage(LoopBounds &LB) : LB(LB) {}
466+
po_iterator_storage(LoopBounds &lb) : LB(lb) {}
461467

462468
void finishPostorder(const MachineBasicBlock*) {}
463469

@@ -540,7 +546,7 @@ MachineTraceMetrics::Ensemble::invalidate(const MachineBasicBlock *BadMBB) {
540546
if (BadTBI.hasValidHeight()) {
541547
BadTBI.invalidateHeight();
542548
WorkList.push_back(BadMBB);
543-
while (!WorkList.empty()) {
549+
do {
544550
const MachineBasicBlock *MBB = WorkList.pop_back_val();
545551
LLVM_DEBUG(dbgs() << "Invalidate " << printMBBReference(*MBB) << ' '
546552
<< getName() << " height.\n");
@@ -558,14 +564,14 @@ MachineTraceMetrics::Ensemble::invalidate(const MachineBasicBlock *BadMBB) {
558564
// Verify that TBI.Succ is actually a *I successor.
559565
assert((!TBI.Succ || Pred->isSuccessor(TBI.Succ)) && "CFG changed");
560566
}
561-
}
567+
} while (!WorkList.empty());
562568
}
563569

564570
// Invalidate depth resources of blocks below MBB.
565571
if (BadTBI.hasValidDepth()) {
566572
BadTBI.invalidateDepth();
567573
WorkList.push_back(BadMBB);
568-
while (!WorkList.empty()) {
574+
do {
569575
const MachineBasicBlock *MBB = WorkList.pop_back_val();
570576
LLVM_DEBUG(dbgs() << "Invalidate " << printMBBReference(*MBB) << ' '
571577
<< getName() << " depth.\n");
@@ -583,7 +589,7 @@ MachineTraceMetrics::Ensemble::invalidate(const MachineBasicBlock *BadMBB) {
583589
// Verify that TBI.Pred is actually a *I predecessor.
584590
assert((!TBI.Pred || Succ->isPredecessor(TBI.Pred)) && "CFG changed");
585591
}
586-
}
592+
} while (!WorkList.empty());
587593
}
588594

589595
// Clear any per-instruction data. We only have to do this for BadMBB itself
@@ -599,7 +605,7 @@ void MachineTraceMetrics::Ensemble::verify() const {
599605
#ifndef NDEBUG
600606
assert(BlockInfo.size() == MTM.MF->getNumBlockIDs() &&
601607
"Outdated BlockInfo size");
602-
for (unsigned Num = 0; Num < BlockInfo.size(); ++Num) {
608+
for (unsigned Num = 0, e = BlockInfo.size(); Num != e; ++Num) {
603609
const TraceBlockInfo &TBI = BlockInfo[Num];
604610
if (TBI.hasValidDepth() && TBI.Pred) {
605611
const MachineBasicBlock *MBB = MTM.MF->getBlockNumbered(Num);
@@ -680,7 +686,7 @@ static bool getDataDeps(const MachineInstr &UseMI,
680686
}
681687
// Collect virtual register reads.
682688
if (MO.readsReg())
683-
Deps.emplace_back(MRI, Reg, MO.getOperandNo());
689+
Deps.push_back(DataDep(MRI, Reg, MO.getOperandNo()));
684690
}
685691
return HasPhysRegs;
686692
}
@@ -696,10 +702,10 @@ static void getPHIDeps(const MachineInstr &UseMI,
696702
if (!Pred)
697703
return;
698704
assert(UseMI.isPHI() && UseMI.getNumOperands() % 2 && "Bad PHI");
699-
for (unsigned Idx = 1; Idx < UseMI.getNumOperands(); Idx += 2) {
700-
if (UseMI.getOperand(Idx + 1).getMBB() == Pred) {
701-
Register Reg = UseMI.getOperand(Idx).getReg();
702-
Deps.emplace_back(MRI, Reg, Idx);
705+
for (unsigned i = 1; i != UseMI.getNumOperands(); i += 2) {
706+
if (UseMI.getOperand(i + 1).getMBB() == Pred) {
707+
Register Reg = UseMI.getOperand(i).getReg();
708+
Deps.push_back(DataDep(MRI, Reg, i));
703709
return;
704710
}
705711
}
@@ -733,7 +739,7 @@ static void updatePhysDepsDownwards(const MachineInstr *UseMI,
733739
SparseSet<LiveRegUnit>::iterator I = RegUnits.find(Unit);
734740
if (I == RegUnits.end())
735741
continue;
736-
Deps.emplace_back(I->MI, I->Op, MO.getOperandNo());
742+
Deps.push_back(DataDep(I->MI, I->Op, MO.getOperandNo()));
737743
break;
738744
}
739745
}
@@ -846,14 +852,14 @@ computeInstrDepths(const MachineBasicBlock *MBB) {
846852
// implies Head->HasValidInstrDepths, so we only need to start from the first
847853
// block in the trace that needs to be recomputed.
848854
SmallVector<const MachineBasicBlock*, 8> Stack;
849-
while (MBB) {
855+
do {
850856
TraceBlockInfo &TBI = BlockInfo[MBB->getNumber()];
851857
assert(TBI.hasValidDepth() && "Incomplete trace");
852858
if (TBI.HasValidInstrDepths)
853859
break;
854860
Stack.push_back(MBB);
855861
MBB = TBI.Pred;
856-
}
862+
} while (MBB);
857863

858864
// FIXME: If MBB is non-null at this point, it is the last pre-computed block
859865
// in the trace. We should track any live-out physregs that were defined in
@@ -874,7 +880,7 @@ computeInstrDepths(const MachineBasicBlock *MBB) {
874880
LLVM_DEBUG({
875881
dbgs() << format("%7u Instructions\n", TBI.InstrDepth);
876882
ArrayRef<unsigned> PRDepths = getProcResourceDepths(MBB->getNumber());
877-
for (unsigned K = 0; K < PRDepths.size(); ++K)
883+
for (unsigned K = 0; K != PRDepths.size(); ++K)
878884
if (PRDepths[K]) {
879885
unsigned Factor = MTM.SchedModel.getResourceFactor(K);
880886
dbgs() << format("%6uc @ ", MTM.getCycles(PRDepths[K]))
@@ -963,8 +969,10 @@ static bool pushDepHeight(const DataDep &Dep, const MachineInstr &UseMI,
963969
Dep.UseOp);
964970

965971
// Update Heights[DefMI] to be the maximum height seen.
966-
const auto &[I, Inserted] = Heights.insert({Dep.DefMI, UseHeight});
967-
if (Inserted)
972+
MIHeightMap::iterator I;
973+
bool New;
974+
std::tie(I, New) = Heights.insert(std::make_pair(Dep.DefMI, UseHeight));
975+
if (New)
968976
return true;
969977

970978
// DefMI has been pushed before. Give it the max height.
@@ -1002,15 +1010,15 @@ computeInstrHeights(const MachineBasicBlock *MBB) {
10021010
// The bottom of the trace may already be computed.
10031011
// Find the blocks that need updating.
10041012
SmallVector<const MachineBasicBlock*, 8> Stack;
1005-
while (MBB) {
1013+
do {
10061014
TraceBlockInfo &TBI = BlockInfo[MBB->getNumber()];
10071015
assert(TBI.hasValidHeight() && "Incomplete trace");
10081016
if (TBI.HasValidInstrHeights)
10091017
break;
10101018
Stack.push_back(MBB);
10111019
TBI.LiveIns.clear();
10121020
MBB = TBI.Succ;
1013-
}
1021+
} while (MBB);
10141022

10151023
// As we move upwards in the trace, keep track of instructions that are
10161024
// required by deeper trace instructions. Map MI -> height required so far.
@@ -1052,7 +1060,7 @@ computeInstrHeights(const MachineBasicBlock *MBB) {
10521060
LLVM_DEBUG({
10531061
dbgs() << format("%7u Instructions\n", TBI.InstrHeight);
10541062
ArrayRef<unsigned> PRHeights = getProcResourceHeights(MBB->getNumber());
1055-
for (unsigned K = 0; K < PRHeights.size(); ++K)
1063+
for (unsigned K = 0; K != PRHeights.size(); ++K)
10561064
if (PRHeights[K]) {
10571065
unsigned Factor = MTM.SchedModel.getResourceFactor(K);
10581066
dbgs() << format("%6uc @ ", MTM.getCycles(PRHeights[K]))
@@ -1137,7 +1145,7 @@ computeInstrHeights(const MachineBasicBlock *MBB) {
11371145

11381146
// Transfer the live regunits to the live-in list.
11391147
for (const LiveRegUnit &RU : RegUnits) {
1140-
TBI.LiveIns.emplace_back(RU.RegUnit, RU.Cycle);
1148+
TBI.LiveIns.push_back(LiveInReg(RU.RegUnit, RU.Cycle));
11411149
LLVM_DEBUG(dbgs() << ' ' << printRegUnit(RU.RegUnit, MTM.TRI) << '@'
11421150
<< RU.Cycle);
11431151
}
@@ -1197,7 +1205,7 @@ unsigned MachineTraceMetrics::Trace::getResourceDepth(bool Bottom) const {
11971205
ArrayRef<unsigned> PRDepths = TE.getProcResourceDepths(getBlockNum());
11981206
if (Bottom) {
11991207
ArrayRef<unsigned> PRCycles = TE.MTM.getProcReleaseAtCycles(getBlockNum());
1200-
for (unsigned K = 0; K < PRDepths.size(); ++K)
1208+
for (unsigned K = 0; K != PRDepths.size(); ++K)
12011209
PRMax = std::max(PRMax, PRDepths[K] + PRCycles[K]);
12021210
} else {
12031211
for (unsigned PRD : PRDepths)
@@ -1227,8 +1235,9 @@ unsigned MachineTraceMetrics::Trace::getResourceLength(
12271235
unsigned PRMax = 0;
12281236

12291237
// Capture computing cycles from extra instructions
1230-
auto ExtraCycles = [this](ArrayRef<const MCSchedClassDesc *> Instrs,
1231-
unsigned ResourceIdx) -> unsigned {
1238+
auto extraCycles = [this](ArrayRef<const MCSchedClassDesc *> Instrs,
1239+
unsigned ResourceIdx)
1240+
->unsigned {
12321241
unsigned Cycles = 0;
12331242
for (const MCSchedClassDesc *SC : Instrs) {
12341243
if (!SC->isValid())
@@ -1246,12 +1255,12 @@ unsigned MachineTraceMetrics::Trace::getResourceLength(
12461255
return Cycles;
12471256
};
12481257

1249-
for (unsigned K = 0; K < PRDepths.size(); ++K) {
1258+
for (unsigned K = 0; K != PRDepths.size(); ++K) {
12501259
unsigned PRCycles = PRDepths[K] + PRHeights[K];
12511260
for (const MachineBasicBlock *MBB : Extrablocks)
12521261
PRCycles += TE.MTM.getProcReleaseAtCycles(MBB->getNumber())[K];
1253-
PRCycles += ExtraCycles(ExtraInstrs, K);
1254-
PRCycles -= ExtraCycles(RemoveInstrs, K);
1262+
PRCycles += extraCycles(ExtraInstrs, K);
1263+
PRCycles -= extraCycles(RemoveInstrs, K);
12551264
PRMax = std::max(PRMax, PRCycles);
12561265
}
12571266
// Convert to cycle count.
@@ -1283,9 +1292,9 @@ bool MachineTraceMetrics::Trace::isDepInTrace(const MachineInstr &DefMI,
12831292

12841293
void MachineTraceMetrics::Ensemble::print(raw_ostream &OS) const {
12851294
OS << getName() << " ensemble:\n";
1286-
for (unsigned Idx = 0; Idx < BlockInfo.size(); ++Idx) {
1287-
OS << " %bb." << Idx << '\t';
1288-
BlockInfo[Idx].print(OS);
1295+
for (unsigned i = 0, e = BlockInfo.size(); i != e; ++i) {
1296+
OS << " %bb." << i << '\t';
1297+
BlockInfo[i].print(OS);
12891298
OS << '\n';
12901299
}
12911300
}

0 commit comments

Comments
 (0)