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

    <tr>
        <th>Summary</th>
        <td>
            [lld][WebAssembly] Linker relaxation: rewrite `global.get GOT.mem.value` to `i32.const <value>`
        </td>
    </tr>

    <tr>
      <th>Labels</th>
      <td>
            lld
      </td>
    </tr>

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

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

<pre>
    As part of our compiler, we hand-emit a small number of helper methods in pure WASM, of a kind like below:
```c
void helper_11(arg1, arg2) {
    extern int compiler_generated_data_symbol_11;
    return SomeMethod(&compiler_generated_data_symbol_11, arg1, arg2);
}
```
Historically this was done without the need to consider `-fPIC`, but recently we've started thinking about supporting that too. However, we would also like to emit code that is "PIC-agnostic" (this is how the compiler works for other targets).

In a non-PIC build, the example from above will emit something like:
```
 000042: 41 80 80 80 80 00 | i32.const 0
           000043: R_WASM_MEMORY_ADDR_SLEB 1 <compiler_generated_data_symbol_11>
```
And that's also what we emit currently (more or less).

For "PIC-agnostic" code, we could emit the same using GOT, like so:
```
 000053: 23 80 80 80 80 00          | global.get 0 <GOT.mem.compiler_generated_data_symbol_11>
           000054: R_WASM_GLOBAL_INDEX_LEB 1 <compiler_generated_data_symbol_11>
```
To keep (roughly) the same code size and runtime performance. However, we will then have a bunch of immutable `GOT.*` globals that don't really serve much purpose in a non-PIE link.

But this seems avoidable if all the references to these `GOT` globals could be rewritten in-place to `i32.const`s with the actual value.
</pre>
<img width="1" height="1" alt="" src="http://email.email.llvm.org/o/eJyklU-P4jgTxj-NuZRAjkMIHDiEfzMtdb89mm5p3j0hJykSL44d2Q5p9tOvKqF7UO-udqSNIiGBq3jq91SVpfeqMohrlmxYspvILtTWrV-UqTRmReEwKGsmuS2v68xDK10AewLbOShs0yqNjokt9Ai1NOUUGxVAgm-k1mC6JkdHx2vULTpo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