Skip to content

SIL: Replace uses of getReferencedFunction() by getReferencedFunction… #25073

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

Conversation

aschwaighofer
Copy link
Contributor

…OrNull() and getInitialReferencedFunction()

With the advent of dynamic_function_ref the actual callee of such a ref
my vary. Optimizations should not assume to know the content of a
function referenced by dynamic_function_ref. Introduce
getReferencedFunctionOrNull which will return null for such function
refs. And getInitialReferencedFunction to return the referenced
function.
Use as appropriate.

rdar://50959798

…OrNull() and getInitialReferencedFunction()

With the advent of dynamic_function_ref the actual callee of such a ref
my vary. Optimizations should not assume to know the content of a
function referenced by dynamic_function_ref. Introduce
getReferencedFunctionOrNull which will return null for such function
refs. And getInitialReferencedFunction to return the referenced
function.
Use as appropriate.

rdar://50959798
@aschwaighofer
Copy link
Contributor Author

@swift-ci Please test

Copy link
Contributor

@gottesmm gottesmm left a comment

Choose a reason for hiding this comment

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

Some typos. Was just reading the code.


/// Return the referenced function if the callee is a function_ref like
/// instruction.
/// WARNING: This not necessarily the function that will be called at runtime.
Copy link
Contributor

Choose a reason for hiding this comment

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

Could you put a newline here before, WARNING.

/// Return the referenced function if the callee is a function_ref like
/// instruction.
/// WARNING: This not necessarily the function that will be called at runtime.
/// If the callee is a (prev_)dynamic_function_ref the actuall function called
Copy link
Contributor

Choose a reason for hiding this comment

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

actuall => actual.


/// Return the referenced function if the callee is a function_ref like
/// instruction.
/// WARNING: This not necessarily the function that will be called at runtime.
Copy link
Contributor

Choose a reason for hiding this comment

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

Same spelling issue here.

/// Return the referenced function.
SILFunction *getReferencedFunction() const { return f; }
/// WARNING: This not necessarily the function that will be called at runtime.
/// If this is a (prev_)dynamic_function_ref the actuall function called might
Copy link
Contributor

Choose a reason for hiding this comment

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

Same spelling issues.

@gottesmm
Copy link
Contributor

Overall, I find the names here to be sort of confusing. When do I want to use getReferencedFunction and when do I want to use getInitialFunction?

When I think at a high level about it, I think the distinction is about the actual function to be called vs the identity of a specific abstract function pointer. I don't have a better name at this point in time, but these names worry me.

@gottesmm
Copy link
Contributor

That being said, if you need to get this in ASAP, we can talk about this offline and follow up.

@aschwaighofer
Copy link
Contributor Author

You want to use getReferencedFunction when the client cares about the dynamic target - the content of the function. The higher level API named the same way on ApplySite and apply instructions already had this behavior: it returned the function_refed sil function or null if the callee was dynamic.

How about something like:
getKnownTargetFunctionOrNull()

I liked the initially referenced naming because to me when I read this I think: initially means something might change ...
Static does not evoke that thought process with me.

I am totally open to a different naming scheme.

@gottesmm
Copy link
Contributor

If you think about the essence of what you are distinguishing, one is the dynamic function at runtime and the other is in a sense an ID for a specific function ptr in the program. The initial function is the identity of that abstract concept. I wonder if we can work that into the names somehow.

@gottesmm
Copy link
Contributor

if you need to get this in ASAP, we can always rename using Xcode's refactoring engine.

@aschwaighofer
Copy link
Contributor Author

There is the distinction (that I think you pointed out earlier):

getStaticallyReferencedFunction()

vs

getDynamicallyReferencedFunction()

but I feel like getStaticallyReferenced does not sound scary enough when read alone in the context of some code. “Initially” conveys that something might change.

getKnownDynamicallyReferenced()

Vs

getInitiallyStaticallyReferenced()

Maybe?

@atrick
Copy link
Contributor

atrick commented May 27, 2019

getReferencedFunctionOrNull is a fine name as long as the comments explain why some apply sites return null.

That's the primary API. getInitiallyReferencedFunction is just a placeholder that I think should go away. So we don't need to make those names somehow symmetric. The only other name I can think of would be getReferencedFunctionPlaceHolder which is pretty awkward.

My main suggestion here is to add more comments to the WARNING so it's clear that this API is deprecated and the alternative is to use the referenced SILFunctionType instead. Maybe even a FIXME to rewrite the current uses of that API. Do you foresee any difficulty rewriting those uses over time?

@gottesmm
Copy link
Contributor

Just had an interesting thought. One thing I noticed while reading the code is that the major places that the getInitiallyReferencedFunction() are cloner like functions. Maybe the right way to do this is to make it so that the dynamic function ref takes a static function ref as its "default value". Then when we clone, the inst will just clone itself and the clone the function_ref which would go through the normal code path.

@aschwaighofer
Copy link
Contributor Author

Yes there are placed like the cloner, verifier, and SILFunction ref counting where it won’t go away.

If we want to make it really hard to use the API, we could make the API private and friend those places.

@atrick
Copy link
Contributor

atrick commented May 27, 2019

Instead of getInitialReferencedFunction, I would handle this with a getFunctionRef API that returns a SILFunctionRef which could be used to build new *function_ref instructions but wouldn't generally allow access to the function body. I'm not sure if that would help the non-cloner use cases or if it's worth the trouble.

@aschwaighofer
Copy link
Contributor Author

@swift-ci Please test

@swift-ci
Copy link
Contributor

Build failed
Swift Test Linux Platform
Git Sha - c187c8a

@swift-ci
Copy link
Contributor

Build failed
Swift Test OS X Platform
Git Sha - c187c8a

@aschwaighofer aschwaighofer merged commit 8d1fabd into swiftlang:master May 28, 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.

4 participants