swiftlang / swift

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

5.10 "LoadableByAddress" non-deterministic compiler crash when parameter pack closure saved to instance property. #74609

Open vanvoorden opened 1 week ago

vanvoorden commented 1 week ago

Description

https://forums.swift.org/t/non-deterministic-5-10-compiler-failures-from-predictablememoryaccessoptimizations-and-swift-5-10/72630

This is a repro for a PredictableMemoryAccessOptimizations crash I originally saw in another project. There's a lot going on in that project… I attempted to look for a more "clean" repro. This is now reporting as a LoadableByAddress crash… but the code is following a similar pattern to my other project.

This crash seems to be non-deterministic… sometimes it crashes… sometimes it doesn't.

This code does not (for now) crash for me from the 6.0 toolchain. I'm only seeing the crashes from production Swift 5.10 (and Xcode 15.4).

It looks like this will be fixed once 6.0 goes live in Q3. I would really like to find some kind of workaround that could unblock me when building from production Xcode 15.4. Any ideas for that would be great!

Reproduction

struct Repeater<each Element> {
  let elements: (repeat () -> each Element)
}

struct Box<T> {

}

let repeater: Repeater<Box<Int>>

This code does not crash with probability one. I can clean and build again and I eventually see a crash.

Saving the parameter pack elements as the return values of a closure pack seems to be important to the crash. It also seems to be important for the repeating element type to be a generic type.

I can prevent the crash with probability one by not saving the closure pack to a variable:

struct Repeater<each Element> {
  //  let elements: (repeat () -> each Element)
}

struct Box<T> {

}

let repeater: Repeater<Box<Int>>

Stack dump

