[LLVMdev] RFA: Alignment of Strings

Bill Wendling isanbard at gmail.com
Tue Jun 2 18:33:52 PDT 2009


One of our developers pointed out that GCC generates something akin to
this on Darwin:

$ cat t.c
__private_extern__ void bar(const char *format, ...)
  __attribute__ ((format (printf, 1, 2)));

void foo(const char *i, const char *b) {
  bar("this is some error message that is kind of longish.");
}

$ gcc -arch i386 -S -o - t.c -Os
	.cstring
	.align 2
LC0:
    	.ascii "this is some error message that is kind of longish.\0"
...

LLVM on the other hand generates this on Darwin:

$ /Developer/usr/bin/llvm-gcc -arch i386 -S -o - t.c -Os
...
        .section __TEXT,__cstring,cstring_literals
        .align  4
LC:
        .asciz  "this is some error message that is kind of longish."

Note that the alignment in LLVM is greater than it is for GCC. When I
look at the alignment of the string in GDB, the front-end is saying
that it's 8, which would be '.align 3'. I have a patch below that sets
the alignment explicitly for STRING_CST types.

My question is two-fold:

* Is the patch good? I don't *think* that it will break anything else
in the front-end or for different platforms, but one never knows, and,

* What magick is GCC performing to get the '.align 2' instead of
'.align 3', which I would expect? Is this something we can/should
replicate in LLVM?

-bw

Index: llvm-gcc.src/gcc/llvm-convert.cpp
===================================================================
--- llvm-gcc.src/gcc/llvm-convert.cpp	(revision 72733)
+++ llvm-gcc.src/gcc/llvm-convert.cpp	(working copy)
@@ -7418,6 +7418,8 @@
                                            TAI ?
                                             TAI->getStringConstantPrefix() :
                                             ".str", TheModule);
+  GV->setAlignment(TYPE_ALIGN(TREE_TYPE(exp)));
+
   if (SlotP) *SlotP = GV;
 #ifdef LLVM_CSTRING_SECTION
   // For Darwin, try to put it into the .cstring section.



More information about the llvm-dev mailing list