[clang] [HLSL] Add matrix support to asint16 (PR #234578)

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


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

Adds matrix support to the HLSL asint16 intrinsic.

Resolves https://github.com/llvm/llvm-project/issues/184473

>From 2369b04fd2efd3d350d005ac528447ce7e3d12d3 Mon Sep 17 00:00:00 2001
From: Alexander Johnston <alexander.johnston at amd.com>
Date: Tue, 13 Oct 2026 14:00:00 +0100
Subject: [PATCH] [HLSL] Add matrix support to asint16

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

diff --git a/clang/lib/Headers/hlsl/hlsl_intrinsics.h b/clang/lib/Headers/hlsl/hlsl_intrinsics.h
index a86fc7dc0988bb..a75ddf1e34bfa2 100644
--- a/clang/lib/Headers/hlsl/hlsl_intrinsics.h
+++ b/clang/lib/Headers/hlsl/hlsl_intrinsics.h
@@ -69,6 +69,16 @@ template <typename T> constexpr int asint(T F) {
 
 #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>> asint16(matrix<T, R, C>
+                                                                   V) {
+  return __detail::bit_cast<int16_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/asint16_mat.hlsl b/clang/test/CodeGenHLSL/builtins/asint16_mat.hlsl
new file mode 100644
index 00000000000000..7a3329b6a2b4af
--- /dev/null
+++ b/clang/test/CodeGenHLSL/builtins/asint16_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]]
+int16_t4x4 test_matrix_int(int16_t4x4 p0) {
+  return asint16(p0);
+}
+
+// CHECK: define {{.*}}test_matrix_uint{{.*}}(<16 x i16> {{.*}} [[VAL:%.*]]){{.*}}
+// CHECK-NOT: bitcast
+// CHECK: ret <16 x i16> [[VAL]]
+int16_t4x4 test_matrix_uint(uint16_t4x4 p0) {
+  return asint16(p0);
+}
+
+// CHECK: define {{.*}}test_matrix_half{{.*}}(<16 x half> {{.*}} [[VAL:%.*]]){{.*}}
+// CHECK: bitcast <16 x half> [[VAL]] to <16 x i16>
+int16_t4x4 test_matrix_half(half4x4 p0) {
+  return asint16(p0);
+}
diff --git a/clang/test/SemaHLSL/BuiltIns/asint16-errors.hlsl b/clang/test/SemaHLSL/BuiltIns/asint16-errors.hlsl
index 5f3d5c9772d843..8faac48da9c591 100644
--- a/clang/test/SemaHLSL/BuiltIns/asint16-errors.hlsl
+++ b/clang/test/SemaHLSL/BuiltIns/asint16-errors.hlsl
@@ -6,6 +6,7 @@ int16_t4 test_asint16_too_many_arg(uint16_t p0, uint16_t p1)
     return asint16(p0, p1);
   // expected-error at -1 {{no matching function for call to 'asint16'}}
   // 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}}
 }
 
@@ -13,6 +14,7 @@ int16_t test_asint16_int(int p1)
 {
     return asint16(p1);
   // expected-error at -1 {{no matching function for call to 'asint16'}}
+  // 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'}}
 }
@@ -21,6 +23,7 @@ int16_t test_asint16_float(float p1)
 {
     return asint16(p1);
   // expected-error at -1 {{no matching function for call to 'asint16'}}
+  // 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'}}
 }
@@ -29,6 +32,7 @@ int16_t4 test_asint16_vector_int(int4 p1)
 {
     return asint16(p1);
   // expected-error at -1 {{no matching function for call to 'asint16'}}
+  // 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'}}
 }
@@ -37,7 +41,7 @@ int16_t4 test_asint16_vector_float(float4 p1)
 {
     return asint16(p1);
   // expected-error at -1 {{no matching function for call to 'asint16'}}
+  // 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/asint16_mat-errors.hlsl b/clang/test/SemaHLSL/BuiltIns/asint16_mat-errors.hlsl
new file mode 100644
index 00000000000000..ff89fbd2fde3ff
--- /dev/null
+++ b/clang/test/SemaHLSL/BuiltIns/asint16_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
+
+
+int16_t4x4 test_half_too_many_arg(half4x4 p0, half4x4 p1) {
+  return asint16(p0, p1);
+  // expected-error at -1 {{no matching function for call to 'asint16'}}
+  // 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}}
+}
+
+int16_t4x4 test_double(double4x4 p1) {
+  return asint16(p1);
+  // expected-error at -1 {{no matching function for call to 'asint16'}}
+  // 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>'}}
+}
+
+int16_t4x4 test_float(float4x4 p1) {
+  return asint16(p1);
+  // expected-error at -1 {{no matching function for call to 'asint16'}}
+  // 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