Skip to content

AST: Remove legacy GSB-based GenericSignature query implementation #40812

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

Merged
merged 1 commit into from
Jan 12, 2022

Conversation

slavapestov
Copy link
Contributor

@slavapestov slavapestov commented Jan 12, 2022

The RequirementMachine-based queries seem to work well these days, and in fact, we're not even testing the old implementation anymore. Only a handful of tests pass -requirement-machine=off or -requirement-machine=verify. There are of course countless bugs in the old implementation, and a few tests will already fail with it I imagine. The old code will remain in the release/5.6 branch, but I'm going to rip it out on main since it has outlived its purpose.

@slavapestov
Copy link
Contributor Author

@swift-ci Please smoke test

@slavapestov slavapestov merged commit af836ea into swiftlang:main Jan 12, 2022
@philipturner
Copy link
Contributor

@slavapestov there are several bugs with RequirementMachine that make me unable to run a lot of basic code with generics. I have to stick with the Xcode 13.2.1 Swift 5.5.2 toolchain just to get this to work. I tried the recent Swift 5.6 toolchain, and it isn't usable either. Is it possible to disable RequirementMachine so that I can proceed with using code that compiled just fine previously on Swift 5.5, while benefiting from lower-level AutoDiff bug fixes in the newer toolchains? I tried passing in -requirement-machine=off and it didn't change anything.

The error below was on the January 11, 2022 Swift 5.6 toolchain. Have these kinds of errors been fixed in the newer ones? If not, could you re-introduce the ability to disable RequirementMachine in Swift 5.6 for clients whose code will now fail to compile?

1.	Apple Swift version 5.6-dev (LLVM 64a4d678f114158, Swift 8ae983c3480462b)
2.	Compiling with the current language version
3.	While evaluating request TypeCheckSourceFileRequest(source_file "/Users/philipturner/Desktop/Experimentation4/Experimentation4/main.swift")
4.	While type-checking 'DifferentiableCollection' (at /Users/philipturner/Desktop/Experimentation4/Experimentation4/main.swift:22:8)
5.	While type-checking 'ElementTangentCollection' (at /Users/philipturner/Desktop/Experimentation4/Experimentation4/main.swift:30:3)
6.	While evaluating request RequirementRequest(Experimentation4.(file).DifferentiableCollection@/Users/philipturner/Desktop/Experimentation4/Experimentation4/main.swift:22:17, 0, interface)
7.	While evaluating request ResolveTypeRequest(while resolving type , ElementTangentCollection.Element, (null))
8.	While evaluating request RequirementSignatureRequest(Experimentation4.(file).DifferentiableCollection@/Users/philipturner/Desktop/Experimentation4/Experimentation4/main.swift:22:17)
9.	While evaluating request RequirementSignatureRequest(Experimentation4.(file).DifferentiableCollectionViewProtocol@/Users/philipturner/Desktop/Experimentation4/Experimentation4/main.swift:60:17)
10.	While checking generic signature <τ_0_0 where τ_0_0 : DifferentiableCollection, τ_0_0.Base : DifferentiableCollection, τ_0_0.TangentVector == τ_0_0.ElementTangentCollection.DifferentiableView.Base.DifferentiableView, τ_0_0.Base.TangentVector == τ_0_0.Base.ElementTangentCollection.DifferentiableView.TangentVector, τ_0_0.Element.TangentVector == τ_0_0.ElementTangentCollection.Element.TangentVector, τ_0_0.Base.Element.TangentVector == τ_0_0.Base.ElementTangentCollection.Element.TangentVector, τ_0_0.ElementTangentCollection.Element.TangentVector == τ_0_0.ElementTangentCollection.ElementTangentCollection.Element.TangentVector, τ_0_0.Base.DifferentiableView.Element.TangentVector == τ_0_0.Base.DifferentiableView.ElementTangentCollection.Element.TangentVector, τ_0_0.Base.ElementTangentCollection.Element.TangentVector == τ_0_0.Base.ElementTangentCollection.ElementTangentCollection.Element.TangentVector, τ_0_0.DifferentiableView.Base.Element.TangentVector == τ_0_0.DifferentiableView.Base.ElementTangentCollection.Element.TangentVector, τ_0_0.ElementTangentCollection.ElementTangentCollection.Element.TangentVector == τ_0_0.TangentVector.Base.ElementTangentCollection.Element.TangentVector, τ_0_0.Base.ElementTangentCollection.ElementTangentCollection.Element.TangentVector == τ_0_0.Base.TangentVector.ElementTangentCollection.Element.TangentVector, τ_0_0.Base.TangentVector.ElementTangentCollection.Element.TangentVector == τ_0_0.Base.ElementTangentCollection.DifferentiableView.ElementTangentCollection.Element.TangentVector, τ_0_0.TangentVector.Base.ElementTangentCollection.Element.TangentVector == τ_0_0.ElementTangentCollection.DifferentiableView.Base.ElementTangentCollection.Element.TangentVector> in requirement #2
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           0x00000001064c6044 llvm::sys::PrintStackTrace(llvm::raw_ostream&, int) + 56
1  swift-frontend           0x00000001064c52a4 llvm::sys::RunSignalHandlers() + 128
2  swift-frontend           0x00000001064c66a8 SignalHandler(int) + 304
3  libsystem_platform.dylib 0x00000001bb5304e4 _sigtramp + 56
4  libsystem_pthread.dylib  0x00000001bb518eb0 pthread_kill + 288
5  libsystem_c.dylib        0x00000001bb456314 abort + 164
6  libsystem_c.dylib        0x00000001bb45572c err + 0
7  swift-frontend           0x00000001068d7ba8 swift::GenericSignatureBuilder::computeGenericSignature(bool, swift::ProtocolDecl const*) && (.cold.40) + 0
8  swift-frontend           0x00000001032204e4 swift::GenericSignatureBuilder::computeGenericSignature(bool, swift::ProtocolDecl const*) && + 3200
9  swift-frontend           0x00000001032222e4 swift::RequirementSignatureRequest::evaluate(swift::Evaluator&, swift::ProtocolDecl*) const::$_71::operator()() const + 356
10 swift-frontend           0x0000000103221ed4 swift::RequirementSignatureRequest::evaluate(swift::Evaluator&, swift::ProtocolDecl*) const + 128
11 swift-frontend           0x0000000103172620 llvm::Expected<swift::RequirementSignatureRequest::OutputType> swift::Evaluator::getResultUncached<swift::RequirementSignatureRequest>(swift::RequirementSignatureRequest const&) + 408
12 swift-frontend           0x00000001031723ac llvm::Expected<swift::RequirementSignatureRequest::OutputType> swift::Evaluator::getResultCached<swift::RequirementSignatureRequest, (void*)0>(swift::RequirementSignatureRequest const&) + 136
13 swift-frontend           0x00000001030f48ec swift::RequirementSignatureRequest::OutputType swift::evaluateOrDefault<swift::RequirementSignatureRequest>(swift::Evaluator&, swift::RequirementSignatureRequest, swift::RequirementSignatureRequest::OutputType) + 56
14 swift-frontend           0x000000010320efbc swift::GenericSignatureBuilder::expandConformanceRequirement(swift::GenericSignatureBuilder::ResolvedType, swift::ProtocolDecl*, swift::GenericSignatureBuilder::RequirementSource const*, bool) + 316
15 swift-frontend           0x0000000103210914 swift::GenericSignatureBuilder::addConformanceRequirement(swift::GenericSignatureBuilder::ResolvedType, swift::ProtocolDecl*, swift::GenericSignatureBuilder::FloatingRequirementSource) + 228
16 swift-frontend           0x00000001032118bc swift::GenericSignatureBuilder::addTypeRequirement(llvm::PointerUnion<swift::GenericSignatureBuilder::PotentialArchetype*, swift::Type>, llvm::PointerUnion<swift::GenericSignatureBuilder::PotentialArchetype*, swift::Type>, swift::GenericSignatureBuilder::FloatingRequirementSource, swift::GenericSignatureBuilder::UnresolvedHandlingKind, swift::ModuleDecl*) + 1156
17 swift-frontend           0x0000000103237404 swift::GenericSignatureBuilder::ConstraintResult llvm::function_ref<swift::GenericSignatureBuilder::ConstraintResult (swift::Type, swift::TypeRepr const*)>::callback_fn<swift::GenericSignatureBuilder::addInheritedRequirements(swift::TypeDecl*, llvm::PointerUnion<swift::GenericSignatureBuilder::PotentialArchetype*, swift::Type>, swift::GenericSignatureBuilder::RequirementSource const*, swift::ModuleDecl*)::$_32>(long, swift::Type, swift::TypeRepr const*) + 508
18 swift-frontend           0x00000001032261f8 std::__1::__function::__func<visitInherited(llvm::PointerUnion<swift::TypeDecl const*, swift::ExtensionDecl const*>, llvm::function_ref<swift::GenericSignatureBuilder::ConstraintResult (swift::Type, swift::TypeRepr const*)>)::$_83, std::__1::allocator<visitInherited(llvm::PointerUnion<swift::TypeDecl const*, swift::ExtensionDecl const*>, llvm::function_ref<swift::GenericSignatureBuilder::ConstraintResult (swift::Type, swift::TypeRepr const*)>)::$_83>, void (swift::Type, swift::TypeRepr const*)>::operator()(swift::Type&&, swift::TypeRepr const*&&) + 236
19 swift-frontend           0x000000010320edfc swift::GenericSignatureBuilder::addInheritedRequirements(swift::TypeDecl*, llvm::PointerUnion<swift::GenericSignatureBuilder::PotentialArchetype*, swift::Type>, swift::GenericSignatureBuilder::RequirementSource const*, swift::ModuleDecl*) + 436
20 swift-frontend           0x000000010320f18c swift::GenericSignatureBuilder::expandConformanceRequirement(swift::GenericSignatureBuilder::ResolvedType, swift::ProtocolDecl*, swift::GenericSignatureBuilder::RequirementSource const*, bool) + 780
21 swift-frontend           0x0000000103210914 swift::GenericSignatureBuilder::addConformanceRequirement(swift::GenericSignatureBuilder::ResolvedType, swift::ProtocolDecl*, swift::GenericSignatureBuilder::FloatingRequirementSource) + 228
22 swift-frontend           0x00000001032118bc swift::GenericSignatureBuilder::addTypeRequirement(llvm::PointerUnion<swift::GenericSignatureBuilder::PotentialArchetype*, swift::Type>, llvm::PointerUnion<swift::GenericSignatureBuilder::PotentialArchetype*, swift::Type>, swift::GenericSignatureBuilder::FloatingRequirementSource, swift::GenericSignatureBuilder::UnresolvedHandlingKind, swift::ModuleDecl*) + 1156
23 swift-frontend           0x0000000103214028 swift::GenericSignatureBuilder::addRequirement(swift::Requirement const&, swift::RequirementRepr const*, swift::GenericSignatureBuilder::FloatingRequirementSource, swift::SubstitutionMap const*, swift::ModuleDecl*) + 808
24 swift-frontend           0x00000001032222d0 swift::RequirementSignatureRequest::evaluate(swift::Evaluator&, swift::ProtocolDecl*) const::$_71::operator()() const + 336
25 swift-frontend           0x0000000103221ed4 swift::RequirementSignatureRequest::evaluate(swift::Evaluator&, swift::ProtocolDecl*) const + 128
26 swift-frontend           0x0000000103172620 llvm::Expected<swift::RequirementSignatureRequest::OutputType> swift::Evaluator::getResultUncached<swift::RequirementSignatureRequest>(swift::RequirementSignatureRequest const&) + 408
27 swift-frontend           0x00000001031723ac llvm::Expected<swift::RequirementSignatureRequest::OutputType> swift::Evaluator::getResultCached<swift::RequirementSignatureRequest, (void*)0>(swift::RequirementSignatureRequest const&) + 136
28 swift-frontend           0x00000001030f48ec swift::RequirementSignatureRequest::OutputType swift::evaluateOrDefault<swift::RequirementSignatureRequest>(swift::Evaluator&, swift::RequirementSignatureRequest, swift::RequirementSignatureRequest::OutputType) + 56
29 swift-frontend           0x000000010320efbc swift::GenericSignatureBuilder::expandConformanceRequirement(swift::GenericSignatureBuilder::ResolvedType, swift::ProtocolDecl*, swift::GenericSignatureBuilder::RequirementSource const*, bool) + 316
30 swift-frontend           0x0000000103210914 swift::GenericSignatureBuilder::addConformanceRequirement(swift::GenericSignatureBuilder::ResolvedType, swift::ProtocolDecl*, swift::GenericSignatureBuilder::FloatingRequirementSource) + 228
31 swift-frontend           0x00000001032118bc swift::GenericSignatureBuilder::addTypeRequirement(llvm::PointerUnion<swift::GenericSignatureBuilder::PotentialArchetype*, swift::Type>, llvm::PointerUnion<swift::GenericSignatureBuilder::PotentialArchetype*, swift::Type>, swift::GenericSignatureBuilder::FloatingRequirementSource, swift::GenericSignatureBuilder::UnresolvedHandlingKind, swift::ModuleDecl*) + 1156
32 swift-frontend           0x0000000103214028 swift::GenericSignatureBuilder::addRequirement(swift::Requirement const&, swift::RequirementRepr const*, swift::GenericSignatureBuilder::FloatingRequirementSource, swift::SubstitutionMap const*, swift::ModuleDecl*) + 808
33 swift-frontend           0x000000010321ead0 swift::GenericSignatureBuilder::addGenericSignature(swift::GenericSignature) + 184
34 swift-frontend           0x0000000103013c20 swift::ASTContext::getOrCreateGenericSignatureBuilder(swift::CanGenericSignature) + 272
35 swift-frontend           0x00000001031fa220 swift::GenericSignatureImpl::lookupNestedType(swift::Type, swift::Identifier) const + 144
36 swift-frontend           0x0000000102ed2dc0 swift::TypeResolution::resolveDependentMemberType(swift::Type, swift::DeclContext*, swift::SourceRange, swift::ComponentIdentTypeRepr*) const + 208
37 swift-frontend           0x0000000102ed7c8c (anonymous namespace)::TypeResolver::resolveIdentifierType(swift::IdentTypeRepr*, swift::TypeResolutionOptions) + 1992
38 swift-frontend           0x0000000102ed52d8 (anonymous namespace)::TypeResolver::resolveType(swift::TypeRepr*, swift::TypeResolutionOptions) + 260
39 swift-frontend           0x0000000102ed50a0 swift::ResolveTypeRequest::evaluate(swift::Evaluator&, swift::TypeResolution const*, swift::TypeRepr*, swift::GenericParamList*) const + 72
40 swift-frontend           0x0000000102ee18ec llvm::Expected<swift::ResolveTypeRequest::OutputType> swift::Evaluator::getResultUncached<swift::ResolveTypeRequest>(swift::ResolveTypeRequest const&) + 252
41 swift-frontend           0x0000000102ed4f40 swift::ResolveTypeRequest::OutputType swift::evaluateOrDefault<swift::ResolveTypeRequest>(swift::Evaluator&, swift::ResolveTypeRequest, swift::ResolveTypeRequest::OutputType) + 44
42 swift-frontend           0x0000000102ed4ef4 swift::TypeResolution::resolveType(swift::TypeRepr*, swift::GenericParamList*) const + 68
43 swift-frontend           0x0000000102e6288c swift::RequirementRequest::evaluate(swift::Evaluator&, swift::WhereClauseOwner, unsigned int, swift::TypeResolutionStage) const + 572
44 swift-frontend           0x000000010331e0a0 llvm::Expected<swift::RequirementRequest::OutputType> swift::Evaluator::getResultUncached<swift::RequirementRequest>(swift::RequirementRequest const&) + 268
45 swift-frontend           0x000000010331de44 llvm::Expected<swift::RequirementRequest::OutputType> swift::Evaluator::getResultCached<swift::RequirementRequest, (void*)0>(swift::RequirementRequest const&) + 484
46 swift-frontend           0x000000010331b568 swift::WhereClauseOwner::visitRequirements(swift::TypeResolutionStage, llvm::function_ref<bool (swift::Requirement, swift::RequirementRepr*)>) const && + 272
47 swift-frontend           0x0000000102e3fc18 swift::ASTVisitor<(anonymous namespace)::DeclChecker, void, void, void, void, void, void>::visit(swift::Decl*) + 2788
48 swift-frontend           0x0000000102e3bdd4 (anonymous namespace)::DeclChecker::visit(swift::Decl*) + 224
49 swift-frontend           0x0000000102e3fb28 swift::ASTVisitor<(anonymous namespace)::DeclChecker, void, void, void, void, void, void>::visit(swift::Decl*) + 2548
50 swift-frontend           0x0000000102e3bdd4 (anonymous namespace)::DeclChecker::visit(swift::Decl*) + 224
51 swift-frontend           0x0000000102e3bce0 swift::TypeChecker::typeCheckDecl(swift::Decl*, bool) + 116
52 swift-frontend           0x0000000102ee2ea8 swift::TypeCheckSourceFileRequest::evaluate(swift::Evaluator&, swift::SourceFile*) const + 176
53 swift-frontend           0x0000000102ee4c64 llvm::Expected<swift::TypeCheckSourceFileRequest::OutputType> swift::Evaluator::getResultUncached<swift::TypeCheckSourceFileRequest>(swift::TypeCheckSourceFileRequest const&) + 400
54 swift-frontend           0x0000000102ee4a04 llvm::Expected<swift::TypeCheckSourceFileRequest::OutputType> swift::Evaluator::getResultCached<swift::TypeCheckSourceFileRequest, (void*)0>(swift::TypeCheckSourceFileRequest const&) + 124
55 swift-frontend           0x0000000102ee2cec swift::TypeCheckSourceFileRequest::OutputType swift::evaluateOrDefault<swift::TypeCheckSourceFileRequest>(swift::Evaluator&, swift::TypeCheckSourceFileRequest, swift::TypeCheckSourceFileRequest::OutputType) + 44
56 swift-frontend           0x00000001022f3020 swift::CompilerInstance::forEachFileToTypeCheck(llvm::function_ref<void (swift::SourceFile&)>) + 148
57 swift-frontend           0x00000001022f2f60 swift::CompilerInstance::performSema() + 76
58 swift-frontend           0x00000001021b5480 withSemanticAnalysis(swift::CompilerInstance&, swift::FrontendObserver*, llvm::function_ref<bool (swift::CompilerInstance&)>) + 56
59 swift-frontend           0x00000001021add60 swift::performFrontend(llvm::ArrayRef<char const*>, char const*, void*, swift::FrontendObserver*) + 2960
60 swift-frontend           0x000000010216c454 swift::mainEntry(int, char const**) + 500
61 dyld                     0x000000010eacd0f4 start + 520

