Skip to content

Commit bc94ac3

Browse files
authored
[AutoDiff] Better diagnose non-differentiability for throwing functions (#81669)
Fixes #81608
1 parent de15dc6 commit bc94ac3

File tree

2 files changed

+32
-4
lines changed

2 files changed

+32
-4
lines changed

lib/SILOptimizer/Differentiation/PullbackCloner.cpp

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1780,8 +1780,12 @@ class PullbackCloner::Implementation final
17801780
void visitMoveValueInst(MoveValueInst *mvi) {
17811781
switch (getTangentValueCategory(mvi)) {
17821782
case SILValueCategory::Address:
1783-
llvm::report_fatal_error("AutoDiff does not support move_value with "
1784-
"SILValueCategory::Address");
1783+
LLVM_DEBUG(getADDebugStream() << "AutoDiff does not support move_value with "
1784+
"SILValueCategory::Address");
1785+
getContext().emitNondifferentiabilityError(
1786+
mvi, getInvoker(), diag::autodiff_expression_not_differentiable_note);
1787+
errorOccurred = true;
1788+
return;
17851789
case SILValueCategory::Object:
17861790
visitValueOwnershipInst(mvi, /*needZeroResAdj=*/true);
17871791
}
@@ -3121,8 +3125,21 @@ void PullbackCloner::Implementation::visitSILBasicBlock(SILBasicBlock *bb) {
31213125
break;
31223126
}
31233127
}
3124-
} else
3125-
llvm::report_fatal_error("do not know how to handle this incoming bb argument");
3128+
} else {
3129+
LLVM_DEBUG(getADDebugStream() <<
3130+
"do not know how to handle this incoming bb argument");
3131+
if (auto term = bbArg->getSingleTerminator()) {
3132+
getContext().emitNondifferentiabilityError(term, getInvoker(),
3133+
diag::autodiff_expression_not_differentiable_note);
3134+
} else {
3135+
// This will be a bit confusing, but still better than nothing.
3136+
getContext().emitNondifferentiabilityError(bbArg, getInvoker(),
3137+
diag::autodiff_expression_not_differentiable_note);
3138+
}
3139+
3140+
errorOccurred = true;
3141+
return;
3142+
}
31263143
}
31273144

31283145
// 3. Build the pullback successor cases for the `switch_enum`

test/AutoDiff/SILOptimizer/differentiation_diagnostics.swift

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,17 @@ func try_apply_rethrows(_ x: Float) -> Float {
4646
return x
4747
}
4848

49+
// This generates `try_apply` which we do not know to handle yet, therefore
50+
// one should use a.differentialMap here. If / when differentiation of throwing
51+
// functions will be supported, we'd need to remove this diagnostics.
52+
// expected-error @+2 {{function is not differentiable}}
53+
// expected-note @+2 {{when differentiating this function definition}}
54+
@differentiable(reverse)
55+
func map_nondiff(_ a: [Float]) -> [Float] {
56+
// expected-note @+1 {{expression is not differentiable}}
57+
return a.map { $0 }
58+
}
59+
4960
//===----------------------------------------------------------------------===//
5061
// Unreachable
5162
//===----------------------------------------------------------------------===//

0 commit comments

Comments
 (0)