<table border="1" cellspacing="0" cellpadding="8">
    <tr>
        <th>Issue</th>
        <td>
            <a href=https://github.com/llvm/llvm-project/issues/123203>123203</a>
        </td>
    </tr>

    <tr>
        <th>Summary</th>
        <td>
            [Clang][OpenCL] Compiler crash on __builtin_assume_aligned in OpenCL
        </td>
    </tr>

    <tr>
      <th>Labels</th>
      <td>
            clang,
            OpenCL,
            crash
      </td>
    </tr>

    <tr>
      <th>Assignees</th>
      <td>
      </td>
    </tr>

    <tr>
      <th>Reporter</th>
      <td>
          ritter-x2a
      </td>
    </tr>
</table>

<pre>
    Using the return value of `__builtin_assume_aligned` in OpenCL hits an assertion in clang. I don't have a strong opinion on whether the builtin should be supported in OpenCL since it's not part of the Khronos spec, but it shouldn't hit an assertion.
Observed with a RelWithDebInfo trunk build, on Ubuntu 22.04.

Reproducer:
```c
void f(__global int *g) {
  __global int *ag = __builtin_assume_aligned(g, 16);
}
```
When compiling this via `clang -c test.cl`, clang first reports unexpected diagnostics (I don't see how `bool`s are involved) and then hits an assertion:
```
test.cl:2:17: error: incompatible integer to pointer conversion initializing '__global int *__private' with an expression of type 'bool' [-Wint-conversion]
    2 | __global int *ag = __builtin_assume_aligned(g, 16);
      | ^                             ~~~~~~
clang: /home/faritter/playground/llvm/llvm-project/llvm/lib/IR/Instructions.cpp:2974: static llvm::CastInst* llvm::CastInst::Create(llvm::Instruction::CastOps, llvm::Value*, llvm::Type*, const llvm::Twine&, llvm::InsertPosition): Assertion `castIsValid(op, S, Ty) && "Invalid cast!"' failed.
PLEASE submit a bug report to https://github.com/llvm/llvm-project/issues/ and include the crash backtrace, preprocessed source, and associated run script.
Stack dump:
0.      Program arguments: ./build/bin/clang -c test.cl
1.      <eof> parser at end of file
2.      test.cl:1:6: LLVM IR generation of declaration 'f'
3.      test.cl:1:6: Generating code for declaration 'f'
 #0 0x000056e464acc7ff llvm::sys::PrintStackTrace(llvm::raw_ostream&, int) /home/faritter/playground/llvm/llvm-project/llvm/lib/Support/Unix/Signals.inc:802:3
[...]
#13 0x000056e4636a9852 llvm::IRBuilderBase::CreateCast(llvm::Instruction::CastOps, llvm::Value*, llvm::Type*, llvm::Twine const&, llvm::MDNode*, llvm::FMFSource) /home/faritter/playground/llvm/llvm-project/llvm/include/llvm/IR/IRBuilder.h:2193:0
#14 0x000056e4636a9852 llvm::IRBuilderBase::CreateIntCast(llvm::Value*, llvm::Type*, bool, llvm::Twine const&) /home/faritter/playground/llvm/llvm-project/llvm/include/llvm/IR/IRBuilder.h:2231:0
#15 0x000056e464e2f677 (anonymous namespace)::ScalarExprEmitter::VisitCastExpr(clang::CastExpr*) /home/faritter/playground/llvm/llvm-project/clang/lib/CodeGen/CGExprScalar.cpp:2574:44
#16 0x000056e464e2c104 Visit /home/faritter/playground/llvm/llvm-project/clang/lib/CodeGen/CGExprScalar.cpp:449:3
#17 0x000056e464e2c104 clang::CodeGen::CodeGenFunction::EmitScalarExpr(clang::Expr const*, bool) /home/faritter/playground/llvm/llvm-project/clang/lib/CodeGen/CGExprScalar.cpp:5591:13
[...]
```

The full backtrace, preprocessed source, and run script are attached:

[backtrace.txt](https://github.com/user-attachments/files/18440429/backtrace.txt)

[test-ff43fa.cl.txt](https://github.com/user-attachments/files/18440433/test-ff43fa.cl.txt)

[test-ff43fa.sh.txt](https://github.com/user-attachments/files/18440434/test-ff43fa.sh.txt)
</pre>
<img width="1" height="1" alt="" src="http://email.email.llvm.org/o/eJy8V09zoz4S_TTypSuUEGDsgw_EDlOpndmZSubP0SWgAe1giZKEk-xhP_tWA4mdZDK79fulxpWyI4Fet17365akc6rRiBuWXLJkt5CDb43dWOU92ot7IReFqR4235zSDfgWwaIfrIaj7AYEUwNb8v2-GFTnld5L54YD7mVHmBVbclAaPveotx-hVd6B1CCdQ-uV0fSs7KRuAriGymgmUg-tPCJIcN4a3YDplaY3jYa7Fn2LdvRhNgeuNUNXQYHghr431mN1ZtApXSIoz0TqQBsPvbSeXCaIf7TWaOPA9VgysYVi8KD8jDi7ovwzfwPGs8-FQ3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