[AutoDiff] Fix differentiation crasher: control flow + non-varied res… (#27554)
Fix PullbackEmitter crash for functions with multiple basic blocks, where some
basic blocks have no dominated active values.
This occurs because PullbackEmitter currently assumes that all bbs in
multiple-bb functions dominate some active values (at least the entry bb
will contain an active value). This assumption is not always true.
One fix is to add PullbackEmitter support for "bbs with no dominated active
values". This sounds plausible but requires nontrivial work.
Instead, we can infer:
- If no original bbs dominate active values,
- Then the original result must be non-varied (it is useful, so non-active
implies non-varied),
- Which can be detected easily, and tells us statically that the derivative of
the original result must be zero,
- So we can skip full pullback generation and simply return zero for wrt
parameters. This is a simpler fix and is robust for arbitrarily complex
functions with non-varied result.
A similar shortcut may be necessary for forward-mode differentiation when
it supports control flow.
Resolves TF-876.
Note that the shortcut is semantically sound: see TF-878 for details.