Skip to content

Commit

Permalink
PR #22509: [ROCm] Avoid hardcoding hipcc compiler includes
Browse files Browse the repository at this point in the history
Imported from GitHub PR #22509

Copybara import of the project:

--
f4e7d6d by Dragan Mladjenovic <[email protected]>:

[ROCm] Avoid hardcoding hipcc compiler includes

Merging this change closes #22509

COPYBARA_INTEGRATE_REVIEW=#22509 from ROCm:automatic_include f4e7d6d
PiperOrigin-RevId: 725993542
  • Loading branch information
draganmladjenovic authored and Google-ML-Automation committed Feb 12, 2025
1 parent 81194d8 commit 92f4589
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 29 deletions.
50 changes: 21 additions & 29 deletions third_party/tsl/third_party/gpus/rocm_configure.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ load(
"get_python_bin",
"raw_exec",
"realpath",
"relative_to",
"which",
)
load(
Expand Down Expand Up @@ -195,45 +196,36 @@ def auto_configure_warning(msg):
# END cc_configure common functions (see TODO above).

def _rocm_include_path(repository_ctx, rocm_config, bash_bin):
"""Generates the cxx_builtin_include_directory entries for rocm inc dirs.
"""Generates the entries for rocm inc dirs based on rocm_config.
Args:
repository_ctx: The repository context.
rocm_config: The path to the gcc host compiler.
bash_bin: path to the bash interpreter.
Returns:
A string containing the Starlark string for each of the gcc
host compiler include directories, which can be added to the CROSSTOOL
A string containing the Starlark string for each of the hipcc
compiler include directories, which can be added to the CROSSTOOL
file.
"""
inc_dirs = []

# Add full paths
rocm_toolkit_path = str(repository_ctx.path(rocm_config.rocm_toolkit_path))
inc_dirs.append(rocm_toolkit_path + "/llvm/lib/clang/8.0/include")
inc_dirs.append(rocm_toolkit_path + "/llvm/lib/clang/9.0.0/include")
inc_dirs.append(rocm_toolkit_path + "/llvm/lib/clang/10.0.0/include")
inc_dirs.append(rocm_toolkit_path + "/llvm/lib/clang/11.0.0/include")
inc_dirs.append(rocm_toolkit_path + "/llvm/lib/clang/12.0.0/include")
inc_dirs.append(rocm_toolkit_path + "/llvm/lib/clang/13.0.0/include")
inc_dirs.append(rocm_toolkit_path + "/llvm/lib/clang/14.0.0/include")
inc_dirs.append(rocm_toolkit_path + "/llvm/lib/clang/15.0.0/include")
inc_dirs.append(rocm_toolkit_path + "/llvm/lib/clang/16.0.0/include")
inc_dirs.append(rocm_toolkit_path + "/llvm/lib/clang/17.0.0/include")
inc_dirs.append(rocm_toolkit_path + "/llvm/lib/clang/17/include")
inc_dirs.append(rocm_toolkit_path + "/lib/llvm/lib/clang/17/include")
inc_dirs.append(rocm_toolkit_path + "/llvm/lib/clang/18/include")
if int(rocm_config.rocm_version_number) >= 60200:
inc_dirs.append(rocm_toolkit_path + "/lib/llvm/lib/clang/18/include")
inc_dirs.append(rocm_toolkit_path + "/lib/llvm/lib/clang/19/include")
inc_dirs.append(rocm_toolkit_path + "/lib/llvm/lib/clang/20/include")

# Support hcc based off clang 10.0.0 (for ROCm 3.3)
inc_dirs.append(rocm_toolkit_path + "/hcc/compiler/lib/clang/10.0.0/include/")
inc_dirs.append(rocm_toolkit_path + "/hcc/lib/clang/10.0.0/include")

# Add hcc headers
inc_dirs.append(rocm_toolkit_path + "/hcc/include")
# Add HIP-Clang headers (relative to rocm root)
rocm_path = repository_ctx.path(rocm_config.rocm_toolkit_path)
clang_path = rocm_path.get_child("llvm/bin/clang")
resource_dir_result = execute(repository_ctx, [str(clang_path), "-print-resource-dir"])

if resource_dir_result.return_code:
auto_configure_fail("Failed to run hipcc -print-resource-dir: %s" % err_out(resource_dir_result))

resource_dir_abs = resource_dir_result.stdout.strip()

resource_dir_rel = relative_to(repository_ctx, str(rocm_path.realpath), resource_dir_abs, bash_bin)

resource_dir = str(rocm_path.get_child(resource_dir_rel))

inc_dirs.append(resource_dir + "/include")
inc_dirs.append(resource_dir + "/share")

return inc_dirs

Expand Down
17 changes: 17 additions & 0 deletions third_party/tsl/third_party/remote_config/common.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -292,6 +292,23 @@ def realpath(repository_ctx, path, bash_bin = None):

return execute(repository_ctx, [bash_bin, "-c", "realpath \"%s\"" % path]).stdout.strip()

def relative_to(repository_ctx, base, path, bash_bin = None):
"""Returns the result of "realpath --relative-to".
Args:
repository_ctx: the repository_ctx
base: a path on the file system
path: a path on the file system
bash_bin: path to the bash interpreter
Returns:
Returns the result of "realpath --relative-to"
"""
if bash_bin == None:
bash_bin = get_bash_bin(repository_ctx)

return execute(repository_ctx, [bash_bin, "-c", "realpath --relative-to \"%s\" \"%s\"" % (base, path)]).stdout.strip()

def err_out(result):
"""Returns stderr if set, else stdout.
Expand Down

0 comments on commit 92f4589

Please sign in to comment.