Skip to content

Commit cf2f715

Browse files
authored
Merge pull request #77836 from michael-yuji/mchiu/freebsd
[FreeBSD] Adding FreeBSD support
2 parents 7e6b7c8 + bc870ef commit cf2f715

30 files changed

+155
-20
lines changed

CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1071,6 +1071,8 @@ elseif(CMAKE_SYSTEM_NAME STREQUAL "Darwin")
10711071
set(SWIFT_USE_LINKER_default "")
10721072
elseif(DISTRO_NAME STREQUAL "Amazon Linux 2023")
10731073
set(SWIFT_USE_LINKER_default "lld")
1074+
elseif(CMAKE_SYSTEM_NAME STREQUAL "FreeBSD")
1075+
set(SWIFT_USE_LINKER_default "lld")
10741076
else()
10751077
get_gold_version(gold_version)
10761078
if(NOT gold_version)

include/swift/AST/AutoDiff.h

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -422,7 +422,14 @@ class DerivativeFunctionTypeError
422422
Kind kind;
423423

424424
/// The type and index of a differentiability parameter or result.
425-
using TypeAndIndex = std::pair<Type, unsigned>;
425+
/// std::pair does not have a trivial copy constructor on FreeBSD for
426+
/// ABI reasons, so we have to define our own type here instead
427+
struct TypeAndIndex {
428+
Type first;
429+
unsigned second;
430+
431+
TypeAndIndex(Type type, unsigned index) : first(type), second(index) {}
432+
};
426433

