Skip to content

Commit c142fcf

Browse files
committed
[AutoDiff] Add SynthesizedFileUnit TBDGen/serialization tests.
Upstream `SynthesizedFileUnit` failing tests from TF-1239.
1 parent 165af54 commit c142fcf

File tree

4 files changed

+68
-0
lines changed

4 files changed

+68
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import _Differentiation
2+
3+
@inlinable
4+
@differentiable(where T: Differentiable)
5+
public func identity<T>(_ x: T) -> T { x }
6+
7+
public func foo<T: Differentiable>(_ f: @differentiable (T) -> T = identity) -> T {
8+
fatalError()
9+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// RUN: %empty-directory(%t)
2+
// RUN: %target-build-swift -emit-module -module-name tf1202 -emit-module-path %t/tf1202.swiftmodule %S/Inputs/tf1202-differentiability-witness-dead-function-elimination.swift
3+
// RUN: %target-build-swift -I%t -emit-module -O %s
4+
5+
// TF-1202: test bug where DeadFunctionElimination eliminated the
6+
// SILFunction for `func identity<T>` even though a differentiability witness for it
7+
// exists. This causes deserialization of this module to crash when
8+
// trying to deserialize the differentiability witness because it can't find
9+
// the original function `func identity<T>`.
10+
11+
// TF-1239: Test `SynthesizedFileUnit` serialization.
12+
13+
import tf1202
14+
15+
func callit() -> Float {
16+
return foo()
17+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import _Differentiation
2+
3+
@differentiable
4+
public func defaultArgument(_ x: Float) -> Float {
5+
return x
6+
}
7+
8+
@differentiable
9+
public func applyArgument(
10+
_ x: Float,
11+
_ f: @differentiable (Float) -> Float = defaultArgument
12+
) -> Float {
13+
return f(x)
14+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
// RUN: %empty-directory(%t)
2+
// RUN: %target-build-swift -working-directory %t -parse-as-library -emit-module -module-name cross_module_differentiation_other -emit-module-path %t/cross_module_differentiation_other.swiftmodule -emit-library -static %S/Inputs/cross_module_differentiation_other.swift
3+
// RUN: %target-build-swift -I%t -L%t %s -o %t/a.out -lcross_module_differentiation_other
4+
// RUN: %target-run %t/a.out
5+
// REQUIRES: executable_test
6+
7+
// TF-1025: Test differentiability witness linkage for `PublicNonABI` original functions.
8+
// TF-1239: Test `SynthesizedFileUnit` TBDGen.
9+
10+
import cross_module_differentiation_other
11+
import _Differentiation
12+
import StdlibUnittest
13+
14+
var CrossModuleTests = TestSuite("E2ECrossModule")
15+
16+
CrossModuleTests.test("differentiable function default argument") {
17+
let actualGrad = gradient(at: 0) { applyArgument($0) }
18+
let expectedGrad: Float = 1
19+
expectEqual(actualGrad, expectedGrad)
20+
}
21+
22+
CrossModuleTests.test("differentiable function specified default argument") {
23+
let actualGrad = gradient(at: 0) { applyArgument($0, { $0 }) }
24+
let expectedGrad: Float = 1
25+
expectEqual(actualGrad, expectedGrad)
26+
}
27+
28+
runAllTests()

0 commit comments

Comments
 (0)