@@ -437,10 +437,14 @@ class IndexBitcodeWriter : public BitcodeWriterBase {
437
437
// / index and a value id generated by this class to use in references.
438
438
std::map<GlobalValue::GUID, unsigned > GUIDToValueIdMap;
439
439
440
- // The sorted stack id indices actually used in the summary entries being
441
- // written, which will be a subset of those in the full index in the case of
442
- // distributed indexes.
443
- std::vector<unsigned > StackIdIndices;
440
+ // The stack ids used by this index, which will be a subset of those in
441
+ // the full index in the case of distributed indexes.
442
+ std::vector<uint64_t > StackIds;
443
+
444
+ // Keep a map of the stack id indices used by records being written for this
445
+ // index to the index of the corresponding stack id in the above StackIds
446
+ // vector. Ensures we write each referenced stack id once.
447
+ DenseMap<unsigned , unsigned > StackIdIndicesToIndex;
444
448
445
449
// / Tracks the last value id recorded in the GUIDToValueMap.
446
450
unsigned GlobalValueId = 0 ;
@@ -464,6 +468,19 @@ class IndexBitcodeWriter : public BitcodeWriterBase {
464
468
: BitcodeWriterBase(Stream, StrtabBuilder), Index(Index),
465
469
DecSummaries (DecSummaries),
466
470
ModuleToSummariesForIndex(ModuleToSummariesForIndex) {
471
+
472
+ // See if the StackIdIndex was already added to the StackId map and
473
+ // vector. If not, record it.
474
+ auto RecordStackIdReference = [&](unsigned StackIdIndex) {
475
+ // If the StackIdIndex is not yet in the map, the below insert ensures
476
+ // that it will point to the new StackIds vector entry we push to just
477
+ // below.
478
+ auto Inserted =
479
+ StackIdIndicesToIndex.insert ({StackIdIndex, StackIds.size ()});
480
+ if (Inserted.second )
481
+ StackIds.push_back (Index.getStackIdAtIndex (StackIdIndex));
482
+ };
483
+
467
484
// Assign unique value ids to all summaries to be written, for use
468
485
// in writing out the call graph edges. Save the mapping from GUID
469
486
// to the new global value id to use when writing those edges, which
@@ -494,17 +511,13 @@ class IndexBitcodeWriter : public BitcodeWriterBase {
494
511
continue ;
495
512
}
496
513
for (auto Idx : CI.StackIdIndices )
497
- StackIdIndices. push_back (Idx);
514
+ RecordStackIdReference (Idx);
498
515
}
499
516
for (auto &AI : FS->allocs ())
500
517
for (auto &MIB : AI.MIBs )
501
518
for (auto Idx : MIB.StackIdIndices )
502
- StackIdIndices. push_back (Idx);
519
+ RecordStackIdReference (Idx);
503
520
});
504
- llvm::sort (StackIdIndices);
505
- StackIdIndices.erase (
506
- std::unique (StackIdIndices.begin (), StackIdIndices.end ()),
507
- StackIdIndices.end ());
508
521
}
509
522
510
523
// / The below iterator returns the GUID and associated summary.
@@ -4492,18 +4505,15 @@ void IndexBitcodeWriter::writeCombinedGlobalValueSummary() {
4492
4505
ArrayRef<uint64_t >{GVI.second , GVI.first });
4493
4506
}
4494
4507
4495
- if (!StackIdIndices.empty ()) {
4508
+ // Write the stack ids used by this index, which will be a subset of those in
4509
+ // the full index in the case of distributed indexes.
4510
+ if (!StackIds.empty ()) {
4496
4511
auto StackIdAbbv = std::make_shared<BitCodeAbbrev>();
4497
4512
StackIdAbbv->Add (BitCodeAbbrevOp (bitc::FS_STACK_IDS));
4498
4513
// numids x stackid
4499
4514
StackIdAbbv->Add (BitCodeAbbrevOp (BitCodeAbbrevOp::Array));
4500
4515
StackIdAbbv->Add (BitCodeAbbrevOp (BitCodeAbbrevOp::VBR, 8 ));
4501
4516
unsigned StackIdAbbvId = Stream.EmitAbbrev (std::move (StackIdAbbv));
4502
- // Write the stack ids used by this index, which will be a subset of those in
4503
- // the full index in the case of distributed indexes.
4504
- std::vector<uint64_t > StackIds;
4505
- for (auto &I : StackIdIndices)
4506
- StackIds.push_back (Index.getStackIdAtIndex (I));
4507
4517
Stream.EmitRecord (bitc::FS_STACK_IDS, StackIds, StackIdAbbvId);
4508
4518
}
4509
4519
@@ -4669,11 +4679,11 @@ void IndexBitcodeWriter::writeCombinedGlobalValueSummary() {
4669
4679
return *ValueID;
4670
4680
},
4671
4681
/* GetStackIndex*/ [&](unsigned I) {
4672
- // Get the corresponding index into the list of StackIdIndices
4673
- // actually being written for this combined index (which may be a
4674
- // subset in the case of distributed indexes).
4675
- auto Lower = llvm::lower_bound (StackIdIndices, I );
4676
- return std::distance (StackIdIndices. begin (), Lower) ;
4682
+ // Get the corresponding index into the list of StackIds actually
4683
+ // being written for this combined index (which may be a subset in
4684
+ // the case of distributed indexes).
4685
+ assert (StackIdIndicesToIndex. contains (I) );
4686
+ return StackIdIndicesToIndex[I] ;
4677
4687
});
4678
4688
4679
4689
NameVals.push_back (*ValueId);
0 commit comments