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

    <tr>
        <th>Summary</th>
        <td>
            [SPIR-V] Verifier fails with invalid uses of intrinsic global variable
        </td>
    </tr>

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

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

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

<pre>
    ### Reproducer
_example.cpp_
```
int *ptr_0 = nullptr;
void *ptr_1 = ptr_0;
```
1.  clang -S -emit-llvm -cl-std=clc++ --target=spir example.cpp
2. llc --verify-machineinstrs -O0 -mtriple=spirv64-unknown-unknown example.ll -o - 2>&1

### Stack Trace
```
invalid uses of intrinsic global variable
ptr @llvm.global_ctors
LLVM ERROR: Broken module found, compilation aborted!
PLEASE submit a bug report to https://github.com/llvm/llvm-project/issues/ and include the crash backtrace.
Stack dump:
0.      Program arguments: llc --verify-machineinstrs -O0 -mtriple=spirv64-unknown-unknown global_crash.ll -o -
 #0 0x000055e5a16c0e72 llvm::sys::PrintStackTrace(llvm::raw_ostream&, int) <truncated>llvm-project/llvm/lib/Support/Unix/Signals.inc:723:22
 #1 0x000055e5a16c1289 PrintStackTraceSignalHandler(void*) <truncated>llvm-project/llvm/lib/Support/Unix/Signals.inc:798:1
 #2 0x000055e5a16be659 llvm::sys::RunSignalHandlers() <truncated>llvm-project/llvm/lib/Support/Signals.cpp:105:20
 #3 0x000055e5a16c071d SignalHandler(int) <truncated>llvm-project/llvm/lib/Support/Unix/Signals.inc:413:1
 #4 0x00007f2bc2845320 (/lib/x86_64-linux-gnu/libc.so.6+0x45320)
 #5 0x00007f2bc289eb1c __pthread_kill_implementation ./nptl/pthread_kill.c:44:76
 #6 0x00007f2bc289eb1c __pthread_kill_internal ./nptl/pthread_kill.c:78:10
 #7 0x00007f2bc289eb1c pthread_kill ./nptl/pthread_kill.c:89:10
 #8 0x00007f2bc284526e raise ./signal/../sysdeps/posix/raise.c:27:6
 #9 0x00007f2bc28288ff abort ./stdlib/abort.c:81:7
#10 0x000055e5a15eb3c0 llvm::report_fatal_error(llvm::Twine const&, bool) <truncated>llvm-project/llvm/lib/Support/ErrorHandling.cpp:126:9
#11 0x000055e5a15eb194 llvm::report_fatal_error(llvm::StringRef, bool) <truncated>llvm-project/llvm/lib/Support/ErrorHandling.cpp:87:68
#12 0x000055e5a096b006 (anonymous namespace)::VerifierLegacyPass::doFinalization(llvm::Module&) <truncated>llvm-project/llvm/lib/IR/Verifier.cpp:7213:12
#13 0x000055e5a0857edb llvm::FPPassManager::doFinalization(llvm::Module&) <truncated>llvm-project/llvm/lib/IR/LegacyPassManager.cpp:1472:13
#14 0x000055e5a08584bb (anonymous namespace)::MPPassManager::runOnModule(llvm::Module&) <truncated>llvm-project/llvm/lib/IR/LegacyPassManager.cpp:1559:13
#15 0x000055e5a08530e3 llvm::legacy::PassManagerImpl::run(llvm::Module&) <truncated>llvm-project/llvm/lib/IR/LegacyPassManager.cpp:541:13
#16 0x000055e5a0858b15 llvm::legacy::PassManager::run(llvm::Module&) <truncated>llvm-project/llvm/lib/IR/LegacyPassManager.cpp:1651:1
#17 0x000055e59edf4eea compileModule(char**, llvm::LLVMContext&) <truncated>llvm-project/llvm/tools/llc/llc.cpp:755:34
#18 0x000055e59edf2605 main <truncated>llvm-project/llvm/tools/llc/llc.cpp:412:35
#19 0x00007f2bc282a1ca __libc_start_call_main ./csu/../sysdeps/nptl/libc_start_call_main.h:74:3
#20 0x00007f2bc282a28b call_init ./csu/../csu/libc-start.c:128:20
#21 0x00007f2bc282a28b __libc_start_main ./csu/../csu/libc-start.c:347:5
#22 0x000055e59edf1065 _start (./llvm-project/build-debug/bin/llc+0x801065)
Aborted (core dumped)
```

###  Additional info
I localized the source of the fail in `Verifier::visitGlobalVariable` function during:
```
    840 if (GV.hasName() && (GV.getName() == "llvm.global_ctors" ||
    841 GV.getName() == "llvm.global_dtors")) {
    842 Check(!GV.hasInitializer() || GV.hasAppendingLinkage(),
    843 "invalid linkage for intrinsic global variable", &GV);
    844 Check(GV.materialized_use_empty(),
    845           "invalid uses of intrinsic global variable", &GV);
```
It sets Broken variable to true in `Check(GV.materialized_use_empty()` which checks if `UseList == nullptr` if not it fails.
I checked the same example with X86 backend and it works without fail.

</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJy8WF1v46rW_jX0ZskRBtuxL3qRfmTeSp13qnbv6txFGJOEUwwW4H7sX38EtpvE0z0ze87MqaImNvCsZ32wFgvmnNxpIc5RfoHyqzPW-72x5_WLMg2z_K-z2jRv54jQ4QP3orOm6bmwCF8hvNqIV9Z2Six4122GV6jA4yc-Su0BkVXn7QYDolege6U6bxG9GCY8G9lMM9I4I859H5_hpQsArpjeQfIAiWilT5R6biHhKnG-QfSKK47IBSIXkCSe2Z3wiF65Tlo4IjuAkQUoxSFJnoWV27ekZXwvtZDaeesg-YIhab2VnRIjxHORJb1-0uZFT9_vqEpBYiABgug1IkU60h_-v1vwwTP-BH9YxsXf2OuZKdlA74QDswWpvZXaSQ47ZWqm4JlZyWo1ru68BZThYILFMGHDvbFuGL29ffwM1_f3X-4RXcGFNU9CQ2uaXgnYml43iFwCN20nFfPSaGC1sV40iIzs726vVw_X4Pq6lR4Y1P0OrOiM9eAN7L3vHKIrRNaIrHfS7_t6wU2LyDoQGr-Szpp_C-4RWUvneuEQWQPTDUjNVd8I8HsB3DK3h5rxJx9MsxjED8Zq-rYLUuIrvID4d2fNzrIWmN31rdA-8PgFzpxsGOhMHh0EAyIUA37FGOM8FzlLC47FkkBUla4QXbk3N_y4s1L7SH5wNCkPkyx72RjnrWAtIkVwgNQekQoQvfS215wFB9Drmekmg8oakfVD3wUfILL-U8vX8ELuNFNuITVHdLUkFNEVIQfi6Yx4SsoKZiwHjP9julHCIlKGjYnI6hdTq0pEV-mBGTllVosirz4y6X2vTwg6RMqfpjZxComArlK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