-
Notifications
You must be signed in to change notification settings - Fork 12.7k
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
Tags should be represented using null ptrs where possible #1271
Comments
Hmm would be nice to have this in other cases, too, by annotating the NULLary case that should be represented by null. Additonally, now that I think about it, might be desirable to assign explicit values to the different nullary cases for representing C enums. |
Yes, I like the idea of adding explicit annotations to help guide the representation. This seems related to supporting C-style enums, where the value of each case can be specified explicitly. |
We could in principle support "single-word" variant type representation (i.e., use NULL) for tags with a small number of variants, but we would have to use "tagged pointers" (i.e., set the low bits). In this way we could support up to 4 nullary variants and 4 pointer variants. I would certainly be inclined to start with |
A long time ago I set out to make this part of the mandatory representation of tags in rust (including the bit-packing on unused pointer bits). I'd be willing to revive the concept, though I'll admit it feels weird to encode as a first-class language feature, and likely hard to cover all cases. |
It doesn't seem like such a weird thing to me, though I wouldn't call it a super high priority feature. Still, I'm pretty keen on the idea of Rust giving you very good control of how data is represented in memory, and I think being able to present a null pointer as an option type can be really important for something like a big array of data where you don't want to spend an extra word per item. Tagged pointers would just be icing on the cake. |
I just noticed this bug and was wondering about the issue. Not directly related, but another thing I would like to see is using special values to represent the case when there is only one unary variant and the rest are nullary to avoid having to have a separate tag field. I am guessing the typestate system can be used to enforce the constrain that the value not be one of the special values. Naturally this is something the user need to request explicitly somehow. However, I agree the special case of one unary that is a pointer and one nullary is the most important. |
The only issue to me with this is that it makes the rules about memory layout more complex with tags. This matters for C interop. As an alternative, we could build |
Yes, it does make things more complex. Though, frankly, most C types probably use a null pointer for this case rather than a (uint, ptr*) pair! Basically, enums with data don't have a very natural C representation imo. I don't know that most I could also imagine some attributes that specify the layout as well. |
I would prefer not to build Issue #1704 requests a |
Oh, also, removing RFC tag. This doesn't really require "everyone nodding" consensus. It's mostly a codegen thing; users aren't even that likely to notice most of the time. |
You don't need to build http://citeseerx.ist.psu.edu/viewdoc/summary?doi=10.1.1.156.3526 and also a chapter in Appel's Compiling with Continuations. |
Agreed, building in option is more complex than is warranted. To address @pcwalton's objection, just have an annotation for specifying the layout of an enum from the various choices we have available. In the absence of such an annotation, we just select as we think best. |
When I was looking at work related to this for my dissertation proposal, I found http://people.ischool.berkeley.edu/~parikh/projects/WIL/ -- a proposal that talks about just such annotations (in the context of the Whirlwind compiler) -- though the work was never implemented and I couldn't find anything close to it. So the problem of how to specify "hints" to the compiler about how to lay out data structures is novel/interesting in and of itself, IMO. |
For ease of searching: this issue is about representation optimizations for enum variants. (The issue talks about tags but that's very old terminology.) |
I will tackle this next week. |
I think I have this working, modulo some cleanup and a bug in LLVM. |
Fixed now, closing. |
Support compiling codegen units in parallel
A tag with one nullary variant and one variant that accepts only a pointer should be implemented using a null pointer at runtime. This would save space and be generally nifty.
The text was updated successfully, but these errors were encountered: