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

Variadic generics crash with Embedded Swift in SILCloner #72117

Closed
MaxDesiatov opened this issue Mar 6, 2024 · 1 comment · Fixed by #72990
Closed

Variadic generics crash with Embedded Swift in SILCloner #72117

MaxDesiatov opened this issue Mar 6, 2024 · 1 comment · Fixed by #72990
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. crash Bug: A crash, i.e., an abnormal termination of software embedded Embedded Swift SIL

Comments

@MaxDesiatov
Copy link
Member

MaxDesiatov commented Mar 6, 2024

Description

When building a snippet that contains variadic generics with swiftc -enable-experimental-feature Embedded -wmo repro.swift with latest development snapshot of the main branch of the toolchain, the compiler crashes in swift::SILCloner.

Reproduction

struct Mixer<each Source: Signal>: Signal {
  var sources: (repeat each Source)

  mutating func next() -> Float {
    var result: Float = 0

    sources = (repeat (each sources).next(accumulatingInto: &result))

    return result
  }
}

protocol Signal {
  mutating func next() -> Float
}

extension Signal {
  consuming func next(accumulatingInto: inout Float) -> Self {
    accumulatingInto += self.next()
    return self
  }
}

struct Constant: Signal { func next() -> Float { 42 } }

var mixer = Mixer(sources: Constant())
if mixer.next() == 42 {
  print("forty two")
} else {
  print("not forty two")
}

Stack dump

1.      Apple Swift version 6.0-dev (LLVM 7fe091223bc821e, Swift 92f5eeeefc88005)
2.      Compiling with effective version 5.10
3.      While evaluating request ExecuteSILPipelineRequest(Run pipelines { Non-Diagnostic Mandatory Optimizations, Serialization, Rest of Onone } on SIL for repro)
4.      While running pass #342 SILModuleTransform "CrossModuleOptimization".
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           0x00000001057d2e18 llvm::sys::PrintStackTrace(llvm::raw_ostream&, int) + 56
1  swift-frontend           0x00000001057d15e4 llvm::sys::RunSignalHandlers() + 112
2  swift-frontend           0x00000001057d3460 SignalHandler(int) + 304
3  libsystem_platform.dylib 0x00000001970e3584 _sigtramp + 56
4  swift-frontend           0x0000000100bfd6bc swift::SILCloner<(anonymous namespace)::InstructionVisitor>::doesOpTupleDisappear(swift::CanTypeWrapper<swift::TupleType>) + 260
5  swift-frontend           0x0000000100bf7208 (anonymous namespace)::InstructionVisitor::makeTypesUsableFromInline(swift::SILInstruction*, (anonymous namespace)::CrossModuleOptimization&) + 11384
6  swift-frontend           0x0000000100bf2f18 (anonymous namespace)::CrossModuleOptimization::serializeFunction(swift::SILFunction*, llvm::DenseMap<swift::SILFunction*, bool, llvm::DenseMapInfo<swift::SILFunction*, void>, llvm::detail::DenseMapPair<swift::SILFunction*, bool>> const&) + 268
7  swift-frontend           0x0000000100bf282c (anonymous namespace)::CrossModuleOptimizationPass::run() + 308
8  swift-frontend           0x0000000100d85e08 swift::SILPassManager::runModulePass(unsigned int) + 856
9  swift-frontend           0x0000000100d88174 swift::SILPassManager::execute() + 624
10 swift-frontend           0x0000000100d829ec swift::SILPassManager::executePassPipelinePlan(swift::SILPassPipelinePlan const&) + 72
11 swift-frontend           0x0000000100d8296c swift::ExecuteSILPipelineRequest::evaluate(swift::Evaluator&, swift::SILPipelineExecutionDescriptor) const + 68
12 swift-frontend           0x0000000100dbdd68 swift::SimpleRequest<swift::ExecuteSILPipelineRequest, std::__1::tuple<> (swift::SILPipelineExecutionDescriptor), (swift::RequestFlags)1>::evaluateRequest(swift::ExecuteSILPipelineRequest const&, swift::Evaluator&) + 28
13 swift-frontend           0x0000000100d9e63c swift::ExecuteSILPipelineRequest::OutputType swift::Evaluator::getResultUncached<swift::ExecuteSILPipelineRequest, swift::ExecuteSILPipelineRequest::OutputType swift::evaluateOrFatal<swift::ExecuteSILPipelineRequest>(swift::Evaluator&, swift::ExecuteSILPipelineRequest)::'lambda'()>(swift::ExecuteSILPipelineRequest const&, swift::ExecuteSILPipelineRequest::OutputType swift::evaluateOrFatal<swift::ExecuteSILPipelineRequest>(swift::Evaluator&, swift::ExecuteSILPipelineRequest)::'lambda'()) + 204
14 swift-frontend           0x0000000100d82bc8 swift::executePassPipelinePlan(swift::SILModule*, swift::SILPassPipelinePlan const&, bool, swift::irgen::IRGenModule*) + 64
15 swift-frontend           0x0000000100da0d48 swift::runSILPassesForOnone(swift::SILModule&) + 80
16 swift-frontend           0x00000001005edc34 swift::CompilerInstance::performSILProcessing(swift::SILModule*) + 252
17 swift-frontend           0x00000001003b3dec 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*) + 796
18 swift-frontend           0x00000001003b3724 swift::performCompileStepsPostSema(swift::CompilerInstance&, int&, swift::FrontendObserver*) + 1164
19 swift-frontend           0x00000001003c465c withSemanticAnalysis(swift::CompilerInstance&, swift::FrontendObserver*, llvm::function_ref<bool (swift::CompilerInstance&)>, bool) + 160
20 swift-frontend           0x00000001003b5b10 performCompile(swift::CompilerInstance&, int&, swift::FrontendObserver*) + 708
21 swift-frontend           0x00000001003b4a7c swift::performFrontend(llvm::ArrayRef<char const*>, char const*, void*, swift::FrontendObserver*) + 2368
22 swift-frontend           0x00000001001eb4dc swift::mainEntry(int, char const**) + 3096
23 dyld                     0x0000000196d2a0e0 start + 2360

