Skip to content
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

[libunwind][WebAssembly] Make libunwind compilable #92192

Merged
merged 5 commits into from
May 21, 2024

Conversation

aheejin
Copy link
Member

@aheejin aheejin commented May 14, 2024

This tries to make Wasm compilable in LLVM tree with CMake for non-Emscripten platform.

This

  • Adds -D__USING_WASM_EXCEPTIONS__ when you compile with -fwasm-exceptions (like other EH options) in Clang
  • Exclude UnwindLevel1.c, UnwindRegistersSave.S, and UnwindRegistersRestore.S when compiling with Wasm
  • Changed some __USING_WASM_EXCEPTIONS__ to __wasm__; they should be applied when compiling with Wasm w/o exceptions.
  • Define some unused macros to make it compile

Fixes #72771.

This tries to make Wasm compilable in LLVM tree with CMake for
non-Emscripten platform.

This
- Adds `-D__USING_WASM_EXCEPTIONS__` when you compile with
  `-fwasm-exceptions` (like other EH options) in Clang
- Exclude `UnwindLevel1.c` when compiling with Wasm
- Changed some `__USING_WASM_EXCEPTIONS__` to `__wasm__`; they should be
  applied when compiling with Wasm w/o exceptions.
- Define some unused macros to make it compile
Copy link

github-actions bot commented May 14, 2024

✅ With the latest revision this PR passed the C/C++ code formatter.

@aheejin aheejin marked this pull request as ready for review May 15, 2024 22:29
@aheejin aheejin requested a review from a team as a code owner May 15, 2024 22:29
@llvmbot llvmbot added clang Clang issues not falling into any other category libunwind labels May 15, 2024
@llvmbot
Copy link
Collaborator

llvmbot commented May 15, 2024

@llvm/pr-subscribers-libunwind

Author: Heejin Ahn (aheejin)

Changes

This tries to make Wasm compilable in LLVM tree with CMake for non-Emscripten platform.

This

  • Adds -D__USING_WASM_EXCEPTIONS__ when you compile with -fwasm-exceptions (like other EH options) in Clang
  • Exclude UnwindLevel1.c, UnwindRegistersSave.S, and UnwindRegistersRestore.S when compiling with Wasm
  • Changed some __USING_WASM_EXCEPTIONS__ to __wasm__; they should be applied when compiling with Wasm w/o exceptions.
  • Define some unused macros to make it compile

Fixes #72771.


Full diff: https://github.com/llvm/llvm-project/pull/92192.diff

7 Files Affected:

  • (modified) clang/lib/Frontend/InitPreprocessor.cpp (+2)
  • (modified) libunwind/cmake/config-ix.cmake (+1)
  • (modified) libunwind/include/__libunwind_config.h (+4)
  • (modified) libunwind/src/UnwindLevel1.c (+2-1)
  • (modified) libunwind/src/UnwindRegistersRestore.S (+2-2)
  • (modified) libunwind/src/UnwindRegistersSave.S (+2-2)
  • (modified) libunwind/src/libunwind.cpp (+2-2)
