Skip to content

Commit 284c5a8

Browse files
committed
FreeBSD: Add FreeBSD Support
Cherry-picking commits in cf2f715
1 parent 2c69f02 commit 284c5a8

30 files changed

+142
-20
lines changed

CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1076,6 +1076,8 @@ elseif(CMAKE_SYSTEM_NAME STREQUAL "Darwin")
10761076
set(SWIFT_USE_LINKER_default "")
10771077
elseif(DISTRO_NAME STREQUAL "Amazon Linux 2023")
10781078
set(SWIFT_USE_LINKER_default "lld")
1079+
elseif(CMAKE_SYSTEM_NAME STREQUAL "FreeBSD")
1080+
set(SWIFT_USE_LINKER_default "lld")
10791081
else()
10801082
get_gold_version(gold_version)
10811083
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 all platforms for
426+
/// ABI reasons. We must define our own.
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: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -71,8 +71,14 @@ 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+
/// std::pair is not trivially copyable on all supported platforms.
75+
/// This struct works around that limitation.
76+
struct IndirectDifferentiation {
77+
ApplyInst *applyInst;
78+
SILDifferentiabilityWitness *witness;
79+
};
80+
IndirectDifferentiation indirectDifferentiation;
81+
7682
Value(ApplyInst *applyInst, SILDifferentiabilityWitness *witness)
7783
: indirectDifferentiation({applyInst, witness}) {}
7884

@@ -111,7 +117,8 @@ struct DifferentiationInvoker {
111117
std::pair<ApplyInst *, SILDifferentiabilityWitness *>
112118
getIndirectDifferentiation() const {
113119
assert(kind == Kind::IndirectDifferentiation);
114-
return value.indirectDifferentiation;
120+
return std::make_pair(value.indirectDifferentiation.applyInst,
121+
value.indirectDifferentiation.witness);
115122
}
116123

117124
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:
@@ -158,6 +159,8 @@ static bool isPlatformActiveForTarget(PlatformKind Platform,
158159
case PlatformKind::visionOS:
159160
case PlatformKind::visionOSApplicationExtension:
160161
return Target.isXROS();
162+
case PlatformKind::FreeBSD:
163+
return Target.isOSFreeBSD();
161164
case PlatformKind::OpenBSD:
162165
return Target.isOSOpenBSD();
163166
case PlatformKind::Windows:
@@ -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:
@@ -319,6 +324,7 @@ bool swift::isPlatformSPI(PlatformKind Platform) {
319324
case PlatformKind::watchOSApplicationExtension:
320325
case PlatformKind::visionOS:
321326
case PlatformKind::visionOSApplicationExtension:
327+
case PlatformKind::FreeBSD:
322328
case PlatformKind::OpenBSD:
323329
case PlatformKind::Windows:
324330
case PlatformKind::none:

lib/AST/Type.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4817,7 +4817,8 @@ AnyFunctionType::getAutoDiffDerivativeFunctionLinearMapType(
48174817
if (!resultTan)
48184818
return llvm::make_error<DerivativeFunctionTypeError>(
48194819
this, DerivativeFunctionTypeError::Kind::NonDifferentiableResult,
4820-
std::make_pair(originalResultType, unsigned(originalResult.index)));
4820+
DerivativeFunctionTypeError::TypeAndIndex(
4821+
originalResultType, unsigned(originalResult.index)));
48214822

48224823
if (!originalResult.isSemanticResultParameter)
48234824
resultTanTypes.push_back(resultTan->getType());
@@ -4847,7 +4848,7 @@ AnyFunctionType::getAutoDiffDerivativeFunctionLinearMapType(
48474848
this,
48484849
DerivativeFunctionTypeError::Kind::
48494850
NonDifferentiableDifferentiabilityParameter,
4850-
std::make_pair(paramType, i));
4851+
DerivativeFunctionTypeError::TypeAndIndex(paramType, i));
48514852

48524853
differentialParams.push_back(AnyFunctionType::Param(
48534854
paramTan->getType(), Identifier(), diffParam.getParameterFlags()));
@@ -4895,7 +4896,7 @@ AnyFunctionType::getAutoDiffDerivativeFunctionLinearMapType(
48954896
this,
48964897
DerivativeFunctionTypeError::Kind::
48974898
NonDifferentiableDifferentiabilityParameter,
4898-
std::make_pair(paramType, i));
4899+
DerivativeFunctionTypeError::TypeAndIndex(paramType, i));
48994900

49004901
if (diffParam.isAutoDiffSemanticResult()) {
49014902
if (paramType->isVoid())

lib/ClangImporter/ClangImporter.cpp

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2557,7 +2557,9 @@ PlatformAvailability::PlatformAvailability(const LangOptions &langOpts)
25572557
case PlatformKind::visionOS:
25582558
case PlatformKind::visionOSApplicationExtension:
25592559
break;
2560-
2560+
case PlatformKind::FreeBSD:
2561+
deprecatedAsUnavailableMessage = "";
2562+
break;
25612563
case PlatformKind::OpenBSD:
25622564
deprecatedAsUnavailableMessage = "";
25632565
break;
@@ -2605,6 +2607,9 @@ bool PlatformAvailability::isPlatformRelevant(StringRef name) const {
26052607
return name == "xros" || name == "xros_app_extension" ||
26062608
name == "visionos" || name == "visionos_app_extension";
26072609

2610+
case PlatformKind::FreeBSD:
2611+
return name == "freebsd";
2612+
26082613
case PlatformKind::OpenBSD:
26092614
return name == "openbsd";
26102615

@@ -2676,6 +2681,10 @@ bool PlatformAvailability::treatDeprecatedAsUnavailable(
26762681
// No deprecation filter on xrOS
26772682
return false;
26782683

2684+
case PlatformKind::FreeBSD;
2685+
// No deprecation filter on OpenBSD
2686+
return false;
2687+
26792688
case PlatformKind::OpenBSD:
26802689
// No deprecation filter on OpenBSD
26812690
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())) {
171+
if (!(Triple.isOSDarwin() || Triple.isOSLinux() || Triple.isOSWindows() || 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;

0 commit comments

Comments
 (0)