@@ -1065,9 +1065,9 @@ bool CodeLoopSinking::loopSink(Loop *L, LoopSinkMode Mode) {
1065
1065
auto getLeafInstToCandidateMap = [&](BasicBlock *TgtBB, CandidatePtrVec &Candidates,
1066
1066
InstToCandidateMap &InstToCandidate) {
1067
1067
InstToCandidateMap LeafInstToCandidate;
1068
- SmallSet<Candidate *, 32 > NotLeafCandidates;
1068
+ CandidatePtrSet NotLeafCandidates;
1069
1069
1070
- for (Candidate * C : Candidates) {
1070
+ for (const auto & C : Candidates) {
1071
1071
PrintDump (VerbosityLevel::High, " Finding leaf candidates... Checking:\n " );
1072
1072
for (Instruction *I : *C) {
1073
1073
PrintInstructionDump (VerbosityLevel::High, I);
@@ -1081,7 +1081,7 @@ bool CodeLoopSinking::loopSink(Loop *L, LoopSinkMode Mode) {
1081
1081
continue ;
1082
1082
1083
1083
if (InstToCandidate.count (Op)) {
1084
- Candidate * OpCandidate = InstToCandidate[Op];
1084
+ const auto & OpCandidate = InstToCandidate[Op];
1085
1085
if (OpCandidate != C) {
1086
1086
PrintDump (VerbosityLevel::High, " Operand uses the current candidate, so is not a leaf:\n " );
1087
1087
PrintInstructionDump (VerbosityLevel::High, Op);
@@ -1091,7 +1091,7 @@ bool CodeLoopSinking::loopSink(Loop *L, LoopSinkMode Mode) {
1091
1091
}
1092
1092
}
1093
1093
}
1094
- for (Candidate * C : Candidates) {
1094
+ for (const auto & C : Candidates) {
1095
1095
if (NotLeafCandidates.count (C))
1096
1096
continue ;
1097
1097
@@ -1111,8 +1111,8 @@ bool CodeLoopSinking::loopSink(Loop *L, LoopSinkMode Mode) {
1111
1111
bool Changed = false ;
1112
1112
1113
1113
CandidatePtrVec SinkedCandidatesPtrs;
1114
- for (auto CI = SinkedCandidates.begin (), CE = SinkedCandidates.end (); CI != CE; CI++) {
1115
- Candidate * C = CI-> get () ;
1114
+ for (auto * CI = SinkedCandidates.begin (), * CE = SinkedCandidates.end (); CI != CE; CI++) {
1115
+ const auto & C = *CI ;
1116
1116
if (C->TgtBB == BB)
1117
1117
SinkedCandidatesPtrs.push_back (C);
1118
1118
}
@@ -1187,7 +1187,7 @@ bool CodeLoopSinking::loopSink(Loop *L, LoopSinkMode Mode) {
1187
1187
continue ;
1188
1188
1189
1189
Changed = true ;
1190
- SinkCandidates.push_back (std::make_unique <Candidate>(I, TgtBB, Worthiness, I->getNextNode ()));
1190
+ SinkCandidates.push_back (std::make_shared <Candidate>(I, TgtBB, Worthiness, I->getNextNode ()));
1191
1191
continue ;
1192
1192
}
1193
1193
@@ -1209,15 +1209,16 @@ bool CodeLoopSinking::loopSink(Loop *L, LoopSinkMode Mode) {
1209
1209
InstToCandidateMap CurrentInstToCandidate;
1210
1210
1211
1211
// Candidate ownership:
1212
- // Unique pointers are created in CurrentSinkCandidates on every iteration.
1213
- // Then they are moved to ToSink collection to be sinked (done in refineLoopSinkCandidates).
1214
- // Then they are moved to SinkedCandidates within iteration if they are actually sinked.
1215
- // The actually sinked Candidates have therefore live time until the end ot loopSink function.
1216
-
1217
- // CurrentInstToCandidate and InstToCandidate are maps Instruction->Candidate *
1212
+ // Shared pointers are created in CurrentSinkCandidates on every iteration.
1213
+ // Then they are put in ToSink collection to be sinked (done in refineLoopSinkCandidates).
1214
+ // Then they are put in SinkedCandidates within iteration if they are actually sinked.
1215
+ // The actually sinked Candidates have therefore lifetime until the end ot loopSink function.
1216
+ //
1217
+ // CurrentInstToCandidate and InstToCandidate are maps Instruction->std::shared_ptr<Candidate>
1218
+ //
1219
+ // It's assumed that using std::shared_ptr we will successfully ensure only needed Candidates
1220
+ // will remain.
1218
1221
1219
- // It's assumed the pointers are never invalidated, because the Candidate object is created
1220
- // via make_unique and is not relocated and destroyed until the end of the function
1221
1222
1222
1223
InstSet SkipInstructions;
1223
1224
@@ -1270,7 +1271,7 @@ bool CodeLoopSinking::loopSink(Loop *L, LoopSinkMode Mode) {
1270
1271
PrintInstructionDump (VerbosityLevel::Medium, I);
1271
1272
1272
1273
CurrentSinkCandidates.push_back (
1273
- std::make_unique <Candidate>(I, I->getParent (), LoopSinkWorthiness::IntraLoopSink, I->getNextNode ()));
1274
+ std::make_shared <Candidate>(I, I->getParent (), LoopSinkWorthiness::IntraLoopSink, I->getNextNode ()));
1274
1275
Found2dBlockReads = true ;
1275
1276
}
1276
1277
@@ -1317,19 +1318,18 @@ bool CodeLoopSinking::loopSink(Loop *L, LoopSinkMode Mode) {
1317
1318
if (C->Worthiness == LoopSinkWorthiness::Sink || C->Worthiness == LoopSinkWorthiness::IntraLoopSink) {
1318
1319
IGC_ASSERT (C->size () > 0 );
1319
1320
1320
- SinkedCandidates.push_back (std::move (C));
1321
- Candidate *SC = SinkedCandidates.back ().get ();
1321
+ SinkedCandidates.push_back (C);
1322
1322
1323
- bool SinkFromPH = SC ->Worthiness == LoopSinkWorthiness::Sink;
1324
- Instruction *InsertPoint = SinkFromPH ? &*SC ->TgtBB ->getFirstInsertionPt () : SC ->first ()->getNextNode ();
1323
+ bool SinkFromPH = C ->Worthiness == LoopSinkWorthiness::Sink;
1324
+ Instruction *InsertPoint = SinkFromPH ? &*(C ->TgtBB ->getFirstInsertionPt ()) : C ->first ()->getNextNode ();
1325
1325
1326
- for (Instruction *I : *SC ) {
1326
+ for (Instruction *I : *C ) {
1327
1327
PrintDump (VerbosityLevel::Medium,
1328
1328
(SinkFromPH ? " Sinking instruction:\n " : " Scheduling instruction for local sink:\n " ));
1329
1329
PrintInstructionDump (VerbosityLevel::Medium, I);
1330
1330
1331
- CurrentInstToCandidate[I] = SC ;
1332
- InstToCandidate[I] = SC ;
1331
+ CurrentInstToCandidate[I] = C ;
1332
+ InstToCandidate[I] = C ;
1333
1333
1334
1334
I->moveBefore (InsertPoint);
1335
1335
InsertPoint = I;
@@ -1340,8 +1340,8 @@ bool CodeLoopSinking::loopSink(Loop *L, LoopSinkMode Mode) {
1340
1340
}
1341
1341
}
1342
1342
1343
- UndoBlkSet.insert (SC ->UndoPos ->getParent ());
1344
- LocalBlkSet.insert (SC ->TgtBB );
1343
+ UndoBlkSet.insert (C ->UndoPos ->getParent ());
1344
+ LocalBlkSet.insert (C ->TgtBB );
1345
1345
1346
1346
PrintDump (VerbosityLevel::Medium, " \n " );
1347
1347
IterChanged = true ;
@@ -1503,7 +1503,7 @@ bool CodeLoopSinking::loopSink(Loop *L, LoopSinkMode Mode) {
1503
1503
// We decided we don't rollback, change the names of the instructions in IR
1504
1504
for (auto &Pair : InstToCandidate) {
1505
1505
Instruction *I = Pair.first ;
1506
- Candidate * C = Pair.second ;
1506
+ const auto & C = Pair.second ;
1507
1507
if (I->getType ()->isVoidTy ())
1508
1508
continue ;
1509
1509
std::string Prefix = C->Worthiness == LoopSinkWorthiness::IntraLoopSink ? " sched" : " sink" ;
@@ -1775,7 +1775,7 @@ bool CodeLoopSinking::tryCreate2dBlockReadGroupSinkingCandidate(Instruction *I,
1775
1775
if (IGC_IS_FLAG_ENABLED (LoopSinkCoarserLoadsRescheduling)) {
1776
1776
if (allUsesAreDominatedByRemainingUses (CurrentCandidateInsts, RemainingCandidateInsts)) {
1777
1777
NCandidates++;
1778
- SinkCandidates.push_back (std::make_unique <Candidate>(CurrentCandidateInsts, TgtBB, Worthiness,
1778
+ SinkCandidates.push_back (std::make_shared <Candidate>(CurrentCandidateInsts, TgtBB, Worthiness,
1779
1779
CurrentCandidateInsts[0 ]->getNextNode ()));
1780
1780
CurrentCandidateInsts.clear ();
1781
1781
}
@@ -1823,7 +1823,7 @@ bool CodeLoopSinking::tryCreate2dBlockReadGroupSinkingCandidate(Instruction *I,
1823
1823
1824
1824
assertOneLoad (CurrentCandidateInsts);
1825
1825
NCandidates++;
1826
- SinkCandidates.push_back (std::make_unique <Candidate>(CurrentCandidateInsts, TgtBB, Worthiness,
1826
+ SinkCandidates.push_back (std::make_shared <Candidate>(CurrentCandidateInsts, TgtBB, Worthiness,
1827
1827
CurrentCandidateInsts[0 ]->getNextNode ()));
1828
1828
CurrentCandidateInsts.clear ();
1829
1829
}
@@ -1839,7 +1839,7 @@ bool CodeLoopSinking::tryCreate2dBlockReadGroupSinkingCandidate(Instruction *I,
1839
1839
}
1840
1840
NCandidates++;
1841
1841
SinkCandidates.push_back (
1842
- std::make_unique <Candidate>(CurrentCandidateInsts, TgtBB, Worthiness, CurrentCandidateInsts[0 ]->getNextNode ()));
1842
+ std::make_shared <Candidate>(CurrentCandidateInsts, TgtBB, Worthiness, CurrentCandidateInsts[0 ]->getNextNode ()));
1843
1843
}
1844
1844
1845
1845
PrintDump (VerbosityLevel::Medium, " Successfully created " << NCandidates << " candidates.\n " );
@@ -1983,8 +1983,7 @@ bool CodeLoopSinking::tryCreateShufflePatternCandidates(BasicBlock *BB, Loop *L,
1983
1983
}
1984
1984
1985
1985
DenseMap<InsertElementInst *, InstSet> DestVecToShuffleInst;
1986
- CandidateVec ShuffleCandidates;
1987
- DenseMap<Instruction *, Candidate *> ShuffleInstToCandidate;
1986
+ InstToCandidateMap ShuffleInstToCandidate;
1988
1987
1989
1988
for (auto &VecIEs : SourceVectors) {
1990
1989
DestVecToShuffleInst.clear ();
@@ -2028,12 +2027,12 @@ bool CodeLoopSinking::tryCreateShufflePatternCandidates(BasicBlock *BB, Loop *L,
2028
2027
PrintDump (VerbosityLevel::Medium, " DestVector used in the loop:\n " );
2029
2028
PrintInstructionDump (VerbosityLevel::Medium, DestVec);
2030
2029
2031
- ShuffleCandidates. push_back ( std::make_unique <Candidate>(
2032
- InstrVec{}, TgtBB, SinkFromPH ? LoopSinkWorthiness::Sink : LoopSinkWorthiness::IntraLoopSink, nullptr )) ;
2030
+ auto C = std::make_shared <Candidate>(
2031
+ InstrVec{}, TgtBB, SinkFromPH ? LoopSinkWorthiness::Sink : LoopSinkWorthiness::IntraLoopSink, nullptr );
2033
2032
Changed = true ;
2034
2033
2035
2034
for (Instruction *I : ShuffleInst) {
2036
- ShuffleInstToCandidate[I] = ShuffleCandidates. back (). get () ;
2035
+ ShuffleInstToCandidate[I] = C ;
2037
2036
}
2038
2037
}
2039
2038
}
@@ -2047,7 +2046,7 @@ bool CodeLoopSinking::tryCreateShufflePatternCandidates(BasicBlock *BB, Loop *L,
2047
2046
Instruction *I = &*IB;
2048
2047
2049
2048
if (ShuffleInstToCandidate.count (I)) {
2050
- Candidate * C = ShuffleInstToCandidate[I];
2049
+ auto & C = ShuffleInstToCandidate[I];
2051
2050
if (C->size () == 0 ) {
2052
2051
C->UndoPos = I->getNextNode ();
2053
2052
ShuffleCandidatesOrdered.push_back (C);
@@ -2058,8 +2057,8 @@ bool CodeLoopSinking::tryCreateShufflePatternCandidates(BasicBlock *BB, Loop *L,
2058
2057
}
2059
2058
2060
2059
// Add the candidates to the main list
2061
- for (auto * C : ShuffleCandidatesOrdered) {
2062
- SinkCandidates.push_back (std::make_unique<Candidate>(*C) );
2060
+ for (const auto & C : ShuffleCandidatesOrdered) {
2061
+ SinkCandidates.push_back (C );
2063
2062
}
2064
2063
return Changed;
2065
2064
}
@@ -2276,7 +2275,7 @@ CodeLoopSinking::CandidateVec CodeLoopSinking::refineLoopSinkCandidates(Candidat
2276
2275
InstSet &LoadChains, Loop *L) {
2277
2276
struct OperandUseGroup {
2278
2277
SmallPtrSet<Value *, 4 > Operands;
2279
- SmallVector<std::unique_ptr <Candidate> * , 16 > Users;
2278
+ SmallVector<std::shared_ptr <Candidate>, 16 > Users;
2280
2279
2281
2280
void print (raw_ostream &OS) {
2282
2281
OS << " OUG " << Operands.size () << " -> " << Users.size () << " \n " ;
@@ -2289,7 +2288,7 @@ CodeLoopSinking::CandidateVec CodeLoopSinking::refineLoopSinkCandidates(Candidat
2289
2288
OS << " Users:\n " ;
2290
2289
for (auto &C : Users) {
2291
2290
OS << " " ;
2292
- (*C) ->print (OS);
2291
+ C ->print (OS);
2293
2292
OS << " \n " ;
2294
2293
}
2295
2294
}
@@ -2358,8 +2357,8 @@ CodeLoopSinking::CandidateVec CodeLoopSinking::refineLoopSinkCandidates(Candidat
2358
2357
};
2359
2358
2360
2359
auto allUsersAreLoadChains = [&](OperandUseGroup &OUG) {
2361
- return std::all_of (OUG.Users .begin (), OUG.Users .end (), [&](std::unique_ptr <Candidate> * C) {
2362
- return std::all_of ((*C) ->begin (), (*C) ->end (), [&](Instruction *I) { return isLoadChain (I, LoadChains); });
2360
+ return std::all_of (OUG.Users .begin (), OUG.Users .end (), [&](std::shared_ptr <Candidate> C) {
2361
+ return std::all_of (C ->begin (), C ->end (), [&](Instruction *I) { return isLoadChain (I, LoadChains); });
2363
2362
});
2364
2363
};
2365
2364
@@ -2378,8 +2377,8 @@ CodeLoopSinking::CandidateVec CodeLoopSinking::refineLoopSinkCandidates(Candidat
2378
2377
}
2379
2378
2380
2379
bool AllUsersAreUniform = true ;
2381
- for (auto &C : OUG.Users ) {
2382
- for (Value *V : ** C) {
2380
+ for (const auto &C : OUG.Users ) {
2381
+ for (Value *V : *C) {
2383
2382
if (!V->hasNUsesOrMore (1 ))
2384
2383
continue ;
2385
2384
if (!isUsedOnlyInLoop (V, L))
@@ -2449,9 +2448,9 @@ CodeLoopSinking::CandidateVec CodeLoopSinking::refineLoopSinkCandidates(Candidat
2449
2448
2450
2449
CandidateVec ToSink;
2451
2450
2452
- for (auto &C : SinkCandidates) {
2451
+ for (const auto &C : SinkCandidates) {
2453
2452
if (C->Worthiness == LoopSinkWorthiness::Sink || C->Worthiness == LoopSinkWorthiness::IntraLoopSink) {
2454
- ToSink.push_back (std::move (C) );
2453
+ ToSink.push_back (C );
2455
2454
continue ;
2456
2455
}
2457
2456
@@ -2465,9 +2464,9 @@ CodeLoopSinking::CandidateVec CodeLoopSinking::refineLoopSinkCandidates(Candidat
2465
2464
});
2466
2465
2467
2466
if (it != InstUseInfo.end ())
2468
- it->Users .push_back (& C);
2467
+ it->Users .push_back (C);
2469
2468
else
2470
- InstUseInfo.push_back (OperandUseGroup{CandidateOperands, {& C}});
2469
+ InstUseInfo.push_back (OperandUseGroup{CandidateOperands, {C}});
2471
2470
}
2472
2471
2473
2472
// Check if it's beneficial to sink every OUG
@@ -2480,8 +2479,8 @@ CodeLoopSinking::CandidateVec CodeLoopSinking::refineLoopSinkCandidates(Candidat
2480
2479
continue ;
2481
2480
PrintDump (VerbosityLevel::Medium, " >> Beneficial to sink.\n\n " );
2482
2481
for (auto &C : OUG.Users ) {
2483
- (*C) ->Worthiness = LoopSinkWorthiness::Sink;
2484
- ToSink.push_back (std::move (*C) );
2482
+ C ->Worthiness = LoopSinkWorthiness::Sink;
2483
+ ToSink.push_back (C );
2485
2484
}
2486
2485
}
2487
2486
@@ -2635,7 +2634,7 @@ bool CodeLoopSinking::localSink(BasicBlock *BB, InstToCandidateMap &InstToCandid
2635
2634
PrintDump (VerbosityLevel::Medium, " Found candidate to local sink:\n " );
2636
2635
PrintInstructionDump (VerbosityLevel::Medium, Def);
2637
2636
2638
- Candidate * C = Cit->second ;
2637
+ const auto & C = Cit->second ;
2639
2638
2640
2639
IGC_ASSERT (C->size () > 0 );
2641
2640
Instruction *MainInst = C->first ();
0 commit comments