[clang] [libunwind] [llvm] [wasm] Toolchain support for `wasm32-wali-linux-musl` target (PR #156087)
Arjun Ramesh via llvm-commits
llvm-commits at lists.llvm.org
Fri Sep 5 12:22:20 PDT 2025
https://github.com/arjunr2 updated https://github.com/llvm/llvm-project/pull/156087
>From cb996a12aa20c65980f620381731fad1622e0417 Mon Sep 17 00:00:00 2001
From: Arjun Ramesh <arjunr2 at andrew.cmu.edu>
Date: Sat, 30 Aug 2025 01:01:02 -0400
Subject: [PATCH 1/4] Support for `wasm32-wali-linux-musl target` in `clang`,
`lld`, and `libcxx`
---
clang/lib/Basic/Targets.cpp | 7 ++++++-
clang/lib/Basic/Targets/OSTargets.h | 17 +++++++++++++++++
clang/lib/Basic/Targets/WebAssembly.h | 18 +++++++++++++-----
clang/lib/Driver/Driver.cpp | 2 ++
libunwind/src/UnwindRegistersRestore.S | 7 +++----
libunwind/src/UnwindRegistersSave.S | 8 ++++----
llvm/include/llvm/TargetParser/Triple.h | 7 +++++++
llvm/lib/TargetParser/Triple.cpp | 3 +++
8 files changed, 55 insertions(+), 14 deletions(-)
diff --git a/clang/lib/Basic/Targets.cpp b/clang/lib/Basic/Targets.cpp
index e3f9760ac7ce3..11222bc836775 100644
--- a/clang/lib/Basic/Targets.cpp
+++ b/clang/lib/Basic/Targets.cpp
@@ -687,7 +687,8 @@ std::unique_ptr<TargetInfo> AllocateTarget(const llvm::Triple &Triple,
}
case llvm::Triple::wasm32:
if (Triple.getSubArch() != llvm::Triple::NoSubArch ||
- Triple.getVendor() != llvm::Triple::UnknownVendor ||
+ (!Triple.isWALI() &&
+ Triple.getVendor() != llvm::Triple::UnknownVendor) ||
!Triple.isOSBinFormatWasm())
return nullptr;
switch (os) {
@@ -697,6 +698,10 @@ std::unique_ptr<TargetInfo> AllocateTarget(const llvm::Triple &Triple,
case llvm::Triple::Emscripten:
return std::make_unique<EmscriptenTargetInfo<WebAssembly32TargetInfo>>(
Triple, Opts);
+ // WALI OS target
+ case llvm::Triple::Linux:
+ return std::make_unique<WALITargetInfo<WebAssembly32TargetInfo>>(Triple,
+ Opts);
case llvm::Triple::UnknownOS:
return std::make_unique<WebAssemblyOSTargetInfo<WebAssembly32TargetInfo>>(
Triple, Opts);
diff --git a/clang/lib/Basic/Targets/OSTargets.h b/clang/lib/Basic/Targets/OSTargets.h
index a733f6e97b3a4..2199bfcfbd7ab 100644
--- a/clang/lib/Basic/Targets/OSTargets.h
+++ b/clang/lib/Basic/Targets/OSTargets.h
@@ -936,6 +936,23 @@ class LLVM_LIBRARY_VISIBILITY WASITargetInfo
using WebAssemblyOSTargetInfo<Target>::WebAssemblyOSTargetInfo;
};
+// WALI target
+template <typename Target>
+class LLVM_LIBRARY_VISIBILITY WALITargetInfo
+ : public WebAssemblyOSTargetInfo<Target> {
+ void getOSDefines(const LangOptions &Opts, const llvm::Triple &Triple,
+ MacroBuilder &Builder) const final {
+ WebAssemblyOSTargetInfo<Target>::getOSDefines(Opts, Triple, Builder);
+ // Linux defines; list based off of gcc output
+ DefineStd(Builder, "unix", Opts);
+ DefineStd(Builder, "linux", Opts);
+ Builder.defineMacro("__wali__");
+ }
+
+public:
+ using WebAssemblyOSTargetInfo<Target>::WebAssemblyOSTargetInfo;
+};
+
// Emscripten target
template <typename Target>
class LLVM_LIBRARY_VISIBILITY EmscriptenTargetInfo
diff --git a/clang/lib/Basic/Targets/WebAssembly.h b/clang/lib/Basic/Targets/WebAssembly.h
index eba74229dfc14..81fd40a62d3a3 100644
--- a/clang/lib/Basic/Targets/WebAssembly.h
+++ b/clang/lib/Basic/Targets/WebAssembly.h
@@ -88,12 +88,20 @@ class LLVM_LIBRARY_VISIBILITY WebAssemblyTargetInfo : public TargetInfo {
LongDoubleWidth = LongDoubleAlign = 128;
LongDoubleFormat = &llvm::APFloat::IEEEquad();
MaxAtomicPromoteWidth = MaxAtomicInlineWidth = 64;
- // size_t being unsigned long for both wasm32 and wasm64 makes mangled names
- // more consistent between the two.
- SizeType = UnsignedLong;
- PtrDiffType = SignedLong;
- IntPtrType = SignedLong;
HasUnalignedAccess = true;
+ if (T.isWALI()) {
+ // WALI ABI requires 64-bit longs on both wasm32 and wasm64
+ LongAlign = LongWidth = 64;
+ SizeType = UnsignedInt;
+ PtrDiffType = SignedInt;
+ IntPtrType = SignedInt;
+ } else {
+ // size_t being unsigned long for both wasm32 and wasm64 makes mangled
+ // names more consistent between the two.
+ SizeType = UnsignedLong;
+ PtrDiffType = SignedLong;
+ IntPtrType = SignedLong;
+ }
}
StringRef getABI() const override;
diff --git a/clang/lib/Driver/Driver.cpp b/clang/lib/Driver/Driver.cpp
index f110dbab3e5a5..e99b2263c81c6 100644
--- a/clang/lib/Driver/Driver.cpp
+++ b/clang/lib/Driver/Driver.cpp
@@ -6833,6 +6833,8 @@ const ToolChain &Driver::getToolChain(const ArgList &Args,
TC = std::make_unique<toolchains::VEToolChain>(*this, Target, Args);
else if (Target.isOHOSFamily())
TC = std::make_unique<toolchains::OHOS>(*this, Target, Args);
+ else if (Target.isWALI())
+ TC = std::make_unique<toolchains::WebAssembly>(*this, Target, Args);
else
TC = std::make_unique<toolchains::Linux>(*this, Target, Args);
break;
diff --git a/libunwind/src/UnwindRegistersRestore.S b/libunwind/src/UnwindRegistersRestore.S
index 5e199188945df..eda0b10205ac3 100644
--- a/libunwind/src/UnwindRegistersRestore.S
+++ b/libunwind/src/UnwindRegistersRestore.S
@@ -6,6 +6,8 @@
//
//===----------------------------------------------------------------------===//
+#if !defined(__USING_SJLJ_EXCEPTIONS__) && !defined(__wasm__)
+
#include "assembly.h"
#define FROM_0_TO_15 0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15
@@ -20,8 +22,6 @@
.text
#endif
-#if !defined(__USING_SJLJ_EXCEPTIONS__) && !defined(__wasm__)
-
#if defined(__i386__)
DEFINE_LIBUNWIND_FUNCTION(__libunwind_Registers_x86_jumpto)
#
@@ -1250,7 +1250,6 @@ DEFINE_LIBUNWIND_FUNCTION(_ZN9libunwind19Registers_loongarch6jumptoEv)
#endif
-#endif /* !defined(__USING_SJLJ_EXCEPTIONS__) && !defined(__wasm__) */
-
NO_EXEC_STACK_DIRECTIVE
+#endif /* !defined(__USING_SJLJ_EXCEPTIONS__) && !defined(__wasm__) */
diff --git a/libunwind/src/UnwindRegistersSave.S b/libunwind/src/UnwindRegistersSave.S
index 5139a551ad245..a2ff6578b1057 100644
--- a/libunwind/src/UnwindRegistersSave.S
+++ b/libunwind/src/UnwindRegistersSave.S
@@ -6,6 +6,8 @@
//
//===----------------------------------------------------------------------===//
+#if !defined(__USING_SJLJ_EXCEPTIONS__) && !defined(__wasm__)
+
#include "assembly.h"
#define FROM_0_TO_15 0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15
@@ -20,8 +22,6 @@
.text
#endif
-#if !defined(__USING_SJLJ_EXCEPTIONS__) && !defined(__wasm__)
-
#if defined(__i386__)
#
@@ -1232,6 +1232,6 @@ DEFINE_LIBUNWIND_FUNCTION(__unw_getcontext)
WEAK_ALIAS(__unw_getcontext, unw_getcontext)
#endif
-#endif /* !defined(__USING_SJLJ_EXCEPTIONS__) && !defined(__wasm__) */
-
NO_EXEC_STACK_DIRECTIVE
+
+#endif /* !defined(__USING_SJLJ_EXCEPTIONS__) && !defined(__wasm__) */
diff --git a/llvm/include/llvm/TargetParser/Triple.h b/llvm/include/llvm/TargetParser/Triple.h
index ede9797ac7488..447a94a48af67 100644
--- a/llvm/include/llvm/TargetParser/Triple.h
+++ b/llvm/include/llvm/TargetParser/Triple.h
@@ -199,6 +199,7 @@ class Triple {
SUSE,
OpenEmbedded,
Intel,
+ WALI,
Meta,
LastVendorType = Meta
};
@@ -795,6 +796,12 @@ class Triple {
return getObjectFormat() == Triple::DXContainer;
}
+ /// Tests whether the target uses WALI Wasm
+ bool isWALI() const {
+ return getArch() == Triple::wasm32 && getVendor() == Triple::WALI &&
+ getOS() == Triple::Linux;
+ }
+
/// Tests whether the target is the PS4 platform.
bool isPS4() const {
return getArch() == Triple::x86_64 &&
diff --git a/llvm/lib/TargetParser/Triple.cpp b/llvm/lib/TargetParser/Triple.cpp
index 6acb0bc49ecfe..5fc0086fc8d76 100644
--- a/llvm/lib/TargetParser/Triple.cpp
+++ b/llvm/lib/TargetParser/Triple.cpp
@@ -277,6 +277,8 @@ StringRef Triple::getVendorTypeName(VendorType Kind) {
case PC: return "pc";
case SCEI: return "scei";
case SUSE: return "suse";
+ case WALI:
+ return "wali";
case Meta:
return "meta";
}
@@ -681,6 +683,7 @@ static Triple::VendorType parseVendor(StringRef VendorName) {
.Case("suse", Triple::SUSE)
.Case("oe", Triple::OpenEmbedded)
.Case("intel", Triple::Intel)
+ .Case("wali", Triple::WALI)
.Case("meta", Triple::Meta)
.Default(Triple::UnknownVendor);
}
>From 2d4f58190d00385986c3fb3cabf0903718978e19 Mon Sep 17 00:00:00 2001
From: Arjun Ramesh <arjunr2 at andrew.cmu.edu>
Date: Mon, 1 Sep 2025 21:31:04 -0400
Subject: [PATCH 2/4] Added tests for wasm32 wali target compilation
---
clang/test/Driver/unsupported-target-arch.c | 10 +++++++++
clang/test/Driver/wasm-toolchain.c | 7 ++++++
clang/test/Driver/wasm-toolchain.cpp | 25 +++++++++++++++++++++
3 files changed, 42 insertions(+)
diff --git a/clang/test/Driver/unsupported-target-arch.c b/clang/test/Driver/unsupported-target-arch.c
index 426f646db50b0..b5874a41e7ef6 100644
--- a/clang/test/Driver/unsupported-target-arch.c
+++ b/clang/test/Driver/unsupported-target-arch.c
@@ -77,3 +77,13 @@
// RUN: not %clang_cl --target=x86-unknown-uefi -o %t.o %s 2> %t.err
// RUN: FileCheck --input-file=%t.err -check-prefixes=CHECK-x86 %s
// CHECK-x86: error: unknown target triple 'x86-unknown-uefi'{{$}}
+
+// Linux API for wasm must include `wali` vendor and `linux` OS
+//
+// RUN: not %clang -### --target=wasm32-linux-musl -o %t.o %s 2> %t.err \
+// RUN: FileCheck --input-file=%t.err -check-prefixes=CHECK-WASM-WALI-NO-VENDOR %s
+// CHECK-WASM-WALI-NO-VENDOR: error: unknown target triple 'wasm32-unknown-linux-musl'
+//
+// RUN: not %clang -### --target=wasm32-wali-wasi-musl -o %t.o %s 2> %t.err \
+// RUN: FileCheck --input-file=%t.err -check-prefixes=CHECK-WASM-WALI-NO-LINUX %s
+// CHECK-WASM-WALI-NO-LINUX: error: unknown target triple 'wasm32-wali-wasi-musl'
diff --git a/clang/test/Driver/wasm-toolchain.c b/clang/test/Driver/wasm-toolchain.c
index 91803fe6bc1f2..0abf75f2b998d 100644
--- a/clang/test/Driver/wasm-toolchain.c
+++ b/clang/test/Driver/wasm-toolchain.c
@@ -296,3 +296,10 @@
// RUN: | FileCheck -check-prefix=LINK_WASIP2_USE_WASMLD %s
// LINK_WASIP2_USE_WASMLD: "-cc1" {{.*}} "-o" "[[temp:[^"]*]]"
// LINK_WASIP2_USE_WASMLD: wasm-ld{{.*}}" "-m" "wasm32" {{.*}} "[[temp]]" {{.*}}
+
+// Basic `wasm32-wali-linux-musl` compile-link test.
+
+// RUN: %clang -### --target=wasm32-wali-linux-musl --sysroot=/foo %s 2>&1 \
+// RUN: | FileCheck -check-prefix=LINK_WALI_BASIC %s
+// LINK_WALI_BASIC: "-cc1" {{.*}} "-o" "[[temp:[^"]*]]"
+// LINK_WALI_BASIC: wasm-ld{{.*}}" "-L/foo/lib/wasm32-linux-musl" "crt1.o" "[[temp]]" "-lc" "{{.*[/\\]}}libclang_rt.builtins.a" "-o" "a.out"
diff --git a/clang/test/Driver/wasm-toolchain.cpp b/clang/test/Driver/wasm-toolchain.cpp
index ba1c55b33edca..3b1587033b547 100644
--- a/clang/test/Driver/wasm-toolchain.cpp
+++ b/clang/test/Driver/wasm-toolchain.cpp
@@ -86,3 +86,28 @@
// COMPILE_STDCXX: "-internal-isystem" "[[RESOURCE_DIR]]{{(/|\\\\)}}include"
// COMPILE_STDCXX: "-internal-isystem" "[[SYSROOT:[^"]+]]/include/wasm32-wasi"
// COMPILE_STDCXX: "-internal-isystem" "[[SYSROOT:[^"]+]]/include"
+
+// RUN: %clangxx -### --target=wasm32-wali-linux-musl --stdlib=libc++ %s 2>&1 \
+// RUN: --sysroot=%S/Inputs/basic_linux_libcxx_tree/usr \
+// RUN: | FileCheck -check-prefix=COMPILE_WALI %s
+// COMPILE_WALI: "-cc1"
+// COMPILE_WALI: "-resource-dir" "[[RESOURCE_DIR:[^"]*]]"
+// COMPILE_WALI: "-isysroot" "[[SYSROOT:[^"]+]]"
+// COMPILE_WALI: "-internal-isystem" "[[SYSROOT:[^"]+]]/include/wasm32-linux-musl/c++/v1"
+// COMPILE_WALI: "-internal-isystem" "[[SYSROOT:[^"]+]]/include/c++/v1"
+// COMPILE_WALI: "-internal-isystem" "[[RESOURCE_DIR]]{{(/|\\\\)}}include"
+// COMPILE_WALI: "-internal-isystem" "[[SYSROOT:[^"]+]]/include/wasm32-linux-musl"
+// COMPILE_WALI: "-internal-isystem" "[[SYSROOT:[^"]+]]/include"
+
+// RUN: %clangxx -### --target=wasm32-wali-linux-musl --stdlib=libstdc++ %s 2>&1 \
+// RUN: --sysroot=%S/Inputs/basic_linux_libstdcxx_libcxxv2_tree/usr \
+// RUN: | FileCheck -check-prefix=COMPILE_WALI_STDCXX %s
+// COMPILE_WALI_STDCXX: "-cc1"
+// COMPILE_WALI_STDCXX: "-resource-dir" "[[RESOURCE_DIR:[^"]*]]"
+// COMPILE_WALI_STDCXX: "-isysroot" "[[SYSROOT:[^"]+]]"
+// COMPILE_WALI_STDCXX: "-internal-isystem" "[[SYSROOT:[^"]+]]/include/c++/4.8/wasm32-linux-musl"
+// COMPILE_WALI_STDCXX: "-internal-isystem" "[[SYSROOT:[^"]+]]/include/c++/4.8"
+// COMPILE_WALI_STDCXX: "-internal-isystem" "[[SYSROOT:[^"]+]]/include/c++/4.8/backward"
+// COMPILE_WALI_STDCXX: "-internal-isystem" "[[RESOURCE_DIR]]{{(/|\\\\)}}include"
+// COMPILE_WALI_STDCXX: "-internal-isystem" "[[SYSROOT:[^"]+]]/include/wasm32-linux-musl"
+// COMPILE_WALI_STDCXX: "-internal-isystem" "[[SYSROOT:[^"]+]]/include"
>From f5cedb2b1c7ce8082da38c688754a4dde3e2a053 Mon Sep 17 00:00:00 2001
From: Arjun Ramesh <arjunr2 at andrew.cmu.edu>
Date: Thu, 4 Sep 2025 15:15:50 -0400
Subject: [PATCH 3/4] Add link to WALI ABI and fix unwind for sjlj non-wasm
targets
---
clang/lib/Basic/Targets/WebAssembly.h | 5 ++++-
libunwind/src/UnwindRegistersRestore.S | 8 ++++++--
libunwind/src/UnwindRegistersSave.S | 8 ++++++--
3 files changed, 16 insertions(+), 5 deletions(-)
diff --git a/clang/lib/Basic/Targets/WebAssembly.h b/clang/lib/Basic/Targets/WebAssembly.h
index 81fd40a62d3a3..4de6ce6bb5a21 100644
--- a/clang/lib/Basic/Targets/WebAssembly.h
+++ b/clang/lib/Basic/Targets/WebAssembly.h
@@ -90,7 +90,10 @@ class LLVM_LIBRARY_VISIBILITY WebAssemblyTargetInfo : public TargetInfo {
MaxAtomicPromoteWidth = MaxAtomicInlineWidth = 64;
HasUnalignedAccess = true;
if (T.isWALI()) {
- // WALI ABI requires 64-bit longs on both wasm32 and wasm64
+ // The WALI ABI is documented here:
+ // https://doc.rust-lang.org/rustc/platform-support/wasm32-wali-linux.html
+ // Currently, this ABI only applies to wasm32 targets and notably requires
+ // 64-bit longs
LongAlign = LongWidth = 64;
SizeType = UnsignedInt;
PtrDiffType = SignedInt;
diff --git a/libunwind/src/UnwindRegistersRestore.S b/libunwind/src/UnwindRegistersRestore.S
index eda0b10205ac3..c916cca277cca 100644
--- a/libunwind/src/UnwindRegistersRestore.S
+++ b/libunwind/src/UnwindRegistersRestore.S
@@ -6,7 +6,7 @@
//
//===----------------------------------------------------------------------===//
-#if !defined(__USING_SJLJ_EXCEPTIONS__) && !defined(__wasm__)
+#if !defined(__wasm__)
#include "assembly.h"
@@ -22,6 +22,8 @@
.text
#endif
+#if !defined(__USING_SJLJ_EXCEPTIONS__)
+
#if defined(__i386__)
DEFINE_LIBUNWIND_FUNCTION(__libunwind_Registers_x86_jumpto)
#
@@ -1250,6 +1252,8 @@ DEFINE_LIBUNWIND_FUNCTION(_ZN9libunwind19Registers_loongarch6jumptoEv)
#endif
+#endif /* !defined(__USING_SJLJ_EXCEPTIONS__) */
+
NO_EXEC_STACK_DIRECTIVE
-#endif /* !defined(__USING_SJLJ_EXCEPTIONS__) && !defined(__wasm__) */
+#endif /* !defined(__wasm__) */
diff --git a/libunwind/src/UnwindRegistersSave.S b/libunwind/src/UnwindRegistersSave.S
index a2ff6578b1057..66d8e962a16d0 100644
--- a/libunwind/src/UnwindRegistersSave.S
+++ b/libunwind/src/UnwindRegistersSave.S
@@ -6,7 +6,7 @@
//
//===----------------------------------------------------------------------===//
-#if !defined(__USING_SJLJ_EXCEPTIONS__) && !defined(__wasm__)
+#if !defined(__wasm__)
#include "assembly.h"
@@ -22,6 +22,8 @@
.text
#endif
+#if !defined(__USING_SJLJ_EXCEPTIONS__)
+
#if defined(__i386__)
#
@@ -1232,6 +1234,8 @@ DEFINE_LIBUNWIND_FUNCTION(__unw_getcontext)
WEAK_ALIAS(__unw_getcontext, unw_getcontext)
#endif
+#endif /* !defined(__USING_SJLJ_EXCEPTIONS__) */
+
NO_EXEC_STACK_DIRECTIVE
-#endif /* !defined(__USING_SJLJ_EXCEPTIONS__) && !defined(__wasm__) */
+#endif /* !defined(__wasm__) */
\ No newline at end of file
>From a07eb13e9658f08abf1bcc4d4723b5a9438573c8 Mon Sep 17 00:00:00 2001
From: Arjun Ramesh <arjunr2 at andrew.cmu.edu>
Date: Thu, 4 Sep 2025 15:29:18 -0400
Subject: [PATCH 4/4] Add newline
---
libunwind/src/UnwindRegistersSave.S | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/libunwind/src/UnwindRegistersSave.S b/libunwind/src/UnwindRegistersSave.S
index 66d8e962a16d0..8bf99ebd942fa 100644
--- a/libunwind/src/UnwindRegistersSave.S
+++ b/libunwind/src/UnwindRegistersSave.S
@@ -1238,4 +1238,4 @@ DEFINE_LIBUNWIND_FUNCTION(__unw_getcontext)
NO_EXEC_STACK_DIRECTIVE
-#endif /* !defined(__wasm__) */
\ No newline at end of file
+#endif /* !defined(__wasm__) */
More information about the llvm-commits
mailing list