[clang] [libcxxabi] [llvm] Add support for WASI builds (PR #91051)
https://github.com/veluca93 updated https://github.com/llvm/llvm-project/pull/91051 >From a573b261c878c26e74831b101287945b6c414fc9 Mon Sep 17 00:00:00 2001 From: Luca Versari Date: Wed, 1 May 2024 15:42:57 +0200 Subject: [PATCH 1/2] Adapt the build system for WASI. --- clang/CMakeLists.txt | 2 +- libcxxabi/src/CMakeLists.txt | 4 ++-- llvm/cmake/modules/HandleLLVMOptions.cmake | 4 llvm/lib/CMakeLists.txt| 2 ++ llvm/lib/Transforms/CMakeLists.txt | 2 ++ llvm/tools/CMakeLists.txt | 4 6 files changed, 15 insertions(+), 3 deletions(-) diff --git a/clang/CMakeLists.txt b/clang/CMakeLists.txt index 2ac0bccb42f50..e1da8297ddd5f 100644 --- a/clang/CMakeLists.txt +++ b/clang/CMakeLists.txt @@ -426,7 +426,7 @@ CMAKE_DEPENDENT_OPTION(CLANG_PLUGIN_SUPPORT # If libstdc++ is statically linked, clang-repl needs to statically link libstdc++ # itself, which is not possible in many platforms because of current limitations in # JIT stack. (more platforms need to be supported by JITLink) -if(NOT LLVM_STATIC_LINK_CXX_STDLIB) +if(NOT LLVM_STATIC_LINK_CXX_STDLIB AND NOT WASI) set(HAVE_CLANG_REPL_SUPPORT ON) endif() diff --git a/libcxxabi/src/CMakeLists.txt b/libcxxabi/src/CMakeLists.txt index c54ced4dc3ea8..f96d23b3c0315 100644 --- a/libcxxabi/src/CMakeLists.txt +++ b/libcxxabi/src/CMakeLists.txt @@ -36,8 +36,8 @@ else() ) endif() -if (LIBCXXABI_ENABLE_THREADS AND (UNIX OR FUCHSIA) AND NOT (APPLE OR CYGWIN) -AND NOT (${CMAKE_SYSTEM_NAME} MATCHES "AIX")) +if (LIBCXXABI_ENABLE_THREADS AND (UNIX OR FUCHSIA OR WASI) AND NOT + (APPLE OR CYGWIN) AND NOT (${CMAKE_SYSTEM_NAME} MATCHES "AIX")) list(APPEND LIBCXXABI_SOURCES cxa_thread_atexit.cpp ) diff --git a/llvm/cmake/modules/HandleLLVMOptions.cmake b/llvm/cmake/modules/HandleLLVMOptions.cmake index 5ca580fbb59c5..11cc1af78b9ba 100644 --- a/llvm/cmake/modules/HandleLLVMOptions.cmake +++ b/llvm/cmake/modules/HandleLLVMOptions.cmake @@ -217,6 +217,10 @@ elseif(FUCHSIA OR UNIX) else() set(LLVM_HAVE_LINK_VERSION_SCRIPT 1) endif() +elseif(WASI) + set(LLVM_ON_WIN32 0) + set(LLVM_ON_UNIX 1) + set(LLVM_HAVE_LINK_VERSION_SCRIPT 0) elseif(CMAKE_SYSTEM_NAME STREQUAL "Generic") set(LLVM_ON_WIN32 0) set(LLVM_ON_UNIX 0) diff --git a/llvm/lib/CMakeLists.txt b/llvm/lib/CMakeLists.txt index 74e2d03c07953..b32411ae0b860 100644 --- a/llvm/lib/CMakeLists.txt +++ b/llvm/lib/CMakeLists.txt @@ -31,7 +31,9 @@ add_subdirectory(Remarks) add_subdirectory(Debuginfod) add_subdirectory(DebugInfo) add_subdirectory(DWP) +if (NOT WASI) add_subdirectory(ExecutionEngine) +endif () add_subdirectory(Target) add_subdirectory(AsmParser) add_subdirectory(LineEditor) diff --git a/llvm/lib/Transforms/CMakeLists.txt b/llvm/lib/Transforms/CMakeLists.txt index 84a7e34147d08..1843abf1bdaa4 100644 --- a/llvm/lib/Transforms/CMakeLists.txt +++ b/llvm/lib/Transforms/CMakeLists.txt @@ -5,7 +5,9 @@ add_subdirectory(InstCombine) add_subdirectory(Scalar) add_subdirectory(IPO) add_subdirectory(Vectorize) +if (NOT WASI) add_subdirectory(Hello) +endif () add_subdirectory(ObjCARC) add_subdirectory(Coroutines) add_subdirectory(CFGuard) diff --git a/llvm/tools/CMakeLists.txt b/llvm/tools/CMakeLists.txt index db66dad5dc0db..acd3cde1f85c1 100644 --- a/llvm/tools/CMakeLists.txt +++ b/llvm/tools/CMakeLists.txt @@ -28,12 +28,14 @@ endif() # Add LTO, llvm-ar, llvm-config, and llvm-profdata before clang, ExternalProject # requires targets specified in DEPENDS to exist before the call to # ExternalProject_Add. +if (NOT WASI) add_llvm_tool_subdirectory(lto) add_llvm_tool_subdirectory(gold) add_llvm_tool_subdirectory(llvm-ar) add_llvm_tool_subdirectory(llvm-config) add_llvm_tool_subdirectory(llvm-lto) add_llvm_tool_subdirectory(llvm-profdata) +endif () # Projects supported via LLVM_EXTERNAL_*_SOURCE_DIR need to be explicitly # specified. @@ -43,6 +45,7 @@ add_llvm_external_project(mlir) # accordingly so place them afterwards add_llvm_external_project(clang) add_llvm_external_project(flang) +if (NOT WASI) add_llvm_external_project(lldb) add_llvm_external_project(bolt) @@ -54,6 +57,7 @@ add_llvm_external_project(polly) # libclc depends on clang add_llvm_external_project(libclc) +endif () # Add subprojects specified using LLVM_EXTERNAL_PROJECTS foreach(p ${LLVM_EXTERNAL_PROJECTS}) >From e1c9c7a0c03f82e714dca866395c7b796ea7ddc2 Mon Sep 17 00:00:00 2001 From: Luca Versari Date: Wed, 1 May 2024 22:20:56 +0200 Subject: [PATCH 2/2] Add support for building clang on WASI. --- clang/lib/Driver/Driver.cpp | 2 +- llvm/include/llvm/ADT/bit.h | 4 +- llvm/include/llvm/Support/Memory.h| 2 + llvm/lib/Support/CrashRecoveryContext.cpp | 48 ++- llvm/lib/Support/LockFileManager.cpp | 19 + llvm/lib/Support/Unix/Memory.inc | 20 ++ llvm/lib/Support/Unix/Path.inc| 44 +
[clang] [libcxxabi] [llvm] Add support for WASI builds (PR #91051)
https://github.com/veluca93 closed https://github.com/llvm/llvm-project/pull/91051 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [libcxxabi] [llvm] Add support for WASI builds (PR #91051)
veluca93 wrote: Will close this PR for now waiting for the other one to go through - if that does not happen, feel free to reopen it! https://github.com/llvm/llvm-project/pull/91051 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [libcxxabi] [llvm] Add support for WASI builds (PR #91051)
https://github.com/veluca93 created https://github.com/llvm/llvm-project/pull/91051 This PR modifies the LLVM source code to compile (and run) in a WASI environment. The question of whether having WASI support in LLVM is one that doesn't have a clear answer for me (although of course I can see some use cases), but since the patch ended up being small enough I figured I'd create a PR and see what the LLVM community would make of it :-) The code compiles & runs successfully when compiled with an unmodified wasi-libc (I only tested running clang++ and wasm-ld). Caveats: - a bunch of things are not supported. Most importantly, executing binaries is not supported, but also memory mapping, signals, dynamic linking, set/longjump, and JIT. - There are no tests. I also would not know how to go about adding tests. - Some projects are disabled on WASI. >From efed655cadea23fe2bf2e3bd4b555f0a815af4fc Mon Sep 17 00:00:00 2001 From: Luca Versari Date: Wed, 1 May 2024 15:42:57 +0200 Subject: [PATCH 1/2] Adapt the build system for WASI. --- clang/CMakeLists.txt | 2 +- libcxxabi/src/CMakeLists.txt | 4 ++-- llvm/cmake/modules/HandleLLVMOptions.cmake | 4 llvm/lib/CMakeLists.txt| 2 ++ llvm/lib/Transforms/CMakeLists.txt | 2 ++ llvm/tools/CMakeLists.txt | 4 6 files changed, 15 insertions(+), 3 deletions(-) diff --git a/clang/CMakeLists.txt b/clang/CMakeLists.txt index cf97e3c6e851ae..8e2a59566702fd 100644 --- a/clang/CMakeLists.txt +++ b/clang/CMakeLists.txt @@ -427,7 +427,7 @@ CMAKE_DEPENDENT_OPTION(CLANG_PLUGIN_SUPPORT # If libstdc++ is statically linked, clang-repl needs to statically link libstdc++ # itself, which is not possible in many platforms because of current limitations in # JIT stack. (more platforms need to be supported by JITLink) -if(NOT LLVM_STATIC_LINK_CXX_STDLIB) +if(NOT LLVM_STATIC_LINK_CXX_STDLIB AND NOT WASI) set(HAVE_CLANG_REPL_SUPPORT ON) endif() diff --git a/libcxxabi/src/CMakeLists.txt b/libcxxabi/src/CMakeLists.txt index c8cc93de50777b..9c86ba7051ad1c 100644 --- a/libcxxabi/src/CMakeLists.txt +++ b/libcxxabi/src/CMakeLists.txt @@ -36,8 +36,8 @@ else() ) endif() -if (LIBCXXABI_ENABLE_THREADS AND (UNIX OR FUCHSIA) AND NOT (APPLE OR CYGWIN) -AND NOT (${CMAKE_SYSTEM_NAME} MATCHES "AIX")) +if (LIBCXXABI_ENABLE_THREADS AND (UNIX OR FUCHSIA OR WASI) AND NOT + (APPLE OR CYGWIN) AND NOT (${CMAKE_SYSTEM_NAME} MATCHES "AIX")) list(APPEND LIBCXXABI_SOURCES cxa_thread_atexit.cpp ) diff --git a/llvm/cmake/modules/HandleLLVMOptions.cmake b/llvm/cmake/modules/HandleLLVMOptions.cmake index 185266c0861e86..292a5c957f1104 100644 --- a/llvm/cmake/modules/HandleLLVMOptions.cmake +++ b/llvm/cmake/modules/HandleLLVMOptions.cmake @@ -217,6 +217,10 @@ elseif(FUCHSIA OR UNIX) else() set(LLVM_HAVE_LINK_VERSION_SCRIPT 1) endif() +elseif(WASI) + set(LLVM_ON_WIN32 0) + set(LLVM_ON_UNIX 1) + set(LLVM_HAVE_LINK_VERSION_SCRIPT 0) elseif(CMAKE_SYSTEM_NAME STREQUAL "Generic") set(LLVM_ON_WIN32 0) set(LLVM_ON_UNIX 0) diff --git a/llvm/lib/CMakeLists.txt b/llvm/lib/CMakeLists.txt index 74e2d03c07953d..b32411ae0b8600 100644 --- a/llvm/lib/CMakeLists.txt +++ b/llvm/lib/CMakeLists.txt @@ -31,7 +31,9 @@ add_subdirectory(Remarks) add_subdirectory(Debuginfod) add_subdirectory(DebugInfo) add_subdirectory(DWP) +if (NOT WASI) add_subdirectory(ExecutionEngine) +endif () add_subdirectory(Target) add_subdirectory(AsmParser) add_subdirectory(LineEditor) diff --git a/llvm/lib/Transforms/CMakeLists.txt b/llvm/lib/Transforms/CMakeLists.txt index 84a7e34147d084..1843abf1bdaa46 100644 --- a/llvm/lib/Transforms/CMakeLists.txt +++ b/llvm/lib/Transforms/CMakeLists.txt @@ -5,7 +5,9 @@ add_subdirectory(InstCombine) add_subdirectory(Scalar) add_subdirectory(IPO) add_subdirectory(Vectorize) +if (NOT WASI) add_subdirectory(Hello) +endif () add_subdirectory(ObjCARC) add_subdirectory(Coroutines) add_subdirectory(CFGuard) diff --git a/llvm/tools/CMakeLists.txt b/llvm/tools/CMakeLists.txt index db66dad5dc0dbd..acd3cde1f85c11 100644 --- a/llvm/tools/CMakeLists.txt +++ b/llvm/tools/CMakeLists.txt @@ -28,12 +28,14 @@ endif() # Add LTO, llvm-ar, llvm-config, and llvm-profdata before clang, ExternalProject # requires targets specified in DEPENDS to exist before the call to # ExternalProject_Add. +if (NOT WASI) add_llvm_tool_subdirectory(lto) add_llvm_tool_subdirectory(gold) add_llvm_tool_subdirectory(llvm-ar) add_llvm_tool_subdirectory(llvm-config) add_llvm_tool_subdirectory(llvm-lto) add_llvm_tool_subdirectory(llvm-profdata) +endif () # Projects supported via LLVM_EXTERNAL_*_SOURCE_DIR need to be explicitly # specified. @@ -43,6 +45,7 @@ add_llvm_external_project(mlir) # accordingly so place them afterwards add_llvm_external_project(clang) add_llvm_external_project(flang) +if (NOT WASI) add_llvm_external_project(lldb) add_llvm_external_project(bolt) @@ -54,6 +57,7 @@ ad
[clang] [libcxxabi] [llvm] Add support for WASI builds (PR #91051)
veluca93 wrote: Ping (I suspect that the assigned reviewers are likely not the most appropriate, but I would be unsure on how to change them and to whom) https://github.com/llvm/llvm-project/pull/91051 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [libcxxabi] [llvm] Add support for WASI builds (PR #91051)
veluca93 wrote: > https://discourse.llvm.org/t/rfc-building-llvm-for-webassembly/79073 and PR > #92677 also just arrived at the scene with a different take on implementing > the same thing. Looks like there's significant interest in this area! That's cool! That PR seems significantly more polished than mine, happy to go with that one instead :-) https://github.com/llvm/llvm-project/pull/91051 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits