<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 - Optimizer generating wrong code"
   href="https://bugs.llvm.org/show_bug.cgi?id=47984">47984</a>
          </td>
        </tr>

        <tr>
          <th>Summary</th>
          <td>Optimizer generating wrong code
          </td>
        </tr>

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

        <tr>
          <th>Version</th>
          <td>trunk
          </td>
        </tr>

        <tr>
          <th>Hardware</th>
          <td>Macintosh
          </td>
        </tr>

        <tr>
          <th>OS</th>
          <td>MacOS X
          </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>LLVM Codegen
          </td>
        </tr>

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

        <tr>
          <th>Reporter</th>
          <td>tbruio@outlook.com
          </td>
        </tr>

        <tr>
          <th>CC</th>
          <td>llvm-bugs@lists.llvm.org, neeilans@live.com, richard-llvm@metafoo.co.uk
          </td>
        </tr></table>
      <p>
        <div>
        <pre>Version
==========
Tested Clang 10 through 12.


Issue
==========
While running tests for Blender, we encountered an intermittent bug on binaries
compiled with clang.
It shows up in -O2 builds, not -O1, or -O0.

The code inside the for-loop at [1] is not executed with -O2. That causes more
problems down the line.

We fixed it by marking `last_remapped_id` volatile. Another way was to access
other fields of `last_remapped_id`.

Steps to redo:
==========
- git clone <a href="https://git.blender.org/blender.git">https://git.blender.org/blender.git</a>
- cd blender
- make update
- git revert 2ddecfffc3d3 --no-commit
- cmake -B ../blender_build -C build_files/cmake/config/blender_lite.cmake -G
Ninja -DCMAKE_BUILD_TYPE=RelWithDebInfo
- cd ../blender_build  && make install
- ctest -R id_man -C relwithdebinfo -VV

The test will fail.

Diagnostics
==========
It has been been very difficult to isolate the bug to a simple code. Even
moving out the function `id_delete` to
another file fixes the bug.
There is a possibility that the following might be the reason of the bug
(quoting from <a href="https://developer.blender.org/D9315#230626">https://developer.blender.org/D9315#230626</a>)
"""
Just for context, repeating what I said earlier in the chat - this is what **I
think** goes on:
* The crash makes sense to me now, the "MECube" (which is the default cube from
the factory-startup I think) is tagged properly for deletion, but not the
"OBCube" - because the loop isn't executed correctly.
* The optimizer realizes that `tagged_deleted_ids` is collected the same way on
each iteration (which is based on `ID.tag`) and it operates on the same data,
so it assumes it will always yield the same result.
* It does not realise however that `ID.tag` is changed within the loop,
somewhere deeper down the call-stack (here in fact
<a href="https://developer.blender.org/diffusion/B/browse/master/source/blender/blenkernel/intern/lib_remap.c$158">https://developer.blender.org/diffusion/B/browse/master/source/blender/blenkernel/intern/lib_remap.c$158</a>).

Note that this should be reported to the Clang team. The Godbolt links we used
in the chat will hopefully be enough, although if my analysis above is correct,
the code on Godbolt won't show the error, as the part of `ID.tag` being changed
indirectly in the loop is missing. Maybe a simple (non-inline!) function that
just modifies `ID.tag` will be enough.
"""

Attachments
===========
Four files are attached:
With volatile assembly
With volatile optimization record
Without volatile assembly
Without volatile optimization record

I can run more tests if needed.

[1]
<a href="https://developer.blender.org/diffusion/B/browse/master/source/blender/blenkernel/intern/lib_id_delete.c;2ddecfffc3d3a3a1db4ae45e8665caa2a85ab43a$293-310">https://developer.blender.org/diffusion/B/browse/master/source/blender/blenkernel/intern/lib_id_delete.c;2ddecfffc3d3a3a1db4ae45e8665caa2a85ab43a$293-310</a>

if you prefer github: <a href="https://github.com/blender/blender">https://github.com/blender/blender</a></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>