Skip to content

Commit e19cccd

Browse files
oligamiqbjorn3
andauthored
Compile rustc+LLVM to wasm (#6)
* Compile rustc for wasm15 with llvm * Some fixes * Fix llvm-project submodule * Another fix * Fix linking libc++ on wasi * Disable linker optimizations for wasm This is really slow when producing a 156MB rustc.wasm output. * Better linking of libc++ * Faster LLVM compiles * Remove some unnecessary configs * Some preparation for once LLVM can compile for wasm32-wasip1 without threads * Rustfmt * Simplify LLVM build code a bit --------- Co-authored-by: bjorn3 <[email protected]>
1 parent 214c2df commit e19cccd

File tree

14 files changed

+283
-16
lines changed

14 files changed

+283
-16
lines changed

.gitmodules

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,8 @@
3232
shallow = true
3333
[submodule "src/llvm-project"]
3434
path = src/llvm-project
35-
url = https://github.com/rust-lang/llvm-project.git
36-
branch = rustc/19.1-2024-07-30
35+
url = https://github.com/YoWASP/llvm-project
36+
branch = main+wasm
3737
shallow = true
3838
[submodule "src/doc/embedded-book"]
3939
path = src/doc/embedded-book

Cargo.lock

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -407,9 +407,12 @@ version = "0.1.0"
407407

408408
[[package]]
409409
name = "cc"
410-
version = "1.0.105"
410+
version = "1.1.15"
411411
source = "registry+https://github.com/rust-lang/crates.io-index"
412-
checksum = "5208975e568d83b6b05cc0a063c8e7e9acc2b43bee6da15616a5b73e109d7437"
412+
checksum = "57b6a275aa2903740dc87da01c62040406b8812552e97129a63ea8850a17c6e6"
413+
dependencies = [
414+
"shlex",
415+
]
413416

414417
[[package]]
415418
name = "cfg-if"

comment.txt

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,25 @@ $ gcc -fuse-ld=lld tmp/rmeta*/lib.rmeta tmp/rust_out.* dist/lib/rustlib/x86_64-u
1616
$ ./rust_out
1717
Hello World!
1818
```
19+
20+
use LLVM
21+
Install example:
22+
WASI_SDK_PATH=`pwd`/wasi-sdk-24.0-x86_64-linux WASI_SYSROOT=`pwd`/wasi-sdk-24.0-x86_64-linux/share/wasi-sysroot ./x.py install
23+
24+
If you just want to run it, https://github.com/oligamiq/rust_wasm/tree/main/rustc_llvm
25+
```
26+
$ mkdir tmp
27+
$ echo 'fn main() { println!("Hello World!"); }' | wasmtime run -Sthreads=y -Spreview2=n --dir tmp::/ --dir dist --env RUST_MIN_STACK=16777216 dist/bin/rustc.wasm - --sysroot dist --target wasm32-wasip1-threads -Csave-temps
28+
$ gcc -fuse-ld=lld tmp/rmeta*/lib.rmeta tmp/rust_out.*.o dist/lib/rustlib/x86_64-unknown-linux-gnu/lib/lib*.rlib -o rust_out
29+
$ ./rust_out
30+
Hello World!
31+
```
32+
33+
to Wasi
34+
```
35+
$ mkdir tmp
36+
$ echo 'fn main() { println!("Hello World!"); }' | wasmtime run -Sthreads=y -Spreview2=n --dir tmp::/ --dir dist --env RUST_MIN_STACK=16777216 dist/bin/rustc.wasm - --sysroot dist --target wasm32-wasip1-threads -Csave-temps
37+
$ wasi-sdk-24.0-x86_64-linux/bin/wasm-ld --shared-memory --max-memory=1073741824 --import-memory --export __main_void -z stack-size=1048576 --stack-first --allow-undefined --no-demangle --import-memory --export-memory --shared-memory dist/lib/rustlib/wasm32-wasip1-threads/lib/self-contained/crt1-command.o tmp/rust_out.*.o dist/lib/rustlib/wasm32-wasip1-threads/lib/lib*.rlib -L dist/lib/rustlib/wasm32-wasip1-threads/lib/self-contained -lc -o rust_out.wasm
38+
$ wasmtime run -Sthreads=y rust_out.wasm
39+
Hello World!
40+
```

compiler/rustc_codegen_ssa/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ edition = "2021"
88
ar_archive_writer = "0.4.2"
99
arrayvec = { version = "0.7", default-features = false }
1010
bitflags = "2.4.1"
11-
cc = "=1.0.105" # FIXME(cc): pinned to keep support for VS2013
11+
cc = "1.1.15"
1212
either = "1.5.0"
1313
itertools = "0.12"
1414
jobserver = "0.1.28"

compiler/rustc_codegen_ssa/src/back/linker.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -657,7 +657,7 @@ impl<'a> Linker for GccLinker<'a> {
657657
if self.sess.opts.optimize == config::OptLevel::Default
658658
|| self.sess.opts.optimize == config::OptLevel::Aggressive
659659
{
660-
self.link_arg("-O1");
660+
//self.link_arg("-O1");
661661
}
662662
}
663663

compiler/rustc_fs_util/src/lib.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#[cfg(any(unix, windows))]
1+
#[cfg(any(unix, windows, target_os = "wasi"))]
22
use std::ffi::CString;
33
use std::path::{absolute, Path, PathBuf};
44
use std::{fs, io};
@@ -84,7 +84,7 @@ pub fn path_to_c_string(p: &Path) -> CString {
8484
let p: &OsStr = p.as_ref();
8585
CString::new(p.as_bytes()).unwrap()
8686
}
87-
#[cfg(windows)]
87+
#[cfg(any(windows, target_os = "wasi"))]
8888
pub fn path_to_c_string(p: &Path) -> CString {
8989
CString::new(p.to_str().unwrap()).unwrap()
9090
}

compiler/rustc_interface/src/util.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -103,10 +103,8 @@ fn run_in_thread_with_globals<F: FnOnce(CurrentGcx) -> R + Send, R: Send>(
103103
// name contains null bytes.
104104
let r = builder
105105
.spawn_scoped(s, move || {*/
106-
rustc_span::create_session_globals_then(edition, Some(sm_inputs), || {
107-
f(CurrentGcx::new())
108-
})
109-
/*})
106+
rustc_span::create_session_globals_then(edition, Some(sm_inputs), || f(CurrentGcx::new()))
107+
/*})
110108
.unwrap()
111109
.join();
112110

compiler/rustc_llvm/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,5 @@ libc = "0.2.73"
1010

1111
[build-dependencies]
1212
# tidy-alphabetical-start
13-
cc = "=1.0.105" # FIXME(cc): pinned to keep support for VS2013
13+
cc = "1.1.15"
1414
# tidy-alphabetical-end

compiler/rustc_llvm/build.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,10 @@ fn output(cmd: &mut Command) -> String {
102102
}
103103

104104
fn main() {
105+
if env::var("TARGET").expect("TARGET was not set").contains("wasi") {
106+
std::env::var("WASI_SYSROOT").expect("WASI_SYSROOT not set");
107+
}
108+
105109
for component in REQUIRED_COMPONENTS.iter().chain(OPTIONAL_COMPONENTS.iter()) {
106110
println!("cargo:rustc-check-cfg=cfg(llvm_component,values(\"{component}\"))");
107111
}
@@ -201,6 +205,15 @@ fn main() {
201205
cfg.define("NDEBUG", None);
202206
}
203207

208+
if target.contains("wasi") {
209+
// ref src/bootstrap/src/core/build_steps/llvm.rs
210+
211+
let wasi_sysroot = env::var("WASI_SYSROOT").expect("WASI_SYSROOT not set");
212+
cfg.compiler(format!("{wasi_sysroot}/../../bin/{target}-clang++"));
213+
cfg.flag("-pthread");
214+
cfg.flag("-D_WASI_EMULATED_MMAN");
215+
}
216+
204217
rerun_if_changed_anything_in_dir(Path::new("llvm-wrapper"));
205218
cfg.file("llvm-wrapper/PassWrapper.cpp")
206219
.file("llvm-wrapper/RustWrapper.cpp")
@@ -363,6 +376,7 @@ fn main() {
363376
|| target.contains("freebsd")
364377
|| target.contains("windows-gnullvm")
365378
|| target.contains("aix")
379+
|| target.contains("wasi")
366380
{
367381
"c++"
368382
} else if target.contains("netbsd") && llvm_static_stdcpp.is_some() {

compiler/rustc_target/src/spec/targets/wasm32_wasip1_threads.rs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,13 @@ pub(crate) fn target() -> Target {
1717

1818
options.add_pre_link_args(
1919
LinkerFlavor::WasmLld(Cc::No),
20-
&["--import-memory", "--export-memory", "--shared-memory", "-Wl,--max-memory=1073741824"],
20+
&[
21+
"--import-memory",
22+
"--export-memory",
23+
"--shared-memory",
24+
"--max-memory=1073741824",
25+
"-lwasi-emulated-mman",
26+
],
2127
);
2228
options.add_pre_link_args(
2329
LinkerFlavor::WasmLld(Cc::Yes),
@@ -27,6 +33,7 @@ pub(crate) fn target() -> Target {
2733
"-Wl,--export-memory,",
2834
"-Wl,--shared-memory",
2935
"-Wl,--max-memory=1073741824",
36+
"-lwasi-emulated-mman",
3037
],
3138
);
3239

0 commit comments

Comments
 (0)