Skip to content

Commit 1e75d08

Browse files
authored
[DXIL] Add radians intrinsic (#110616)
makes progress on #99151 ### Changes - Added int_dx_radians intrinsic in IntrinsicsDirectX.td - Added expansion for int_dx_radians in DXILIntrinsicExpansion.cpp` - Added DXIL backend test case ### Related PRs * [[clang][HLSL] Add radians intrinsic #110802](#110802) * [[SPIRV] Add radians intrinsic #110800](#110800)
1 parent 9144fed commit 1e75d08

File tree

3 files changed

+92
-0
lines changed

3 files changed

+92
-0
lines changed

llvm/include/llvm/IR/IntrinsicsDirectX.td

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

llvm/lib/Target/DirectX/DXILIntrinsicExpansion.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ static bool isIntrinsicExpansion(Function &F) {
6464
case Intrinsic::dx_udot:
6565
case Intrinsic::dx_sign:
6666
case Intrinsic::dx_step:
67+
case Intrinsic::dx_radians:
6768
return true;
6869
}
6970
return false;
@@ -442,6 +443,14 @@ static Value *expandStepIntrinsic(CallInst *Orig) {
442443
return Builder.CreateSelect(Cond, Zero, One);
443444
}
444445

446+
static Value *expandRadiansIntrinsic(CallInst *Orig) {
447+
Value *X = Orig->getOperand(0);
448+
Type *Ty = X->getType();
449+
IRBuilder<> Builder(Orig);
450+
Value *PiOver180 = ConstantFP::get(Ty, llvm::numbers::pi / 180.0);
451+
return Builder.CreateFMul(X, PiOver180);
452+
}
453+
445454
static Intrinsic::ID getMaxForClamp(Type *ElemTy,
446455
Intrinsic::ID ClampIntrinsic) {
447456
if (ClampIntrinsic == Intrinsic::dx_uclamp)
@@ -561,6 +570,9 @@ static bool expandIntrinsic(Function &F, CallInst *Orig) {
561570
break;
562571
case Intrinsic::dx_step:
563572
Result = expandStepIntrinsic(Orig);
573+
case Intrinsic::dx_radians:
574+
Result = expandRadiansIntrinsic(Orig);
575+
break;
564576
}
565577
if (Result) {
566578
Orig->replaceAllUsesWith(Result);

llvm/test/CodeGen/DirectX/radians.ll

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

0 commit comments

Comments
 (0)