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

Masked store of <8 x bf16> is not lowered

Open quic-akaryaki opened this issue 6 months ago • 8 comments

With v9.2a, masked stores of bf16 cause an ICE: https://godbolt.org/z/PsPbdzTM7

llc --mtriple=aarch64-none-linux-gnu -O2 aarch64-bf16-mstore.ll -o -

LLVM ERROR: Cannot select: t12: ch = masked_store<(store unknown-size into %ir.P, align 2)> t0, t2, t4, undef:i64, t14
  t2: v8bf16,ch = CopyFromReg t0, Register:v8bf16 %0
  t4: i64,ch = CopyFromReg t0, Register:i64 %1
  t14: v8i16 = setcc t6, t8, seteq:ch
    t6: v8i16,ch = CopyFromReg t0, Register:v8i16 %2
    t8: v8i16,ch = CopyFromReg t0, Register:v8i16 %3
In function: f_bf16

The source code:

    target datalayout = "e-m:e-p270:32:32-p271:32:32-p272:64:64-i8:8:32-i16:16:32-i64:64-i128:128-n32:64-S128-Fn32"
    target triple = "aarch64-none-linux-gnu"
    define void @f_f16(<8 x half> %V, ptr noundef %P, <8 x i16> %X, <8 x i16> %Y) #1 {
    entry:
      %cmp = icmp eq <8 x i16> %X, %Y
      tail call void @llvm.masked.store(<8 x half> %V, ptr align 2 %P, <8 x i1> %cmp)
      ret void
    }
    define void @f_i16(<8 x i16> %V, ptr noundef %P, <8 x i16> %X, <8 x i16> %Y) #1 {
    entry:
      %cmp = icmp eq <8 x i16> %X, %Y
      tail call void @llvm.masked.store(<8 x i16> %V, ptr align 2 %P, <8 x i1> %cmp)
      ret void
    }
    define void @f_bf16(<8 x bfloat> %V, ptr noundef %P, <8 x i16> %X, <8 x i16> %Y) #1 {
    entry:
      %cmp = icmp eq <8 x i16> %X, %Y
      tail call void @llvm.masked.store.v8bf16.p0(<8 x bfloat> %V, ptr align 2 %P, <8 x i1> %cmp)
      ret void
    }
    attributes #1 = { mustprogress "target-cpu"="generic" "target-features"="+bf16,+sve,+sve2,+v8.1a,+v8.2a,+v8.3a,+v8.4a,+v8.5a,+v8.6a,+v8.7a,+v8a,+v9.1a,+v9.2a,+v9a" }

Masked stores of other 16-bit types (i16, half) are lowered to st1h { z0.h }, p1, [x0], they are in the same example.

We generate such masked stores with the Ripple front-end.

LLVM commit: 0212229ac05d60314db7dfb8ae89c11a015bb7bc

quic-akaryaki avatar Jun 02 '26 16:06 quic-akaryaki

@llvm/issue-subscribers-backend-aarch64

Author: Alexey Karyakin (quic-akaryaki)

With v9.2a, masked stores of bf16 cause an ICE:
llc --mtriple=aarch64-none-linux-gnu -O2 aarch64-bf16-mstore.ll -o -

LLVM ERROR: Cannot select: t12: ch = masked_store&lt;(store unknown-size into %ir.P, align 2)&gt; t0, t2, t4, undef:i64, t14
  t2: v8bf16,ch = CopyFromReg t0, Register:v8bf16 %0
  t4: i64,ch = CopyFromReg t0, Register:i64 %1
  t14: v8i16 = setcc t6, t8, seteq:ch
    t6: v8i16,ch = CopyFromReg t0, Register:v8i16 %2
    t8: v8i16,ch = CopyFromReg t0, Register:v8i16 %3
In function: f_bf16

The source code:

target datalayout = "e-m:e-p270:32:32-p271:32:32-p272:64:64-i8:8:32-i16:16:32-i64:64-i128:128-n32:64-S128-Fn32"
target triple = "aarch64-none-linux-gnu"
define void @<!-- -->f_f16(&lt;8 x half&gt; %V, ptr noundef %P, &lt;8 x i16&gt; %X, &lt;8 x i16&gt; %Y) #<!-- -->1 {
entry:
  %cmp = icmp eq &lt;8 x i16&gt; %X, %Y
  tail call void @<!-- -->llvm.masked.store(&lt;8 x half&gt; %V, ptr align 2 %P, &lt;8 x i1&gt; %cmp)
  ret void
}
define void @<!-- -->f_i16(&lt;8 x i16&gt; %V, ptr noundef %P, &lt;8 x i16&gt; %X, &lt;8 x i16&gt; %Y) #<!-- -->1 {
entry:
  %cmp = icmp eq &lt;8 x i16&gt; %X, %Y
  tail call void @<!-- -->llvm.masked.store(&lt;8 x i16&gt; %V, ptr align 2 %P, &lt;8 x i1&gt; %cmp)
  ret void
}
define void @<!-- -->f_bf16(&lt;8 x bfloat&gt; %V, ptr noundef %P, &lt;8 x i16&gt; %X, &lt;8 x i16&gt; %Y) #<!-- -->1 {
entry:
  %cmp = icmp eq &lt;8 x i16&gt; %X, %Y
  tail call void @<!-- -->llvm.masked.store.v8bf16.p0(&lt;8 x bfloat&gt; %V, ptr align 2 %P, &lt;8 x i1&gt; %cmp)
  ret void
}
attributes #<!-- -->1 = { mustprogress "target-cpu"="generic" "target-features"="+bf16,+sve,+sve2,+v8.1a,+v8.2a,+v8.3a,+v8.4a,+v8.5a,+v8.6a,+v8.7a,+v8a,+v9.1a,+v9.2a,+v9a" }

Masked stores of other 16-bit types (i16, half) are lowered to st1h { z0.h }, p1, [x0], they are in the same example.

We generate such masked stores with the Ripple front-end.

LLVM commit: 0212229ac05d60314db7dfb8ae89c11a015bb7bc

We can hopefully use this for the other issues in BF16 fixed-length vectorization too. The ones I know of are:

  • #208744 Loads / stores.
  • #209194 FPExt (already on the branch).
  • #209411 FPTrunc.
  • #210273 Masked gather / scatter.

davemgreen avatar Jul 17 '26 07:07 davemgreen

/cherry-pick 50f2b9443d23eba757c3c0f42eef71e0f9721b15

davemgreen avatar Jul 17 '26 07:07 davemgreen

/pull-request llvm/llvm-project#210264

llvmbot avatar Jul 17 '26 07:07 llvmbot

/cherry-pick b9869c8c920a7dfa983e215bc30729b13d8b353b

davemgreen avatar Jul 17 '26 08:07 davemgreen

/cherry-pick 50f2b9443d23eba757c3c0f42eef71e0f9721b15

davemgreen avatar Jul 17 '26 12:07 davemgreen

It looks like the cherry picks didn't work like I expected - The second overrode the first in stead of creating a new one. I am trying to set it back and will create the other elsewhere.

davemgreen avatar Jul 17 '26 12:07 davemgreen

Should be fixed in clang23. Thanks all.

davemgreen avatar Aug 06 '26 12:08 davemgreen