Skip to content

Commit 674767d

Browse files
Merge pull request #2911 from ROCm/r2.18-rocm-enhanced-clang-19
Support clang19 as host compiler
2 parents 44c80c1 + 37996a3 commit 674767d

File tree

6 files changed

+94
-204
lines changed

6 files changed

+94
-204
lines changed

third_party/gpus/compiler_common_tools.bzl

+8-2
Original file line numberDiff line numberDiff line change
@@ -72,8 +72,14 @@ def _get_cxx_inc_directories_impl(repository_ctx, cc, lang_is_cpp, tf_sys_root):
7272
sysroot = []
7373
if tf_sys_root:
7474
sysroot += ["--sysroot", tf_sys_root]
75+
no_canonical_prefixes_supported = _is_compiler_option_supported(
76+
repository_ctx,
77+
cc,
78+
"-no-canonical-prefixes",
79+
)
80+
no_canonical_prefixes = (["-no-canonical-prefixes"] if no_canonical_prefixes_supported else [])
7581
result = raw_exec(repository_ctx, [cc, "-E", "-x" + lang, "-", "-v"] +
76-
sysroot)
82+
sysroot + no_canonical_prefixes)
7783
stderr = err_out(result)
7884
index1 = stderr.find(_INC_DIR_MARKER_BEGIN)
7985
if index1 == -1:
@@ -148,7 +154,7 @@ def _get_cxx_inc_directories_impl(repository_ctx, cc, lang_is_cpp, tf_sys_root):
148154
compiler_includes = compiler_includes + unresolved_compiler_includes
149155
return compiler_includes
150156

151-
def get_cxx_inc_directories(repository_ctx, cc, tf_sys_root):
157+
def get_cxx_inc_directories(repository_ctx, cc, tf_sys_root = None):
152158
"""Compute the list of default C and C++ include directories."""
153159

154160
# For some reason `clang -xc` sometimes returns include paths that are

third_party/gpus/rocm_configure.bzl

+22-100
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,12 @@ load(
2020
"get_python_bin",
2121
"raw_exec",
2222
"realpath",
23+
"relative_to",
2324
"which",
2425
)
2526
load(
2627
":compiler_common_tools.bzl",
28+
"get_cxx_inc_directories",
2729
"to_list_of_strings",
2830
)
2931
load(
@@ -101,68 +103,6 @@ def find_cc(repository_ctx):
101103
" environment variable").format(target_cc_name, cc_path_envvar))
102104
return cc
103105

104-
_INC_DIR_MARKER_BEGIN = "#include <...>"
105-
106-
def _cxx_inc_convert(path):
107-
"""Convert path returned by cc -E xc++ in a complete path."""
108-
path = path.strip()
109-
return path
110-
111-
def _get_cxx_inc_directories_impl(repository_ctx, cc, lang_is_cpp):
112-
"""Compute the list of default C or C++ include directories."""
113-
if lang_is_cpp:
114-
lang = "c++"
115-
else:
116-
lang = "c"
117-
118-
# TODO: We pass -no-canonical-prefixes here to match the compiler flags,
119-
# but in rocm_clang CROSSTOOL file that is a `feature` and we should
120-
# handle the case when it's disabled and no flag is passed
121-
result = raw_exec(repository_ctx, [
122-
cc,
123-
"-no-canonical-prefixes",
124-
"-E",
125-
"-x" + lang,
126-
"-",
127-
"-v",
128-
])
129-
stderr = err_out(result)
130-
index1 = stderr.find(_INC_DIR_MARKER_BEGIN)
131-
if index1 == -1:
132-
return []
133-
index1 = stderr.find("\n", index1)
134-
if index1 == -1:
135-
return []
136-
index2 = stderr.rfind("\n ")
137-
if index2 == -1 or index2 < index1:
138-
return []
139-
index2 = stderr.find("\n", index2 + 1)
140-
if index2 == -1:
141-
inc_dirs = stderr[index1 + 1:]
142-
else:
143-
inc_dirs = stderr[index1 + 1:index2].strip()
144-
145-
return [
146-
str(repository_ctx.path(_cxx_inc_convert(p)))
147-
for p in inc_dirs.split("\n")
148-
]
149-
150-
def get_cxx_inc_directories(repository_ctx, cc):
151-
"""Compute the list of default C and C++ include directories."""
152-
153-
# For some reason `clang -xc` sometimes returns include paths that are
154-
# different from the ones from `clang -xc++`. (Symlink and a dir)
155-
# So we run the compiler with both `-xc` and `-xc++` and merge resulting lists
156-
includes_cpp = _get_cxx_inc_directories_impl(repository_ctx, cc, True)
157-
includes_c = _get_cxx_inc_directories_impl(repository_ctx, cc, False)
158-
159-
includes_cpp_set = depset(includes_cpp)
160-
return includes_cpp + [
161-
inc
162-
for inc in includes_c
163-
if inc not in includes_cpp_set.to_list()
164-
]
165-
166106
def auto_configure_fail(msg):
167107
"""Output failure message when rocm configuration fails."""
168108
red = "\033[0;31m"
@@ -178,54 +118,36 @@ def auto_configure_warning(msg):
178118
# END cc_configure common functions (see TODO above).
179119

180120
def _rocm_include_path(repository_ctx, rocm_config, bash_bin):
181-
"""Generates the cxx_builtin_include_directory entries for rocm inc dirs.
121+
"""Generates the entries for rocm inc dirs based on rocm_config.
182122
183123
Args:
184124
repository_ctx: The repository context.
185125
rocm_config: The path to the gcc host compiler.
126+
bash_bin: path to the bash interpreter.
186127
187128
Returns:
188-
A string containing the Starlark string for each of the gcc
189-
host compiler include directories, which can be added to the CROSSTOOL
129+
A string containing the Starlark string for each of the hipcc
130+
compiler include directories, which can be added to the CROSSTOOL
190131
file.
191132
"""
192133
inc_dirs = []
193134

194-
# Add HSA headers (needs to match $HSA_PATH)
195-
inc_dirs.append(rocm_config.rocm_toolkit_path + "/hsa/include")
196-
197-
# Add HIP headers (needs to match $HIP_PATH)
198-
inc_dirs.append(rocm_config.rocm_toolkit_path + "/hip/include")
199-
if int(rocm_config.rocm_version_number) >= 50200:
200-
inc_dirs.append(rocm_config.rocm_toolkit_path + "/include")
201-
inc_dirs.append(rocm_config.rocm_toolkit_path + "/include/hip")
202-
inc_dirs.append(rocm_config.rocm_paths["ROCPRIM"] + "/include/rocprim")
203-
inc_dirs.append(rocm_config.rocm_paths["ROCSOLVER"] + "/include/rocsolver")
204-
inc_dirs.append(rocm_config.rocm_paths["ROCBLAS"] + "/include/rocblas")
205-
206-
# Add HIP-Clang headers (realpath relative to compiler binary)
207-
inc_dirs.append(rocm_config.llvm_path + "/lib/clang/12.0.0/include")
208-
inc_dirs.append(rocm_config.llvm_path + "/lib/clang/13.0.0/include")
209-
inc_dirs.append(rocm_config.llvm_path + "/lib/clang/14.0.0/include")
210-
inc_dirs.append(rocm_config.llvm_path + "/lib/clang/15.0.0/include")
211-
inc_dirs.append(rocm_config.llvm_path + "/lib/clang/16.0.0/include")
212-
inc_dirs.append(rocm_config.llvm_path + "/lib/clang/17.0.0/include/")
213-
inc_dirs.append(rocm_config.llvm_path + "/lib/clang/17/include")
214-
inc_dirs.append(rocm_config.llvm_path + "/lib/clang/18/include")
215-
inc_dirs.append(rocm_config.llvm_path + "/lib/clang/19/include")
216-
rocm_toolkit_path = realpath(repository_ctx, rocm_config.rocm_toolkit_path, bash_bin)
217-
if int(rocm_config.rocm_version_number) >= 60200:
218-
inc_dirs.append(rocm_toolkit_path + "/lib/llvm/lib/clang/17/include")
219-
inc_dirs.append(rocm_toolkit_path + "/lib/llvm/lib/clang/18/include")
220-
inc_dirs.append(rocm_toolkit_path + "/lib/llvm/lib/clang/19/include")
221-
inc_dirs.append(rocm_toolkit_path + "/lib/llvm/lib/clang/20/include")
222-
223-
# Support hcc based off clang 10.0.0 (for ROCm 3.3)
224-
inc_dirs.append(rocm_toolkit_path + "/hcc/compiler/lib/clang/10.0.0/include/")
225-
inc_dirs.append(rocm_toolkit_path + "/hcc/lib/clang/10.0.0/include")
226-
227-
# Add hcc headers
228-
inc_dirs.append(rocm_toolkit_path + "/hcc/include")
135+
# Add HIP-Clang headers (relative to rocm root)
136+
rocm_path = repository_ctx.path(rocm_config.rocm_toolkit_path)
137+
clang_path = rocm_path.get_child("llvm/bin/clang")
138+
resource_dir_result = execute(repository_ctx, [str(clang_path), "-print-resource-dir"])
139+
140+
if resource_dir_result.return_code:
141+
auto_configure_fail("Failed to run hipcc -print-resource-dir: %s" % err_out(resource_dir_result))
142+
143+
resource_dir_abs = resource_dir_result.stdout.strip()
144+
145+
resource_dir_rel = relative_to(repository_ctx, str(rocm_path.realpath), resource_dir_abs, bash_bin)
146+
147+
resource_dir = str(rocm_path.get_child(resource_dir_rel))
148+
149+
inc_dirs.append(resource_dir + "/include")
150+
inc_dirs.append(resource_dir + "/share")
229151

230152
return inc_dirs
231153

third_party/remote_config/common.bzl

+17
Original file line numberDiff line numberDiff line change
@@ -289,6 +289,23 @@ def realpath(repository_ctx, path, bash_bin = None):
289289

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

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

third_party/xla/third_party/tsl/third_party/gpus/compiler_common_tools.bzl

+8-2
Original file line numberDiff line numberDiff line change
@@ -72,8 +72,14 @@ def _get_cxx_inc_directories_impl(repository_ctx, cc, lang_is_cpp, tf_sys_root):
7272
sysroot = []
7373
if tf_sys_root:
7474
sysroot += ["--sysroot", tf_sys_root]
75+
no_canonical_prefixes_supported = _is_compiler_option_supported(
76+
repository_ctx,
77+
cc,
78+
"-no-canonical-prefixes",
79+
)
80+
no_canonical_prefixes = (["-no-canonical-prefixes"] if no_canonical_prefixes_supported else [])
7581
result = raw_exec(repository_ctx, [cc, "-E", "-x" + lang, "-", "-v"] +
76-
sysroot)
82+
sysroot + no_canonical_prefixes)
7783
stderr = err_out(result)
7884
index1 = stderr.find(_INC_DIR_MARKER_BEGIN)
7985
if index1 == -1:
@@ -148,7 +154,7 @@ def _get_cxx_inc_directories_impl(repository_ctx, cc, lang_is_cpp, tf_sys_root):
148154
compiler_includes = compiler_includes + unresolved_compiler_includes
149155
return compiler_includes
150156

151-
def get_cxx_inc_directories(repository_ctx, cc, tf_sys_root):
157+
def get_cxx_inc_directories(repository_ctx, cc, tf_sys_root = None):
152158
"""Compute the list of default C and C++ include directories."""
153159

154160
# For some reason `clang -xc` sometimes returns include paths that are

third_party/xla/third_party/tsl/third_party/gpus/rocm_configure.bzl

+22-100
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,12 @@ load(
2020
"get_python_bin",
2121
"raw_exec",
2222
"realpath",
23+
"relative_to",
2324
"which",
2425
)
2526
load(
2627
":compiler_common_tools.bzl",
28+
"get_cxx_inc_directories",
2729
"to_list_of_strings",
2830
)
2931
load(
@@ -92,68 +94,6 @@ def find_cc(repository_ctx):
9294
" environment variable").format(target_cc_name, cc_path_envvar))
9395
return cc
9496

95-
_INC_DIR_MARKER_BEGIN = "#include <...>"
96-
97-
def _cxx_inc_convert(path):
98-
"""Convert path returned by cc -E xc++ in a complete path."""
99-
path = path.strip()
100-
return path
101-
102-
def _get_cxx_inc_directories_impl(repository_ctx, cc, lang_is_cpp):
103-
"""Compute the list of default C or C++ include directories."""
104-
if lang_is_cpp:
105-
lang = "c++"
106-
else:
107-
lang = "c"
108-
109-
# TODO: We pass -no-canonical-prefixes here to match the compiler flags,
110-
# but in rocm_clang CROSSTOOL file that is a `feature` and we should
111-
# handle the case when it's disabled and no flag is passed
112-
result = raw_exec(repository_ctx, [
113-
cc,
114-
"-no-canonical-prefixes",
115-
"-E",
116-
"-x" + lang,
117-
"-",
118-
"-v",
119-
])
120-
stderr = err_out(result)
121-
index1 = stderr.find(_INC_DIR_MARKER_BEGIN)
122-
if index1 == -1:
123-
return []
124-
index1 = stderr.find("\n", index1)
125-
if index1 == -1:
126-
return []
127-
index2 = stderr.rfind("\n ")
128-
if index2 == -1 or index2 < index1:
129-
return []
130-
index2 = stderr.find("\n", index2 + 1)
131-
if index2 == -1:
132-
inc_dirs = stderr[index1 + 1:]
133-
else:
134-
inc_dirs = stderr[index1 + 1:index2].strip()
135-
136-
return [
137-
str(repository_ctx.path(_cxx_inc_convert(p)))
138-
for p in inc_dirs.split("\n")
139-
]
140-
141-
def get_cxx_inc_directories(repository_ctx, cc):
142-
"""Compute the list of default C and C++ include directories."""
143-
144-
# For some reason `clang -xc` sometimes returns include paths that are
145-
# different from the ones from `clang -xc++`. (Symlink and a dir)
146-
# So we run the compiler with both `-xc` and `-xc++` and merge resulting lists
147-
includes_cpp = _get_cxx_inc_directories_impl(repository_ctx, cc, True)
148-
includes_c = _get_cxx_inc_directories_impl(repository_ctx, cc, False)
149-
150-
includes_cpp_set = depset(includes_cpp)
151-
return includes_cpp + [
152-
inc
153-
for inc in includes_c
154-
if inc not in includes_cpp_set.to_list()
155-
]
156-
15797
def auto_configure_fail(msg):
15898
"""Output failure message when rocm configuration fails."""
15999
red = "\033[0;31m"
@@ -169,54 +109,36 @@ def auto_configure_warning(msg):
169109
# END cc_configure common functions (see TODO above).
170110

171111
def _rocm_include_path(repository_ctx, rocm_config, bash_bin):
172-
"""Generates the cxx_builtin_include_directory entries for rocm inc dirs.
112+
"""Generates the entries for rocm inc dirs based on rocm_config.
173113
174114
Args:
175115
repository_ctx: The repository context.
176116
rocm_config: The path to the gcc host compiler.
117+
bash_bin: path to the bash interpreter.
177118
178119
Returns:
179-
A string containing the Starlark string for each of the gcc
180-
host compiler include directories, which can be added to the CROSSTOOL
120+
A string containing the Starlark string for each of the hipcc
121+
compiler include directories, which can be added to the CROSSTOOL
181122
file.
182123
"""
183124
inc_dirs = []
184125

185-
# Add HSA headers (needs to match $HSA_PATH)
186-
inc_dirs.append(rocm_config.rocm_toolkit_path + "/hsa/include")
187-
188-
# Add HIP headers (needs to match $HIP_PATH)
189-
inc_dirs.append(rocm_config.rocm_toolkit_path + "/hip/include")
190-
if int(rocm_config.rocm_version_number) >= 50200:
191-
inc_dirs.append(rocm_config.rocm_toolkit_path + "/include")
192-
inc_dirs.append(rocm_config.rocm_toolkit_path + "/include/hip")
193-
inc_dirs.append(rocm_config.rocm_toolkit_path + "/include/rocprim")
194-
inc_dirs.append(rocm_config.rocm_toolkit_path + "/include/rocsolver")
195-
inc_dirs.append(rocm_config.rocm_toolkit_path + "/include/rocblas")
196-
197-
# Add HIP-Clang headers (realpath relative to compiler binary)
198-
rocm_toolkit_path = realpath(repository_ctx, rocm_config.rocm_toolkit_path, bash_bin)
199-
inc_dirs.append(rocm_toolkit_path + "/llvm/lib/clang/8.0/include")
200-
inc_dirs.append(rocm_toolkit_path + "/llvm/lib/clang/9.0.0/include")
201-
inc_dirs.append(rocm_toolkit_path + "/llvm/lib/clang/10.0.0/include")
202-
inc_dirs.append(rocm_toolkit_path + "/llvm/lib/clang/11.0.0/include")
203-
inc_dirs.append(rocm_toolkit_path + "/llvm/lib/clang/12.0.0/include")
204-
inc_dirs.append(rocm_toolkit_path + "/llvm/lib/clang/13.0.0/include")
205-
inc_dirs.append(rocm_toolkit_path + "/llvm/lib/clang/14.0.0/include")
206-
inc_dirs.append(rocm_toolkit_path + "/llvm/lib/clang/15.0.0/include")
207-
inc_dirs.append(rocm_toolkit_path + "/llvm/lib/clang/16.0.0/include")
208-
inc_dirs.append(rocm_toolkit_path + "/llvm/lib/clang/17.0.0/include")
209-
inc_dirs.append(rocm_toolkit_path + "/llvm/lib/clang/17/include")
210-
inc_dirs.append(rocm_toolkit_path + "/llvm/lib/clang/18/include")
211-
if int(rocm_config.rocm_version_number) >= 60200:
212-
inc_dirs.append(rocm_toolkit_path + "/lib/llvm/lib/clang/18/include")
213-
214-
# Support hcc based off clang 10.0.0 (for ROCm 3.3)
215-
inc_dirs.append(rocm_toolkit_path + "/hcc/compiler/lib/clang/10.0.0/include/")
216-
inc_dirs.append(rocm_toolkit_path + "/hcc/lib/clang/10.0.0/include")
217-
218-
# Add hcc headers
219-
inc_dirs.append(rocm_toolkit_path + "/hcc/include")
126+
# Add HIP-Clang headers (relative to rocm root)
127+
rocm_path = repository_ctx.path(rocm_config.rocm_toolkit_path)
128+
clang_path = rocm_path.get_child("llvm/bin/clang")
129+
resource_dir_result = execute(repository_ctx, [str(clang_path), "-print-resource-dir"])
130+
131+
if resource_dir_result.return_code:
132+
auto_configure_fail("Failed to run hipcc -print-resource-dir: %s" % err_out(resource_dir_result))
133+
134+
resource_dir_abs = resource_dir_result.stdout.strip()
135+
136+
resource_dir_rel = relative_to(repository_ctx, str(rocm_path.realpath), resource_dir_abs, bash_bin)
137+
138+
resource_dir = str(rocm_path.get_child(resource_dir_rel))
139+
140+
inc_dirs.append(resource_dir + "/include")
141+
inc_dirs.append(resource_dir + "/share")
220142

221143
return inc_dirs
222144

0 commit comments

Comments
 (0)