Skip to content

Commit 12d3ab7

Browse files
committed
The radian dxil intrinsic lowering working
1 parent e379b4b commit 12d3ab7

File tree

3 files changed

+97
-0
lines changed

3 files changed

+97
-0
lines changed

llvm/include/llvm/IR/IntrinsicsDirectX.td

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,4 +85,5 @@ def int_dx_rsqrt : DefaultAttrsIntrinsic<[llvm_anyfloat_ty], [LLVMMatchType<0>]
8585
def int_dx_wave_is_first_lane : DefaultAttrsIntrinsic<[llvm_i1_ty], [], [IntrConvergent]>;
8686
def int_dx_sign : DefaultAttrsIntrinsic<[LLVMScalarOrSameVectorWidth<0, llvm_i32_ty>], [llvm_any_ty], [IntrNoMem]>;
8787
def int_dx_step : DefaultAttrsIntrinsic<[LLVMMatchType<0>], [llvm_anyfloat_ty, LLVMMatchType<0>], [IntrNoMem]>;
88+
def int_dx_radians : DefaultAttrsIntrinsic<[llvm_anyfloat_ty], [LLVMMatchType<0>], [IntrNoMem]>;
8889
}

llvm/lib/Target/DirectX/DXILIntrinsicExpansion.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ static bool isIntrinsicExpansion(Function &F) {
6363
case Intrinsic::dx_udot:
6464
case Intrinsic::dx_sign:
6565
case Intrinsic::dx_step:
66+
case Intrinsic::dx_radians:
6667
return true;
6768
}
6869
return false;
@@ -405,6 +406,16 @@ static Value *expandStepIntrinsic(CallInst *Orig) {
405406
return Builder.CreateSelect(Cond, Zero, One);
406407
}
407408

409+
static Value *expandRadiansIntrinsic(CallInst *Orig) {
410+
Value *X = Orig->getOperand(0);
411+
Type *Ty = X->getType();
412+
IRBuilder<> Builder(Orig);
413+
414+
Value *OneEightyOverPi = ConstantFP::get(Ty, llvm::numbers::pi / 180.0);
415+
return Builder.CreateFMul(X, OneEightyOverPi);
416+
}
417+
418+
408419
static Intrinsic::ID getMaxForClamp(Type *ElemTy,
409420
Intrinsic::ID ClampIntrinsic) {
410421
if (ClampIntrinsic == Intrinsic::dx_uclamp)
@@ -521,6 +532,9 @@ static bool expandIntrinsic(Function &F, CallInst *Orig) {
521532
break;
522533
case Intrinsic::dx_step:
523534
Result = expandStepIntrinsic(Orig);
535+
case Intrinsic::dx_radians:
536+
Result = expandRadiansIntrinsic(Orig);
537+
break;
524538
}
525539
if (Result) {
526540
Orig->replaceAllUsesWith(Result);

llvm/test/CodeGen/DirectX/radians.ll

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --version 5
2+
; RUN: opt -S -dxil-op-lower -mtriple=dxil-pc-shadermodel6.3-library %s | FileCheck %s
3+
4+
declare half @llvm.dx.radians.f16(half)
5+
declare float @llvm.dx.radians.f32(float)
6+
declare double @llvm.dx.radians.f64(double)
7+
8+
declare <4 x half> @llvm.dx.radians.v4f16(<4 x half>)
9+
declare <4 x float> @llvm.dx.radians.v4f32(<4 x float>)
10+
declare <4 x double> @llvm.dx.radians.v4f64(<4 x double>)
11+
12+
define noundef half @radians_half(half noundef %a) {
13+
; CHECK-LABEL: define noundef half @radians_half(
14+
; CHECK-SAME: half noundef [[A:%.*]]) {
15+
; CHECK-NEXT: [[ENTRY:.*:]]
16+
; CHECK-NEXT: [[TMP0:%.*]] = fmul half [[A]], 0xH2478
17+
; CHECK-NEXT: ret half [[TMP0]]
18+
;
19+
entry:
20+
%elt.radians = call half @llvm.dx.radians.f16(half %a)
21+
ret half %elt.radians
22+
}
23+
24+
define noundef float @radians_float(float noundef %a) {
25+
; CHECK-LABEL: define noundef float @radians_float(
26+
; CHECK-SAME: float noundef [[A:%.*]]) {
27+
; CHECK-NEXT: [[ENTRY:.*:]]
28+
; CHECK-NEXT: [[TMP0:%.*]] = fmul float [[A]], 0x3F91DF46A0000000
29+
; CHECK-NEXT: ret float [[TMP0]]
30+
;
31+
entry:
32+
%elt.radians = call float @llvm.dx.radians.f32(float %a)
33+
ret float %elt.radians
34+
}
35+
36+
define noundef double @radians_double(double noundef %a) {
37+
; CHECK-LABEL: define noundef double @radians_double(
38+
; CHECK-SAME: double noundef [[A:%.*]]) {
39+
; CHECK-NEXT: [[ENTRY:.*:]]
40+
; CHECK-NEXT: [[TMP0:%.*]] = fmul double [[A]], 0x3F91DF46A2529D39
41+
; CHECK-NEXT: ret double [[TMP0]]
42+
;
43+
entry:
44+
%elt.radians = call double @llvm.dx.radians.f64(double %a)
45+
ret double %elt.radians
46+
}
47+
48+
define noundef <4 x half> @radians_half_vector(<4 x half> noundef %a) {
49+
; CHECK-LABEL: define noundef <4 x half> @radians_half_vector(
50+
; CHECK-SAME: <4 x half> noundef [[A:%.*]]) {
51+
; CHECK-NEXT: [[ENTRY:.*:]]
52+
; CHECK-NEXT: [[TMP0:%.*]] = fmul <4 x half> [[A]], <half 0xH2478, half 0xH2478, half 0xH2478, half 0xH2478>
53+
; CHECK-NEXT: ret <4 x half> [[TMP0]]
54+
;
55+
entry:
56+
%elt.radians = call <4 x half> @llvm.dx.radians.v4f16(<4 x half> %a)
57+
ret <4 x half> %elt.radians
58+
}
59+
60+
define noundef <4 x float> @radians_float_vector(<4 x float> noundef %a) {
61+
; CHECK-LABEL: define noundef <4 x float> @radians_float_vector(
62+
; CHECK-SAME: <4 x float> noundef [[A:%.*]]) {
63+
; CHECK-NEXT: [[ENTRY:.*:]]
64+
; CHECK-NEXT: [[TMP0:%.*]] = fmul <4 x float> [[A]], <float 0x3F91DF46A0000000, float 0x3F91DF46A0000000, float 0x3F91DF46A0000000, float 0x3F91DF46A0000000>
65+
; CHECK-NEXT: ret <4 x float> [[TMP0]]
66+
;
67+
entry:
68+
%elt.radians = call <4 x float> @llvm.dx.radians.v4f32(<4 x float> %a)
69+
ret <4 x float> %elt.radians
70+
}
71+
72+
define noundef <4 x double> @radians_double_vector(<4 x double> noundef %a) {
73+
; CHECK-LABEL: define noundef <4 x double> @radians_double_vector(
74+
; CHECK-SAME: <4 x double> noundef [[A:%.*]]) {
75+
; CHECK-NEXT: [[ENTRY:.*:]]
76+
; CHECK-NEXT: [[TMP0:%.*]] = fmul <4 x double> [[A]], <double 0x3F91DF46A2529D39, double 0x3F91DF46A2529D39, double 0x3F91DF46A2529D39, double 0x3F91DF46A2529D39>
77+
; CHECK-NEXT: ret <4 x double> [[TMP0]]
78+
;
79+
entry:
80+
%elt.radians = call <4 x double> @llvm.dx.radians.v4f64(<4 x double> %a)
81+
ret <4 x double> %elt.radians
82+
}

0 commit comments

Comments
 (0)