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

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

    XMLWordPrintable

    Details

    • Type: Bug
    • Status: Closed
    • 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 tracked: Tracked<Float> = 10
        @Wrapper var nontrivial: NontrivialLoadable<Float> = 20
      
        @differentiable
        static func trackedGetter(_ s: GenericStruct) -> Tracked<Float> {
          return s.tracked
        }
      
        @differentiable
        static func nontrivialGetter(_ s: GenericStruct) -> NontrivialLoadable<Float> {
          return s.nontrivial
        }
      }
      
      $ swiftc nontrivial_loadable_type.swift property_wrappers.swift
      Begin Error in Function: 'AD__$s17property_wrappers13GenericStructV10nontrivialAA18NontrivialLoadableVySfGvg__pullback_src_0_wrt_0_16_Differentiation14DifferentiableRzl'
      Found an operand with a value that is not compatible with the operand's operand ownership kind map.
      Value: %1 = argument of bb0 : $NontrivialLoadable<Float> // user: %10
      Value Ownership Kind: guaranteed
      Instruction:
         %1 = argument of bb0 : $NontrivialLoadable<Float> // user: %10
           %9 = struct_element_addr %3 : $*GenericStruct<τ_0_0>.TangentVector, #GenericStruct.TangentVector.nontrivial // user: %10
      ->   store %1 to [init] %9 : $*NontrivialLoadable<Float> // id: %10
      Operand Ownership Kind Map: -- OperandOwnershipKindMap --
      unowned:  No.
      owned: Yes. Liveness: MustBeInvalidated
      guaranteed:  No.
      any: Yes. Liveness: MustBeLive
      End Error in Function: 'AD__$s17property_wrappers13GenericStructV10nontrivialAA18NontrivialLoadableVySfGvg__pullback_src_0_wrt_0_16_Differentiation14DifferentiableRzl'
      Found ownership error?!
      
      Stack dump:
      ...
      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 #495 SILModuleTransform "Differentiation".
      4.	While canonicalizing `differentiable_function` SIL node   %8 = differentiable_function [parameters 0] %7 : $@callee_guaranteed (@in_guaranteed GenericStruct<τ_0_0>) -> @owned NontrivialLoadable<Float> // users: %13, %9
      5.	While ...in SIL function "@AD__$s17property_wrappers13GenericStructV16nontrivialGetteryAA18NontrivialLoadableVySfGACyxGFZ__vjp_src_0_wrt_0_16_Differentiation14DifferentiableRzl".
       for 'nontrivialGetter(_:)' (at swift/test/AutoDiff/SILOptimizer/property_wrappers.swift:58:3)
      6.	While processing // differentiability witness for GenericStruct.nontrivial.getter
      sil_differentiability_witness private [parameters 0] [results 0] <τ_0_0 where τ_0_0 : Differentiable> @$s17property_wrappers13GenericStructV10nontrivialAA18NontrivialLoadableVySfGvg : $@convention(method) <T where T : Differentiable> (@in_guaranteed GenericStruct<T>) -> @owned NontrivialLoadable<Float> {
      }
      
       on SIL function "@$s17property_wrappers13GenericStructV10nontrivialAA18NontrivialLoadableVySfGvg".
       for getter for nontrivial (at swift/test/AutoDiff/SILOptimizer/property_wrappers.swift:49:31)
      7.	While generating VJP for SIL function "@$s17property_wrappers13GenericStructV10nontrivialAA18NontrivialLoadableVySfGvg".
       for getter for nontrivial (at swift/test/AutoDiff/SILOptimizer/property_wrappers.swift:49:31)
      8.	While generating pullback for SIL function "@$s17property_wrappers13GenericStructV10nontrivialAA18NontrivialLoadableVySfGvg".
       for getter for nontrivial (at swift/test/AutoDiff/SILOptimizer/property_wrappers.swift:49:31)
      0  swiftc                   0x00000001048ac4d8 llvm::sys::PrintStackTrace(llvm::raw_ostream&) + 40
      1  swiftc                   0x00000001048ab458 llvm::sys::RunSignalHandlers() + 248
      2  swiftc                   0x00000001048acacd SignalHandler(int) + 285
      3  libsystem_platform.dylib 0x00007fff6d6115fd _sigtramp + 29
      4  libsystem_platform.dylib 000000000000000000 _sigtramp + 18446603338681084448
      5  libsystem_c.dylib        0x00007fff6d4e7808 abort + 120
      6  swiftc                   0x000000010035f7b7 swift::performFrontend(llvm::ArrayRef<char const*>, char const*, void*, swift::FrontendObserver*)::$_1::__invoke(void*, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&, bool) + 711
      7  swiftc                   0x0000000104815d8b llvm::report_fatal_error(llvm::Twine const&, bool) + 251
      8  swiftc                   0x0000000104815c82 llvm::report_fatal_error(char const*, bool) + 50
      9  swiftc                   0x0000000100e2ceb2 swift::LinearLifetimeChecker::ErrorBuilder::handleError(llvm::function_ref<void ()>&&, bool) const + 594
      10 swiftc                   0x0000000100e2c63a swift::SILInstruction::verifyOperandOwnership() const + 442
      11 swiftc                   0x0000000100de08f2 swift::SILBuilder::createStore(swift::SILLocation, swift::SILValue, swift::SILValue, swift::StoreOwnershipQualifier) + 258
      12 swiftc                   0x0000000100dfcfd3 swift::SILBuilder::emitStoreValueOperation(swift::SILLocation, swift::SILValue, swift::SILValue, swift::StoreOwnershipQualifier) + 227
      13 swiftc                   0x0000000100830bfc swift::autodiff::PullbackEmitter::runForSemanticMemberGetter() + 3196
      
      

        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: