Skip to content

[SR-12887] Differentiation transform: debug scope error for VJPEmitter-generated function #55334

@dan-zheng

Description

@dan-zheng
Previous ID SR-12887
Radar None
Original Reporter @dan-zheng
Type Bug
Status Closed
Resolution Done
Environment

Depends on SR-12886 (alloc_stack [dynamic_lifetime] cloning) fix in https://github.com/apple/swift/pull/32031/commits/6f5d4ccd3da998c1ab1addcd5b0039ddcf3ee768.

Additional Detail from JIRA
Votes 0
Component/s Compiler
Labels Bug, CompilerCrash
Assignee @dan-zheng
Priority Medium

md5: d0dcf67e4ff40651e95783289bd4f86d

relates to:

  • SR-12886 SIL memory ownership error for differentiation transform + definite initialization

Issue Description:

import _Differentiation

enum Enum {
  case a
}

struct Tensor<T>: Differentiable {
  @noDerivative var x: T
  @noDerivative var optional: Int?

  init(_ x: T, _ e: Enum) {
    self.x = x
    switch e {
      case .a: optional = 1
    }
  }

  // Definite initialization triggers for this initializer.
  @differentiable
  init(_ x: T, _ other: Self) {
    self = Self(x, Enum.a)
  }
}
$ swift crash.swift
SIL verification failed: Basic block contains a non-contiguous lexical scope at -Onone: DS == LastSeenScope
Verifying instruction:
     %25 = builtin "trunc_Int2_Int1"(%24 : $Builtin.Int2) : $Builtin.Int1 // user: %27
->   cond_br %25, bb1, bb2                        // id: %27
In function:
// AD__$s5crash6TensorVyACyxGx_ADtcfC__vjp_src_0_wrt_1_l
sil hidden [ossa] @AD__$s5crash6TensorVyACyxGx_ADtcfC__vjp_src_0_wrt_1_l : $@convention(method) <τ_0_0> (@in τ_0_0, @in Tensor<τ_0_0>, @thin Tensor<τ_0_0>.Type) -> (@out Tensor<τ_0_0>, @owned @callee_guaranteed @substituted <τ_0_0, τ_0_1> (@in_guaranteed τ_0_0) -> @out τ_0_1 for <Tensor<τ_0_0>.TangentVector, Tensor<τ_0_0>.TangentVector>) {
// %0                                             // user: %61
// %1                                             // users: %63, %17, %8
// %2                                             // users: %62, %9
bb0(%0 : $*Tensor<τ_0_0>, %1 : $*τ_0_0, %2 : $*Tensor<τ_0_0>, %3 : $@thin Tensor<τ_0_0>.Type):
  %4 = alloc_stack $Builtin.Int2                  // users: %67, %57, %39, %24, %7
  %5 = alloc_stack [dynamic_lifetime] $Tensor<τ_0_0>, var, name "self" // users: %65, %64, %61, %46, %29, %23, %12
  %6 = integer_literal $Builtin.Int2, 0           // user: %7
  store %6 to [trivial] %4 : $*Builtin.Int2       // id: %7
  debug_value_addr %1 : $*τ_0_0, let, name "x", argno 1 // id: %8
  debug_value_addr %2 : $*Tensor<τ_0_0>, let, name "other", argno 2 // id: %9
  // function_ref variable initialization expression of Tensor.optional
  %10 = function_ref @$s5crash6TensorV8optionalSiSgvpfi : $@convention(thin) <τ_0_0> () -> Optional<Int> // user: %11
  %11 = apply %10<τ_0_0>() : $@convention(thin) <τ_0_0> () -> Optional<Int> // user: %13
  %12 = struct_element_addr %5 : $*Tensor<τ_0_0>, #Tensor.optional // user: %13
  store %11 to [trivial] %12 : $*Optional<Int>    // id: %13
  %14 = alloc_stack $Tensor<τ_0_0>               // users: %60, %58, %21
  %15 = metatype $@thin Tensor<τ_0_0>.Type       // user: %21
  %16 = alloc_stack $τ_0_0                       // users: %22, %21, %17
  copy_addr %1 to [initialization] %16 : $*τ_0_0 // id: %17
  %18 = metatype $@thin Enum.Type
  %19 = enum $Enum, #Enum.a!enumelt               // user: %21
  // function_ref Tensor.init(_:_:)
  %20 = function_ref @$s5crash6TensorVyACyxGx_AA4EnumOtcfC : $@convention(method) <τ_0_0> (@in τ_0_0, Enum, @thin Tensor<τ_0_0>.Type) -> @out Tensor<τ_0_0> // user: %21
  %21 = apply %20<τ_0_0>(%14, %16, %19, %15) : $@convention(method) <τ_0_0> (@in τ_0_0, Enum, @thin Tensor<τ_0_0>.Type) -> @out Tensor<τ_0_0>
  dealloc_stack %16 : $*τ_0_0                    // id: %22
  %23 = begin_access [modify] [static] %5 : $*Tensor<τ_0_0> // users: %59, %58
  %24 = load [trivial] %4 : $*Builtin.Int2        // user: %25
  %25 = builtin "trunc_Int2_Int1"(%24 : $Builtin.Int2) : $Builtin.Int1 // user: %27
  %26 = struct $_AD__$s5crash6TensorVyACyxGx_ADtcfC_bb0__PB__src_0_wrt_1_l<τ_0_0> () // users: %34, %28
  cond_br %25, bb1, bb2                           // id: %27

The debug scope issue is related to the VJP-generated pullback struct struct instruction:

%26 = struct $_AD__$s5crash6TensorVyACyxGx_ADtcfC_bb0__PB__src_0_wrt_1_l<τ_0_0> ()

Metadata

Metadata

Assignees

Labels

bugA deviation from expected or documented behavior. Also: expected but undesirable behavior.compilerThe Swift compiler itselfcrashBug: A crash, i.e., an abnormal termination of software

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions