Skip to content

[ROCm][Windows] Restore per-config HIP optimization flags - #201482

Closed
pablo-garay wants to merge 1 commit into
pytorch:mainfrom
ROCm:jeffdaily/windows-hip-per-config-flags
Closed

pablo-garay wants to merge 1 commit into
pytorch:mainfrom
ROCm:jeffdaily/windows-hip-per-config-flags

Conversation

@pablo-garay

@pablo-garay pablo-garay commented Oct 19, 2026 •

Copy link
Copy Markdown
Contributor

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).

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

Fixes pytorch#184397.

pytorch#183856 moved the Windows HIP optimization flags out of CMAKE_HIP_FLAGS_<CONFIG> and into CMAKE_HIP_FLAGS, keyed on CMAKE_BUILD_TYPE, on the premise that per-config flags never reached the HIP compile line under LoadHIP's custom CMAKE_HIP_COMPILE_OBJECT rule. That premise does not hold: the rule's <FLAGS> is filled from CMAKE_HIP_FLAGS plus CMAKE_HIP_FLAGS_<CONFIG> like any other language, and the tree just before pytorch#183856 already put -O3 -DNDEBUG on every HIP compile in build.ninja (CMake 3.31.6 through 4.4.4). The side effect of the workaround is that multi-config generators (Ninja Multi-Config) compile HIP with no -O flag at all, so at clang's -O0.

The -O0 regression in pytorch#183853 came from somewhere else. With C/CXX on clang-cl, CMake treats HIP as an MSVC-frontend language: before CMake 4.3 it seeds no per-config HIP flags, and from 4.3 on it seeds MSVC ones (/O2 /Ob2 /DNDEBUG) that clang++ rejects. pytorch#180485 relied on those defaults, so HIP built at -O0; pytorch#183365 stripped the MSVC flags and appended GNU ones, which had already fixed it. The regression was bisected and measured on builds that predate pytorch#183365.

This drops the CMAKE_BUILD_TYPE injection and, after the existing strip, gives each standard config the GNU default (-O0 -g, -O3 -DNDEBUG, -Os -DNDEBUG, -O2 -g -DNDEBUG) in CMAKE_HIP_FLAGS_<CONFIG> unless it already sets an -O level. That keeps user overrides and is idempotent when LoadHIP.cmake is included a second time (aten/CMakeLists.txt). Visual Studio generators do not support HIP at all, so Ninja, Ninja Multi-Config and Makefiles are the only generators this affects.

Test Plan

Windows 11, TheRock rocm-sdk 7.14.0a20260519, gfx1151, SDK clang-cl as CC/CXX, cmake 4.4.4:

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

build/build.ninja: all 262 HIP compile edges have -O3 -DNDEBUG after the CMAKE_HIP_FLAGS part of FLAGS and none have it injected at the front. A standalone CMake project that uses LoadHIP's setup was also checked on CMake 3.31.6, 4.2.3, 4.3.2 and 4.4.4: Ninja Debug, Release, RelWithDebInfo and MinSizeRel each get their config's flags, Ninja Multi-Config gets per-config flags in every impl-<Config>.ninja (main's code gives none), and -DCMAKE_HIP_FLAGS_RELEASE="-O1 ..." is kept. conv2d fp16 (1x3x1024x1024, k7, 64 out channels) and the native normal_/add/sum/softmax kernels run at parity with the TheRock torch 2.12 wheel on gfx1151 (conv2d 2458-2907 vs 2437-2517 GFLOP/s).

Authored with Claude.
@pytorch-bot

pytorch-bot Bot commented Oct 19, 2026 •

Copy link
Copy Markdown

🔗 Helpful Links

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

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

❌ 1 Cancelled Job, 10 Unrelated Failures

As of commit 612140e with merge base 7f2c7e8 (image):

CANCELLED JOB - The following job was cancelled. Please retry:

UNSTABLE - The following jobs are marked as unstable, possibly due to flakiness on trunk:

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 Oct 19, 2026
@jithunnair-amd jithunnair-amd added the ciflow/trunk Trigger trunk jobs on your pull request label Oct 19, 2026
@jeffdaily
jeffdaily marked this pull request as ready for review October 21, 2026 23:04
@jeffdaily

Copy link
Copy Markdown
Collaborator

@pytorchbot merge

@github-actions github-actions Bot added bot-triaged This is a label only to be used by the auto triage bot triaged This issue has been looked at a team member, and triaged and prioritized into an appropriate module labels Oct 21, 2026
@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

@jeffdaily

Copy link
Copy Markdown
Collaborator

@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: 2 jobs have failed, first few of them are: Auto Request Review, trunk / macos-py3-arm64 / test (mps, 1, 1, macos-m2-15)

Details for Dev Infra team

Raised by workflow job

@jeffdaily

Copy link
Copy Markdown
Collaborator

@pytorchbot merge -i

This branch was previously deployed

1 inactive deployment
bedrock — 612140e6 Deployed Oct 21, 2026 by jeffdaily
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bot-triaged This is a label only to be used by the auto triage bot ciflow/trunk Trigger trunk jobs on your pull request Merged module: rocm AMD GPU support for Pytorch open source topic: not user facing topic category triaged This issue has been looked at a team member, and triaged and prioritized into an appropriate module

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[ROCm][Windows] Track proper fix for HIP per-config flag injection bypass (follow-up to #183856)

5 participants