Skip to content

Commit f7b2c2e

Browse files
authored
[openmp][WebAssembly] Allow openmp to compile and run under emscripten toolchain (#95169)
* Separate wasi and emscripten as they have different constraints and abilities * Emscripten mimics Linux/POSIX by statically linking the musl runtime. This allow nearly all KMP_OS_LINUX code paths to work correctly. There are only a few places that need to be adjusted related to dynamic linking (dl_open) * Internally link openmp globals * With CommonLinkage it is needed to emit them in an assembly file, now they are defined and used within each compilation unit * With ExternalLinkage they suffer from duplicate symbols during linking for unnamed globals like reduction/critical * Interestingly this aligns with the TODO comment above this code
1 parent 6f8e8fa commit f7b2c2e

File tree

4 files changed

+10
-24
lines changed

4 files changed

+10
-24
lines changed

llvm/lib/Frontend/OpenMP/OMPIRBuilder.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7211,7 +7211,7 @@ OpenMPIRBuilder::getOrCreateInternalVariable(Type *Ty, const StringRef &Name,
72117211
// create different versions of the function for different OMP internal
72127212
// variables.
72137213
auto Linkage = this->M.getTargetTriple().rfind("wasm32") == 0
7214-
? GlobalValue::ExternalLinkage
7214+
? GlobalValue::InternalLinkage
72157215
: GlobalValue::CommonLinkage;
72167216
auto *GV = new GlobalVariable(M, Ty, /*IsConstant=*/false, Linkage,
72177217
Constant::getNullValue(Ty), Elem.first(),

openmp/runtime/src/kmp_os.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@
7777

7878
#if (KMP_OS_LINUX || KMP_OS_WINDOWS || KMP_OS_FREEBSD || KMP_OS_NETBSD || \
7979
KMP_OS_DRAGONFLY || KMP_OS_AIX) && \
80-
!KMP_OS_WASI
80+
!KMP_OS_WASI && !KMP_OS_EMSCRIPTEN
8181
#define KMP_AFFINITY_SUPPORTED 1
8282
#if KMP_OS_WINDOWS && KMP_ARCH_X86_64
8383
#define KMP_GROUP_AFFINITY 1
@@ -1293,7 +1293,7 @@ bool __kmp_atomic_compare_store_rel(std::atomic<T> *p, T expected, T desired) {
12931293
extern void *__kmp_lookup_symbol(const char *name, bool next = false);
12941294
#define KMP_DLSYM(name) __kmp_lookup_symbol(name)
12951295
#define KMP_DLSYM_NEXT(name) __kmp_lookup_symbol(name, true)
1296-
#elif KMP_OS_WASI
1296+
#elif KMP_OS_WASI || KMP_OS_EMSCRIPTEN
12971297
#define KMP_DLSYM(name) nullptr
12981298
#define KMP_DLSYM_NEXT(name) nullptr
12991299
#else

openmp/runtime/src/kmp_platform.h

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
#define KMP_OS_HURD 0
2626
#define KMP_OS_SOLARIS 0
2727
#define KMP_OS_WASI 0
28+
#define KMP_OS_EMSCRIPTEN 0
2829
#define KMP_OS_UNIX 0 /* disjunction of KMP_OS_LINUX, KMP_OS_DARWIN etc. */
2930

3031
#ifdef _WIN32
@@ -44,6 +45,11 @@
4445
#elif (defined __linux__)
4546
#undef KMP_OS_LINUX
4647
#define KMP_OS_LINUX 1
48+
#elif defined(__EMSCRIPTEN__)
49+
#undef KMP_OS_LINUX
50+
#undef KMP_OS_EMSCRIPTEN
51+
#define KMP_OS_LINUX 1
52+
#define KMP_OS_EMSCRIPTEN 1
4753
#else
4854
#endif
4955

@@ -77,7 +83,7 @@
7783
#define KMP_OS_SOLARIS 1
7884
#endif
7985

80-
#if (defined __wasi__) || (defined __EMSCRIPTEN__)
86+
#if (defined __wasi__)
8187
#undef KMP_OS_WASI
8288
#define KMP_OS_WASI 1
8389
#endif

openmp/runtime/src/z_Linux_asm.S

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -2452,23 +2452,3 @@ KMP_PREFIX_UNDERSCORE(__kmp_unnamed_critical_addr):
24522452
.section .note.GNU-stack,"",@progbits
24532453
# endif
24542454
#endif
2455-
2456-
#if KMP_ARCH_WASM
2457-
.data
2458-
.global .gomp_critical_user_
2459-
.global .gomp_critical_user_.var
2460-
.global .gomp_critical_user_.reduction.var
2461-
.global __kmp_unnamed_critical_addr
2462-
.gomp_critical_user_:
2463-
.zero 4
2464-
.size .gomp_critical_user_, 4
2465-
.gomp_critical_user_.var:
2466-
.zero 4
2467-
.size .gomp_critical_user_.var, 4
2468-
.gomp_critical_user_.reduction.var:
2469-
.zero 4
2470-
.size .gomp_critical_user_.reduction.var, 4
2471-
__kmp_unnamed_critical_addr:
2472-
.4byte .gomp_critical_user_
2473-
.size __kmp_unnamed_critical_addr, 4
2474-
#endif

0 commit comments

Comments
 (0)