Skip to content

[Explicit Module Builds] Ensure IRGen uses Swift compiler's target triple when '-clang-target' is set. #65930

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

Merged
merged 1 commit into from
May 16, 2023

Conversation

artemcm
Copy link
Contributor

@artemcm artemcm commented May 15, 2023

The Clang importer's Clang instance may be configured with a different (higher) OS version than the compilation target itself in order to be able to load pre-compiled Clang modules that are aligned with the broader SDK, and match the SDK deployment target against which Swift modules are also built. In this case, we must use the Swift compiler's OS version triple in order to generate the binary as-requested.

This change makes ClangImporter's Implementation keep track of a distinct TargetInfo and CodeGenOpts containers that are meant to be used by clients in IRGen. When -clang-target is not set, they are defined to be copies of the ClangImporter's built-in module-loading Clang instance. When -clang-target is set, they are configured with the Swift compilation's target triple and OS version (but otherwise identical) instead. To distinguish IRGen clients from module loading clients, getModuleAvailabilityTarget is added for module loading clients of ClangImporter.

The notion of using a different triple for loading Clang modules arises for the following reason:

  • Swift is able to load Swift modules built against a different target triple than the source module that is being compiled. Swift relies on availability annotations on the API within the loaded modules to ensure that compilation for the current target only uses appropriately-available API from its dependencies.
  • Clang, in contrast, requires that compilation only ever load modules (.pcm) that are precisely aligned to the current source compilation. Because the target triple (OS version, specifically) between Swift source compilation and Swift dependency module compilation may differ, this would otherwise result in builtin multiple copies of the same Clang module, against different OS versions, once for each different triple in the build graph. Instead, with Explicitly-Built Modules, Swift sets a -clang-target argument that ensures that all Clang modules participating in the build are built against the SDK deployment target, matching the Swift modules in the SDK, which allows them to expose a maximally-available API surface as required by potentially-depending Swift modules' target OS version.

For example:
Suppose we are building a source module Foo, targeting macosx10.0, using an SDK with a deployment target of macosx12.0. Swift modules in said SDK will be built for macosx12.0 (as hard-coded in their textual interfaces), meaning they may reference symbols expected to be present in dependency Clang modules at that target OS version.

Suppose the source module Foo depends on Swift module Bar, which then depends on Clang module Baz. 'Bar' must be built targeting macosx12.0 (SDK-matching deployment target is hard-coded into its textual interface). Which means that Bar expects Baz to expose symbols that may only be available when targeting at least macosx12.0. e.g. Baz may have symbols guarded with __MAC_OS_X_VERSION_MIN_REQUIRED >= __MAC_12_0. For this reason, we use -clang-target to ensure Baz is built targeting macosx12.0, and can be loaded by both Foo and Bar.

As a result, we cannot direclty use the Clang instance's target triple here and must check if we need to instead use the triple of the Swift compiler instance.

Resolves rdar://109228963

@artemcm artemcm force-pushed the SeparateClangTargetHandlingForIRGen branch from e6fb338 to 9a4ad05 Compare May 15, 2023 22:41
@artemcm artemcm requested a review from aschwaighofer May 15, 2023 22:41
@artemcm
Copy link
Contributor Author

artemcm commented May 15, 2023

@swift-ci test

@artemcm
Copy link
Contributor Author

artemcm commented May 15, 2023

This replaces my original attempt here:
#65872
Which was incomplete and I was concerned would cause subtle and hard-to-track mistakes.

@artemcm
Copy link
Contributor Author

artemcm commented May 15, 2023

Notably, this is largely a non-functional change when -clang-target is not set, which is the case for all implicit module builds today.

@artemcm artemcm marked this pull request as ready for review May 16, 2023 15:17
Copy link
Contributor

@aschwaighofer aschwaighofer left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm

…iple when '-clang-target' is set.

The Clang importer's Clang instance may be configured with a different (higher)
OS version than the compilation target itself in order to be able to load
pre-compiled Clang modules that are aligned with the broader SDK, and match the
SDK deployment target against which Swift modules are also built. In this case,
we must use the Swift compiler's OS version triple in order to generate the
binary as-requested.

This change makes 'ClangImporter' 'Implementation' keep track of a distinct
'TargetInfo' and 'CodeGenOpts' containers that are meant to be used by clients
in IRGen. When '-clang-target' is not set, they are defined to be copies of the
'ClangImporter's built-in module-loading Clang instance. When '-clang-target' is
set, they are configured with the Swift compilation's target triple and OS
version (but otherwise identical) instead. To distinguish IRGen clients from
module loading clients, 'getModuleAvailabilityTarget' is added for module
loading clients of 'ClangImporter'.

