Skip to content

[AutoDiff] Better diagnose non-differentiability for throwing functions #81669

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 21, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 21 additions & 4 deletions lib/SILOptimizer/Differentiation/PullbackCloner.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1780,8 +1780,12 @@ class PullbackCloner::Implementation final
void visitMoveValueInst(MoveValueInst *mvi) {
switch (getTangentValueCategory(mvi)) {
case SILValueCategory::Address:
llvm::report_fatal_error("AutoDiff does not support move_value with "
"SILValueCategory::Address");
LLVM_DEBUG(getADDebugStream() << "AutoDiff does not support move_value with "
"SILValueCategory::Address");
getContext().emitNondifferentiabilityError(
mvi, getInvoker(), diag::autodiff_expression_not_differentiable_note);
errorOccurred = true;
return;
case SILValueCategory::Object:
visitValueOwnershipInst(mvi, /*needZeroResAdj=*/true);
}
Expand Down Expand Up @@ -3121,8 +3125,21 @@ void PullbackCloner::Implementation::visitSILBasicBlock(SILBasicBlock *bb) {
break;
}
}
} else
llvm::report_fatal_error("do not know how to handle this incoming bb argument");
} else {
LLVM_DEBUG(getADDebugStream() <<
"do not know how to handle this incoming bb argument");
if (auto term = bbArg->getSingleTerminator()) {
getContext().emitNondifferentiabilityError(term, getInvoker(),
diag::autodiff_expression_not_differentiable_note);
} else {
// This will be a bit confusing, but still better than nothing.
getContext().emitNondifferentiabilityError(bbArg, getInvoker(),
diag::autodiff_expression_not_differentiable_note);
}

errorOccurred = true;
return;
}
}

// 3. Build the pullback successor cases for the `switch_enum`
Expand Down
11 changes: 11 additions & 0 deletions test/AutoDiff/SILOptimizer/differentiation_diagnostics.swift
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,17 @@ func try_apply_rethrows(_ x: Float) -> Float {
return x
}

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

//===----------------------------------------------------------------------===//
// Unreachable
//===----------------------------------------------------------------------===//
Expand Down