Commit Graph

125 Commits

Author SHA1 Message Date
Evan Wilde
250082df25 [NFC] Reformat all the LLVMs
Reformatting everything now that we have `llvm` namespaces. I've
separated this from the main commit to help manage merge-conflicts and
for making it a bit easier to read the mega-patch.
2023-06-27 09:03:52 -07:00
Evan Wilde
f3ff561c6f [NFC] add llvm namespace to Optional and None
This is phase-1 of switching from llvm::Optional to std::optional in the
next rebranch. llvm::Optional was removed from upstream LLVM, so we need
to migrate off rather soon. On Darwin, std::optional, and llvm::Optional
have the same layout, so we don't need to be as concerned about ABI
beyond the name mangling. `llvm::Optional` is only returned from one
function in
```
getStandardTypeSubst(StringRef TypeName,
                     bool allowConcurrencyManglings);
```
It's the return value, so it should not impact the mangling of the
function, and the layout is the same as `std::optional`, so it should be
mostly okay. This function doesn't appear to have users, and the ABI was
already broken 2 years ago for concurrency and no one seemed to notice
so this should be "okay".

I'm doing the migration incrementally so that folks working on main can
cherry-pick back to the release/5.9 branch. Once 5.9 is done and locked
away, then we can go through and finish the replacement. Since `None`
and `Optional` show up in contexts where they are not `llvm::None` and
`llvm::Optional`, I'm preparing the work now by going through and
removing the namespace unwrapping and making the `llvm` namespace
explicit. This should make it fairly mechanical to go through and
replace llvm::Optional with std::optional, and llvm::None with
std::nullopt. It's also a change that can be brought onto the
release/5.9 with minimal impact. This should be an NFC change.
2023-06-27 09:03:52 -07:00
Steven Wu
284eb2a264 [NFC] Cleanup deprecated Optional API usage in depscan 2023-06-26 10:34:27 -07:00
Steven Wu
2b7d38dc16 [CAS] Improve swift cas options
Using the same CASOption from clang to communicate CAS configurations so
it is easier to exchange CAS configuration between them.
2023-06-26 10:34:26 -07:00
Steven Wu
b1f99b8e93 [CAS] swift dependency scanning using CAS for compiler caching (#66366)
Teach swift dependency scanner to use CAS to capture the full dependencies for a build and construct build commands with immutable inputs from CAS.

This allows swift compilation caching using CAS.
2023-06-12 10:55:53 -07:00
Artem Chikin
9d4e831ad3 [Dependency Scanner] Ensure the Clang dependency scanner working directory matches the invocation when querying bridging header dependencies
The Swift compiler does not have a concept of a working directory. It is instead handled by the Swift driver by resolving relative paths according to the driver's working directory argument. On the other hand, Clang does have a concept working directory which may be specified on this Clang invocation with '-working-directory'. If so, it is crucial that we use this directory as an argument to the Clang scanner API. Otherwiswe, we risk having a mismatch between the working directory specified on the scanner's Clang invocation and the one use from the scanner API entry-points, which leads to downstream inconsistencies and errors.

This was originally fixed for the main by-name module dependencies query in https://github.com/apple/swift/pull/61025 (03136e06aa), but the Bridging Header dependencies code has continued to incorrectly expect the Swift ASTContext to both have the working directory set *and* be consistent with that of the Clang scanner instance.

Resolves rdar://108464467
2023-04-26 10:55:01 -07:00
Artem Chikin
6fcd8be072 [Dependency Scanning] Pull optional dependencies from the adjacent binary module for direct interface dependencies
For a `@Testable` import in program source, if a Swift interface dependency is discovered, and has an adjacent binary `.swiftmodule`, open up the module, and pull in its optional dependencies. If an optional dependency cannot be resolved on the filesystem, fail silently without raising a diagnostic.
2023-04-17 14:47:46 -07:00
Artem Chikin
0f7e909a28 Always specify -gmodules on built-in Clang invocations
This is currently always done when instantiating `ClangImporter` by manually setting the option on the Clang invocation with:
```
Invocation->getCodeGenOpts().DebugTypeExtRefs = true
```
Now also add it to `importer::addCommonInvocationArguments` so that the dependency scanner always generates command-lines with the required for Swift Clang flags.

Resolves rdar://107570568
2023-04-05 09:42:47 -07:00
Ben Langmuir
9f2ab36888 Update for clang dependency scanning API change 2023-04-04 10:47:21 -07:00
Artem Chikin
4ed874c276 [Dependency Scanning] Do not add extra search paths to the Clang's explicit PCM prebuild commands
Clang dependency scanner's output will be complete here w.r.t. the required search paths.

Resolves rdar://107018240
2023-03-22 09:36:17 -07:00
swift-ci
8e7eae199e Merge remote-tracking branch 'origin/main' into rebranch 2023-02-08 07:33:02 -08:00
Artem Chikin
be3812d686 [Dependency Scanning] Add swift dependency compile arguments required for self-contained commands
- '-o <output_path>'
- '-disable-implicit-swift-modules'
- '-Xcc -fno-implicit-modules' and '-Xcc -fno-implicit-module-maps'
- '-candidate-module-file'

These were previously supplied by the driver. Instead, they will now be ready to be run directly from the dependency scanner's output.
2023-02-07 11:35:11 -07:00
Artem Chikin
c24c81e979 [Dependency Scanning] Adopt new Clang scan-deps C++ API for querying module deps
As introduced in https://reviews.llvm.org/D140176.
2023-02-02 09:13:46 -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
1230966e80 [Dependency Scanner] Rename 'ModuleDependenceis' -> 'ModuleDependencyInfo' 2022-12-15 14:18:29 -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
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
dc7cae21d2 [Dependency Scanning] Produce canonical output path for Clang PCMs.
Instead of relying on the client (driver) to perform its own computation of the matching output path.
2022-11-14 13:53:40 -08: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
c5bd61bf25 [Dependency Scanning] Adapt 'ClangModuleDependencyScanner' output to clang frontend-only results coming from scan-deps 2022-09-28 09:00:59 -07:00
Jan Svoboda
38686bd402 [importer] Adjust for new dependency scanner API 2022-09-27 15:55:28 -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
d8f77c601b [Dependency Scanner] Use Clang scanner's module name query API instead of creating a dummy file 2022-09-12 10:30:19 -07:00
Artem Chikin
03136e06aa [Dependency Scanner] Ensure the Clang dependency scanner working directory matches the invocation.
The Swift compiler does not have a concept of a working directory. It is instead handled by the Swift driver by resolving relative paths according to the driver's working directory argument. On the other hand, Clang does have a concept working directory which may be specified on this Clang invocation with '-working-directory'. If so, it is crucial that we use this directory as an argument to the Clang scanner API. Otherwiswe, we risk having a mismatch between the working directory specified on the scanner's Clang invocation and the one use from the scanner API entry-points, which leads to downstream inconsistencies and errors.
2022-09-09 13:32:14 -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
Steven Wu
9bd2d40084 [ClangImporter] Update to match new DependencyScanningService API
Update ClangImporter to match new DependencyScanningService API from
clang depscan.
2022-08-30 12:49:17 -07:00
Steven Wu
149efac878 Update swift to build against cas-support 2022-06-27 16:44:16 -07:00
Ben Barham
a3f14cc733 [next] Update dependency scanner to inline old API
apple/llvm-project 000a90ab63ce0595d442b3e7e8adc9c0506d2525 removed
`getAdditionalArgsWithoutModulePaths`.
092b2ddf7a swapped to using
`getCommandLineWithoutModulePaths` instead, but this is (almost) all cc1
args used to build the module - which is very different to what was
being looped over previously.

Inline `getAdditionalArgsWithoutModulePaths` instead.

Resolves rdar://94545725.
2022-06-08 14:45:13 -07:00
Ben Barham
092b2ddf7a [next] Update dependency scanner to use new Clang API
apple/llvm-project 000a90ab63ce0595d442b3e7e8adc9c0506d2525 removed
`getAdditionalArgsWithoutModulePaths`. Based on the v2 libclang API, we
should be using `getCommandLineWithoutModulePaths` now.
2022-05-05 16:25:10 -07:00
Alex Hoppen
fe7878ecce [Serialization] Improve module loading performance
When looking for a Swift module on disk, we were scanning all module search paths if they contain the module we are searching for. In a setup where each module is contained in its own framework search path, this scaled quadratically with the number of modules being imported. E.g. a setup with 100 modules being imported form 100 module search paths could cause on the order of 10,000 checks of `FileSystem::exists`. While these checks are fairly fast (~10µs), they add up to ~100ms.

To improve this, perform a first scan of all module search paths and list the files they contain. From this, create a lookup map that maps filenames to the search paths they can be found in. E.g. for
```
searchPath1/
  Module1.framework

searchPath2/
  Module1.framework
  Module2.swiftmodule
```
we create the following lookup table
```
Module1.framework -> [searchPath1, searchPath2]
Module2.swiftmodule -> [searchPath2]
```
2021-12-14 12:44:13 +01:00
Ben Barham
5e43702ff6 [DepScanner] Do not remove the resource directory path
The clang importer sets the clang resource directory to
`RuntimeResourcePath`/clang (usually "lib/swift/clang", a symbolic link
into "lib/clang/<version>"). This was inadvertently being removed in a
check to remove the clang *executable* path.

Modify that check to only the first argument so the resource directory
is properly passed through.
2021-10-28 15:53:44 +10:00
swift-ci
3f8fea8508 Merge remote-tracking branch 'origin/main' into rebranch 2021-10-06 10:17:29 -07:00
Artem Chikin
a17d5ee012 [Dependency Scanning] Emit path to clang from PCM build command-line
It gets interpreted as just an executable we feed to the linker, since we launch `emit-pcm` actions via `swift-frontend` invocations, not `clang`.
2021-09-30 15:02:25 -07:00
swift-ci
a7839bb80f Merge remote-tracking branch 'origin/main' into rebranch 2021-09-23 14:14:43 -07:00
Artem Chikin
6176657285 [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.
2021-09-22 11:34:02 -07:00
swift-ci
d12197f488 Merge remote-tracking branch 'origin/main' into rebranch 2021-09-21 08:33:46 -07: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
Evan Wilde
e6c25be684 fix SingleCommandCompilationDatabase references
The SingleCommandCompilationDatabase type was removed from LLVM and the
API's that took it were changed to just take the vector of arguments
instead. This patch updates the calls to `getFullDependencies` to pass
in the argument vector to reflect that change and get things building
again.
2021-09-15 16:35: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
Eric Miotto
fc366f0e96 [rebranch] remove unneeded SingleCommandCompilationDatabase (#38961)
This class will now be provided by LLVM as part of apple/llvm-project#3183
2021-08-20 07:42:13 -07:00
Arnold Schwaighofer
5a83172a55 Merge remote-tracking branch 'upstream/main' into rebranch 2021-08-05 12:04:56 -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
Eric Miotto
ad6076a7bc Merge 'origin/main' into rebranch - 2021-07-16
Conflicts:
	lib/IRGen/IRGenFunction.cpp
    Take the changes made in #38386
2021-07-16 07:57:06 -07:00
Artem Chikin
d6f7a017ab [Dependency Scanning] Do not include -target in extraPCMArgs when scanning an interface
https://github.com/apple/swift/pull/37774 (related to rdar://72480261) has made it so that the target of built clang modules (even downstream from Swift interface dependencies) can be consistent with that of the main module. This means that when building transitive Clang dependencies of the main module, they will always have a matching triple to the main module itself (ensured with `-clang-target`). This means we no longer have to report the target triple in the set of `extraPCMArgs` which encode an interface-specific requirement for building its dependencies.
2021-07-15 16:24:37 -07:00
swift_jenkins
992318c128 Merge remote-tracking branch 'origin/main' into next 2021-06-24 16:01:49 -07:00
Ben Barham
28bb2505b0 [ClangImporter] Respect -working-directory in the created VFS
Pass a wrapped VFS down into `clang::createInvocationFromCommandLine` so
that the working directory is set and then used in the underlying Clang
`CompilerInstance`.

Fixes the possibility of differing modules hashes when the same
arguments are used in Clang directly vs from the importer.

Resolves rdar://79376364
2021-06-24 11:07:40 +10:00
Evan Wilde
1cec66e158 Update ContextHash access
The ContextHash is also in the ID of the ModuleDep, so access it through
there.
2021-06-22 11:48:43 -07:00
Evan Wilde
450b78f48d NonPathCommandLine -> getAdditionalArgsWithoutModulePaths()
This value was replaced with a call to 'getNonPathCommandLine()' in
llvm-project commit 2b73565210ef0b3b29e0f067eef150a32adbb967. The API
was later renamed 'getAdditionalArgsWithoutModulePaths()' in
llvm-project commit 77fc4d1feef5f3041f614206447b4461992054bb.
2021-06-22 11:01:37 -07:00
Evan Wilde
96aae72756 Update ModuleName lookup
The ModuleName is now put behind a ModuleID struct called "ID" instead
of on the ModuleDep directly.
2021-06-22 10:53:01 -07:00