Skip to content

Reimplement function builders as statement transformations. #29133

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 12 commits into from
Jan 17, 2020

Conversation

DougGregor
Copy link
Member

@DougGregor DougGregor commented Jan 10, 2020

Refactor the implementation of function builders so they maintain the statement structure of the closure/function body to which they are applied. This replaces the previous "fold everything into a single expression" implementation with one that is better in several regards:

  • It matches better with the evolving function builder proposal/pitch document, which describes the transformation as introducing intermediate local variables to capture the results of each buildBlock/buildExpression/buildIf/etc. invocation. The result is easier to reason about.
  • It allows generalization to statement kinds that cannot be expressed in a single expression, e.g., if let, although each of these will require work.
  • It charts a path toward a more usable model for type checking multi-statement closures

This refactoring is a major step toward generalizing function builders (rdar://problem/50426203).

@DougGregor
Copy link
Member Author

@swift-ci please smoke test

@DougGregor
Copy link
Member Author

@swift-ci please test source compatibility

@DougGregor
Copy link
Member Author

This is going to conflict with #28837, which should be merged first and I'll rebase/cleanup once that happens. This also needs some serious stress-testing on SwiftUI apps, so we can get some more complicated function builder uses.

@DougGregor DougGregor changed the title [WIP] Reimplement function builders as statement transformations. Reimplement function builders as statement transformations. Jan 15, 2020
@DougGregor
Copy link
Member Author

@swift-ci please smoke test

1 similar comment
@DougGregor
Copy link
Member Author

@swift-ci please smoke test

@DougGregor DougGregor requested review from xedin and hborla January 16, 2020 17:10
@DougGregor
Copy link
Member Author

@swift-ci please smoke test

@DougGregor
Copy link
Member Author

@swift-ci please test source compatibility

A couple of trivial fixes as part of the builder transform refactoring:
* When recording captured expressions, record the source entity appropriately
* Make sure to check (and skip) #if declarations in application
* Check and diagnostic #warning/#error as part of application (not generation)
* Simplify the body result type for return coercion during application
* When checking for applicability of a function builder, don't do any
  constraint generation.
When generating constraints in the context of the "old" diagnostic
path that type-checks subexpressions independently, we might not have
types for closure parameters. Cope with the case with a narrower form
of the original hack.
If we encountered an error, just skip it; there's nothing more to do.
… applied.

We used to get this contextualization "for free" because closures that
had function builders applied to them would get translated into
single-expression closures. Now, we need to check for this explicitly.
The right solution is to extend the notion of the "anchor" of a locator
to also cover statements (and TypeReprs, and Patterns, and more), so
this is a stop-gap.
@DougGregor DougGregor force-pushed the generlize-function-builders branch from 05dda38 to 0a7f040 Compare January 16, 2020 21:44
@DougGregor
Copy link
Member Author

@swift-ci please smoke test

@DougGregor
Copy link
Member Author

@swift-ci please test source compatibility

@DougGregor
Copy link
Member Author

@swift-ci please smoke test compiler performance

@swift-ci
Copy link
Contributor

Summary for master smoketest

Regressions found (see below)

Debug

debug brief

Regressed (2)
name old new delta delta_pct
Frontend.NumInstructionsExecuted 932,900,264,015 978,873,946,897 45,973,682,882 4.93% ⛔
time.swift-driver.wall 74.6s 79.0s 4.5s 6.02% ⛔
Improved (0)
name old new delta delta_pct
Unchanged (delta < 1.0% or delta < 100.0ms) (1)
name old new delta delta_pct
LLVM.NumLLVMBytesOutput 50,728,544 50,728,632 88 0.0%

debug detailed

Regressed (6)
name old new delta delta_pct
AST.NumTotalClangImportedEntities 160,507 164,053 3,546 2.21% ⛔
Sema.NumConformancesDeserialized 187,265 203,025 15,760 8.42% ⛔
Sema.NumDeclsDeserialized 1,493,265 1,615,943 122,678 8.22% ⛔
Sema.NumGenericSignatureBuilders 27,114 27,882 768 2.83% ⛔
Sema.NumLazyIterableDeclContexts 188,443 193,682 5,239 2.78% ⛔
Sema.NumTypesDeserialized 481,048 500,547 19,499 4.05% ⛔
Improved (0)
name old new delta delta_pct
Unchanged (delta < 1.0% or delta < 100.0ms) (12)
name old new delta delta_pct
AST.NumLoadedModules 6,089 6,089 0 0.0%
IRModule.NumIRBasicBlocks 189,031 189,031 0 0.0%
IRModule.NumIRFunctions 101,626 101,626 0 0.0%
IRModule.NumIRGlobals 110,101 110,101 0 0.0%
IRModule.NumIRInsts 2,079,387 2,079,387 0 0.0%
IRModule.NumIRValueSymbols 193,083 193,083 0 0.0%
LLVM.NumLLVMBytesOutput 50,728,544 50,728,632 88 0.0%
SILModule.NumSILGenFunctions 49,397 49,397 0 0.0%
SILModule.NumSILOptFunctions 67,748 67,748 0 0.0%
Sema.NumConstraintScopes 981,827 983,753 1,926 0.2%
Sema.NumFunctionsTypechecked 10,984 10,984 0 0.0%
Sema.NumTypesValidated 31,169 31,169 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 1,318,612,672,245 1,319,531,421,489 918,749,244 0.07%
LLVM.NumLLVMBytesOutput 58,409,784 58,409,816 32 0.0%
time.swift-driver.wall 134.6s 134.9s 359.4ms 0.27%

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) (18)
name old new delta delta_pct
AST.NumLoadedModules 552 552 0 0.0%
AST.NumTotalClangImportedEntities 33,336 33,336 0 0.0%
IRModule.NumIRBasicBlocks 176,853 176,853 0 0.0%
IRModule.NumIRFunctions 78,186 78,186 0 0.0%
IRModule.NumIRGlobals 88,183 88,183 0 0.0%
IRModule.NumIRInsts 1,519,337 1,519,337 0 0.0%
IRModule.NumIRValueSymbols 155,650 155,650 0 0.0%
LLVM.NumLLVMBytesOutput 58,409,784 58,409,816 32 0.0%
SILModule.NumSILGenFunctions 28,956 28,956 0 0.0%
SILModule.NumSILOptFunctions 40,247 40,247 0 0.0%
Sema.NumConformancesDeserialized 79,399 79,399 0 0.0%
Sema.NumConstraintScopes 967,926 967,926 0 0.0%
Sema.NumDeclsDeserialized 198,370 198,370 0 0.0%
Sema.NumFunctionsTypechecked 10,984 10,984 0 0.0%
Sema.NumGenericSignatureBuilders 5,295 5,295 0 0.0%
Sema.NumLazyIterableDeclContexts 24,811 24,811 0 0.0%
Sema.NumTypesDeserialized 107,100 107,100 0 0.0%
Sema.NumTypesValidated 18,705 18,705 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.

