Skip to content

Commit

Permalink
[ROCm] Pass AMDGPU_TARGETS to crosstool wrapper
Browse files Browse the repository at this point in the history
Passing amdgpu targets to crosstool wrapper which calls hipcc can
restrict the kernels generated to specific set of supported amdgpu
architectures.
  • Loading branch information
hsharsha authored and Ruturaj4 committed Dec 2, 2024
1 parent c65e770 commit 96fd686
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ HIP_RUNTIME_LIBRARY = '%{hip_runtime_library}'
ROCR_RUNTIME_PATH = '%{rocr_runtime_path}'
ROCR_RUNTIME_LIBRARY = '%{rocr_runtime_library}'
VERBOSE = '%{crosstool_verbose}'=='1'
ROCM_AMDGPU_TARGETS = '%{rocm_amdgpu_targets}'

def Log(s):
print('gpus/crosstool: {0}'.format(s))
Expand Down Expand Up @@ -99,6 +100,29 @@ def GetHostCompilerOptions(argv):

return opts

def GetHipccOptions(argv):
"""Collect the -hipcc_options values from argv.
Args:
argv: A list of strings, possibly the argv passed to main().
Returns:
The string that can be passed directly to hipcc.
"""

parser = ArgumentParser()
parser.add_argument('--offload-arch', nargs='*', action='append')
# TODO find a better place for this
parser.add_argument('-gline-tables-only', action='store_true')

args, _ = parser.parse_known_args(argv)

hipcc_opts = ' -gline-tables-only ' if args.gline_tables_only else ''
if args.offload_arch:
hipcc_opts = hipcc_opts + ' '.join(['--offload-arch=' + a for a in sum(args.offload_arch, [])])

return hipcc_opts

def system(cmd):
"""Invokes cmd with os.system().
Expand All @@ -115,7 +139,6 @@ def system(cmd):
else:
return -os.WTERMSIG(retv)


def InvokeHipcc(argv, log=False):
"""Call hipcc with arguments assembled from argv.
Expand All @@ -128,6 +151,7 @@ def InvokeHipcc(argv, log=False):
"""

host_compiler_options = GetHostCompilerOptions(argv)
hipcc_compiler_options = GetHipccOptions(argv)
opt_option = GetOptionValue(argv, 'O')
m_options = GetOptionValue(argv, 'm')
m_options = ''.join([' -m' + m for m in m_options if m in ['32', '64']])
Expand Down Expand Up @@ -166,7 +190,7 @@ def InvokeHipcc(argv, log=False):
srcs = ' '.join(src_files)
out = ' -o ' + out_file[0]

hipccopts = ' '
hipccopts = hipcc_compiler_options + ' '
# In hip-clang environment, we need to make sure that hip header is included
# before some standard math header like <complex> is included in any source.
# Otherwise, we get build error.
Expand Down Expand Up @@ -222,6 +246,7 @@ def main():

if VERBOSE: print('PWD=' + os.getcwd())
if VERBOSE: print('HIPCC_ENV=' + HIPCC_ENV)
if VERBOSE: print('ROCM_AMDGPU_TARGETS= ' + ROCM_AMDGPU_TARGETS)

if args.x and args.x[0] == 'rocm':
# compilation for GPU objects
Expand Down
3 changes: 3 additions & 0 deletions third_party/tsl/third_party/gpus/rocm_configure.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -798,6 +798,9 @@ def _create_local_rocm_repository(repository_ctx):
"%{hip_runtime_library}": "amdhip64",
"%{crosstool_verbose}": _crosstool_verbose(repository_ctx),
"%{gcc_host_compiler_path}": str(cc),
"%{rocm_amdgpu_targets}": ",".join(
["\"%s\"" % c for c in rocm_config.amdgpu_targets],
),
},
)

Expand Down

0 comments on commit 96fd686

Please sign in to comment.