Skip to content

optimize keypath instructions #24929

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 2 commits into from
May 21, 2019
Merged

Conversation

eeckstein
Copy link
Contributor

If the keypath argument of a keypath access function is a keypath literal instruction, generate the projection inline and remove the access function.

For example, replaces (simplified SIL):
%kp = keypath ... stored_property #Foo.bar
apply %keypath_runtime_function(%root_object, %kp, %addr)
with:
%addr = struct_element_addr %root_object, #Foo.bar
load/store %addr

Currently this only handles stored property patterns.

rdar://problem/36244734

@eeckstein
Copy link
Contributor Author

@swift-ci test

@eeckstein eeckstein requested a review from jckarter May 20, 2019 22:01
@@ -1393,6 +1582,12 @@ SILInstruction *SILCombiner::visitApplyInst(ApplyInst *AI) {
return nullptr;
}

SILInstruction *SILCombiner::visitBeginApplyInst(BeginApplyInst *BAI) {
if (tryOptimizeInoutKeypath(BAI))
Copy link
Contributor

Choose a reason for hiding this comment

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

Should we try this more generally on all address values, not only begin_applys?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is the call of the keypath runtime function itself. It can only be an apply or begin_apply.

Copy link
Contributor

Choose a reason for hiding this comment

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

Thanks, sorry, I got it confused with "begin_access".

Copy link
Contributor

@jckarter jckarter left a comment

Choose a reason for hiding this comment

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

Awesome!

@swift-ci
Copy link
Contributor

Build failed
Swift Test Linux Platform
Git Sha - 56e6bbc05c2e612738c5d72061fc19a2a3aa6a43

@eeckstein
Copy link
Contributor Author

@swift-ci test linux

@palimondo
Copy link
Contributor

@eeckstein:

Currently this only handles stored property patterns.

Just out of curiosity, it would not yet be able to optimize something more complex like this segment in DriverUtils, correct?

 
    func deltaEquation(_ stat: KeyPath<rusage, Int>) -> String {
      let b = baseline[keyPath: stat], c = current[keyPath: stat]
      return "\(c) - \(b) = \(c - b)"
    }
    logVerbose(
        """
            MAX_RSS \(deltaEquation(\rusage.ru_maxrss)) (\(pages()) pages)
            ICS \(deltaEquation(\rusage.ru_nivcsw))
            VCS \(deltaEquation(\rusage.ru_nvcsw))
        """)

@jckarter
Copy link
Contributor

If deltaEquation gets inlined, then it seems like this pass ought to be able to constant-fold the key path access.

eeckstein added 2 commits May 21, 2019 09:44
If the keypath argument of a keypath access function is a keypath literal instruction, generate the projection inline and remove the access function.
For example, replaces (simplified SIL):
   %kp = keypath ... stored_property #Foo.bar
   apply %keypath_runtime_function(%root_object, %kp, %addr)
with:
   %addr = struct_element_addr %root_object, #Foo.bar
   load/store %addr

Currently this only handles stored property patterns.

rdar://problem/36244734
@eeckstein
Copy link
Contributor Author

@swift-ci smoke test and merge

1 similar comment
@eeckstein
Copy link
Contributor Author

@swift-ci smoke test and merge

@swift-ci swift-ci merged commit a96890c into swiftlang:master May 21, 2019
@eeckstein eeckstein deleted the optimize-keypath branch May 21, 2019 19:16
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