[llvm-dev] Expected behavior of lld during LTO for global symbols (Attr Internal/Common)

Mani, Suresh via llvm-dev llvm-dev at lists.llvm.org
Mon Jun 10 04:45:34 PDT 2019



Hi ,

I have an issue during LTO phase of llvm compiler which is as follows,


File t3.c
---------


#include <stdio.h>
#include <stdlib.h>

// A linked list node
struct Node {
    int data;
    struct Node* next;
    struct Node* prev;
};

struct Node* head;

/* Given a reference (pointer to pointer) to the head of a list
and an int, inserts a new node on the front of the list. */
void push(struct Node** head_ref, int new_data)
{
    struct Node* new_node = (struct Node*)malloc(sizeof(struct Node));

    new_node->data = new_data;

    new_node->next = (*head_ref);
    new_node->prev = NULL;

    if ((*head_ref) != NULL)
        (*head_ref)->prev = new_node;

    (*head_ref) = new_node;
}


// This function prints contents of linked list starting from the given node
void printList(struct Node* node)
{
    struct Node* last;
    printf("\nTraversal in forward direction \n");
    while (node != NULL) {
        printf(" %d ", node->data);
        last = node;
        node = node->next;
    }

    printf("\nTraversal in reverse direction \n");
    while (last != NULL) {
        printf(" %d ", last->data);
        last = last->prev;
    }
}


/* Driver program to test above functions*/
int main()
{

    head = NULL;
    push(&head, 7);
    push(&head, 1);
    push(&head, 4);

    printList(head);

    return 0;
}




Compiler invocation:
--------------------

clang -flto -fuse-ld=lld -O3 t3.c -o a.out


Expected behavior during LTO:
------------------------------

The compiler optimization during LTO needs to figure out that variable "head" is not referred by any precompiled object or library.
Until May-16-2019 variable "head" had internal attribute as follows,

@head = internal global %struct.Node* null, align 8

And the compiler was rightly able to recognize that "head" is not referred by any external precompiled object or library.

But after May-16-2019  the attribute of head was changed as follows,

@head = common dso_local global %struct.Node* null, align 8


Not sure if this is correct behavior?

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?


Thanks
M Suresh
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/llvm-dev/attachments/20190610/2f227e26/attachment.html>


More information about the llvm-dev mailing list