@BradLarson
Copy link
Contributor

@philipturner - In pretty much every case we've encountered so far, RequirementMachine has only exposed pre-existing problems and allowed us to fix them at the source (see SR-15530 as one example). I think it's inappropriate to ask for this to be turned off when the problems most likely lie elsewhere. We should instead isolate and fix the real issues here.

@philipturner
Copy link
Contributor

philipturner commented Jan 29, 2022

I think it's inappropriate to ask for this to be turned off when the problems most likely lie elsewhere.

@BradLarson this code produces an error caused exclusively by RequirementMachine:

protocol DifferentiableCollection
where INVALID_1 == DifferentiableCollectionView<ElementTangentCollection> {
  associatedtype ElementTangentCollection: DifferentiableCollection
  where ElementTangentCollection.INVALID_2 == INVALID_3
}

extension DifferentiableCollection {
  typealias DifferentiableView = DifferentiableCollectionView<Self>
}

struct DifferentiableCollectionView<Base: DifferentiableCollection> {}

That's what I meant when I said "unable to run a lot of basic code with generics". This instance has nothing to do with AutoDiff, yet it's blocking further progress on my end.

Full link to crash reproducer:

https://github.com/philipturner/swift-differentiable-collection-experiments/blob/main/compiler_crashers/crash_1.swift

