<html><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8"></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; line-break: after-white-space;" class=""><br class=""><div><br class=""><blockquote type="cite" class=""><div class="">On Dec 14, 2018, at 6:34 AM, James Y Knight <<a href="mailto:jyknight@google.com" class="">jyknight@google.com</a>> wrote:</div><br class="Apple-interchange-newline"><div class=""><div dir="ltr" class=""><div class="gmail_quote"><div dir="ltr" class="">On Thu, Dec 13, 2018 at 2:45 PM JF Bastien <<a href="mailto:jfbastien@apple.com" target="_blank" class="">jfbastien@apple.com</a>> wrote:<br class=""></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div class=""><div class=""><blockquote type="cite" class=""><div class=""><div dir="ltr" class=""><div dir="ltr" class=""><div dir="ltr" class=""><div dir="ltr" class=""><div class=""><div class="gmail_quote"><div dir="ltr" class="">On Tue, Dec 11, 2018 at 6:51 PM James Y Knight via cfe-dev <<a href="mailto:cfe-dev@lists.llvm.org" target="_blank" class="">cfe-dev@lists.llvm.org</a>> wrote:<br class=""></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div dir="ltr" class=""><div dir="ltr" class=""><div dir="ltr" class=""><div dir="ltr" class=""><div dir="ltr" class=""><div dir="ltr" class=""><div dir="ltr" class=""><div dir="ltr" class=""><div dir="ltr" class=""><div dir="ltr" class=""><div dir="ltr" class=""><div dir="ltr" class=""><div dir="ltr" class="">After considering this a bit, I'm not sure the __builtin_clear_padding proposal is workable. Worse, I'm not sure that the standards proposal itself is workable, either.<div class=""><br class=""></div><div class="">1. Regarding general implementability of P0528:</div><div class=""><br class=""></div><div class="">Given a call to</div><div class=""> bool atomic_compare_exchange_strong(std::atomic<T>* obj, T* expected, T desired)</div><div class="">we need to first, compare OBJ to EXPECTED -- ignoring padding. Then:</div><div class="">a. if they're equal, store DESIRED into the bytes pointed to by OBJ.</div><div class="">b. if they're not equal, copy the bytes pointed to by OBJ into the bytes pointed to by EXPECTED.</div><div class=""><br class=""></div><div class=""><div class="">The problem is: how to do a comparison ignoring padding? We cannot reasonably modify the comparison operation itself (it's either a hardware instruction, or else a library routine taking only a size). The obvious solution is to clear the padding on both sides of the comparison (thus __builtin_clear_padding).</div><div class=""><br class=""></div><div class="">We can clear the padding in EXPECTED, although we'll need to make a copy first, as we're not allowed to store into it unless the compare-exchange fails. But, we cannot modify OBJ except as part of the "exchange", when the comparison has already succeeded. </div><div class=""><br class=""></div><div class="">If we can ensure that the existing value in OBJ has always had its padding cleared, we're okay. We can be fully in-control of all stores that occur into a std::atomic, and, I believe that's true for C11 _Atomic types as well. (it's unclear to me whether using memcpy to non-atomically overwrite a C11 "_Atomic struct Foo" is allowable, but I'll assume that it is not.)</div><div class=""><br class=""></div><div class="">However, it seems infeasible to make this guarantee for std::atomic_ref, as that can be created pointing to any existing object. Said existing object may have its padding in a non-zero state. Of course, <<a href="http://wg21.link/P0528r3" target="_blank" class="">http://wg21.link/P0528r3</a>> doesn't mention atomic_ref, but the atomic_ref paper, <<a href="http://wg21.link/P0019r8" target="_blank" class="">http://wg21.link/P0019r8</a>> mentions this concern, and simply states "We require that the resolution of padding bits follow [P0528r2]."</div><div class=""><br class=""></div><div class="">It's also infeasible to ensure for the GCC __atomic_compare_exchange builtin, as it also operates on arbitrary existing objects. (Of course, this is irrelevant to the C++ standard.)</div><br class="m_2423383706440950122gmail-m_5240734703991512813gmail-m_-4780080291188902875gmail-Apple-interchange-newline"></div><div class="">I believe a workable alternative would be to generate an extra load of OBJ into a temporary memory, copy only the non-padding bytes of EXPECTED on top of that, and then do the compare_exchange operation of OBJ with the temporary. Finally, copy the temporary back into EXPECTED upon a fail result. While that seems like it could work, requiring an extra load before compare-exchange operations is rather poor.</div></div></div></div></div></div></div></div></div></div></div></div></div></div></blockquote></div></div></div></div></div></div></div></blockquote><div class=""><br class=""></div><div class="">I suggest you synchronize with your company’s C++ representatives, who were present at the EWG discussion which led to this change. They were strongly in favor of this direction, and AFAIK it was understood that there would be a cost for structs with padding bits.</div></div></div></blockquote><div class=""><br class=""></div><div class="">I'm not on the standards committee myself, and, while I am employed by Google, I do not generally coordinate my llvm-dev emails with my company, nor do I have a list of all Google employees who attend which standards body meetings readily available. However, certainly some of said representatives are on this list and can read this message, and could chime in with their own opinions if they so desire. Or, if you tell me which individuals you mean, I could contact them directly.</div></div></div></div></blockquote><div><br class=""></div><div>The main Google representative is Chandler. I suggest talking to him or Richard (who you’ve talked to) because they have access to the discussion’s transcripts, which aren’t public and therefore can’t go to cfe-dev. They can provide the context you’re asking for. I don’t think I can provide that context on a public list without asking everyone who participated.</div><div><br class=""></div><br class=""><blockquote type="cite" class=""><div class=""><div dir="ltr" class=""><div class="gmail_quote"><div class="">Was requiring that the previous value of the atomic be loaded during a compare_exchange the understood implementation strategy in committee discussions?</div></div></div></div></blockquote><div><br class=""></div><div>It was *one of* the understood implementation strategies. Another was specific HW support, and yet another was the compiler just always “doing the right thing” w.r.t. padding (which is the approach you seem to want to take).</div><div><br class=""></div><br class=""><blockquote type="cite" class=""><div class=""><div dir="ltr" class=""><div class="gmail_quote"><div class="">From what I can tell from the outside, it seemed like people were in favor of the change because it was thought it would be easy and cheap to implement. Adding an extraneous load of the atomic value seems to me a whole different matter in terms of cost, and not one I would expect that people would've been readily willing to agree to.</div></div></div></div></blockquote><div><br class=""></div><div>An extra load for cmpxchg of atomic types with padding… which occurs ~never, and likely never on purpose. People agreed that it didn’t matter, but should Just Work, and if there’s a very minor cost then people didn’t really care.</div><div><br class=""></div><br class=""><blockquote type="cite" class=""><div class=""><div dir="ltr" class=""><div class="gmail_quote"><div class="">Had it been discussed that the example in the R0 paper was not an infinite-loop because padding is unspecified during object initialization object copies, as the paper suggests is the case?</div></div></div></div></blockquote><div><br class=""></div><div>?</div><div><br class=""></div><br class=""><blockquote type="cite" class=""><div class=""><div dir="ltr" class=""><div class="gmail_quote"><div class="">But, that there is a potential infinite loop only if the C++ standard allows the padding bits in "expected" to arbitrarily modify themselves at any time between the iterations of the loop, which, unfortunately, it would appear to allow?</div></div></div></div></blockquote><div><br class=""></div><div>This is exactly what the paper is addressing, yes.</div><div><br class=""></div><br class=""><blockquote type="cite" class=""><div class=""><div dir="ltr" class=""><div class="gmail_quote"><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><blockquote type="cite" style="color:rgb(80,0,80)" class=""><div class=""><div dir="ltr" class=""><div dir="ltr" class=""><div dir="ltr" class=""><div dir="ltr" class=""><div class=""><div class="gmail_quote"><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div dir="ltr" class=""><div dir="ltr" class=""><div dir="ltr" class=""><div dir="ltr" class=""><div dir="ltr" class=""><div dir="ltr" class=""><div dir="ltr" class=""><div dir="ltr" class=""><div dir="ltr" class=""><div dir="ltr" class=""><div dir="ltr" class=""><div dir="ltr" class=""><div dir="ltr" class=""><div class="">Which leads into...</div><div class=""><br class=""></div><div class="">2. Do we really need to make this change?</div><div class=""><br class=""></div><div class="">The initial paper gives a sample program in <a href="http://wg21.link/P0528r0" target="_blank" class="">http://wg21.link/P0528r0</a> asserting that it might infinite loop. That would be quite unfortunate.</div><div class=""><br class=""></div><div class="">Repeating the example here, it's basically:</div><div class="">```<br class=""></div><div class=""> T desired = ...;</div><div class=""> T expected;</div><div class=""><div class=""> while (</div><div class=""> !atomic->compare_exchange_strong(</div><div class=""> expected,</div><div class=""> desired // Padding bits added and removed here</div><div class=""> ));</div></div><div class="">```</div><div class=""><br class=""></div><div class="">However, that padding bits in DESIRED may be modified is not super problematic. As long as padding bits in EXPECTED (passed by reference) are not spuriously-modified, we should be fine.</div><div class=""><br class=""></div><div class="">That is, in practice, I believe the loop should execute at most twice, not infinitely. Upon the first failure, all the bits (including padding) pointed to by ATOMIC are copied into the object pointed to by EXPECTED (a reference parameter). At the next iteration of the loop, the call will compare all the bits, including the padding, which was copied as-is from OBJ. As nothing writes to EXPECTED between the two calls, the padding bits will then remain unchanged, and therefore the comparison will succeed on the second iteration.</div><div class=""><br class="">However, Richard Smith informs me that while the above is true in Clang, apparently in theory it is broken -- nothing in the C++ standard prohibits padding bits of an object from be discarded or modified arbitrarily, at any time! That is, according to the spec (but not Clang), the below function "foo" could arbitrarily return non-zero, randomly! (I used "my_mem*" simply for demonstrating that even if memcpy/memcmp calls themselves are simply function calls and not specially-handled, the problem remains.). One could imagine this occurring in a non-pathologically-evil compiler, if it were to copy EXPECTED into registers and then back into memory between the two calls -- and while doing so, only copies the non-padding parts of the object, for efficiency.</div><div class=""><br class=""></div><div class="">```</div><div class=""><div class="">struct Foo { char x; /* padding exists here */ long y; };</div><div class=""><br class=""></div><div class="">int foo(char *mem) {</div><div class=""> Foo expected;</div><div class=""> my_memcpy((char*)&expected, mem, sizeof(Foo));</div><div class=""> return my_memcmp((char*)&expected, mem, sizeof(Foo));</div><div class="">}</div></div><div class="">```</div><div class=""><br class=""></div><div class="">The C standard does appear to prohibit this behavior (C11 <a href="http://6.2.6.1/6" target="_blank" class="">6.2.6.1/6</a>), specifying that the padding bytes get set to unspecified values only upon writes to the object.</div><div class=""><br class=""></div><div class="">In summary: in C++, it would appear that the motivating example can in theory infinite-loop, but in Clang, and the C standard, it'd seem that it cannot.</div><div class=""><br class=""></div><div class="">Changing C++ to require the same semantics for padding stability as C could be a reasonable alternative resolution to the original issue, especially if no implementations actually make use of this allowance today. You may still have a single spurious failure, but it would never infinite-loop.</div><div class=""><br class=""></div><div class=""><br class=""></div><div class="">3. Finally, if the decision is to go forward with P0528 anyways, and if padding bits *can* arbitrarily change, then `__builtin_clear_padding(T* ptr)` is not a theoretically sound interface, because the padding bits could change again, after the call to clear them returns.</div><div class=""><br class=""></div><div class="">We'd need to either implement the support within the atomic builtins themselves (which, as noted, cannot be done within the existing GCC builtins), or define some alternative interface.</div></div></div></div></div></div></div></div></div></div></div></div></div></div></blockquote></div></div></div></div></div></div></div></blockquote></blockquote></div></div>
</div></blockquote></div><br class=""></body></html>