@@ -592,7 +592,9 @@ LTO::LTO(Config Conf, ThinBackend Backend,
592
592
unsigned ParallelCodeGenParallelismLevel, LTOKind LTOMode)
593
593
: Conf(std::move(Conf)),
594
594
RegularLTO(ParallelCodeGenParallelismLevel, this ->Conf),
595
- ThinLTO(std::move(Backend)), LTOMode(LTOMode) {}
595
+ ThinLTO(std::move(Backend)),
596
+ GlobalResolutions(std::make_optional<StringMap<GlobalResolution>>()),
597
+ LTOMode(LTOMode) {}
596
598
597
599
// Requires a destructor for MapVector<BitcodeModule>.
598
600
LTO::~LTO () = default ;
@@ -610,7 +612,7 @@ void LTO::addModuleToGlobalRes(ArrayRef<InputFile::Symbol> Syms,
610
612
assert (ResI != ResE);
611
613
SymbolResolution Res = *ResI++;
612
614
613
- auto &GlobalRes = GlobalResolutions[Sym.getName ()];
615
+ auto &GlobalRes = (* GlobalResolutions) [Sym.getName ()];
614
616
GlobalRes.UnnamedAddr &= Sym.isUnnamedAddr ();
615
617
if (Res.Prevailing ) {
616
618
assert (!GlobalRes.Prevailing &&
@@ -1125,7 +1127,7 @@ Error LTO::run(AddStreamFn AddStream, FileCache Cache) {
1125
1127
// Compute "dead" symbols, we don't want to import/export these!
1126
1128
DenseSet<GlobalValue::GUID> GUIDPreservedSymbols;
1127
1129
DenseMap<GlobalValue::GUID, PrevailingType> GUIDPrevailingResolutions;
1128
- for (auto &Res : GlobalResolutions) {
1130
+ for (auto &Res : * GlobalResolutions) {
1129
1131
// Normally resolution have IR name of symbol. We can do nothing here
1130
1132
// otherwise. See comments in GlobalResolution struct for more details.
1131
1133
if (Res.second .IRName .empty ())
@@ -1169,6 +1171,8 @@ Error LTO::run(AddStreamFn AddStream, FileCache Cache) {
1169
1171
1170
1172
Error Result = runRegularLTO (AddStream);
1171
1173
if (!Result)
1174
+ // This will reset the GlobalResolutions optional once done with it to
1175
+ // reduce peak memory before importing.
1172
1176
Result = runThinLTO (AddStream, Cache, GUIDPreservedSymbols);
1173
1177
1174
1178
if (StatsFile)
@@ -1273,8 +1277,8 @@ Error LTO::runRegularLTO(AddStreamFn AddStream) {
1273
1277
// This returns true when the name is local or not defined. Locals are
1274
1278
// expected to be handled separately.
1275
1279
auto IsVisibleToRegularObj = [&](StringRef name) {
1276
- auto It = GlobalResolutions. find (name);
1277
- return (It == GlobalResolutions. end () || It->second .VisibleOutsideSummary );
1280
+ auto It = GlobalResolutions-> find (name);
1281
+ return (It == GlobalResolutions-> end () || It->second .VisibleOutsideSummary );
1278
1282
};
1279
1283
1280
1284
// If allowed, upgrade public vcall visibility metadata to linkage unit
@@ -1291,7 +1295,7 @@ Error LTO::runRegularLTO(AddStreamFn AddStream) {
1291
1295
return finalizeOptimizationRemarks (std::move (DiagnosticOutputFile));
1292
1296
1293
1297
if (!Conf.CodeGenOnly ) {
1294
- for (const auto &R : GlobalResolutions) {
1298
+ for (const auto &R : * GlobalResolutions) {
1295
1299
GlobalValue *GV =
1296
1300
RegularLTO.CombinedModule ->getNamedValue (R.second .IRName );
1297
1301
if (!R.second .isPrevailingIRSymbol ())
@@ -1708,8 +1712,8 @@ Error LTO::runThinLTO(AddStreamFn AddStream, FileCache Cache,
1708
1712
// This returns true when the name is local or not defined. Locals are
1709
1713
// expected to be handled separately.
1710
1714
auto IsVisibleToRegularObj = [&](StringRef name) {
1711
- auto It = GlobalResolutions. find (name);
1712
- return (It == GlobalResolutions. end () ||
1715
+ auto It = GlobalResolutions-> find (name);
1716
+ return (It == GlobalResolutions-> end () ||
1713
1717
It->second .VisibleOutsideSummary );
1714
1718
};
1715
1719
@@ -1739,15 +1743,11 @@ Error LTO::runThinLTO(AddStreamFn AddStream, FileCache Cache,
1739
1743
ContextDisambiguation.run (ThinLTO.CombinedIndex , isPrevailing);
1740
1744
}
1741
1745
1742
- if (Conf.OptLevel > 0 )
1743
- ComputeCrossModuleImport (ThinLTO.CombinedIndex , ModuleToDefinedGVSummaries,
1744
- isPrevailing, ImportLists, ExportLists);
1745
-
1746
1746
// Figure out which symbols need to be internalized. This also needs to happen
1747
1747
// at -O0 because summary-based DCE is implemented using internalization, and
1748
1748
// we must apply DCE consistently with the full LTO module in order to avoid
1749
1749
// undefined references during the final link.
1750
- for (auto &Res : GlobalResolutions) {
1750
+ for (auto &Res : * GlobalResolutions) {
1751
1751
// If the symbol does not have external references or it is not prevailing,
1752
1752
// then not need to mark it as exported from a ThinLTO partition.
1753
1753
if (Res.second .Partition != GlobalResolution::External ||
@@ -1760,6 +1760,16 @@ Error LTO::runThinLTO(AddStreamFn AddStream, FileCache Cache,
1760
1760
ExportedGUIDs.insert (GUID);
1761
1761
}
1762
1762
1763
+ // Reset the GlobalResolutions to deallocate the associated memory, as there
1764
+ // are no further accesses. We specifically want to do this before computing
1765
+ // cross module importing, which adds to peak memory via the computed import
1766
+ // and export lists.
1767
+ GlobalResolutions.reset ();
1768
+
1769
+ if (Conf.OptLevel > 0 )
1770
+ ComputeCrossModuleImport (ThinLTO.CombinedIndex , ModuleToDefinedGVSummaries,
1771
+ isPrevailing, ImportLists, ExportLists);
1772
+
1763
1773
// Any functions referenced by the jump table in the regular LTO object must
1764
1774
// be exported.
1765
1775
for (auto &Def : ThinLTO.CombinedIndex .cfiFunctionDefs ())
0 commit comments