<html>
    <head>
      <base href="http://llvm.org/bugs/" />
    </head>
    <body><table border="1" cellspacing="0" cellpadding="8">
        <tr>
          <th>Bug ID</th>
          <td><a class="bz_bug_link 
          bz_status_NEW "
   title="NEW --- - Compiler misses struct field alignment optimization opportunities."
   href="http://llvm.org/bugs/show_bug.cgi?id=19882">19882</a>
          </td>
        </tr>

        <tr>
          <th>Summary</th>
          <td>Compiler misses struct field alignment optimization opportunities.
          </td>
        </tr>

        <tr>
          <th>Product</th>
          <td>clang
          </td>
        </tr>

        <tr>
          <th>Version</th>
          <td>3.3
          </td>
        </tr>

        <tr>
          <th>Hardware</th>
          <td>PC
          </td>
        </tr>

        <tr>
          <th>OS</th>
          <td>All
          </td>
        </tr>

        <tr>
          <th>Status</th>
          <td>NEW
          </td>
        </tr>

        <tr>
          <th>Severity</th>
          <td>normal
          </td>
        </tr>

        <tr>
          <th>Priority</th>
          <td>P
          </td>
        </tr>

        <tr>
          <th>Component</th>
          <td>C++
          </td>
        </tr>

        <tr>
          <th>Assignee</th>
          <td>unassignedclangbugs@nondot.org
          </td>
        </tr>

        <tr>
          <th>Reporter</th>
          <td>jujjyl@gmail.com
          </td>
        </tr>

        <tr>
          <th>CC</th>
          <td>dgregor@apple.com, llvmbugs@cs.uiuc.edu
          </td>
        </tr>

        <tr>
          <th>Classification</th>
          <td>Unclassified
          </td>
        </tr></table>
      <p>
        <div>
        <pre>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
<a href="https://github.com/kripken/emscripten/issues/2378">https://github.com/kripken/emscripten/issues/2378</a> for discussion of this in
Emscripten repo.</pre>
        </div>
      </p>
      <hr>
      <span>You are receiving this mail because:</span>
      
      <ul>
          <li>You are on the CC list for the bug.</li>
      </ul>
    </body>
</html>