Skip to content

Commit d6de186

Browse files
draganmladjenoviccopybara-github
authored andcommitted
PR #22509: [ROCm] Avoid hardcoding hipcc compiler includes
Imported from GitHub PR openxla/xla#22509 Copybara import of the project: -- f4e7d6d91fa349eab54478a9f03875159378f237 by Dragan Mladjenovic <[email protected]>: [ROCm] Avoid hardcoding hipcc compiler includes Merging this change closes #22509 PiperOrigin-RevId: 725993542
1 parent bc32bb2 commit d6de186

File tree

2 files changed

+38
-29
lines changed

2 files changed

+38
-29
lines changed

third_party/gpus/rocm_configure.bzl

Lines changed: 21 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ load(
2828
"get_python_bin",
2929
"raw_exec",
3030
"realpath",
31+
"relative_to",
3132
"which",
3233
)
3334
load(
@@ -195,45 +196,36 @@ def auto_configure_warning(msg):
195196
# END cc_configure common functions (see TODO above).
196197

197198
def _rocm_include_path(repository_ctx, rocm_config, bash_bin):
198-
"""Generates the cxx_builtin_include_directory entries for rocm inc dirs.
199+
"""Generates the entries for rocm inc dirs based on rocm_config.
199200
200201
Args:
201202
repository_ctx: The repository context.
202203
rocm_config: The path to the gcc host compiler.
204+
bash_bin: path to the bash interpreter.
203205
204206
Returns:
205-
A string containing the Starlark string for each of the gcc
206-
host compiler include directories, which can be added to the CROSSTOOL
207+
A string containing the Starlark string for each of the hipcc
208+
compiler include directories, which can be added to the CROSSTOOL
207209
file.
208210
"""
209211
inc_dirs = []
210212

211-
# Add full paths
212-
rocm_toolkit_path = str(repository_ctx.path(rocm_config.rocm_toolkit_path))
213-
inc_dirs.append(rocm_toolkit_path + "/llvm/lib/clang/8.0/include")
214-
inc_dirs.append(rocm_toolkit_path + "/llvm/lib/clang/9.0.0/include")
215-
inc_dirs.append(rocm_toolkit_path + "/llvm/lib/clang/10.0.0/include")
216-
inc_dirs.append(rocm_toolkit_path + "/llvm/lib/clang/11.0.0/include")
217-
inc_dirs.append(rocm_toolkit_path + "/llvm/lib/clang/12.0.0/include")
218-
inc_dirs.append(rocm_toolkit_path + "/llvm/lib/clang/13.0.0/include")
219-
inc_dirs.append(rocm_toolkit_path + "/llvm/lib/clang/14.0.0/include")
220-
inc_dirs.append(rocm_toolkit_path + "/llvm/lib/clang/15.0.0/include")
221-
inc_dirs.append(rocm_toolkit_path + "/llvm/lib/clang/16.0.0/include")
222-
inc_dirs.append(rocm_toolkit_path + "/llvm/lib/clang/17.0.0/include")
223-
inc_dirs.append(rocm_toolkit_path + "/llvm/lib/clang/17/include")
224-
inc_dirs.append(rocm_toolkit_path + "/lib/llvm/lib/clang/17/include")
225-
inc_dirs.append(rocm_toolkit_path + "/llvm/lib/clang/18/include")
226-
if int(rocm_config.rocm_version_number) >= 60200:
227-
inc_dirs.append(rocm_toolkit_path + "/lib/llvm/lib/clang/18/include")
228-
inc_dirs.append(rocm_toolkit_path + "/lib/llvm/lib/clang/19/include")
229-
inc_dirs.append(rocm_toolkit_path + "/lib/llvm/lib/clang/20/include")
230-
231-
# Support hcc based off clang 10.0.0 (for ROCm 3.3)
232-
inc_dirs.append(rocm_toolkit_path + "/hcc/compiler/lib/clang/10.0.0/include/")
233-
inc_dirs.append(rocm_toolkit_path + "/hcc/lib/clang/10.0.0/include")
234-
235-
# Add hcc headers
236-
inc_dirs.append(rocm_toolkit_path + "/hcc/include")
213+
# Add HIP-Clang headers (relative to rocm root)
214+
rocm_path = repository_ctx.path(rocm_config.rocm_toolkit_path)
215+
clang_path = rocm_path.get_child("llvm/bin/clang")
216+
resource_dir_result = execute(repository_ctx, [str(clang_path), "-print-resource-dir"])
217+
218+
if resource_dir_result.return_code:
219+
auto_configure_fail("Failed to run hipcc -print-resource-dir: %s" % err_out(resource_dir_result))
220+
221+
resource_dir_abs = resource_dir_result.stdout.strip()
222+
223+
resource_dir_rel = relative_to(repository_ctx, str(rocm_path.realpath), resource_dir_abs, bash_bin)
224+
225+
resource_dir = str(rocm_path.get_child(resource_dir_rel))
226+
227+
inc_dirs.append(resource_dir + "/include")
228+
inc_dirs.append(resource_dir + "/share")
237229

238230
return inc_dirs
239231

third_party/remote_config/common.bzl

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -292,6 +292,23 @@ def realpath(repository_ctx, path, bash_bin = None):
292292

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

295+
def relative_to(repository_ctx, base, path, bash_bin = None):
296+
"""Returns the result of "realpath --relative-to".
297+
298+
Args:
299+
repository_ctx: the repository_ctx
300+
base: a path on the file system
301+
path: a path on the file system
302+
bash_bin: path to the bash interpreter
303+
304+
Returns:
305+
Returns the result of "realpath --relative-to"
306+
"""
307+
if bash_bin == None:
308+
bash_bin = get_bash_bin(repository_ctx)
309+
310+
return execute(repository_ctx, [bash_bin, "-c", "realpath --relative-to \"%s\" \"%s\"" % (base, path)]).stdout.strip()
311+
295312
def err_out(result):
296313
"""Returns stderr if set, else stdout.
297314

0 commit comments

Comments
 (0)