Skip to content

[ROCm] Repair the HIP build for current ROCm and document it - #2

Open
jeffdaily wants to merge 7 commits into
burtscher:mainfrom
AMD-Ecosystem:moat-port
Open

jeffdaily wants to merge 7 commits into
burtscher:mainfrom
AMD-Ecosystem:moat-port

Conversation

@jeffdaily

Copy link
Copy Markdown

Summary

LC-framework already ships a HIP path for AMD GPUs (the AMD shim block in include/macros.h, the __HIPCC__ guards around the cuda/std headers, and the hand-written wave64 ballot/popcll variants), but that path keys on the __AMDGCN_WAVEFRONT_SIZE macro, which current ROCm (7.x) no longer predefines. As a result the AMD shim block never compiles (__trap, __syncwarp, atomic*_block, and the cuda::std::numeric_limits alias all end up undeclared) and the generated WS wave-width selector silently falls back to 32 on wave64 hardware, so the HIP build fails on a current toolchain and would mis-select the wave width even if it built. This PR repairs that path, fixes a few latent include gaps in the same area, and documents the build for AMD GPUs in the README for both the main lc tool and the standalone compressor/decompressor.

To build for an AMD GPU, follow the new README sections: run the generator as usual, convert the generated files and the framework headers from CUDA to HIP with hipify-perl, then compile with hipcc --offload-arch=<arch> -ffp-contract=off (the hipcc counterpart of the documented nvcc -fmad=false recipe). No new files, flags, or build options are introduced; the existing generate-then-compile workflow is kept, matching the *.prehip entry already in .gitignore.

What is implemented

  • include/macros.h: the AMD shim block is gated on __HIP_PLATFORM_AMD__ instead of the no-longer-predefined __AMDGCN_WAVEFRONT_SIZE. It now includes <limits> so the cuda::std::numeric_limits alias resolves, and the __syncwarp no-op shim is only defined when the toolchain does not provide __syncwarp natively (recent ROCm does, via amd_warp_sync_functions.h, so the unconditional shim would collide).
  • Wave-width selection: the WS selector in compressor-framework.cu/.cpp, decompressor-framework.cu/.cpp, and the include/consts.h emitters of generate_Device_LC-Framework.py and generate_Hybrid_LC-Framework.py now reads #if defined(__GFX8__) || defined(__GFX9__) || (defined(__AMDGCN_WAVEFRONT_SIZE) && (__AMDGCN_WAVEFRONT_SIZE == 64)). The __GFX*__ macros are per-architecture compiler predefines of the device compilation pass, so a fat binary built for both a wave64 and a wave32 architecture selects the correct width in each code object, and the retained __AMDGCN_WAVEFRONT_SIZE term keeps older ROCm toolchains working. The "AMD GPU version" banner in framework.cu is re-gated the same way.
  • preprocessors/d_QUANT_INOA_0_f64.h: adds the thrust includes its f32 sibling already has. On CUDA the file compiled only because other headers pull thrust in transitively; on HIP those headers are __HIPCC__-guarded off and the file fails to compile without the explicit includes.
  • compressor-framework.cu and decompressor-framework.cu (the standalone templates): add <cstring> for strcmp, matching the .cpp twins, so the generated standalone GPU compressor and decompressor compile.
  • framework.h: the <strings.h>, <unistd.h>, and <sys/time.h> includes are guarded with #ifndef _MSC_VER so the HIP build works on Windows, where hipcc uses clang-cl as the host compiler and those POSIX headers do not exist. The first two have no users anywhere in the tree, and <sys/time.h> is only used by the USE_CPU timer path, which is unaffected. Also casts warpSize to int in one debug printf, since HIP's warpSize is not a plain int and device printf rejects it as a vararg; on CUDA, where warpSize is already int, the cast is a no-op.
  • README.md: new AMD GPU build instructions next to the existing nvcc ones, for the main build and the standalone build, including the in-place-rewrite behavior of hipify-perl and the Windows notes.

CUDA path

