[clang] [HLSL] Add matrix support for asuint16 (PR #234576)

Alexander Johnston via cfe-commits cfe-commits at lists.llvm.org
Thu Oct 22 03:55:35 PDT 2026


https://github.com/Alexander-Johnston created https://github.com/llvm/llvm-project/pull/234576

Adds matrix support for the HLSL asuint16 intrinsic.

>From 983bca69c18f735947386f27e2187e1f3652029b Mon Sep 17 00:00:00 2001
From: Alexander Johnston <alexander.johnston at amd.com>
Date: Wed, 21 Oct 2026 15:59:48 +0100
Subject: [PATCH] [HLSL] Add matrix support for asuint16

---
 clang/lib/Headers/hlsl/hlsl_intrinsics.h      | 10 +++++++
 .../CodeGenHLSL/builtins/asuint16_mat.hlsl    | 21 +++++++++++++++
 .../SemaHLSL/BuiltIns/asuint16-errors.hlsl    |  7 ++++-
 .../BuiltIns/asuint16_mat-errors.hlsl         | 26 +++++++++++++++++++
 4 files changed, 63 insertions(+), 1 deletion(-)
 create mode 100644 clang/test/CodeGenHLSL/builtins/asuint16_mat.hlsl
 create mode 100644 clang/test/SemaHLSL/BuiltIns/asuint16_mat-errors.hlsl

diff --git a/clang/lib/Headers/hlsl/hlsl_intrinsics.h b/clang/lib/Headers/hlsl/hlsl_intrinsics.h
index a86fc7dc0988bb..956cfc2065d327 100644
--- a/clang/lib/Headers/hlsl/hlsl_intrinsics.h
+++ b/clang/lib/Headers/hlsl/hlsl_intrinsics.h
@@ -140,6 +140,16 @@ void asuint(matrix<double, R, C>, out matrix<uint, R, C>,
 
 #ifdef __HLSL_ENABLE_16_BIT
 
+template <typename T, int R, int C>
+_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,
+                                matrix<int16_t, R, C>> asuint16(matrix<T, R, C>
+                                                                    V) {
+  return __detail::bit_cast<uint16_t, T, R, C>(V);
+}
+
 template <typename T, int N>
 _HLSL_16BIT_AVAILABILITY(shadermodel, 6.2)
 constexpr __detail::enable_if_t<__detail::is_same<int16_t, T>::value ||
diff --git a/clang/test/CodeGenHLSL/builtins/asuint16_mat.hlsl b/clang/test/CodeGenHLSL/builtins/asuint16_mat.hlsl
new file mode 100644
index 00000000000000..aa0023fba8c347
--- /dev/null
+++ b/clang/test/CodeGenHLSL/builtins/asuint16_mat.hlsl
@@ -0,0 +1,21 @@
+// RUN: %clang_cc1 -finclude-default-header -x hlsl -triple dxil-pc-shadermodel6.3-library %s -fnative-half-type -fnative-int16-type -emit-llvm -O1 -o - | FileCheck %s
+
+// CHECK: define {{.*}}test_matrix_int{{.*}}(<16 x i16> {{.*}} [[VAL:%.*]]){{.*}}
+// CHECK-NOT: bitcast
+// CHECK: ret <16 x i16> [[VAL]]
+uint16_t4x4 test_matrix_int(int16_t4x4 p0) {
+  return asuint16(p0);
+}
+
+// CHECK: define {{.*}}test_matrix_uint{{.*}}(<16 x i16> {{.*}} [[VAL:%.*]]){{.*}}
+// CHECK-NOT: bitcast
+// CHECK: ret <16 x i16> [[VAL]]
+uint16_t4x4 test_matrix_uint(uint16_t4x4 p0) {
+  return asuint16(p0);
+}
+
+// CHECK: define {{.*}}test_matrix_half{{.*}}(<16 x half> {{.*}} [[VAL:%.*]]){{.*}}
+// CHECK: bitcast <16 x half> [[VAL]] to <16 x i16>
+uint16_t4x4 test_matrix_half(half4x4 p0) {
+  return asuint16(p0);
+}
diff --git a/clang/test/SemaHLSL/BuiltIns/asuint16-errors.hlsl b/clang/test/SemaHLSL/BuiltIns/asuint16-errors.hlsl
index 709d2067d9df28..664c75c5a1f729 100644
--- a/clang/test/SemaHLSL/BuiltIns/asuint16-errors.hlsl
+++ b/clang/test/SemaHLSL/BuiltIns/asuint16-errors.hlsl
@@ -5,6 +5,7 @@ uint16_t test_asuint16_less_argument()
     return asuint16();
   // expected-error at -1 {{no matching function for call to 'asuint16'}}
   // expected-note at hlsl/hlsl_intrinsics.h:* {{candidate function template not viable: requires single argument 'V', but no arguments were provided}}
+  // expected-note at hlsl/hlsl_intrinsics.h:* {{candidate function template not viable: requires single argument 'V', but no arguments were provided}}
   // expected-note at hlsl/hlsl_intrinsics.h:* {{candidate function template not viable: requires single argument 'F', but no arguments were provided}}
 
 }
@@ -14,6 +15,7 @@ int16_t4 test_asuint16_too_many_arg(uint16_t p0, uint16_t p1)
     return asuint16(p0, p1);
   // expected-error at -1 {{no matching function for call to 'asuint16'}}
   // expected-note at hlsl/hlsl_intrinsics.h:* {{candidate function template not viable: requires single argument 'V', but 2 arguments were provided}}
+  // expected-note at hlsl/hlsl_intrinsics.h:* {{candidate function template not viable: requires single argument 'V', but 2 arguments were provided}}
   // expected-note at hlsl/hlsl_intrinsics.h:* {{candidate function template not viable: requires single argument 'F', but 2 arguments were provided}}
 
 }
