Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[DXIL] Add radians intrinsic (#110616) #113546

Conversation

agozillon
Copy link
Contributor

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

Created using spr 1.3.4
@llvmbot
Copy link
Member

llvmbot commented Oct 24, 2024

@llvm/pr-subscribers-backend-directx

Author: None (agozillon)

Changes

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


Full diff: https://github.com/llvm/llvm-project/pull/113546.diff

3 Files Affected:

  • (modified) llvm/include/llvm/IR/IntrinsicsDirectX.td (+1)
  • (modified) llvm/lib/Target/DirectX/DXILIntrinsicExpansion.cpp (+12)
  • (added) llvm/test/CodeGen/DirectX/radians.ll (+79)
diff --git a/llvm/include/llvm/IR/IntrinsicsDirectX.td b/llvm/include/llvm/IR/IntrinsicsDirectX.td
index 1adfcdc9a1ed2f..f2b9e286ebb476 100644
--- a/llvm/include/llvm/IR/IntrinsicsDirectX.td
+++ b/llvm/include/llvm/IR/IntrinsicsDirectX.td
@@ -86,4 +86,5 @@ def int_dx_rsqrt  : DefaultAttrsIntrinsic<[llvm_anyfloat_ty], [LLVMMatchType<0>]
 def int_dx_wave_is_first_lane : DefaultAttrsIntrinsic<[llvm_i1_ty], [], [IntrConvergent]>;
 def int_dx_sign : DefaultAttrsIntrinsic<[LLVMScalarOrSameVectorWidth<0, llvm_i32_ty>], [llvm_any_ty], [IntrNoMem]>;
 def int_dx_step : DefaultAttrsIntrinsic<[LLVMMatchType<0>], [llvm_anyfloat_ty, LLVMMatchType<0>], [IntrNoMem]>;
+def int_dx_radians : DefaultAttrsIntrinsic<[llvm_anyfloat_ty], [LLVMMatchType<0>], [IntrNoMem]>;
 }
diff --git a/llvm/lib/Target/DirectX/DXILIntrinsicExpansion.cpp b/llvm/lib/Target/DirectX/DXILIntrinsicExpansion.cpp
index 39e73a11d9d7ee..0bcd03c7fad38d 100644
--- a/llvm/lib/Target/DirectX/DXILIntrinsicExpansion.cpp
+++ b/llvm/lib/Target/DirectX/DXILIntrinsicExpansion.cpp
@@ -64,6 +64,7 @@ static bool isIntrinsicExpansion(Function &F) {
   case Intrinsic::dx_udot:
   case Intrinsic::dx_sign:
   case Intrinsic::dx_step:
+  case Intrinsic::dx_radians:
     return true;
   }
   return false;
@@ -442,6 +443,14 @@ static Value *expandStepIntrinsic(CallInst *Orig) {
   return Builder.CreateSelect(Cond, Zero, One);
 }
 
+static Value *expandRadiansIntrinsic(CallInst *Orig) {
+  Value *X = Orig->getOperand(0);
+  Type *Ty = X->getType();
+  IRBuilder<> Builder(Orig);
+  Value *PiOver180 = ConstantFP::get(Ty, llvm::numbers::pi / 180.0);
+  return Builder.CreateFMul(X, PiOver180);
+}
+
 static Intrinsic::ID getMaxForClamp(Type *ElemTy,
                                     Intrinsic::ID ClampIntrinsic) {
   if (ClampIntrinsic == Intrinsic::dx_uclamp)
@@ -561,6 +570,9 @@ static bool expandIntrinsic(Function &F, CallInst *Orig) {
     break;
   case Intrinsic::dx_step:
     Result = expandStepIntrinsic(Orig);
+  case Intrinsic::dx_radians:
+    Result = expandRadiansIntrinsic(Orig);
+    break;
   }
   if (Result) {
     Orig->replaceAllUsesWith(Result);
diff --git a/llvm/test/CodeGen/DirectX/radians.ll b/llvm/test/CodeGen/DirectX/radians.ll
new file mode 100644
index 00000000000000..73ec013775c3e9
--- /dev/null
+++ b/llvm/test/CodeGen/DirectX/radians.ll
@@ -0,0 +1,79 @@
+; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --version 5
+; RUN: opt -S -dxil-intrinsic-expansion -scalarizer -mtriple=dxil-pc-shadermodel6.3-library %s | FileCheck %s
+
+declare half @llvm.dx.radians.f16(half)
+declare float @llvm.dx.radians.f32(float)
+
+declare <4 x half> @llvm.dx.radians.v4f16(<4 x half>)
+declare <4 x float> @llvm.dx.radians.v4f32(<4 x float>)
+
+define noundef half @radians_half(half noundef %a) {
+; CHECK-LABEL: define noundef half @radians_half(
+; CHECK-SAME: half noundef [[A:%.*]]) {
+; CHECK-NEXT:  [[ENTRY:.*:]]
+; CHECK-NEXT:    [[TMP0:%.*]] = fmul half [[A]], 0xH2478
+; CHECK-NEXT:    ret half [[TMP0]]
+;
+entry:
+  %elt.radians = call half @llvm.dx.radians.f16(half %a)
+  ret half %elt.radians
+}
+
+define noundef float @radians_float(float noundef %a) {
+; CHECK-LABEL: define noundef float @radians_float(
+; CHECK-SAME: float noundef [[A:%.*]]) {
+; CHECK-NEXT:  [[ENTRY:.*:]]
+; CHECK-NEXT:    [[TMP0:%.*]] = fmul float [[A]], 0x3F91DF46A0000000
+; CHECK-NEXT:    ret float [[TMP0]]
+;
+entry:
+  %elt.radians = call float @llvm.dx.radians.f32(float %a)
+  ret float %elt.radians
+}
+
+define noundef <4 x half> @radians_half_vector(<4 x half> noundef %a) {
+; CHECK-LABEL: define noundef <4 x half> @radians_half_vector(
+; CHECK-SAME: <4 x half> noundef [[A:%.*]]) {
+; CHECK-NEXT:  [[ENTRY:.*:]]
+; CHECK: [[ee0:%.*]] = extractelement <4 x half> [[A]], i64 0
+; CHECK: [[ie0:%.*]] = fmul half [[ee0]], 0xH2478
+; CHECK: [[ee1:%.*]] = extractelement <4 x half> [[A]], i64 1
+; CHECK: [[ie1:%.*]] = fmul half [[ee1]], 0xH2478
+; CHECK: [[ee2:%.*]] = extractelement <4 x half> [[A]], i64 2
+; CHECK: [[ie2:%.*]] = fmul half [[ee2]], 0xH2478
+; CHECK: [[ee3:%.*]] = extractelement <4 x half> [[A]], i64 3
+; CHECK: [[ie3:%.*]] = fmul half [[ee3]], 0xH2478
+; CHECK: [[TMP0:%.*]] = insertelement <4 x half> poison, half [[ie0]], i64 0
+; CHECK: [[TMP1:%.*]] = insertelement <4 x half> %[[TMP0]], half [[ie1]], i64 1
+; CHECK: [[TMP2:%.*]] = insertelement <4 x half> %[[TMP1]], half [[ie2]], i64 2
+; CHECK: [[TMP3:%.*]] = insertelement <4 x half> %[[TMP2]], half [[ie3]], i64 3
+; CHECK: ret <4 x half> [[TMP3]]
+;
+entry:
+  %elt.radians = call <4 x half> @llvm.dx.radians.v4f16(<4 x half> %a)
+  ret <4 x half> %elt.radians
+}
+
+define noundef <4 x float> @radians_float_vector(<4 x float> noundef %a) {
+; CHECK-LABEL: define noundef <4 x float> @radians_float_vector(
+; CHECK-SAME: <4 x float> noundef [[A:%.*]]) {
+; CHECK-NEXT:  [[ENTRY:.*:]]
+; CHECK: [[ee0:%.*]] = extractelement <4 x float> [[A]], i64 0
+; CHECK: [[ie0:%.*]] = fmul float [[ee0]], 0x3F91DF46A0000000
+; CHECK: [[ee1:%.*]] = extractelement <4 x float> [[A]], i64 1
+; CHECK: [[ie1:%.*]] = fmul float [[ee1]], 0x3F91DF46A0000000
+; CHECK: [[ee2:%.*]] = extractelement <4 x float> [[A]], i64 2
+; CHECK: [[ie2:%.*]] = fmul float [[ee2]], 0x3F91DF46A0000000
+; CHECK: [[ee3:%.*]] = extractelement <4 x float> [[A]], i64 3
+; CHECK: [[ie3:%.*]] = fmul float [[ee3]], 0x3F91DF46A0000000
+; CHECK: [[TMP0:%.*]] = insertelement <4 x float> poison, float [[ie0]], i64 0
+; CHECK: [[TMP1:%.*]] = insertelement <4 x float> %[[TMP0]], float [[ie1]], i64 1
+; CHECK: [[TMP2:%.*]] = insertelement <4 x float> %[[TMP1]], float [[ie2]], i64 2
+; CHECK: [[TMP3:%.*]] = insertelement <4 x float> %[[TMP2]], float [[ie3]], i64 3
+; CHECK: ret <4 x float> [[TMP3]]
+;
+entry:
+  %elt.radians = call <4 x float> @llvm.dx.radians.v4f32(<4 x float> %a)
+  ret <4 x float> %elt.radians
+}
+

@llvmbot
Copy link
Member

llvmbot commented Oct 24, 2024

@llvm/pr-subscribers-llvm-ir

Author: None (agozillon)

Changes

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


Full diff: https://github.com/llvm/llvm-project/pull/113546.diff

3 Files Affected:

  • (modified) llvm/include/llvm/IR/IntrinsicsDirectX.td (+1)
  • (modified) llvm/lib/Target/DirectX/DXILIntrinsicExpansion.cpp (+12)
  • (added) llvm/test/CodeGen/DirectX/radians.ll (+79)
diff --git a/llvm/include/llvm/IR/IntrinsicsDirectX.td b/llvm/include/llvm/IR/IntrinsicsDirectX.td
index 1adfcdc9a1ed2f..f2b9e286ebb476 100644
--- a/llvm/include/llvm/IR/IntrinsicsDirectX.td
+++ b/llvm/include/llvm/IR/IntrinsicsDirectX.td
@@ -86,4 +86,5 @@ def int_dx_rsqrt  : DefaultAttrsIntrinsic<[llvm_anyfloat_ty], [LLVMMatchType<0>]
 def int_dx_wave_is_first_lane : DefaultAttrsIntrinsic<[llvm_i1_ty], [], [IntrConvergent]>;
 def int_dx_sign : DefaultAttrsIntrinsic<[LLVMScalarOrSameVectorWidth<0, llvm_i32_ty>], [llvm_any_ty], [IntrNoMem]>;
 def int_dx_step : DefaultAttrsIntrinsic<[LLVMMatchType<0>], [llvm_anyfloat_ty, LLVMMatchType<0>], [IntrNoMem]>;
+def int_dx_radians : DefaultAttrsIntrinsic<[llvm_anyfloat_ty], [LLVMMatchType<0>], [IntrNoMem]>;
 }
diff --git a/llvm/lib/Target/DirectX/DXILIntrinsicExpansion.cpp b/llvm/lib/Target/DirectX/DXILIntrinsicExpansion.cpp
index 39e73a11d9d7ee..0bcd03c7fad38d 100644
--- a/llvm/lib/Target/DirectX/DXILIntrinsicExpansion.cpp
+++ b/llvm/lib/Target/DirectX/DXILIntrinsicExpansion.cpp
@@ -64,6 +64,7 @@ static bool isIntrinsicExpansion(Function &F) {
   case Intrinsic::dx_udot:
   case Intrinsic::dx_sign:
   case Intrinsic::dx_step:
+  case Intrinsic::dx_radians:
     return true;
   }
   return false;
@@ -442,6 +443,14 @@ static Value *expandStepIntrinsic(CallInst *Orig) {
   return Builder.CreateSelect(Cond, Zero, One);
 }
 
+static Value *expandRadiansIntrinsic(CallInst *Orig) {
+  Value *X = Orig->getOperand(0);
+  Type *Ty = X->getType();
+  IRBuilder<> Builder(Orig);
+  Value *PiOver180 = ConstantFP::get(Ty, llvm::numbers::pi / 180.0);
+  return Builder.CreateFMul(X, PiOver180);
+}
+
 static Intrinsic::ID getMaxForClamp(Type *ElemTy,
                                     Intrinsic::ID ClampIntrinsic) {
   if (ClampIntrinsic == Intrinsic::dx_uclamp)
@@ -561,6 +570,9 @@ static bool expandIntrinsic(Function &F, CallInst *Orig) {
     break;
   case Intrinsic::dx_step:
     Result = expandStepIntrinsic(Orig);
+  case Intrinsic::dx_radians:
+    Result = expandRadiansIntrinsic(Orig);
+    break;
   }
   if (Result) {
     Orig->replaceAllUsesWith(Result);
diff --git a/llvm/test/CodeGen/DirectX/radians.ll b/llvm/test/CodeGen/DirectX/radians.ll
new file mode 100644
index 00000000000000..73ec013775c3e9
--- /dev/null
+++ b/llvm/test/CodeGen/DirectX/radians.ll
@@ -0,0 +1,79 @@
+; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --version 5
+; RUN: opt -S -dxil-intrinsic-expansion -scalarizer -mtriple=dxil-pc-shadermodel6.3-library %s | FileCheck %s
+
+declare half @llvm.dx.radians.f16(half)
+declare float @llvm.dx.radians.f32(float)
+
+declare <4 x half> @llvm.dx.radians.v4f16(<4 x half>)
+declare <4 x float> @llvm.dx.radians.v4f32(<4 x float>)
+
+define noundef half @radians_half(half noundef %a) {
+; CHECK-LABEL: define noundef half @radians_half(
+; CHECK-SAME: half noundef [[A:%.*]]) {
+; CHECK-NEXT:  [[ENTRY:.*:]]
+; CHECK-NEXT:    [[TMP0:%.*]] = fmul half [[A]], 0xH2478
+; CHECK-NEXT:    ret half [[TMP0]]
+;
+entry:
+  %elt.radians = call half @llvm.dx.radians.f16(half %a)
+  ret half %elt.radians
+}
+
+define noundef float @radians_float(float noundef %a) {
+; CHECK-LABEL: define noundef float @radians_float(
+; CHECK-SAME: float noundef [[A:%.*]]) {
+; CHECK-NEXT:  [[ENTRY:.*:]]
+; CHECK-NEXT:    [[TMP0:%.*]] = fmul float [[A]], 0x3F91DF46A0000000
+; CHECK-NEXT:    ret float [[TMP0]]
+;
+entry:
+  %elt.radians = call float @llvm.dx.radians.f32(float %a)
+  ret float %elt.radians
+}
+
+define noundef <4 x half> @radians_half_vector(<4 x half> noundef %a) {
+; CHECK-LABEL: define noundef <4 x half> @radians_half_vector(
+; CHECK-SAME: <4 x half> noundef [[A:%.*]]) {
+; CHECK-NEXT:  [[ENTRY:.*:]]
+; CHECK: [[ee0:%.*]] = extractelement <4 x half> [[A]], i64 0
+; CHECK: [[ie0:%.*]] = fmul half [[ee0]], 0xH2478
+; CHECK: [[ee1:%.*]] = extractelement <4 x half> [[A]], i64 1
+; CHECK: [[ie1:%.*]] = fmul half [[ee1]], 0xH2478
+; CHECK: [[ee2:%.*]] = extractelement <4 x half> [[A]], i64 2
+; CHECK: [[ie2:%.*]] = fmul half [[ee2]], 0xH2478
+; CHECK: [[ee3:%.*]] = extractelement <4 x half> [[A]], i64 3
+; CHECK: [[ie3:%.*]] = fmul half [[ee3]], 0xH2478
+; CHECK: [[TMP0:%.*]] = insertelement <4 x half> poison, half [[ie0]], i64 0
+; CHECK: [[TMP1:%.*]] = insertelement <4 x half> %[[TMP0]], half [[ie1]], i64 1
+; CHECK: [[TMP2:%.*]] = insertelement <4 x half> %[[TMP1]], half [[ie2]], i64 2
+; CHECK: [[TMP3:%.*]] = insertelement <4 x half> %[[TMP2]], half [[ie3]], i64 3
+; CHECK: ret <4 x half> [[TMP3]]
+;
+entry:
+  %elt.radians = call <4 x half> @llvm.dx.radians.v4f16(<4 x half> %a)
+  ret <4 x half> %elt.radians
+}
+
+define noundef <4 x float> @radians_float_vector(<4 x float> noundef %a) {
+; CHECK-LABEL: define noundef <4 x float> @radians_float_vector(
+; CHECK-SAME: <4 x float> noundef [[A:%.*]]) {
+; CHECK-NEXT:  [[ENTRY:.*:]]
+; CHECK: [[ee0:%.*]] = extractelement <4 x float> [[A]], i64 0
+; CHECK: [[ie0:%.*]] = fmul float [[ee0]], 0x3F91DF46A0000000
+; CHECK: [[ee1:%.*]] = extractelement <4 x float> [[A]], i64 1
+; CHECK: [[ie1:%.*]] = fmul float [[ee1]], 0x3F91DF46A0000000
+; CHECK: [[ee2:%.*]] = extractelement <4 x float> [[A]], i64 2
+; CHECK: [[ie2:%.*]] = fmul float [[ee2]], 0x3F91DF46A0000000
+; CHECK: [[ee3:%.*]] = extractelement <4 x float> [[A]], i64 3
+; CHECK: [[ie3:%.*]] = fmul float [[ee3]], 0x3F91DF46A0000000
+; CHECK: [[TMP0:%.*]] = insertelement <4 x float> poison, float [[ie0]], i64 0
+; CHECK: [[TMP1:%.*]] = insertelement <4 x float> %[[TMP0]], float [[ie1]], i64 1
+; CHECK: [[TMP2:%.*]] = insertelement <4 x float> %[[TMP1]], float [[ie2]], i64 2
+; CHECK: [[TMP3:%.*]] = insertelement <4 x float> %[[TMP2]], float [[ie3]], i64 3
+; CHECK: ret <4 x float> [[TMP3]]
+;
+entry:
+  %elt.radians = call <4 x float> @llvm.dx.radians.v4f32(<4 x float> %a)
+  ret <4 x float> %elt.radians
+}
+

@agozillon
Copy link
Contributor Author

Closing as it was an unintentional PR generation via SPR (someone elses PR that's already landed)

@agozillon agozillon closed this Oct 24, 2024
@agozillon agozillon deleted the users/agozillon/dxil-add-radians-intrinsic-110616 branch October 24, 2024 10:51
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Archived in project
Development

Successfully merging this pull request may close these issues.

3 participants