<div dir="ltr"><div>Hi Akira,</div><div><br></div>I have applied this patch in my workspace to add the attributes to function prototypes.<div><div><div>---------------------------------------------------------------------------------------------------------------------</div><div>--- basecode/cfe-3.5.0.src/lib/CodeGen/CodeGenModule.cpp</div><div>+++ myworkspace/cfe-3.5.0.src/lib/CodeGen/CodeGenModule.cpp</div><div>@@ -834,6 +834,10 @@</div><div> if (!IsIncompleteFunction)</div><div> SetLLVMFunctionAttributes(FD, getTypes().arrangeGlobalDeclaration(GD), F);</div><div><br></div><div>+ // add target specific attributes to function declaration/prototype as well</div><div>+ if (!FD->isDefined())</div><div>+ getTargetCodeGenInfo().SetTargetAttributes(FD, F, *this);</div><div>+</div><div> // Add the Returned attribute for "this", except for iOS 5 and earlier</div><div> // where substantial code, including the libstdc++ dylib, was compiled with</div><div> // GCC and does not actually return "this".</div></div><div>---------------------------------------------------------------------------------------------------------------------<br></div><div><br></div><div>This code comes in 'SetFunctionAttributes' which is common to both function definition and declaration. (target attributes for all global definitions - both functions and variables, are handled already at 'setNonAliasAttributes')<br></div><div><br></div><div>Thanks,</div><div>Christu<br></div></div></div><div class="gmail_extra"><br><div class="gmail_quote">On Fri, Sep 9, 2016 at 10:30 AM, Akira Hatanaka <span dir="ltr"><<a href="mailto:ahatanaka@apple.com" target="_blank">ahatanaka@apple.com</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 Sep 7, 2016, at 9:08 PM, Christudasan D via cfe-dev <<a href="mailto:cfe-dev@lists.llvm.org" target="_blank">cfe-dev@lists.llvm.org</a>> wrote:</div><br><div><div dir="ltr"><p class="MsoNormal" style="font-size:12.8px">All,</p><p class="MsoNormal" style="font-size:12.8px"><br></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></p><p class="MsoNormal" style="font-size:12.8px">Here is my observation.</p><div style="font-size:12.8px"> <br></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"> <br></div><p class="MsoNormal" style="font-size:12.8px">This virtual function (given below) invocation in clang/lib/CodeGen/CodeGenModul<wbr>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>etAttributes(D, GO, *this)</p><div style="font-size:12.8px"> <br></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"> <br></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"> <br></div><p class="MsoNormal" style="font-size:12.8px"><b><u>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)">__attribute__</span><span>((</span><b><span style="color:rgb(127,0,85)">const</span></b><span>)), 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"> <br></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>F.hasFnAttribute("XYZ")</b>, where XYZ is the new attribute name.</p><div style="font-size:12.8px"> <br></div><div>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>'const',</b> for example) are actually propagated to the backend through function prototype.</div><div><br></div><div>In the sample program below I have declared function <b>foo</b> with attribute '<b>const'</b> and you can see '<b>readnone'</b> is added to the attribute list.</div><div><br></div><div><b><u>Sample Program:</u></b></div><div><br></div><div>extern void __attribute__((const)) foo (int);</div><div><br></div><div>int main ()</div><div>{</div><div><span style="white-space:pre-wrap"> </span>int my_var = 45;</div><div><br></div><div><span style="white-space:pre-wrap"> </span>foo(my_var);</div><div><span style="white-space:pre-wrap"> </span>return 0;</div><div>}</div><div>------------------------------<wbr>------------------------------<wbr>-----</div><div><b><u>Generated IR:</u></b></div><div><br></div><div>; Function Attrs: nounwind</div><div>define i16 @main() #0 {</div><div> %1 = alloca i16, align 2</div><div> %my_var = alloca i16, align 2</div><div> store i16 0, i16* %1</div><div> call void @llvm.dbg.declare(metadata !{i16* %my_var}, metadata !13), !dbg !14</div><div> store i16 45, i16* %my_var, align 2, !dbg !15</div><div> %2 = load i16* %my_var, align 2, !dbg !16</div><div> call void @foo(i16 %2) #1, !dbg !16</div><div> ret i16 0, !dbg !17</div><div>}</div><div><br></div><div>; Function Attrs: nounwind readnone</div><div>declare void @llvm.dbg.declare(metadata, metadata) #1</div><div><br></div><div>; Function Attrs: nounwind <b style="background-color:rgb(241,194,50)">readnone</b></div><div>declare void @foo(i16) #2</div><div>------------------------------<wbr>------------------------------<wbr>------------------------------<wbr>--------------------------</div><p class="MsoNormal" style="font-size:12.8px"><br></p></div></div></blockquote></div></div><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"><div><span class=""><div dir="ltr"><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"> <br></div><p class="MsoNormal" style="font-size:12.8px">Thanks,</p><p class="MsoNormal" style="font-size:12.8px">Christu</p></div></span>
______________________________<wbr>_________________<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="http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev" target="_blank">http://lists.llvm.org/cgi-bin/<wbr>mailman/listinfo/cfe-dev</a><br></div></blockquote></div><br></div></blockquote></div><br></div>