<div dir="ltr">Hello,<div><br></div><div>We recently came across a "suspicious" behavior of Clang w.r.t. volatile accesses, and aren't sure if this is expected or if this is a bug/limitation.</div><div><br></div><div>Here is the sample code:</div><div><br></div><div><div style="color:rgb(0,0,0);background-color:rgb(255,255,254);font-family:Consolas,"Liberation Mono",Courier,monospace,Consolas,"Courier New",monospace;font-size:14px;line-height:19px;white-space:pre"><div><span style="color:rgb(0,0,255)">struct</span> parent { <span style="color:rgb(0,0,255)">int</span> a; };</div><div><span style="color:rgb(0,0,255)">struct</span> child : parent { <span style="color:rgb(0,0,255)">int</span> x; };</div><br><div><span style="color:rgb(0,0,255)">int</span> bar(<span style="color:rgb(0,0,255)">volatile</span> child &v) {</div><div>    <span style="color:rgb(0,0,255)">return</span> v.x + v.a;</div><div>}</div></div><div><br></div><div>And here is the resulting IR:</div><div><br></div><div><div style="color:rgb(0,0,0);background-color:rgb(255,255,254);font-family:Consolas,"Liberation Mono",Courier,monospace,Consolas,"Courier New",monospace;font-size:14px;line-height:19px;white-space:pre"><div><span style="color:rgb(205,49,49)">%</span><span style="color:rgb(0,0,255)">struct.child</span> <span style="color:rgb(205,49,49)">=</span> <span style="color:rgb(0,128,128)">type</span> { <span style="color:rgb(0,128,128)">%struct.parent</span>, <span style="color:rgb(0,128,128)">i32</span> }</div><div><span style="color:rgb(205,49,49)">%</span><span style="color:rgb(0,0,255)">struct.parent</span> <span style="color:rgb(205,49,49)">=</span> <span style="color:rgb(0,128,128)">type</span> { <span style="color:rgb(0,128,128)">i32</span> }</div><br><div><span style="color:rgb(0,0,255)">define</span> <span style="color:rgb(0,128,128)">dso_local</span> <span style="color:rgb(0,128,128)">i32</span> <span style="color:rgb(0,128,128)">@_Z3barRV5child</span>(<span style="color:rgb(0,128,128)">%struct.child</span>* <span style="color:rgb(0,128,128)">nonnull</span> <span style="color:rgb(0,128,128)">align</span> <span style="color:rgb(9,134,88)">4</span> <span style="color:rgb(0,128,128)">dereferenceable</span>(<span style="color:rgb(9,134,88)">8</span>) <span style="color:rgb(0,128,128)">%v</span>) <span style="color:rgb(0,128,128)">local_unnamed_addr</span> <span style="color:rgb(9,134,88)">#0</span> {</div><div><span style="color:rgb(0,128,128)">entry:</span></div><div>  <span style="color:rgb(205,49,49)">%</span><span style="color:rgb(0,0,255)">x</span> <span style="color:rgb(205,49,49)">=</span> <span style="color:rgb(0,128,128)">getelementptr</span> <span style="color:rgb(0,128,128)">inbounds</span> <span style="color:rgb(0,128,128)">%struct.child</span>, <span style="color:rgb(0,128,128)">%struct.child</span>* <span style="color:rgb(0,128,128)">%v</span>, <span style="color:rgb(0,128,128)">i64</span> <span style="color:rgb(9,134,88)">0</span>, <span style="color:rgb(0,128,128)">i32</span> <span style="color:rgb(9,134,88)">1</span></div><div>  <span style="color:rgb(205,49,49)">%0</span> <span style="color:rgb(205,49,49)">=</span> <span style="color:rgb(0,0,255)">load</span> <span style="color:rgb(0,128,128)">volatile</span> <span style="color:rgb(0,128,128)">i32</span>, <span style="color:rgb(0,128,128)">i32</span>* <span style="color:rgb(0,128,128)">%x</span>, <span style="color:rgb(0,128,128)">align</span> <span style="color:rgb(9,134,88)">4</span>, !<span style="color:rgb(0,128,128)">tbaa</span> !<span style="color:rgb(9,134,88)">3</span></div><div>  <span style="color:rgb(205,49,49)">%</span><span style="color:rgb(0,0,255)">a</span> <span style="color:rgb(205,49,49)">=</span> <span style="color:rgb(0,128,128)">getelementptr</span> <span style="color:rgb(0,128,128)">inbounds</span> <span style="color:rgb(0,128,128)">%struct.child</span>, <span style="color:rgb(0,128,128)">%struct.child</span>* <span style="color:rgb(0,128,128)">%v</span>, <span style="color:rgb(0,128,128)">i64</span> <span style="color:rgb(9,134,88)">0</span>, <span style="color:rgb(0,128,128)">i32</span> <span style="color:rgb(9,134,88)">0</span>, <span style="color:rgb(0,128,128)">i32</span> <span style="color:rgb(9,134,88)">0</span></div><div>  <span style="color:rgb(205,49,49)">%1</span> <span style="color:rgb(205,49,49)">=</span> <span style="color:rgb(0,0,255)">load</span> <span style="color:rgb(0,128,128)">i32</span>, <span style="color:rgb(0,128,128)">i32</span>* <span style="color:rgb(0,128,128)">%a</span>, <span style="color:rgb(0,128,128)">align</span> <span style="color:rgb(9,134,88)">4</span>, !<span style="color:rgb(0,128,128)">tbaa</span> !<span style="color:rgb(9,134,88)">8</span></div><div>  <span style="color:rgb(205,49,49)">%</span><span style="color:rgb(0,0,255)">add</span> <span style="color:rgb(205,49,49)">=</span> <span style="color:rgb(0,128,128)">add</span> <span style="color:rgb(0,128,128)">nsw</span> <span style="color:rgb(0,128,128)">i32</span> <span style="color:rgb(205,49,49)">%</span><span style="color:rgb(9,134,88)">1</span>, <span style="color:rgb(205,49,49)">%</span><span style="color:rgb(9,134,88)">0</span></div><div>  <span style="color:rgb(0,0,255)">ret</span> <span style="color:rgb(0,128,128)">i32</span> <span style="color:rgb(0,128,128)">%add</span></div><div>}</div></div></div><div><br></div><div>As you can see, only the access of the x field is volatile, not the a field (from the parent). As this is clearly different from what would happen to "const" modifier, I'm curious to know if that is standard? Maybe the standard is unspecified?</div><div><br></div><div>Here is a godbolt link to this example on trunk: <a href="https://godbolt.org/z/aT51sb4Pv">https://godbolt.org/z/aT51sb4Pv</a></div><div><br></div><div>Best regards!</div><div><br></div>-- <br><div dir="ltr" class="gmail_signature" data-smartmail="gmail_signature"><div dir="ltr"><b>Alexandre Isoard</b><br></div></div></div></div>