Skip to content

Commit 09a240b

Browse files
[MLIR][ArmSVE] Add an ArmSVE dialect operation mapping to bfmmla
1 parent 13daf65 commit 09a240b

File tree

5 files changed

+73
-3
lines changed

5 files changed

+73
-3
lines changed

mlir/include/mlir/Dialect/ArmSVE/IR/ArmSVE.td

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -293,6 +293,35 @@ def UsmmlaOp : ArmSVE_Op<"usmmla", [Pure,
293293
"$acc `,` $src1 `,` $src2 attr-dict `:` type($src1) `to` type($dst)";
294294
}
295295

296+
297+
def BfmmlaOp : ArmSVE_Op<"bfmmla", [Pure,
298+
AllTypesMatch<["src1", "src2"]>,
299+
AllTypesMatch<["acc", "dst"]>]> {
300+
let summary = "BFloat16 matrix multiply-accumulate";
301+
let description = [{
302+
BFMMLA: BFloat16 matrix multiply-accumulate into 2×2 matrices";
303+
304+
This operation multiplies the 2x4 BFloat16 matrix held in each 128-bit
305+
segment of the first source vector by the 4x2 BFloat16 matrix in the
306+
corresponding segment of the second source vector, then accumulates
307+
this intermediate result with the 2x2 Float32 matrix in the corresponding
308+
segment of the accumulator vector, yielding the final 2x2 Float32
309+
segment of the result.
310+
311+
Source:
312+
https://developer.arm.com/documentation/100987/0000
313+
}];
314+
// Supports (vector<[8]xbf16>, vector<[8]xbf16>) -> (vector<[4]xf32>)
315+
let arguments = (ins
316+
ScalableVectorOfLengthAndType<[4], [F32]>:$acc,
317+
ScalableVectorOfLengthAndType<[8], [BF16]>:$src1,
318+
ScalableVectorOfLengthAndType<[8], [BF16]>:$src2
319+
);
320+
let results = (outs ScalableVectorOfLengthAndType<[4], [F32]>:$dst);
321+
let assemblyFormat =
322+
"$acc `,` $src1 `,` $src2 attr-dict `:` type($src1) `to` type($dst)";
323+
}
324+
296325
class SvboolTypeConstraint<string lhsArg, string rhsArg> : TypesMatchWith<
297326
"expected corresponding svbool type widened to [16]xi1",
298327
lhsArg, rhsArg,
@@ -590,6 +619,12 @@ def UsmmlaIntrOp :
590619
ArmSVE_IntrBinaryOverloadedOp<"usmmla">,
591620
Arguments<(ins AnyScalableVectorOfAnyRank, AnyScalableVectorOfAnyRank, AnyScalableVectorOfAnyRank)>;
592621

622+
def BfmmlaIntrOp :
623+
ArmSVE_IntrOp<"bfmmla", [Pure, TypeIs<"res", ScalableVectorOfLengthAndType<[4], [F32]>>]>,
624+
Arguments<(ins Arg<ScalableVectorOfLengthAndType<[4], [F32]>, "acc">:$acc,
625+
Arg<ScalableVectorOfLengthAndType<[8], [BF16]>, "lhs">:$lhs,
626+
Arg<ScalableVectorOfLengthAndType<[8], [BF16]>, "rhs">:$rhs)>;
627+
593628
def SdotIntrOp :
594629
ArmSVE_IntrBinaryOverloadedOp<"sdot">,
595630
Arguments<(ins AnyScalableVectorOfAnyRank, AnyScalableVectorOfAnyRank, AnyScalableVectorOfAnyRank)>;

mlir/lib/Dialect/ArmSVE/Transforms/LegalizeForLLVMExport.cpp

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ using SmmlaOpLowering = OneToOneConvertToLLVMPattern<SmmlaOp, SmmlaIntrOp>;
2525
using UdotOpLowering = OneToOneConvertToLLVMPattern<UdotOp, UdotIntrOp>;
2626
using UmmlaOpLowering = OneToOneConvertToLLVMPattern<UmmlaOp, UmmlaIntrOp>;
2727
using UsmmlaOpLowering = OneToOneConvertToLLVMPattern<UsmmlaOp, UsmmlaIntrOp>;
28+
using BfmmlaOpLowering = OneToOneConvertToLLVMPattern<BfmmlaOp, BfmmlaIntrOp>;
2829
using DupQLaneLowering =
2930
OneToOneConvertToLLVMPattern<DupQLaneOp, DupQLaneIntrOp>;
3031
using ScalableMaskedAddIOpLowering =
@@ -191,7 +192,8 @@ void mlir::populateArmSVELegalizeForLLVMExportPatterns(
191192
// Populate conversion patterns
192193

193194
// clang-format off
194-
patterns.add<ConvertFromSvboolOpLowering,
195+
patterns.add<BfmmlaOpLowering,
196+
ConvertFromSvboolOpLowering,
195197
ConvertToSvboolOpLowering,
196198
DupQLaneLowering,
197199
PselOpLowering,
@@ -220,7 +222,8 @@ void mlir::populateArmSVELegalizeForLLVMExportPatterns(
220222
void mlir::configureArmSVELegalizeForExportTarget(
221223
LLVMConversionTarget &target) {
222224
// clang-format off
223-
target.addLegalOp<ConvertFromSvboolIntrOp,
225+
target.addLegalOp<BfmmlaIntrOp,
226+
ConvertFromSvboolIntrOp,
224227
ConvertToSvboolIntrOp,
225228
DupQLaneIntrOp,
226229
PselIntrOp,
@@ -241,7 +244,8 @@ void mlir::configureArmSVELegalizeForExportTarget(
241244
ZipX2IntrOp,
242245
ZipX4IntrOp,
243246
SdotIntrOp>();
244-
target.addIllegalOp<ConvertFromSvboolOp,
247+
target.addIllegalOp<BfmmlaOp,
248+
ConvertFromSvboolOp,
245249
ConvertToSvboolOp,
246250
DupQLaneOp,
247251
PselOp,

mlir/test/Dialect/ArmSVE/legalize-for-llvm.mlir

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,15 @@ func.func @arm_sve_usmmla(%a: vector<[16]xi8>,
6060

6161
// -----
6262

63+
func.func @arm_sve_bfmmla(%a: vector<[8]xbf16>,
64+
%b: vector<[8]xbf16>,
65+
%c: vector<[4]xf32>) -> vector<[4]xf32> {
66+
// CHECK: arm_sve.intr.bfmmla
67+
%0 = arm_sve.bfmmla %c, %a, %b : vector<[8]xbf16> to vector<[4]xf32>
68+
return %0 : vector<[4]xf32>
69+
}
70+
// -----
71+
6372
func.func @arm_sve_arithi_masked(%a: vector<[4]xi32>,
6473
%b: vector<[4]xi32>,
6574
%c: vector<[4]xi32>,

mlir/test/Dialect/ArmSVE/roundtrip.mlir

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,16 @@ func.func @arm_sve_usmmla(%a: vector<[16]xi8>,
5555

5656
// -----
5757

58+
func.func @arm_sve_bfmmla(%a: vector<[8]xbf16>,
59+
%b: vector<[8]xbf16>,
60+
%c: vector<[4]xf32>) -> vector<[4]xf32> {
61+
// CHECK: arm_sve.bfmmla {{.*}}: vector<[8]xbf16> to vector<[4]xf32>
62+
%0 = arm_sve.bfmmla %c, %a, %b : vector<[8]xbf16> to vector<[4]xf32>
63+
return %0 : vector<[4]xf32>
64+
}
65+
66+
// -----
67+
5868
func.func @arm_sve_masked_arithi(%a: vector<[4]xi32>,
5969
%b: vector<[4]xi32>,
6070
%c: vector<[4]xi32>,

mlir/test/Target/LLVMIR/arm-sve.mlir

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,18 @@ llvm.func @arm_sve_usmmla(%arg0: vector<[16]xi8>,
6060
llvm.return %0 : vector<[4]xi32>
6161
}
6262

63+
// CHECK-LABEL: define <vscale x 4 x float> @arm_sve_bfmmla
64+
llvm.func @arm_sve_bfmmla(%arg0: vector<[8]xbf16>,
65+
%arg1: vector<[8]xbf16>,
66+
%arg2: vector<[4]xf32>)
67+
-> vector<[4]xf32> {
68+
// CHECK: call <vscale x 4 x float> @llvm.aarch64.sve.bfmmla(<vscale x 4 x float>
69+
%0 = "arm_sve.intr.bfmmla"(%arg2, %arg0, %arg1) :
70+
(vector<[4]xf32>, vector<[8]xbf16>, vector<[8]xbf16>)
71+
-> vector<[4]xf32>
72+
llvm.return %0 : vector<[4]xf32>
73+
}
74+
6375
// CHECK-LABEL: define <vscale x 4 x i32> @arm_sve_arithi
6476
llvm.func @arm_sve_arithi(%arg0: vector<[4]xi32>,
6577
%arg1: vector<[4]xi32>,

0 commit comments

Comments
 (0)