From a6bf84e20b39f74c70a5eca73f81564d1cc51fdf Mon Sep 17 00:00:00 2001 From: David Ungar Date: Sat, 16 Nov 2019 23:17:37 -0800 Subject: [PATCH] refactoring WIP unfmt --- .../swift/Driver/DriverIncrementalRanges.h | 6 +- lib/Driver/Compilation.cpp | 168 +++++++++--------- lib/Driver/DriverIncrementalRanges.cpp | 29 --- 3 files changed, 84 insertions(+), 119 deletions(-) diff --git a/include/swift/Driver/DriverIncrementalRanges.h b/include/swift/Driver/DriverIncrementalRanges.h index 8ded72d934f..d7779345a90 100644 --- a/include/swift/Driver/DriverIncrementalRanges.h +++ b/include/swift/Driver/DriverIncrementalRanges.h @@ -103,15 +103,13 @@ public: neededCompileJobsForRangeBasedIncrementalCompilation( const llvm::StringMap &allInfos, std::vector, - function_ref schedule, - function_ref defer, function_ref noteBuilding); -private: +public: static bool shouldScheduleCompileJob( const llvm::StringMap &allInfos, const driver::Job *, function_ref); - +private: static Optional isFileNewerThan(StringRef lhs, StringRef rhs, DiagnosticEngine&); diff --git a/lib/Driver/Compilation.cpp b/lib/Driver/Compilation.cpp index d16e896faae..c5738991f1d 100644 --- a/lib/Driver/Compilation.cpp +++ b/lib/Driver/Compilation.cpp @@ -874,98 +874,87 @@ namespace driver { template void scheduleFirstRoundJobsForIncrementalCompilation( DependencyGraphT &DepGraph) { - const auto compileJobsToSchedule = - computeFirstRoundJobsForIncrementalCompilation(DepGraph); + llvm::SmallPtrSet compileJobsToSchedule; + for (const Job *Cmd: computeFirstRoundCompileJobsForIncrementalCompilation(DepGraph)) + compileJobsToSchedule.insert(Cmd); for (const Job *Cmd : Comp.getJobs()) { - if (Cmd->getFirstSwiftPrimaryInput().empty() || - compileJobsToSchedule.count(Cmd)) + if (Cmd->getFirstSwiftPrimaryInput().empty() || compileJobsToSchedule.count(Cmd)) scheduleCommandIfNecessaryAndPossible(Cmd); - else { - assert(ScheduledCommands.count(Cmd) == 0 && - "Cannot defer already-scheduled command"); + else DeferredCommands.insert(Cmd); - } } } - /// Figure out the best strategy and return those jobs. + /// Figure out the best strategy and return those jobs. May return duplicates. template - llvm::SmallPtrSet - computeFirstRoundJobsForIncrementalCompilation(DependencyGraphT &DepGraph) { + llvm::SmallVector + computeFirstRoundCompileJobsForIncrementalCompilation(DependencyGraphT &DepGraph) { auto compileJobsToScheduleViaDependencies = computeDependenciesAndGetNeededCompileJobs(DepGraph); - if (!Comp.getEnableSourceRangeDependencies()) { - if (Comp.CompareIncrementalSchemes) { -#error factor - auto jobs = computeRangesAndGetNeededCompileJobs(DepGraph); - auto &compileJobsToScheduleViaSourceRanges = jobs.first; - auto &jobsLackingSupplementaryOutputs = jobs.second; + const bool mustConsultRanges = Comp.getEnableSourceRangeDependencies() || Comp.CompareIncrementalSchemes; - const StringRef whyFallingBack = - reasonToFallBack(compileJobsToScheduleViaDependencies.size(), - compileJobsToScheduleViaSourceRanges.size(), - jobsLackingSupplementaryOutputs.size()); - - Comp.updateJobsForComparison(compileJobsToScheduleViaDependencies, - compileJobsToScheduleViaSourceRanges); - if (!whyFallingBack.empty()) - Comp.setFallingBackForComparison(); - } - return compileJobsToScheduleViaDependencies; - } + if (!mustConsultRanges) + return compileJobsToScheduleViaDependencies; auto jobs = computeRangesAndGetNeededCompileJobs(DepGraph); - auto &compileJobsToScheduleViaSourceRanges = jobs.first; - auto &jobsLackingSupplementaryOutputs = jobs.second; + llvm::SmallVector &compileJobsToScheduleViaSourceRanges = jobs.first; + llvm::SmallVector &jobsLackingSourceRangeSupplementaryOutputs = jobs.second; - const StringRef whyFallingBack = - reasonToFallBack(compileJobsToScheduleViaDependencies.size(), - compileJobsToScheduleViaSourceRanges.size(), - jobsLackingSupplementaryOutputs.size()); + const bool shouldFallBack = decideAndExplainWhetherToFallBackToDependencies(compileJobsToScheduleViaSourceRanges, jobsLackingSourceRangeSupplementaryOutputs); - if (whyFallingBack.empty()) { - Comp.setUseSourceRangeDependencies(true); - if (Comp.getShowIncrementalBuildDecisions()) - llvm::outs() << "Using ranges\n"; - Comp.updateJobsForComparison(compileJobsToScheduleViaDependencies, - compileJobsToScheduleViaSourceRanges); - return compileJobsToScheduleViaSourceRanges; + Comp.updateJobsForComparison(compileJobsToScheduleViaDependencies, + compileJobsToScheduleViaSourceRanges); + if (shouldFallBack) + Comp.setFallingBackForComparison(); + + if (!Comp.getEnableSourceRangeDependencies()) + return compileJobsToScheduleViaDependencies; + + Comp.setUseSourceRangeDependencies(!shouldFallBack); + + if (shouldFallBack) { + // Even if dependencies would not schedule these, we want them to run + // to create the supplementary outputs for next time. + for (const Job *Cmd : jobsLackingSourceRangeSupplementaryOutputs) + compileJobsToScheduleViaDependencies.push_back(Cmd); } - Comp.setFallingBackForComparison(); - Comp.setUseSourceRangeDependencies(false); - // Even if dependencies would not schedule these, we want them to run - // to create the supplementary outputs for next time. - for (const Job *Cmd : jobsLackingSupplementaryOutputs) - compileJobsToScheduleViaDependencies.insert(Cmd); - if (Comp.getShowIncrementalBuildDecisions()) - llvm::outs() << "Using dependenciess: " << whyFallingBack << "\n"; - return compileJobsToScheduleViaDependencies; + return shouldFallBack ? compileJobsToScheduleViaDependencies : compileJobsToScheduleViaSourceRanges; } - StringRef reasonToFallBack(const size_t depCount, const size_t rangeCount, - const size_t lackingSupplementaryOutputsCount) { - if (lackingSupplementaryOutputsCount) - return "Some input lacks supplementary output needed for the source " - "range strategy.\n Maybe dependencies can do better than " - "recompiling every file."; - + bool decideAndExplainWhetherToFallBackToDependencies(llvm::SmallVector &compileJobsToScheduleViaSourceRanges, + const llvm::SmallVector &jobsLackingSourceRangeSupplementaryOutputs) { + if (!jobsLackingSourceRangeSupplementaryOutputs.empty()) { + if (Comp.getShowIncrementalBuildDecisions()) { + llvm::outs() << "Using dependencies: Some input lacks supplementary output needed for the source " + "range strategy.\n Maybe dependencies can do better than " + "recompiling every file.\n"; + } + return true; + } // Unless the source-range scheme would compile every file, // it's likely a better bet. - if (rangeCount < Comp.countSwiftInputs()) - return StringRef(); - - (void)depCount; // Will likely compile more than this anyway - return "Range strategy would compile every input; dependencies cannot be " - "any worse."; + llvm::SmallPtrSet uniqueJobs; + for (const auto* J: compileJobsToScheduleViaSourceRanges) + uniqueJobs.insert(J); + if (uniqueJobs.size() < Comp.countSwiftInputs()) { + if (Comp.getShowIncrementalBuildDecisions()) + llvm::outs() << "Using ranges\n"; + return false; + } + if (Comp.getShowIncrementalBuildDecisions()) + llvm::outs() << "Using dependencies: Range strategy would compile every input; dependencies cannot be " + "any worse."; + return true; } + /// Return both the jobs to compile if using ranges, and also any jobs that /// must be compiled to use ranges in the future (because they were lacking - /// supplementary output files). + /// supplementary output files). May include duplicates. template - std::pair, + std::pair, llvm::SmallVector> computeRangesAndGetNeededCompileJobs(DependencyGraphT &DepGraph) { using namespace incremental_ranges; @@ -975,8 +964,7 @@ namespace driver { const bool dumpCompiledSourceDiffs = Comp.getArgs().hasArg(options::OPT_driver_dump_compiled_source_diffs); - const auto jobs = Comp.getJobsSimply(); - const auto allSourceRangeInfo = + const auto allSourceRangeInfo = incremental_ranges::SourceRangeBasedInfo::loadAllInfo(Comp); incremental_ranges::SourceRangeBasedInfo::dumpAllInfo( @@ -986,29 +974,37 @@ namespace driver { // But, since we register errors by recording massive changes to // primaries, could just keep on. // load dependencies for external dependencies and interfacehashes - auto jobsToCompileAndJobsNeededForSupplementaryOutputs = - SourceRangeBasedInfo:: - neededCompileJobsForRangeBasedIncrementalCompilation( - allSourceRangeInfo, Comp.getJobsSimply(), - [&](const Job *Cmd) { - scheduleCommandIfNecessaryAndPossible(Cmd); - }, - [&](const Job *Cmd) { DeferredCommands.insert(Cmd); }, - [&](const Job *Cmd, Twine why) { - noteBuilding(Cmd, true, why.str()); - }); + + llvm::SmallVector neededJobs; + for (const Job *Cmd: Comp.getJobs()) { + if ( SourceRangeBasedInfo::shouldScheduleCompileJob( + allSourceRangeInfo, Cmd, [&](Twine why) { noteBuilding(Cmd, true, why.str()); })) + neededJobs.push_back(Cmd); + } + + llvm::SmallVector jobsLackingSupplementaryOutputs; + for (const Job* Cmd: Comp.getJobs()) { + if (allSourceRangeInfo.count(Cmd->getFirstSwiftPrimaryInput()) == 0) { + noteBuilding( + Cmd, + true, + "to create source-range and compiled-source files for the next time when falling back from source-ranges"); + jobsLackingSupplementaryOutputs.push_back(Cmd); + } + } for (const Job *Cmd : externallyDependentJobsForRangeBasedIncrementalCompilation(DepGraph)) - jobsToCompileAndJobsNeededForSupplementaryOutputs.first.insert(Cmd); + neededJobs.push_back(Cmd); - return jobsToCompileAndJobsNeededForSupplementaryOutputs; + return {neededJobs, jobsLackingSupplementaryOutputs}; } + /// Return jobs to run if using dependencies, may include duplicates. template - llvm::SmallPtrSet + llvm::SmallVector computeDependenciesAndGetNeededCompileJobs(DependencyGraphT &DepGraph) { - llvm::SmallPtrSet jobsToSchedule; + llvm::SmallVector jobsToSchedule; for (const Job *Cmd : Comp.getJobs()) { if (Cmd->getFirstSwiftPrimaryInput().empty()) continue; // not Compile @@ -1019,19 +1015,19 @@ namespace driver { // Dependency load error, just run them all for (const Job *Cmd : Comp.getJobs()) { if (!Cmd->getFirstSwiftPrimaryInput().empty()) - jobsToSchedule.insert(Cmd); + jobsToSchedule.push_back(Cmd); } return jobsToSchedule; } if (shouldSched.getValue()) - jobsToSchedule.insert(Cmd); + jobsToSchedule.push_back(Cmd); } { const auto additionalJobs = additionalJobsToScheduleForDependencyBasedIncrementalCompilation( DepGraph); for (const auto *Cmd : additionalJobs) - jobsToSchedule.insert(Cmd); + jobsToSchedule.push_back(Cmd); } return jobsToSchedule; } diff --git a/lib/Driver/DriverIncrementalRanges.cpp b/lib/Driver/DriverIncrementalRanges.cpp index 13daed09b7c..c151067c60b 100644 --- a/lib/Driver/DriverIncrementalRanges.cpp +++ b/lib/Driver/DriverIncrementalRanges.cpp @@ -219,35 +219,6 @@ Ranges SourceRangeBasedInfo::computeNonlocalChangedRanges( // MARK: scheduling //============================================================================== -std::pair, - llvm::SmallVector> -SourceRangeBasedInfo::neededCompileJobsForRangeBasedIncrementalCompilation( - const llvm::StringMap &allInfos, - std::vector jobs, function_ref schedule, - function_ref defer, - function_ref noteBuilding) { - - llvm::SmallPtrSet neededJobs; - llvm::SmallVector jobsLackingSupplementaryOutputs; - for (const Job *Cmd : jobs) { - const auto primary = Cmd->getFirstSwiftPrimaryInput(); - if (primary.empty()) { - schedule(Cmd); // not compile - continue; - } - const bool shouldSchedule = shouldScheduleCompileJob( - allInfos, Cmd, [&](Twine why) { noteBuilding(Cmd, why.str()); }); - if (shouldSchedule) - neededJobs.insert(Cmd); - if (allInfos.count(primary) == 0) { - jobsLackingSupplementaryOutputs.push_back(Cmd); - noteBuilding( - Cmd, - "to create source-range and compiled-source files for the next time"); - } - } - return {neededJobs, jobsLackingSupplementaryOutputs}; -} bool SourceRangeBasedInfo::shouldScheduleCompileJob( const llvm::StringMap &allInfos, const Job *Cmd,