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-14008] "Library not loaded: /usr/lib/swift/libswift_Differentiation.dylib" issue for swift test #4454

Open
dan-zheng opened this issue Dec 30, 2020 · 34 comments
Labels

Comments

@dan-zheng
Copy link
Contributor

Previous ID SR-14008
Radar None
Original Reporter @dan-zheng
Type Bug

Attachment: Download

Environment

https://swift.org/builds/development/xcode/swift-DEVELOPMENT-SNAPSHOT-2020-12-23-a/swift-DEVELOPMENT-SNAPSHOT-2020-12-23-a-osx.pkg

Apple Swift version 5.3-dev (LLVM bcee1b98f3b26c5, Swift dbcf7fe1bf76226)
Target: x86_64-apple-darwin19.6.0

Additional Detail from JIRA
Votes 2
Component/s Package Manager
Labels Bug, AutoDiff
Assignee None
Priority Medium

md5: 4ecdf44eee77ebefd73035c76ce61be5

Issue Description:

With development snapshot toolchains, swift build/swift run for executable/library targets importing _Differentiation has no problem.

However, running swift test for test targets importing _Differentiation fails with a dynamic linker issue:

$ swift test
[7/7] Linking SwiftDifferentiationPackageTests

* Build Completed!
2020-12-30 14:28:23.465 xctest[20739:26994456] The bundleSwiftDifferentiationPackageTests.xctestcouldnt be loaded because it is damaged or missing necessary resources. Try reinstalling the bundle.
2020-12-30 14:28:23.465 xctest[20739:26994456] (dlopen_preflight(/Users/danielzheng/Differentiation/.build/x86_64-apple-macosx/debug/SwiftDifferentiationPackageTests.xctest/Contents/MacOS/SwiftDifferentiationPackageTests): Library not loaded: /usr/lib/swift/libswift_Differentiation.dylib
  Referenced from: /Users/danielzheng/Differentiation/.build/x86_64-apple-macosx/debug/SwiftDifferentiationPackageTests.xctest/Contents/MacOS/SwiftDifferentiationPackageTests
  Reason: image not found)

Setting DYLD_LIBRARY_PATH to the toolchain library directory containing libswift_Differentiation.dylib does not help:

$ DYLD_LIBRARY_PATH=/Library/Developer/Toolchains/swift-DEVELOPMENT-SNAPSHOT-2020-12-23-a.xctoolchain/usr/lib/swift/macosx swift test
2020-12-30 14:29:55.586 xctest[20925:26997481] The bundle “SwiftDifferentiationPackageTests.xctest” couldn’t be loaded because it is damaged or missing necessary resources. Try reinstalling the bundle.
2020-12-30 14:29:55.586 xctest[20925:26997481] (dlopen_preflight(/Users/danielzheng/Differentiation/.build/x86_64-apple-macosx/debug/SwiftDifferentiationPackageTests.xctest/Contents/MacOS/SwiftDifferentiationPackageTests): Library not loaded: /usr/lib/swift/libswift_Differentiation.dylib
  Referenced from: /Users/danielzheng/Differentiation/.build/x86_64-apple-macosx/debug/SwiftDifferentiationPackageTests.xctest/Contents/MacOS/SwiftDifferentiationPackageTests
  Reason: image not found)

However, manually invoking the xctest command from swift test -v with DYLD_LIBRARY_PATH does work:

$ DYLD_LIBRARY_PATH=/Library/Developer/Toolchains/swift-DEVELOPMENT-SNAPSHOT-2020-12-23-a.xctoolchain/usr/lib/swift/macosx /Applications/Xcode-beta12_3.app/Contents/Developer/usr/bin/xctest /Users/danielzheng/Differentiation/.build/x86_64-apple-macosx/debug/SwiftDifferentiationPackageTests.xctest
Test Suite 'All tests' started at 2020-12-30 14:35:43.472
Test Suite 'SwiftDifferentiationPackageTests.xctest' started at 2020-12-30 14:35:43.473
Test Suite 'DifferentiationTests' started at 2020-12-30 14:35:43.473
Test Case '-[DifferentiationTests.DifferentiationTests testExample]' started.
Test Case '-[DifferentiationTests.DifferentiationTests testExample]' passed (0.112 seconds).
Test Suite 'DifferentiationTests' passed at 2020-12-30 14:35:43.585.
     Executed 1 test, with 0 failures (0 unexpected) in 0.112 (0.112) seconds
Test Suite 'SwiftDifferentiationPackageTests.xctest' passed at 2020-12-30 14:35:43.585.
     Executed 1 test, with 0 failures (0 unexpected) in 0.112 (0.112) seconds
Test Suite 'All tests' passed at 2020-12-30 14:35:43.585.
     Executed 1 test, with 0 failures (0 unexpected) in 0.112 (0.113) seconds

Maybe this is because swift test does not pass the DYLD_LIBRARY_PATH environment variable to the xctest invocation?

@dan-zheng
Copy link
Contributor Author

Potentially related PR: apple/swift#34216 The PR changes the install name directory (LC_ID_DYLIB) of libswift_Differentiation.dylib from @rpath/libswift_Differentiation.dylib to /usr/lib/swift/libswift_Differentiation.dylib.

@compnerd
Copy link
Collaborator

With SIP enabled, `DYLD_*` variables will be scrubbed, which means that `DYLD_LIBRARY_PATH` would not make it to the process AIUI.

A quick sanity check:

$ codesign --display --entitlements :- /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/Library/Xcode/Agents/xctest
Executable=/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/Library/Xcode/Agents/xctest
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
    <key>com.apple.security.get-task-allow</key>
    <true/>
</dict>
</plist>

This seems to not have the required com.apple.security.cs.allow-dyld-environment-variables entitlement as per https://developer.apple.com/documentation/bundleresources/entitlements/com_apple_security_cs_allow-dyld-environment-variables . On systems with SIP enabled, this would disallow execution with a custom toolchain (or a snapshot).

@compnerd
Copy link
Collaborator

CC: @lorentey @edymtt

@swift-ci
Copy link
Contributor

swift-ci commented Apr 5, 2021

Comment by Alexey Novakov (JIRA)

it seems my issue is related to this:

DYLD_LIBRARY_PATH=/Users/Alexey_Novakov/Library/Developer/Toolchains/swift-5.4-DEVELOPMENT-SNAPSHOT-2021-03-25-a.xctoolchain/usr/lib/swift/macosx xcrun --toolchain "Swift Development Snapshot" swift run
[1/1] Planning build

* Build Completed!dyld: lazy symbol binding failed: Symbol not found: _swift_autoDiffCreateLinearMapContext
 Referenced from: /Users/Alexey_Novakov/dev/git/swift-linear-regression/.build/x86_64-apple-macosx/debug/swift-linear-regression
 Expected in: /usr/lib/swift/libswiftCore.dylib

dyld: Symbol not found: _swift_autoDiffCreateLinearMapContext
 Referenced from: /Users/Alexey_Novakov/dev/git/swift-linear-regression/.build/x86_64-apple-macosx/debug/swift-linear-regression
 Expected in: /usr/lib/swift/libswiftCore.dylib

zsh: abort DYLD_LIBRARY_PATH= xcrun --toolchain "Swift Development Snapshot" swift run

otool paths:

% otool -L .build/x86_64-apple-macosx/debug/swift-linear-regression
.build/x86_64-apple-macosx/debug/swift-linear-regression:
    /System/Library/Frameworks/CoreFoundation.framework/Versions/A/CoreFoundation (compatibility version 150.0.0, current version 1770.255.0)
    /usr/lib/libobjc.A.dylib (compatibility version 1.0.0, current version 228.0.0)
    /usr/lib/libSystem.B.dylib (compatibility version 1.0.0, current version 1292.60.1)
    @rpath/libswiftCore.dylib (compatibility version 1.0.0, current version 0.0.0)
    @rpath/libswiftCoreFoundation.dylib (compatibility version 1.0.0, current version 0.0.0, weak)
    @rpath/libswiftCoreGraphics.dylib (compatibility version 1.0.0, current version 0.0.0, weak)
    @rpath/libswiftDarwin.dylib (compatibility version 1.0.0, current version 0.0.0, weak)
    @rpath/libswiftDispatch.dylib (compatibility version 1.0.0, current version 0.0.0, weak)
    @rpath/libswiftFoundation.dylib (compatibility version 1.0.0, current version 0.0.0, weak)
    @rpath/libswiftIOKit.dylib (compatibility version 1.0.0, current version 1.0.0, weak)
    @rpath/libswiftObjectiveC.dylib (compatibility version 1.0.0, current version 0.0.0, weak)
    @rpath/libswiftSwiftOnoneSupport.dylib (compatibility version 1.0.0, current version 0.0.0)
    @rpath/libswiftXPC.dylib (compatibility version 1.0.0, current version 1.1.0, weak)
    @rpath/libswift_Differentiation.dylib (compatibility version 1.0.0, current version 0.0.0)

@swift-ci swift-ci transferred this issue from apple/swift-issues Apr 25, 2022
@shahmishal shahmishal transferred this issue from apple/swift May 4, 2022
@philipturner
Copy link

I have a reproducer for this. The Swift package tests usually run just fine, but encountering this edge case stops them from happening. To reproduce:

  1. Make an empty Swift package.
  2. In the Sources/PACKAGE_NAME.swift file, paste the following:
import _Differentiation

extension Array.DifferentiableView: RandomAccessCollection
where Element: Differentiable {
  public typealias Element = Array<Element>.Element
  public typealias Index = Array<Element>.Index
  public typealias SubSequence = Array<Element>.SubSequence

  public subscript(position: Array<Element>.Index) -> Element {
    get { fatalError() }
    set { fatalError() }
  }

  public subscript(bounds: Range<Array<Element>.Index>) -> SubSequence {
    get { fatalError() }
    set { fatalError() }
  }

  public var startIndex: Index { fatalError() }
  public var endIndex: Index { fatalError() }
}
  1. You can have anything in the Tests directory, as long as there's a file. It can even be blank.
  2. Run swift test from the command line, and you get the following output. Notice the very last line.
(base) philipturner@m1-max-mbp sr14008 % swift test
objc[92004]: Class _TtCs25CheckedContinuationCanary is implemented in both /usr/lib/swift/libswift_Concurrency.dylib (0x218cb4450) and /Library/Developer/Toolchains/swift-DEVELOPMENT-SNAPSHOT-2022-05-04-a.xctoolchain/usr/lib/swift/macosx/libswift_Concurrency.dylib (0x106cfc8c8). One of the two will be used. Which one is undefined.
objc[92004]: Class _TtCs17DispatchQueueShim is implemented in both /usr/lib/swift/libswift_Concurrency.dylib (0x218cb44e0) and /Library/Developer/Toolchains/swift-DEVELOPMENT-SNAPSHOT-2022-05-04-a.xctoolchain/usr/lib/swift/macosx/libswift_Concurrency.dylib (0x106cfc958). One of the two will be used. Which one is undefined.
objc[92004]: Class _TtCs9MainActor is implemented in both /usr/lib/swift/libswift_Concurrency.dylib (0x218cb4570) and /Library/Developer/Toolchains/swift-DEVELOPMENT-SNAPSHOT-2022-05-04-a.xctoolchain/usr/lib/swift/macosx/libswift_Concurrency.dylib (0x106cfc9f0). One of the two will be used. Which one is undefined.
warning: Usage of /Users/philipturner/Library/org.swift.swiftpm/collections.json has been deprecated. Please delete it and use the new /Users/philipturner/Library/org.swift.swiftpm/configuration/collections.json instead.
Building for debugging...
[3/3] Linking sr14008PackageTests
Build complete! (0.36s)
2022-05-14 15:30:31.658 xctest[92072:1569176] The bundle “sr14008PackageTests.xctest” couldn’t be loaded. Try reinstalling the bundle.
2022-05-14 15:30:31.658 xctest[92072:1569176] (dlopen(/Users/philipturner/Desktop/sr14008/.build/arm64-apple-macosx/debug/sr14008PackageTests.xctest/Contents/MacOS/sr14008PackageTests, 0x0109): Symbol not found: _$sSa16_DifferentiationAA14DifferentiableRzlE0B4ViewVMn
  Referenced from: /Users/philipturner/Desktop/sr14008/.build/arm64-apple-macosx/debug/sr14008PackageTests.xctest/Contents/MacOS/sr14008PackageTests
  Expected in: /usr/lib/swift/libswift_Differentiation.dylib)

@novakov-alexey I wasn't able to reproduce your compile failure from #4454 (comment). From your recollection, did it involve something reminiscent of my reproducer?

@philipturner
Copy link

philipturner commented May 14, 2022

Related: tensorflow/swift-models#727

Note as a result of this, the build process for testing s4tf/s4tf (formerly swift-apis) becomes greatly more complex. But it's still possible (this is an example of a workaround).

# prepare for building S4TF
export TOOLCHAINS="org.swift.57202205041a"
tensorflow_version="2.8.0"
DESTDIR=${PWD}/Library/tensorflow-${tensorflow_version}

# build S4TF
cd s4tf
swift build -Xswiftc -DTENSORFLOW_USE_STANDARD_TOOLCHAIN \
  -Xcc -I${DESTDIR}/usr/include -Xlinker -L${DESTDIR}/usr/lib

cp ${DESTDIR}/usr/lib/libx10.dylib .build/$(uname -m)-apple-macosx/debug/libx10.dylib

# fails, but that's okay
swift test -Xswiftc -DTENSORFLOW_USE_STANDARD_TOOLCHAIN \
  -Xcc -I${DESTDIR}/usr/include -Xlinker -L${DESTDIR}/usr/lib

export DYLD_LIBRARY_PATH="/Library/Developer/Toolchains/swift-DEVELOPMENT-SNAPSHOT-2022-05-04-a.xctoolchain/usr/lib/swift/macosx"

# actually runs the tests
/Applications/Xcode.app/Contents/Developer/usr/bin/xctest \
  .build/$(uname -m)-apple-macosx/debug/TensorFlowPackageTests.xctest

@philipturner
Copy link

philipturner commented Jun 19, 2022

Perhaps I could remove this crash from S4TF by taking the Array.DifferentiableView extension out of S4TF and into the Swift Standard Library. The code moved there may or may not conflict with the intentions of DifferentiableCollection, if that protocol ever makes it into the stdlib. After that, we may need a second reproducer because the original one won't fail anymore.

Would my idea be permissible?

@tomerd
Copy link
Member

tomerd commented Jun 20, 2022

@abertelrud @neonichu @lorentey - this echoes some of our discussion, correct?

@lorentey
Copy link
Member

The contents of the _Differentiation module have not gone through Swift Evolution. It is not an ABI stable component of the Standard Library and therefore it isn't shipping as a public SDK component on ABI stable platforms. Moving Array.DifferentiableView to the core stdlib is not an option until it has been proposed & accepted as public API.

(Beware: while /usr/lib/swift/libswift_Differentation.dylib does ship in some versions of macOS, it is not a public library on any Apple platform. Accordingly, it may disappear or its contents may change in any OS release, including point releases. Do not link to it from any production binary -- such code will break.)

% otool -L .build/x86_64-apple-macosx/debug/swift-linear-regression
.build/x86_64-apple-macosx/debug/swift-linear-regression:
    /System/Library/Frameworks/CoreFoundation.framework/Versions/A/CoreFoundation (compatibility version 150.0.0, current version 1770.255.0)
    /usr/lib/libobjc.A.dylib (compatibility version 1.0.0, current version 228.0.0)
    /usr/lib/libSystem.B.dylib (compatibility version 1.0.0, current version 1292.60.1)
    @rpath/libswiftCore.dylib (compatibility version 1.0.0, current version 0.0.0)
    @rpath/libswiftCoreFoundation.dylib (compatibility version 1.0.0, current version 0.0.0, weak)
    @rpath/libswiftCoreGraphics.dylib (compatibility version 1.0.0, current version 0.0.0, weak)
    @rpath/libswiftDarwin.dylib (compatibility version 1.0.0, current version 0.0.0, weak)
    @rpath/libswiftDispatch.dylib (compatibility version 1.0.0, current version 0.0.0, weak)
    @rpath/libswiftFoundation.dylib (compatibility version 1.0.0, current version 0.0.0, weak)
    @rpath/libswiftIOKit.dylib (compatibility version 1.0.0, current version 1.0.0, weak)
    @rpath/libswiftObjectiveC.dylib (compatibility version 1.0.0, current version 0.0.0, weak)
    @rpath/libswiftSwiftOnoneSupport.dylib (compatibility version 1.0.0, current version 0.0.0)
    @rpath/libswiftXPC.dylib (compatibility version 1.0.0, current version 1.1.0, weak)
    @rpath/libswift_Differentiation.dylib (compatibility version 1.0.0, current version 0.0.0)

The version of dyld in macOS Monterey (and later) does not support using DYLD_LIBRARY_PATH to override libraries loaded via @rpath. To avoid duplicate loading of these modules (and subsequent crashes), arrange for the binary to pick up its dependencies directly from /usr/lib/swift. One way to do this is to set the minimum deployment target to macOS 10.15 or later -- this stops the compiler from using rpath-relative install names for these libraries. (As it can safely assume that /usr/lib/libswiftCore.dylib etc do exist.)

(If this dyld limitation is causing you trouble, please do file a feedback report describing the problem -- such reports do help prioritizing the issue.)

@philipturner
Copy link

Moving Array.DifferentiableView to the core stdlib is not an option until it has been proposed & accepted as public API.

Array.DifferentiableView is already in the core stdlib. It is just removed on release toolchains, where I have to depend on philipturner/differentiation to add it back in.

@lorentey
Copy link
Member

Also beware -- custom builds of the Swift Standard Library (whether they were produced by manually building the stdlib from the apple/swift repo, or by downloading one of the toolchain snapshots from swift.org) aren't drop-in replacements for the stdlib that ships in any OS version.

Such stdlib builds are not guaranteed to interoperate with OS components that load the Swift stdlib, including Foundation.

@philipturner
Copy link

One way to do this is to set the minimum deployment target to macOS 10.15 or later

So if I set platforms to [.macOS(.v10_15)] in the Swift package manifest, you expect the error to go away?

@lorentey
Copy link
Member

Array.DifferentiableView is already in the core stdlib. It is just removed on release toolchains, where I have to depend on philipturner/differentiation to add it back in.

What I call the "core stdlib" lives in the stdlib/public/core directory in the source tree.

The code you've linked to is part of a separate, standalone module called _Differentiation. This module is not part of the public API of the Swift Standard Library, as (AFAIK) it has not gone through Swift Evolution.

@philipturner
Copy link

philipturner commented Jun 20, 2022

The code you've linked to is part of a separate, standalone module called _Differentiation. This module is not part of the public API of the Swift Standard Library, as (AFAIK) it has not gone through Swift Evolution.

Yes; I was asking if it's okay to make a PR to apple/swift, where I add this extension to code in the _Differentiation module inside stdlib/public/Differentiation. I wanted to know whether there was a reasonable chance such a PR would be merged, and integrated into nightly snapshots.

@lorentey
Copy link
Member

So if I set platforms to [.macOS(.v10_15)] in the Swift package manifest, you expect the error to go away?

Setting a higher minimum deployment target will cause the compiler to use absolute /usr/lib/swift install names for these dylibs, allowing DYLD_LIBRARY_PATH to work properly on macOS Monterey and Ventura.

@lorentey
Copy link
Member

Yes; I was asking if it's okay to make a PR to apple/swift, where I add this extension to code in the _Differentiation module inside stdlib/public/Differentiation. I wanted to know whether there was a reasonable chance such a PR would be merged, and integrated into nightly snapshots.

I don't see why not. Changes to private parts of the stdlib merely require an approval from a stdlib code owner -- the only challenge there is to find someone who has time to understand & review the addition. This is a much lower bar than going through Swift Evolution, but it's not entirely free: new entry points come with a code size & maintenance cost.

@philipturner
Copy link

How do you set the minimum deployment target when compiling with SwiftPM? I tried changing platforms: in the package manifest and the test still failed.

@lorentey
Copy link
Member

Did adding the version number cause the resulting binary to use /usr/lib/swift as the install name for its libswift*.dylib dependencies?

@lorentey
Copy link
Member

Yes; I was asking if it's okay to make a PR to apple/swift, where I add this extension to code in the _Differentiation module inside stdlib/public/Differentiation. I wanted to know whether there was a reasonable chance such a PR would be merged, and integrated into nightly snapshots.

On closer look, isn't the extension already in the _Differentiation module? Obviously I don't quite understand what you're asking to do here.

@philipturner
Copy link

On closer look, isn't the extension already in the _Differentiation module?

Where did you find the extension? I didn't see anything that conforms Array.DifferentiableView to RandomAccessCollection in ArrayDifferentiation.swift.

@philipturner
Copy link

philipturner commented Jun 20, 2022

Did adding the version number cause the resulting binary to use /usr/lib/swift as the install name for its libswift*.dylib dependencies?

It's the same before

Terminal output
(base) philipturner@m1-max-mbp MyPackage % rm -rf .build
(base) philipturner@m1-max-mbp MyPackage % swift build                                                                                               
warning: Usage of /Users/philipturner/Library/org.swift.swiftpm/collections.json has been deprecated. Please delete it and use the new /Users/philipturner/Library/org.swift.swiftpm/configuration/collections.json instead.
Building for debugging...
[2/2] Compiling MyPackage MyPackage.swift
Build complete! (1.24s)
(base) philipturner@m1-max-mbp MyPackage % swift test
warning: Usage of /Users/philipturner/Library/org.swift.swiftpm/collections.json has been deprecated. Please delete it and use the new /Users/philipturner/Library/org.swift.swiftpm/configuration/collections.json instead.
Building for debugging...
[3/3] Linking MyPackagePackageTests
Build complete! (14.12s)
2022-06-20 17:58:58.478 xctest[75824:8397467] The bundle “MyPackagePackageTests.xctest” couldn’t be loaded. Try reinstalling the bundle.
2022-06-20 17:58:58.479 xctest[75824:8397467] (dlopen(/Users/philipturner/Desktop/MyPackage/.build/arm64-apple-macosx/debug/MyPackagePackageTests.xctest/Contents/MacOS/MyPackagePackageTests, 0x0109): Symbol not found: _$sSa16_DifferentiationAA14DifferentiableRzlE0B4ViewVMn
  Referenced from: /Users/philipturner/Desktop/MyPackage/.build/arm64-apple-macosx/debug/MyPackagePackageTests.xctest/Contents/MacOS/MyPackagePackageTests
  Expected in: /usr/lib/swift/libswift_Differentiation.dylib)
(base) philipturner@m1-max-mbp MyPackage % otool -L .build/arm64-apple-macosx/debug/MyPackagePackageTests.xctest/Contents/MacOS/MyPackagePackageTests
.build/arm64-apple-macosx/debug/MyPackagePackageTests.xctest/Contents/MacOS/MyPackagePackageTests:
	/usr/lib/libobjc.A.dylib (compatibility version 1.0.0, current version 228.0.0)
	/usr/lib/libSystem.B.dylib (compatibility version 1.0.0, current version 1311.100.3)
	/usr/lib/swift/libswiftAppKit.dylib (compatibility version 1.0.0, current version 109.0.0, weak)
	/usr/lib/swift/libswiftCore.dylib (compatibility version 1.0.0, current version 0.0.0)
	/usr/lib/swift/libswiftCoreData.dylib (compatibility version 1.0.0, current version 19.0.0, weak)
	/usr/lib/swift/libswiftCoreFoundation.dylib (compatibility version 1.0.0, current version 14.0.0, weak)
	/usr/lib/swift/libswiftCoreGraphics.dylib (compatibility version 1.0.0, current version 2.0.0, weak)
	/usr/lib/swift/libswiftCoreImage.dylib (compatibility version 1.0.0, current version 2.0.0, weak)
	/usr/lib/swift/libswiftDarwin.dylib (compatibility version 1.0.0, current version 0.0.0, weak)
	/usr/lib/swift/libswiftDispatch.dylib (compatibility version 1.0.0, current version 11.0.0, weak)
	/usr/lib/swift/libswiftFoundation.dylib (compatibility version 1.0.0, current version 72.105.0)
	/usr/lib/swift/libswiftIOKit.dylib (compatibility version 1.0.0, current version 1.0.0, weak)
	/usr/lib/swift/libswiftMetal.dylib (compatibility version 1.0.0, current version 261.13.0, weak)
	/usr/lib/swift/libswiftObjectiveC.dylib (compatibility version 1.0.0, current version 3.0.0, weak)
	/usr/lib/swift/libswiftQuartzCore.dylib (compatibility version 1.0.0, current version 3.0.0, weak)
	/usr/lib/swift/libswiftXPC.dylib (compatibility version 1.0.0, current version 1.1.0, weak)
	/usr/lib/swift/libswift_Differentiation.dylib (compatibility version 1.0.0, current version 0.0.0)
	@rpath/XCTest.framework/Versions/A/XCTest (compatibility version 1.0.0, current version 20501.0.0)
	@rpath/libXCTestSwiftSupport.dylib (compatibility version 1.0.0, current version 1.0.0)

And after

Terminal output
(base) philipturner@m1-max-mbp MyPackage % rm -rf .build
(base) philipturner@m1-max-mbp MyPackage % swift build                                                                                               
warning: Usage of /Users/philipturner/Library/org.swift.swiftpm/collections.json has been deprecated. Please delete it and use the new /Users/philipturner/Library/org.swift.swiftpm/configuration/collections.json instead.
Building for debugging...
[2/2] Compiling MyPackage MyPackage.swift
Build complete! (1.18s)
(base) philipturner@m1-max-mbp MyPackage % swift test
warning: Usage of /Users/philipturner/Library/org.swift.swiftpm/collections.json has been deprecated. Please delete it and use the new /Users/philipturner/Library/org.swift.swiftpm/configuration/collections.json instead.
Building for debugging...
[3/3] Linking MyPackagePackageTests
Build complete! (14.18s)
2022-06-20 17:59:57.955 xctest[75961:8398652] The bundle “MyPackagePackageTests.xctest” couldn’t be loaded. Try reinstalling the bundle.
2022-06-20 17:59:57.955 xctest[75961:8398652] (dlopen(/Users/philipturner/Desktop/MyPackage/.build/arm64-apple-macosx/debug/MyPackagePackageTests.xctest/Contents/MacOS/MyPackagePackageTests, 0x0109): Symbol not found: _$sSa16_DifferentiationAA14DifferentiableRzlE0B4ViewVMn
  Referenced from: /Users/philipturner/Desktop/MyPackage/.build/arm64-apple-macosx/debug/MyPackagePackageTests.xctest/Contents/MacOS/MyPackagePackageTests
  Expected in: /usr/lib/swift/libswift_Differentiation.dylib)
(base) philipturner@m1-max-mbp MyPackage % otool -L .build/arm64-apple-macosx/debug/MyPackagePackageTests.xctest/Contents/MacOS/MyPackagePackageTests
.build/arm64-apple-macosx/debug/MyPackagePackageTests.xctest/Contents/MacOS/MyPackagePackageTests:
	/usr/lib/libobjc.A.dylib (compatibility version 1.0.0, current version 228.0.0)
	/usr/lib/libSystem.B.dylib (compatibility version 1.0.0, current version 1311.100.3)
	/usr/lib/swift/libswiftAppKit.dylib (compatibility version 1.0.0, current version 109.0.0, weak)
	/usr/lib/swift/libswiftCore.dylib (compatibility version 1.0.0, current version 0.0.0)
	/usr/lib/swift/libswiftCoreData.dylib (compatibility version 1.0.0, current version 19.0.0, weak)
	/usr/lib/swift/libswiftCoreFoundation.dylib (compatibility version 1.0.0, current version 14.0.0, weak)
	/usr/lib/swift/libswiftCoreGraphics.dylib (compatibility version 1.0.0, current version 2.0.0, weak)
	/usr/lib/swift/libswiftCoreImage.dylib (compatibility version 1.0.0, current version 2.0.0, weak)
	/usr/lib/swift/libswiftDarwin.dylib (compatibility version 1.0.0, current version 0.0.0, weak)
	/usr/lib/swift/libswiftDispatch.dylib (compatibility version 1.0.0, current version 11.0.0, weak)
	/usr/lib/swift/libswiftFoundation.dylib (compatibility version 1.0.0, current version 72.105.0)
	/usr/lib/swift/libswiftIOKit.dylib (compatibility version 1.0.0, current version 1.0.0, weak)
	/usr/lib/swift/libswiftMetal.dylib (compatibility version 1.0.0, current version 261.13.0, weak)
	/usr/lib/swift/libswiftObjectiveC.dylib (compatibility version 1.0.0, current version 3.0.0, weak)
	/usr/lib/swift/libswiftQuartzCore.dylib (compatibility version 1.0.0, current version 3.0.0, weak)
	/usr/lib/swift/libswiftXPC.dylib (compatibility version 1.0.0, current version 1.1.0, weak)
	/usr/lib/swift/libswift_Differentiation.dylib (compatibility version 1.0.0, current version 0.0.0)
	@rpath/XCTest.framework/Versions/A/XCTest (compatibility version 1.0.0, current version 20501.0.0)
	@rpath/libXCTestSwiftSupport.dylib (compatibility version 1.0.0, current version 1.0.0)

Adding platforms: [.macOS(.v12)], to Package.swift.

@philipturner
Copy link

philipturner commented Jun 20, 2022

Also, here's my /usr/lib and /usr/lib/swift:

Screen Shot 2022-06-20 at 5 52 22 PM

@lorentey
Copy link
Member

lorentey commented Jun 20, 2022

Ah thanks! What I missed is that you want to add this conformance to this existing type. I don't have an opinion on whether that is a desirable thing -- I am not familiar with this module -- however, I doubt moving the conformance to the Swift repository will resolve the root cause of the loader error.

objc[92004]: Class _TtCs25CheckedContinuationCanary is implemented in both /usr/lib/swift/libswift_Concurrency.dylib (0x218cb4450) and /Library/Developer/Toolchains/swift-DEVELOPMENT-SNAPSHOT-2022-05-04-a.xctoolchain/usr/lib/swift/macosx/libswift_Concurrency.dylib (0x106cfc8c8). One of the two will be used. Which one is undefined.
objc[92004]: Class _TtCs17DispatchQueueShim is implemented in both /usr/lib/swift/libswift_Concurrency.dylib (0x218cb44e0) and /Library/Developer/Toolchains/swift-DEVELOPMENT-SNAPSHOT-2022-05-04-a.xctoolchain/usr/lib/swift/macosx/libswift_Concurrency.dylib (0x106cfc958). One of the two will be used. Which one is undefined.
objc[92004]: Class _TtCs9MainActor is implemented in both /usr/lib/swift/libswift_Concurrency.dylib (0x218cb4570) and /Library/Developer/Toolchains/swift-DEVELOPMENT-SNAPSHOT-2022-05-04-a.xctoolchain/usr/lib/swift/macosx/libswift_Concurrency.dylib (0x106cfc9f0). One of the two will be used. Which one is undefined.

These messages indicate that the library libswift_Concurrency.dylib has been loaded twice. This is a significant problem that must be fixed.

The most likely cause is that the executable invoked here has an @rpath-relative load command for this library (to support back deployment of concurrency features below macOS 12), while it also loads OS frameworks that load it directly from /usr/lib/swift. (E.g., such a combination can trigger the dyld issue with DYLD_LIBRARY_PATH, but if the binary's RPATH is set incorrectly, it can also lead to two different concurrency libraries getting loaded by default, even without a library path override.)

The first order of business would be to figure out which binary is doing this and fixing it. (It may well be a binary that ships in the downloadable toolchain -- I know we have such issues there, we've seen something similar last week. 😞)

2022-05-14 15:30:31.658 xctest[92072:1569176] The bundle “sr14008PackageTests.xctest” couldn’t be loaded. Try reinstalling the bundle.
2022-05-14 15:30:31.658 xctest[92072:1569176] (dlopen(/Users/philipturner/Desktop/sr14008/.build/arm64-apple-macosx/debug/sr14008PackageTests.xctest/Contents/MacOS/sr14008PackageTests, 0x0109): Symbol not found: _$sSa16_DifferentiationAA14DifferentiableRzlE0B4ViewVMn
  Referenced from: /Users/philipturner/Desktop/sr14008/.build/arm64-apple-macosx/debug/sr14008PackageTests.xctest/Contents/MacOS/sr14008PackageTests
  Expected in: /usr/lib/swift/libswift_Differentiation.dylib)

This implies that the loader tried to load the non-public library /usr/lib/swift/libswift_Differentiation.dylib from the OS, so either DYLD_LIBRARY_PATH wasn't set correctly, or the launched process did not inherit the environment variable as expected.

As the _Differentiation module is configured as if it was a stdlib component (even though it doesn't ship as one), the right way to test its contents is to override its location via DYLD_LIBRARY_PATH. Unfortunately, since it shares the same install location as actual stdlib modules, overriding the library path also overrides the stdlib, which is generally inadvisable if you're also importing Foundation.

Did adding the version number cause the resulting binary to use /usr/lib/swift as the install name for its libswift*.dylib dependencies?
It's the same before

Oh I see, so Alexey's comment above is outdated. That's great -- so the dyld issue does not apply: you only need to figure out how to get DYLD_LIBRARY_PATH to apply to the launched process.

@lorentey
Copy link
Member

Also, here's my /usr/lib and /usr/lib/swift:

Most dylibs whose install names are under these directories do not actually exist there as separate files; instead, the dynamic loader picks them up from the system-wide dyld shared cache.

@philipturner
Copy link

philipturner commented Jun 20, 2022

These messages indicate that the library libswift_Concurrency.dylib has been loaded twice. This is a significant problem that must be fixed.

That error is present on trunk snapshots, but not on 5.7 branch snapshots. For example, the 2022-05-04 and 2022-06-08 trunk snapshots produce the concurrency error. The 2022-06-04 v5.7 and 2022-06-13 v5.7 snapshots do not. I was building the package using the June 13, 2022 v5.7 Development Snapshot in #4454 (comment). Same behavior on the June 8 trunk snapshot though.

Terminal output (before, then after adding platforms to Package.swift)
(base) philipturner@m1-max-mbp MyPackage % rm -rf .build
(base) philipturner@m1-max-mbp MyPackage % swift build
objc[76736]: Class _TtCs25CheckedContinuationCanary is implemented in both /usr/lib/swift/libswift_Concurrency.dylib (0x235b06670) and /Library/Developer/Toolchains/swift-DEVELOPMENT-SNAPSHOT-2022-06-08-a.xctoolchain/usr/lib/swift/macosx/libswift_Concurrency.dylib (0x1028e88c8). One of the two will be used. Which one is undefined.
objc[76736]: Class _TtCs17DispatchQueueShim is implemented in both /usr/lib/swift/libswift_Concurrency.dylib (0x235b06700) and /Library/Developer/Toolchains/swift-DEVELOPMENT-SNAPSHOT-2022-06-08-a.xctoolchain/usr/lib/swift/macosx/libswift_Concurrency.dylib (0x1028e8958). One of the two will be used. Which one is undefined.
objc[76736]: Class _TtCs9MainActor is implemented in both /usr/lib/swift/libswift_Concurrency.dylib (0x235b06790) and /Library/Developer/Toolchains/swift-DEVELOPMENT-SNAPSHOT-2022-06-08-a.xctoolchain/usr/lib/swift/macosx/libswift_Concurrency.dylib (0x1028e89f0). One of the two will be used. Which one is undefined.
warning: Usage of /Users/philipturner/Library/org.swift.swiftpm/collections.json has been deprecated. Please delete it and use the new /Users/philipturner/Library/org.swift.swiftpm/configuration/collections.json instead.
Building for debugging...
[2/2] Emitting module MyPackage
Build complete! (1.31s)
(base) philipturner@m1-max-mbp MyPackage % swift test
objc[76816]: Class _TtCs25CheckedContinuationCanary is implemented in both /usr/lib/swift/libswift_Concurrency.dylib (0x235b06670) and /Library/Developer/Toolchains/swift-DEVELOPMENT-SNAPSHOT-2022-06-08-a.xctoolchain/usr/lib/swift/macosx/libswift_Concurrency.dylib (0x1044808c8). One of the two will be used. Which one is undefined.
objc[76816]: Class _TtCs17DispatchQueueShim is implemented in both /usr/lib/swift/libswift_Concurrency.dylib (0x235b06700) and /Library/Developer/Toolchains/swift-DEVELOPMENT-SNAPSHOT-2022-06-08-a.xctoolchain/usr/lib/swift/macosx/libswift_Concurrency.dylib (0x104480958). One of the two will be used. Which one is undefined.
objc[76816]: Class _TtCs9MainActor is implemented in both /usr/lib/swift/libswift_Concurrency.dylib (0x235b06790) and /Library/Developer/Toolchains/swift-DEVELOPMENT-SNAPSHOT-2022-06-08-a.xctoolchain/usr/lib/swift/macosx/libswift_Concurrency.dylib (0x1044809f0). One of the two will be used. Which one is undefined.
warning: Usage of /Users/philipturner/Library/org.swift.swiftpm/collections.json has been deprecated. Please delete it and use the new /Users/philipturner/Library/org.swift.swiftpm/configuration/collections.json instead.
Building for debugging...
[3/3] Linking MyPackagePackageTests
Build complete! (14.48s)
2022-06-20 18:39:56.054 xctest[76878:8415670] The bundle “MyPackagePackageTests.xctest” couldn’t be loaded. Try reinstalling the bundle.
2022-06-20 18:39:56.054 xctest[76878:8415670] (dlopen(/Users/philipturner/Desktop/MyPackage/.build/arm64-apple-macosx/debug/MyPackagePackageTests.xctest/Contents/MacOS/MyPackagePackageTests, 0x0109): Symbol not found: _$sSa16_DifferentiationAA14DifferentiableRzlE0B4ViewVMn
  Referenced from: /Users/philipturner/Desktop/MyPackage/.build/arm64-apple-macosx/debug/MyPackagePackageTests.xctest/Contents/MacOS/MyPackagePackageTests
  Expected in: /usr/lib/swift/libswift_Differentiation.dylib)
(base) philipturner@m1-max-mbp MyPackage % otool -L .build/arm64-apple-macosx/debug/MyPackagePackageTests.xctest/Contents/MacOS/MyPackagePackageTests
.build/arm64-apple-macosx/debug/MyPackagePackageTests.xctest/Contents/MacOS/MyPackagePackageTests:
	/usr/lib/libobjc.A.dylib (compatibility version 1.0.0, current version 228.0.0)
	/usr/lib/libSystem.B.dylib (compatibility version 1.0.0, current version 1311.100.3)
	/usr/lib/swift/libswiftAppKit.dylib (compatibility version 1.0.0, current version 109.0.0, weak)
	/usr/lib/swift/libswiftCore.dylib (compatibility version 1.0.0, current version 0.0.0)
	/usr/lib/swift/libswiftCoreData.dylib (compatibility version 1.0.0, current version 19.0.0, weak)
	/usr/lib/swift/libswiftCoreFoundation.dylib (compatibility version 1.0.0, current version 14.0.0, weak)
	/usr/lib/swift/libswiftCoreGraphics.dylib (compatibility version 1.0.0, current version 2.0.0, weak)
	/usr/lib/swift/libswiftCoreImage.dylib (compatibility version 1.0.0, current version 2.0.0, weak)
	/usr/lib/swift/libswiftDarwin.dylib (compatibility version 1.0.0, current version 0.0.0, weak)
	/usr/lib/swift/libswiftDispatch.dylib (compatibility version 1.0.0, current version 11.0.0, weak)
	/usr/lib/swift/libswiftFoundation.dylib (compatibility version 1.0.0, current version 72.105.0)
	/usr/lib/swift/libswiftIOKit.dylib (compatibility version 1.0.0, current version 1.0.0, weak)
	/usr/lib/swift/libswiftMetal.dylib (compatibility version 1.0.0, current version 261.13.0, weak)
	/usr/lib/swift/libswiftObjectiveC.dylib (compatibility version 1.0.0, current version 3.0.0, weak)
	/usr/lib/swift/libswiftQuartzCore.dylib (compatibility version 1.0.0, current version 3.0.0, weak)
	/usr/lib/swift/libswiftXPC.dylib (compatibility version 1.0.0, current version 1.1.0, weak)
	/usr/lib/swift/libswift_Differentiation.dylib (compatibility version 1.0.0, current version 0.0.0)
	@rpath/XCTest.framework/Versions/A/XCTest (compatibility version 1.0.0, current version 20501.0.0)
	@rpath/libXCTestSwiftSupport.dylib (compatibility version 1.0.0, current version 1.0.0)
(base) philipturner@m1-max-mbp MyPackage % rm -rf .build                                                                                             
(base) philipturner@m1-max-mbp MyPackage % swift build
objc[76886]: Class _TtCs25CheckedContinuationCanary is implemented in both /usr/lib/swift/libswift_Concurrency.dylib (0x235b06670) and /Library/Developer/Toolchains/swift-DEVELOPMENT-SNAPSHOT-2022-06-08-a.xctoolchain/usr/lib/swift/macosx/libswift_Concurrency.dylib (0x1044148c8). One of the two will be used. Which one is undefined.
objc[76886]: Class _TtCs17DispatchQueueShim is implemented in both /usr/lib/swift/libswift_Concurrency.dylib (0x235b06700) and /Library/Developer/Toolchains/swift-DEVELOPMENT-SNAPSHOT-2022-06-08-a.xctoolchain/usr/lib/swift/macosx/libswift_Concurrency.dylib (0x104414958). One of the two will be used. Which one is undefined.
objc[76886]: Class _TtCs9MainActor is implemented in both /usr/lib/swift/libswift_Concurrency.dylib (0x235b06790) and /Library/Developer/Toolchains/swift-DEVELOPMENT-SNAPSHOT-2022-06-08-a.xctoolchain/usr/lib/swift/macosx/libswift_Concurrency.dylib (0x1044149f0). One of the two will be used. Which one is undefined.
warning: Usage of /Users/philipturner/Library/org.swift.swiftpm/collections.json has been deprecated. Please delete it and use the new /Users/philipturner/Library/org.swift.swiftpm/configuration/collections.json instead.
Building for debugging...
[2/2] Compiling MyPackage MyPackage.swift
Build complete! (1.00s)
(base) philipturner@m1-max-mbp MyPackage % swift test
objc[76929]: Class _TtCs25CheckedContinuationCanary is implemented in both /usr/lib/swift/libswift_Concurrency.dylib (0x235b06670) and /Library/Developer/Toolchains/swift-DEVELOPMENT-SNAPSHOT-2022-06-08-a.xctoolchain/usr/lib/swift/macosx/libswift_Concurrency.dylib (0x1045908c8). One of the two will be used. Which one is undefined.
objc[76929]: Class _TtCs17DispatchQueueShim is implemented in both /usr/lib/swift/libswift_Concurrency.dylib (0x235b06700) and /Library/Developer/Toolchains/swift-DEVELOPMENT-SNAPSHOT-2022-06-08-a.xctoolchain/usr/lib/swift/macosx/libswift_Concurrency.dylib (0x104590958). One of the two will be used. Which one is undefined.
objc[76929]: Class _TtCs9MainActor is implemented in both /usr/lib/swift/libswift_Concurrency.dylib (0x235b06790) and /Library/Developer/Toolchains/swift-DEVELOPMENT-SNAPSHOT-2022-06-08-a.xctoolchain/usr/lib/swift/macosx/libswift_Concurrency.dylib (0x1045909f0). One of the two will be used. Which one is undefined.
warning: Usage of /Users/philipturner/Library/org.swift.swiftpm/collections.json has been deprecated. Please delete it and use the new /Users/philipturner/Library/org.swift.swiftpm/configuration/collections.json instead.
Building for debugging...
[3/3] Linking MyPackagePackageTests
Build complete! (14.11s)
2022-06-20 18:40:37.120 xctest[76994:8416593] The bundle “MyPackagePackageTests.xctest” couldn’t be loaded. Try reinstalling the bundle.
2022-06-20 18:40:37.120 xctest[76994:8416593] (dlopen(/Users/philipturner/Desktop/MyPackage/.build/arm64-apple-macosx/debug/MyPackagePackageTests.xctest/Contents/MacOS/MyPackagePackageTests, 0x0109): Symbol not found: _$sSa16_DifferentiationAA14DifferentiableRzlE0B4ViewVMn
  Referenced from: /Users/philipturner/Desktop/MyPackage/.build/arm64-apple-macosx/debug/MyPackagePackageTests.xctest/Contents/MacOS/MyPackagePackageTests
  Expected in: /usr/lib/swift/libswift_Differentiation.dylib)
(base) philipturner@m1-max-mbp MyPackage % otool -L .build/arm64-apple-macosx/debug/MyPackagePackageTests.xctest/Contents/MacOS/MyPackagePackageTests
.build/arm64-apple-macosx/debug/MyPackagePackageTests.xctest/Contents/MacOS/MyPackagePackageTests:
	/usr/lib/libobjc.A.dylib (compatibility version 1.0.0, current version 228.0.0)
	/usr/lib/libSystem.B.dylib (compatibility version 1.0.0, current version 1311.100.3)
	/usr/lib/swift/libswiftAppKit.dylib (compatibility version 1.0.0, current version 109.0.0, weak)
	/usr/lib/swift/libswiftCore.dylib (compatibility version 1.0.0, current version 0.0.0)
	/usr/lib/swift/libswiftCoreData.dylib (compatibility version 1.0.0, current version 19.0.0, weak)
	/usr/lib/swift/libswiftCoreFoundation.dylib (compatibility version 1.0.0, current version 14.0.0, weak)
	/usr/lib/swift/libswiftCoreGraphics.dylib (compatibility version 1.0.0, current version 2.0.0, weak)
	/usr/lib/swift/libswiftCoreImage.dylib (compatibility version 1.0.0, current version 2.0.0, weak)
	/usr/lib/swift/libswiftDarwin.dylib (compatibility version 1.0.0, current version 0.0.0, weak)
	/usr/lib/swift/libswiftDispatch.dylib (compatibility version 1.0.0, current version 11.0.0, weak)
	/usr/lib/swift/libswiftFoundation.dylib (compatibility version 1.0.0, current version 72.105.0)
	/usr/lib/swift/libswiftIOKit.dylib (compatibility version 1.0.0, current version 1.0.0, weak)
	/usr/lib/swift/libswiftMetal.dylib (compatibility version 1.0.0, current version 261.13.0, weak)
	/usr/lib/swift/libswiftObjectiveC.dylib (compatibility version 1.0.0, current version 3.0.0, weak)
	/usr/lib/swift/libswiftQuartzCore.dylib (compatibility version 1.0.0, current version 3.0.0, weak)
	/usr/lib/swift/libswiftXPC.dylib (compatibility version 1.0.0, current version 1.1.0, weak)
	/usr/lib/swift/libswift_Differentiation.dylib (compatibility version 1.0.0, current version 0.0.0)
	@rpath/XCTest.framework/Versions/A/XCTest (compatibility version 1.0.0, current version 20501.0.0)
	@rpath/libXCTestSwiftSupport.dylib (compatibility version 1.0.0, current version 1.0.0)

@philipturner
Copy link

As the _Differentiation module is configured as if it was a stdlib component (even though it doesn't ship as one), the right way to test its contents is to override its location via DYLD_LIBRARY_PATH. Unfortunately, since it shares the same install location as actual stdlib modules, overriding the library path also overrides the stdlib, which is generally inadvisable if you're also importing Foundation.

Would that mean I can't access one of Apple's API frameworks like Metal if DYLD_LIBRARY_PATH was overriden? For reference, Metal depends on Foundation and ObjectiveC.

@lorentey
Copy link
Member

lorentey commented Jun 20, 2022

On Apple platforms, the Swift Standard Library is an integral part of the OS, and while its public ABI is stable, its binary interface with the rest of the operating system isn't. Therefore, we can only support loading higher-level OS frameworks (incl. Foundation) with the specific version of the Standard Library that ships within the same OS release. In particular, some system integration entry points that are shipping in the OS may not be present in the open source toolchains, or they may have different expected behavior across OS releases. Allowing the system entry points to change is crucial to improving and/or maintaining system performance across OS versions.

Replacing the OS stdlib with alternative builds may or may not work. In general, I would expect things to mostly work when loading fresh stdlib builds on the macOS version that we have installed on ci.swift.org -- but our test suite is limited to exercising parts of Foundation (some of the bridging implementations, to be specific). It is not testing other frameworks like Metal, and it doesn't even cover all parts of Foundation.

(We try to avoid intentionally breaking things, but since we're not actively testing interoperability (or even designing for it), things may be in various stages of brokenness at any given point. The more frameworks you import and the farther you get from the macOS version that's on CI, the more likely you'll run into subtle (or not so subtle) issues.)

@philipturner
Copy link

I don't have an opinion on whether that is a desirable thing -- I am not familiar with this module -- however, I doubt moving the conformance to the Swift repository will resolve the root cause of the loader error.

Let's test that assumption, because if it happens to fix the error, that's good news for S4TF. And if it doesn't fix the error, we learn that we should not add that extension to the Stdlib yet (maybe hold off on DifferentiableCollection too).

@philipturner
Copy link

I got S4TF compiling on a release tool chain (Xcode 14 beta, Swift 5.7 release). The test error still happened, even though AutoDiff is imported as a Swift package (philipturner/differentiation). This means we can narrow down the crash without building a custom toolchain. This is great news!

@philipturner
Copy link

philipturner commented Jul 3, 2022

(maybe hold off on DifferentiableCollection too).

Definitely.

Things just got a lot wierder... I linked SR-14008 to a compiler crash that happens exclusively on release toolchains. It might not even be a bug with SwiftPM, but instead something extremely subtle and hidden in the compiler. In dev toolchains, the compiler might continue in a corrupted state all the way to IRGen and assembly, mutating the final Dylib symbol so that it can't load properly. The bug could be so hidden that it requires rewriting the dependency chain of _Differentiation just to be exposed. I found it on the Xcode beta (5.7 release), but immediately reproduced it on the 5.6.1 release, which we can all test. This also fails on development toolchains, but with a different error message. Probably because you're not supposed to compile philipturner/differentiation on development toolchains.

I did get S4TF to compile on a development toolchain + philipturner/differentiation (against my expectations), and I only had to disable the code causing this crash. Attempting to override the standard library's _Differentiation with my custom package's Differentiation seems awfully similar to attempts to extend Array, which spawned SR-14008. I suspect they are related.


Reproducer (this could be narrowed down further by shrinking philipturner/differentiation):

Make an empty Swift package via swift package init. In my case, it is AutoDiffPackage2. For the Package.swift manifest, paste the following. It creates a dependency to philipturner/differentiation. The Swift tools version at the top is 5.6, but earlier versions also work.

// swift-tools-version: 5.6
// The swift-tools-version declares the minimum version of Swift required to build this package.

import PackageDescription

let package = Package(
    name: "AutoDiffPackage2",
    products: [
        .library(
            name: "AutoDiffPackage2",
            targets: ["AutoDiffPackage2"]),
    ],
    dependencies: [
       .package(url: "https://github.com/philipturner/differentiation", branch: "main"),
    ],
    targets: [
        .target(
            name: "AutoDiffPackage2",
            dependencies: [
                .product(name: "_Differentiation", package: "differentiation"),
            ]),
        .testTarget(
            name: "AutoDiffPackage2Tests",
            dependencies: ["AutoDiffPackage2"]),
    ]
)

In the only source file within Sources/AutoDiffPackage2, replace the template code with the following. It imports a module called Differentiation (no underscore), which is part of my workaround for enabling AutoDiff on release (and now apparently development?) toolchains.

import Differentiation

extension Array.DifferentiableView: RandomAccessCollection
where Element: Differentiable {
  // All of these properties must be public.
  public typealias Element = Array<Element>.Element
  public typealias Index = Array<Element>.Index
  public typealias SubSequence = Array<Element>.SubSequence

  public subscript(position: Array<Element>.Index) -> Element {
    get { fatalError() }
    set { fatalError() }
  }

  public subscript(bounds: Range<Array<Element>.Index>) -> SubSequence {
    get { fatalError() }
    set { fatalError() }
  }

  public var startIndex: Index { fatalError() }
  public var endIndex: Index { fatalError() }
}

struct Tensor: Differentiable & AdditiveArithmetic {
  typealias TangentVector = Self
  
  static func == (lhs: Self, rhs: Self) {
    fatalError()
  }
  
  static func + (lhs: Self, rhs: Self) {
    fatalError()
  }
}

@differentiable(reverse)
func Tensor_stack(_ lhs: Tensor) -> Tensor {
  Tensor_init(stacking: [lhs])
}

@differentiable(reverse)
func Tensor_init(stacking tensors: [Tensor]) -> Tensor {
  fatalError()
}

@derivative(of: Tensor_init(stacking:))
func _vjpStacking(
  stacking tensors: [Tensor]
) -> (value: Tensor, pullback: (Tensor) -> Array<Tensor>.TangentVector) {
  fatalError()
}

Error message on release toolchains:

Google Colab (Swift 5.5 Release)
Cloning into 's4tf-crasher-1'...
remote: Enumerating objects: 18, done.        
remote: Counting objects: 100% (18/18), done.        
remote: Compressing objects: 100% (16/16), done.        
remote: Total 18 (delta 3), reused 11 (delta 0), pack-reused 0        
Unpacking objects: 100% (18/18), done.
Fetching https://github.com/philipturner/differentiation
Fetched https://github.com/philipturner/differentiation (0.42s)
Creating working copy for https://github.com/philipturner/differentiation
Working copy of https://github.com/philipturner/differentiation resolved at main
swift-frontend: /home/buildnode/jenkins/workspace/oss-swift-5.5-package-linux-ubuntu-18_04/swift/lib/SILOptimizer/Differentiation/PullbackCloner.cpp:3006: swift::AllocStackInst *swift::autodiff::PullbackCloner::Implementation::getArrayAdjointElementBuffer(swift::SILValue, int, swift::SILLocation): Assertion `subscriptDecl && "No `Array.TangentVector.subscript`"' failed.
Please submit a bug report (https://swift.org/contributing/#reporting-bugs) and include the project and the crash backtrace.
Stack dump:
0.	Program arguments: /opt/swift/toolchain/usr/bin/swift-frontend -frontend -c -primary-file /content/s4tf-crasher-1/Sources/AutoDiffPackage2/AutoDiffPackage2.swift -emit-module-path /content/s4tf-crasher-1/.build/x86_64-unknown-linux-gnu/debug/AutoDiffPackage2.build/AutoDiffPackage2~partial.swiftmodule -emit-module-doc-path /content/s4tf-crasher-1/.build/x86_64-unknown-linux-gnu/debug/AutoDiffPackage2.build/AutoDiffPackage2~partial.swiftdoc -emit-module-source-info-path /content/s4tf-crasher-1/.build/x86_64-unknown-linux-gnu/debug/AutoDiffPackage2.build/AutoDiffPackage2~partial.swiftsourceinfo -emit-dependencies-path /content/s4tf-crasher-1/.build/x86_64-unknown-linux-gnu/debug/AutoDiffPackage2.build/AutoDiffPackage2.d -emit-reference-dependencies-path /content/s4tf-crasher-1/.build/x86_64-unknown-linux-gnu/debug/AutoDiffPackage2.build/AutoDiffPackage2.swiftdeps -target x86_64-unknown-linux-gnu -disable-objc-interop -I /content/s4tf-crasher-1/.build/x86_64-unknown-linux-gnu/debug -color-diagnostics -enable-testing -g -module-cache-path /content/s4tf-crasher-1/.build/x86_64-unknown-linux-gnu/debug/ModuleCache -swift-version 5 -Onone -D SWIFT_PACKAGE -D DEBUG -new-driver-path /opt/swift/toolchain/usr/bin/swift-driver -resource-dir /opt/swift/toolchain/usr/lib/swift -enable-anonymous-context-mangled-names -module-name AutoDiffPackage2 -parse-as-library -o /content/s4tf-crasher-1/.build/x86_64-unknown-linux-gnu/debug/AutoDiffPackage2.build/AutoDiffPackage2.swift.o -index-store-path /content/s4tf-crasher-1/.build/x86_64-unknown-linux-gnu/debug/index/store -index-system-modules
1.	Swift version 5.5 (swift-5.5-RELEASE)
2.	
3.	While evaluating request ExecuteSILPipelineRequest(Run pipelines { Mandatory Diagnostic Passes + Enabling Optimization Passes } on SIL for AutoDiffPackage2.AutoDiffPackage2)
4.	While running pass #409 SILModuleTransform "Differentiation".
5.	While processing // differentiability witness for Tensor_stack(_:)
sil_differentiability_witness [serialized] [reverse] [parameters 0] [results 0] @$s16AutoDiffPackage212Tensor_stackyAA0D0VADF : $@convention(thin) (Tensor) -> Tensor {
}

 on SIL function "@$s16AutoDiffPackage212Tensor_stackyAA0D0VADF".
 for 'Tensor_stack(_:)' (at /content/s4tf-crasher-1/Sources/AutoDiffPackage2/AutoDiffPackage2.swift:37:1)
6.	While generating VJP for SIL function "@$s16AutoDiffPackage212Tensor_stackyAA0D0VADF".
 for 'Tensor_stack(_:)' (at /content/s4tf-crasher-1/Sources/AutoDiffPackage2/AutoDiffPackage2.swift:37:1)
7.	While generating pullback for SIL function "@$s16AutoDiffPackage212Tensor_stackyAA0D0VADF".
 for 'Tensor_stack(_:)' (at /content/s4tf-crasher-1/Sources/AutoDiffPackage2/AutoDiffPackage2.swift:37:1)
Stack dump without symbol names (ensure you have llvm-symbolizer in your PATH or set the environment var `LLVM_SYMBOLIZER_PATH` to point to it):
/opt/swift/toolchain/usr/bin/swift-frontend[0x5c3ed43]
/opt/swift/toolchain/usr/bin/swift-frontend[0x5c3ca9e]
/opt/swift/toolchain/usr/bin/swift-frontend[0x5c3f0cc]
/lib/x86_64-linux-gnu/libpthread.so.0(+0x12980)[0x7fe2a9af7980]
/lib/x86_64-linux-gnu/libc.so.6(gsignal+0xc7)[0x7fe2a8399e87]
/lib/x86_64-linux-gnu/libc.so.6(abort+0x141)[0x7fe2a839b7f1]
/lib/x86_64-linux-gnu/libc.so.6(+0x303fa)[0x7fe2a838b3fa]
/lib/x86_64-linux-gnu/libc.so.6(+0x30472)[0x7fe2a838b472]
/opt/swift/toolchain/usr/bin/swift-frontend[0x12c096d]
/opt/swift/toolchain/usr/bin/swift-frontend[0x12bfcd0]
/opt/swift/toolchain/usr/bin/swift-frontend[0x12b7835]
/opt/swift/toolchain/usr/bin/swift-frontend[0x12c8ad4]
/opt/swift/toolchain/usr/bin/swift-frontend[0x12bd21e]
/opt/swift/toolchain/usr/bin/swift-frontend[0x12b8a28]
/opt/swift/toolchain/usr/bin/swift-frontend[0x12b54ac]
/opt/swift/toolchain/usr/bin/swift-frontend[0x12b3a9e]
/opt/swift/toolchain/usr/bin/swift-frontend[0x129982f]
/opt/swift/toolchain/usr/bin/swift-frontend[0x129a0bf]
/opt/swift/toolchain/usr/bin/swift-frontend[0x11e4056]
/opt/swift/toolchain/usr/bin/swift-frontend[0x11e18df]
/opt/swift/toolchain/usr/bin/swift-frontend[0xdc5b3f]
/opt/swift/toolchain/usr/bin/swift-frontend[0xdc86f1]
/opt/swift/toolchain/usr/bin/swift-frontend[0xdc2338]
/opt/swift/toolchain/usr/bin/swift-frontend[0xdc22eb]
/opt/swift/toolchain/usr/bin/swift-frontend[0xddf71a]
/opt/swift/toolchain/usr/bin/swift-frontend[0xdcd9a4]
/opt/swift/toolchain/usr/bin/swift-frontend[0xdc2505]
/opt/swift/toolchain/usr/bin/swift-frontend[0xdcf411]
/opt/swift/toolchain/usr/bin/swift-frontend[0x6fff99]
/opt/swift/toolchain/usr/bin/swift-frontend[0x56c6a5]
/opt/swift/toolchain/usr/bin/swift-frontend[0x56bf78]
/opt/swift/toolchain/usr/bin/swift-frontend[0x55fc8e]
/opt/swift/toolchain/usr/bin/swift-frontend[0x4b0646]
/lib/x86_64-linux-gnu/libc.so.6(__libc_start_main+0xe7)[0x7fe2a837cc87]
/opt/swift/toolchain/usr/bin/swift-frontend[0x4b027a]
Google Colab (Swift 5.6 Release)
Cloning into 's4tf-crasher-1'...
remote: Enumerating objects: 18, done.        
remote: Counting objects: 100% (18/18), done.        
remote: Compressing objects: 100% (16/16), done.        
remote: Total 18 (delta 3), reused 11 (delta 0), pack-reused 0        
Unpacking objects: 100% (18/18), done.
Fetching https://github.com/philipturner/differentiation
[272/272] Fetching objects
Fetched https://github.com/philipturner/differentiation (0.28s)
Creating working copy for https://github.com/philipturner/differentiation
Working copy of https://github.com/philipturner/differentiation resolved at main
Building for debugging...
Please submit a bug report (https://swift.org/contributing/#reporting-bugs) and include the project and the crash backtrace.
Stack dump:
0.	Program arguments: /opt/swift/toolchain/usr/bin/swift-frontend -frontend -c -primary-file /content/s4tf-crasher-1/Sources/AutoDiffPackage2/AutoDiffPackage2.swift -emit-dependencies-path /content/s4tf-crasher-1/.build/x86_64-unknown-linux-gnu/debug/AutoDiffPackage2.build/AutoDiffPackage2.d -emit-reference-dependencies-path /content/s4tf-crasher-1/.build/x86_64-unknown-linux-gnu/debug/AutoDiffPackage2.build/AutoDiffPackage2.swiftdeps -target x86_64-unknown-linux-gnu -disable-objc-interop -I /content/s4tf-crasher-1/.build/x86_64-unknown-linux-gnu/debug -color-diagnostics -enable-testing -g -module-cache-path /content/s4tf-crasher-1/.build/x86_64-unknown-linux-gnu/debug/ModuleCache -swift-version 5 -Onone -D SWIFT_PACKAGE -D DEBUG -new-driver-path /opt/swift/toolchain/usr/bin/swift-driver -resource-dir /opt/swift/toolchain/usr/lib/swift -enable-anonymous-context-mangled-names -module-name AutoDiffPackage2 -parse-as-library -o /content/s4tf-crasher-1/.build/x86_64-unknown-linux-gnu/debug/AutoDiffPackage2.build/AutoDiffPackage2.swift.o -index-store-path /content/s4tf-crasher-1/.build/x86_64-unknown-linux-gnu/debug/index/store -index-system-modules
1.	Swift version 5.6 (swift-5.6-RELEASE)
2.	Compiling with the current language version
3.	While evaluating request ExecuteSILPipelineRequest(Run pipelines { Mandatory Diagnostic Passes + Enabling Optimization Passes } on SIL for AutoDiffPackage2)
4.	While running pass #352 SILModuleTransform "Differentiation".
5.	While processing // differentiability witness for Tensor_stack(_:)
sil_differentiability_witness [serialized] [reverse] [parameters 0] [results 0] @$s16AutoDiffPackage212Tensor_stackyAA0D0VADF : $@convention(thin) (Tensor) -> Tensor {
}

 on SIL function "@$s16AutoDiffPackage212Tensor_stackyAA0D0VADF".
 for 'Tensor_stack(_:)' (at /content/s4tf-crasher-1/Sources/AutoDiffPackage2/AutoDiffPackage2.swift:37:1)
6.	While generating VJP for SIL function "@$s16AutoDiffPackage212Tensor_stackyAA0D0VADF".
 for 'Tensor_stack(_:)' (at /content/s4tf-crasher-1/Sources/AutoDiffPackage2/AutoDiffPackage2.swift:37:1)
7.	While generating pullback for SIL function "@$s16AutoDiffPackage212Tensor_stackyAA0D0VADF".
 for 'Tensor_stack(_:)' (at /content/s4tf-crasher-1/Sources/AutoDiffPackage2/AutoDiffPackage2.swift:37:1)
Stack dump without symbol names (ensure you have llvm-symbolizer in your PATH or set the environment var `LLVM_SYMBOLIZER_PATH` to point to it):
/opt/swift/toolchain/usr/bin/swift-frontend[0x5187163]
/opt/swift/toolchain/usr/bin/swift-frontend[0x5184efe]
/opt/swift/toolchain/usr/bin/swift-frontend[0x51874f6]
/lib/x86_64-linux-gnu/libpthread.so.0(+0x12980)[0x7fdd28941980]
/opt/swift/toolchain/usr/bin/swift-frontend[0x157a53e]
/opt/swift/toolchain/usr/bin/swift-frontend[0xff5feb]
/opt/swift/toolchain/usr/bin/swift-frontend[0xff5e45]
/opt/swift/toolchain/usr/bin/swift-frontend[0xff0f86]
/opt/swift/toolchain/usr/bin/swift-frontend[0xffc408]
/opt/swift/toolchain/usr/bin/swift-frontend[0xff16b6]
/opt/swift/toolchain/usr/bin/swift-frontend[0xfefa0c]
/opt/swift/toolchain/usr/bin/swift-frontend[0xfdb447]
/opt/swift/toolchain/usr/bin/swift-frontend[0xf3bde0]
/opt/swift/toolchain/usr/bin/swift-frontend[0xf3a06e]
/opt/swift/toolchain/usr/bin/swift-frontend[0xbf7e5b]
/opt/swift/toolchain/usr/bin/swift-frontend[0xbf9592]
/opt/swift/toolchain/usr/bin/swift-frontend[0xbf5128]
/opt/swift/toolchain/usr/bin/swift-frontend[0xbf50db]
/opt/swift/toolchain/usr/bin/swift-frontend[0xc0a48a]
/opt/swift/toolchain/usr/bin/swift-frontend[0xbfcbfd]
/opt/swift/toolchain/usr/bin/swift-frontend[0xbf5252]
/opt/swift/toolchain/usr/bin/swift-frontend[0xbfe441]
/opt/swift/toolchain/usr/bin/swift-frontend[0x6490b9]
/opt/swift/toolchain/usr/bin/swift-frontend[0x498f13]
/opt/swift/toolchain/usr/bin/swift-frontend[0x498765]
/opt/swift/toolchain/usr/bin/swift-frontend[0x48d508]
/opt/swift/toolchain/usr/bin/swift-frontend[0x4463af]
/lib/x86_64-linux-gnu/libc.so.6(__libc_start_main+0xe7)[0x7fdd26bafc87]
/opt/swift/toolchain/usr/bin/swift-frontend[0x445dda]
Xcode 13.4.1 Toolchain (Swift 5.6.1 Release)
(base) philipturner@m1-max-mbp AutoDiffPackage2 % swift build
Fetching https://github.com/philipturner/differentiation from cache
Fetched https://github.com/philipturner/differentiation (0.21s)
Creating working copy for https://github.com/philipturner/differentiation
Working copy of https://github.com/philipturner/differentiation resolved at main
Building for debugging...
Please submit a bug report (https://swift.org/contributing/#reporting-bugs) and include the project and the crash backtrace.
Stack dump:
0.	Program arguments: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/swift-frontend -frontend -c -primary-file /Users/philipturner/Desktop/AutoDiffPackage2/Sources/AutoDiffPackage2/AutoDiffPackage2.swift -emit-dependencies-path /Users/philipturner/Desktop/AutoDiffPackage2/.build/arm64-apple-macosx/debug/AutoDiffPackage2.build/AutoDiffPackage2.d -emit-reference-dependencies-path /Users/philipturner/Desktop/AutoDiffPackage2/.build/arm64-apple-macosx/debug/AutoDiffPackage2.build/AutoDiffPackage2.swiftdeps -target arm64-apple-macosx10.10 -Xllvm -aarch64-use-tbi -enable-objc-interop -sdk /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX12.3.sdk -I /Users/philipturner/Desktop/AutoDiffPackage2/.build/arm64-apple-macosx/debug -I /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/usr/lib -F /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/Library/Frameworks -color-diagnostics -enable-testing -g -module-cache-path /Users/philipturner/Desktop/AutoDiffPackage2/.build/arm64-apple-macosx/debug/ModuleCache -swift-version 5 -Onone -D SWIFT_PACKAGE -D DEBUG -new-driver-path /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/swift-driver -resource-dir /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/swift -enable-anonymous-context-mangled-names -module-name AutoDiffPackage2 -target-sdk-version 12.3 -parse-as-library -o /Users/philipturner/Desktop/AutoDiffPackage2/.build/arm64-apple-macosx/debug/AutoDiffPackage2.build/AutoDiffPackage2.swift.o -index-store-path /Users/philipturner/Desktop/AutoDiffPackage2/.build/arm64-apple-macosx/debug/index/store -index-system-modules
1.	Apple Swift version 5.6.1 (swiftlang-5.6.0.323.66 clang-1316.0.20.12)
2.	Compiling with the current language version
3.	While evaluating request ExecuteSILPipelineRequest(Run pipelines { Mandatory Diagnostic Passes + Enabling Optimization Passes } on SIL for AutoDiffPackage2)
4.	While running pass #352 SILModuleTransform "Differentiation".
5.	While processing // differentiability witness for Tensor_stack(_:)
sil_differentiability_witness [serialized] [reverse] [parameters 0] [results 0] @$s16AutoDiffPackage212Tensor_stackyAA0D0VADF : $@convention(thin) (Tensor) -> Tensor {
}

 on SIL function "@$s16AutoDiffPackage212Tensor_stackyAA0D0VADF".
 for 'Tensor_stack(_:)' (at /Users/philipturner/Desktop/AutoDiffPackage2/Sources/AutoDiffPackage2/AutoDiffPackage2.swift:37:1)
6.	While generating VJP for SIL function "@$s16AutoDiffPackage212Tensor_stackyAA0D0VADF".
 for 'Tensor_stack(_:)' (at /Users/philipturner/Desktop/AutoDiffPackage2/Sources/AutoDiffPackage2/AutoDiffPackage2.swift:37:1)
7.	While generating pullback for SIL function "@$s16AutoDiffPackage212Tensor_stackyAA0D0VADF".
 for 'Tensor_stack(_:)' (at /Users/philipturner/Desktop/AutoDiffPackage2/Sources/AutoDiffPackage2/AutoDiffPackage2.swift:37:1)
Stack dump without symbol names (ensure you have llvm-symbolizer in your PATH or set the environment var `LLVM_SYMBOLIZER_PATH` to point to it):
0  swift-frontend           0x0000000105617078 llvm::sys::PrintStackTrace(llvm::raw_ostream&, int) + 56
1  swift-frontend           0x000000010561608c llvm::sys::RunSignalHandlers() + 112
2  swift-frontend           0x0000000105617708 SignalHandler(int) + 344
3  libsystem_platform.dylib 0x00000001877974a4 _sigtramp + 56
4  swift-frontend           0x000000010166d7e0 swift::autodiff::PullbackCloner::Implementation::getArrayAdjointElementBuffer(swift::SILValue, int, swift::SILLocation) + 724
5  swift-frontend           0x000000010166d7e0 swift::autodiff::PullbackCloner::Implementation::getArrayAdjointElementBuffer(swift::SILValue, int, swift::SILLocation) + 724
6  swift-frontend           0x000000010166ae50 swift::autodiff::PullbackCloner::Implementation::getAdjointBuffer(swift::SILBasicBlock*, swift::SILValue) + 2912
7  swift-frontend           0x0000000101673914 swift::autodiff::PullbackCloner::Implementation::visitStoreOperation(swift::SILBasicBlock*, swift::SILLocation, swift::SILValue, swift::SILValue) + 60
8  swift-frontend           0x0000000101665bb0 swift::autodiff::PullbackCloner::Implementation::run() + 6280
9  swift-frontend           0x0000000101680798 swift::autodiff::VJPCloner::Implementation::run() + 4012
10 swift-frontend           0x0000000101775d1c (anonymous namespace)::DifferentiationTransformer::canonicalizeDifferentiabilityWitness(swift::SILDifferentiabilityWitness*, swift::autodiff::DifferentiationInvoker, swift::IsSerialized_t) + 5288
11 swift-frontend           0x0000000101773df4 (anonymous namespace)::Differentiation::run() + 1660
12 swift-frontend           0x00000001017d11c8 swift::SILPassManager::executePassPipelinePlan(swift::SILPassPipelinePlan const&) + 11384
13 swift-frontend           0x00000001017e88e8 swift::SimpleRequest<swift::ExecuteSILPipelineRequest, std::__1::tuple<> (swift::SILPipelineExecutionDescriptor), (swift::RequestFlags)1>::evaluateRequest(swift::ExecuteSILPipelineRequest const&, swift::Evaluator&) + 56
14 swift-frontend           0x00000001017d6830 llvm::Expected<swift::ExecuteSILPipelineRequest::OutputType> swift::Evaluator::getResultUncached<swift::ExecuteSILPipelineRequest>(swift::ExecuteSILPipelineRequest const&) + 484
15 swift-frontend           0x00000001017d820c swift::runSILDiagnosticPasses(swift::SILModule&) + 192
16 swift-frontend           0x000000010102be24 swift::CompilerInstance::performSILProcessing(swift::SILModule*) + 72
17 swift-frontend           0x0000000100ec4e50 performCompileStepsPostSILGen(swift::CompilerInstance&, std::__1::unique_ptr<swift::SILModule, std::__1::default_delete<swift::SILModule> >, llvm::PointerUnion<swift::ModuleDecl*, swift::SourceFile*>, swift::PrimarySpecificPaths const&, int&, swift::FrontendObserver*) + 1080
18 swift-frontend           0x0000000100eb8acc swift::performFrontend(llvm::ArrayRef<char const*>, char const*, void*, swift::FrontendObserver*) + 5344
19 swift-frontend           0x0000000100e803c4 swift::mainEntry(int, char const**) + 808
20 dyld                     0x0000000107f3908c start + 520

Error message on development toolchains:

  • July 22, 2020 (v5.3-dev) couldn't parse the package manifest. I guess this is as far back as I go.
  • January 27, 2021 (v5.4-dev) has the same error. But I remember a time when Swift-Colab compiled itself using a v5.4 release toolchain. I tested that and reported the results above.
April 18, 2021 (v5.5-dev)
(base) philipturner@m1-max-mbp AutoDiffPackage2 % swift build
Fetching https://github.com/philipturner/differentiation from cache
Cloning https://github.com/philipturner/differentiation
Resolving https://github.com/philipturner/differentiation at main
Assertion failed: (subscriptDecl && "No `Array.TangentVector.subscript`"), function getArrayAdjointElementBuffer, file /Users/buildnode/jenkins/workspace/oss-swift-package-macos/swift/lib/SILOptimizer/Differentiation/PullbackCloner.cpp, line 3006.
Please submit a bug report (https://swift.org/contributing/#reporting-bugs) and include the project and the crash backtrace.
Stack dump:
0.	Program arguments: /Library/Developer/Toolchains/swift-DEVELOPMENT-SNAPSHOT-2021-04-18-a.xctoolchain/usr/bin/swift-frontend -frontend -c -primary-file /Users/philipturner/Desktop/AutoDiffPackage2/Sources/AutoDiffPackage2/AutoDiffPackage2.swift -emit-module-path /Users/philipturner/Desktop/AutoDiffPackage2/.build/x86_64-apple-macosx/debug/AutoDiffPackage2.build/AutoDiffPackage2~partial.swiftmodule -emit-module-doc-path /Users/philipturner/Desktop/AutoDiffPackage2/.build/x86_64-apple-macosx/debug/AutoDiffPackage2.build/AutoDiffPackage2~partial.swiftdoc -emit-module-source-info-path /Users/philipturner/Desktop/AutoDiffPackage2/.build/x86_64-apple-macosx/debug/AutoDiffPackage2.build/AutoDiffPackage2~partial.swiftsourceinfo -emit-dependencies-path /Users/philipturner/Desktop/AutoDiffPackage2/.build/x86_64-apple-macosx/debug/AutoDiffPackage2.build/AutoDiffPackage2.d -emit-reference-dependencies-path /Users/philipturner/Desktop/AutoDiffPackage2/.build/x86_64-apple-macosx/debug/AutoDiffPackage2.build/AutoDiffPackage2.swiftdeps -target x86_64-apple-macosx10.10 -enable-objc-interop -sdk /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX12.3.sdk -I /Users/philipturner/Desktop/AutoDiffPackage2/.build/x86_64-apple-macosx/debug -I /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/usr/lib -F /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/Library/Frameworks -color-diagnostics -enable-testing -g -module-cache-path /Users/philipturner/Desktop/AutoDiffPackage2/.build/x86_64-apple-macosx/debug/ModuleCache -swift-version 5 -Onone -D SWIFT_PACKAGE -D DEBUG -new-driver-path /Library/Developer/Toolchains/swift-DEVELOPMENT-SNAPSHOT-2021-04-18-a.xctoolchain/usr/bin/swift-driver -resource-dir /Library/Developer/Toolchains/swift-DEVELOPMENT-SNAPSHOT-2021-04-18-a.xctoolchain/usr/lib/swift -enable-anonymous-context-mangled-names -module-name AutoDiffPackage2 -target-sdk-version 12.3.0 -parse-as-library -o /Users/philipturner/Desktop/AutoDiffPackage2/.build/x86_64-apple-macosx/debug/AutoDiffPackage2.build/AutoDiffPackage2.swift.o -index-store-path /Users/philipturner/Desktop/AutoDiffPackage2/.build/x86_64-apple-macosx/debug/index/store -index-system-modules
1.	Apple Swift version 5.5-dev (LLVM fb5a91b4e892ed9, Swift f17142883421ec0)
2.	While evaluating request ExecuteSILPipelineRequest(Run pipelines { Mandatory Diagnostic Passes + Enabling Optimization Passes } on SIL for AutoDiffPackage2.AutoDiffPackage2)
3.	While running pass #409 SILModuleTransform "Differentiation".
4.	While processing // differentiability witness for Tensor_stack(_:)
sil_differentiability_witness [serialized] [reverse] [parameters 0] [results 0] @$s16AutoDiffPackage212Tensor_stackyAA0D0VADF : $@convention(thin) (Tensor) -> Tensor {
}

 on SIL function "@$s16AutoDiffPackage212Tensor_stackyAA0D0VADF".
 for 'Tensor_stack(_:)' (at /Users/philipturner/Desktop/AutoDiffPackage2/Sources/AutoDiffPackage2/AutoDiffPackage2.swift:37:1)
5.	While generating VJP for SIL function "@$s16AutoDiffPackage212Tensor_stackyAA0D0VADF".
 for 'Tensor_stack(_:)' (at /Users/philipturner/Desktop/AutoDiffPackage2/Sources/AutoDiffPackage2/AutoDiffPackage2.swift:37:1)
6.	While generating pullback for SIL function "@$s16AutoDiffPackage212Tensor_stackyAA0D0VADF".
 for 'Tensor_stack(_:)' (at /Users/philipturner/Desktop/AutoDiffPackage2/Sources/AutoDiffPackage2/AutoDiffPackage2.swift:37:1)
Stack dump without symbol names (ensure you have llvm-symbolizer in your PATH or set the environment var `LLVM_SYMBOLIZER_PATH` to point to it):
0  swift-frontend           0x0000000108e36e87 llvm::sys::PrintStackTrace(llvm::raw_ostream&, int) + 39
1  swift-frontend           0x0000000108e36045 llvm::sys::RunSignalHandlers() + 85
2  swift-frontend           0x0000000108e374a6 SignalHandler(int) + 262
3  libsystem_platform.dylib 0x00007ff801826dfd _sigtramp + 29
4  swift-frontend           0x000000010bb55318 vtable for llvm::PrettyStackTraceEntry + 16
5  libsystem_c.dylib        0x00007ff80175cd24 abort + 123
6  libsystem_c.dylib        0x00007ff80175c0cb err + 0
7  swift-frontend           0x00000001090bbcf3 swift::autodiff::PullbackCloner::Implementation::getArrayAdjointElementBuffer(swift::SILValue, int, swift::SILLocation) (.cold.8) + 35
8  swift-frontend           0x0000000104c17b77 swift::autodiff::PullbackCloner::Implementation::getArrayAdjointElementBuffer(swift::SILValue, int, swift::SILLocation) + 2503
9  swift-frontend           0x0000000104c170fa swift::autodiff::PullbackCloner::Implementation::getAdjointProjection(swift::SILBasicBlock*, swift::SILValue) + 2362
10 swift-frontend           0x0000000104c100a4 swift::autodiff::PullbackCloner::Implementation::getAdjointBuffer(swift::SILBasicBlock*, swift::SILValue) + 228
11 swift-frontend           0x0000000104c1f739 swift::autodiff::PullbackCloner::Implementation::visitStoreOperation(swift::SILBasicBlock*, swift::SILLocation, swift::SILValue, swift::SILValue) + 41
12 swift-frontend           0x0000000104c14ccd swift::autodiff::PullbackCloner::Implementation::visit(swift::SILInstruction*) + 205
13 swift-frontend           0x0000000104c10f9c swift::autodiff::PullbackCloner::Implementation::visitSILBasicBlock(swift::SILBasicBlock*) + 876
14 swift-frontend           0x0000000104c0e41c swift::autodiff::PullbackCloner::Implementation::run() + 6364
15 swift-frontend           0x0000000104c0cb12 swift::autodiff::PullbackCloner::run() + 18
16 swift-frontend           0x0000000104c2e41d swift::autodiff::VJPCloner::Implementation::run() + 1597
17 swift-frontend           0x0000000104c2eb12 swift::autodiff::VJPCloner::run() + 18
18 swift-frontend           0x0000000104d65564 (anonymous namespace)::DifferentiationTransformer::canonicalizeDifferentiabilityWitness(swift::SILDifferentiabilityWitness*, swift::autodiff::DifferentiationInvoker, swift::IsSerialized_t) + 6804
19 swift-frontend           0x0000000104d63059 (anonymous namespace)::Differentiation::run() + 1705
20 swift-frontend           0x0000000104db4f8e swift::SILPassManager::runModulePass(unsigned int) + 702
21 swift-frontend           0x0000000104dbb4ca swift::SILPassManager::execute() + 666
22 swift-frontend           0x0000000104db17e8 swift::SILPassManager::executePassPipelinePlan(swift::SILPassPipelinePlan const&) + 72
23 swift-frontend           0x0000000104db1771 swift::ExecuteSILPipelineRequest::evaluate(swift::Evaluator&, swift::SILPipelineExecutionDescriptor) const + 65
24 swift-frontend           0x0000000104dd65dd swift::SimpleRequest<swift::ExecuteSILPipelineRequest, std::__1::tuple<> (swift::SILPipelineExecutionDescriptor), (swift::RequestFlags)1>::evaluateRequest(swift::ExecuteSILPipelineRequest const&, swift::Evaluator&) + 29
25 swift-frontend           0x0000000104dbdcf0 llvm::Expected<swift::ExecuteSILPipelineRequest::OutputType> swift::Evaluator::getResultUncached<swift::ExecuteSILPipelineRequest>(swift::ExecuteSILPipelineRequest const&) + 240
26 swift-frontend           0x0000000104db1a22 swift::executePassPipelinePlan(swift::SILModule*, swift::SILPassPipelinePlan const&, bool, swift::irgen::IRGenModule*) + 82
27 swift-frontend           0x0000000104dc0047 swift::runSILDiagnosticPasses(swift::SILModule&) + 87
28 swift-frontend           0x000000010457f56c swift::CompilerInstance::performSILProcessing(swift::SILModule*) + 60
29 swift-frontend           0x000000010445e1a0 performCompileStepsPostSILGen(swift::CompilerInstance&, std::__1::unique_ptr<swift::SILModule, std::__1::default_delete<swift::SILModule> >, llvm::PointerUnion<swift::ModuleDecl*, swift::SourceFile*>, swift::PrimarySpecificPaths const&, int&, swift::FrontendObserver*) + 640
30 swift-frontend           0x000000010445db9d performCompileStepsPostSema(swift::CompilerInstance&, int&, swift::FrontendObserver*) + 365
31 swift-frontend           0x0000000104452538 swift::performFrontend(llvm::ArrayRef<char const*>, char const*, void*, swift::FrontendObserver*) + 5016
32 swift-frontend           0x00000001043a2386 main + 566
33 dyld                     0x00000002110df51e start + 462
34 dyld                     0x00000002110da000 start + 18446744073709530288
November 12, 2021 Trunk Development Snapshot (v5.6-dev)
(base) philipturner@m1-max-mbp AutoDiffPackage2 % swift --version
Apple Swift version 5.6-dev (LLVM 93bcb8342e7352f, Swift f2132728b7d0b1f)
Target: arm64-apple-macosx12.0
(base) philipturner@m1-max-mbp AutoDiffPackage2 % rm -rf .build
(base) philipturner@m1-max-mbp AutoDiffPackage2 % swift build
Fetching https://github.com/philipturner/differentiation from cache
Fetched https://github.com/philipturner/differentiation (0.26s)
Creating working copy for https://github.com/philipturner/differentiation
Working copy of https://github.com/philipturner/differentiation resolved at main
Building for debugging...
Assertion failed: (subscriptDecl && "No `Array.TangentVector.subscript`"), function getArrayAdjointElementBuffer, file PullbackCloner.cpp, line 3045.
Please submit a bug report (https://swift.org/contributing/#reporting-bugs) and include the project and the crash backtrace.
Stack dump:
0.	Program arguments: /Library/Developer/Toolchains/swift-DEVELOPMENT-SNAPSHOT-2021-11-12-a.xctoolchain/usr/bin/swift-frontend -frontend -c -primary-file /Users/philipturner/Desktop/AutoDiffPackage2/Sources/AutoDiffPackage2/AutoDiffPackage2.swift -emit-dependencies-path /Users/philipturner/Desktop/AutoDiffPackage2/.build/arm64-apple-macosx/debug/AutoDiffPackage2.build/AutoDiffPackage2.d -emit-reference-dependencies-path /Users/philipturner/Desktop/AutoDiffPackage2/.build/arm64-apple-macosx/debug/AutoDiffPackage2.build/AutoDiffPackage2.swiftdeps -target arm64-apple-macosx10.10 -Xllvm -aarch64-use-tbi -enable-objc-interop -sdk /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX12.3.sdk -I /Users/philipturner/Desktop/AutoDiffPackage2/.build/arm64-apple-macosx/debug -I /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/usr/lib -F /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/Library/Frameworks -color-diagnostics -enable-testing -g -module-cache-path /Users/philipturner/Desktop/AutoDiffPackage2/.build/arm64-apple-macosx/debug/ModuleCache -swift-version 5 -Onone -D SWIFT_PACKAGE -D DEBUG -new-driver-path /Library/Developer/Toolchains/swift-DEVELOPMENT-SNAPSHOT-2021-11-12-a.xctoolchain/usr/bin/swift-driver -resource-dir /Library/Developer/Toolchains/swift-DEVELOPMENT-SNAPSHOT-2021-11-12-a.xctoolchain/usr/lib/swift -enable-anonymous-context-mangled-names -module-name AutoDiffPackage2 -target-sdk-version 12.3.0 -parse-as-library -o /Users/philipturner/Desktop/AutoDiffPackage2/.build/arm64-apple-macosx/debug/AutoDiffPackage2.build/AutoDiffPackage2.swift.o -index-store-path /Users/philipturner/Desktop/AutoDiffPackage2/.build/arm64-apple-macosx/debug/index/store -index-system-modules
1.	Apple Swift version 5.6-dev (LLVM 93bcb8342e7352f, Swift f2132728b7d0b1f)
2.	Compiling with the current language version
3.	While evaluating request ExecuteSILPipelineRequest(Run pipelines { Mandatory Diagnostic Passes + Enabling Optimization Passes } on SIL for AutoDiffPackage2)
4.	While running pass #352 SILModuleTransform "Differentiation".
5.	While processing // differentiability witness for Tensor_stack(_:)
sil_differentiability_witness [serialized] [reverse] [parameters 0] [results 0] @$s16AutoDiffPackage212Tensor_stackyAA0D0VADF : $@convention(thin) (Tensor) -> Tensor {
}

 on SIL function "@$s16AutoDiffPackage212Tensor_stackyAA0D0VADF".
 for 'Tensor_stack(_:)' (at /Users/philipturner/Desktop/AutoDiffPackage2/Sources/AutoDiffPackage2/AutoDiffPackage2.swift:37:1)
6.	While generating VJP for SIL function "@$s16AutoDiffPackage212Tensor_stackyAA0D0VADF".
 for 'Tensor_stack(_:)' (at /Users/philipturner/Desktop/AutoDiffPackage2/Sources/AutoDiffPackage2/AutoDiffPackage2.swift:37:1)
7.	While generating pullback for SIL function "@$s16AutoDiffPackage212Tensor_stackyAA0D0VADF".
 for 'Tensor_stack(_:)' (at /Users/philipturner/Desktop/AutoDiffPackage2/Sources/AutoDiffPackage2/AutoDiffPackage2.swift:37:1)
Stack dump without symbol names (ensure you have llvm-symbolizer in your PATH or set the environment var `LLVM_SYMBOLIZER_PATH` to point to it):
0  swift-frontend           0x0000000104d7ac20 llvm::sys::PrintStackTrace(llvm::raw_ostream&, int) + 56
1  swift-frontend           0x0000000104d79e80 llvm::sys::RunSignalHandlers() + 128
2  swift-frontend           0x0000000104d7b284 SignalHandler(int) + 304
3  libsystem_platform.dylib 0x00000001877974a4 _sigtramp + 56
4  libsystem_pthread.dylib  0x000000018777fee0 pthread_kill + 288
5  libsystem_c.dylib        0x00000001876ba340 abort + 168
6  libsystem_c.dylib        0x00000001876b9754 err + 0
7  swift-frontend           0x0000000104f47230 swift::autodiff::PullbackCloner::Implementation::getArrayAdjointElementBuffer(swift::SILValue, int, swift::SILLocation) (.cold.10) + 0
8  swift-frontend           0x00000001011a7358 swift::autodiff::PullbackCloner::Implementation::getArrayAdjointElementBuffer(swift::SILValue, int, swift::SILLocation) + 2152
9  swift-frontend           0x00000001011a6a3c swift::autodiff::PullbackCloner::Implementation::getAdjointProjection(swift::SILBasicBlock*, swift::SILValue) + 2032
10 swift-frontend           0x00000001011a08c8 swift::autodiff::PullbackCloner::Implementation::getAdjointBuffer(swift::SILBasicBlock*, swift::SILValue) + 228
11 swift-frontend           0x00000001011ad5a4 swift::autodiff::PullbackCloner::Implementation::visitStoreOperation(swift::SILBasicBlock*, swift::SILLocation, swift::SILValue, swift::SILValue) + 76
12 swift-frontend           0x00000001011a4db8 swift::autodiff::PullbackCloner::Implementation::visit(swift::SILInstruction*) + 200
13 swift-frontend           0x00000001011a1814 swift::autodiff::PullbackCloner::Implementation::visitSILBasicBlock(swift::SILBasicBlock*) + 688
14 swift-frontend           0x000000010119ef68 swift::autodiff::PullbackCloner::Implementation::run() + 5064
15 swift-frontend           0x000000010119db74 swift::autodiff::PullbackCloner::run() + 24
16 swift-frontend           0x00000001011b9718 swift::autodiff::VJPCloner::Implementation::run() + 1368
17 swift-frontend           0x00000001011b9d34 swift::autodiff::VJPCloner::run() + 24
18 swift-frontend           0x00000001012af060 (anonymous namespace)::DifferentiationTransformer::canonicalizeDifferentiabilityWitness(swift::SILDifferentiabilityWitness*, swift::autodiff::DifferentiationInvoker, swift::IsSerialized_t) + 5276
19 swift-frontend           0x00000001012ad1b8 (anonymous namespace)::Differentiation::run() + 924
20 swift-frontend           0x00000001012fc688 swift::SILPassManager::runModulePass(unsigned int) + 632
21 swift-frontend           0x00000001013018e0 swift::SILPassManager::execute() + 628
22 swift-frontend           0x00000001012f97b8 swift::SILPassManager::executePassPipelinePlan(swift::SILPassPipelinePlan const&) + 68
23 swift-frontend           0x00000001012f9740 swift::ExecuteSILPipelineRequest::evaluate(swift::Evaluator&, swift::SILPipelineExecutionDescriptor) const + 68
24 swift-frontend           0x0000000101317abc swift::SimpleRequest<swift::ExecuteSILPipelineRequest, std::__1::tuple<> (swift::SILPipelineExecutionDescriptor), (swift::RequestFlags)1>::evaluateRequest(swift::ExecuteSILPipelineRequest const&, swift::Evaluator&) + 28
25 swift-frontend           0x0000000101304050 llvm::Expected<swift::ExecuteSILPipelineRequest::OutputType> swift::Evaluator::getResultUncached<swift::ExecuteSILPipelineRequest>(swift::ExecuteSILPipelineRequest const&) + 252
26 swift-frontend           0x00000001012f99b4 swift::executePassPipelinePlan(swift::SILModule*, swift::SILPassPipelinePlan const&, bool, swift::irgen::IRGenModule*) + 84
27 swift-frontend           0x0000000101306d3c swift::runSILDiagnosticPasses(swift::SILModule&) + 92
28 swift-frontend           0x0000000100bccb14 swift::CompilerInstance::performSILProcessing(swift::SILModule*) + 68
29 swift-frontend           0x0000000100a92054 performCompileStepsPostSILGen(swift::CompilerInstance&, std::__1::unique_ptr<swift::SILModule, std::__1::default_delete<swift::SILModule> >, llvm::PointerUnion<swift::ModuleDecl*, swift::SourceFile*>, swift::PrimarySpecificPaths const&, int&, swift::FrontendObserver*) + 620
30 swift-frontend           0x0000000100a9188c bool llvm::function_ref<bool (swift::CompilerInstance&)>::callback_fn<performAction(swift::CompilerInstance&, int&, swift::FrontendObserver*)::$_19>(long, swift::CompilerInstance&) + 572
31 swift-frontend           0x0000000100a87858 swift::performFrontend(llvm::ArrayRef<char const*>, char const*, void*, swift::FrontendObserver*) + 4368
32 swift-frontend           0x0000000100a46a80 swift::mainEntry(int, char const**) + 500
33 dyld                     0x000000010d12108c start + 520
June 13, 2022 v5.7 Branch Development Snapshot
(base) philipturner@m1-max-mbp AutoDiffPackage2 % swift build
Fetching https://github.com/philipturner/differentiation from cache
Fetched https://github.com/philipturner/differentiation (0.26s)
Creating working copy for https://github.com/philipturner/differentiation
Working copy of https://github.com/philipturner/differentiation resolved at main
Building for debugging...
Assertion failed: (subscriptDecl && "No `Array.TangentVector.subscript`"), function getArrayAdjointElementBuffer, file PullbackCloner.cpp, line 3046.
Please submit a bug report (https://swift.org/contributing/#reporting-bugs) and include the project and the crash backtrace.
Stack dump:
0.	Program arguments: /Library/Developer/Toolchains/swift-5.7-DEVELOPMENT-SNAPSHOT-2022-06-13-a.xctoolchain/usr/bin/swift-frontend -frontend -c -primary-file /Users/philipturner/Desktop/AutoDiffPackage2/Sources/AutoDiffPackage2/AutoDiffPackage2.swift -emit-dependencies-path /Users/philipturner/Desktop/AutoDiffPackage2/.build/arm64-apple-macosx/debug/AutoDiffPackage2.build/AutoDiffPackage2.d -emit-reference-dependencies-path /Users/philipturner/Desktop/AutoDiffPackage2/.build/arm64-apple-macosx/debug/AutoDiffPackage2.build/AutoDiffPackage2.swiftdeps -target arm64-apple-macosx10.10 -Xllvm -aarch64-use-tbi -enable-objc-interop -sdk /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX12.3.sdk -I /Users/philipturner/Desktop/AutoDiffPackage2/.build/arm64-apple-macosx/debug -I /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/usr/lib -F /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/Library/Frameworks -color-diagnostics -enable-testing -g -module-cache-path /Users/philipturner/Desktop/AutoDiffPackage2/.build/arm64-apple-macosx/debug/ModuleCache -swift-version 5 -Onone -D SWIFT_PACKAGE -D DEBUG -new-driver-path /Library/Developer/Toolchains/swift-5.7-DEVELOPMENT-SNAPSHOT-2022-06-13-a.xctoolchain/usr/bin/swift-driver -empty-abi-descriptor -resource-dir /Library/Developer/Toolchains/swift-5.7-DEVELOPMENT-SNAPSHOT-2022-06-13-a.xctoolchain/usr/lib/swift -enable-anonymous-context-mangled-names -module-name AutoDiffPackage2 -target-sdk-version 12.3 -parse-as-library -o /Users/philipturner/Desktop/AutoDiffPackage2/.build/arm64-apple-macosx/debug/AutoDiffPackage2.build/AutoDiffPackage2.swift.o -index-store-path /Users/philipturner/Desktop/AutoDiffPackage2/.build/arm64-apple-macosx/debug/index/store -index-system-modules
1.	Apple Swift version 5.7-dev (LLVM fc0ab39bda0c879, Swift c6bb52ea2e91c0e)
2.	Compiling with the current language version
3.	While evaluating request ExecuteSILPipelineRequest(Run pipelines { Mandatory Diagnostic Passes + Enabling Optimization Passes } on SIL for AutoDiffPackage2)
4.	While running pass #411 SILModuleTransform "Differentiation".
5.	While processing // differentiability witness for Tensor_stack(_:)
sil_differentiability_witness [serialized] [reverse] [parameters 0] [results 0] @$s16AutoDiffPackage212Tensor_stackyAA0D0VADF : $@convention(thin) (Tensor) -> Tensor {
}

 on SIL function "@$s16AutoDiffPackage212Tensor_stackyAA0D0VADF".
 for 'Tensor_stack(_:)' (at /Users/philipturner/Desktop/AutoDiffPackage2/Sources/AutoDiffPackage2/AutoDiffPackage2.swift:37:1)
6.	While generating VJP for SIL function "@$s16AutoDiffPackage212Tensor_stackyAA0D0VADF".
 for 'Tensor_stack(_:)' (at /Users/philipturner/Desktop/AutoDiffPackage2/Sources/AutoDiffPackage2/AutoDiffPackage2.swift:37:1)
7.	While generating pullback for SIL function "@$s16AutoDiffPackage212Tensor_stackyAA0D0VADF".
 for 'Tensor_stack(_:)' (at /Users/philipturner/Desktop/AutoDiffPackage2/Sources/AutoDiffPackage2/AutoDiffPackage2.swift:37:1)
Stack dump without symbol names (ensure you have llvm-symbolizer in your PATH or set the environment var `LLVM_SYMBOLIZER_PATH` to point to it):
0  swift-frontend           0x0000000109395858 llvm::sys::PrintStackTrace(llvm::raw_ostream&, int) + 56
1  swift-frontend           0x0000000109394ab8 llvm::sys::RunSignalHandlers() + 128
2  swift-frontend           0x0000000109395ebc SignalHandler(int) + 304
3  libsystem_platform.dylib 0x00000001877974a4 _sigtramp + 56
4  libsystem_pthread.dylib  0x000000018777fee0 pthread_kill + 288
5  libsystem_c.dylib        0x00000001876ba340 abort + 168
6  libsystem_c.dylib        0x00000001876b9754 err + 0
7  swift-frontend           0x0000000109551508 swift::autodiff::PullbackCloner::Implementation::getArrayAdjointElementBuffer(swift::SILValue, int, swift::SILLocation) (.cold.10) + 0
8  swift-frontend           0x0000000105539b54 swift::autodiff::PullbackCloner::Implementation::getArrayAdjointElementBuffer(swift::SILValue, int, swift::SILLocation) + 2184
9  swift-frontend           0x0000000105539214 swift::autodiff::PullbackCloner::Implementation::getAdjointProjection(swift::SILBasicBlock*, swift::SILValue) + 2036
10 swift-frontend           0x0000000105533070 swift::autodiff::PullbackCloner::Implementation::getAdjointBuffer(swift::SILBasicBlock*, swift::SILValue) + 228
11 swift-frontend           0x000000010553fdb0 swift::autodiff::PullbackCloner::Implementation::visitStoreOperation(swift::SILBasicBlock*, swift::SILLocation, swift::SILValue, swift::SILValue) + 76
12 swift-frontend           0x000000010553758c swift::autodiff::PullbackCloner::Implementation::visit(swift::SILInstruction*) + 200
13 swift-frontend           0x0000000105533fdc swift::autodiff::PullbackCloner::Implementation::visitSILBasicBlock(swift::SILBasicBlock*) + 676
14 swift-frontend           0x0000000105531614 swift::autodiff::PullbackCloner::Implementation::run() + 5028
15 swift-frontend           0x0000000105530244 swift::autodiff::PullbackCloner::run() + 24
16 swift-frontend           0x000000010554aed8 swift::autodiff::VJPCloner::Implementation::run() + 1368
17 swift-frontend           0x000000010554b4f4 swift::autodiff::VJPCloner::run() + 24
18 swift-frontend           0x000000010564b48c (anonymous namespace)::DifferentiationTransformer::canonicalizeDifferentiabilityWitness(swift::SILDifferentiabilityWitness*, swift::autodiff::DifferentiationInvoker, swift::IsSerialized_t) + 5296
19 swift-frontend           0x0000000105649608 (anonymous namespace)::Differentiation::run() + 932
20 swift-frontend           0x00000001056c1f1c swift::SILPassManager::runModulePass(unsigned int) + 956
21 swift-frontend           0x00000001056c74c0 swift::SILPassManager::execute() + 624
22 swift-frontend           0x00000001056beffc swift::SILPassManager::executePassPipelinePlan(swift::SILPassPipelinePlan const&) + 68
23 swift-frontend           0x00000001056bef84 swift::ExecuteSILPipelineRequest::evaluate(swift::Evaluator&, swift::SILPipelineExecutionDescriptor) const + 68
24 swift-frontend           0x00000001056df5b4 swift::SimpleRequest<swift::ExecuteSILPipelineRequest, std::__1::tuple<> (swift::SILPipelineExecutionDescriptor), (swift::RequestFlags)1>::evaluateRequest(swift::ExecuteSILPipelineRequest const&, swift::Evaluator&) + 28
25 swift-frontend           0x00000001056ca07c llvm::Expected<swift::ExecuteSILPipelineRequest::OutputType> swift::Evaluator::getResultUncached<swift::ExecuteSILPipelineRequest>(swift::ExecuteSILPipelineRequest const&) + 252
26 swift-frontend           0x00000001056bf1f8 swift::executePassPipelinePlan(swift::SILModule*, swift::SILPassPipelinePlan const&, bool, swift::irgen::IRGenModule*) + 84
27 swift-frontend           0x00000001056cdb80 swift::runSILDiagnosticPasses(swift::SILModule&) + 92
28 swift-frontend           0x0000000104f0a294 swift::CompilerInstance::performSILProcessing(swift::SILModule*) + 72
29 swift-frontend           0x0000000104eaef6c performCompileStepsPostSILGen(swift::CompilerInstance&, std::__1::unique_ptr<swift::SILModule, std::__1::default_delete<swift::SILModule> >, llvm::PointerUnion<swift::ModuleDecl*, swift::SourceFile*>, swift::PrimarySpecificPaths const&, int&, swift::FrontendObserver*) + 716
30 swift-frontend           0x0000000104eae748 swift::performCompileStepsPostSema(swift::CompilerInstance&, int&, swift::FrontendObserver*) + 540
31 swift-frontend           0x0000000104ebc564 withSemanticAnalysis(swift::CompilerInstance&, swift::FrontendObserver*, llvm::function_ref<bool (swift::CompilerInstance&)>, bool) + 160
32 swift-frontend           0x0000000104eb05d8 swift::performFrontend(llvm::ArrayRef<char const*>, char const*, void*, swift::FrontendObserver*) + 3388
33 swift-frontend           0x0000000104db7858 swift::mainEntry(int, char const**) + 3764
34 dyld                     0x0000000111e4508c start + 520



Also, here's the branch I'm currently working on that compiles S4TF on the Xcode 14 beta toolchain: https://github.com/philipturner/s4tf/tree/compile-on-release-toolchain (PR: s4tf/s4tf#17)

@philipturner
Copy link

philipturner commented Jul 3, 2022

Repository:

https://github.com/philipturner/s4tf-crasher-1

Colab Notebook:

https://colab.research.google.com/drive/1Mq9FdUTfQTyfOkTXZvCFR7mVIaf-ShS0?usp=sharing

Based on the data above, the earliest someone can reproduce this bug is late 2020/early 2021. That's around the same time SR-14008 was filed: December 2020. Furthermore, both bugs require conforming Array.DifferentiableView to RandomAccessCollection. This doesn't seem like just a coincidence.


Update: I'm not so sure this is the same bug as SR-14008. It is reported in apple/swift#59876.

@philipturner
Copy link

philipturner commented Jul 4, 2022

I have a different narrowed reproducer. Substitute the reproducer in #4454 (comment) with this.

// Import not used in this file.
import _Differentiation

fileprivate struct Array_DifferentiableView<Element> {}

extension Array_DifferentiableView: AdditiveArithmetic {
  fileprivate static func + (lhs: Self, rhs: Self) -> Self { fatalError() }
  fileprivate static func - (lhs: Self, rhs: Self) -> Self { fatalError() }
}

This produces the crash as usual:

(base) philipturner@m1-max-mbp AutoDiffPackage2 % rm -rf .build && swift test
Building for debugging...
[5/5] Linking AutoDiffPackage2PackageTests
Build complete! (1.22s)
2022-07-04 11:48:59.526 xctest[74390:611491] The bundle “AutoDiffPackage2PackageTests.xctest” couldn’t be loaded. Try reinstalling the bundle.
2022-07-04 11:48:59.526 xctest[74390:611491] (dlopen(/Users/philipturner/Desktop/AutoDiffPackage2/.build/arm64-apple-macosx/debug/AutoDiffPackage2PackageTests.xctest/Contents/MacOS/AutoDiffPackage2PackageTests, 0x0109): Symbol not found: _$sSa16_DifferentiationAA14DifferentiableRzlE0B4ViewVMn
  Referenced from: /Users/philipturner/Desktop/AutoDiffPackage2/.build/arm64-apple-macosx/debug/AutoDiffPackage2PackageTests.xctest/Contents/MacOS/AutoDiffPackage2PackageTests
  Expected in: /usr/lib/swift/libswift_Differentiation.dylib)

But something interesting happens when you change the visibility of Array_DifferentiableView.+ and Array_DifferentiableView.-.

@philipturner
Copy link

philipturner commented Jul 4, 2022

+ - Crash
fileprivate fileprivate same as usual
private fileprivate Error diagnostic (1)
fileprivate private Compiler crash (2)
private private Error diagnostic (3)

Error diagnostic (1)

Building for debugging...
<unknown>:0: error: multiple definitions of symbol '$s15Differentiation24Array_DifferentiableView33_A86ACB539026E391A658540B6BE229AALLV1poiyADyxGAF_AFtFZ'

error: fatalError

Compiler crash (2)

Here
(base) philipturner@m1-max-mbp AutoDiffPackage2 % rm -rf .build && swift test
Building for debugging...
Please submit a bug report (https://swift.org/contributing/#reporting-bugs) and include the project and the crash backtrace.
Stack dump:
0.	Program arguments: /Library/Developer/Toolchains/swift-5.7-DEVELOPMENT-SNAPSHOT-2022-06-26-a.xctoolchain/usr/bin/swift-frontend -frontend -c -primary-file /Users/philipturner/Desktop/AutoDiffPackage2/Sources/Differentiation/ArrayDifferentiation.swift -emit-dependencies-path /Users/philipturner/Desktop/AutoDiffPackage2/.build/arm64-apple-macosx/debug/Differentiation.build/ArrayDifferentiation.d -emit-reference-dependencies-path /Users/philipturner/Desktop/AutoDiffPackage2/.build/arm64-apple-macosx/debug/Differentiation.build/ArrayDifferentiation.swiftdeps -target arm64-apple-macosx10.13 -Xllvm -aarch64-use-tbi -enable-objc-interop -sdk /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX12.3.sdk -I /Users/philipturner/Desktop/AutoDiffPackage2/.build/arm64-apple-macosx/debug -I /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/usr/lib -F /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/Library/Frameworks -color-diagnostics -enable-testing -g -module-cache-path /Users/philipturner/Desktop/AutoDiffPackage2/.build/arm64-apple-macosx/debug/ModuleCache -swift-version 5 -Onone -D SWIFT_PACKAGE -D DEBUG -new-driver-path /Library/Developer/Toolchains/swift-5.7-DEVELOPMENT-SNAPSHOT-2022-06-26-a.xctoolchain/usr/bin/swift-driver -empty-abi-descriptor -resource-dir /Library/Developer/Toolchains/swift-5.7-DEVELOPMENT-SNAPSHOT-2022-06-26-a.xctoolchain/usr/lib/swift -enable-anonymous-context-mangled-names -module-name Differentiation -target-sdk-version 12.3 -parse-as-library -o /Users/philipturner/Desktop/AutoDiffPackage2/.build/arm64-apple-macosx/debug/Differentiation.build/ArrayDifferentiation.swift.o -index-store-path /Users/philipturner/Desktop/AutoDiffPackage2/.build/arm64-apple-macosx/debug/index/store -index-system-modules
1.	Apple Swift version 5.7-dev (LLVM eecf02df5133efe, Swift ad06e7d7251311a)
2.	Compiling with the current language version
3.	While evaluating request ASTLoweringRequest(Lowering AST to SIL for file "/Users/philipturner/Desktop/AutoDiffPackage2/Sources/Differentiation/ArrayDifferentiation.swift")
4.	While generating SIL witness table protocol conformance to 'AdditiveArithmetic' (in module 'Swift') for type 'Array_DifferentiableView<Element>' (declared at [/Users/philipturner/Desktop/AutoDiffPackage2/Sources/Differentiation/ArrayDifferentiation.swift:3:13 - line:3:55] RangeText="struct Array_DifferentiableView<Element> {")
5.	While generating protocol witness thunk SIL function "@$s15Differentiation24Array_DifferentiableView33_A86ACB539026E391A658540B6BE229AALLVyxGs18AdditiveArithmeticAAsAFP1soiyxx_xtFZTW".
 for '-(_:_:)' (in module 'Differentiation')
Stack dump without symbol names (ensure you have llvm-symbolizer in your PATH or set the environment var `LLVM_SYMBOLIZER_PATH` to point to it):
0  swift-frontend           0x0000000104fd7144 llvm::sys::PrintStackTrace(llvm::raw_ostream&, int) + 56
1  swift-frontend           0x0000000104fd63a4 llvm::sys::RunSignalHandlers() + 128
2  swift-frontend           0x0000000104fd77a8 SignalHandler(int) + 304
3  libsystem_platform.dylib 0x00000001877974a4 _sigtramp + 56
4  swift-frontend           0x0000000101011fec swift::Lowering::SILGenModule::getFunction(swift::SILDeclRef, swift::ForDefinition_t) + 612
5  swift-frontend           0x00000001010c8d58 swift::Lowering::SILGenFunction::emitGlobalFunctionRef(swift::SILLocation, swift::SILDeclRef, swift::Lowering::SILConstantInfo, bool) + 620
6  swift-frontend           0x00000001010b7464 swift::Lowering::SILGenFunction::emitProtocolWitness(swift::Lowering::AbstractionPattern, swift::CanTypeWrapper<swift::AnyFunctionType>, swift::SILDeclRef, swift::SubstitutionMap, swift::SILDeclRef, swift::SubstitutionMap, swift::Lowering::IsFreeFunctionWitness_t, bool, llvm::Optional<swift::ActorIsolation>) + 3416
7  swift-frontend           0x00000001010ccb7c swift::Lowering::SILGenModule::emitProtocolWitness(swift::ProtocolConformanceRef, swift::SILLinkage, swift::IsSerialized_t, swift::SILDeclRef, swift::SILDeclRef, swift::Lowering::IsFreeFunctionWitness_t, swift::Witness) + 2504
8  swift-frontend           0x00000001010ced78 (anonymous namespace)::SILGenWitnessTable<(anonymous namespace)::SILGenConformance>::addMethodImplementation(swift::SILDeclRef, swift::SILDeclRef, swift::Witness) + 448
9  swift-frontend           0x00000001010ce9ac (anonymous namespace)::SILGenWitnessTable<(anonymous namespace)::SILGenConformance>::addMethod(swift::SILDeclRef) + 792
10 swift-frontend           0x00000001010ce4ac swift::SILWitnessVisitor<(anonymous namespace)::SILGenConformance>::visitProtocolDecl(swift::ProtocolDecl*) + 1936
11 swift-frontend           0x00000001010cc050 swift::Lowering::SILGenModule::getWitnessTable(swift::NormalProtocolConformance*) + 316
12 swift-frontend           0x00000001010cdcc0 SILGenExtension::emitExtension(swift::ExtensionDecl*) + 208
13 swift-frontend           0x00000001010cdbe4 swift::Lowering::SILGenModule::visitExtensionDecl(swift::ExtensionDecl*) + 24
14 swift-frontend           0x0000000101017d5c swift::ASTLoweringRequest::evaluate(swift::Evaluator&, swift::ASTLoweringDescriptor) const + 1792
15 swift-frontend           0x00000001010c3170 swift::SimpleRequest<swift::ASTLoweringRequest, std::__1::unique_ptr<swift::SILModule, std::__1::default_delete<swift::SILModule> > (swift::ASTLoweringDescriptor), (swift::RequestFlags)9>::evaluateRequest(swift::ASTLoweringRequest const&, swift::Evaluator&) + 156
16 swift-frontend           0x000000010101bd1c llvm::Expected<swift::ASTLoweringRequest::OutputType> swift::Evaluator::getResultUncached<swift::ASTLoweringRequest>(swift::ASTLoweringRequest const&) + 408
17 swift-frontend           0x00000001010189b4 swift::performASTLowering(swift::FileUnit&, swift::Lowering::TypeConverter&, swift::SILOptions const&) + 104
18 swift-frontend           0x0000000100aeabbc swift::performCompileStepsPostSema(swift::CompilerInstance&, int&, swift::FrontendObserver*) + 496
19 swift-frontend           0x0000000100af8a34 withSemanticAnalysis(swift::CompilerInstance&, swift::FrontendObserver*, llvm::function_ref<bool (swift::CompilerInstance&)>, bool) + 160
20 swift-frontend           0x0000000100aeca78 swift::performFrontend(llvm::ArrayRef<char const*>, char const*, void*, swift::FrontendObserver*) + 3388
21 swift-frontend           0x00000001009f308c swift::mainEntry(int, char const**) + 3764
22 dyld                     0x000000010d80908c start + 520

error: fatalError

Error diagnostic (3)

Building for debugging...
<unknown>:0: error: multiple definitions of symbol '$s15Differentiation24Array_DifferentiableView33_A86ACB539026E391A658540B6BE229AALLV1poiyADyxGAF_AFtFZ'
<unknown>:0: error: multiple definitions of symbol '$s15Differentiation24Array_DifferentiableView33_A86ACB539026E391A658540B6BE229AALLV1soiyADyxGAF_AFtFZ'

error: fatalError

The compiler crash with fileprivate - private is something we can use to fix the bug. It seems unrelated to AutoDiff, and instead a generic SIL crash. Symbols are duplicated/corrupted, which later manifests in an error loading libswift_Differentiation. The duplicated symbols begin with the namespace Differentiation (no underscore) because that's the Swift module I compiled it into. That word does not mean this bug is related to AutoDiff.


This crash can be narrowed down further. Make a Swift file with the contents below and run swift FILENAME.swift.

// Import not used in this file.
import _Differentiation

fileprivate struct Array_DifferentiableView<Element> {}

extension Array_DifferentiableView: AdditiveArithmetic {
  fileprivate static func + (lhs: Self, rhs: Self) -> Self { fatalError() }
  private static func - (lhs: Self, rhs: Self) -> Self { fatalError() }
}

The crash changes some time between 2021-04-18 and 2021-07-07. On the 2021-04-18 toolchain, it doesn't show an error when both methods are fileprivate. If either is private, it shows the diagnostic below. The error stays this way as far back as July 22, 2020.

file.swift:8:23: error: invalid redeclaration of synthesized implementation for protocol requirement '-'
  private static func - (lhs: Self, rhs: Self) -> Self { fatalError() }

On the January 29, 2020 toolchain, the error diagnostic is this:

file.swift:6:1: error: type 'Array_DifferentiableView<Element>' does not conform to protocol 'AdditiveArithmetic'
extension Array_DifferentiableView: AdditiveArithmetic {
^
Swift.AdditiveArithmetic:2:34: note: candidate would match if 'Array_DifferentiableView<Element>' conformed to 'ExpressibleByIntegerLiteral'
    @inlinable public static var zero: Self { get }
                                 ^
Swift.AdditiveArithmetic:2:16: note: protocol requires property 'zero' with type 'Array_DifferentiableView<Element>'
    static var zero: Self { get }
               ^

My original reproducer with conforming Array.DifferentiableView to RandomAccessCollection only works so far back in time. Some time between 2022-01-09 and 2022-02-03, it no longer produces an error in SwiftPM tests.


Using Colab, the fileprivate-private bug was bisected further. The older error occurs on the trunk 2021-04-18 and v5.5 branch 2021-04-19 snapshots. The newer error occurs on the trunk 2021-04-24 snapshot. At this point, fully narrowing down each manifestation of the bug will be pointless. It first occurred in December 2020 in S4TF, which is so far back that testing it may be impossible.


The above information makes it seem like a problem with scoped declarations. Swift automatically synthesizes an internal/fileprivate-access form of + or -. When you declare a private-access version, it doesn't override the automatically synthesized one. Thus, two conflicting versions exist. I don't know how this would create the error in Swift package tests, though. I guess we can see if the SwiftPM error is fixed once the patch for fileprivate-private goes in.

@philipturner
Copy link

philipturner commented Jul 4, 2022

This bug does not actually impact S4TF on release toolchains. I encountered an error while running tests, but did not realize that the error was just the X10 error. I bypassed this by manually copying libx10 into .build/debug. This error masks the one with loading libswift_Differentiation. In the S4TF build script, I shied away from copying X10 because it was useless. Calling .../usr/bin/xctest loaded X10 because DYLD_LIBRARY_PATH actually worked, and no copying was necessary. Also, hard-coding .build/debug would make release builds fail or require more complex setup.

After suppressing the X10 error on v5.7 release, swift test ran the SwiftPM tests against my expectations. This is probably the first time in 20 months that S4TF could run tests on macOS, directly through swift test. I really wanted to fix SR-14008 so that in the future, you could do everything related to S4TF with only Xcode. The backend would be Metal/OpenCL, which is entirely Swift. You don't need to use Bash scripting or install Bazel because X10 isn't in the dependency chain. You can run SwiftPM tests directly in Xcode - if not, then through a simple swift test on the command line. Other editors like VSCode would also become more S4TF-friendly.

My vision for S4TF is something that even beginners to Swift can use. You can compile, import, and contribute to it, immediately. No one needs to wait 20 minutes to download a massive dev toolchain on their computer. No one needs to wait over an hour for Bazel to compile tensorflow/tensorflow. No one needs to learn how to use Bash or the command line. You can run it on any platform - even iOS - because the new dependency chain is written entirely in Swift (philipturner/differentiation, swift-reflection-mirror, swift-opencl, s4tf/metal, s4tf/opencl). These are the accessibility barriers I faced when Google sponsored S4TF, and I'm glad they will be lifted upon the release of Swift 5.7.

With that, I am not going to investigate SR-14008 further.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

6 participants