Skip to content

Commit 3555a3b

Browse files
committed
Initial commit
0 parents  commit 3555a3b

File tree

6 files changed

+104
-0
lines changed

6 files changed

+104
-0
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
build
2+
*.wasm

.gitmodules

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
[submodule "llvm-project"]
2+
path = llvm-project
3+
url = https://github.com/abrown/llvm-project
4+
branch = wasm-openmp
5+
shallow = true

Makefile

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
PROJECT_DIR := $(realpath .)
2+
BUILD_DIR := $(PROJECT_DIR)/build
3+
4+
# Specify a different wasi-sdk with `make WASI_SDK_DIR=...`.
5+
WASI_SDK_DIR := /opt/wasi-sdk/latest
6+
WASI_CC := $(WASI_SDK_DIR)/bin/clang
7+
WASI_SYSROOT := $(WASI_SDK_DIR)/share/wasi-sysroot
8+
TARGET_FLAGS=--target=wasm32-wasi-threads -pthread
9+
WASI_LIBC_FLAGS=-D_WASI_EMULATED_SIGNAL -D_WASI_EMULATED_PROCESS_CLOCKS
10+
SYSROOT_FLAGS=--sysroot=$(WASI_SYSROOT)
11+
12+
# Specify a different location for the OpenMP sources with `make OPENMP_DIR=...`.
13+
OPENMP_DIR := $(PROJECT_DIR)/llvm-project/openmp
14+
OPENMP_LIB := $(BUILD_DIR)/runtime/src/libomp.a
15+
16+
# Specify a different Wasm engine to invoke with `make WASM_ENGINE=...`.
17+
WASM_ENGINE=WASMTIME_LOG=wasmtime_runtime::memory=trace,wasmtime_wasi_threads=trace ../wasmtime/target/release/wasmtime
18+
19+
# The default target--builds and runs everything.
20+
all: run
21+
22+
# Run the compiled example in a WebAssembly engine.
23+
run: example.wasm
24+
$(WASM_ENGINE) -W threads -S threads $^
25+
26+
# Compile the example; note how we will need the `libomp.a` runtime.
27+
example.wasm: example.c $(OPENMP_LIB)
28+
make check-wasi-sdk
29+
$(WASI_CC) -fopenmp=libomp -g --sysroot=$(WASI_SYSROOT) --target=wasm32-wasi-threads \
30+
-I$(BUILD_DIR)/runtime/src -I$(WASI_SYSROOT)/include -pthread \
31+
-Wl,--import-memory,--export-memory,--max-memory=67108864 example.c \
32+
-L$(BUILD_DIR)/runtime/src -lomp \
33+
-lwasi-emulated-getpid \
34+
-o example.wasm
35+
36+
# Compile the `libomp.a` runtime library to WebAssembly. This uses the PR changes from
37+
# https://github.com/llvm/llvm-project/pull/71297.
38+
libomp: $(OPENMP_LIB)
39+
$(OPENMP_LIB): $(OPENMP_DIR)
40+
make check-wasi-sdk
41+
cmake \
42+
-DCMAKE_BUILD_TYPE=Debug \
43+
-DCMAKE_C_COMPILER="$(WASI_SDK_DIR)/bin/clang" \
44+
-DCMAKE_C_FLAGS="$(TARGET_FLAGS) $(WASI_LIBC_FLAGS) $(SYSROOT_FLAGS)" \
45+
-DCMAKE_CXX_COMPILER="$(WASI_SDK_DIR)/bin/clang++" \
46+
-DCMAKE_CXX_FLAGS="$(TARGET_FLAGS) $(WASI_LIBC_FLAGS) $(SYSROOT_FLAGS)" \
47+
-DCMAKE_LINKER="${WASI_SDK}/bin/ld.lld" \
48+
-B $(BUILD_DIR) \
49+
-S $(OPENMP_DIR)
50+
cmake --build $(BUILD_DIR)
51+
$(OPENMP_DIR):
52+
$(error cannot find $@; retrieve the submodule with `git submodule update --init` or rerun with `make OPENMP_DIR=...`)
53+
54+
# Check that the parts of wasi-sdk we need are present; if they're not, fail.
55+
check-wasi-sdk: $(WASI_CC) $(WASI_SYSROOT)
56+
$(WASI_CC) $(WASI_SYSROOT):
57+
$(error cannot find $@; install wasi-sdk from https://github.com/WebAssembly/wasi-sdk/releases and rerun with `make WASI_SDK_DIR=...`)
58+
59+
clean:
60+
rm -f *.wasm
61+
rm -rf $(BUILD_DIR)

README.md

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
`wasm-openmp-examples`
2+
======================
3+
4+
This project contains some sample code demonstrating how to build and run OpenMP code in a
5+
WebAssembly engine. There are several parts to the [`Makefile`]:
6+
- `make libomp`: first, we show how to compile `libomp.a`, the OpenMP runtime, to WebAssembly using
7+
support for [wasi-threads] in [wasi-sdk]
8+
- `make examples`: then, we compile our examples, `example*.c`, to WebAssembly and link them with
9+
`libomp.a`
10+
- `make run`: finally, we run the compiled WebAssembly module in the [wasmtime] engine
11+
- optionally, we show how to patch Clang to avoid a crash with `#pragma omp critical`
12+
13+
[`Makefile`]: ./Makefile
14+
[wasi-threads]: https://github.com/WebAssembly/wasi-threads
15+
[wasi-sdk]: https://github.com/WebAssembly/wasi-sdk
16+
[wasmtime]: https://github.com/bytecodealliance/wasmtime
17+
18+
The work to upstream the "compile-OpenMP-to-WebAssembly" is available at: [llvm-project#71297].
19+
20+
[llvm-project#71297]: https://github.com/llvm/llvm-project/pull/71297
21+
22+
### Build and Run
23+
24+
To execute all Makefile targets in sequence, run:
25+
26+
```
27+
make
28+
```

example.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#include <omp.h>
2+
#include <stdio.h>
3+
4+
int main(int argc, char** argv) {
5+
#pragma omp parallel num_threads(8)
6+
{ printf("Hello World... from thread = %d\n", omp_get_thread_num()); }
7+
}

llvm-project

Submodule llvm-project added at 5bfd89b

0 commit comments

Comments
 (0)