Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Switch to use compiler param file for msvc #2843

Closed
wants to merge 3 commits into from
Closed
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
7 changes: 1 addition & 6 deletions third_party/gpus/crosstool/cc_toolchain_config.bzl.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -599,6 +599,7 @@ def _features(cpu, compiler, ctx):
]
elif cpu == "x64_windows":
return [
feature(name = "compiler_param_file"),
feature(name = "no_legacy_features"),
feature(
name = "common_flags",
Expand All @@ -623,12 +624,6 @@ def _features(cpu, compiler, ctx):
flag_set(
actions = all_compile_actions(),
flag_groups = [
flag_group(
flags = [
"-B",
"external/local_config_cuda/crosstool/windows/msvc_wrapper_for_nvcc.py",
],
),
_nologo(),
flag_group(
flags = [
Expand Down
36 changes: 35 additions & 1 deletion third_party/gpus/crosstool/windows/msvc_wrapper_for_nvcc.py.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,30 @@ def InvokeNvcc(argv, log=False):
proc.wait()
return proc.returncode

def ExpandParamsFileForArgv():
new_argv = []
for arg in sys.argv:
if arg.startswith("@"):
with open(arg.strip("@")) as f:
new_argv.extend([l.strip() for l in f.readlines()])
else:
new_argv.append(arg)

sys.argv = new_argv

def ProcessFlagForCommandFile(flag):
if flag.startswith("/D") or flag.startswith("-D"):
# We need to re-escape /DFOO="BAR" as /DFOO=\"BAR\", so that we get
# `#define FOO "BAR"` after expansion as a string literal define
if flag.endswith('"') and not flag.endswith('\\"'):
flag = '\\"'.join(flag.split('"', 1))
flag = '\\"'.join(flag.rsplit('"', 1))
return flag
return flag


def main():
ExpandParamsFileForArgv()
parser = ArgumentParser()
parser.add_argument('-x', nargs=1)
parser.add_argument('--cuda_log', action='store_true')
Expand All @@ -213,7 +236,18 @@ def main():
if not flag.startswith(('--cuda_log'))
and not flag.startswith(('-nvcc_options'))]

return subprocess.call([CPU_COMPILER] + cpu_compiler_flags)
output = [flag for flag in cpu_compiler_flags if flag.startswith("/Fo")]

# Store command line options in a file to avoid hitting the character limit.
if len(output) == 1:
commandfile_path = output[0][3:] + ".msvc_params"
commandfile = open(commandfile_path, "w")
cpu_compiler_flags = [ProcessFlagForCommandFile(flag) for flag in cpu_compiler_flags]
commandfile.write("\n".join(cpu_compiler_flags))
commandfile.close()
return subprocess.call([CPU_COMPILER, "@" + commandfile_path])
else:
return subprocess.call([CPU_COMPILER] + cpu_compiler_flags)

if __name__ == '__main__':
sys.exit(main())
7 changes: 6 additions & 1 deletion third_party/gpus/cuda_configure.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ def _get_win_cuda_defines(repository_ctx):
),
)

msvc_cl_path = get_python_bin(repository_ctx)
msvc_cl_path = "windows/msvc_wrapper_for_nvcc.bat"
msvc_ml_path = find_msvc_tool(repository_ctx, vc_path, "ml64.exe").replace(
"\\",
"/",
Expand Down Expand Up @@ -1301,6 +1301,11 @@ def _create_local_cuda_repository(repository_ctx):
tpl_paths["crosstool:clang/bin/crosstool_wrapper_driver_is_not_gcc"],
wrapper_defines,
)
repository_ctx.file(
"crosstool/windows/msvc_wrapper_for_nvcc.bat",
content = "@echo OFF\n{} -B external/local_config_cuda/crosstool/windows/msvc_wrapper_for_nvcc.py %*".format(
get_python_bin(repository_ctx))
)
repository_ctx.template(
"crosstool/windows/msvc_wrapper_for_nvcc.py",
tpl_paths["crosstool:windows/msvc_wrapper_for_nvcc.py"],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -599,6 +599,7 @@ def _features(cpu, compiler, ctx):
]
elif cpu == "x64_windows":
return [
feature(name = "compiler_param_file"),
feature(name = "no_legacy_features"),
feature(
name = "common_flags",
Expand All @@ -623,12 +624,6 @@ def _features(cpu, compiler, ctx):
flag_set(
actions = all_compile_actions(),
flag_groups = [
flag_group(
flags = [
"-B",
"external/local_config_cuda/crosstool/windows/msvc_wrapper_for_nvcc.py",
],
),
_nologo(),
flag_group(
flags = [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,30 @@ def InvokeNvcc(argv, log=False):
proc.wait()
return proc.returncode

def ExpandParamsFileForArgv():
new_argv = []
for arg in sys.argv:
if arg.startswith("@"):
with open(arg.strip("@")) as f:
new_argv.extend([l.strip() for l in f.readlines()])
else:
new_argv.append(arg)

sys.argv = new_argv

def ProcessFlagForCommandFile(flag):
if flag.startswith("/D") or flag.startswith("-D"):
# We need to re-escape /DFOO="BAR" as /DFOO=\"BAR\", so that we get
# `#define FOO "BAR"` after expansion as a string literal define
if flag.endswith('"') and not flag.endswith('\\"'):
flag = '\\"'.join(flag.split('"', 1))
flag = '\\"'.join(flag.rsplit('"', 1))
return flag
return flag


def main():
ExpandParamsFileForArgv()
parser = ArgumentParser()
parser.add_argument('-x', nargs=1)
parser.add_argument('--cuda_log', action='store_true')
Expand All @@ -213,7 +236,18 @@ def main():
if not flag.startswith(('--cuda_log'))
and not flag.startswith(('-nvcc_options'))]

return subprocess.call([CPU_COMPILER] + cpu_compiler_flags)
output = [flag for flag in cpu_compiler_flags if flag.startswith("/Fo")]

# Store command line options in a file to avoid hitting the character limit.
if len(output) == 1:
commandfile_path = output[0][3:] + ".msvc_params"
commandfile = open(commandfile_path, "w")
cpu_compiler_flags = [ProcessFlagForCommandFile(flag) for flag in cpu_compiler_flags]
commandfile.write("\n".join(cpu_compiler_flags))
commandfile.close()
return subprocess.call([CPU_COMPILER, "@" + commandfile_path])
else:
return subprocess.call([CPU_COMPILER] + cpu_compiler_flags)

if __name__ == '__main__':
sys.exit(main())
7 changes: 6 additions & 1 deletion third_party/tsl/third_party/gpus/cuda_configure.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ def _get_win_cuda_defines(repository_ctx):
),
)

msvc_cl_path = get_python_bin(repository_ctx)
msvc_cl_path = "windows/msvc_wrapper_for_nvcc.bat"
msvc_ml_path = find_msvc_tool(repository_ctx, vc_path, "ml64.exe").replace(
"\\",
"/",
Expand Down Expand Up @@ -1301,6 +1301,11 @@ def _create_local_cuda_repository(repository_ctx):
tpl_paths["crosstool:clang/bin/crosstool_wrapper_driver_is_not_gcc"],
wrapper_defines,
)
repository_ctx.file(
"crosstool/windows/msvc_wrapper_for_nvcc.bat",
content = "@echo OFF\n{} -B external/local_config_cuda/crosstool/windows/msvc_wrapper_for_nvcc.py %*".format(
get_python_bin(repository_ctx))
)
repository_ctx.template(
"crosstool/windows/msvc_wrapper_for_nvcc.py",
tpl_paths["crosstool:windows/msvc_wrapper_for_nvcc.py"],
Expand Down