@@ -22,6 +24,7 @@ int16_t test_asuint16_int(int p1)
 {
     return asuint16(p1);
   // expected-error at -1 {{no matching function for call to 'asuint16'}}
+  // expected-note at hlsl/hlsl_intrinsics.h:* {{candidate template ignored: could not match 'matrix<T, R, C>' against 'int'}}
   // expected-note at hlsl/hlsl_intrinsics.h:* {{candidate template ignored: could not match 'vector<T, N>' against 'int'}}
   // expected-note at hlsl/hlsl_intrinsics.h:* {{candidate template ignored: substitution failure [with T = int]: no type named 'Type'}}
 }
@@ -30,6 +33,7 @@ int16_t test_asuint16_float(float p1)
 {
     return asuint16(p1);
   // expected-error at -1 {{no matching function for call to 'asuint16'}}
+  // expected-note at hlsl/hlsl_intrinsics.h:* {{candidate template ignored: could not match 'matrix<T, R, C>' against 'float'}}
   // expected-note at hlsl/hlsl_intrinsics.h:* {{candidate template ignored: could not match 'vector<T, N>' against 'float'}}
   // expected-note at hlsl/hlsl_intrinsics.h:* {{candidate template ignored: substitution failure [with T = float]: no type named 'Type'}}
 }
@@ -38,6 +42,7 @@ int16_t4 test_asuint16_vector_int(int4 p1)
 {
     return asuint16(p1);
   // expected-error at -1 {{no matching function for call to 'asuint16'}}
+  // expected-note at hlsl/hlsl_intrinsics.h:* {{candidate template ignored: could not match 'matrix<T, R, C>' against 'int4' (aka 'vector<int, 4>')}}
   // expected-note at hlsl/hlsl_intrinsics.h:* {{candidate template ignored: substitution failure [with T = int, N = 4]: no type named 'Type'}}
   // expected-note at hlsl/hlsl_intrinsics.h:* {{candidate template ignored: substitution failure [with T = int4]: no type named 'Type'}}
 }
