Skip to content

Commit d90ff24

Browse files
[BrdigingHeader] Auto bridging header chaining
Add ability to automatically chaining the bridging headers discovered from all dependencies module when doing swift caching build. This will eliminate all implicit bridging header imports from the build and make the bridging header importing behavior much more reliable, while keep the compatibility at maximum. For example, if the current module A depends on module B and C, and both B and C are binary modules that uses bridging header, when building module A, dependency scanner will construct a new header that chains three bridging headers together with the option to build a PCH from it. This will make all importing errors more obvious while improving the performance.
1 parent 3d2da98 commit d90ff24

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

43 files changed

+1002
-260
lines changed

include/swift-c/DependencyScan/DependencyScan.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
/// SWIFTSCAN_VERSION_MINOR should increase when there are API additions.
2626
/// SWIFTSCAN_VERSION_MAJOR is intended for "major" source/ABI breaking changes.
2727
#define SWIFTSCAN_VERSION_MAJOR 1
28-
#define SWIFTSCAN_VERSION_MINOR 0
28+
#define SWIFTSCAN_VERSION_MINOR 1
2929

3030
SWIFTSCAN_BEGIN_DECLS
3131

@@ -221,6 +221,10 @@ SWIFTSCAN_PUBLIC swiftscan_string_ref_t
221221
swiftscan_swift_textual_detail_get_user_module_version(
222222
swiftscan_module_details_t details);
223223

224+
SWIFTSCAN_PUBLIC swiftscan_string_ref_t
225+
swiftscan_swift_textual_detail_get_chained_bridging_header_content(
226+
swiftscan_module_details_t details);
227+
224228
//=== Swift Binary Module Details query APIs ------------------------------===//
225229

226230
SWIFTSCAN_PUBLIC swiftscan_string_ref_t

include/swift/AST/ModuleDependencies.h

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,13 @@
3030
#include "llvm/ADT/DenseSet.h"
3131
#include "llvm/ADT/IntrusiveRefCntPtr.h"
3232
#include "llvm/ADT/StringSet.h"
33+
#include "llvm/CAS/ActionCache.h"
3334
#include "llvm/CAS/CASProvidingFileSystem.h"
3435
#include "llvm/CAS/CASReference.h"
3536
#include "llvm/CAS/CachingOnDiskFileSystem.h"
3637
#include "llvm/CAS/ObjectStore.h"
3738
#include "llvm/Support/Error.h"
39+
#include "llvm/Support/MemoryBuffer.h"
3840
#include "llvm/Support/Mutex.h"
3941
#include "llvm/Support/PrefixMapper.h"
4042
#include "llvm/Support/VirtualFileSystem.h"
@@ -344,6 +346,9 @@ class SwiftSourceModuleDependenciesStorage
344346
/// The Swift frontend invocation arguments to build bridging header.
345347
std::vector<std::string> bridgingHeaderBuildCommandLine;
346348

349+
/// The chained bridging header source buffer if used.
350+
std::string chainedBridgingHeader;
351+
347352
SwiftSourceModuleDependenciesStorage(
348353
StringRef RootID, ArrayRef<StringRef> buildCommandLine,
349354
ArrayRef<ScannerImportStatementInfo> moduleImports,
@@ -361,6 +366,7 @@ class SwiftSourceModuleDependenciesStorage
361366
return new SwiftSourceModuleDependenciesStorage(*this);
362367
}
363368

369+
364370
static bool classof(const ModuleDependencyInfoStorageBase *base) {
365371
return base->dependencyKind == ModuleDependencyKind::SwiftSource;
366372
}
@@ -377,6 +383,10 @@ class SwiftSourceModuleDependenciesStorage
377383
void addTestableImport(ImportPath::Module module) {
378384
testableImports.insert(module.front().Item.str());
379385
}
386+
387+
void setChainedBridgingHeaderBuffer(StringRef buffer) {
388+
chainedBridgingHeader = buffer.str();
389+
}
380390
};
381391