The notion of using a different triple for loading Clang modules arises for the
following reason:
- Swift is able to load Swift modules built against a different target triple
  than the source module that is being compiled. Swift relies on availability
  annotations on the API within the loaded modules to ensure that compilation
  for the current target only uses appropriately-available API from its
  dependencies.
- Clang, in contrast, requires that compilation only ever load modules (.pcm)
  that are precisely aligned to the current source compilation. Because the
  target triple (OS version in particular) between Swift source compilation and
  Swift dependency module compilation may differ, this would otherwise result in
  builtin multiple copies of the same Clang module, against different OS
  versions, once for each different triple in the build graph.
Instead, with Explicitly-Built Modules, Swift sets a '-clang-target' argument
that ensures that all Clang modules participating in the build are built against
the SDK deployment target, matching the Swift modules in the SDK, which allows
them to expose a maximally-available API surface as required by
potentially-depending Swift modules' target OS version.
--------------------------------------------
For example:
Suppose we are building a source module 'Foo', targeting 'macosx10.0', using an
SDK with a deployment target of 'macosx12.0'. Swift modules in said SDK will be
built for 'macosx12.0' (as hard-coded in their textual interfaces), meaning they
may reference symbols expected to be present in dependency Clang modules at that
target OS version.

Suppose the source module 'Foo' depends on Swift module 'Bar', which then
depends on Clang module `Baz`. 'Bar' must be built targeting 'macosx12.0'
(SDK-matching deployment target is hard-coded into its textual interface). Which
means that 'Bar' expects 'Baz' to expose symbols that may only be available when
targeting at least 'macosx12.0'. e.g. 'Baz' may have symbols guarded with
'__MAC_OS_X_VERSION_MIN_REQUIRED >= __MAC_12_0'. For this reason, we use
'-clang-target' to ensure 'Baz' is built targeting 'macosx12.0', and can be
loaded by both 'Foo' and 'Bar'.

As a result, we cannot direclty use the Clang instance's target triple here and
must check if we need to instead use the triple of the Swift compiler instance.

Resolves rdar://109228963
@artemcm artemcm force-pushed the SeparateClangTargetHandlingForIRGen branch from 9a4ad05 to f2cf55a Compare May 16, 2023 16:42
@artemcm
Copy link
Contributor Author

artemcm commented May 16, 2023

@swift-ci smoke test

@artemcm artemcm merged commit 99f545a into swiftlang:main May 16, 2023
@artemcm artemcm deleted the SeparateClangTargetHandlingForIRGen branch May 16, 2023 22:29
artemcm added a commit to artemcm/swift that referenced this pull request Dec 20, 2023
…sing Swift compilation Target Triple.

As per swiftlang#65930, the Clang importer's Clang instance may be configured with a different (higher) OS version than the compilation target itself in order to be able to load pre-compiled Clang modules that are aligned with the broader SDK, and match the SDK deployment target against which Swift modules are also built. Code-generation, however, must use the actual compilation target triple. This matches how Swift itself loads Swift module dependencies as well: dependency '.swiftinterface' files are type-checked against the availability epoch and code-generated against the actual compilation triple.

Resolves rdar://113712186
artemcm added a commit to swiftlang/llvm-project that referenced this pull request Dec 20, 2023
… instance which may differ from the one in the ASTContext

As per swiftlang/swift#65930, Swift compiler's built-in Clang instance may require to perform type-checking against one OS version and compilation/code-generation against an earlier version. This change allows Swift to configure it's built-in Clang code-generator with a custom 'TargetInfo'.

Part of rdar://113712186
artemcm added a commit to artemcm/swift that referenced this pull request Dec 20, 2023
…sing Swift compilation Target Triple.

As per swiftlang#65930, the Clang importer's Clang instance may be configured with a different (higher) OS version than the compilation target itself in order to be able to load pre-compiled Clang modules that are aligned with the broader SDK, and match the SDK deployment target against which Swift modules are also built. Code-generation, however, must use the actual compilation target triple. This matches how Swift itself loads Swift module dependencies as well: dependency '.swiftinterface' files are type-checked against the availability epoch and code-generated against the actual compilation triple.

Resolves rdar://113712186
artemcm added a commit to artemcm/swift that referenced this pull request Mar 5, 2024
…sing Swift compilation Target Triple.

