<div dir="ltr"><div class="gmail_extra"><div class="gmail_quote">On Tue, Oct 10, 2017 at 12:23 PM, John McCall via cfe-dev <span dir="ltr"><<a href="mailto:cfe-dev@lists.llvm.org" target="_blank">cfe-dev@lists.llvm.org</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div style="word-wrap:break-word"><br><div><div><div class="h5"><blockquote type="cite"><div>On Oct 10, 2017, at 2:57 PM, John McCall via cfe-dev <<a href="mailto:cfe-dev@lists.llvm.org" target="_blank">cfe-dev@lists.llvm.org</a>> wrote:</div><br class="m_7413606527186370467Apple-interchange-newline"><div><div style="font-family:Helvetica;font-size:12px;font-style:normal;font-variant-caps:normal;font-weight:normal;letter-spacing:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px"><blockquote type="cite"><div>On Oct 10, 2017, at 12:12 PM, Hal Finkel via cfe-dev <<a href="mailto:cfe-dev@lists.llvm.org" target="_blank">cfe-dev@lists.llvm.org</a>> wrote:</div><div><div bgcolor="#FFFFFF" text="#000000"><p>[+Richard, Chandler]<br></p><br><div class="m_7413606527186370467moz-cite-prefix">On 10/09/2017 07:00 PM, Hans Wennborg via cfe-dev wrote:<br></div><blockquote type="cite"><pre>I am not a language lawyer, but I'll atttempt to answer anyway.

On Mon, Oct 9, 2017 at 2:16 PM, Kreitzer, David L via cfe-dev
<a class="m_7413606527186370467moz-txt-link-rfc2396E" href="mailto:cfe-dev@lists.llvm.org" target="_blank"><cfe-dev@lists.llvm.org></a> wrote:
</pre><blockquote type="cite"><pre>This llvm patch, <a class="m_7413606527186370467moz-txt-link-freetext" href="https://reviews.llvm.org/D37289" target="_blank">https://reviews.llvm.org/<wbr>D37289</a>, attempts to do an optimization
that involves speculating loads. The patch itself needs some work regardless,
but we are questioning the legality of the optimization, which is currently
performed by both gcc and icc. The desired transformation looks like this:

    typedef struct S {
      char padding[4088];
      struct S *p1;
      struct S *p2;
    } S;

    struct S* f1(struct S *s, int x)
    {
      S *r;
      if (x)
        r = s->p1;
      else
        r = s->p2;
      return r;
    }

TO

    struct S* f1(struct S *s, int x)
    {
      return (x) ? s->p1 : s->p2;
    }

The fundamental question seems to be whether loading one member of struct S
makes it valid to load other members of the same struct.
</pre></blockquote><pre>Yes, I believe that's true. If we're dereferencing s on both paths, it
must point to a struct S object, and then loading from any member of
that object should be fine.</pre></blockquote><br>I also believe that this is correct.<br><br>I think that Chandler summarized some things to be careful about in this regard here:<br> <span class="m_7413606527186370467Apple-converted-space"> </span><a class="m_7413606527186370467moz-txt-link-freetext" href="http://lists.llvm.org/pipermail/llvm-commits/Week-of-Mon-20170807/477944.html" target="_blank">http://lists.llvm.org/<wbr>pipermail/llvm-commits/Week-<wbr>of-Mon-20170807/477944.html</a><br><br>Of the three points highlighted here, the second clearly might apply:<br><blockquote type="cite"><pre style="white-space:pre-wrap;font-style:normal;font-variant-ligatures:normal;font-variant-caps:normal;font-weight:normal;letter-spacing:normal;text-align:start;text-indent:0px;text-transform:none;word-spacing:0px">2) Related to #1, there are applications that rely on this memory model,
for example structures where entire regions of the structure live in
protected pages and cannot be correctly accessed.</pre></blockquote><br>This, however, is clearly an extension to the standard memory model, and I see no reason to support this by default. Speculatively loading cache lines under contention from other cores might not be a good thing to do for performance reasons, but that's not a correctness issue.<br></div></div></blockquote><div><br></div>I agree that this optimization is legal under the C and C++ specs.</div></div></blockquote><div><br></div></div></div>Sorry, I'd meant to add a proviso to this.  It's legal under the C and C++</div><div>specs to the same extent that any load speculation is.</div><div><br></div><div>In general, load speculation is theoretically problematic in C/C++ because</div><div>it is undefined behavior to have a race even if the race is spurious.  For example,</div><div>a program containing a load that races with a store has undefined behavior even</div><div>if the result of the load is never used.  Therefore, strictly speaking, speculating a</div><div>load can introduce a race that didn't otherwise exist, meaning that it can introduce</div><div>undefined behavior to a program, </div></div></blockquote><div><br></div><div>This is where the argument falls down.  If there's no race in the source code, the behavior *is* defined.  It doesn't matter that the hardware might have a racy read in the implementation, so long as it doesn't affect the behavior of the abstract machine.</div><div> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div style="word-wrap:break-word"><div>meaning that it's not a legal transformation. </div></div></blockquote><div><br></div><div>There might be other reasons (maybe practical reasons) why this isn't permitted, but it's not because it introduces UB (it doesn't).</div><div> <br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div style="word-wrap:break-word"><div> Now,</div><div>AFAIK LLVM doesn't have any multi-threaded analyses that would actually</div><div>miscompile such a speculation, but it can still matter because e.g. TSan emits code</div><div>that dynamically enforces the memory model, and it will dutifully report the race here.</div><div><br></div><div>(Or, to quote from C11 5.1.2.4p28:</div><div><br></div><div>  Transformations that introduce a speculative read of a potentially shared memory</div><div>  location may not preserve the semantics of the program as defined in this standard,</div><div>  since they potentially introduce a data race.</div></div></blockquote><div><br></div><div>I think this is somewhat muddy, and essentially contradicted by the text immediately following it:</div><div> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div style="word-wrap:break-word"><div> However, they are typically valid in the</div><div>  context of an optimizing compiler that targets a specific machine with well-defined</div><div>  semantics for data races. They would be invalid for a hypothetical machine that is</div><div>  not tolerant of races or provides hardware race detection.</div><div><br></div><div>In this sense, TSan makes an arbitrary machine race-intolerant.)</div></div></blockquote><div><br></div><div>Right, this transformation is invalid according to TSan (and a compiler targeting TSan should not apply it) .  Apart from such debug implementations, I'm not aware of an extant platform on which it's problematic.</div><div><br></div><div>-- James</div><div><br></div></div></div></div>