Skip to content

[AutoDiff] Fix assert on missing struct decl on cross-file derivative search #77183

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 2 commits into from
Oct 29, 2024
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
3 changes: 2 additions & 1 deletion lib/Sema/TypeCheckAttr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5459,7 +5459,8 @@ static AbstractFunctionDecl *findAutoDiffOriginalFunctionDecl(
if (!baseType && lookupContext->isTypeContext())
baseType = lookupContext->getSelfTypeInContext();
if (baseType) {
results = TypeChecker::lookupMember(lookupContext, baseType, funcName);
if (!baseType->hasError())
results = TypeChecker::lookupMember(lookupContext, baseType, funcName);
} else {
results = TypeChecker::lookupUnqualified(
lookupContext, funcName, funcNameLoc.getBaseNameLoc(), lookupOptions);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import _Differentiation

@inlinable
@derivative(of: min)
func minVJP<T: Comparable & Differentiable>(
_ x: T,
_ y: T
) -> (value: T, pullback: (T.TangentVector) -> (T.TangentVector, T.TangentVector)) {
func pullback(_ v: T.TangentVector) -> (T.TangentVector, T.TangentVector) {
if x <= y {
return (v, .zero)
}
else {
return (.zero, v)
}
}
return (value: min(x, y), pullback: pullback)
}

extension Struct {
@inlinable
@derivative(of: max) // expected-error {{cannot find 'max' in scope}}
static func maxVJP<T: Comparable & Differentiable>(
_ x: T,
_ y: T
) -> (value: T, pullback: (T.TangentVector) -> (T.TangentVector, T.TangentVector)) {
func pullback(_ v: T.TangentVector) -> (T.TangentVector, T.TangentVector) {
if x < y {
return (.zero, v)
}
else {
return (v, .zero)
}
}
return (value: max(x, y), pullback: pullback)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,21 @@ func minVJP<T: Comparable & Differentiable>(
return (value: min(x, y), pullback: pullback)
}

@inlinable
@derivative(of: max)
func maxVJP<T: Comparable & Differentiable>(
_ x: T,
_ y: T
) -> (value: T, pullback: (T.TangentVector) -> (T.TangentVector, T.TangentVector)) {
func pullback(_ v: T.TangentVector) -> (T.TangentVector, T.TangentVector) {
if x < y {
return (.zero, v)
}
else {
return (v, .zero)
extension Struct {
@inlinable
@derivative(of: max)
static func maxVJP<T: Comparable & Differentiable>(
_ x: T,
_ y: T
) -> (value: T, pullback: (T.TangentVector) -> (T.TangentVector, T.TangentVector)) {
func pullback(_ v: T.TangentVector) -> (T.TangentVector, T.TangentVector) {
if x < y {
return (.zero, v)
}
else {
return (v, .zero)
}
}
return (value: max(x, y), pullback: pullback)
}
return (value: max(x, y), pullback: pullback)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import _Differentiation

struct Struct {
@inlinable
static func max<T: Comparable>(
_ x: T,
_ y: T
) -> T {
if x > y
return y
else
return x
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
// RUN: %target-swift-frontend -emit-sil -verify -primary-file %s %S/Inputs/derivatives-error.swift -module-name main -o /dev/null

import _Differentiation

@differentiable(reverse)
func clamp(_ value: Double, _ lowerBound: Double, _ upperBound: Double) -> Double {
return Struct.max(min(value, upperBound), lowerBound) // expected-error {{cannot find 'Struct' in scope}}
}
5 changes: 3 additions & 2 deletions test/AutoDiff/Sema/DerivativeRegistrationCrossFile/main.swift
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
// RUN: %target-swift-frontend -emit-sil -verify -primary-file %s %S/Inputs/derivatives.swift -module-name main -o /dev/null
// RUN: %target-swift-frontend -emit-sil -verify -primary-file %s \
// RUN: %S/Inputs/derivatives.swift %S/Inputs/struct.swift -module-name main -o /dev/null

import _Differentiation

@differentiable(reverse)
func clamp(_ value: Double, _ lowerBound: Double, _ upperBound: Double) -> Double {
// No error expected
return max(min(value, upperBound), lowerBound)
return Struct.max(min(value, upperBound), lowerBound)
}