diff --git a/.bazelrc b/.bazelrc index a5c71cc8..7c659722 100644 --- a/.bazelrc +++ b/.bazelrc @@ -266,5 +266,6 @@ common:rocm_common --copt=-Wno-gnu-offsetof-extensions common:rocm --config=rocm_common common:rocm --dynamic_mode=off common:rocm --@rules_ml_toolchain//common:enable_hermetic_cc=True +common:rocm --@rules_ml_toolchain//common:enable_hermetic_libraries=True try-import %workspace%/.bazelrc.local diff --git a/.github/workflows/rocm_linux_x86_64_linux_x86_64_build.yaml b/.github/workflows/rocm_linux_x86_64_linux_x86_64_build.yaml index 1f9b3500..98d6bfd6 100644 --- a/.github/workflows/rocm_linux_x86_64_linux_x86_64_build.yaml +++ b/.github/workflows/rocm_linux_x86_64_linux_x86_64_build.yaml @@ -90,44 +90,3 @@ jobs: --define=using_rocm=true \ //cc/tests/cpu:all - - name: Test ROCm GPU code compilation (local sysroot) - run: | - bazel test --color=yes \ - --@rules_ml_toolchain//cc/sysroots:use_local_sysroot=True \ - --config=rocm \ - --repo_env=TF_ROCM_AMDGPU_TARGETS="gfx942" \ - --repo_env=ROCM_DISTRO_VERSION="rocm_7.12.0_gfx94X" \ - --define=using_rocm=true \ - //cc/tests/gpu/rocm:all - - - name: Test host (CPU) code compilation (local sysroot) - run: | - bazel test --color=yes \ - --@rules_ml_toolchain//cc/sysroots:use_local_sysroot=True \ - --config=rocm \ - --repo_env=TF_ROCM_AMDGPU_TARGETS="gfx942" \ - --repo_env=ROCM_DISTRO_VERSION="rocm_7.12.0_gfx94X" \ - --define=using_rocm=true \ - //cc/tests/cpu:all - - - name: Test ROCm GPU code compilation (local sysroot + bzlmod) - run: | - bazel test --color=yes \ - --@rules_ml_toolchain//cc/sysroots:use_local_sysroot=True \ - --config=rocm \ - --repo_env=TF_ROCM_AMDGPU_TARGETS="gfx942" \ - --repo_env=ROCM_DISTRO_VERSION="rocm_7.12.0_gfx94X" \ - --config=bzlmod \ - --define=using_rocm=true \ - //cc/tests/gpu/rocm:all - - - name: Test host (CPU) code compilation (local sysroot + bzlmod) - run: | - bazel test --color=yes \ - --@rules_ml_toolchain//cc/sysroots:use_local_sysroot=True \ - --config=rocm \ - --repo_env=TF_ROCM_AMDGPU_TARGETS="gfx942" \ - --repo_env=ROCM_DISTRO_VERSION="rocm_7.12.0_gfx94X" \ - --config=bzlmod \ - --define=using_rocm=true \ - //cc/tests/cpu:all diff --git a/cc/config/constants.bzl b/cc/config/constants.bzl index f55acaa2..871616ad 100644 --- a/cc/config/constants.bzl +++ b/cc/config/constants.bzl @@ -18,4 +18,8 @@ LIBCXX_FLAGS = [ "-lc++", "-lc++abi", "-lunwind", -] \ No newline at end of file +] + +LIBSTDCXX_FLAGS = [ + "-lstdc++", +] diff --git a/cc/impls/linux_x86_64_linux_x86_64_rocm/BUILD b/cc/impls/linux_x86_64_linux_x86_64_rocm/BUILD index e89089b5..06146733 100644 --- a/cc/impls/linux_x86_64_linux_x86_64_rocm/BUILD +++ b/cc/impls/linux_x86_64_linux_x86_64_rocm/BUILD @@ -1,5 +1,3 @@ -load("@local_sysroot_config//:defs.bzl", "local_sysroot_info") - # Copyright 2026 Google LLC # # Licensed under the Apache License, Version 2.0 (the "License"); @@ -21,10 +19,11 @@ load("@local_sysroot_config//:defs.bzl", "local_sysroot_info") load("@config_rocm_hipcc//rocm:build_defs.bzl", "hipcc_config") load("@rules_cc//cc:defs.bzl", "cc_toolchain") +load("//cc/config:constants.bzl", "LIBCXX_FLAGS", "LIBSTDCXX_FLAGS") load("//cc/rocm/features:rocm_hipcc_feature.bzl", "rocm_hipcc_feature") load("//third_party/rules_cc_toolchain:toolchain_config.bzl", "cc_toolchain_config") load("//third_party/rules_cc_toolchain/features:cc_toolchain_import.bzl", "cc_toolchain_import") -load("//third_party/rules_cc_toolchain/features:features.bzl", "cc_toolchain_import_feature") +load("//third_party/rules_cc_toolchain/features:features.bzl", "cc_feature", "cc_toolchain_import_feature") package( default_visibility = [ @@ -85,18 +84,16 @@ cc_toolchain_import( alias( name = "startup_libs", - actual = select({ - # Local sysroot: startup libs via --sysroot flag - "//cc/sysroots:local_sysroot_enabled": ":empty_startup_libs", - # Hermetic sysroot: use hermetic startup libs - "//conditions:default": "@sysroot_linux_x86_64//:startup_libs", - }), + actual = "@sysroot_linux_x86_64//:startup_libs", visibility = ["//visibility:public"], ) cc_toolchain_import_feature( name = "startup_libs_feature", - enabled = True, + enabled = select({ + "//common:is_hermetic_libraries_enabled": True, + "//conditions:default": False, + }), toolchain_import = ":startup_libs", ) @@ -113,25 +110,65 @@ cc_toolchain_import( # Hermetic LLVM headers "@llvm_linux_x86_64//:compiler_incs", "@sysroot_linux_x86_64//:sys_incs", - "@llvm_linux_x86_64//:libclang_rt", - ] + select({ - # Local sysroot: don't include hermetic libs (use system libs via --sysroot=/) - "//cc/sysroots:local_sysroot_enabled": [], - # Hermetic sysroot: use hermetic libs - "//conditions:default": [ - "@sysroot_linux_x86_64//:std_libs", - "@sysroot_linux_x86_64//:sys_libs", - ], - }), + ":std_libs", + "@sysroot_linux_x86_64//:sys_libs", + ], visibility = ["//visibility:public"], ) +config_setting( + name = "is_static_libcxx", + flag_values = { + "//common:stdlib": "libc++", + "//common:static_libcxx": "True", + }, +) + +config_setting( + name = "is_static_libstdcxx", + flag_values = { + "//common:stdlib": "libstdc++", + "//common:static_libcxx": "True", + }, +) + +# Feature to force static linking of libstdc++ +# Use compiler's built-in flags for static linking +cc_feature( + name = "static_link_libstdcxx", + enabled = select({ + ":is_static_libstdcxx": True, + "//conditions:default": False, + }), + linker_flags = [ + "-static-libstdc++", + "-static-libgcc", + ], +) + cc_toolchain_import_feature( name = "imports_feature", enabled = True, toolchain_import = ":imports", ) +# Conditional hermetic libraries feature +# Disabled by default for XLA (to avoid -nodefaultlibs issues with ROCm) +# Can be enabled via --@rules_ml_toolchain//common:enable_hermetic_libraries=True +cc_feature( + name = "hermetic_libraries_conditional", + cc_flags = ["-nostdinc++"], + compiler_flags = ["-nostdinc"], + enabled = select({ + "//common:is_hermetic_libraries_enabled": True, + "//conditions:default": False, + }), + linker_flags = [ + "-nostdlib", + "-nodefaultlibs", + ], +) + # buildifier: leave-alone filegroup( name = "all", @@ -195,15 +232,9 @@ filegroup( # Features # ============================================================================= -FEATURES_ESSENTIAL = select({ - # Local sysroot: DO NOT use hermetic_libraries (no -nostdinc/-nostdinc++) - # We want to use system headers and libraries - "//cc/sysroots:local_sysroot_enabled": [ - "//cc/features:local_sysroot_default_libs", - ], - # Hermetic sysroot: use hermetic_libraries feature - "//conditions:default": ["//third_party/rules_cc_toolchain/features:hermetic_libraries"], -}) + [ +FEATURES_ESSENTIAL = [ + # Conditional hermetic libraries (disabled by default for XLA) + ":hermetic_libraries_conditional", # Library imports ":imports_feature", ":startup_libs_feature", @@ -218,10 +249,7 @@ FEATURES_ESSENTIAL = select({ "//third_party/rules_cc_toolchain/features:warnings", "//third_party/rules_cc_toolchain/features:reproducible", "//third_party/rules_cc_toolchain/features:use_lld", -] + select({ - "//cc/sysroots:local_sysroot_enabled": ["//cc/features:local_sysroot_linux"], - "//conditions:default": ["//cc/features:sysroot_linux_x86_64"], -}) + [ + "//cc/features:sysroot_linux_x86_64", # PIC / PIE flags "//third_party/rules_cc_toolchain/features:supports_pic", "//third_party/rules_cc_toolchain/features:position_independent_code", @@ -254,12 +282,7 @@ cc_toolchain_config( c_compiler = "@llvm_linux_x86_64//:clang", cc_compiler = "@llvm_linux_x86_64//:clang++", compiler_features = FEATURES_ESSENTIAL, - cxx_builtin_include_directories = select({ - # Local sysroot: use dynamically detected system include paths - "//cc/sysroots:local_sysroot_enabled": local_sysroot_info.include_directories, - # Hermetic sysroot: no system directories - "//conditions:default": [], - }), + cxx_builtin_include_directories = [], linker = "@llvm_linux_x86_64//:ld", strip_tool = "@llvm_linux_x86_64//:strip", target_cpu = "x86_64", @@ -281,3 +304,20 @@ cc_toolchain( toolchain_config = ":config", toolchain_identifier = "toolchain_linux_x86_64_linux_x86_64_rocm_id", ) + +# Aliases to select between libc++ and libstdc++ +alias( + name = "std_incs", + actual = select({ + "//common:is_stdlib_libcxx": "@llvm_linux_x86_64//:std_incs", + "//conditions:default": "@sysroot_linux_x86_64//:std_incs", + }), +) + +alias( + name = "std_libs", + actual = select({ + "//common:is_stdlib_libcxx": "@llvm_linux_x86_64//:std_libs", + "//conditions:default": "@sysroot_linux_x86_64//:std_libs", + }), +) diff --git a/cc/impls/linux_x86_64_linux_x86_64_rocm/wrappers/hipcc_wrapper b/cc/impls/linux_x86_64_linux_x86_64_rocm/wrappers/hipcc_wrapper index b523a5fa..72905912 100755 --- a/cc/impls/linux_x86_64_linux_x86_64_rocm/wrappers/hipcc_wrapper +++ b/cc/impls/linux_x86_64_linux_x86_64_rocm/wrappers/hipcc_wrapper @@ -100,63 +100,6 @@ def GetOptionValue(argv, option): return sum(vars(args)[option], []) -def GetHostCompilerOptions(argv, filter_hermetic_llvm=False, filter_sysroot=False): - """Collect include paths and sysroot options from argv. - - This function extracts -isystem, -iquote, --sysroot, and -g flags from - the argument list, with optional filtering for GPU compilation. - - Args: - argv: A list of strings containing command-line arguments. - filter_hermetic_llvm: If True, filter out hermetic LLVM headers. - Used for GPU compilation where ROCm provides its own LLVM headers. - filter_sysroot: If True, filter out --sysroot flag. - Used for GPU compilation to avoid system headers. - - Returns: - A list of options ready to be passed to hipcc or clang. - - Notes: - - Filters hermetic LLVM headers for GPU (they conflict with ROCm's) - - Filters sysroot headers for GPU (incompatible with ROCm clang) - - Used by InvokeHipcc to build GPU compiler options - """ - parser = ArgumentParser() - parser.add_argument("-isystem", nargs="*", action="append") - parser.add_argument("-iquote", nargs="*", action="append") - parser.add_argument("--sysroot", nargs=1) - parser.add_argument("-g", nargs="*", action="append") - parser.add_argument("-no-canonical-prefixes", action="store_true") - - args, _ = parser.parse_known_args(argv) - - opts = [] - - if args.isystem: - for p in sum(args.isystem, []): - # Skip hermetic LLVM headers for GPU compilation - they conflict with ROCm's - # ROCm provides its own LLVM headers via --rocm-path - # Replace + with / for Bzlmod canonical repo name - if filter_hermetic_llvm and "/llvm" in p.replace("+", "/") and "/clang/" in p: - continue - # Skip sysroot headers for GPU compilation - they have non-device-compatible operator new - # ROCm provides device-compatible standard library headers via cuda_wrappers - if filter_sysroot and "/sysroot_" in p: - continue - opts.extend(["-isystem", p]) - if args.iquote: - for p in sum(args.iquote, []): - opts.extend(["-iquote", p]) - if args.g: - opts.append("-g" + "".join(sum(args.g, []))) - if args.no_canonical_prefixes: - opts.append("-no-canonical-prefixes") - if args.sysroot and not filter_sysroot: - opts.extend(["--sysroot", args.sysroot[0]]) - - return opts - - def GetHipccOptions(argv): """Collect ROCm/hipcc-specific compiler options from argv. diff --git a/common/BUILD b/common/BUILD index cb1df122..412b3da7 100644 --- a/common/BUILD +++ b/common/BUILD @@ -108,6 +108,18 @@ config_setting( }, ) +bool_flag( + name = "enable_hermetic_libraries", + build_setting_default = False, +) + +config_setting( + name = "is_hermetic_libraries_enabled", + flag_values = { + ":enable_hermetic_libraries": "True", + }, +) + string_flag( name = "stdlib", build_setting_default = "libstdc++",