Skip to content

Commit 581c3ac

Browse files
author
David Ungar
authored
Merge pull request swiftlang#14532 from davidungar/PR-18-6-ReferencedNameTracker
[Batch Mode] Enable per-primary paths for ReferencedNameTracker. (5)
2 parents e785a64 + 488db9f commit 581c3ac

File tree

14 files changed

+113
-114
lines changed

14 files changed

+113
-114
lines changed

include/swift/AST/Module.h

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -22,18 +22,19 @@
2222
#include "swift/AST/Identifier.h"
2323
#include "swift/AST/LookupKinds.h"
2424
#include "swift/AST/RawComment.h"
25+
#include "swift/AST/ReferencedNameTracker.h"
2526
#include "swift/AST/Type.h"
2627
#include "swift/Basic/Compiler.h"
2728
#include "swift/Basic/OptionSet.h"
28-
#include "swift/Basic/SourceLoc.h"
2929
#include "swift/Basic/STLExtras.h"
30+
#include "swift/Basic/SourceLoc.h"
3031
#include "llvm/ADT/ArrayRef.h"
3132
#include "llvm/ADT/DenseSet.h"
33+
#include "llvm/ADT/STLExtras.h"
3234
#include "llvm/ADT/SetVector.h"
3335
#include "llvm/ADT/SmallSet.h"
3436
#include "llvm/ADT/SmallVector.h"
3537
#include "llvm/ADT/StringMap.h"
36-
#include "llvm/ADT/STLExtras.h"
3738
#include "llvm/ADT/TinyPtrVector.h"
3839
#include "llvm/Support/ErrorHandling.h"
3940
#include "llvm/Support/MD5.h"
@@ -811,7 +812,7 @@ class SourceFile final : public FileUnit {
811812
TypeRefinementContext *TRC = nullptr;
812813

813814
/// If non-null, used to track name lookups that happen within this file.
814-
ReferencedNameTracker *ReferencedNames = nullptr;
815+
Optional<ReferencedNameTracker> ReferencedNames;
815816

816817
/// The class in this file marked \@NS/UIApplicationMain.
817818
ClassDecl *MainClass = nullptr;
@@ -966,13 +967,10 @@ class SourceFile final : public FileUnit {
966967
SourceLoc diagLoc = {});
967968
/// @}
968969

969-
ReferencedNameTracker *getReferencedNameTracker() const {
970-
return ReferencedNames;
971-
}
972-
void setReferencedNameTracker(ReferencedNameTracker *Tracker) {
973-
assert(!ReferencedNames && "This file already has a name tracker.");
974-
ReferencedNames = Tracker;
970+
ReferencedNameTracker *getReferencedNameTracker() {
971+
return ReferencedNames ? ReferencedNames.getPointer() : nullptr;
975972
}
973+
void createReferencedNameTracker();
976974

977975
/// \brief The buffer ID for the file that was imported, or None if there
978976
/// is no associated buffer.

include/swift/Frontend/Frontend.h

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -302,8 +302,11 @@ class CompilerInvocation {
302302
return FrontendOpts.InputKind == InputFileKind::IFK_Swift_Library;
303303
}
304304

305-
PrimarySpecificPaths getPrimarySpecificPathsForAtMostOnePrimary();
306-
PrimarySpecificPaths getPrimarySpecificPathsForPrimary(StringRef filename);
305+
PrimarySpecificPaths getPrimarySpecificPathsForAtMostOnePrimary() const;
306+
PrimarySpecificPaths
307+
getPrimarySpecificPathsForPrimary(StringRef filename) const;
308+
PrimarySpecificPaths
309+
getPrimarySpecificPathsForSourceFile(const SourceFile &SF) const;
307310
};
308311

309312
/// A class which manages the state and execution of the compiler.
@@ -322,7 +325,6 @@ class CompilerInstance {
322325
std::unique_ptr<SILModule> TheSILModule;
323326

324327
DependencyTracker *DepTracker = nullptr;
325-
ReferencedNameTracker *NameTracker = nullptr;
326328

327329
ModuleDecl *MainModule = nullptr;
328330
SerializedModuleLoader *SML = nullptr;
@@ -393,14 +395,6 @@ class CompilerInstance {
393395
return DepTracker;
394396
}
395397

396-
void setReferencedNameTracker(ReferencedNameTracker *tracker) {
397-
assert(PrimarySourceFiles.empty() && "must be called before performSema()");
398-
NameTracker = tracker;
399-
}
400-
ReferencedNameTracker *getReferencedNameTracker() {
401-
return NameTracker;
402-
}
403-
404398
/// Set the SIL module for this compilation instance.
405399
///
406400
/// The CompilerInstance takes ownership of the given SILModule object.
@@ -583,9 +577,13 @@ class CompilerInstance {
583577
void finishTypeChecking(OptionSet<TypeCheckingFlags> TypeCheckOptions);
584578

585579
public:
586-
PrimarySpecificPaths getPrimarySpecificPathsForWholeModuleOptimizationMode();
587-
PrimarySpecificPaths getPrimarySpecificPathsForPrimary(StringRef filename);
588-
PrimarySpecificPaths getPrimarySpecificPathsForAtMostOnePrimary();
580+
PrimarySpecificPaths
581+
getPrimarySpecificPathsForWholeModuleOptimizationMode() const;
582+
PrimarySpecificPaths
583+
getPrimarySpecificPathsForPrimary(StringRef filename) const;
584+
PrimarySpecificPaths getPrimarySpecificPathsForAtMostOnePrimary() const;
585+
PrimarySpecificPaths
586+
getPrimarySpecificPathsForSourceFile(const SourceFile &SF) const;
589587
};
590588

591589
} // namespace swift

include/swift/Frontend/FrontendInputsAndOutputs.h

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -224,13 +224,11 @@ class FrontendInputsAndOutputs {
224224
/// Assumes there is not more than one primary input file, if any.
225225
/// Otherwise, you would need to call getPrimarySpecificPathsForPrimary
226226
/// to tell it which primary input you wanted the outputs for.
227-
///
228-
/// Must not be constructed on-the-fly because some parts of the compiler
229-
/// receive StringRefs to its components, so it must live as long as the
230-
/// compiler.
231-
PrimarySpecificPaths &getPrimarySpecificPathsForAtMostOnePrimary();
232227

233-
PrimarySpecificPaths &getPrimarySpecificPathsForPrimary(StringRef filename);
228+
PrimarySpecificPaths getPrimarySpecificPathsForAtMostOnePrimary() const;
229+
230+
PrimarySpecificPaths
231+
getPrimarySpecificPathsForPrimary(StringRef filename) const;
234232

235233
bool hasDependenciesPath() const;
236234
bool hasReferenceDependenciesPath() const;

include/swift/Frontend/FrontendOptions.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -275,8 +275,8 @@ class FrontendOptions {
275275
InputsAndOutputs.hasSingleInput();
276276
}
277277

278-
PrimarySpecificPaths getPrimarySpecificPathsForAtMostOnePrimary();
279-
PrimarySpecificPaths getPrimarySpecificPathsForPrimary(StringRef);
278+
PrimarySpecificPaths getPrimarySpecificPathsForAtMostOnePrimary() const;
279+
PrimarySpecificPaths getPrimarySpecificPathsForPrimary(StringRef) const;
280280

281281
private:
282282
static bool canActionEmitDependencies(ActionType);

lib/AST/Module.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1472,6 +1472,11 @@ void SourceFile::setTypeRefinementContext(TypeRefinementContext *Root) {
14721472
TRC = Root;
14731473
}
14741474

1475+
void SourceFile::createReferencedNameTracker() {
1476+
assert(!ReferencedNames && "This file already has a name tracker.");
1477+
ReferencedNames.emplace(ReferencedNameTracker());
1478+
}
1479+
14751480
//===----------------------------------------------------------------------===//
14761481
// Miscellaneous
14771482
//===----------------------------------------------------------------------===//

lib/Frontend/Frontend.cpp

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -54,15 +54,20 @@ std::string CompilerInvocation::getPCHHash() const {
5454
}
5555

5656
PrimarySpecificPaths
57-
CompilerInvocation::getPrimarySpecificPathsForAtMostOnePrimary() {
57+
CompilerInvocation::getPrimarySpecificPathsForAtMostOnePrimary() const {
5858
return getFrontendOptions().getPrimarySpecificPathsForAtMostOnePrimary();
5959
}
6060

61-
PrimarySpecificPaths
62-
CompilerInvocation::getPrimarySpecificPathsForPrimary(StringRef filename) {
61+
PrimarySpecificPaths CompilerInvocation::getPrimarySpecificPathsForPrimary(
62+
StringRef filename) const {
6363
return getFrontendOptions().getPrimarySpecificPathsForPrimary(filename);
6464
}
6565

66+
PrimarySpecificPaths CompilerInvocation::getPrimarySpecificPathsForSourceFile(
67+
const SourceFile &SF) const {
68+
return getPrimarySpecificPathsForPrimary(SF.getFilename());
69+
}
70+
6671
void CompilerInstance::createSILModule() {
6772
assert(MainModule && "main module not created yet");
6873
// Assume WMO if a -primary-file option was not provided.
@@ -78,7 +83,7 @@ void CompilerInstance::recordPrimaryInputBuffer(unsigned BufID) {
7883
void CompilerInstance::recordPrimarySourceFile(SourceFile *SF) {
7984
assert(MainModule && "main module not created yet");
8085
PrimarySourceFiles.push_back(SF);
81-
SF->setReferencedNameTracker(NameTracker);
86+
SF->createReferencedNameTracker();
8287
if (SF->getBufferID().hasValue())
8388
recordPrimaryInputBuffer(SF->getBufferID().getValue());
8489
}
@@ -837,14 +842,19 @@ void CompilerInstance::freeASTContext() {
837842
void CompilerInstance::freeSILModule() { TheSILModule.reset(); }
838843

839844
PrimarySpecificPaths
840-
CompilerInstance::getPrimarySpecificPathsForWholeModuleOptimizationMode() {
845+
CompilerInstance::getPrimarySpecificPathsForWholeModuleOptimizationMode()
846+
const {
841847
return getPrimarySpecificPathsForAtMostOnePrimary();
842848
}
843849
PrimarySpecificPaths
844-
CompilerInstance::getPrimarySpecificPathsForAtMostOnePrimary() {
850+
CompilerInstance::getPrimarySpecificPathsForAtMostOnePrimary() const {
845851
return Invocation.getPrimarySpecificPathsForAtMostOnePrimary();
846852
}
847853
PrimarySpecificPaths
848-
CompilerInstance::getPrimarySpecificPathsForPrimary(StringRef filename) {
854+
CompilerInstance::getPrimarySpecificPathsForPrimary(StringRef filename) const {
849855
return Invocation.getPrimarySpecificPathsForPrimary(filename);
850856
}
857+
PrimarySpecificPaths CompilerInstance::getPrimarySpecificPathsForSourceFile(
858+
const SourceFile &SF) const {
859+
return Invocation.getPrimarySpecificPathsForSourceFile(SF);
860+
}

lib/Frontend/FrontendInputsAndOutputs.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -397,13 +397,13 @@ bool FrontendInputsAndOutputs::hasDependencyTrackerPath() const {
397397
hasLoadedModuleTracePath();
398398
}
399399

400-
PrimarySpecificPaths &
401-
FrontendInputsAndOutputs::getPrimarySpecificPathsForAtMostOnePrimary() {
400+
PrimarySpecificPaths
401+
FrontendInputsAndOutputs::getPrimarySpecificPathsForAtMostOnePrimary() const {
402402
return PrimarySpecificPathsForAtMostOnePrimary;
403403
}
404404

405-
PrimarySpecificPaths &
405+
PrimarySpecificPaths
406406
FrontendInputsAndOutputs::getPrimarySpecificPathsForPrimary(
407-
StringRef filename) {
407+
StringRef filename) const {
408408
return getPrimarySpecificPathsForAtMostOnePrimary(); // just a stub for now
409409
}

lib/Frontend/FrontendOptions.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -388,11 +388,11 @@ bool FrontendOptions::doesActionProduceTextualOutput(ActionType action) {
388388
}
389389

390390
PrimarySpecificPaths
391-
FrontendOptions::getPrimarySpecificPathsForAtMostOnePrimary() {
391+
FrontendOptions::getPrimarySpecificPathsForAtMostOnePrimary() const {
392392
return InputsAndOutputs.getPrimarySpecificPathsForAtMostOnePrimary();
393393
}
394394

395395
PrimarySpecificPaths
396-
FrontendOptions::getPrimarySpecificPathsForPrimary(StringRef filename) {
396+
FrontendOptions::getPrimarySpecificPathsForPrimary(StringRef filename) const {
397397
return InputsAndOutputs.getPrimarySpecificPathsForPrimary(filename);
398398
}

lib/FrontendTool/FrontendTool.cpp

Lines changed: 31 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -298,7 +298,7 @@ static bool writeSIL(SILModule &SM, ModuleDecl *M, bool EmitVerboseSIL,
298298
return false;
299299
}
300300

301-
static bool writeSIL(SILModule &SM, const PrimarySpecificPaths PSPs,
301+
static bool writeSIL(SILModule &SM, const PrimarySpecificPaths &PSPs,
302302
CompilerInstance &Instance,
303303
CompilerInvocation &Invocation) {
304304
const FrontendOptions &opts = Invocation.getFrontendOptions();
@@ -482,10 +482,12 @@ static void countStatsPostSema(UnifiedStatsReporter &Stats,
482482
C.NumDependencies = D->getDependencies().size();
483483
}
484484

485-
if (auto *R = Instance.getReferencedNameTracker()) {
486-
C.NumReferencedTopLevelNames = R->getTopLevelNames().size();
487-
C.NumReferencedDynamicNames = R->getDynamicLookupNames().size();
488-
C.NumReferencedMemberNames = R->getUsedMembers().size();
485+
for (auto SF : Instance.getPrimarySourceFiles()) {
486+
if (auto *R = SF->getReferencedNameTracker()) {
487+
C.NumReferencedTopLevelNames += R->getTopLevelNames().size();
488+
C.NumReferencedDynamicNames += R->getDynamicLookupNames().size();
489+
C.NumReferencedMemberNames += R->getUsedMembers().size();
490+
}
489491
}
490492

491493
if (!Instance.getPrimarySourceFiles().empty()) {
@@ -734,8 +736,8 @@ static Optional<bool> dumpASTIfNeeded(CompilerInvocation &Invocation,
734736
return Context.hadError();
735737
}
736738

737-
static void emitReferenceDependenciesIfNeeded(CompilerInvocation &Invocation,
738-
CompilerInstance &Instance) {
739+
static void emitReferenceDependenciesForAllPrimaryInputsIfNeeded(
740+
CompilerInvocation &Invocation, CompilerInstance &Instance) {
739741
if (Invocation.getFrontendOptions()
740742
.InputsAndOutputs.supplementaryOutputs()
741743
.ReferenceDependenciesFilePath.empty())
@@ -746,9 +748,12 @@ static void emitReferenceDependenciesIfNeeded(CompilerInvocation &Invocation,
746748
return;
747749
}
748750
for (auto *SF : Instance.getPrimarySourceFiles()) {
749-
emitReferenceDependencies(Instance.getASTContext().Diags, SF,
750-
*Instance.getDependencyTracker(),
751-
Invocation.getFrontendOptions());
751+
std::string referenceDependenciesFilePath =
752+
Invocation.getPrimarySpecificPathsForSourceFile(*SF)
753+
.SupplementaryOutputs.ReferenceDependenciesFilePath;
754+
emitReferenceDependenciesIfNeeded(Instance.getASTContext().Diags, SF,
755+
*Instance.getDependencyTracker(),
756+
referenceDependenciesFilePath);
752757
}
753758
}
754759

@@ -804,7 +809,7 @@ generateSILModules(CompilerInvocation &Invocation, CompilerInstance &Instance) {
804809
auto SM = performSILGeneration(*PrimaryFile, SILOpts, None);
805810
std::deque<PostSILGenInputs> PSGIs;
806811
const PrimarySpecificPaths PSPs =
807-
Instance.getPrimarySpecificPathsForPrimary(PrimaryFile->getFilename());
812+
Instance.getPrimarySpecificPathsForSourceFile(*PrimaryFile);
808813
PSGIs.push_back(PostSILGenInputs{std::move(SM), true, PrimaryFile, PSPs});
809814
return PSGIs;
810815
}
@@ -826,16 +831,12 @@ generateSILModules(CompilerInvocation &Invocation, CompilerInstance &Instance) {
826831
return PSGIs;
827832
}
828833

829-
static bool performCompileStepsPostSILGen(CompilerInstance &Instance,
830-
CompilerInvocation &Invocation,
831-
std::unique_ptr<SILModule> SM,
832-
bool astGuaranteedToCorrespondToSIL,
833-
ModuleOrSourceFile MSF,
834-
PrimarySpecificPaths PSPs,
835-
bool moduleIsPublic,
836-
int &ReturnValue,
837-
FrontendObserver *observer,
838-
UnifiedStatsReporter *Stats);
834+
static bool performCompileStepsPostSILGen(
835+
CompilerInstance &Instance, CompilerInvocation &Invocation,
836+
std::unique_ptr<SILModule> SM, bool astGuaranteedToCorrespondToSIL,
837+
ModuleOrSourceFile MSF, const PrimarySpecificPaths &PSPs,
838+
bool moduleIsPublic, int &ReturnValue, FrontendObserver *observer,
839+
UnifiedStatsReporter *Stats);
839840

840841
/// Performs the compile requested by the user.
841842
/// \param Instance Will be reset after performIRGeneration when the verifier
@@ -860,13 +861,8 @@ static bool performCompile(CompilerInstance &Instance,
860861
if (Action == FrontendOptions::ActionType::EmitPCH)
861862
return precompileBridgingHeader(Invocation, Instance);
862863

863-
if (Invocation.getInputKind() == InputFileKind::IFK_LLVM_IR)
864-
return compileLLVMIR(Invocation, Instance, Stats);
865-
866-
ReferencedNameTracker nameTracker;
867-
if (!opts.InputsAndOutputs.supplementaryOutputs()
868-
.ReferenceDependenciesFilePath.empty())
869-
Instance.setReferencedNameTracker(&nameTracker);
864+
if (Invocation.getInputKind() == InputFileKind::IFK_LLVM_IR)
865+
return compileLLVMIR(Invocation, Instance, Stats);
870866

871867
if (FrontendOptions::shouldActionOnlyParse(Action))
872868
Instance.performParseOnly();
@@ -914,7 +910,7 @@ static bool performCompile(CompilerInstance &Instance,
914910
(void)emitMakeDependencies(Context.Diags, *Instance.getDependencyTracker(),
915911
opts);
916912

917-
emitReferenceDependenciesIfNeeded(Invocation, Instance);
913+
emitReferenceDependenciesForAllPrimaryInputsIfNeeded(Invocation, Instance);
918914

919915
(void)emitLoadedModuleTraceIfNeeded(Context, Instance.getDependencyTracker(),
920916
opts);
@@ -1177,16 +1173,12 @@ static bool generateCode(CompilerInvocation &Invocation,
11771173
EffectiveLanguageVersion, OutputFilename, Stats);
11781174
}
11791175

1180-
static bool performCompileStepsPostSILGen(CompilerInstance &Instance,
1181-
CompilerInvocation &Invocation,
1182-
std::unique_ptr<SILModule> SM,
1183-
bool astGuaranteedToCorrespondToSIL,
1184-
ModuleOrSourceFile MSF,
1185-
const PrimarySpecificPaths PSPs,
1186-
bool moduleIsPublic,
1187-
int &ReturnValue,
1188-
FrontendObserver *observer,
1189-
UnifiedStatsReporter *Stats) {
1176+
static bool performCompileStepsPostSILGen(
1177+
CompilerInstance &Instance, CompilerInvocation &Invocation,
1178+
std::unique_ptr<SILModule> SM, bool astGuaranteedToCorrespondToSIL,
1179+
ModuleOrSourceFile MSF, const PrimarySpecificPaths &PSPs,
1180+
bool moduleIsPublic, int &ReturnValue, FrontendObserver *observer,
1181+
UnifiedStatsReporter *Stats) {
11901182

11911183
FrontendOptions opts = Invocation.getFrontendOptions();
11921184
FrontendOptions::ActionType Action = opts.RequestedAction;

0 commit comments

Comments
 (0)