68 Commits

Author SHA1 Message Date
Chirag Ramani
5179bc9609 Add IRPGO and CSIRPGO options to Swift (#84335)
This PR introduces three new instrumentation flags and plumbs them
through to IRGen:

1. `-ir-profile-generate` - enable IR-level instrumentation.
2. `-cs-profile-generate` - enable context-sensitive IR-level
instrumentation.
3. `-ir-profile-use` - IR-level PGO input profdata file to enable
profile-guided optimization (both IRPGO and CSIRPGO)

**Context:**
https://forums.swift.org/t/ir-level-pgo-instrumentation-in-swift/82123

**Swift-driver PR:** https://github.com/swiftlang/swift-driver/pull/1992

**Tests and validation:**
This PR includes ir level verification tests, also checks few edge-cases
when `-ir-profile-use` supplied profile is either missing or is an
invalid IR profile.

However, for argument validation, linking, and generating IR profiles
that can later be consumed by -cs-profile-generate, I’ll need
corresponding swift-driver changes. Those changes are being tracked in
https://github.com/swiftlang/swift-driver/pull/1992
2025-10-09 17:41:47 -07:00
Evan Wilde
dbe7c13160 Unversioned clang resource OS name
The clang resource directory generally does not use a versioned form of
the platform name. This aligns the old driver behavior with the new
driver for FreeBSD support.

a7f32bd005/Sources/SwiftDriver/Jobs/GenericUnixToolchain%2BLinkerSupport.swift (L309)
2025-06-25 14:26:05 -07:00
Evan Wilde
96ea03f255 Merge pull request #80479 from etcwilde/ewilde/clang-linkers-choice
[Driver] Don't hardcode default linker on Linux
2025-04-03 17:32:33 -07:00
Evan Wilde
500ddfeba4 [Driver] Don't hardcode default linker on Linux
This is the C++ driver counterpart to a change that landed in the Swift
driver a while ago to use the clang-linker to determine what the default
linker is. This is to avoid hard-coding gold, which is deprecated and
not available on some newer Linux distributions. The challenge is that
these newer Linux distributions don't already have Swift so we have to
use the old C++ driver implementation.
2025-04-02 16:32:44 -07:00
3405691582
a341ce5570 Add a build flavor to opt-out of BTCFI on OpenBSD.
To work-around #80059, we need to stop return address signing and
opt-out of BTCFI enforcement via enabling a platform linker option.

We don't want to completely undo the BTCFI work in the rare case that
we later figure out how to properly address the above issue, or allow
users who might want to benefit from BTCFI enforcement and won't use
Concurrency. To do this, condition the existing BTCFI flag enforcement
into a configuration option that defaults to off for now.

Because the new swift-driver needs to "know" whether the frontend is
configured to opt-out or not, and since the new driver communicates with
the frontend via the target info JSON to begin with, we add a field
that emits the build flavor to signal the right behavior.
2025-03-29 10:47:23 -04:00
Saleem Abdulrasool
ef39af0f55 Driver: support -nostartfiles in the C++ driver
This is required to bootstrap the `-static-stdlib` support for Windows.
With this, we are able to properly build the Swift SDK both dynamically
and statically, which is needed to enable us to make further progress
towards an early swift-driver.
2025-02-07 17:40:26 -08:00
Egor Zhdan
2df1b6fee4 [cxx-interop] Remove -experimental-cxx-stdlib flag
This flag was added back in 2020, but it didn't function properly, since a lot of other code in the compiler assumed the platform-default C++ stdlib until recently (https://github.com/swiftlang/swift/pull/75589).

The recommended way to use a non-default C++ stdlib in Swift now is to pass `-Xcc -stdlib=xyz` argument to the compiler.

This change removes the `-experimental-cxx-stdlib` flag.
2024-09-05 15:19:19 +01:00
Daniel Rodríguez Troitiño
5280cea889 [Macros] In-process plugin server library tied to compiler host, not target (#74785)
PR #73725 introduced the in-process plugin server library, but the
selection of the library depends on the selected toolchain, which
depends on the compiler target, not the host. When cross-compiling (for
example from macOS to a embedded Unix target), the compiler will
incorrectly chose the `.so` file, not find it, and fail to compile
things like the `@debugDescription` macro.

Move the in-process plugin server library code from the platform
toolchains into the parent type, and code it so it uses the right name
depending on the compiler host at compilation time. This discards the
target and only relies on the compiler host for selecting the right
library.
2024-07-01 08:50:54 -07:00
Rintaro Ishizaki
5ebd715a2a Merge pull request #73725 from rintaro/macros-inproc-plugin
[Macros] In-process plugin server
2024-06-26 06:33:38 -07:00
Rintaro Ishizaki
2f7aa428db [Macros] In-process plugin server
Separate swift-syntax libs for the compiler and for the library plugins.
Compiler communicates with library plugins using serialized messages
just like executable plugins.

* `lib/swift/host/compiler/lib_Compiler*.dylib`(`lib/CompilerSwiftSyntax`):
  swift-syntax libraries for compiler. Library evolution is disabled.
* Compiler (`ASTGen` and `swiftIDEUtilsBridging`) only depends on
  `lib/swift/host/compiler` libraries.
* `SwiftInProcPluginServer`: In-process plugin server shared library.
  This has one `swift_inproc_plugins_handle_message` entry point that
  receives a message and return the response.
* In the compiler
  * Add `-in-process-plugin-server-path` front-end option, which specifies
    the `SwiftInProcPluginServer` shared library path.
  * Remove `LoadedLibraryPlugin`, because all library plugins are managed
    by `SwiftInProcPluginServer`
  * Introduce abstract `CompilerPlugin` class that has 2 subclasses:
    * `LoadedExecutablePlugin` existing class that represents an
      executable plugin
    * `InProcessPlugins` wraps `dlopen`ed `SwiftInProcPluginServer`
  * Unified the code path in `TypeCheckMacros.cpp` and `ASTGen`, the
    difference between executable plugins and library plugins are now
    abstracted by `CompilerPlugin`
2024-06-17 11:36:52 -07:00
Tim Kientzle
1d961ba22d Add #include "swift/Basic/Assertions.h" to a lot of source files
Although I don't plan to bring over new assertions wholesale
into the current qualification branch, it's entirely possible
that various minor changes in main will use the new assertions;
having this basic support in the release branch will simplify that.
(This is why I'm adding the includes as a separate pass from
rewriting the individual assertions)
2024-06-05 19:37:30 -07:00
Alastair Houghton
a25cd37750 Add support to the compiler for musl and the LINUX_STATIC SDK.
This is really just about setting appropriate defaults (such as
making sure that the static Linux triple causes us to use lld).

rdar://123506306
2024-05-07 16:48:52 +01:00
Alastair Houghton
3c029ebd3f [IRGen] Use llvm.used on ELF instead of llvm.compiler.used.
We changed to `llvm.compiler.used` because of the behaviour of `gold`,
which refuses to coalesce sections that have different `SHF_GNU_RETAIN`
flags, which causes problems with metadata.

Originally I thought we were going to have to generate two sections
with distinct names and have the runtime look for both of them, but
it turns out that the runtime only wants to see sections that have
`SHF_GNU_RETAIN` in any case.  It's really the reflection code that
is interested in being able to see non-retained sections.  The upshot
is that we don't need to use `llvm.compiler.used`; it's just fine if
we have duplicate sections, as long as the reflection code looks for
them when it's inspecting an ELF image.

This also means we no longer need to pass `-z nostart-stop-gc` to the
linker if we're using `lld`.

rdar://123504095
2024-04-29 10:48:22 +01:00
futurejones
03a364eb62 set default linker to lld for Amazon Linux 2023 2024-03-05 11:19:50 +09:00
Finagolfin
bf137cb30d [android] Add more changes to build the compiler 2023-11-06 21:53:56 +05:30
Adrian Prantl
5d978b44ca Revert "Revert "Make the DWARF version emitted by the Swift compiler configurable."" 2023-10-26 12:29:18 -07:00
Adrian Prantl
2a32ca5944 Revert "Make the DWARF version emitted by the Swift compiler configurable." 2023-10-25 13:28:28 -07:00
Adrian Prantl
d19fe381fa Use LLVM definitions for Swift section names (NFC) 2023-10-24 10:58:40 -07:00
Saleem Abdulrasool
fcd7b9e533 Driver: virtualise the plugin path handling
The plugin layouts are different across platforms.  Move this into a
virtual method and allow replacement.  On Windows, the plugins are
placed into the `bin` directory as the DLLs should always be co-located
to ensure that the proper DLLs are found (there is no concept of RPATH).
2023-09-04 15:21:39 -07:00
Matt Jacobson
028fcd09cf driver: on FreeBSD, use lld, not gold
This is equivalent to passing `-use-ld=lld` to the driver.
2022-11-01 21:27:49 -04:00
buttaface
cc0e122125 [Driver] Work around lld 13+ issue with --gc-sections for ELF by adding -z nostart-stop-gc (#60544)
Fixes #60406 

Ran the linux x86_64 compiler validation suite on this pull with `-use-ld=lld -Xlinker --gc-sections`, then without stripping as in the linked issue, and only saw the 20 unrelated failures just like @drodriguez had.
2022-08-19 10:24:05 -07:00
swift-ci
85a2e5c42b Merge remote-tracking branch 'origin/main' into rebranch 2021-11-17 21:13:46 -08:00
buttaface
30c292ca87 [android] Update to NDK 23b (#39921)
The latest Long Term Support NDK finally removed binutils, including the bfd/gold
linkers and libgcc. This simplifies our Android support, including making lld the
default linker for Android. Disable three reflection tests that now fail, likely
related to issues with swift-reflection-dump and switching to lld.
2021-11-17 20:58:42 -08:00
Ben Barham
c676dfc2fa [rebranch] Update uses of ErrorHandling.h functions
llvm-project `ErrorHandling.h` was updated to remove std::string. This
added a new `report_fatal_error` overload taking a `const Twine &`,
removed the overload that took `const std::string &`, and updated
`fatal_error_handler_t` to use `const char *` rather than `const
std::string &`.

Fix uses of these functions to take into account these updates. Note
that without the `const std::string &` overload, passing a `std::string`
into `report_fatal_error` now results in an ambiguous match between the
`StringRef` and `Twine` overloads so we need to be explicit about one or
the other.
2021-11-13 15:33:09 +10:00
Saleem Abdulrasool
15a5d2c54b Revert "[android] Update to NDK 23" 2021-10-16 11:07:23 -07:00
Butta
7fa1b4b2ac [android] Update to NDK 23
The latest Long Term Support NDK finally removed binutils, including the bfd/gold
linkers and libgcc. This simplifies our Android support, including making lld the
default linker for Android. Disable three reflection tests that now fail, likely
related to issues with swift-reflection-dump and switching to lld.

Also, add the libatomic dependency for Android armv7, just as on linux.
2021-10-12 12:37:01 +05:30
Rajagopalan-Gangadharan
48de17d140 Address review comments for reusable function 2021-08-11 10:08:59 +05:30
Rajagopalan-Gangadharan
90637aa8ee Take care of OPT_linker_option_Group to produce desired behaviour
The -l args values are handled by OPT_l and OPT_linker_option_Group,
to do this have created a helper method under Toolchain
2021-08-07 16:23:08 +05:30
Robert Widmann
0149ccd0ca Add arm64_32 support for Swift
Commit the platform definition and build script work necessary to
cross-compile for arm64_32.

arm64_32 is a variant of AARCH64 that supports an ILP32 architecture.
2021-04-20 14:59:04 -07:00
Dario Rexin
2083e1bba1 [Driver] Use library search group when linking statically (#36356) 2021-03-15 12:10:39 -07:00
Butta
f5e1429bf8 [Driver] Remove redundant clang '-target' flag that wasn't being used
The target was added for Unix toolchains in #901, but a later pull #1891 added
it again. Since clang only uses the last target flag that's passed in, all
customization done for the first one was unused these last 4+ years, so remove
it and change tests that look for custom strings passed by the first one.
2020-12-09 16:35:21 +05:30
Michael Gottesman
f196c16532 Merge pull request #32430 from kateinoigakukun/katei/llvm-lto-driver
[LTO] Support LLVM LTO for driver
2020-09-18 14:49:07 -05:00
Yuta Saito
d6cddaabb5 [LTO] Support LLVM LTO for driver
This commit adds LTO support for handling linker options and LLVM BC
emission. Even for ELF, swift-autolink-extract is unnecessary because
linker options are embeded in LLVM BC content when LTO.
2020-07-31 10:17:59 +09:00
martinboehme
40104ba8b1 Link against the C++ standard library when C++ interop is enabled (#30914)
This doesn't yet allow including C++ headers on platforms where libc++
isn't the default; see comments in UnixToolChains.cpp for details.

However, it does, for example, allow throwing and catching exceptions in C++
code used through interop, unblocking
https://github.com/apple/swift/pull/30674/files.

The flags (-enable-experimental-cxx-interop and -experimental-cxx-stdlib) carry
"experimental" in the name to emphasize that C++ interop is still an
experimental feature.

Co-authored-by: Michael Forster <forster@google.com>
2020-07-09 15:59:26 +02:00
Butta
e6fe8d47c2 Driver: update old doc comments from when clang++ was the linker driver 2020-06-24 18:38:03 +05:30
Saleem Abdulrasool
71309a8fa9 Revert "[LTO] Support LLVM level link time optimization on Darwin, Linux and Windows" 2020-06-07 11:25:48 -07:00
Yuta Saito
915c4a6997 [LTO] Support LLVM level link time optimization
This commit adds -lto flag for driver to enable LTO at LLVM level.
When -lto=llvm given, compiler emits LLVM bitcode file instead of object
file and perform thin LTO using libLTO.dylib plugin.
When -lto=llvm-full given, perform full LTO instead of thin LTO.
2020-05-31 08:12:52 +09:00
Dario Rexin
e4e6c16319 Fix linker flags for -static-executable
Also adds a test to ensure it does not break in the future
2020-04-08 13:25:27 -07:00
Saleem Abdulrasool
c7a198bc03 Driver: honour -no-toolchain-stdlib-rpath on Unices
Handle `-no-toolchain-stdlib-rpath` on Linux/android as well as Darwin.  This
enables the flags on other Unix platforms as it can be useful to control the
embedded rpath for the library.
2020-03-17 17:38:24 +00:00
3405691582
0d3b92dfb1 Swift Basic/Driver recognizes OpenBSD.
Add the platform conditional and set up other basics for the toolchain.

The ConditionalCompilation tests are updated to match, since otherwise
they seem to trip when building on non-OpenBSD platforms. The
Driver/linker test is updated to ensure lld is passed on this platform.
Note that OpenBSD calls "x86_64" as "amd64", so we use that name for the
architecture instead of trying to alias one to the other, as this makes
things simpler.
2020-02-27 17:14:22 -05:00
Butta
14cc620016 [android] A few tweaks for native compilation and to get more tests working
Now that CMAKE_HOST_SYSTEM_NAME and CMAKE_SYSTEM_NAME are set by default to
Android in the Termux app, make the needed tweaks. Some tests were adapted
to work natively on Android too, adds sys/cdefs.h to the Bionic modulemap,
and includes the start of native Android platform support in the build-script.
2019-12-07 01:01:59 +05:30
Saleem Abdulrasool
551a2a58a5 Driver: normalise linker triple on all android targets
Currently we only support building for android armv7, arm64, x86,
x86_64.  In the future, if support for MIPS and MIPS64 is added, we
should normalise those as well.  This is needed to support compilation
against modern NDKs.
2019-08-04 19:48:57 -07:00
Saleem Abdulrasool
67475dced4 Driver: avoid linking against the C++ runtime
Use `clang` rather than `clang++` as the linker driver.  This ensures
that we do not force a C++ runtime on the general code.  This is fine
for now as C++ interop is not yet available for Swift.  This prevents
the accidental mix-and-match of various C++ runtimes.  This can cause
problems on platforms like android where `libstdc++` is an unsupported
runtime but is generally the default for Linux platforms.
2019-07-24 19:48:02 -07:00
Saleem Abdulrasool
987c1fd657 Driver: pass -target immediately after clang++ (NFC)
This just adjusts the command invocation for the driver.  It makes it
clear that the `clang` invocation was correct.
2019-07-21 17:12:26 -07:00
Brent Royal-Gordon
1e81ced48d Revert "Revert "Look up runtime libraries in SDK"" 2019-07-03 14:18:48 -07:00
Xi Ge
fdd41aeb68 Revert "Look up runtime libraries in SDK (#25740)"
This reverts commit b818b441f2.
2019-06-25 17:48:41 -07:00
Brent Royal-Gordon
b818b441f2 Look up runtime libraries in SDK (#25740)
In #23175, we started looking in the SDK for swiftmodules, but we want to look for the dylibs there too. Fixes <rdar://problem/52059706>.
2019-06-25 15:39:03 -07:00
Thomas Roughton
e5ea42d774 Driver: renamic LinkJob to DynamicLinkJob and ArchiveJob to StaticLinkJob
# Conflicts:
#	lib/Driver/DarwinToolChains.cpp
2019-06-17 12:49:17 +12:00
Thomas Roughton
b34a1cbbc6 Driver: add -static flag for generating static archives 2019-06-17 12:49:03 +12:00
Julian Lettner
1cac0bedf1 [Driver] TSan runtime now has a link-time dependency on libdispatch
Previously, the TSan runtime only required libdispatch on Darwin, which
required no explicit linker flags, because libdispatch is always
provided by the system (libSystem).

Now, the TSan runtime also has a link-time dependency on libdispatch on
Linux, where libdispatch (and the blocks runtime) is just another
library. We therefore need to specify them as additonal linker options.
2019-03-28 17:53:49 -07:00