<table border="1" cellspacing="0" cellpadding="8">
<tr>
<th>Issue</th>
<td>
<a href=https://github.com/llvm/llvm-project/issues/65793>65793</a>
</td>
</tr>
<tr>
<th>Summary</th>
<td>
[WebAssembly] Inbounds GEPs do not get folded into relocation addends
</td>
</tr>
<tr>
<th>Labels</th>
<td>
new issue
</td>
</tr>
<tr>
<th>Assignees</th>
<td>
</td>
</tr>
<tr>
<th>Reporter</th>
<td>
SingleAccretion
</td>
</tr>
</table>
<pre>
Reproduction:
```llvm
target triple = "wasm32-unknown-wasi"
@extern_data = external global i8
define ptr @func() {
%1 = getelementptr inbounds i8, ptr @extern_data, i32 4
ret ptr %1
}
```
```
clang -c repro.ll -O2
wasm-objdump -d -r repro.o
```
```
000041 func[0] <func>:
000042: 41 80 80 80 80 00 | i32.const 0
000043: R_WASM_MEMORY_ADDR_SLEB 1 <extern_data>
000048: 41 04 | i32.const 4
00004a: 6a | i32.add
00004b: 0b | end
```
Expected result: the offset addition gets folded into the relocation as an addend.
Actual result: the addition stays in code and turns into `const + const` after final linking.
We have a good number of cases in NativeAOT-LLVM where this optimization would kick in, e. g. we emit addresses of statics as `@static_base + offset`. This can be worked around by defining aliases for the referenced symbols, but that has compilation time cost.
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJyMVE1v4zYQ_TX0ZWCBJiXbOvjgrJ2iQNIUyaJBTwY_RjI3FGmQVLzpry9IOVk32BYlBNuk3jzOPM88EaPpHeKGNDek2c3EmI4-bJ6M6y1ulQqYjHcz6fXb5hFPwetR5RPCt4TuCN2SJZ0ea1-H6SiJ0GOCFMzJIhC-A8LYWcSBs_noXpw_u_lZREMYu3BMnzXF7wmDO2iRRImb9sJCb70UFsz6OkBjZxzCKQUgNe1GpwhbE9YCWd1MCADCmkWh6jGhxQFdynjjpB-djpmRfXmnuLo-nxrOoH4nCpgmGGsWlyRWu08S_HSrrHA9zBWELF9lLcwfLnVnTeZeftPjcIK5hnm4gPz_IaaU0noBpe7mhpJmB4R_K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