Skip to content

Commit cf2ccf1

Browse files
committed
[AutoDiff] Use auto-generated locations for temporary allocations.
In `TangentBuilder` temporary allocations are emitted in order to make generic calls to `AdditiveArithmetic.zero` and `AdditiveArithmetic.+`. When we perform accumulation of two adjoint values that correspond to a lexical value in the original function, the stack allocations require a debug variable, as we use the original instruction's location. However such a debug variable wouldn't really make sense as these temporary allocations do not represent the original variable's adjoint. With lexical borrow scopes, we hit a crasher when creating temporary allocations when accumulating adjoints for lexical `begin_borrow` instructions. This PR makes `TangentBuilder`'s temporary allocations use an auto-generated location. The existing debug info emission behavior is not changed, as we still emit `debug_value` when materializing an `AdjointValue` after any necessary adjoint accumulation.
1 parent e2f1907 commit cf2ccf1

File tree

5 files changed

+30
-8
lines changed

5 files changed

+30
-8
lines changed

lib/SILOptimizer/Differentiation/TangentBuilder.cpp

+6-4
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,8 @@ void TangentBuilder::emitZeroIntoBuffer(SILLocation loc, SILValue buffer,
6161
SILValue TangentBuilder::emitZero(SILLocation loc, CanType type) {
6262
auto silType = getModule().Types.getLoweredLoadableType(
6363
type, TypeExpansionContext::minimal(), getModule());
64-
auto *alloc = createAllocStack(loc, silType);
64+
auto tempAllocLoc = RegularLocation::getAutoGeneratedLocation();
65+
auto *alloc = createAllocStack(tempAllocLoc, silType);
6566
emitZeroIntoBuffer(loc, alloc, IsInitialization);
6667
auto zeroValue = emitLoadValueOperation(
6768
loc, alloc, LoadOwnershipQualifier::Take);
@@ -176,9 +177,10 @@ SILValue TangentBuilder::emitAdd(SILLocation loc, SILValue lhs, SILValue rhs) {
176177
auto lhsCopy = emitCopyValueOperation(loc, lhs);
177178
auto rhsCopy = emitCopyValueOperation(loc, rhs);
178179
// Allocate buffers for inputs and output.
179-
auto *resultBuf = createAllocStack(loc, type);
180-
auto *lhsBuf = createAllocStack(loc, type);
181-
auto *rhsBuf = createAllocStack(loc, type);
180+
auto tempAllocLoc = RegularLocation::getAutoGeneratedLocation();
181+
auto *resultBuf = createAllocStack(tempAllocLoc, type);
182+
auto *lhsBuf = createAllocStack(tempAllocLoc, type);
183+
auto *rhsBuf = createAllocStack(tempAllocLoc, type);
182184
// Initialize input buffers.
183185
emitStoreValueOperation(loc, lhsCopy, lhsBuf,
184186
StoreOwnershipQualifier::Init);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// RUN: %target-build-swift %s
2+
// RUN: %target-swift-frontend -c -g -Xllvm -verify-di-holes=true %s
3+
4+
// SR-15566: Differentiable functions with control flow yield an assertion failure: "location is a VarDecl, but SILDebugVariable is empty"
5+
6+
import _Differentiation
7+
8+
public struct Test: Differentiable {
9+
public var v1: [[Float]]
10+
11+
@differentiable(reverse)
12+
public init(v1: [[Float]]) {
13+
if v1.count != 2 {
14+
fatalError("Mismatched counts")
15+
}
16+
self.v1 = v1
17+
}
18+
}
19+
20+
// Assertion failed: ((!dyn_cast_or_null<VarDecl>(Loc.getAsASTNode<Decl>()) || Var) && "location is a VarDecl, but SILDebugVariable is empty"), function createAllocStack, file SILBuilder.h, line 389.

test/AutoDiff/validation-test/array.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// RUN: %target-run-simple-swift(-Xfrontend -enable-lexical-borrow-scopes=false)
1+
// RUN: %target-run-simple-swift
22

33
// REQUIRES: executable_test
44

test/AutoDiff/validation-test/optional.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// RUN: %target-run-simple-swift(-Xfrontend -enable-lexical-borrow-scopes=false)
1+
// RUN: %target-run-simple-swift
22

33
// REQUIRES: executable_test
44

test/AutoDiff/validation-test/optional_property.swift

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
// RUN: %target-run-simple-swift(-Xfrontend -enable-lexical-borrow-scopes=false)
2-
// RUN: %target-swift-emit-sil -Xllvm -debug-only=differentiation -enable-lexical-borrow-scopes=false -module-name null -o /dev/null 2>&1 %s | %FileCheck %s
1+
// RUN: %target-run-simple-swift
2+
// RUN: %target-swift-emit-sil -Xllvm -debug-only=differentiation -module-name null -o /dev/null 2>&1 %s | %FileCheck %s
33

44
// REQUIRES: executable_test
55
// REQUIRES: asserts

0 commit comments

Comments
 (0)