<html>
    <head>
      <base href="https://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 --- - UBSan breaks target intrinsics taking constants: should we fold overflow intrinsics? Where?"
   href="https://llvm.org/bugs/show_bug.cgi?id=23517">23517</a>
          </td>
        </tr>

        <tr>
          <th>Summary</th>
          <td>UBSan breaks target intrinsics taking constants: should we fold overflow intrinsics? Where?
          </td>
        </tr>

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

        <tr>
          <th>Version</th>
          <td>trunk
          </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>LLVM Codegen
          </td>
        </tr>

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

        <tr>
          <th>Reporter</th>
          <td>ahmed.bougacha@gmail.com
          </td>
        </tr>

        <tr>
          <th>CC</th>
          <td>llvmbugs@cs.uiuc.edu
          </td>
        </tr>

        <tr>
          <th>Classification</th>
          <td>Unclassified
          </td>
        </tr></table>
      <p>
        <div>
        <pre>Consider:

#include <arm_neon.h>
int32x2_t test(int64x2_t a) {
  return vqrshrn_n_s64(a, 31 - 2);
}

  clang -fsanitize=undefined -target arm64-apple-darwin -O0

will generate:

  %3 = call { i32, i1 } @llvm.ssub.with.overflow.i32(i32 31, i32 2)
  %4 = extractvalue { i32, i1 } %3, 0
  ...
  %vqrshrn_n1 = call <2 x i32> @llvm.aarch64.neon.sqrshrn.v2i32(<2 x i64>
%vqrshrn_n, i32 %4)


Which is not OK, and breaks various assumptions in clang and LLVM (and will
causes us to eventually crash).

With -O0, we can't expect any pass to fold these;  in fact, clang itself
assumes these intrinsics *will* take Constants, so we have to deal with this
during IR gen.

I see two alternatives:
- teach ConstantFolder to deal with overflow intrinsics (messy, because it's
implemented in terms of ConstantExprs)
- teach the UBSan clang CodeGen to do the folding (which sounds wrong)</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>