Skip to content

[BE] Use compiler wrapper when building C++ extensions - #175696

Closed
malfet wants to merge 4 commits into
gh/malfet/730/basefrom
gh/malfet/730/head
Closed

malfet wants to merge 4 commits into
gh/malfet/730/basefrom
gh/malfet/730/head

Conversation

@malfet

@malfet malfet commented Feb 25, 2026 •

Copy link
Copy Markdown
Collaborator

Stack from ghstack (oldest at bottom):

I.e. if either ccache or sccache are present on the system, use it

[ghstack-poisoned]
@malfet
malfet requested review from ezyang and fmassa as code owners February 25, 2026 00:44
@pytorch-bot

pytorch-bot Bot commented Feb 25, 2026 •

Copy link
Copy Markdown

🔗 Helpful Links

🧪 See artifacts and rendered test results at hud.pytorch.org/pr/175696

Note: Links to docs will display an error until the docs builds have been completed.

❗ 1 Active SEVs

There are 1 currently active SEVs. If your PR is affected, please view them below:

❌ 1 New Failure, 2 Unrelated Failures

As of commit 3508ae1 with merge base 00c683a (image):

NEW FAILURE - The following job has failed:

FLAKY - The following jobs failed but were likely due to flakiness present on trunk:

This comment was automatically generated by Dr. CI and updates every 15 minutes.

malfet added a commit that referenced this pull request Feb 25, 2026
I.e. if either ccache or sccache are present on the system, use it


ghstack-source-id: d04b81f
Pull-Request: #175696
@pytorch-bot

pytorch-bot Bot commented Feb 25, 2026

Copy link
Copy Markdown

This PR needs a release notes: label

If your changes are user facing and intended to be a part of release notes, please use a label starting with release notes:.

If not, please add the topic: not user facing label.

To add a label, you can comment to pytorchbot, for example
@pytorchbot label "topic: not user facing"

For more information, see
https://github.com/pytorch/pytorch/wiki/PyTorch-AutoLabel-Bot#why-categorize-for-release-notes-and-how-does-it-work.

Comment thread torch/utils/cpp_extension.py Outdated
if shutil.which(wrapper):
if isinstance(compiler, list):
return [wrapper] + compiler
return f'{wrapper} {compiler}'

@Skylion007 Skylion007 Feb 25, 2026 •

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use shlex.join for added security here to prevent string escapes, unless that is intentional and probably better to just split into a list with shlex.split

Comment thread torch/utils/cpp_extension.py Resolved
@malfet malfet added the ciflow/trunk Trigger trunk jobs on your pull request label Feb 25, 2026
[ghstack-poisoned]
@malfet

malfet commented Feb 26, 2026

Copy link
Copy Markdown
Collaborator Author

@pytorchbot merge

@pytorchmergebot

Copy link
Copy Markdown
Collaborator

Merge failed

Reason: This PR needs a release notes: label
If your changes are user facing and intended to be a part of release notes, please use a label starting with release notes:.

If not, please add the topic: not user facing label.

To add a label, you can comment to pytorchbot, for example
@pytorchbot label "topic: not user facing"

For more information, see
https://github.com/pytorch/pytorch/wiki/PyTorch-AutoLabel-Bot#why-categorize-for-release-notes-and-how-does-it-work.

Details for Dev Infra team Raised by workflow job

@pytorch-bot

pytorch-bot Bot commented Feb 26, 2026

Copy link
Copy Markdown

This PR needs a release notes: label

If your changes are user facing and intended to be a part of release notes, please use a label starting with release notes:.

If not, please add the topic: not user facing label.

To add a label, you can comment to pytorchbot, for example
@pytorchbot label "topic: not user facing"

For more information, see
https://github.com/pytorch/pytorch/wiki/PyTorch-AutoLabel-Bot#why-categorize-for-release-notes-and-how-does-it-work.

@malfet malfet added release notes: python_frontend python frontend release notes category topic: improvements topic category labels Feb 26, 2026
@malfet

malfet commented Feb 26, 2026

Copy link
Copy Markdown
Collaborator Author

@pytorchbot merge

@pytorchmergebot

Copy link
Copy Markdown
Collaborator

