Skip to content

[ROCm][Windows] Remove MSVC flags from CMAKE_HIP_FLAGS - #183365

Closed
astrelsky wants to merge 1 commit into
pytorch:mainfrom
astrelsky:therock_5175
Closed

astrelsky wants to merge 1 commit into
pytorch:mainfrom
astrelsky:therock_5175

Conversation

@astrelsky

@astrelsky astrelsky commented May 11, 2026 •

Copy link
Copy Markdown
Contributor

This removes the MSVC flags from CMAKE_HIP_FLAGS{,_<CONFIG> after enable_language(HIP) as discussed here ROCm/TheRock#5175 (comment)

Fixes ROCm/TheRock#5175

cc @jeffdaily @sunway513 @jithunnair-amd @pruthvistony @ROCmSupport @jataylo @hongxiayang @naromero77amd @pragupta @jerrymannil @xinyazhang

@pytorch-bot

pytorch-bot Bot commented May 11, 2026 •

Copy link
Copy Markdown

🔗 Helpful Links

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

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

⏳ No Failures, 66 Pending

As of commit 1b073e8 with merge base 380fc8b (image):
💚 Looks good so far! There are no failures yet. 💚

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

@pytorch-bot pytorch-bot Bot added the module: rocm AMD GPU support for Pytorch label May 11, 2026
@astrelsky

Copy link
Copy Markdown
Contributor Author

@pytorchbot label "topic: not user facing"

@jeffdaily

Copy link
Copy Markdown
Collaborator

@pytorchbot merge -f "rocm-only cmake change inside if(WIN32) block, no pytorch/pytorch CI here can test it"

@pytorchmergebot

Copy link
Copy Markdown
Collaborator

Merge started

Your change will be merged immediately since you used the force (-f) flag, bypassing any CI checks (ETA: 1-5 minutes). Please use -f as last resort and instead consider -i/--ignore-current to continue the merge ignoring current failures. This will allow currently pending tests to finish and report signal before the merge.

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

@astrelsky
astrelsky deleted the therock_5175 branch May 11, 2026 22:06
pytorchmergebot pushed a commit to ayushsatyam146/pytorch that referenced this pull request May 12, 2026
This removes the MSVC flags from `CMAKE_HIP_FLAGS{,_<CONFIG>` after `enable_language(HIP)` as discussed here ROCm/TheRock#5175 (comment)

Fixes ROCm/TheRock#5175

Pull Request resolved: pytorch#183365
Approved by: https://github.com/jeffdaily
Alokksinha00 pushed a commit to Alokksinha00/pytorch that referenced this pull request May 15, 2026
This removes the MSVC flags from `CMAKE_HIP_FLAGS{,_<CONFIG>` after `enable_language(HIP)` as discussed here ROCm/TheRock#5175 (comment)

Fixes ROCm/TheRock#5175

Pull Request resolved: pytorch#183365
Approved by: https://github.com/jeffdaily
jeffdaily added a commit to adityas-amd/pytorch that referenced this pull request May 18, 2026
…_FLAGS

PR pytorch#180485 migrated the Windows HIP build to native enable_language(HIP). PR pytorch#183365 stripped MSVC-style defaults (/DWIN32 /D_WINDOWS /O2 /Ob2 /DNDEBUG) out of CMAKE_HIP_FLAGS{,_<CONFIG>} so clang++ would stop warn-ignoring them, and tried to reinstate GNU equivalents by appending to CMAKE_HIP_FLAGS_RELEASE, CMAKE_HIP_FLAGS_DEBUG, etc.

The reinstatement step does not work on this code path. The generated HIP compile rule expands <FLAGS> from CMAKE_HIP_FLAGS, but the per-config CMAKE_HIP_FLAGS_<CONFIG> variables do not reach <FLAGS> (verified by inspecting build.ninja: CMAKE_HIP_FLAGS contents like --offload-compress and --offload-arch appear, but the -O3 -DNDEBUG appended to CMAKE_HIP_FLAGS_RELEASE does not). That is the actual cause of the "silent -O0 fallback" reported in TheRock#5157 / pytorch#183853: with the MSVC /O2 stripped and the GNU -O3 unreachable, HIP code was effectively compiled at -O0.

The previous version of this PR hardcoded -O3 in the CMAKE_HIP_COMPILE_OBJECT rule template. That restores Release perf but the -O3 sits outside the per-config plumbing, so it also leaks into Debug, RelWithDebInfo, and MinSizeRel builds.

Inject the per-config optimization flags into CMAKE_HIP_FLAGS instead, gated on CMAKE_BUILD_TYPE. CMAKE_HIP_FLAGS does reach <FLAGS> in the generated rule, so this lands -O3/-O2/-O0/-Os (matching the build type) on the compile line for single-config generators (Ninja, Makefiles) without contaminating other configs. Drop the now-dead string(APPEND CMAKE_HIP_FLAGS_<CONFIG>) lines and the -O3 hack in the compile rule.

Validation on gfx1151 / Radeon AI MAX 395 with CMake 4.3.2: Release shows -O3 -DNDEBUG in build.ninja FLAGS and Sleep.hip compiles cleanly with optimized ISA; Debug shows -O0 -g.

Co-investigated with @astrelsky and @adityas-amd on TheRock#5157.

Authored with assistance from Claude.
pytorchmergebot pushed a commit that referenced this pull request May 18, 2026
…_FLAGS (#183856)

## Issue

Fixes ROCm/TheRock#5157.
Fixes #183853.

Windows ROCm HIP code was effectively compiled at `-O0` after #180485 + #183365 landed, causing a ~99% conv2d FP16 perf regression on gfx1150 (bisected and validated by @astrelsky and @adityas-amd at ROCm/TheRock#5157).

## Diagnosis

After #183365 stripped the MSVC-style `/O2 /Ob2 /DNDEBUG` out of `CMAKE_HIP_FLAGS_RELEASE` and re-appended GNU `-O3 -DNDEBUG`, the actual build command still came up unoptimized. Inspecting the generated `build.ninja` shows why:

- `CMAKE_HIP_FLAGS` contents (e.g. `--offload-compress -std=c++20 --offload-arch=...`) **do** reach the `$FLAGS` ninja variable used by every HIP compile.
- `CMAKE_HIP_FLAGS_<CONFIG>` contents (e.g. `-O3 -DNDEBUG`) **do not**.

That's the actual cause of the `-O0` fallback — `CMAKE_HIP_FLAGS_RELEASE` is dead code on the Windows HIP compile path.

## Fix

- Drop the dead `string(APPEND CMAKE_HIP_FLAGS_<CONFIG> ...)` lines.
- Inject the appropriate `-O{3,2,0,s}` plus `-DNDEBUG`/`-g` directly into `CMAKE_HIP_FLAGS`, gated on `CMAKE_BUILD_TYPE`. This works for single-config generators (Ninja, Makefiles), which is what the Windows ROCm build uses.
- Drop the `-O3` from the compile rule template so non-Release configs aren't tainted.

Pull Request resolved: #183856
Approved by: https://github.com/jeffdaily, https://github.com/astrelsky

Co-authored-by: Jeff Daily <[email protected]>
gplutop7 pushed a commit to gplutop7/pytorch that referenced this pull request Jul 15, 2026
This removes the MSVC flags from `CMAKE_HIP_FLAGS{,_<CONFIG>` after `enable_language(HIP)` as discussed here ROCm/TheRock#5175 (comment)

Fixes ROCm/TheRock#5175

Pull Request resolved: pytorch#183365
Approved by: https://github.com/jeffdaily
gplutop7 pushed a commit to gplutop7/pytorch that referenced this pull request Jul 15, 2026
…_FLAGS (pytorch#183856)

## Issue

Fixes ROCm/TheRock#5157.
Fixes pytorch#183853.

Windows ROCm HIP code was effectively compiled at `-O0` after pytorch#180485 + pytorch#183365 landed, causing a ~99% conv2d FP16 perf regression on gfx1150 (bisected and validated by @astrelsky and @adityas-amd at ROCm/TheRock#5157).

## Diagnosis

After pytorch#183365 stripped the MSVC-style `/O2 /Ob2 /DNDEBUG` out of `CMAKE_HIP_FLAGS_RELEASE` and re-appended GNU `-O3 -DNDEBUG`, the actual build command still came up unoptimized. Inspecting the generated `build.ninja` shows why:

- `CMAKE_HIP_FLAGS` contents (e.g. `--offload-compress -std=c++20 --offload-arch=...`) **do** reach the `$FLAGS` ninja variable used by every HIP compile.
- `CMAKE_HIP_FLAGS_<CONFIG>` contents (e.g. `-O3 -DNDEBUG`) **do not**.

That's the actual cause of the `-O0` fallback — `CMAKE_HIP_FLAGS_RELEASE` is dead code on the Windows HIP compile path.

## Fix

- Drop the dead `string(APPEND CMAKE_HIP_FLAGS_<CONFIG> ...)` lines.
- Inject the appropriate `-O{3,2,0,s}` plus `-DNDEBUG`/`-g` directly into `CMAKE_HIP_FLAGS`, gated on `CMAKE_BUILD_TYPE`. This works for single-config generators (Ninja, Makefiles), which is what the Windows ROCm build uses.
- Drop the `-O3` from the compile rule template so non-Release configs aren't tainted.

Pull Request resolved: pytorch#183856
Approved by: https://github.com/jeffdaily, https://github.com/astrelsky

Co-authored-by: Jeff Daily <[email protected]>
aws-kingrj pushed a commit to amazon-contributing/upstream-to-pytorch that referenced this pull request Jul 29, 2026
This removes the MSVC flags from `CMAKE_HIP_FLAGS{,_<CONFIG>` after `enable_language(HIP)` as discussed here ROCm/TheRock#5175 (comment)

Fixes ROCm/TheRock#5175

Pull Request resolved: pytorch#183365
Approved by: https://github.com/jeffdaily
aws-kingrj pushed a commit to amazon-contributing/upstream-to-pytorch that referenced this pull request Jul 29, 2026
…_FLAGS (pytorch#183856)

## Issue

Fixes ROCm/TheRock#5157.
Fixes pytorch#183853.

Windows ROCm HIP code was effectively compiled at `-O0` after pytorch#180485 + pytorch#183365 landed, causing a ~99% conv2d FP16 perf regression on gfx1150 (bisected and validated by @astrelsky and @adityas-amd at ROCm/TheRock#5157).

## Diagnosis

After pytorch#183365 stripped the MSVC-style `/O2 /Ob2 /DNDEBUG` out of `CMAKE_HIP_FLAGS_RELEASE` and re-appended GNU `-O3 -DNDEBUG`, the actual build command still came up unoptimized. Inspecting the generated `build.ninja` shows why:

- `CMAKE_HIP_FLAGS` contents (e.g. `--offload-compress -std=c++20 --offload-arch=...`) **do** reach the `$FLAGS` ninja variable used by every HIP compile.
- `CMAKE_HIP_FLAGS_<CONFIG>` contents (e.g. `-O3 -DNDEBUG`) **do not**.

That's the actual cause of the `-O0` fallback — `CMAKE_HIP_FLAGS_RELEASE` is dead code on the Windows HIP compile path.

## Fix

- Drop the dead `string(APPEND CMAKE_HIP_FLAGS_<CONFIG> ...)` lines.
- Inject the appropriate `-O{3,2,0,s}` plus `-DNDEBUG`/`-g` directly into `CMAKE_HIP_FLAGS`, gated on `CMAKE_BUILD_TYPE`. This works for single-config generators (Ninja, Makefiles), which is what the Windows ROCm build uses.
- Drop the `-O3` from the compile rule template so non-Release configs aren't tainted.

Pull Request resolved: pytorch#183856
Approved by: https://github.com/jeffdaily, https://github.com/astrelsky

Co-authored-by: Jeff Daily <[email protected]>
pytorchmergebot pushed a commit that referenced this pull request Oct 22, 2026
## Linked issue or supporting maintainer

Fixes #184397
Supported by @jeffdaily

Related: #183856 (the workaround this replaces), #183853 and ROCm/TheRock#5157 (the original -O0 regression).

## BC-breaking?

No.

## Summary (human written only)

Replaces the CMAKE_BUILD_TYPE-gated HIP flag injection on Windows with per-config defaults, so multi-config generators get optimized HIP code too.

## Agent details (if applicable)

Problem: #183856 injects the HIP optimization flags into `CMAKE_HIP_FLAGS` based on `CMAKE_BUILD_TYPE`, because per-config flags were believed not to reach the HIP compile line under LoadHIP's custom `CMAKE_HIP_COMPILE_OBJECT`. As a result, Ninja Multi-Config compiles HIP with no `-O` flag at all, in every configuration.

Cause: that belief was wrong. The rule's `<FLAGS>` includes `CMAKE_HIP_FLAGS_<CONFIG>` as for any language; the tree just before #183856 already had `-O3 -DNDEBUG` on every HIP compile in build.ninja (CMake 3.31.6 through 4.4.4). The -O0 regression came from CMake treating HIP as an MSVC-frontend language when C/CXX use clang-cl: no per-config HIP defaults before CMake 4.3, MSVC ones that clang++ rejects from 4.3 on. #183365 had already fixed that; the regression was measured only on builds predating it.

Change: drop the `CMAKE_BUILD_TYPE` injection and, after the existing strip, set each standard config's GNU default (`-O0 -g`, `-O3 -DNDEBUG`, `-Os -DNDEBUG`, `-O2 -g -DNDEBUG`) in `CMAKE_HIP_FLAGS_<CONFIG>` unless it already has an `-O` level. This keeps user overrides and is idempotent for the second `include(LoadHIP)` in `aten/CMakeLists.txt`.

Test plan, done with Claude on Windows 11, TheRock rocm-sdk 7.14, gfx1151, SDK clang-cl, cmake 4.4.4:

```
python tools/amd_build/build_amd.py
python -m pip install -e . -v --no-build-isolation
```

All 262 HIP compile edges in build/build.ninja get `-O3 -DNDEBUG` in the per-config position. A standalone project with LoadHIP's setup on CMake 3.31.6, 4.2.3, 4.3.2 and 4.4.4: Ninja Debug/Release/RelWithDebInfo/MinSizeRel each get their config's flags, Ninja Multi-Config gets per-config flags (main gives none), and `-DCMAKE_HIP_FLAGS_RELEASE=-O1 ...` is kept. conv2d fp16 (1x3x1024x1024, k7, 64 out channels) runs at parity with the TheRock 2.12 wheel on gfx1151 (2458-2907 vs 2437-2517 GFLOP/s).

Pull Request resolved: #201482
Approved by: https://github.com/jeffdaily

Co-authored-by: Jeff Daily <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Merged module: rocm AMD GPU support for Pytorch open source topic: not user facing topic category

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Issue] Windows PyTorch nightly wheel build fails when HIP objects are compiled with clang++ using MSVC-style flags

4 participants