Skip to content

Commit 382f406

Browse files
committed
[Dependency Scanning] Include initial PCM arguments on Clang module dependency details
Doing so will allow clients to know which Swift-specific PCM arguments are already captured from the scan that first discovered this module. SwiftDriver, in particular, will be able to use this information to avoid re-scanning a given Clang module if the initial scan was sufficient for all possible sets of PCM arguments on Swift modules that depend on said Clang module.
1 parent 1d25b89 commit 382f406

File tree

10 files changed

+62
-17
lines changed

10 files changed

+62
-17
lines changed

include/swift-c/DependencyScan/DependencyScan.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,9 @@ swiftscan_clang_detail_get_context_hash(swiftscan_module_details_t details);
203203
SWIFTSCAN_PUBLIC swiftscan_string_set_t *
204204
swiftscan_clang_detail_get_command_line(swiftscan_module_details_t details);
205205

206+
SWIFTSCAN_PUBLIC swiftscan_string_set_t *
207+
swiftscan_clang_detail_get_captured_pcm_args(swiftscan_module_details_t details);
208+
206209
//=== Batch Scan Input Functions ------------------------------------------===//
207210

208211
/// Create an \c swiftscan_batch_scan_input_t instance.

include/swift/AST/ModuleDependencies.h

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -247,16 +247,22 @@ class ClangModuleDependenciesStorage : public ModuleDependenciesStorageBase {
247247
/// The file dependencies
248248
const std::vector<std::string> fileDependencies;
249249

250+
/// The swift-specific PCM arguments captured by this dependencies object
251+
/// as found by the scanning action that discovered it
252+
const std::vector<std::string> capturedPCMArgs;
253+
250254
ClangModuleDependenciesStorage(
251255
const std::string &moduleMapFile,
252256
const std::string &contextHash,
253257
const std::vector<std::string> &nonPathCommandLine,
254-
const std::vector<std::string> &fileDependencies
258+
const std::vector<std::string> &fileDependencies,
259+
const std::vector<std::string> &capturedPCMArgs
255260
) : ModuleDependenciesStorageBase(ModuleDependenciesKind::Clang),
256261
moduleMapFile(moduleMapFile),
257262
contextHash(contextHash),
258263
nonPathCommandLine(nonPathCommandLine),
259-
fileDependencies(fileDependencies) { }
264+
fileDependencies(fileDependencies),
265+
capturedPCMArgs(capturedPCMArgs) {}
260266

261267
ModuleDependenciesStorageBase *clone() const override {
262268
return new ClangModuleDependenciesStorage(*this);
@@ -361,10 +367,12 @@ class ModuleDependencies {
361367
const std::string &moduleMapFile,
362368
const std::string &contextHash,
363369
const std::vector<std::string> &nonPathCommandLine,
364-
const std::vector<std::string> &fileDependencies) {
370+
const std::vector<std::string> &fileDependencies,
371+
const std::vector<std::string> &capturedPCMArgs) {
365372
return ModuleDependencies(
366373
std::make_unique<ClangModuleDependenciesStorage>(
367-
moduleMapFile, contextHash, nonPathCommandLine, fileDependencies));
374+
moduleMapFile, contextHash, nonPathCommandLine,
375+
fileDependencies, capturedPCMArgs));
368376
}
369377

370378
/// Describe a placeholder dependency swift module.

include/swift/DependencyScan/DependencyScanImpl.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,9 @@ typedef struct {
129129

130130
/// Options to the compile command required to build this clang modulemap
131131
swiftscan_string_set_t *command_line;
132+
133+
/// The swift-specific PCM arguments captured by this dependencies object
134+
swiftscan_string_set_t *captured_pcm_args;
132135
} swiftscan_clang_details_t;
133136

134137
struct swiftscan_module_details_s {

include/swift/DependencyScan/SerializedModuleDependencyCacheFormat.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,6 @@ const unsigned MODULE_DEPENDENCY_CACHE_FORMAT_VERSION_MINOR = 0;
4646
using IdentifierIDField = BCVBR<13>;
4747
using FileIDField = IdentifierIDField;
4848
using ModuleIDField = IdentifierIDField;
49-
using CompilerFlagField = IdentifierIDField;
5049
using ContextHashField = IdentifierIDField;
5150

5251
/// A bit that indicates whether or not a module is a framework
@@ -166,7 +165,8 @@ using ClangModuleDetailsLayout =
166165
FileIDField, // moduleMapPath
167166
ContextHashField, // contextHash
168167
FlagIDArrayIDField, // commandLine
169-
FileIDArrayIDField // fileDependencies
168+
FileIDArrayIDField, // fileDependencies
169+
FlagIDArrayIDField // capturedPCMArgs
170170
>;
171171
} // namespace graph_block
172172

lib/ClangImporter/ClangModuleDependencyScanner.cpp

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -193,13 +193,16 @@ static ClangModuleDependenciesCacheImpl *getOrCreateClangImpl(
193193
void ClangImporter::recordModuleDependencies(
194194
ModuleDependenciesCache &cache,
195195
const FullDependenciesResult &clangDependencies) {
196-
struct ModuleInfo {
197-
std::string PCMPath;
198-
std::string ModuleMapPath;
199-
};
200196
auto &ctx = Impl.SwiftContext;
201197
auto currentSwiftSearchPathSet = ctx.getAllModuleSearchPathsSet();
202198

199+
// This scanner invocation's already-captured APINotes version
200+
std::vector<std::string> capturedPCMArgs = {
201+
"-Xcc",
202+
("-fapinotes-swift-version=" +
203+
ctx.LangOpts.EffectiveLanguageVersion.asAPINotesVersionString())
204+
};
205+
203206
for (const auto &clangModuleDep : clangDependencies.DiscoveredModules) {
204207
// If we've already cached this information, we're done.
205208
if (cache.hasDependencies(
@@ -293,7 +296,8 @@ void ClangImporter::recordModuleDependencies(
293296
clangModuleDep.ClangModuleMapFile,
294297
clangModuleDep.ContextHash,
295298
swiftArgs,
296-
fileDeps);
299+
fileDeps,
300+
capturedPCMArgs);
297301
for (const auto &moduleName : clangModuleDep.ClangModuleDeps) {
298302
dependencies.addModuleDependency(moduleName.ModuleName, &alreadyAddedModules);
299303
}

lib/DependencyScan/ModuleDependencyCacheSerialization.cpp

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -447,10 +447,11 @@ bool Deserializer::readGraph(GlobalModuleDependenciesCache &cache) {
447447
llvm::report_fatal_error("Unexpected CLANG_MODULE_DETAILS_NODE record");
448448
cache.configureForTriple(getTriple());
449449
unsigned moduleMapPathID, contextHashID, commandLineArrayID,
450-
fileDependenciesArrayID;
450+
fileDependenciesArrayID, capturedPCMArgsArrayID;
451451
ClangModuleDetailsLayout::readRecord(Scratch, moduleMapPathID,
452452
contextHashID, commandLineArrayID,
453-
fileDependenciesArrayID);
453+
fileDependenciesArrayID,
454+
capturedPCMArgsArrayID);
454455
auto moduleMapPath = getIdentifier(moduleMapPathID);
455456
if (!moduleMapPath)
456457
llvm::report_fatal_error("Bad module map path");
@@ -463,10 +464,15 @@ bool Deserializer::readGraph(GlobalModuleDependenciesCache &cache) {
463464
auto fileDependencies = getArray(fileDependenciesArrayID);
464465
if (!fileDependencies)
465466
llvm::report_fatal_error("Bad file dependencies");
467+
auto capturedPCMArgs = getArray(capturedPCMArgsArrayID);
468+
if (!capturedPCMArgs)
469+
llvm::report_fatal_error("Bad captured PCM Args");
466470

467471
// Form the dependencies storage object
468472
auto moduleDep = ModuleDependencies::forClangModule(
469-
*moduleMapPath, *contextHash, *commandLineArgs, *fileDependencies);
473+
*moduleMapPath, *contextHash, *commandLineArgs, *fileDependencies,
474+
*capturedPCMArgs);
475+
470476
// Add dependencies of this module
471477
for (const auto &moduleName : *currentModuleDependencies)
472478
moduleDep.addModuleDependency(moduleName);
@@ -572,6 +578,7 @@ enum ModuleIdentifierArrayKind : uint8_t {
572578
BridgingModuleDependencies,
573579
NonPathCommandLine,
574580
FileDependencies,
581+
CapturedPCMArgs,
575582
LastArrayKind
576583
};
577584

@@ -846,7 +853,8 @@ void Serializer::writeModuleInfo(ModuleDependencyID moduleID,
846853
getIdentifier(clangDeps->moduleMapFile),
847854
getIdentifier(clangDeps->contextHash),
848855
getArray(moduleID, ModuleIdentifierArrayKind::NonPathCommandLine),
849-
getArray(moduleID, ModuleIdentifierArrayKind::FileDependencies));
856+
getArray(moduleID, ModuleIdentifierArrayKind::FileDependencies),
857+
getArray(moduleID, ModuleIdentifierArrayKind::CapturedPCMArgs));
850858

851859
break;
852860
}
@@ -1015,6 +1023,8 @@ void Serializer::collectStringsAndArrays(
10151023
clangDeps->nonPathCommandLine);
10161024
addArray(moduleID, ModuleIdentifierArrayKind::FileDependencies,
10171025
clangDeps->fileDependencies);
1026+
addArray(moduleID, ModuleIdentifierArrayKind::CapturedPCMArgs,
1027+
clangDeps->capturedPCMArgs);
10181028
break;
10191029
}
10201030
default:

lib/DependencyScan/ScanDependencies.cpp

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -716,6 +716,10 @@ static void writeJSON(llvm::raw_ostream &out,
716716

717717
// Command line.
718718
writeJSONSingleField(out, "commandLine", clangDeps->command_line, 5,
719+
/*trailingComma=*/true);
720+
721+
// Captured PCM arguments.
722+
writeJSONSingleField(out, "capturedPCMArgs", clangDeps->captured_pcm_args, 5,
719723
/*trailingComma=*/false);
720724
}
721725

@@ -841,7 +845,6 @@ generateFullDependencyGraph(CompilerInstance &instance,
841845
// TODO: Once the clients are taught about the new dependency kind,
842846
// switch to using a bespoke kind here.
843847
details->kind = SWIFTSCAN_DEPENDENCY_INFO_SWIFT_TEXTUAL;
844-
845848
details->swift_textual_details = {
846849
moduleInterfacePath,
847850
create_empty_set(),
@@ -870,7 +873,9 @@ generateFullDependencyGraph(CompilerInstance &instance,
870873
details->clang_details = {
871874
create_clone(clangDeps->moduleMapFile.c_str()),
872875
create_clone(clangDeps->contextHash.c_str()),
873-
create_set(clangDeps->nonPathCommandLine)};
876+
create_set(clangDeps->nonPathCommandLine),
877+
create_set(clangDeps->capturedPCMArgs)
878+
};
874879
}
875880
return details;
876881
};

test/ScanDependencies/module_deps.swift

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,11 @@ import SubE
133133
// CHECK-NEXT: "-module-name",
134134
// CHECK-NEXT: "C"
135135

136+
// CHECK: "capturedPCMArgs": [
137+
// CHECK-NEXT: "-Xcc",
138+
// CHECK-NEXT: "-fapinotes-swift-version=4"
139+
// CHECK-NEXT: ]
140+
136141
/// --------Swift module E
137142
// CHECK: "swift": "E"
138143
// CHECK-LABEL: modulePath": "E.swiftmodule"

tools/libSwiftScan/libSwiftScan.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ void swiftscan_dependency_info_details_dispose(
7373
swiftscan_string_dispose(details_impl->clang_details.module_map_path);
7474
swiftscan_string_dispose(details_impl->clang_details.context_hash);
7575
swiftscan_string_set_dispose(details_impl->clang_details.command_line);
76+
swiftscan_string_set_dispose(details_impl->clang_details.captured_pcm_args);
7677
break;
7778
}
7879
delete details_impl;
@@ -345,6 +346,11 @@ swiftscan_clang_detail_get_command_line(swiftscan_module_details_t details) {
345346
return details->clang_details.command_line;
346347
}
347348

349+
swiftscan_string_set_t *
350+
swiftscan_clang_detail_get_captured_pcm_args(swiftscan_module_details_t details) {
351+
return details->clang_details.captured_pcm_args;
352+
}
353+
348354
//=== Batch Scan Input Functions ------------------------------------------===//
349355

350356
swiftscan_batch_scan_input_t *swiftscan_batch_scan_input_create() {

tools/libSwiftScan/libSwiftScan.exports

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ swiftscan_swift_placeholder_detail_get_module_source_info_path
2424
swiftscan_clang_detail_get_module_map_path
2525
swiftscan_clang_detail_get_context_hash
2626
swiftscan_clang_detail_get_command_line
27+
swiftscan_clang_detail_get_captured_pcm_args
2728
swiftscan_batch_scan_input_set_modules
2829
swiftscan_batch_scan_entry_set_module_name
2930
swiftscan_batch_scan_entry_set_arguments

0 commit comments

Comments
 (0)