Merge started

Your change will be merged once all checks pass (ETA 0-4 Hours).

Learn more about merging in the wiki.

Questions? Feedback? Please reach out to the PyTorch DevX Team

Advanced Debugging
Check the merge workflow status
here

@pytorchmergebot

Copy link
Copy Markdown
Collaborator

Merge failed

Reason: 1 jobs have failed, first few of them are: trunk / macos-py3-arm64 / test (default, 2, 3, macos-m1-stable)

Details for Dev Infra team Raised by workflow job

@malfet

malfet commented Feb 27, 2026

Copy link
Copy Markdown
Collaborator Author

@pytorchbot merge

@pytorchmergebot

Copy link
Copy Markdown
Collaborator

Merge started

Your change will be merged once all checks pass (ETA 0-4 Hours).

Learn more about merging in the wiki.

Questions? Feedback? Please reach out to the PyTorch DevX Team

Advanced Debugging
Check the merge workflow status
here

@atalman

atalman commented Mar 8, 2026 •

Copy link
Copy Markdown
Collaborator

Looks like this PR breaks audio and vision as well with the same error mentioned in: #176585

audio: https://hud.pytorch.org/hud/pytorch/audio/nightly/1?per_page=50
vision: https://hud.pytorch.org/hud/pytorch/vision/nightly/1?per_page=50

Summary from claude:

Root Cause

The commit cf70a7b (2026-02-28 nightly) added _wrap_compiler() to wrap compilers with ccache/sccache. Look at this
critical line for the Windows CUDA path:

  • BEFORE:
  cmd = [nvcc, '-c', src, '-o', obj] + include_list + cflags
  • AFTER:
  cmd = _wrap_compiler([nvcc, '-c', src, '-o', obj] + include_list + cflags)

The _wrap_compiler function has IS_WINDOWS as a bail-out (if ... IS_WINDOWS ...: return compiler), so on Windows
it's a no-op. That part is fine.

But the real issue is the restructured control flow for the non-CUDA (CXX) case on Windows:

  • BEFORE (flat if/elif/elif):
  elif isinstance(self.cflags, dict):
      cflags = COMMON_MSVC_FLAGS + self.cflags['cxx']
      ...
      cmd += cflags
  elif isinstance(self.cflags, list):
      cflags = COMMON_MSVC_FLAGS + self.cflags
      ...
      cmd += cflags
  • AFTER (nested under else):
 else:
     if isinstance(self.cflags, dict):
         ...
         cmd += cflags
     elif isinstance(self.cflags, list):
         ...
         cmd += cflags
     cmd = _wrap_compiler(cmd)    # <-- THIS runs for ALL non-CUDA files

For CUDA files on Windows, the command was changed from:
cmd = [nvcc, '-c', src, '-o', obj] + include_list + cflags
to:
cmd = _wrap_compiler([nvcc, '-c', src, '-o', obj] + include_list + cflags)

The _wrap_compiler passes the entire [nvcc, '-c', src, '-o', obj, ...] list through. On Windows it returns it
unchanged. But then this cmd is passed to original_spawn(cmd).

Now look at the ninja path — at line ~3066:
nvcc = _join_cuda_home('bin', 'nvcc')
nvcc = shlex.join(_wrap_compiler(nvcc)) # nvcc is a STRING, not a list!

