Skip to content

Commit 85bfbe2

Browse files
Merge pull request swiftlang#25473 from aschwaighofer/back_deploy_dynamic_replacement
stdlib: Add backward deployment versions for the dynamic-replacement runtime functions
2 parents 4d6a18a + a9c83e7 commit 85bfbe2

File tree

23 files changed

+246
-67
lines changed

23 files changed

+246
-67
lines changed

cmake/modules/SwiftSource.cmake

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -269,6 +269,7 @@ function(_compile_swift_files
269269
# into the new runtime.
270270
if (SWIFTFILE_IS_STDLIB OR SWIFTFILE_IS_SDK_OVERLAY)
271271
list(APPEND swift_flags "-runtime-compatibility-version" "none")
272+
list(APPEND swift_flags "-disable-autolinking-runtime-compatibility-dynamic-replacements")
272273
endif()
273274

274275
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
@@ -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/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
@@ -912,4 +912,10 @@ def disable_autolinking_runtime_compatibility : Flag<["-"], "disable-autolinking
912912
Flags<[FrontendOption]>,
913913
HelpText<"Do not use autolinking for runtime compatibility libraries">;
914914

915+
def disable_autolinking_runtime_compatibility_dynamic_replacements
916+
: Flag<[ "-" ], "disable-autolinking-runtime-compatibility-dynamic-replacements">,
917+
Flags<[ FrontendOption ]>,
918+
HelpText<"Do not use autolinking for the dynamic replacement runtime "
919+
"compatibility library">;
920+
915921
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
@@ -427,6 +427,22 @@ toolchains::Darwin::constructInvocation(const LinkJobAction &job,
427427
}
428428
}
429429

430+
if (job.getKind() == LinkKind::Executable) {
431+
if (runtimeCompatibilityVersion)
432+
if (*runtimeCompatibilityVersion <= llvm::VersionTuple(5, 0)) {
433+
// Swift 5.0 dynamic replacement compatibility library.
434+
SmallString<128> BackDeployLib;
435+
BackDeployLib.append(RuntimeLibPath);
436+
llvm::sys::path::append(BackDeployLib,
437+
"libswiftCompatibilityDynamicReplacements.a");
438+
439+
if (llvm::sys::fs::exists(BackDeployLib)) {
440+
Arguments.push_back("-force_load");
441+
Arguments.push_back(context.Args.MakeArgString(BackDeployLib));
442+
}
443+
}
444+
}
445+
430446
// Link the standard library.
431447
Arguments.push_back("-L");
432448
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
@@ -445,7 +445,12 @@ ToolChain::constructInvocation(const CompileJobAction &job,
445445
Arguments.push_back("-runtime-compatibility-version");
446446
Arguments.push_back(arg->getValue());
447447
}
448-
448+
449+
context.Args.AddLastArg(
450+
Arguments,
451+
options::
452+
OPT_disable_autolinking_runtime_compatibility_dynamic_replacements);
453+
449454
return II;
450455
}
451456

0 commit comments

Comments
 (0)