Skip to content

Commit 7e2d238

Browse files
authored
Merge pull request swiftlang#36974 from integralpro/SR-14290
[AutoDiff] Use correct debug scope for pullback trampoline block.
2 parents 7611ecc + 841618e commit 7e2d238

File tree

2 files changed

+48
-0
lines changed

2 files changed

+48
-0
lines changed

lib/SILOptimizer/Differentiation/PullbackCloner.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2345,6 +2345,9 @@ SILBasicBlock *PullbackCloner::Implementation::buildPullbackSuccessor(
23452345
// Propagate pullback struct argument.
23462346
TangentBuilder pullbackTrampolineBBBuilder(
23472347
pullbackTrampolineBB, getContext());
2348+
pullbackTrampolineBBBuilder.setCurrentDebugScope(
2349+
remapScope(origPredBB->getTerminator()->getDebugScope()));
2350+
23482351
auto *pullbackTrampolineBBArg = pullbackTrampolineBB->getArguments().front();
23492352
if (vjpCloner.getLoopInfo()->getLoopFor(origPredBB)) {
23502353
assert(pullbackTrampolineBBArg->getType() ==
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
// RUN: %target-build-swift %s
2+
// RUN: %target-swift-frontend -c -g -Xllvm -verify-di-holes=true %s
3+
4+
// rdar://74876596 ([SR-14290]: SIL verification fails when differentiating a function of [[Double]])
5+
6+
import _Differentiation
7+
8+
let values: [[Double]] = [[0, 0], [0, 0]]
9+
let const = 1.12345
10+
let result = add(const, to: values)
11+
12+
@differentiable(reverse)
13+
func add(_ const: Double, to values: [[Double]]) -> [[Double]] {
14+
var result = values
15+
for i in withoutDerivative(at: values.indices) {
16+
for j in withoutDerivative(at: values.indices) {
17+
result.updated(at: i, j, with: values[i][j] + const)
18+
}
19+
}
20+
return result
21+
}
22+
23+
extension Array where Element == [Double] {
24+
@differentiable(reverse)
25+
mutating func updated(at i: Int, _ j: Int, with newValue: Double) {
26+
self[i][j] = newValue
27+
}
28+
29+
@derivative(of: updated)
30+
mutating func vjpUpdated(at i: Int, _ j: Int, with newValue: Double)
31+
-> (value: Void, pullback: (inout TangentVector) -> (Double.TangentVector)) {
32+
self.updated(at: i, j, with: newValue)
33+
34+
func pullback(dSelf: inout TangentVector) -> (Double.TangentVector) {
35+
let dElement = dSelf[i][j]
36+
dSelf.base[i].base[j] = 0
37+
return dElement
38+
}
39+
let value: Void = ()
40+
41+
return (value, pullback)
42+
}
43+
}
44+
45+

0 commit comments

Comments
 (0)