Skip to content

Commit 4c44542

Browse files
committed
Add Wasm support (in progress)
1 parent be56c3b commit 4c44542

8 files changed

Lines changed: 42 additions & 5 deletions

File tree

examples/wasm_example/BUILD.bazel

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
load("@rules_ll//ll:defs.bzl", "ll_binary")
2+
3+
ll_binary(
4+
name = "wasm_example",
5+
srcs = ["wasm_example.cpp"],
6+
compilation_mode = "wasm",
7+
compile_flags = ["-std=c++20"],
8+
visibility = ["@//:__pkg__"],
9+
)
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
extern "C" int add(int a, int b) {
2+
return a + b;
3+
}

ll/args.bzl

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -257,6 +257,9 @@ def compile_object_args(
257257
format = "--rocm-device-lib-path=%s",
258258
)
259259

260+
if ctx.attr.compilation_mode == "wasm":
261+
args.add("--target=wasm32")
262+
260263
# Write compilation database.
261264
args.add("-Xarch_host")
262265
args.add(cdf, format = "-MJ%s")
@@ -520,6 +523,10 @@ def link_executable_args(ctx, in_files, out_file, mode):
520523
format = "-rpath=$ORIGIN/%s",
521524
)
522525

526+
if ctx.attr.compilation_mode == "wasm":
527+
args.add("--no-entry")
528+
args.add("--export-all") # This must be changed (similar to cppm)
529+
523530
# Additional system libraries.
524531
args.add("-lm") # Math.
525532
args.add("-ldl") # Dynamic linking.

ll/attributes.bzl

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ DEFAULT_ATTRS = {
4040
"cuda_nvptx",
4141
"hip_amdgpu",
4242
"hip_nvptx",
43+
"wasm",
4344
"bootstrap",
4445
],
4546
),
@@ -309,6 +310,12 @@ LL_TOOLCHAIN_ATTRS = {
309310
cfg = transition_to_bootstrap,
310311
default = "@llvm-project//llvm:llvm-link",
311312
),
313+
"wasm_linker": attr.label(
314+
doc = "The linker for WebAssembly",
315+
executable = True,
316+
cfg = transition_to_bootstrap,
317+
default = "@llvm-project//llvm:wasm-ld",
318+
),
312319
"builtin_includes": attr.label(
313320
doc = "Clang's built-in header files.",
314321
cfg = "target",

ll/environment.bzl

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ def compile_object_environment(ctx):
2020
config = ctx.attr.toolchain_configuration[BuildSettingInfo].value
2121
toolchain = ctx.toolchains["//ll:toolchain_type"]
2222

23-
if config in ["cpp", "omp_cpu"]:
23+
if config in ["cpp", "omp_cpu", "wasm"]:
2424
return {
2525
"LINK": toolchain.bitcode_linker.path,
2626
"LLD": toolchain.linker.path,
@@ -30,6 +30,16 @@ def compile_object_environment(ctx):
3030
toolchain.linker_executable.dirname,
3131
]),
3232
}
33+
elif config in ["wasm"]:
34+
return {
35+
"LINK": toolchain.bitcode_linker.path,
36+
"LLD": toolchain.wasm_linker.path,
37+
"LLVM_SYMBOLIZER_PATH": toolchain.symbolizer.path,
38+
"PATH": ":".join([
39+
toolchain.linker.dirname,
40+
toolchain.linker_executable.dirname,
41+
]),
42+
}
3343
elif config in ["cuda_nvptx", "hip_amdgpu", "hip_nvptx"]:
3444
return {
3545
"CLANG_OFFLOAD_BUNDLER": toolchain.offload_bundler.path,

ll/inputs.bzl

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ def compile_object_inputs(
7171
toolchain.compiler_runtime
7272
)
7373

74-
if config == "cpp":
74+
if config in ["cpp", "wasm"]:
7575
pass
7676
elif config == "omp_cpu":
7777
direct += toolchain.omp_header
@@ -135,7 +135,7 @@ def link_executable_inputs(ctx, in_files):
135135
if ctx.attr.depends_on_llvm:
136136
direct += toolchain.llvm_project_artifacts
137137

138-
if config == "cpp":
138+
if config in ["cpp", "wasm"]:
139139
pass
140140
elif config == "omp_cpu":
141141
direct += toolchain.libomp
@@ -185,7 +185,7 @@ def link_shared_object_inputs(ctx, in_files):
185185
toolchain.compiler_runtime
186186
)
187187

188-
if config == "cpp":
188+
if config == ["cpp", "wasm"]:
189189
pass
190190
elif config == "cuda_nvptx":
191191
pass

ll/tools.bzl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ def compile_object_tools(ctx):
3333
toolchain.opt,
3434
]
3535

36-
if config in ["cpp", "omp_cpu"]:
36+
if config in ["cpp", "omp_cpu", "wasm"]:
3737
return tools
3838

3939
if config in ["cuda_nvptx", "hip_nvptx", "hip_amdgpu"]:

ll/transitions.bzl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ COMPILATION_MODES = [
1010
"cuda_nvptx",
1111
"hip_amdgpu",
1212
"hip_nvptx",
13+
"wasm",
1314
]
1415

1516
def _ll_transition_impl(

0 commit comments

Comments
 (0)