382392
/// Describes the dependencies of a pre-built Swift module (with no
@@ -786,11 +796,11 @@ class ModuleDependencyInfo {
786796
setLinkLibraries(const ArrayRef<LinkLibrary> linkLibraries) {
787797
storage->linkLibraries.assign(linkLibraries.begin(), linkLibraries.end());
788798
}
789-
799+
790800
const ArrayRef<std::string> getAuxiliaryFiles() const {
791801
return storage->auxiliaryFiles;
792802
}
793-
803+
794804
void
795805
setAuxiliaryFiles(const ArrayRef<std::string> auxiliaryFiles) {
796806
storage->auxiliaryFiles.assign(auxiliaryFiles.begin(), auxiliaryFiles.end());
@@ -978,11 +988,14 @@ class ModuleDependencyInfo {
978988
void addSourceFile(StringRef sourceFile);
979989

980990
/// Add source files that the header input depends on.
981-
void addHeaderSourceFile(StringRef bridgingSourceFile);
991+
void setHeaderSourceFiles(const std::vector<std::string> &sourceFiles);
982992

983993
/// Add bridging header include tree.
984994
void addBridgingHeaderIncludeTree(StringRef ID);
985995

996+
/// Set the chained bridging header buffer.
997+
void setChainedBridgingHeaderBuffer(StringRef buffer);
998+
986999
/// Collect a map from a secondary module name to a list of cross-import
9871000
/// overlays, when this current module serves as the primary module.
9881001
llvm::StringMap<llvm::SmallSetVector<Identifier, 4>>
@@ -1042,8 +1055,9 @@ class SwiftDependencyScanningService {
10421055
/// If use clang include tree.
10431056
bool UseClangIncludeTree = false;
10441057

1045-
/// CAS ObjectStore Instance.
1058+
/// CAS Instance.
10461059
std::shared_ptr<llvm::cas::ObjectStore> CAS;
1060+
std::shared_ptr<llvm::cas::ActionCache> ActionCache;
10471061

10481062
/// File prefix mapper.
10491063
std::unique_ptr<llvm::PrefixMapper> Mapper;
@@ -1196,11 +1210,11 @@ class ModuleDependenciesCache {
11961210
/// Query all dependencies
11971211
ModuleDependencyIDSetVector
11981212
getAllDependencies(const ModuleDependencyID &moduleID) const;
1199-
1213+
12001214
/// Query all Clang module dependencies.
12011215
ModuleDependencyIDSetVector
12021216
getClangDependencies(const ModuleDependencyID &moduleID) const;
1203-
1217+
12041218
/// Query all directly-imported Swift dependencies
12051219
llvm::ArrayRef<ModuleDependencyID>
12061220
getImportedSwiftDependencies(const ModuleDependencyID &moduleID) const;

include/swift/AST/SearchPathOptions.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -580,6 +580,9 @@ class SearchPathOptions {
580580
/// "in-package", must not require package-only module dependencies.
581581
bool ResolveInPackageModuleDependencies = false;
582582

583+
/// Enable auto bridging header chaining.
584+
bool BridgingHeaderChaining = false;
585+
583586
/// Return all module search paths that (non-recursively) contain a file whose
584587
/// name is in \p Filenames.
585588
SmallVector<const ModuleSearchPath *, 4>

include/swift/Basic/LangOptions.h

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,7 @@ namespace swift {
201201
/// Maximum number of typo corrections we are allowed to perform.
202202
/// This is disabled by default until we can get typo-correction working within acceptable performance bounds.
203203
unsigned TypoCorrectionLimit = 0;
204-
204+
205205
/// Should access control be respected?
206206
bool EnableAccessControl = true;
207207

@@ -260,7 +260,7 @@ namespace swift {
260260

261261
/// Emit a remark when indexing a system module.
262262
bool EnableIndexingSystemModuleRemarks = false;
263-
263+
264264
/// Emit a remark on early exit in explicit interface build
265265
bool EnableSkipExplicitInterfaceModuleBuildRemarks = false;
266266

@@ -691,7 +691,7 @@ namespace swift {
691691
void clearAllPlatformConditionValues() {
692692
PlatformConditionValues.clear();
693693
}
694-
694+
695695
/// Returns the value for the given platform condition or an empty string.
696696
StringRef getPlatformConditionValue(PlatformConditionKind Kind) const;
697697

@@ -871,7 +871,7 @@ namespace swift {
871871
/// 4.2 GHz Intel Core i7.
872872
/// (It's arbitrary, but will keep the compiler from taking too much time.)
873873
unsigned SwitchCheckingInvocationThreshold = 200000;
874-
874+
875875
/// If true, the time it takes to type-check each function will be dumped
876876
/// to llvm::errs().
877877
bool DebugTimeFunctionBodies = false;
@@ -915,7 +915,7 @@ namespace swift {
915915

916916
/// Enable experimental operator designated types feature.
917917
bool EnableOperatorDesignatedTypes = false;
918-
918+
919919
/// Disable constraint system performance hacks.
920920
bool DisableConstraintSolverPerformanceHacks = false;
921921

@@ -963,6 +963,9 @@ namespace swift {
963963
/// The bridging header or PCH that will be imported.
964964
std::string BridgingHeader;
965965

966+
/// The bridging header PCH file.
967+
std::string BridgingHeaderPCH;
968+
966969
/// When automatically generating a precompiled header from the bridging
967970
/// header, place it in this directory.
968971
std::string PrecompiledHeaderOutputDir;
@@ -1084,6 +1087,9 @@ namespace swift {
10841087
/// compilation source targets.
10851088
std::vector<std::string>
10861089
getReducedExtraArgsForSwiftModuleDependency() const;
1090+
1091+
/// Get PCH input path. Return empty string if there is no PCH input.
1092+
std::string getPCHInputPath() const;
10871093
};
10881094

10891095
} // end namespace swift

include/swift/ClangImporter/ClangImporter.h

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -506,14 +506,17 @@ class ClangImporter final : public ClangModuleLoader {
506506
/// information will be augmented with information about the given
507507
/// textual header inputs.
508508
///
509+
/// \param headerPath the path to the header to be scanned.
510+
///
509511
/// \param clangScanningTool The clang dependency scanner.
510512
///
511513
/// \param cache The module dependencies cache to update, with information
512514
/// about new Clang modules discovered along the way.
513515
///
514516
/// \returns \c true if an error occurred, \c false otherwise
515517
bool getHeaderDependencies(
516-
ModuleDependencyID moduleID,
518+
ModuleDependencyID moduleID, std::optional<StringRef> headerPath,
519+
std::optional<llvm::MemoryBufferRef> sourceBuffer,
517520
clang::tooling::dependencies::DependencyScanningTool &clangScanningTool,
518521
ModuleDependenciesCache &cache,
519522
ModuleDependencyIDSetVector &headerClangModuleDependencies,
@@ -675,6 +678,11 @@ class ClangImporter final : public ClangModuleLoader {
675678
const clang::TypedefType *getTypeDefForCXXCFOptionsDefinition(
676679
const clang::Decl *candidateDecl) override;
677680

681+
/// Create cache key for embedded bridging header.
682+
static llvm::Expected<llvm::cas::ObjectRef>
683+
createEmbeddedBridgingHeaderCacheKey(
684+
llvm::cas::ObjectStore &CAS, llvm::cas::ObjectRef ChainedPCHIncludeTree);
685+
678686
SourceLoc importSourceLocation(clang::SourceLocation loc) override;
679687
};
680688

include/swift/DependencyScan/DependencyScanImpl.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,10 @@ typedef struct {
135135

136136
/// User module version
137137
swiftscan_string_ref_t user_module_version;
138+
139+
/// Chained bridging header content.
140+
swiftscan_string_ref_t chained_bridging_header_content;
141+
138142
} swiftscan_swift_textual_details_t;
139143

140144
/// Swift modules with only a binary module file.

include/swift/DependencyScan/ModuleDependencyScanner.h

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
#include "swift/AST/ModuleDependencies.h"
1717
#include "swift/Frontend/ModuleInterfaceLoader.h"
1818
#include "swift/Serialization/SerializedModuleLoader.h"
19+
#include "llvm/CAS/CASReference.h"
1920
#include "llvm/Support/ThreadPool.h"
2021

2122
namespace swift {
@@ -48,6 +49,11 @@ class ModuleDependencyScanningWorker {
4849
llvm::PrefixMapper *prefixMapper,
4950
bool isTestableImport = false);
5051

52+
/// Store cache entry for include tree.
53+
llvm::Error
54+
createCacheKeyForEmbeddedHeader(std::string embeddedHeaderIncludeTree,
55+
std::string chainedHeaderIncludeTree);
56+
5157
// Worker-specific instance of CompilerInvocation
5258
std::unique_ptr<CompilerInvocation> workerCompilerInvocation;
5359
// Worker-specific instance of ASTContext
@@ -59,6 +65,9 @@ class ModuleDependencyScanningWorker {
5965
// Swift and Clang module loaders acting as scanners.
6066
std::unique_ptr<ModuleInterfaceLoader> swiftScannerModuleLoader;
6167
std::unique_ptr<ClangImporter> clangScannerModuleLoader;
68+
// CAS instance.
69+
std::shared_ptr<llvm::cas::ObjectStore> CAS;
70+
std::shared_ptr<llvm::cas::ActionCache> ActionCache;
6271
// Restrict access to the parent scanner class.
6372
friend class ModuleDependencyScanner;
6473
};
@@ -116,10 +125,10 @@ class ModuleDependencyScanner {
116125
resolveAllClangModuleDependencies(ArrayRef<ModuleDependencyID> swiftModules,
117126
ModuleDependenciesCache &cache,
118127
ModuleDependencyIDSetVector &discoveredClangModules);
119-
void
120-
resolveHeaderDependencies(ArrayRef<ModuleDependencyID> swiftModules,
121-
ModuleDependenciesCache &cache,
122-
ModuleDependencyIDSetVector &discoveredHeaderDependencyClangModules);
128+
void resolveHeaderDependencies(
129+
const ModuleDependencyID &rootModuleID,
130+
ArrayRef<ModuleDependencyID> swiftModules, ModuleDependenciesCache &cache,
131+
ModuleDependencyIDSetVector &discoveredHeaderDependencyClangModules);
123132
void
124133
resolveSwiftOverlayDependencies(ArrayRef<ModuleDependencyID> swiftModules,
125134
ModuleDependenciesCache &cache,
@@ -151,6 +160,12 @@ class ModuleDependencyScanner {
151160
ModuleDependenciesCache &cache,
152161
llvm::function_ref<void(ModuleDependencyID)> action);
153162

163+
/// Performance BridgingHeader Chaining.
164+
llvm::Error
165+
performBridgingHeaderChaining(const ModuleDependencyID &rootModuleID,
166+
ModuleDependenciesCache &cache,
167+
ModuleDependencyIDSetVector &allModules);
168+
154169
/// Perform an operation utilizing one of the Scanning workers
155170
/// available to this scanner.
156171
template <typename Function, typename... Args>

include/swift/Frontend/CompileJobCacheKey.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
#include "swift/AST/DiagnosticEngine.h"
2222
#include "swift/Basic/FileTypes.h"
2323
#include "llvm/ADT/ArrayRef.h"
24+
#include "llvm/CAS/CASReference.h"
2425
#include "llvm/CAS/ObjectStore.h"
2526
#include "llvm/Support/Error.h"
2627
#include "llvm/Support/raw_ostream.h"
@@ -49,6 +50,7 @@ createCompileJobCacheKeyForOutput(llvm::cas::ObjectStore &CAS,
4950
llvm::Error printCompileJobCacheKey(llvm::cas::ObjectStore &CAS,
5051
llvm::cas::ObjectRef Key,
5152
llvm::raw_ostream &os);
53+
5254
} // namespace swift
5355

5456
#endif

include/swift/Frontend/FrontendOptions.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,9 @@ class FrontendOptions {
5454
/// An Objective-C header to import and make implicitly visible.
5555
std::string ImplicitObjCHeaderPath;
5656

57+
/// An Objective-C pch to import and make implicitly visible.
58+
std::string ImplicitObjCPCHPath;
59+
5760
/// The map of aliases and real names of imported or referenced modules.
5861
llvm::StringMap<StringRef> ModuleAliasMap;
5962

@@ -369,6 +372,9 @@ class FrontendOptions {
369372
/// Emit remarks indicating use of the serialized module dependency scanning cache.
370373
bool EmitDependencyScannerCacheRemarks = false;
371374

375+
/// The path at which the dependency scanner can write generated files.
376+
std::string ScannerOutputDir;
377+
372378
/// Whether the dependency scanner invocation should resolve imports
373379
/// to filesystem modules in parallel.
374380
bool ParallelDependencyScan = true;

0 commit comments

Comments
 (0)