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-9709] IRGen crash in emitPartialApplicationForwarder #52147

Closed
dan-zheng opened this issue Jan 20, 2019 · 2 comments
Closed

[SR-9709] IRGen crash in emitPartialApplicationForwarder #52147

dan-zheng opened this issue Jan 20, 2019 · 2 comments
Assignees
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. compiler The Swift compiler in itself IRGen LLVM IR generation

Comments

@dan-zheng
Copy link
Collaborator

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

swift: DEVELOPMENT-SNAPSHOT-2019-01-16-a from https://swift.org/download/#snapshots

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

md5: be328039c0dc2312016a971130143fb3

Issue Description:

The following program crashes:

class InnerClass<T> {}

struct Tensor<T> {
  var x: InnerClass<T>
  init() { x = InnerClass<T>() }
}

func jvpFoo<T>(_ x: Tensor<T>) -> (Tensor<T>, (Tensor<T>) -> Tensor<T>) {
  return (x, { v in v })
}

func test(_ x: Tensor<Float>) {
  let tmp = jvpFoo as (Tensor<Float>) -> (Tensor<Float>, (Tensor<Float>) -> Tensor<Float>)
  print(tmp(x))
}
print(test(Tensor()))
Assertion failed: (castIsValid(op, S, Ty) && "Invalid cast!"), function Create, file /Users/buildnode/jenkins/workspace/oss-swift-package-osx/llvm/lib/IR/Instructions.cpp, line 2463.
Stack dump:
0.  Program arguments: /Library/Developer/Toolchains/swift-DEVELOPMENT-SNAPSHOT-2019-01-16-a.xctoolchain/usr/bin/swift -frontend -interpret crash.swift -enable-objc-interop -sdk /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.14.sdk -color-diagnostics -module-name crash
1.  While emitting IR SIL function "@$s5crash4testyyAA6TensorVySfGF".
 for 'test(_:)' (at crash.swift:12:1)
0  swift                    0x00000001053a5048 llvm::sys::PrintStackTrace(llvm::raw_ostream&) + 40
1  swift                    0x00000001053a42c5 llvm::sys::RunSignalHandlers() + 85
2  swift                    0x00000001053a5652 SignalHandler(int) + 258
3  libsystem_platform.dylib 0x00007fff74912f5a _sigtramp + 26
4  libsystem_platform.dylib 0x0000000000003e98 _sigtramp + 2339311448
5  libsystem_c.dylib        0x00007fff746b01ae abort + 127
6  libsystem_c.dylib        0x00007fff746781ac basename_r + 0
7  swift                    0x00000001052601fb llvm::CastInst::Create(llvm::Instruction::CastOps, llvm::Value*, llvm::Type*, llvm::Twine const&, llvm::Instruction*) + 1483
8  swift                    0x00000001016f897e emitPartialApplicationForwarder(swift::irgen::IRGenModule&, llvm::Optional<swift::irgen::FunctionPointer> const&, bool, swift::irgen::Signature const&, swift::CanTypeWrapper<swift::SILFunctionType>, swift::CanTypeWrapper<swift::SILFunctionType>, swift::CanTypeWrapper<swift::SILFunctionType>, swift::SubstitutionMap, swift::irgen::HeapLayout const*, llvm::ArrayRef<swift::ParameterConvention>) + 8846
9  swift                    0x00000001016f60ba swift::irgen::emitFunctionPartialApplication(swift::irgen::IRGenFunction&, swift::SILFunction&, swift::irgen::FunctionPointer const&, llvm::Value*, swift::irgen::Explosion&, llvm::ArrayRef<swift::SILParameterInfo>, swift::SubstitutionMap, swift::CanTypeWrapper<swift::SILFunctionType>, swift::CanTypeWrapper<swift::SILFunctionType>, swift::CanTypeWrapper<swift::SILFunctionType>, swift::irgen::Explosion&, bool) + 4058
10 swift                    0x00000001017b244b swift::SILInstructionVisitor<(anonymous namespace)::IRGenSILFunction, void>::visit(swift::SILInstruction*) + 50075
11 swift                    0x00000001017a26f2 swift::irgen::IRGenModule::emitSILFunction(swift::SILFunction*) + 7954
12 swift                    0x00000001016aa24b swift::irgen::IRGenerator::emitLazyDefinitions() + 683
13 swift                    0x000000010177b976 performIRGeneration(swift::IRGenOptions&, swift::ModuleDecl*, std::__1::unique_ptr<swift::SILModule, std::__1::default_delete<swift::SILModule> >, llvm::StringRef, swift::PrimarySpecificPaths const&, llvm::LLVMContext&, swift::SourceFile*, llvm::GlobalVariable**) + 1318
14 swift                    0x000000010177a38b swift::performIRGeneration(swift::IRGenOptions&, swift::ModuleDecl*, std::__1::unique_ptr<swift::SILModule, std::__1::default_delete<swift::SILModule> >, llvm::StringRef, swift::PrimarySpecificPaths const&, llvm::LLVMContext&, llvm::ArrayRef<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > >, llvm::GlobalVariable**) + 795
15 swift                    0x00000001016524d7 swift::RunImmediately(swift::CompilerInstance&, std::__1::vector<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >, std::__1::allocator<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > > > const&, swift::IRGenOptions&, swift::SILOptions const&) + 311
16 swift                    0x000000010162d20c performCompile(swift::CompilerInstance&, swift::CompilerInvocation&, llvm::ArrayRef<char const*>, int&, swift::FrontendObserver*, swift::UnifiedStatsReporter*) + 12876
17 swift                    0x0000000101628f52 swift::performFrontend(llvm::ArrayRef<char const*>, char const*, void*, swift::FrontendObserver*) + 2978
18 swift                    0x00000001015daf2e main + 686
19 libdyld.dylib            0x00007fff74604015 start + 1
[1]    16239 abort      swift crash.swift

The problem occurs during IRGen for an partial_apply instruction in test that applies only a generic parameter.

// test(_:)
sil hidden @$s5crash4testyyAA6TensorVySfGF : $@convention(thin) (@guaranteed Tensor<Float>) -> () {
// %0                                             // users: %12, %1
bb0(%0 : $Tensor<Float>):
  debug_value %0 : $Tensor<Float>, let, name "x", argno 1 // id: %1
  // function_ref jvpFoo<A>(_:)
  %2 = function_ref @$s5crash6jvpFooyAA6TensorVyxG_A2EctAElF : $@convention(thin) <τ_0_0> (@guaranteed Tensor<τ_0_0>) -> (@owned Tensor<τ_0_0>, @owned @callee_guaranteed (@guaranteed Tensor<τ_0_0>) -> @owned Tensor<τ_0_0>) // user: %3
  %3 = partial_apply [callee_guaranteed] %2<Float>() : $@convention(thin) <τ_0_0> (@guaranteed Tensor<τ_0_0>) -> (@owned Tensor<τ_0_0>, @owned @callee_guaranteed (@guaranteed Tensor<τ_0_0>) -> @owned Tensor<τ_0_0>) // users: %33, %31, %12, %11, %4
  ...
} // end sil function '$s5crash4testyyAA6TensorVySfGF'

A bitcast between these two types fails:

{ %T5crash10InnerClassC*, i8*, %swift.refcounted* } // InnerClass
{ %T5crash10InnerClassCySfG*, i8*, %swift.refcounted* } // InnerClass<Swift.Float>
@dan-zheng
Copy link
Collaborator Author

This problem occurs only when the outer struct type ({{Tensor}}) wraps a class type ({{InnerClass}}).

If the inner class is removed, IRGen no longer crashes.

struct Tensor<T> {
  // No inner class.
}

func jvpFoo<T>(_ x: Tensor<T>) -> (Tensor<T>, (Tensor<T>) -> Tensor<T>) {
  return (x, { v in v })
}

func test(_ x: Tensor<Float>) {
  let tmp = jvpFoo as (Tensor<Float>) -> (Tensor<Float>, (Tensor<Float>) -> Tensor<Float>)
  print(tmp(x))
}
print(test(Tensor()))

EDIT: If I simply change Tensor from a struct with no contents to a class, IRGen crashes with the same error. This is a simpler reproducer.

@rxwei
Copy link
Member

rxwei commented Jan 20, 2019

I sent an initial fix to the tensorflow branch to unblock our things: #22005 @rjmccall Could you take a look and see if this is the right fix to upstream?

@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
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. compiler The Swift compiler in itself IRGen LLVM IR generation
Projects
None yet
Development

No branches or pull requests

2 participants