Author: metkarpoonam
Date: 2025-03-21T14:44:22-04:00
New Revision: 5ecbf46f8645e6294fab129c989fca67fd85d689

URL: 
https://github.com/llvm/llvm-project/commit/5ecbf46f8645e6294fab129c989fca67fd85d689
DIFF: 
https://github.com/llvm/llvm-project/commit/5ecbf46f8645e6294fab129c989fca67fd85d689.diff

LOG: Hlsl asuint16 function (#132315)

Implemented the asuint16 function and added test cases for codegen,
Sema, and SPIR-V backend.
fixes https://github.com/llvm/llvm-project/issues/99185

Added: 
    clang/test/CodeGenHLSL/builtins/asuint16.hlsl
    clang/test/SemaHLSL/BuiltIns/asuint16-errors.hlsl

Modified: 
    clang/lib/Headers/hlsl/hlsl_intrinsics.h

Removed: 
    


################################################################################
diff  --git a/clang/lib/Headers/hlsl/hlsl_intrinsics.h 
b/clang/lib/Headers/hlsl/hlsl_intrinsics.h
index a48a8e998a015..103709f3baa4e 100644
--- a/clang/lib/Headers/hlsl/hlsl_intrinsics.h
+++ b/clang/lib/Headers/hlsl/hlsl_intrinsics.h
@@ -80,6 +80,34 @@ void asuint(double3, out uint3, out uint3);
 _HLSL_BUILTIN_ALIAS(__builtin_hlsl_elementwise_splitdouble)
 void asuint(double4, out uint4, out uint4);
 
+//===----------------------------------------------------------------------===//
+// asuint16 builtins
+//===----------------------------------------------------------------------===//
+
+/// \fn uint16_t asuint16(T X)
+/// \brief Interprets the bit pattern of \a X as an 16-bit unsigned integer.
+/// \param X The input value.
+#ifdef __HLSL_ENABLE_16_BIT
+
+template <typename T, int N>
+_HLSL_16BIT_AVAILABILITY(shadermodel, 6.2)
+constexpr __detail::enable_if_t<__detail::is_same<int16_t, T>::value ||
+                                    __detail::is_same<uint16_t, T>::value ||
+                                    __detail::is_same<half, T>::value,
+                                vector<uint16_t, N>> asuint16(vector<T, N> V) {
+  return __detail::bit_cast<uint16_t, T, N>(V);
+}
+
+template <typename T>
+_HLSL_16BIT_AVAILABILITY(shadermodel, 6.2)
+constexpr __detail::enable_if_t<__detail::is_same<int16_t, T>::value ||
+                                    __detail::is_same<uint16_t, T>::value ||
+                                    __detail::is_same<half, T>::value,
+                                uint16_t> asuint16(T F) {
+  return __detail::bit_cast<uint16_t, T>(F);
+}
+#endif
+
 
//===----------------------------------------------------------------------===//
 // distance builtins
 
//===----------------------------------------------------------------------===//

diff  --git a/clang/test/CodeGenHLSL/builtins/asuint16.hlsl 
b/clang/test/CodeGenHLSL/builtins/asuint16.hlsl
new file mode 100644
index 0000000000000..3ed7de9dffbe5
--- /dev/null
+++ b/clang/test/CodeGenHLSL/builtins/asuint16.hlsl
@@ -0,0 +1,60 @@
+// RUN: %clang_cc1 -finclude-default-header -x hlsl -triple 
dxil-pc-shadermodel6.2-library %s -fnative-half-type -emit-llvm -O1 -o - | 
FileCheck %s
+
+//CHECK-LABEL: define {{.*}}test_ints
+//CHECK-SAME: {{.*}}(i16 {{.*}} [[VAL:%.*]]){{.*}}
+//CHECK-NOT: bitcast
+//CHECK: entry:
+//CHECK: ret i16 [[VAL]]
+uint16_t test_int(int16_t p0)
+{
+    return asuint16(p0);
+}
+
+//CHECK-LABEL: define {{.*}}test_uint
+//CHECK-SAME: {{.*}}(i16 {{.*}} [[VAL:%.*]]){{.*}}
+//CHECK-NOT: bitcast
+//CHECK: entry:
+//CHECK-NEXT: ret i16 [[VAL]]
+uint16_t test_uint(uint16_t p0)
+{
+    return asuint16(p0);
+}
+
+//CHECK-LABEL: define {{.*}}test_half
+//CHECK-SAME: {{.*}}(half {{.*}} [[VAL:%.*]]){{.*}}
+//CHECK: [[RES:%.*]] = bitcast half [[VAL]] to i16
+//CHECK-NEXT: ret i16 [[RES]]
+uint16_t test_half(half p0)
+{
+    return asuint16(p0);
+}
+
+//CHECK-LABEL: define {{.*}}test_vector_int
+//CHECK-SAME: {{.*}}(<4 x i16> {{.*}} [[VAL:%.*]]){{.*}}
+//CHECK-NOT: bitcast
+//CHECK: entry:
+//CHECK-NEXT: ret <4 x i16> [[VAL]]
+uint16_t4 test_vector_int(int16_t4 p0)
+{
+    return asuint16(p0);
+}
+
+//CHECK-LABEL: define {{.*}}test_vector_uint
+//CHECK-SAME: {{.*}}(<4 x i16> {{.*}} [[VAL:%.*]]){{.*}}
+//CHECK-NOT: bitcast
+//CHECK: entry:
+//CHECK-NEXT: ret <4 x i16> [[VAL]]
+uint16_t4 test_vector_uint(uint16_t4 p0)
+{
+    return asuint16(p0);
+}
+
+//CHECK-LABEL: define {{.*}}fn
+//CHECK-SAME: {{.*}}(<4 x half> {{.*}} [[VAL:%.*]]){{.*}}
+//CHECK: [[RES:%.*]] = bitcast <4 x half> [[VAL]] to <4 x i16>
+//CHECK-NEXT: ret <4 x i16> [[RES]]
+uint16_t4 fn(half4 p1)
+{
+    return asuint16(p1);
+}
+

diff  --git a/clang/test/SemaHLSL/BuiltIns/asuint16-errors.hlsl 
b/clang/test/SemaHLSL/BuiltIns/asuint16-errors.hlsl
new file mode 100644
index 0000000000000..bd29f3fd23262
--- /dev/null
+++ b/clang/test/SemaHLSL/BuiltIns/asuint16-errors.hlsl
@@ -0,0 +1,52 @@
+// RUN: %clang_cc1 -finclude-default-header -triple 
dxil-pc-shadermodel6.2-library %s -fnative-half-type -verify
+
+uint16_t test_asuint16_less_argument()
+{
+    return asuint16();
+  // expected-error@-1 {{no matching function for call to 'asuint16'}}
+  // expected-note@hlsl/hlsl_intrinsics.h:* {{candidate function template not 
viable: requires single argument 'V', but no arguments were provided}}
+  // expected-note@hlsl/hlsl_intrinsics.h:* {{candidate function template not 
viable: requires single argument 'F', but no arguments were provided}}
+
+}
+
+int16_t4 test_asuint16_too_many_arg(uint16_t p0, uint16_t p1)
+{
+    return asuint16(p0, p1);
+  // expected-error@-1 {{no matching function for call to 'asuint16'}}
+  // expected-note@hlsl/hlsl_intrinsics.h:* {{candidate function template not 
viable: requires single argument 'V', but 2 arguments were provided}}
+  // expected-note@hlsl/hlsl_intrinsics.h:* {{candidate function template not 
viable: requires single argument 'F', but 2 arguments were provided}}
+
+}
+
+int16_t test_asuint16_int(int p1)
+{
+    return asuint16(p1);
+  // expected-error@-1 {{no matching function for call to 'asuint16'}}
+  // expected-note@hlsl/hlsl_intrinsics.h:* {{candidate template ignored: 
could not match 'vector<T, N>' against 'int'}}
+  // expected-note@hlsl/hlsl_intrinsics.h:* {{candidate template ignored: 
substitution failure [with T = int]: no type named 'Type'}}
+}
+
+int16_t test_asuint16_float(float p1)
+{
+    return asuint16(p1);
+  // expected-error@-1 {{no matching function for call to 'asuint16'}}
+  // expected-note@hlsl/hlsl_intrinsics.h:* {{candidate template ignored: 
could not match 'vector<T, N>' against 'float'}}
+  // expected-note@hlsl/hlsl_intrinsics.h:* {{candidate template ignored: 
substitution failure [with T = float]: no type named 'Type'}}
+}
+
+int16_t4 test_asuint16_vector_int(int4 p1)
+{
+    return asuint16(p1);
+  // expected-error@-1 {{no matching function for call to 'asuint16'}}
+  // expected-note@hlsl/hlsl_intrinsics.h:* {{candidate template ignored: 
substitution failure [with T = int, N = 4]: no type named 'Type'}}
+  // expected-note@hlsl/hlsl_intrinsics.h:* {{candidate template ignored: 
substitution failure [with T = int4]: no type named 'Type'}}
+}
+
+int16_t4 test_asuint16_vector_float(float4 p1)
+{
+    return asuint16(p1);
+  // expected-error@-1 {{no matching function for call to 'asuint16'}}
+  // expected-note@hlsl/hlsl_intrinsics.h:* {{candidate template ignored: 
substitution failure [with T = float, N = 4]: no type named 'Type'}}
+  // expected-note@hlsl/hlsl_intrinsics.h:* {{candidate template ignored: 
substitution failure [with T = float4]: no type named 'Type'}}
+}
+


        
_______________________________________________
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to