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-14004] differentiable_function_extract SIL verification error related to cross-module inlining #56397

Open
dan-zheng opened this issue Dec 29, 2020 · 0 comments
Assignees
Labels
AutoDiff bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. compiler The Swift compiler in itself

Comments

@dan-zheng
Copy link
Collaborator

Previous ID SR-14004
Radar None
Original Reporter @dan-zheng
Type Bug
Status In Progress
Resolution
Environment

swift-DEVELOPMENT-SNAPSHOT-2020-12-20-a

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

md5: 53b89215ebc9ec25f5ef29560f6b5ea5

Issue Description:

Reproducer:

// library.swift
import _Differentiation

public struct Struct<Scalar>: Differentiable {}

@differentiable
public func foo<Scalar>(_ x: Struct<Scalar>) -> Struct<Scalar> { x }

@inlinable
@differentiable
public func bar<Scalar>(_ x: Struct<Scalar>) -> Struct<Scalar> {
  foo(x)
}
// user.swift
import _Differentiation
import Library

public func foo(
  body: @differentiable (Struct<Float>) -> Struct<Float> = bar
) {
  fatalError()
}
$ swiftc -emit-module -emit-module-path Library.swiftmodule -module-name Library library.swift
$ swiftc -I . -O -emit-module user.swift
Assertion failed: ((module.getStage() == SILStage::Canonical || module.getStage() == SILStage::Lowered) && "Explicit type is valid only in canonical or lowered SIL"), function DifferentiableFunctionExtractInst, file lib/SIL/IR/SILInstructions.cpp, line 739.
Please submit a bug report (https://swift.org/contributing/#reporting-bugs) and include the project and the crash backtrace.
Stack dump:
0.  Program arguments: build/Ninja-ReleaseAssert/swift-macosx-x86_64/bin/swift-frontend -frontend -merge-modules -emit-module /var/folders/m_/6f7q8zfs3n9fr0c_4gy8840m00hc_q/T/user-b0d13c.swiftmodule -parse-as-library -disable-diagnostic-passes -disable-sil-perf-optzns -target x86_64-apple-darwin19.6.0 -enable-objc-interop -sdk /Applications/Xcode-beta.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk -color-diagnostics -I . -O -target-sdk-version 11.0 -emit-module-doc-path user.swiftdoc -emit-module-source-info-path user.swiftsourceinfo -module-name user -o user.swiftmodule
1.  Swift version 5.3-dev (LLVM 4603a02b51b9411, Swift 212acff1e1bdf77)
2.  While evaluating request ASTLoweringRequest(Lowering AST to SIL for module user)
3.  While deserializing SIL function "$s55AD__$s7Library3baryAA6StructVyxGAElF__vjp_src_0_wrt_0_lSf_Tgq5"
0  swift-frontend           0x0000000112e48a75 llvm::sys::PrintStackTrace(llvm::raw_ostream&) + 37
1  swift-frontend           0x0000000112e479b8 llvm::sys::RunSignalHandlers() + 248
2  swift-frontend           0x0000000112e49056 SignalHandler(int) + 262
3  libsystem_platform.dylib 0x00007fff706b85fd _sigtramp + 29
4  libsystem_platform.dylib 000000000000000000 _sigtramp + 18446603338630068768
5  libsystem_c.dylib        0x00007fff7058e808 abort + 120
6  libsystem_c.dylib        0x00007fff7058dac6 err + 0
7  swift-frontend           0x00000001131d32b3 swift::DifferentiableFunctionExtractInst::DifferentiableFunctionExtractInst(swift::SILModule&, swift::SILDebugLocation, swift::NormalDifferentiableFunctionTypeComponent, swift::SILValue, llvm::Optional<swift::SILType>) (.cold.1) + 35
8  swift-frontend           0x000000010f05419a swift::DifferentiableFunctionExtractInst::DifferentiableFunctionExtractInst(swift::SILModule&, swift::SILDebugLocation, swift::NormalDifferentiableFunctionTypeComponent, swift::SILValue, llvm::Optional<swift::SILType>) + 314
9  swift-frontend           0x000000010ef3e574 swift::SILDeserializer::readSILInstruction(swift::SILFunction*, swift::SILBuilder&, unsigned int, llvm::SmallVectorImpl<unsigned long long>&) + 5220
10 swift-frontend           0x000000010ef3b18c swift::SILDeserializer::readSILFunctionChecked(llvm::PointerEmbeddedInt<unsigned int, 31>, swift::SILFunction*, llvm::StringRef, bool, bool) + 6812

differentiability_function_extract instruction has an optional explicit extractee type. This is currently used by the TypeSubstCloner and the LoadableByAddress transformation, which need to rewrite differentiability_function_extract instructions while preserving @differentiable function type invariants. (See here for an explanation.)

There is an assertion that differentiability_function_extract instructions do not have explicit extractee types outside of canonical/lowered SIL. However, this does not handle the SIL deserialization case above: when a function containing a differentiable_function_extract with an explicit type is deserialized into a raw SIL module due to optimizations being enabled.


Single-file lit reproducer:

// RUN: %empty-directory(%t)
// RUN: %target-build-swift-dylib(%t/%target-library-name(cross_module_differentiation_other_new)) -emit-module -emit-module-path %t/cross_module_differentiation_other_new.swiftmodule -module-name cross_module_differentiation_other_new -DLIBRARY %s
// RUN: %target-build-swift -I %t -O -emit-module %s

#if LIBRARY

import _Differentiation

public struct Struct<Scalar>: Differentiable {}

@differentiable
public func foo<Scalar>(_ x: Struct<Scalar>) -> Struct<Scalar> { x }

@inlinable
@differentiable
public func bar<Scalar>(_ x: Struct<Scalar>) -> Struct<Scalar> {
  foo(x)
}

#else

import _Differentiation
import cross_module_differentiation_other_new

public func foo(
  body: @differentiable (Struct<Float>) -> Struct<Float> = bar
) {
  fatalError()
}

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

No branches or pull requests

1 participant