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-14241] [AutoDiff] Struct member ordering/naming causes compiler crash #56601

Closed
porterchild opened this issue Feb 17, 2021 · 11 comments
Closed
Assignees
Labels
AutoDiff bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. compiler The Swift compiler in itself

Comments

@porterchild
Copy link

Previous ID SR-14241
Radar rdar://problem/74659803
Original Reporter @porterchild
Type Bug
Status Resolved
Resolution Done

Attachment: Download

Environment

2/9 snapshot

Additional Detail from JIRA
Votes 1
Component/s Compiler
Labels Bug, AutoDiff
Assignee @rxwei
Priority Medium

md5: bc029c769146d96103460a0dc9cb8f14

Issue Description:

This ordering of struct members causes a compiler crash:

import _Differentiation

public struct Thing: Differentiable {
    public var simd: [Float]
    public var scalar: Float
}

and this ordering is fine:

public struct Thing: Differentiable {
    public var scalar: Float
    public var simd: [Float]
}

Also, changing the name to from "simd" to "array", "fish", "pineapple" etc. stops the crash:
("s", "si", "sim", and "simdd" also cause the crash)

public struct Thing: Differentiable {
    public var array: [Float]
    public var scalar: Float
}

Also, changing access control stops the crash (removing `public`).

As a possible hint, this wasn't an issue on the 1/19 snapshot. So it may be a recent regression.

Stack trace:

SIL verification failed: struct operand type does not match field type
  $Array<Float>.DifferentiableView
  $Float
Verifying instruction:
   %0 = argument of bb0 : $Float                  // user: %7
     %5 = apply %4<Float>(%3) : $@convention(method) <τ_0_0 where τ_0_0 : AdditiveArithmetic, τ_0_0 : Differentiable> (@thin Array<τ_0_0>.DifferentiableView.Type) -> @owned Array<τ_0_0>.DifferentiableView // user: %7
->   %7 = struct $Thing.TangentVector (%5 : $Array<Float>.DifferentiableView, %0 : $Float) // user: %8
     return %7 : $Thing.TangentVector             // id: %8
In function:
// pullback of Thing.scalar.getter with respect to parameters {0} and results {0}
sil [serialized] [canonical] [ossa] @$s15MockLoopManager5ThingV6scalarSfvgTJpSpSr : $@convention(thin) (Float, @owned _AD__$s15MockLoopManager5ThingV6scalarSfvg_bb0__PB__src_0_wrt_0) -> @owned Thing.TangentVector {
// %0                                             // user: %7
// %1                                             // user: %2
bb0(%0 : $Float, %1 : $_AD__$s15MockLoopManager5ThingV6scalarSfvg_bb0__PB__src_0_wrt_0):
  destructure_struct %1 : $_AD__$s15MockLoopManager5ThingV6scalarSfvg_bb0__PB__src_0_wrt_0 // id: %2
  %3 = metatype $@thin Array<Float>.DifferentiableView.Type // user: %5
  // function_ref static Array.DifferentiableView<A>.zero.getter
  %4 = function_ref @$sSa16_DifferentiationE18DifferentiableViewVAAs18AdditiveArithmeticRzAA0B0RzlE4zeroACyx_GvgZ : $@convention(method) <τ_0_0 where τ_0_0 : AdditiveArithmetic, τ_0_0 : Differentiable> (@thin Array<τ_0_0>.DifferentiableView.Type) -> @owned Array<τ_0_0>.DifferentiableView // user: %5
  %5 = apply %4<Float>(%3) : $@convention(method) <τ_0_0 where τ_0_0 : AdditiveArithmetic, τ_0_0 : Differentiable> (@thin Array<τ_0_0>.DifferentiableView.Type) -> @owned Array<τ_0_0>.DifferentiableView // user: %7
  %6 = tuple ()
  %7 = struct $Thing.TangentVector (%5 : $Array<Float>.DifferentiableView, %0 : $Float) // user: %8
  return %7 : $Thing.TangentVector                // id: %8
} // end sil function '$s15MockLoopManager5ThingV6scalarSfvgTJpSpSr'



 ... 



