@@ -1460,6 +1460,55 @@ static bool validateTBDIfNeeded(const CompilerInvocation &Invocation,
1460
1460
Opts, allSymbols);
1461
1461
}
1462
1462
1463
+ enum class DeallocatableResources {
1464
+ None,
1465
+ SILModule,
1466
+ SILModuleAndASTContext,
1467
+ };
1468
+ static DeallocatableResources
1469
+ computeDeallocatableResources (const CompilerInvocation &Invocation,
1470
+ const CompilerInstance &Instance) {
1471
+ // If the stats reporter is installed, we need the ASTContext and SILModule
1472
+ // to live through the entire compilation process.
1473
+ if (Instance.getASTContext ().Stats ) {
1474
+ return DeallocatableResources::None;
1475
+ }
1476
+
1477
+ // If we're going to dump the API of the module, we cannot tear down
1478
+ // the ASTContext, as that would cause the module to be freed prematurely.
1479
+ if (!Invocation.getFrontendOptions ().DumpAPIPath .empty ()) {
1480
+ return DeallocatableResources::SILModule;
1481
+ }
1482
+
1483
+ // If there are multiple primary inputs it is too soon to free
1484
+ // the ASTContext, etc.. OTOH, if this compilation generates code for > 1
1485
+ // primary input, then freeing it after processing the last primary is
1486
+ // unlikely to reduce the peak heap size. So, only optimize the
1487
+ // single-primary-case (or WMO).
1488
+ if (Invocation.getFrontendOptions ()
1489
+ .InputsAndOutputs .hasMultiplePrimaryInputs ()) {
1490
+ return DeallocatableResources::SILModule;
1491
+ }
1492
+
1493
+ return DeallocatableResources::SILModuleAndASTContext;
1494
+ }
1495
+
1496
+ static void
1497
+ freeDeallocatableResourcesIfPossible (const CompilerInvocation &Invocation,
1498
+ CompilerInstance &Instance) {
1499
+ switch (computeDeallocatableResources (Invocation, Instance)) {
1500
+ case DeallocatableResources::None:
1501
+ break ;
1502
+ case DeallocatableResources::SILModule:
1503
+ Instance.freeSILModule ();
1504
+ break ;
1505
+ case DeallocatableResources::SILModuleAndASTContext:
1506
+ Instance.freeSILModule ();
1507
+ Instance.freeASTContext ();
1508
+ break ;
1509
+ }
1510
+ }
1511
+
1463
1512
static bool generateCode (const CompilerInvocation &Invocation,
1464
1513
CompilerInstance &Instance, StringRef OutputFilename,
1465
1514
llvm::Module *IRModule,
@@ -1469,19 +1518,8 @@ static bool generateCode(const CompilerInvocation &Invocation,
1469
1518
version::Version EffectiveLanguageVersion =
1470
1519
Instance.getASTContext ().LangOpts .EffectiveLanguageVersion ;
1471
1520
1472
- if (!Stats) {
1473
- // Free up some compiler resources now that we have an IRModule.
1474
- Instance.freeSILModule ();
1475
-
1476
- // If there are multiple primary inputs it is too soon to free
1477
- // the ASTContext, etc.. OTOH, if this compilation generates code for > 1
1478
- // primary input, then freeing it after processing the last primary is
1479
- // unlikely to reduce the peak heap size. So, only optimize the
1480
- // single-primary-case (or WMO).
1481
- if (!Invocation.getFrontendOptions ()
1482
- .InputsAndOutputs .hasMultiplePrimaryInputs ())
1483
- Instance.freeASTContext ();
1484
- }
1521
+ // Free up some compiler resources now that we have an IRModule.
1522
+ freeDeallocatableResourcesIfPossible (Invocation, Instance);
1485
1523
1486
1524
// Now that we have a single IR Module, hand it over to performLLVM.
1487
1525
return performLLVM (Invocation.getIRGenOptions (), Instance.getDiags (),
0 commit comments