[compiler-rt] [libcxx] [llvm] [clang] [clang-tools-extra] [libunwind] [mlir] [lldb] [libcxxabi] [flang] [libc] [libunwind] Fix build for wasm (PR #79667)

via cfe-commits cfe-commits at lists.llvm.org
Sat Jan 27 21:04:26 PST 2024


https://github.com/trcrsired updated https://github.com/llvm/llvm-project/pull/79667

>From 55c3537800b0a310999f58e2b410dc5c1a6f91e4 Mon Sep 17 00:00:00 2001
From: trcrsired <uwgghhbcad at gmail.com>
Date: Fri, 26 Jan 2024 18:44:41 -0500
Subject: [PATCH 1/2] [libunwind] Fix build for wasm

The wasm unwind build appears to be dysfunctional, likely because the author has only supplied a customized LLVM build on request, rather than a fully functional patch.

This patch fixes the build

Apply formatting patch proposed by github bot

use "" to prevent CMAKE_SYSTEM_PROCESSOR not defined

[libunwind] logAPI functions should also be built
---
 libunwind/include/__libunwind_config.h |  1 +
 libunwind/include/libunwind.h          |  2 +
 libunwind/src/CMakeLists.txt           | 56 +++++++++++++++-----------
 libunwind/src/Unwind-wasm.c            | 16 ++++----
 libunwind/src/config.h                 | 15 +++----
 libunwind/src/libunwind.cpp            |  2 +
 6 files changed, 54 insertions(+), 38 deletions(-)

diff --git a/libunwind/include/__libunwind_config.h b/libunwind/include/__libunwind_config.h
index 8db336b2d727ce7..1cda20d72255005 100644
--- a/libunwind/include/__libunwind_config.h
+++ b/libunwind/include/__libunwind_config.h
@@ -180,6 +180,7 @@
 #endif
 #define _LIBUNWIND_HIGHEST_DWARF_REGISTER                                      \
   _LIBUNWIND_HIGHEST_DWARF_REGISTER_LOONGARCH
+#elif defined(__wasm__)
 # else
 #  error "Unsupported architecture."
 # endif
diff --git a/libunwind/include/libunwind.h b/libunwind/include/libunwind.h
index b2dae8feed9a3b5..63e147ae2423e28 100644
--- a/libunwind/include/libunwind.h
+++ b/libunwind/include/libunwind.h
@@ -15,6 +15,7 @@
 
 #include <__libunwind_config.h>
 
+#ifndef __wasm__
 #include <stdint.h>
 #include <stddef.h>
 
@@ -1299,5 +1300,6 @@ enum {
   UNW_LOONGARCH_F30 = 62,
   UNW_LOONGARCH_F31 = 63,
 };
+#endif
 
 #endif
diff --git a/libunwind/src/CMakeLists.txt b/libunwind/src/CMakeLists.txt
index 9c6f5d908b09454..2b21f4400797acf 100644
--- a/libunwind/src/CMakeLists.txt
+++ b/libunwind/src/CMakeLists.txt
@@ -1,31 +1,41 @@
 # Get sources
 
-set(LIBUNWIND_CXX_SOURCES
-    libunwind.cpp
-    Unwind-EHABI.cpp
-    Unwind-seh.cpp
-    )
+if("${CMAKE_SYSTEM_PROCESSOR}" STREQUAL "wasm32" OR
+  "${CMAKE_SYSTEM_PROCESSOR}" STREQUAL "wasm64")
+  set(LIBUNWIND_C_SOURCES
+      Unwind-wasm.c
+      )
+  set(LIBUNWIND_CXX_SOURCES
+      libunwind.cpp
+     )
+else()
+  set(LIBUNWIND_CXX_SOURCES
+      libunwind.cpp
+      Unwind-EHABI.cpp
+      Unwind-seh.cpp
+      )
+
+  if(${CMAKE_SYSTEM_NAME} MATCHES "AIX")
+    list(APPEND LIBUNWIND_CXX_SOURCES
+      Unwind_AIXExtras.cpp
+      )
+  endif()
 
-if(${CMAKE_SYSTEM_NAME} MATCHES "AIX")
-  list(APPEND LIBUNWIND_CXX_SOURCES
-    Unwind_AIXExtras.cpp
-    )
-endif()
+  set(LIBUNWIND_C_SOURCES
+      UnwindLevel1.c
+      UnwindLevel1-gcc-ext.c
+      Unwind-sjlj.c
+      )
 
-set(LIBUNWIND_C_SOURCES
-    UnwindLevel1.c
-    UnwindLevel1-gcc-ext.c
-    Unwind-sjlj.c
-    Unwind-wasm.c
-    )
-set_source_files_properties(${LIBUNWIND_C_SOURCES}
-                            PROPERTIES
-                              COMPILE_FLAGS "-std=c99")
+  set(LIBUNWIND_ASM_SOURCES
+      UnwindRegistersRestore.S
+      UnwindRegistersSave.S
+      )
 
-set(LIBUNWIND_ASM_SOURCES
-    UnwindRegistersRestore.S
-    UnwindRegistersSave.S
-    )
+  set_source_files_properties(${LIBUNWIND_C_SOURCES}
+                              PROPERTIES
+                                COMPILE_FLAGS "-std=c99")
+endif()
 
 set(LIBUNWIND_HEADERS
     AddressSpace.hpp
diff --git a/libunwind/src/Unwind-wasm.c b/libunwind/src/Unwind-wasm.c
index f7f39d38b59c181..87be87e9fd92a86 100644
--- a/libunwind/src/Unwind-wasm.c
+++ b/libunwind/src/Unwind-wasm.c
@@ -10,14 +10,11 @@
 //
 //===----------------------------------------------------------------------===//
 
+#if __STDC_VERSION__ < 202311L
 #include <stdbool.h>
-
+#endif
 #include "config.h"
-
-#ifdef __USING_WASM_EXCEPTIONS__
-
 #include "unwind.h"
-#include <threads.h>
 
 _Unwind_Reason_Code __gxx_personality_wasm0(int version, _Unwind_Action actions,
                                             uint64_t exceptionClass,
@@ -35,7 +32,12 @@ struct _Unwind_LandingPadContext {
 
 // Communication channel between compiler-generated user code and personality
 // function
-thread_local struct _Unwind_LandingPadContext __wasm_lpad_context;
+#if __STDC_VERSION__ >= 202311L
+thread_local
+#else
+_Thread_local
+#endif
+    struct _Unwind_LandingPadContext __wasm_lpad_context;
 
 /// Calls to this function is in landing pads in compiler-generated user code.
 /// In other EH schemes, stack unwinding is done by libunwind library, which
@@ -119,5 +121,3 @@ _LIBUNWIND_EXPORT uintptr_t
 _Unwind_GetRegionStart(struct _Unwind_Context *context) {
   return 0;
 }
-
-#endif // defined(__USING_WASM_EXCEPTIONS__)
diff --git a/libunwind/src/config.h b/libunwind/src/config.h
index deb5a4d4d73d467..2a57df41acca9f7 100644
--- a/libunwind/src/config.h
+++ b/libunwind/src/config.h
@@ -66,13 +66,14 @@
   #define _LIBUNWIND_EXPORT
   #define _LIBUNWIND_HIDDEN
 #else
-  #if !defined(__ELF__) && !defined(__MACH__) && !defined(_AIX)
-    #define _LIBUNWIND_EXPORT __declspec(dllexport)
-    #define _LIBUNWIND_HIDDEN
-  #else
-    #define _LIBUNWIND_EXPORT __attribute__((visibility("default")))
-    #define _LIBUNWIND_HIDDEN __attribute__((visibility("hidden")))
-  #endif
+#if !defined(__ELF__) && !defined(__MACH__) && !defined(_AIX) &&               \
+    !defined(__wasm__)
+#define _LIBUNWIND_EXPORT __declspec(dllexport)
+#define _LIBUNWIND_HIDDEN
+#else
+#define _LIBUNWIND_EXPORT __attribute__((visibility("default")))
+#define _LIBUNWIND_HIDDEN __attribute__((visibility("hidden")))
+#endif
 #endif
 
 #define STR(a) #a
diff --git a/libunwind/src/libunwind.cpp b/libunwind/src/libunwind.cpp
index 217dde909863793..cd12570da1141a6 100644
--- a/libunwind/src/libunwind.cpp
+++ b/libunwind/src/libunwind.cpp
@@ -12,6 +12,7 @@
 #include <libunwind.h>
 
 #include "config.h"
+#ifndef __wasm__
 #include "libunwind_ext.h"
 
 #include <stdlib.h>
@@ -431,6 +432,7 @@ int __unw_remove_find_dynamic_unwind_sections(
 }
 
 #endif // __APPLE__
+#endif
 
 // Add logging hooks in Debug builds only
 #ifndef NDEBUG

>From 1318a5ecd3499f4b32b280d3a2d1a14db5fa9471 Mon Sep 17 00:00:00 2001
From: trcrsired <uwgghhbcad at gmail.com>
Date: Sun, 28 Jan 2024 00:03:47 -0500
Subject: [PATCH 2/2] [libcxxabi] Fix function signatures for wasm

wasm does not define the function signatures correctly for cxxabi
Fix them
---
 libcxx/include/__exception/exception_ptr.h | 7 ++++++-
 libcxxabi/include/cxxabi.h                 | 7 ++++++-
 libcxxabi/src/cxa_exception.cpp            | 8 +++++++-
 3 files changed, 19 insertions(+), 3 deletions(-)

diff --git a/libcxx/include/__exception/exception_ptr.h b/libcxx/include/__exception/exception_ptr.h
index 53e2f718bc1b358..ce3f77f599f9c9f 100644
--- a/libcxx/include/__exception/exception_ptr.h
+++ b/libcxx/include/__exception/exception_ptr.h
@@ -36,7 +36,12 @@ struct __cxa_exception;
 _LIBCPP_OVERRIDABLE_FUNC_VIS __cxa_exception* __cxa_init_primary_exception(
     void*,
     std::type_info*,
-    void(
+#  if defined(__USING_WASM_EXCEPTIONS__)
+    void*
+#  else
+    void
+#  endif
+    (
 #  if defined(_WIN32)
         __thiscall
 #  endif
diff --git a/libcxxabi/include/cxxabi.h b/libcxxabi/include/cxxabi.h
index d0701181751c501..9b94a8b25ae8aca 100644
--- a/libcxxabi/include/cxxabi.h
+++ b/libcxxabi/include/cxxabi.h
@@ -48,7 +48,12 @@ extern _LIBCXXABI_FUNC_VIS void
 __cxa_free_exception(void *thrown_exception) throw();
 // This function is an LLVM extension, which mirrors the same extension in libsupc++ and libcxxrt
 extern _LIBCXXABI_FUNC_VIS __cxa_exception*
-__cxa_init_primary_exception(void* object, std::type_info* tinfo, void(_LIBCXXABI_DTOR_FUNC* dest)(void*)) throw();
+__cxa_init_primary_exception(void* object, std::type_info* tinfo,
+            void
+#ifdef __USING_WASM_EXCEPTIONS__
+	    *
+#endif
+	    (_LIBCXXABI_DTOR_FUNC* dest)(void*)) throw();
 
 // 2.4.3 Throwing the Exception Object
 extern _LIBCXXABI_FUNC_VIS _LIBCXXABI_NORETURN void
diff --git a/libcxxabi/src/cxa_exception.cpp b/libcxxabi/src/cxa_exception.cpp
index 65e9f4504ddade0..1dfb3f6741bebe3 100644
--- a/libcxxabi/src/cxa_exception.cpp
+++ b/libcxxabi/src/cxa_exception.cpp
@@ -206,8 +206,14 @@ void __cxa_free_exception(void *thrown_object) throw() {
     __aligned_free_with_fallback((void *)raw_buffer);
 }
 
+#ifdef __USING_WASM_EXCEPTIONS__
+__cxa_exception* __cxa_init_primary_exception(void* object, std::type_info* tinfo,
+                                              void* (_LIBCXXABI_DTOR_FUNC* dest)(void*)) throw()
+#else
 __cxa_exception* __cxa_init_primary_exception(void* object, std::type_info* tinfo,
-                                              void(_LIBCXXABI_DTOR_FUNC* dest)(void*)) throw() {
+                                              void (_LIBCXXABI_DTOR_FUNC* dest)(void*)) throw()
+#endif
+{
   __cxa_exception* exception_header = cxa_exception_from_thrown_object(object);
   exception_header->referenceCount = 0;
   exception_header->unexpectedHandler = std::get_unexpected();



More information about the cfe-commits mailing list