The CUDA/NVIDIA build is untouched in behavior: the changes are AMD-only preprocessor branches, additive includes (which the CUDA build also benefits from, as the missing thrust includes were a latent omission there), and the no-op (int)warpSize cast. As a check, lc.cu, compressor-standalone.cu, and decompressor-standalone.cu generated from this branch compile with nvcc 12.8 (-arch=sm_80) with 0 errors.

Validation

All testing below was done at the exact tip commit of this branch, each run rebuilding from a clean copy of the tree with the documented README commands verbatim. Test inputs were locally generated: a 1 MB random file, a smaller structured file (runs of zeros/repeats/random), and 65536-element f32/f64/i32 arrays for the lossy pipelines.

  • AMD Instinct MI300X (gfx942, wave64), Linux, ROCm 7.14.60850 (AMD clang 23.0): lossless AL round-trips 12/12 passed (RZE_4, RZE_1, RLE_4, BIT_4 RLE_4, RRE_4 RZE_4, RAZE_4 on both inputs); TS self-test over all component pairs 4491 passed, 0 failed; lossy pipelines 4/4 passed (QUANT_ABS_0_f32, QUANT_INOA_0_f64 exercising rocThrust, QUANT_NOA_0_f32 exercising the cuda::std::numeric_limits alias, LOR1D_i32 exercising hipCUB DeviceScan); standalone GPU compressor/decompressor round-trip byte-identical.
  • AMD Instinct MI250X (gfx90a, wave64), Linux, HIP 7.14.60850 (AMD clang 23.0): lossless round-trips 6/6 passed; TS self-test 4491 passed, 0 failed; lossy pipelines 4/4 passed.
  • AMD Radeon Pro W7800 (gfx1100, wave32), Linux, ROCm 7.2.3 (HIP 7.2.53211): lossless round-trips 6/6 passed; TS self-test 4491 passed, 0 failed; lossy pipelines 4/4 passed.
  • AMD Radeon 8060S (gfx1151, wave32), Windows 11, ROCm SDK 7.14.0a20260612 (clang-cl host): lossless round-trips 6/6 passed; TS self-test 4491 passed, 0 failed; lossy QUANT_ABS_0_f32 and QUANT_INOA_0_f64 pipelines passed; standalone GPU round-trip byte-identical.

Because the wave-width selector is the load-bearing change, the serialized format was checked across devices on wave64 and wave32 hardware: on gfx942, gfx90a, and gfx1100, the standalone GPU compressor's output is byte-identical to the CPU compressor's output for the same input, GPU-compressed files decompress correctly with the CPU decompressor and vice versa, and on the Windows gfx1151 machine the GPU-compressed file decompresses byte-identically with the CPU decompressor. So the wave width does not leak into the compressed format, preserving the cross-device compatibility the project advertises. The CPU/OpenMP build (generate_Host_LC-Framework.py + g++) was also rebuilt and its round-trip re-run on each Linux machine and with MinGW g++ on Windows, all passing, confirming the CPU path is unaffected.

This work was authored with the assistance of Claude, an AI assistant.


This pull request was prepared with the help of an AI assistant acting as a coding agent and was read and approved by a person before it was opened. It comes from an ongoing effort to add AMD GPU support to widely used CUDA projects, one repository at a time: https://github.com/AMD-Ecosystem/moat -- that repository describes how the work is done and what a person checks before anything is submitted.

If you would rather not receive pull requests from this effort, say so here or open an issue at https://github.com/AMD-Ecosystem/moat/issues/new/choose and we will close this and stop. That can cover this repository alone or everything you own, whichever you prefer.

LC already ships an author-maintained HIP path, but it gated everything
on __AMDGCN_WAVEFRONT_SIZE, a predefined macro that ROCm 7.x no longer
provides. With the macro gone the AMD shim block in include/macros.h
never compiled (so __trap, atomic*_block, and the cuda::std alias were
undeclared) and the WS wave-width define silently fell back to 32 on
wave64 hardware, which would corrupt the warp-collective zero- and
repetition-elimination paths. This restores the HIP build on a current
ROCm without changing the CUDA build.