@philipturner
Copy link
Contributor

I’m just hoping that I can build S4TF on the 5.6 release toolchain using my Differentiation package. A massive amount of crashes are gone when RequirementMachine is disabled. Otherwise, I’ll be stuck on 5.5.2 for Swift-Colab and iOS until WWDC 2022.

@slavapestov
Copy link
Contributor Author

@philipturner I didn't remove anything in the Swift 5.6 branch. The -requirement-machine=off flag still works there. The code you showed doesn't crash in the RequirementMachine, it crashes in the GSB:

7 swift-frontend 0x00000001068d7ba8 swift::GenericSignatureBuilder::computeGenericSignature(bool, swift::ProtocolDecl const*) && (.cold.40) + 0
8 swift-frontend 0x00000001032204e4 swift::GenericSignatureBuilder::computeGenericSignature(bool, swift::ProtocolDecl const*) && + 3200
9 swift-frontend 0x00000001032222e4 swift::RequirementSignatureRequest::evaluate(swift::Evaluator&, swift::ProtocolDecl*) const::$_71::operator()() const + 356
10 swift-frontend 0x0000000103221ed4 swift::RequirementSignatureRequest::evaluate(swift::Evaluator&, swift::ProtocolDecl*) const + 128

@slavapestov
Copy link
Contributor Author

About the other assertion:

Re-entrant construction of requirement machine for <τ_0_0 where τ_0_0 : DifferentiableCollection>

Passing -requirement-machine=off will indeed work around this, but it's a real bug. The GSB silently allowed re-entrant construction, but you would "see" a partially-constructed instance which would return invalid results for queries. The root cause of the issue should be addressed elsewhere.

@philipturner
Copy link
Contributor

philipturner commented Jan 30, 2022

Thanks for the explanation you gave me. What about the code I cited in the comment to Brad Larson? I just reproduced it on Swift main and it still crashes. It seems to be an incorrect assert. Would you mind helping me file a test for it and the crash you just explained?

I just narrowed down the bug you explained, and it indeed is caused by AutoDiff. It won't happen unless I add import _Differentiation. This is unlike the one I cited in the comment to Brad Larson.

@slavapestov
Copy link
Contributor Author

I tried your test case on main with the feature flags which turn off the GenericSignatureBuilder entirely, using the RequirementMachine for all generic signature computations: -Xfrontend -requirement-machine-protocol-signatures=on -Xfrontend -requirement-machine-inferred-signatures=on -Xfrontend -requirement-machine-abstract-signatures=on and it no longer crashes:

protocol DifferentiableCollection
where INVALID_1 == DifferentiableCollectionView<ElementTangentCollection> {
  associatedtype ElementTangentCollection: DifferentiableCollection
  where ElementTangentCollection.INVALID_2 == INVALID_3
}

extension DifferentiableCollection {
  typealias DifferentiableView = DifferentiableCollectionView<Self>
}