diff --git a/clang/lib/Frontend/InitPreprocessor.cpp b/clang/lib/Frontend/InitPreprocessor.cpp
index c1d209466ffe5..3cc85ff502776 100644
--- a/clang/lib/Frontend/InitPreprocessor.cpp
+++ b/clang/lib/Frontend/InitPreprocessor.cpp
@@ -1006,6 +1006,8 @@ static void InitializePredefinedMacros(const TargetInfo &TI,
   else if (LangOpts.hasDWARFExceptions() &&
            (TI.getTriple().isThumb() || TI.getTriple().isARM()))
     Builder.defineMacro("__ARM_DWARF_EH__");
+  else if (LangOpts.hasWasmExceptions())
+    Builder.defineMacro("__USING_WASM_EXCEPTIONS__");
 
   if (LangOpts.Deprecated)
     Builder.defineMacro("__DEPRECATED");
diff --git a/libunwind/cmake/config-ix.cmake b/libunwind/cmake/config-ix.cmake
index 126c872f0d489..d505ee1d346fa 100644
--- a/libunwind/cmake/config-ix.cmake
+++ b/libunwind/cmake/config-ix.cmake
@@ -109,6 +109,7 @@ check_cxx_compiler_flag(-nostdinc++ CXX_SUPPORTS_NOSTDINCXX_FLAG)
 check_symbol_exists(__arm__ "" LIBUNWIND_TARGET_ARM)
 check_symbol_exists(__USING_SJLJ_EXCEPTIONS__ "" LIBUNWIND_USES_SJLJ_EXCEPTIONS)
 check_symbol_exists(__ARM_DWARF_EH__ "" LIBUNWIND_USES_DWARF_EH)
+check_symbol_exists(__USING_WASM_EXCEPTIONS__ "" LIBUNWIND_USES_WASM_EXCEPTIONS)
 
 if(LIBUNWIND_TARGET_ARM AND NOT LIBUNWIND_USES_SJLJ_EXCEPTIONS AND NOT LIBUNWIND_USES_DWARF_EH)
   # This condition is copied from __libunwind_config.h
diff --git a/libunwind/include/__libunwind_config.h b/libunwind/include/__libunwind_config.h
index 8db336b2d727c..028b9e3baa806 100644
--- a/libunwind/include/__libunwind_config.h
+++ b/libunwind/include/__libunwind_config.h
@@ -180,6 +180,10 @@
 #endif
 #define _LIBUNWIND_HIGHEST_DWARF_REGISTER                                      \
   _LIBUNWIND_HIGHEST_DWARF_REGISTER_LOONGARCH
+#elif defined(__wasm__)
+// Unused
+#define _LIBUNWIND_CONTEXT_SIZE 0
+#define _LIBUNWIND_CURSOR_SIZE 0
 # else
 #  error "Unsupported architecture."
 # endif
diff --git a/libunwind/src/UnwindLevel1.c b/libunwind/src/UnwindLevel1.c
index 05d0f2cb0a0a7..48e7bc3b9e00e 100644
--- a/libunwind/src/UnwindLevel1.c
+++ b/libunwind/src/UnwindLevel1.c
@@ -31,7 +31,8 @@
 #include "libunwind_ext.h"
 #include "unwind.h"
 
-#if !defined(_LIBUNWIND_ARM_EHABI) && !defined(__USING_SJLJ_EXCEPTIONS__)
+#if !defined(_LIBUNWIND_ARM_EHABI) && !defined(__USING_SJLJ_EXCEPTIONS__) &&   \
+    !defined(__wasm__)
 
 #ifndef _LIBUNWIND_SUPPORT_SEH_UNWIND
 
diff --git a/libunwind/src/UnwindRegistersRestore.S b/libunwind/src/UnwindRegistersRestore.S
index 42c2488fc7cf7..d8552babfe081 100644
--- a/libunwind/src/UnwindRegistersRestore.S
+++ b/libunwind/src/UnwindRegistersRestore.S
@@ -20,7 +20,7 @@
   .text
 #endif
 
-#if !defined(__USING_SJLJ_EXCEPTIONS__)
+#if !defined(__USING_SJLJ_EXCEPTIONS__) && !defined(__wasm__)
 
 #if defined(__i386__)
 DEFINE_LIBUNWIND_FUNCTION(__libunwind_Registers_x86_jumpto)
@@ -1232,7 +1232,7 @@ DEFINE_LIBUNWIND_FUNCTION(_ZN9libunwind19Registers_loongarch6jumptoEv)
 
 #endif
 
-#endif /* !defined(__USING_SJLJ_EXCEPTIONS__) */
+#endif /* !defined(__USING_SJLJ_EXCEPTIONS__) && !defined(__wasm) */
 
 NO_EXEC_STACK_DIRECTIVE
 
diff --git a/libunwind/src/UnwindRegistersSave.S b/libunwind/src/UnwindRegistersSave.S
index 19a0e87d683ce..5bf6055fe4147 100644
--- a/libunwind/src/UnwindRegistersSave.S
+++ b/libunwind/src/UnwindRegistersSave.S
@@ -20,7 +20,7 @@
     .text
 #endif
 
-#if !defined(__USING_SJLJ_EXCEPTIONS__)
+#if !defined(__USING_SJLJ_EXCEPTIONS__) && !defined(__wasm__)
 
 #if defined(__i386__)
 
@@ -1177,6 +1177,6 @@ DEFINE_LIBUNWIND_FUNCTION(__unw_getcontext)
 
   WEAK_ALIAS(__unw_getcontext, unw_getcontext)
 
-#endif /* !defined(__USING_SJLJ_EXCEPTIONS__) */
+#endif /* !defined(__USING_SJLJ_EXCEPTIONS__) && !defined(__wasm__) */
 
 NO_EXEC_STACK_DIRECTIVE
diff --git a/libunwind/src/libunwind.cpp b/libunwind/src/libunwind.cpp
index 217dde9098637..7e5c6bd263e14 100644
--- a/libunwind/src/libunwind.cpp
+++ b/libunwind/src/libunwind.cpp
@@ -26,7 +26,7 @@
 #include <sanitizer/asan_interface.h>
 #endif
 
-#if !defined(__USING_SJLJ_EXCEPTIONS__) && !defined(__USING_WASM_EXCEPTIONS__)
+#if !defined(__USING_SJLJ_EXCEPTIONS__) && !defined(__wasm__)
 #include "AddressSpace.hpp"
 #include "UnwindCursor.hpp"
 
@@ -348,7 +348,7 @@ void __unw_remove_dynamic_eh_frame_section(unw_word_t eh_frame_start) {
 
 #endif // defined(_LIBUNWIND_SUPPORT_DWARF_UNWIND)
 #endif // !defined(__USING_SJLJ_EXCEPTIONS__) &&
-       // !defined(__USING_WASM_EXCEPTIONS__)
+       // !defined(__wasm__)
 
 #ifdef __APPLE__
 

@llvmbot
Copy link
Collaborator

llvmbot commented May 15, 2024

@llvm/pr-subscribers-clang

Author: Heejin Ahn (aheejin)

Changes

This tries to make Wasm compilable in LLVM tree with CMake for non-Emscripten platform.

This

  • Adds -D__USING_WASM_EXCEPTIONS__ when you compile with -fwasm-exceptions (like other EH options) in Clang
  • Exclude UnwindLevel1.c, UnwindRegistersSave.S, and UnwindRegistersRestore.S when compiling with Wasm
  • Changed some __USING_WASM_EXCEPTIONS__ to __wasm__; they should be applied when compiling with Wasm w/o exceptions.
  • Define some unused macros to make it compile

Fixes #72771.


Full diff: https://github.com/llvm/llvm-project/pull/92192.diff

7 Files Affected:

  • (modified) clang/lib/Frontend/InitPreprocessor.cpp (+2)
  • (modified) libunwind/cmake/config-ix.cmake (+1)
  • (modified) libunwind/include/__libunwind_config.h (+4)
  • (modified) libunwind/src/UnwindLevel1.c (+2-1)
  • (modified) libunwind/src/UnwindRegistersRestore.S (+2-2)
  • (modified) libunwind/src/UnwindRegistersSave.S (+2-2)
  • (modified) libunwind/src/libunwind.cpp (+2-2)
diff --git a/clang/lib/Frontend/InitPreprocessor.cpp b/clang/lib/Frontend/InitPreprocessor.cpp
index c1d209466ffe5..3cc85ff502776 100644
--- a/clang/lib/Frontend/InitPreprocessor.cpp
+++ b/clang/lib/Frontend/InitPreprocessor.cpp
@@ -1006,6 +1006,8 @@ static void InitializePredefinedMacros(const TargetInfo &TI,
   else if (LangOpts.hasDWARFExceptions() &&
            (TI.getTriple().isThumb() || TI.getTriple().isARM()))
     Builder.defineMacro("__ARM_DWARF_EH__");
+  else if (LangOpts.hasWasmExceptions())
+    Builder.defineMacro("__USING_WASM_EXCEPTIONS__");
 
   if (LangOpts.Deprecated)
     Builder.defineMacro("__DEPRECATED");
diff --git a/libunwind/cmake/config-ix.cmake b/libunwind/cmake/config-ix.cmake
index 126c872f0d489..d505ee1d346fa 100644
--- a/libunwind/cmake/config-ix.cmake
+++ b/libunwind/cmake/config-ix.cmake
@@ -109,6 +109,7 @@ check_cxx_compiler_flag(-nostdinc++ CXX_SUPPORTS_NOSTDINCXX_FLAG)
 check_symbol_exists(__arm__ "" LIBUNWIND_TARGET_ARM)
 check_symbol_exists(__USING_SJLJ_EXCEPTIONS__ "" LIBUNWIND_USES_SJLJ_EXCEPTIONS)
 check_symbol_exists(__ARM_DWARF_EH__ "" LIBUNWIND_USES_DWARF_EH)
+check_symbol_exists(__USING_WASM_EXCEPTIONS__ "" LIBUNWIND_USES_WASM_EXCEPTIONS)
 
 if(LIBUNWIND_TARGET_ARM AND NOT LIBUNWIND_USES_SJLJ_EXCEPTIONS AND NOT LIBUNWIND_USES_DWARF_EH)
   # This condition is copied from __libunwind_config.h
diff --git a/libunwind/include/__libunwind_config.h b/libunwind/include/__libunwind_config.h
index 8db336b2d727c..028b9e3baa806 100644
--- a/libunwind/include/__libunwind_config.h
+++ b/libunwind/include/__libunwind_config.h
@@ -180,6 +180,10 @@
 #endif
 #define _LIBUNWIND_HIGHEST_DWARF_REGISTER                                      \
   _LIBUNWIND_HIGHEST_DWARF_REGISTER_LOONGARCH
+#elif defined(__wasm__)
+// Unused
+#define _LIBUNWIND_CONTEXT_SIZE 0
+#define _LIBUNWIND_CURSOR_SIZE 0
 # else
 #  error "Unsupported architecture."
 # endif
diff --git a/libunwind/src/UnwindLevel1.c b/libunwind/src/UnwindLevel1.c
index 05d0f2cb0a0a7..48e7bc3b9e00e 100644
--- a/libunwind/src/UnwindLevel1.c
+++ b/libunwind/src/UnwindLevel1.c
@@ -31,7 +31,8 @@
 #include "libunwind_ext.h"
 #include "unwind.h"
 
-#if !defined(_LIBUNWIND_ARM_EHABI) && !defined(__USING_SJLJ_EXCEPTIONS__)
+#if !defined(_LIBUNWIND_ARM_EHABI) && !defined(__USING_SJLJ_EXCEPTIONS__) &&   \
+    !defined(__wasm__)
 
 #ifndef _LIBUNWIND_SUPPORT_SEH_UNWIND
 
diff --git a/libunwind/src/UnwindRegistersRestore.S b/libunwind/src/UnwindRegistersRestore.S
index 42c2488fc7cf7..d8552babfe081 100644
--- a/libunwind/src/UnwindRegistersRestore.S
+++ b/libunwind/src/UnwindRegistersRestore.S
@@ -20,7 +20,7 @@
   .text
 #endif
 
-#if !defined(__USING_SJLJ_EXCEPTIONS__)
+#if !defined(__USING_SJLJ_EXCEPTIONS__) && !defined(__wasm__)
 
 #if defined(__i386__)
 DEFINE_LIBUNWIND_FUNCTION(__libunwind_Registers_x86_jumpto)
@@ -1232,7 +1232,7 @@ DEFINE_LIBUNWIND_FUNCTION(_ZN9libunwind19Registers_loongarch6jumptoEv)
 
 #endif
 
-#endif /* !defined(__USING_SJLJ_EXCEPTIONS__) */
+#endif /* !defined(__USING_SJLJ_EXCEPTIONS__) && !defined(__wasm) */
 
 NO_EXEC_STACK_DIRECTIVE
 
diff --git a/libunwind/src/UnwindRegistersSave.S b/libunwind/src/UnwindRegistersSave.S
index 19a0e87d683ce..5bf6055fe4147 100644
--- a/libunwind/src/UnwindRegistersSave.S
+++ b/libunwind/src/UnwindRegistersSave.S
@@ -20,7 +20,7 @@
     .text
 #endif
 
-#if !defined(__USING_SJLJ_EXCEPTIONS__)
+#if !defined(__USING_SJLJ_EXCEPTIONS__) && !defined(__wasm__)
 
 #if defined(__i386__)
 
@@ -1177,6 +1177,6 @@ DEFINE_LIBUNWIND_FUNCTION(__unw_getcontext)
 
   WEAK_ALIAS(__unw_getcontext, unw_getcontext)
 
-#endif /* !defined(__USING_SJLJ_EXCEPTIONS__) */
+#endif /* !defined(__USING_SJLJ_EXCEPTIONS__) && !defined(__wasm__) */
 
 NO_EXEC_STACK_DIRECTIVE
diff --git a/libunwind/src/libunwind.cpp b/libunwind/src/libunwind.cpp
index 217dde9098637..7e5c6bd263e14 100644
--- a/libunwind/src/libunwind.cpp
+++ b/libunwind/src/libunwind.cpp
@@ -26,7 +26,7 @@
 #include <sanitizer/asan_interface.h>
 #endif
 
-#if !defined(__USING_SJLJ_EXCEPTIONS__) && !defined(__USING_WASM_EXCEPTIONS__)
+#if !defined(__USING_SJLJ_EXCEPTIONS__) && !defined(__wasm__)
 #include "AddressSpace.hpp"
 #include "UnwindCursor.hpp"
 
@@ -348,7 +348,7 @@ void __unw_remove_dynamic_eh_frame_section(unw_word_t eh_frame_start) {
 
 #endif // defined(_LIBUNWIND_SUPPORT_DWARF_UNWIND)
 #endif // !defined(__USING_SJLJ_EXCEPTIONS__) &&
-       // !defined(__USING_WASM_EXCEPTIONS__)
+       // !defined(__wasm__)
 
 #ifdef __APPLE__
 

@aheejin
Copy link
Member Author

aheejin commented May 17, 2024

Can someone review this?

@@ -20,7 +20,7 @@
.text
#endif

#if !defined(__USING_SJLJ_EXCEPTIONS__)
#if !defined(__USING_SJLJ_EXCEPTIONS__) && !defined(__wasm__)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You are defining __USING_WASM_EXCEPTIONS__. Why you are not using it here and everywhere else?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

__wasm__ is defined whenever we compile the code to wasm, and __USING_WASM_EXCEPTIONS__ is defined when we compile the code to wasm and use Wasm exceptions (-fwasm-exeptions). And this code should be excluded whenever it is compiled to wasm, even if -fwasm-exceptions is not used.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok. Then does the clang change really belong here? LIBUNWIND_USES_WASM_EXCEPTIONS also seems to be unused.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removed the clang change and LIBUNWIND_USES_WASM_EXCEPTIONS.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can I land this? Thank you for your review!

@aheejin
Copy link
Member Author

aheejin commented May 21, 2024

I'd appreciate if someone LGTMs this. Thanks!

@aheejin aheejin requested a review from dschuff May 21, 2024 22:13
Copy link
Member

@dschuff dschuff left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These changes make sense from the wasm point of view, and it looks like you've addressed the comment, so LGTM

Copy link
Collaborator

@asl asl left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok from the generic point of view. I cannot judge if wasm-related part is ok though :)

@aheejin aheejin merged commit f6ff87d into llvm:main May 21, 2024
53 checks passed
@aheejin aheejin deleted the wasm_libunwind_compile branch May 21, 2024 22:43
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
clang Clang issues not falling into any other category libunwind
Projects
None yet
Development

Successfully merging this pull request may close these issues.

How to build libunwind for wasm?
4 participants