Skip to content

Commit 7e7c923

Browse files
authored
[WebAssembly] Support for new target wasm32-linux-muslwali (#162581)
Add toolchain support for the [WALI](https://doc.rust-lang.org/rustc/platform-support/wasm32-wali-linux.html) target as per its corresponding [RFC](https://discourse.llvm.org/t/rfc-new-wasm-linux-target-support/88203)
1 parent a4a0a4b commit 7e7c923

File tree

10 files changed

+93
-9
lines changed

10 files changed

+93
-9
lines changed

clang/lib/Basic/Targets.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -704,6 +704,10 @@ std::unique_ptr<TargetInfo> AllocateTarget(const llvm::Triple &Triple,
704704
case llvm::Triple::Emscripten:
705705
return std::make_unique<EmscriptenTargetInfo<WebAssembly32TargetInfo>>(
706706
Triple, Opts);
707+
708+
case llvm::Triple::Linux:
709+
return std::make_unique<WALITargetInfo<WebAssembly32TargetInfo>>(Triple,
710+
Opts);
707711
case llvm::Triple::UnknownOS:
708712
return std::make_unique<WebAssemblyOSTargetInfo<WebAssembly32TargetInfo>>(
709713
Triple, Opts);

clang/lib/Basic/Targets/OSTargets.h

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -948,6 +948,23 @@ class LLVM_LIBRARY_VISIBILITY WASITargetInfo
948948
using WebAssemblyOSTargetInfo<Target>::WebAssemblyOSTargetInfo;
949949
};
950950

951+
// WALI target
952+
template <typename Target>
953+
class LLVM_LIBRARY_VISIBILITY WALITargetInfo
954+
: public WebAssemblyOSTargetInfo<Target> {
955+
void getOSDefines(const LangOptions &Opts, const llvm::Triple &Triple,
956+
MacroBuilder &Builder) const final {
957+
WebAssemblyOSTargetInfo<Target>::getOSDefines(Opts, Triple, Builder);
958+
// Linux defines; list based off of gcc output
959+
DefineStd(Builder, "unix", Opts);
960+
DefineStd(Builder, "linux", Opts);
961+
Builder.defineMacro("__wali__");
962+
}
963+
964+
public:
965+
using WebAssemblyOSTargetInfo<Target>::WebAssemblyOSTargetInfo;
966+
};
967+
951968
// Emscripten target
952969
template <typename Target>
953970
class LLVM_LIBRARY_VISIBILITY EmscriptenTargetInfo

clang/lib/Basic/Targets/WebAssembly.h

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -88,12 +88,23 @@ class LLVM_LIBRARY_VISIBILITY WebAssemblyTargetInfo : public TargetInfo {
8888
LongDoubleWidth = LongDoubleAlign = 128;
8989
LongDoubleFormat = &llvm::APFloat::IEEEquad();
9090
MaxAtomicPromoteWidth = MaxAtomicInlineWidth = 64;
91-
// size_t being unsigned long for both wasm32 and wasm64 makes mangled names
92-
// more consistent between the two.
93-
SizeType = UnsignedLong;
94-
PtrDiffType = SignedLong;
95-
IntPtrType = SignedLong;
9691
HasUnalignedAccess = true;
92+
if (T.isWALI()) {
93+
// The WALI ABI is documented here:
94+
// https://doc.rust-lang.org/rustc/platform-support/wasm32-wali-linux.html
95+
// Currently, this ABI only applies to wasm32 targets and notably requires
96+
// 64-bit longs
97+
LongAlign = LongWidth = 64;
98+
SizeType = UnsignedInt;
99+
PtrDiffType = SignedInt;
100+
IntPtrType = SignedInt;
101+
} else {
102+
// size_t being unsigned long for both wasm32 and wasm64 makes mangled
103+
// names more consistent between the two.
104+
SizeType = UnsignedLong;
105+
PtrDiffType = SignedLong;
106+
IntPtrType = SignedLong;
107+
}
97108
}
98109

99110
StringRef getABI() const override;

clang/lib/Driver/Driver.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6836,6 +6836,8 @@ const ToolChain &Driver::getToolChain(const ArgList &Args,
68366836
TC = std::make_unique<toolchains::VEToolChain>(*this, Target, Args);
68376837
else if (Target.isOHOSFamily())
68386838
TC = std::make_unique<toolchains::OHOS>(*this, Target, Args);
6839+
else if (Target.isWALI())
6840+
TC = std::make_unique<toolchains::WebAssembly>(*this, Target, Args);
68396841
else
68406842
TC = std::make_unique<toolchains::Linux>(*this, Target, Args);
68416843
break;

clang/test/Driver/wasm-toolchain.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -296,3 +296,10 @@
296296
// RUN: | FileCheck -check-prefix=LINK_WASIP2_USE_WASMLD %s
297297
// LINK_WASIP2_USE_WASMLD: "-cc1" {{.*}} "-o" "[[temp:[^"]*]]"
298298
// LINK_WASIP2_USE_WASMLD: wasm-ld{{.*}}" "-m" "wasm32" {{.*}} "[[temp]]" {{.*}}
299+
300+
// Basic `wasm32-linux-muslwali` compile-link test.
301+
302+
// RUN: %clang -### --target=wasm32-linux-muslwali --sysroot=/foo %s 2>&1 \
303+
// RUN: | FileCheck -check-prefix=LINK_WALI_BASIC %s
304+
// LINK_WALI_BASIC: "-cc1" {{.*}} "-o" "[[temp:[^"]*]]"
305+
// LINK_WALI_BASIC: wasm-ld{{.*}}" "-L/foo/lib/wasm32-linux-muslwali" "crt1.o" "[[temp]]" "-lc" "{{.*[/\\]}}libclang_rt.builtins.a" "-o" "a.out"

clang/test/Driver/wasm-toolchain.cpp

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,3 +86,28 @@
8686
// COMPILE_STDCXX: "-internal-isystem" "[[RESOURCE_DIR]]{{(/|\\\\)}}include"
8787
// COMPILE_STDCXX: "-internal-isystem" "[[SYSROOT:[^"]+]]/include/wasm32-wasi"
8888
// COMPILE_STDCXX: "-internal-isystem" "[[SYSROOT:[^"]+]]/include"
89+
90+
// RUN: %clangxx -### --target=wasm32-linux-muslwali --stdlib=libc++ %s 2>&1 \
91+
// RUN: --sysroot=%S/Inputs/basic_linux_libcxx_tree/usr \
92+
// RUN: | FileCheck -check-prefix=COMPILE_WALI %s
93+
// COMPILE_WALI: "-cc1"
94+
// COMPILE_WALI: "-resource-dir" "[[RESOURCE_DIR:[^"]*]]"
95+
// COMPILE_WALI: "-isysroot" "[[SYSROOT:[^"]+]]"
96+
// COMPILE_WALI: "-internal-isystem" "[[SYSROOT:[^"]+]]/include/wasm32-linux-muslwali/c++/v1"
97+
// COMPILE_WALI: "-internal-isystem" "[[SYSROOT:[^"]+]]/include/c++/v1"
98+
// COMPILE_WALI: "-internal-isystem" "[[RESOURCE_DIR]]{{(/|\\\\)}}include"
99+
// COMPILE_WALI: "-internal-isystem" "[[SYSROOT:[^"]+]]/include/wasm32-linux-muslwali"
100+
// COMPILE_WALI: "-internal-isystem" "[[SYSROOT:[^"]+]]/include"
101+
102+
// RUN: %clangxx -### --target=wasm32-linux-muslwali --stdlib=libstdc++ %s 2>&1 \
103+
// RUN: --sysroot=%S/Inputs/basic_linux_libstdcxx_libcxxv2_tree/usr \
104+
// RUN: | FileCheck -check-prefix=COMPILE_WALI_STDCXX %s
105+
// COMPILE_WALI_STDCXX: "-cc1"
106+
// COMPILE_WALI_STDCXX: "-resource-dir" "[[RESOURCE_DIR:[^"]*]]"
107+
// COMPILE_WALI_STDCXX: "-isysroot" "[[SYSROOT:[^"]+]]"
108+
// COMPILE_WALI_STDCXX: "-internal-isystem" "[[SYSROOT:[^"]+]]/include/c++/4.8/wasm32-linux-muslwali"
109+
// COMPILE_WALI_STDCXX: "-internal-isystem" "[[SYSROOT:[^"]+]]/include/c++/4.8"
110+
// COMPILE_WALI_STDCXX: "-internal-isystem" "[[SYSROOT:[^"]+]]/include/c++/4.8/backward"
111+
// COMPILE_WALI_STDCXX: "-internal-isystem" "[[RESOURCE_DIR]]{{(/|\\\\)}}include"
112+
// COMPILE_WALI_STDCXX: "-internal-isystem" "[[SYSROOT:[^"]+]]/include/wasm32-linux-muslwali"
113+
// COMPILE_WALI_STDCXX: "-internal-isystem" "[[SYSROOT:[^"]+]]/include"

libunwind/src/UnwindRegistersRestore.S

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
//
77
//===----------------------------------------------------------------------===//
88

9+
#if !defined(__wasm__)
10+
911
#include "assembly.h"
1012

1113
#define FROM_0_TO_15 0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15
@@ -20,7 +22,7 @@
2022
.text
2123
#endif
2224

23-
#if !defined(__USING_SJLJ_EXCEPTIONS__) && !defined(__wasm__)
25+
#if !defined(__USING_SJLJ_EXCEPTIONS__)
2426

2527
#if defined(__i386__)
2628
DEFINE_LIBUNWIND_FUNCTION(__libunwind_Registers_x86_jumpto)
@@ -1253,7 +1255,8 @@ DEFINE_LIBUNWIND_FUNCTION(_ZN9libunwind19Registers_loongarch6jumptoEv)
12531255

12541256
#endif
12551257

1256-
#endif /* !defined(__USING_SJLJ_EXCEPTIONS__) && !defined(__wasm__) */
1258+
#endif /* !defined(__USING_SJLJ_EXCEPTIONS__) */
12571259

12581260
NO_EXEC_STACK_DIRECTIVE
12591261

1262+
#endif /* !defined(__wasm__) */

libunwind/src/UnwindRegistersSave.S

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
//
77
//===----------------------------------------------------------------------===//
88

9+
#if !defined(__wasm__)
10+
911
#include "assembly.h"
1012

1113
#define FROM_0_TO_15 0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15
@@ -20,7 +22,7 @@
2022
.text
2123
#endif
2224

23-
#if !defined(__USING_SJLJ_EXCEPTIONS__) && !defined(__wasm__)
25+
#if !defined(__USING_SJLJ_EXCEPTIONS__)
2426

2527
#if defined(__i386__)
2628

@@ -1232,6 +1234,8 @@ DEFINE_LIBUNWIND_FUNCTION(__unw_getcontext)
12321234
WEAK_ALIAS(__unw_getcontext, unw_getcontext)
12331235
#endif
12341236

1235-
#endif /* !defined(__USING_SJLJ_EXCEPTIONS__) && !defined(__wasm__) */
1237+
#endif /* !defined(__USING_SJLJ_EXCEPTIONS__) */
12361238

12371239
NO_EXEC_STACK_DIRECTIVE
1240+
1241+
#endif /* !defined(__wasm__) */

llvm/include/llvm/TargetParser/Triple.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -277,6 +277,7 @@ class Triple {
277277
MuslF32,
278278
MuslSF,
279279
MuslX32,
280+
MuslWALI,
280281
LLVM,
281282

282283
MSVC,
@@ -798,6 +799,12 @@ class Triple {
798799
return getObjectFormat() == Triple::DXContainer;
799800
}
800801

802+
/// Tests whether the target uses WALI Wasm
803+
bool isWALI() const {
804+
return getArch() == Triple::wasm32 && isOSLinux() &&
805+
getEnvironment() == Triple::MuslWALI;
806+
}
807+
801808
/// Tests whether the target is the PS4 platform.
802809
bool isPS4() const {
803810
return getArch() == Triple::x86_64 &&
@@ -840,6 +847,7 @@ class Triple {
840847
getEnvironment() == Triple::MuslF32 ||
841848
getEnvironment() == Triple::MuslSF ||
842849
getEnvironment() == Triple::MuslX32 ||
850+
getEnvironment() == Triple::MuslWALI ||
843851
getEnvironment() == Triple::OpenHOS || isOSLiteOS();
844852
}
845853

llvm/lib/TargetParser/Triple.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -375,6 +375,8 @@ StringRef Triple::getEnvironmentTypeName(EnvironmentType Kind) {
375375
case MuslSF:
376376
return "muslsf";
377377
case MuslX32: return "muslx32";
378+
case MuslWALI:
379+
return "muslwali";
378380
case Simulator: return "simulator";
379381
case Pixel: return "pixel";
380382
case Vertex: return "vertex";
@@ -767,6 +769,7 @@ static Triple::EnvironmentType parseEnvironment(StringRef EnvironmentName) {
767769
.StartsWith("muslf32", Triple::MuslF32)
768770
.StartsWith("muslsf", Triple::MuslSF)
769771
.StartsWith("muslx32", Triple::MuslX32)
772+
.StartsWith("muslwali", Triple::MuslWALI)
770773
.StartsWith("musl", Triple::Musl)
771774
.StartsWith("msvc", Triple::MSVC)
772775
.StartsWith("itanium", Triple::Itanium)

0 commit comments

Comments
 (0)