struct DifferentiableCollectionView<Base: DifferentiableCollection> {}
circular.swift:2:7: error: cannot find type 'INVALID_1' in scope
where INVALID_1 == DifferentiableCollectionView<ElementTangentCollection> {
      ^~~~~~~~~
circular.swift:1:10: error: circular reference
protocol DifferentiableCollection
         ^
circular.swift:11:8: note: through reference here
struct DifferentiableCollectionView<Base: DifferentiableCollection> {}
       ^
circular.swift:2:20: note: while resolving type 'DifferentiableCollectionView<ElementTangentCollection>'
where INVALID_1 == DifferentiableCollectionView<ElementTangentCollection> {
                   ^
circular.swift:1:10: note: through reference here
protocol DifferentiableCollection
         ^
circular.swift:1:10: error: circular reference
protocol DifferentiableCollection
         ^
circular.swift:1:10: note: through reference here
protocol DifferentiableCollection
         ^
circular.swift:1:10: note: through reference here
protocol DifferentiableCollection
         ^
circular.swift:11:8: note: through reference here
struct DifferentiableCollectionView<Base: DifferentiableCollection> {}
       ^
circular.swift:2:20: note: while resolving type 'DifferentiableCollectionView<ElementTangentCollection>'
where INVALID_1 == DifferentiableCollectionView<ElementTangentCollection> {
                   ^
circular.swift:1:10: note: through reference here
protocol DifferentiableCollection
         ^
circular.swift:1:10: error: circular reference
protocol DifferentiableCollection
         ^
circular.swift:1:10: note: through reference here
protocol DifferentiableCollection
         ^
circular.swift:1:10: note: through reference here
protocol DifferentiableCollection
         ^
circular.swift:11:8: note: through reference here
struct DifferentiableCollectionView<Base: DifferentiableCollection> {}
       ^
circular.swift:2:20: note: while resolving type 'DifferentiableCollectionView<ElementTangentCollection>'
where INVALID_1 == DifferentiableCollectionView<ElementTangentCollection> {
                   ^
circular.swift:1:10: note: through reference here
protocol DifferentiableCollection
         ^
circular.swift:1:10: error: circular reference
protocol DifferentiableCollection
         ^
circular.swift:1:10: note: through reference here
protocol DifferentiableCollection
         ^
circular.swift:1:10: note: through reference here
protocol DifferentiableCollection
         ^
circular.swift:11:8: note: through reference here
struct DifferentiableCollectionView<Base: DifferentiableCollection> {}
       ^
circular.swift:2:20: note: while resolving type 'DifferentiableCollectionView<ElementTangentCollection>'
where INVALID_1 == DifferentiableCollectionView<ElementTangentCollection> {
                   ^
circular.swift:4:47: error: cannot find type 'INVALID_3' in scope
  where ElementTangentCollection.INVALID_2 == INVALID_3
                                              ^~~~~~~~~
circular.swift:4:34: error: 'INVALID_2' is not a member type of type 'Self.ElementTangentCollection'
  where ElementTangentCollection.INVALID_2 == INVALID_3
        ~~~~~~~~~~~~~~~~~~~~~~~~ ^
circular.swift:2:20: error: type 'Self.ElementTangentCollection' does not conform to protocol 'DifferentiableCollection'
where INVALID_1 == DifferentiableCollectionView<ElementTangentCollection> {
                   ^

The large number of 'circular reference' diagnostics is unfortunate but that's an old problem.

@slavapestov
Copy link
Contributor Author

I looked at crash4.swift in your repository. Here is a reduction without import _Differentiation:

public protocol Differentiable {
  associatedtype TangentVector: Differentiable
    where TangentVector.TangentVector == TangentVector
}

public protocol DifferentiableCollection: Collection, Differentiable
where
  Element: Differentiable,
  TangentVector == ElementTangentCollection.DifferentiableView
{
  associatedtype DifferentiableView: DifferentiableCollectionViewProtocol
  associatedtype ElementTangentCollection: DifferentiableCollection
}

public protocol DifferentiableCollectionViewProtocol: DifferentiableCollection {
  associatedtype Base: DifferentiableCollection
}

This crashes in an assert build of 5.5 and 5.6 with this assertion:

Assertion failed: (isCanonicalAnchor(secondType)), function checkGenericSignature, file GenericSignatureBuilder.cpp, line 8486.

and on main it's slightly different but it's the same error:

Right hand side does not have a canonical parent: same_type: τ_0_0.Base.TangentVector τ_0_0.Base.ElementTangentCollection.DifferentiableView.TangentVector

The problem in all cases is the GenericSignatureBuilder incorrectly minimizing same-type requirements. The test case works if I pass -Xfrontend -requirement-machine-protocol-signatures=on. I'll add it to our test suite.

@philipturner
Copy link
Contributor

I just got it to compile successfully when using your build flags. So I don't consider it a compiler crasher anymore. I actually just came across a 5th crasher when copying and pasting my existing "release toolchain workaround" and compiling it. I'm isolating that bug right now.

@philipturner
Copy link
Contributor

The errors you got were the same two ones I kept seeing in Xcode when I switched between dev 01/09 and 5.6 01/11.

@slavapestov
Copy link
Contributor Author

#41094

@philipturner
Copy link
Contributor

philipturner commented Jan 30, 2022

I added crash_5.swift to that repository. It seems to be a problem with AutoDiff, not RequirementMachine. I checked it against the existing compiler crashers and none matched.

Still, could you isolate it further and report it? I don't have enough experience to do so effectively myself.

Going back through the crash logs, the original crash has 54 lines, while the final one has 33. That means there's going to be a 6th crash I have to report soon. In the meantime, I still have one of the 54-line stack traces in the terminal:

1.	Swift version 5.7-dev (LLVM 59972a8d54ac3bf, Swift d6f6edf0712b0c1)
2.	Compiling with the current language version
3.	While evaluating request ASTLoweringRequest(Lowering AST to SIL for module file)
4.	While silgen emitFunction SIL function "@$s4file24DifferentiableCollectionPAAE17differentiableMapySayqd__Gqd__7ElementQzYjrXE16_Differentiation0B0Rd__AhiGRQlF".
 for 'differentiableMap(_:)' (at file.swift:50:3)
5.	While evaluating request AbstractGenericSignatureRequest(<τ_0_0, τ_0_1>, {}, {τ_0_0 : Differentiable, Self.Element : Differentiable})
6.	While evaluating request AbstractGenericSignatureRequest(<τ_0_0, τ_0_1>, {}, {τ_0_0 : Differentiable, τ_0_0.Element : Differentiable})
7.	While evaluating request AbstractGenericSignatureRequestRQM(<τ_0_0, τ_0_1>, {}, {τ_0_0 : Differentiable, τ_0_0.Element : Differentiable})
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           0x0000000105772314 llvm::sys::PrintStackTrace(llvm::raw_ostream&, int) + 56
1  swift-frontend           0x0000000105771444 llvm::sys::RunSignalHandlers() + 128
2  swift-frontend           0x0000000105772978 SignalHandler(int) + 304
3  libsystem_platform.dylib 0x00000001bb5304e4 _sigtramp + 56
4  libsystem_pthread.dylib  0x00000001bb518eb0 pthread_kill + 288
5  libsystem_c.dylib        0x00000001bb456314 abort + 164
6  swift-frontend           0x0000000105d28bb0 (anonymous namespace)::MinimalConformances::decomposeTermIntoConformanceRuleLeftHandSides(swift::rewriting::MutableTerm, llvm::SmallVectorImpl<unsigned int>&) const (.cold.12) + 0
7  swift-frontend           0x00000001020b1e4c llvm::MapVector<unsigned int, llvm::SmallVector<unsigned int, 2u>, llvm::DenseMap<unsigned int, unsigned int, llvm::DenseMapInfo<unsigned int>, llvm::detail::DenseMapPair<unsigned int, unsigned int> >, std::__1::vector<std::__1::pair<unsigned int, llvm::SmallVector<unsigned int, 2u> >, std::__1::allocator<std::__1::pair<unsigned int, llvm::SmallVector<unsigned int, 2u> > > > >::operator[](unsigned int const&) + 0
8  swift-frontend           0x00000001020afd38 swift::rewriting::RewriteSystem::computeMinimalConformances(llvm::DenseSet<unsigned int, llvm::DenseMapInfo<unsigned int> >&) + 892
9  swift-frontend           0x00000001020a75a4 swift::rewriting::RewriteSystem::minimizeRewriteSystem() + 348
10 swift-frontend           0x00000001020ca470 swift::rewriting::RequirementMachine::computeMinimalGenericSignatureRequirements() + 56
11 swift-frontend           0x00000001020ca828 swift::AbstractGenericSignatureRequestRQM::evaluate(swift::Evaluator&, swift::GenericSignatureImpl const*, llvm::SmallVector<swift::GenericTypeParamType*, 2u>, llvm::SmallVector<swift::Requirement, 2u>) const + 764
12 swift-frontend           0x0000000101b6804c llvm::PointerIntPair<swift::GenericSignature, 1u, unsigned int, llvm::PointerLikeTypeTraits<swift::GenericSignature>, llvm::PointerIntPairInfo<swift::GenericSignature, 1u, llvm::PointerLikeTypeTraits<swift::GenericSignature> > > swift::SimpleRequest<swift::AbstractGenericSignatureRequestRQM, llvm::PointerIntPair<swift::GenericSignature, 1u, unsigned int, llvm::PointerLikeTypeTraits<swift::GenericSignature>, llvm::PointerIntPairInfo<swift::GenericSignature, 1u, llvm::PointerLikeTypeTraits<swift::GenericSignature> > > (swift::GenericSignatureImpl const*, llvm::SmallVector<swift::GenericTypeParamType*, 2u>, llvm::SmallVector<swift::Requirement, 2u>), (swift::RequestFlags)2>::callDerived<0ul, 1ul, 2ul>(swift::Evaluator&, std::__1::integer_sequence<unsigned long, 0ul, 1ul, 2ul>) const + 148
13 swift-frontend           0x000000010200edcc llvm::Expected<swift::AbstractGenericSignatureRequestRQM::OutputType> swift::Evaluator::getResultUncached<swift::AbstractGenericSignatureRequestRQM>(swift::AbstractGenericSignatureRequestRQM const&) + 356
14 swift-frontend           0x000000010200eaac llvm::Expected<swift::AbstractGenericSignatureRequestRQM::OutputType> swift::Evaluator::getResultCached<swift::AbstractGenericSignatureRequestRQM, (void*)0>(swift::AbstractGenericSignatureRequestRQM const&) + 1316
15 swift-frontend           0x000000010200e3ec swift::AbstractGenericSignatureRequestRQM::OutputType swift::evaluateOrDefault<swift::AbstractGenericSignatureRequestRQM>(swift::Evaluator&, swift::AbstractGenericSignatureRequestRQM, swift::AbstractGenericSignatureRequestRQM::OutputType) + 28
16 swift-frontend           0x0000000102007700 swift::AbstractGenericSignatureRequest::evaluate(swift::Evaluator&, swift::GenericSignatureImpl const*, llvm::SmallVector<swift::GenericTypeParamType*, 2u>, llvm::SmallVector<swift::Requirement, 2u>) const + 2544
17 swift-frontend           0x0000000101b67f70 llvm::PointerIntPair<swift::GenericSignature, 1u, unsigned int, llvm::PointerLikeTypeTraits<swift::GenericSignature>, llvm::PointerIntPairInfo<swift::GenericSignature, 1u, llvm::PointerLikeTypeTraits<swift::GenericSignature> > > swift::SimpleRequest<swift::AbstractGenericSignatureRequest, llvm::PointerIntPair<swift::GenericSignature, 1u, unsigned int, llvm::PointerLikeTypeTraits<swift::GenericSignature>, llvm::PointerIntPairInfo<swift::GenericSignature, 1u, llvm::PointerLikeTypeTraits<swift::GenericSignature> > > (swift::GenericSignatureImpl const*, llvm::SmallVector<swift::GenericTypeParamType*, 2u>, llvm::SmallVector<swift::Requirement, 2u>), (swift::RequestFlags)2>::callDerived<0ul, 1ul, 2ul>(swift::Evaluator&, std::__1::integer_sequence<unsigned long, 0ul, 1ul, 2ul>) const + 148
18 swift-frontend           0x0000000101fe5350 llvm::Expected<swift::AbstractGenericSignatureRequest::OutputType> swift::Evaluator::getResultUncached<swift::AbstractGenericSignatureRequest>(swift::AbstractGenericSignatureRequest const&) + 252
19 swift-frontend           0x0000000101fe5098 llvm::Expected<swift::AbstractGenericSignatureRequest::OutputType> swift::Evaluator::getResultCached<swift::AbstractGenericSignatureRequest, (void*)0>(swift::AbstractGenericSignatureRequest const&) + 1316
20 swift-frontend           0x0000000101fe3cc4 swift::AbstractGenericSignatureRequest::OutputType swift::evaluateOrDefault<swift::AbstractGenericSignatureRequest>(swift::Evaluator&, swift::AbstractGenericSignatureRequest, swift::AbstractGenericSignatureRequest::OutputType) + 28
21 swift-frontend           0x0000000102007144 swift::AbstractGenericSignatureRequest::evaluate(swift::Evaluator&, swift::GenericSignatureImpl const*, llvm::SmallVector<swift::GenericTypeParamType*, 2u>, llvm::SmallVector<swift::Requirement, 2u>) const + 1076
22 swift-frontend           0x0000000101b67f70 llvm::PointerIntPair<swift::GenericSignature, 1u, unsigned int, llvm::PointerLikeTypeTraits<swift::GenericSignature>, llvm::PointerIntPairInfo<swift::GenericSignature, 1u, llvm::PointerLikeTypeTraits<swift::GenericSignature> > > swift::SimpleRequest<swift::AbstractGenericSignatureRequest, llvm::PointerIntPair<swift::GenericSignature, 1u, unsigned int, llvm::PointerLikeTypeTraits<swift::GenericSignature>, llvm::PointerIntPairInfo<swift::GenericSignature, 1u, llvm::PointerLikeTypeTraits<swift::GenericSignature> > > (swift::GenericSignatureImpl const*, llvm::SmallVector<swift::GenericTypeParamType*, 2u>, llvm::SmallVector<swift::Requirement, 2u>), (swift::RequestFlags)2>::callDerived<0ul, 1ul, 2ul>(swift::Evaluator&, std::__1::integer_sequence<unsigned long, 0ul, 1ul, 2ul>) const + 148
23 swift-frontend           0x0000000101fe5350 llvm::Expected<swift::AbstractGenericSignatureRequest::OutputType> swift::Evaluator::getResultUncached<swift::AbstractGenericSignatureRequest>(swift::AbstractGenericSignatureRequest const&) + 252
24 swift-frontend           0x0000000101fe5098 llvm::Expected<swift::AbstractGenericSignatureRequest::OutputType> swift::Evaluator::getResultCached<swift::AbstractGenericSignatureRequest, (void*)0>(swift::AbstractGenericSignatureRequest const&) + 1316
25 swift-frontend           0x0000000101fe3cc4 swift::AbstractGenericSignatureRequest::OutputType swift::evaluateOrDefault<swift::AbstractGenericSignatureRequest>(swift::Evaluator&, swift::AbstractGenericSignatureRequest, swift::AbstractGenericSignatureRequest::OutputType) + 28
26 swift-frontend           0x0000000101fe40cc swift::buildGenericSignature(swift::ASTContext&, swift::GenericSignature, llvm::SmallVector<swift::GenericTypeParamType*, 2u>, llvm::SmallVector<swift::Requirement, 2u>) + 136
27 swift-frontend           0x0000000100f48db0 buildDifferentiableGenericSignature(swift::CanGenericSignature, swift::CanType, swift::CanType) + 712
28 swift-frontend           0x0000000100f40250 swift::SILFunctionType::getAutoDiffDerivativeFunctionType(swift::IndexSubset*, swift::IndexSubset*, swift::AutoDiffDerivativeFunctionKind, swift::Lowering::TypeConverter&, llvm::function_ref<swift::ProtocolConformanceRef (swift::CanType, swift::Type, swift::ProtocolDecl*)>, swift::CanGenericSignature, bool, swift::CanType) + 2000
29 swift-frontend           0x0000000100fcfa5c (anonymous namespace)::TypeClassifierBase<(anonymous namespace)::LowerType, swift::Lowering::TypeLowering*>::getNormalDifferentiableSILFunctionTypeRecursiveProperties(swift::CanTypeWrapper<swift::SILFunctionType>, swift::Lowering::AbstractionPattern) + 184
30 swift-frontend           0x0000000100fc1d54 swift::CanTypeVisitor<(anonymous namespace)::LowerType, swift::Lowering::TypeLowering*, swift::Lowering::AbstractionPattern, swift::Lowering::IsTypeExpansionSensitive_t>::visit(swift::CanType, swift::Lowering::AbstractionPattern, swift::Lowering::IsTypeExpansionSensitive_t) + 1548
31 swift-frontend           0x0000000100fc27ec swift::Lowering::TypeConverter::getTypeLoweringForLoweredType(swift::Lowering::AbstractionPattern, swift::CanType, swift::TypeExpansionContext, swift::Lowering::IsTypeExpansionSensitive_t) + 632
32 swift-frontend           0x0000000100fc159c swift::Lowering::TypeConverter::getTypeLowering(swift::Lowering::AbstractionPattern, swift::Type, swift::TypeExpansionContext) + 692
33 swift-frontend           0x00000001013e9b64 swift::Lowering::TypeConverter::getLoweredType(swift::Type, swift::TypeExpansionContext) + 144
34 swift-frontend           0x000000010149218c (anonymous namespace)::EmitBBArguments::visitType(swift::CanType, swift::Lowering::AbstractionPattern, bool) + 84
35 swift-frontend           0x0000000101492d70 swift::CanTypeVisitor<(anonymous namespace)::EmitBBArguments, swift::Lowering::ManagedValue, swift::Lowering::AbstractionPattern>::visit(swift::CanType, swift::Lowering::AbstractionPattern) + 1628
36 swift-frontend           0x0000000101492118 (anonymous namespace)::ArgumentInitHelper::makeArgument(swift::Type, bool, bool, swift::SILBasicBlock*, swift::SILLocation) + 244
37 swift-frontend           0x0000000101491ac0 (anonymous namespace)::ArgumentInitHelper::emitParam(swift::ParamDecl*) + 648
38 swift-frontend           0x000000010149080c swift::Lowering::SILGenFunction::emitBasicProlog(swift::ParameterList*, swift::ParamDecl*, swift::Type, swift::DeclContext*, bool, swift::SourceLoc, llvm::Optional<swift::Lowering::AbstractionPattern>) + 368
39 swift-frontend           0x000000010148f878 swift::Lowering::SILGenFunction::emitProlog(swift::CaptureInfo, swift::ParameterList*, swift::ParamDecl*, swift::DeclContext*, swift::Type, bool, swift::SourceLoc, llvm::Optional<swift::Lowering::AbstractionPattern>) + 108
40 swift-frontend           0x00000001014550fc swift::Lowering::SILGenFunction::emitFunction(swift::FuncDecl*) + 244
41 swift-frontend           0x00000001013e4fbc swift::Lowering::SILGenModule::emitFunctionDefinition(swift::SILDeclRef, swift::SILFunction*) + 6396
42 swift-frontend           0x00000001013e6908 emitOrDelayFunction(swift::Lowering::SILGenModule&, swift::SILDeclRef, bool) + 384
43 swift-frontend           0x00000001013e36a8 swift::Lowering::SILGenModule::emitFunction(swift::FuncDecl*) + 140
44 swift-frontend           0x00000001014a3384 SILGenExtension::visitFuncDecl(swift::FuncDecl*) + 160
45 swift-frontend           0x000000010149fd14 SILGenExtension::emitExtension(swift::ExtensionDecl*) + 68
46 swift-frontend           0x000000010149fcc4 swift::Lowering::SILGenModule::visitExtensionDecl(swift::ExtensionDecl*) + 24
47 swift-frontend           0x00000001013e8ac8 swift::ASTLoweringRequest::evaluate(swift::Evaluator&, swift::ASTLoweringDescriptor) const + 1696
48 swift-frontend           0x0000000101493710 swift::SimpleRequest<swift::ASTLoweringRequest, std::__1::unique_ptr<swift::SILModule, std::__1::default_delete<swift::SILModule> > (swift::ASTLoweringDescriptor), (swift::RequestFlags)9>::evaluateRequest(swift::ASTLoweringRequest const&, swift::Evaluator&) + 140
49 swift-frontend           0x00000001013ec674 llvm::Expected<swift::ASTLoweringRequest::OutputType> swift::Evaluator::getResultUncached<swift::ASTLoweringRequest>(swift::ASTLoweringRequest const&) + 392
50 swift-frontend           0x00000001013e95d8 swift::performASTLowering(swift::ModuleDecl*, swift::Lowering::TypeConverter&, swift::SILOptions const&) + 128
51 swift-frontend           0x0000000100e8f7f4 swift::performCompileStepsPostSema(swift::CompilerInstance&, int&, swift::FrontendObserver*) + 988
52 swift-frontend           0x0000000100e912d4 swift::performFrontend(llvm::ArrayRef<char const*>, char const*, void*, swift::FrontendObserver*) + 3012
53 swift-frontend           0x0000000100dcb108 swift::mainEntry(int, char const**) + 484
54 dyld                     0x00000001179750f4 start + 520
zsh: abort      ../../build/Ninja-RelWithDebInfoAssert/swift-macosx-arm64/bin/swift -Xfronten

@slavapestov
Copy link
Contributor Author

slavapestov commented Jan 30, 2022

{τ_0_0 : Differentiable, τ_0_0.Element : Differentiable} is an invalid set of generic requirements. Differentiable does not have an associated type named Element. The bug is in the code making the call to buildGenericSignature() inside buildDifferentiableGenericSignature(). I think @rxwei is the appropriate person to look at this one.

@philipturner
Copy link
Contributor

CC: @BradLarson

@philipturner
Copy link
Contributor

philipturner commented Jan 30, 2022

@slavapestov I misunderstood which crash you were referring to (I thought it was 5). On line 28, there is a function that refers to AutoDiff. So both crashes 5 and 6 should be discussed by someone with expertise on AutoDiff. To anyone who wishes to address my concerns, please do look at my comments in #41094.

Both crashers call into swift::SILFunctionType::getAutoDiffDerivativeFunctionType at line 804 on "lib/SIL/IR/SILFunctionType.cpp".

For crash 6, I had conformed the DifferentiableCollection type to Sequence, where it would have an associated type named Element. Here is a quick link to the compiler crashers: compiler_crashers.

Update: I isolated crashes 5 and 6 further. They seem to be the same crash, just slightly modified to get different effects.

@philipturner
Copy link
Contributor

Is the following a valid set of generic requirements?

{τ_0_0 : Differentiable, τ_0_0.Iterator : IteratorProtocol, τ_0_0.Iterator : Differentiable}

@AnthonyLatsis
Copy link
Collaborator

AnthonyLatsis commented Jan 30, 2022

@philipturner No, Differentiable does not have an Iterator associated type, its sole associated type is TangentVector. You'd need an additional τ_0_0 : Sequence requirement or something for that to be valid.

@philipturner
Copy link
Contributor

So the following would be correct then?

{τ_0_0 : Differentiable, τ_0_0 : Sequence, τ_0_0.Iterator : IteratorProtocol, τ_0_0.Iterator : Differentiable}

The crash message always says this for crash 6:

Term does not conform to protocol: τ_0_0.[Sequence]

@AnthonyLatsis
Copy link
Collaborator

AnthonyLatsis commented Jan 30, 2022

It could mean that Iterator in τ_0_0.Iterator is indeed Sequence.Iterator, and someone forgot to add the τ_0_0 : Sequence requirement (if this is a manually built generic signature). Looks like that same bug in buildDifferentiableGenericSignature() that Slava mentioned earlier.

@slavapestov
Copy link
Contributor Author

Term does not conform to protocol: τ_0_0.[Sequence]

This should only happen if τ_0_0 : Sequence is missing. Are you sure it's getting added by AutoDiff?

@philipturner
Copy link
Contributor

@slavapestov I got something very similar to crash_4.swift, but with entirely different conditions. I don't know how you managed to narrow it down the first one. The stack traces are identical except for two function calls near the end. Please take a look at crash_7.swift.

It passes regardless of whether RequirementMachine is on. I got the same stack trace on both the January 9 developer nighty and Swift main. I also got the same crash with a different stack trace using the Xcode 5.5.2 toolchain and my Differentiation package. This all just adds to the confusion, because crash_4 was fixed by enabling RequirementMachine. Meanwhile, crash_7 was active when crash_4 didn't exist yet.

@AnthonyLatsis
Copy link
Collaborator

AnthonyLatsis commented Feb 1, 2022

This crash is unrelated to the requirement machine. It trips in AutoDiff code over an unconditional runtime cast on this line, because it expects AnyFunctionType but the interface type is of ErrorType.

@philipturner
Copy link
Contributor

Is it possible to just solve by checking if that's an ErrorType beforehand? If so, I'd like to solve it myself so that it's my first experience solving a bug in the C++ code.

@AnthonyLatsis
Copy link
Collaborator

Sure, go ahead!

Is it possible to just solve by checking if that's an ErrorType beforehand

We may want to use either Decl::isInvalid or TypeBase::hasError instead to rule out types containing ErrorType as well.

@philipturner
Copy link
Contributor

#41128

@philipturner
Copy link
Contributor

philipturner commented Feb 4, 2022

@slavapestov crash_6 in my repository seems to be wierdly related to RequirementMachine. It now runs successfully when you pass in no flags, but crashes when you pass in:

-Xfrontend -requirement-machine-protocol-signatures=on -Xfrontend -requirement-machine-inferred-signatures=on -Xfrontend -requirement-machine-abstract-signatures=on

This contrasts with crashes 1 and 4, which crash normally but run fine with the flags above. This crashing is one of the multiple bugs blocking my collection differentiation PR. I caught it while testing whether #41174 (which fixed crash_5) fixed crash_6. Crashes 5 and 6 were closely related, so I thought they had the same source.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants