Semantic hint for the standard library #1256
Replies: 1 comment 3 replies
-
I am really really wary of this. In Swift, the back-end team added these hints and as a standard library author I had no way to know what they meant nor whether I was going to break them by any particular refactor. So I've been burnt by a feature exactly like this one. Before we go any further with this discussion though, it's important that you explain the problem this is actually solving. What optimization pass are you trying to write and how is it hampered by not having these hints?
Really, all optimizations to be enabled by this feature boil down to constant-folding? If that were the case, a much more targeted annotation like Swift's |
Beta Was this translation helpful? Give feedback.
-
One problem we have to write optimization passes on Hylo IR that it is hard to know what an operation is trying to achieve, even on "compiler-known" types. Part of the problem is that the user is free to redefine conformances everywhere, thereby shadowing the semantics of the standard library.
The compiler might be allowed to skip some operations (e.g., necessary moves or copies), but I don't think that is an approach we can apply to all operations. There are some conformances that are legit to shadow on standard library types, like hashing when the user has extra knowledge about the instances that can be hashed in some particular scope. Further, even without conformance shadowing, it is still not obvious to figure out the semantics of a particular operation once we're deep in the IR.
To address this issue, I propose to add semantic hints on the definition of some operations. These hints would be stored in the declaration of the function/method implementing the operation and so during IR processing we would be able to check whether the function being called implements an operation that the compiler knows about.
In code it would look like this:
Now when the compiler sees a call to this specific implementation of
Int.infix+
, it can emit different code or even evaluate the result of the operation at compile time.This feature wouldn't be necessary if we were already able to run any function at compile time, which is a goal we have on our roadmap. Implementing generalized compile-time evaluation is a big project, though, so these semantics hint might help fill the gap before we get there.
Beta Was this translation helpful? Give feedback.
All reactions