Skip to content

[AutoDiff] Fix derivative forwarding thunk linkage. #31726

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
merged 1 commit into from
May 12, 2020
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
4 changes: 2 additions & 2 deletions lib/SILGen/SILGen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -839,8 +839,8 @@ void SILGenModule::emitDifferentiabilityWitness(
SILDifferentiabilityWitnessKey key{originalFunction->getName(), silConfig};
auto *diffWitness = M.lookUpDifferentiabilityWitness(key);
if (!diffWitness) {
// Strip external from linkage of original function.
// Necessary for Clang-imported functions, which have external linkage.
// Differentiability witnesses have the same linkage as the original
// function, stripping external.
auto linkage = stripExternalFromLinkage(originalFunction->getLinkage());
diffWitness = SILDifferentiabilityWitness::createDefinition(
M, linkage, originalFunction, silConfig.parameterIndices,
Expand Down
11 changes: 7 additions & 4 deletions lib/SILGen/SILGenPoly.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3941,14 +3941,17 @@ SILFunction *SILGenModule::getOrCreateCustomDerivativeThunk(

auto loc = customDerivativeFn->getLocation();
SILGenFunctionBuilder fb(*this);
// This thunk is publicly exposed and cannot be transparent.
// Instead, mark it as "always inline" for optimization.
// Derivative thunks have the same linkage as the original function, stripping
// external.
auto linkage = stripExternalFromLinkage(originalFn->getLinkage());
auto *thunk = fb.getOrCreateFunction(
loc, name, customDerivativeFn->getLinkage(), thunkFnTy, IsBare,
IsNotTransparent, customDerivativeFn->isSerialized(),
loc, name, linkage, thunkFnTy, IsBare, IsNotTransparent,
customDerivativeFn->isSerialized(),
customDerivativeFn->isDynamicallyReplaceable(),
customDerivativeFn->getEntryCount(), IsThunk,
customDerivativeFn->getClassSubclassScope());
// This thunk may be publicly exposed and cannot be transparent.
// Instead, mark it as "always inline" for optimization.
thunk->setInlineStrategy(AlwaysInline);
if (!thunk->empty())
return thunk;
Expand Down
21 changes: 21 additions & 0 deletions test/AutoDiff/Sema/derivative_attr_type_checking.swift
Original file line number Diff line number Diff line change
Expand Up @@ -837,6 +837,27 @@ fileprivate func _fileprivate_original_fileprivate_derivative(_ x: Float) -> (va
fatalError()
}

func internal_original_usablefrominline_derivative(_ x: Float) -> Float { x }
@usableFromInline
@derivative(of: internal_original_usablefrominline_derivative)
func _internal_original_usablefrominline_derivative(_ x: Float) -> (value: Float, pullback: (Float) -> Float) {
fatalError()
}

func internal_original_inlinable_derivative(_ x: Float) -> Float { x }
@inlinable
@derivative(of: internal_original_inlinable_derivative)
func _internal_original_inlinable_derivative(_ x: Float) -> (value: Float, pullback: (Float) -> Float) {
fatalError()
}

func internal_original_alwaysemitintoclient_derivative(_ x: Float) -> Float { x }
@_alwaysEmitIntoClient
@derivative(of: internal_original_alwaysemitintoclient_derivative)
func _internal_original_alwaysemitintoclient_derivative(_ x: Float) -> (value: Float, pullback: (Float) -> Float) {
fatalError()
}

// MARK: - Original function visibility < derivative function visibility

@usableFromInline
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// RUN: %target-swift-frontend -c %s -verify
// REQUIRES: asserts

// TF-1160: Linker error for `@usableFromInline` derivative function but
// non-`@usableFromInline` internal original function.

import _Differentiation

func internalOriginal(_ x: Float) -> Float { x }

@usableFromInline
@derivative(of: internalOriginal)
func usableFromInlineDerivative(_ x: Float) -> (value: Float, pullback: (Float) -> Float) {
(x, { $0 })
}

// Original error: type-checking passes but TBDGen is not consistent with IRGen.
// <unknown>:0: error: symbol 'AD__$s4main16internalOriginalyS2fF__vjp_src_0_wrt_0' (AD__$s4main16internalOriginalyS2fF__vjp_src_0_wrt_0) is in generated IR file, but not in TBD file
// <unknown>:0: error: please file a radar or open a bug on bugs.swift.org with this code, and add -Xfrontend -validate-tbd-against-ir=none to squash the errors