Review include/macros.h first: the shim block is re-gated on
__HIP_PLATFORM_AMD__ (defined in both host and device passes), <limits>
is pulled in so the cuda::std::numeric_limits alias resolves, and the
__syncwarp shim is now conditional because recent ROCm provides
__syncwarp natively. Then the wave-width selector in the four framework
sources and the two Python generators that emit include/consts.h: wave64
is selected on GCN/CDNA via the device-pass __GFX8__/__GFX9__ macros and
wave32 elsewhere, which keeps the serialized bitstream format identical
across wave widths (verified by decoding gfx90a output on the CPU
reference). framework.h casts warpSize to int for device printf, and
d_QUANT_INOA_0_f64.h gains the thrust includes its f32 sibling already
has (a latent omission that compiled on CUDA only by transitive luck).

The CUDA path is unchanged: none of __HIP_PLATFORM_AMD__/__GFX8__/
__GFX9__ are defined for nvcc, so WS stays 32 and the shim block stays
disabled exactly as before.

This work was authored with the assistance of Claude, an AI assistant.

Test Plan:
Generated, hipified, and built the framework for gfx90a on ROCm 7.2.1:

```
./generate_Device_LC-Framework.py
hipify-perl -inplace lc.cu lc.h
for h in framework.h $(find include components preprocessors verifiers -name '*.h'); do hipify-perl -inplace "$h"; done
hipcc -O3 --offload-arch=gfx90a -ffp-contract=off -DUSE_GPU -I. -std=c++17 -o lc lc.cu
```

Confirmed the device pass selects WS=64 on gfx90a (device asm stores 64).
Ran the built-in lossless round-trip (AL mode) across the warp-collective
pipelines (RZE_4, RZE_1, RLE_4, BIT_4 RLE_4, RRE_4 RZE_4): all pass
bit-for-bit. Ran the TS self-test over all component pairs with no
failures. Lossy quantizers verified within bound (QUANT_ABS, QUANT_NOA,
QUANT_INOA_0_f64 with MAXABS/MAXNOA verifiers), exercising the thrust and
cuda::std paths. Cross-device format gate: data compressed on gfx90a
(wave64) decompressed correctly on the CPU reference build (wave32),
byte-for-byte identical to the original.
strings.h, unistd.h and sys/time.h are POSIX headers that do not exist
under the MSVC-compatible toolchain (clang-cl or cl.exe), which is the
required host compiler for a HIP translation unit on Windows. None of
the three headers is actually used when building with USE_GPU alone:
strings.h and unistd.h have no callers anywhere in this file, and the
sys/time.h user (CPUTimer, for gettimeofday/timeval) only compiles
under USE_CPU, a path this change does not touch. Guarding the three
includes on _MSC_VER (defined by both cl.exe and clang-cl, but not by
MinGW g++, so the existing CPU/OpenMP build is unaffected) lets the
generated lc.cu/lc.h compile with hipcc on Windows without changing
behavior anywhere else.

This change was made with the assistance of an AI coding agent and
verified by building and running on real AMD GPU hardware.

Test Plan:
Generated the GPU source, hipified it, and built with hipcc for
gfx1151 on Windows:

    python generate_Device_LC-Framework.py
    hipify-perl -inplace lc.cu lc.h
    for h in framework.h $(find include components preprocessors verifiers -name '*.h'); do
      hipify-perl -inplace "$h"
    done
    hipcc -O3 --offload-arch=gfx1151 -ffp-contract=off -DUSE_GPU -I. -std=c++17 -c -o lc.o lc.cu
    hipcc --offload-arch=gfx1151 -o lc lc.o

Ran the built binary on a real AMD Radeon 8060S (gfx1151):

    ./lc test.dat AL "" "RZE_4"
    ./lc small.dat TS

Both report "verification passed" with no failures. The CPU/OpenMP
build (generate_Host_LC-Framework.py + g++) was rebuilt unchanged and
continues to pass its own round-trip test.
The tutorial explains how to compile the generated GPU code with nvcc but
says nothing about hipcc, even though a HIP version of the code is included,
so anyone building for an AMD GPU has to reconstruct the steps.

Add the ROCm counterparts next to the existing nvcc commands, both for the
framework binary in the installation section and for the standalone
compressor and decompressor. The recipe converts the generated code and the
headers it includes with hipify-perl before compiling, since hipcc compiles
HIP rather than CUDA sources. The -ffp-contract=off flag plays the same
role as the -fmad=false in the nvcc commands and keeps the lossy quantizers
bit-for-bit consistent with the CPU version, while the lossless components
are integer-only and unaffected. The GPU architecture is left as a
placeholder rather than a fixed value, and a short note covers the two
things the Windows toolchain needs. No code is changed.

This change was prepared with the assistance of an AI coding agent.

Test Plan:

Build the framework binary by following the documented commands, on a
gfx1151 GPU with ROCm 7.x, and run the built-in verification:

```
./generate_Device_LC-Framework.py
hipify-perl -inplace lc.cu lc.h
for h in framework.h $(find include components preprocessors verifiers -name '*.h'); do hipify-perl -inplace "$h"; done
hipcc -O3 --offload-arch=gfx1151 -ffp-contract=off -DUSE_GPU -I. -std=c++17 -c -o lc.o lc.cu
hipcc --offload-arch=gfx1151 -o lc lc.o
./lc input.dat AL "" "RZE_4"
./lc input.dat TS
```

Build the standalone compressor and decompressor the same way:

```
./generate_standalone_GPU_compressor_decompressor.py "" "RZE_4"
hipify-perl -inplace compressor-standalone.cu decompressor-standalone.cu
for h in framework.h $(find include components preprocessors verifiers -name '*.h'); do hipify-perl -inplace "$h"; done
hipcc -O3 --offload-arch=gfx1151 -ffp-contract=off -I. -std=c++17 -c -o compress.o compressor-standalone.cu
hipcc --offload-arch=gfx1151 -o compress compress.o
hipcc -O3 --offload-arch=gfx1151 -ffp-contract=off -I. -std=c++17 -c -o decompress.o decompressor-standalone.cu
hipcc --offload-arch=gfx1151 -o decompress decompress.o
```

Both builds complete with cosmetic warnings only, the AL run reports
"LOSSLESS verification passed", and the TS self-test reports no failures.
The framework build was also run, with the architecture changed, on gfx90a
and gfx1100 with ROCm 7.2.1 on Linux.
compressor-framework.cu and decompressor-framework.cu, which the standalone
code generator copies verbatim into compressor-standalone.cu and
decompressor-standalone.cu, call strcmp but include only <string>. On the
CUDA path <cuda/atomic> and <cuda/std/limits> pull string.h in, so nvcc
never notices; those two includes are guarded off for HIP, so the generated
standalone compressor and decompressor fail to compile with hipcc under
libstdc++:

  compressor-standalone.cu:339:30: error: use of undeclared identifier 'strcmp'

Microsoft's <string> also pulls <cstring> in, which is why the same recipe
builds on Windows. Include <cstring> explicitly instead of relying on either
transitive path. The include is inert for the CUDA build and for the CPU
build, whose .cpp templates already have it.

This change was prepared with the assistance of an AI coding agent.

Test Plan:

Generate and build the standalone compressor and decompressor for ROCm,
following the documented commands, on a gfx942 GPU with ROCm 7.14 and
libstdc++, then check that they round-trip a file:

```
./generate_standalone_GPU_compressor_decompressor.py "" "RZE_4"
hipify-perl -inplace compressor-standalone.cu decompressor-standalone.cu
for h in framework.h $(find include components preprocessors verifiers -name '*.h'); do hipify-perl -inplace "$h"; done
hipcc -O3 --offload-arch=gfx942 -ffp-contract=off -I. -std=c++17 -c -o compress.o compressor-standalone.cu
hipcc --offload-arch=gfx942 -o compress compress.o
hipcc -O3 --offload-arch=gfx942 -ffp-contract=off -I. -std=c++17 -c -o decompress.o decompressor-standalone.cu
hipcc --offload-arch=gfx942 -o decompress decompress.o
./compress input.dat LC.encoded
./decompress LC.encoded LC.decoded
cmp input.dat LC.decoded
```

Both builds now complete with cosmetic warnings only, where before this
change both failed on strcmp, and cmp reports no difference.

Check that the CUDA build is unaffected, with CUDA 12.8:

