Skip to content

Commit 66e2f97

Browse files
authored
Merge pull request #75432 from tshortli/maccatalyst-upstream
Upstream missing macCatalyst support
2 parents fc51e75 + eb16025 commit 66e2f97

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

42 files changed

+1406
-24
lines changed

include/swift/AST/ASTContext.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -734,6 +734,13 @@ class ASTContext final {
734734
// Retrieve the declaration of Swift._stdlib_isOSVersionAtLeast.
735735
FuncDecl *getIsOSVersionAtLeastDecl() const;
736736

737+
// Retrieve the declaration of Swift._stdlib_isVariantOSVersionAtLeast.
738+
FuncDecl *getIsVariantOSVersionAtLeastDecl() const;
739+
740+
// Retrieve the declaration of
741+
// Swift._stdlib_isOSVersionAtLeastOrVariantVersionAtLeast.
742+
FuncDecl *getIsOSVersionAtLeastOrVariantVersionAtLeast() const;
743+
737744
/// Look for the declaration with the given name within the
738745
/// passed in module.
739746
void lookupInModule(ModuleDecl *M, StringRef name,

include/swift/AST/Attr.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2316,7 +2316,7 @@ class BackDeployedAttr : public DeclAttribute {
23162316
const llvm::VersionTuple Version;
23172317

23182318
/// Returns true if this attribute is active given the current platform.
2319-
bool isActivePlatform(const ASTContext &ctx) const;
2319+
bool isActivePlatform(const ASTContext &ctx, bool forTargetVariant) const;
23202320

23212321
static bool classof(const DeclAttribute *DA) {
23222322
return DA->getKind() == DeclAttrKind::BackDeployed;
@@ -2745,7 +2745,8 @@ class DeclAttributes {
27452745

27462746
/// Returns the `@backDeployed` attribute that is active for the current
27472747
/// platform.
2748-
const BackDeployedAttr *getBackDeployed(const ASTContext &ctx) const;
2748+
const BackDeployedAttr *getBackDeployed(const ASTContext &ctx,
2749+
bool forTargetVariant) const;
27492750

27502751
SWIFT_DEBUG_DUMPER(dump(const Decl *D = nullptr));
27512752
void print(ASTPrinter &Printer, const PrintOptions &Options,

include/swift/AST/Builtins.def

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -786,6 +786,12 @@ BUILTIN_MISC_OPERATION(ResumeNonThrowingContinuationReturning,
786786
/// targetOSVersionAtLeast has type (Builtin.Int32, Builtin.Int32, Builtin.Int32) -> Builtin.Int32
787787
BUILTIN_MISC_OPERATION(TargetOSVersionAtLeast, "targetOSVersionAtLeast", "n", Special)
788788

789+
/// targetVariantOSVersionAtLeast has type (Builtin.Int32, Builtin.Int32, Builtin.Int32) -> Builtin.Int32
790+
BUILTIN_MISC_OPERATION(TargetVariantOSVersionAtLeast, "targetVariantOSVersionAtLeast", "n", Special)
791+
792+
/// targetOSVersionOrVariantOSVersionAtLeast has type (Builtin.UInt32, Builtin.UInt32, Builtin.UInt32, Builtin.UInt32, Builtin.UInt32, Builtin.UInt32) -> Builtin.UInt32
793+
BUILTIN_MISC_OPERATION(TargetOSVersionOrVariantOSVersionAtLeast, "targetOSVersionOrVariantOSVersionAtLeast", "n", Special)
794+
789795
/// Resume a throwing continuation normally with the given result.
790796
BUILTIN_MISC_OPERATION(ResumeThrowingContinuationReturning,
791797
"resumeThrowingContinuationReturning", "", Special)

include/swift/AST/Decl.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1067,7 +1067,8 @@ class alignas(1 << DeclAlignInBits) Decl : public ASTAllocated<Decl> {
10671067
/// Returns the OS version in which the decl became ABI as specified by the
10681068
/// `@backDeployed` attribute.
10691069
std::optional<llvm::VersionTuple>
1070-
getBackDeployedBeforeOSVersion(ASTContext &Ctx) const;
1070+
getBackDeployedBeforeOSVersion(ASTContext &Ctx,
1071+
bool forTargetVariant = false) const;
10711072

10721073
/// Returns true if the decl has an active `@backDeployed` attribute for the
10731074
/// given context.

include/swift/Runtime/RuntimeFunctions.def

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2151,6 +2151,21 @@ FUNCTION(PlatformVersionAtLeast, __isPlatformVersionAtLeast,
21512151
EFFECT(NoEffect),
21522152
UNKNOWN_MEMEFFECTS)
21532153

2154+
// int32 __isPlatformOrVariantPlatformVersionAtLeast(uint32_t platform1,
2155+
// uint32_t major1, uint32_t minor1, uint32_t patch1,
2156+
// uint32_t platform2, uint32_t major2, uint32_t minor2,
2157+
// uint32_t patch2);
2158+
// This is a C builtin provided by compiler-rt.
2159+
FUNCTION(TargetOSVersionOrVariantOSVersionAtLeast,
2160+
__isPlatformOrVariantPlatformVersionAtLeast,
2161+
C_CC, AlwaysAvailable,
2162+
RETURNS(Int32Ty),
2163+
ARGS(Int32Ty, Int32Ty, Int32Ty, Int32Ty, Int32Ty, Int32Ty, Int32Ty,
2164+
Int32Ty),
2165+
ATTRS(NoUnwind),
2166+
EFFECT(NoEffect),
2167+
UNKNOWN_MEMEFFECTS)
2168+
21542169
FUNCTION(GetKeyPath, swift_getKeyPath, C_CC, AlwaysAvailable,
21552170
RETURNS(RefCountedPtrTy),
21562171
ARGS(Int8PtrTy, Int8PtrTy),

lib/AST/ASTContext.cpp

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -389,6 +389,23 @@ struct ASTContext::Implementation {
389389
/// -> Builtin.Int1
390390
FuncDecl *IsOSVersionAtLeastDecl = nullptr;
391391

392+
/// func _stdlib_isVariantOSVersionAtLeast(
393+
/// Builtin.Word,
394+
/// Builtin.Word,
395+
/// Builtin.word)
396+
/// -> Builtin.Int1
397+
FuncDecl *IsVariantOSVersionAtLeastDecl = nullptr;
398+
399+
/// func _stdlib_isOSVersionAtLeastOrVariantVersionAtLeast(
400+
/// Builtin.Word,
401+
/// Builtin.Word,
402+
/// Builtin.Word,
403+
/// Builtin.Word,
404+
/// Builtin.Word,
405+
/// Builtin.Word)
406+
/// -> Builtin.Int1
407+
FuncDecl *IsOSVersionAtLeastOrVariantVersionAtLeastDecl = nullptr;
408+
392409
/// The set of known protocols, lazily populated as needed.
393410
ProtocolDecl *KnownProtocols[NumKnownProtocols] = { };
394411

@@ -1842,6 +1859,31 @@ FuncDecl *ASTContext::getIsOSVersionAtLeastDecl() const {
18421859
return decl;
18431860
}
18441861

1862+
FuncDecl *ASTContext::getIsVariantOSVersionAtLeastDecl() const {
1863+
if (getImpl().IsVariantOSVersionAtLeastDecl)
1864+
return getImpl().IsVariantOSVersionAtLeastDecl;
1865+
1866+
auto decl = findLibraryIntrinsic(*this, "_stdlib_isVariantOSVersionAtLeast");
1867+
if (!decl)
1868+
return nullptr;
1869+
1870+
getImpl().IsVariantOSVersionAtLeastDecl = decl;
1871+
return decl;
1872+
}
1873+
1874+
FuncDecl *ASTContext::getIsOSVersionAtLeastOrVariantVersionAtLeast() const {
1875+
if (getImpl().IsOSVersionAtLeastOrVariantVersionAtLeastDecl)
1876+
return getImpl().IsOSVersionAtLeastOrVariantVersionAtLeastDecl;
1877+
1878+
auto decl = findLibraryIntrinsic(*this,
1879+
"_stdlib_isOSVersionAtLeastOrVariantVersionAtLeast");
1880+
if (!decl)
1881+
return nullptr;
1882+
1883+
getImpl().IsOSVersionAtLeastOrVariantVersionAtLeastDecl = decl;
1884+
return decl;
1885+
}
1886+
18451887
static bool isHigherPrecedenceThan(PrecedenceGroupDecl *a,
18461888
PrecedenceGroupDecl *b) {
18471889
assert(a != b && "exact match should already have been filtered");

lib/AST/Attr.cpp

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -617,7 +617,8 @@ const AvailableAttr *DeclAttributes::getNoAsync(const ASTContext &ctx) const {
617617
}
618618

619619
const BackDeployedAttr *
620-
DeclAttributes::getBackDeployed(const ASTContext &ctx) const {
620+
DeclAttributes::getBackDeployed(const ASTContext &ctx,
621+
bool forTargetVariant) const {
621622
const BackDeployedAttr *bestAttr = nullptr;
622623

623624
for (auto attr : *this) {
@@ -626,7 +627,7 @@ DeclAttributes::getBackDeployed(const ASTContext &ctx) const {
626627
continue;
627628

628629
if (backDeployedAttr->isInvalid() ||
629-
!backDeployedAttr->isActivePlatform(ctx))
630+
!backDeployedAttr->isActivePlatform(ctx, forTargetVariant))
630631
continue;
631632

632633
// We have an attribute that is active for the platform, but
@@ -2226,8 +2227,9 @@ bool AvailableAttr::isActivePlatform(const ASTContext &ctx) const {
22262227
return isPlatformActive(Platform, ctx.LangOpts);
22272228
}
22282229

2229-
bool BackDeployedAttr::isActivePlatform(const ASTContext &ctx) const {
2230-
return isPlatformActive(Platform, ctx.LangOpts);
2230+
bool BackDeployedAttr::isActivePlatform(const ASTContext &ctx,
2231+
bool forTargetVariant) const {
2232+
return isPlatformActive(Platform, ctx.LangOpts, forTargetVariant);
22312233
}
22322234

22332235
AvailableAttr *AvailableAttr::clone(ASTContext &C, bool implicit) const {

lib/AST/Builtins.cpp

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1766,6 +1766,21 @@ static ValueDecl *getTargetOSVersionAtLeast(ASTContext &Context,
17661766
return getBuiltinFunction(Id, {int32Type, int32Type, int32Type}, int32Type);
17671767
}
17681768

1769+
static ValueDecl *getTargetVariantOSVersionAtLeast(ASTContext &Context,
1770+
Identifier Id) {
1771+
auto int32Type = BuiltinIntegerType::get(32, Context);
1772+
return getBuiltinFunction(Id, {int32Type, int32Type, int32Type}, int32Type);
1773+
}
1774+
1775+
static ValueDecl *
1776+
getTargetOSVersionOrVariantOSVersionAtLeast(ASTContext &Context,
1777+
Identifier Id) {
1778+
auto int32Type = BuiltinIntegerType::get(32, Context);
1779+
return getBuiltinFunction(Id, {int32Type, int32Type, int32Type,
1780+
int32Type, int32Type, int32Type},
1781+
int32Type);
1782+
}
1783+
17691784
static ValueDecl *getBuildOrdinaryTaskExecutorRef(ASTContext &ctx,
17701785
Identifier id) {
17711786
return getBuiltinFunction(ctx, id, _thin,
@@ -3162,6 +3177,12 @@ ValueDecl *swift::getBuiltinValueDecl(ASTContext &Context, Identifier Id) {
31623177
case BuiltinValueKind::TargetOSVersionAtLeast:
31633178
return getTargetOSVersionAtLeast(Context, Id);
31643179

3180+
case BuiltinValueKind::TargetVariantOSVersionAtLeast:
3181+
return getTargetVariantOSVersionAtLeast(Context, Id);
3182+
3183+
case BuiltinValueKind::TargetOSVersionOrVariantOSVersionAtLeast:
3184+
return getTargetOSVersionOrVariantOSVersionAtLeast(Context, Id);
3185+
31653186
case BuiltinValueKind::ConvertTaskToJob:
31663187
return getConvertTaskToJob(Context, Id);
31673188

lib/AST/Decl.cpp

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -565,8 +565,9 @@ Decl::getIntroducedOSVersion(PlatformKind Kind) const {
565565
}
566566

567567
std::optional<llvm::VersionTuple>
568-
Decl::getBackDeployedBeforeOSVersion(ASTContext &Ctx) const {
569-
if (auto *attr = getAttrs().getBackDeployed(Ctx)) {
568+
Decl::getBackDeployedBeforeOSVersion(ASTContext &Ctx,
569+
bool forTargetVariant) const {
570+
if (auto *attr = getAttrs().getBackDeployed(Ctx, forTargetVariant)) {
570571
auto version = attr->Version;
571572
StringRef ignoredPlatformString;
572573
AvailabilityInference::updateBeforePlatformForFallback(
@@ -582,13 +583,22 @@ Decl::getBackDeployedBeforeOSVersion(ASTContext &Ctx) const {
582583

583584
// Accessors may inherit `@backDeployed`.
584585
if (auto *AD = dyn_cast<AccessorDecl>(this))
585-
return AD->getStorage()->getBackDeployedBeforeOSVersion(Ctx);
586+
return AD->getStorage()->getBackDeployedBeforeOSVersion(Ctx,
587+
forTargetVariant);
586588

587589
return std::nullopt;
588590
}
589591

590592
bool Decl::isBackDeployed(ASTContext &Ctx) const {
591-
return getBackDeployedBeforeOSVersion(Ctx) != std::nullopt;
593+
if (getBackDeployedBeforeOSVersion(Ctx))
594+
return true;
595+
596+
if (Ctx.LangOpts.TargetVariant) {
597+
if (getBackDeployedBeforeOSVersion(Ctx, /*forTargetVariant=*/true))
598+
return true;
599+
}
600+
601+
return false;
592602
}
593603

594604
bool Decl::hasBackDeployedAttr() const {

lib/ClangImporter/ClangImporter.cpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -869,6 +869,22 @@ importer::addCommonInvocationArguments(
869869
invocationArgStrs.push_back("-mcx16");
870870
}
871871

872+
if (triple.isOSDarwin()) {
873+
if (auto variantTriple = ctx.LangOpts.TargetVariant) {
874+
// Passing the -target-variant along to clang causes clang's
875+
// CodeGenerator to emit zippered .o files.
876+
invocationArgStrs.push_back("-darwin-target-variant");
877+
invocationArgStrs.push_back(variantTriple->str());
878+
}
879+
880+
if (ctx.LangOpts.VariantSDKVersion) {
881+
invocationArgStrs.push_back("-Xclang");
882+
invocationArgStrs.push_back(
883+
("-darwin-target-variant-sdk-version=" +
884+
ctx.LangOpts.VariantSDKVersion->getAsString()));
885+
}
886+
}
887+
872888
if (std::optional<StringRef> R = ctx.SearchPathOpts.getWinSDKRoot()) {
873889
invocationArgStrs.emplace_back("-Xmicrosoft-windows-sdk-root");
874890
invocationArgStrs.emplace_back(*R);

lib/IRGen/AllocStackHoisting.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -442,7 +442,9 @@ bool inhibitsAllocStackHoisting(SILInstruction *I) {
442442
return Apply->hasSemantics(semantics::AVAILABILITY_OSVERSION);
443443
}
444444
if (auto *bi = dyn_cast<BuiltinInst>(I)) {
445-
return bi->getBuiltinInfo().ID == BuiltinValueKind::TargetOSVersionAtLeast;
445+
return bi->getBuiltinInfo().ID == BuiltinValueKind::TargetOSVersionAtLeast
446+
|| bi->getBuiltinInfo().ID == BuiltinValueKind::TargetVariantOSVersionAtLeast
447+
|| bi->getBuiltinInfo().ID == BuiltinValueKind::TargetOSVersionOrVariantOSVersionAtLeast;
446448
}
447449
if (isa<HasSymbolInst>(I)) {
448450
return true;

lib/IRGen/GenBuiltin.cpp

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1390,7 +1390,34 @@ void irgen::emitBuiltinCall(IRGenFunction &IGF, const BuiltinInfo &Builtin,
13901390
out.add(result);
13911391
return;
13921392
}
1393-
1393+
1394+
if (Builtin.ID == BuiltinValueKind::TargetVariantOSVersionAtLeast) {
1395+
auto major = args.claimNext();
1396+
auto minor = args.claimNext();
1397+
auto patch = args.claimNext();
1398+
auto result = IGF.emitTargetVariantOSVersionAtLeastCall(major, minor,
1399+
patch);
1400+
out.add(result);
1401+
return;
1402+
}
1403+
1404+
if (Builtin.ID ==
1405+
BuiltinValueKind::TargetOSVersionOrVariantOSVersionAtLeast) {
1406+
auto major1 = args.claimNext();
1407+
auto minor1 = args.claimNext();
1408+
auto patch1 = args.claimNext();
1409+
1410+
auto major2 = args.claimNext();
1411+
auto minor2 = args.claimNext();
1412+
auto patch2 = args.claimNext();
1413+
1414+
auto result = IGF.emitTargetOSVersionOrVariantOSVersionAtLeastCall(major1,
1415+
minor1, patch1, major2, minor2, patch2);
1416+
1417+
out.add(result);
1418+
return;
1419+
}
1420+
13941421
if (Builtin.ID == BuiltinValueKind::TypePtrAuthDiscriminator) {
13951422
(void)args.claimAll();
13961423
Type valueTy = substitutions.getReplacementTypes()[0];

lib/IRGen/IRGen.cpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1030,6 +1030,22 @@ static void initLLVMModule(const IRGenModule &IGM, SILModule &SIL) {
10301030
assert(Module->getSDKVersion() == *IGM.Context.LangOpts.SDKVersion);
10311031
}
10321032

1033+
if (!IGM.VariantTriple.str().empty()) {
1034+
if (Module->getDarwinTargetVariantTriple().empty()) {
1035+
Module->setDarwinTargetVariantTriple(IGM.VariantTriple.str());
1036+
} else {
1037+
assert(Module->getDarwinTargetVariantTriple() == IGM.VariantTriple.str());
1038+
}
1039+
}
1040+
1041+
if (IGM.Context.LangOpts.VariantSDKVersion) {
1042+
if (Module->getDarwinTargetVariantSDKVersion().empty())
1043+
Module->setDarwinTargetVariantSDKVersion(*IGM.Context.LangOpts.VariantSDKVersion);
1044+
else
1045+
assert(Module->getDarwinTargetVariantSDKVersion() ==
1046+
*IGM.Context.LangOpts.VariantSDKVersion);
1047+
}
1048+
10331049
// Set the module's string representation.
10341050
Module->setDataLayout(IGM.DataLayout.getStringRepresentation());
10351051

lib/IRGen/IRGenFunction.cpp

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -309,6 +309,39 @@ IRGenFunction::emitTargetOSVersionAtLeastCall(llvm::Value *major,
309309
return Builder.CreateCall(fn, {platformID, major, minor, patch});
310310
}
311311

312+
llvm::Value *
313+
IRGenFunction::emitTargetVariantOSVersionAtLeastCall(llvm::Value *major,
314+
llvm::Value *minor,
315+
llvm::Value *patch) {
316+
auto *fn = cast<llvm::Function>(IGM.getPlatformVersionAtLeastFn());
317+
318+
llvm::Value *iOSPlatformID =
319+
llvm::ConstantInt::get(IGM.Int32Ty, llvm::MachO::PLATFORM_IOS);
320+
return Builder.CreateCall(fn->getFunctionType(), fn, {iOSPlatformID, major, minor, patch});
321+
}
322+
323+
llvm::Value *
324+
IRGenFunction::emitTargetOSVersionOrVariantOSVersionAtLeastCall(
325+
llvm::Value *major, llvm::Value *minor, llvm::Value *patch,
326+
llvm::Value *variantMajor, llvm::Value *variantMinor,
327+
llvm::Value *variantPatch) {
328+
auto *fn = cast<llvm::Function>(
329+
IGM.getTargetOSVersionOrVariantOSVersionAtLeastFn());
330+
331+
llvm::Value *macOSPlatformID =
332+
llvm::ConstantInt::get(IGM.Int32Ty, llvm::MachO::PLATFORM_MACOS);
333+
334+
llvm::Value *iOSPlatformID =
335+
llvm::ConstantInt::get(IGM.Int32Ty, llvm::MachO::PLATFORM_IOS);
336+
337+
return Builder.CreateCall(fn->getFunctionType(),
338+
fn, {macOSPlatformID, major, minor, patch,
339+
iOSPlatformID, variantMajor, variantMinor,
340+
variantPatch});
341+
}
342+
343+
344+
312345
/// Initialize a relative indirectable pointer to the given value.
313346
/// This always leaves the value in the direct state; if it's not a
314347
/// far reference, it's the caller's responsibility to ensure that the

lib/IRGen/IRGenFunction.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -358,6 +358,15 @@ class IRGenFunction {
358358
llvm::Value *minor,
359359
llvm::Value *patch);
360360

361+
llvm::Value *emitTargetVariantOSVersionAtLeastCall(llvm::Value *major,
362+
llvm::Value *minor,
363+
llvm::Value *patch);
364+
365+
llvm::Value *emitTargetOSVersionOrVariantOSVersionAtLeastCall(
366+
llvm::Value *major, llvm::Value *minor, llvm::Value *patch,
367+
llvm::Value *variantMajor, llvm::Value *variantMinor,
368+
llvm::Value *variantPatch);
369+
361370
llvm::Value *emitProjectBoxCall(llvm::Value *box, llvm::Value *typeMetadata);
362371

363372
llvm::Value *emitAllocEmptyBoxCall();

0 commit comments

Comments
 (0)