<html><head><meta http-equiv="Content-Type" content="text/html charset=utf-8"></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class=""><br class=""><div><blockquote type="cite" class=""><div class="">On Sep 7, 2016, at 9:08 PM, Christudasan D via cfe-dev <<a href="mailto:cfe-dev@lists.llvm.org" class="">cfe-dev@lists.llvm.org</a>> wrote:</div><br class="Apple-interchange-newline"><div class=""><div dir="ltr" class=""><p class="MsoNormal" style="font-size:12.8px">All,</p><p class="MsoNormal" style="font-size:12.8px"><br class=""></p><p class="MsoNormal" style="font-size:12.8px">I am facing a problem with LLVM 3.5 Compiler and suspect it is a bug in the clang. I earlier posted this query to llvm-dev list. </p><p class="MsoNormal" style="font-size:12.8px"><br class=""></p><p class="MsoNormal" style="font-size:12.8px">Here is my observation.</p><div style="font-size: 12.8px;" class=""> <br class="webkit-block-placeholder"></div><p class="MsoNormal" style="font-size:12.8px">I am trying to support a target specific function attribute for a particular target machine.</p><p class="MsoNormal" style="font-size:12.8px">Having followed the steps specified in the compiler documentation, I see the attribute is passed to the backend if we include the attribute in the function definition. But this attribute is not propagate to the backend if I include it only in the function’s prototype/declaration (in the C program).</p><div style="font-size: 12.8px;" class=""> <br class="webkit-block-placeholder"></div><p class="MsoNormal" style="font-size:12.8px">This virtual function (given below) invocation in clang/lib/CodeGen/CodeGenModul<wbr class="">e.cpp in clang is responsible for attaching the target specific attributes to the backend CodeGen objects (functions, variables etc.)</p><p class="MsoNormal" style="font-size:12.8px;text-indent:0.5in">getTargetCodeGenInfo().SetTarg<wbr class="">etAttributes(D, GO, *this)</p><div style="font-size: 12.8px;" class=""> <br class="webkit-block-placeholder"></div><p class="MsoNormal" style="font-size:12.8px">I found that this virtual function is getting invoked only for function definitions and not for function declarations.</p><div style="font-size: 12.8px;" class=""> <br class="webkit-block-placeholder"></div><p class="MsoNormal" style="font-size:12.8px">Actual requirement:</p><p class="MsoNormal" style="font-size:12.8px">Along with regular C functions we have special functions supported in the target and its definition and invocation will always come in different compilation units. There must be a way to recognize these special functions at the call-sites during codegen in the backend. By attaching some info in the function prototype, we can recognize these special functions at the call-site during backend codegen. That’s why I thought of a new target specific attribute to achieve this purpose.</p><div style="font-size: 12.8px;" class=""> <br class="webkit-block-placeholder"></div><p class="MsoNormal" style="font-size:12.8px"><b class=""><u class="">Is it really a bug?</u></b></p><p class="MsoNormal" style="font-size:12.8px">When I compile a program having function declarations/prototypes with generic attributes like <span style="color:rgb(127,0,85)" class="">__attribute__</span><span style="" class="">((</span><b class=""><span style="color:rgb(127,0,85)" class="">const</span></b><span style="" class="">)), clang will add it to that function’s attributelist in the IR and it will be available in the backend.</span></p><div style="font-size: 12.8px;" class=""> <br class="webkit-block-placeholder"></div><p class="MsoNormal" style="font-size:12.8px">Similarly I expected the target specific attribute should also be added to the attributelist available in the backend and I should be able to identify it using <b class="">F.hasFnAttribute("XYZ")</b>, where XYZ is the new attribute name.</p><div style="font-size: 12.8px;" class=""> <br class="webkit-block-placeholder"></div><div class="">I understand that a look-up in the prototype for function-attrs is very rare at codegen. But I was keen to ask this when I found that the standard attributes (<b class="">'const',</b> for example) are actually propagated to the backend through function prototype.</div><div class=""><br class=""></div><div class="">In the sample program below I have declared function <b class="">foo</b> with attribute '<b class="">const'</b> and you can see '<b class="">readnone'</b> is added to the attribute list.</div><div class=""><br class=""></div><div class=""><b class=""><u class="">Sample Program:</u></b></div><div class=""><br class=""></div><div class="">extern void __attribute__((const)) foo (int);</div><div class=""><br class=""></div><div class="">int main ()</div><div class="">{</div><div class=""><span class="gmail-Apple-tab-span" style="white-space:pre">      </span>int  my_var = 45;</div><div class=""><br class=""></div><div class=""><span class="gmail-Apple-tab-span" style="white-space:pre">       </span>foo(my_var);</div><div class=""><span class="gmail-Apple-tab-span" style="white-space:pre">        </span>return 0;</div><div class="">}</div><div class="">-----------------------------------------------------------------</div><div class=""><b class=""><u class="">Generated IR:</u></b></div><div class=""><br class=""></div><div class="">; Function Attrs: nounwind</div><div class="">define i16 @main() #0 {</div><div class="">  %1 = alloca i16, align 2</div><div class="">  %my_var = alloca i16, align 2</div><div class="">  store i16 0, i16* %1</div><div class="">  call void @llvm.dbg.declare(metadata !{i16* %my_var}, metadata !13), !dbg !14</div><div class="">  store i16 45, i16* %my_var, align 2, !dbg !15</div><div class="">  %2 = load i16* %my_var, align 2, !dbg !16</div><div class="">  call void @foo(i16 %2) #1, !dbg !16</div><div class="">  ret i16 0, !dbg !17</div><div class="">}</div><div class=""><br class=""></div><div class="">; Function Attrs: nounwind readnone</div><div class="">declare void @llvm.dbg.declare(metadata, metadata) #1</div><div class=""><br class=""></div><div class="">; Function Attrs: nounwind <b style="background-color:rgb(241,194,50)" class="">readnone</b></div><div class="">declare void @foo(i16) #2</div><div class="">--------------------------------------------------------------------------------------------------------------------</div><p class="MsoNormal" style="font-size:12.8px"><br class=""></p></div></div></blockquote><div>I think you are right: SetTargetAttributes doesn’t get called for function declarations. GetOrCreateLLVMFunction is called to generate the LLVM IR for a function declaration, which  eventually calls ConstructAttributeList, in which readnone is added. If you want to add target attributes to function declarations, I believe you’ll have to somehow call SetTargetAttributes in ConstructAttributeList.</div><blockquote type="cite" class=""><div class=""><div dir="ltr" class=""><p class="MsoNormal" style="font-size:12.8px">Since I work with LLVM 3.5 code-base, I am not sure whether this issue (if it is really a bug) has already been fixed with recent compiler releases.</p><p class="MsoNormal" style="font-size:12.8px">Please write to me.</p><div style="font-size: 12.8px;" class=""> <br class="webkit-block-placeholder"></div><p class="MsoNormal" style="font-size:12.8px">Thanks,</p><p class="MsoNormal" style="font-size:12.8px">Christu</p></div>
_______________________________________________<br class="">cfe-dev mailing list<br class=""><a href="mailto:cfe-dev@lists.llvm.org" class="">cfe-dev@lists.llvm.org</a><br class="">http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev<br class=""></div></blockquote></div><br class=""></body></html>