diff --git a/benchmark/CMakeLists.txt b/benchmark/CMakeLists.txt index 0db48967fad2a..08dded4dd11c4 100644 --- a/benchmark/CMakeLists.txt +++ b/benchmark/CMakeLists.txt @@ -80,6 +80,7 @@ set(SWIFT_BENCH_MODULES single-source/DictionaryRemove single-source/DictionarySubscriptDefault single-source/DictionarySwap + single-source/Differentiation single-source/Diffing single-source/DiffingMyers single-source/DropFirst diff --git a/benchmark/single-source/Differentiation.swift b/benchmark/single-source/Differentiation.swift new file mode 100644 index 0000000000000..f15f64348bec6 --- /dev/null +++ b/benchmark/single-source/Differentiation.swift @@ -0,0 +1,73 @@ +//===--- Differentiation.swift -------------------------------------------===// +// +// This source file is part of the Swift.org open source project +// +// Copyright (c) 2014 - 2020 Apple Inc. and the Swift project authors +// Licensed under Apache License v2.0 with Runtime Library Exception +// +// See https://swift.org/LICENSE.txt for license information +// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors +// +//===----------------------------------------------------------------------===// + +#if canImport(_Differentiation) + +import TestsUtils +import _Differentiation + +public let Differentiation = [ + BenchmarkInfo( + name: "DifferentiationIdentity", + runFunction: run_DifferentiationIdentity, + tags: [.regression, .differentiation] + ), + BenchmarkInfo( + name: "DifferentiationSquare", + runFunction: run_DifferentiationSquare, + tags: [.regression, .differentiation] + ), + BenchmarkInfo( + name: "DifferentiationArraySum", + runFunction: run_DifferentiationArraySum, + tags: [.regression, .differentiation], + setUpFunction: { blackHole(onesArray) } + ), +] + +@inline(never) +public func run_DifferentiationIdentity(N: Int) { + func f(_ x: Float) -> Float { + x + } + for _ in 0..<1000*N { + blackHole(valueWithGradient(at: 1, in: f)) + } +} + +@inline(never) +public func run_DifferentiationSquare(N: Int) { + func f(_ x: Float) -> Float { + x * x + } + for _ in 0..<1000*N { + blackHole(valueWithGradient(at: 1, in: f)) + } +} + +let onesArray: [Float] = Array(repeating: 1, count: 50) + +@inline(never) +public func run_DifferentiationArraySum(N: Int) { + func sum(_ array: [Float]) -> Float { + var result: Float = 0 + for i in withoutDerivative(at: 0..