swiftlang / swift

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

Assertion failure from swiftc compiling `WidgetKit.swiftmodule` from `WidgetKit.swiftmodule/arm64-apple-ios.swiftinterface` #68192

Open SharonXSharon opened 12 months ago

SharonXSharon commented 12 months ago

Description

The swiftc throws an assertion failure (ty->isLegalSILType() && "constructing SILType with type that should have been " "eliminated by SIL lowering"), function SILType, file SILType.h, line 114, when building the WidgetKit.swiftmodule from the WidgetKit.swiftmodule/arm64-apple-ios.swiftinterface.

Full trace of the assertion:

1.  Swift version 5.9-dev (LLVM 8e2daa73d613753, Swift a1292875235823f)
2.  Compiling with the current language version
3.  While evaluating request ExecuteSILPipelineRequest(Run pipelines { PrepareOptimizationPasses, EarlyModulePasses, HighLevel,Function+EarlyLoopOpt, HighLevel,Module+StackPromote } on SIL for WidgetKit)
4.  While running pass #15612 SILModuleTransform "SerializeSILPass".
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           0x0000000107ca3e40 llvm::sys::PrintStackTrace(llvm::raw_ostream&, int) + 56
1  swift-frontend           0x0000000107ca3108 llvm::sys::RunSignalHandlers() + 112
2  swift-frontend           0x0000000107ca447c SignalHandler(int) + 304
3  libsystem_platform.dylib 0x00000001865fea24 _sigtramp + 56
4  libsystem_pthread.dylib  0x00000001865cfc28 pthread_kill + 288
5  libsystem_c.dylib        0x00000001864ddae8 abort + 180
6  libsystem_c.dylib        0x00000001864dce44 err + 0
7  swift-frontend           0x0000000107fcab00 swift::SILCloner<(anonymous namespace)::MapOpaqueArchetypes>::getOpASTType(swift::CanType) (.cold.4) + 0
8  swift-frontend           0x00000001036f6008 swift::SILCloner<(anonymous namespace)::MapOpaqueArchetypes>::getOpASTType(swift::CanType) + 176
9  swift-frontend           0x00000001036f0558 swift::SILCloner<(anonymous namespace)::MapOpaqueArchetypes>::visitScalarPackIndexInst(swift::ScalarPackIndexInst*) + 80
10 swift-frontend           0x00000001036e75f0 updateOpaqueArchetypes(swift::SILFunction&) + 2336
11 swift-frontend           0x00000001036f6794 SerializeSILPass::removeSerializedFlagFromAllFunctions(swift::SILModule&) + 148
12 swift-frontend           0x00000001035a8d3c swift::SILPassManager::runModulePass(unsigned int) + 792
13 swift-frontend           0x00000001035aaf70 swift::SILPassManager::execute() + 624
14 swift-frontend           0x00000001035a5d98 swift::SILPassManager::executePassPipelinePlan(swift::SILPassPipelinePlan const&) + 72
15 swift-frontend           0x00000001035a5d34 swift::ExecuteSILPipelineRequest::evaluate(swift::Evaluator&, swift::SILPipelineExecutionDescriptor) const + 52
16 swift-frontend           0x00000001035d5aec swift::SimpleRequest<swift::ExecuteSILPipelineRequest, std::__1::tuple<> (swift::SILPipelineExecutionDescriptor), (swift::RequestFlags)1>::evaluateRequest(swift::ExecuteSILPipelineRequest const&, swift::Evaluator&) + 28
17 swift-frontend           0x00000001035b3530 llvm::Expected<swift::ExecuteSILPipelineRequest::OutputType> swift::Evaluator::getResultUncached<swift::ExecuteSILPipelineRequest>(swift::ExecuteSILPipelineRequest const&) + 252
18 swift-frontend           0x00000001035a5f98 swift::executePassPipelinePlan(swift::SILModule*, swift::SILPassPipelinePlan const&, bool, swift::irgen::IRGenModule*) + 68
19 swift-frontend           0x00000001035b5c00 swift::runSILOptimizationPasses(swift::SILModule&) + 156
20 swift-frontend           0x0000000102e7153c swift::CompilerInstance::performSILProcessing(swift::SILModule*) + 260
21 swift-frontend           0x0000000102e80360 swift::ExplicitModuleInterfaceBuilder::buildSwiftModuleFromInterface(llvm::StringRef, llvm::StringRef, bool, std::__1::unique_ptr<llvm::MemoryBuffer, std::__1::default_delete<llvm::MemoryBuffer>>*, llvm::ArrayRef<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char>>>, llvm::StringRef) + 2568
22 swift-frontend           0x0000000102e81574 std::__1::error_code llvm::function_ref<std::__1::error_code (swift::SubCompilerInstanceInfo&)>::callback_fn<swift::ImplicitModuleInterfaceBuilder::buildSwiftModuleInternal(llvm::StringRef, bool, std::__1::unique_ptr<llvm::MemoryBuffer, std::__1::default_delete<llvm::MemoryBuffer>>*, llvm::ArrayRef<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char>>>)::$_3::operator()() const::'lambda'(swift::SubCompilerInstanceInfo&)>(long, swift::SubCompilerInstanceInfo&) + 184
23 swift-frontend           0x0000000102e8a7dc swift::InterfaceSubContextDelegateImpl::runInSubCompilerInstance(llvm::StringRef, llvm::StringRef, llvm::StringRef, swift::SourceLoc, bool, llvm::function_ref<std::__1::error_code (swift::SubCompilerInstanceInfo&)>) + 3116
24 swift-frontend           0x0000000102e81348 void llvm::function_ref<void ()>::callback_fn<swift::ImplicitModuleInterfaceBuilder::buildSwiftModuleInternal(llvm::StringRef, bool, std::__1::unique_ptr<llvm::MemoryBuffer, std::__1::default_delete<llvm::MemoryBuffer>>*, llvm::ArrayRef<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char>>>)::$_3>(long) + 192
25 swift-frontend           0x0000000107bdfd24 llvm::CrashRecoveryContext::RunSafely(llvm::function_ref<void ()>) + 208
26 swift-frontend           0x0000000107be0068 RunSafelyOnThread_Dispatch(void*) + 52
27 swift-frontend           0x0000000107be01b8 void* llvm::thread::ThreadProxy<std::__1::tuple<void (*)(void*), (anonymous namespace)::RunSafelyOnThreadInfo*>>(void*) + 24
28 libsystem_pthread.dylib  0x00000001865cffa8 _pthread_start + 148
29 libsystem_pthread.dylib  0x00000001865cada0 thread_start + 8
zsh: abort       -frontend -compile-module-from-interface  -suppress-warnings -sdk     -o 

