Skip to content

🍒6.2: Grabbing missing changes on main for FreeBSD support #83500

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 13 commits into
base: release/6.2
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
2 changes: 2 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -1076,6 +1076,8 @@ elseif(CMAKE_SYSTEM_NAME STREQUAL "Darwin")
set(SWIFT_USE_LINKER_default "")
elseif(DISTRO_NAME STREQUAL "Amazon Linux 2023")
set(SWIFT_USE_LINKER_default "lld")
elseif(CMAKE_SYSTEM_NAME STREQUAL "FreeBSD")
set(SWIFT_USE_LINKER_default "lld")
else()
get_gold_version(gold_version)
if(NOT gold_version)
Expand Down
1 change: 0 additions & 1 deletion cmake/modules/SwiftCXXUtils.cmake
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
# Platforms that use libstdc++ as the system-wide default C++ standard library.
set(SWIFT_LIBSTDCXX_PLATFORMS
"LINUX"
"FREEBSD"
"CYGWIN"
"HAIKU")
9 changes: 8 additions & 1 deletion include/swift/AST/AutoDiff.h
Original file line number Diff line number Diff line change
Expand Up @@ -422,7 +422,14 @@ class DerivativeFunctionTypeError
Kind kind;

/// The type and index of a differentiability parameter or result.
using TypeAndIndex = std::pair<Type, unsigned>;
/// std::pair does not have a trivial copy constructor on all platforms for
/// ABI reasons. We must define our own.
struct TypeAndIndex {
Type first;
unsigned second;

TypeAndIndex(Type type, unsigned index) : first(type), second(index) {}
};

