[clang] [llvm] [HLSL] Implementation of the frac intrinsic (PR #83315)

Farzon Lotfi via cfe-commits cfe-commits at lists.llvm.org
Wed Feb 28 13:46:28 PST 2024


https://github.com/farzonl updated https://github.com/llvm/llvm-project/pull/83315

>From cd8a27e4571b8e791ce5ccaaf51dd570ee8bc6d3 Mon Sep 17 00:00:00 2001
From: Farzon Lotfi <farzon at farzon.org>
Date: Wed, 28 Feb 2024 13:20:01 -0500
Subject: [PATCH 1/2] [HLSL] Implementation of the frac intrinsic This change
 implements the frontend  for #70099 Builtins.td           - add the frac
 builtin CGBuiltin.cpp         - add the builtin to DirectX intrinsic mapping
 hlsl_intrinsics.h     - add the frac api SemaChecking.cpp      - add type
 checks for builtin IntrinsicsDirectX.td  - add the frac intrinsic

The backend changes for this are going to be very simple:
https://github.com/llvm/llvm-project/commit/f309a0eb558b65dfaff0d1d23b7d07fb07e27121

They were not included because llvm/lib/Target/DirectX/DXIL.td is going
through a major refactor.
---
 clang/include/clang/Basic/Builtins.td         |  6 ++
 clang/lib/CodeGen/CGBuiltin.cpp               |  8 +++
 clang/lib/Headers/hlsl/hlsl_intrinsics.h      | 32 +++++++++
 clang/lib/Sema/SemaChecking.cpp               | 15 ++++
 clang/test/CodeGenHLSL/builtins/frac.hlsl     | 69 +++++++++++++++++++
 clang/test/SemaHLSL/BuiltIns/frac-errors.hlsl | 27 ++++++++
 .../test/SemaHLSL/OverloadResolutionBugs.hlsl | 12 ++++
 llvm/include/llvm/IR/IntrinsicsDirectX.td     |  2 +
 8 files changed, 171 insertions(+)
 create mode 100644 clang/test/CodeGenHLSL/builtins/frac.hlsl
 create mode 100644 clang/test/SemaHLSL/BuiltIns/frac-errors.hlsl

diff --git a/clang/include/clang/Basic/Builtins.td b/clang/include/clang/Basic/Builtins.td
index 3bc35c5bb38ecf..415552d19e0700 100644
--- a/clang/include/clang/Basic/Builtins.td
+++ b/clang/include/clang/Basic/Builtins.td
@@ -4536,6 +4536,12 @@ def HLSLDotProduct : LangBuiltin<"HLSL_LANG"> {
   let Prototype = "void(...)";
 }
 
+def HLSLFrac : LangBuiltin<"HLSL_LANG"> {
+  let Spellings = ["__builtin_hlsl_elementwise_frac"];
+  let Attributes = [NoThrow, Const];
+  let Prototype = "void(...)";
+}
+
 // Builtins for XRay.
 def XRayCustomEvent : Builtin {
   let Spellings = ["__xray_customevent"];
diff --git a/clang/lib/CodeGen/CGBuiltin.cpp b/clang/lib/CodeGen/CGBuiltin.cpp
index 2d16e7cdc06053..fbf34ae94c0af6 100644
--- a/clang/lib/CodeGen/CGBuiltin.cpp
+++ b/clang/lib/CodeGen/CGBuiltin.cpp
@@ -18007,6 +18007,14 @@ Value *CodeGenFunction::EmitHLSLBuiltinExpr(unsigned BuiltinID,
         /*ReturnType*/ T0->getScalarType(), Intrinsic::dx_dot,
         ArrayRef<Value *>{Op0, Op1}, nullptr, "dx.dot");
   } break;
+  case Builtin::BI__builtin_hlsl_elementwise_frac: {
+    Value *Op0 = EmitScalarExpr(E->getArg(0));
+    if (!E->getArg(0)->getType()->hasFloatingRepresentation())
+      llvm_unreachable("frac operand must have a float representation");
+    return Builder.CreateIntrinsic(
+        /*ReturnType*/ Op0->getType(), Intrinsic::dx_frac,
+        ArrayRef<Value *>{Op0}, nullptr, "dx.frac");
+  }
   }
   return nullptr;
 }
diff --git a/clang/lib/Headers/hlsl/hlsl_intrinsics.h b/clang/lib/Headers/hlsl/hlsl_intrinsics.h
index 08e5d981a4a4ca..17b600abdfad1e 100644
--- a/clang/lib/Headers/hlsl/hlsl_intrinsics.h
+++ b/clang/lib/Headers/hlsl/hlsl_intrinsics.h
@@ -317,6 +317,38 @@ double3 floor(double3);
 _HLSL_BUILTIN_ALIAS(__builtin_elementwise_floor)
 double4 floor(double4);
 
+//===----------------------------------------------------------------------===//
+// frac builtins
+//===----------------------------------------------------------------------===//
+
+/// \fn T frac(T x)
+/// \brief Returns the fractional (or decimal) part of x. \a x parameter.
+/// \param x The specified input value.
+///
+/// If \a the return value is greater than or equal to 0 and less than 1.
+
+_HLSL_16BIT_AVAILABILITY(shadermodel, 6.2)
+_HLSL_BUILTIN_ALIAS(__builtin_hlsl_elementwise_frac)
+half frac(half);
+_HLSL_16BIT_AVAILABILITY(shadermodel, 6.2)
+_HLSL_BUILTIN_ALIAS(__builtin_hlsl_elementwise_frac)
+half2 frac(half2);
+_HLSL_16BIT_AVAILABILITY(shadermodel, 6.2)
+_HLSL_BUILTIN_ALIAS(__builtin_hlsl_elementwise_frac)
+half3 frac(half3);
+_HLSL_16BIT_AVAILABILITY(shadermodel, 6.2)
+_HLSL_BUILTIN_ALIAS(__builtin_hlsl_elementwise_frac)
+half4 frac(half4);
+
+_HLSL_BUILTIN_ALIAS(__builtin_hlsl_elementwise_frac)
+float frac(float);
+_HLSL_BUILTIN_ALIAS(__builtin_hlsl_elementwise_frac)
+float2 frac(float2);
+_HLSL_BUILTIN_ALIAS(__builtin_hlsl_elementwise_frac)
+float3 frac(float3);
+_HLSL_BUILTIN_ALIAS(__builtin_hlsl_elementwise_frac)
+float4 frac(float4);
+
 //===----------------------------------------------------------------------===//
 // log builtins
 //===----------------------------------------------------------------------===//
diff --git a/clang/lib/Sema/SemaChecking.cpp b/clang/lib/Sema/SemaChecking.cpp
index 0de76ee119cf81..5583150a2be731 100644
--- a/clang/lib/Sema/SemaChecking.cpp
+++ b/clang/lib/Sema/SemaChecking.cpp
@@ -5247,6 +5247,21 @@ bool Sema::CheckHLSLBuiltinFunctionCall(unsigned BuiltinID, CallExpr *TheCall) {
       return true;
     break;
   }
+  case Builtin::BI__builtin_hlsl_elementwise_frac: {
+    if (PrepareBuiltinElementwiseMathOneArgCall(TheCall))
+      return true;
+    QualType PassedType = TheCall->getArg(0)->getType();
+    if (!PassedType->hasFloatingRepresentation()) {
+      QualType ExpectedType = this->Context.FloatTy;
+      if (auto *VecTyA = PassedType->getAs<VectorType>())
+        ExpectedType = this->Context.getVectorType(
+            ExpectedType, VecTyA->getNumElements(), VecTyA->getVectorKind());
+      Diag(TheCall->getArg(0)->getBeginLoc(),
+           diag::err_typecheck_convert_incompatible)
+          << PassedType << ExpectedType << 1 << 0 << 0;
+      return true;
+    }
+  }
   }
   return false;
 }
