[clang] [Matrix] Implement matrix support for the `mad` intrinsic (PR #239357)
Kaitlin Peng via cfe-commits
cfe-commits at lists.llvm.org
Thu Nov 5 12:06:11 PST 2026
https://github.com/kmpeng created https://github.com/llvm/llvm-project/pull/239357
Closes #184510.
This PR implements the matrix api for `mad` 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
>From 8f8eaa4e14aae9fc0f36ea64c21c5fb8206995bb Mon Sep 17 00:00:00 2001
From: kmpeng <kaitlinpeng at microsoft.com>
Date: Thu, 5 Nov 2026 11:59:52 -0800
Subject: [PATCH] mad matrix implementation and tests
---
clang/include/clang/Basic/HLSLIntrinsics.td | 1 -
clang/test/CodeGenHLSL/builtins/mad_mat.hlsl | 58 +++++++++++++++++++
.../SemaHLSL/BuiltIns/mad_mat-errors.hlsl | 31 ++++++++++
3 files changed, 89 insertions(+), 1 deletion(-)
create mode 100644 clang/test/CodeGenHLSL/builtins/mad_mat.hlsl
create mode 100644 clang/test/SemaHLSL/BuiltIns/mad_mat-errors.hlsl
diff --git a/clang/include/clang/Basic/HLSLIntrinsics.td b/clang/include/clang/Basic/HLSLIntrinsics.td
index c8eb074f484e9b..60bcc5e635dbee 100644
--- a/clang/include/clang/Basic/HLSLIntrinsics.td
+++ b/clang/include/clang/Basic/HLSLIntrinsics.td
@@ -1286,7 +1286,6 @@ def hlsl_mad : HLSLThreeArgBuiltin<"mad", "__builtin_hlsl_mad"> {
\param B The second addition value.
}];
let VaryingTypes = AllNumericTypes;
- let VaryingMatDims = [];
let VaryingLongVector = 1;
}
diff --git a/clang/test/CodeGenHLSL/builtins/mad_mat.hlsl b/clang/test/CodeGenHLSL/builtins/mad_mat.hlsl
new file mode 100644
index 00000000000000..2d1360e1933022
--- /dev/null
+++ b/clang/test/CodeGenHLSL/builtins/mad_mat.hlsl
@@ -0,0 +1,58 @@
+// 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,DXCHECK,NATIVE_HALF
+// 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,SPVCHECK,NO_HALF
+
+// CHECK-LABEL: test_mad_half2x3
+// NATIVE_HALF: call reassoc nnan ninf nsz arcp afn <6 x half> @llvm.fmuladd.v6f16(<6 x half> %{{.*}}, <6 x half> %{{.*}}, <6 x half> %{{.*}})
+// NO_HALF: call reassoc nnan ninf nsz arcp afn <6 x float> @llvm.fmuladd.v6f32(<6 x float> %{{.*}}, <6 x float> %{{.*}}, <6 x float> %{{.*}})
+half2x3 test_mad_half2x3(half2x3 p0, half2x3 p1, half2x3 p2) { return mad(p0, p1, p2); }
+
+// CHECK-LABEL: test_mad_half3x4
+// NATIVE_HALF: call reassoc nnan ninf nsz arcp afn <12 x half> @llvm.fmuladd.v12f16(<12 x half> %{{.*}}, <12 x half> %{{.*}}, <12 x half> %{{.*}})
+// NO_HALF: call reassoc nnan ninf nsz arcp afn <12 x float> @llvm.fmuladd.v12f32(<12 x float> %{{.*}}, <12 x float> %{{.*}}, <12 x float> %{{.*}})
+half3x4 test_mad_half3x4(half3x4 p0, half3x4 p1, half3x4 p2) { return mad(p0, p1, p2); }
+
+// CHECK-LABEL: test_mad_half4x4
+// NATIVE_HALF: call reassoc nnan ninf nsz arcp afn <16 x half> @llvm.fmuladd.v16f16(<16 x half> %{{.*}}, <16 x half> %{{.*}}, <16 x half> %{{.*}})
+// NO_HALF: call reassoc nnan ninf nsz arcp afn <16 x float> @llvm.fmuladd.v16f32(<16 x float> %{{.*}}, <16 x float> %{{.*}}, <16 x float> %{{.*}})
+half4x4 test_mad_half4x4(half4x4 p0, half4x4 p1, half4x4 p2) { return mad(p0, p1, p2); }
+
+// CHECK-LABEL: test_mad_int2x3
+// DXCHECK: call <6 x i32> @llvm.dx.imad.v6i32(<6 x i32> %{{.*}}, <6 x i32> %{{.*}}, <6 x i32> %{{.*}})
+// SPVCHECK: [[MUL:%.*]] = mul nsw <6 x i32> %{{.*}}, %{{.*}}
+// SPVCHECK: add nsw <6 x i32> [[MUL]], %{{.*}}
+int2x3 test_mad_int2x3(int2x3 p0, int2x3 p1, int2x3 p2) { return mad(p0, p1, p2); }
+
+// CHECK-LABEL: test_mad_int3x4
+// DXCHECK: call <12 x i32> @llvm.dx.imad.v12i32(<12 x i32> %{{.*}}, <12 x i32> %{{.*}}, <12 x i32> %{{.*}})
+// SPVCHECK: [[MUL:%.*]] = mul nsw <12 x i32> %{{.*}}, %{{.*}}
+// SPVCHECK: add nsw <12 x i32> [[MUL]], %{{.*}}
+int3x4 test_mad_int3x4(int3x4 p0, int3x4 p1, int3x4 p2) { return mad(p0, p1, p2); }
+
+// CHECK-LABEL: test_mad_int4x4
+// DXCHECK: call <16 x i32> @llvm.dx.imad.v16i32(<16 x i32> %{{.*}}, <16 x i32> %{{.*}}, <16 x i32> %{{.*}})
+// SPVCHECK: [[MUL:%.*]] = mul nsw <16 x i32> %{{.*}}, %{{.*}}
+// SPVCHECK: add nsw <16 x i32> [[MUL]], %{{.*}}
+int4x4 test_mad_int4x4(int4x4 p0, int4x4 p1, int4x4 p2) { return mad(p0, p1, p2); }
+
+// CHECK-LABEL: test_mad_uint2x3
+// DXCHECK: call <6 x i32> @llvm.dx.umad.v6i32(<6 x i32> %{{.*}}, <6 x i32> %{{.*}}, <6 x i32> %{{.*}})
+// SPVCHECK: [[MUL:%.*]] = mul nuw <6 x i32> %{{.*}}, %{{.*}}
+// SPVCHECK: add nuw <6 x i32> [[MUL]], %{{.*}}
+uint2x3 test_mad_uint2x3(uint2x3 p0, uint2x3 p1, uint2x3 p2) { return mad(p0, p1, p2); }
+
+// CHECK-LABEL: test_mad_uint3x4
+// DXCHECK: call <12 x i32> @llvm.dx.umad.v12i32(<12 x i32> %{{.*}}, <12 x i32> %{{.*}}, <12 x i32> %{{.*}})
+// SPVCHECK: [[MUL:%.*]] = mul nuw <12 x i32> %{{.*}}, %{{.*}}
+// SPVCHECK: add nuw <12 x i32> [[MUL]], %{{.*}}
+uint3x4 test_mad_uint3x4(uint3x4 p0, uint3x4 p1, uint3x4 p2) { return mad(p0, p1, p2); }
+
+// CHECK-LABEL: test_mad_uint4x4
+// DXCHECK: call <16 x i32> @llvm.dx.umad.v16i32(<16 x i32> %{{.*}}, <16 x i32> %{{.*}}, <16 x i32> %{{.*}})
+// SPVCHECK: [[MUL:%.*]] = mul nuw <16 x i32> %{{.*}}, %{{.*}}
+// SPVCHECK: add nuw <16 x i32> [[MUL]], %{{.*}}
+uint4x4 test_mad_uint4x4(uint4x4 p0, uint4x4 p1, uint4x4 p2) { return mad(p0, p1, p2); }
diff --git a/clang/test/SemaHLSL/BuiltIns/mad_mat-errors.hlsl b/clang/test/SemaHLSL/BuiltIns/mad_mat-errors.hlsl
new file mode 100644
index 00000000000000..3346b3a40b44f3
--- /dev/null
+++ b/clang/test/SemaHLSL/BuiltIns/mad_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_mad(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_mad(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_mad(p0, p0, p1);
+ // expected-error at -1 {{arguments are of different types ('matrix<[...], 2, 2>' vs 'matrix<[...], 3, 3>')}}
+}
+
+float2x2 test_mismatched_element_types(float2x2 p0, half2x2 p1) {
+ return __builtin_hlsl_mad(p0, p0, p1);
+ // expected-error at -1 {{arguments are of different types ('matrix<float, [2 * ...]>' vs 'matrix<half, [2 * ...]>')}}
+}
+
+float2x2 test_scalar_and_matrix(float p0, float2x2 p1) {
+ return __builtin_hlsl_mad(p0, p1, p1);
+ // expected-error at -1 {{arguments are of different types ('float' vs 'float2x2' (aka 'matrix<float, 2, 2>'))}}
+}
+
+float2x2 test_vector_and_matrix(float4 p0, float2x2 p1) {
+ return __builtin_hlsl_mad(p1, p1, p0);
+ // expected-error at -1 {{arguments are of different types ('float2x2' (aka 'matrix<float, 2, 2>') vs 'float4' (aka 'vector<float, 4>'))}}
+}
More information about the cfe-commits
mailing list