Skip to content

Commit 18fc8cb

Browse files
farzonlvar-const
authored andcommitted
[DirectX] legalize powi (llvm#135228)
fixes llvm#135221 - have powi use the same legalization path as pow - use CreateSIToFP to cast the int back to a float type - add tests for powi
1 parent 25cbac3 commit 18fc8cb

File tree

2 files changed

+35
-4
lines changed

2 files changed

+35
-4
lines changed

llvm/lib/Target/DirectX/DXILIntrinsicExpansion.cpp

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ static bool isIntrinsicExpansion(Function &F) {
4949
case Intrinsic::log:
5050
case Intrinsic::log10:
5151
case Intrinsic::pow:
52+
case Intrinsic::powi:
5253
case Intrinsic::dx_all:
5354
case Intrinsic::dx_any:
5455
case Intrinsic::dx_cross:
@@ -251,7 +252,7 @@ static Value *expandExpIntrinsic(CallInst *Orig) {
251252
}
252253

253254
static Value *expandAnyOrAllIntrinsic(CallInst *Orig,
254-
Intrinsic::ID intrinsicId) {
255+
Intrinsic::ID IntrinsicId) {
255256
Value *X = Orig->getOperand(0);
256257
IRBuilder<> Builder(Orig);
257258
Type *Ty = X->getType();
@@ -285,7 +286,7 @@ static Value *expandAnyOrAllIntrinsic(CallInst *Orig,
285286
Result = Builder.CreateExtractElement(Cond, (uint64_t)0);
286287
for (unsigned I = 1; I < XVec->getNumElements(); I++) {
287288
Value *Elt = Builder.CreateExtractElement(Cond, I);
288-
Result = ApplyOp(intrinsicId, Result, Elt);
289+
Result = ApplyOp(IntrinsicId, Result, Elt);
289290
}
290291
}
291292
return Result;
@@ -410,13 +411,16 @@ static Value *expandAtan2Intrinsic(CallInst *Orig) {
410411
return Result;
411412
}
412413

413-
static Value *expandPowIntrinsic(CallInst *Orig) {
414+
static Value *expandPowIntrinsic(CallInst *Orig, Intrinsic::ID IntrinsicId) {
414415

415416
Value *X = Orig->getOperand(0);
416417
Value *Y = Orig->getOperand(1);
417418
Type *Ty = X->getType();
418419
IRBuilder<> Builder(Orig);
419420

421+
if (IntrinsicId == Intrinsic::powi)
422+
Y = Builder.CreateSIToFP(Y, Ty);
423+
420424
auto *Log2Call =
421425
Builder.CreateIntrinsic(Ty, Intrinsic::log2, {X}, nullptr, "elt.log2");
422426
auto *Mul = Builder.CreateFMul(Log2Call, Y);
@@ -542,7 +546,8 @@ static bool expandIntrinsic(Function &F, CallInst *Orig) {
542546
Result = expandLog10Intrinsic(Orig);
543547
break;
544548
case Intrinsic::pow:
545-
Result = expandPowIntrinsic(Orig);
549+
case Intrinsic::powi:
550+
Result = expandPowIntrinsic(Orig, IntrinsicId);
546551
break;
547552
case Intrinsic::dx_all:
548553
case Intrinsic::dx_any:

llvm/test/CodeGen/DirectX/pow.ll

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,5 +25,31 @@ entry:
2525
ret half %elt.pow
2626
}
2727

28+
define noundef float @powi_float(float noundef %a, i32 noundef %b) {
29+
entry:
30+
; CHECK: [[CAST:%.*]] = sitofp i32 %b to float
31+
; DOPCHECK: call float @dx.op.unary.f32(i32 23, float %a)
32+
; EXPCHECK: call float @llvm.log2.f32(float %a)
33+
; CHECK: fmul float %{{.*}}, [[CAST]]
34+
; DOPCHECK: call float @dx.op.unary.f32(i32 21, float %{{.*}})
35+
; EXPCHECK: call float @llvm.exp2.f32(float %{{.*}})
36+
%elt.powi = call float @llvm.powi.f32.i32(float %a, i32 %b)
37+
ret float %elt.powi
38+
}
39+
40+
define noundef half @powi_half(half noundef %a, i32 noundef %b) {
41+
entry:
42+
; CHECK: [[CAST:%.*]] = sitofp i32 %b to half
43+
; DOPCHECK: call half @dx.op.unary.f16(i32 23, half %a)
44+
; EXPCHECK: call half @llvm.log2.f16(half %a)
45+
; CHECK: fmul half %{{.*}}, [[CAST]]
46+
; DOPCHECK: call half @dx.op.unary.f16(i32 21, half %{{.*}})
47+
; EXPCHECK: call half @llvm.exp2.f16(half %{{.*}})
48+
%elt.powi = call half @llvm.powi.f16.i32(half %a, i32 %b)
49+
ret half %elt.powi
50+
}
51+
2852
declare half @llvm.pow.f16(half,half)
2953
declare float @llvm.pow.f32(float,float)
54+
declare half @llvm.powi.f16.i32(half,i32)
55+
declare float @llvm.powi.f32.i32(float,i32)

0 commit comments

Comments
 (0)