Skip to content

[CS] Adjust applied overload simplification #30716

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

Conversation

hamishknight
Copy link
Contributor

Currently simplifyAppliedOverloads depends on the order in which constraints are simplified, specifically that a lookup constraint for a function gets simplified before the applicable function constraint. This happens to work out just fine today with the order in which we re-activate constraints, but I'm planning on changing that order.

This PR changes the logic such that it's no longer affected by the order in which constraints are simplified. We'll now run it when either an applicable function constraint is added, or a new bind overload disjunction is added. This also means we no longer need to run it potentially multiple times when simplifying an applicable fn.

Currently `simplifyAppliedOverloads` depends on
the order in which constraints are simplified,
specifically that a lookup constraint for a
function gets simplified before the applicable
function constraint. This happens to work out
just fine today with the order in which we
re-activate constraints, but I'm planning on
changing that order.

This commit changes the logic such that it it's no
longer affected by the order in which constraints
are simplified. We'll now run it when either an
applicable function constraint is added, or a new
bind overload disjunction is added. This also
means we no longer need to run it potentially
multiple times when simplifying the applicable fn.
@hamishknight
Copy link
Contributor Author

@swift-ci please test

@hamishknight
Copy link
Contributor Author

@swift-ci please test compiler performance

@hamishknight hamishknight requested a review from xedin March 30, 2020 15:17
@swift-ci
Copy link
Contributor

Summary for master full

Unexpected test results, excluded stats for RxCocoa, Base64CoderSwiftUI, SwifterSwift

Regressions found (see below)

Debug-batch

debug-batch brief

Regressed (0)
name old new delta delta_pct
Improved (0)
name old new delta delta_pct
Unchanged (delta < 1.0% or delta < 100.0ms) (3)
name old new delta delta_pct
Frontend.NumInstructionsExecuted 56,274,042,594,982 56,124,652,123,075 -149,390,471,907 -0.27%
LLVM.NumLLVMBytesOutput 1,869,665,058 1,869,665,700 642 0.0%
time.swift-driver.wall 3966.6s 3935.0s -31.6s -0.8%

debug-batch detailed

Regressed (2)
name old new delta delta_pct
Driver.NumDriverPipePolls 105,739 108,677 2,938 2.78% ⛔
Driver.NumDriverPipeReads 96,592 99,521 2,929 3.03% ⛔
Improved (1)
name old new delta delta_pct
Sema.IsCallableNominalTypeRequest 2,169 2,137 -32 -1.48% ✅
Unchanged (delta < 1.0% or delta < 100.0ms) (229)
name old new delta delta_pct
AST.ImportSetCacheHit 1,945,393 1,945,665 272 0.01%
AST.ImportSetCacheMiss 605,938 605,951 13 0.0%
AST.ImportSetFoldHit 289,352 289,367 15 0.01%
AST.ImportSetFoldMiss 316,585 316,584 -1 -0.0%
AST.ModuleShadowCacheHit 3,597 3,597 0 0.0%
AST.ModuleShadowCacheMiss 1,866 1,866 0 0.0%
AST.ModuleVisibilityCacheHit 30,263 30,263 0 0.0%
AST.ModuleVisibilityCacheMiss 6,961 6,961 0 0.0%
AST.NumASTBytesAllocated 52,785,474,967 52,727,577,192 -57,897,775 -0.11%
AST.NumASTScopeLookups 3,620,920 3,621,082 162 0.0%
AST.NumBraceStmtASTScopeExpansions 638,329 638,329 0 0.0%
AST.NumBraceStmtASTScopes 638,329 638,329 0 0.0%
AST.NumDecls 144,947 144,947 0 0.0%
AST.NumDependencies 302,585 302,599 14 0.0%
AST.NumIterableTypeBodyASTScopeExpansions 238,553 238,566 13 0.01%
AST.NumIterableTypeBodyASTScopes 287,274 287,353 79 0.03%
AST.NumLinkLibraries 0 0 0 0.0%
AST.NumLoadedModules 283,910 283,910 0 0.0%
AST.NumLocalTypeDecls 255 255 0 0.0%
AST.NumModuleLookupClassMember 6,993 6,993 0 0.0%
AST.NumModuleLookupValue 48,084,521 48,089,728 5,207 0.01%
AST.NumObjCMethods 25,242 25,242 0 0.0%
AST.NumOperators 589 589 0 0.0%
AST.NumPrecedenceGroups 93 93 0 0.0%
AST.NumReferencedDynamicNames 191 191 0 0.0%
AST.NumReferencedMemberNames 6,678,567 6,678,345 -222 -0.0%
AST.NumReferencedTopLevelNames 571,937 571,937 0 0.0%
AST.NumSourceBuffers 323,909 323,909 0 0.0%
AST.NumSourceLines 4,828,874 4,828,874 0 0.0%
AST.NumSourceLinesPerSecond 3,376,286 3,383,528 7,242 0.21%
AST.NumTotalClangImportedEntities 1,821,009 1,821,233 224 0.01%
Driver.ChildrenMaxRSS 261,304,342,528 261,078,384,640 -225,957,888 -0.09%
Driver.DriverDepCascadingDynamic 0 0 0 0.0%
Driver.DriverDepCascadingExternal 0 0 0 0.0%
Driver.DriverDepCascadingMember 0 0 0 0.0%
Driver.DriverDepCascadingNominal 0 0 0 0.0%
Driver.DriverDepCascadingTopLevel 0 0 0 0.0%
Driver.DriverDepDynamic 0 0 0 0.0%
Driver.DriverDepExternal 0 0 0 0.0%
Driver.DriverDepMember 0 0 0 0.0%
Driver.DriverDepNominal 0 0 0 0.0%
Driver.DriverDepTopLevel 0 0 0 0.0%
Driver.NumDriverJobsRun 28,809 28,809 0 0.0%
Driver.NumDriverJobsSkipped 0 0 0 0.0%
Driver.NumProcessFailures 0 0 0 0.0%
Frontend.MaxMallocUsage 1,125,939,673,392 1,124,142,139,056 -1,797,534,336 -0.16%
Frontend.NumInstructionsExecuted 56,274,042,594,982 56,124,652,123,075 -149,390,471,907 -0.27%
Frontend.NumProcessFailures 0 0 0 0.0%
IRGen.IRGenSourceFileRequest 27,506 27,506 0 0.0%
IRGen.IRGenWholeModuleRequest 20 20 0 0.0%
IRModule.NumGOTEntries 249,106 249,106 0 0.0%
IRModule.NumIRAliases 201,631 201,631 0 0.0%
IRModule.NumIRBasicBlocks 8,194,698 8,194,702 4 0.0%
IRModule.NumIRComdatSymbols 0 0 0 0.0%
IRModule.NumIRFunctions 3,425,656 3,425,660 4 0.0%
IRModule.NumIRGlobals 3,832,852 3,832,852 0 0.0%
IRModule.NumIRIFuncs 0 0 0 0.0%
IRModule.NumIRInsts 94,144,928 94,144,976 48 0.0%
IRModule.NumIRNamedMetaData 139,130 139,130 0 0.0%
IRModule.NumIRValueSymbols 6,585,362 6,585,366 4 0.0%
LLVM.NumLLVMBytesOutput 1,869,665,058 1,869,665,700 642 0.0%
Parse.CodeCompletionSecondPassRequest 0 0 0 0.0%
Parse.NumFunctionsParsed 275,938 275,938 0 0.0%
Parse.NumIterableDeclContextParsed 618,413 618,414 1 0.0%
Parse.ParseAbstractFunctionBodyRequest 251,941 251,941 0 0.0%
Parse.ParseMembersRequest 486,946 486,947 1 0.0%
Parse.ParseSourceFileRequest 314,763 314,763 0 0.0%
SILGen.SILGenSourceFileRequest 27,506 27,506 0 0.0%
SILGen.SILGenWholeModuleRequest 1,292 1,292 0 0.0%
SILModule.NumSILGenDefaultWitnessTables 0 0 0 0.0%
SILModule.NumSILGenFunctions 1,755,745 1,755,747 2 0.0%
SILModule.NumSILGenGlobalVariables 54,955 54,955 0 0.0%
SILModule.NumSILGenVtables 19,063 19,063 0 0.0%
SILModule.NumSILGenWitnessTables 75,017 75,017 0 0.0%
SILModule.NumSILOptDefaultWitnessTables 0 0 0 0.0%
SILModule.NumSILOptFunctions 2,589,081 2,589,083 2 0.0%
SILModule.NumSILOptGlobalVariables 56,642 56,642 0 0.0%
SILModule.NumSILOptVtables 32,288 32,288 0 0.0%
SILModule.NumSILOptWitnessTables 173,885 173,885 0 0.0%
SILOptimizer.ExecuteSILPipelineRequest 111,381 111,381 0 0.0%
Sema.AbstractGenericSignatureRequest 32,950 32,950 0 0.0%
Sema.AccessLevelRequest 11,953,690 11,939,757 -13,933 -0.12%
Sema.AnyObjectLookupRequest 283 283 0 0.0%
Sema.AreAllStoredPropertiesDefaultInitableRequest 21,501 21,501 0 0.0%
Sema.AttachedFunctionBuilderRequest 1 1 0 0.0%
Sema.AttachedPropertyWrapperTypeRequest 575,181 575,181 0 0.0%
Sema.AttachedPropertyWrappersRequest 2,388,578 2,388,577 -1 -0.0%
Sema.CallerSideDefaultArgExprRequest 79,248 79,248 0 0.0%
Sema.CheckRedeclarationRequest 1,005,852 1,005,852 0 0.0%
Sema.ClassAncestryFlagsRequest 101,289 101,289 0 0.0%
Sema.ClosureHasExplicitResultRequest 92,741 92,741 0 0.0%
Sema.CollectOverriddenDeclsRequest 7,300,625 7,294,921 -5,704 -0.08%
Sema.CompareDeclSpecializationRequest 476,497 477,217 720 0.15%
Sema.CursorInfoRequest 0 0 0 0.0%
Sema.CustomAttrNominalRequest 1 1 0 0.0%
Sema.DefaultAndMaxAccessLevelRequest 55,178 55,178 0 0.0%
Sema.DefaultArgumentExprRequest 39,602 39,602 0 0.0%
Sema.DefaultArgumentInitContextRequest 461 461 0 0.0%
Sema.DefaultDefinitionTypeRequest 8,145 8,145 0 0.0%
Sema.DefaultTypeRequest 491,512 491,590 78 0.02%
Sema.DifferentiableAttributeTypeCheckRequest 0 0 0 0.0%
Sema.DirectLookupRequest 33,770,482 33,765,848 -4,634 -0.01%
Sema.DirectOperatorLookupRequest 491,844 491,903 59 0.01%
Sema.DirectPrecedenceGroupLookupRequest 145,161 145,161 0 0.0%
Sema.DynamicallyReplacedDeclRequest 1,035,052 1,035,052 0 0.0%
Sema.EmittedMembersRequest 26,601 26,601 0 0.0%
Sema.EnumRawTypeRequest 22,622 22,622 0 0.0%
Sema.EnumRawValuesRequest 10,786 10,786 0 0.0%
Sema.ExistentialConformsToSelfRequest 11,308 11,333 25 0.22%
Sema.ExistentialTypeSupportedRequest 16,396 16,396 0 0.0%
Sema.ExpandASTScopeRequest 6,670,040 6,670,376 336 0.01%
Sema.ExtendedNominalRequest 495,921 495,921 0 0.0%
Sema.ExtendedTypeRequest 78,031 78,041 10 0.01%
Sema.FunctionBuilderTypeRequest 1 1 0 0.0%
Sema.FunctionOperatorRequest 51,372 51,372 0 0.0%
Sema.GenericParamListRequest 8,980,368 8,971,856 -8,512 -0.09%
Sema.GenericSignatureRequest 2,352,093 2,352,213 120 0.01%
Sema.GetDestructorRequest 27,246 27,246 0 0.0%
Sema.HasCircularInheritanceRequest 23,001 23,001 0 0.0%
Sema.HasCircularInheritedProtocolsRequest 10,372 10,372 0 0.0%
Sema.HasCircularRawValueRequest 9,196 9,196 0 0.0%
Sema.HasDefaultInitRequest 52,918 52,918 0 0.0%
Sema.HasDynamicCallableAttributeRequest 0 0 0 0.0%
Sema.HasDynamicMemberLookupAttributeRequest 576,467 576,369 -98 -0.02%
Sema.HasMemberwiseInitRequest 19,769 19,769 0 0.0%
Sema.HasMissingDesignatedInitializersRequest 25,279 25,279 0 0.0%
Sema.HasUserDefinedDesignatedInitRequest 52,928 52,928 0 0.0%
Sema.InferredGenericSignatureRequest 164,334 164,347 13 0.01%
Sema.InheritedDeclsReferencedRequest 5,169,486 5,168,909 -577 -0.01%
Sema.InheritedProtocolsRequest 516,359 516,395 36 0.01%
Sema.InheritedTypeRequest 284,480 284,633 153 0.05%
Sema.InheritsSuperclassInitializersRequest 26,246 26,246 0 0.0%
Sema.InitKindRequest 93,774 93,774 0 0.0%
Sema.InterfaceTypeRequest 13,274,572 13,266,389 -8,183 -0.06%
Sema.IsABICompatibleOverrideRequest 138,484 138,484 0 0.0%
Sema.IsAccessorTransparentRequest 315,634 315,634 0 0.0%
Sema.IsDeclApplicableRequest 0 0 0 0.0%
Sema.IsDynamicRequest 1,727,754 1,727,754 0 0.0%
Sema.IsFinalRequest 2,603,780 2,604,109 329 0.01%
Sema.IsGetterMutatingRequest 428,515 428,515 0 0.0%
Sema.IsImplicitlyUnwrappedOptionalRequest 2,382,197 2,383,540 1,343 0.06%
Sema.IsObjCRequest 1,546,893 1,546,629 -264 -0.02%
Sema.IsSetterMutatingRequest 346,657 346,657 0 0.0%
Sema.IsStaticRequest 886,593 886,672 79 0.01%
Sema.LazyStoragePropertyRequest 2,492 2,492 0 0.0%
Sema.LookupAllConformancesInContextRequest 84,558 84,558 0 0.0%
Sema.LookupConformanceInModuleRequest 37,315,564 37,207,702 -107,862 -0.29%
Sema.LookupInModuleRequest 6,187,457 6,191,893 4,436 0.07%
Sema.LookupInfixOperatorRequest 82,854 82,854 0 0.0%
Sema.LookupPostfixOperatorRequest 184 184 0 0.0%
Sema.LookupPrecedenceGroupRequest 32,174 32,174 0 0.0%
Sema.LookupPrefixOperatorRequest 563 563 0 0.0%
Sema.MangleLocalTypeDeclRequest 510 510 0 0.0%
Sema.ModuleQualifiedLookupRequest 2,822,202 2,826,515 4,313 0.15%
Sema.NamedLazyMemberLoadSuccessCount 20,222,888 20,203,229 -19,659 -0.1%
Sema.NamingPatternRequest 203,735 203,734 -1 -0.0%
Sema.NeedsNewVTableEntryRequest 679,577 679,577 0 0.0%
Sema.NumAccessorBodiesSynthesized 193,430 193,430 0 0.0%
Sema.NumAccessorsSynthesized 285,614 285,614 0 0.0%
Sema.NumConformancesDeserialized 8,831,132 8,825,770 -5,362 -0.06%
Sema.NumConstraintScopes 32,660,460 32,667,504 7,044 0.02%
Sema.NumConstraintsConsideredForEdgeContraction 114,358,046 113,822,904 -535,142 -0.47%
Sema.NumCrossImportsChecked 0 0 0 0.0%
Sema.NumCrossImportsFound 0 0 0 0.0%
Sema.NumCyclicOneWayComponentsCollapsed 0 0 0 0.0%
Sema.NumDeclsDeserialized 68,476,338 68,429,925 -46,413 -0.07%
Sema.NumDeclsTypechecked 1,450,456 1,450,456 0 0.0%
Sema.NumGenericSignatureBuilders 1,252,894 1,253,020 126 0.01%
Sema.NumLazyIterableDeclContexts 7,996,308 7,993,087 -3,221 -0.04%
Sema.NumLazyRequirementSignatures 778,153 777,795 -358 -0.05%
Sema.NumLazyRequirementSignaturesLoaded 577,465 577,201 -264 -0.05%
Sema.NumLeafScopes 20,634,907 20,655,720 20,813 0.1%
Sema.NumTypesDeserialized 21,481,894 21,480,053 -1,841 -0.01%
Sema.NumTypesValidated 1,426,492 1,426,520 28 0.0%
Sema.NumUnloadedLazyIterableDeclContexts 4,995,773 4,995,530 -243 -0.0%
Sema.OpaqueReadOwnershipRequest 270,338 270,338 0 0.0%
Sema.OpaqueResultTypeRequest 0 0 0 0.0%
Sema.OperatorPrecedenceGroupRequest 749 749 0 0.0%
Sema.OverriddenDeclsRequest 2,433,392 2,433,733 341 0.01%
Sema.ParamSpecifierRequest 1,218,738 1,218,747 9 0.0%
Sema.PatternBindingEntryRequest 465,989 465,988 -1 -0.0%
Sema.PatternTypeRequest 540,517 540,516 -1 -0.0%
Sema.PreCheckFunctionBuilderRequest 0 0 0 0.0%
Sema.PropertyWrapperBackingPropertyInfoRequest 570,483 570,483 0 0.0%
Sema.PropertyWrapperBackingPropertyTypeRequest 575,181 575,181 0 0.0%
Sema.PropertyWrapperMutabilityRequest 629,128 629,128 0 0.0%
Sema.PropertyWrapperTypeInfoRequest 7 7 0 0.0%
Sema.ProtocolRequiresClassRequest 35,464 35,548 84 0.24%
Sema.ProvideDefaultImplForRequest 7,300,625 7,294,921 -5,704 -0.08%
Sema.QualifiedLookupRequest 5,487,953 5,487,432 -521 -0.01%
Sema.RangeInfoRequest 0 0 0 0.0%
Sema.RequirementRequest 113,224 113,235 11 0.01%
Sema.RequirementSignatureRequest 628,372 628,249 -123 -0.02%
Sema.RequiresOpaqueAccessorsRequest 1,333,710 1,333,710 0 0.0%
Sema.RequiresOpaqueModifyCoroutineRequest 259,760 259,760 0 0.0%
Sema.ResilienceExpansionRequest 1,754,551 1,754,593 42 0.0%
Sema.ResolveEffectiveMemberwiseInitRequest 0 0 0 0.0%
Sema.ResolveImplicitMemberRequest 381,766 381,570 -196 -0.05%
Sema.ResolveProtocolNameRequest 0 0 0 0.0%
Sema.ResultTypeRequest 647,279 647,279 0 0.0%
Sema.RootAndResultTypeOfKeypathDynamicMemberRequest 0 0 0 0.0%
Sema.RootTypeOfKeypathDynamicMemberRequest 0 0 0 0.0%
Sema.SPIGroupsRequest 13,592,214 13,582,824 -9,390 -0.07%
Sema.ScopedImportLookupRequest 1,076 1,076 0 0.0%
Sema.SelfAccessKindRequest 609,572 609,640 68 0.01%
Sema.SelfBoundsFromWhereClauseRequest 1,570,383 1,572,321 1,938 0.12%
Sema.SetterAccessLevelRequest 145,926 145,926 0 0.0%
Sema.StorageImplInfoRequest 1,449,785 1,449,785 0 0.0%
Sema.StoredPropertiesAndMissingMembersRequest 31,852 31,852 0 0.0%
Sema.StoredPropertiesRequest 320,957 320,957 0 0.0%
Sema.StructuralTypeRequest 2,037 2,037 0 0.0%
Sema.SuperclassDeclRequest 406,701 406,709 8 0.0%
Sema.SuperclassTypeRequest 54,086 54,099 13 0.02%
Sema.SynthesizeAccessorRequest 285,614 285,614 0 0.0%
Sema.SynthesizeDefaultInitRequest 4,856 4,856 0 0.0%
Sema.SynthesizeMemberwiseInitRequest 3,164 3,164 0 0.0%
Sema.TypeCheckFunctionBodyUntilRequest 543,354 543,354 0 0.0%
Sema.TypeCheckSourceFileRequest 27,826 27,826 0 0.0%
Sema.TypeDeclsFromWhereClauseRequest 29,641 29,641 0 0.0%
Sema.TypeEraserHasViableInitRequest 0 0 0 0.0%
Sema.TypeRelationCheckRequest 0 0 0 0.0%
Sema.TypeWitnessRequest 9,067 9,087 20 0.22%
Sema.USRGenerationRequest 8,672,285 8,666,791 -5,494 -0.06%
Sema.UnderlyingTypeDeclsReferencedRequest 257,969 258,012 43 0.02%
Sema.UnderlyingTypeRequest 34,611 34,631 20 0.06%
Sema.UnqualifiedLookupRequest 3,769,653 3,769,815 162 0.0%
Sema.ValidatePrecedenceGroupRequest 182,645 182,645 0 0.0%
Sema.ValueWitnessRequest 65,166 65,166 0 0.0%
TBDGen.GenerateTBDRequest 27,526 27,526 0 0.0%

