last day (292 days later) » 

8:56 PM
supercat: What problem would there be with saying that a forming reference or pointer to a union-of-PODS member that will later be used, without laundering, to write a byte will render any pre-existing pointers or references to other members of that union unusable for accessing that byte unless/until they are laundered,
supercat: and forming a reference or pointer to a UOPM that will be used without laundering to read a byte will render any pre-existing pointers or references to other members unusable for writing that byte unless/until they are laundered?
supercat: If code takes the addresses of two union members and passes them into a function, and no byte that is written using either is accessed by the other, code which would work when the objects are distinct should work just as well if they are used to disjoint parts of the same union and/or parts of the union that aren't modified.
supercat: Meanwhile, the calling code should work just as well whether the bytes of the union are written using the first address or the second. Only cases where a byte is modified through one pointer and accessed via the other should pose any problem.
supercat: The Standard's concept of "active member" makes the legitimacy of code to handle an object dependent upon the last type as which an object was written (in some arbitrarily-distant context) before the function was called, and keeps track of any changes made to an object's type until the next time it's written (which may also be in an arbitrarily-distant context).
supercat: My concept, by contrast, wouldn't require a compiler to care about what happens before a function is called or after it returns.
@supercat I've copied your comments here, hope you don't mind.
I've got some questions: Are you seeing these rules as replacing the union active member rules, but not the object lifetime rules, or both?
If it's the former, then I suppose this will come together with "all union member subobjects are within their lifetimes at the same time". This would need an (additional) exception in the C++ object model, as these objects are not of the kind that the current model allows to have overlapping lifetimes and storage at the same time.
If it's the latter, are we defining any kind of concept of lifetime? std::launder, for example, depends on lifetimes, so the laundering part would need adjusting, at least, in this case.
 
9:12 PM
Fundamentally, I would like to recognize that only certain specific sequences of actions involve "aliasing", and say that when dealing with PODS, all type-based access restrictions are only applicable in cases that actually involve aliasing.
 
So, basically, slightly finer grained restrictions than what we have now?
 
Scalars and PODS *don't have lifetimes* apart from the storage in which they are contained. What matters for purposes of aliasing isn't the content of the storage in question, but rather existence of active references to it. Consider the function:

void test1(float *fp, int *ip, int mode)
{
*fp = 1.0f;
*ip = 1.0f;
if (mode)
*fp = 1.0f;
}

By my understanding of dynamic type rules, compilers would be required to perform the integer store last if mode==0, or the float store last if mode==1.
Under my preferred rules, compilers could optimize out the conditional store, since if both *fp and *ip identify the same storage, writing to either would cause the other to become unusable without laundering.
 
@supercat As far as I can tell, under the current C++ rules, if *fp and *ip designate overlapping union subobjects, that code has to have undefined behaviour.
Because those assignments are not of the forms that can start the lifetime of a union member subobject, so at least one of them assigns something to an object that is not within its lifetime.
So I think under the current rules the compiler can safely optimize out the last store to *fp.
 
9:28 PM
Given something like union { unsigned u; float f;} wuzza;, there should be a huge difference between float *fp = &wuzza.f; unsigned *up = &wuzza.u; *fp = 1.0f; cout << *up; [which actually involves aliasing], and float *fp = &wuzza.f; *fp = 1.0f; unsigned *up = &wuzza.u; cout << *up; [which doesn't]
If the things in question happen to point to unions, that may be true, but would imply that something like cin >> wuzza.u; would only be valid if code had written some meaningless value to wuzza.u first, which is probably not what the Standard should say. If they could point to malloc storage, the dynamic type rule would imply that each store changes the type of the storage.
 
The malloc issue is still open in C++ as far as I know, let's not bring it in just now...
Why doesn't the second case involve aliasing?
 
Because between the time fp is created and the last time it is used, every reference that is used to access the storage is derived from fp.
Additionally, the compiler can see that the next lvalue which will be used to access that storage is derived from the same object as fp was derived.
 
Are these current C rules (sorry, I'm not up-to-date on that front), or are they part of what you're proposing?
 
They're how the C rules should have been interpreted from the beginning.
 
OK, but in the first case, nothing is accessing the storage between the point when fp is created and the point when it's last used either.
unsigned *up = &wuzza.u; is not an access
 
9:44 PM
The only way in which any version of the C or C++ rules makes any sense is if one recognizes that an access to a reference which is derived from another (e.g. &aggregate.member) is an access to the original.
 
"access to a reference" -> "access through a reference", I suppose?
 
Yeah.
The Standard does not allow an lvalue of an aggregate's member type to be used to access an object of the aggregate type. If, however, an access via an lvalue which is derived from one of another type is an access to the parent object, then that case is handled easily.
 
Well, yes, there are even explicit rules that the pointer you get from &x, when indirected through, will give you an lvalue designating x (in modern C++ terminology, it points to the object named by x).
 
Anyway, getting back to the examples, the problem in the aliasing case is that the storage in question is that between the time up is created and the last time it is used, the storage in question is written using lvalue *fp which is not derived from up.
 
OK, so it's up we're looking at, yes there is a difference there, but I'm not sure if it's a good idea to see the two as hugely different.
 
9:54 PM
I think things may be cleaner if one defines a verb [perhaps "to address a byte via a reference"] referring to the act of either accessing a byte via reference, or using the address of a reference to derive a pointer or lvalue that will later be used to address that byte.
If one looks at what an optimizing compiler would have to do to handle each case in a fashion consistent with performing all accesses as actual loads and stores, the no-aliasing case is a lot easier.
 
@supercat Well, in C++ at the moment, both are equally easy, since both have undefined behaviour :-)
 
If one defines "to read-address" and "to write-address" as variations of "to address" based upon whether the ultimate accesses will include reads or will include writes, and defines a reference as "read-active" with respect to a byte if it will ever be used to read-access it, and likewise "write-active", then aliasing becomes pretty easy to define.
Two references X and Y alias each other if X is addressed while Y is write-active, or X is write-addressed while Y is active, and X is not visibly derived from Y.
@bogdan: Even an access to a struct member of non-character type invokes UB.
 
@supercat What do you mean?
@supercat Regarding this, wouldn't this involve unbounded "look into the future" actions for the optimizing compiler?
 
The information a compiler would actually need to know to apply this definition would fit very nicely with the information available to it.
A compiler's job after all shouldn't be to go "neener neener" if things alias, but rather to recognize and properly handle the usage cases where they don't.
 
@supercat What if the read-access is somewhere in a function that is not analyzed as part of the current compilation?
(The read-access from the "if it will ever be used to read-access".)
 
10:10 PM
In the absence of aliasing, a compiler may regard accesses via reference as being unsequenced with regard to anything else in the universe.
If the last time a reference is used to address a byte occurs before the next time its parent is used to access that byte in conflicting fashion, then all operations done via the reference will occur between the previous and succeeding operations on the parent.
Addressing a parent object while a child is read-active would essentially mean that operations on the old and new references would be unsequenced relative to each other. Not a problem if all such operations are reads, but an obvious problem if any are writes.
ttyl
 
10:32 PM
Yeah, I'm sorry, maybe it's too late over here, but I'm struggling to see how much improvement these alternative rules would bring, besides enabling some type punning through unions, perhaps.
 
10:49 PM
The current rules are simply unworkable. They're close enough to being workable that it's possible to muddle through, but there are some cases gcc and clang would need major rework to handle in conforming fashion, and...
...the easiest way to fix them would require logic that would be close to what would be needed for my proposed rules.
As for complexity/simplicity, if the current rules were simple, there wouldn't be so much confusion about what they're supposed to mean.
My rules would also fix a couple of cases where the current rules mandate behavior in situations that genuinely involve aliasing, and thus degrade performance in situations where no aliasing would actually occur.
Given file-scope variables char *dataptr; int datalen;, can you think of any way to write a function void add_byte(char ch) { *dataptr++ = dat; datalen++; } that could work efficiently if called from within a simple loop? The fact that character pointers are allowed to alias anything would make it very difficult for a compiler to optimize, and I can't think of any way to help the compiler out while still allowing for the add_byte function to be changed should the need arise.
If one were to say that character pointers may only be used in ways that don't actually involve aliasing (i.e. the creation of the pointer and all use thereof must occur between the next and succeeding operations on the logic that don't involve it), then a compiler could cache dataptr and datalen in registers during a loop unless dataptr was set to point to either dataptr or datalen within that loop.
 

  last day (292 days later) »