Looks great! I've left a couple of minor comments inline.

};

/// A mapping from expressions whose values are captured by the builder
/// to information about the temporary variable capturing the
Copy link
Contributor

Choose a reason for hiding this comment

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

Nit: looks like the rest of the comment is missing here...

// FIXME: Need a locator for the "if" statement.
Type resultType = cs->addJoinConstraint(nullptr,
{
{ cs->getType(thenExpr), cs->getConstraintLocator(thenExpr) },
Copy link
Contributor

Choose a reason for hiding this comment

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

using TernaryBranch(bool) locator element would help to get better diagnostics in there is a mismatch here just like in ConstraintGenerator::visitIfExpr.

Copy link
Member Author

Choose a reason for hiding this comment

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

Okay, thank you! I'll do that in a follow-up PR.

Copy link
Member Author

Choose a reason for hiding this comment

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

Bah, I still need an "anchor", which pretty much has to be the "if" statement for this to make sense. I'll leave it as-is for the moment.

@DougGregor
Copy link
Member Author

@swift-ci please test source compatibility

@DougGregor
Copy link
Member Author

@swift-ci please smoke test

@DougGregor DougGregor merged commit 09bfd94 into swiftlang:master Jan 17, 2020
@DougGregor DougGregor deleted the generlize-function-builders branch January 17, 2020 18:39
@DougGregor
Copy link
Member Author

Bah, I broke something. Reverting!

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