Skip to content

Commit 9c26b47

Browse files
fadeevalvmaksimo
authored andcommitted
Add nearbyint support (#774)
* Add support for llvm.nearbyint.* intrinsic Signed-off-by: Aleksander Fadeev <aleksander.fadeev@intel.com>
1 parent afa5592 commit 9c26b47

File tree

3 files changed

+38
-1
lines changed

3 files changed

+38
-1
lines changed

llvm-spirv/lib/SPIRV/SPIRVUtil.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1533,7 +1533,8 @@ bool checkTypeForSPIRVExtendedInstLowering(IntrinsicInst *II, SPIRVModule *BM) {
15331533
switch (II->getIntrinsicID()) {
15341534
case Intrinsic::fabs:
15351535
case Intrinsic::ceil:
1536-
case Intrinsic::maxnum: {
1536+
case Intrinsic::maxnum:
1537+
case Intrinsic::nearbyint: {
15371538
Type *Ty = II->getType();
15381539
if (II->getArgOperand(0)->getType() != Ty)
15391540
return false;

llvm-spirv/lib/SPIRV/SPIRVWriter.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1986,6 +1986,7 @@ bool LLVMToSPIRV::isKnownIntrinsic(Intrinsic::ID Id) {
19861986
case Intrinsic::fmuladd:
19871987
case Intrinsic::memset:
19881988
case Intrinsic::memcpy:
1989+
case Intrinsic::nearbyint:
19891990
case Intrinsic::lifetime_start:
19901991
case Intrinsic::lifetime_end:
19911992
case Intrinsic::dbg_declare:
@@ -2316,6 +2317,13 @@ SPIRVValue *LLVMToSPIRV::transIntrinsicInst(IntrinsicInst *II,
23162317
Size = 0;
23172318
return BM->addLifetimeInst(OC, transValue(II->getOperand(1), BB), Size, BB);
23182319
}
2320+
case Intrinsic::nearbyint: {
2321+
if (!checkTypeForSPIRVExtendedInstLowering(II, BM))
2322+
break;
2323+
return BM->addExtInst(transType(II->getType()),
2324+
BM->getExtInstSetId(SPIRVEIS_OpenCL), OpenCLLIB::Rint,
2325+
{transValue(II->getOperand(0), BB)}, BB);
2326+
}
23192327
// We don't want to mix translation of regular code and debug info, because
23202328
// it creates a mess, therefore translation of debug intrinsics is
23212329
// postponed until LLVMToSPIRVDbgTran::finalizeDebug...() methods.

llvm-spirv/test/nearbyint.ll

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
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+
target datalayout = "e-p:32:32-i64:64-v16:16-v24:32-v32:32-v48:64-v96:128-v192:256-v256:256-v512:512-v1024:1024"
7+
target triple = "spir"
8+
9+
; CHECK: ExtInst {{[0-9]+}} {{[0-9]+}} {{[0-9]+}} rint
10+
11+
; Function Attrs: nounwind readnone
12+
define dso_local spir_func float @foo(float %x) local_unnamed_addr #0 {
13+
entry:
14+
%0 = tail call float @llvm.nearbyint.f32(float %x)
15+
ret float %0
16+
}
17+
18+
; Function Attrs: nounwind readnone speculatable willreturn
19+
declare float @llvm.nearbyint.f32(float) #1
20+
21+
attributes #0 = { nounwind readnone "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" "unsafe-fp-math"="false" "use-soft-float"="false" }
22+
attributes #1 = { nounwind readnone speculatable willreturn }
23+
24+
!llvm.module.flags = !{!0}
25+
!llvm.ident = !{!1}
26+
27+
!0 = !{i32 1, !"wchar_size", i32 4}
28+
!1 = !{!"clang version 12.0.0 (https://github.com/llvm/llvm-project.git 68e1a8d20795802077987529e1268c184d749564)"}

0 commit comments

Comments
 (0)