Skip to content

[macCatalyst] Support generating macCatalyst object files #75261

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

Closed
wants to merge 1 commit into from
Closed
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
3 changes: 3 additions & 0 deletions include/swift/AST/ASTContext.h
Original file line number Diff line number Diff line change
Expand Up @@ -734,6 +734,9 @@ class ASTContext final {
// Retrieve the declaration of Swift._stdlib_isOSVersionAtLeast.
FuncDecl *getIsOSVersionAtLeastDecl() const;

// Retrieve the declaration of Swift._stdlib_isOSVersionAtLeastOrVariantVersionAtLeast.
FuncDecl *getIsVariantOSVersionAtLeastDecl() const;

/// Look for the declaration with the given name within the
/// passed in module.
void lookupInModule(ModuleDecl *M, StringRef name,
Expand Down
38 changes: 38 additions & 0 deletions lib/AST/ASTContext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -389,6 +389,10 @@ struct ASTContext::Implementation {
/// -> Builtin.Int1
FuncDecl *IsOSVersionAtLeastDecl = nullptr;

/// func _stdlib_isOSVersionAtLeastOrVariantVersionAtLeast(Builtin.Word,Builtin.Word, Builtin.word, Builtin.Word,Builtin.Word, Builtin.word)
/// -> Builtin.Int1
FuncDecl *IsVariantOSVersionAtLeastDecl = nullptr;

/// The set of known protocols, lazily populated as needed.
ProtocolDecl *KnownProtocols[NumKnownProtocols] = { };

Expand Down Expand Up @@ -1841,6 +1845,40 @@ FuncDecl *ASTContext::getIsOSVersionAtLeastDecl() const {
return decl;
}

FuncDecl *ASTContext::getIsVariantOSVersionAtLeastDecl() const {
if (getImpl().IsVariantOSVersionAtLeastDecl)
return getImpl().IsVariantOSVersionAtLeastDecl;

// Look for the function.
auto decl =
findLibraryIntrinsic(*this, "_stdlib_isOSVersionAtLeastOrVariantVersionAtLeast");
if (!decl)
return nullptr;

auto *fnType = getIntrinsicCandidateType(decl, /*allowTypeMembers=*/false);
if (!fnType)
return nullptr;

// Input must be (Builtin.Word, Builtin.Word, Builtin.Word, Builtin.Word, Builtin.Word, Builtin.Word)
auto intrinsicsParams = fnType->getParams();
if (intrinsicsParams.size() != 6)
return nullptr;

if (llvm::any_of(intrinsicsParams, [](AnyFunctionType::Param param) {
return (param.isVariadic() || param.isInOut() ||
!isBuiltinWordType(param.getPlainType()));
})) {
return nullptr;
}

// Output must be Builtin.Int1
if (!isBuiltinInt1Type(fnType->getResult()))
return nullptr;

getImpl().IsVariantOSVersionAtLeastDecl = decl;
return decl;
}

static bool isHigherPrecedenceThan(PrecedenceGroupDecl *a,
PrecedenceGroupDecl *b) {
assert(a != b && "exact match should already have been filtered");
Expand Down
4 changes: 4 additions & 0 deletions lib/IRGen/IRGen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1029,6 +1029,10 @@ static void initLLVMModule(const IRGenModule &IGM, SILModule &SIL) {
else
assert(Module->getSDKVersion() == *IGM.Context.LangOpts.SDKVersion);
}
if (IGM.Context.LangOpts.TargetVariant)
Module->setDarwinTargetVariantTriple(IGM.Context.LangOpts.TargetVariant->getTriple());
if (IGM.Context.LangOpts.VariantSDKVersion)
Module->setDarwinTargetVariantSDKVersion(*IGM.Context.LangOpts.VariantSDKVersion);

// Set the module's string representation.
Module->setDataLayout(IGM.DataLayout.getStringRepresentation());
Expand Down
4 changes: 4 additions & 0 deletions lib/IRGen/IRGenModule.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2308,6 +2308,10 @@ bool swift::writeEmptyOutputFilesFor(
Context.getClangModuleLoader());
llvmModule->setTargetTriple(
clangImporter->getTargetInfo().getTargetOpts().Triple);
if (const auto *TVT = clangImporter->getTargetInfo().getDarwinTargetVariantTriple())
llvmModule->setDarwinTargetVariantTriple(TVT->getTriple());
if (auto TVSDKVersion = clangImporter->getTargetInfo().getDarwinTargetVariantSDKVersion())
llvmModule->setDarwinTargetVariantSDKVersion(*TVSDKVersion);

// Add LLVM module flags.
auto &clangASTContext = clangImporter->getClangASTContext();
Expand Down
2 changes: 1 addition & 1 deletion lib/SILGen/SILGenBackDeploy.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ static void emitBackDeployIfAvailableCondition(SILGenFunction &SGF,
SILType i1 = SILType::getBuiltinIntegerType(1, SGF.getASTContext());
booleanTestValue = SGF.B.createIntegerLiteral(loc, i1, 1);
} else {
booleanTestValue = SGF.emitOSVersionRangeCheck(loc, OSVersion);
booleanTestValue = SGF.emitOSVersionRangeCheck(loc, OSVersion, VersionRange::empty());
}

SGF.B.createCondBranch(loc, booleanTestValue, availableBB, unavailableBB);
Expand Down
29 changes: 23 additions & 6 deletions lib/SILGen/SILGenDecl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1725,24 +1725,40 @@ emitVersionLiterals(SILLocation loc, SILGenBuilder &B, ASTContext &ctx,
/// the specified version range and 0 otherwise. The returned SILValue
/// (which has type Builtin.Int1) represents the result of this check.
SILValue SILGenFunction::emitOSVersionRangeCheck(SILLocation loc,
const VersionRange &range) {
const VersionRange &range,
const VersionRange &variantRange) {
// Emit constants for the checked version range.
SILValue majorValue;
SILValue minorValue;
SILValue subminorValue;
std::tie(majorValue, minorValue, subminorValue) =
emitVersionLiterals(loc, B, getASTContext(), range.getLowerEndpoint());

// Emit call to _stdlib_isOSVersionAtLeast(major, minor, patch)
FuncDecl *versionQueryDecl =
getASTContext().getIsOSVersionAtLeastDecl();
std::vector<SILValue> args{majorValue, minorValue, subminorValue};

FuncDecl *versionQueryDecl = nullptr;
if (!variantRange.isEmpty()) {
SILValue variantMajorValue;
SILValue variantMinorValue;
SILValue variantSubminorValue;
std::tie(variantMajorValue, variantMinorValue, variantSubminorValue) =
emitVersionLiterals(loc, B, getASTContext(), variantRange.getLowerEndpoint());

args.push_back(variantMajorValue);
args.push_back(variantMinorValue);
args.push_back(variantSubminorValue);
versionQueryDecl = getASTContext().getIsVariantOSVersionAtLeastDecl();
} else {
// Emit call to _stdlib_isOSVersionAtLeast(major, minor, patch)
versionQueryDecl =
getASTContext().getIsOSVersionAtLeastDecl();
}
assert(versionQueryDecl);

auto silDeclRef = SILDeclRef(versionQueryDecl);
SILValue availabilityGTEFn = emitGlobalFunctionRef(
loc, silDeclRef, getConstantInfo(getTypeExpansionContext(), silDeclRef));

SILValue args[] = {majorValue, minorValue, subminorValue};
return B.createApply(loc, availabilityGTEFn, SubstitutionMap(), args);
}

Expand Down Expand Up @@ -1795,6 +1811,7 @@ void SILGenFunction::emitStmtCondition(StmtCondition Cond, JumpDest FalseDest,
// specified by elt.
PoundAvailableInfo *availability = elt.getAvailability();
VersionRange OSVersion = availability->getAvailableRange();
VersionRange VariantOSVersion = availability->getVariantAvailableRange();

// The OS version might be left empty if availability checking was
// disabled. Treat it as always-true in that case.
Expand All @@ -1808,7 +1825,7 @@ void SILGenFunction::emitStmtCondition(StmtCondition Cond, JumpDest FalseDest,
bool value = !availability->isUnavailability();
booleanTestValue = B.createIntegerLiteral(loc, i1, value);
} else {
booleanTestValue = emitOSVersionRangeCheck(loc, OSVersion);
booleanTestValue = emitOSVersionRangeCheck(loc, OSVersion, VariantOSVersion);
if (availability->isUnavailability()) {
// If this is an unavailability check, invert the result
// by emitting a call to Builtin.xor_Int1(lhs, -1).
Expand Down
2 changes: 1 addition & 1 deletion lib/SILGen/SILGenFunction.h
Original file line number Diff line number Diff line change
Expand Up @@ -1591,7 +1591,7 @@ class LLVM_LIBRARY_VISIBILITY SILGenFunction
// Patterns
//===--------------------------------------------------------------------===//

SILValue emitOSVersionRangeCheck(SILLocation loc, const VersionRange &range);
SILValue emitOSVersionRangeCheck(SILLocation loc, const VersionRange &range, const VersionRange &variantRange);
void emitStmtCondition(StmtCondition Cond, JumpDest FalseDest, SILLocation loc,
ProfileCounter NumTrueTaken = ProfileCounter(),
ProfileCounter NumFalseTaken = ProfileCounter());
Expand Down
6 changes: 5 additions & 1 deletion stdlib/public/core/Availability.swift
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,11 @@ public func _stdlib_isOSVersionAtLeastOrVariantVersionAtLeast(
_ variantMinor: Builtin.Word,
_ variantPatch: Builtin.Word
) -> Builtin.Int1 {
return _stdlib_isOSVersionAtLeast(major, minor, patch)
var isAtLeast = _stdlib_isOSVersionAtLeast(major, minor, patch)
if !Bool(isAtLeast) {
isAtLeast = _stdlib_isOSVersionAtLeast(variantMajor, variantMinor, variantPatch)
}
return isAtLeast
}
#endif

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,5 +29,5 @@ withUnsafeTemporaryAllocation(byteCount: 0x0FFF_FFFF, alignment: 1) { buffer in

// CHECK-LARGE-ALLOC-DAG: [[IS_SAFE:%[0-9]+]] = {{(tail )?}}call {{(zeroext )?}}i1 @swift_stdlib_isStackAllocationSafe([[WORD]] 268435455, [[WORD]] 1)
// CHECK-LARGE-ALLOC-DAG: br i1 [[IS_SAFE]], label %{{[0-9]+}}, label %{{[0-9]+}}
// CHECK-LARGE-ALLOC-apple-DAG: [[IS_OS_OK:%[0-9]+]] = {{(tail )?}}call swiftcc i1 @"$ss26_stdlib_isOSVersionAtLeastyBi1_Bw_BwBwtF"
// CHECK-LARGE-ALLOC-apple-DAG: [[IS_OS_OK:%[0-9]+]] = {{(tail )?}}call swiftcc i1 @"${{ss26_stdlib_isOSVersionAtLeastyBi1_Bw_BwBwtF|ss042_stdlib_isOSVersionAtLeastOrVariantVersiondE0yBi1_Bw_BwBwBwBwBwtF}}"
// CHECK-LARGE-ALLOC-apple-DAG: br i1 [[IS_OS_OK]], label %{{[0-9]+}}, label %{{[0-9]+}}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// RUN: %target-swift-emit-silgen %s -target x86_64-apple-macosx10.52 -target-variant x86_64-apple-ios50.0-macabi | %FileCheck %s
// RUN: %target-swift-emit-silgen %s -target x86_64-apple-ios50.0-macabi -target-variant x86_64-apple-macosx10.52 | %FileCheck %s
// RUN: %target-swift-emit-silgen %s -target x86_64-apple-macosx10.52 -target-variant x86_64-apple-ios50.0-macabi | %FileCheck %s --check-prefixes MAC,CHECK
// RUN: %target-swift-emit-silgen %s -target x86_64-apple-ios50.0-macabi -target-variant x86_64-apple-macosx10.52 | %FileCheck %s --check-prefixes IOS,CHECK


// REQUIRES: maccatalyst_support
Expand All @@ -10,13 +10,21 @@
// Test for the runtime non-canonical version hack for canonical macOS versioning.
// This will eventually change to be the correctly canonicalized version.

// CHECK: [[MACOS_MAJOR:%.*]] = integer_literal $Builtin.Word, 10
// CHECK: [[MACOS_MINOR:%.*]] = integer_literal $Builtin.Word, 16
// CHECK: [[MACOS_PATCH:%.*]] = integer_literal $Builtin.Word, 0
// CHECK: [[IOS_MAJOR:%.*]] = integer_literal $Builtin.Word, 51
// CHECK: [[IOS_MINOR:%.*]] = integer_literal $Builtin.Word, 1
// CHECK: [[IOS_PATCH:%.*]] = integer_literal $Builtin.Word, 2
// MAC: [[TARGET_MAJOR:%.*]] = integer_literal $Builtin.Word, 10
// MAC: [[TARGET_MINOR:%.*]] = integer_literal $Builtin.Word, 16
// MAC: [[TARGET_PATCH:%.*]] = integer_literal $Builtin.Word, 0
// MAC: [[VARIANT_MAJOR:%.*]] = integer_literal $Builtin.Word, 51
// MAC: [[VARIANT_MINOR:%.*]] = integer_literal $Builtin.Word, 1
// MAC: [[VARIANT_PATCH:%.*]] = integer_literal $Builtin.Word, 2

// IOS: [[TARGET_MAJOR:%.*]] = integer_literal $Builtin.Word, 51
// IOS: [[TARGET_MINOR:%.*]] = integer_literal $Builtin.Word, 1
// IOS: [[TARGET_PATCH:%.*]] = integer_literal $Builtin.Word, 2
// IOS: [[VARIANT_MAJOR:%.*]] = integer_literal $Builtin.Word, 10
// IOS: [[VARIANT_MINOR:%.*]] = integer_literal $Builtin.Word, 16
// IOS: [[VARIANT_PATCH:%.*]] = integer_literal $Builtin.Word, 0

// CHECK: [[FUNC:%.*]] = function_ref @$ss042_stdlib_isOSVersionAtLeastOrVariantVersiondE0yBi1_Bw_BwBwBwBwBwtF : $@convention(thin) (Builtin.Word, Builtin.Word, Builtin.Word, Builtin.Word, Builtin.Word, Builtin.Word) -> Builtin.Int1
// CHECK: [[QUERY_RESULT:%.*]] = apply [[FUNC]]([[MACOS_MAJOR]], [[MACOS_MINOR]], [[MACOS_PATCH]], [[IOS_MAJOR]], [[IOS_MINOR]], [[IOS_PATCH]]) : $@convention(thin) (Builtin.Word, Builtin.Word, Builtin.Word, Builtin.Word, Builtin.Word, Builtin.Word) -> Builtin.Int1
// CHECK: [[QUERY_RESULT:%.*]] = apply [[FUNC]]([[TARGET_MAJOR]], [[TARGET_MINOR]], [[TARGET_PATCH]], [[VARIANT_MAJOR]], [[VARIANT_MINOR]], [[VARIANT_PATCH]]) : $@convention(thin) (Builtin.Word, Builtin.Word, Builtin.Word, Builtin.Word, Builtin.Word, Builtin.Word) -> Builtin.Int1
if #available(OSX 10.16, iOS 51.1.2, *) {
}