@@ -148,6 +148,20 @@ static cl::opt<bool> EmulateOldLDV("emulate-old-livedebugvalues", cl::Hidden,
148
148
cl::desc (" Act like old LiveDebugValues did" ),
149
149
cl::init(false ));
150
150
151
+ // Limit for the maximum number of stack slots we should track, past which we
152
+ // will ignore any spills. InstrRefBasedLDV gathers detailed information on all
153
+ // stack slots which leads to high memory consumption, and in some scenarios
154
+ // (such as asan with very many locals) the working set of the function can be
155
+ // very large, causing many spills. In these scenarios, it is very unlikely that
156
+ // the developer has hundreds of variables live at the same time that they're
157
+ // carefully thinking about -- instead, they probably autogenerated the code.
158
+ // When this happens, gracefully stop tracking excess spill slots, rather than
159
+ // consuming all the developer's memory.
160
+ static cl::opt<unsigned >
161
+ StackWorkingSetLimit (" livedebugvalues-max-stack-slots" , cl::Hidden,
162
+ cl::desc (" livedebugvalues-stack-ws-limit" ),
163
+ cl::init(250 ));
164
+
151
165
// / Tracker for converting machine value locations and variable values into
152
166
// / variable locations (the output of LiveDebugValues), recorded as DBG_VALUEs
153
167
// / specifying block live-in locations and transfers within blocks.
@@ -757,9 +771,15 @@ void MLocTracker::writeRegMask(const MachineOperand *MO, unsigned CurBB,
757
771
Masks.push_back (std::make_pair (MO, InstID));
758
772
}
759
773
760
- SpillLocationNo MLocTracker::getOrTrackSpillLoc (SpillLoc L) {
774
+ Optional< SpillLocationNo> MLocTracker::getOrTrackSpillLoc (SpillLoc L) {
761
775
SpillLocationNo SpillID (SpillLocs.idFor (L));
776
+
762
777
if (SpillID.id () == 0 ) {
778
+ // If there is no location, and we have reached the limit of how many stack
779
+ // slots to track, then don't track this one.
780
+ if (SpillLocs.size () >= StackWorkingSetLimit)
781
+ return None;
782
+
763
783
// Spill location is untracked: create record for this one, and all
764
784
// subregister slots too.
765
785
SpillID = SpillLocationNo (SpillLocs.insert (L));
@@ -898,7 +918,7 @@ bool InstrRefBasedLDV::isCalleeSaved(LocIdx L) const {
898
918
// void InstrRefBasedLDV::printVarLocInMBB(..)
899
919
#endif
900
920
901
- SpillLocationNo
921
+ Optional< SpillLocationNo>
902
922
InstrRefBasedLDV::extractSpillBaseRegAndOffset (const MachineInstr &MI) {
903
923
assert (MI.hasOneMemOperand () &&
904
924
" Spill instruction does not have exactly one memory operand?" );
@@ -913,8 +933,11 @@ InstrRefBasedLDV::extractSpillBaseRegAndOffset(const MachineInstr &MI) {
913
933
return MTracker->getOrTrackSpillLoc ({Reg, Offset});
914
934
}
915
935
916
- Optional<LocIdx> InstrRefBasedLDV::findLocationForMemOperand (const MachineInstr &MI) {
917
- SpillLocationNo SpillLoc = extractSpillBaseRegAndOffset (MI);
936
+ Optional<LocIdx>
937
+ InstrRefBasedLDV::findLocationForMemOperand (const MachineInstr &MI) {
938
+ Optional<SpillLocationNo> SpillLoc = extractSpillBaseRegAndOffset (MI);
939
+ if (!SpillLoc)
940
+ return None;
918
941
919
942
// Where in the stack slot is this value defined -- i.e., what size of value
920
943
// is this? An important question, because it could be loaded into a register
@@ -930,7 +953,7 @@ Optional<LocIdx> InstrRefBasedLDV::findLocationForMemOperand(const MachineInstr
930
953
// occur, but the safe action is to indicate the variable is optimised out.
931
954
return None;
932
955
933
- unsigned SpillID = MTracker->getSpillIDWithIdx (SpillLoc, IdxIt->second );
956
+ unsigned SpillID = MTracker->getSpillIDWithIdx (* SpillLoc, IdxIt->second );
934
957
return MTracker->getSpillMLoc (SpillID);
935
958
}
936
959
@@ -1251,7 +1274,12 @@ bool InstrRefBasedLDV::transferDebugPHI(MachineInstr &MI) {
1251
1274
Register Base;
1252
1275
StackOffset Offs = TFI->getFrameIndexReference (*MI.getMF (), FI, Base);
1253
1276
SpillLoc SL = {Base, Offs};
1254
- SpillLocationNo SpillNo = MTracker->getOrTrackSpillLoc (SL);
1277
+ Optional<SpillLocationNo> SpillNo = MTracker->getOrTrackSpillLoc (SL);
1278
+
1279
+ // We might be able to find a value, but have chosen not to, to avoid
1280
+ // tracking too much stack information.
1281
+ if (!SpillNo)
1282
+ return true ;
1255
1283
1256
1284
// Problem: what value should we extract from the stack? LLVM does not
1257
1285
// record what size the last store to the slot was, and it would become
@@ -1263,7 +1291,7 @@ bool InstrRefBasedLDV::transferDebugPHI(MachineInstr &MI) {
1263
1291
Optional<ValueIDNum> Result = None;
1264
1292
Optional<LocIdx> SpillLoc = None;
1265
1293
for (unsigned CS : CandidateSizes) {
1266
- unsigned SpillID = MTracker->getLocID (SpillNo, {CS, 0 });
1294
+ unsigned SpillID = MTracker->getLocID (* SpillNo, {CS, 0 });
1267
1295
SpillLoc = MTracker->getSpillMLoc (SpillID);
1268
1296
ValueIDNum Val = MTracker->readMLoc (*SpillLoc);
1269
1297
// If this value was defined in it's own position, then it was probably
@@ -1280,7 +1308,7 @@ bool InstrRefBasedLDV::transferDebugPHI(MachineInstr &MI) {
1280
1308
// "supposed" to be is more complex, and benefits a small number of
1281
1309
// locations.
1282
1310
if (!Result) {
1283
- unsigned SpillID = MTracker->getLocID (SpillNo, {64 , 0 });
1311
+ unsigned SpillID = MTracker->getLocID (* SpillNo, {64 , 0 });
1284
1312
SpillLoc = MTracker->getSpillMLoc (SpillID);
1285
1313
Result = MTracker->readMLoc (*SpillLoc);
1286
1314
}
@@ -1357,11 +1385,12 @@ void InstrRefBasedLDV::transferRegisterDef(MachineInstr &MI) {
1357
1385
1358
1386
// If this instruction writes to a spill slot, def that slot.
1359
1387
if (hasFoldedStackStore (MI)) {
1360
- SpillLocationNo SpillNo = extractSpillBaseRegAndOffset (MI);
1361
- for (unsigned int I = 0 ; I < MTracker->NumSlotIdxes ; ++I) {
1362
- unsigned SpillID = MTracker->getSpillIDWithIdx (SpillNo, I);
1363
- LocIdx L = MTracker->getSpillMLoc (SpillID);
1364
- MTracker->setMLoc (L, ValueIDNum (CurBB, CurInst, L));
1388
+ if (Optional<SpillLocationNo> SpillNo = extractSpillBaseRegAndOffset (MI)) {
1389
+ for (unsigned int I = 0 ; I < MTracker->NumSlotIdxes ; ++I) {
1390
+ unsigned SpillID = MTracker->getSpillIDWithIdx (*SpillNo, I);
1391
+ LocIdx L = MTracker->getSpillMLoc (SpillID);
1392
+ MTracker->setMLoc (L, ValueIDNum (CurBB, CurInst, L));
1393
+ }
1365
1394
}
1366
1395
}
1367
1396
@@ -1396,11 +1425,12 @@ void InstrRefBasedLDV::transferRegisterDef(MachineInstr &MI) {
1396
1425
1397
1426
// Tell TTracker about any folded stack store.
1398
1427
if (hasFoldedStackStore (MI)) {
1399
- SpillLocationNo SpillNo = extractSpillBaseRegAndOffset (MI);
1400
- for (unsigned int I = 0 ; I < MTracker->NumSlotIdxes ; ++I) {
1401
- unsigned SpillID = MTracker->getSpillIDWithIdx (SpillNo, I);
1402
- LocIdx L = MTracker->getSpillMLoc (SpillID);
1403
- TTracker->clobberMloc (L, MI.getIterator (), true );
1428
+ if (Optional<SpillLocationNo> SpillNo = extractSpillBaseRegAndOffset (MI)) {
1429
+ for (unsigned int I = 0 ; I < MTracker->NumSlotIdxes ; ++I) {
1430
+ unsigned SpillID = MTracker->getSpillIDWithIdx (*SpillNo, I);
1431
+ LocIdx L = MTracker->getSpillMLoc (SpillID);
1432
+ TTracker->clobberMloc (L, MI.getIterator (), true );
1433
+ }
1404
1434
}
1405
1435
}
1406
1436
}
@@ -1436,23 +1466,24 @@ void InstrRefBasedLDV::performCopy(Register SrcRegNum, Register DstRegNum) {
1436
1466
}
1437
1467
}
1438
1468
1439
- bool InstrRefBasedLDV::isSpillInstruction (const MachineInstr &MI,
1440
- MachineFunction *MF) {
1469
+ Optional<SpillLocationNo>
1470
+ InstrRefBasedLDV::isSpillInstruction (const MachineInstr &MI,
1471
+ MachineFunction *MF) {
1441
1472
// TODO: Handle multiple stores folded into one.
1442
1473
if (!MI.hasOneMemOperand ())
1443
- return false ;
1474
+ return None ;
1444
1475
1445
1476
// Reject any memory operand that's aliased -- we can't guarantee its value.
1446
1477
auto MMOI = MI.memoperands_begin ();
1447
1478
const PseudoSourceValue *PVal = (*MMOI)->getPseudoValue ();
1448
1479
if (PVal->isAliased (MFI))
1449
- return false ;
1480
+ return None ;
1450
1481
1451
1482
if (!MI.getSpillSize (TII) && !MI.getFoldedSpillSize (TII))
1452
- return false ; // This is not a spill instruction, since no valid size was
1453
- // returned from either function.
1483
+ return None ; // This is not a spill instruction, since no valid size was
1484
+ // returned from either function.
1454
1485
1455
- return true ;
1486
+ return extractSpillBaseRegAndOffset (MI) ;
1456
1487
}
1457
1488
1458
1489
bool InstrRefBasedLDV::isLocationSpill (const MachineInstr &MI,
@@ -1509,13 +1540,11 @@ bool InstrRefBasedLDV::transferSpillOrRestoreInst(MachineInstr &MI) {
1509
1540
// First, if there are any DBG_VALUEs pointing at a spill slot that is
1510
1541
// written to, terminate that variable location. The value in memory
1511
1542
// will have changed. DbgEntityHistoryCalculator doesn't try to detect this.
1512
- if (isSpillInstruction (MI, MF)) {
1513
- SpillLocationNo Loc = extractSpillBaseRegAndOffset (MI);
1514
-
1543
+ if (Optional<SpillLocationNo> Loc = isSpillInstruction (MI, MF)) {
1515
1544
// Un-set this location and clobber, so that earlier locations don't
1516
1545
// continue past this store.
1517
1546
for (unsigned SlotIdx = 0 ; SlotIdx < MTracker->NumSlotIdxes ; ++SlotIdx) {
1518
- unsigned SpillID = MTracker->getSpillIDWithIdx (Loc, SlotIdx);
1547
+ unsigned SpillID = MTracker->getSpillIDWithIdx (* Loc, SlotIdx);
1519
1548
Optional<LocIdx> MLoc = MTracker->getSpillMLoc (SpillID);
1520
1549
if (!MLoc)
1521
1550
continue ;
@@ -1533,7 +1562,9 @@ bool InstrRefBasedLDV::transferSpillOrRestoreInst(MachineInstr &MI) {
1533
1562
1534
1563
// Try to recognise spill and restore instructions that may transfer a value.
1535
1564
if (isLocationSpill (MI, MF, Reg)) {
1536
- SpillLocationNo Loc = extractSpillBaseRegAndOffset (MI);
1565
+ // isLocationSpill returning true should guarantee we can extract a
1566
+ // location.
1567
+ SpillLocationNo Loc = *extractSpillBaseRegAndOffset (MI);
1537
1568
1538
1569
auto DoTransfer = [&](Register SrcReg, unsigned SpillID) {
1539
1570
auto ReadValue = MTracker->readReg (SrcReg);
@@ -1560,10 +1591,9 @@ bool InstrRefBasedLDV::transferSpillOrRestoreInst(MachineInstr &MI) {
1560
1591
unsigned SpillID = MTracker->getLocID (Loc, {Size , 0 });
1561
1592
DoTransfer (Reg, SpillID);
1562
1593
} else {
1563
- Optional<SpillLocationNo> OptLoc = isRestoreInstruction (MI, MF, Reg);
1564
- if (!OptLoc )
1594
+ Optional<SpillLocationNo> Loc = isRestoreInstruction (MI, MF, Reg);
1595
+ if (!Loc )
1565
1596
return false ;
1566
- SpillLocationNo Loc = *OptLoc;
1567
1597
1568
1598
// Assumption: we're reading from the base of the stack slot, not some
1569
1599
// offset into it. It seems very unlikely LLVM would ever generate
@@ -1590,13 +1620,13 @@ bool InstrRefBasedLDV::transferSpillOrRestoreInst(MachineInstr &MI) {
1590
1620
1591
1621
for (MCSubRegIterator SRI (Reg, TRI, false ); SRI.isValid (); ++SRI) {
1592
1622
unsigned Subreg = TRI->getSubRegIndex (Reg, *SRI);
1593
- unsigned SpillID = MTracker->getLocID (Loc, Subreg);
1623
+ unsigned SpillID = MTracker->getLocID (* Loc, Subreg);
1594
1624
DoTransfer (*SRI, SpillID);
1595
1625
}
1596
1626
1597
1627
// Directly look up this registers slot idx by size, and transfer.
1598
1628
unsigned Size = TRI->getRegSizeInBits (Reg, *MRI);
1599
- unsigned SpillID = MTracker->getLocID (Loc, {Size , 0 });
1629
+ unsigned SpillID = MTracker->getLocID (* Loc, {Size , 0 });
1600
1630
DoTransfer (Reg, SpillID);
1601
1631
}
1602
1632
return true ;
0 commit comments