As per swiftlang#65930, the Clang importer's Clang instance may be configured with a different (higher) OS version than the compilation target itself in order to be able to load pre-compiled Clang modules that are aligned with the broader SDK, and match the SDK deployment target against which Swift modules are also built. Code-generation, however, must use the actual compilation target triple. This matches how Swift itself loads Swift module dependencies as well: dependency '.swiftinterface' files are type-checked against the availability epoch and code-generated against the actual compilation triple.

Resolves rdar://113712186
artemcm added a commit to artemcm/swift that referenced this pull request Mar 6, 2024
…sing Swift compilation Target Triple.

As per swiftlang#65930, the Clang importer's Clang instance may be configured with a different (higher) OS version than the compilation target itself in order to be able to load pre-compiled Clang modules that are aligned with the broader SDK, and match the SDK deployment target against which Swift modules are also built. Code-generation, however, must use the actual compilation target triple. This matches how Swift itself loads Swift module dependencies as well: dependency '.swiftinterface' files are type-checked against the availability epoch and code-generated against the actual compilation triple.

Resolves rdar://113712186
artemcm added a commit to artemcm/llvm-project that referenced this pull request Apr 16, 2024
…etInfo instance which may differ from the one in the ASTContext

As per swiftlang/swift#65930, Swift compiler's built-in Clang instance may require to perform type-checking against one OS version and compilation/code-generation against an earlier version. This change allows Swift to configure it's built-in Clang code-generator with a custom 'TargetInfo'.
artemcm added a commit to artemcm/llvm-project that referenced this pull request Apr 16, 2024
…etInfo instance which may differ from the one in the ASTContext

As per swiftlang/swift#65930, Swift compiler's built-in Clang instance may require to perform type-checking against one OS version and compilation/code-generation against an earlier version. This change allows Swift to configure it's built-in Clang code-generator with a custom 'TargetInfo'.
artemcm added a commit to swiftlang/llvm-project that referenced this pull request Oct 21, 2024
… instance which may differ from the one in the ASTContext

As per swiftlang/swift#65930, Swift compiler's built-in Clang instance may require to perform type-checking against one OS version and compilation/code-generation against an earlier version. This change allows Swift to configure it's built-in Clang code-generator with a custom 'TargetInfo'.

Part of rdar://113712186
artemcm added a commit to artemcm/swift that referenced this pull request Oct 21, 2024
…sing Swift compilation Target Triple.

As per swiftlang#65930, the Clang importer's Clang instance may be configured with a different (higher) OS version than the compilation target itself in order to be able to load pre-compiled Clang modules that are aligned with the broader SDK, and match the SDK deployment target against which Swift modules are also built. Code-generation, however, must use the actual compilation target triple. This matches how Swift itself loads Swift module dependencies as well: dependency '.swiftinterface' files are type-checked against the availability epoch and code-generated against the actual compilation triple.

Resolves rdar://113712186
artemcm added a commit to swiftlang/llvm-project that referenced this pull request Oct 24, 2024
… instance which may differ from the one in the ASTContext

As per swiftlang/swift#65930, Swift compiler's built-in Clang instance may require to perform type-checking against one OS version and compilation/code-generation against an earlier version. This change allows Swift to configure it's built-in Clang code-generator with a custom 'TargetInfo'.

Part of rdar://113712186
artemcm added a commit to swiftlang/llvm-project that referenced this pull request Oct 24, 2024
… instance which may differ from the one in the ASTContext

As per swiftlang/swift#65930, Swift compiler's built-in Clang instance may require to perform type-checking against one OS version and compilation/code-generation against an earlier version. This change allows Swift to configure it's built-in Clang code-generator with a custom 'TargetInfo'.

Part of rdar://113712186
bnbarham pushed a commit to swiftlang/llvm-project that referenced this pull request Apr 23, 2025
… instance which may differ from the one in the ASTContext

As per swiftlang/swift#65930, Swift compiler's built-in Clang instance may require to perform type-checking against one OS version and compilation/code-generation against an earlier version. This change allows Swift to configure it's built-in Clang code-generator with a custom 'TargetInfo'.

Part of rdar://113712186

(cherry picked from commit 9894e7a)
bnbarham pushed a commit to swiftlang/llvm-project that referenced this pull request Apr 23, 2025
… instance which may differ from the one in the ASTContext

As per swiftlang/swift#65930, Swift compiler's built-in Clang instance may require to perform type-checking against one OS version and compilation/code-generation against an earlier version. This change allows Swift to configure it's built-in Clang code-generator with a custom 'TargetInfo'.

Part of rdar://113712186

(cherry picked from commit 9894e7a)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants