Skip to content

[OSLogOptimization] Improve the OSLogOptimization pass so that it can fold constant arrays and closure literals #28168

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

ravikandhadai
Copy link
Contributor

This PR enhances the OSLogOptimization pass so that it can fold constant arrays and closure literals that are inferred by the constant evaluator while evaluating os log calls. The main changes of this PR are mostly confined to the function emitCodeForSymbolicValue of the OSLogOptimization pass. In addition to adding support for array and closure folding, this PR also contains a fix to a (minor) bug in the code that manages the lifetime of the folded value. For perspicuity, this PR is split into 3 self-contained commits as described below:

  • Commit 9b57208 fixes a bug in the code that replaces a guaranteed value by an owned (constant) value. It also slightly improves the replacement logic for non-OSSA (which is meant to be phased out soon once OSSA is enabled in the mandatory pipeline) so that it can handle the folding of arrays and closures introduced in this PR. This commit updates OSLogPrototypeCompileTest.sil test suite to reflect these changes.

  • Commit fa0ec3d adds support for folding constant arrays. It also adds SIL tests for testing array folding logic to the file: OSLogPrototypeCompileTest.sil.

  • Commit f7b489e adds support for folding symbolic closures, which are representations of closure literals in the constant evaluator. It also adds SIL tests for testing the closure folding logic. Importantly, this commit updates the OSLogPrototypeCompileTest.swift to check whether the "array of closures" used by the new OSLog overlay is folded.

@ravikandhadai
Copy link
Contributor Author

@swift-ci Please test

@swift-ci
Copy link
Contributor

swift-ci commented Nov 9, 2019

Build failed
Swift Test Linux Platform
Git Sha - f7b489ea689d4fd06a9d2d414342fd59b71cfa14

@ravikandhadai
Copy link
Contributor Author

@swift-ci Please test Linux Platform

Copy link
Contributor

@atrick atrick left a comment

Choose a reason for hiding this comment

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

Generally looks good. A couple of comments are inline.

SILInstruction *constructorInst = definingInst;
if (isa<DestructureTupleInst>(definingInst) ||
isa<TupleExtractInst>(definingInst)) {
constructorInst = definingInst->getOperand(0)->getDefiningInstruction();
Copy link
Contributor

Choose a reason for hiding this comment

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

constructorInst is an unused variable.
If this is done because some particular array allocation intrinsics returns a tuple, you might want to make that a little more explicit at least in comments and have a small test case if it's not too hard.

Copy link
Contributor

Choose a reason for hiding this comment

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

Is value required to be a result of definingInstruction?

/// Return true iff the given value is a closure but is not a creation of a
/// closure e.g., through partial_apply or thin_to_thick_function or
/// convert_function.
static bool isFoldableClosure(SILValue value, SILInstruction *definingInst) {
Copy link
Contributor

Choose a reason for hiding this comment

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

It doesn't look like you actually need definingInst. Unless value comes from a different instruction altogether, in which case the meaning of these arguments should be explained.

Copy link
Contributor

Choose a reason for hiding this comment

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

You can check a value's type directly. And if you really need its defining instruction, it's not a big deal to either dyn_cast it to SingleValueInstruction or just call getDefiningInstruction again.

@ravikandhadai ravikandhadai force-pushed the oslog-closure-array-folding branch from f7b489e to 99ce129 Compare November 13, 2019 01:13
@ravikandhadai
Copy link
Contributor Author

@swift-ci Please test

@swift-ci
Copy link
Contributor

Build failed
Swift Test OS X Platform
Git Sha - 99ce1291d9256abe126a200be486a8619942810a

@ravikandhadai ravikandhadai force-pushed the oslog-closure-array-folding branch from 99ce129 to e325d37 Compare November 13, 2019 01:20
@ravikandhadai
Copy link
Contributor Author

@swift-ci Please test

1 similar comment
@ravikandhadai
Copy link
Contributor Author

@swift-ci Please test

@ravikandhadai
Copy link
Contributor Author

ravikandhadai commented Nov 13, 2019

@atrick Thanks for the comments. I have pushed a commit that addressed the comments. I have also added a test testNoFoldingOfArrayLiterals in OSLogPrototypeCompileTest.sil that checks that array literals are not folded.

@swiftlang swiftlang deleted a comment from swift-ci Nov 13, 2019
@swiftlang swiftlang deleted a comment from swift-ci Nov 13, 2019
@swift-ci
Copy link
Contributor

Build failed
Swift Test Linux Platform
Git Sha - 99ce1291d9256abe126a200be486a8619942810a

of the OSLogOptimization pass. This commit contain two changes:
 - It handles non-OSSA better (but it is meant to be phased out) so
   that array and closure folding can be supported
 - It fixes a bug in the OSSA folding by making sure that when an
   owned value replaces a guaranteed value, the owned value is
   borrowed and the borrow is used in place of the guaranteed value.
pass so that it constant folds array symbolic values inferred by the
constant evaluator when evaluting os log calls.
fold a symbolic closure, which is the representation of a closure literal
in the constant evaluator. This commit improves the function
emitCodeForSymbolicValue so that given a symbolic closure it can emit
SIL code for constructing the closure.

This improvement enables folding the arguments array, which is an array
of closures, by its constant value inferred by constant evaluating the
new OSLog calls.
@ravikandhadai ravikandhadai force-pushed the oslog-closure-array-folding branch from e325d37 to 633bc79 Compare November 13, 2019 02:39
@ravikandhadai
Copy link
Contributor Author

@swift-ci Please test

1 similar comment
@ravikandhadai
Copy link
Contributor Author

@swift-ci Please test

@swift-ci
Copy link
Contributor

Build failed
Swift Test OS X Platform
Git Sha - e325d37cba1319784764d074b8a364dfb290cda2

@swift-ci
Copy link
Contributor

Build failed
Swift Test Linux Platform
Git Sha - e325d37cba1319784764d074b8a364dfb290cda2

@ravikandhadai ravikandhadai merged commit 6f83668 into swiftlang:master Nov 13, 2019
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