Please submit a bug report (https://swift.org/contributing/#reporting-bugs) and include the crash backtrace.
Stack dump:
0.  Program arguments: /Applications/Xcode_15.4.0.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/swift-frontend -frontend -c -primary-file /Users/rick/Desktop/RepeaterDemo/Sources/main.swift -emit-dependencies-path /Users/rick/Library/Developer/Xcode/DerivedData/RepeaterDemo-aoiveyvurevauqfmtsmslveqfmoc/Build/Intermediates.noindex/RepeaterDemo.build/Debug/RepeaterDemo.build/Objects-normal/arm64/main.d -emit-const-values-path /Users/rick/Library/Developer/Xcode/DerivedData/RepeaterDemo-aoiveyvurevauqfmtsmslveqfmoc/Build/Intermediates.noindex/RepeaterDemo.build/Debug/RepeaterDemo.build/Objects-normal/arm64/main.swiftconstvalues -emit-reference-dependencies-path /Users/rick/Library/Developer/Xcode/DerivedData/RepeaterDemo-aoiveyvurevauqfmtsmslveqfmoc/Build/Intermediates.noindex/RepeaterDemo.build/Debug/RepeaterDemo.build/Objects-normal/arm64/main.swiftdeps -serialize-diagnostics-path /Users/rick/Library/Developer/Xcode/DerivedData/RepeaterDemo-aoiveyvurevauqfmtsmslveqfmoc/Build/Intermediates.noindex/RepeaterDemo.build/Debug/RepeaterDemo.build/Objects-normal/arm64/main.dia -target arm64-apple-macos14.0 -Xllvm -aarch64-use-tbi -enable-objc-interop -stack-check -sdk /Applications/Xcode_15.4.0.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX14.5.sdk -I /Users/rick/Library/Developer/Xcode/DerivedData/RepeaterDemo-aoiveyvurevauqfmtsmslveqfmoc/Build/Products/Debug -I /Applications/Xcode_15.4.0.app/Contents/Developer/Platforms/MacOSX.platform/Developer/usr/lib -F /Users/rick/Library/Developer/Xcode/DerivedData/RepeaterDemo-aoiveyvurevauqfmtsmslveqfmoc/Build/Products/Debug -F /Applications/Xcode_15.4.0.app/Contents/Developer/Platforms/MacOSX.platform/Developer/Library/Frameworks -no-color-diagnostics -enable-testing -g -module-cache-path /Users/rick/Library/Developer/Xcode/DerivedData/ModuleCache.noindex -swift-version 5 -enforce-exclusivity=checked -Onone -D SWIFT_PACKAGE -D DEBUG -D Xcode -serialize-debugging-options -package-name repeaterdemo -const-gather-protocols-file /Users/rick/Library/Developer/Xcode/DerivedData/RepeaterDemo-aoiveyvurevauqfmtsmslveqfmoc/Build/Intermediates.noindex/RepeaterDemo.build/Debug/RepeaterDemo.build/Objects-normal/arm64/RepeaterDemo_const_extract_protocols.json -empty-abi-descriptor -validate-clang-modules-once -clang-build-session-file /Users/rick/Library/Developer/Xcode/DerivedData/ModuleCache.noindex/Session.modulevalidation -Xcc -working-directory -Xcc /Users/rick/Desktop/RepeaterDemo -resource-dir /Applications/Xcode_15.4.0.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/swift -enable-anonymous-context-mangled-names -Xcc -ivfsstatcache -Xcc /Users/rick/Library/Developer/Xcode/DerivedData/SDKStatCaches.noindex/macosx14.5-23F73-12794b5fd74727c21755f2ea17ca96a1.sdkstatcache -Xcc -I/Users/rick/Library/Developer/Xcode/DerivedData/RepeaterDemo-aoiveyvurevauqfmtsmslveqfmoc/Build/Products/Debug/include -Xcc -I/Users/rick/Library/Developer/Xcode/DerivedData/RepeaterDemo-aoiveyvurevauqfmtsmslveqfmoc/Build/Intermediates.noindex/RepeaterDemo.build/Debug/RepeaterDemo.build/DerivedSources-normal/arm64 -Xcc -I/Users/rick/Library/Developer/Xcode/DerivedData/RepeaterDemo-aoiveyvurevauqfmtsmslveqfmoc/Build/Intermediates.noindex/RepeaterDemo.build/Debug/RepeaterDemo.build/DerivedSources/arm64 -Xcc -I/Users/rick/Library/Developer/Xcode/DerivedData/RepeaterDemo-aoiveyvurevauqfmtsmslveqfmoc/Build/Intermediates.noindex/RepeaterDemo.build/Debug/RepeaterDemo.build/DerivedSources -Xcc -DSWIFT_PACKAGE -Xcc -DDEBUG=1 -module-name RepeaterDemo -frontend-parseable-output -disable-clang-spi -target-sdk-version 14.5 -target-sdk-name macosx14.5 -external-plugin-path /Applications/Xcode_15.4.0.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX14.5.sdk/usr/lib/swift/host/plugins#/Applications/Xcode_15.4.0.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX14.5.sdk/usr/bin/swift-plugin-server -external-plugin-path /Applications/Xcode_15.4.0.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX14.5.sdk/usr/local/lib/swift/host/plugins#/Applications/Xcode_15.4.0.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX14.5.sdk/usr/bin/swift-plugin-server -external-plugin-path /Applications/Xcode_15.4.0.app/Contents/Developer/Platforms/MacOSX.platform/Developer/usr/lib/swift/host/plugins#/Applications/Xcode_15.4.0.app/Contents/Developer/Platforms/MacOSX.platform/Developer/usr/bin/swift-plugin-server -external-plugin-path /Applications/Xcode_15.4.0.app/Contents/Developer/Platforms/MacOSX.platform/Developer/usr/local/lib/swift/host/plugins#/Applications/Xcode_15.4.0.app/Contents/Developer/Platforms/MacOSX.platform/Developer/usr/bin/swift-plugin-server -plugin-path /Applications/Xcode_15.4.0.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/swift/host/plugins -plugin-path /Applications/Xcode_15.4.0.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/local/lib/swift/host/plugins -o /Users/rick/Library/Developer/Xcode/DerivedData/RepeaterDemo-aoiveyvurevauqfmtsmslveqfmoc/Build/Intermediates.noindex/RepeaterDemo.build/Debug/RepeaterDemo.build/Objects-normal/arm64/main.o -index-unit-output-path /RepeaterDemo.build/Debug/RepeaterDemo.build/Objects-normal/arm64/main.o -index-store-path /Users/rick/Library/Developer/Xcode/DerivedData/RepeaterDemo-aoiveyvurevauqfmtsmslveqfmoc/Index.noindex/DataStore -index-system-modules
1.  Apple Swift version 5.10 (swiftlang-5.10.0.13 clang-1500.3.9.4)
2.  Compiling with the current language version
3.  While evaluating request IRGenRequest(IR Generation for file "/Users/rick/Desktop/RepeaterDemo/Sources/main.swift")
4.  While converting type 'Repeater<Box<Int>>' (declared at [/Users/rick/Desktop/RepeaterDemo/Sources/main.swift:1:1 - line:3:1] RangeText="struct Repeater<each Element> {
  let elements: (repeat () -> each Element)
")
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           0x0000000105cabf3c llvm::sys::PrintStackTrace(llvm::raw_ostream&, int) + 56
1  swift-frontend           0x0000000105cab0f8 llvm::sys::RunSignalHandlers() + 112
2  swift-frontend           0x0000000105cac544 SignalHandler(int) + 360
3  libsystem_platform.dylib 0x0000000192893584 _sigtramp + 56
4  swift-frontend           0x00000001014348c8 (anonymous namespace)::SubstFunctionTypePatternVisitor::handleGenericNominalType(swift::Lowering::AbstractionPattern, swift::CanType) + 312
5  swift-frontend           0x00000001014348c8 (anonymous namespace)::SubstFunctionTypePatternVisitor::handleGenericNominalType(swift::Lowering::AbstractionPattern, swift::CanType) + 312
6  swift-frontend           0x0000000101432c2c (anonymous namespace)::SubstFunctionTypePatternVisitor::visit(swift::CanType, swift::Lowering::AbstractionPattern) + 1104
7  swift-frontend           0x00000001014326c8 (anonymous namespace)::SubstFunctionTypePatternVisitor::handleUnabstractedFunctionType(swift::CanTypeWrapper<swift::AnyFunctionType>, swift::Lowering::AbstractionPattern, swift::CanType, swift::Lowering::AbstractionPattern) + 208
8  swift-frontend           0x0000000101432234 swift::Lowering::AbstractionPattern::getSubstFunctionTypePattern(swift::CanTypeWrapper<swift::AnyFunctionType>, swift::Lowering::TypeConverter&, swift::Lowering::AbstractionPattern, swift::CanType, bool&) const + 236
9  swift-frontend           0x000000010147aad8 getSILFunctionType(swift::Lowering::TypeConverter&, swift::TypeExpansionContext, swift::Lowering::AbstractionPattern, swift::CanTypeWrapper<swift::AnyFunctionType>, swift::SILExtInfoBuilder, (anonymous namespace)::Conventions const&, swift::ForeignInfo const&, llvm::Optional<swift::SILDeclRef>, llvm::Optional<swift::SILDeclRef>, llvm::Optional<swift::SubstitutionMap>, swift::ProtocolConformanceRef) + 2008
10 swift-frontend           0x0000000101472cbc getNativeSILFunctionType(swift::Lowering::TypeConverter&, swift::TypeExpansionContext, swift::Lowering::AbstractionPattern, swift::CanTypeWrapper<swift::AnyFunctionType>, swift::SILExtInfoBuilder, llvm::Optional<swift::SILDeclRef>, llvm::Optional<swift::SILDeclRef>, llvm::Optional<swift::SubstitutionMap>, swift::ProtocolConformanceRef) + 2004
11 swift-frontend           0x0000000101519c78 swift::Lowering::TypeConverter::computeLoweredRValueType(swift::TypeExpansionContext, swift::Lowering::AbstractionPattern, swift::CanType)::LoweredRValueTypeVisitor::visitAnyFunctionType(swift::CanTypeWrapper<swift::AnyFunctionType>) + 804
12 swift-frontend           0x0000000101509334 swift::Lowering::TypeConverter::getTypeLowering(swift::Lowering::AbstractionPattern, swift::Type, swift::TypeExpansionContext) + 556
13 swift-frontend           0x00000001014fbe9c swift::SILType::getFieldType(swift::VarDecl*, swift::Lowering::TypeConverter&, swift::TypeExpansionContext) const + 532
14 swift-frontend           0x0000000100a38064 swift::irgen::TypeConverter::convertStructType(swift::TypeBase*, swift::CanType, swift::StructDecl*) + 5756
15 swift-frontend           0x0000000100a65360 swift::irgen::TypeConverter::getTypeEntry(swift::CanType) + 1108
16 swift-frontend           0x00000001009b11c4 swift::irgen::IRGenModule::emitSILGlobalVariable(swift::SILGlobalVariable*) + 208
17 swift-frontend           0x000000010092037c 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&) + 2292
18 swift-frontend           0x0000000100a7f940 swift::IRGenRequest::evaluate(swift::Evaluator&, swift::IRGenDescriptor) const + 4016
19 swift-frontend           0x0000000100ac954c swift::SimpleRequest<swift::IRGenRequest, swift::GeneratedModule (swift::IRGenDescriptor), (swift::RequestFlags)9>::evaluateRequest(swift::IRGenRequest const&, swift::Evaluator&) + 176
20 swift-frontend           0x0000000100a8c264 llvm::Expected<swift::IRGenRequest::OutputType> swift::Evaluator::getResultUncached<swift::IRGenRequest>(swift::IRGenRequest const&) + 836
21 swift-frontend           0x0000000100a82438 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**) + 264
22 swift-frontend           0x00000001005b00b8 generateIR(swift::IRGenOptions const&, swift::TBDGenOptions const&, std::__1::unique_ptr<swift::SILModule, std::__1::default_delete<swift::SILModule>>, swift::PrimarySpecificPaths const&, llvm::StringRef, llvm::PointerUnion<swift::ModuleDecl*, swift::SourceFile*>, llvm::GlobalVariable*&, llvm::ArrayRef<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char>>>) + 156
23 swift-frontend           0x00000001005aa294 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*) + 1892
24 swift-frontend           0x00000001005a9444 swift::performCompileStepsPostSema(swift::CompilerInstance&, int&, swift::FrontendObserver*) + 608
25 swift-frontend           0x00000001005ad694 performCompile(swift::CompilerInstance&, int&, swift::FrontendObserver*) + 1448
26 swift-frontend           0x00000001005ab6d0 swift::performFrontend(llvm::ArrayRef<char const*>, char const*, void*, swift::FrontendObserver*) + 4968
27 swift-frontend           0x000000010053ae8c swift::mainEntry(int, char const**) + 2612
28 dyld                     0x00000001924da0e0 start + 2360
Command SwiftCompile failed with a nonzero exit code

Environment

swift-driver version: 1.90.11.1 Apple Swift version 5.10 (swiftlang-5.10.0.13 clang-1500.3.9.4) Target: arm64-apple-macosx14.0

vanvoorden commented 3 days ago

@AnthonyLatsis Would you know if we were tracking anything related to this? This crash seems to be fixed in 6.0… but I don't know if someone landed a fix to this issue directly or this fix came along implicitly with a fix for a different issue. Were there any related 5.10 workarounds to another issue that might workaround this one before 6.0 ships? Thanks!