1.  Apple Swift version 5.4-dev (LLVM 1e4181b99f530d2, Swift 842cc9c88f5463b)
2.  While evaluating request ASTLoweringRequest(Lowering AST to SIL for module MockLoopManager)
3.  While verifying SIL function "@$s15MockLoopManager5ThingV6scalarSfvgTJpSpSr".
 for <<debugloc at "<compiler-generated>":0:0>>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           0x0000000111144857 llvm::sys::PrintStackTrace(llvm::raw_ostream&, int) + 39
1  swift-frontend           0x0000000111143a15 llvm::sys::RunSignalHandlers() + 85
2  swift-frontend           0x0000000111144e76 SignalHandler(int) + 262
3  libsystem_platform.dylib 0x00007fff7375c5fd _sigtramp + 29
4  libsystem_platform.dylib 0x00007fe8429b0590 _sigtramp + 18446743974105661360
5  libsystem_c.dylib        0x00007fff73632808 abort + 120
6  swift-frontend           0x000000010d363e66 (anonymous namespace)::SILVerifier::_require(bool, llvm::Twine const&, std::__1::function<void ()> const&) + 1558
7  swift-frontend           0x000000010d36d91c swift::SILInstructionVisitor<(anonymous namespace)::SILVerifier, void>::visit(swift::SILInstruction*) + 18268
8  swift-frontend           0x000000010d3684e4 (anonymous namespace)::SILVerifier::visitSILBasicBlock(swift::SILBasicBlock*) + 1428
9  swift-frontend           0x000000010d366b6c (anonymous namespace)::SILVerifier::visitSILFunction(swift::SILFunction*) + 9468
10 swift-frontend           0x000000010d36304e swift::SILModule::verify() const + 286
11 swift-frontend           0x000000010cb8b834 swift::Lowering::SILGenModule::~SILGenModule() + 36
12 swift-frontend           0x000000010cb955f3 swift::ASTLoweringRequest::evaluate(swift::Evaluator&, swift::ASTLoweringDescriptor) const + 5987
13 swift-frontend           0x000000010cc5e56e 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&) + 142
14 swift-frontend           0x000000010cb985a1 llvm::Expected<swift::ASTLoweringRequest::OutputType> swift::Evaluator::getResultUncached<swift::ASTLoweringRequest>(swift::ASTLoweringRequest const&) + 417
15 swift-frontend           0x000000010cb95783 swift::performASTLowering(swift::ModuleDecl*, swift::Lowering::TypeConverter&, swift::SILOptions const&) + 179
16 swift-frontend           0x000000010c7daf6d performCompileStepsPostSema(swift::CompilerInstance&, int&, swift::FrontendObserver*) + 509
17 swift-frontend           0x000000010c7d0028 swift::performFrontend(llvm::ArrayRef<char const*>, char const*, void*, swift::FrontendObserver*) + 5016
18 swift-frontend           0x000000010c76a566 main + 566
19 libdyld.dylib            0x00007fff73563cc9 start + 1
20 libdyld.dylib            0x0000000000000044 start + 18446603338581132156
@porterchild
Copy link
Author

@rxwei I'm transitioning from the TF 0.12 release and I've got a cascade of compiler crashes that look related to this one, I wonder if you could take a look? Seems like there's a chance it's a recent regression, so hopefully there's a quick fix. If it's not readily solvable I can go about finding workarounds.

@porterchild
Copy link
Author

Just want to make sure this one is on your radar @rxwei, I'd like to take advantage of some recent fixes like SR-14106 , but I'm stuck back on the 1/19 toolchain because of this bug. I'm effectively blocked by either this bug or SR-14106 depending on the toolchain, so I'm going back to the TF toolchain for now.

@rxwei
Copy link
Member

rxwei commented Feb 23, 2021

@porterchild Thanks. Will take a look this week.

@rxwei
Copy link
Member

rxwei commented Feb 23, 2021

@swift-ci create

@porterchild
Copy link
Author

Thanks!

@rxwei
Copy link
Member

rxwei commented Feb 23, 2021

@porterchild I'm unable to reproduce this with swiftc in swift-DEVELOPMENT-SNAPSHOT-2021-02-20-a. Can you share your exact steps to reproduce this crash?

@porterchild
Copy link
Author

I'm trying to get a command line reproducer, getting some weird error. I'll upload the Xcode project version in the meantime

Uploaded. Does that crash for you?

@rxwei
Copy link
Member

rxwei commented Feb 24, 2021

Thanks, it helped me find the actual problem. This happens when we read SIL modules. Here's a command line reproducer.

xcrun /Library/Developer/Toolchains/swift-DEVELOPMENT-SNAPSHOT-2021-02-20-a.xctoolchain/usr/bin/swift-frontend -module-name SR_14241 -emit-module SR-14241.swift
xcrun /Library/Developer/Toolchains/swift-DEVELOPMENT-SNAPSHOT-2021-02-20-a.xctoolchain/usr/bin/swift-frontend -c SR_14241.swiftmodule

@rxwei
Copy link
Member

rxwei commented Feb 24, 2021

One strange behavior here is that when you emit SIL, the tangent vector properties are laid out in a different order.

swiftc SR-14241.swift -emit-silgen
public struct Thing : Differentiable {
  @differentiable(reverse, wrt: self)
  @_hasStorage public var simd: [Float] { get set }
  @differentiable(reverse, wrt: self)
  @_hasStorage public var scalar: Float { get set }
  struct TangentVector : AdditiveArithmetic, Differentiable {
    static func + (lhs: Thing.TangentVector, rhs: Thing.TangentVector) -> Thing.TangentVector
    static func - (lhs: Thing.TangentVector, rhs: Thing.TangentVector) -> Thing.TangentVector
    @_implements(Equatable, ==(_:_:)) static func __derived_struct_equals(_ a: Thing.TangentVector, _ b: Thing.TangentVector) -> Bool
    typealias TangentVector = Thing.TangentVector
    init(simd: Array<Float>.DifferentiableView, scalar: Float)
    @_hasStorage var scalar: Float { get set }
    @_hasStorage var simd: Array<Float>.DifferentiableView { get set }
    static var zero: Thing.TangentVector { get }
  }
  init(simd: [Float], scalar: Float)
  mutating func move(by offset: Thing.TangentVector)
}

@rxwei
Copy link
Member

rxwei commented Feb 24, 2021

If you do `-print-ast` the layout is correct however:

import _Differentiation
public struct Thing : Differentiable {
  @differentiable(reverse, wrt: self)
  public var simd: [Float]
  @differentiable(reverse, wrt: self)
  public var scalar: Float
  public struct TangentVector : AdditiveArithmetic, Differentiable {
    public var simd: Array<Float>.TangentVector
    public var scalar: Float.TangentVector
    public static func + (lhs: Thing.TangentVector, rhs: Thing.TangentVector) -> Thing.TangentVector
    public static func - (lhs: Thing.TangentVector, rhs: Thing.TangentVector) -> Thing.TangentVector
    @_implements(Equatable, ==(_:_:)) public static func __derived_struct_equals(_ a: Thing.TangentVector, _ b: Thing.TangentVector) -> Bool
    public typealias TangentVector = Thing.TangentVector
    public init(simd: Array<Float>.TangentVector, scalar: Float.TangentVector)
    public static var zero: Thing.TangentVector { get }
  }
  internal init(simd: [Float], scalar: Float)
  public mutating func move(by offset: Thing.TangentVector)
} 

@rxwei
Copy link
Member

rxwei commented Feb 25, 2021

CodaFi (JIRA User) offered a lot of help bugging this and #36154 is a possible fix.

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

No branches or pull requests

2 participants