Skip to content

Update swift compiler after clang gmodule CAS build update #83621

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion include/swift/AST/ModuleDependencies.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
#include "llvm/ADT/DenseSet.h"
#include "llvm/ADT/IntrusiveRefCntPtr.h"
#include "llvm/ADT/StringSet.h"
#include "llvm/CAS/CASConfiguration.h"
#include "llvm/CAS/CachingOnDiskFileSystem.h"
#include "llvm/Support/Mutex.h"
#include <optional>
Expand Down Expand Up @@ -967,7 +968,7 @@ using ModuleDependenciesKindMap =
/// dependency scanner.
class SwiftDependencyScanningService {
/// The CASOption created the Scanning Service if used.
std::optional<clang::CASOptions> CASOpts;
std::optional<llvm::cas::CASConfiguration> CASConfig;

/// The persistent Clang dependency scanner service
std::optional<clang::tooling::dependencies::DependencyScanningService>
Expand Down
14 changes: 12 additions & 2 deletions include/swift/Basic/CASOptions.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

#include "clang/CAS/CASOptions.h"
#include "llvm/ADT/Hashing.h"
#include "llvm/CAS/CASConfiguration.h"

namespace swift {

Expand All @@ -37,8 +38,8 @@ class CASOptions final {
/// Import modules from CAS.
bool ImportModuleFromCAS = false;

/// CASOptions
clang::CASOptions CASOpts;
/// CAS Configuration.
llvm::cas::CASConfiguration Config;

/// Clang Include Trees.
std::string ClangIncludeTree;
Expand Down Expand Up @@ -80,6 +81,15 @@ class CASOptions final {
llvm::hash_code getModuleScanningHashComponents() const {
return getPCHHashComponents();
}

/// Return corresponding clang::CASOptions for swift CASOptions.
clang::CASOptions getClangCASOptions() const {
clang::CASOptions CASOpts;
CASOpts.CASPath = Config.CASPath;
CASOpts.PluginPath = Config.PluginPath;
CASOpts.PluginOptions = Config.PluginOptions;
return CASOpts;
}
};

} // namespace swift
Expand Down
8 changes: 4 additions & 4 deletions lib/AST/ModuleDependencies.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -636,9 +636,9 @@ bool SwiftDependencyScanningService::setupCachingDependencyScanningService(
if (!Instance.getInvocation().getCASOptions().EnableCaching)
return false;

if (CASOpts) {
if (CASConfig) {
// If CASOption matches, the service is initialized already.
if (*CASOpts == Instance.getInvocation().getCASOptions().CASOpts)
if (*CASConfig == Instance.getInvocation().getCASOptions().Config)
return false;

// CASOption mismatch, return error.
Expand All @@ -647,12 +647,12 @@ bool SwiftDependencyScanningService::setupCachingDependencyScanningService(
}

// Setup CAS.
CASOpts = Instance.getInvocation().getCASOptions().CASOpts;
CASConfig = Instance.getInvocation().getCASOptions().Config;

ClangScanningService.emplace(
clang::tooling::dependencies::ScanningMode::DependencyDirectivesScan,
clang::tooling::dependencies::ScanningOutputFormat::FullIncludeTree,
Instance.getInvocation().getCASOptions().CASOpts,
Instance.getInvocation().getCASOptions().getClangCASOptions(),
Instance.getSharedCASInstance(), Instance.getSharedCacheInstance(),
/*CachingOnDiskFileSystem=*/nullptr,
// The current working directory optimization (off by default)
Expand Down
10 changes: 5 additions & 5 deletions lib/Basic/CASOptions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,14 @@ void CASOptions::enumerateCASConfigurationFlags(
llvm::function_ref<void(llvm::StringRef)> Callback) const {
if (EnableCaching) {
Callback("-cache-compile-job");
if (!CASOpts.CASPath.empty()) {
if (!Config.CASPath.empty()) {
Callback("-cas-path");
Callback(CASOpts.CASPath);
Callback(Config.CASPath);
}
if (!CASOpts.PluginPath.empty()) {
if (!Config.PluginPath.empty()) {
Callback("-cas-plugin-path");
Callback(CASOpts.PluginPath);
for (auto Opt : CASOpts.PluginOptions) {
Callback(Config.PluginPath);
for (auto Opt : Config.PluginOptions) {
Callback("-cas-plugin-option");
Callback((llvm::Twine(Opt.first) + "=" + Opt.second).str());
}
Expand Down
2 changes: 1 addition & 1 deletion lib/ClangImporter/ClangImporter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1184,7 +1184,7 @@ std::optional<std::vector<std::string>> ClangImporter::getClangCC1Arguments(
// compiler can be more efficient to compute swift cache key without having
// the knowledge about clang command-line options.
if (ctx.CASOpts.EnableCaching || ctx.CASOpts.ImportModuleFromCAS) {
CI->getCASOpts() = ctx.CASOpts.CASOpts;
CI->getCASOpts() = ctx.CASOpts.getClangCASOptions();
// When clangImporter is used to compile (generate .pcm or .pch), need to
// inherit the include tree from swift args (last one wins) and clear the
// input file.
Expand Down
9 changes: 6 additions & 3 deletions lib/DependencyScan/ScanDependencies.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,8 @@ class ExplicitModuleDependencyResolver {
bridgingHeaderBuildCmd.push_back("-Xcc");
bridgingHeaderBuildCmd.push_back("-fmodule-file-cache-key");
bridgingHeaderBuildCmd.push_back("-Xcc");
bridgingHeaderBuildCmd.push_back(clangDep->mappedPCMPath);
bridgingHeaderBuildCmd.push_back(
llvm::sys::path::filename(clangDep->mappedPCMPath).str());
bridgingHeaderBuildCmd.push_back("-Xcc");
bridgingHeaderBuildCmd.push_back(clangDep->moduleCacheKey);
}
Expand Down Expand Up @@ -276,17 +277,19 @@ class ExplicitModuleDependencyResolver {
bool handleClangModuleDependency(
ModuleDependencyID depModuleID,
const ClangModuleDependencyStorage &clangDepDetails) {
auto pcmPath =
llvm::sys::path::filename(clangDepDetails.mappedPCMPath).str();
if (!resolvingDepInfo.isSwiftSourceModule()) {
if (!resolvingDepInfo.isClangModule()) {
commandline.push_back("-Xcc");
commandline.push_back("-fmodule-file=" + depModuleID.ModuleName + "=" +
clangDepDetails.mappedPCMPath);
pcmPath);
}
if (!clangDepDetails.moduleCacheKey.empty()) {
commandline.push_back("-Xcc");
commandline.push_back("-fmodule-file-cache-key");
commandline.push_back("-Xcc");
commandline.push_back(clangDepDetails.mappedPCMPath);
commandline.push_back(pcmPath);
commandline.push_back("-Xcc");
commandline.push_back(clangDepDetails.moduleCacheKey);
}
Expand Down
10 changes: 5 additions & 5 deletions lib/Frontend/CompilerInvocation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -788,17 +788,17 @@ static bool ParseCASArgs(CASOptions &Opts, ArgList &Args,
Opts.EnableCachingRemarks |= Args.hasArg(OPT_cache_remarks);
Opts.CacheSkipReplay |= Args.hasArg(OPT_cache_disable_replay);
if (const Arg *A = Args.getLastArg(OPT_cas_path))
Opts.CASOpts.CASPath = A->getValue();
else if (Opts.CASOpts.CASPath.empty())
Opts.CASOpts.CASPath = llvm::cas::getDefaultOnDiskCASPath();
Opts.Config.CASPath = A->getValue();
else if (Opts.Config.CASPath.empty())
Opts.Config.CASPath = llvm::cas::getDefaultOnDiskCASPath();

if (const Arg *A = Args.getLastArg(OPT_cas_plugin_path))
Opts.CASOpts.PluginPath = A->getValue();
Opts.Config.PluginPath = A->getValue();

for (StringRef Opt : Args.getAllArgValues(OPT_cas_plugin_option)) {
StringRef Name, Value;
std::tie(Name, Value) = Opt.split('=');
Opts.CASOpts.PluginOptions.emplace_back(std::string(Name),
Opts.Config.PluginOptions.emplace_back(std::string(Name),
std::string(Value));
}

Expand Down
2 changes: 1 addition & 1 deletion lib/Frontend/Frontend.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -465,7 +465,7 @@ bool CompilerInstance::setupCASIfNeeded(ArrayRef<const char *> Args) {
return false;

const auto &Opts = getInvocation().getCASOptions();
auto MaybeDB = Opts.CASOpts.getOrCreateDatabases();
auto MaybeDB = Opts.Config.createDatabases();
if (!MaybeDB) {
Diagnostics.diagnose(SourceLoc(), diag::error_cas_initialization,
toString(MaybeDB.takeError()));
Expand Down
2 changes: 1 addition & 1 deletion lib/Frontend/ModuleInterfaceLoader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1805,7 +1805,7 @@ void InterfaceSubContextDelegateImpl::inheritOptionsForBuildingInterface(

if (casOpts.EnableCaching) {
genericSubInvocation.getCASOptions().EnableCaching = casOpts.EnableCaching;
genericSubInvocation.getCASOptions().CASOpts = casOpts.CASOpts;
genericSubInvocation.getCASOptions().Config = casOpts.Config;
genericSubInvocation.getCASOptions().HasImmutableFileSystem =
casOpts.HasImmutableFileSystem;
casOpts.enumerateCASConfigurationFlags(
Expand Down
4 changes: 2 additions & 2 deletions lib/FrontendTool/FrontendTool.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1440,8 +1440,8 @@ static bool generateReproducer(CompilerInstance &Instance,
llvm::sys::path::append(casPath, "cas");
clang::CASOptions newCAS;
newCAS.CASPath = casPath.str();
newCAS.PluginPath = casOpts.CASOpts.PluginPath;
newCAS.PluginOptions = casOpts.CASOpts.PluginOptions;
newCAS.PluginPath = casOpts.Config.PluginPath;
newCAS.PluginOptions = casOpts.Config.PluginOptions;
auto db = newCAS.getOrCreateDatabases();
if (!db) {
diags.diagnose(SourceLoc(), diag::error_cas_initialization,
Expand Down
18 changes: 9 additions & 9 deletions lib/IRGen/IRGenDebugInfo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -913,10 +913,10 @@ class IRGenDebugInfoImpl : public IRGenDebugInfo {
// Note: The implementation here assumes that all clang submodules
// belong to the same PCM file.
ASTSourceDescriptor ParentDescriptor(*ClangModule->Parent);
Parent = getOrCreateModule({ParentDescriptor.getModuleName(),
ParentDescriptor.getPath(),
Desc.getASTFile(), Desc.getSignature()},
ClangModule->Parent);
Parent = getOrCreateModule(
{ParentDescriptor.getModuleName(), ParentDescriptor.getPath(),
Desc.getASTFile(), Desc.getSignature(), /*CASID=*/""},
ClangModule->Parent);
}
return getOrCreateModule(ClangModule, Parent, Desc.getModuleName(),
IncludePath, Signature, Desc.getASTFile());
Expand Down Expand Up @@ -2565,11 +2565,11 @@ class IRGenDebugInfoImpl : public IRGenDebugInfo {
// Describe the submodule, but substitute the cached ASTFile from
// the toplevel module. The ASTFile pointer in SubModule may be
// dangling and cant be trusted.
Scope = getOrCreateModule({SubModuleDesc->getModuleName(),
SubModuleDesc->getPath(),
TopLevelModuleDesc->getASTFile(),
TopLevelModuleDesc->getSignature()},
SubModuleDesc->getModuleOrNull());
Scope = getOrCreateModule(
{SubModuleDesc->getModuleName(), SubModuleDesc->getPath(),
TopLevelModuleDesc->getASTFile(),
TopLevelModuleDesc->getSignature(), /*CASID=*/""},
SubModuleDesc->getModuleOrNull());
else if (SubModuleDesc->getModuleOrNull() == nullptr)
// This is (bridging header) PCH.
Scope = getOrCreateModule(*SubModuleDesc, nullptr);
Expand Down
4 changes: 2 additions & 2 deletions test/CAS/bridging-header.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,13 @@
// CHECK: "-dwarf-ext-refs"
// CHECK: "-fmodule-file-cache-key",
// CHECK-NEXT: "-Xcc",
// CHECK-NEXT: "{{.*}}{{/|\\}}A-{{.*}}.pcm",
// CHECK-NEXT: "A-{{.*}}.pcm",
// CHECK-NEXT: "-Xcc",
// CHECK-NEXT: "llvmcas://{{.*}}",
// CHECK-NEXT: "-Xcc",
// CHECK-NEXT: "-fmodule-file-cache-key",
// CHECK-NEXT: "-Xcc",
// CHECK-NEXT: "{{.*}}{{/|\\}}B-{{.*}}.pcm",
// CHECK-NEXT: "B-{{.*}}.pcm",
// CHECK-NEXT: "-Xcc",
// CHECK-NEXT: "llvmcas://{{.*}}"

Expand Down
41 changes: 41 additions & 0 deletions test/CAS/debug_info_pcm.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
// RUN: %empty-directory(%t)
// RUN: split-file %s %t

// RUN: %target-swift-frontend -scan-dependencies -module-name Test -module-cache-path %t/clang-module-cache -O \
// RUN: -disable-implicit-string-processing-module-import -disable-implicit-concurrency-module-import \
// RUN: %t/test.swift -o %t/deps.json -cache-compile-job -cas-path %t/cas -I %t/include

// RUN: %{python} %S/Inputs/BuildCommandExtractor.py %t/deps.json clang:SwiftShims > %t/shim.cmd
// RUN: %swift_frontend_plain @%t/shim.cmd
// RUN: %{python} %S/Inputs/BuildCommandExtractor.py %t/deps.json clang:B > %t/B.cmd
// RUN: %swift_frontend_plain @%t/B.cmd
// RUN: %{python} %S/Inputs/BuildCommandExtractor.py %t/deps.json clang:A > %t/A.cmd
// RUN: %swift_frontend_plain @%t/A.cmd
// RUN: %{python} %S/Inputs/SwiftDepsExtractor.py %t/deps.json clang:A modulePath > %t/A.path

// RUN: dwarfdump --debug-info @%t/A.path | %FileCheck %s

// CHECK: DW_AT_GNU_dwo_name ("llvmcas://{{.*}}")

//--- test.swift
import A

//--- include/a.h
#include "b.h"
struct A {
int a;
};

//--- include/b.h
void b(void);

//--- include/module.modulemap
module A {
header "a.h"
export *
}

module B {
header "b.h"
export *
}
4 changes: 2 additions & 2 deletions test/CAS/module_path_remap.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,15 @@
// DEPS-FS: /^src/test/CAS/module_path_remap.swift

// RUN: %{python} %S/Inputs/SwiftDepsExtractor.py %t/deps.json deps bridgingHeader | %FileCheck %s -check-prefix DEPS-BRIDGING
// DEPS-BRIDGING: -fmodule-file=F=/^tmp/clang-module-cache/F-{{.*}}.pcm
// DEPS-BRIDGING: -fmodule-file=F=F-{{.*}}.pcm

// RUN: %{python} %S/Inputs/SwiftDepsExtractor.py %t/deps.json F casFSRootID > %t/F.fs.casid
// RUN: %cache-tool -cas-path %t/cas -cache-tool-action print-include-tree-list @%t/F.fs.casid | %FileCheck %s -check-prefix F-FS
// F-FS: /^src/test/CAS/../ScanDependencies/Inputs/Swift/F.swiftinterface

// RUN: %{python} %S/Inputs/SwiftDepsExtractor.py %t/deps.json F commandLine | %FileCheck %s -check-prefix F-CMD
// F-CMD: /^src/test/CAS/../ScanDependencies/Inputs/Swift/F.swiftinterface
// F-CMD: -fmodule-file=SwiftShims=/^tmp/clang-module-cache/SwiftShims-{{.*}}.pcm
// F-CMD: -fmodule-file=SwiftShims=SwiftShims-{{.*}}.pcm

// RUN: %{python} %S/Inputs/SwiftDepsExtractor.py %t/deps.json clang:F clangIncludeTree > %t/tree.casid
// RUN: clang-cas-test --cas %t/cas --print-include-tree @%t/tree.casid | %FileCheck %s -check-prefix TREE
Expand Down