Skip to content

Commit ced51b9

Browse files
committed
[DXIL] Add sign intrinsic part 2
1 parent 04f6504 commit ced51b9

File tree

2 files changed

+35
-0
lines changed

2 files changed

+35
-0
lines changed

llvm/include/llvm/IR/IntrinsicsDirectX.td

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,4 +60,5 @@ def int_dx_imad : DefaultAttrsIntrinsic<[llvm_anyint_ty], [LLVMMatchType<0>, LLV
6060
def int_dx_umad : DefaultAttrsIntrinsic<[llvm_anyint_ty], [LLVMMatchType<0>, LLVMMatchType<0>, LLVMMatchType<0>]>;
6161
def int_dx_rcp : DefaultAttrsIntrinsic<[llvm_anyfloat_ty], [LLVMMatchType<0>]>;
6262
def int_dx_rsqrt : DefaultAttrsIntrinsic<[llvm_anyfloat_ty], [LLVMMatchType<0>]>;
63+
def int_dx_sign : DefaultAttrsIntrinsic<[LLVMScalarOrSameVectorWidth<0, llvm_i32_ty>], [llvm_any_ty]>;
6364
}

llvm/lib/Target/DirectX/DXILIntrinsicExpansion.cpp

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
#include "llvm/ADT/SmallVector.h"
1717
#include "llvm/CodeGen/Passes.h"
1818
#include "llvm/IR/IRBuilder.h"
19+
#include "llvm/IR/InstrTypes.h"
1920
#include "llvm/IR/Instruction.h"
2021
#include "llvm/IR/Instructions.h"
2122
#include "llvm/IR/Intrinsics.h"
@@ -45,6 +46,7 @@ static bool isIntrinsicExpansion(Function &F) {
4546
case Intrinsic::dx_length:
4647
case Intrinsic::dx_sdot:
4748
case Intrinsic::dx_udot:
49+
case Intrinsic::dx_sign:
4850
return true;
4951
}
5052
return false;
@@ -293,6 +295,36 @@ static bool expandClampIntrinsic(CallInst *Orig, Intrinsic::ID ClampIntrinsic) {
293295
return true;
294296
}
295297

298+
static bool expandSignIntrinsic(CallInst *Orig) {
299+
IRBuilder<> Builder(Orig->getParent());
300+
Value *X = Orig->getOperand(0);
301+
Type *Ty = X->getType();
302+
Type *ScalarTy = Ty->getScalarType();
303+
Type *RetTy = Orig->getType();
304+
Constant *Zero = Constant::getNullValue(Ty);
305+
Builder.SetInsertPoint(Orig);
306+
307+
Value *GT;
308+
Value *LT;
309+
if (ScalarTy->isFloatingPointTy()) {
310+
GT = Builder.CreateFCmpOLT(Zero, X);
311+
LT = Builder.CreateFCmpOLT(X, Zero);
312+
} else {
313+
assert(ScalarTy->isIntegerTy());
314+
GT = Builder.CreateICmpSLT(Zero, X);
315+
LT = Builder.CreateICmpSLT(X, Zero);
316+
}
317+
318+
Value *ZextGT = Builder.CreateZExt(GT, RetTy);
319+
Value *ZextLT = Builder.CreateZExt(LT, RetTy);
320+
321+
Value *Ret = Builder.CreateSub(ZextGT, ZextLT);
322+
323+
Orig->replaceAllUsesWith(Ret);
324+
Orig->eraseFromParent();
325+
return true;
326+
}
327+
296328
static bool expandIntrinsic(Function &F, CallInst *Orig) {
297329
switch (F.getIntrinsicID()) {
298330
case Intrinsic::abs:
@@ -317,6 +349,8 @@ static bool expandIntrinsic(Function &F, CallInst *Orig) {
317349
case Intrinsic::dx_sdot:
318350
case Intrinsic::dx_udot:
319351
return expandIntegerDot(Orig, F.getIntrinsicID());
352+
case Intrinsic::dx_sign:
353+
return expandSignIntrinsic(Orig);
320354
}
321355
return false;
322356
}

0 commit comments

Comments
 (0)