Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[SR-12779] Wrapped property differentiation: ownership error for setter pullback (non-trivial loadable property) #55224

Closed
dan-zheng opened this issue May 11, 2020 · 1 comment
Assignees
Labels
AutoDiff bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. compiler The Swift compiler in itself

Comments

@dan-zheng
Copy link
Collaborator

Previous ID SR-12779
Radar None
Original Reporter @dan-zheng
Type Bug
Status Resolved
Resolution Done
Additional Detail from JIRA
Votes 0
Component/s Compiler
Labels Bug, AutoDiff
Assignee @dan-zheng
Priority Medium

md5: 33f7db793aa428d1420cdfa7d18ae9b0

Issue 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 #&#8203;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)
...
@dan-zheng
Copy link
Collaborator Author

Fixed in #31701.

@swift-ci swift-ci transferred this issue from apple/swift-issues Apr 25, 2022
This issue was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
AutoDiff bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. compiler The Swift compiler in itself
Projects
None yet
Development

No branches or pull requests

1 participant