Skip to content

Commit 4a01d54

Browse files
Merge pull request #25528 from aschwaighofer/back_deploy_dynamic_replacement-5.1
[5.1] stdlib: Add backward deployment versions for the
2 parents b2758ca + eea9e00 commit 4a01d54

File tree

23 files changed

+245
-67
lines changed

23 files changed

+245
-67
lines changed

cmake/modules/SwiftSource.cmake

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -274,6 +274,7 @@ function(_compile_swift_files
274274
# into the new runtime.
275275
if (SWIFTFILE_IS_STDLIB OR SWIFTFILE_IS_SDK_OVERLAY)
276276
list(APPEND swift_flags "-runtime-compatibility-version" "none")
277+
list(APPEND swift_flags "-disable-autolinking-runtime-compatibility-dynamic-replacements")
277278
endif()
278279

279280
if (SWIFTFILE_IS_STDLIB_CORE OR SWIFTFILE_IS_SDK_OVERLAY)

include/swift/AST/ASTContext.h

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

601+
/// Get the runtime availability of features introduced in the Swift 5.1
602+
/// compiler for the target platform.
603+
AvailabilityContext getSwift51Availability();
604+
601605
//===--------------------------------------------------------------------===//
602606
// Diagnostics Helper functions
603607
//===--------------------------------------------------------------------===//

include/swift/AST/IRGenOptions.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -228,6 +228,7 @@ class IRGenOptions {
228228

229229
/// Pull in runtime compatibility shim libraries by autolinking.
230230
Optional<llvm::VersionTuple> AutolinkRuntimeCompatibilityLibraryVersion;
231+
Optional<llvm::VersionTuple> AutolinkRuntimeCompatibilityDynamicReplacementLibraryVersion;
231232

232233
IRGenOptions()
233234
: DWARFVersion(2), OutputKind(IRGenOutputKind::LLVMAssembly),

include/swift/Option/Options.td

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -892,4 +892,10 @@ def disable_autolinking_runtime_compatibility : Flag<["-"], "disable-autolinking
892892
Flags<[FrontendOption]>,
893893
HelpText<"Do not use autolinking for runtime compatibility libraries">;
894894

895+
def disable_autolinking_runtime_compatibility_dynamic_replacements
896+
: Flag<[ "-" ], "disable-autolinking-runtime-compatibility-dynamic-replacements">,
897+
Flags<[ FrontendOption ]>,
898+
HelpText<"Do not use autolinking for the dynamic replacement runtime "
899+
"compatibility library">;
900+
895901
include "FrontendOptions.td"

include/swift/Runtime/Exclusivity.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,13 +44,19 @@ void swift_beginAccess(void *pointer, ValueBuffer *buffer,
4444
/// replacement function if it should be called.
4545
/// Returns null if the original function (which is passed in \p CurrFn) should
4646
/// be called.
47+
#ifdef __APPLE__
48+
__attribute__((weak_import))
49+
#endif
4750
SWIFT_RUNTIME_EXPORT
4851
char *swift_getFunctionReplacement(char **ReplFnPtr, char *CurrFn);
4952

5053
/// Returns the original function of a replaced function, which is loaded from
5154
/// \p OrigFnPtr.
5255
/// This function is called from a replacement function to call the original
5356
/// function.
57+
#ifdef __APPLE__
58+
__attribute__((weak_import))
59+
#endif
5460
SWIFT_RUNTIME_EXPORT
5561
char *swift_getOrigOfReplaceable(char **OrigFnPtr);
5662

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: 6 additions & 4 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))
@@ -1225,12 +1225,14 @@ FUNCTION(EndAccess, swift_endAccess, C_CC, AlwaysAvailable,
12251225
ARGS(getFixedBufferTy()->getPointerTo()),
12261226
ATTRS(NoUnwind))
12271227

1228-
FUNCTION(GetOrigOfReplaceable, swift_getOrigOfReplaceable, C_CC, AlwaysAvailable,
1228+
FUNCTION(GetOrigOfReplaceable, swift_getOrigOfReplaceable, C_CC,
1229+
AvailableByCompatibilityLibrary,
12291230
RETURNS(FunctionPtrTy),
12301231
ARGS(FunctionPtrTy->getPointerTo()),
12311232
ATTRS(NoUnwind))
12321233

1233-
FUNCTION(GetReplacement, swift_getFunctionReplacement, C_CC, AlwaysAvailable,
1234+
FUNCTION(GetReplacement, swift_getFunctionReplacement, C_CC,
1235+
AvailableByCompatibilityLibrary,
12341236
RETURNS(FunctionPtrTy),
12351237
ARGS(FunctionPtrTy->getPointerTo(), FunctionPtrTy),
12361238
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/Driver/DarwinToolChains.cpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -434,6 +434,22 @@ toolchains::Darwin::constructInvocation(const LinkJobAction &job,
434434
}
435435
}
436436

437+
if (job.getKind() == LinkKind::Executable) {
438+
if (runtimeCompatibilityVersion)
439+
if (*runtimeCompatibilityVersion <= llvm::VersionTuple(5, 0)) {
440+
// Swift 5.0 dynamic replacement compatibility library.
441+
SmallString<128> BackDeployLib;
442+
BackDeployLib.append(RuntimeLibPath);
443+
llvm::sys::path::append(BackDeployLib,
444+
"libswiftCompatibilityDynamicReplacements.a");
445+
446+
if (llvm::sys::fs::exists(BackDeployLib)) {
447+
Arguments.push_back("-force_load");
448+
Arguments.push_back(context.Args.MakeArgString(BackDeployLib));
449+
}
450+
}
451+
}
452+
437453
// Link the standard library.
438454
Arguments.push_back("-L");
439455
if (context.Args.hasFlag(options::OPT_static_stdlib,

lib/Driver/ToolChains.cpp

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -401,7 +401,12 @@ ToolChain::constructInvocation(const CompileJobAction &job,
401401
Arguments.push_back("-runtime-compatibility-version");
402402
Arguments.push_back(arg->getValue());
403403
}
404-
404+
405+
context.Args.AddLastArg(
406+
Arguments,
407+
options::
408+
OPT_disable_autolinking_runtime_compatibility_dynamic_replacements);
409+
405410
return II;
406411
}
407412

lib/Frontend/CompilerInvocation.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1180,6 +1180,11 @@ static bool ParseIRGenArgs(IRGenOptions &Opts, ArgList &Args,
11801180
runtimeCompatibilityVersion;
11811181
}
11821182

1183+
if (!Args.hasArg(options::
1184+
OPT_disable_autolinking_runtime_compatibility_dynamic_replacements)) {
1185+
Opts.AutolinkRuntimeCompatibilityDynamicReplacementLibraryVersion =
1186+
getSwiftRuntimeCompatibilityVersionForTarget(Triple);
1187+
}
11831188
return false;
11841189
}
11851190

lib/IRGen/GenDecl.cpp

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -468,6 +468,15 @@ void IRGenModule::emitSourceFile(SourceFile &SF) {
468468
/*forceLoad*/ true));
469469
}
470470
}
471+
472+
if (auto compatibilityVersion =
473+
IRGen.Opts.AutolinkRuntimeCompatibilityDynamicReplacementLibraryVersion) {
474+
if (*compatibilityVersion <= llvm::VersionTuple(5, 0)) {
475+
this->addLinkLibrary(LinkLibrary("swiftCompatibilityDynamicReplacements",
476+
LibraryKind::Library,
477+
/*forceLoad*/ true));
478+
}
479+
}
471480
}
472481
}
473482

@@ -2238,8 +2247,8 @@ void IRGenModule::createReplaceableProlog(IRGenFunction &IGF, SILFunction *f) {
22382247
rhs = FnAddr;
22392248
} else {
22402249
// Call swift_getFunctionReplacement to check which function to call.
2241-
auto *callRTFunc = IGF.Builder.CreateCall(getGetReplacementFn(),
2242-
{ ReplAddr, FnAddr });
2250+
auto *callRTFunc =
2251+
IGF.Builder.CreateCall(getGetReplacementFn(), {ReplAddr, FnAddr});
22432252
callRTFunc->setDoesNotThrow();
22442253
ReplFn = callRTFunc;
22452254
rhs = llvm::ConstantExpr::getNullValue(ReplFn->getType());
@@ -2409,8 +2418,8 @@ void IRGenModule::emitDynamicReplacementOriginalFunctionThunk(SILFunction *f) {
24092418
llvm::ConstantExpr::getInBoundsGetElementPtr(nullptr, linkEntry, indices),
24102419
FunctionPtrTy->getPointerTo());
24112420

2412-
auto *OrigFn = B.CreateCall(getGetOrigOfReplaceableFn(),
2413-
{ fnPtrAddr });
2421+
auto *OrigFn = B.CreateCall(getGetOrigOfReplaceableFn(), {fnPtrAddr});
2422+
24142423
OrigFn->setDoesNotThrow();
24152424

24162425
auto *typeFnPtr =

lib/IRGen/IRGenModule.cpp

Lines changed: 37 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -539,18 +539,12 @@ namespace RuntimeConstants {
539539
const auto NoUnwind = llvm::Attribute::NoUnwind;
540540
const auto ZExt = llvm::Attribute::ZExt;
541541
const auto FirstParamReturned = llvm::Attribute::Returned;
542-
543-
bool AlwaysAvailable(ASTContext &Context) {
544-
return false;
545-
}
546-
547-
bool OpaqueTypeAvailability(ASTContext &Context) {
548-
auto deploymentAvailability =
549-
AvailabilityContext::forDeploymentTarget(Context);
550-
auto featureAvailability = Context.getOpaqueTypeAvailability();
551-
552-
return !deploymentAvailability.isContainedIn(featureAvailability);
553-
}
542+
543+
const auto AlwaysAvailable = RuntimeAvailability::AlwaysAvailable;
544+
const auto AvailableByCompatibilityLibrary =
545+
RuntimeAvailability::AvailableByCompatibilityLibrary;
546+
const auto ConditionallyAvailable =
547+
RuntimeAvailability::ConditionallyAvailable;
554548
} // namespace RuntimeConstants
555549

556550
// We don't use enough attributes to justify generalizing the
@@ -594,13 +588,40 @@ llvm::Constant *swift::getRuntimeFn(llvm::Module &Module,
594588
llvm::Constant *&cache,
595589
const char *name,
596590
llvm::CallingConv::ID cc,
597-
bool isWeakLinked,
591+
RuntimeAvailability availability,
592+
ASTContext *context,
598593
llvm::ArrayRef<llvm::Type*> retTypes,
599594
llvm::ArrayRef<llvm::Type*> argTypes,
600595
ArrayRef<Attribute::AttrKind> attrs) {
596+
601597
if (cache)
602598
return cache;
603-
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+
604625
llvm::Type *retTy;
605626
if (retTypes.size() == 1)
606627
retTy = *retTypes.begin();
@@ -612,7 +633,7 @@ llvm::Constant *swift::getRuntimeFn(llvm::Module &Module,
612633
{argTypes.begin(), argTypes.end()},
613634
/*isVararg*/ false);
614635

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

617638
// Add any function attributes and set the calling convention.
618639
if (auto fn = dyn_cast<llvm::Function>(cache)) {
@@ -667,7 +688,7 @@ llvm::Constant *swift::getRuntimeFn(llvm::Module &Module,
667688
llvm::Constant *IRGenModule::get##ID##Fn() { \
668689
using namespace RuntimeConstants; \
669690
return getRuntimeFn(Module, ID##Fn, #NAME, CC, \
670-
(AVAILABILITY)(this->Context), \
691+
AVAILABILITY, &this->Context, \
671692
RETURNS, ARGS, ATTRS); \
672693
}
673694

0 commit comments

Comments
 (0)