diff --git a/clang/test/CodeGenHLSL/builtins/frac.hlsl b/clang/test/CodeGenHLSL/builtins/frac.hlsl
new file mode 100644
index 00000000000000..f2d36adc1ad200
--- /dev/null
+++ b/clang/test/CodeGenHLSL/builtins/frac.hlsl
@@ -0,0 +1,69 @@
+// RUN: %clang_cc1 -finclude-default-header -x hlsl -triple \
+// RUN:   dxil-pc-shadermodel6.3-library %s -fnative-half-type \
+// RUN:   -emit-llvm -disable-llvm-passes -o - | FileCheck %s \ 
+// RUN:   --check-prefixes=CHECK,NATIVE_HALF
+// RUN: %clang_cc1 -finclude-default-header -x hlsl -triple \
+// RUN:   dxil-pc-shadermodel6.3-library %s -emit-llvm -disable-llvm-passes \
+// RUN:   -o - | FileCheck %s --check-prefixes=CHECK,NO_HALF
+
+// NATIVE_HALF: define noundef half @
+// NATIVE_HALF: %dx.frac = call half @llvm.dx.frac.f16(
+// NATIVE_HALF: ret half %dx.frac
+// NO_HALF: define noundef float @"?test_frac_half@@YA$halff@$halff@@Z"(
+// NO_HALF: %dx.frac = call float @llvm.dx.frac.f32(
+// NO_HALF: ret float %dx.frac
+half test_frac_half ( half p0 ) {
+  return frac ( p0 );
+}
+// NATIVE_HALF: define noundef <2 x half> @
+// NATIVE_HALF: %dx.frac = call <2 x half> @llvm.dx.frac.v2f16
+// NATIVE_HALF: ret <2 x half> %dx.frac
+// NO_HALF: define noundef <2 x float> @
+// NO_HALF: %dx.frac = call <2 x float> @llvm.dx.frac.v2f32(
+// NO_HALF: ret <2 x float> %dx.frac
+half2 test_frac_half2 ( half2 p0 ) {
+  return frac ( p0 );
+}
+// NATIVE_HALF: define noundef <3 x half> @
+// NATIVE_HALF: %dx.frac = call <3 x half> @llvm.dx.frac.v3f16
+// NATIVE_HALF: ret <3 x half> %dx.frac
+// NO_HALF: define noundef <3 x float> @
+// NO_HALF: %dx.frac = call <3 x float> @llvm.dx.frac.v3f32(
+// NO_HALF: ret <3 x float> %dx.frac
+half3 test_frac_half3 ( half3 p0 ) {
+  return frac ( p0 );
+}
+// NATIVE_HALF: define noundef <4 x half> @
+// NATIVE_HALF: %dx.frac = call <4 x half> @llvm.dx.frac.v4f16
+// NATIVE_HALF: ret <4 x half> %dx.frac
+// NO_HALF: define noundef <4 x float> @
+// NO_HALF: %dx.frac = call <4 x float> @llvm.dx.frac.v4f32(
+// NO_HALF: ret <4 x float> %dx.frac
+half4 test_frac_half4 ( half4 p0 ) {
+  return frac ( p0 );
+}
+
+// CHECK: define noundef float @
+// CHECK: %dx.frac = call float @llvm.dx.frac.f32(
+// CHECK: ret float %dx.frac
+float test_frac_float ( float p0 ) {
+  return frac ( p0 );
+}
+// CHECK: define noundef <2 x float> @
+// CHECK: %dx.frac = call <2 x float> @llvm.dx.frac.v2f32
+// CHECK: ret <2 x float> %dx.frac
+float2 test_frac_float2 ( float2 p0 ) {
+  return frac ( p0 );
+}
+// CHECK: define noundef <3 x float> @
+// CHECK: %dx.frac = call <3 x float> @llvm.dx.frac.v3f32
+// CHECK: ret <3 x float> %dx.frac
+float3 test_frac_float3 ( float3 p0 ) {
+  return frac ( p0 );
+}
+// CHECK: define noundef <4 x float> @
+// CHECK: %dx.frac = call <4 x float> @llvm.dx.frac.v4f32
+// CHECK: ret <4 x float> %dx.frac
+float4 test_frac_float4 ( float4 p0 ) {
+  return frac ( p0 );
+}
diff --git a/clang/test/SemaHLSL/BuiltIns/frac-errors.hlsl b/clang/test/SemaHLSL/BuiltIns/frac-errors.hlsl
new file mode 100644
index 00000000000000..6e7b98c4a611af
--- /dev/null
+++ b/clang/test/SemaHLSL/BuiltIns/frac-errors.hlsl
@@ -0,0 +1,27 @@
+
+// RUN: %clang_cc1 -finclude-default-header -triple dxil-pc-shadermodel6.6-library %s -fnative-half-type -emit-llvm -disable-llvm-passes -verify -verify-ignore-unexpected
+
+float test_too_few_arg () {
+  return __builtin_hlsl_elementwise_frac ();
+  // expected-error at -1 {{too few arguments to function call, expected 1, have 0}}
+}
+
+float2 test_too_many_arg ( float2 p0) {
+  return __builtin_hlsl_elementwise_frac ( p0, p0);
+  // expected-error at -1 {{too many arguments to function call, expected 1, have 2}}
+}
+
+float builtin_bool_to_float_type_promotion ( bool p1 ) {
+  return __builtin_hlsl_elementwise_frac ( p1 );
+  // expected-error at -1 {{1st argument must be a vector, integer or floating point type (was 'bool')}}
+}
+
+float builtin_frac_int_to_float_promotion (int p1 ) {
+  return __builtin_hlsl_elementwise_frac ( p1 );
+  // expected-error at -1 {{passing 'int' to parameter of incompatible type 'float'}}
+}
+
+float2 builtin_frac_int2_to_float2_promotion (int2 p1 ) {
+  return __builtin_hlsl_elementwise_frac ( p1 );
+  // expected-error at -1 {{passing 'int2' (aka 'vector<int, 2>') to parameter of incompatible type '__attribute__((__vector_size__(2 * sizeof(float)))) float' (vector of 2 'float' values)}}
+}
diff --git a/clang/test/SemaHLSL/OverloadResolutionBugs.hlsl b/clang/test/SemaHLSL/OverloadResolutionBugs.hlsl
index 8464f1c1a7c2cd..de8cdb750079d7 100644
--- a/clang/test/SemaHLSL/OverloadResolutionBugs.hlsl
+++ b/clang/test/SemaHLSL/OverloadResolutionBugs.hlsl
@@ -64,6 +64,18 @@ int64_t test_builtin_dot_vec_int16_to_int64_promotion( int64_t2 p0, int16_t2 p1
   return dot( p0, p1 );
 }
 
+float4 test_frac_int4 ( int4 p0 ) {
+  return frac ( p0 );
+}
+
+float test_frac_int ( int p0 ) {
+  return frac ( p0 );
+}
+
+float test_frac_bool( bool p0 ) {
+  return frac ( p0 );
+}
+
 // https://github.com/llvm/llvm-project/issues/81049
 
 // RUN: %clang_cc1 -std=hlsl2021 -finclude-default-header -x hlsl -triple \
diff --git a/llvm/include/llvm/IR/IntrinsicsDirectX.td b/llvm/include/llvm/IR/IntrinsicsDirectX.td
index c192d4b84417c9..c9bfa533e9a319 100644
--- a/llvm/include/llvm/IR/IntrinsicsDirectX.td
+++ b/llvm/include/llvm/IR/IntrinsicsDirectX.td
@@ -24,4 +24,6 @@ def int_dx_dot :
     Intrinsic<[LLVMVectorElementType<0>], 
     [llvm_anyvector_ty, LLVMScalarOrSameVectorWidth<0, LLVMVectorElementType<0>>],
     [IntrNoMem, IntrWillReturn, Commutative] >;
+
+def int_dx_frac  : DefaultAttrsIntrinsic<[llvm_anyfloat_ty], [LLVMMatchType<0>]>;
 }

>From c18831240f8a17f9501cf732b52f3f2faff31c6d Mon Sep 17 00:00:00 2001
From: Farzon Lotfi <farzon at farzon.org>
Date: Wed, 28 Feb 2024 16:46:04 -0500
Subject: [PATCH 2/2] run clang format on tests

---
 clang/test/CodeGenHLSL/builtins/frac.hlsl     | 32 +++++--------------
 clang/test/SemaHLSL/BuiltIns/frac-errors.hlsl | 20 ++++++------
 2 files changed, 18 insertions(+), 34 deletions(-)

diff --git a/clang/test/CodeGenHLSL/builtins/frac.hlsl b/clang/test/CodeGenHLSL/builtins/frac.hlsl
index f2d36adc1ad200..7c4d1468e96d27 100644
--- a/clang/test/CodeGenHLSL/builtins/frac.hlsl
+++ b/clang/test/CodeGenHLSL/builtins/frac.hlsl
@@ -12,58 +12,42 @@
 // NO_HALF: define noundef float @"?test_frac_half@@YA$halff@$halff@@Z"(
 // NO_HALF: %dx.frac = call float @llvm.dx.frac.f32(
 // NO_HALF: ret float %dx.frac
-half test_frac_half ( half p0 ) {
-  return frac ( p0 );
-}
+half test_frac_half(half p0) { return frac(p0); }
 // NATIVE_HALF: define noundef <2 x half> @
 // NATIVE_HALF: %dx.frac = call <2 x half> @llvm.dx.frac.v2f16
 // NATIVE_HALF: ret <2 x half> %dx.frac
 // NO_HALF: define noundef <2 x float> @
 // NO_HALF: %dx.frac = call <2 x float> @llvm.dx.frac.v2f32(
 // NO_HALF: ret <2 x float> %dx.frac
-half2 test_frac_half2 ( half2 p0 ) {
-  return frac ( p0 );
-}
+half2 test_frac_half2(half2 p0) { return frac(p0); }
 // NATIVE_HALF: define noundef <3 x half> @
 // NATIVE_HALF: %dx.frac = call <3 x half> @llvm.dx.frac.v3f16
 // NATIVE_HALF: ret <3 x half> %dx.frac
 // NO_HALF: define noundef <3 x float> @
 // NO_HALF: %dx.frac = call <3 x float> @llvm.dx.frac.v3f32(
 // NO_HALF: ret <3 x float> %dx.frac
-half3 test_frac_half3 ( half3 p0 ) {
-  return frac ( p0 );
-}
+half3 test_frac_half3(half3 p0) { return frac(p0); }
 // NATIVE_HALF: define noundef <4 x half> @
 // NATIVE_HALF: %dx.frac = call <4 x half> @llvm.dx.frac.v4f16
 // NATIVE_HALF: ret <4 x half> %dx.frac
 // NO_HALF: define noundef <4 x float> @
 // NO_HALF: %dx.frac = call <4 x float> @llvm.dx.frac.v4f32(
 // NO_HALF: ret <4 x float> %dx.frac
-half4 test_frac_half4 ( half4 p0 ) {
-  return frac ( p0 );
-}
+half4 test_frac_half4(half4 p0) { return frac(p0); }
 
 // CHECK: define noundef float @
 // CHECK: %dx.frac = call float @llvm.dx.frac.f32(
 // CHECK: ret float %dx.frac
-float test_frac_float ( float p0 ) {
-  return frac ( p0 );
-}
+float test_frac_float(float p0) { return frac(p0); }
 // CHECK: define noundef <2 x float> @
 // CHECK: %dx.frac = call <2 x float> @llvm.dx.frac.v2f32
 // CHECK: ret <2 x float> %dx.frac
-float2 test_frac_float2 ( float2 p0 ) {
-  return frac ( p0 );
-}
+float2 test_frac_float2(float2 p0) { return frac(p0); }
 // CHECK: define noundef <3 x float> @
 // CHECK: %dx.frac = call <3 x float> @llvm.dx.frac.v3f32
 // CHECK: ret <3 x float> %dx.frac
-float3 test_frac_float3 ( float3 p0 ) {
-  return frac ( p0 );
-}
+float3 test_frac_float3(float3 p0) { return frac(p0); }
 // CHECK: define noundef <4 x float> @
 // CHECK: %dx.frac = call <4 x float> @llvm.dx.frac.v4f32
 // CHECK: ret <4 x float> %dx.frac
-float4 test_frac_float4 ( float4 p0 ) {
-  return frac ( p0 );
-}
+float4 test_frac_float4(float4 p0) { return frac(p0); }
diff --git a/clang/test/SemaHLSL/BuiltIns/frac-errors.hlsl b/clang/test/SemaHLSL/BuiltIns/frac-errors.hlsl
index 6e7b98c4a611af..06dbdf0a68dfc1 100644
--- a/clang/test/SemaHLSL/BuiltIns/frac-errors.hlsl
+++ b/clang/test/SemaHLSL/BuiltIns/frac-errors.hlsl
@@ -1,27 +1,27 @@
 
 // RUN: %clang_cc1 -finclude-default-header -triple dxil-pc-shadermodel6.6-library %s -fnative-half-type -emit-llvm -disable-llvm-passes -verify -verify-ignore-unexpected
 
-float test_too_few_arg () {
-  return __builtin_hlsl_elementwise_frac ();
+float test_too_few_arg() {
+  return __builtin_hlsl_elementwise_frac();
   // expected-error at -1 {{too few arguments to function call, expected 1, have 0}}
 }
 
-float2 test_too_many_arg ( float2 p0) {
-  return __builtin_hlsl_elementwise_frac ( p0, p0);
+float2 test_too_many_arg(float2 p0) {
+  return __builtin_hlsl_elementwise_frac(p0, p0);
   // expected-error at -1 {{too many arguments to function call, expected 1, have 2}}
 }
 
-float builtin_bool_to_float_type_promotion ( bool p1 ) {
-  return __builtin_hlsl_elementwise_frac ( p1 );
+float builtin_bool_to_float_type_promotion(bool p1) {
+  return __builtin_hlsl_elementwise_frac(p1);
   // expected-error at -1 {{1st argument must be a vector, integer or floating point type (was 'bool')}}
 }
 
-float builtin_frac_int_to_float_promotion (int p1 ) {
-  return __builtin_hlsl_elementwise_frac ( p1 );
+float builtin_frac_int_to_float_promotion(int p1) {
+  return __builtin_hlsl_elementwise_frac(p1);
   // expected-error at -1 {{passing 'int' to parameter of incompatible type 'float'}}
 }
 
-float2 builtin_frac_int2_to_float2_promotion (int2 p1 ) {
-  return __builtin_hlsl_elementwise_frac ( p1 );
+float2 builtin_frac_int2_to_float2_promotion(int2 p1) {
+  return __builtin_hlsl_elementwise_frac(p1);
   // expected-error at -1 {{passing 'int2' (aka 'vector<int, 2>') to parameter of incompatible type '__attribute__((__vector_size__(2 * sizeof(float)))) float' (vector of 2 'float' values)}}
 }



More information about the cfe-commits mailing list