[clang] b796466 - [Matrix] Implement matrix support for the `clamp` intrinsic (#240969)
via cfe-commits
cfe-commits at lists.llvm.org
Wed Nov 11 15:46:35 PST 2026
Author: Kaitlin Peng
Date: 2026-11-11T15:46:30-08:00
New Revision: b796466db4cdcc972ce82d38e3df341b782dc886
URL: https://github.com/llvm/llvm-project/commit/b796466db4cdcc972ce82d38e3df341b782dc886
DIFF: https://github.com/llvm/llvm-project/commit/b796466db4cdcc972ce82d38e3df341b782dc886.diff
LOG: [Matrix] Implement matrix support for the `clamp` intrinsic (#240969)
Closes #184498.
This PR implements the matrix api for `clamp` and adds matrix codegen
tests and matrix sema tests. Matrix backend tests were not added because
no DirectX or SPIRV backend changes were made.
Assisted-by: Claude Opus 5.5
Added:
clang/test/CodeGenHLSL/builtins/clamp_mat.hlsl
clang/test/SemaHLSL/BuiltIns/clamp_mat-errors.hlsl
Modified:
clang/include/clang/Basic/HLSLIntrinsics.td
clang/lib/CodeGen/CGHLSLBuiltins.cpp
Removed:
################################################################################
diff --git a/clang/include/clang/Basic/HLSLIntrinsics.td b/clang/include/clang/Basic/HLSLIntrinsics.td
index d0500838a81f7..5fcad5aa54998 100644
--- a/clang/include/clang/Basic/HLSLIntrinsics.td
+++ b/clang/include/clang/Basic/HLSLIntrinsics.td
@@ -536,7 +536,6 @@ However for values of NaN, the results are undefined.
}];
let VaryingTypes = AllNumericTypes;
let VaryingLongVector = 1;
- let VaryingMatDims = [];
}
// Discards the current pixel if the specified value is less than zero.
diff --git a/clang/lib/CodeGen/CGHLSLBuiltins.cpp b/clang/lib/CodeGen/CGHLSLBuiltins.cpp
index 2d899bc680a53..31bcfd78a5b87 100644
--- a/clang/lib/CodeGen/CGHLSLBuiltins.cpp
+++ b/clang/lib/CodeGen/CGHLSLBuiltins.cpp
@@ -1224,6 +1224,8 @@ Value *CodeGenFunction::EmitHLSLBuiltinExpr(unsigned BuiltinID,
QualType Ty = E->getArg(0)->getType();
if (auto *VecTy = Ty->getAs<VectorType>())
Ty = VecTy->getElementType();
+ else if (auto *MatTy = Ty->getAs<ConstantMatrixType>())
+ Ty = MatTy->getElementType();
Intrinsic::ID Intr;
if (Ty->isFloatingType()) {
diff --git a/clang/test/CodeGenHLSL/builtins/clamp_mat.hlsl b/clang/test/CodeGenHLSL/builtins/clamp_mat.hlsl
new file mode 100644
index 0000000000000..ca9ce7d5c1e82
--- /dev/null
+++ b/clang/test/CodeGenHLSL/builtins/clamp_mat.hlsl
@@ -0,0 +1,46 @@
+// 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 -DTARGET=dx
+// RUN: %clang_cc1 -finclude-default-header -x hlsl -triple \
+// RUN: spirv-unknown-vulkan-library %s -emit-llvm -disable-llvm-passes \
+// RUN: -o - | FileCheck %s --check-prefixes=CHECK,NO_HALF -DTARGET=spv
+
+// CHECK-LABEL: test_clamp_half2x3
+// NATIVE_HALF: call reassoc nnan ninf nsz arcp afn <6 x half> @llvm.[[TARGET]].nclamp.v6f16(<6 x half> %{{.*}}, <6 x half> %{{.*}}, <6 x half> %{{.*}})
+// NO_HALF: call reassoc nnan ninf nsz arcp afn <6 x float> @llvm.[[TARGET]].nclamp.v6f32(<6 x float> %{{.*}}, <6 x float> %{{.*}}, <6 x float> %{{.*}})
+half2x3 test_clamp_half2x3(half2x3 p0, half2x3 p1, half2x3 p2) { return clamp(p0, p1, p2); }
+
+// CHECK-LABEL: test_clamp_half3x4
+// NATIVE_HALF: call reassoc nnan ninf nsz arcp afn <12 x half> @llvm.[[TARGET]].nclamp.v12f16(<12 x half> %{{.*}}, <12 x half> %{{.*}}, <12 x half> %{{.*}})
+// NO_HALF: call reassoc nnan ninf nsz arcp afn <12 x float> @llvm.[[TARGET]].nclamp.v12f32(<12 x float> %{{.*}}, <12 x float> %{{.*}}, <12 x float> %{{.*}})
+half3x4 test_clamp_half3x4(half3x4 p0, half3x4 p1, half3x4 p2) { return clamp(p0, p1, p2); }
+
+// CHECK-LABEL: test_clamp_half4x4
+// NATIVE_HALF: call reassoc nnan ninf nsz arcp afn <16 x half> @llvm.[[TARGET]].nclamp.v16f16(<16 x half> %{{.*}}, <16 x half> %{{.*}}, <16 x half> %{{.*}})
+// NO_HALF: call reassoc nnan ninf nsz arcp afn <16 x float> @llvm.[[TARGET]].nclamp.v16f32(<16 x float> %{{.*}}, <16 x float> %{{.*}}, <16 x float> %{{.*}})
+half4x4 test_clamp_half4x4(half4x4 p0, half4x4 p1, half4x4 p2) { return clamp(p0, p1, p2); }
+
+// CHECK-LABEL: test_clamp_int2x3
+// CHECK: call <6 x i32> @llvm.[[TARGET]].sclamp.v6i32(<6 x i32> %{{.*}}, <6 x i32> %{{.*}}, <6 x i32> %{{.*}})
+int2x3 test_clamp_int2x3(int2x3 p0, int2x3 p1, int2x3 p2) { return clamp(p0, p1, p2); }
+
+// CHECK-LABEL: test_clamp_int3x4
+// CHECK: call <12 x i32> @llvm.[[TARGET]].sclamp.v12i32(<12 x i32> %{{.*}}, <12 x i32> %{{.*}}, <12 x i32> %{{.*}})
+int3x4 test_clamp_int3x4(int3x4 p0, int3x4 p1, int3x4 p2) { return clamp(p0, p1, p2); }
+
+// CHECK-LABEL: test_clamp_int4x4
+// CHECK: call <16 x i32> @llvm.[[TARGET]].sclamp.v16i32(<16 x i32> %{{.*}}, <16 x i32> %{{.*}}, <16 x i32> %{{.*}})
+int4x4 test_clamp_int4x4(int4x4 p0, int4x4 p1, int4x4 p2) { return clamp(p0, p1, p2); }
+
+// CHECK-LABEL: test_clamp_uint2x3
+// CHECK: call <6 x i32> @llvm.[[TARGET]].uclamp.v6i32(<6 x i32> %{{.*}}, <6 x i32> %{{.*}}, <6 x i32> %{{.*}})
+uint2x3 test_clamp_uint2x3(uint2x3 p0, uint2x3 p1, uint2x3 p2) { return clamp(p0, p1, p2); }
+
+// CHECK-LABEL: test_clamp_uint3x4
+// CHECK: call <12 x i32> @llvm.[[TARGET]].uclamp.v12i32(<12 x i32> %{{.*}}, <12 x i32> %{{.*}}, <12 x i32> %{{.*}})
+uint3x4 test_clamp_uint3x4(uint3x4 p0, uint3x4 p1, uint3x4 p2) { return clamp(p0, p1, p2); }
+
+// CHECK-LABEL: test_clamp_uint4x4
+// CHECK: call <16 x i32> @llvm.[[TARGET]].uclamp.v16i32(<16 x i32> %{{.*}}, <16 x i32> %{{.*}}, <16 x i32> %{{.*}})
+uint4x4 test_clamp_uint4x4(uint4x4 p0, uint4x4 p1, uint4x4 p2) { return clamp(p0, p1, p2); }
diff --git a/clang/test/SemaHLSL/BuiltIns/clamp_mat-errors.hlsl b/clang/test/SemaHLSL/BuiltIns/clamp_mat-errors.hlsl
new file mode 100644
index 0000000000000..7f91e8c5664d2
--- /dev/null
+++ b/clang/test/SemaHLSL/BuiltIns/clamp_mat-errors.hlsl
@@ -0,0 +1,31 @@
+// RUN: %clang_cc1 -finclude-default-header -triple dxil-pc-shadermodel6.6-library %s -fnative-half-type -fnative-int16-type -emit-llvm-only -disable-llvm-passes -verify
+
+float2x2 test_too_few_arg(float2x2 p0) {
+ return __builtin_hlsl_elementwise_clamp(p0, p0);
+ // expected-error at -1 {{too few arguments to function call, expected 3, have 2}}
+}
+
+float2x2 test_too_many_arg(float2x2 p0) {
+ return __builtin_hlsl_elementwise_clamp(p0, p0, p0, p0);
+ // expected-error at -1 {{too many arguments to function call, expected 3, have 4}}
+}
+
+float2x2 test_mismatched_dims(float2x2 p0, float3x3 p1) {
+ return __builtin_hlsl_elementwise_clamp(p0, p0, p1);
+ // expected-error at -1 {{arguments are of
diff erent types ('matrix<[...], 2, 2>' vs 'matrix<[...], 3, 3>')}}
+}
+
+float2x2 test_mismatched_element_types(float2x2 p0, half2x2 p1) {
+ return __builtin_hlsl_elementwise_clamp(p0, p0, p1);
+ // expected-error at -1 {{arguments are of
diff erent types ('matrix<float, [2 * ...]>' vs 'matrix<half, [2 * ...]>')}}
+}
+
+float2x2 test_scalar_and_matrix(float p0, float2x2 p1) {
+ return __builtin_hlsl_elementwise_clamp(p0, p1, p1);
+ // expected-error at -1 {{arguments are of
diff erent types ('float' vs 'float2x2' (aka 'matrix<float, 2, 2>'))}}
+}
+
+float2x2 test_vector_and_matrix(float4 p0, float2x2 p1) {
+ return __builtin_hlsl_elementwise_clamp(p1, p1, p0);
+ // expected-error at -1 {{arguments are of
diff erent types ('float2x2' (aka 'matrix<float, 2, 2>') vs 'float4' (aka 'vector<float, 4>'))}}
+}
More information about the cfe-commits
mailing list