<html>
    <head>
      <base href="https://bugs.llvm.org/">
    </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 - [Regression] Clang 6 generated code produces incorrect result with -02 and above"
   href="https://bugs.llvm.org/show_bug.cgi?id=36929">36929</a>
          </td>
        </tr>

        <tr>
          <th>Summary</th>
          <td>[Regression] Clang 6 generated code produces incorrect result with -02 and above
          </td>
        </tr>

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

        <tr>
          <th>Version</th>
          <td>6.0
          </td>
        </tr>

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

        <tr>
          <th>OS</th>
          <td>Linux
          </td>
        </tr>

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

        <tr>
          <th>Severity</th>
          <td>release blocker
          </td>
        </tr>

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

        <tr>
          <th>Component</th>
          <td>-New Bugs
          </td>
        </tr>

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

        <tr>
          <th>Reporter</th>
          <td>budai@tresorit.com
          </td>
        </tr>

        <tr>
          <th>CC</th>
          <td>llvm-bugs@lists.llvm.org
          </td>
        </tr></table>
      <p>
        <div>
        <pre>Created <span class=""><a href="attachment.cgi?id=20137" name="attach_20137" title="Source code, runner script and results for the original example">attachment 20137</a> <a href="attachment.cgi?id=20137&action=edit" title="Source code, runner script and results for the original example">[details]</a></span>
Source code, runner script and results for the original example

We have a code that uses a custom allocator with `std::vector` to provide some
extra memory safety features. After switching to Clang 6, our tests started to
fail on 32-bit x86 Linux platforms.

We boiled down our code to this simple example with no dependencies but STL:

    #include <cstddef>
    #include <vector>

    template<typename T>
    struct my_allocator {
        typedef T value_type;

        T* allocate(size_t n) {
            return new T[n];
        }

        void deallocate(T* p, size_t n) {
            delete[] p;
        }

        template<typename U, typename... A>
        void construct(U* p, A... a) {
            new ((void*)p)U(std::forward<A>(a)...);
        }
    };

    int main() {
        std::vector<char, my_allocator<char>> vec(32, 'a');
        vec.insert(vec.begin(), 1, 'b');
        return vec.size() == 33 ? 0 : 1;
    }

This code should extend `vec` by one by inserting a single element on the
beginning, but it produces incorrect results (exit code 1) with Clang 6 with
libcxx, cross-compiling from 64 to 32-bit Linux in optimization level 2 and
above:

    clang 5, x86_32, libstdc++, -O0: ok
    clang 5, x86_32, libstdc++, -O1: ok
    clang 5, x86_32, libstdc++, -O2: ok
    clang 5, x86_32, libstdc++, -O3: ok
    clang 5, x86_32, libc++, -O0: ok
    clang 5, x86_32, libc++, -O1: ok
    clang 5, x86_32, libc++, -O2: ok
    clang 5, x86_32, libc++, -O3: ok
    clang 5, x86_64, libstdc++, -O0: ok
    clang 5, x86_64, libstdc++, -O1: ok
    clang 5, x86_64, libstdc++, -O2: ok
    clang 5, x86_64, libstdc++, -O3: ok
    clang 5, x86_64, libc++, -O0: ok
    clang 5, x86_64, libc++, -O1: ok
    clang 5, x86_64, libc++, -O2: ok
    clang 5, x86_64, libc++, -O3: ok
    clang 6, x86_32, libstdc++, -O0: ok
    clang 6, x86_32, libstdc++, -O1: ok
    clang 6, x86_32, libstdc++, -O2: ok
    clang 6, x86_32, libstdc++, -O3: ok
    clang 6, x86_32, libc++, -O0: ok
    clang 6, x86_32, libc++, -O1: ok
    clang 6, x86_32, libc++, -O2: fail 1
    clang 6, x86_32, libc++, -O3: fail 1
    clang 6, x86_64, libstdc++, -O0: ok
    clang 6, x86_64, libstdc++, -O1: ok
    clang 6, x86_64, libstdc++, -O2: ok
    clang 6, x86_64, libstdc++, -O3: ok
    clang 6, x86_64, libc++, -O0: ok
    clang 6, x86_64, libc++, -O1: ok
    clang 6, x86_64, libc++, -O2: ok
    clang 6, x86_64, libc++, -O3: ok

With using either Clang 5, stdlibc++, or x86_64 target, or removing/changing
the `construct` method fixes the generated code, yielding in correct results.

You can find the source code, runner script and the above results in the
attachment `original.tar.gz`.</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>