swiftlang / swift

The Swift Programming Language
https://swift.org
Apache License 2.0
67.65k stars 10.38k forks source link

Optimizer crash #62270

Open dabrahams opened 1 year ago

dabrahams commented 1 year ago

Compile this with -O for a crash in Swift 5.7.1

@inline(never)
func test1(_ x: AnySequence<String?>) {
  _ = x.lazy.map({ $0 ?? "_" }).joined2()
}

extension Sequence where Element: StringProtocol {
  @inline(__always)
  internal func joined2(separator: String = "") -> String {
    if let me = self as? any Collection<Element> {
      return me.joined3(separator: separator)
    }
    fatalError()
  }
}

extension Collection where Element: StringProtocol {
  @inline(__always)
  internal func joined3(separator: String = "") -> String {
    _ = self.lazy.map {$0}
    fatalError()
  }
}
@inline(never)
func test1(_ x: AnySequence<String?>) {
  _ = x.lazy.map({ $0 ?? "_" }).joined2()
}

extension Sequence where Element: StringProtocol {
  @inline(__always)
  internal func joined2(separator: String = "") -> String {
    if let me = self as? any Collection<Element> {
      return me.joined3(separator: separator)
    }
    fatalError()
  }
}

extension Collection where Element: StringProtocol {
  @inline(__always)
  internal func joined3(separator: String = "") -> String {
    _ = self.lazy.map {$0}
    fatalError()
  }
}
error: compile command failed due to signal 11 (use -v to see invocation)
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 x.swift -target arm64-apple-macosx13.0 -Xllvm -aarch64-use-tbi -enable-objc-interop -stack-check -sdk /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX13.0.sdk -O -new-driver-path /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/swift-driver -empty-abi-descriptor -resource-dir /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/swift -module-name x -disable-clang-spi -target-sdk-version 13.0 -o /var/folders/wg/wvvfbgl96f727hzqdtpfrrl80000gn/T/TemporaryDirectory.j3vFEB/x-1.o
1.  Apple Swift version 5.7.1 (swiftlang-5.7.1.135.3 clang-1400.0.29.51)
2.  Compiling with the current language version
3.  While evaluating request IRGenRequest(IR Generation for file "x.swift")
4.  While emitting IR SIL function "@$s1x5test1yys11AnySequenceVySSSgGF".
 for 'test1(_:)' (at x.swift:2: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           0x00000001076575b0 llvm::sys::PrintStackTrace(llvm::raw_ostream&, int) + 56
1  swift-frontend           0x00000001076565b4 llvm::sys::RunSignalHandlers() + 112
2  swift-frontend           0x0000000107657c34 SignalHandler(int) + 344
3  libsystem_platform.dylib 0x000000019c96c2a4 _sigtramp + 56
4  swift-frontend           0x0000000103035854 void llvm::function_ref<void (swift::irgen::GenericRequirement)>::callback_fn<swift::irgen::NecessaryBindings::computeBindings(swift::irgen::IRGenModule&, swift::CanTypeWrapper<swift::SILFunctionType>, swift::SubstitutionMap, bool, bool)::$_7>(long, swift::irgen::GenericRequirement) + 188
5  swift-frontend           0x000000010303471c void llvm::function_ref<void (swift::irgen::GenericRequirement)>::callback_fn<(anonymous namespace)::PolymorphicConvention::enumerateUnfulfilledRequirements(llvm::function_ref<void (swift::irgen::GenericRequirement)> const&)::$_12>(long, swift::irgen::GenericRequirement) + 176
6  swift-frontend           0x0000000103027a30 swift::irgen::enumerateGenericSignatureRequirements(swift::CanGenericSignature, llvm::function_ref<void (swift::irgen::GenericRequirement)> const&) + 380
7  swift-frontend           0x000000010302e93c swift::irgen::NecessaryBindings::computeBindings(swift::irgen::IRGenModule&, swift::CanTypeWrapper<swift::SILFunctionType>, swift::SubstitutionMap, bool, bool) + 512
8  swift-frontend           0x0000000102fcd12c 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) + 384
9  swift-frontend           0x00000001030bea70 swift::SILInstructionVisitor<(anonymous namespace)::IRGenSILFunction, void>::visit(swift::SILInstruction*) + 77152
10 swift-frontend           0x00000001030a6d84 swift::irgen::IRGenModule::emitSILFunction(swift::SILFunction*) + 7860
11 swift-frontend           0x0000000102f73418 swift::irgen::IRGenerator::emitGlobalTopLevel(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&) + 1552
12 swift-frontend           0x0000000103074bec swift::IRGenRequest::evaluate(swift::Evaluator&, swift::IRGenDescriptor) const + 2556
13 swift-frontend           0x00000001030a4c04 swift::SimpleRequest<swift::IRGenRequest, swift::GeneratedModule (swift::IRGenDescriptor), (swift::RequestFlags)9>::evaluateRequest(swift::IRGenRequest const&, swift::Evaluator&) + 188
14 swift-frontend           0x00000001030810e0 llvm::Expected<swift::IRGenRequest::OutputType> swift::Evaluator::getResultUncached<swift::IRGenRequest>(swift::IRGenRequest const&) + 744
15 swift-frontend           0x0000000103077fd4 swift::performIRGeneration(swift::FileUnit*, swift::IRGenOptions const&, swift::TBDGenOptions const&, std::__1::unique_ptr<swift::SILModule, std::__1::default_delete<swift::SILModule> >, llvm::StringRef, swift::PrimarySpecificPaths const&, llvm::StringRef, llvm::GlobalVariable**) + 236
16 swift-frontend           0x0000000102ca2b40 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*) + 2160
17 swift-frontend           0x0000000102ca48cc swift::performFrontend(llvm::ArrayRef<char const*>, char const*, void*, swift::FrontendObserver*) + 5932
18 swift-frontend           0x0000000102c453c0 swift::mainEntry(int, char const**) + 3940
19 dyld                     0x000000019c613e50 start + 2544

Reproduces on Mac and on Linux (godbolt).

AnthonyLatsis commented 1 year ago

Here’s a simpler example producing a different and possibly more insightful stack trace (Swift version 5.8-dev):

protocol P {
  associatedtype Element
}
protocol Q<Element>: P {
  var element: Element { get }
}

extension Q where Element: Equatable {
  func bar() {
    _ = self.element
  }
}

func foo<T: P>(_ p: T) where T.Element: Equatable {
  if let q = p as? any Q<T.Element> {
    q.bar()
  }
}

struct S: P {
  typealias Element = Int
}
func test(s: S) {
  foo(s)
}