Skip to content

Commit f8141e2

Browse files
authored
Ensure we are materializing adjoint value into buffer correctly for optionals: (swiftlang#74964)
- Ue enum $Optional<T.TangentVector>, #Optional.some!enumelt, %wrappedAdjoint : $T for objects - Use inject_enum_addr %optArgBuf : $*Optional<T.TangentVector>, #Optional.some!enumelt for addresses Fixes swiftlang#74841
1 parent 080a82b commit f8141e2

File tree

2 files changed

+14
-1
lines changed

2 files changed

+14
-1
lines changed

lib/SILOptimizer/Differentiation/PullbackCloner.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2617,7 +2617,7 @@ AllocStackInst *PullbackCloner::Implementation::createOptionalAdjoint(
26172617
// Initialize an `Optional<T.TangentVector>` buffer from `wrappedAdjoint` as
26182618
// the input for `Optional<T>.TangentVector.init`.
26192619
auto *optArgBuf = builder.createAllocStack(pbLoc, optionalOfWrappedTanType);
2620-
if (optionalOfWrappedTanType.isLoadableOrOpaque(builder.getFunction())) {
2620+
if (optionalOfWrappedTanType.isObject()) {
26212621
// %enum = enum $Optional<T.TangentVector>, #Optional.some!enumelt,
26222622
// %wrappedAdjoint : $T
26232623
auto *enumInst = builder.createEnum(pbLoc, wrappedAdjoint, someEltDecl,
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// RUN: %target-swift-frontend -emit-sil -verify %s
2+
3+
// https://github.com/swiftlang/swift/issues/74841
4+
// We used to create invalid adjoint buffer for Optional<T>
5+
// if this buffer originated from unchecked_take_enum_data_addr
6+
// instruction
7+
import _Differentiation;
8+
9+
struct F<I> {subscript(_ i: Int) -> S<I>? {get {nil} set {}}}
10+
struct S<I> {subscript(_ i: Int) -> I? {get {nil} set {}}}
11+
extension F: Differentiable {}
12+
extension S: Differentiable {}
13+
struct A{@differentiable(reverse) func b(c: inout F<Double>, d: S<Double>) {c[0]![0] = 0}}

0 commit comments

Comments
 (0)