Skip to content

Compiler tweaks for Wasm & WASI#5158

Open
QuantumSegfault wants to merge 3 commits into
ldc-developers:masterfrom
QuantumSegfault:wasm-wasi-compiler-changes
Open

Compiler tweaks for Wasm & WASI#5158
QuantumSegfault wants to merge 3 commits into
ldc-developers:masterfrom
QuantumSegfault:wasm-wasi-compiler-changes

Conversation

@QuantumSegfault

Copy link
Copy Markdown
Contributor
  • On Wasm, do as Clang does and mangle C main as either __main_void or __main_argc_argv as appropriate
  • Don't force use of direct wasm-ld for WASI targets. Allows clang to be set as the link driver (via -gcc).
  • Define appropriate versions for the new wasm32-unknown-wasip1, -wasip2, and -wasip3 targets, and account for the fact that the legacy -wasi triple aliases to p1 at the moment.
  • Emit musl-style __assert_failed on WASI.

Split from #5152

@QuantumSegfault

Copy link
Copy Markdown
Contributor Author

Requesting review

@thewilsonator

@QuantumSegfault

Copy link
Copy Markdown
Contributor Author

Should the dmd/mangle/package.d‎ changes be upstreamed instead? It's a little awkward cause it would still be dependent on this PR being merged first (for the isTargetWasm helper).

@thewilsonator

Copy link
Copy Markdown
Contributor

Should the dmd/mangle/package.d‎ changes be upstreamed instead?

Yes.

@thewilsonator

Copy link
Copy Markdown
Contributor
FAIL: LDC :: codegen/wasi.d (222 of 397)
******************** TEST 'LDC :: codegen/wasi.d' FAILED ********************
Exit Code: 1
 Command Output (stdout):
--
# RUN: at line 4
/__w/ldc/build/bin/ldc2 -mtriple=wasm32-unknown-wasi -output-ll -output-o -of=/__w/ldc/build/tests/codegen/Output/wasi.d.tmp.wasm /__w/ldc/ldc/tests/codegen/wasi.d
# executed command: /__w/ldc/build/bin/ldc2 -mtriple=wasm32-unknown-wasi -output-ll -output-o -of=/__w/ldc/build/tests/codegen/Output/wasi.d.tmp.wasm /__w/ldc/ldc/tests/codegen/wasi.d
# .---command stderr------------
# | wasm-ld: error: cannot open crt1.o: No such file or directory
# | wasm-ld: error: unable to find library -lc
# | wasm-ld: error: cannot open /usr/lib/llvm21/lib/clang/21/lib/wasm32-unknown-wasi/libclang_rt.builtins.a: No such file or directory
# | clang: error: linker command failed with exit code 1 (use -v to see invocation)
# | Error: /usr/lib/llvm21/bin/clang failed with status: 1
# `-----------------------------
# error: command failed with exit status: 1

on the Alpine Musl targets, not sure why.

@QuantumSegfault

Copy link
Copy Markdown
Contributor Author
FAIL: LDC :: codegen/wasi.d (222 of 397)
******************** TEST 'LDC :: codegen/wasi.d' FAILED ********************
Exit Code: 1
 Command Output (stdout):
--
# RUN: at line 4
/__w/ldc/build/bin/ldc2 -mtriple=wasm32-unknown-wasi -output-ll -output-o -of=/__w/ldc/build/tests/codegen/Output/wasi.d.tmp.wasm /__w/ldc/ldc/tests/codegen/wasi.d
# executed command: /__w/ldc/build/bin/ldc2 -mtriple=wasm32-unknown-wasi -output-ll -output-o -of=/__w/ldc/build/tests/codegen/Output/wasi.d.tmp.wasm /__w/ldc/ldc/tests/codegen/wasi.d
# .---command stderr------------
# | wasm-ld: error: cannot open crt1.o: No such file or directory
# | wasm-ld: error: unable to find library -lc
# | wasm-ld: error: cannot open /usr/lib/llvm21/lib/clang/21/lib/wasm32-unknown-wasi/libclang_rt.builtins.a: No such file or directory
# | clang: error: linker command failed with exit code 1 (use -v to see invocation)
# | Error: /usr/lib/llvm21/bin/clang failed with status: 1
# `-----------------------------
# error: command failed with exit status: 1

on the Alpine Musl targets, not sure why.

I guess the WASI test only runs on Alpine musl?

But the reason is now WASI links with clang, not wasm-ld directly, so it's automatically trying to link against libc and the compiler rt (which was exactly my intention for doing so). Problem is system clang isn't WASI enabled (need wasi-sdk's wasm32-wasip2-clang)

Short of introducing wasi-sdk as a dependency into the test-suite, need to keep clang from grabbing system libs

@QuantumSegfault

Copy link
Copy Markdown
Contributor Author

Okay. Main mangle upstreamed: dlang/dmd#23303

@QuantumSegfault QuantumSegfault force-pushed the wasm-wasi-compiler-changes branch from 7488640 to 454080f Compare June 23, 2026 19:43
@QuantumSegfault QuantumSegfault force-pushed the wasm-wasi-compiler-changes branch from 454080f to 9c90b84 Compare June 23, 2026 20:13
@QuantumSegfault

Copy link
Copy Markdown
Contributor Author

Okay...that should do it.

I don't know what's special about Alpine's build of llvm/clang...

But it should be fixed now. Forced it to use wasm-ld (requesting lld explicitly asks for wasm-ld) instead of default wasm-component-ld (which requires separate install) for WASIp2 target. And also to not try to link in anything from libc, etc. (cause normal distributions don't come with a WASI sysroot).

Eventually we'll want to reevaluate the Wasm/WASI CI situation, but we can do that later.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants