<div dir="ltr">It seems that there's the same issue with following code:<div><br></div><div>**************************************************************<br><div><div style="color:rgb(0,0,0);background-color:rgb(255,255,254)"><div><font face="verdana, sans-serif"><span style="color:rgb(0,0,255)">template</span><<span style="color:rgb(0,0,255)">typename</span> T></font></div><div><font face="verdana, sans-serif"><span style="color:rgb(0,0,255)">class</span> complex{</font></div><div><font face="verdana, sans-serif"><span style="color:rgb(0,0,255)">public</span>:</font></div><div><font face="verdana, sans-serif">  T x;</font></div><div><font face="verdana, sans-serif">  T y;</font></div><div>  <font face="verdana, sans-serif"><span style="color:rgb(0,0,255)">void</span> add( complex b) {</font></div><div><font face="verdana, sans-serif">    x = x + b.x;</font></div><div><font face="verdana, sans-serif">    y = y + b.y;</font></div><div><font face="verdana, sans-serif">  }</font></div><div><font face="verdana, sans-serif">};</font></div><font face="verdana, sans-serif"><br></font><div><font face="verdana, sans-serif"><span style="color:rgb(0,0,255)">typedef</span> complex<<span style="color:rgb(0,0,255)">float</span>> AA;</font></div><font face="verdana, sans-serif"><br></font><div><font face="verdana, sans-serif"><span style="color:rgb(0,0,255)">float</span> sub(AA b[<span style="color:rgb(9,136,90)">10</span>][<span style="color:rgb(9,136,90)">5</span>]) {</font></div><div>  <font face="verdana, sans-serif"><span style="color:rgb(0,0,255)">return</span> <span style="color:rgb(9,136,90)">0</span>;</font></div><div><font face="verdana, sans-serif">}</font></div><font face="verdana, sans-serif"><br></font><div><font face="verdana, sans-serif"><span style="color:rgb(0,0,255)">float</span> dut( AA a[<span style="color:rgb(9,136,90)">10</span>][<span style="color:rgb(9,136,90)">10</span>][<span style="color:rgb(9,136,90)">5</span>] ) {</font></div><font face="verdana, sans-serif"><br></font><div>  <font face="verdana, sans-serif"><span style="color:rgb(0,0,255)">return</span> sub(a[<span style="color:rgb(9,136,90)">3</span>]);;</font></div><div><font face="verdana, sans-serif">}</font></div></div></div><div>**************************************************************  <font face="verdana, sans-serif"><br></font></div><div><br></div><div><font face="arial, sans-serif">and clang will generate following code:</font></div><div><font face="arial, sans-serif"><br></font></div><div>**************************************************************  <font face="arial, sans-serif"><br></font></div><div><div style="background-color:rgb(255,255,254)"><div style="color:rgb(0,0,0)"><font face="verdana, sans-serif"><span style="color:rgb(205,49,49)">%</span><span style="color:rgb(0,0,255)">class.complex</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)">float</span>, <span style="color:rgb(0,128,128)">float</span> }</font></div><font face="verdana, sans-serif" style="color:rgb(0,0,0)"><br></font><div style=""><font face="verdana, sans-serif" style=""><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)">float</span> <span style="color:rgb(0,128,128)">@_Z3subPA5_7complexIfE</span><font color="#000000">(</font><b style=""><font color="#ff0000">[5 x i8]*</font></b><font color="#000000">) </font><span style="color:rgb(9,136,90)">#0</span><font color="#000000"> {</font></font></div><div style="color:rgb(0,0,0)">  <font face="verdana, sans-serif"><span style="color:rgb(205,49,49)">%2</span> <span style="color:rgb(205,49,49)">=</span> <span style="color:rgb(0,0,255)">alloca</span> [<span style="color:rgb(9,136,90)">5</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)">align</span> <span style="color:rgb(9,136,90)">8</span></font></div><div style="color:rgb(0,0,0)">  <font face="verdana, sans-serif"><span style="color:rgb(0,0,255)">store</span> [<span style="color:rgb(9,136,90)">5</span> <span style="color:rgb(0,128,128)">x</span> <span style="color:rgb(0,128,128)">i8</span>]* <span style="color:rgb(205,49,49)">%</span><span style="color:rgb(9,136,90)">0</span>, [<span style="color:rgb(9,136,90)">5</span> <span style="color:rgb(0,128,128)">x</span> <span style="color:rgb(0,128,128)">i8</span>]** <span style="color:rgb(205,49,49)">%</span><span style="color:rgb(9,136,90)">2</span>, <span style="color:rgb(0,128,128)">align</span> <span style="color:rgb(9,136,90)">8</span></font></div><div style="color:rgb(0,0,0)">  <font face="verdana, sans-serif"><span style="color:rgb(0,0,255)">ret</span> <span style="color:rgb(0,128,128)">float</span> <span style="color:rgb(9,136,90)">0.000000e+00</span></font></div><div style="color:rgb(0,0,0)"><font face="verdana, sans-serif">}</font></div><font face="verdana, sans-serif" style="color:rgb(0,0,0)"><br></font><div style="color:rgb(0,0,0)"><font face="verdana, sans-serif"><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)">float</span> <span style="color:rgb(0,128,128)">@_Z3dutPA10_A5_7complexIfE</span>([<span style="color:rgb(9,136,90)">10</span> <span style="color:rgb(0,128,128)">x</span> [<span style="color:rgb(9,136,90)">5</span> <span style="color:rgb(0,128,128)">x</span> <span style="color:rgb(0,128,128)">%class.complex</span>]]*) <span style="color:rgb(9,136,90)">#0</span> {</font></div><div style="color:rgb(0,0,0)">  <font face="verdana, sans-serif"><span style="color:rgb(205,49,49)">%2</span> <span style="color:rgb(205,49,49)">=</span> <span style="color:rgb(0,0,255)">alloca</span> [<span style="color:rgb(9,136,90)">10</span> <span style="color:rgb(0,128,128)">x</span> [<span style="color:rgb(9,136,90)">5</span> <span style="color:rgb(0,128,128)">x</span> <span style="color:rgb(0,128,128)">%class.complex</span>]]*, <span style="color:rgb(0,128,128)">align</span> <span style="color:rgb(9,136,90)">8</span></font></div><div style="color:rgb(0,0,0)">  <font face="verdana, sans-serif"><span style="color:rgb(205,49,49)">%3</span> <span style="color:rgb(205,49,49)">=</span> <span style="color:rgb(0,0,255)">alloca</span> [<span style="color:rgb(9,136,90)">5</span> <span style="color:rgb(0,128,128)">x</span> <span style="color:rgb(0,128,128)">%class.complex</span>]*, <span style="color:rgb(0,128,128)">align</span> <span style="color:rgb(9,136,90)">8</span></font></div><div style="color:rgb(0,0,0)">  <font face="verdana, sans-serif"><span style="color:rgb(0,0,255)">store</span> [<span style="color:rgb(9,136,90)">10</span> <span style="color:rgb(0,128,128)">x</span> [<span style="color:rgb(9,136,90)">5</span> <span style="color:rgb(0,128,128)">x</span> <span style="color:rgb(0,128,128)">%class.complex</span>]]* <span style="color:rgb(205,49,49)">%</span><span style="color:rgb(9,136,90)">0</span>, [<span style="color:rgb(9,136,90)">10</span> <span style="color:rgb(0,128,128)">x</span> [<span style="color:rgb(9,136,90)">5</span> <span style="color:rgb(0,128,128)">x</span> <span style="color:rgb(0,128,128)">%class.complex</span>]]** <span style="color:rgb(205,49,49)">%</span><span style="color:rgb(9,136,90)">2</span>, <span style="color:rgb(0,128,128)">align</span> <span style="color:rgb(9,136,90)">8</span></font></div><div style="color:rgb(0,0,0)">  <font face="verdana, sans-serif"><span style="color:rgb(205,49,49)">%4</span> <span style="color:rgb(205,49,49)">=</span> <span style="color:rgb(0,0,255)">load</span> [<span style="color:rgb(9,136,90)">10</span> <span style="color:rgb(0,128,128)">x</span> [<span style="color:rgb(9,136,90)">5</span> <span style="color:rgb(0,128,128)">x</span> <span style="color:rgb(0,128,128)">%class.complex</span>]]*, [<span style="color:rgb(9,136,90)">10</span> <span style="color:rgb(0,128,128)">x</span> [<span style="color:rgb(9,136,90)">5</span> <span style="color:rgb(0,128,128)">x</span> <span style="color:rgb(0,128,128)">%class.complex</span>]]** <span style="color:rgb(205,49,49)">%</span><span style="color:rgb(9,136,90)">2</span>, <span style="color:rgb(0,128,128)">align</span> <span style="color:rgb(9,136,90)">8</span></font></div><div style="color:rgb(0,0,0)">  <font face="verdana, sans-serif"><span style="color:rgb(205,49,49)">%5</span> <span style="color:rgb(205,49,49)">=</span> <span style="color:rgb(0,0,255)">getelementptr</span> <span style="color:rgb(0,128,128)">inbounds</span> [<span style="color:rgb(9,136,90)">10</span> <span style="color:rgb(0,128,128)">x</span> [<span style="color:rgb(9,136,90)">5</span> <span style="color:rgb(0,128,128)">x</span> <span style="color:rgb(0,128,128)">%class.complex</span>]], [<span style="color:rgb(9,136,90)">10</span> <span style="color:rgb(0,128,128)">x</span> [<span style="color:rgb(9,136,90)">5</span> <span style="color:rgb(0,128,128)">x</span> <span style="color:rgb(0,128,128)">%class.complex</span>]]* <span style="color:rgb(205,49,49)">%</span><span style="color:rgb(9,136,90)">4</span>, <span style="color:rgb(0,128,128)">i64</span> <span style="color:rgb(9,136,90)">3</span></font></div><div style="color:rgb(0,0,0)">  <font face="verdana, sans-serif"><span style="color:rgb(205,49,49)">%6</span> <span style="color:rgb(205,49,49)">=</span> <span style="color:rgb(0,0,255)">getelementptr</span> <span style="color:rgb(0,128,128)">inbounds</span> [<span style="color:rgb(9,136,90)">10</span> <span style="color:rgb(0,128,128)">x</span> [<span style="color:rgb(9,136,90)">5</span> <span style="color:rgb(0,128,128)">x</span> <span style="color:rgb(0,128,128)">%class.complex</span>]], [<span style="color:rgb(9,136,90)">10</span> <span style="color:rgb(0,128,128)">x</span> [<span style="color:rgb(9,136,90)">5</span> <span style="color:rgb(0,128,128)">x</span> <span style="color:rgb(0,128,128)">%class.complex</span>]]* <span style="color:rgb(205,49,49)">%</span><span style="color:rgb(9,136,90)">5</span>, <span style="color:rgb(0,128,128)">i32</span> <span style="color:rgb(9,136,90)">0</span>, <span style="color:rgb(0,128,128)">i32</span> <span style="color:rgb(9,136,90)">0</span></font></div><div style="color:rgb(0,0,0)">  <font face="verdana, sans-serif"><span style="color:rgb(0,0,255)">store</span> [<span style="color:rgb(9,136,90)">5</span> <span style="color:rgb(0,128,128)">x</span> <span style="color:rgb(0,128,128)">%class.complex</span>]* <span style="color:rgb(205,49,49)">%</span><span style="color:rgb(9,136,90)">6</span>, [<span style="color:rgb(9,136,90)">5</span> <span style="color:rgb(0,128,128)">x</span> <span style="color:rgb(0,128,128)">%class.complex</span>]** <span style="color:rgb(205,49,49)">%</span><span style="color:rgb(9,136,90)">3</span>, <span style="color:rgb(0,128,128)">align</span> <span style="color:rgb(9,136,90)">8</span></font></div><div style="color:rgb(0,0,0)">  <font face="verdana, sans-serif"><span style="color:rgb(205,49,49)">%7</span> <span style="color:rgb(205,49,49)">=</span> <span style="color:rgb(0,0,255)">load</span> [<span style="color:rgb(9,136,90)">5</span> <span style="color:rgb(0,128,128)">x</span> <span style="color:rgb(0,128,128)">%class.complex</span>]*, [<span style="color:rgb(9,136,90)">5</span> <span style="color:rgb(0,128,128)">x</span> <span style="color:rgb(0,128,128)">%class.complex</span>]** <span style="color:rgb(205,49,49)">%</span><span style="color:rgb(9,136,90)">3</span>, <span style="color:rgb(0,128,128)">align</span> <span style="color:rgb(9,136,90)">8</span></font></div><div style="color:rgb(0,0,0)">  <font face="verdana, sans-serif"><span style="color:rgb(205,49,49)">%8</span> <span style="color:rgb(205,49,49)">=</span> <span style="color:rgb(0,0,255)">bitcast</span> [<span style="color:rgb(9,136,90)">5</span> <span style="color:rgb(0,128,128)">x</span> <span style="color:rgb(0,128,128)">%class.complex</span>]* <span style="color:rgb(205,49,49)">%</span><span style="color:rgb(9,136,90)">7</span> <span style="color:rgb(0,128,128)">to</span> [<span style="color:rgb(9,136,90)">5</span> <span style="color:rgb(0,128,128)">x</span> <span style="color:rgb(0,128,128)">i8</span>]*</font></div><div style="">  <font face="verdana, sans-serif" style=""><span style="color:rgb(205,49,49)">%9</span> <span style="color:rgb(205,49,49)">=</span> <span style="color:rgb(0,0,255)">call</span> <span style="color:rgb(0,128,128)">float</span> <span style="color:rgb(0,128,128)">@_Z3subPA5_7complexIfE</span><font color="#000000">(</font><font color="#ff0000"><b>[5 x i8]* %8</b></font><font color="#000000">)</font></font></div><div style="color:rgb(0,0,0)">  <font face="verdana, sans-serif"><span style="color:rgb(0,0,255)">ret</span> <span style="color:rgb(0,128,128)">float</span> <span style="color:rgb(205,49,49)">%</span><span style="color:rgb(9,136,90)">9</span></font></div><div style="color:rgb(0,0,0)"><font face="verdana, sans-serif">}</font></div><div style="color:rgb(0,0,0)"><span style="color:rgb(34,34,34);background-color:rgb(255,255,255)">**************************************************************</span>  <font face="verdana, sans-serif"><br></font></div></div></div><div><font face="verdana, sans-serif">If we use '-ast-print', and compile the '-ast-print' output again, then it will generate the complete type, like:</font></div></div><div><br></div><div>************************************************************** </div><font face="verdana, sans-serif">%class.complex = type { float, float }<br><br>; Function Attrs: noinline nounwind optnone uwtable<br>define float @_Z3subPA5_7complexIfE(<b><font color="#93c47d">[5 x %class.complex]* %b</font></b>) #0 {<br>entry:<br>  %b.addr = alloca [5 x %class.complex]*, align 8<br>  store [5 x %class.complex]* %b, [5 x %class.complex]** %b.addr, align 8<br>  ret float 0.000000e+00<br>}<br><br>; Function Attrs: noinline nounwind optnone uwtable<br>define float @_Z3dutPA10_A5_7complexIfE([10 x [5 x %class.complex]]* %a) #0 {<br>entry:<br>  %a.addr = alloca [10 x [5 x %class.complex]]*, align 8<br>  store [10 x [5 x %class.complex]]* %a, [10 x [5 x %class.complex]]** %a.addr, align 8<br>  %0 = load [10 x [5 x %class.complex]]*, [10 x [5 x %class.complex]]** %a.addr, align 8<br>  %arrayidx = getelementptr inbounds [10 x [5 x %class.complex]], [10 x [5 x %class.complex]]* %0, i64 3<br>  %arraydecay = getelementptr inbounds [10 x [5 x %class.complex]], [10 x [5 x %class.complex]]* %arrayidx, i32 0, i32 0<br>  %call = call float @_Z3subPA5_7complexIfE(<b style=""><font color="#93c47d">[5 x %class.complex]* %arraydecay</font></b>)<br>  ret float %call<br>} </font><div>**************************************************************  <br></div><div><br></div><div>Are we confident it does not introduce issues to do the CodeGen early?</div><div>If we had an option to do the codeGen after complete AST build, we could at least detect this kind of difference at testing?</div><div><br></div><div>Thanks,</div><div>Fangqing</div></div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">Alexandre Isoard via cfe-dev <<a href="mailto:cfe-dev@lists.llvm.org">cfe-dev@lists.llvm.org</a>> 于2019年6月25日周二 下午7:19写道:<br></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">Hello,<div><br></div><div>We are having issues with modifying the Clang AST while it is being built, and we suspect they are due to it being code generated before it is fully populated, and <b>we were wondering if it was possible to require the Sema to be fully finished before doing any CodeGen?</b></div><div><br></div><div>An example would be when we process custom pragma:</div><div><br></div><blockquote style="margin:0px 0px 0px 40px;border:none;padding:0px"><div>int my_global;</div><div><br></div><div>int my_function() {</div><div>    my_global = 1;</div><div>}</div><div><br></div><div>int main() {</div>#pragma FOO SOMETHING var=my_global<div>    my_function();</div><div>    return 0;</div><div>}</div></blockquote><div><br></div><div>Here the way I process the pragma is that it attach to the AST node for my_global, an attribute node. So that once I code-generate that global variable I can "do something" based on that attribute. Unfortunately, today Clang might codegen that global as soon as it deem it necessary (typically, when code-generating my_function as it uses it). And the pragma has not yet been semantically parsed yet.</div><div><br></div><div>If I were to use -ast-print and dump the result into a file, then the attribute would be there, and if I parse that file again I would get the correct codegen. But I assume it is unsafe to rely on being able to parse -ast-print and/or it's AST to be the same.</div><div><br></div><div>Any input on that issue?</div><div><div><br></div>-- <br><div dir="ltr" class="gmail-m_-7887075756064839658gmail_signature"><div dir="ltr"><b>Alexandre Isoard</b><br></div></div></div></div>
_______________________________________________<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>