@@ -46,7 +51,7 @@ int16_t4 test_asuint16_vector_float(float4 p1)
 {
     return asuint16(p1);
   // expected-error at -1 {{no matching function for call to 'asuint16'}}
+  // expected-note at hlsl/hlsl_intrinsics.h:* {{candidate template ignored: could not match 'matrix<T, R, C>' against 'float4' (aka 'vector<float, 4>')}}
   // expected-note at hlsl/hlsl_intrinsics.h:* {{candidate template ignored: substitution failure [with T = float, N = 4]: no type named 'Type'}}
   // expected-note at hlsl/hlsl_intrinsics.h:* {{candidate template ignored: substitution failure [with T = float4]: no type named 'Type'}}
 }
-
diff --git a/clang/test/SemaHLSL/BuiltIns/asuint16_mat-errors.hlsl b/clang/test/SemaHLSL/BuiltIns/asuint16_mat-errors.hlsl
new file mode 100644
index 00000000000000..7573ede6a1a5a0
--- /dev/null
+++ b/clang/test/SemaHLSL/BuiltIns/asuint16_mat-errors.hlsl
@@ -0,0 +1,26 @@
+// RUN: %clang_cc1 -finclude-default-header -triple dxil-pc-shadermodel6.6-library %s -fnative-half-type -fnative-int16-type -verify
+
+
+uint16_t4x4 test_half_too_many_arg(half4x4 p0, half4x4 p1) {
+  return asuint16(p0, p1);
+  // expected-error at -1 {{no matching function for call to 'asuint16'}}
+  // expected-note at hlsl/hlsl_intrinsics.h:* {{candidate function template not viable: requires single argument 'V', but 2 arguments were provided}}
+  // expected-note at hlsl/hlsl_intrinsics.h:* {{candidate function template not viable: requires single argument 'V', but 2 arguments were provided}}
+  // expected-note at hlsl/hlsl_intrinsics.h:* {{candidate function template not viable: requires single argument 'F', but 2 arguments were provided}}
+}
+
+uint16_t4x4 test_double(double4x4 p1) {
+  return asuint16(p1);
+  // expected-error at -1 {{no matching function for call to 'asuint16'}}
+  // expected-note at hlsl/hlsl_intrinsics.h:* {{candidate template ignored: substitution failure [with T = double, R = 4, C = 4]: no type named 'Type' in 'hlsl::__detail::enable_if<false, matrix<short, 4, 4>>'}}
+  // expected-note at hlsl/hlsl_intrinsics.h:* {{candidate template ignored: could not match 'vector<T, N>' against 'double4x4' (aka 'matrix<double, 4, 4>')}}
+  // expected-note at hlsl/hlsl_intrinsics.h:* {{candidate template ignored: substitution failure [with T = double4x4]: no type named 'Type' in 'hlsl::__detail::enable_if<false, short>'}}
+}
+
+uint16_t4x4 test_float(float4x4 p1) {
+  return asuint16(p1);
+  // expected-error at -1 {{no matching function for call to 'asuint16'}}
+  // expected-note at hlsl/hlsl_intrinsics.h:* {{candidate template ignored: substitution failure [with T = float, R = 4, C = 4]: no type named 'Type' in 'hlsl::__detail::enable_if<false, matrix<short, 4, 4>>'}}
+  // expected-note at hlsl/hlsl_intrinsics.h:* {{candidate template ignored: could not match 'vector<T, N>' against 'float4x4' (aka 'matrix<float, 4, 4>')}}
+  // expected-note at hlsl/hlsl_intrinsics.h:* {{candidate template ignored: substitution failure [with T = float4x4]: no type named 'Type' in 'hlsl::__detail::enable_if<false, short>'}}
+}



More information about the cfe-commits mailing list