```
./generate_standalone_GPU_compressor_decompressor.py "" "RZE_4"
nvcc -O3 -arch=sm_80 -fmad=false -I. -std=c++17 -c -o compress.o compressor-standalone.cu
nvcc -O3 -arch=sm_80 -fmad=false -I. -std=c++17 -c -o decompress.o decompressor-standalone.cu
```

Both compile without errors.
The wave width selection tests __HIP_PLATFORM_AMD__ before testing __GFX8__
and __GFX9__, but __HIP_PLATFORM_AMD__ is not a compiler predefine: it comes
from hip/hip_runtime.h. The test sits above every include in the standalone
templates and above the runtime header in the framework path, so it only
gets the right answer because hipify-perl happens to insert that header at
the top of the file, and it quietly answers wave32 for anyone who compiles
the converted code without that insertion. Nothing else in the expression
needs it: __GFX8__ and __GFX9__ are themselves predefined by the AMD
compiler in the device pass and are never defined by nvcc or by a host
compiler, so testing them alone is both shorter and self-contained.

Restore __AMDGCN_WAVEFRONT_SIZE as an alternative in the same expression
while removing the dependency on the runtime header. It is the more accurate
answer on the two toolchains where it exists: one old enough to predefine it
but not the __GFX*__ macros, and any compiler that reflects an explicit
-mwavefrontsize64 in it, which the __GFX*__ macros cannot express. Where
both are available they agree.

Also drop the paragraph in the README that explained the old ordering
requirement, since it no longer holds, and say instead what a reader
following the recipe does need to know: hipify-perl rewrites the checked-out
sources in place and leaves .prehip backups of them.

This change was prepared with the assistance of an AI coding agent.

Test Plan:

Check what the compiler actually predefines, with ROCm 7.14:

```
echo | hipcc -x hip --offload-arch=gfx942 -dM -E - | grep -E 'GFX|WAVEFRONT|HIP_PLATFORM'
```

This prints __GFX9__ and no __HIP_PLATFORM_AMD__ and no
__AMDGCN_WAVEFRONT_SIZE, which is the situation the change addresses.

Check the width the new expression selects on each architecture, without
including any header first, by generating include/consts.h and compiling a
file that asserts the expected width in the device pass:

```
./generate_Device_LC-Framework.py
cat > wscheck.hip <<'END'
#include "include/consts.h"
#include <hip/hip_runtime.h>
__global__ void k() {
#ifdef __HIP_DEVICE_COMPILE__
  static_assert(WS == EXPECT, "wrong wave width in the device pass");
#endif
}
END
for a in gfx803 gfx908 gfx90a gfx942 gfx950; do hipcc -x hip --offload-arch=$a -DEXPECT=64 -I. -std=c++17 -c -o /dev/null wscheck.hip; done
for a in gfx1100 gfx1151 gfx1201; do hipcc -x hip --offload-arch=$a -DEXPECT=32 -I. -std=c++17 -c -o /dev/null wscheck.hip; done
```

Every architecture compiles, so wave64 is selected on GCN and CDNA and
wave32 on RDNA, with no header included ahead of the selection.

Build the framework binary on a gfx942 GPU and run the built-in
verification, which also traps if the selected width disagrees with the
hardware:

```
./generate_Device_LC-Framework.py
hipify-perl -inplace lc.cu lc.h
for h in framework.h $(find include components preprocessors verifiers -name '*.h'); do hipify-perl -inplace "$h"; done
hipcc -O3 --offload-arch=gfx942 -ffp-contract=off -DUSE_GPU -I. -std=c++17 -c -o lc.o lc.cu
hipcc --offload-arch=gfx942 -o lc lc.o
./lc input.dat AL "" "RZE_4"
./lc input.dat AL "" "RRE_4 RZE_4"
./lc input.dat TS
```

The AL runs report "LOSSLESS verification passed" and the TS self-test
reports 4491 verifications and no failures.

Check that the compressed format is unchanged, by compressing on the GPU at
wave64 and decompressing with the CPU version, and the other way around:

```
./generate_standalone_CPU_compressor_decompressor.py "" "RZE_4"
g++ -O3 -march=native -fopenmp -mno-fma -ffp-contract=off -I. -std=c++17 -o compress_cpu compressor-standalone.cpp
g++ -O3 -march=native -fopenmp -mno-fma -ffp-contract=off -I. -std=c++17 -o decompress_cpu decompressor-standalone.cpp
./compress input.dat GPU.encoded && ./decompress_cpu GPU.encoded CPU.decoded && cmp input.dat CPU.decoded
./compress_cpu input.dat CPU.encoded && ./decompress CPU.encoded GPU.decoded && cmp input.dat GPU.decoded
cmp GPU.encoded CPU.encoded
```

Both directions decode correctly and the two encodings are byte-identical.

Check that the CUDA build is unaffected, with CUDA 12.8:

```
./generate_Device_LC-Framework.py
nvcc -O3 -arch=sm_80 -fmad=false -DUSE_GPU -I. -std=c++17 -c -o lc.o lc.cu
```

This compiles without errors, and none of the macros in the expression are
defined by nvcc, so the CUDA build selects wave32 exactly as before.
The fallback definition of __syncwarp is emitted only when
HIP_DISABLE_WARP_SYNC_BUILTINS is defined. That matches the guard ROCm uses
around its own definition in hip/amd_detail/amd_warp_sync_functions.h, so it
is right for any release that ships that header, but a release from before
it existed now gets neither definition and fails to compile, because the two
calls to __syncwarp in framework.h and compressor-framework.cu are not
optional.

Emit the fallback when the header is absent as well. __has_include answers
that directly and needs no version arithmetic.

This change was prepared with the assistance of an AI coding agent.

Test Plan:

Build the framework binary on a gfx942 GPU with ROCm 7.14, which provides
__syncwarp, so the fallback is not emitted:

```
./generate_Device_LC-Framework.py
hipify-perl -inplace lc.cu lc.h
for h in framework.h $(find include components preprocessors verifiers -name '*.h'); do hipify-perl -inplace "$h"; done
hipcc -O3 --offload-arch=gfx942 -ffp-contract=off -DUSE_GPU -I. -std=c++17 -c -o lc.o lc.cu
hipcc --offload-arch=gfx942 -o lc lc.o
./lc input.dat AL "" "RZE_4"
```

The build completes with cosmetic warnings only and the run reports
"LOSSLESS verification passed".

Build the other side of the condition, where the fallback is emitted and
ROCm's own definition is suppressed:

```
hipcc -O3 --offload-arch=gfx942 -ffp-contract=off -DUSE_GPU -DHIP_DISABLE_WARP_SYNC_BUILTINS -I. -std=c++17 -c -o lc.o lc.cu
```

This compiles without errors.
The note on the conversion suggested grepping the tree for CUDA spellings
before compiling, but that check reports failure after a conversion that
succeeded: it also finds the .prehip backups the conversion itself writes,
the framework templates that the documented loop deliberately leaves alone
and the build never compiles, and the README line proposing the check. It
also attributed unconverted files to hipify-perl skipping them, which is not
what happens; a conversion loop over the whole tree simply takes a while,
and one that is interrupted leaves the rest of the headers untouched.

Say that instead, and describe what an unconverted header looks like at
compile time, which is unambiguous and needs no separate step.

This change was prepared with the assistance of an AI coding agent.

Test Plan:

Follow the documented conversion on a gfx942 GPU with ROCm 7.14, letting the
loop run to completion, and confirm the described symptom does not appear:

```
./generate_Device_LC-Framework.py
hipify-perl -inplace lc.cu lc.h
for h in framework.h $(find include components preprocessors verifiers -name '*.h'); do hipify-perl -inplace "$h"; done
hipcc -O3 --offload-arch=gfx942 -ffp-contract=off -DUSE_GPU -I. -std=c++17 -c -o lc.o lc.cu
hipcc --offload-arch=gfx942 -o lc lc.o
./lc input.dat AL "" "RZE_4"
```

The build completes with cosmetic warnings only and the run reports
"LOSSLESS verification passed". Interrupting the loop and compiling produces
the undeclared-identifier error the note now describes.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant