From 876996cf3ecf7509e7aad8e1d4c923d147745ff7 Mon Sep 17 00:00:00 2001 From: Dan Zheng Date: Mon, 14 Jan 2019 16:49:15 -0800 Subject: [PATCH 1/2] [Sema] [AutoDiff] Synthesize more `@_fieldwiseProductSpace` attributes. When all members have all `Differentiable` associated types equal to `Self`, a single member `AllDifferentiableVariables` struct is synthesized. Later, `TangentVector` and `CotangentVector` typealiases are generated that forward to it. This patch proactively generates `TangentVector` and `CotangentVector` aliases with the `@_fieldwiseProductSpace` attribute. --- lib/Sema/DerivedConformanceDifferentiable.cpp | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/lib/Sema/DerivedConformanceDifferentiable.cpp b/lib/Sema/DerivedConformanceDifferentiable.cpp index 608905298baff..04c2ac6981575 100644 --- a/lib/Sema/DerivedConformanceDifferentiable.cpp +++ b/lib/Sema/DerivedConformanceDifferentiable.cpp @@ -786,8 +786,9 @@ deriveDifferentiable_AssociatedStruct(DerivedConformance &derived, } // Add a typealias declaration with the given name and underlying target - // struct type to the source struct. - auto addAssociatedTypeAliasDecl = [&](Identifier name, StructDecl *source, + // struct type to the source nominal type. + auto addAssociatedTypeAliasDecl = [&](Identifier name, + NominalTypeDecl *source, StructDecl *target) { auto lookup = source->lookupDirect(name); assert(lookup.size() < 2 && @@ -845,6 +846,10 @@ deriveDifferentiable_AssociatedStruct(DerivedConformance &derived, allDiffableVarsStruct, allDiffableVarsStruct); addAssociatedTypeAliasDecl(C.Id_CotangentVector, allDiffableVarsStruct, allDiffableVarsStruct); + addAssociatedTypeAliasDecl(C.Id_TangentVector, + nominal, allDiffableVarsStruct); + addAssociatedTypeAliasDecl(C.Id_CotangentVector, + nominal, allDiffableVarsStruct); TC.validateDecl(allDiffableVarsStruct); return parentDC->mapTypeIntoContext( allDiffableVarsStruct->getDeclaredInterfaceType()); From bdab19defad1c09c326c80e62926f1ea49f888a7 Mon Sep 17 00:00:00 2001 From: Dan Zheng Date: Mon, 14 Jan 2019 17:08:09 -0800 Subject: [PATCH 2/2] Add test. --- test/AutoDiff/derived_differentiable_properties.swift | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/AutoDiff/derived_differentiable_properties.swift b/test/AutoDiff/derived_differentiable_properties.swift index d68b2d013463f..6e8c219112b78 100644 --- a/test/AutoDiff/derived_differentiable_properties.swift +++ b/test/AutoDiff/derived_differentiable_properties.swift @@ -13,8 +13,8 @@ public struct Foo : Differentiable { // CHECK-AST: @_fieldwiseProductSpace typealias AllDifferentiableVariables = Foo.AllDifferentiableVariables // CHECK-AST: @_fieldwiseProductSpace typealias TangentVector = Foo.AllDifferentiableVariables // CHECK-AST: @_fieldwiseProductSpace typealias CotangentVector = Foo.AllDifferentiableVariables -// CHECK-AST: typealias TangentVector = Foo.AllDifferentiableVariables -// CHECK-AST: typealias CotangentVector = Foo.AllDifferentiableVariables +// CHECK-AST: @_fieldwiseProductSpace typealias TangentVector = Foo.AllDifferentiableVariables +// CHECK-AST: @_fieldwiseProductSpace typealias CotangentVector = Foo.AllDifferentiableVariables // CHECK-SILGEN-LABEL: // Foo.a.getter // CHECK-SILGEN: sil [transparent] [serialized] [differentiable source 0 wrt 0] @$s33derived_differentiable_properties3FooV1aSfvg : $@convention(method) (Foo) -> Float