_wrap_compiler receives a string, converts it to [string], and returns [string]. Then shlex.join(['C:\Program
Files\NVIDIA GPU Computing Toolkit\CUDA\v12.6\bin\nvcc']) produces the nvcc path. This gets written into the
build.ninja file as:
nvcc = C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v12.6\bin\nvcc

The space in C:\Program Files... is the problem. Without proper quoting in the ninja file, ninja tries to execute
C:\Program as the command, which gives CreateProcess failed: The system cannot find the file specified and ReadFile:
The handle is invalid.

Summary

The _wrap_compiler() change in the 2026-02-28 nightly broke the Windows CUDA build because shlex.join() on Windows
doesn't produce ninja-compatible quoting for paths with spaces (like C:\Program Files...). The previous code wrote
the raw nvcc path directly into the ninja config, which was likely already quoted or handled differently.

Fix

The nvcc path needs to be properly quoted when written to the ninja file on Windows. The simplest fix would be to
not apply shlex.join(_wrap_compiler(...)) on Windows for the nvcc path, or to ensure the path is quoted
appropriately for ninja on Windows.

@atalman

atalman commented Mar 8, 2026

Copy link
Copy Markdown
Collaborator

@pytorchmergebot revert -c nosignal -m "break windows cuda domain builds"

@pytorchmergebot

Copy link
Copy Markdown
Collaborator

@pytorchbot successfully started a revert job. Check the current status here.
Questions? Feedback? Please reach out to the PyTorch DevX Team

pytorchmergebot added a commit that referenced this pull request Mar 8, 2026
@pytorchmergebot

Copy link
Copy Markdown
Collaborator

@malfet your PR has been successfully reverted.

@pytorchmergebot pytorchmergebot added Reverted ci-no-td Do not run TD on this PR labels Mar 8, 2026
@astrelsky

Copy link
Copy Markdown
Contributor

@pytorchmergebot revert -c nosignal -m "break windows cuda domain builds"

#176827

sandy-gags pushed a commit to sandy-gags/pytorch that referenced this pull request Mar 12, 2026
I.e. if either ccache or sccache are present on the system, use it

ghstack-source-id: 1df049a
Pull-Request: pytorch/pytorch#175696
EmanueleCoradin pushed a commit to EmanueleCoradin/pytorch that referenced this pull request Mar 30, 2026
EmanueleCoradin pushed a commit to EmanueleCoradin/pytorch that referenced this pull request Mar 30, 2026
[ghstack-poisoned]
malfet added a commit that referenced this pull request Apr 27, 2026
I.e. if either ccache or sccache are present on the system, use it

ghstack-source-id: 5e21e52
Pull-Request: #175696
[ghstack-poisoned]
malfet added a commit that referenced this pull request Apr 27, 2026
I.e. if either ccache or sccache are present on the system, use it

ghstack-source-id: 9fdc701
Pull-Request: #175696
Comment on lines +238 to +239
def _windows_quote(arg: str) -> str:
"""Quote a single argument per the MSVC ``CommandLineToArgvW`` rules."""

@astrelsky astrelsky Apr 27, 2026 •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You could use subprocess.list2cmdline but it is an undocumented function. Alternatively you could just copy it so you know it is correct. I can't tell from my phone if that is what you've done.

If this is merged, please close #176827.

@malfet malfet Apr 27, 2026 •

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I though about subprocess.list2cmdline but indeed it's not documented what it does and what it does not. This is not a literal copy, but spiritually it works in a similar way

@malfet

malfet commented Apr 27, 2026

Copy link
Copy Markdown
Collaborator Author

@pytorchbot merge -i

@pytorchmergebot

Copy link
Copy Markdown
Collaborator

Merge started

Your change will be merged while ignoring the following 3 checks: pull / linux-jammy-py3.10-gcc11 / test (distributed, 2, 3, linux.2xlarge.amx), trunk / linux-jammy-aarch64-py3.10 / test (default, 2, 5, linux.arm64.m7g.4xlarge), trunk / linux-jammy-cuda13.0-py3.10-gcc11 / test (default, 2, 5, linux.g6.4xlarge.experimental.nvidia.gpu)

Learn more about merging in the wiki.

Questions? Feedback? Please reach out to the PyTorch DevX Team

Advanced Debugging
Check the merge workflow status
here

@github-actions
github-actions Bot deleted the gh/malfet/730/head branch May 28, 2026 02:29
aperson30 pushed a commit to aperson30/pytorch that referenced this pull request Jun 20, 2026
gplutop7 pushed a commit to gplutop7/pytorch that referenced this pull request Jul 15, 2026
gplutop7 pushed a commit to gplutop7/pytorch that referenced this pull request Jul 15, 2026
aws-kingrj pushed a commit to amazon-contributing/upstream-to-pytorch that referenced this pull request Jul 29, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ci-no-td Do not run TD on this PR ciflow/trunk Trigger trunk jobs on your pull request Merged release notes: python_frontend python frontend release notes category Reverted topic: improvements topic category

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants