[swiftinterface] Handle target variants the same as targets (#77156)

Based on preliminary work from @rmaz.

The compilation arguments for a swiftinterface file are preprocessed to
modify the `-target` argument to match the preferred target (which comes
from the command line) in cases in which the sub-architecture differs,
but it is compatible (for example using `arm64e` when `arm64` is being
compiled), but this was not done for the target variant, which ended up
with mismatches on the sub-architecture used by the target and target
variant, which fails an assert in assert toolchains.
This commit is contained in:
Daniel Rodríguez Troitiño
2024-10-22 16:34:19 -07:00
committed by GitHub
parent bc30ecc438
commit 4e2fbe17c3
3 changed files with 45 additions and 22 deletions

View File

@@ -454,7 +454,7 @@ public:
: ctx(ctx),
fs(*ctx.SourceMgr.getFileSystem()),
requiresOSSAModules(requiresOSSAModules) {}
// Check if all the provided file dependencies are up-to-date compared to
// what's currently on disk.
bool dependenciesAreUpToDate(StringRef modulePath,
@@ -524,7 +524,7 @@ public:
moduleBuffer = std::move(*OutBuf);
return serializedASTBufferIsUpToDate(modulePath, *moduleBuffer, rebuildInfo, AllDeps);
}
enum class DependencyStatus {
UpToDate,
OutOfDate,
@@ -1513,7 +1513,8 @@ bool ModuleInterfaceLoader::buildSwiftModuleFromSwiftInterface(
static bool readSwiftInterfaceVersionAndArgs(
SourceManager &SM, DiagnosticEngine &Diags, llvm::StringSaver &ArgSaver,
SwiftInterfaceInfo &interfaceInfo, StringRef interfacePath,
SourceLoc diagnosticLoc, llvm::Triple preferredTarget) {
SourceLoc diagnosticLoc, llvm::Triple preferredTarget,
std::optional<llvm::Triple> preferredTargetVariant) {
llvm::vfs::FileSystem &fs = *SM.getFileSystem();
auto FileOrError = swift::vfs::getFileOrSTDIN(fs, interfacePath);
if (!FileOrError) {
@@ -1537,7 +1538,8 @@ static bool readSwiftInterfaceVersionAndArgs(
if (extractCompilerFlagsFromInterface(interfacePath, SB, ArgSaver,
interfaceInfo.Arguments,
preferredTarget)) {
preferredTarget,
preferredTargetVariant)) {
InterfaceSubContextDelegateImpl::diagnose(
interfacePath, diagnosticLoc, SM, &Diags,
diag::error_extracting_version_from_module_interface);
@@ -1592,7 +1594,7 @@ bool ModuleInterfaceLoader::buildExplicitSwiftModuleFromSwiftInterface(
StringRef outputPath, bool ShouldSerializeDeps,
ArrayRef<std::string> CompiledCandidates,
DependencyTracker *tracker) {
if (!Instance.getInvocation().getIRGenOptions().AlwaysCompile) {
// First, check if the expected output already exists and possibly
// up-to-date w.r.t. all of the dependencies it was built with. If so, early
@@ -1613,7 +1615,7 @@ bool ModuleInterfaceLoader::buildExplicitSwiftModuleFromSwiftInterface(
return false;
}
}
// Read out the compiler version.
llvm::BumpPtrAllocator alloc;
llvm::StringSaver ArgSaver(alloc);
@@ -1621,7 +1623,8 @@ bool ModuleInterfaceLoader::buildExplicitSwiftModuleFromSwiftInterface(
readSwiftInterfaceVersionAndArgs(
Instance.getSourceMgr(), Instance.getDiags(), ArgSaver, InterfaceInfo,
interfacePath, SourceLoc(),
Instance.getInvocation().getLangOptions().Target);
Instance.getInvocation().getLangOptions().Target,
Instance.getInvocation().getLangOptions().TargetVariant);
auto Builder = ExplicitModuleInterfaceBuilder(
Instance, &Instance.getDiags(), Instance.getSourceMgr(),
@@ -1670,6 +1673,15 @@ void InterfaceSubContextDelegateImpl::inheritOptionsForBuildingInterface(
GenericArgs.push_back(triple);
}
if (LangOpts.TargetVariant.has_value()) {
genericSubInvocation.getLangOptions().TargetVariant = LangOpts.TargetVariant;
auto variantTriple = ArgSaver.save(genericSubInvocation.getLangOptions().TargetVariant->str());
if (!variantTriple.empty()) {
GenericArgs.push_back("-target-variant");
GenericArgs.push_back(variantTriple);
}
}
// Inherit the target SDK name and version
if (!LangOpts.SDKName.empty()) {
genericSubInvocation.getLangOptions().SDKName = LangOpts.SDKName;
@@ -1835,7 +1847,8 @@ bool InterfaceSubContextDelegateImpl::extractSwiftInterfaceVersionAndArgs(
StringRef interfacePath, SourceLoc diagnosticLoc) {
if (readSwiftInterfaceVersionAndArgs(SM, *Diags, ArgSaver, interfaceInfo,
interfacePath, diagnosticLoc,
subInvocation.getLangOptions().Target))
subInvocation.getLangOptions().Target,
subInvocation.getLangOptions().TargetVariant))
return true;
// Prior to Swift 5.9, swiftinterfaces were always built (accidentally) with