From b437c2bdfa6a8ce3c9893d0d5d7b4e6b60753cb9 Mon Sep 17 00:00:00 2001 From: Lukas Geiger Date: Thu, 9 Apr 2020 17:55:14 +0100 Subject: [PATCH] Add aarch64 cross compile toolchain --- arm_compiler.BUILD | 10 + tensorflow/workspace.bzl | 17 +- third_party/toolchains/cpus/arm/BUILD | 26 +++ .../cpus/arm/arm_compiler_configure.bzl | 8 +- .../toolchains/cpus/arm/cc_config.bzl.tpl | 178 +++++++++++++++++- 5 files changed, 232 insertions(+), 7 deletions(-) diff --git a/arm_compiler.BUILD b/arm_compiler.BUILD index b3e1b2f12c5..1ddc0c62dfa 100644 --- a/arm_compiler.BUILD +++ b/arm_compiler.BUILD @@ -50,6 +50,16 @@ filegroup( ]), ) +filegroup( + name = "aarch64_compiler_pieces", + srcs = glob([ + "aarch64-none-linux-gnu/**", + "libexec/**", + "lib/gcc/aarch64-none-linux-gnu/**", + "include/**", + ]), +) + filegroup( name = "compiler_components", srcs = [ diff --git a/tensorflow/workspace.bzl b/tensorflow/workspace.bzl index f4fb80d5caa..5f5b9d8cdb4 100755 --- a/tensorflow/workspace.bzl +++ b/tensorflow/workspace.bzl @@ -110,7 +110,8 @@ def tf_repositories(path_prefix = "", tf_repo_name = ""): arm_compiler_configure( name = "local_config_arm_compiler", build_file = clean_dep("//third_party/toolchains/cpus/arm:BUILD"), - remote_config_repo = "../arm_compiler", + remote_config_repo_arm = "../arm_compiler", + remote_config_repo_aarch64 = "../aarch64_compiler", ) mkl_repository( @@ -222,6 +223,20 @@ def tf_repositories(path_prefix = "", tf_repo_name = ""): ], ) + tf_http_archive( + # This is the latest `aarch64-none-linux-gnu` compiler provided by ARM + # See https://developer.arm.com/tools-and-software/open-source-software/developer-tools/gnu-toolchain/gnu-a/downloads + # The archive contains GCC version 9.2.1 + name = "aarch64_compiler", + build_file = "//:arm_compiler.BUILD", + sha256 = "8dfe681531f0bd04fb9c53cf3c0a3368c616aa85d48938eebe2b516376e06a66", + strip_prefix = "gcc-arm-9.2-2019.12-x86_64-aarch64-none-linux-gnu", + urls = [ + "https://storage.googleapis.com/mirror.tensorflow.org/developer.arm.com/-/media/Files/downloads/gnu-a/9.2-2019.12/binrel/gcc-arm-9.2-2019.12-x86_64-aarch64-none-linux-gnu.tar.xz", + "https://developer.arm.com/-/media/Files/downloads/gnu-a/9.2-2019.12/binrel/gcc-arm-9.2-2019.12-x86_64-aarch64-none-linux-gnu.tar.xz", + ], + ) + tf_http_archive( name = "libxsmm_archive", build_file = clean_dep("//third_party:libxsmm.BUILD"), diff --git a/third_party/toolchains/cpus/arm/BUILD b/third_party/toolchains/cpus/arm/BUILD index 5d388e918b2..30a39254fc6 100644 --- a/third_party/toolchains/cpus/arm/BUILD +++ b/third_party/toolchains/cpus/arm/BUILD @@ -12,6 +12,7 @@ cc_toolchain_suite( "armeabi|compiler": ":cc-compiler-armeabi", "local|compiler": ":cc-compiler-local", "armeabi": ":cc-compiler-armeabi", + "aarch64": ":cc-compiler-aarch64", "k8": ":cc-compiler-local", "piii": ":cc-compiler-local", "arm": ":cc-compiler-local", @@ -31,6 +32,13 @@ filegroup( ], ) +filegroup( + name = "aarch64_linux_all_files", + srcs = [ + "@aarch64_compiler//:aarch64_compiler_pieces", + ], +) + cc_toolchain_config( name = "local_config", cpu = "local", @@ -66,3 +74,21 @@ cc_toolchain( toolchain_config = ":armeabi_config", toolchain_identifier = "arm-linux-gnueabihf", ) + +cc_toolchain_config( + name = "aarch64_config", + cpu = "aarch64", +) + +cc_toolchain( + name = "cc-compiler-aarch64", + all_files = ":aarch64_linux_all_files", + compiler_files = ":aarch64_linux_all_files", + dwp_files = ":empty", + linker_files = ":aarch64_linux_all_files", + objcopy_files = "aarch64_linux_all_files", + strip_files = "aarch64_linux_all_files", + supports_param_files = 1, + toolchain_config = ":aarch64_config", + toolchain_identifier = "aarch64-linux-gnu", +) diff --git a/third_party/toolchains/cpus/arm/arm_compiler_configure.bzl b/third_party/toolchains/cpus/arm/arm_compiler_configure.bzl index 8433acfc916..3215ea849fc 100644 --- a/third_party/toolchains/cpus/arm/arm_compiler_configure.bzl +++ b/third_party/toolchains/cpus/arm/arm_compiler_configure.bzl @@ -22,7 +22,10 @@ def _arm_compiler_configure_impl(repository_ctx): python_include_path = "/usr/include/python2.7" _tpl(repository_ctx, "cc_config.bzl", { "%{ARM_COMPILER_PATH}%": str(repository_ctx.path( - repository_ctx.attr.remote_config_repo, + repository_ctx.attr.remote_config_repo_arm, + )), + "%{AARCH64_COMPILER_PATH}%": str(repository_ctx.path( + repository_ctx.attr.remote_config_repo_aarch64, )), "%{PYTHON_INCLUDE_PATH}%": python_include_path, }) @@ -31,7 +34,8 @@ def _arm_compiler_configure_impl(repository_ctx): arm_compiler_configure = repository_rule( implementation = _arm_compiler_configure_impl, attrs = { - "remote_config_repo": attr.string(mandatory = False, default = ""), + "remote_config_repo_arm": attr.string(mandatory = False, default = ""), + "remote_config_repo_aarch64": attr.string(mandatory = False, default = ""), "build_file": attr.label(), }, ) diff --git a/third_party/toolchains/cpus/arm/cc_config.bzl.tpl b/third_party/toolchains/cpus/arm/cc_config.bzl.tpl index bfe91e711bc..6c68660a162 100644 --- a/third_party/toolchains/cpus/arm/cc_config.bzl.tpl +++ b/third_party/toolchains/cpus/arm/cc_config.bzl.tpl @@ -18,6 +18,8 @@ load("@bazel_tools//tools/build_defs/cc:action_names.bzl", "ACTION_NAMES") def _impl(ctx): if (ctx.attr.cpu == "armeabi"): toolchain_identifier = "arm-linux-gnueabihf" + elif (ctx.attr.cpu == "aarch64"): + toolchain_identifier = "aarch64-linux-gnu" elif (ctx.attr.cpu == "local"): toolchain_identifier = "local_linux" else: @@ -25,6 +27,8 @@ def _impl(ctx): if (ctx.attr.cpu == "armeabi"): host_system_name = "armeabi" + elif (ctx.attr.cpu == "aarch64"): + host_system_name = "aarch64" elif (ctx.attr.cpu == "local"): host_system_name = "local" else: @@ -32,6 +36,8 @@ def _impl(ctx): if (ctx.attr.cpu == "armeabi"): target_system_name = "armeabi" + elif (ctx.attr.cpu == "aarch64"): + target_system_name = "aarch64" elif (ctx.attr.cpu == "local"): target_system_name = "local" else: @@ -39,6 +45,8 @@ def _impl(ctx): if (ctx.attr.cpu == "armeabi"): target_cpu = "armeabi" + elif (ctx.attr.cpu == "aarch64"): + target_cpu = "aarch64" elif (ctx.attr.cpu == "local"): target_cpu = "local" else: @@ -46,6 +54,8 @@ def _impl(ctx): if (ctx.attr.cpu == "armeabi"): target_libc = "armeabi" + elif (ctx.attr.cpu == "aarch64"): + target_libc = "aarch64" elif (ctx.attr.cpu == "local"): target_libc = "local" else: @@ -55,6 +65,8 @@ def _impl(ctx): if (ctx.attr.cpu == "armeabi"): abi_version = "armeabi" + elif (ctx.attr.cpu == "aarch64"): + abi_version = "aarch64" elif (ctx.attr.cpu == "local"): abi_version = "local" else: @@ -62,6 +74,8 @@ def _impl(ctx): if (ctx.attr.cpu == "armeabi"): abi_libc_version = "armeabi" + elif (ctx.attr.cpu == "aarch64"): + abi_libc_version = "aarch64" elif (ctx.attr.cpu == "local"): abi_libc_version = "local" else: @@ -127,6 +141,8 @@ def _impl(ctx): if (ctx.attr.cpu == "armeabi"): action_configs = [] + elif (ctx.attr.cpu == "aarch64"): + action_configs = [] elif (ctx.attr.cpu == "local"): action_configs = [objcopy_embed_data_action] else: @@ -165,7 +181,7 @@ def _impl(ctx): ], ) - if (ctx.attr.cpu == "armeabi"): + if (ctx.attr.cpu == "armeabi" or ctx.attr.cpu == "aarch64"): unfiltered_compile_flags_feature = feature( name = "unfiltered_compile_flags", enabled = True, @@ -340,6 +356,107 @@ def _impl(ctx): "%{ARM_COMPILER_PATH}%/arm-rpi-linux-gnueabihf/include/c++/6.5.0/", "-isystem", "%{PYTHON_INCLUDE_PATH}%", + ], + ), + ], + ), + ], + ) + elif (ctx.attr.cpu == "aarch64"): + default_compile_flags_feature = feature( + name = "default_compile_flags", + enabled = True, + flag_sets = [ + flag_set( + actions = [ + ACTION_NAMES.assemble, + ACTION_NAMES.preprocess_assemble, + ACTION_NAMES.linkstamp_compile, + ACTION_NAMES.c_compile, + ACTION_NAMES.cpp_compile, + ACTION_NAMES.cpp_header_parsing, + ACTION_NAMES.cpp_module_compile, + ACTION_NAMES.cpp_module_codegen, + ACTION_NAMES.lto_backend, + ACTION_NAMES.clif_match, + ], + flag_groups = [ + flag_group( + flags = [ + "-U_FORTIFY_SOURCE", + "-D_FORTIFY_SOURCE=1", + "-fstack-protector", + "-DRASPBERRY_PI", + ], + ), + ], + ), + flag_set( + actions = [ + ACTION_NAMES.assemble, + ACTION_NAMES.preprocess_assemble, + ACTION_NAMES.linkstamp_compile, + ACTION_NAMES.c_compile, + ACTION_NAMES.cpp_compile, + ACTION_NAMES.cpp_header_parsing, + ACTION_NAMES.cpp_module_compile, + ACTION_NAMES.cpp_module_codegen, + ACTION_NAMES.lto_backend, + ACTION_NAMES.clif_match, + ], + flag_groups = [flag_group(flags = ["-g"])], + with_features = [with_feature_set(features = ["dbg"])], + ), + flag_set( + actions = [ + ACTION_NAMES.assemble, + ACTION_NAMES.preprocess_assemble, + ACTION_NAMES.linkstamp_compile, + ACTION_NAMES.c_compile, + ACTION_NAMES.cpp_compile, + ACTION_NAMES.cpp_header_parsing, + ACTION_NAMES.cpp_module_compile, + ACTION_NAMES.cpp_module_codegen, + ACTION_NAMES.lto_backend, + ACTION_NAMES.clif_match, + ], + flag_groups = [ + flag_group( + flags = [ + "-g0", + "-O2", + "-DNDEBUG", + "-ffunction-sections", + "-fdata-sections", + ], + ), + ], + with_features = [with_feature_set(features = ["opt"])], + ), + flag_set( + actions = [ + ACTION_NAMES.linkstamp_compile, + ACTION_NAMES.cpp_compile, + ACTION_NAMES.cpp_header_parsing, + ACTION_NAMES.cpp_module_compile, + ACTION_NAMES.cpp_module_codegen, + ACTION_NAMES.lto_backend, + ACTION_NAMES.clif_match, + ], + flag_groups = [ + flag_group( + flags = [ + "-std=c++11", + "-isystem", + "%{AARCH64_COMPILER_PATH}%/aarch64-none-linux-gnu/include/c++/9.2.1/", + "-isystem", + "%{AARCH64_COMPILER_PATH}%/lib/gcc/aarch64-none-linux-gnu/9.2.1/include", + "-isystem", + "%{AARCH64_COMPILER_PATH}%/lib/gcc/aarch64-none-linux-gnu/9.2.1/include-fixed", + "-isystem", + "%{AARCH64_COMPILER_PATH}%/aarch64-none-linux-gnu/libc/usr/include/", + "-isystem", + "%{PYTHON_INCLUDE_PATH}%", "-isystem", "/usr/include/", ], @@ -467,7 +584,7 @@ def _impl(ctx): ), ], ) - elif (ctx.attr.cpu == "armeabi"): + elif (ctx.attr.cpu == "armeabi" or ctx.attr.cpu == "aarch64"): default_link_flags_feature = feature( name = "default_link_flags", enabled = True, @@ -542,7 +659,7 @@ def _impl(ctx): sysroot_feature, unfiltered_compile_flags_feature, ] - elif (ctx.attr.cpu == "armeabi"): + elif (ctx.attr.cpu == "armeabi" or ctx.attr.cpu == "aarch64"): features = [ default_compile_flags_feature, default_link_flags_feature, @@ -566,6 +683,15 @@ def _impl(ctx): "/usr/include", "/tmp/openblas_install/include/", ] + elif (ctx.attr.cpu == "aarch64"): + cxx_builtin_include_directories = [ + "%{AARCH64_COMPILER_PATH}%/aarch64-none-linux-gnu/include/c++/9.2.1/", + "%{AARCH64_COMPILER_PATH}%/lib/gcc/aarch64-none-linux-gnu/9.2.1/include", + "%{AARCH64_COMPILER_PATH}%/lib/gcc/aarch64-none-linux-gnu/9.2.1/include-fixed", + "%{AARCH64_COMPILER_PATH}%/aarch64-none-linux-gnu/libc/usr/include/", + "/usr/include", + "/tmp/openblas_install/include/", + ] elif (ctx.attr.cpu == "local"): cxx_builtin_include_directories = ["/usr/lib/gcc/", "/usr/local/include", "/usr/include"] else: @@ -619,6 +745,50 @@ def _impl(ctx): path = "%{ARM_COMPILER_PATH}%/bin/arm-rpi-linux-gnueabihf-strip", ), ] + elif (ctx.attr.cpu == "aarch64"): + tool_paths = [ + tool_path( + name = "ar", + path = "%{AARCH64_COMPILER_PATH}%/bin/aarch64-none-linux-gnu-ar", + ), + tool_path(name = "compat-ld", path = "/bin/false"), + tool_path( + name = "cpp", + path = "%{AARCH64_COMPILER_PATH}%/bin/aarch64-none-linux-gnu-cpp", + ), + tool_path( + name = "dwp", + path = "%{AARCH64_COMPILER_PATH}%/bin/aarch64-none-linux-gnu-dwp", + ), + tool_path( + name = "gcc", + path = "%{AARCH64_COMPILER_PATH}%/bin/aarch64-none-linux-gnu-gcc", + ), + tool_path( + name = "gcov", + path = "%{AARCH64_COMPILER_PATH}%/bin/aarch64-none-linux-gnu-gcov", + ), + tool_path( + name = "ld", + path = "%{AARCH64_COMPILER_PATH}%/bin/aarch64-none-linux-gnu-ld", + ), + tool_path( + name = "nm", + path = "%{AARCH64_COMPILER_PATH}%/bin/aarch64-none-linux-gnu-nm", + ), + tool_path( + name = "objcopy", + path = "%{AARCH64_COMPILER_PATH}%/bin/aarch64-none-linux-gnu-objcopy", + ), + tool_path( + name = "objdump", + path = "%{AARCH64_COMPILER_PATH}%/bin/aarch64-none-linux-gnu-objdump", + ), + tool_path( + name = "strip", + path = "%{AARCH64_COMPILER_PATH}%/bin/aarch64-none-linux-gnu-strip", + ), + ] elif (ctx.attr.cpu == "local"): tool_paths = [ tool_path(name = "ar", path = "/usr/bin/ar"), @@ -666,7 +836,7 @@ def _impl(ctx): cc_toolchain_config = rule( implementation = _impl, attrs = { - "cpu": attr.string(mandatory=True, values=["armeabi", "local"]), + "cpu": attr.string(mandatory=True, values=["armeabi", "aarch64", "local"]), }, provides = [CcToolchainConfigInfo], executable = True,