Skip to content

Commit 21a0eee

Browse files
authored
[AutoDiff] Revisit differentiation of struct instruction with aggregate adjoint value. (swiftlang#25234)
In the latest Swift, no calls to an initializer will emit a `struct` instruction, so differentiation of the `struct` instruction will only be for differentiating struct initializer functions themselves. In struct initializer functions, since there's only one `struct` operation, `AdjointEmitter::visitStructInst` will never get an adjoint value of kind `AdjointValueKind::Aggregate`. This PR marks that case unreachable.
1 parent f537a92 commit 21a0eee

File tree

2 files changed

+7
-3
lines changed

2 files changed

+7
-3
lines changed

lib/SILOptimizer/Mandatory/Differentiation.cpp

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4541,8 +4541,11 @@ class AdjointEmitter final : public SILInstructionVisitor<AdjointEmitter> {
45414541
break;
45424542
}
45434543
case AdjointValueKind::Aggregate: {
4544-
llvm_unreachable("Unhandled. Are you trying to differentiate a "
4545-
"memberwise initializer?");
4544+
// Note: All user-called initializations go through the calls to the
4545+
// initializer, and synthesized initializers only have one level of struct
4546+
// formation which will not result into any aggregate adjoint valeus.
4547+
llvm_unreachable("Aggregate adjoint values should not occur for `struct` "
4548+
"instructions");
45464549
}
45474550
}
45484551
}

test/AutoDiff/simple_math.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,8 @@ SimpleMathTests.test("StructMemberwiseInitializer") {
184184

185185
let 𝛁foo = pullback(at: Float(4), in: { input -> Foo in
186186
let foo = Foo(stored: input)
187-
return foo + foo
187+
let foo2 = foo + foo
188+
return Foo(stored: foo2.stored)
188189
})(Foo.TangentVector(stored: 1))
189190
expectEqual(2, 𝛁foo)
190191

0 commit comments

Comments
 (0)