Expected behavior

Variadic generics snippet compiles successfully or an actionable diagnostic error message is produced.

Environment

Apple Swift version 6.0-dev (LLVM 7fe091223bc821e, Swift 92f5eee)
Target: arm64-apple-macosx14.0

Additional information

No response

@MaxDesiatov MaxDesiatov added bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. crash Bug: A crash, i.e., an abnormal termination of software SIL labels Mar 6, 2024
@rauhul rauhul added the embedded Embedded Swift label Mar 6, 2024
@persidskiy
Copy link

persidskiy commented Mar 15, 2024

Hi, I am experiencing the same issue when compiling the following code with Whole Module Optimization and Testability enabled. There is no crash if I compile it either without WMO or testability.

Xcode: 15.3, Swift: 5.10.

public protocol View {}

public struct TupleView<T>: View {
    var children: T
    init<each Content>(_ content: repeat each Content)
        where repeat each Content: View, T == (repeat each Content) {
        children = (repeat each content)
    }
}

@resultBuilder
public struct ViewBuilder {
    public static func buildBlock<each Content>(_ content: repeat each Content) -> TupleView<(repeat each Content)> where repeat each Content: View {
        TupleView(repeat each content)
    }
}

A minimal reproducible SPM project is here: https://github.com/persidskiy/swift-wmo-crash

Stacktrace:

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 ExecuteSILPipelineRequest(Run pipelines { PrepareOptimizationPasses, EarlyModulePasses, HighLevel,Function+EarlyLoopOpt, HighLevel,Module+StackPromote, MidLevel,Function, ClosureSpecialize, LowLevel,Function, LateLoopOpt, SIL Debug Info Generator } on SIL for swift_crash)
4.      While running pass #1244 SILModuleTransform "CrossModuleOptimization".
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           0x0000000109de7f3c llvm::sys::PrintStackTrace(llvm::raw_ostream&, int) + 56
1  swift-frontend           0x0000000109de70f8 llvm::sys::RunSignalHandlers() + 112
2  swift-frontend           0x0000000109de8544 SignalHandler(int) + 360
3  libsystem_platform.dylib 0x000000018d3f9a24 _sigtramp + 56
4  swift-frontend           0x0000000104f60fb4 swift::SILCloner<(anonymous namespace)::InstructionVisitor>::doesOpTupleDisappear(swift::CanTypeWrapper<swift::TupleType>) + 300
5  swift-frontend           0x0000000104f5b89c (anonymous namespace)::InstructionVisitor::makeTypesUsableFromInline(swift::SILInstruction*, (anonymous namespace)::CrossModuleOptimization&) + 4276
6  swift-frontend           0x0000000104f58fb0 (anonymous namespace)::CrossModuleOptimization::serializeFunction(swift::SILFunction*, llvm::DenseMap<swift::SILFunction*, bool, llvm::DenseMapInfo<swift::SILFunction*, void>, llvm::detail::DenseMapPair<swift::SILFunction*, bool>> const&) + 228
7  swift-frontend           0x0000000104f587f8 (anonymous namespace)::CrossModuleOptimizationPass::run() + 468
8  swift-frontend           0x0000000105120978 swift::SILPassManager::executePassPipelinePlan(swift::SILPassPipelinePlan const&) + 16476
9  swift-frontend           0x0000000105159354 swift::SimpleRequest<swift::ExecuteSILPipelineRequest, std::__1::tuple<> (swift::SILPipelineExecutionDescriptor), (swift::RequestFlags)1>::evaluateRequest(swift::ExecuteSILPipelineRequest const&, swift::Evaluator&) + 56
10 swift-frontend           0x000000010513d184 llvm::Expected<swift::ExecuteSILPipelineRequest::OutputType> swift::Evaluator::getResultUncached<swift::ExecuteSILPipelineRequest>(swift::ExecuteSILPipelineRequest const&) + 476
11 swift-frontend           0x000000010513fe94 swift::runSILOptimizationPasses(swift::SILModule&) + 504
12 swift-frontend           0x00000001048f516c swift::CompilerInstance::performSILProcessing(swift::SILModule*) + 1224
13 swift-frontend           0x00000001046e5f98 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*) + 1128
14 swift-frontend           0x00000001046e5a18 swift::performCompileStepsPostSema(swift::CompilerInstance&, int&, swift::FrontendObserver*) + 2100
15 swift-frontend           0x00000001046e9694 performCompile(swift::CompilerInstance&, int&, swift::FrontendObserver*) + 1448
16 swift-frontend           0x00000001046e76d0 swift::performFrontend(llvm::ArrayRef<char const*>, char const*, void*, swift::FrontendObserver*) + 4968
17 swift-frontend           0x0000000104676e8c swift::mainEntry(int, char const**) + 2612
18 dyld                     0x000000018d0490e0 start + 2360

rjmccall added a commit to rjmccall/swift that referenced this issue Apr 11, 2024
It seems really unfortunate that we use SILCloner to, basically,
implement a recursive visitor of the types used in a SIL function,
but apparently it's what we do.

Fixes apple#72117.
rjmccall added a commit to rjmccall/swift that referenced this issue Apr 12, 2024
It seems really unfortunate that we use SILCloner to, basically,
implement a recursive visitor of the types used in a SIL function,
but apparently it's what we do.

Fixes apple#72117.
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. crash Bug: A crash, i.e., an abnormal termination of software embedded Embedded Swift SIL
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants