Skip to content

Commit 4a858cf

Browse files
committed
Add translation for Intrinsic::{atan,acos,asin,cosh,sinh,tanh} (KhronosGroup#2657)
Add translation for atan, acos, asin, cosh, sinh and tanh LLVM intrinsics which are mapped to corresponding OpenCL extended instructions.
1 parent a59c801 commit 4a858cf

File tree

3 files changed

+102
-0
lines changed

3 files changed

+102
-0
lines changed

lib/SPIRV/SPIRVUtil.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1846,9 +1846,13 @@ std::string decodeSPIRVTypeName(StringRef Name,
18461846
// Returns true if type(s) and number of elements (if vector) is valid
18471847
bool checkTypeForSPIRVExtendedInstLowering(IntrinsicInst *II, SPIRVModule *BM) {
18481848
switch (II->getIntrinsicID()) {
1849+
case Intrinsic::acos:
1850+
case Intrinsic::asin:
1851+
case Intrinsic::atan:
18491852
case Intrinsic::ceil:
18501853
case Intrinsic::copysign:
18511854
case Intrinsic::cos:
1855+
case Intrinsic::cosh:
18521856
case Intrinsic::exp:
18531857
case Intrinsic::exp2:
18541858
case Intrinsic::fabs:
@@ -1868,8 +1872,10 @@ bool checkTypeForSPIRVExtendedInstLowering(IntrinsicInst *II, SPIRVModule *BM) {
18681872
case Intrinsic::round:
18691873
case Intrinsic::roundeven:
18701874
case Intrinsic::sin:
1875+
case Intrinsic::sinh:
18711876
case Intrinsic::sqrt:
18721877
case Intrinsic::tan:
1878+
case Intrinsic::tanh:
18731879
case Intrinsic::trunc: {
18741880
// Although some of the intrinsics above take multiple arguments, it is
18751881
// sufficient to check arg 0 because the LLVM Verifier will have checked

lib/SPIRV/SPIRVWriter.cpp

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3874,12 +3874,20 @@ static SPIRVWord getBuiltinIdForIntrinsic(Intrinsic::ID IID) {
38743874
// and assume that the operations have no side effects (FP status flags
38753875
// aren't maintained), so the OpenCL builtin behavior should be
38763876
// acceptable.
3877+
case Intrinsic::acos:
3878+
return OpenCLLIB::Acos;
3879+
case Intrinsic::asin:
3880+
return OpenCLLIB::Asin;
3881+
case Intrinsic::atan:
3882+
return OpenCLLIB::Atan;
38773883
case Intrinsic::ceil:
38783884
return OpenCLLIB::Ceil;
38793885
case Intrinsic::copysign:
38803886
return OpenCLLIB::Copysign;
38813887
case Intrinsic::cos:
38823888
return OpenCLLIB::Cos;
3889+
case Intrinsic::cosh:
3890+
return OpenCLLIB::Cosh;
38833891
case Intrinsic::exp:
38843892
return OpenCLLIB::Exp;
38853893
case Intrinsic::exp2:
@@ -3920,10 +3928,14 @@ static SPIRVWord getBuiltinIdForIntrinsic(Intrinsic::ID IID) {
39203928
return OpenCLLIB::Rint;
39213929
case Intrinsic::sin:
39223930
return OpenCLLIB::Sin;
3931+
case Intrinsic::sinh:
3932+
return OpenCLLIB::Sinh;
39233933
case Intrinsic::sqrt:
39243934
return OpenCLLIB::Sqrt;
39253935
case Intrinsic::tan:
39263936
return OpenCLLIB::Tan;
3937+
case Intrinsic::tanh:
3938+
return OpenCLLIB::Tanh;
39273939
case Intrinsic::trunc:
39283940
return OpenCLLIB::Trunc;
39293941
default:
@@ -4056,8 +4068,12 @@ SPIRVValue *LLVMToSPIRVBase::transIntrinsicInst(IntrinsicInst *II,
40564068
}
40574069

40584070
// Unary FP intrinsic
4071+
case Intrinsic::acos:
4072+
case Intrinsic::asin:
4073+
case Intrinsic::atan:
40594074
case Intrinsic::ceil:
40604075
case Intrinsic::cos:
4076+
case Intrinsic::cosh:
40614077
case Intrinsic::exp:
40624078
case Intrinsic::exp2:
40634079
case Intrinsic::fabs:
@@ -4070,8 +4086,10 @@ SPIRVValue *LLVMToSPIRVBase::transIntrinsicInst(IntrinsicInst *II,
40704086
case Intrinsic::round:
40714087
case Intrinsic::roundeven:
40724088
case Intrinsic::sin:
4089+
case Intrinsic::sinh:
40734090
case Intrinsic::sqrt:
40744091
case Intrinsic::tan:
4092+
case Intrinsic::tanh:
40754093
case Intrinsic::trunc: {
40764094
if (!checkTypeForSPIRVExtendedInstLowering(II, BM))
40774095
break;

test/llvm-intrinsics/fp-intrinsics.ll

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -344,3 +344,81 @@ entry:
344344
}
345345

346346
declare float @llvm.fma.f32(float, float, float)
347+
348+
; CHECK: Function
349+
; CHECK: FunctionParameter {{[0-9]+}} [[x:[0-9]+]]
350+
; CHECK: ExtInst [[var1]] {{[0-9]+}} [[extinst_id]] acos [[x]]
351+
; CHECK: FunctionEnd
352+
353+
define spir_func float @TestAcos(float %x) {
354+
entry:
355+
%t = tail call float @llvm.acos.f32(float %x)
356+
ret float %t
357+
}
358+
359+
declare float @llvm.acos.f32(float)
360+
361+
; CHECK: Function
362+
; CHECK: FunctionParameter {{[0-9]+}} [[x:[0-9]+]]
363+
; CHECK: ExtInst [[var1]] {{[0-9]+}} [[extinst_id]] asin [[x]]
364+
; CHECK: FunctionEnd
365+
366+
define spir_func float @TestAsin(float %x) {
367+
entry:
368+
%t = tail call float @llvm.asin.f32(float %x)
369+
ret float %t
370+
}
371+
372+
declare float @llvm.asin.f32(float)
373+
374+
; CHECK: Function
375+
; CHECK: FunctionParameter {{[0-9]+}} [[x:[0-9]+]]
376+
; CHECK: ExtInst [[var1]] {{[0-9]+}} [[extinst_id]] atan [[x]]
377+
; CHECK: FunctionEnd
378+
379+
define spir_func float @TestAtan(float %x) {
380+
entry:
381+
%t = tail call float @llvm.atan.f32(float %x)
382+
ret float %t
383+
}
384+
385+
declare float @llvm.atan.f32(float)
386+
387+
; CHECK: Function
388+
; CHECK: FunctionParameter {{[0-9]+}} [[x:[0-9]+]]
389+
; CHECK: ExtInst [[var1]] {{[0-9]+}} [[extinst_id]] cosh [[x]]
390+
; CHECK: FunctionEnd
391+
392+
define spir_func float @TestCosh(float %x) {
393+
entry:
394+
%t = tail call float @llvm.cosh.f32(float %x)
395+
ret float %t
396+
}
397+
398+
declare float @llvm.cosh.f32(float)
399+
400+
; CHECK: Function
401+
; CHECK: FunctionParameter {{[0-9]+}} [[x:[0-9]+]]
402+
; CHECK: ExtInst [[var1]] {{[0-9]+}} [[extinst_id]] sinh [[x]]
403+
; CHECK: FunctionEnd
404+
405+
define spir_func float @TestSinh(float %x) {
406+
entry:
407+
%t = tail call float @llvm.sinh.f32(float %x)
408+
ret float %t
409+
}
410+
411+
declare float @llvm.sinh.f32(float)
412+
413+
; CHECK: Function
414+
; CHECK: FunctionParameter {{[0-9]+}} [[x:[0-9]+]]
415+
; CHECK: ExtInst [[var1]] {{[0-9]+}} [[extinst_id]] tanh [[x]]
416+
; CHECK: FunctionEnd
417+
418+
define spir_func float @TestTanh(float %x) {
419+
entry:
420+
%t = tail call float @llvm.tanh.f32(float %x)
421+
ret float %t
422+
}
423+
424+
declare float @llvm.tanh.f32(float)

0 commit comments

Comments
 (0)