llvm-project icon indicating copy to clipboard operation
llvm-project copied to clipboard

[AMDGPU] v_dual_dot2acc_f32_bf16 (VOPD) not emitted on GFX11, unlike f16 equivalent

Open mgehre-amd opened this issue 8 months ago • 5 comments

On GFX11 targets, __builtin_amdgcn_fdot2_f32_bf16 emits v_dot2_f32_bf16 (VOP3P, 8 bytes), which the GCNCreateVOPD pass cannot pair into VOPD dual-issue instructions.

In contrast, the f16 equivalent __builtin_amdgcn_fdot2 correctly emits v_dot2acc_f32_f16 (VOP2), which is then paired into v_dual_dot2acc_f32_f16 by the VOPD pass.

The assembler confirms v_dual_dot2acc_f32_bf16 is a valid instruction on GFX11:

$ echo "v_dual_dot2acc_f32_bf16 v0, v2, v4 :: v_dual_dot2acc_f32_bf16 v1, v3, v5" \
    | llvm-mc --triple=amdgcn -mcpu=gfx1100 --show-encoding
	v_dual_dot2acc_f32_bf16 v0, v2, v4 :: v_dual_dot2acc_f32_bf16 v1, v3, v5 ; encoding: [0x02,0x09,0x5a,0xcb,0x03,0x0b,0x00,0x00]

Reproducer

// dot2_vopd_repro.hip
//
// hipcc -DTYPE=half    --offload-arch=gfx1151 -O2 -S -o dot2_f16.s  dot2_vopd_repro.hip
// hipcc -DTYPE=bf16_t  --offload-arch=gfx1151 -O2 -S -o dot2_bf16.s dot2_vopd_repro.hip
//
// grep -c v_dual_dot2acc dot2_f16.s    # expected > 0
// grep -c v_dual_dot2acc dot2_bf16.s   # expected > 0, actual: 0

#include <hip/hip_runtime.h>
#include <hip/hip_bf16.h>
#include <hip/hip_fp16.h>

#ifndef TYPE
#error "Define TYPE=half or TYPE=bf16_t"
#endif

using bf16_t = __hip_bfloat16;
using scalar_t = TYPE;

__device__ __forceinline__ float dot2acc(float acc, int packed_a, int packed_b) {
  if constexpr (std::is_same_v<scalar_t, half>) {
    return __builtin_amdgcn_fdot2(*((half2*)&packed_a),
                                  *((half2*)&packed_b), acc, false);
  } else {
    typedef short __attribute__((ext_vector_type(2))) v2bf16;
    return __builtin_amdgcn_fdot2_f32_bf16(*((v2bf16*)&packed_a),
                                           *((v2bf16*)&packed_b), acc, false);
  }
}

__global__ void dot2_kernel(float* __restrict__ out,
                            const int* __restrict__ a,
                            const int* __restrict__ b, int n) {
  int tid = threadIdx.x + blockIdx.x * blockDim.x;
  float acc0 = 0.0f, acc1 = 0.0f;

  for (int i = 0; i < n; i += 4) {
    int a0 = a[tid + i], a1 = a[tid + i + 1];
    int b0 = b[tid + i], b1 = b[tid + i + 1];
    int b2 = b[tid + i + 2], b3 = b[tid + i + 3];

    acc0 = dot2acc(acc0, a0, b0);
    acc1 = dot2acc(acc1, a0, b1);
    acc0 = dot2acc(acc0, a1, b2);
    acc1 = dot2acc(acc1, a1, b3);
  }

  out[tid] = acc0 + acc1;
}

Observed Behavior

f16 inner loop (correct — uses VOPD pairing):

v_dot2acc_f32_f16 v1, v7, v3
v_dual_dot2acc_f32_f16 v2, v7, v4 :: v_dual_dot2acc_f32_f16 v1, v8, v5
v_dot2acc_f32_f16 v2, v8, v6

bf16 inner loop (missed optimization — no VOPD pairing):

v_dot2_f32_bf16 v1, v7, v3, v1
v_dot2_f32_bf16 v2, v7, v4, v2
v_dot2_f32_bf16 v1, v8, v5, v1
v_dot2_f32_bf16 v2, v8, v6, v2

Expected Behavior

The bf16 path should also produce VOPD pairs, e.g.:

v_dual_dot2acc_f32_bf16 v1, v7, v3 :: v_dual_dot2acc_f32_bf16 v2, v7, v4
v_dual_dot2acc_f32_bf16 v1, v8, v5 :: v_dual_dot2acc_f32_bf16 v2, v8, v6

Notes

  • The issue affects all GFX11 targets (confirmed on gfx1100 and gfx1151).
  • Unlike f16, there is no standalone VOP2 v_dot2acc_f32_bf16 on GFX11 — the instruction only exists in VOPD form. A fix would need to either:
    • Enable the VOP2 pseudo on GFX11 with a fallback to VOP3P for unpaired instructions, or
    • Extend GCNCreateVOPD to convert VOP3P V_DOT2_F32_BF16 to VOPD when the accumulator equals the destination.
  • Tested with AMD clang 22.0.0git (ROCm/llvm-project 985b939f68).

mgehre-amd avatar Mar 12 '26 16:03 mgehre-amd

@llvm/issue-subscribers-backend-amdgpu

Author: Matthias Gehre (mgehre-amd)

On GFX11 targets, `__builtin_amdgcn_fdot2_f32_bf16` emits `v_dot2_f32_bf16` (VOP3P, 8 bytes), which the GCNCreateVOPD pass cannot pair into VOPD dual-issue instructions.

In contrast, the f16 equivalent __builtin_amdgcn_fdot2 correctly emits v_dot2acc_f32_f16 (VOP2), which is then paired into v_dual_dot2acc_f32_f16 by the VOPD pass.

The assembler confirms v_dual_dot2acc_f32_bf16 is a valid instruction on GFX11:

$ echo "v_dual_dot2acc_f32_bf16 v0, v2, v4 :: v_dual_dot2acc_f32_bf16 v1, v3, v5" \
    | llvm-mc --triple=amdgcn -mcpu=gfx1100 --show-encoding
	v_dual_dot2acc_f32_bf16 v0, v2, v4 :: v_dual_dot2acc_f32_bf16 v1, v3, v5 ; encoding: [0x02,0x09,0x5a,0xcb,0x03,0x0b,0x00,0x00]

Reproducer

// dot2_vopd_repro.hip
//
// hipcc -DTYPE=half    --offload-arch=gfx1151 -O2 -S -o dot2_f16.s  dot2_vopd_repro.hip
// hipcc -DTYPE=bf16_t  --offload-arch=gfx1151 -O2 -S -o dot2_bf16.s dot2_vopd_repro.hip
//
// grep -c v_dual_dot2acc dot2_f16.s    # expected &gt; 0
// grep -c v_dual_dot2acc dot2_bf16.s   # expected &gt; 0, actual: 0

#include &lt;hip/hip_runtime.h&gt;
#include &lt;hip/hip_bf16.h&gt;
#include &lt;hip/hip_fp16.h&gt;

#ifndef TYPE
#error "Define TYPE=half or TYPE=bf16_t"
#endif

using bf16_t = __hip_bfloat16;
using scalar_t = TYPE;

__device__ __forceinline__ float dot2acc(float acc, int packed_a, int packed_b) {
  if constexpr (std::is_same_v&lt;scalar_t, half&gt;) {
    return __builtin_amdgcn_fdot2(*((half2*)&amp;packed_a),
                                  *((half2*)&amp;packed_b), acc, false);
  } else {
    typedef short __attribute__((ext_vector_type(2))) v2bf16;
    return __builtin_amdgcn_fdot2_f32_bf16(*((v2bf16*)&amp;packed_a),
                                           *((v2bf16*)&amp;packed_b), acc, false);
  }
}

__global__ void dot2_kernel(float* __restrict__ out,
                            const int* __restrict__ a,
                            const int* __restrict__ b, int n) {
  int tid = threadIdx.x + blockIdx.x * blockDim.x;
  float acc0 = 0.0f, acc1 = 0.0f;

  for (int i = 0; i &lt; n; i += 4) {
    int a0 = a[tid + i], a1 = a[tid + i + 1];
    int b0 = b[tid + i], b1 = b[tid + i + 1];
    int b2 = b[tid + i + 2], b3 = b[tid + i + 3];

    acc0 = dot2acc(acc0, a0, b0);
    acc1 = dot2acc(acc1, a0, b1);
    acc0 = dot2acc(acc0, a1, b2);
    acc1 = dot2acc(acc1, a1, b3);
  }

  out[tid] = acc0 + acc1;
}

Observed Behavior

f16 inner loop (correct — uses VOPD pairing):

v_dot2acc_f32_f16 v1, v7, v3
v_dual_dot2acc_f32_f16 v2, v7, v4 :: v_dual_dot2acc_f32_f16 v1, v8, v5
v_dot2acc_f32_f16 v2, v8, v6

bf16 inner loop (missed optimization — no VOPD pairing):

v_dot2_f32_bf16 v1, v7, v3, v1
v_dot2_f32_bf16 v2, v7, v4, v2
v_dot2_f32_bf16 v1, v8, v5, v1
v_dot2_f32_bf16 v2, v8, v6, v2

Expected Behavior

The bf16 path should also produce VOPD pairs, e.g.:

v_dual_dot2acc_f32_bf16 v1, v7, v3 :: v_dual_dot2acc_f32_bf16 v2, v7, v4
v_dual_dot2acc_f32_bf16 v1, v8, v5 :: v_dual_dot2acc_f32_bf16 v2, v8, v6

Notes

  • The issue affects all GFX11 targets (confirmed on gfx1100 and gfx1151).
  • Unlike f16, there is no standalone VOP2 v_dot2acc_f32_bf16 on GFX11 — the instruction only exists in VOPD form. A fix would need to either:
    • Enable the VOP2 pseudo on GFX11 with a fallback to VOP3P for unpaired instructions, or
    • Extend GCNCreateVOPD to convert VOP3P V_DOT2_F32_BF16 to VOPD when the accumulator equals the destination.
  • Tested with AMD clang 22.0.0git (ROCm/llvm-project 985b939f68).

llvmbot avatar Mar 12 '26 17:03 llvmbot

There is already a PR for this that explores the VOP2 psuedo idea for gfx11/gfx12 https://github.com/llvm/llvm-project/pull/179226

mbrkusanin avatar Mar 12 '26 18:03 mbrkusanin

Original PRs were merged and later reverted via https://github.com/llvm/llvm-project/pull/190159.

mgehre-amd avatar Apr 29 '26 07:04 mgehre-amd

On-going work in https://github.com/llvm/llvm-project/pull/196516

mgehre-amd avatar May 19 '26 13:05 mgehre-amd

Seems fixed now: https://godbolt.org/z/Yda17rhKr

tgymnich avatar Aug 15 '26 22:08 tgymnich