Uploaded image for project: 'Swift'
  1. Swift
  2. SR-12779

Wrapped property differentiation: ownership error for setter pullback (non-trivial loadable property)

    XMLWordPrintable

    Details

    • Type: Bug
    • Status: Resolved
    • Priority: Medium
    • Resolution: Done
    • Component/s: Compiler
    • Labels:

      Description

      // nontrivial_loadable_type.swift
      import _Differentiation
      
      /// A non-trivial, loadable type.
      ///
      /// Used to test differentiation transform coverage.
      struct NontrivialLoadable<T> {
        fileprivate class Box {
          fileprivate var value: T
          init(_ value: T) {
            self.value = value
          }
        }
        private var handle: Box
      
        init(_ value: T) {
          self.handle = Box(value)
        }
      
        var value: T {
          get { handle.value }
          set { handle.value = newValue }
        }
      }
      
      extension NontrivialLoadable: ExpressibleByFloatLiteral
      where T: ExpressibleByFloatLiteral {
        init(floatLiteral value: T.FloatLiteralType) {
          self.handle = Box(T(floatLiteral: value))
        }
      }
      
      extension NontrivialLoadable: ExpressibleByIntegerLiteral
      where T: ExpressibleByIntegerLiteral {
        init(integerLiteral value: T.IntegerLiteralType) {
          self.handle = Box(T(integerLiteral: value))
        }
      }
      
      extension NontrivialLoadable: Equatable where T: Equatable {
        static func == (lhs: NontrivialLoadable, rhs: NontrivialLoadable) -> Bool {
          return lhs.value == rhs.value
        }
      }
      
      extension NontrivialLoadable: AdditiveArithmetic where T: AdditiveArithmetic {
        static var zero: NontrivialLoadable { return NontrivialLoadable(T.zero) }
        static func + (lhs: NontrivialLoadable, rhs: NontrivialLoadable)
          -> NontrivialLoadable
        {
          return NontrivialLoadable(lhs.value + rhs.value)
        }
        static func - (lhs: NontrivialLoadable, rhs: NontrivialLoadable)
          -> NontrivialLoadable
        {
          return NontrivialLoadable(lhs.value - rhs.value)
        }
      }
      
      extension NontrivialLoadable: Differentiable
      where T: Differentiable, T == T.TangentVector {
        typealias TangentVector = NontrivialLoadable<T.TangentVector>
      }
      
      // property-wrappers.swift
      // RUN: %target-swift-frontend -emit-sil -verify %s %S/Inputs/nontrivial_loadable_type.swift
      
      import DifferentiationUnittest
      
      @propertyWrapper
      struct Wrapper<Value> {
        private var value: Value
        var wrappedValue: Value { // computed property
          get { value }
          set { value = newValue }
        }
      
        init(wrappedValue: Value) {
          self.value = wrappedValue
        }
      }
      
      struct GenericStruct<T: Differentiable>: Differentiable {
        @Wrapper var nontrivial: NontrivialLoadable<Float> = 20
      
        @differentiable
        static func nontrivialSetter(_ s: inout GenericStruct, _ x: NontrivialLoadable<Float>) {
          s.nontrivial = x
        }
      }
      
      $ swiftc nontrivial_loadable_type.swift property_wrappers.swift
      Begin Error in Function: 'AD__$s17property_wrappers13GenericStructV10nontrivialAA18NontrivialLoadableVySfGvs__pullback_src_0_wrt_0_1_16_Differentiation14DifferentiableRzl'
      Error! Found a leaked owned value that was never consumed.
      Value:   %4 = load [take] %3 : $*NontrivialLoadable<Float> // user: %8
      
      End Error in Function: 'AD__$s17property_wrappers13GenericStructV10nontrivialAA18NontrivialLoadableVySfGvs__pullback_src_0_wrt_0_1_16_Differentiation14DifferentiableRzl'
      Found ownership error?!
      Stack dump:
      0.	Program arguments: build/Ninja-ReleaseAssert/swift-macosx-x86_64/bin/swiftc -frontend -target x86_64-apple-macosx10.9 -module-cache-path build/Ninja-ReleaseAssert/swift-macosx-x86_64/swift-test-results/x86_64-apple-macosx10.9/clang-module-cache -sdk /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.15.sdk -swift-version 4 -ignore-module-source-info -typo-correction-limit 10 -emit-sil -verify swift/test/AutoDiff/SILOptimizer/property_wrappers.swift swift/test/AutoDiff/SILOptimizer/Inputs/nontrivial_loadable_type.swift
      1.	Swift version 5.3-dev (LLVM f66b332548, Swift bb2bc9e176)
      2.	While evaluating request ExecuteSILPipelineRequest(Run pipelines { Guaranteed Passes } on SIL for property_wrappers.property_wrappers)
      3.	While running pass #354 SILModuleTransform "MandatoryInlining".
      4.	While verifying SIL function "@AD__$s17property_wrappers13GenericStructV10nontrivialAA18NontrivialLoadableVySfGvs__pullback_src_0_wrt_0_1_16_Differentiation14DifferentiableRzl".
       for setter for nontrivial (at swift/test/AutoDiff/SILOptimizer/property_wrappers.swift:19:16)
      ...
      

        Attachments

          Activity

            People

            Assignee:
            danzheng Dan Zheng
            Reporter:
            danzheng Dan Zheng
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

              Dates

              Created:
              Updated:
              Resolved: