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

Alexander Johnston via cfe-commits cfe-commits at lists.llvm.org
Thu Nov 12 08:50:50 PST 2026


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

>From 4f480b28117379eb0112566b4c40760db3c438ef 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 1/2] [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 a86fc7dc0988b..956cfc2065d32 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 0000000000000..aa0023fba8c34
--- /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 709d2067d9df2..664c75c5a1f72 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 0000000000000..2b5c8ce01e53d
--- /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, unsigned 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, unsigned short>'}}
+}

>From c6bc5fcc6425c4f24b700a1ee242da007b850498 Mon Sep 17 00:00:00 2001
From: Alexander Johnston <alexander.johnston at amd.com>
Date: Tue, 3 Nov 2026 15:23:19 +0000
Subject: [PATCH 2/2] Fix return type

---
 clang/lib/Headers/hlsl/hlsl_intrinsics.h              | 4 ++--
 clang/test/SemaHLSL/BuiltIns/asuint16_mat-errors.hlsl | 4 ++--
 2 files changed, 4 insertions(+), 4 deletions(-)

diff --git a/clang/lib/Headers/hlsl/hlsl_intrinsics.h b/clang/lib/Headers/hlsl/hlsl_intrinsics.h
index 956cfc2065d32..9398075dd6993 100644
--- a/clang/lib/Headers/hlsl/hlsl_intrinsics.h
+++ b/clang/lib/Headers/hlsl/hlsl_intrinsics.h
@@ -145,8 +145,8 @@ _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) {
+                                matrix<uint16_t, R, C>> asuint16(matrix<T, R, C>
+                                                                     V) {
   return __detail::bit_cast<uint16_t, T, R, C>(V);
 }
 
diff --git a/clang/test/SemaHLSL/BuiltIns/asuint16_mat-errors.hlsl b/clang/test/SemaHLSL/BuiltIns/asuint16_mat-errors.hlsl
index 2b5c8ce01e53d..131ca98594844 100644
--- a/clang/test/SemaHLSL/BuiltIns/asuint16_mat-errors.hlsl
+++ b/clang/test/SemaHLSL/BuiltIns/asuint16_mat-errors.hlsl
@@ -12,7 +12,7 @@ uint16_t4x4 test_half_too_many_arg(half4x4 p0, half4x4 p1) {
 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: substitution failure [with T = double, R = 4, C = 4]: no type named 'Type' in 'hlsl::__detail::enable_if<false, matrix<unsigned 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, unsigned short>'}}
 }
@@ -20,7 +20,7 @@ uint16_t4x4 test_double(double4x4 p1) {
 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: substitution failure [with T = float, R = 4, C = 4]: no type named 'Type' in 'hlsl::__detail::enable_if<false, matrix<unsigned 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, unsigned short>'}}
 }



More information about the cfe-commits mailing list