Issue 240072
Summary [WebAssembly][Clang] numerical type usage in global scope initialized to 0
Labels clang
Assignees
Reporter jeremy-code
    Create a global (per [this comment](https://github.com/llvm/llvm-project/issues/141011#issuecomment-3815916985), WebAssembly globals have to have `address_space(1)`) and initialize it to some value. The const is not necessary, this works with mutable globals too. The compiled code will have that global initialized to 0.

```c
__attribute__((address_space(1), visibility("default"))) const int num = 4;
```

```shell
clang \
  --target=wasm32 \
 --no-standard-libraries \
  -Wl,--export=num \
  -Wl,--no-entry \
  -o ./example.wasm \
  ./example.c
```

```wat
(module $example.wasm
 (memory (;0;) 1)
  (global $num i32 (i32.const 0))
  (export "memory" (memory 0))
  (export "num" (global $num))
  (@custom "name" "\00\0d\0cexample.wasm\07\06\01\00\03num")
  (@custom "producers" "\01\0cprocessed-by\01\0eHomebrew clang\0623.1.4")
 (@custom "target_features" "\08+\0bbulk-memory+\0fbulk-memory-opt+\16call-indirect-overlong+\0amultivalue+\0fmutable-globals+\13nontrapping-fptoint+\0freference-types+\08sign-ext"))
```

```
import source mod from "./dist/example.wasm";

const instance = await WebAssembly.instantiate(mod);

console.log(instance.exports.num.valueOf());
```

```shell
node index.js
> 0
```
_______________________________________________
llvm-bugs mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs

Reply via email to