Issue 242985
Summary [WebAssembly] llvm-mc checks indirect-call indices as i32 for 64-bit tables
Labels new issue
Assignees
Reporter Edenlia
    ### Description

While bringing up an experimental [CoreCLR Wasm64 target](https://github.com/dotnet/runtime/issues/94108), I encountered an assembler error for an indirect call with an `i64` callee index. Reducing it to standalone assembly shows a problem independent of CoreCLR, .NET, or Emscripten: the WebAssembly assembly type checker requires `i32` for the index of `call_indirect` and `return_call_indirect`, even when the selected function table has a 64-bit address type.

There are two observable failures:

- Valid table64/i64 assembly is rejected.
- Replacing the index with i32 makes assembly succeed, but produces an invalid WebAssembly object. Independent validation rejects it; successful linking does not repair it.

### Tested version

- LLVM 24.0.0git, revision `01bf8c1e1985ac5e191ef607849c1c31b8dc8d34`, release build with assertions enabled, WebAssembly backend.
- Independent validation: Wasmtime Python 50.0.1, with memory64 and tail calls enabled.

### Minimal reproduction

Save as `repro.s`:

```asm
  .globl f
f:
  .functype f () -> ()
  i64.const 0
  call_indirect () -> ()
  end_function
```

Run with `llvm-mc` built from the revision above:

```sh
build/bin/llvm-mc -triple=wasm64-unknown-unknown \
  -mattr=+tail-call,+reference-types -filetype=obj repro.s -o repro.o
```

**Actual:** the assembler rejects the input with `error: type mismatch, expected [i32] but got [i64]`.

**Expected:** the assembler accepts the i64 index, because the selected default function table is table64.

Replacing `call_indirect` with `return_call_indirect` reproduces the same problem. `+tail-call` is required for that variant, not for ordinary `call_indirect`.

### Inverse case: invalid output is accepted

```sh
sed 's/i64.const/i32.const/' repro.s > wrong-width.s
build/bin/llvm-mc -triple=wasm64-unknown-unknown \
 -mattr=+tail-call,+reference-types -filetype=obj wrong-width.s -o wrong-width.o
build/bin/obj2yaml wrong-width.o
```

Assembly succeeds. The imported `__indirect_function_table` has `Limits.Flags: [ IS_64 ]`, while the instruction supplies an i32 index.

For independent validation, install `wasmtime==50.0.1` in a Python environment and run:

```python
from pathlib import Path
import wasmtime

config = wasmtime.Config()
config.wasm_memory64 = True
config.wasm_tail_call = True
engine = wasmtime.Engine(config)
wasmtime.Module.validate(engine, Path("wrong-width.o").read_bytes())
```

Validation fails with a type mismatch: expected i64, found i32. Corresponding linked wasm64/i32 fixtures also fail validation.

### Root cause

The stack callee index must have the selected table's address type. This is distinct from the table-number immediate and from the target's pointer width. At the tested revision, `WebAssemblyAsmTypeCheck.cpp` consumes `I32` for both indirect-call instructions.

### Related work and limitations

- #172907 requests explicit table address types in assembly.
- #173063 proposes corresponding `.tabletype` syntax and related table-operation checking. Its inspected head, `328c359cb1eb0250daf11e22311f3faad2a065fc`, does not change these indirect-call checks.
- #92042 provides historical context for table64 on wasm64; I have not bisected this issue to that change.

This report is related to, but does not replace or close, #172907. Current assembly syntax cannot independently preserve every table32/table64 combination, so crossed-width tests used actual table metadata installed in `MCContext` before invoking the parser. A separate wasm64 table32 assembly-printing round trip still loses width information and is outside this focused checker fix.

Assisted-by: OpenAI Codex
_______________________________________________
llvm-bugs mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs

Reply via email to