Skip to content

Enable dynamic replacement with library evolution #30965

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

Merged
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
5 changes: 0 additions & 5 deletions include/swift/AST/DiagnosticsSema.def
Original file line number Diff line number Diff line change
Expand Up @@ -4460,11 +4460,6 @@ ERROR(dynamic_with_transparent,none,
"a declaration cannot be both '@_tranparent' and 'dynamic'",
())

ERROR(dynamic_and_library_evolution_not_supported,none,
"marking non-'@objc' Swift declaration 'dynamic' in library evolution mode is not supported",
())


//------------------------------------------------------------------------------
// MARK: @_dynamicReplacement(for:)
//------------------------------------------------------------------------------
Expand Down
2 changes: 2 additions & 0 deletions lib/IRGen/Linking.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -629,6 +629,8 @@ SILLinkage LinkEntity::getLinkage(ForDefinition_t forDefinition) const {
return SILLinkage::Private;

case Kind::DynamicallyReplaceableFunctionKey:
return getSILFunction()->getLinkage();
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@aschwaighofer This was the only actual change I had to make. Are you aware of anything that I might have missed?


case Kind::SILFunction:
return getSILFunction()->getEffectiveSymbolLinkage();

Expand Down
4 changes: 0 additions & 4 deletions lib/Sema/TypeCheckAttr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -347,10 +347,6 @@ void AttributeChecker::visitDynamicAttr(DynamicAttr *attr) {
// Members cannot be both dynamic and @_transparent.
if (D->getAttrs().hasAttribute<TransparentAttr>())
diagnoseAndRemoveAttr(attr, diag::dynamic_with_transparent);
if (!D->getAttrs().hasAttribute<ObjCAttr>() &&
D->getModuleContext()->isResilient())
diagnoseAndRemoveAttr(attr,
diag::dynamic_and_library_evolution_not_supported);
}

static bool
Expand Down
11 changes: 11 additions & 0 deletions test/IRGen/class_resilience_dynamic_replacement.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// RUN: %target-swift-frontend -emit-ir -enable-library-evolution %s | %FileCheck %s

public class ResilientClass {
public dynamic func resilientMethod() {}
}

// The dynamic replacement key should be public even though the method
// implementation is hidden.

// CHECK-LABEL: @"$s36class_resilience_dynamic_replacement14ResilientClassC15resilientMethodyyFTx" = {{(protected )?}}constant %swift.dyn_repl_key
// CHECK-LABEL: define hidden swiftcc void @"$s36class_resilience_dynamic_replacement14ResilientClassC15resilientMethodyyF"
5 changes: 5 additions & 0 deletions test/Interpreter/Inputs/dynamic_replacement_module.swift
Original file line number Diff line number Diff line change
Expand Up @@ -162,10 +162,15 @@ public func replacement_for_public_global_generic_func<T>(_ t: T.Type) -> String
}

