[Openmp-commits] [llvm] [openmp] [openmp][wasm] Allow openmp to compile and run under emscripten toolchain (PR #95169)

via Openmp-commits openmp-commits at lists.llvm.org
Thu Jun 13 11:18:09 PDT 2024


arsnyder16 wrote:

> > I haven't dug into this PR much yet review-wise but I think it is on the right track:
> > 
> > * I was shaky at best on the differences between `InternalLinkage` versus `ExternalLinkage` when I originally pieced [[openmp][wasm] Allow compiling OpenMP to WebAssembly  #71297](https://github.com/llvm/llvm-project/pull/71297) together so this seems to make sense
> > * it is true that Emscripten and WASI have quite different paradigms so it is plausible that we need to handle the Emscripten bits differently
> > 
> > I tested this PR out in a [simplistic WASI example](https://github.com/abrown/wasm-openmp-examples) and everything seemed to work as before on the WASI side. This "OpenMP + WebAssembly" support is still very experimental so I think that is probably the best we can do for now. Of course, it would be useful to have an Emscripten version of that around — @arsnyder16, do you have something like that?
> 

https://github.com/abrown/wasm-openmp-examples/pull/2

You can see a few of the issues here depending on the variants of clang and openmp, wasi vs emscripten as well.

For reference on the linkage, both wasi and emscripten suffer the same issues 

### ExternalLinkage
```
/root/wasm-openmp-examples/wasi-sdk-22.0/bin/clang -fopenmp=libomp -g --sysroot=/root/wasm-openmp-examples/wasi-sdk-22.0/share/wasi-sysroot --target=wasm32-wasi-threads \
  -I/root/wasm-openmp-examples/build/runtime/src -I/root/wasm-openmp-examples/wasi-sdk-22.0/share/wasi-sysroot/include -pthread \
  -Wl,--import-memory,--export-memory,--max-memory=67108864 example.c other.c \
  -L/root/wasm-openmp-examples/build/runtime/src -lomp \
  -lwasi-emulated-getpid \
  -o example.wasm
wasm-ld: error: duplicate symbol: .gomp_critical_user_.var
>>> defined in /tmp/example-5ca29e.o
>>> defined in /tmp/other-19151f.o

wasm-ld: error: duplicate symbol: .gomp_critical_user_NAME.var
>>> defined in /tmp/example-5ca29e.o
>>> defined in /tmp/other-19151f.o

wasm-ld: error: duplicate symbol: .gomp_critical_user_.reduction.var
>>> defined in /tmp/example-5ca29e.o
>>> defined in /tmp/other-19151f.o
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make: *** [Makefile:31: example.wasm] Error 1
```
### CommonLinkage
```
/root/wasm-openmp-examples/wasi-sdk-22.0/bin/clang -fopenmp=libomp -g --sysroot=/root/wasm-openmp-examples/wasi-sdk-22.0/share/wasi-sysroot --target=wasm32-wasi-threads \
  -I/root/wasm-openmp-examples/build/runtime/src -I/root/wasm-openmp-examples/wasi-sdk-22.0/share/wasi-sysroot/include -pthread \
  -Wl,--import-memory,--export-memory,--max-memory=67108864 example.c other.c \
  -L/root/wasm-openmp-examples/build/runtime/src -lomp \
  -lwasi-emulated-getpid \
  -o example.wasm
wasm-ld: error: /tmp/example-1bd196.o: undefined symbol: .gomp_critical_user_.var
wasm-ld: error: /tmp/example-1bd196.o: undefined symbol: .gomp_critical_user_NAME.var
wasm-ld: error: /tmp/example-1bd196.o: undefined symbol: .gomp_critical_user_.reduction.var
wasm-ld: error: /tmp/example-1bd196.o: undefined symbol: .gomp_critical_user_.reduction.var
wasm-ld: error: /tmp/example-1bd196.o: undefined symbol: .gomp_critical_user_.var
wasm-ld: error: /tmp/example-1bd196.o: undefined symbol: .gomp_critical_user_NAME.var
wasm-ld: error: /tmp/example-1bd196.o: undefined symbol: .gomp_critical_user_.reduction.var
wasm-ld: error: /tmp/example-1bd196.o: undefined symbol: .gomp_critical_user_.reduction.var
```

### InternalLinkage
```
/root/wasm-openmp-examples/wasi-sdk-22.0/bin/clang -fopenmp=libomp -g --sysroot=/root/wasm-openmp-examples/wasi-sdk-22.0/share/wasi-sysroot --target=wasm32-wasi-threads \
  -I/root/wasm-openmp-examples/build/runtime/src -I/root/wasm-openmp-examples/wasi-sdk-22.0/share/wasi-sysroot/include -pthread \
  -Wl,--import-memory,--export-memory,--max-memory=67108864 example.c other.c \
  -L/root/wasm-openmp-examples/build/runtime/src -lomp \
  -lwasi-emulated-getpid \
  -o example.wasm
/root/.wasmtime/bin/wasmtime -W threads -S threads example.wasm
Hello World... from thread = 0
Hello World... from thread = 3
Hello World... from thread = 2
Hello World... from thread = 1
Hello World... from thread = 4
Hello World... from thread = 6
Hello World... from thread = 5
Hello World... from thread = 7
example.c critical_unamed: 8
example.c critical_named: 8
example.c reduction: 8
other.c critical_unamed: 8
other.c critical_named: 8
other.c reduction: 8
```

The open question i have is why is wasm target different here? Native platforms seem to work fine with `CommonLinkage` so i am curious if there is a related issue elsewhere in clang that is not emitting the proper definitions for these variables to support CommonLinkage when compiling for wasm 

https://github.com/llvm/llvm-project/pull/95169


More information about the Openmp-commits mailing list