Skip to content

Commit eb32194

Browse files
committed
Refactor to use RuntimeAvailability
1 parent f29b77f commit eb32194

File tree

8 files changed

+91
-101
lines changed

8 files changed

+91
-101
lines changed

include/swift/AST/ASTContext.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -590,6 +590,10 @@ class ASTContext final {
590590
/// Get the runtime availability of the opaque types language feature for the target platform.
591591
AvailabilityContext getOpaqueTypeAvailability();
592592

593+
/// Get the runtime availability of features introduced in the Swift 5.1
594+
/// compiler for the target platform.
595+
AvailabilityContext getSwift51Availability();
596+
593597
//===--------------------------------------------------------------------===//
594598
// Diagnostics Helper functions
595599
//===--------------------------------------------------------------------===//

include/swift/Runtime/RuntimeFnWrappersGen.h

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,13 @@
2222
namespace swift {
2323

2424
class AvailabilityContext;
25+
class ASTContext;
26+
27+
enum class RuntimeAvailability {
28+
AlwaysAvailable,
29+
AvailableByCompatibilityLibrary,
30+
ConditionallyAvailable
31+
};
2532

2633
/// Generate an llvm declaration for a runtime entry with a
2734
/// given name, return types, argument types, attributes and
@@ -30,7 +37,8 @@ llvm::Constant *getRuntimeFn(llvm::Module &Module,
3037
llvm::Constant *&cache,
3138
char const *name,
3239
llvm::CallingConv::ID cc,
33-
bool isWeakLinked,
40+
RuntimeAvailability availability,
41+
ASTContext *context,
3442
llvm::ArrayRef<llvm::Type*> retTypes,
3543
llvm::ArrayRef<llvm::Type*> argTypes,
3644
llvm::ArrayRef<llvm::Attribute::AttrKind> attrs);

include/swift/Runtime/RuntimeFunctions.def

Lines changed: 4 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -638,7 +638,7 @@ FUNCTION(GetGenericMetadata, swift_getGenericMetadata,
638638
// const OpaqueTypeDescriptor *descriptor,
639639
// uintptr_t index);
640640
FUNCTION(GetOpaqueTypeMetadata, swift_getOpaqueTypeMetadata,
641-
SwiftCC, OpaqueTypeAvailability,
641+
SwiftCC, ConditionallyAvailable,
642642
RETURNS(TypeMetadataResponseTy),
643643
ARGS(SizeTy, Int8PtrTy, OpaqueTypeDescriptorPtrTy, SizeTy),
644644
ATTRS(NoUnwind, ReadOnly))
@@ -647,7 +647,7 @@ FUNCTION(GetOpaqueTypeMetadata, swift_getOpaqueTypeMetadata,
647647
// const OpaqueTypeDescriptor *descriptor,
648648
// uintptr_t index);
649649
FUNCTION(GetOpaqueTypeConformance, swift_getOpaqueTypeConformance,
650-
SwiftCC, OpaqueTypeAvailability,
650+
SwiftCC, ConditionallyAvailable,
651651
RETURNS(WitnessTablePtrTy),
652652
ARGS(Int8PtrTy, OpaqueTypeDescriptorPtrTy, SizeTy),
653653
ATTRS(NoUnwind, ReadOnly))
@@ -1226,25 +1226,13 @@ FUNCTION(EndAccess, swift_endAccess, C_CC, AlwaysAvailable,
12261226
ATTRS(NoUnwind))
12271227

12281228
FUNCTION(GetOrigOfReplaceable, swift_getOrigOfReplaceable, C_CC,
1229-
GetReplacementAvailability,
1229+
AvailableByCompatibilityLibrary,
12301230
RETURNS(FunctionPtrTy),
12311231
ARGS(FunctionPtrTy->getPointerTo()),
12321232
ATTRS(NoUnwind))
12331233

12341234
FUNCTION(GetReplacement, swift_getFunctionReplacement, C_CC,
1235-
GetReplacementAvailability,
1236-
RETURNS(FunctionPtrTy),
1237-
ARGS(FunctionPtrTy->getPointerTo(), FunctionPtrTy),
1238-
ATTRS(NoUnwind))
1239-
1240-
FUNCTION(GetOrigOfReplaceable50, swift_getOrigOfReplaceable50, C_CC,
1241-
AlwaysAvailable,
1242-
RETURNS(FunctionPtrTy),
1243-
ARGS(FunctionPtrTy->getPointerTo()),
1244-
ATTRS(NoUnwind))
1245-
1246-
FUNCTION(GetReplacement50, swift_getFunctionReplacement50, C_CC,
1247-
AlwaysAvailable,
1235+
AvailableByCompatibilityLibrary,
12481236
RETURNS(FunctionPtrTy),
12491237
ARGS(FunctionPtrTy->getPointerTo(), FunctionPtrTy),
12501238
ATTRS(NoUnwind))

lib/AST/Availability.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,10 @@ AvailabilityContext AvailabilityInference::inferForType(Type t) {
218218
}
219219

220220
AvailabilityContext ASTContext::getOpaqueTypeAvailability() {
221+
return getSwift51Availability();
222+
}
223+
224+
AvailabilityContext ASTContext::getSwift51Availability() {
221225
auto target = LangOpts.Target;
222226

223227
if (target.isMacOSX()) {

lib/IRGen/GenDecl.cpp

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2239,12 +2239,8 @@ void IRGenModule::createReplaceableProlog(IRGenFunction &IGF, SILFunction *f) {
22392239
rhs = FnAddr;
22402240
} else {
22412241
// Call swift_getFunctionReplacement to check which function to call.
2242-
auto *getReplacementFn = IRGenModule::isGetReplacementAvailable(Context)
2243-
? getGetReplacementFn()
2244-
: getGetReplacement50Fn();
2245-
22462242
auto *callRTFunc =
2247-
IGF.Builder.CreateCall(getReplacementFn, {ReplAddr, FnAddr});
2243+
IGF.Builder.CreateCall(getGetReplacementFn(), {ReplAddr, FnAddr});
22482244
callRTFunc->setDoesNotThrow();
22492245
ReplFn = callRTFunc;
22502246
rhs = llvm::ConstantExpr::getNullValue(ReplFn->getType());
@@ -2412,10 +2408,8 @@ void IRGenModule::emitDynamicReplacementOriginalFunctionThunk(SILFunction *f) {
24122408
llvm::ConstantExpr::getInBoundsGetElementPtr(nullptr, linkEntry, indices),
24132409
FunctionPtrTy->getPointerTo());
24142410

2415-
auto *getOrigOfReplaceableFn = IRGenModule::isGetReplacementAvailable(Context)
2416-
? getGetOrigOfReplaceableFn()
2417-
: getGetOrigOfReplaceable50Fn();
2418-
auto *OrigFn = IGF.Builder.CreateCall(getOrigOfReplaceableFn, {fnPtrAddr});
2411+
auto *OrigFn =
2412+
IGF.Builder.CreateCall(getGetOrigOfReplaceableFn(), {fnPtrAddr});
24192413

24202414
OrigFn->setDoesNotThrow();
24212415

lib/IRGen/IRGenModule.cpp

Lines changed: 36 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -527,31 +527,6 @@ IRGenModule::~IRGenModule() {
527527

528528
static bool isReturnAttribute(llvm::Attribute::AttrKind Attr);
529529

530-
static AvailabilityContext
531-
getGetReplacementAvailability(ASTContext &context) {
532-
auto target = context.LangOpts.Target;
533-
534-
if (target.isMacOSX()) {
535-
return AvailabilityContext(
536-
VersionRange::allGTE(llvm::VersionTuple(10, 15, 0)));
537-
} else if (target.isiOS()) {
538-
return AvailabilityContext(
539-
VersionRange::allGTE(llvm::VersionTuple(13, 0, 0)));
540-
} else if (target.isWatchOS()) {
541-
return AvailabilityContext(
542-
VersionRange::allGTE(llvm::VersionTuple(6, 0, 0)));
543-
} else {
544-
return AvailabilityContext::alwaysAvailable();
545-
}
546-
}
547-
548-
bool IRGenModule::isGetReplacementAvailable(ASTContext &Context) {
549-
auto deploymentAvailability =
550-
AvailabilityContext::forDeploymentTarget(Context);
551-
auto featureAvailability = getGetReplacementAvailability(Context);
552-
return deploymentAvailability.isContainedIn(featureAvailability);
553-
}
554-
555530
// Explicitly listing these constants is an unfortunate compromise for
556531
// making the database file much more compact.
557532
//
@@ -564,22 +539,12 @@ namespace RuntimeConstants {
564539
const auto NoUnwind = llvm::Attribute::NoUnwind;
565540
const auto ZExt = llvm::Attribute::ZExt;
566541
const auto FirstParamReturned = llvm::Attribute::Returned;
567-
568-
bool AlwaysAvailable(ASTContext &Context) {
569-
return false;
570-
}
571-
572-
bool OpaqueTypeAvailability(ASTContext &Context) {
573-
auto deploymentAvailability =
574-
AvailabilityContext::forDeploymentTarget(Context);
575-
auto featureAvailability = Context.getOpaqueTypeAvailability();
576-
577-
return !deploymentAvailability.isContainedIn(featureAvailability);
578-
}
579542

580-
bool GetReplacementAvailability(ASTContext &Context) {
581-
return !IRGenModule::isGetReplacementAvailable(Context);
582-
}
543+
const auto AlwaysAvailable = RuntimeAvailability::AlwaysAvailable;
544+
const auto AvailableByCompatibilityLibrary =
545+
RuntimeAvailability::AvailableByCompatibilityLibrary;
546+
const auto ConditionallyAvailable =
547+
RuntimeAvailability::ConditionallyAvailable;
583548
} // namespace RuntimeConstants
584549

585550
// We don't use enough attributes to justify generalizing the
@@ -623,13 +588,40 @@ llvm::Constant *swift::getRuntimeFn(llvm::Module &Module,
623588
llvm::Constant *&cache,
624589
const char *name,
625590
llvm::CallingConv::ID cc,
626-
bool isWeakLinked,
591+
RuntimeAvailability availability,
592+
ASTContext *context,
627593
llvm::ArrayRef<llvm::Type*> retTypes,
628594
llvm::ArrayRef<llvm::Type*> argTypes,
629595
ArrayRef<Attribute::AttrKind> attrs) {
596+
630597
if (cache)
631598
return cache;
632-
599+
600+
bool isWeakLinked = false;
601+
std::string functionName(name);
602+
603+
auto isFeatureAvailable = [&]() -> bool {
604+
auto deploymentAvailability =
605+
AvailabilityContext::forDeploymentTarget(*context);
606+
auto featureAvailability = context->getSwift51Availability();
607+
return deploymentAvailability.isContainedIn(featureAvailability);
608+
};
609+
610+
switch (availability) {
611+
case RuntimeAvailability::AlwaysAvailable:
612+
// Nothing to do.
613+
break;
614+
case RuntimeAvailability::ConditionallyAvailable: {
615+
isWeakLinked = !isFeatureAvailable();
616+
break;
617+
}
618+
case RuntimeAvailability::AvailableByCompatibilityLibrary: {
619+
if (!isFeatureAvailable())
620+
functionName.append("50");
621+
break;
622+
}
623+
}
624+
633625
llvm::Type *retTy;
634626
if (retTypes.size() == 1)
635627
retTy = *retTypes.begin();
@@ -641,7 +633,7 @@ llvm::Constant *swift::getRuntimeFn(llvm::Module &Module,
641633
{argTypes.begin(), argTypes.end()},
642634
/*isVararg*/ false);
643635

644-
cache = Module.getOrInsertFunction(name, fnTy);
636+
cache = Module.getOrInsertFunction(functionName.c_str(), fnTy);
645637

646638
// Add any function attributes and set the calling convention.
647639
if (auto fn = dyn_cast<llvm::Function>(cache)) {
@@ -696,7 +688,7 @@ llvm::Constant *swift::getRuntimeFn(llvm::Module &Module,
696688
llvm::Constant *IRGenModule::get##ID##Fn() { \
697689
using namespace RuntimeConstants; \
698690
return getRuntimeFn(Module, ID##Fn, #NAME, CC, \
699-
(AVAILABILITY)(this->Context), \
691+
AVAILABILITY, &this->Context, \
700692
RETURNS, ARGS, ATTRS); \
701693
}
702694

lib/IRGen/IRGenModule.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1441,7 +1441,6 @@ private: \
14411441

14421442
void emitOpaqueTypeDescriptorAccessor(OpaqueTypeDecl *);
14431443

1444-
static bool isGetReplacementAvailable(ASTContext &context);
14451444
private:
14461445
llvm::Constant *
14471446
getAddrOfSharedContextDescriptor(LinkEntity entity,

lib/LLVMPasses/ARCEntryPointBuilder.h

Lines changed: 31 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -220,8 +220,8 @@ class ARCEntryPointBuilder {
220220
Retain = getRuntimeFn(
221221
getModule(), cache,
222222
isNonAtomic(OrigI) ? "swift_nonatomic_retain" : "swift_retain",
223-
DefaultCC, false, {ObjectPtrTy}, {ObjectPtrTy},
224-
{NoUnwind, FirstParamReturned});
223+
DefaultCC, RuntimeAvailability::AlwaysAvailable, nullptr, {ObjectPtrTy},
224+
{ObjectPtrTy}, {NoUnwind, FirstParamReturned});
225225

226226
return Retain.get();
227227
}
@@ -234,10 +234,11 @@ class ARCEntryPointBuilder {
234234
auto *VoidTy = Type::getVoidTy(getModule().getContext());
235235

236236
llvm::Constant *cache = nullptr;
237-
Release = getRuntimeFn(
238-
getModule(), cache,
239-
isNonAtomic(OrigI) ? "swift_nonatomic_release" : "swift_release",
240-
DefaultCC, false, {VoidTy}, {ObjectPtrTy}, {NoUnwind});
237+
Release = getRuntimeFn(getModule(), cache,
238+
isNonAtomic(OrigI) ? "swift_nonatomic_release"
239+
: "swift_release",
240+
DefaultCC, RuntimeAvailability::AlwaysAvailable,
241+
nullptr, {VoidTy}, {ObjectPtrTy}, {NoUnwind});
241242

242243
return Release.get();
243244
}
@@ -272,8 +273,8 @@ class ARCEntryPointBuilder {
272273
RetainN = getRuntimeFn(
273274
getModule(), cache,
274275
isNonAtomic(OrigI) ? "swift_nonatomic_retain_n" : "swift_retain_n",
275-
DefaultCC, false, {ObjectPtrTy}, {ObjectPtrTy, Int32Ty},
276-
{NoUnwind, FirstParamReturned});
276+
DefaultCC, RuntimeAvailability::AlwaysAvailable, nullptr, {ObjectPtrTy},
277+
{ObjectPtrTy, Int32Ty}, {NoUnwind, FirstParamReturned});
277278

278279
return RetainN.get();
279280
}
@@ -290,7 +291,8 @@ class ARCEntryPointBuilder {
290291
ReleaseN = getRuntimeFn(
291292
getModule(), cache,
292293
isNonAtomic(OrigI) ? "swift_nonatomic_release_n" : "swift_release_n",
293-
DefaultCC, false, {VoidTy}, {ObjectPtrTy, Int32Ty}, {NoUnwind});
294+
DefaultCC, RuntimeAvailability::AlwaysAvailable, nullptr, {VoidTy},
295+
{ObjectPtrTy, Int32Ty}, {NoUnwind});
294296

295297
return ReleaseN.get();
296298
}
@@ -304,13 +306,12 @@ class ARCEntryPointBuilder {
304306
auto *Int32Ty = Type::getInt32Ty(getModule().getContext());
305307

306308
llvm::Constant *cache = nullptr;
307-
UnknownObjectRetainN =
308-
getRuntimeFn(getModule(), cache,
309-
isNonAtomic(OrigI)
310-
? "swift_nonatomic_unknownObjectRetain_n"
311-
: "swift_unknownObjectRetain_n",
312-
DefaultCC, false, {ObjectPtrTy}, {ObjectPtrTy, Int32Ty},
313-
{NoUnwind, FirstParamReturned});
309+
UnknownObjectRetainN = getRuntimeFn(
310+
getModule(), cache,
311+
isNonAtomic(OrigI) ? "swift_nonatomic_unknownObjectRetain_n"
312+
: "swift_unknownObjectRetain_n",
313+
DefaultCC, RuntimeAvailability::AlwaysAvailable, nullptr, {ObjectPtrTy},
314+
{ObjectPtrTy, Int32Ty}, {NoUnwind, FirstParamReturned});
314315

315316
return UnknownObjectRetainN.get();
316317
}
@@ -324,13 +325,12 @@ class ARCEntryPointBuilder {
324325
auto *VoidTy = Type::getVoidTy(getModule().getContext());
325326

326327
llvm::Constant *cache = nullptr;
327-
UnknownObjectReleaseN =
328-
getRuntimeFn(getModule(), cache,
329-
isNonAtomic(OrigI)
330-
? "swift_nonatomic_unknownObjectRelease_n"
331-
: "swift_unknownObjectRelease_n",
332-
DefaultCC, false,
333-
{VoidTy}, {ObjectPtrTy, Int32Ty}, {NoUnwind});
328+
UnknownObjectReleaseN = getRuntimeFn(
329+
getModule(), cache,
330+
isNonAtomic(OrigI) ? "swift_nonatomic_unknownObjectRelease_n"
331+
: "swift_unknownObjectRelease_n",
332+
DefaultCC, RuntimeAvailability::AlwaysAvailable, nullptr, {VoidTy},
333+
{ObjectPtrTy, Int32Ty}, {NoUnwind});
334334

335335
return UnknownObjectReleaseN.get();
336336
}
@@ -343,12 +343,12 @@ class ARCEntryPointBuilder {
343343
auto *Int32Ty = Type::getInt32Ty(getModule().getContext());
344344

345345
llvm::Constant *cache = nullptr;
346-
BridgeRetainN =
347-
getRuntimeFn(getModule(), cache,
348-
isNonAtomic(OrigI) ? "swift_nonatomic_bridgeObjectRetain_n"
349-
: "swift_bridgeObjectRetain_n",
350-
DefaultCC, false, {BridgeObjectPtrTy},
351-
{BridgeObjectPtrTy, Int32Ty}, {NoUnwind});
346+
BridgeRetainN = getRuntimeFn(
347+
getModule(), cache,
348+
isNonAtomic(OrigI) ? "swift_nonatomic_bridgeObjectRetain_n"
349+
: "swift_bridgeObjectRetain_n",
350+
DefaultCC, RuntimeAvailability::AlwaysAvailable, nullptr,
351+
{BridgeObjectPtrTy}, {BridgeObjectPtrTy, Int32Ty}, {NoUnwind});
352352
return BridgeRetainN.get();
353353
}
354354

@@ -366,7 +366,8 @@ class ARCEntryPointBuilder {
366366
getModule(), cache,
367367
isNonAtomic(OrigI) ? "swift_nonatomic_bridgeObjectRelease_n"
368368
: "swift_bridgeObjectRelease_n",
369-
DefaultCC, false, {VoidTy}, {BridgeObjectPtrTy, Int32Ty}, {NoUnwind});
369+
DefaultCC, RuntimeAvailability::AlwaysAvailable, nullptr, {VoidTy},
370+
{BridgeObjectPtrTy, Int32Ty}, {NoUnwind});
370371
return BridgeReleaseN.get();
371372
}
372373

0 commit comments

Comments
 (0)