Skip to content

Commit 3abd7b0

Browse files
devincoughlineeckstein
authored andcommitted
Add a targetOSVersionAtLeast builtin.
This allows the compiler to parse the Swift swiftinterface file of a recent SDK.
1 parent 17f1cf9 commit 3abd7b0

File tree

10 files changed

+71
-0
lines changed

10 files changed

+71
-0
lines changed

include/swift/AST/Builtins.def

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -770,6 +770,9 @@ BUILTIN_MISC_OPERATION(InitializeDistributedRemoteActor,
770770
BUILTIN_MISC_OPERATION(ResumeNonThrowingContinuationReturning,
771771
"resumeNonThrowingContinuationReturning", "", Special)
772772

773+
/// targetOSVersionAtLeast has type (Builtin.Int32, Builtin.Int32, Builtin.Int32) -> Builtin.Int32
774+
BUILTIN_MISC_OPERATION(TargetOSVersionAtLeast, "targetOSVersionAtLeast", "n", Special)
775+
773776
/// Resume a throwing continuation normally with the given result.
774777
BUILTIN_MISC_OPERATION(ResumeThrowingContinuationReturning,
775778
"resumeThrowingContinuationReturning", "", Special)

include/swift/Runtime/RuntimeFunctions.def

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1691,6 +1691,16 @@ FUNCTION(TSanInoutAccess, __tsan_external_write, C_CC, AlwaysAvailable,
16911691
ATTRS(NoUnwind),
16921692
EFFECT(NoEffect))
16931693

1694+
// int32 __isPlatformVersionAtLeast(uint32_t platform, uint32_t major,
1695+
// uint32_t minor, uint32_t patch);
1696+
// This a C builtin provided by compiler-rt.
1697+
FUNCTION(PlatformVersionAtLeast, __isPlatformVersionAtLeast,
1698+
C_CC, AlwaysAvailable,
1699+
RETURNS(Int32Ty),
1700+
ARGS(Int32Ty, Int32Ty, Int32Ty, Int32Ty),
1701+
ATTRS(NoUnwind),
1702+
EFFECT(NoEffect))
1703+
16941704
FUNCTION(GetKeyPath, swift_getKeyPath, C_CC, AlwaysAvailable,
16951705
RETURNS(RefCountedPtrTy),
16961706
ARGS(Int8PtrTy, Int8PtrTy),

lib/AST/Builtins.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1578,6 +1578,12 @@ static ValueDecl *getBuildDefaultActorExecutorRef(ASTContext &ctx,
15781578
_executor);
15791579
}
15801580

1581+
static ValueDecl *getTargetOSVersionAtLeast(ASTContext &Context,
1582+
Identifier Id) {
1583+
auto int32Type = BuiltinIntegerType::get(32, Context);
1584+
return getBuiltinFunction(Id, {int32Type, int32Type, int32Type}, int32Type);
1585+
}
1586+
15811587
static ValueDecl *getBuildOrdinarySerialExecutorRef(ASTContext &ctx,
15821588
Identifier id) {
15831589
return getBuiltinFunction(ctx, id, _thin,
@@ -2825,6 +2831,9 @@ ValueDecl *swift::getBuiltinValueDecl(ASTContext &Context, Identifier Id) {
28252831
case BuiltinValueKind::CreateAsyncTaskInGroup:
28262832
return getCreateAsyncTaskInGroup(Context, Id);
28272833

2834+
case BuiltinValueKind::TargetOSVersionAtLeast:
2835+
return getTargetOSVersionAtLeast(Context, Id);
2836+
28282837
case BuiltinValueKind::ConvertTaskToJob:
28292838
return getConvertTaskToJob(Context, Id);
28302839

lib/IRGen/GenBuiltin.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1223,6 +1223,15 @@ if (Builtin.ID == BuiltinValueKind::id) { \
12231223
return;
12241224
}
12251225

1226+
if (Builtin.ID == BuiltinValueKind::TargetOSVersionAtLeast) {
1227+
auto major = args.claimNext();
1228+
auto minor = args.claimNext();
1229+
auto patch = args.claimNext();
1230+
auto result = IGF.emitTargetOSVersionAtLeastCall(major, minor, patch);
1231+
out.add(result);
1232+
return;
1233+
}
1234+
12261235
if (Builtin.ID == BuiltinValueKind::Swift3ImplicitObjCEntrypoint) {
12271236
llvm::Value *entrypointArgs[7];
12281237
auto argIter = IGF.CurFn->arg_begin();

lib/IRGen/IRGenFunction.cpp

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
#include "llvm/IR/Function.h"
2323
#include "llvm/Support/CommandLine.h"
2424
#include "llvm/Support/raw_ostream.h"
25+
#include "llvm/BinaryFormat/MachO.h"
2526

2627
#include "Callee.h"
2728
#include "Explosion.h"
@@ -289,6 +290,35 @@ void IRGenFunction::emitTSanInoutAccessCall(llvm::Value *address) {
289290
Builder.CreateCall(fn, {castAddress, callerPC, castTag});
290291
}
291292

293+
// This is shamelessly copied from clang's codegen. We need to get the clang
294+
// functionality into a shared header so that platforms only
295+
// needs to be updated in one place.
296+
static unsigned getBaseMachOPlatformID(const llvm::Triple &TT) {
297+
switch (TT.getOS()) {
298+
case llvm::Triple::Darwin:
299+
case llvm::Triple::MacOSX:
300+
return llvm::MachO::PLATFORM_MACOS;
301+
case llvm::Triple::IOS:
302+
return llvm::MachO::PLATFORM_IOS;
303+
case llvm::Triple::TvOS:
304+
return llvm::MachO::PLATFORM_TVOS;
305+
case llvm::Triple::WatchOS:
306+
return llvm::MachO::PLATFORM_WATCHOS;
307+
default:
308+
return /*Unknown platform*/ 0;
309+
}
310+
}
311+
312+
llvm::Value *
313+
IRGenFunction::emitTargetOSVersionAtLeastCall(llvm::Value *major,
314+
llvm::Value *minor,
315+
llvm::Value *patch) {
316+
auto *fn = cast<llvm::Function>(IGM.getPlatformVersionAtLeastFn());
317+
318+
llvm::Value *platformID =
319+
llvm::ConstantInt::get(IGM.Int32Ty, getBaseMachOPlatformID(IGM.Triple));
320+
return Builder.CreateCall(fn, {platformID, major, minor, patch});
321+
}
292322

293323
/// Initialize a relative indirectable pointer to the given value.
294324
/// This always leaves the value in the direct state; if it's not a

lib/IRGen/IRGenFunction.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -295,6 +295,10 @@ class IRGenFunction {
295295

296296
void emitTSanInoutAccessCall(llvm::Value *address);
297297

298+
llvm::Value *emitTargetOSVersionAtLeastCall(llvm::Value *major,
299+
llvm::Value *minor,
300+
llvm::Value *patch);
301+
298302
llvm::Value *emitProjectBoxCall(llvm::Value *box, llvm::Value *typeMetadata);
299303

300304
llvm::Value *emitAllocEmptyBoxCall();

lib/SIL/IR/OperandOwnership.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -776,6 +776,7 @@ BUILTIN_OPERAND_OWNERSHIP(InstantaneousUse, Swift3ImplicitObjCEntrypoint)
776776
BUILTIN_OPERAND_OWNERSHIP(InstantaneousUse, PoundAssert)
777777
BUILTIN_OPERAND_OWNERSHIP(InstantaneousUse, GlobalStringTablePointer)
778778
BUILTIN_OPERAND_OWNERSHIP(InstantaneousUse, TypePtrAuthDiscriminator)
779+
BUILTIN_OPERAND_OWNERSHIP(InstantaneousUse, TargetOSVersionAtLeast)
779780
BUILTIN_OPERAND_OWNERSHIP(InstantaneousUse, IntInstrprofIncrement)
780781
BUILTIN_OPERAND_OWNERSHIP(InstantaneousUse, Move)
781782
BUILTIN_OPERAND_OWNERSHIP(UnownedInstantaneousUse, Copy)

lib/SIL/IR/ValueOwnership.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -534,6 +534,7 @@ CONSTANT_OWNERSHIP_BUILTIN(None, Swift3ImplicitObjCEntrypoint)
534534
CONSTANT_OWNERSHIP_BUILTIN(None, PoundAssert)
535535
CONSTANT_OWNERSHIP_BUILTIN(None, TypePtrAuthDiscriminator)
536536
CONSTANT_OWNERSHIP_BUILTIN(None, IntInstrprofIncrement)
537+
CONSTANT_OWNERSHIP_BUILTIN(None, TargetOSVersionAtLeast)
537538
CONSTANT_OWNERSHIP_BUILTIN(None, GlobalStringTablePointer)
538539
CONSTANT_OWNERSHIP_BUILTIN(None, GetCurrentAsyncTask)
539540
CONSTANT_OWNERSHIP_BUILTIN(None, CancelAsyncTask)

lib/SILOptimizer/Transforms/AccessEnforcementReleaseSinking.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,7 @@ static bool isBarrier(SILInstruction *inst) {
138138
case BuiltinValueKind::CondFailMessage:
139139
case BuiltinValueKind::PoundAssert:
140140
case BuiltinValueKind::TypePtrAuthDiscriminator:
141+
case BuiltinValueKind::TargetOSVersionAtLeast:
141142
case BuiltinValueKind::GlobalStringTablePointer:
142143
case BuiltinValueKind::COWBufferForReading:
143144
case BuiltinValueKind::IntInstrprofIncrement:

test/stdlib/Runtime.swift.gyb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -747,9 +747,12 @@ AvailabilityVersionsTestSuite.test("_stdlib_isOSVersionAtLeast") {
747747
// This test assumes that no version component on an OS we test upon
748748
// will ever be greater than 1066 and that every major version will always
749749
// be greater than 1.
750+
751+
#if !targetEnvironment(macCatalyst)
750752
expectFalse(isAtLeastOS(1066, 0, 0))
751753
expectTrue(isAtLeastOS(0, 1066, 0))
752754
expectTrue(isAtLeastOS(0, 0, 1066))
755+
#endif
753756

754757
// When using a runtime that's not part of the OS, 9999 is a special version
755758
// that's always available. The _swift_classIsSwiftMask symbol is absent in OS

0 commit comments

Comments
 (0)