[libc-commits] [libc] [libc] Add wasm support for some submodules in llvm-libc. (PR #154383)

via libc-commits libc-commits at lists.llvm.org
Tue Aug 19 11:59:32 PDT 2025


https://github.com/google-yfyang updated https://github.com/llvm/llvm-project/pull/154383

>From 50c3b0280355f300209929df06ec9879feef2ac2 Mon Sep 17 00:00:00 2001
From: google-yfyang <yfyang at google.com>
Date: Tue, 19 Aug 2025 12:39:39 -0400
Subject: [PATCH 1/2] [libc] Add wasm support for some submodules in llvm-libc

---
 libc/src/__support/StringUtil/platform_errors.h    |  2 +-
 .../src/__support/StringUtil/tables/posix_errors.h |  5 +++++
 libc/src/__support/StringUtil/tables/stdc_errors.h |  5 +++++
 .../__support/macros/properties/architectures.h    |  4 ++++
 libc/src/setjmp/wasm/sigsetjmp.cpp                 | 14 ++++++++++++++
 5 files changed, 29 insertions(+), 1 deletion(-)
 create mode 100644 libc/src/setjmp/wasm/sigsetjmp.cpp

diff --git a/libc/src/__support/StringUtil/platform_errors.h b/libc/src/__support/StringUtil/platform_errors.h
index 32e8414b3e3de..5f83865482e72 100644
--- a/libc/src/__support/StringUtil/platform_errors.h
+++ b/libc/src/__support/StringUtil/platform_errors.h
@@ -9,7 +9,7 @@
 #ifndef LLVM_LIBC_SRC___SUPPORT_STRINGUTIL_PLATFORM_ERRORS_H
 #define LLVM_LIBC_SRC___SUPPORT_STRINGUTIL_PLATFORM_ERRORS_H
 
-#if defined(__linux__) || defined(__Fuchsia__)
+#if defined(__linux__) || defined(__Fuchsia__) || defined(__EMSCRIPTEN__)
 #include "tables/linux_platform_errors.h"
 #else
 #include "tables/minimal_platform_errors.h"
diff --git a/libc/src/__support/StringUtil/tables/posix_errors.h b/libc/src/__support/StringUtil/tables/posix_errors.h
index b21f28f0b1321..d386a48c21d44 100644
--- a/libc/src/__support/StringUtil/tables/posix_errors.h
+++ b/libc/src/__support/StringUtil/tables/posix_errors.h
@@ -63,7 +63,12 @@ LIBC_INLINE_VAR constexpr MsgTable<76> POSIX_ERRORS = {
     MsgMapping(EPROTO, "Protocol error"),
     MsgMapping(EMULTIHOP, "Multihop attempted"),
     MsgMapping(EBADMSG, "Bad message"),
+#ifdef __EMSCRIPTEN__
+    // For now, match the musl string
+    MsgMapping(EOVERFLOW, "Value too large for data type"),
+#else
     MsgMapping(EOVERFLOW, "Value too large for defined data type"),
+#endif
     MsgMapping(ENOTSOCK, "Socket operation on non-socket"),
     MsgMapping(EDESTADDRREQ, "Destination address required"),
     MsgMapping(EMSGSIZE, "Message too long"),
diff --git a/libc/src/__support/StringUtil/tables/stdc_errors.h b/libc/src/__support/StringUtil/tables/stdc_errors.h
index a326616f20ef5..9a23d0718ea13 100644
--- a/libc/src/__support/StringUtil/tables/stdc_errors.h
+++ b/libc/src/__support/StringUtil/tables/stdc_errors.h
@@ -16,7 +16,12 @@
 namespace LIBC_NAMESPACE_DECL {
 
 LIBC_INLINE_VAR constexpr const MsgTable<4> STDC_ERRORS = {
+#ifdef __EMSCRIPTEN__
+    // For now, match the musl name for errno 0.
+    MsgMapping(0, "No error information"),
+#else
     MsgMapping(0, "Success"),
+#endif
     MsgMapping(EDOM, "Numerical argument out of domain"),
     MsgMapping(ERANGE, "Numerical result out of range"),
     MsgMapping(EILSEQ, "Invalid or incomplete multibyte or wide character"),
diff --git a/libc/src/__support/macros/properties/architectures.h b/libc/src/__support/macros/properties/architectures.h
index ecc93196be286..21e9bc4288cd7 100644
--- a/libc/src/__support/macros/properties/architectures.h
+++ b/libc/src/__support/macros/properties/architectures.h
@@ -41,6 +41,10 @@
 #define LIBC_TARGET_ARCH_IS_ARM
 #endif
 
+#if defined(__wasm__)
+#define LIBC_TARGET_ARCH_IS_WASM
+#endif
+
 #if defined(__aarch64__) || defined(__arm64__) || defined(_M_ARM64)
 #define LIBC_TARGET_ARCH_IS_AARCH64
 #endif
diff --git a/libc/src/setjmp/wasm/sigsetjmp.cpp b/libc/src/setjmp/wasm/sigsetjmp.cpp
new file mode 100644
index 0000000000000..809356d4577eb
--- /dev/null
+++ b/libc/src/setjmp/wasm/sigsetjmp.cpp
@@ -0,0 +1,14 @@
+#include "src/setjmp/sigsetjmp.h"
+#include "hdr/offsetof_macros.h"
+#include "src/__support/common.h"
+#include "src/__support/macros/config.h"
+
+#if !defined(LIBC_TARGET_ARCH_IS_WASM)
+#error "Invalid file include"
+#endif
+
+namespace LIBC_NAMESPACE_DECL {
+[[gnu::returns_twice]] int sigsetjmp(jmp_buf sigjmp_buf, int savesigs) {
+    return setjmp(sigjmp_buf);
+}
+}

>From 062e630fbe9f4c584c6e74dd3cc4d2095aa50453 Mon Sep 17 00:00:00 2001
From: google-yfyang <yfyang at google.com>
Date: Tue, 19 Aug 2025 14:58:47 -0400
Subject: [PATCH 2/2] Create an emscripten-specific platform errors.h

---
 .../__support/StringUtil/platform_errors.h    |  4 ++-
 .../tables/emscripten_platform_errors.h       | 26 +++++++++++++++++++
 libc/src/setjmp/wasm/sigsetjmp.cpp            |  8 ++++++
 3 files changed, 37 insertions(+), 1 deletion(-)
 create mode 100644 libc/src/__support/StringUtil/tables/emscripten_platform_errors.h

diff --git a/libc/src/__support/StringUtil/platform_errors.h b/libc/src/__support/StringUtil/platform_errors.h
index 5f83865482e72..455e55e1fdadf 100644
--- a/libc/src/__support/StringUtil/platform_errors.h
+++ b/libc/src/__support/StringUtil/platform_errors.h
@@ -9,8 +9,10 @@
 #ifndef LLVM_LIBC_SRC___SUPPORT_STRINGUTIL_PLATFORM_ERRORS_H
 #define LLVM_LIBC_SRC___SUPPORT_STRINGUTIL_PLATFORM_ERRORS_H
 
-#if defined(__linux__) || defined(__Fuchsia__) || defined(__EMSCRIPTEN__)
+#if defined(__linux__) || defined(__Fuchsia__)
 #include "tables/linux_platform_errors.h"
+#elif defined(__EMSCRIPTEN__)
+#include "tables/emscripten_platform_errors.h"
 #else
 #include "tables/minimal_platform_errors.h"
 #endif
diff --git a/libc/src/__support/StringUtil/tables/emscripten_platform_errors.h b/libc/src/__support/StringUtil/tables/emscripten_platform_errors.h
new file mode 100644
index 0000000000000..c9316aa7e18a8
--- /dev/null
+++ b/libc/src/__support/StringUtil/tables/emscripten_platform_errors.h
@@ -0,0 +1,26 @@
+//===-- Map of error numbers to strings for the Emscripten platform --*- C++ -*-===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_LIBC_SRC___SUPPORT_STRINGUTIL_TABLES_EMSCRPITEN_PLATFORM_ERRORS_H
+#define LLVM_LIBC_SRC___SUPPORT_STRINGUTIL_TABLES_EMSCRPITEN_PLATFORM_ERRORS_H
+
+#include "posix_errors.h"
+#include "src/__support/macros/config.h"
+#include "stdc_errors.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+LIBC_INLINE_VAR constexpr auto PLATFORM_ERRORS =
+    STDC_ERRORS + POSIX_ERRORS;
+
+LIBC_INLINE_VAR constexpr auto PLATFORM_ERRNO_NAMES =
+    STDC_ERRNO_NAMES + POSIX_ERRNO_NAMES;
+
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SRC___SUPPORT_STRINGUTIL_TABLES_EMSCRPITEN_PLATFORM_ERRORS_H
diff --git a/libc/src/setjmp/wasm/sigsetjmp.cpp b/libc/src/setjmp/wasm/sigsetjmp.cpp
index 809356d4577eb..a921108b927f9 100644
--- a/libc/src/setjmp/wasm/sigsetjmp.cpp
+++ b/libc/src/setjmp/wasm/sigsetjmp.cpp
@@ -1,3 +1,11 @@
+//===-- Implementation of sigsetjmp ---------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
 #include "src/setjmp/sigsetjmp.h"
 #include "hdr/offsetof_macros.h"
 #include "src/__support/common.h"



More information about the libc-commits mailing list