From 4c4454274a17d058b5173e8d3eff7a8a43b2baa5 Mon Sep 17 00:00:00 2001 From: SpamDoodler Date: Wed, 12 Apr 2023 11:18:37 +0200 Subject: [PATCH 1/3] Add Wasm support (in progress) --- examples/wasm_example/BUILD.bazel | 9 +++++++++ examples/wasm_example/wasm_example.cpp | 3 +++ ll/args.bzl | 7 +++++++ ll/attributes.bzl | 7 +++++++ ll/environment.bzl | 12 +++++++++++- ll/inputs.bzl | 6 +++--- ll/tools.bzl | 2 +- ll/transitions.bzl | 1 + 8 files changed, 42 insertions(+), 5 deletions(-) create mode 100644 examples/wasm_example/BUILD.bazel create mode 100644 examples/wasm_example/wasm_example.cpp 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..92481b1c 100644 --- a/ll/args.bzl +++ b/ll/args.bzl @@ -257,6 +257,9 @@ def compile_object_args( format = "--rocm-device-lib-path=%s", ) + if ctx.attr.compilation_mode == "wasm": + args.add("--target=wasm32") + # Write compilation database. args.add("-Xarch_host") args.add(cdf, format = "-MJ%s") @@ -520,6 +523,10 @@ def link_executable_args(ctx, in_files, out_file, mode): format = "-rpath=$ORIGIN/%s", ) + if ctx.attr.compilation_mode == "wasm": + 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..f0f79056 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,12 @@ LL_TOOLCHAIN_ATTRS = { cfg = transition_to_bootstrap, default = "@llvm-project//llvm:llvm-link", ), + "wasm_linker": attr.label( + doc = "The linker for WebAssembly", + executable = True, + cfg = transition_to_bootstrap, + default = "@llvm-project//llvm: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..7e88e65c 100644 --- a/ll/environment.bzl +++ b/ll/environment.bzl @@ -20,7 +20,7 @@ def compile_object_environment(ctx): config = ctx.attr.toolchain_configuration[BuildSettingInfo].value toolchain = ctx.toolchains["//ll:toolchain_type"] - if config in ["cpp", "omp_cpu"]: + if config in ["cpp", "omp_cpu", "wasm"]: return { "LINK": toolchain.bitcode_linker.path, "LLD": toolchain.linker.path, @@ -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/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( From 889995d379b7d28746fc411ab87d3509d92041f5 Mon Sep 17 00:00:00 2001 From: SpamDoodler Date: Wed, 12 Apr 2023 12:11:30 +0200 Subject: [PATCH 2/3] Change wasm-ld target --- ll/args.bzl | 1 + ll/attributes.bzl | 3 ++- ll/environment.bzl | 2 +- ll/toolchain.bzl | 1 + 4 files changed, 5 insertions(+), 2 deletions(-) diff --git a/ll/args.bzl b/ll/args.bzl index 92481b1c..ffdd1eca 100644 --- a/ll/args.bzl +++ b/ll/args.bzl @@ -259,6 +259,7 @@ def compile_object_args( if ctx.attr.compilation_mode == "wasm": args.add("--target=wasm32") + args.add("-emit-llvm-bc") # Write compilation database. args.add("-Xarch_host") diff --git a/ll/attributes.bzl b/ll/attributes.bzl index f0f79056..5f544c92 100644 --- a/ll/attributes.bzl +++ b/ll/attributes.bzl @@ -313,8 +313,9 @@ LL_TOOLCHAIN_ATTRS = { "wasm_linker": attr.label( doc = "The linker for WebAssembly", executable = True, + allow_single_file = True, cfg = transition_to_bootstrap, - default = "@llvm-project//llvm:wasm-ld", + default = "@llvm-project//lld:wasm-ld", ), "builtin_includes": attr.label( doc = "Clang's built-in header files.", diff --git a/ll/environment.bzl b/ll/environment.bzl index 7e88e65c..3d20ad72 100644 --- a/ll/environment.bzl +++ b/ll/environment.bzl @@ -20,7 +20,7 @@ def compile_object_environment(ctx): config = ctx.attr.toolchain_configuration[BuildSettingInfo].value toolchain = ctx.toolchains["//ll:toolchain_type"] - if config in ["cpp", "omp_cpu", "wasm"]: + if config in ["cpp", "omp_cpu"]: return { "LINK": toolchain.bitcode_linker.path, "LLD": toolchain.linker.path, 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, From 151e6247ddbbac9e370b8223ad1119c4fdc005a2 Mon Sep 17 00:00:00 2001 From: SpamDoodler Date: Wed, 12 Apr 2023 12:34:38 +0200 Subject: [PATCH 3/3] Sync --- ll/args.bzl | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/ll/args.bzl b/ll/args.bzl index ffdd1eca..dc3e4022 100644 --- a/ll/args.bzl +++ b/ll/args.bzl @@ -258,8 +258,8 @@ def compile_object_args( ) if ctx.attr.compilation_mode == "wasm": - args.add("--target=wasm32") - args.add("-emit-llvm-bc") + args.add("--target=wasm32-unknown-unknown") + # args.add("-emit-llvm-bc") # Write compilation database. args.add("-Xarch_host") @@ -525,8 +525,9 @@ def link_executable_args(ctx, in_files, out_file, mode): ) if ctx.attr.compilation_mode == "wasm": - args.add("--no-entry") - args.add("--export-all") # This must be changed (similar to cppm) + pass + # args.add("--no-entry") + # args.add("--export-all") # This must be changed (similar to cppm) # Additional system libraries. args.add("-lm") # Math.