diff --git a/examples/wasm_example/BUILD.bazel b/examples/wasm_example/BUILD.bazel new file mode 100644 index 00000000..b10402ec --- /dev/null +++ b/examples/wasm_example/BUILD.bazel @@ -0,0 +1,9 @@ +load("@rules_ll//ll:defs.bzl", "ll_binary") + +ll_binary( + name = "wasm_example", + srcs = ["wasm_example.cpp"], + compilation_mode = "wasm", + compile_flags = ["-std=c++20"], + visibility = ["@//:__pkg__"], +) diff --git a/examples/wasm_example/wasm_example.cpp b/examples/wasm_example/wasm_example.cpp new file mode 100644 index 00000000..6c84aed1 --- /dev/null +++ b/examples/wasm_example/wasm_example.cpp @@ -0,0 +1,3 @@ +extern "C" int add(int a, int b) { + return a + b; +} diff --git a/ll/args.bzl b/ll/args.bzl index 3352c59c..dc3e4022 100644 --- a/ll/args.bzl +++ b/ll/args.bzl @@ -257,6 +257,10 @@ def compile_object_args( format = "--rocm-device-lib-path=%s", ) + if ctx.attr.compilation_mode == "wasm": + args.add("--target=wasm32-unknown-unknown") + # args.add("-emit-llvm-bc") + # Write compilation database. args.add("-Xarch_host") args.add(cdf, format = "-MJ%s") @@ -520,6 +524,11 @@ def link_executable_args(ctx, in_files, out_file, mode): format = "-rpath=$ORIGIN/%s", ) + if ctx.attr.compilation_mode == "wasm": + pass + # args.add("--no-entry") + # args.add("--export-all") # This must be changed (similar to cppm) + # Additional system libraries. args.add("-lm") # Math. args.add("-ldl") # Dynamic linking. diff --git a/ll/attributes.bzl b/ll/attributes.bzl index 51b03847..5f544c92 100644 --- a/ll/attributes.bzl +++ b/ll/attributes.bzl @@ -40,6 +40,7 @@ DEFAULT_ATTRS = { "cuda_nvptx", "hip_amdgpu", "hip_nvptx", + "wasm", "bootstrap", ], ), @@ -309,6 +310,13 @@ LL_TOOLCHAIN_ATTRS = { cfg = transition_to_bootstrap, default = "@llvm-project//llvm:llvm-link", ), + "wasm_linker": attr.label( + doc = "The linker for WebAssembly", + executable = True, + allow_single_file = True, + cfg = transition_to_bootstrap, + default = "@llvm-project//lld:wasm-ld", + ), "builtin_includes": attr.label( doc = "Clang's built-in header files.", cfg = "target", diff --git a/ll/environment.bzl b/ll/environment.bzl index 2d6dbe59..3d20ad72 100644 --- a/ll/environment.bzl +++ b/ll/environment.bzl @@ -30,6 +30,16 @@ def compile_object_environment(ctx): toolchain.linker_executable.dirname, ]), } + elif config in ["wasm"]: + return { + "LINK": toolchain.bitcode_linker.path, + "LLD": toolchain.wasm_linker.path, + "LLVM_SYMBOLIZER_PATH": toolchain.symbolizer.path, + "PATH": ":".join([ + toolchain.linker.dirname, + toolchain.linker_executable.dirname, + ]), + } elif config in ["cuda_nvptx", "hip_amdgpu", "hip_nvptx"]: return { "CLANG_OFFLOAD_BUNDLER": toolchain.offload_bundler.path, diff --git a/ll/inputs.bzl b/ll/inputs.bzl index 8bb69fed..6563d5d1 100644 --- a/ll/inputs.bzl +++ b/ll/inputs.bzl @@ -71,7 +71,7 @@ def compile_object_inputs( toolchain.compiler_runtime ) - if config == "cpp": + if config in ["cpp", "wasm"]: pass elif config == "omp_cpu": direct += toolchain.omp_header @@ -135,7 +135,7 @@ def link_executable_inputs(ctx, in_files): if ctx.attr.depends_on_llvm: direct += toolchain.llvm_project_artifacts - if config == "cpp": + if config in ["cpp", "wasm"]: pass elif config == "omp_cpu": direct += toolchain.libomp @@ -185,7 +185,7 @@ def link_shared_object_inputs(ctx, in_files): toolchain.compiler_runtime ) - if config == "cpp": + if config == ["cpp", "wasm"]: pass elif config == "cuda_nvptx": pass diff --git a/ll/toolchain.bzl b/ll/toolchain.bzl index e6d39eeb..06a2c5e3 100644 --- a/ll/toolchain.bzl +++ b/ll/toolchain.bzl @@ -30,6 +30,7 @@ def _ll_toolchain_impl(ctx): linker = lld_alias, linker_executable = ctx.executable.linker, linker_wrapper = ctx.executable.linker_wrapper, + wasm_linker = ctx.executable.wasm_linker, address_sanitizer = ctx.files.address_sanitizer, leak_sanitizer = ctx.files.leak_sanitizer, memory_sanitizer = ctx.files.memory_sanitizer, diff --git a/ll/tools.bzl b/ll/tools.bzl index 0a5e3656..eff0db62 100644 --- a/ll/tools.bzl +++ b/ll/tools.bzl @@ -33,7 +33,7 @@ def compile_object_tools(ctx): toolchain.opt, ] - if config in ["cpp", "omp_cpu"]: + if config in ["cpp", "omp_cpu", "wasm"]: return tools if config in ["cuda_nvptx", "hip_nvptx", "hip_amdgpu"]: diff --git a/ll/transitions.bzl b/ll/transitions.bzl index 645d7f76..986fe8b2 100644 --- a/ll/transitions.bzl +++ b/ll/transitions.bzl @@ -10,6 +10,7 @@ COMPILATION_MODES = [ "cuda_nvptx", "hip_amdgpu", "hip_nvptx", + "wasm", ] def _ll_transition_impl(