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

    <tr>
        <th>Summary</th>
        <td>
            Compilation crash targetting wasm with -O2 and reference types
        </td>
    </tr>

    <tr>
      <th>Labels</th>
      <td>
            new issue
      </td>
    </tr>

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

    <tr>
      <th>Reporter</th>
      <td>
          hoodmane
      </td>
    </tr>
</table>

<pre>
    Tested against:

Ubuntu clang version 17.0.3 (++20231017073226+888437e1b600-1~exp1~20231017073338.54)
clang version 18.0.0 (https://github.com/llvm/llvm-project d1e685df45dc5944b43d2547d0138cd4a3ee4efe)

cc @sbc100 @pmatos

### testcase.c

```c
__externref_t foo(void);
void bar(__externref_t);

void
test(int flag, __externref_t ref1, __externref_t ref2)
{
  if (flag) {
    ref1 = foo();
    ref2 = foo();
  }
  bar(ref1);
  bar(ref2);
}
```
Compilation command:
```sh
clang -c testcase.c -O2   -mreference-types -target wasm32-unknown-unknown
```

The files it asked me to upload:
### testcase-ccc7e1.c

```
# 1 "<built-in>"
# 1 "testcase.c"
__externref_t foo(void);
void bar(__externref_t);

void
test(int flag, __externref_t ref1, __externref_t ref2)
{
  if (flag) {
    ref1 = foo();
    ref2 = foo();
  }
  bar(ref1);
  bar(ref2);
}
```

### testcase-ccc7e1.sh

```
# Crash reproducer for Ubuntu clang version 17.0.3 (++20231017073226+888437e1b600-1~exp1~20231017073338.54)
# Driver args: "-c" "testcase.c" "-O2" "-mreference-types" "-target" "wasm32-unknown-unknown"
# Original command: "/usr/lib/llvm-17/bin/clang" "-cc1" "-triple" "wasm32-unknown-unknown" "-emit-obj" "-disable-free" "-clear-ast-before-backend" "-disable-llvm-verifier" "-discard-value-names" "-main-file-name" "testcase.c" "-mrelocation-model" "static" "-mframe-pointer=none" "-ffp-contract=on" "-fno-rounding-math" "-mconstructor-aliases" "-target-cpu" "generic" "-target-feature" "+reference-types" "-fvisibility=hidden" "-debugger-tuning=gdb" "-fcoverage-compilation-dir=/home/hood/Documents/programming/pyodide" "-resource-dir" "/usr/lib/llvm-17/lib/clang/17" "-internal-isystem" "/usr/lib/llvm-17/lib/clang/17/include" "-O2" "-fdebug-compilation-dir=/home/hood/Documents/programming/pyodide" "-ferror-limit" "19" "-fgnuc-version=4.2.1" "-fcolor-diagnostics" "-vectorize-loops" "-vectorize-slp" "-o" "testcase.o" "-x" "c" "testcase.c"
 "/usr/lib/llvm-17/bin/clang" "-cc1" "-triple" "wasm32-unknown-unknown" "-emit-obj" "-disable-free" "-clear-ast-before-backend" "-disable-llvm-verifier" "-discard-value-names" "-main-file-name" "testcase.c" "-mrelocation-model" "static" "-mframe-pointer=none" "-ffp-contract=on" "-fno-rounding-math" "-mconstructor-aliases" "-target-cpu" "generic" "-target-feature" "+reference-types" "-fvisibility=hidden" "-debugger-tuning=gdb" "-fcoverage-compilation-dir=/home/hood/Documents/programming/pyodide" "-O2" "-fdebug-compilation-dir=/home/hood/Documents/programming/pyodide" "-ferror-limit" "19" "-fgnuc-version=4.2.1" "-fcolor-diagnostics" "-vectorize-loops" "-vectorize-slp" "-x" "c" "testcase-ccc7e1.c"
```


</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJzsV0-v2ygQ_zTkgohssBP7kMO-l_W1l-65wjB2aDFYgPP69tDPvsL_4ry-qKpU7UqrSlECM8P8_c0EuPeqNQAnlD-h_LzjQ7hYd7pYKztuYFdb-Xr6CD6AxLzlyviA2B8oOaNk_v6rHkwYsNDctPgKzitrcHrcJ3uGES0QfUL0iSaUpUl6TI6M0gOiT0VRZOwIaX1IEpJ-g699-m0jxFixzzNEy8nGG-XFPtknUfklhN5Hf2iFaNWqcBnqvbAdopXW1-WH9M5-BhGwTOFQ5LLJcinyMsvqjEmaZ0eZpKwQMuMMIIMGVruzdYFRlvhapEkSV33Hg_VbCUTZ9MEBfBDcw17c8Q_J9Jmpnz7B1wDOOGg-Bd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