Steps to reproduce

  1. Get a swiftc (5.9) with asserts enabled. I pulled the open-source swift 5.9 branch https://github.com/apple/swift/tree/release/5.9 and built the toolchain following https://github.com/apple/swift/blob/release/5.9/docs/HowToGuides/GettingStarted.md
  2. Install the Xcode 15 Beta 7 (15A5229h).
  3. Run the build command below and observe the assertion:
    PATH_TO_YOUR_LOCAL_ASSERTS_SWIFTC -frontend -compile-module-from-interface -sdk PATH_TO_YOUR_XCODE15.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS17.0.sdk PATH_TO_YOUR_XCODE15.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS17.0.sdk/System/Library/Frameworks/WidgetKit.framework/Modules/WidgetKit.swiftmodule/arm64-apple-ios.swiftinterface -module-name WidgetKit -o /tmp/WidgetKit.swiftmodule 

Expected behavior

Compilation finishes successfully without assertion failure.

Environment

manman-ren commented 11 months ago

@gottesmm @eeckstein Hey Erik and Michael, wondering if you have any pointer/suggestion on how to fix this issue?

Thanks, Manman

eeckstein commented 11 months ago

@manman-ren thanks for the ping. I'll check who can take care of this

nate-chandler commented 11 months ago

Corresponding radar: rdar://115355709

drodriguez commented 11 months ago

Using the Xcode 15.0 RC (15A240d), I have been able to avoid the crash by commenting the @_alwaysEmitIntoClient annotation of DynamicIslandExpandedContentBuilder.buildPartialBlock<C0, C1>(accumulated: WidgetKit.DynamicIslandExpandedContent<C0>, next: WidgetKit.DynamicIslandExpandedRegion<C1>) -> WidgetKit.DynamicIslandExpandedContent<some SwiftUI.View> where C0 : SwiftUI.View, C1 : SwiftUI.View

A minimal repro seems to be the using the following .swiftinterface file:

// swift-interface-format-version: 1.0
// swift-compiler-version: Apple Swift version 5.9 (swiftlang-5.9.0.123.204 clang-1500.0.37.1)
// swift-module-flags: -target arm64-apple-ios17.0 -enable-objc-interop -enable-library-evolution -swift-version 5 -enforce-exclusivity=checked -O -library-level api -module-name WidgetKit
// swift-module-flags-ignorable: -enable-experimental-feature FreestandingMacros -enable-bare-slash-regex -user-module-version 401.1.107
import SwiftUI
@available(iOS 16.1, *)
@available(macOS, unavailable)
@available(tvOS, unavailable)
@available(watchOS, unavailable)
public struct DynamicIslandExpandedContent<Content> where Content : SwiftUI.View {
  @usableFromInline
  internal var content: Content
  @usableFromInline
  internal init(@SwiftUI.ViewBuilder content: () -> Content)
}
@available(iOS 16.1, *)
@available(macOS, unavailable)
@available(tvOS, unavailable)
@available(watchOS, unavailable)
@_functionBuilder public struct DynamicIslandExpandedContentBuilder {
  @_alwaysEmitIntoClient public static func buildPartialBlock<C>(first: WidgetKit.DynamicIslandExpandedRegion<C>) -> WidgetKit.DynamicIslandExpandedContent<some SwiftUI.View> where C : SwiftUI.View {
        DynamicIslandExpandedContent {
            first._viewRepresentation
        }
    }

  // REMOVE @_alwaysEmitIntoClient HERE
  @_alwaysEmitIntoClient public static func buildPartialBlock<C0, C1>(accumulated: WidgetKit.DynamicIslandExpandedContent<C0>, next: WidgetKit.DynamicIslandExpandedRegion<C1>) -> WidgetKit.DynamicIslandExpandedContent<some SwiftUI.View> where C0 : SwiftUI.View, C1 : SwiftUI.View {
        DynamicIslandExpandedContent {
            accumulated.content
            next._viewRepresentation
        }
    }
}
@available(iOS 16.1, *)
@available(macOS, unavailable)
@available(tvOS, unavailable)
@available(watchOS, unavailable)
public struct DynamicIslandExpandedRegion<Content> where Content : SwiftUI.View {
@SwiftUI.ViewBuilder content: () -> Content)
  public var _viewRepresentation: some SwiftUI.View {
    get
  }
}

I was able to get the results described in issue with the swift.org toolchain downloaded from https://download.swift.org/swift-5.9-branch/xcode/swift-5.9-DEVELOPMENT-SNAPSHOT-2023-09-05-a/swift-5.9-DEVELOPMENT-SNAPSHOT-2023-09-05-a-osx.pkg