<div dir="ltr">Definitely a bug.<div><br></div><div>Looks like a bug in the combination of block addresses, constexpr, and lambda functions. Note that the "labels" global value gets duplicated -- one for the function it's defined in, and then a copy for the inner lambda function. The copy for the lambda got the block references broken, by attempting to change the function the block is defined in, to the lambda. Obviously, those blocks don't actually exist within the lambda function, so that's busted!<div><div style="color:rgb(0,0,0);background-color:rgb(255,255,254)"><div><span style="color:rgb(0,128,128)"><br></span></div><div><span style="color:rgb(0,128,128)">@__const._Z22run_with_computed_gotoPK8bytecode.labels =</span> <span style="color:rgb(0,0,255)">private</span> <span style="color:rgb(0,128,128)">unnamed_addr</span> <span style="color:rgb(0,128,128)">constant</span> [<span style="color:rgb(9,136,90)">3</span> <span style="color:rgb(0,128,128)">x</span> <span style="color:rgb(0,128,128)">i8</span>*] [<span style="color:rgb(0,128,128)">i8</span>* <span style="color:rgb(0,128,128)">blockaddress</span>(<span style="color:rgb(0,128,128)">@_Z22run_with_computed_gotoPK8bytecode</span>, <span style="color:rgb(205,49,49)">%</span><span style="color:rgb(9,136,90)">9</span>), <span style="color:rgb(0,128,128)">i8</span>* <span style="color:rgb(0,128,128)">blockaddress</span>(<span style="color:rgb(0,128,128)">@_Z22run_with_computed_gotoPK8bytecode</span>, <span style="color:rgb(205,49,49)">%</span><span style="color:rgb(9,136,90)">13</span>), <span style="color:rgb(0,128,128)">i8</span>* <span style="color:rgb(0,128,128)">blockaddress</span>(<span style="color:rgb(0,128,128)">@_Z22run_with_computed_gotoPK8bytecode</span>, <span style="color:rgb(205,49,49)">%</span><span style="color:rgb(9,136,90)">17</span>)], <span style="color:rgb(0,128,128)">align</span> <span style="color:rgb(9,136,90)">16</span></div><div><span style="color:rgb(0,128,128)">@__const._Z22run_with_computed_gotoPK8bytecode.labels.1 =</span> <span style="color:rgb(0,0,255)">private</span> <span style="color:rgb(0,128,128)">unnamed_addr</span> <span style="color:rgb(0,128,128)">constant</span> [<span style="color:rgb(9,136,90)">3</span> <span style="color:rgb(0,128,128)">x</span> <span style="color:rgb(0,128,128)">i8</span>*] [<span style="color:rgb(0,128,128)">i8</span>* <span style="color:rgb(0,128,128)">blockaddress</span>(<span style="color:rgb(0,128,128)">@</span><span style="color:rgb(163,21,21)">"_ZZ22run_with_computed_gotoPK8bytecodeENK3$_0clEv"</span>, <<span style="color:rgb(0,128,128)">badref</span>>), <span style="color:rgb(0,128,128)">i8</span>* <span style="color:rgb(0,128,128)">blockaddress</span>(<span style="color:rgb(0,128,128)">@</span><span style="color:rgb(163,21,21)">"_ZZ22run_with_computed_gotoPK8bytecodeENK3$_0clEv"</span>, <<span style="color:rgb(0,128,128)">badref</span>>), <span style="color:rgb(0,128,128)">i8</span>* <span style="color:rgb(0,128,128)">blockaddress</span>(<span style="color:rgb(0,128,128)">@</span><span style="color:rgb(163,21,21)">"_ZZ22run_with_computed_gotoPK8bytecodeENK3$_0clEv"</span>, <<span style="color:rgb(0,128,128)">badref</span>>)], <span style="color:rgb(0,128,128)">align</span> <span style="color:rgb(9,136,90)">16</span></div></div></div><div style="color:rgb(0,0,0);background-color:rgb(255,255,254)"><br></div><div style="color:rgb(0,0,0);background-color:rgb(255,255,254)">If you want a workaround, I suggest not using the lambda. Changing it to e.g.</div><div style="background-color:rgb(255,255,254)"><div style="color:rgb(0,0,0)"><div><span style="color:rgb(0,0,255)"> #define</span> next() labels[*instructions++]</div></div></div><div style="color:rgb(0,0,0);background-color:rgb(255,255,254)">should be fine.</div></div></div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Thu, Oct 3, 2019 at 8:48 AM De Azevedo Piovezan, Felipe via cfe-dev <<a href="mailto:cfe-dev@lists.llvm.org">cfe-dev@lists.llvm.org</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">Hello cfe,<br>
<br>
This year at CppCon (<a href="https://www.youtube.com/watch?v=9cPU1NdsgDQ" rel="noreferrer" target="_blank">https://www.youtube.com/watch?v=9cPU1NdsgDQ</a>), there was a funny talk about the use of what they called a "computed goto".<br>
I was playing around with the idea, and there might be something incorrect about the code generated by clang when constexpr is involved (starting with 9.0).<br>
<br>
The idea is this: the address of a bunch of labels are taken and stored into a table:<br>
<br>
enum bytecode : int8_t { add1, add2, halt };<br>
<br>
constexpr void* labels[] = {<br>
[bytecode::add1] = &&add1_label,<br>
[bytecode::add2] = &&add2_label,<br>
[bytecode::halt] = &&halt_label,<br>
};<br>
<br>
//labels defined here...<br>
<br>
However, this is the IR generated with -O0 -emit-llvm:<br>
<br>
@labels = private unnamed_addr constant [3 x i8*] [i8* blockaddress(@"label1", <badref>), i8* blockaddress(@"label2", <badref>), i8* blockaddress(@"label3", <badref>)], align 16<br>
<br>
Note the badrefs! Also, we generate a basic block with no predecessors:<br>
<br>
12: ; No predecessors!<br>
indirectbr i8* undef, [label <badref>, label <badref>, label <badref>]<br>
<br>
As such, as soon as we start optimizing, the whole function is optimized away.<br>
If we remove constexpr, however, everything seems sane. Godbolt link: <a href="https://godbolt.org/z/Anc9EI" rel="noreferrer" target="_blank">https://godbolt.org/z/Anc9EI</a><br>
<br>
Any insights on what might be happening here? I suspect a lot of people will play around with this construction just "for fun" and will encounter this.<br>
<br>
--<br>
Felipe<br>
<br>
_______________________________________________<br>
cfe-dev mailing list<br>
<a href="mailto:cfe-dev@lists.llvm.org" target="_blank">cfe-dev@lists.llvm.org</a><br>
<a href="https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev" rel="noreferrer" target="_blank">https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev</a><br>
</blockquote></div>