extension PublicClass {

// Designated initializers of resilient classes cannot be
// dynamically replaced.
#if !EVOLUTION
@_dynamicReplacement(for: init(x:))
public init(y: Int) {
str = "replacement of public_class_init"
}
#endif

@_dynamicReplacement(for: function())
public func replacement_function() -> String {
Expand Down
135 changes: 98 additions & 37 deletions test/Interpreter/dynamic_replacement.swift
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,58 @@
// RUN: %target-codesign %t/main %t/%target-library-name(Module1) %t/%target-library-name(Module2)
// RUN: %target-run %t/main %t/%target-library-name(Module1) %t/%target-library-name(Module2)

///////////////////////////////////////////////////////////////////////////////
// Now repeat all of the same tests, but with library evolution enabled:
///////////////////////////////////////////////////////////////////////////////

// RUN: %empty-directory(%t)
// RUN: %target-build-swift-dylib(%t/%target-library-name(Module1)) -DMODULE -module-name Module1 -emit-module -emit-module-path %t/Module1.swiftmodule -swift-version 5 %S/Inputs/dynamic_replacement_module.swift -enable-library-evolution
// RUN: %target-build-swift-dylib(%t/%target-library-name(Module2)) -I%t -L%t -lModule1 %target-rpath(%t) -DMODULE2 -module-name Module2 -emit-module -emit-module-path %t/Module2.swiftmodule -swift-version 5 %S/Inputs/dynamic_replacement_module.swift -DEVOLUTION
// RUN: %target-build-swift -I%t -L%t -lModule1 -DMAIN -o %t/main %target-rpath(%t) %s -swift-version 5 -DEVOLUTION
// RUN: %target-codesign %t/main %t/%target-library-name(Module1) %t/%target-library-name(Module2)
// RUN: %target-run %t/main %t/%target-library-name(Module1) %t/%target-library-name(Module2)

// Now the same in optimized mode.
// RUN: %empty-directory(%t)
// RUN: %target-build-swift-dylib(%t/%target-library-name(Module1)) -O -DMODULE -module-name Module1 -emit-module -emit-module-path %t/Module1.swiftmodule -swift-version 5 %S/Inputs/dynamic_replacement_module.swift -enable-library-evolution
// RUN: %target-build-swift-dylib(%t/%target-library-name(Module2)) -O -I%t -L%t -lModule1 %target-rpath(%t) -DMODULE2 -module-name Module2 -emit-module -emit-module-path %t/Module2.swiftmodule -swift-version 5 %S/Inputs/dynamic_replacement_module.swift -DEVOLUTION
// RUN: %target-build-swift -O -I%t -L%t -lModule1 -DMAIN -o %t/main %target-rpath(%t) %s -swift-version 5 -DEVOLUTION
// RUN: %target-codesign %t/main %t/%target-library-name(Module1) %t/%target-library-name(Module2)
// RUN: %target-run %t/main %t/%target-library-name(Module1) %t/%target-library-name(Module2)

// Now the same in size mode.
// RUN: %empty-directory(%t)
// RUN: %target-build-swift-dylib(%t/%target-library-name(Module1)) -Osize -DMODULE -module-name Module1 -emit-module -emit-module-path %t/Module1.swiftmodule -swift-version 5 %S/Inputs/dynamic_replacement_module.swift -enable-library-evolution
// RUN: %target-build-swift-dylib(%t/%target-library-name(Module2)) -Osize -I%t -L%t -lModule1 %target-rpath(%t) -DMODULE2 -module-name Module2 -emit-module -emit-module-path %t/Module2.swiftmodule -swift-version 5 %S/Inputs/dynamic_replacement_module.swift -DEVOLUTION
// RUN: %target-build-swift -Osize -I%t -L%t -lModule1 -DMAIN -o %t/main %target-rpath(%t) %s -swift-version 5 -DEVOLUTION
// RUN: %target-codesign %t/main %t/%target-library-name(Module1) %t/%target-library-name(Module2)
// RUN: %target-run %t/main %t/%target-library-name(Module1) %t/%target-library-name(Module2)

// Now the same in optimized wholemodule mode.
// RUN: %empty-directory(%t)
// RUN: %target-build-swift-dylib(%t/%target-library-name(Module1)) -O -wmo -DMODULE -module-name Module1 -emit-module -emit-module-path %t/Module1.swiftmodule -swift-version 5 %S/Inputs/dynamic_replacement_module.swift -enable-library-evolution
// RUN: %target-build-swift-dylib(%t/%target-library-name(Module2)) -O -wmo -I%t -L%t -lModule1 %target-rpath(%t) -DMODULE2 -module-name Module2 -emit-module -emit-module-path %t/Module2.swiftmodule -swift-version 5 %S/Inputs/dynamic_replacement_module.swift -DEVOLUTION
// RUN: %target-build-swift -O -wmo -I%t -L%t -lModule1 -DMAIN -o %t/main %target-rpath(%t) %s -swift-version 5 -DEVOLUTION
// RUN: %target-codesign %t/main %t/%target-library-name(Module1) %t/%target-library-name(Module2)
// RUN: %target-run %t/main %t/%target-library-name(Module1) %t/%target-library-name(Module2)

// Test the -enable-implicit-dynamic flag.

// RUN: %empty-directory(%t)
// RUN: %target-build-swift-dylib(%t/%target-library-name(Module1)) -DMODULENODYNAMIC -module-name Module1 -emit-module -emit-module-path %t/Module1.swiftmodule -swift-version 5 %S/Inputs/dynamic_replacement_module.swift -Xfrontend -enable-implicit-dynamic -enable-library-evolution
// RUN: %target-build-swift-dylib(%t/%target-library-name(Module2)) -I%t -L%t -lModule1 %target-rpath(%t) -DMODULE2 -module-name Module2 -emit-module -emit-module-path %t/Module2.swiftmodule -swift-version 5 %S/Inputs/dynamic_replacement_module.swift -DEVOLUTION
// RUN: %target-build-swift -I%t -L%t -lModule1 -DMAIN -o %t/main %target-rpath(%t) %s -swift-version 5 -DEVOLUTION
// RUN: %target-codesign %t/main %t/%target-library-name(Module1) %t/%target-library-name(Module2)
// RUN: %target-run %t/main %t/%target-library-name(Module1) %t/%target-library-name(Module2)

// Test the -enable-implicit-dynamic flag in optimized wholemodule mode.
// RUN: %empty-directory(%t)
// RUN: %target-build-swift-dylib(%t/%target-library-name(Module1)) -O -wmo -DMODULENODYNAMIC -module-name Module1 -emit-module -emit-module-path %t/Module1.swiftmodule -swift-version 5 %S/Inputs/dynamic_replacement_module.swift -Xfrontend -enable-implicit-dynamic -enable-library-evolution
// RUN: %target-build-swift-dylib(%t/%target-library-name(Module2)) -O -wmo -I%t -L%t -lModule1 %target-rpath(%t) -DMODULE2 -module-name Module2 -emit-module -emit-module-path %t/Module2.swiftmodule -swift-version 5 %S/Inputs/dynamic_replacement_module.swift -DEVOLUTION
// RUN: %target-build-swift -O -wmo -I%t -L%t -lModule1 -DMAIN -o %t/main %target-rpath(%t) %s -swift-version 5 -DEVOLUTION
// RUN: %target-codesign %t/main %t/%target-library-name(Module1) %t/%target-library-name(Module2)
// RUN: %target-run %t/main %t/%target-library-name(Module1) %t/%target-library-name(Module2)


// REQUIRES: executable_test

Expand Down Expand Up @@ -75,42 +127,51 @@ func expectedResult(_ forOriginalLibrary: Bool, _ expectedOriginalString: Strin
}

func checkExpectedResults(forOriginalLibrary useOrig: Bool) {
expectTrue(public_global_var == expectedResult(useOrig, "public_global_var"))

expectTrue(public_global_func() ==
expectedResult(useOrig, "public_global_func"))
expectTrue(public_global_generic_func(Int.self) ==
expectedResult(useOrig, "public_global_generic_func"))

expectTrue(PublicClass().function() ==
expectedResult(useOrig, "public_class_func"))
expectTrue(PublicClass().finalFunction() ==
expectedResult(useOrig, "public_class_final_func"))
expectTrue(PublicClass().genericFunction(Int.self) ==
expectedResult(useOrig, "public_class_generic_func"))

expectTrue(PublicStruct().function() ==
expectedResult(useOrig, "public_struct_func"))
expectTrue(PublicStruct().genericFunction(Int.self) ==
expectedResult(useOrig, "public_struct_generic_func"))
expectTrue(PublicStruct().public_stored_property ==
expectedResult(useOrig, "public_stored_property"))
expectTrue(PublicStruct()[0] ==
expectedResult(useOrig, "public_subscript_get"))
expectTrue(PublicStruct()[y:0] ==
expectedResult(useOrig, "public_subscript_get_modify_read"))
var testSetter = PublicStruct()
testSetter[0] = "public_subscript_set"
expectTrue(testSetter.str ==
expectedResult(useOrig, "public_subscript_set"))
testSetter[y:0] = "public_subscript_set_modify_read"
expectTrue(testSetter.str ==
expectedResult(useOrig, "public_subscript_set_modify_read"))

expectTrue(PublicEnumeration<Int>.A.function() ==
expectedResult(useOrig, "public_enum_func"))
expectTrue(PublicEnumeration<Int>.B.genericFunction(Int.self) ==
expectedResult(useOrig, "public_enum_generic_func"))
expectTrue(public_global_var == expectedResult(useOrig, "public_global_var"))

expectTrue(public_global_func() ==
expectedResult(useOrig, "public_global_func"))
expectTrue(public_global_generic_func(Int.self) ==
expectedResult(useOrig, "public_global_generic_func"))

// Designated initializers of resilient classes cannot be
// dynamically replaced.
#if !EVOLUTION
expectTrue(PublicClass(x: 0).str ==
expectedResult(useOrig, "public_class_init"))
#endif

expectTrue(PublicClass().function() ==
expectedResult(useOrig, "public_class_func"))
expectTrue(PublicClass().finalFunction() ==
expectedResult(useOrig, "public_class_final_func"))
expectTrue(PublicClass().genericFunction(Int.self) ==
expectedResult(useOrig, "public_class_generic_func"))

expectTrue(PublicStruct(x: 0).str ==
expectedResult(useOrig, "public_struct_init"))
expectTrue(PublicStruct().function() ==
expectedResult(useOrig, "public_struct_func"))
expectTrue(PublicStruct().genericFunction(Int.self) ==
expectedResult(useOrig, "public_struct_generic_func"))
expectTrue(PublicStruct().public_stored_property ==
expectedResult(useOrig, "public_stored_property"))
expectTrue(PublicStruct()[0] ==
expectedResult(useOrig, "public_subscript_get"))
expectTrue(PublicStruct()[y:0] ==
expectedResult(useOrig, "public_subscript_get_modify_read"))
var testSetter = PublicStruct()
testSetter[0] = "public_subscript_set"
expectTrue(testSetter.str ==
expectedResult(useOrig, "public_subscript_set"))
testSetter[y:0] = "public_subscript_set_modify_read"
expectTrue(testSetter.str ==
expectedResult(useOrig, "public_subscript_set_modify_read"))

expectTrue(PublicEnumeration<Int>.A.function() ==
expectedResult(useOrig, "public_enum_func"))
expectTrue(PublicEnumeration<Int>.B.genericFunction(Int.self) ==
expectedResult(useOrig, "public_enum_generic_func"))
}

private func target_library_name(_ name: String) -> String {
Expand All @@ -135,7 +196,7 @@ DynamicallyReplaceable.test("DynamicallyReplaceable") {
#if os(Linux)
_ = dlopen(target_library_name("Module2"), RTLD_NOW)
#elseif os(Windows)
_ = LoadLibraryA(target_library_name("Module2"))
_ = LoadLibraryA(target_library_name("Module2"))
#else
_ = dlopen(executablePath+target_library_name("Module2"), RTLD_NOW)
#endif
Expand Down
12 changes: 6 additions & 6 deletions test/Interpreter/dynamic_replacement_chaining.swift
Original file line number Diff line number Diff line change
Expand Up @@ -53,14 +53,14 @@ DynamicallyReplaceable.test("DynamicallyReplaceable") {
executablePath.removeLast(4)

#if os(Linux)
_ = dlopen(target_library_name("B"), RTLD_NOW)
_ = dlopen(target_library_name("C"), RTLD_NOW)
_ = dlopen(target_library_name("B"), RTLD_NOW)
_ = dlopen(target_library_name("C"), RTLD_NOW)
#elseif os(Windows)
_ = LoadLibraryA(target_library_name("B"))
_ = LoadLibraryA(target_library_name("C"))
_ = LoadLibraryA(target_library_name("B"))
_ = LoadLibraryA(target_library_name("C"))
#else
_ = dlopen(executablePath+target_library_name("B"), RTLD_NOW)
_ = dlopen(executablePath+target_library_name("C"), RTLD_NOW)
_ = dlopen(executablePath+target_library_name("B"), RTLD_NOW)
_ = dlopen(executablePath+target_library_name("C"), RTLD_NOW)
#endif

#if CHAINING
Expand Down
4 changes: 4 additions & 0 deletions test/Interpreter/dynamic_replacement_opaque_result2.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@
// RUN: %target-codesign %t/main
// RUN: %target-run %t/main | %FileCheck %s

// RUN: %target-build-swift -o %t/main %target-rpath(%t) %s -swift-version 5 -enable-library-evolution
// RUN: %target-codesign %t/main
// RUN: %target-run %t/main | %FileCheck %s

// REQUIRES: executable_test
// REQUIRES: swift_test_mode_optimize_none
// REQUIRES: CPU=arm64 || CPU=arm64e || CPU=x86_64
Expand Down
6 changes: 6 additions & 0 deletions test/Interpreter/dynamic_replacement_protocol_self.swift
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
// RUN: %empty-directory(%t)

// RUN: %target-build-swift-dylib(%t/%target-library-name(TestModuleLinking)) -module-name TestModuleLinking -emit-module -emit-module-path %t/TestModuleLinking.swiftmodule -swift-version 5 %S/Inputs/dynamic_replacement_protocol_self_orig.swift -Xfrontend -enable-private-imports -Xfrontend -enable-implicit-dynamic
// RUN: %target-build-swift -I%t -L%t -lTestModuleLinking -o %t/main %target-rpath(%t) %s -swift-version 5
// RUN: %target-codesign %t/main %t/%target-library-name(TestModuleLinking)
// RUN: %target-run %t/main %t/%target-library-name(TestModuleLinking) %t/%target-library-name(TestModuleLinking)

// RUN: %target-build-swift-dylib(%t/%target-library-name(TestModuleLinking)) -module-name TestModuleLinking -emit-module -emit-module-path %t/TestModuleLinking.swiftmodule -swift-version 5 %S/Inputs/dynamic_replacement_protocol_self_orig.swift -Xfrontend -enable-private-imports -Xfrontend -enable-implicit-dynamic -enable-library-evolution
// RUN: %target-build-swift -I%t -L%t -lTestModuleLinking -o %t/main %target-rpath(%t) %s -swift-version 5
// RUN: %target-codesign %t/main %t/%target-library-name(TestModuleLinking)
// RUN: %target-run %t/main %t/%target-library-name(TestModuleLinking) %t/%target-library-name(TestModuleLinking)

// N.B. We're not actually executing anything here - all we care about is
// if the linker is content.

Expand Down
17 changes: 0 additions & 17 deletions test/attr/dynamicReplacement_library_evolution.swift

This file was deleted.