Skip to content

Commit 7930bca

Browse files
committed
[AutoDiff] Temporarily disable non-varied result warning.
Previously, a warning and fixit was generated for differentiable functions with non-varied results. ``` tf-775.swift:3:3: warning: result does not depend on differentiation arguments and will always have a zero derivative; do you want to use 'withoutDerivative(at:)'? .zero ^ withoutDerivative(at: ) ``` However, TF-775 exposes that the fixit does not work and the warning is unsilenceable. This patch temporarily disables the warning, as a robust fix requires non-trivial activity analysis changes. A lack of warning is better than an unsilenceable false positive. TF-788 tracks re-enabling the warning. Add regression test to ensure that unsilenceable warnings will not happen again.
1 parent 56abb79 commit 7930bca

File tree

3 files changed

+27
-9
lines changed

3 files changed

+27
-9
lines changed

lib/SILOptimizer/Mandatory/Differentiation.cpp

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4594,12 +4594,13 @@ class JVPEmitter final
45944594
attr->getIndices().parameters->getNumIndices());
45954595
auto origParamArgs = original->getArgumentsWithoutIndirectResults();
45964596

4597-
// Check if result is not varied.
4597+
// TODO(TF-788): Re-enable non-varied result warning.
4598+
/*
4599+
// Emit a warning and fixit if original result is not varied, because it
4600+
// will always have a zero derivative.
45984601
SmallVector<SILValue, 8> origFormalResults;
45994602
collectAllFormalResultsInTypeOrder(*original, origFormalResults);
46004603
auto origResult = origFormalResults[getIndices().source];
4601-
// Emit warning if original result is not varied, because it will always
4602-
// have a zero derivative.
46034604
if (!activityInfo.isVaried(origResult, getIndices().parameters)) {
46044605
// Emit fixit if original result has a valid source location.
46054606
auto startLoc = origResult.getLoc().getStartSourceLoc();
@@ -4610,6 +4611,7 @@ class JVPEmitter final
46104611
.fixItInsertAfter(endLoc, ")");
46114612
}
46124613
}
4614+
*/
46134615

46144616
auto *diffEntry = getDifferential().getEntryBlock();
46154617
diffBuilder.setInsertionPoint(
@@ -5624,8 +5626,10 @@ class PullbackEmitter final : public SILInstructionVisitor<PullbackEmitter> {
56245626
SmallVector<SILValue, 8> origFormalResults;
56255627
collectAllFormalResultsInTypeOrder(original, origFormalResults);
56265628
auto origResult = origFormalResults[getIndices().source];
5627-
// Emit warning if original result is not varied, because it will always
5628-
// have a zero derivative.
5629+
// TODO(TF-788): Re-enable non-varied result warning.
5630+
/*
5631+
// Emit a warning and fixit if original result is not varied, because it
5632+
// will always have a zero derivative.
56295633
if (!getActivityInfo().isVaried(origResult, getIndices().parameters)) {
56305634
// Emit fixit if original result has a valid source location.
56315635
auto startLoc = origResult.getLoc().getStartSourceLoc();
@@ -5636,6 +5640,7 @@ class PullbackEmitter final : public SILInstructionVisitor<PullbackEmitter> {
56365640
.fixItInsertAfter(endLoc, ")");
56375641
}
56385642
}
5643+
*/
56395644
builder.setInsertionPoint(
56405645
pullbackEntry, getNextFunctionLocalAllocationInsertionPoint());
56415646
if (seed->getType().isAddress()) {

test/AutoDiff/autodiff_diagnostics.swift

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,11 +50,13 @@ _ = gradient(at: NoDerivativeProperty(x: 1, y: 1)) { s -> Float in
5050
return tmp.x
5151
}
5252
_ = gradient(at: NoDerivativeProperty(x: 1, y: 1)) { s in
53-
// expected-warning @+1 {{result does not depend on differentiation arguments and will always have a zero derivative; do you want to use 'withoutDerivative(at:)'?}} {{10-10=withoutDerivative(at:}} {{13-13=)}}
53+
// TODO(TF-788): Re-enable non-varied result warning.
54+
// xpected-warning @+1 {{result does not depend on differentiation arguments and will always have a zero derivative; do you want to use 'withoutDerivative(at:)'?}} {{10-10=withoutDerivative(at:}} {{13-13=)}}
5455
return s.y
5556
}
5657
_ = gradient(at: NoDerivativeProperty(x: 1, y: 1)) {
57-
// expected-warning @+1 {{result does not depend on differentiation arguments and will always have a zero derivative; do you want to use 'withoutDerivative(at:)'?}} {{3-3=withoutDerivative(at:}} {{7-7=)}}
58+
// TODO(TF-788): Re-enable non-varied result warning.
59+
// xpected-warning @+1 {{result does not depend on differentiation arguments and will always have a zero derivative; do you want to use 'withoutDerivative(at:)'?}} {{3-3=withoutDerivative(at:}} {{7-7=)}}
5860
$0.y
5961
}
6062

@@ -295,10 +297,20 @@ func one() -> Float {
295297
}
296298
@differentiable
297299
func nonVariedResult(_ x: Float) -> Float {
298-
// expected-warning @+1 {{result does not depend on differentiation arguments and will always have a zero derivative; do you want to use 'withoutDerivative(at:)'?}} {{10-10=withoutDerivative(at:}} {{15-15=)}}
300+
// TODO(TF-788): Re-enable non-varied result warning.
301+
// xpected-warning @+1 {{result does not depend on differentiation arguments and will always have a zero derivative; do you want to use 'withoutDerivative(at:)'?}} {{10-10=withoutDerivative(at:}} {{15-15=)}}
299302
return one()
300303
}
301304

305+
// Check that `withoutDerivative(at:)` silences the warning.
306+
307+
struct TF_775: Differentiable {
308+
@differentiable(wrt: (self))
309+
func nonVariedResult(_ input: Float) -> Float {
310+
withoutDerivative(at: input)
311+
}
312+
}
313+
302314
//===----------------------------------------------------------------------===//
303315
// Subset parameters
304316
//===----------------------------------------------------------------------===//

test/AutoDiff/forward_mode_diagnostics.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,8 @@ func one() -> Float {
8080
}
8181
@differentiable
8282
func nonVariedResult(_ x: Float) -> Float {
83-
// expected-warning @+1 2 {{result does not depend on differentiation arguments and will always have a zero derivative; do you want to use 'withoutDerivative(at:)'?}} {{10-10=withoutDerivative(at:}}
83+
// TODO(TF-788): Re-enable non-varied result warning.
84+
// xpected-warning @+1 2 {{result does not depend on differentiation arguments and will always have a zero derivative; do you want to use 'withoutDerivative(at:)'?}} {{10-10=withoutDerivative(at:}}
8485
return one()
8586
}
8687

0 commit comments

Comments
 (0)