[AutoDiff] Enable variable debugging support for pullback functions.

- Properly clone and use debug scopes for all instructions in pullback functions.
- Emit `debug_value` instructions for adjoint values.
- Add debug locations and variable info to adjoint buffer allocations.
- Add `TangentBuilder` (a `SILBuilder` subclass) to unify and simplify special emitter utilities for tangent vector code generation. More simplifications to come.

Pullback variable inspection example:
```console
(lldb) n
Process 50984 stopped
* thread #1, queue = 'com.apple.main-thread', stop reason = step over
    frame #0: 0x0000000100003497 main`pullback of foo(x=0) at main.swift:12:11
   9   	import _Differentiation
   10
   11  	func foo(_ x: Float) -> Float {
-> 12  	  let y = sin(x)
   13  	  let z = cos(y)
   14  	  let k = tanh(z) + cos(y)
   15  	  return k
Target 0: (main) stopped.
(lldb) fr v
(Float) x = 0
(Float) k = 1
(Float) z = 0.495846391
(Float) y = -0.689988375
```

Resolves rdar://68616528 / SR-13535.
This commit is contained in:
Richard Wei
2020-12-19 22:00:28 -08:00
parent 1c2b80fae7
commit e3b480b0c9
14 changed files with 573 additions and 425 deletions

View File

@@ -95,6 +95,17 @@ FuncDecl *ADContext::getPlusEqualDecl() const {
return cachedPlusEqualFn;
}
AccessorDecl *ADContext::getAdditiveArithmeticZeroGetter() const {
if (cachedZeroGetter)
return cachedZeroGetter;
auto zeroDeclLookup = getAdditiveArithmeticProtocol()
->lookupDirect(getASTContext().Id_zero);
auto *zeroDecl = cast<VarDecl>(zeroDeclLookup.front());
assert(zeroDecl->isProtocolRequirement());
cachedZeroGetter = zeroDecl->getOpaqueAccessor(AccessorKind::Get);
return cachedZeroGetter;
}
void ADContext::cleanUp() {
// Delete all references to generated functions.
for (auto fnRef : generatedFunctionReferences) {