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-14290] SIL verification fails when differentiating a function of [[Double]] #56649

Closed
vojtamolda opened this issue Mar 1, 2021 · 6 comments
Labels
AutoDiff bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. compiler The Swift compiler in itself

Comments

@vojtamolda
Copy link
Contributor

Previous ID SR-14290
Radar rdar://problem/74876596
Original Reporter @vojtamolda
Type Bug
Status Resolved
Resolution Done

Attachment: Download

Environment

Development snapshot from 2/24 on macOS 11.2.2:

Apple Swift version 5.4-dev (LLVM 0eb4a6165bbbce5, Swift 288a0db849d8506)
Target: x86_64-apple-darwin20.3.0
Additional Detail from JIRA
Votes 1
Component/s Compiler
Labels Bug, AutoDiff
Assignee pgp (JIRA)
Priority Medium

md5: 4bd8592e77d009035310c80a5fd2e9bc

relates to:

  • SR-14298 [AutoDiff] SIL verification failed: non-contiguous lexical scope at -Onone

Issue Description:

Hello,

I don't have much experience filing Swift compiler bugs so please, excuse me if this is a duplicate. I tried searching for the keywords here but no dice. Closest match I could find is SR-8114 but it is marked as resolved.

The following code fails to compile:

import _Differentiation

let values: [[Double]] = [[0, 0], [0, 0]]
let const = 1.12345
let result = add(const, to: values)

@differentiable(reverse)
func add(_ const: Double, to values: [[Double]]) -> [[Double]] {
    var result = values
    for i in withoutDerivative(at: values.indices) {
        for j in withoutDerivative(at: values.indices) {
            result.updated(at: i, j, with: values[i][j] + const)
        }
    }
    return result
}

extension Array where Element == [Double] {
    @differentiable(reverse)
    mutating func updated(at i: Int, _ j: Int, with newValue: Double) {
        self[i][j] = newValue
    }
 
    @derivative(of: updated)
    mutating func vjpUpdated(at i: Int, _ j: Int, with newValue: Double)
    -> (value: Void, pullback: (inout TangentVector) -> (Double.TangentVector)) {
        self.updated(at: i, j, with: newValue)

        func pullback(dSelf: inout TangentVector) -> (Double.TangentVector) {
            let dElement = dSelf[i][j]
            dSelf.base[i].base[j] = 0
            return dElement
        }
        let value: Void = ()

        return (value, pullback)
    }
}

This is the error message when running swiftc main.swift. The full output is attached.

SIL verification failed: Basic block contains a non-contiguous lexical scope at -Onone: DS == LastSeenScope
Verifying instruction:
   **%360** = destructure_struct %359 : $_AD__$s4main3add_2toSaySaySdGGSd_AEtF_bb2__PB__src_0_wrt_0_1 // user: %370
->   switch_enum %360 : $_AD__$s4main3add_2toSaySaySdGGSd_AEtF_bb2__Pred__src_0_wrt_0_1, case #_AD__$s4main3add_2toSaySaySdGGSd_AEtF_bb2__Pred__src_0_wrt_0_1.bb1!enumelt: bb1 // id: %370
In function:

...

I've also noticed two things that may be helpful. First, differentiating a version of the code that uses a 1D array, i.e. [Double], works fine. And second, turning on the optimization, i.e. the `-O` flag works fine as well.

@typesanitizer
Copy link

cc @rxwei

@rxwei
Copy link
Member

rxwei commented Mar 1, 2021

Thanks. Looking into this now. @vojtamolda for faster responses, feel free to add label "AutoDiff" and ping me here.

@rxwei
Copy link
Member

rxwei commented Mar 1, 2021

@swift-ci create

@vojtamolda
Copy link
Contributor Author

Good to know for the next bug 😉

Thanks for looking into it, @rxwei! Please, let me know if you need more more details to reproduce it.

@porterchild
Copy link

SR-14298 may be a simpler reproducer

@rxwei
Copy link
Member

rxwei commented Apr 21, 2021

#36974

@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

4 participants