[llvm] [DXIL] Add radians intrinsic (PR #110616)

Adam Yang via llvm-commits llvm-commits at lists.llvm.org
Wed Oct 2 00:38:33 PDT 2024


https://github.com/adam-yang updated https://github.com/llvm/llvm-project/pull/110616

>From 15a9b9f7c583aa2c4786bf329fb5dfa6640f764a Mon Sep 17 00:00:00 2001
From: Adam Yang <hanbyang at microsoft.com>
Date: Mon, 30 Sep 2024 18:23:54 -0700
Subject: [PATCH 1/2] The radian dxil intrinsic lowering working

---
 llvm/include/llvm/IR/IntrinsicsDirectX.td     |  1 +
 .../Target/DirectX/DXILIntrinsicExpansion.cpp | 14 ++++
 llvm/test/CodeGen/DirectX/radians.ll          | 82 +++++++++++++++++++
 3 files changed, 97 insertions(+)
 create mode 100644 llvm/test/CodeGen/DirectX/radians.ll

diff --git a/llvm/include/llvm/IR/IntrinsicsDirectX.td b/llvm/include/llvm/IR/IntrinsicsDirectX.td
index 555877e7aaf0e5..f9acddf17f8c17 100644
--- a/llvm/include/llvm/IR/IntrinsicsDirectX.td
+++ b/llvm/include/llvm/IR/IntrinsicsDirectX.td
@@ -85,4 +85,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 926cbe97f24fda..e53290d2c5cb03 100644
--- a/llvm/lib/Target/DirectX/DXILIntrinsicExpansion.cpp
+++ b/llvm/lib/Target/DirectX/DXILIntrinsicExpansion.cpp
@@ -53,6 +53,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;
@@ -395,6 +396,16 @@ 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 *OneEightyOverPi = ConstantFP::get(Ty, llvm::numbers::pi / 180.0);
+  return Builder.CreateFMul(X, OneEightyOverPi);
+}
+
+
 static Intrinsic::ID getMaxForClamp(Type *ElemTy,
                                     Intrinsic::ID ClampIntrinsic) {
   if (ClampIntrinsic == Intrinsic::dx_uclamp)
@@ -511,6 +522,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..d17a06527df311
--- /dev/null
+++ b/llvm/test/CodeGen/DirectX/radians.ll
@@ -0,0 +1,82 @@
+; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --version 5
+; RUN: opt -S  -dxil-op-lower  -mtriple=dxil-pc-shadermodel6.3-library %s | FileCheck %s
+
+declare half @llvm.dx.radians.f16(half)
+declare float @llvm.dx.radians.f32(float)
+declare double @llvm.dx.radians.f64(double)
+
+declare <4 x half> @llvm.dx.radians.v4f16(<4 x half>)
+declare <4 x float> @llvm.dx.radians.v4f32(<4 x float>)
+declare <4 x double> @llvm.dx.radians.v4f64(<4 x double>)
+
+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 double @radians_double(double noundef %a) {
+; CHECK-LABEL: define noundef double @radians_double(
+; CHECK-SAME: double noundef [[A:%.*]]) {
+; CHECK-NEXT:  [[ENTRY:.*:]]
+; CHECK-NEXT:    [[TMP0:%.*]] = fmul double [[A]], 0x3F91DF46A2529D39
+; CHECK-NEXT:    ret double [[TMP0]]
+;
+entry:
+  %elt.radians = call double @llvm.dx.radians.f64(double %a)
+  ret double %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-NEXT:    [[TMP0:%.*]] = fmul <4 x half> [[A]], <half 0xH2478, half 0xH2478, half 0xH2478, half 0xH2478>
+; CHECK-NEXT:    ret <4 x half> [[TMP0]]
+;
+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-NEXT:    [[TMP0:%.*]] = fmul <4 x float> [[A]], <float 0x3F91DF46A0000000, float 0x3F91DF46A0000000, float 0x3F91DF46A0000000, float 0x3F91DF46A0000000>
+; CHECK-NEXT:    ret <4 x float> [[TMP0]]
+;
+entry:
+  %elt.radians = call <4 x float> @llvm.dx.radians.v4f32(<4 x float> %a)
+  ret <4 x float> %elt.radians
+}
+
+define noundef <4 x double> @radians_double_vector(<4 x double> noundef %a) {
+; CHECK-LABEL: define noundef <4 x double> @radians_double_vector(
+; CHECK-SAME: <4 x double> noundef [[A:%.*]]) {
+; CHECK-NEXT:  [[ENTRY:.*:]]
+; CHECK-NEXT:    [[TMP0:%.*]] = fmul <4 x double> [[A]], <double 0x3F91DF46A2529D39, double 0x3F91DF46A2529D39, double 0x3F91DF46A2529D39, double 0x3F91DF46A2529D39>
+; CHECK-NEXT:    ret <4 x double> [[TMP0]]
+;
+entry:
+  %elt.radians = call <4 x double> @llvm.dx.radians.v4f64(<4 x double> %a)
+  ret <4 x double> %elt.radians
+}

>From 29f7fa0ed8be07c1f776d21f07ec4d0c46fd2a38 Mon Sep 17 00:00:00 2001
From: Adam Yang <hanbyang at microsoft.com>
Date: Wed, 2 Oct 2024 00:37:50 -0700
Subject: [PATCH 2/2] No more double support

---
 .../Target/DirectX/DXILIntrinsicExpansion.cpp |  1 -
 llvm/test/CodeGen/DirectX/radians.ll          | 26 -------------------
 2 files changed, 27 deletions(-)

diff --git a/llvm/lib/Target/DirectX/DXILIntrinsicExpansion.cpp b/llvm/lib/Target/DirectX/DXILIntrinsicExpansion.cpp
index e53290d2c5cb03..33d1a12bd7c75a 100644
--- a/llvm/lib/Target/DirectX/DXILIntrinsicExpansion.cpp
+++ b/llvm/lib/Target/DirectX/DXILIntrinsicExpansion.cpp
@@ -400,7 +400,6 @@ static Value *expandRadiansIntrinsic(CallInst *Orig) {
   Value *X = Orig->getOperand(0);
   Type *Ty = X->getType();
   IRBuilder<> Builder(Orig);
-
   Value *OneEightyOverPi = ConstantFP::get(Ty, llvm::numbers::pi / 180.0);
   return Builder.CreateFMul(X, OneEightyOverPi);
 }
diff --git a/llvm/test/CodeGen/DirectX/radians.ll b/llvm/test/CodeGen/DirectX/radians.ll
index d17a06527df311..3d7fdb3b090093 100644
--- a/llvm/test/CodeGen/DirectX/radians.ll
+++ b/llvm/test/CodeGen/DirectX/radians.ll
@@ -3,11 +3,9 @@
 
 declare half @llvm.dx.radians.f16(half)
 declare float @llvm.dx.radians.f32(float)
-declare double @llvm.dx.radians.f64(double)
 
 declare <4 x half> @llvm.dx.radians.v4f16(<4 x half>)
 declare <4 x float> @llvm.dx.radians.v4f32(<4 x float>)
-declare <4 x double> @llvm.dx.radians.v4f64(<4 x double>)
 
 define noundef half @radians_half(half noundef %a) {
 ; CHECK-LABEL: define noundef half @radians_half(
@@ -33,18 +31,6 @@ entry:
   ret float %elt.radians
 }
 
-define noundef double @radians_double(double noundef %a) {
-; CHECK-LABEL: define noundef double @radians_double(
-; CHECK-SAME: double noundef [[A:%.*]]) {
-; CHECK-NEXT:  [[ENTRY:.*:]]
-; CHECK-NEXT:    [[TMP0:%.*]] = fmul double [[A]], 0x3F91DF46A2529D39
-; CHECK-NEXT:    ret double [[TMP0]]
-;
-entry:
-  %elt.radians = call double @llvm.dx.radians.f64(double %a)
-  ret double %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:%.*]]) {
@@ -68,15 +54,3 @@ entry:
   %elt.radians = call <4 x float> @llvm.dx.radians.v4f32(<4 x float> %a)
   ret <4 x float> %elt.radians
 }
-
-define noundef <4 x double> @radians_double_vector(<4 x double> noundef %a) {
-; CHECK-LABEL: define noundef <4 x double> @radians_double_vector(
-; CHECK-SAME: <4 x double> noundef [[A:%.*]]) {
-; CHECK-NEXT:  [[ENTRY:.*:]]
-; CHECK-NEXT:    [[TMP0:%.*]] = fmul <4 x double> [[A]], <double 0x3F91DF46A2529D39, double 0x3F91DF46A2529D39, double 0x3F91DF46A2529D39, double 0x3F91DF46A2529D39>
-; CHECK-NEXT:    ret <4 x double> [[TMP0]]
-;
-entry:
-  %elt.radians = call <4 x double> @llvm.dx.radians.v4f64(<4 x double> %a)
-  ret <4 x double> %elt.radians
-}



More information about the llvm-commits mailing list