From e4960a9e1d21e1b6bca447dcf1665353fc79129a Mon Sep 17 00:00:00 2001 From: Demetrius Kanios Date: Tue, 4 Aug 2026 00:22:05 -0700 Subject: [PATCH 1/2] druntime: Implement dummy WASI `getpid` --- runtime/druntime/src/core/sys/posix/unistd.d | 12 +++--------- 1 file changed, 3 insertions(+), 9 deletions(-) diff --git a/runtime/druntime/src/core/sys/posix/unistd.d b/runtime/druntime/src/core/sys/posix/unistd.d index ca4b06f329..a9abdd4e6f 100644 --- a/runtime/druntime/src/core/sys/posix/unistd.d +++ b/runtime/druntime/src/core/sys/posix/unistd.d @@ -114,15 +114,9 @@ else version (CRuntime_WASI) { - version (WASI_EMULATED_GETPID) - pid_t getpid() @trusted; - else - deprecated("WASI lacks process identifiers; to enable emulation of"~ - " the `getpid` function using a placeholder value, which"~ - " doesn't reflect the host PID of the program, compile"~ - " with --d-version=WASI_EMULATED_GETPID and link with"~ - " -lwasi-emulated-getpid") - pid_t getpid() @trusted; + // WASI has no processes, and wasi-libc requires you to jump through hoops + // to link this in. We just let D code use it silently. + extern(D) pid_t getpid()() @trusted => 42; } else pid_t getpid() @trusted; From 0dd6cc5f25e0c9140dafeea2b283009b9dfb20e8 Mon Sep 17 00:00:00 2001 From: Demetrius Kanios Date: Sun, 2 Aug 2026 13:38:41 -0700 Subject: [PATCH 2/2] Phobos support for WASI --- .github/actions/5a-wasi/action.yml | 25 ++++++++++++++++++++++--- .github/workflows/main.yml | 8 ++++---- CHANGELOG.md | 4 ++-- runtime/phobos | 2 +- 4 files changed, 29 insertions(+), 10 deletions(-) diff --git a/.github/actions/5a-wasi/action.yml b/.github/actions/5a-wasi/action.yml index d5e4992c96..c82a1ff99a 100644 --- a/.github/actions/5a-wasi/action.yml +++ b/.github/actions/5a-wasi/action.yml @@ -1,4 +1,4 @@ -name: "WASI: Cross-compile druntime incl. test runners, install libs + .conf, run druntime unittests" +name: "WASI: Cross-compile druntime & phobos incl. test runners, install libs + .conf, run druntime & phobos unittests" inputs: os: required: true @@ -7,7 +7,7 @@ inputs: runs: using: composite steps: - - name: Cross-compile druntime incl. test runners & install libs + .conf + - name: Cross-compile druntime & phobos incl. test runners & install libs + .conf shell: bash run: | set -eux @@ -34,7 +34,6 @@ runs: --testrunners \ CMAKE_INSTALL_PREFIX="$PWD/install" \ RT_CONF_TRIPLE_REGEX="$arch-.*-$os" \ - PHOBOS2_DIR= \ CMAKE_TOOLCHAIN_FILE="$PWD/wasi-sdk/share/cmake/wasi-sdk${toolchain_suffix}.cmake" \ BUILD_LTO_LIBS=ON @@ -48,6 +47,26 @@ runs: ../wasmtime/wasmtime -W exceptions ./druntime-test-runner-debug ../wasmtime/wasmtime -W exceptions ./druntime-test-runner + - name: Run phobos unittests + shell: bash + run: | + set -eux + cd ../ldc-build-runtime.${{ inputs.os }}-${{ inputs.arch }} + + mkdir -p testrunner_tmpdir/tmp + + export WASMTIME_BACKTRACE_DETAILS=1 + + for testrunner in ./phobos2-test-runner*; do + ../wasmtime/wasmtime -W exceptions \ + --dir testrunner_tmpdir::/ \ + --dir /usr/share/zoneinfo::/tz \ + --env TZDIR=/tz/ \ + --env HOME=/tmp \ + -S inherit-network \ + $testrunner + done + - name: Run `importc_compare` shell: bash run: | diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index e50c2d9f85..dd998f011f 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -407,12 +407,12 @@ jobs: uses: ./.github/actions/1a-setup-wasi - name: Build bootstrap LDC uses: ./.github/actions/2-build-bootstrap - - name: 'WASIp1: Cross-compile druntime incl. test runners & run druntime unittests' + - name: 'WASIp1: Cross-compile druntime & phobos incl. test runners & run druntime & phobos unittests' uses: ./.github/actions/5a-wasi with: os: wasip1 arch: ${{ matrix.arch }} - - name: 'WASIp2: Cross-compile druntime incl. test runners & run druntime unittests' + - name: 'WASIp2: Cross-compile druntime & phobos incl. test runners & run druntime & phobos unittests' uses: ./.github/actions/5a-wasi with: os: wasip2 @@ -432,13 +432,13 @@ jobs: cd ../bootstrap-ldc tar xf ${{ github.workspace }}/artifacts/*-addon-*.tar.xz # generate hello.d - echo 'void main() { import core.stdc.stdio; printf("Hello world, %d bits\n", size_t.sizeof * 8); }' > hello.d + echo 'void main() { import std.stdio; writefln!"Hello world, %d bits"(size_t.sizeof * 8); }' > hello.d # put WASI-SDK clang onto PATH export PATH="$PWD/../wasi-sdk/bin:$PATH" # build & run hello-world for os in wasip1 wasip2; do # test LTO too - for args in "" "-O -flto=full -defaultlib=druntime-ldc-lto"; do + for args in "" "-O -flto=full -defaultlib=druntime-ldc-lto,phobos2-ldc-lto"; do bin/ldc2 -mtriple=${{ matrix.arch }}-unknown-$os $args hello.d ls -l hello.wasm ../wasmtime/wasmtime -W exceptions ./hello.wasm diff --git a/CHANGELOG.md b/CHANGELOG.md index def7a5ffab..4bb81cbf26 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,10 +5,11 @@ - `-foptimize-nothrow` is the default setting when targeting Windows or WebAssembly, to make hitting https://github.com/ldc-developers/ldc/issues/3504 much less likely. - Support for [LLVM 22](https://releases.llvm.org/22.1.0/docs/ReleaseNotes.html). The prebuilt packages use v22.1.8. (#5097, #5102, #5172) - Minimum LLVM version raised to 18. (#5094) +- DRuntime and Phobos now support [WASI](https://github.com/WebAssembly/WASI) preview levels 1 and 2, based on [`wasi-libc`](https://github.com/WebAssembly/wasi-libc). Two caveats being that WASI is strictly single-threaded (currently), and that fibers are unavailable. (#5186, #5229, #5230, #5242) - New prebuilt **addon packages** (`ldc2-*-addon-.tar.xz`) for simple cross-compilation. Simply unpack them directly into your LDC installation directory, and then cross-compile via `-mtriple=…` (or `dub --target=…`): (#5240) - Windows: `x86_64-windows-msvc`, `i686-windows-msvc`; no SDK required. - Android: `aarch64-linux-android30`, `armv7a-linux-androideabi29`, `x86_64-linux-android29`, `i686-linux-android29`; needs an [Android NDK](https://developer.android.com/ndk/downloads) (r27) and its `toolchains/llvm/prebuilt/*/bin/` dir in your PATH. - - WASI: `wasm32-unknown-wasip1`, `wasm32-unknown-wasip2`; needs a [WASI SDK](https://github.com/webassembly/wasi-sdk) and its `bin/` dir in your PATH. Phobos support is coming, currently druntime only. (#5207) + - WASI: `wasm32-unknown-wasip1`, `wasm32-unknown-wasip2`; needs a [WASI SDK](https://github.com/webassembly/wasi-sdk) and its `bin/` dir in your PATH. (#5207, #5242) - The `@ldc.attributes.restrict` UDA is now supported for dynamic-array parameters too. Builtin array ops (`c[] = a[] * b[]` etc.) use it, enabling auto-vectorization. Note that for such array ops, the dlang spec prescribes that the slice on the left and any slices on the right must not overlap (https://dlang.org/spec/arrays.html#array-operations), and this knowledge is now used by the optimizer; e.g., `x[] *= x[]` is invalid. (#5148, #5168, #5176) - DMD-style inline assembly: `asm { naked; }` is now much less of a special case wrt. codegen - such naked functions are now emitted as if `asm { naked; }` was replaced with the `@naked` function UDA. IR-wise, they are not emitted as module-level asm blobs anymore, but regular IR functions. This should lift a few former `asm { naked; }`-specific restrictions and fixed at least one LTO issue. (#5041) - **dcompute**: Added support for Native device code Embedding (PTX and SPIR-V) into the host executable's `.rodata` section. (#5140) @@ -16,7 +17,6 @@ - The default `-fvisibility` setting was changed to `hidden`, like clang, so that only explicit `export`ed symbols are not hidden. And `-L--export-dynamic` isn't used by default for linking anymore either; add it manually to export all non-hidden symbols, as for normal Posix targets. The net effect is that the size of linked wasm binaries shrinks significantly by default. (#5216) - Exceptions are now supported. Currently, using D and C++ exceptions are mutually exclusive. If linking in DRuntime, you cannot link in C++ code relying on `libunwind` (exceptions), due to the hacks required to convince LLVM to use D's EH personality. A future LLVM release may help remove the constraint. (#5162) - In order to allow the GC to function correctly, a new pass is run after optimizations, to spill potential pointers onto the "stack". This pass is enabled by default for all Wasm targets, but can be disabled with `-betterC` or the new `-disable-wasm-ptrs-spill`. (#5178) -- DRuntime now supports [WASI](https://github.com/WebAssembly/WASI) preview levels 1 and 2, based on [`wasi-libc`](https://github.com/WebAssembly/wasi-libc). Two caveats being that WASI is strictly single-threaded (currently), and that fibers are unavailable. (#5186, #5229, #5230) - A new `-fno-moduleinfo-localclasses` switch has been added. This disables populating `ModuleInfo.localClasses` for each module. This can help eliminate/strip more dead-code (thus decreasing binary sizes), but breaks `Object.factory` and any other code relying on iterating over said `localClasses`. Enabled by default for Wasm targets. (#5224, #5231) - Predefined version `LDC_LLVM_*` now only contains the LLVM major version, i.e., former `version (LDC_LLVM_1801)` with LLVM v18.1 is now `version (LDC_LLVM_18)`. Use `ldc.intrinsics.LLVM_version` for backwards compatibility if really needed. (#5109) diff --git a/runtime/phobos b/runtime/phobos index 57477cf702..8017f195c4 160000 --- a/runtime/phobos +++ b/runtime/phobos @@ -1 +1 @@ -Subproject commit 57477cf70207e9692e2f51847f6b9bdd5d34b450 +Subproject commit 8017f195c44ce6dd7f883489b7b12f33dac66c45