-
Notifications
You must be signed in to change notification settings - Fork 10.5k
Add Builtin.isConcrete<T>(T.Type) -> Int1
#26466
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
Thanks @nvzqz! I'll take a look later today. |
@swift-ci please smoke test |
Our existing precedent for this is to name these sorts of things |
Is this actually tri-state logically though, or do we always know by the time we reach IRGen? |
If you look at how |
@jckarter that name does seem better. I'm not sure if there's a way of expressing this as a tristate. |
@nvzqz Maybe not in this case, but I think by following the pattern set by |
The way that |
@swift-ci please smoke test |
1 similar comment
@swift-ci please smoke test |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM, modulo @jckarter's questions about naming and reuse of existing code.
After thinking about it some more, I'm not sure if a tri-state naming convention makes sense for this. I think that @jckarter I've attempted a different implementation that is done at the same stage as See stack dump
I'm of the opinion that the current changes are sufficient for now and that we should defer optimizing it or reusing the code for |
Just as an exploration of the state space, consider the following: Imagine that we had a builtin called canBeConcrete. It returns either always, never, sometimes (i.e. a tri-state). If we have a concrete type, easily the builtin must return always since once a type is concrete we can not further specialize it. What about generic types? Easily, there are two possibilities:
The key thing to notice is that the 2nd bullet point via our semantics is IRGen time/Lowered SIL. That might be the way forward for this in a way that keeps the logic of when to fold this in a utility that the various code can invoke. Thats all I got. NOTE: If we were to embed these semantics if we don't assert that the specialization machinery never runs in Lowered SIL, we would necessarily have to do so in order to be careful and prevent mistakes. |
@nvzqz I commented on the linked commit. You should not be erasing the instruction from inside the |
d184918
to
45777bc
Compare
I walked through my implementations of @jckarter's suggestions with @atrick and found places where it was ill-formed. The concerns I expressed to @airspeedswift and @gottesmm previously with regard to eager evaluation (always emitting |
45777bc
to
44cc730
Compare
@swift-ci please smoke test |
/// this returns `false` if the check has failed. | ||
/// | ||
/// Note that there may be cases in which, despite `T` being concrete at some | ||
/// point in the caller chain, this function will return `false`. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe add that it will never return a false positive.
@swift-ci please smoke test |
@swift-ci please smoke test |
@swift-ci please smoke test |
a3e180c
to
a73e146
Compare
My concerns are resolved, but please check with @atrick to make sure he's satisfied with the updated SIL tests. |
Returns `true` if `T.Type` is known to refer to a concrete type. The implementation allows for the optimizer to specialize this at -O and eliminate conditional code. Includes `Swift._isConcrete<T>(T.Type) -> Bool` wrapper function.
The `Builtin.isConcrete` function is apparently sensitive to inlining in unoptimized builds.
Returns `false` otherwise in Debug builds because it is generic.
When calling `Builtin.isConcrete` in SIL, the provided type parameter should be sufficient.
c649378
to
931ab46
Compare
@atrick the SIL tests have been addressed as well as the concerns around |
@atrick you good to go with this? |
@swift-ci please smoke test |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I see the unreachable code in constandFoldBuiltin was removed. And the .sil test cases look reasonable. Thanks.
@swift-ci please test and merge |
Returns
true
ifT.Type
is known to refer to a concrete type during IRGen. Otherwise, if still generic, then it is lowered tofalse
right before IRGen in IRGenPrepare. This allows for the optimizer to specialize this at -O and eliminate conditional code.This also Includes:
Swift._isConcrete<T>(T.Type) -> Bool
wrapper function.isConcrete
on generic and concrete types in -O[none] builds.This is prior work to adding
Builtin.generic_add<T>(T, T) -> T
and friends for SIMD vector types.