Skip to content

Commit 05b4cda

Browse files
MochalovaAnvmaksimo
authored andcommitted
Add llvm.abs.i32 translation (#758)
* Add llvm.abs.i32 intrinsic translation Add translation of @llvm.abs.* to `s_abs` extended instruction. Signed-off-by: amochalo <anastasiya.mochalova@intel.com>
1 parent 47d007e commit 05b4cda

File tree

3 files changed

+67
-0
lines changed

3 files changed

+67
-0
lines changed

llvm-spirv/lib/SPIRV/SPIRVUtil.cpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1551,6 +1551,21 @@ bool checkTypeForSPIRVExtendedInstLowering(IntrinsicInst *II, SPIRVModule *BM) {
15511551
}
15521552
break;
15531553
}
1554+
case Intrinsic::abs: {
1555+
Type *Ty = II->getType();
1556+
int NumElems = 1;
1557+
if (auto *VecTy = dyn_cast<FixedVectorType>(Ty)) {
1558+
NumElems = VecTy->getNumElements();
1559+
Ty = VecTy->getElementType();
1560+
}
1561+
if ((!Ty->isIntegerTy()) ||
1562+
((NumElems > 4) && (NumElems != 8) && (NumElems != 16))) {
1563+
BM->getErrorLog().checkError(false, SPIRVEC_InvalidFunctionCall,
1564+
II->getCalledOperand()->getName().str(), "",
1565+
__FILE__, __LINE__);
1566+
}
1567+
break;
1568+
}
15541569
default:
15551570
break;
15561571
}

llvm-spirv/lib/SPIRV/SPIRVWriter.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1961,6 +1961,7 @@ bool LLVMToSPIRV::isKnownIntrinsic(Intrinsic::ID Id) {
19611961
case Intrinsic::bitreverse:
19621962
case Intrinsic::sqrt:
19631963
case Intrinsic::fabs:
1964+
case Intrinsic::abs:
19641965
case Intrinsic::ceil:
19651966
case Intrinsic::ctlz:
19661967
case Intrinsic::cttz:
@@ -2075,6 +2076,17 @@ SPIRVValue *LLVMToSPIRV::transIntrinsicInst(IntrinsicInst *II,
20752076
return BM->addExtInst(STy, BM->getExtInstSetId(SPIRVEIS_OpenCL), ExtOp, Ops,
20762077
BB);
20772078
}
2079+
case Intrinsic::abs: {
2080+
if (!checkTypeForSPIRVExtendedInstLowering(II, BM))
2081+
break;
2082+
// LLVM has only one version of abs and it is only for signed integers. We
2083+
// unconditionally choose SAbs here
2084+
SPIRVWord ExtOp = OpenCLLIB::SAbs;
2085+
SPIRVType *STy = transType(II->getType());
2086+
std::vector<SPIRVValue *> Ops(1, transValue(II->getArgOperand(0), BB));
2087+
return BM->addExtInst(STy, BM->getExtInstSetId(SPIRVEIS_OpenCL), ExtOp, Ops,
2088+
BB);
2089+
}
20782090
case Intrinsic::ceil: {
20792091
if (!checkTypeForSPIRVExtendedInstLowering(II, BM))
20802092
break;

llvm-spirv/test/abs.ll

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
; RUN: llvm-as %s -o %t.bc
2+
; RUN: llvm-spirv %t.bc -spirv-text -o - | FileCheck %s
3+
; RUN: llvm-spirv %t.bc -o %t.spv
4+
; RUN: spirv-val %t.spv
5+
6+
7+
; CHECK: ExtInst {{[0-9]+}} {{[0-9]+}} {{[0-9]+}} s_abs
8+
; CHECK: ExtInst {{[0-9]+}} {{[0-9]+}} {{[0-9]+}} s_abs
9+
10+
target datalayout = "e-i64:64-v16:16-v24:32-v32:32-v48:64-v96:128-v192:256-v256:256-v512:512-v1024:1024-n8:16:32:64"
11+
target triple = "spir64-unknown-linux-sycldevice"
12+
13+
; Function Attrs: norecurse nounwind readnone
14+
define dso_local spir_kernel void @test(i32 %a, <4 x i32> %b) local_unnamed_addr #0 !kernel_arg_buffer_location !5 {
15+
entry:
16+
%0 = tail call i32 @llvm.abs.i32(i32 %a, i1 0) #2
17+
%1 = tail call <4 x i32> @llvm.abs.v4i32(<4 x i32> %b, i1 0) #2
18+
ret void
19+
}
20+
21+
; Function Attrs: inaccessiblememonly nounwind willreturn
22+
declare i32 @llvm.abs.i32(i32, i1) #1
23+
24+
; Function Attrs: inaccessiblememonly nounwind willreturn
25+
declare <4 x i32> @llvm.abs.v4i32(<4 x i32>, i1) #1
26+
27+
attributes #0 = { norecurse nounwind readnone "correctly-rounded-divide-sqrt-fp-math"="false" "disable-tail-calls"="false" "frame-pointer"="all" "less-precise-fpmad"="false" "min-legal-vector-width"="0" "no-infs-fp-math"="false" "no-jump-tables"="false" "no-nans-fp-math"="false" "no-signed-zeros-fp-math"="false" "no-trapping-math"="true" "stack-protector-buffer-size"="8" "sycl-module-id"="test.cl" "uniform-work-group-size"="true" "unsafe-fp-math"="false" "use-soft-float"="false" }
28+
29+
!llvm.module.flags = !{!0}
30+
!opencl.ocl.version = !{!1}
31+
!opencl.spir.version = !{!2, !2}
32+
!spirv.Source = !{!3}
33+
!llvm.ident = !{!4}
34+
35+
!0 = !{i32 1, !"wchar_size", i32 4}
36+
!1 = !{i32 1, i32 0}
37+
!2 = !{i32 1, i32 2}
38+
!3 = !{i32 4, i32 100000}
39+
!4 = !{!"clang version 12.0.0 (https://github.com/c199914007/llvm.git 7f855fa5b04d46494c34a425aa777f8bfc3433b1)"}
40+
!5 = !{i32 -1}

0 commit comments

Comments
 (0)