Skip to content

[ROCm] Use CMake native HIP language support, enable_language(HIP) - #180485

Closed
jeffdaily wants to merge 7 commits into
mainfrom
jeffdaily/enable-language-hip
Closed

jeffdaily wants to merge 7 commits into
mainfrom
jeffdaily/enable-language-hip

Conversation

@jeffdaily

@jeffdaily jeffdaily commented Apr 15, 2026 •

Copy link
Copy Markdown
Collaborator

Replace FindHIP.cmake's hip_add_library/hip_add_executable with CMake's native enable_language(HIP) and standard add_library/add_executable. This enables proper HIP device compilation on Windows where FindHIP's bash-based linker helper doesn't work.

Key changes:

  • LoadHIP.cmake: enable_language(HIP) with Windows workarounds for MSVC/GNU frontend mixing (forced compiler detection, GNU-style compile/link rules, MSVC runtime library mapping)
  • Dependencies.cmake: migrate HIP_HIPCC_FLAGS to CMAKE_HIP_FLAGS, replace hip_include_directories with target_include_directories, use hip::host instead of hip::amdhip64
  • All CMakeLists: replace hip_add_library with add_library, replace HIP_SOURCE_PROPERTY_FORMAT with LANGUAGE HIP, set LINKER_LANGUAGE CXX on shared HIP libraries for proper Windows linking
  • Fix -mllvm flag deduplication by using -mllvm= form
  • Add HAVE_AVX{2,512}_CPU_DEFINITION to CMAKE_HIP_FLAGS for DispatchStub ABI compatibility between torch_cpu and torch_hip
  • Fix unconditional ck_group_gemm reference when USE_ROCM_CK_GEMM=OFF

Authored with Claude Code.

Co-Authored-By: Claude Opus 4.6 (1M context)

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

Replace FindHIP.cmake's hip_add_library/hip_add_executable with CMake's
native enable_language(HIP) and standard add_library/add_executable.
This enables proper HIP device compilation on Windows where FindHIP's
bash-based linker helper doesn't work.

Key changes:
- LoadHIP.cmake: enable_language(HIP) with Windows workarounds for
  MSVC/GNU frontend mixing (forced compiler detection, GNU-style
  compile/link rules, MSVC runtime library mapping)
- Dependencies.cmake: migrate HIP_HIPCC_FLAGS to CMAKE_HIP_FLAGS,
  replace hip_include_directories with target_include_directories,
  use hip::host instead of hip::amdhip64
- All CMakeLists: replace hip_add_library with add_library, replace
  HIP_SOURCE_PROPERTY_FORMAT with LANGUAGE HIP, set LINKER_LANGUAGE CXX
  on shared HIP libraries for proper Windows linking
- Fix -mllvm flag deduplication by using -mllvm=<arg> form
- Add HAVE_AVX{2,512}_CPU_DEFINITION to CMAKE_HIP_FLAGS for
  DispatchStub ABI compatibility between torch_cpu and torch_hip
- Fix unconditional ck_group_gemm reference when USE_ROCM_CK_GEMM=OFF
- Third-party (gloo, fbgemm): dual-path support for native/legacy HIP

Authored with Claude Code.

Co-Authored-By: Claude Opus 4.6 (1M context)
@pytorch-bot

pytorch-bot Bot commented Apr 15, 2026 •

Copy link
Copy Markdown

🔗 Helpful Links

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

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

❌ 1 New Failure, 1 Unrelated Failure

As of commit 059210c with merge base b127675 (image):

NEW FAILURE - The following job has failed:

FLAKY - The following job failed but was likely due to flakiness present on trunk:

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

@pytorch-bot pytorch-bot Bot added ciflow/b200 ciflow/h100 ciflow/rocm-mi300 Trigger "default" config CI on ROCm MI300 module: rocm AMD GPU support for Pytorch release notes: distributed (c10d) release notes category labels Apr 15, 2026
@jeffdaily

Copy link
Copy Markdown
Collaborator Author

Hey @GZGavinZhao, I was inspired by your #172775 and got enable_language(HIP) working on TheRock Windows build. I'd appreciate your review here. Thanks.

message(STATUS "HIP language enabled with compiler: ${CMAKE_HIP_COMPILER}")
message(STATUS "HIP architectures: ${CMAKE_HIP_ARCHITECTURES}")

if(WIN32)

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.

I don't have a Windows machine to test, but can you try CMake v4.3.0 and above and see if this section is still necessary? v4.3.0 and above contains my upstreamed fix. Be sure you're using clang-cl.exe for this.

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.

Thanks for the pointer. Two reasons to keep this block as-is for now:

  1. PyTorch's cmake_minimum_required(VERSION 3.27) (top of the root CMakeLists.txt) sets the supported floor well below the 4.3.0 that contains your upstream fix. Even if the workaround becomes a no-op on 4.3.0+, it's still required for any user on a 3.27 ≤ CMake < 4.3.0 toolchain — and gating the override behind if(CMAKE_VERSION VERSION_LESS 4.3.0) adds a conditional path I'd rather not maintain until the floor moves up.
  2. On clang-cl.exe: I haven't taken that path. The PR is currently set up with clang++.exe (the GNU-frontend driver), which is what got the TheRock Windows build working end-to-end. Trying clang-cl.exe would be a separate exercise — and given (1), it wouldn't remove the workaround from the supported version range anyway.

Happy to revisit once the cmake floor moves, or to take a follow-up PR that tries clang-cl.exe if there's a concrete reason to prefer it.

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.

Thanks @GZGavinZhao. Tested on Windows with CMake 4.3.2 (which contains your fix). Two scenarios:

  1. clang-cl.exe for both C/CXX and HIP, no Windows workarounds: Configure succeeds, the generated HIP compile rule uses MSVC-style syntax that clang-cl accepts, and Sleep.hip compiles cleanly. Two minor warnings about -std=c++20 and -fclang-abi-compat=17 being ignored by clang-cl — those flags in Dependencies.cmake's CMAKE_HIP_FLAGS would need MSVC-driver equivalents (/std:c++20 and -Xclang -fclang-abi-compat=17).

  2. clang-cl.exe for C/CXX + clang++.exe for HIP (current PR), no Windows workarounds: enable_language(HIP) fails because the HIP compiler ABI test compiles with C/CXX-inherited flags (/DWIN32 /Ob0 /Od /RTC1 --cuda-host-only) which clang++ (GNU frontend) rejects. Even after skipping ABI detection (CMAKE_HIP_COMPILER_FORCED=TRUE), the generated compile rule still uses MSVC-style syntax (/nologo /Fo /Fd -clang:-MD -imsvc) which clang++ rejects.

So the if(WIN32) blocks are necessary if we keep clang++.exe as the HIP compiler. They become unnecessary if we switch to clang-cl.exe for HIP, as your fix supports. Switching has the appeal of dropping ~50 lines of platform-specific overrides; the tradeoff is reworking the Dependencies.cmake CMAKE_HIP_FLAGS from GNU-style to MSVC-driver-style. Happy to do that as a follow-up if preferred — let me know.

@GZGavinZhao GZGavinZhao left a comment •

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.

Looks fine from a quick scan. Building this is a bit time-consuming so I won't be able to test this properly until this weekend. Just left a comment about the possibility of dropping if (WIN32) section in LoadHIP.cmake.

I'd also suggest explicitly testing the build with both fbgemm and gloo enabled. Legacy HIP builds use find_package(HIP) which is known to have a conflict with enable_language(HIP) (see discussion here). I believe gloo has migrated away from that in pytorch/gloo#478 but fbgemm still has this line. If this conflict is not fixed yet, theoretically we would have a problem when building fbgemm_gpu together with PyTorch.

@jerryzh168 jerryzh168 added the triaged This issue has been looked at a team member, and triaged and prioritized into an appropriate module label Apr 17, 2026
When using CMake native HIP language support, marking .cpp files as
LANGUAGE HIP causes clang to emit a per-TU __hip_cuid_<hash> symbol.
sccache invokes the compiler with preprocessed input, which leaves
clang's driver with no unique path to hash — every .cpp.o ends up with
the same __hip_cuid_ symbol and torch_hip fails to link with
"multiple definition" errors. Local builds without sccache weren't
affected because each TU got a unique CUID from its real source path.

Filter Caffe2_HIP_SRCS so only .cu/.hip files get LANGUAGE HIP (matching
the legacy hip_add_library behavior); .cpp files compile as CXX and
never emit __hip_cuid_. Also drop the redundant LANGUAGE HIP on
GENERATED_CXX_TORCH_CUDA (c_shim_cuda.cpp).

While here, clean up c10/hip/CMakeLists.txt: the old block marking
.cc files as HIP was dead code from 2018 (c10/cuda has no .cu files,
HIPIFY renames .cu->.hip anyway) and referenced the long-removed hcc
compiler. c10/hip is host-only, so all sources compile as CXX.

Authored-by: Claude Opus 4.7 (1M context)
@drisspg

drisspg commented Apr 22, 2026

Copy link
Copy Markdown
Contributor

@claude Please do a deep review focused on subtle correctness bugs and regressions that are easy for human reviewers to miss. Prioritize findings over summary. I dont care about anything that isnt a real bug drop all the noise but that, if you there isnt anything to report just say so with a small comment

@claude

claude Bot commented Apr 22, 2026 •

Copy link
Copy Markdown

Claude finished @drisspg's task in 6m 9s —— View job


I did a thorough review of all 10 changed files looking specifically for subtle correctness bugs and regressions. I didn't find any real bugs.

The key behavioral changes are all intentional and correct:

  • .cpp files in Caffe2_HIP_SRCS now compile as CXX instead of HIP (avoiding __hip_cuid_ collisions) — this is the stated goal and is safe since those files contain no device code
  • hip::host replaces hip::amdhip64 for c10_hip and Caffe2_PUBLIC_HIP_DEPENDENCY_LIBS — hip::host transitively links the runtime, so this is equivalent
  • torch_rocshmem's -fgpu-rdc override via target_compile_options correctly wins over the global -fno-gpu-rdc in CMAKE_HIP_FLAGS due to CMake flag ordering (global flags come first, target flags appended after, and clang uses last-one-wins)
  • The HAVE_AVX{2,512}_CPU_DEFINITION propagation to CMAKE_HIP_FLAGS for DispatchStub ABI compatibility is a genuine fix
  • The -mllvm=<arg> flag deduplication change is a no-op semantically (clang accepts both forms)
  • The GroupedBlas.cpp #if defined(USE_ROCM_CK_GEMM) refactoring is logically equivalent to the old bool use_fast_path pattern

Nothing to flag.


# Conflicts:
#	c10/hip/CMakeLists.txt
#	caffe2/CMakeLists.txt
@jeffdaily

Copy link
Copy Markdown
Collaborator Author

@drisspg would you be able to finish your review?

@drisspg
drisspg requested a review from malfet May 5, 2026 00:14
@drisspg

drisspg commented May 5, 2026

Copy link
Copy Markdown
Contributor

Did we resolve the fbgemm_gpu issue?

@jeffdaily

Copy link
Copy Markdown
Collaborator Author

@drisspg Did some experiments to back up the answer.

Short answer: PyTorch's main CMake never invokes fbgemm_gpu/cmake/Hip.cmake — only the CPU fbgemm library is added via add_subdirectory("${FBGEMM_SOURCE_DIR}") in cmake/Dependencies.cmake, and the fbgemm root CMakeLists.txt does not include fbgemm_gpu/. So the find_package(HIP) + enable_language(HIP) conflict in fbgemm_gpu's cmake (which @GZGavinZhao flagged) is internal to fbgemm_gpu's own build and cannot affect this PR. fbgemm_gpu is built standalone by FBGEMM's own CI.

The dep that is invoked from PyTorch's tree on the ROCm path is gloo, via third_party/gloo/cmake/Dependencies.cmake → cmake/Hip.cmake. The current submodule pin (3135b0b) still uses legacy find_package(HIP) + hip_add_library, which is why CI is green on Linux today. Upstream pytorch/gloo#478 ("ROCm: Migrate to native CMake HIP support", merged 2026-02) modernizes this.

Experiments run locally on this PR branch (merged with current main):

  1. gloo bump: checked out third_party/gloo at bcd1672 (ROCm: Migrate to native CMake HIP support gloo#478) and ran MAX_JOBS= PYTORCH_ROCM_ARCH=gfx90a .ci/pytorch/build.sh. PyTorch wheel built and installed cleanly. gloo_hip was compiled via the native HIP language path (Building HIP object .../gloo_hip.dir/...) and libgloo_hip.a linked successfully. torch.distributed.is_gloo_available() returns True.
  2. fbgemm_gpu against experimental torch: built fbgemm_gpu standalone (BUILD_ROCM_VERSION=7.2 PYTORCH_ROCM_ARCH=gfx90a python setup.py bdist_wheel --build-variant rocm --build-target default) against the torch wheel from Matrix multiplication operator #1. All 516 ninja targets compiled, wheel installed, import fbgemm_gpu succeeds, and torch.ops.fbgemm.permute_2D_sparse_data(...) dispatches and returns the expected tensors.

The PR does not bump the gloo submodule pin — keeping the current scope minimal. Happy to add a gloo bump in a follow-up PR if reviewers prefer the cleaner story.

jeffdaily added a commit to alugorey/pytorch that referenced this pull request May 7, 2026
PR pytorch#180485 ([ROCm] Use CMake native HIP language support) replaced
FindHIP's hip_add_library and global HIP_CLANG_FLAGS arch filtering
with native enable_language(HIP) and per-target HIP_ARCHITECTURES.

Re-express the ck_sdpa gfx9 denylist (gfx900, gfx906, gfx90c) using
the new HIP_ARCHITECTURES target property, mirroring the mslk allowlist
pattern that pytorch#180485 already established in the same file. Same
behavior, just expressed in the new build system.

Authored with Claude Code.
Alokksinha00 pushed a commit to Alokksinha00/pytorch that referenced this pull request May 15, 2026
On ROCm builds with USE_ROCM_CK_GEMM=OFF, GroupedBlas.cpp still included ck_group_gemm.h and emitted a source-level reference to at::hip::detail::group_gemm_ck(...) inside an always-false `if (use_fast_path)` branch. The Linux/legacy-CMake build path tolerated this (dead-code-elimination hid the unresolved symbol), but the Windows build under enable_language(HIP) in pytorch#180485 surfaces it as a linker failure.

Gate the include and the call site uniformly on USE_ROCM_CK_GEMM and replace the bool indirection with explicit `#if defined(USE_ROCM_CK_GEMM)` / `#else`, so the fallback path is the only thing the compiler sees when CK is disabled.

Split out from pytorch#180485 per @malfet review.

Authored with Claude Code.

Pull Request resolved: pytorch#182615
Approved by: https://github.com/malfet
Alokksinha00 pushed a commit to Alokksinha00/pytorch that referenced this pull request May 15, 2026
…ytorch#180485)

Replace FindHIP.cmake's hip_add_library/hip_add_executable with CMake's native enable_language(HIP) and standard add_library/add_executable. This enables proper HIP device compilation on Windows where FindHIP's bash-based linker helper doesn't work.

Key changes:
- LoadHIP.cmake: enable_language(HIP) with Windows workarounds for MSVC/GNU frontend mixing (forced compiler detection, GNU-style compile/link rules, MSVC runtime library mapping)
- Dependencies.cmake: migrate HIP_HIPCC_FLAGS to CMAKE_HIP_FLAGS, replace hip_include_directories with target_include_directories, use hip::host instead of hip::amdhip64
- All CMakeLists: replace hip_add_library with add_library, replace HIP_SOURCE_PROPERTY_FORMAT with LANGUAGE HIP, set LINKER_LANGUAGE CXX on shared HIP libraries for proper Windows linking
- Fix -mllvm flag deduplication by using -mllvm=<arg> form
- Add HAVE_AVX{2,512}_CPU_DEFINITION to CMAKE_HIP_FLAGS for DispatchStub ABI compatibility between torch_cpu and torch_hip
- Fix unconditional ck_group_gemm reference when USE_ROCM_CK_GEMM=OFF

Authored with Claude Code.

Co-Authored-By: Claude Opus 4.6 (1M context)

Pull Request resolved: pytorch#180485
Approved by: https://github.com/drisspg, https://github.com/malfet
adityas-amd added a commit to adityas-amd/pytorch that referenced this pull request May 18, 2026
PR pytorch#180485 migrated the Windows HIP build from hip_add_library/hipcc
to CMake's native HIP language with clang++ direct. The custom
CMAKE_HIP_COMPILE_OBJECT rule installed in cmake/public/LoadHIP.cmake
expands <FLAGS>, but the per-config CMAKE_HIP_FLAGS_RELEASE values do
not reliably reach <FLAGS> for the HIP language, so HIP code silently
fell back to clang++'s -O0 default.

Every kernel in torch_hip.dll ended up unoptimized: 100% scratch
spillage in the ISA, and the AMD runtime emits scratch ranges on
every dispatch (palvirtual.hpp: scratch = ptr:[...] size:[90177536]).
Result: ~99% conv2d FP16 perf regression on gfx1150 (2715 -> 16
GFLOP/s, bisected and validated at ROCm/TheRock#5157).

Fix: inject -O3 directly into the compile rule template so it is
guaranteed regardless of how <FLAGS> expands.

Validation on gfx1150 / Radeon 890M (validate_torch_vroom.py):
  5/5 nightly (pre-pytorch#180485): 2715 GFLOP/s
  5/9 nightly (regressed):     16 GFLOP/s
  5/9 + this patch:          2515 GFLOP/s

The compile-rule location was identified and validated by @astrelsky
on ROCm/TheRock#5157.

Authored with assistance from Claude.
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]>
@github-actions
github-actions Bot deleted the jeffdaily/enable-language-hip branch June 7, 2026 02:30
aperson30 pushed a commit to aperson30/pytorch that referenced this pull request Jun 20, 2026
On ROCm builds with USE_ROCM_CK_GEMM=OFF, GroupedBlas.cpp still included ck_group_gemm.h and emitted a source-level reference to at::hip::detail::group_gemm_ck(...) inside an always-false `if (use_fast_path)` branch. The Linux/legacy-CMake build path tolerated this (dead-code-elimination hid the unresolved symbol), but the Windows build under enable_language(HIP) in pytorch#180485 surfaces it as a linker failure.

Gate the include and the call site uniformly on USE_ROCM_CK_GEMM and replace the bool indirection with explicit `#if defined(USE_ROCM_CK_GEMM)` / `#else`, so the fallback path is the only thing the compiler sees when CK is disabled.

Split out from pytorch#180485 per @malfet review.

Authored with Claude Code.

Pull Request resolved: pytorch#182615
Approved by: https://github.com/malfet
aperson30 pushed a commit to aperson30/pytorch that referenced this pull request Jun 20, 2026
…ytorch#180485)

Replace FindHIP.cmake's hip_add_library/hip_add_executable with CMake's native enable_language(HIP) and standard add_library/add_executable. This enables proper HIP device compilation on Windows where FindHIP's bash-based linker helper doesn't work.

Key changes:
- LoadHIP.cmake: enable_language(HIP) with Windows workarounds for MSVC/GNU frontend mixing (forced compiler detection, GNU-style compile/link rules, MSVC runtime library mapping)
- Dependencies.cmake: migrate HIP_HIPCC_FLAGS to CMAKE_HIP_FLAGS, replace hip_include_directories with target_include_directories, use hip::host instead of hip::amdhip64
- All CMakeLists: replace hip_add_library with add_library, replace HIP_SOURCE_PROPERTY_FORMAT with LANGUAGE HIP, set LINKER_LANGUAGE CXX on shared HIP libraries for proper Windows linking
- Fix -mllvm flag deduplication by using -mllvm=<arg> form
- Add HAVE_AVX{2,512}_CPU_DEFINITION to CMAKE_HIP_FLAGS for DispatchStub ABI compatibility between torch_cpu and torch_hip
- Fix unconditional ck_group_gemm reference when USE_ROCM_CK_GEMM=OFF

Authored with Claude Code.

Co-Authored-By: Claude Opus 4.6 (1M context)

Pull Request resolved: pytorch#180485
Approved by: https://github.com/drisspg, https://github.com/malfet
gplutop7 pushed a commit to gplutop7/pytorch that referenced this pull request Jul 15, 2026
On ROCm builds with USE_ROCM_CK_GEMM=OFF, GroupedBlas.cpp still included ck_group_gemm.h and emitted a source-level reference to at::hip::detail::group_gemm_ck(...) inside an always-false `if (use_fast_path)` branch. The Linux/legacy-CMake build path tolerated this (dead-code-elimination hid the unresolved symbol), but the Windows build under enable_language(HIP) in pytorch#180485 surfaces it as a linker failure.

Gate the include and the call site uniformly on USE_ROCM_CK_GEMM and replace the bool indirection with explicit `#if defined(USE_ROCM_CK_GEMM)` / `#else`, so the fallback path is the only thing the compiler sees when CK is disabled.

Split out from pytorch#180485 per @malfet review.

Authored with Claude Code.

Pull Request resolved: pytorch#182615
Approved by: https://github.com/malfet
gplutop7 pushed a commit to gplutop7/pytorch that referenced this pull request Jul 15, 2026
…ytorch#180485)

Replace FindHIP.cmake's hip_add_library/hip_add_executable with CMake's native enable_language(HIP) and standard add_library/add_executable. This enables proper HIP device compilation on Windows where FindHIP's bash-based linker helper doesn't work.

Key changes:
- LoadHIP.cmake: enable_language(HIP) with Windows workarounds for MSVC/GNU frontend mixing (forced compiler detection, GNU-style compile/link rules, MSVC runtime library mapping)
- Dependencies.cmake: migrate HIP_HIPCC_FLAGS to CMAKE_HIP_FLAGS, replace hip_include_directories with target_include_directories, use hip::host instead of hip::amdhip64
- All CMakeLists: replace hip_add_library with add_library, replace HIP_SOURCE_PROPERTY_FORMAT with LANGUAGE HIP, set LINKER_LANGUAGE CXX on shared HIP libraries for proper Windows linking
- Fix -mllvm flag deduplication by using -mllvm=<arg> form
- Add HAVE_AVX{2,512}_CPU_DEFINITION to CMAKE_HIP_FLAGS for DispatchStub ABI compatibility between torch_cpu and torch_hip
- Fix unconditional ck_group_gemm reference when USE_ROCM_CK_GEMM=OFF

Authored with Claude Code.

Co-Authored-By: Claude Opus 4.6 (1M context)

Pull Request resolved: pytorch#180485
Approved by: https://github.com/drisspg, https://github.com/malfet
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
On ROCm builds with USE_ROCM_CK_GEMM=OFF, GroupedBlas.cpp still included ck_group_gemm.h and emitted a source-level reference to at::hip::detail::group_gemm_ck(...) inside an always-false `if (use_fast_path)` branch. The Linux/legacy-CMake build path tolerated this (dead-code-elimination hid the unresolved symbol), but the Windows build under enable_language(HIP) in pytorch#180485 surfaces it as a linker failure.

Gate the include and the call site uniformly on USE_ROCM_CK_GEMM and replace the bool indirection with explicit `#if defined(USE_ROCM_CK_GEMM)` / `#else`, so the fallback path is the only thing the compiler sees when CK is disabled.

Split out from pytorch#180485 per @malfet review.

Authored with Claude Code.

Pull Request resolved: pytorch#182615
Approved by: https://github.com/malfet
aws-kingrj pushed a commit to amazon-contributing/upstream-to-pytorch that referenced this pull request Jul 29, 2026
…ytorch#180485)

Replace FindHIP.cmake's hip_add_library/hip_add_executable with CMake's native enable_language(HIP) and standard add_library/add_executable. This enables proper HIP device compilation on Windows where FindHIP's bash-based linker helper doesn't work.

Key changes:
- LoadHIP.cmake: enable_language(HIP) with Windows workarounds for MSVC/GNU frontend mixing (forced compiler detection, GNU-style compile/link rules, MSVC runtime library mapping)
- Dependencies.cmake: migrate HIP_HIPCC_FLAGS to CMAKE_HIP_FLAGS, replace hip_include_directories with target_include_directories, use hip::host instead of hip::amdhip64
- All CMakeLists: replace hip_add_library with add_library, replace HIP_SOURCE_PROPERTY_FORMAT with LANGUAGE HIP, set LINKER_LANGUAGE CXX on shared HIP libraries for proper Windows linking
- Fix -mllvm flag deduplication by using -mllvm=<arg> form
- Add HAVE_AVX{2,512}_CPU_DEFINITION to CMAKE_HIP_FLAGS for DispatchStub ABI compatibility between torch_cpu and torch_hip
- Fix unconditional ck_group_gemm reference when USE_ROCM_CK_GEMM=OFF

Authored with Claude Code.

Co-Authored-By: Claude Opus 4.6 (1M context)

Pull Request resolved: pytorch#180485
Approved by: https://github.com/drisspg, https://github.com/malfet
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]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ciflow/b200 ciflow/h100 ciflow/rocm-mi300 Trigger "default" config CI on ROCm MI300 ciflow/trunk Trigger trunk jobs on your pull request Merged module: rocm AMD GPU support for Pytorch open source release notes: distributed (c10d) release notes 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.

7 participants