Skip to content

[HIPIFY][fix] Map nv_bfloat16 and nv_bfloat162 to __hip_bfloat16 and __hip_bfloat162 - #2810

Open
yassinsolim wants to merge 1 commit into
amd-developfrom
users/ysoliman/hipify-bf16-mapping
Open

yassinsolim wants to merge 1 commit into
amd-developfrom
users/ysoliman/hipify-bf16-mapping

Conversation

@yassinsolim

Copy link
Copy Markdown
Member

Motivation

Fixes #2809.

hipify-perl and hipify-clang convert nv_bfloat16 to hip_bfloat16, a type that hip/hip_bf16.h does not declare. nv_bfloat162 has no mapping, so it is marked unsupported and left in place. The converted file then fails to compile with hipcc (unknown type name 'hip_bfloat16').

nv_bfloat16 and nv_bfloat162 are typedefs of __nv_bfloat16 and __nv_bfloat162 in cuda_bf16.h. The double-underscore names already map to __hip_bfloat16 and __hip_bfloat162, so the typedef names should map the same way.

Technical Details

  • src/CUDA2HIP_Device_types.cpp: nv_bfloat16 maps to __hip_bfloat16 (was hip_bfloat16), and nv_bfloat162 maps to __hip_bfloat162 (was unsupported).
  • bin/hipify-perl and docs/reference/tables/CUDA_Device_API_supported_by_HIP.md are regenerated with hipify-clang built from this branch, using the commands in the repo docs (--perl, and --md with --doc-roc=joint and separate). bin/hipify-perl changed upstream since the branch was first written, so it was regenerated on the current amd-develop instead of merged by hand. Regenerating again gives no diff.
  • tests/unit_tests/synthetic/libraries/cudevice2hipdevice.cu: nv_bfloat16 now expects __hip_bfloat16, and there is a new nv_bfloat162 case.

What could break: nv_bfloat16 changes from hip_bfloat16 to __hip_bfloat16 for every user. Code that converted before and compiled only because it also included hip/hip_bfloat16.h now gets the hip/hip_bf16.h type, which matches how __nv_bfloat16 is already mapped. The table lists __hip_bfloat16 as available since ROCm 5.7.0.

Test Plan

  • Run cudevice2hipdevice.cu and the full lit suite with hipify-clang built from amd-develop and from this branch.
  • Convert a small file that uses nv_bfloat16 and nv_bfloat162 with hipify-perl and hipify-clang before and after, and compile the output with hipcc.

Test Result

LLVM 20.1.8, CUDA 12.8, Linux. The cuDNN, cuTensor, cuFile and cuSPARSELt tests are excluded because no roots were available.

  • hipify-clang from amd-develop (9a85980) with the updated cudevice2hipdevice.cu: 141 of 142 pass, and that test fails because nv_bfloat16 bf16 becomes hip_bfloat16 bf16 and nv_bfloat162 bf162c stays unconverted. With this change the full suite passes, 142 of 142. The lit output of the failing run is cut off by lit's output limit, so the two lines above come from running hipify-clang on the test file directly.
  • hipify-perl and hipify-clang on bf16_in.cu (nv_bfloat16 *x, nv_bfloat162 *y): before, hip_bfloat16 *x, nv_bfloat162 *y and a warning for nv_bfloat162. After, __hip_bfloat16 *x, __hip_bfloat162 *y and no warning.
  • hipcc 7.1.25424, --offload-arch=gfx942 --cuda-device-only -c: the output from before fails with unknown type name 'hip_bfloat16' and unknown type name 'nv_bfloat162' (hipify-perl and hipify-clang output both). The output from after compiles, rc 0 for both. The same input written with __nv_bfloat16 and __nv_bfloat162 converts with hipify-perl to the same __hip_ types on amd-develop already and compiles, rc 0.
  • Not run: ROCm releases older than 5.7, other LLVM or CUDA versions, and any run on a GPU (only source conversion and device-only compilation).

Submission Checklist

…__hip_bfloat162

nv_bfloat162 was marked unsupported, so hipify-perl and hipify-clang left it
in place. nv_bfloat16 was mapped to hip_bfloat16, which hip/hip_bf16.h does not
declare (it lives in hip/hip_bfloat16.h), so converted code did not compile.
Both are typedefs of the double-underscore types in cuda_bf16.h, so map them
to the matching __hip_ types, like __nv_bfloat16 and __nv_bfloat162.

Regenerate bin/hipify-perl and the device API table.

Tested: synthetic/libraries/cudevice2hipdevice.cu fails on the base commit and
passes with this change (LLVM 20.1.8, CUDA 12.8).
@github-actions

Copy link
Copy Markdown
Security scan found no critical or high findings on lines changed by this pull request

