<div dir="ltr">It seems that we do not at least set IsUsedInRegularObj for common symbols passed to LTO.</div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Tue, Jun 11, 2019 at 10:53 PM Teresa Johnson <<a href="mailto:tejohnson@google.com">tejohnson@google.com</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"><div dir="ltr">LTO can, but it is linker driven. I confirmed that when it is a common symbol the resolution indicates that the symbol is exported, and when I add an initializer so that it is a def we no longer think it is exported and are able to internalize. So this seems to be due to a change in what the linker is telling LTO. I would have to dig in the debugger to confirm, but perhaps lld is now indicating that it might be used by a regular obj? I.e. in BitcodeCompiler::add.<div><br></div><div>Teresa</div></div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Tue, Jun 11, 2019 at 5:48 AM Rui Ueyama <<a href="mailto:ruiu@google.com" target="_blank">ruiu@google.com</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"><div dir="ltr"><div dir="ltr">Looks like this is indeed related to r360841.<div><br></div><div>In C, there are distinctions between declarations, definitions and tentative definitions. Global variables declared with "extern" are declarations. Global variables that don't have "extern" and have initializers are definitions. If global variables have neither "extern" nor initializers, they are called tentative definitions.</div><div><br></div><div>Common symbols represent tentative definitions.</div><div></div><div><br></div><div>Tentative definition get special treatment in the linker. Usually if you define the same symbol in two object files, a linker report an error. However, common symbols are allowed to duplicate. Two or more common symbols are merged and then placed to the .bss section, so that they will be zero-initialized at runtime.</div><div><br></div><div>So, a global variable defined as `struct Node* head` is actually a common symbol.<br></div><div><br></div><div>I'm not sure why LTO cannot internalize common symbols though. Teresa, is this expected?</div></div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Mon, Jun 10, 2019 at 11:06 PM Teresa Johnson <<a href="mailto:tejohnson@google.com" target="_blank">tejohnson@google.com</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"><div dir="ltr"><div dir="ltr"><div dir="ltr">My guess is that it is due to lld change r360841 on that date (Introduce CommonSymbol). +Rui for comments.</div></div></div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Mon, Jun 10, 2019 at 4:45 AM Mani, Suresh via llvm-dev <<a href="mailto:llvm-dev@lists.llvm.org" target="_blank">llvm-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">
<div lang="EN-US">
<div class="gmail-m_8861579540098269826gmail-m_-7228805455322376645gmail-m_-6544574929198352205gmail-m_-5899584911161328454WordSection1">
<p class="MsoNormal"><u></u> <u></u></p>
<p class="MsoNormal"><u></u> <u></u></p>
<p class="MsoNormal">Hi ,<u></u><u></u></p>
<p class="MsoNormal"><u></u> <u></u></p>
<p class="MsoNormal">I have an issue during LTO phase of llvm compiler which is as follows,<u></u><u></u></p>
<p class="MsoNormal"><u></u> <u></u></p>
<p class="MsoNormal"><u></u> <u></u></p>
<p class="MsoNormal">File t3.c<u></u><u></u></p>
<p class="MsoNormal">---------<u></u><u></u></p>
<p class="MsoNormal"><u></u> <u></u></p>
<p class="MsoNormal"><u></u> <u></u></p>
<p class="MsoNormal">#include <stdio.h> <u></u><u></u></p>
<p class="MsoNormal">#include <stdlib.h> <u></u><u></u></p>
<p class="MsoNormal"> <u></u><u></u></p>
<p class="MsoNormal">// A linked list node <u></u><u></u></p>
<p class="MsoNormal">struct Node { <u></u><u></u></p>
<p class="MsoNormal"> int data; <u></u><u></u></p>
<p class="MsoNormal"> struct Node* next; <u></u><u></u></p>
<p class="MsoNormal"> struct Node* prev; <u></u><u></u></p>
<p class="MsoNormal">}; <u></u><u></u></p>
<p class="MsoNormal"><u></u> <u></u></p>
<p class="MsoNormal"><b><span style="font-size:14pt">struct Node* head;<u></u><u></u></span></b></p>
<p class="MsoNormal"> <u></u><u></u></p>
<p class="MsoNormal">/* Given a reference (pointer to pointer) to the head of a list
<u></u><u></u></p>
<p class="MsoNormal">and an int, inserts a new node on the front of the list. */<u></u><u></u></p>
<p class="MsoNormal">void push(struct Node** head_ref, int new_data) <u></u><u></u></p>
<p class="MsoNormal">{ <u></u><u></u></p>
<p class="MsoNormal"> struct Node* new_node = (struct Node*)malloc(sizeof(struct Node));
<u></u><u></u></p>
<p class="MsoNormal"> <u></u><u></u></p>
<p class="MsoNormal"> new_node->data = new_data; <u></u><u></u></p>
<p class="MsoNormal"> <u></u><u></u></p>
<p class="MsoNormal"> new_node->next = (*head_ref); <u></u><u></u></p>
<p class="MsoNormal"> new_node->prev = NULL; <u></u><u></u></p>
<p class="MsoNormal"> <u></u><u></u></p>
<p class="MsoNormal"> if ((*head_ref) != NULL) <u></u><u></u></p>
<p class="MsoNormal"> (*head_ref)->prev = new_node; <u></u><u></u></p>
<p class="MsoNormal"> <u></u><u></u></p>
<p class="MsoNormal"> (*head_ref) = new_node; <u></u><u></u></p>
<p class="MsoNormal">} <u></u><u></u></p>
<p class="MsoNormal"> <u></u><u></u></p>
<p class="MsoNormal"> <u></u><u></u></p>
<p class="MsoNormal">// This function prints contents of linked list starting from the given node
<u></u><u></u></p>
<p class="MsoNormal">void printList(struct Node* node) <u></u><u></u></p>
<p class="MsoNormal">{ <u></u><u></u></p>
<p class="MsoNormal"> struct Node* last; <u></u><u></u></p>
<p class="MsoNormal"> printf("\nTraversal in forward direction \n"); <u></u><u></u></p>
<p class="MsoNormal"> while (node != NULL) { <u></u><u></u></p>
<p class="MsoNormal"> printf(" %d ", node->data); <u></u><u></u></p>
<p class="MsoNormal"> last = node; <u></u><u></u></p>
<p class="MsoNormal"> node = node->next; <u></u><u></u></p>
<p class="MsoNormal"> } <u></u><u></u></p>
<p class="MsoNormal"> <u></u><u></u></p>
<p class="MsoNormal"> printf("\nTraversal in reverse direction \n"); <u></u><u></u></p>
<p class="MsoNormal"> while (last != NULL) { <u></u><u></u></p>
<p class="MsoNormal"> printf(" %d ", last->data); <u></u><u></u></p>
<p class="MsoNormal"> last = last->prev; <u></u><u></u></p>
<p class="MsoNormal"> } <u></u><u></u></p>
<p class="MsoNormal">} <u></u><u></u></p>
<p class="MsoNormal"><u></u> <u></u></p>
<p class="MsoNormal"> <u></u><u></u></p>
<p class="MsoNormal">/* Driver program to test above functions*/<u></u><u></u></p>
<p class="MsoNormal">int main() <u></u><u></u></p>
<p class="MsoNormal">{ <u></u><u></u></p>
<p class="MsoNormal"> <u></u><u></u></p>
<p class="MsoNormal"> head = NULL;<u></u><u></u></p>
<p class="MsoNormal"> push(&head, 7); <u></u><u></u></p>
<p class="MsoNormal"> push(&head, 1); <u></u><u></u></p>
<p class="MsoNormal"> push(&head, 4); <u></u><u></u></p>
<p class="MsoNormal"> <u></u><u></u></p>
<p class="MsoNormal"> printList(head); <u></u><u></u></p>
<p class="MsoNormal"> <u></u><u></u></p>
<p class="MsoNormal"> return 0; <u></u><u></u></p>
<p class="MsoNormal">} <u></u><u></u></p>
<p class="MsoNormal"><u></u> <u></u></p>
<p class="MsoNormal"><u></u> <u></u></p>
<p class="MsoNormal"><u></u> <u></u></p>
<p class="MsoNormal"><u></u> <u></u></p>
<p class="MsoNormal">Compiler invocation: <u></u><u></u></p>
<p class="MsoNormal">--------------------<u></u><u></u></p>
<p class="MsoNormal"><u></u> <u></u></p>
<p class="MsoNormal">clang -flto -fuse-ld=lld -O3 t3.c -o a.out<u></u><u></u></p>
<p class="MsoNormal"><u></u> <u></u></p>
<p class="MsoNormal"><u></u> <u></u></p>
<p class="MsoNormal">Expected behavior during LTO:<u></u><u></u></p>
<p class="MsoNormal">------------------------------<u></u><u></u></p>
<p class="MsoNormal"><u></u> <u></u></p>
<p class="MsoNormal">The compiler optimization during LTO needs to figure out that variable "head" is not referred by any precompiled object or library.<u></u><u></u></p>
<p class="MsoNormal">Until May-16-2019 variable "head" had internal attribute as follows,<u></u><u></u></p>
<p class="MsoNormal"><u></u> <u></u></p>
<p class="MsoNormal">@head = internal global %struct.Node* null, align 8<u></u><u></u></p>
<p class="MsoNormal"><u></u> <u></u></p>
<p class="MsoNormal">And the compiler was rightly able to recognize that "head" is not referred by any external precompiled object or library.<u></u><u></u></p>
<p class="MsoNormal"><u></u> <u></u></p>
<p class="MsoNormal">But after May-16-2019 the attribute of head was changed as follows,<u></u><u></u></p>
<p class="MsoNormal"><u></u> <u></u></p>
<p class="MsoNormal">@head = common dso_local global %struct.Node* null, align 8<u></u><u></u></p>
<p class="MsoNormal"><u></u> <u></u></p>
<p class="MsoNormal"><u></u> <u></u></p>
<p class="MsoNormal">Not sure if this is correct behavior?<u></u><u></u></p>
<p class="MsoNormal"><u></u> <u></u></p>
<p class="MsoNormal">If this is a correct behavior then can you please let me know how could the compiler figure out that variable "head" is not referred by any external precompiled object or library?<u></u><u></u></p>
<p class="MsoNormal"><u></u> <u></u></p>
<p class="MsoNormal"><u></u> <u></u></p>
<p class="MsoNormal">Thanks<u></u><u></u></p>
<p class="MsoNormal">M Suresh<u></u><u></u></p>
</div>
</div>
_______________________________________________<br>
LLVM Developers mailing list<br>
<a href="mailto:llvm-dev@lists.llvm.org" target="_blank">llvm-dev@lists.llvm.org</a><br>
<a href="https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev" rel="noreferrer" target="_blank">https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev</a><br>
</blockquote></div><br clear="all"><div><br></div>-- <br><div dir="ltr" class="gmail-m_8861579540098269826gmail-m_-7228805455322376645gmail-m_-6544574929198352205gmail_signature"><div dir="ltr"><div><span style="font-family:Times;font-size:medium"><table cellspacing="0" cellpadding="0"><tbody><tr style="color:rgb(85,85,85);font-family:sans-serif;font-size:small"><td nowrap style="border-top:2px solid rgb(213,15,37)">Teresa Johnson |</td><td nowrap style="border-top:2px solid rgb(51,105,232)"> Software Engineer |</td><td nowrap style="border-top:2px solid rgb(0,153,57)"> <a href="mailto:tejohnson@google.com" target="_blank">tejohnson@google.com</a> |</td><td nowrap style="border-top:2px solid rgb(238,178,17)"><br></td></tr></tbody></table></span></div></div></div>
</blockquote></div></div>
</blockquote></div><br clear="all"><div><br></div>-- <br><div dir="ltr" class="gmail-m_8861579540098269826gmail_signature"><div dir="ltr"><div><span style="font-family:Times;font-size:medium"><table cellspacing="0" cellpadding="0"><tbody><tr style="color:rgb(85,85,85);font-family:sans-serif;font-size:small"><td nowrap style="border-top:2px solid rgb(213,15,37)">Teresa Johnson |</td><td nowrap style="border-top:2px solid rgb(51,105,232)"> Software Engineer |</td><td nowrap style="border-top:2px solid rgb(0,153,57)"> <a href="mailto:tejohnson@google.com" target="_blank">tejohnson@google.com</a> |</td><td nowrap style="border-top:2px solid rgb(238,178,17)"><br></td></tr></tbody></table></span></div></div></div>
</blockquote></div>