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

    <tr>
        <th>Summary</th>
        <td>
            [llvm-link] Crash on linking global_ctors without opaque pointers
        </td>
    </tr>

    <tr>
      <th>Labels</th>
      <td>
            bug
      </td>
    </tr>

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

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

<pre>
    `llvm-link` runs into verification errors when linking two files with `global_ctors` that do not use opaque pointers.

Executing `llvm-link --opaque-pointers=0 f0.ll f1.ll` (see files further down) results in the following crash:

```
invalid uses of intrinsic global variable
[2 x { i32, void ()*, i8* }]* @llvm.global_ctors
./bin/llvm-link: error: linked module is broken!
```

**Files:**

f0.ll:
```llvm
@llvm.global_ctors = appending global [1 x { i32, void()*, i8* }] [{ i32, void()*, i8* } { i32 0, void()* @foo, i8* null }]

define void @foo() {
  ret void
}
```

f1.ll:
```llvm
@llvm.global_ctors = appending global [1 x { i32, void()*, i8* }] [{ i32, void()*, i8* } { i32 0, void()* @bar, i8* null }]

define void @bar() {
  ret void
}
```

---

git bisect indicates that this crash starts to occur since b67c16ff6af557174e498d6d82df9936e333f3d6 by @arsenm. However, this commit only adds a verifier that checks an assumption used all over the place, so this is not the root cause of this problem. Note that the commit message even indicates that there were some unexpected problems. 

After some debugging, I found out that the introduced bitcasts in `IRLinker::linkAppendingVarProto` seem to stick around, even though they should have no usages . 

See: https://github.com/llvm/llvm-project/blob/117ecdd5789e6efd28505a8b84680020fd935cd1/llvm/lib/Linker/IRMover.cpp#L975 https://github.com/llvm/llvm-project/blob/117ecdd5789e6efd28505a8b84680020fd935cd1/llvm/lib/Linker/IRMover.cpp#L986

I tried to address this issue by avoiding these bitcasts for magic globals (`llvm.global_ctors` and `llvm.global_dtors`) but I'm getting issues with unmaterialized values in tests using `llvm-lto`.

</pre>
<img width="1px" height="1px" alt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