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-12778] Wrapped property differentiation: ownership error for getter pullback (non-trivial loadable property) #55223

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-12778
Radar None
Original Reporter @dan-zheng
Type Bug
Status Closed
Resolution Done
Additional Detail from JIRA
Votes 0
Component/s Compiler
Labels Bug, AutoDiff
Assignee @dan-zheng
Priority Medium

md5: f506e7fa99832c0fc6b9eb3c3d551824

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 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 #&#8203;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
@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