llvm / llvm-project

The LLVM Project is a collection of modular and reusable compiler and toolchain technologies.
http://llvm.org
Other
29.39k stars 12.15k forks source link

[WebAssembly] Inbounds GEPs do not get folded into relocation addends #65793

Open SingleAccretion opened 1 year ago

SingleAccretion commented 1 year ago

Reproduction:

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.

llvmbot commented 1 year ago

@llvm/issue-subscribers-backend-webassembly