View the security scan run for complete reports.

“New” means the finding is located in a changed file on an added or modified line; it is not a base-versus-head findings delta.

@yassinsolim
yassinsolim marked this pull request as ready for review November 11, 2026 20:15
Copilot AI balanced review requested due to automatic review settings November 11, 2026 20:15

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🟢 Approval recommended

No active review issues remain for the CUDA bfloat16 typedef mapping updates.

No open issues 🎉

What changed in this PR

Fixes #2809 by mapping CUDA’s nv_bfloat16 and nv_bfloat162 typedefs to the HIP types declared in hip/hip_bf16.h, matching the existing double-underscore mappings.

Changes:

  • Corrects nv_bfloat16 conversion and enables support for nv_bfloat162.
  • Updates the generated Perl converter and support documentation.
  • Adjusts the scalar regression expectation and adds coverage for the paired type.
File Description
tests/​unit_tests/​synthetic/​libraries/​cudevice2hipdevice.cu Checks both typedefs convert to the correct HIP types.
src/​CUDA2HIP_Device_types.cpp Corrects both mappings and removes the unsupported flag for nv_bfloat162.
docs/​reference/​tables/​CUDA_Device_API_supported_by_HIP.md Documents the corrected mappings and ROCm availability.
bin/​hipify-perl Synchronizes generated mappings and supported-type lists.

Review effort: Balanced

@emankov emankov added device device API-related fix It fixes bug labels Nov 12, 2026
Comment on lines +1052 to +1053
|`nv_bfloat16`|11.0| | | |`__hip_bfloat16`|5.7.0| | | | | |
|`nv_bfloat162`|11.0| | | |`__hip_bfloat162`|5.7.0| | | | | |

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.

Was it changed manually?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

No, it's generated. I rebuilt hipify-clang from this branch (LLVM 18.1.3), reran the --perl and --md generators (joint and separate), and they give no diff against the PR.

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.

But in the PR, HIP versions of the newly supported data types are not present in the source code.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

They're already in the source, just not in this diff. HIP_DEVICE_TYPE_NAME_VER_MAP in src/CUDA2HIP_Device_types.cpp has __hip_bfloat16 and __hip_bfloat162 as HIP_5070 at lines 239 and 257. They came in with 5466c48 for __nv_bfloat16 and __nv_bfloat162. The doc generator looks the HIP version up by HIP name, so the nv_ typedef rows get 5.7.0 as well. curandRngType and curandRngType_t share hiprandRngType_t the same way. If you meant something else, could you point me to it?

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.

hip_bfloat16 is declared in hip/amd_detail/amd_hip_bfloat16.h (clr):

typedef struct {
  uint16_t data;
} hip_bfloat16;

__hip_bfloat16 is declared in hip/amd_detail/amd_hip_bf16.h (clr):

struct __attribute__((aligned(2))) __hip_bfloat16 {
 protected:
  union {
    /*! \brief raw representation of bfloat16 */
    unsigned short __x;
    /*! \brief bf16 represenation */
    __bf16 __x_bf16;
  };

Yes, there is a corresponding typedef for them:

typedef __hip_bfloat16 hip_bfloat16;

But 1) it is not reachable by default; 2) they are still different types.

The question with hip_bfloat16 is different: should it be visible to the compiler by default, or does it require an explicit include?

As for hip_bfloat162, it is not declared in clr. It probably needs to be declared; thus, a ticket for clr would help determine this.

So, I'd not recommend mixing different types in the hipification process, but I'm open to discussion.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

The PR doesn't map to hip_bfloat16, it maps nv_bfloat16 and nv_bfloat162 to __hip_bfloat16 and __hip_bfloat162. In cuda_bf16.h nv_bfloat16 is only typedef __nv_bfloat16 nv_bfloat16, so it's the same type HIPIFY already maps to __hip_bfloat16, and this keeps clear of the old hip_bfloat16 struct and the missing hip_bfloat162. If you'd rather map to the typedef names once clr declares hip_bfloat162, I can open that clr ticket.

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.

Was:
nv_bfloat16 -> hip_bfloat16
nv_bfloat162 -> unsupported by HIP
Now:
nv_bfloat16 -> __hip_bfloat16
nv_bfloat162 -> __hip_bfloat162

Why I'm for Was, I explained above.

@emankov emankov left a comment

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.

Please regenerate hipify-perl and CUDA2HIP documentation for the PR.

@yassinsolim
yassinsolim requested a review from emankov November 15, 2026 12:33
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

device device API-related fix It fixes bug

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[HIPIFY][Issue] nv_bfloat16 converts to hip_bfloat16 and nv_bfloat162 is left unconverted

3 participants