Commit Graph

34 Commits

Author SHA1 Message Date
Artem Chikin
b1707ef534 Merge pull request #63201 from artemcm/ScannerNoPriorSourceDeps
[Dependency Scanner] Do not re-use prior source dependencies in successive scans
2023-01-26 08:57:00 -08:00
Artem Chikin
e2eb8c8f47 [Dependency Scanner] Do not re-use prior source dependencies in successive scans
If a scanner instance is used for multiple scans, the `SwiftDependencyScanningService` will have collected prior scans' main source modules' dependency info.
Suppose the scanner first scans a source module `A` and records its dependencies. If a successive scan of some source module `B` finds that it depends on `A`, it would get a cache hit on the source module from a prior scan and re-use dependency information. This change no longer allows such re-use of the prior source module scan dependencies. Said prior scan may have had an entirely different context and therefore different set of dependencies on `A` than may be visible otherwise. Instead, all scanning actions must rely on such modules being re-discovered on the filesystem.
2023-01-25 16:47:38 -08:00
Erik Eckstein
7d8bf37e5e change to the new llvm::Optional APIs
This is a follow-up of https://github.com/apple/swift/pull/62217
2023-01-25 09:18:36 +01:00
Artem Chikin
12477b7b79 [Dependency Scanning] Refactor the scanner to resolve unqualified module imports
This changes the scanner's behavior to "resolve" a discovered module's dependencies to a set of Module IDs: module name + module kind (swift textual, swift binary, clang, etc.).

The 'ModuleDependencyInfo' objects that are stored in the dependency scanner's cache now carry a set of kind-qualified ModuleIDs for their dependencies, in addition to unqualified imported module names of their dependencies.

Previously, the scanner's internal state would cache a module dependnecy as having its own set of dependencies which were stored as names of imported modules. This led to a design where any time we needed to process the dependency downstream from its discovery (e.g. cycle detection, graph construction), we had to query the ASTContext to resolve this dependency's imports, which shouldn't be necessary. Now, upon discovery, we "resolve" a discovered dependency by executing a lookup for each of its imported module names (this operation happens regardless of this patch) and store a fully-resolved set of dependencies in the dependency module info.

Moreover, looking up a given module dependency by name (via `ASTContext`'s `getModuleDependencies`) would result in iterating over the scanner's module "loaders" and querying each for the module name. The corresponding modules would then check the scanner's cache for a respective discovered module, and if no such module is found the "loader" would search the filesystem.

This meant that in practice, we searched the filesystem on many occasions where we actually had cached the required dependency, as follows:
Suppose we had previously discovered a Clang module "foo" and cached its dependency info.
-> ASTContext.getModuleDependencies("foo")
--> (1) Swift Module "Loader" checks caches for a Swift module "foo" and doesn't find one, so it searches the filesystem for "foo" and fails to find one.
--> (2) Clang Module "Loader" checks caches for a Clang module "foo", finds one and returns it to the client.

This means that we were always searching the filesystem in (1) even if we knew that to be futile.
With this change, queries to `ASTContext`'s `getModuleDependencies` will always check all the caches first, and only delegate to the scanner "loaders" if no cached dependency is found. The loaders are then no longer in the business of checking the cached contents.

To handle cases in the scanner where we must only lookup either a Swift-only module or a Clang-only module, this patch splits 'getModuleDependencies' into an alrady-existing 'getSwiftModuleDependencies' and a newly-added 'getClangModuleDependencies'.
2023-01-05 11:44:06 -08:00
Artem Chikin
a59db26a55 [Dependency Scanner] Rename 'GlobalModuleDependenciesCache' -> 'SwiftDependencyScanningService' 2022-12-19 11:08:50 -08:00
Artem Chikin
1230966e80 [Dependency Scanner] Rename 'ModuleDependenceis' -> 'ModuleDependencyInfo' 2022-12-15 14:18:29 -08:00
Artem Chikin
44a8f99d19 [DependencyScanning] Count target variant, clang target, and sdk versions as components of scanning context hash
Resolves rdar://103093122
2022-12-09 09:24:44 -08:00
Artem Chikin
9f62b19058 [Dependency Scanning] Use persistent dependency scanning filesystem in the Swift dependency scanner
Adopts Clang's 'DependencyScanningWorkerFilesystem' for use by the scanner, with the persistent
scanner instance keeping a 'DependencyScanningFilesystemSharedCache'.
2022-12-08 11:41:20 -08:00
Artem Chikin
f39c6385df [Dependency Scanning] Do not disambiguate 'GlobalModuleDependenciesCache' by search path set
This is no longer necessary since the cache is always configured for the current scanning context hash, which includes the search path set.
2022-12-06 14:15:39 -08:00
Artem Chikin
990b95e9d8 [Dependency Scanning] Key into a global (shared) dependency scanning cache via context hash
Introduces a concept of a dependency scanning action context hash, which is used to select an instance of a global dependency scanning cache which gets re-used across dependency scanning actions.
2022-12-06 08:26:28 -08:00
Erik Eckstein
ab1b343dad use new llvm::Optional API
`getValue` -> `value`
`getValueOr` -> `value_or`
`hasValue` -> `has_value`
`map` -> `transform`

The old API will be deprecated in the rebranch.
To avoid merge conflicts, use the new API already in the main branch.

rdar://102362022
2022-11-21 19:44:24 +01:00
Artem Chikin
11cd472f14 [Dependency Scanning] Configure the Clang dependency scanner to generate PCM output paths
Using the convention of : <Module_Cache_Path> + "/" + <Module_Name> + "-" + <Context_Hash> + ".pcm"
2022-11-09 09:49:35 -08:00
Artem Chikin
b426757eda [Dependency Scanning] Share the Clang Dependency Scanning service among scanner invocations.
It was recently moved to the ModuleDependenciesCache. This is undesireable, instead this should live in the GlobalModuleDependenciesCache so that we benefit from the filesystem caching it performs across diferent scanning actions.
2022-09-29 16:06:56 -07:00
Artem Chikin
96a82cdf9a [Dependency Scanner] Clean up/Gardening on 'ClangModuleDependencyScanner'
Move clangScanningTool and clangScanningService to be parts of 'ModuleDependenciesCache' state, getting rid of the 'ClangModuleDependenciesCacheImpl', which is no-longer needed since we moved moved to by-name lookup of Clang modules.
2022-09-12 15:10:43 -07:00
Artem Chikin
6da780e272 [Dependency Scanner] Diagnose failure to find a module
And produce a dependency path from the missing dependency to the main module being scanned.
2022-09-07 15:56:44 -07:00
Artem Chikin
eebebd9a55 [Dependency Scanning] Do not persist cached Clang module dependencies between scans.
This change tweaks the 'GlobalModuleDependenciesCache', which persists across scanner invocations with the same 'DependencyScanningTool' to no longer cache discovered Clang modules.

Doing so felt like a premature optimization, and we should instead attempt to share as much state as possible by keeping around the actual Clang scanner's state, which performs its own caching. Caching discovered dependencies both in the Clang scanner instance, and in our own cache is much more error-prone - the Clang scanner has a richer context for what is okay and not okay to cache/re-use.

Instead, we still cache discovered Clang dependencies *within* a given scan, since those are discovered using a common Clang scanner instance and should be safe to keep for the duration of the scan.

This change should make it simpler to pin down the core functionality and correctness of the scanner.
Once we turn our attention to the scanner's performance, we can revisit this strategy and optimize the caching behaviour.
2022-08-29 15:40:59 -07:00
Artem Chikin
9fcd202918 [Dependency Scanning] Do not add underlying Clang moduel to persistent (global) scanner cache
When we are building a Swift module which has an underlying Clang module, and which generates an ObjC interface ('-Swift.h'), the mechanism for building the latter involves a VFS redirect of its modulemap to one that does not yet have the generated Swift code, because it must be built before the Swift portion is built because the Swift portion depends on it. This means that the invocation to build this module is different to one used by the clients which depend on this module.

To avoid the subsequent client scans from re-using the partial (VFS-redirected) module, ensure that we do not store dependency info of the underlying Clang module into the global scanner cache. This will cause subsequent client scans to re-scan for this module, and find the fully-resolved modulemap without a VFS redirect.

Resolves rdar://88309064
2022-08-26 10:31:01 -07:00
Ellie Shin
5da881256f Module aliasing: Use real module names with -scan-dependencies
Resolves rdar://85991587
2021-12-02 15:45:30 -08:00
Artem Chikin
709676c20c [Dependency Scanning] Make GlobalModuleDependenciesCache aware of the current scanning action's target triple
And only resolve cached dependencies that came from scanning actions with the same target triple.

This change means that the `GlobalModuleDependenciesCache` must be configured with a specific target triple for every scannig action, and it will only resolve previously-found dependencies from previous scannig actions using the exact same triple.

Furthermore, the `GlobalModuleDependenciesCache` separately tracks source-file-based module dependencies as those represent main Swift modules of previous scanning actions, and we must be able to resolve those regardless of the target triple.

Resolves rdar://83105455
2021-09-20 11:21:16 -07:00
Artem Chikin
e64a40451b [Dependency Scanning] Model main module as separate dependency kind: SwiftSource
These kinds of modules differ from `SwiftTextual` modules in that they do not have an interface and have source-files.
It is cleaner to enforce this distinction with types, instead of checking for interface optionality everywhere.
2021-09-15 09:31:20 -07:00
Artem Chikin
9429136112 [Dependency Scanning] Split the ModuleDependencyCache into two: current scan cache & global.
This change causes the cache to be layered with a local "cache" that wraps the global cache, which will serve as the source of truth. The local cache persists only for the duration of a given scanning action, and has a store of references to dependencies resolved as a part of the current scanning action only, while the global cache is the one that persists across scanning actions (e.g. in `DependencyScanningTool`) and stores actual module dependency info values.

Only the local cache can answer dependency lookup queries, checking current scanning action results first, before falling back to querying the global cache, with queries disambiguated by the current scannning action's search paths, ensuring we never resolve a dependency lookup query with a module info that could not be found in the current action's search paths.

This change is required because search-path disambiguation can lead to false-negatives: for example, the Clang dependency scanner may find modules relative to the compiler's path that are not on the compiler's direct search paths. While such false-negative query responses should be functionally safe, we rely on the current scanning action's results being always-present-in-the-cache for the scanner's functionality. This layering ensures that the cache use-sites remain unchanged and that we get both: preserved global state which can be queried disambiguated with the search path details, and an always-consistent local (current action) cache state.
2021-08-06 09:13:42 -07:00
Artem Chikin
6a12dc0070 [Dependency Scanning] Have the scanner cache answer queries relevant to current search paths only.
The dependency scanner's cache persists across different queries and answering a subsequent query's module lookup with a module not in the query's search path is not correct.

For example, suppose we are looking for a Swift module `Foo` with a set of search paths `SP`.
And dependency scanner cache already contains a module `Foo`, for which we found an interface file at location `L`. If `L`∉`SP`, then we cannot re-use the cached entry because we’d be resolving the scanning query to a filesystem location that the current scanning context is not aware of.

Resolves rdar://81175942
2021-07-30 09:53:04 -07:00
Artem Chikin
14229f13b0 [Dependency Scanning] Add a binary serialization format for the Inter-Module Dependencies Cache
- Adds serialization format based on the LLVM Bitcode File Format (https://llvm.org/docs/BitCodeFormat.html).
- Adds Serialization and Deserialization code.
2021-05-21 14:31:00 -07:00
Artem Chikin
3221734e7b Merge pull request #34244 from artemcm/RefactorScannerBinaryDependencies
[Dependency Scanner] Refactor ModuleDependencies to represent binary-only Swift modules explicitly
2020-10-12 17:03:25 -07:00
Artem Chikin
f9d6c6a619 [Dependency Scanner] Refactor ModuleDependencies to represent binary-only Swift modules explicitly
This matches the behavior of the current client (`swift-driver`) and reduces ambiguity in how the nodes in the graph are to be treated. Swift dependencies with a textual interface, for example, must be built into a binary module by clients. Swift dependencies without a textual interface, with only a binary module, are to be used directly, without any up-to-date checks.

Note, this is distinct from Swift dependencies that have a textual interface, for which we also detect potential pre-build binary module candidates. Those are still reported in the `details` field of textual Swift dependencies as `prebuiltModuleCandidates`.
2020-10-12 09:56:03 -07:00
Brent Royal-Gordon
d02b34cccc [NFC] Add conveniences to clean up import code 2020-10-09 18:59:19 -07:00
Artem Chikin
965ca698d5 Add in-source rationale for why Placeholder dependencies are needed. 2020-07-28 11:34:27 -07:00
Artem Chikin
746e89b252 Rename external dependencies to placeholder dependencies. 2020-07-27 09:24:36 -07:00
Artem Chikin
b5af62f93f Add new dependency kind (swiftExternal) to model external dependencies. 2020-07-27 09:24:35 -07:00
Xi Ge
056f00322d DependenciesScanner: refactor cross import overlay resolver to a standalone function. NFC
This change facilitates resolving cross-import overlays from transitive dependencies.
2020-07-23 17:47:47 -07:00
Saleem Abdulrasool
09975d1253 sprinkle llvm_unreachable for covered switches (NFC)
Annotate the covered switches with `llvm_unreachable` to avoid the MSVC
warning which does not recognise the covered switches.  This allows us
to avoid a spew of warnings.
2020-05-07 11:05:35 -07:00
Michael Forster
4852210fd5 More StringRef->std::string conversion fixes 2020-04-29 17:40:51 +02:00
Doug Gregor
d6484c8280 [Dependency scanning] Scan and record bridging header dependencies.
When there is a bridging header associated with the module, scan and record
its dependencies. Note them in a separate structure to capture the specific
dependencies of the bridging header.
2020-04-24 12:58:45 -07:00
Doug Gregor
33cdd61835 Fast dependency scanning for Swift
Implement a new "fast" dependency scanning option,
`-scan-dependencies`, in the Swift frontend that determines all
of the source file and module dependencies for a given set of
Swift sources. It covers four forms of modules:

1) Swift (serialized) module files, by reading the module header
2) Swift interface files, by parsing the source code to find imports
3) Swift source modules, by parsing the source code to find imports
4) Clang modules, using Clang's fast dependency scanning tool

A single `-scan-dependencies` operation maps out the full
dependency graph for the given Swift source files, including all
of the Swift and Clang modules that may need to be built, such
that all of the work can be scheduled up front by the Swift
driver or any other build system that understands this
option. The dependency graph is emitted as JSON, which can be
consumed by these other tools.
2020-04-24 12:58:41 -07:00