Release

release brief

Regressed (0)
name old new delta delta_pct
Improved (0)
name old new delta delta_pct
Unchanged (delta < 1.0% or delta < 100.0ms) (3)
name old new delta delta_pct
Frontend.NumInstructionsExecuted 55,214,102,602,612 55,159,617,724,991 -54,484,877,621 -0.1%
LLVM.NumLLVMBytesOutput 1,856,153,968 1,856,151,472 -2,496 -0.0%
time.swift-driver.wall 6294.5s 6292.2s -2.3s -0.04%

release detailed

Regressed (0)
name old new delta delta_pct
Improved (0)
name old new delta delta_pct
Unchanged (delta < 1.0% or delta < 100.0ms) (17)
name old new delta delta_pct
AST.NumLoadedModules 31,800 31,800 0 0.0%
AST.NumTotalClangImportedEntities 466,355 466,355 0 0.0%
IRModule.NumIRBasicBlocks 4,916,857 4,916,857 0 0.0%
IRModule.NumIRFunctions 2,826,914 2,826,914 0 0.0%
IRModule.NumIRGlobals 3,324,399 3,324,399 0 0.0%
IRModule.NumIRInsts 49,764,872 49,764,872 0 0.0%
IRModule.NumIRValueSymbols 5,783,975 5,783,975 0 0.0%
LLVM.NumLLVMBytesOutput 1,856,153,968 1,856,151,472 -2,496 -0.0%
SILModule.NumSILGenFunctions 1,228,854 1,228,856 2 0.0%
SILModule.NumSILOptFunctions 1,034,579 1,034,579 0 0.0%
Sema.NumConformancesDeserialized 3,554,237 3,554,257 20 0.0%
Sema.NumConstraintScopes 32,359,783 32,366,795 7,012 0.02%
Sema.NumDeclsDeserialized 11,082,563 11,083,367 804 0.01%
Sema.NumGenericSignatureBuilders 277,319 277,345 26 0.01%
Sema.NumLazyIterableDeclContexts 1,375,671 1,375,728 57 0.0%
Sema.NumTypesDeserialized 5,384,354 5,385,168 814 0.02%
Sema.NumTypesValidated 847,880 847,880 0 0.0%

Copy link
Contributor

@xedin xedin left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My biggest concern is that we are getting even more expensive calls to gatherConstraints involved in simplication. Maybe a better approach would be to remove disjunction constraint and associate all of the choices direct with corresponding type variable as a set of possible types. It might be a bigger refactoring but it should allow us to eliminate all prominent uses of gatherConstraints which should speed up solving.

addDisjunctionConstraint(choices, locator, ForgetChoice);
auto *disjunction =
Constraint::createDisjunction(*this, choices, locator, ForgetChoice);
addUnsolvedConstraint(disjunction);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems weird because we add disjunction just to retire it right away, shouldn't it be check and add instead?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@xedin Unfortunately filterDisjunction currently requires it to be in the inactive list.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh okay then...

@hamishknight
Copy link
Contributor Author

Maybe a better approach would be to remove disjunction constraint and associate all of the choices direct with corresponding type variable as a set of possible types.

@xedin Could you elaborate on what you mean by this?

I definitely agree that it would be nice to remove a lot of the calls to gatherConstraints! Would you be okay with me landing this as a temporary measure to get #30487 landed in time for 5.3 though? (which does remove a particularly expensive call to gatherConstraints)

I'd be more than happy to work on a follow-up to help remove more calls to gatherConstraints.

@xedin
Copy link
Contributor

xedin commented Mar 30, 2020

I think re-activating all of the constraints is a bit backwards if we are trying to avoid traversing constraint graph to find constraints related to newly bound type variable (I haven't actually looked at the PR so I'm just assuming that's what it does). Is it possible to optimize constraint graph representation so equivalence classes are not as expensive to traverse?

Could you elaborate on what you mean by this?

I think we should move towards a solver where bindings are computed incrementally as new constraints are being added/removed and each type variable (or constraint system) keeps track of its current "possible types" set. For disjunctions we already allocate a new type variable which then gets bound to a particular choice, if we where to keep disjunction choices are "possible types" that would unify TypeVariableStep and DisjunctionStep and avoid gathering constraints in certain cases like simplifyAppliedOverloads and other optimizations which currently look for disjunctions.

@hamishknight
Copy link
Contributor Author

I think re-activating all of the constraints is a bit backwards if we are trying to avoid traversing constraint graph to find constraints related to newly bound type variable (I haven't actually looked at the PR so I'm just assuming that's what it does).

@xedin The idea behind re-activating all constraints was that if we were to fix gatherConstraints to visit all fixed bindings and adjacencies for a type variable in AllMentions mode (such as in #26691), then it might be worth doing away with the graph traversal altogether and just re-activating everything in the component. The benchmark results were actually not too bad :)

Though having thought about it some more, I'm actually not sure why we need to visit constraints along a type variable's adjacencies, do you recall why we do this? The test suite appears to more or less pass if we just visit fixed bindings (which I believe should be quite a bit cheaper to traverse).

Just to be clear, my primary motive here is correctness, we currently miscompile programs like this:

func foo(_ x: Int) {
  (x ?? 0) as String
}

Because we don't transitively visit fixed bindings when we bind a type variable, and then drop the coercion constraint. If possible, I'd really like to get a fix for this into 5.3 :)

Is it possible to optimize constraint graph representation so equivalence classes are not as expensive to traverse?

Hmm, I suppose we could try unifying the constraints and fixed bindings we need to visit on the representative, though I'm not sure how much of a win that would be as it would mean additional work when adding/removing constraints and rolling back a scope.

I think we should move towards a solver where bindings are computed incrementally as new constraints are being added/removed and each type variable (or constraint system) keeps track of its current "possible types" set. For disjunctions we already allocate a new type variable which then gets bound to a particular choice, if we where to keep disjunction choices are "possible types" that would unify TypeVariableStep and DisjunctionStep and avoid gathering constraints in certain cases like simplifyAppliedOverloads and other optimizations which currently look for disjunctions.

That would be great! Though for simplifyAppliedOverloads wouldn't we still have to use gatherConstraints to look through intermediate optional object constraints?

@xedin
Copy link
Contributor

xedin commented Apr 6, 2020

func foo(_ x: Int) {
  (x ?? 0) as String
}

This example looks like a problem with binding inference, of overload of ?? which accepts both arguments as optional we'd try to binding argument to parameter directly instead of preserving optional. This is actually a consequence of https://github.com/apple/swift/blob/master/lib/Sema/CSBindings.cpp#L780 which doesn't seem to be correct.

I think the intention of that code was to avoid inferring incorrect bindings but unwrapping an optional there leads to us trying to binding two type variables together, which should actually be avoided.

I think it should be possible to extend this logic to discard bindings or at least mark them as FullyBound in cases where type doesn't conform to a literal protocol.

WDYT?

@hamishknight
Copy link
Contributor Author

@swift-ci please test

@hamishknight
Copy link
Contributor Author

@swift-ci please test Windows

@hamishknight
Copy link
Contributor Author

Talked with Pavel offline, we're going to merge this so the fix for gatherConstraints can be landed, but we should re-visit it when we refactor how we deal with bindings.

@hamishknight hamishknight merged commit 76881a3 into swiftlang:master Apr 8, 2020
@hamishknight hamishknight deleted the candidate-application-process branch April 8, 2020 02:04
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.

3 participants