[LLVMbugs] [Bug 19882] New: Compiler misses struct field alignment optimization opportunities.

bugzilla-daemon at llvm.org bugzilla-daemon at llvm.org
Wed May 28 14:04:40 PDT 2014


http://llvm.org/bugs/show_bug.cgi?id=19882

            Bug ID: 19882
           Summary: Compiler misses struct field alignment optimization
                    opportunities.
           Product: clang
           Version: 3.3
          Hardware: PC
                OS: All
            Status: NEW
          Severity: normal
          Priority: P
         Component: C++
          Assignee: unassignedclangbugs at nondot.org
          Reporter: jujjyl at gmail.com
                CC: dgregor at apple.com, llvmbugs at cs.uiuc.edu
    Classification: Unclassified

Consider the following example:

#include <cstdint>
#include <stdio.h>
#include <cassert>

struct __attribute__((packed)) foo // packed attribute: we expect that the
compiler tightly lays out double after int, so total struct size 12 bytes.
{
        __attribute__((aligned(4))) int x; // aligned attribute: we expect that
in any instance of this struct, the int is always aligned to 4 bytes.
        double d; // Since this struct is packed, and the int before this is
aligned to 4 bytes, we can deduce that this double is also aligned to four
bytes!
};


char data[256];
__attribute__((noinline)) foo *getfoo() // Make compiler not optimize out the
whole thing we're doing at compiletime.
{
        return (foo*)data;
}

int main()
{
        assert(sizeof(foo) == 12);
        foo *f = getfoo();
        f->x = 42;
        f->d = 1.0; // We expect this to perform a two 4-byte stores to write
the double.
        printf("addr: %p, value: %f\n", &f->d, f->d); // We expect this to
perform load of the double as two 4-byte loads.
}

One would expect that in the generated bit code, the store and load of f->d
would be marked as aligned to 4 bytes, since it could be deduced from the
alignment of the previous field f->x, but this does not seem to be the case,
and f->d is treated as unaligned (aligned to 1 byte). Is it possible to improve
this analysis so that one doesn't need to annotate every field of a struct with
the __attribute__((aligned(4)))?

Tested in Emscripten fork of Clang 3.3. See also
https://github.com/kripken/emscripten/issues/2378 for discussion of this in
Emscripten repo.

-- 
You are receiving this mail because:
You are on the CC list for the bug.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/llvm-bugs/attachments/20140528/a3907c69/attachment.html>


More information about the llvm-bugs mailing list