<html>
    <head>
      <base href="https://bugs.freedesktop.org/" />
    </head>
    <body><table border="1" cellspacing="0" cellpadding="8">
        <tr>
          <th>Priority</th>
          <td>medium
          </td>
        </tr>

        <tr>
          <th>Bug ID</th>
          <td><a class="bz_bug_link 
          bz_status_NEW "
   title="NEW --- - PHINode containing itself causes segfault in LLVM when compiling Blender OpenCL kernel with R600 backend"
   href="https://bugs.freedesktop.org/show_bug.cgi?id=84232">84232</a>
          </td>
        </tr>

        <tr>
          <th>Assignee</th>
          <td>dri-devel@lists.freedesktop.org
          </td>
        </tr>

        <tr>
          <th>Summary</th>
          <td>PHINode containing itself causes segfault in LLVM when compiling Blender OpenCL kernel with R600 backend
          </td>
        </tr>

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

        <tr>
          <th>Classification</th>
          <td>Unclassified
          </td>
        </tr>

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

        <tr>
          <th>Reporter</th>
          <td>vitalif@yourcmc.ru
          </td>
        </tr>

        <tr>
          <th>Hardware</th>
          <td>Other
          </td>
        </tr>

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

        <tr>
          <th>Version</th>
          <td>XOrg CVS
          </td>
        </tr>

        <tr>
          <th>Component</th>
          <td>DRM/Radeon
          </td>
        </tr>

        <tr>
          <th>Product</th>
          <td>DRI
          </td>
        </tr></table>
      <p>
        <div>
        <pre>Created <span class=""><a href="attachment.cgi?id=106717" name="attach_106717" title="Blender output with R600_DEBUG=compute,fs,vs,gs,ps,cs">attachment 106717</a> <a href="attachment.cgi?id=106717&action=edit" title="Blender output with R600_DEBUG=compute,fs,vs,gs,ps,cs">[details]</a></span>
Blender output with R600_DEBUG=compute,fs,vs,gs,ps,cs

I'm trying to run Blender using Mesa OpenCL implementation on a radeonsi  card.
First the kernel didn't want to compile, but that was caused by a  bug in it
(they were using . instead of -> in 1 place), and after fixing  this bug I've
got the kernel to compile...

...But after that, LLVM started to crash during translation of IR into  shader
code with R600 backend.

I've done some investigation and figured out that the crash is caused by a 
PHINode containing itself. SIAnnotateControlFlow::handleLoopCondition()  can't
handle such situation - it recurses into itself, calls  Phi->eraseFromParent()
inside the inner execution, returns into outer one,  gets zeroed out object and
crashes when trying to do something with its  members... for example when
trying to erase it again.

I've tried to understand the semantics of such PHINodes from reading the code
and got a suspicion that the rest of LLVM code just ignores PHINodes equal to
their parent, so I've tried to fix  the bug by making handleLoopCondition()
skip IncomingValues equal to the Phi itself, but the bug didn't go away!
Surprisingly, PHINode may not just contain itself directly, but it also may
contain itself inside another PHINode, i.e.
Phi->getIncomingValue(0)->getIncomingValue(0) == Phi, which results in the same
problem with SIAnnotateControlFlow...

I'll attach Blender output with R600_DEBUG = trace everything and a stack trace
of the crash.

The bug reproduced with llvm 3.5 and snapshot of 3.6; blender is 2.71; Mesa is
from OIBAF's repository.

sudo -E CYCLES_OPENCL_TEST=1 R600_DEBUG=compute,fs,vs,gs,ps,cs blender cup\ of\
coffee\ 5.blend &> kernel-llvm.log</pre>
        </div>
      </p>
      <hr>
      <span>You are receiving this mail because:</span>
      
      <ul>
          <li>You are the assignee for the bug.</li>
      </ul>
    </body>
</html>