swift
4da5f18e - [AutoDiff] Fix usefulness propagation through buffers in activity analysis.

Commit
6 years ago
[AutoDiff] Fix usefulness propagation through buffers in activity analysis. ```swift func foo(x: Float) -> Float { var a = x a = a + x // <- read from memory return a + x } _ = gradient(of: foo) ``` Previously, the result of `apply` that corresponds to the right hand side of `a = a + x` was not correctly marked as _USEFUL_, thus not _ACTIVE_, but it should be differentiated because it contributes to the output based on the input. This patch fixes that by applying the following rules in the bottom-up pass in activity analysis that computes usefulness: * For any read from memory, if any result is _USEFUL_, all buffer operands will be marked _USEFUL_. * For any buffer operand above marked _USEFUL_, every projection of the operand value will be marked _USEFUL_. Now function `foo` gets currect activity: ```swift [ACTIVE] %0 = argument of bb0 : $Float // users: %3, %18, %9, %1 [ACTIVE] %2 = alloc_stack $Float, var, name "a" // users: %3, %19, %5, %10, %14 [NONE] %4 = metatype $@thin Float.Type // user: %9 [ACTIVE] %5 = begin_access [read] [static] %2 : $*Float // users: %6, %7 [ACTIVE] %6 = load %5 : $*Float // user: %9 [NONE] // function_ref static Float.+ infix(_:_:) %8 = function_ref @$sSf1poiyS2f_SftFZ : $@convention(method) (Float, Float, @thin Float.Type) -> Float // user: %9 [ACTIVE] %9 = apply %8(%6, %0, %4) : $@convention(method) (Float, Float, @thin Float.Type) -> Float // user: %11 [ACTIVE] %10 = begin_access [modify] [static] %2 : $*Float // users: %11, %12 [NONE] %13 = metatype $@thin Float.Type // user: %18 [ACTIVE] %14 = begin_access [read] [static] %2 : $*Float // users: %15, %16 [ACTIVE] %15 = load %14 : $*Float // user: %18 [NONE] // function_ref static Float.+ infix(_:_:) %17 = function_ref @$sSf1poiyS2f_SftFZ : $@convention(method) (Float, Float, @thin Float.Type) -> Float // user: %18 [ACTIVE] %18 = apply %17(%15, %0, %13) : $@convention(method) (Float, Float, @thin Float.Type) -> Float // user: %20 ```
Author
Committer
Parents
  • lib/SILOptimizer/Mandatory
    • File
      Differentiation.cpp