-
Notifications
You must be signed in to change notification settings - Fork 5.4k
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
Monomorphization implementation is not true monomorphization #862
Comments
Just to provide some context around how methods for these monomorphized types were going to work in my original implementation (although remain unimplemented)... Assuming type id We need to make it such that tl;dr: the auto-derived |
I'm not sure what exactly you mean by true -- in the type system, if the types that are referenced by two ids are equal, then they should be the same type. That's where this is falling apart, not the fact that we use type ids so liberally. |
I am proposing that instead of making one monomorphized copy per enum/struct/function use, leading to potentially many copies of the same type definition, that instead we only make one copy per type. |
I'm not sure I see the benefit -- that's a pretty hefty rewrite/restructuring, and the purpose of the original design is that it is cheap and easy to monomorphize all over the place and delegate the work of resolving the types to the type engine. If things are working correctly, there should be lots of differing type ids. If the equality of types is defined in terms of its representation in the type engine, and not by raw type ids, would that accomplish the same goal, without having to do all of that rewriting? |
This will be solved by #1821. |
Closing in favor of #2636 |
The implementation of monomorphization is currently not actually monomorphization but instead a close approximation. This is causing implementation difficulties.
Given this Sway code:
the resulting types of the variable declarations during codegen are:
meaning that monomorphization has produced (in approximate code):
instead of what "true" monomorphization produces:
This is because monomorphization happens each time a struct is created:
sway/sway-core/src/semantic_analysis/ast_node/expression/typed_expression.rs
Line 873 in fb93b1b
In contrast, Rust performs monomorphization by first going through a "collection phase" where all types used for generics are collected, and this information is used for monomorphization: https://rustc-dev-guide.rust-lang.org/backend/monomorph.html#collection
As it stands, there may be no practical difference between "true" monomorphization and the current implementation, aside from additional complexity resulting from duplicate definitions. However there is motivation for changing it going forward:
sway/sway-core/src/semantic_analysis/namespace.rs
Line 114 in fb93b1b
I propose that we consider restructuring monomorphization to be more akin to "true" monomorphization, with these issues as the motivation.
The text was updated successfully, but these errors were encountered: