Skip to content

Commit 50753a5

Browse files
igorban-inteligcbot
authored andcommitted
Support SPIR-V Dot operation
.
1 parent 57ee6b0 commit 50753a5

File tree

2 files changed

+82
-1
lines changed

2 files changed

+82
-1
lines changed

IGC/VectorCompiler/lib/GenXOpts/CMTrans/GenXTranslateSPIRVBuiltins.cpp

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,8 +66,8 @@ class SPIRVExpander : public InstVisitor<SPIRVExpander, Value *> {
6666
Value *emitMathIntrinsic(IRBuilder<> &Builder, unsigned IID, Type *Ty,
6767
ArrayRef<Value *> Args, bool AFN = false);
6868
Value *emitFDiv(IRBuilder<> &Builder, Value *L, Value *R, bool ARCP = false);
69-
7069
Value *emitAddcSubb(IRBuilder<> &Builder, unsigned IID, CallInst &CI);
70+
Value *emitDot(IRBuilder<> &Builder, unsigned IID, CallInst &CI);
7171

7272
Module *M;
7373
};
@@ -142,6 +142,17 @@ Value *SPIRVExpander::emitAddcSubb(IRBuilder<> &Builder, unsigned IID,
142142
Builder.CreateStore(ExtRes, ResPtr);
143143
return Builder.CreateStore(Carry, CarryPtr);
144144
}
145+
Value *SPIRVExpander::emitDot(IRBuilder<> &Builder, unsigned IID,
146+
CallInst &CI) {
147+
auto *Ty = CI.getType();
148+
auto *Src0 = CI.getOperand(0);
149+
auto *Src1 = CI.getOperand(1);
150+
IGC_ASSERT_EXIT(Ty->isFloatingPointTy());
151+
auto *Mul = Builder.CreateFMul(Src0, Src1, CI.getName());
152+
auto *Res = Builder.CreateFAddReduce(Constant::getNullValue(Ty), Mul);
153+
Res->setHasAllowReassoc(true);
154+
return Res;
155+
}
145156

146157
Value *SPIRVExpander::visitCallInst(CallInst &CI) {
147158
if (CI.isInlineAsm())
@@ -217,6 +228,10 @@ Value *SPIRVExpander::visitCallInst(CallInst &CI) {
217228
if (IID != Intrinsic::not_intrinsic)
218229
return emitAddcSubb(Builder, IID, CI);
219230

231+
if (CalleeName.startswith("Dot")) {
232+
return emitDot(Builder, IID, CI);
233+
}
234+
220235
// OpenCL extended instruction set
221236
if (!CalleeName.consume_front("ocl_"))
222237
return nullptr;
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
;=========================== begin_copyright_notice ============================
2+
;
3+
; Copyright (C) 2024 Intel Corporation
4+
;
5+
; SPDX-License-Identifier: MIT
6+
;
7+
;============================ end_copyright_notice =============================
8+
;
9+
; RUN: %opt %use_old_pass_manager% -GenXTranslateSPIRVBuiltins \
10+
; RUN: -vc-spirv-builtins-bif-path=%VC_SPIRV_OCL_BIF% -march=genx64 \
11+
; RUN: -mtriple=spir64-unknown-unknown -mcpu=XeHPC -S < %s | FileCheck %s
12+
; ------------------------------------------------
13+
; GenXTranslateSPIRVBuiltins
14+
; ------------------------------------------------
15+
16+
declare spir_func float @_Z11__spirv_DotDv16_fS_(<16 x float>, <16 x float>)
17+
declare spir_func float @_Z11__spirv_DotDv15_fS_(<15 x float>, <15 x float>)
18+
declare spir_func double @_Z11__spirv_DotDv4_fS_(<4 x double>, <4 x double>)
19+
declare spir_func bfloat @_Z11__spirv_DotDv7_fS_(<7 x bfloat>, <7 x bfloat>)
20+
21+
; CHECK-LABEL: spir_func float @dotFloatConst
22+
define spir_func float @dotFloatConst() {
23+
; CHECK: [[FMUL:%[^ ]*]] = fmul <16 x float>
24+
; CHECK: [[RES:%[^ ]*]] = call reassoc float @llvm.vector.reduce.fadd.v16f32(float 0.000000e+00, <16 x float> [[FMUL]])
25+
; CHECK: ret float [[RES]]
26+
%alloc = alloca <16 x float>, align 64
27+
%src1 = load <16 x float>, <16 x float>* %alloc, align 64
28+
%dot = call spir_func float @_Z11__spirv_DotDv16_fS_(<16 x float> %src1, <16 x float> <float 1.000000e+00, float 1.000000e+00, float 1.000000e+00, float 1.000000e+00, float 1.000000e+00, float 1.000000e+00, float 1.000000e+00, float 1.000000e+00, float 1.000000e+00, float 1.000000e+00, float 1.000000e+00, float 1.000000e+00, float 1.000000e+00, float 1.000000e+00, float 1.000000e+00, float 1.000000e+00>)
29+
ret float %dot
30+
}
31+
32+
; CHECK-LABEL: spir_func float @dotFloatVal
33+
define spir_func float @dotFloatVal() {
34+
; CHECK: [[FMUL:%[^ ]*]] = fmul <15 x float>
35+
; CHECK: [[RES:%[^ ]*]] = call reassoc float @llvm.vector.reduce.fadd.v15f32(float 0.000000e+00, <15 x float> [[FMUL]])
36+
; CHECK: ret float [[RES]]
37+
%alloc = alloca <15 x float>, align 64
38+
%src1 = load <15 x float>, <15 x float>* %alloc, align 64
39+
%src2 = load <15 x float>, <15 x float>* %alloc, align 64
40+
%dot = call spir_func float @_Z11__spirv_DotDv15_fS_(<15 x float> %src1, <15 x float> %src2)
41+
ret float %dot
42+
}
43+
44+
; CHECK-LABEL: spir_func double @dotDoubleVal
45+
define spir_func double @dotDoubleVal() {
46+
; CHECK: [[FMUL:%[^ ]*]] = fmul <4 x double> {{.*}}
47+
; CHECK: [[RES:%[^ ]*]] = call reassoc double @llvm.vector.reduce.fadd.v4f64(double 0.000000e+00, <4 x double> [[FMUL]])
48+
; CHECK: ret double [[RES]]
49+
%alloc = alloca <4 x double>, align 128
50+
%src1 = load <4 x double>, <4 x double>* %alloc, align 128
51+
%src2 = load <4 x double>, <4 x double>* %alloc, align 128
52+
%dot = call spir_func double @_Z11__spirv_DotDv4_fS_(<4 x double> %src1, <4 x double> %src2)
53+
ret double %dot
54+
}
55+
56+
; CHECK-LABEL: spir_func bfloat @dotBFloatVal
57+
define spir_func bfloat @dotBFloatVal() {
58+
; CHECK: [[FMUL:%[^ ]*]] = fmul <7 x bfloat> {{.*}}
59+
; CHECK: [[RES:%[^ ]*]] = call reassoc bfloat @llvm.vector.reduce.fadd.v7bf16(bfloat 0xR0000, <7 x bfloat> [[FMUL]])
60+
; CHECK: ret bfloat [[RES]]
61+
%alloc = alloca <7 x bfloat>, align 128
62+
%src1 = load <7 x bfloat>, <7 x bfloat>* %alloc, align 32
63+
%src2 = load <7 x bfloat>, <7 x bfloat>* %alloc, align 32
64+
%dot = call spir_func bfloat @_Z11__spirv_DotDv7_fS_(<7 x bfloat> %src1, <7 x bfloat> %src2)
65+
ret bfloat %dot
66+
}

0 commit comments

Comments
 (0)