Skip to content

Commit 3323c74

Browse files
authored
Translate llvm.maxnum intrinsic function (#2299)
Map to fmax SPIR-V instruction from OpenCL extended instruction set. Cherry-pick of KhronosGroup/SPIRV-LLVM-Translator@380c429.
1 parent 4688cb3 commit 3323c74

File tree

3 files changed

+33
-1
lines changed

3 files changed

+33
-1
lines changed

llvm-spirv/lib/SPIRV/SPIRVUtil.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1509,7 +1509,8 @@ bool hasLoopMetadata(const Module *M) {
15091509
bool checkTypeForSPIRVExtendedInstLowering(IntrinsicInst *II, SPIRVModule *BM) {
15101510
switch (II->getIntrinsicID()) {
15111511
case Intrinsic::fabs:
1512-
case Intrinsic::ceil: {
1512+
case Intrinsic::ceil:
1513+
case Intrinsic::maxnum: {
15131514
Type *Ty = II->getType();
15141515
if (II->getArgOperand(0)->getType() != Ty)
15151516
return false;

llvm-spirv/lib/SPIRV/SPIRVWriter.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2041,6 +2041,16 @@ SPIRVValue *LLVMToSPIRV::transIntrinsicInst(IntrinsicInst *II,
20412041
return BM->addBinaryInst(OpFAdd, Ty, Mul,
20422042
transValue(II->getArgOperand(2), BB), BB);
20432043
}
2044+
case Intrinsic::maxnum: {
2045+
if (!checkTypeForSPIRVExtendedInstLowering(II, BM))
2046+
break;
2047+
SPIRVWord ExtOp = OpenCLLIB::Fmax;
2048+
SPIRVType *STy = transType(II->getType());
2049+
std::vector<SPIRVValue *> Ops{transValue(II->getArgOperand(0), BB),
2050+
transValue(II->getArgOperand(1), BB)};
2051+
return BM->addExtInst(STy, BM->getExtInstSetId(SPIRVEIS_OpenCL), ExtOp, Ops,
2052+
BB);
2053+
}
20442054
case Intrinsic::usub_sat: {
20452055
// usub.sat(a, b) -> (a > b) ? a - b : 0
20462056
SPIRVType *Ty = transType(II->getType());

llvm-spirv/test/maxnum.ll

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
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-i64:64-v16:16-v24:32-v32:32-v48:64-v96:128-v192:256-v256:256-v512:512-v1024:1024"
7+
target triple = "spir64-unknown-unknown"
8+
9+
define spir_func float @Test(float %x, float %y) {
10+
entry:
11+
%0 = call float @llvm.maxnum.f32(float %x, float %y)
12+
ret float %0
13+
}
14+
15+
; CHECK: Function
16+
; CHECK: FunctionParameter {{[0-9]+}} [[x:[0-9]+]]
17+
; CHECK: FunctionParameter {{[0-9]+}} [[y:[0-9]+]]
18+
; CHECK: ExtInst {{[0-9]+}} [[res:[0-9]+]] {{[0-9]+}} fmax [[x]] [[y]]
19+
; CHECK: ReturnValue [[res]]
20+
21+
declare float @llvm.maxnum.f32(float, float)

0 commit comments

Comments
 (0)