Skip to content

Commit 32c57f2

Browse files
committed
Frontend: allow specifying a different target triple for internal clang instance to use
Before this change, we always use the Swift target triple to instantiate the internal Clang instance. When loading a Swift module from the textual interface, we may pick up a lower target triple to use to build the Swift module because the target is hard-coded in the textual interface file. This implies we may end up building multiple versions of the same Clang module, one for each target triple of the loading Swift module. This change adds a new frontend flag -clang-target to allow clients to specify a consistent clang target to use across the Swift module boundaries. This value won't change because it's not part of .swiftinterface files. swift-driver should pass down -clang-target for each frontend invocation, and its value should be identical to -target. Related to: rdar://72480261
1 parent bed3849 commit 32c57f2

File tree

7 files changed

+70
-1
lines changed

7 files changed

+70
-1
lines changed

include/swift/Basic/LangOptions.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,16 @@ namespace swift {
9090
/// performed.
9191
llvm::Optional<llvm::Triple> TargetVariant;
9292

93+
/// The target triple to instantiate the internal clang instance.
94+
/// When not specified, the compiler will use the value of -target to
95+
/// instantiate the clang instance.
96+
/// This is mainly used to avoid lowering the target triple to use for clang when
97+
/// importing a .swiftinterface whose -target value may be different from
98+
/// the loading module.
99+
/// The lowering triple may result in multiple versions of the same Clang
100+
/// modules being built.
101+
llvm::Optional<llvm::Triple> ClangTarget;
102+
93103
/// The SDK version, if known.
94104
Optional<llvm::VersionTuple> SDKVersion;
95105

include/swift/Option/Options.td

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1066,6 +1066,10 @@ def target_variant : Separate<["-"], "target-variant">,
10661066
HelpText<"Generate 'zippered' code for macCatalyst that can run on the specified"
10671067
" variant target triple in addition to the main -target triple">;
10681068

1069+
def clang_target : Separate<["-"], "clang-target">,
1070+
Flags<[FrontendOption, SwiftAPIExtractOption, SwiftSymbolGraphExtractOption, SwiftAPIDigesterOption]>,
1071+
HelpText<"Separately set the target we should use for internal Clang instance">;
1072+
10691073
def profile_generate : Flag<["-"], "profile-generate">,
10701074
Flags<[FrontendOption, NoInteractiveOption]>,
10711075
HelpText<"Generate instrumented code to collect execution counts">;

lib/ClangImporter/ClangImporter.cpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -729,7 +729,11 @@ importer::addCommonInvocationArguments(
729729
std::vector<std::string> &invocationArgStrs,
730730
ASTContext &ctx) {
731731
using ImporterImpl = ClangImporter::Implementation;
732-
const llvm::Triple &triple = ctx.LangOpts.Target;
732+
llvm::Triple triple = ctx.LangOpts.Target;
733+
// Use clang specific target triple if given.
734+
if (ctx.LangOpts.ClangTarget.hasValue()) {
735+
triple = ctx.LangOpts.ClangTarget.getValue();
736+
}
733737
SearchPathOptions &searchPathOpts = ctx.SearchPathOpts;
734738
const ClangImporterOptions &importerOpts = ctx.ClangImporterOpts;
735739

lib/Frontend/CompilerInvocation.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -640,6 +640,20 @@ static bool ParseLangArgs(LangOptions &Opts, ArgList &Args,
640640
Opts.TargetVariant = llvm::Triple(A->getValue());
641641
}
642642

643+
// Collect -clang-target value if specified in the front-end invocation.
644+
// Usually, the driver will pass down a clang target with the
645+
// exactly same value as the main target, so we could dignose the usage of
646+
// unavailable APIs.
647+
// The reason we cannot infer clang target from -target is that not all
648+
// front-end invocation will include a -target to start with. For instance,
649+
// when compiling a Swift module from a textual interface, -target isn't
650+
// necessary because the textual interface hardcoded the proper target triple
651+
// to use. Inferring -clang-target there will always give us the default
652+
// target triple.
653+
if (const Arg *A = Args.getLastArg(OPT_clang_target)) {
654+
Opts.ClangTarget = llvm::Triple(A->getValue());
655+
}
656+
643657
Opts.EnableCXXInterop |= Args.hasArg(OPT_enable_cxx_interop);
644658
Opts.EnableObjCInterop =
645659
Args.hasFlag(OPT_enable_objc_interop, OPT_disable_objc_interop,

lib/Frontend/ModuleInterfaceLoader.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1132,6 +1132,17 @@ void InterfaceSubContextDelegateImpl::inheritOptionsForBuildingInterface(
11321132
GenericArgs.push_back(triple);
11331133
}
11341134

1135+
if (LangOpts.ClangTarget.hasValue()) {
1136+
genericSubInvocation.getLangOptions().ClangTarget = LangOpts.ClangTarget;
1137+
auto triple = ArgSaver.save(genericSubInvocation.getLangOptions()
1138+
.ClangTarget->getTriple());
1139+
assert(!triple.empty());
1140+
// In explicit module build, all PCMs will be built using the given clang target.
1141+
// So the Swift interface should know that as well to load these PCMs properly.
1142+
GenericArgs.push_back("-clang-target");
1143+
GenericArgs.push_back(triple);
1144+
}
1145+
11351146
// Inherit the Swift language version
11361147
genericSubInvocation.getLangOptions().EffectiveLanguageVersion =
11371148
LangOpts.EffectiveLanguageVersion;
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
// swift-interface-format-version: 1.0
2+
// swift-module-flags: -module-name XWithTarget -target x86_64-apple-macosx10.9
3+
import Swift
4+
@_exported import X
5+
public func overlayFuncX() { }
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// REQUIRES: VENDOR=apple
2+
// RUN: %empty-directory(%t.module-cache)
3+
// RUN: %target-swift-frontend -emit-module -o %t.foo.swiftmodule -module-cache-path %t.module-cache -I %S/Inputs/CHeaders -I %S/Inputs/Swift %s -target x86_64-apple-macosx10.14
4+
5+
// Without -clang-target, we build two X.pcm
6+
// RUN: find %t.module-cache -name "X-*.pcm" | count 2
7+
8+
// RUN: %empty-directory(%t.module-cache)
9+
// RUN: %target-swift-frontend -emit-module -o %t.foo.swiftmodule -module-cache-path %t.module-cache -I %S/Inputs/CHeaders -I %S/Inputs/Swift %s -target x86_64-apple-macosx10.14 -clang-target x86_64-apple-macosx10.14
10+
11+
// With -clang-target, we build one X.pcm
12+
// RUN: find %t.module-cache -name "X-*.pcm" | count 1
13+
14+
// RUN: %target-swift-frontend -scan-dependencies -module-cache-path %t.module-cache %s -o %t.deps.json -I %S/Inputs/CHeaders -I %S/Inputs/Swift -target x86_64-apple-macosx10.14 -clang-target x86_64-apple-macosx10.14
15+
// RUN: %FileCheck %s < %t.deps.json
16+
17+
// CHECK: "-clang-target"
18+
// CHECK-NEXT: "x86_64-apple-macosx10.14"
19+
20+
import X
21+
import XWithTarget

0 commit comments

Comments
 (0)