[LLVMdev] what's correct behavior for struct forward declarations?

Philip Guo pg at cs.stanford.edu
Wed Feb 18 20:20:17 PST 2009


hi all,

i'm trying to use LLVM to compile some linux kernel code, and i noticed a
mismatch with gcc.  here is a simplified test case:

struct foo {
  int a;
  int b;
  int c;
};

static struct foo x; // 'forward' declaration?

int bar() {
  printf("a: %d, b: %d, c: %d\n", x.a, x.b, x.c);
}

static struct foo x = {
  .a = 1, .b = 2, .c = 3,
};

int main() {
  bar();
  return 0;
}


when this code is compiled with gcc and run, stdout prints "a: 1, b: 2, c:
3", which means that it takes the true declaration of x, initialized to 1,
2, 3.  however, when it's compiled with llvm, llvm emits the following code
for x:

@x = internal global %struct.foo zeroinitializer    ; <%struct.foo*>
[#uses=3]

which seems to me like it's taking the first declaration of x, which is a
forward declaration.  is that the correct behavior?  i believe that the
kernel developers intended for the second (real declaration) of x to be
visible, even in bar(), but that's not what's happening with llvm.  is there
an easy workaround where i can get llvm to emit code initializing x to
{1,2,3}?  thanks!

Philip
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/llvm-dev/attachments/20090218/2f65fc21/attachment.html>


More information about the llvm-dev mailing list