-
Notifications
You must be signed in to change notification settings - Fork 4
opaque type translation #208
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
Do note that translating the layout of monomorphized generic types is not on the short-term queue, it will be a very complex change. |
Well, a more temporary way would be to parse the result from "-Zprint-type-sizes", I guess everything is already included. |
Let's keep that discussion to Zulip |
Your proposal description is very high-level, so I'm going to try to fill in the blanks so that we can describe precisely what it is that you propose. If I had to guess, what you're proposing is the following. Please fill in the blanks and/or rephrase, I left question marks where I'm trying to guess. Background:
(Question: is this only for a multi-crate scenario or can this happen within a single crate?) Proposal:
(Question: what if only some of the fields of a type are private? what happens then?) Open questions:
Please comment -- thanks! |
Thanks for this! Let me fill the blanks: Background
Question: is this only for a multi-crate scenario or can this happen within a single crate?A: In summary, a multi-crate scenario is so common as real-world codes almost always use As an example, my plan of systematic support of the std would be to parse Proposal
Question: what if only some of the fields of a type are private? what happens then?A: I'm considering to either make the fields all public by Open questions
Yes.
Yes, and this should be guaranteed by Charon. |
Seems that the current Mlkem error is from that you have provided a true definition for the type, while it is deemed opaque and the layout is generated in the new implementation. |
To be clear, this notion of opaqueness is not a rust concept. Rustc has access to the entirety of all type definitions in crate dependencies. These opaque types are a notion invented by charon to avoid translating everything at all times. Running charon with |
Thanks for the note. Translating everything very easily leads to errors when the codes use |
In this case the easiest solution is to use |
@Nadrieril and I are discussing this right now -- we have further thoughts about your proposal:
Questions for you:
|
@Nadrieril's proposal seems like it might help you achieve what you want -- perhaps let us know a little bit more about your use-case? we might be able to help you better this way (and if this is something you'd rather discuss in private, that's fine, and we can have a private chat on zulip!) cheers |
@Nadrieril Thanks for your proposal, but this requires too much human-work, and I would prefer a more auto way. On the other hand, to include everything, even if possible, produces too much extra-stuff that requires more cleanup? @msprotz Thanks for your considerations and kindness! For the alignment problem, as the layout info from Charon includes this, I think it can be easily added. For the second, let us study more about this in a broader range. But we are sure that we are not working on compilers that might get this wrong. But yes, this might not be correct for every potential compiler. So shall we make this translation optional? |
There is a mechanism in Eurydice to eliminate all unused definitions -- see the .yaml file I added in test/, it tells Eurydice to only keep the definitions that are reachable from that particular test file. Would this help you?
So let's think a little bit here, and let's look at, say, arm64: https://github.com/ARM-software/abi-aa/blob/main/aapcs64/aapcs64.rst#composite-types. It's clear that you must preserve the alignment between the two "visions" of the type (raw bytes, and actual type definition). But then there are other subtleties: if you look at 5.10, a C struct becomes what's known as a composite type in that document... unless it's made up entirely of floats! See 5.10.5.1 -- in that case it's called a homogeneous floating-point aggregate (HFA) Then, let's look at 6 https://github.com/ARM-software/abi-aa/blob/main/aapcs64/aapcs64.rst#the-base-procedure-call-standard. The rules are different for structs and for HFA... see 6.8.2. Structs might go into general-purpose registers if there are some left (Rule C.12), or to the stack if there aren't enough registers left (Rule C.15), unless the struct has only float fields, and no more than 4, in which case it's an HFA and the struct goes into SIMD/Floating-point registers (Rule C.2). I'm not trying to be difficult here: I'm simply pointing out that the scheme you describe will be guaranteed not to work in some cases, because compilers have to obey the ABI. So it's not a matter of "we know the compiler won't get it wrong", it's a matter of "the compiler is required to get it wrong". Given that information, I want to make sure we exhaust every other possibility before contemplating what you describe, because your solution comes with deep limitations (see above) which in turn will require more design work and will make it harder for me to say "great idea! let's merge immediately". Questions for you.
If the code that you're trying to run through Eurydice is public, it might help @Nadrieril and I provide good guidance, but understood if it's not. |
I also forgot to mention that --extract-opaque-bodies should pick all the definitions that are reachable |
Thanks for the useful info! Let us study more on this case! |
Sure, your points make sense, and this implementation is potentially buggy. Thanks for the very insightful discussion! |
Originally, Eurydice simply drops the external types with private fields (opaque types), which causes many converted codes referring to undefined types. Thanks to the update in Charon in Charon/PR#704. Now we can fetch the layout info and translate the opaque types to:
Here
SIZE
is the actual size coming from the layout info.This helps maintain the correct semantics of the original program and inter-crate modular translation.
Currently, the monomorphised types are still not supported to fetch layout info. Once Charon is ready, the code should work smoothly for types like
Vec<i32>
.