swiftlang / swift

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

Assert failure on `extend_lifetime` verify with `onone-simplification` pass disabled #77451

Open kovdan01 opened 1 week ago

kovdan01 commented 1 week ago

Description

When passing a member function to a free function as an argument and having onone-simplification pass disabled (swift-frontend -emit-sil -Xllvm -sil-disable-pass=OnoneSimplification), we have an assertion failure when verifying extend_lifetime instruction with self operand.

Reproduction

func caller<Thing: FloatingPoint>(
  of f: (_: Thing) -> Thing
) -> Int {
   return 42
}

public struct Solution2<Thing: FloatingPoint> {
    public static func optimization() -> Int {
        return caller(of: simulationWithLoss)
    }

    static func simulationWithLoss(input: Thing) -> Thing {
        return input // implementation
    }
}

Stack dump

0.      Program arguments: /path/to/swift/build/Ninja-RelWithDebInfoAssert/swift-linux-x86_64/bin/swift-frontend -emit-sil -Xllvm -sil-disable-pass=OnoneSimplification extend.swift
1.      Swift version 6.1-dev (LLVM 1f4c018cf20a994, Swift 45657fe1dc2350c)
2.      Compiling with effective version 5.10
3.      While verifying SIL function "@$s6extend9Solution2V12optimizationSiyFZ".
 for 'optimization()' (at extend.swift:8:12)
 #0 0x00000ca2f03e4e48 llvm::sys::PrintStackTrace(llvm::raw_ostream&, int) /path/to/swift/llvm-project/llvm/lib/Support/Unix/Signals.inc:723:13
 #1 0x00000ca2f03e2f70 llvm::sys::RunSignalHandlers() /path/to/swift/llvm-project/llvm/lib/Support/Signals.cpp:106:18
 #2 0x00000ca2f03e54c8 SignalHandler(int) /path/to/swift/llvm-project/llvm/lib/Support/Unix/Signals.inc:413:1
 #3 0x00006fd55164c1d0 (/usr/lib/libc.so.6+0x3d1d0)
 #4 0x00006fd5516a53f4 __pthread_kill_implementation /usr/src/debug/glibc/glibc/nptl/pthread_kill.c:44:76
 #5 0x00006fd55164c120 raise /usr/src/debug/glibc/glibc/signal/../sysdeps/posix/raise.c:27:6
 #6 0x00006fd5516334c3 abort /usr/src/debug/glibc/glibc/stdlib/abort.c:81:7
 #7 0x00000ca2ea8cb715 (/path/to/swift/build/Ninja-RelWithDebInfoAssert/swift-linux-x86_64/bin/swift-frontend+0x2297715)
 #8 0x00000ca2ea8dd028 std::_Function_base::~_Function_base() /sbin/../lib64/gcc/x86_64-pc-linux-gnu/14.2.1/../../../../include/c++/14.2.1/bits/std_function.h:243:11
 #9 0x00000ca2ea8dd028 (anonymous namespace)::SILVerifier::checkExtendLifetimeInst(swift::ExtendLifetimeInst*) /path/to/swift/swift/lib/SIL/Verifier/SILVerifier.cpp:2722:5
#10 0x00000ca2ea8dd028 (anonymous namespace)::SILVerifierBase<(anonymous namespace)::SILVerifier>::visitExtendLifetimeInst(swift::ExtendLifetimeInst*) /path/to/swift/swift/include/swift/SIL/SILNodes.def:871:1
#11 0x00000ca2ea8dd028 swift::SILInstructionVisitor<(anonymous namespace)::SILVerifier, void>::visit(swift::SILInstruction*) /path/to/swift/swift/include/swift/SIL/SILNodes.def:871:1
#12 0x00000ca2ea8dd028 swift::SILVisitorBase<(anonymous namespace)::SILVerifier, void>::visitSILBasicBlock(swift::SILBasicBlock*) /path/to/swift/swift/include/swift/SIL/SILVisitor.h:38:16
#13 0x00000ca2ea8d4e2c llvm::cl::opt_storage<bool, false, false>::getValue() const /path/to/swift/llvm-project/llvm/include/llvm/Support/CommandLine.h:1406:38
#14 0x00000ca2ea8d4e2c llvm::cl::opt_storage<bool, false, false>::operator bool() const /path/to/swift/llvm-project/llvm/include/llvm/Support/CommandLine.h:1410:38
#15 0x00000ca2ea8d4e2c (anonymous namespace)::SILVerifier::verifyDebugScopeHoles(swift::SILBasicBlock*) /path/to/swift/swift/lib/SIL/Verifier/SILVerifier.cpp:7024:10
#16 0x00000ca2ea8d4e2c (anonymous namespace)::SILVerifier::visitSILBasicBlock(swift::SILBasicBlock*) /path/to/swift/swift/lib/SIL/Verifier/SILVerifier.cpp:7136:5
#17 0x00000ca2ea8d318b bool std::operator==<swift::SILBasicBlock**>(std::reverse_iterator<swift::SILBasicBlock**> const&, std::reverse_iterator<swift::SILBasicBlock**> const&) /sbin/../lib64/gcc/x86_64-pc-linux-gnu/14.2.1/../../../../include/c++/14.2.1/bit
s/stl_iterator.h:443:25
#18 0x00000ca2ea8d318b bool std::operator!=<swift::SILBasicBlock**>(std::reverse_iterator<swift::SILBasicBlock**> const&, std::reverse_iterator<swift::SILBasicBlock**> const&) /sbin/../lib64/gcc/x86_64-pc-linux-gnu/14.2.1/../../../../include/c++/14.2.1/bit
s/stl_iterator.h:457:20
#19 0x00000ca2ea8d318b (anonymous namespace)::SILVerifier::visitSILBasicBlocks(swift::SILFunction*) /path/to/swift/swift/lib/SIL/Verifier/SILVerifier.cpp:7152:57
#20 0x00000ca2ea8d318b (anonymous namespace)::SILVerifier::visitSILFunction(swift::SILFunction*) /path/to/swift/swift/lib/SIL/Verifier/SILVerifier.cpp:7321:5
#21 0x00000ca2ea8cb8f5 swift::SILFunction::verify(swift::CalleeCache*, bool, bool, bool) const /path/to/swift/swift/lib/SIL/Verifier/SILVerifier.cpp:7380:1
#22 0x00000ca2ea8cf258 swift::SILModule::verify(swift::CalleeCache*, bool, bool) const /path/to/swift/swift/lib/SIL/Verifier/SILVerifier.cpp:7702:29
#23 0x00000ca2ea8cf10f swift::CalleeCache::~CalleeCache() /path/to/swift/swift/include/swift/SIL/CalleeCache.h:157:15
#24 0x00000ca2ea8cf10f swift::SILModule::verify(bool, bool) const /path/to/swift/swift/lib/SIL/Verifier/SILVerifier.cpp:7690:1
#25 0x00000ca2e9839d64 swift::CompilerInstance::performSILProcessing(swift::SILModule*) /path/to/swift/swift/lib/Frontend/Frontend.cpp:1845:3
#26 0x00000ca2e950a3a7 performCompileStepsPostSILGen(swift::CompilerInstance&, std::unique_ptr<swift::SILModule, std::default_delete<swift::SILModule>>, llvm::PointerUnion<swift::ModuleDecl*, swift::SourceFile*>, swift::PrimarySpecificPaths const&, int&, s
wift::FrontendObserver*) /path/to/swift/swift/lib/FrontendTool/FrontendTool.cpp:1707:7
#27 0x00000ca2e95097b9 swift::performCompileStepsPostSema(swift::CompilerInstance&, int&, swift::FrontendObserver*) /path/to/swift/swift/lib/FrontendTool/FrontendTool.cpp:732:12
#28 0x00000ca2e9519eea withSemanticAnalysis(swift::CompilerInstance&, swift::FrontendObserver*, llvm::function_ref<bool (swift::CompilerInstance&)>, bool) /path/to/swift/swift/lib/FrontendTool/FrontendTool.cpp:1155:25
#29 0x00000ca2e950c136 performAction(swift::CompilerInstance&, int&, swift::FrontendObserver*) /path/to/swift/swift/lib/FrontendTool/FrontendTool.cpp:0:12
#30 0x00000ca2e950c136 performCompile(swift::CompilerInstance&, int&, swift::FrontendObserver*) /path/to/swift/swift/lib/FrontendTool/FrontendTool.cpp:1367:19
#31 0x00000ca2e950b2e5 swift::performFrontend(llvm::ArrayRef<char const*>, char const*, void*, swift::FrontendObserver*) /path/to/swift/swift/lib/FrontendTool/FrontendTool.cpp:2075:19
#32 0x00000ca2e92cfffe run_driver(llvm::StringRef, llvm::ArrayRef<char const*>, llvm::ArrayRef<char const*>) /path/to/swift/swift/lib/DriverTool/driver.cpp:0:0
#33 0x00000ca2e92cfffe swift::mainEntry(int, char const**) /path/to/swift/swift/lib/DriverTool/driver.cpp:530:10
#34 0x00006fd551634e08 __libc_start_call_main /usr/src/debug/glibc/glibc/csu/../sysdeps/nptl/libc_start_call_main.h:74:3
#35 0x00006fd551634ecc call_init /usr/src/debug/glibc/glibc/csu/../csu/libc-start.c:128:20
#36 0x00006fd551634ecc __libc_start_main /usr/src/debug/glibc/glibc/csu/../csu/libc-start.c:347:5
#37 0x00000ca2e92cefb5 _start (/path/to/swift/build/Ninja-RelWithDebInfoAssert/swift-linux-x86_64/bin/swift-frontend+0xc9afb5)

Expected behavior

I suppose that extend_lifetime instruction should handle self properly

Environment

Swift version 6.1-dev

Additional information

No response

kovdan01 commented 1 week ago

Tagging @asl

asl commented 1 week ago

Also tagging @atrick @eeckstein

eeckstein commented 1 week ago

@atrick The extend_lifetime is created in SILGenFunction::destroyLocalVariable. Can you please take a look?

tbkka commented 1 week ago

I'm curious: Can you explain why you're disabling onone-simplification?

kovdan01 commented 1 week ago

I'm curious: Can you explain why you're disabling onone-simplification?

@tbkka I was working on another AutoDiff-related issue, and some tests there run with disabled onone-simplification. So, just occasionally revealed this crash while constructing new test cases.

atrick commented 3 days ago

PR: https://github.com/swiftlang/swift/pull/77575