-
Notifications
You must be signed in to change notification settings - Fork 14.3k
[openmp][WebAssembly] Allow openmp to compile and run under emscripten toolchain #95169
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
This change allows building the static OpenMP runtime, `libomp.a`, as WebAssembly. It builds on the work done in [D142593] but goes further in several ways: - it makes the OpenMP CMake files more WebAssembly-aware - it conditions much more code (or code that had been refactored since [D142593]) for `KMP_ARCH_WASM` and `KMP_OS_WASI` - it fixes a Clang crash due to unimplemented common symbols in WebAssembly The commit messages have more details. Please understand this PR as a start, not the completed work, for WebAssembly support in OpenMP. Getting the tests running somehow would be a good next step, e.g.; but what is contained here works, at least with recent versions of [wasi-sdk] and engines that support [wasi-threads]. I suspect the same is true for Emscripten and browsers, but I have not tested that workflow. [D142593]: https://reviews.llvm.org/D142593 [wasi-sdk]: https://github.com/WebAssembly/wasi-sdk [wasi-threads]: https://github.com/WebAssembly/wasi-threads --------- Co-authored-by: Atanas Atanasov <[email protected]>
I haven't dug into this PR much yet review-wise but I think it is on the right track:
I tested this PR out in a simplistic WASI example 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? |
Yea I can supply an emscripten variant, I'll fork your branch, I'll add a few more openmp scenarios as well to it to mimic the various link scenarios I encountered as well |
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
CommonLinkage
InternalLinkage
The open question i have is why is wasm target different here? Native platforms seem to work fine with |
@llvm/pr-subscribers-flang-openmp Author: arsnyder16 (arsnyder16) Changes
Full diff: https://github.com/llvm/llvm-project/pull/95169.diff 4 Files Affected:
diff --git a/llvm/lib/Frontend/OpenMP/OMPIRBuilder.cpp b/llvm/lib/Frontend/OpenMP/OMPIRBuilder.cpp
index 5154c33502526..01c8ae3a6e867 100644
--- a/llvm/lib/Frontend/OpenMP/OMPIRBuilder.cpp
+++ b/llvm/lib/Frontend/OpenMP/OMPIRBuilder.cpp
@@ -5356,7 +5356,7 @@ OpenMPIRBuilder::getOrCreateInternalVariable(Type *Ty, const StringRef &Name,
// create different versions of the function for different OMP internal
// variables.
auto Linkage = this->M.getTargetTriple().rfind("wasm32") == 0
- ? GlobalValue::ExternalLinkage
+ ? GlobalValue::InternalLinkage
: GlobalValue::CommonLinkage;
auto *GV = new GlobalVariable(M, Ty, /*IsConstant=*/false, Linkage,
Constant::getNullValue(Ty), Elem.first(),
diff --git a/openmp/runtime/src/kmp_os.h b/openmp/runtime/src/kmp_os.h
index 24b40ed32d988..2252f5e7e97a7 100644
--- a/openmp/runtime/src/kmp_os.h
+++ b/openmp/runtime/src/kmp_os.h
@@ -77,7 +77,7 @@
#if (KMP_OS_LINUX || KMP_OS_WINDOWS || KMP_OS_FREEBSD || KMP_OS_NETBSD || \
KMP_OS_DRAGONFLY || KMP_OS_AIX) && \
- !KMP_OS_WASI
+ !KMP_OS_WASI && !KMP_OS_EMSCRIPTEN
#define KMP_AFFINITY_SUPPORTED 1
#if KMP_OS_WINDOWS && KMP_ARCH_X86_64
#define KMP_GROUP_AFFINITY 1
@@ -1293,7 +1293,7 @@ bool __kmp_atomic_compare_store_rel(std::atomic<T> *p, T expected, T desired) {
extern void *__kmp_lookup_symbol(const char *name, bool next = false);
#define KMP_DLSYM(name) __kmp_lookup_symbol(name)
#define KMP_DLSYM_NEXT(name) __kmp_lookup_symbol(name, true)
-#elif KMP_OS_WASI
+#elif KMP_OS_WASI || KMP_OS_EMSCRIPTEN
#define KMP_DLSYM(name) nullptr
#define KMP_DLSYM_NEXT(name) nullptr
#else
diff --git a/openmp/runtime/src/kmp_platform.h b/openmp/runtime/src/kmp_platform.h
index 200fdf697dd0e..9c2215140467d 100644
--- a/openmp/runtime/src/kmp_platform.h
+++ b/openmp/runtime/src/kmp_platform.h
@@ -25,6 +25,7 @@
#define KMP_OS_HURD 0
#define KMP_OS_SOLARIS 0
#define KMP_OS_WASI 0
+#define KMP_OS_EMSCRIPTEN 0
#define KMP_OS_UNIX 0 /* disjunction of KMP_OS_LINUX, KMP_OS_DARWIN etc. */
#ifdef _WIN32
@@ -44,6 +45,11 @@
#elif (defined __linux__)
#undef KMP_OS_LINUX
#define KMP_OS_LINUX 1
+#elif defined(__EMSCRIPTEN__)
+#undef KMP_OS_LINUX
+#undef KMP_OS_EMSCRIPTEN
+#define KMP_OS_LINUX 1
+#define KMP_OS_EMSCRIPTEN 1
#else
#endif
@@ -77,7 +83,7 @@
#define KMP_OS_SOLARIS 1
#endif
-#if (defined __wasi__) || (defined __EMSCRIPTEN__)
+#if (defined __wasi__)
#undef KMP_OS_WASI
#define KMP_OS_WASI 1
#endif
diff --git a/openmp/runtime/src/z_Linux_asm.S b/openmp/runtime/src/z_Linux_asm.S
index 5b614e26a8337..f119f64647daa 100644
--- a/openmp/runtime/src/z_Linux_asm.S
+++ b/openmp/runtime/src/z_Linux_asm.S
@@ -2452,23 +2452,3 @@ KMP_PREFIX_UNDERSCORE(__kmp_unnamed_critical_addr):
.section .note.GNU-stack,"",@progbits
# endif
#endif
-
-#if KMP_ARCH_WASM
-.data
-.global .gomp_critical_user_
-.global .gomp_critical_user_.var
-.global .gomp_critical_user_.reduction.var
-.global __kmp_unnamed_critical_addr
-.gomp_critical_user_:
-.zero 4
-.size .gomp_critical_user_, 4
-.gomp_critical_user_.var:
-.zero 4
-.size .gomp_critical_user_.var, 4
-.gomp_critical_user_.reduction.var:
-.zero 4
-.size .gomp_critical_user_.reduction.var, 4
-__kmp_unnamed_critical_addr:
- .4byte .gomp_critical_user_
- .size __kmp_unnamed_critical_addr, 4
-#endif
|
cc: @jpeyton52, can you take a look at this? |
@abrown @jpeyton52 Any updates here? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The changes look fine to me.
Thanks, good to merge? |
@jpeyton52 Can we get this merged? |
Sorry about that! It has been merged. |
@arsnyder16 Once something lands in LLVM, it is automatically picked up by Emscripten's tip of tree builds (which you can install with |
Uh oh!
There was an error while loading. Please reload this page.