427434
private:
428435
union Value {

include/swift/AST/PlatformKinds.def

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ AVAILABILITY_PLATFORM(visionOSApplicationExtension, "application extensions for
3434
AVAILABILITY_PLATFORM(macOSApplicationExtension, "application extensions for macOS")
3535
AVAILABILITY_PLATFORM(macCatalyst, "Mac Catalyst")
3636
AVAILABILITY_PLATFORM(macCatalystApplicationExtension, "application extensions for Mac Catalyst")
37+
AVAILABILITY_PLATFORM(FreeBSD, "FreeBSD")
3738
AVAILABILITY_PLATFORM(OpenBSD, "OpenBSD")
3839
AVAILABILITY_PLATFORM(Windows, "Windows")
3940

include/swift/SILOptimizer/Differentiation/DifferentiationInvoker.h

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -71,10 +71,17 @@ struct DifferentiationInvoker {
7171

7272
/// The parent `apply` instruction and the witness associated with the
7373
/// `IndirectDifferentiation` case.
74-
std::pair<ApplyInst *, SILDifferentiabilityWitness *>
75-
indirectDifferentiation;
74+
/// Note: This used to be a std::pair, but on FreeBSD, libc++ is
75+
/// configured with _LIBCPP_DEPRECATED_ABI_DISABLE_PAIR_TRIVIAL_COPY_CTOR
76+
/// and hence does not have a trivial copy constructor
77+
struct IndirectDifferentiation {
78+
ApplyInst *applyInst;
79+
SILDifferentiabilityWitness *witness;
80+
};
81+
IndirectDifferentiation indirectDifferentiation;
82+
7683
Value(ApplyInst *applyInst, SILDifferentiabilityWitness *witness)
77-
: indirectDifferentiation({applyInst, witness}) {}
84+
: indirectDifferentiation({applyInst, witness}) {}
7885

7986
/// The witness associated with the `SILDifferentiabilityWitnessInvoker`
8087
/// case.
@@ -111,7 +118,8 @@ struct DifferentiationInvoker {
111118
std::pair<ApplyInst *, SILDifferentiabilityWitness *>
112119
getIndirectDifferentiation() const {
113120
assert(kind == Kind::IndirectDifferentiation);
114-
return value.indirectDifferentiation;
121+
return std::make_pair(value.indirectDifferentiation.applyInst,
122+
value.indirectDifferentiation.witness);
115123
}
116124

117125
SILDifferentiabilityWitness *getSILDifferentiabilityWitnessInvoker() const {

lib/AST/PlatformKind.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,7 @@ swift::basePlatformForExtensionPlatform(PlatformKind Platform) {
116116
case PlatformKind::tvOS:
117117
case PlatformKind::watchOS:
118118
case PlatformKind::visionOS:
119+
case PlatformKind::FreeBSD:
119120
case PlatformKind::OpenBSD:
120121
case PlatformKind::Windows:
121122
case PlatformKind::none:
@@ -160,6 +161,8 @@ static bool isPlatformActiveForTarget(PlatformKind Platform,
160161
return Target.isXROS();
161162
case PlatformKind::OpenBSD:
162163
return Target.isOSOpenBSD();
164+
case PlatformKind::FreeBSD:
165+
return Target.isOSFreeBSD();
163166
case PlatformKind::Windows:
164167
return Target.isOSWindows();
165168
case PlatformKind::none:
@@ -283,6 +286,8 @@ swift::tripleOSTypeForPlatform(PlatformKind platform) {
283286
case PlatformKind::visionOS:
284287
case PlatformKind::visionOSApplicationExtension:
285288
return llvm::Triple::XROS;
289+
case PlatformKind::FreeBSD:
290+
return llvm::Triple::FreeBSD;
286291
case PlatformKind::OpenBSD:
287292
return llvm::Triple::OpenBSD;
288293
case PlatformKind::Windows:
@@ -320,6 +325,7 @@ bool swift::isPlatformSPI(PlatformKind Platform) {
320325
case PlatformKind::visionOS:
321326
case PlatformKind::visionOSApplicationExtension:
322327
case PlatformKind::OpenBSD:
328+
case PlatformKind::FreeBSD:
323329
case PlatformKind::Windows:
324330
case PlatformKind::none:
325331
return false;

lib/AST/Type.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4843,7 +4843,8 @@ AnyFunctionType::getAutoDiffDerivativeFunctionLinearMapType(
48434843
if (!resultTan)
48444844
return llvm::make_error<DerivativeFunctionTypeError>(
48454845
this, DerivativeFunctionTypeError::Kind::NonDifferentiableResult,
4846-
std::make_pair(originalResultType, unsigned(originalResult.index)));
4846+
DerivativeFunctionTypeError::TypeAndIndex(
4847+
originalResultType, unsigned(originalResult.index)));
48474848

48484849
if (!originalResult.isSemanticResultParameter)
48494850
resultTanTypes.push_back(resultTan->getType());
@@ -4873,7 +4874,7 @@ AnyFunctionType::getAutoDiffDerivativeFunctionLinearMapType(
48734874
this,
48744875
DerivativeFunctionTypeError::Kind::
48754876
NonDifferentiableDifferentiabilityParameter,
4876-
std::make_pair(paramType, i));
4877+
DerivativeFunctionTypeError::TypeAndIndex(paramType, i));
48774878

48784879
differentialParams.push_back(AnyFunctionType::Param(
48794880
paramTan->getType(), Identifier(), diffParam.getParameterFlags()));
@@ -4921,7 +4922,7 @@ AnyFunctionType::getAutoDiffDerivativeFunctionLinearMapType(
49214922
this,
49224923
DerivativeFunctionTypeError::Kind::
49234924
NonDifferentiableDifferentiabilityParameter,
4924-
std::make_pair(paramType, i));
4925+
DerivativeFunctionTypeError::TypeAndIndex(paramType, i));
49254926

49264927
if (diffParam.isAutoDiffSemanticResult()) {
49274928
if (paramType->isVoid())

lib/ClangImporter/ClangImporter.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2557,6 +2557,10 @@ PlatformAvailability::PlatformAvailability(const LangOptions &langOpts)
25572557
case PlatformKind::visionOSApplicationExtension:
25582558
break;
25592559

2560+
case PlatformKind::FreeBSD:
2561+
deprecatedAsUnavailableMessage = "";
2562+
break;
2563+
25602564
case PlatformKind::OpenBSD:
25612565
deprecatedAsUnavailableMessage = "";
25622566
break;
@@ -2604,6 +2608,9 @@ bool PlatformAvailability::isPlatformRelevant(StringRef name) const {
26042608
return name == "xros" || name == "xros_app_extension" ||
26052609
name == "visionos" || name == "visionos_app_extension";
26062610

2611+
case PlatformKind::FreeBSD:
2612+
return name == "freebsd";
2613+
26072614
case PlatformKind::OpenBSD:
26082615
return name == "openbsd";
26092616

@@ -2675,6 +2682,10 @@ bool PlatformAvailability::treatDeprecatedAsUnavailable(
26752682
// No deprecation filter on xrOS
26762683
return false;
26772684

2685+
case PlatformKind::FreeBSD:
2686+
// No deprecation filter on FreeBSD
2687+
return false;
2688+
26782689
case PlatformKind::OpenBSD:
26792690
// No deprecation filter on OpenBSD
26802691
return false;

lib/IRGen/TBDGen.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -245,6 +245,8 @@ getLinkerPlatformId(OriginallyDefinedInAttr::ActiveVersion Ver,
245245
switch(Ver.Platform) {
246246
case swift::PlatformKind::none:
247247
llvm_unreachable("cannot find platform kind");
248+
case swift::PlatformKind::FreeBSD:
249+
llvm_unreachable("not used for this platform");
248250
case swift::PlatformKind::OpenBSD:
249251
llvm_unreachable("not used for this platform");
250252
case swift::PlatformKind::Windows:

lib/Option/SanitizerOptions.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,7 @@ OptionSet<SanitizerKind> swift::parseSanitizerArgValues(
168168
}
169169

170170
// Check that we're one of the known supported targets for sanitizers.
171-
if (!(Triple.isOSDarwin() || Triple.isOSLinux() || Triple.isOSWindows() || Triple.isOSWASI())) {
171+
if (!(Triple.isOSDarwin() || Triple.isOSLinux() || Triple.isOSWindows() || Triple.isOSWASI() || Triple.isOSFreeBSD())) {
172172
SmallString<128> b;
173173
Diags.diagnose(SourceLoc(), diag::error_unsupported_opt_for_target,
174174
(A->getOption().getPrefixedName() +

lib/PrintAsClang/DeclAndTypePrinter.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1795,6 +1795,9 @@ class DeclAndTypePrinter::Implementation
17951795
case PlatformKind::visionOSApplicationExtension:
17961796
plat = "visionos_app_extension";
17971797
break;
1798+
case PlatformKind::FreeBSD:
1799+
plat = "freebsd";
1800+
break;
17981801
case PlatformKind::OpenBSD:
17991802
plat = "openbsd";
18001803
break;

lib/SymbolGraphGen/AvailabilityMixin.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,8 @@ StringRef getDomain(const SemanticAvailableAttr &AvAttr) {
5454
return { "watchOSAppExtension" };
5555
case swift::PlatformKind::visionOSApplicationExtension:
5656
return { "visionOSAppExtension" };
57+
case swift::PlatformKind::FreeBSD:
58+
return { "FreeBSD" };
5759
case swift::PlatformKind::OpenBSD:
5860
return { "OpenBSD" };
5961
case swift::PlatformKind::Windows:

stdlib/public/Cxx/std/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ add_swift_target_library(swiftCxxStdlib STATIC NO_LINK_NAME IS_STDLIB IS_SWIFT_O
6363
DEPLOYMENT_VERSION_XROS ${COMPATIBILITY_MINIMUM_DEPLOYMENT_VERSION_XROS}
6464

6565
LINK_FLAGS "${SWIFT_RUNTIME_SWIFT_LINK_FLAGS}"
66-
TARGET_SDKS ALL_APPLE_PLATFORMS LINUX LINUX_STATIC WINDOWS ANDROID
66+
TARGET_SDKS ALL_APPLE_PLATFORMS LINUX LINUX_STATIC WINDOWS ANDROID FREEBSD
6767
MACCATALYST_BUILD_FLAVOR zippered
6868
INSTALL_IN_COMPONENT compiler
6969
INSTALL_BINARY_SWIFTMODULE NON_DARWIN_ONLY

stdlib/public/Platform/Platform.swift

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -236,12 +236,12 @@ public func ioctl(
236236
// signal.h
237237
//===----------------------------------------------------------------------===//
238238

239-
#if os(OpenBSD)
239+
#if os(OpenBSD) || os(FreeBSD)
240240
public var SIG_DFL: sig_t? { return nil }
241241
public var SIG_IGN: sig_t { return unsafeBitCast(1, to: sig_t.self) }
242242
public var SIG_ERR: sig_t { return unsafeBitCast(-1, to: sig_t.self) }
243243
public var SIG_HOLD: sig_t { return unsafeBitCast(3, to: sig_t.self) }
244-
#elseif os(Linux) || os(FreeBSD) || os(PS4) || os(Android) || os(Haiku)
244+
#elseif os(Linux) || os(PS4) || os(Android) || os(Haiku)
245245
#if !canImport(SwiftMusl)
246246
public typealias sighandler_t = __sighandler_t
247247
#endif
@@ -384,3 +384,9 @@ public var environ: UnsafeMutablePointer<UnsafeMutablePointer<CChar>?> {
384384
}
385385
#endif
386386
#endif // SWIFT_STDLIB_HAS_ENVIRON
387+
388+
#if os(FreeBSD)
389+
@inlinable public func inet_pton(_ af: CInt, _ src: UnsafePointer<CChar>!, _ dst: UnsafeMutableRawPointer!) -> CInt {
390+
__inet_pton(af, src, dst)
391+
}
392+
#endif

stdlib/public/Platform/SwiftGlibc.h.gyb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ headers = [
5757
'nl_types.h',
5858
'poll.h',
5959
'pthread.h',
60+
'pthread_np.h',
6061
'pwd.h',
6162
'regex.h',
6263
'sched.h',
@@ -65,6 +66,7 @@ headers = [
6566
'spawn.h',
6667
'strings.h',
6768
'sys/event.h',
69+
'sys/extattr.h',
6870
'sys/file.h',
6971
'sys/inotify.h',
7072
'sys/ioctl.h',
@@ -84,6 +86,7 @@ headers = [
8486
'sys/times.h',
8587
'sys/types.h',
8688
'sys/uio.h',
89+
'sys/umtx.h',
8790
'sys/un.h',
8891
'sys/user.h',
8992
'sys/utsname.h',

stdlib/public/Platform/glibc.modulemap.gyb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
/// It's not named just Glibc so that it doesn't conflict in the event of a
2020
/// future official glibc modulemap.
2121
module SwiftGlibc [system] {
22-
% if CMAKE_SDK in ["LINUX", "OPENBSD"]:
22+
% if CMAKE_SDK in ["LINUX", "FREEBSD", "OPENBSD"]:
2323
link "m"
2424
% end
2525
% if CMAKE_SDK in ["LINUX", "FREEBSD", "OPENBSD", "CYGWIN"]:

stdlib/public/SwiftShims/swift/shims/SwiftStdint.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424

2525
// Clang has been defining __INTxx_TYPE__ macros for a long time.
2626
// __UINTxx_TYPE__ are defined only since Clang 3.5.
27-
#if !defined(__APPLE__) && !defined(__linux__) && !defined(__OpenBSD__) && !defined(__wasi__) && !defined(__swift_embedded__)
27+
#if !defined(__APPLE__) && !defined(__linux__) && !defined(__OpenBSD__) && !defined(__FreeBSD__) && !defined(__wasi__) && !defined(__swift_embedded__)
2828
#include <stdint.h>
2929
typedef int64_t __swift_int64_t;
3030
typedef uint64_t __swift_uint64_t;

stdlib/public/SwiftShims/swift/shims/_SynchronizationShims.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,4 +66,9 @@ static inline __swift_uint32_t _swift_stdlib_futex_unlock(__swift_uint32_t *lock
6666

6767
#endif // defined(__linux__)
6868

69+
#if defined(__FreeBSD__)
70+
#include <sys/types.h>
71+
#include <sys/umtx.h>
72+
#endif
73+
6974
#endif // SWIFT_STDLIB_SYNCHRONIZATION_SHIMS_H

stdlib/public/Synchronization/CMakeLists.txt

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,13 @@ set(SWIFT_SYNCHRONIZATION_LINUX_SOURCES
5959
Mutex/SpinLoopHint.swift
6060
)
6161

62+
# FreeBSD sources
63+
64+
set(SWIFT_SYNCHRONIZATION_FREEBSD_SOURCES
65+
Mutex/FreeBSDImpl.swift
66+
Mutex/Mutex.swift
67+
)
68+
6269
# Wasm sources
6370

6471
set(SWIFT_SYNCHRONIZATION_WASM_SOURCES
@@ -106,6 +113,8 @@ add_swift_target_library(swiftSynchronization ${SWIFT_STDLIB_LIBRARY_BUILD_TYPES
106113
${SWIFT_SYNCHRONIZATION_WASM_SOURCES}
107114
SWIFT_SOURCES_DEPENDS_WINDOWS
108115
${SWIFT_SYNCHRONIZATION_WINDOWS_SOURCES}
116+
SWIFT_SOURCES_DEPENDS_FREEBSD
117+
${SWIFT_SYNCHRONIZATION_FREEBSD_SOURCES}
109118
SWIFT_SOURCES_DEPENDS_FREESTANDING
110119
Mutex/MutexUnavailable.swift
111120

@@ -129,6 +138,8 @@ add_swift_target_library(swiftSynchronization ${SWIFT_STDLIB_LIBRARY_BUILD_TYPES
129138
Android
130139
SWIFT_MODULE_DEPENDS_WINDOWS
131140
WinSDK
141+
SWIFT_MODULE_DEPENDS_FREEBSD
142+
Glibc
132143

133144
SWIFT_COMPILE_FLAGS
134145
${SWIFT_SYNCHRNOIZATION_SWIFT_FLAGS}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// This source file is part of the Swift Atomics open source project
4+
//
5+
// Copyright (c) 2024 Apple Inc. and the Swift project authors
6+
// Licensed under Apache License v2.0 with Runtime Library Exception
7+
//
8+
// See https://swift.org/LICENSE.txt for license information
9+
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
10+
//
11+
//===----------------------------------------------------------------------===//
12+
13+
import Glibc
14+
15+
@available(SwiftStdlib 6.0, *)
16+
@frozen
17+
@_staticExclusiveOnly
18+
public struct _MutexHandle: ~Copyable {
19+
@usableFromInline
20+
let value: _Cell<umutex>
21+
22+
@available(SwiftStdlib 6.0, *)
23+
@_alwaysEmitIntoClient
24+
@_transparent
25+
public init() {
26+
value = _Cell(umutex())
27+
}
28+
29+
@available(SwiftStdlib 6.0, *)
30+
@_alwaysEmitIntoClient
31+
@_transparent
32+
internal borrowing func _lock() {
33+
_umtx_op(value._address, UMTX_OP_MUTEX_LOCK, 0, nil, nil)
34+
}
35+
36+
@available(SwiftStdlib 6.0, *)
37+
@_alwaysEmitIntoClient
38+
@_transparent
39+
internal borrowing func _tryLock() -> Bool {
40+
_umtx_op(value._address, UMTX_OP_MUTEX_TRYLOCK, 0, nil, nil) != -1
41+
}
42+
43+
@available(SwiftStdlib 6.0, *)
44+
@_alwaysEmitIntoClient
45+
@_transparent
46+
internal borrowing func _unlock() {
47+
_umtx_op(value._address, UMTX_OP_MUTEX_UNLOCK, 0, nil, nil)
48+
}
49+
}

stdlib/public/core/CTypes.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -114,10 +114,10 @@ public typealias CLongDouble = Double
114114
#error("CLongDouble needs to be defined for this OpenBSD architecture")
115115
#endif
116116
#elseif os(FreeBSD)
117+
// On FreeBSD, long double is Float128 for arm64, which we don't have yet in
118+
// Swift
117119
#if arch(x86_64) || arch(i386)
118120
public typealias CLongDouble = Float80
119-
#else
120-
#error("CLongDouble needs to be defined for this FreeBSD architecture")
121121
#endif
122122
#elseif $Embedded
123123
#if arch(x86_64) || arch(i386)

test/Concurrency/Runtime/async_task_executor_and_serial_executor_both_executor.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,12 @@
88

99
// TODO: Need to find out how to combine %env- and %target-run and %import-libdispatch reliably.
1010
// UNSUPPORTED: OS=linux-gnu
11+
// UNSUPPORTED: OS=freebsd
1112

1213
// REQUIRES: concurrency
1314
// REQUIRES: executable_test
1415
// REQUIRES: libdispatch
15-
//
16+
//
1617
// REQUIRES: concurrency_runtime
1718
// UNSUPPORTED: back_deployment_runtime
1819

test/Concurrency/Runtime/custom_executors_complex_equality_crash.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
// TODO: Need to find out how to combine %env- and %target-run and %import-libdispatch reliably.
1010
// UNSUPPORTED: OS=linux-gnu
11+
// UNSUPPORTED: OS=freebsd
1112

1213
// REQUIRES: concurrency
1314
// REQUIRES: executable_test

0 commit comments

Comments
 (0)