Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .bazelrc
Original file line number Diff line number Diff line change
Expand Up @@ -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
41 changes: 0 additions & 41 deletions .github/workflows/rocm_linux_x86_64_linux_x86_64_build.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
6 changes: 5 additions & 1 deletion cc/config/constants.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,8 @@ LIBCXX_FLAGS = [
"-lc++",
"-lc++abi",
"-lunwind",
]
]

LIBSTDCXX_FLAGS = [
"-lstdc++",
]
118 changes: 79 additions & 39 deletions cc/impls/linux_x86_64_linux_x86_64_rocm/BUILD
Original file line number Diff line number Diff line change
@@ -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");
Expand All @@ -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 = [
Expand Down Expand Up @@ -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",
)

Expand All @@ -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",
},
)
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All config_setting in //common use names with prefix 'is_', try to use the same name convention in ROCM.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done


# 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",
Expand Down Expand Up @@ -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",
Expand All @@ -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",
Expand Down Expand Up @@ -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",
Expand All @@ -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",
}),
)
57 changes: 0 additions & 57 deletions cc/impls/linux_x86_64_linux_x86_64_rocm/wrappers/hipcc_wrapper
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand Down
12 changes: 12 additions & 0 deletions common/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -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++",
Expand Down
Loading