private:
union Value {
Expand Down
1 change: 1 addition & 0 deletions include/swift/AST/PlatformKinds.def
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ AVAILABILITY_PLATFORM(visionOSApplicationExtension, "application extensions for
AVAILABILITY_PLATFORM(macOSApplicationExtension, "application extensions for macOS")
AVAILABILITY_PLATFORM(macCatalyst, "Mac Catalyst")
AVAILABILITY_PLATFORM(macCatalystApplicationExtension, "application extensions for Mac Catalyst")
AVAILABILITY_PLATFORM(FreeBSD, "FreeBSD")
AVAILABILITY_PLATFORM(OpenBSD, "OpenBSD")
AVAILABILITY_PLATFORM(Windows, "Windows")

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,14 @@ struct DifferentiationInvoker {

/// The parent `apply` instruction and the witness associated with the
/// `IndirectDifferentiation` case.
std::pair<ApplyInst *, SILDifferentiabilityWitness *>
indirectDifferentiation;
/// std::pair is not trivially copyable on all supported platforms.
/// This struct works around that limitation.
struct IndirectDifferentiation {
ApplyInst *applyInst;
SILDifferentiabilityWitness *witness;
};
IndirectDifferentiation indirectDifferentiation;

Value(ApplyInst *applyInst, SILDifferentiabilityWitness *witness)
: indirectDifferentiation({applyInst, witness}) {}

Expand Down Expand Up @@ -111,7 +117,8 @@ struct DifferentiationInvoker {
std::pair<ApplyInst *, SILDifferentiabilityWitness *>
getIndirectDifferentiation() const {
assert(kind == Kind::IndirectDifferentiation);
return value.indirectDifferentiation;
return std::make_pair(value.indirectDifferentiation.applyInst,
value.indirectDifferentiation.witness);
}

SILDifferentiabilityWitness *getSILDifferentiabilityWitnessInvoker() const {
Expand Down
3 changes: 2 additions & 1 deletion lib/AST/ModuleDependencies.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -600,7 +600,8 @@ void swift::dependencies::registerCxxInteropLibraries(
return mainModuleName == Name;
})) {
// Only link with CxxStdlib on platforms where the overlay is available.
if (Target.isOSDarwin() || Target.isOSLinux() || Target.isOSWindows())
if (Target.isOSDarwin() || Target.isOSLinux() || Target.isOSWindows() ||
Target.isOSFreeBSD())
RegistrationCallback(LinkLibrary{"swiftCxxStdlib", LibraryKind::Library,
hasStaticCxxStdlib});
}
Expand Down
6 changes: 6 additions & 0 deletions lib/AST/PlatformKind.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@ swift::basePlatformForExtensionPlatform(PlatformKind Platform) {
case PlatformKind::tvOS:
case PlatformKind::watchOS:
case PlatformKind::visionOS:
case PlatformKind::FreeBSD:
case PlatformKind::OpenBSD:
case PlatformKind::Windows:
case PlatformKind::none:
Expand Down Expand Up @@ -158,6 +159,8 @@ static bool isPlatformActiveForTarget(PlatformKind Platform,
case PlatformKind::visionOS:
case PlatformKind::visionOSApplicationExtension:
return Target.isXROS();
case PlatformKind::FreeBSD:
return Target.isOSFreeBSD();
case PlatformKind::OpenBSD:
return Target.isOSOpenBSD();
case PlatformKind::Windows:
Expand Down Expand Up @@ -283,6 +286,8 @@ swift::tripleOSTypeForPlatform(PlatformKind platform) {
case PlatformKind::visionOS:
case PlatformKind::visionOSApplicationExtension:
return llvm::Triple::XROS;
case PlatformKind::FreeBSD:
return llvm::Triple::FreeBSD;
case PlatformKind::OpenBSD:
return llvm::Triple::OpenBSD;
case PlatformKind::Windows:
Expand Down Expand Up @@ -319,6 +324,7 @@ bool swift::isPlatformSPI(PlatformKind Platform) {
case PlatformKind::watchOSApplicationExtension:
case PlatformKind::visionOS:
case PlatformKind::visionOSApplicationExtension:
case PlatformKind::FreeBSD:
case PlatformKind::OpenBSD:
case PlatformKind::Windows:
case PlatformKind::none:
Expand Down
7 changes: 4 additions & 3 deletions lib/AST/Type.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4817,7 +4817,8 @@ AnyFunctionType::getAutoDiffDerivativeFunctionLinearMapType(
if (!resultTan)
return llvm::make_error<DerivativeFunctionTypeError>(
this, DerivativeFunctionTypeError::Kind::NonDifferentiableResult,
std::make_pair(originalResultType, unsigned(originalResult.index)));
DerivativeFunctionTypeError::TypeAndIndex(
originalResultType, unsigned(originalResult.index)));

if (!originalResult.isSemanticResultParameter)
resultTanTypes.push_back(resultTan->getType());
Expand Down Expand Up @@ -4847,7 +4848,7 @@ AnyFunctionType::getAutoDiffDerivativeFunctionLinearMapType(
this,
DerivativeFunctionTypeError::Kind::
NonDifferentiableDifferentiabilityParameter,
std::make_pair(paramType, i));
DerivativeFunctionTypeError::TypeAndIndex(paramType, i));

differentialParams.push_back(AnyFunctionType::Param(
paramTan->getType(), Identifier(), diffParam.getParameterFlags()));
Expand Down Expand Up @@ -4895,7 +4896,7 @@ AnyFunctionType::getAutoDiffDerivativeFunctionLinearMapType(
this,
DerivativeFunctionTypeError::Kind::
NonDifferentiableDifferentiabilityParameter,
std::make_pair(paramType, i));
DerivativeFunctionTypeError::TypeAndIndex(paramType, i));

if (diffParam.isAutoDiffSemanticResult()) {
if (paramType->isVoid())
Expand Down
11 changes: 10 additions & 1 deletion lib/ClangImporter/ClangImporter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2557,7 +2557,9 @@ PlatformAvailability::PlatformAvailability(const LangOptions &langOpts)
case PlatformKind::visionOS:
case PlatformKind::visionOSApplicationExtension:
break;

case PlatformKind::FreeBSD:
deprecatedAsUnavailableMessage = "";
break;
case PlatformKind::OpenBSD:
deprecatedAsUnavailableMessage = "";
break;
Expand Down Expand Up @@ -2605,6 +2607,9 @@ bool PlatformAvailability::isPlatformRelevant(StringRef name) const {
return name == "xros" || name == "xros_app_extension" ||
name == "visionos" || name == "visionos_app_extension";

case PlatformKind::FreeBSD:
return name == "freebsd";

case PlatformKind::OpenBSD:
return name == "openbsd";

Expand Down Expand Up @@ -2676,6 +2681,10 @@ bool PlatformAvailability::treatDeprecatedAsUnavailable(
// No deprecation filter on xrOS
return false;

case PlatformKind::FreeBSD;
// No deprecation filter on OpenBSD
return false;

case PlatformKind::OpenBSD:
// No deprecation filter on OpenBSD
return false;
Expand Down
7 changes: 4 additions & 3 deletions lib/Driver/ToolChains.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1656,9 +1656,10 @@ const char *ToolChain::getClangLinkerDriver(
// a C++ standard library if it's not needed, in particular because the
// standard library that `clang++` selects by default may not be the one that
// is desired.
const char *LinkerDriver =
Args.hasArg(options::OPT_enable_experimental_cxx_interop) ? "clang++"
: "clang";
bool useCxxLinker = Args.hasArg(options::OPT_enable_experimental_cxx_interop);
if (Arg *arg = Args.getLastArg(options::OPT_cxx_interoperability_mode))
useCxxLinker |= StringRef(arg->getValue()) != "off";
const char *LinkerDriver = useCxxLinker ? "clang++" : "clang";
if (const Arg *A = Args.getLastArg(options::OPT_tools_directory)) {
StringRef toolchainPath(A->getValue());

Expand Down
7 changes: 3 additions & 4 deletions lib/Driver/UnixToolChains.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -334,10 +334,9 @@ toolchains::GenericUnix::constructInvocation(const DynamicLinkJobAction &job,
SmallString<128> LibProfile(SharedResourceDirPath);
llvm::sys::path::remove_filename(LibProfile); // remove platform name
llvm::sys::path::append(LibProfile, "clang", "lib");

llvm::sys::path::append(LibProfile, getTriple().getOSName(),
Twine("libclang_rt.profile-") +
getTriple().getArchName() + ".a");
llvm::sys::path::append(
LibProfile, getUnversionedTriple(getTriple()).getOSName(),
Twine("libclang_rt.profile-") + getTriple().getArchName() + ".a");
Arguments.push_back(context.Args.MakeArgString(LibProfile));
Arguments.push_back(context.Args.MakeArgString(
Twine("-u", llvm::getInstrProfRuntimeHookVarName())));
Expand Down
3 changes: 2 additions & 1 deletion lib/Frontend/CompilerInvocation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -390,7 +390,8 @@ void CompilerInvocation::computeCXXStdlibOptions() {
// (see https://reviews.llvm.org/D101479).
LangOpts.CXXStdlib = CXXStdlibKind::Msvcprt;
LangOpts.PlatformDefaultCXXStdlib = CXXStdlibKind::Msvcprt;
} else if (LangOpts.Target.isOSLinux() || LangOpts.Target.isOSDarwin()) {
} else if (LangOpts.Target.isOSLinux() || LangOpts.Target.isOSDarwin() ||
LangOpts.Target.isOSFreeBSD()) {
auto [clangDriver, clangDiagEngine] =
ClangImporter::createClangDriver(LangOpts, ClangImporterOpts);
auto clangDriverArgs = ClangImporter::createClangArgs(
Expand Down
2 changes: 2 additions & 0 deletions lib/IRGen/TBDGen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,8 @@ getLinkerPlatformId(OriginallyDefinedInAttr::ActiveVersion Ver,
switch(Ver.Platform) {
case swift::PlatformKind::none:
llvm_unreachable("cannot find platform kind");
case swift::PlatformKind::FreeBSD:
llvm_unreachable("not used for this platform");
case swift::PlatformKind::OpenBSD:
llvm_unreachable("not used for this platform");
case swift::PlatformKind::Windows:
Expand Down
2 changes: 1 addition & 1 deletion lib/Option/SanitizerOptions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ OptionSet<SanitizerKind> swift::parseSanitizerArgValues(
}

// Check that we're one of the known supported targets for sanitizers.
if (!(Triple.isOSDarwin() || Triple.isOSLinux() || Triple.isOSWindows())) {
if (!(Triple.isOSDarwin() || Triple.isOSLinux() || Triple.isOSWindows() || Triple.isOSFreeBSD())) {
SmallString<128> b;
Diags.diagnose(SourceLoc(), diag::error_unsupported_opt_for_target,
(A->getOption().getPrefixedName() +
Expand Down
3 changes: 3 additions & 0 deletions lib/PrintAsClang/DeclAndTypePrinter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1795,6 +1795,9 @@ class DeclAndTypePrinter::Implementation
case PlatformKind::visionOSApplicationExtension:
plat = "visionos_app_extension";
break;
case PlatformKind::FreeBSD:
plat = "freebsd";
break;
case PlatformKind::OpenBSD:
plat = "openbsd";
break;
Expand Down
2 changes: 2 additions & 0 deletions lib/SymbolGraphGen/AvailabilityMixin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@ StringRef getDomain(const SemanticAvailableAttr &AvAttr) {
return { "watchOSAppExtension" };
case swift::PlatformKind::visionOSApplicationExtension:
return { "visionOSAppExtension" };
case swift::PlatformKind::FreeBSD:
return { "FreeBSD" };
case swift::PlatformKind::OpenBSD:
return { "OpenBSD" };
case swift::PlatformKind::Windows:
Expand Down
2 changes: 1 addition & 1 deletion stdlib/public/Cxx/std/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ add_swift_target_library(swiftCxxStdlib STATIC NO_LINK_NAME IS_STDLIB IS_SWIFT_O
DEPLOYMENT_VERSION_XROS ${COMPATIBILITY_MINIMUM_DEPLOYMENT_VERSION_XROS}

LINK_FLAGS "${SWIFT_RUNTIME_SWIFT_LINK_FLAGS}"
TARGET_SDKS ALL_APPLE_PLATFORMS LINUX LINUX_STATIC WINDOWS ANDROID
TARGET_SDKS ALL_APPLE_PLATFORMS LINUX LINUX_STATIC WINDOWS ANDROID FREEBSD
MACCATALYST_BUILD_FLAVOR zippered
INSTALL_IN_COMPONENT compiler
INSTALL_BINARY_SWIFTMODULE NON_DARWIN_ONLY
Expand Down
10 changes: 8 additions & 2 deletions stdlib/public/Platform/Platform.swift
Original file line number Diff line number Diff line change
Expand Up @@ -338,12 +338,12 @@ public var SIG_DFL: sig_t? { return nil }
public var SIG_IGN: sig_t { return unsafeBitCast(1, to: sig_t.self) }
public var SIG_ERR: sig_t { return unsafeBitCast(-1, to: sig_t.self) }
public var SIG_HOLD: sig_t { return unsafeBitCast(5, to: sig_t.self) }
#elseif os(OpenBSD)
#elseif os(OpenBSD) || os(FreeBSD)
public var SIG_DFL: sig_t? { return nil }
public var SIG_IGN: sig_t { return unsafeBitCast(1, to: sig_t.self) }
public var SIG_ERR: sig_t { return unsafeBitCast(-1, to: sig_t.self) }
public var SIG_HOLD: sig_t { return unsafeBitCast(3, to: sig_t.self) }
#elseif os(Linux) || os(FreeBSD) || os(PS4) || os(Android) || os(Haiku)
#elseif os(Linux) || os(PS4) || os(Android) || os(Haiku)
#if !canImport(SwiftMusl)
public typealias sighandler_t = __sighandler_t
#endif
Expand Down Expand Up @@ -495,3 +495,9 @@ public var environ: UnsafeMutablePointer<UnsafeMutablePointer<CChar>?> {
}
#endif
#endif // SWIFT_STDLIB_HAS_ENVIRON

#if os(FreeBSD)
@inlineable public func inet_pton(_ af: CInt, _ src: UnsafePointer<CChar>!, _ dst: UnsafeMutablePointer!) -> CInt {
__inet_pton(af, src, dst)
}
#endif
3 changes: 3 additions & 0 deletions stdlib/public/Platform/SwiftGlibc.h.gyb
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ headers = [
'nl_types.h',
'poll.h',
'pthread.h',
'pthread_np.h',
'pwd.h',
'regex.h',
'sched.h',
Expand All @@ -65,6 +66,7 @@ headers = [
'spawn.h',
'strings.h',
'sys/event.h',
'sys/extattr.h',
'sys/file.h',
'sys/inotify.h',
'sys/ioctl.h',
Expand All @@ -84,6 +86,7 @@ headers = [
'sys/times.h',
'sys/types.h',
'sys/uio.h',
'sys/umtx.h',
'sys/un.h',
'sys/user.h',
'sys/utsname.h',
Expand Down
2 changes: 1 addition & 1 deletion stdlib/public/Platform/glibc.modulemap.gyb
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
/// It's not named just Glibc so that it doesn't conflict in the event of a
/// future official glibc modulemap.
module SwiftGlibc [system] {
% if CMAKE_SDK in ["LINUX", "OPENBSD"]:
% if CMAKE_SDK in ["LINUX", "FREEBSD", "OPENBSD"]:
link "m"
% end
% if CMAKE_SDK in ["LINUX", "FREEBSD", "OPENBSD", "CYGWIN"]:
Expand Down
2 changes: 1 addition & 1 deletion stdlib/public/SwiftShims/swift/shims/SwiftStdint.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@

// Clang has been defining __INTxx_TYPE__ macros for a long time.
// __UINTxx_TYPE__ are defined only since Clang 3.5.
#if !defined(__APPLE__) && !defined(__linux__) && !defined(__OpenBSD__) && !defined(__wasi__)
#if !defined(__APPLE__) && !defined(__linux__) && !defined(__OpenBSD__) && !defined(__FreeBSD__) && !defined(__wasi__)
#include <stdint.h>
typedef int64_t __swift_int64_t;
typedef uint64_t __swift_uint64_t;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,4 +66,9 @@ static inline __swift_uint32_t _swift_stdlib_futex_unlock(__swift_uint32_t *lock

#endif // defined(__linux__)

#if defined(__FreeBSD__)
#include <sys/types.h>
#include <sys/umtx.h>
#endif

#endif // SWIFT_STDLIB_SYNCHRONIZATION_SHIMS_H
10 changes: 10 additions & 0 deletions stdlib/public/Synchronization/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,12 @@ set(SWIFT_SYNCHRONIZATION_LINUX_SOURCES
Mutex/SpinLoopHint.swift
)

# FreeBSD sources
set(SWIFT_SYNCRHONIZATION_FREEBSD_SOURCES
Mutex/FreeBSDImpl.swift
Mutex/Mutex.swift
)

# Wasm sources

set(SWIFT_SYNCHRONIZATION_WASM_SOURCES
Expand Down Expand Up @@ -112,6 +118,8 @@ add_swift_target_library(swiftSynchronization ${SWIFT_STDLIB_LIBRARY_BUILD_TYPES
${SWIFT_SYNCHRONIZATION_WASM_SOURCES}
SWIFT_SOURCES_DEPENDS_WINDOWS
${SWIFT_SYNCHRONIZATION_WINDOWS_SOURCES}
SWFIT_SOURCES_DEPENDS_FREEBSD
${SWIFT_SYNCRHONIZATION_FREEBSD_SOURCES}
SWIFT_SOURCES_DEPENDS_FREESTANDING
Mutex/MutexUnavailable.swift

Expand All @@ -135,6 +143,8 @@ add_swift_target_library(swiftSynchronization ${SWIFT_STDLIB_LIBRARY_BUILD_TYPES
Android
SWIFT_MODULE_DEPENDS_WINDOWS
WinSDK
SWIFT_MODULE_DEPENDS_FREEBSD
Glibc

SWIFT_COMPILE_FLAGS
${SWIFT_SYNCHRNOIZATION_SWIFT_FLAGS}
Expand Down
Loading