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;

0 commit comments

Comments
 (0)