[clang] 2f1153a - [Matrix] Fix `fmuladd` crash on `+` or `-` between a matrix and scalar (#245686)
via cfe-commits
cfe-commits at lists.llvm.org
Wed Nov 25 10:48:30 PST 2026
Author: Kaitlin Peng
Date: 2026-11-25T10:48:25-08:00
New Revision: 2f1153a278e8fd29f47ede2b364279b040abe6d1
URL: https://github.com/llvm/llvm-project/commit/2f1153a278e8fd29f47ede2b364279b040abe6d1
DIFF: https://github.com/llvm/llvm-project/commit/2f1153a278e8fd29f47ede2b364279b040abe6d1.diff
LOG: [Matrix] Fix `fmuladd` crash on `+` or `-` between a matrix and scalar (#245686)
Fixes #244340.
With FP contraction on (the default for `clang-dxc`), Clang tries to
fuse a `+` or `-` with a multiply operand into an `fmuladd`. For a
matrix and a scalar, the scalar isn't splatted yet at that point, so the
`fmuladd` builds with mismatched operand types and hits an assertion.
This PR fixes that by skipping fusion when the operand types differ. The
check is intentionally general rather than matrix-specific, since
`fmuladd` requires all three operands to have the same type. In
practice, a mismatch is currently only reachable with matrix + scalar,
but the general check also guards against future changes.
This PR also adds HLSL and C codegen tests for the crash and for cases
that should still fuse (matrix + matrix, vector + scalar, mixed scalar
types).
Assisted-by: Claude Opus 5.5
Added:
clang/test/CodeGen/matrix-scalar-fmuladd.c
clang/test/CodeGenHLSL/Operators/matrix-scalar-fmuladd.hlsl
Modified:
clang/lib/CodeGen/CGExprScalar.cpp
Removed:
################################################################################
diff --git a/clang/lib/CodeGen/CGExprScalar.cpp b/clang/lib/CodeGen/CGExprScalar.cpp
index 1378db5715fbf2..768e01deb9ceb3 100644
--- a/clang/lib/CodeGen/CGExprScalar.cpp
+++ b/clang/lib/CodeGen/CGExprScalar.cpp
@@ -4726,6 +4726,11 @@ static Value* tryEmitFMulAdd(const BinOpInfo &op,
if (!op.FPFeatures.allowFPContractWithinStatement())
return nullptr;
+ // fmuladd requires all of its operands to have the same type, so an add/sub
+ // with operands of
diff erent types can't be fused.
+ if (op.LHS->getType() != op.RHS->getType())
+ return nullptr;
+
Value *LHS = op.LHS;
Value *RHS = op.RHS;
diff --git a/clang/test/CodeGen/matrix-scalar-fmuladd.c b/clang/test/CodeGen/matrix-scalar-fmuladd.c
new file mode 100644
index 00000000000000..7634994255e9d2
--- /dev/null
+++ b/clang/test/CodeGen/matrix-scalar-fmuladd.c
@@ -0,0 +1,84 @@
+// RUN: %clang_cc1 -fenable-matrix -triple x86_64-apple-darwin %s -ffp-contract=on -emit-llvm -disable-llvm-passes -o - | FileCheck %s
+// RUN: %clang_cc1 -fenable-matrix -triple x86_64-apple-darwin %s -ffp-contract=on -ffp-exception-behavior=strict -emit-llvm -disable-llvm-passes -o - | FileCheck %s --check-prefix=STRICT
+
+// Regression test for issue llvm/llvm-project#244340
+
+typedef float fx2x3_t __attribute__((matrix_type(2, 3)));
+typedef float fx4_t __attribute__((ext_vector_type(4)));
+
+// CHECK-LABEL: @test_matrix_mul_plus_scalar
+// CHECK: [[MUL:%.*]] = fmul <6 x float> %{{.*}}, splat (float 2.000000e+00)
+// CHECK: fadd <6 x float> [[MUL]], splat (float 3.000000e+00)
+// STRICT-LABEL: @test_matrix_mul_plus_scalar
+// STRICT: [[MUL:%.*]] = call <6 x float> @llvm.experimental.constrained.fmul.v6f32(<6 x float> %{{.*}}, <6 x float> splat (float 2.000000e+00), {{.*}})
+// STRICT: call <6 x float> @llvm.experimental.constrained.fadd.v6f32(<6 x float> [[MUL]], <6 x float> splat (float 3.000000e+00), {{.*}})
+fx2x3_t test_matrix_mul_plus_scalar(fx2x3_t m) {
+ return m * 2.0f + 3.0f;
+}
+
+// CHECK-LABEL: @test_scalar_minus_matrix_mul
+// CHECK: [[MUL:%.*]] = fmul <6 x float> splat (float 2.000000e+00), %{{.*}}
+// CHECK: fsub <6 x float> splat (float 3.000000e+00), [[MUL]]
+// STRICT-LABEL: @test_scalar_minus_matrix_mul
+// STRICT: [[MUL:%.*]] = call <6 x float> @llvm.experimental.constrained.fmul.v6f32(<6 x float> splat (float 2.000000e+00), <6 x float> %{{.*}}, {{.*}})
+// STRICT: call <6 x float> @llvm.experimental.constrained.fsub.v6f32(<6 x float> splat (float 3.000000e+00), <6 x float> [[MUL]], {{.*}})
+fx2x3_t test_scalar_minus_matrix_mul(fx2x3_t m) {
+ return 3.0f - 2.0f * m;
+}
+
+// CHECK-LABEL: @test_scalar_mul_plus_matrix
+// CHECK: [[MUL:%.*]] = fmul float %{{.*}}, %{{.*}}
+// CHECK: [[INSERT:%.*]] = insertelement <6 x float> poison, float [[MUL]], i64 0
+// CHECK: [[SPLAT:%.*]] = shufflevector <6 x float> [[INSERT]], <6 x float> poison, <6 x i32> zeroinitializer
+// CHECK: fadd <6 x float> [[SPLAT]], %{{.*}}
+// STRICT-LABEL: @test_scalar_mul_plus_matrix
+// STRICT: [[MUL:%.*]] = call float @llvm.experimental.constrained.fmul.f32(float %{{.*}}, float %{{.*}}, {{.*}})
+// STRICT: [[INSERT:%.*]] = insertelement <6 x float> poison, float [[MUL]], i64 0
+// STRICT: [[SPLAT:%.*]] = shufflevector <6 x float> [[INSERT]], <6 x float> poison, <6 x i32> zeroinitializer
+// STRICT: call <6 x float> @llvm.experimental.constrained.fadd.v6f32(<6 x float> [[SPLAT]], <6 x float> %{{.*}}, {{.*}})
+fx2x3_t test_scalar_mul_plus_matrix(fx2x3_t m, float a, float b) {
+ return a * b + m;
+}
+
+// CHECK-LABEL: @test_matrix_plus_scalar_mul
+// CHECK: [[MUL:%.*]] = fmul float %{{.*}}, %{{.*}}
+// CHECK: [[INSERT:%.*]] = insertelement <6 x float> poison, float [[MUL]], i64 0
+// CHECK: [[SPLAT:%.*]] = shufflevector <6 x float> [[INSERT]], <6 x float> poison, <6 x i32> zeroinitializer
+// CHECK: fadd <6 x float> %{{.*}}, [[SPLAT]]
+// STRICT-LABEL: @test_matrix_plus_scalar_mul
+// STRICT: [[MUL:%.*]] = call float @llvm.experimental.constrained.fmul.f32(float %{{.*}}, float %{{.*}}, {{.*}})
+// STRICT: [[INSERT:%.*]] = insertelement <6 x float> poison, float [[MUL]], i64 0
+// STRICT: [[SPLAT:%.*]] = shufflevector <6 x float> [[INSERT]], <6 x float> poison, <6 x i32> zeroinitializer
+// STRICT: call <6 x float> @llvm.experimental.constrained.fadd.v6f32(<6 x float> %{{.*}}, <6 x float> [[SPLAT]], {{.*}})
+fx2x3_t test_matrix_plus_scalar_mul(fx2x3_t m, float a, float b) {
+ return m + a * b;
+}
+
+// Make sure operands of the same type are still fused.
+// CHECK-LABEL: @test_matrix_mul_plus_matrix
+// CHECK: call <6 x float> @llvm.fmuladd.v6f32(<6 x float> %{{.*}}, <6 x float> splat (float 2.000000e+00), <6 x float> %{{.*}})
+// STRICT-LABEL: @test_matrix_mul_plus_matrix
+// STRICT: call <6 x float> @llvm.experimental.constrained.fmuladd.v6f32(<6 x float> %{{.*}}, <6 x float> splat (float 2.000000e+00), <6 x float> %{{.*}}, {{.*}})
+fx2x3_t test_matrix_mul_plus_matrix(fx2x3_t m, fx2x3_t n) {
+ return m * 2.0f + n;
+}
+
+// A vector add/sub's scalar operand is splatted, so it still fuses.
+// CHECK-LABEL: @test_vector_mul_plus_scalar
+// CHECK: call <4 x float> @llvm.fmuladd.v4f32(<4 x float> %{{.*}}, <4 x float> splat (float 2.000000e+00), <4 x float> splat (float 3.000000e+00))
+// STRICT-LABEL: @test_vector_mul_plus_scalar
+// STRICT: call <4 x float> @llvm.experimental.constrained.fmuladd.v4f32(<4 x float> %{{.*}}, <4 x float> splat (float 2.000000e+00), <4 x float> splat (float 3.000000e+00), {{.*}})
+fx4_t test_vector_mul_plus_scalar(fx4_t v) {
+ return v * 2.0f + 3.0f;
+}
+
+// The float operand is converted to double, so it still fuses.
+// CHECK-LABEL: @test_double_mul_plus_float
+// CHECK: [[CONV:%.*]] = fpext float %{{.*}} to double
+// CHECK: call double @llvm.fmuladd.f64(double %{{.*}}, double %{{.*}}, double [[CONV]])
+// STRICT-LABEL: @test_double_mul_plus_float
+// STRICT: [[CONV:%.*]] = call double @llvm.experimental.constrained.fpext.f64.f32(float %{{.*}}, {{.*}})
+// STRICT: call double @llvm.experimental.constrained.fmuladd.f64(double %{{.*}}, double %{{.*}}, double [[CONV]], {{.*}})
+double test_double_mul_plus_float(double a, double b, float c) {
+ return a * b + c;
+}
diff --git a/clang/test/CodeGenHLSL/Operators/matrix-scalar-fmuladd.hlsl b/clang/test/CodeGenHLSL/Operators/matrix-scalar-fmuladd.hlsl
new file mode 100644
index 00000000000000..5ba6412b43407d
--- /dev/null
+++ b/clang/test/CodeGenHLSL/Operators/matrix-scalar-fmuladd.hlsl
@@ -0,0 +1,130 @@
+// RUN: %clang_cc1 -finclude-default-header -triple dxil-pc-shadermodel6.6-library %s -ffp-contract=on -emit-llvm -disable-llvm-passes -o - | FileCheck %s
+// RUN: %clang_cc1 -finclude-default-header -triple spirv-unknown-vulkan-library %s -ffp-contract=on -emit-llvm -disable-llvm-passes -o - | FileCheck %s
+
+// Regression test for issue llvm/llvm-project#244340
+
+// CHECK-LABEL: test_matrix_mul_plus_scalar
+// CHECK: [[MUL:%.*]] = fmul {{.*}}<6 x float> %{{.*}}, splat (float 2.000000e+00)
+// CHECK: fadd {{.*}}<6 x float> [[MUL]], splat (float 3.000000e+00)
+float2x3 test_matrix_mul_plus_scalar(float2x3 m) {
+ return m * 2 + 3;
+}
+
+// CHECK-LABEL: test_matrix_mul_minus_scalar
+// CHECK: [[MUL:%.*]] = fmul {{.*}}<6 x float> %{{.*}}, splat (float 2.000000e+00)
+// CHECK: fsub {{.*}}<6 x float> [[MUL]], splat (float 3.000000e+00)
+float2x3 test_matrix_mul_minus_scalar(float2x3 m) {
+ return m * 2 - 3;
+}
+
+// CHECK-LABEL: test_scalar_plus_matrix_mul
+// CHECK: [[MUL:%.*]] = fmul {{.*}}<6 x float> splat (float 2.000000e+00), %{{.*}}
+// CHECK: fadd {{.*}}<6 x float> splat (float 3.000000e+00), [[MUL]]
+float2x3 test_scalar_plus_matrix_mul(float2x3 m) {
+ return 3 + 2 * m;
+}
+
+// CHECK-LABEL: test_scalar_minus_matrix_mul
+// CHECK: [[MUL:%.*]] = fmul {{.*}}<6 x float> splat (float 2.000000e+00), %{{.*}}
+// CHECK: fsub {{.*}}<6 x float> splat (float 3.000000e+00), [[MUL]]
+float2x3 test_scalar_minus_matrix_mul(float2x3 m) {
+ return 3 - 2 * m;
+}
+
+// CHECK-LABEL: test_scalar_plus_neg_matrix_mul
+// CHECK: [[MUL:%.*]] = fmul {{.*}}<6 x float> %{{.*}}, %{{.*}}
+// CHECK: [[NEG:%.*]] = fneg {{.*}}<6 x float> [[MUL]]
+// CHECK: fadd {{.*}}<6 x float> splat (float 3.000000e+00), [[NEG]]
+float2x3 test_scalar_plus_neg_matrix_mul(float2x3 m) {
+ return 3 + -(m * m);
+}
+
+// CHECK-LABEL: test_scalar_mul_plus_matrix
+// CHECK: [[MUL:%.*]] = fmul {{.*}}float %{{.*}}, %{{.*}}
+// CHECK: [[INSERT:%.*]] = insertelement <6 x float> poison, float [[MUL]], i64 0
+// CHECK: [[SPLAT:%.*]] = shufflevector <6 x float> [[INSERT]], <6 x float> poison, <6 x i32> zeroinitializer
+// CHECK: fadd {{.*}}<6 x float> [[SPLAT]], %{{.*}}
+float2x3 test_scalar_mul_plus_matrix(float2x3 m, float a, float b) {
+ return a * b + m;
+}
+
+// CHECK-LABEL: test_scalar_mul_minus_matrix
+// CHECK: [[MUL:%.*]] = fmul {{.*}}float %{{.*}}, %{{.*}}
+// CHECK: [[INSERT:%.*]] = insertelement <6 x float> poison, float [[MUL]], i64 0
+// CHECK: [[SPLAT:%.*]] = shufflevector <6 x float> [[INSERT]], <6 x float> poison, <6 x i32> zeroinitializer
+// CHECK: fsub {{.*}}<6 x float> [[SPLAT]], %{{.*}}
+float2x3 test_scalar_mul_minus_matrix(float2x3 m, float a, float b) {
+ return a * b - m;
+}
+
+// CHECK-LABEL: test_matrix_plus_scalar_mul
+// CHECK: [[MUL:%.*]] = fmul {{.*}}float %{{.*}}, %{{.*}}
+// CHECK: [[INSERT:%.*]] = insertelement <6 x float> poison, float [[MUL]], i64 0
+// CHECK: [[SPLAT:%.*]] = shufflevector <6 x float> [[INSERT]], <6 x float> poison, <6 x i32> zeroinitializer
+// CHECK: fadd {{.*}}<6 x float> %{{.*}}, [[SPLAT]]
+float2x3 test_matrix_plus_scalar_mul(float2x3 m, float a, float b) {
+ return m + a * b;
+}
+
+// CHECK-LABEL: test_matrix_minus_scalar_mul
+// CHECK: [[MUL:%.*]] = fmul {{.*}}float %{{.*}}, %{{.*}}
+// CHECK: [[INSERT:%.*]] = insertelement <6 x float> poison, float [[MUL]], i64 0
+// CHECK: [[SPLAT:%.*]] = shufflevector <6 x float> [[INSERT]], <6 x float> poison, <6 x i32> zeroinitializer
+// CHECK: fsub {{.*}}<6 x float> %{{.*}}, [[SPLAT]]
+float2x3 test_matrix_minus_scalar_mul(float2x3 m, float a, float b) {
+ return m - a * b;
+}
+
+// CHECK-LABEL: test_neg_scalar_mul_plus_matrix
+// CHECK: [[MUL:%.*]] = fmul {{.*}}float %{{.*}}, %{{.*}}
+// CHECK: [[NEG:%.*]] = fneg {{.*}}float [[MUL]]
+// CHECK: [[INSERT:%.*]] = insertelement <6 x float> poison, float [[NEG]], i64 0
+// CHECK: [[SPLAT:%.*]] = shufflevector <6 x float> [[INSERT]], <6 x float> poison, <6 x i32> zeroinitializer
+// CHECK: fadd {{.*}}<6 x float> [[SPLAT]], %{{.*}}
+float2x3 test_neg_scalar_mul_plus_matrix(float2x3 m, float a, float b) {
+ return -(a * b) + m;
+}
+
+// CHECK-LABEL: test_add_assign_scalar_mul
+// CHECK: [[MUL:%.*]] = fmul {{.*}}float %{{.*}}, %{{.*}}
+// CHECK: [[INSERT:%.*]] = insertelement <6 x float> poison, float [[MUL]], i64 0
+// CHECK: [[SPLAT:%.*]] = shufflevector <6 x float> [[INSERT]], <6 x float> poison, <6 x i32> zeroinitializer
+// CHECK: [[ADD:%.*]] = fadd {{.*}}<6 x float> %{{.*}}, [[SPLAT]]
+// CHECK: store <6 x float> [[ADD]], ptr %m.addr
+float2x3 test_add_assign_scalar_mul(float2x3 m, float a, float b) {
+ m += a * b;
+ return m;
+}
+
+// CHECK-LABEL: test_sub_assign_scalar_mul
+// CHECK: [[MUL:%.*]] = fmul {{.*}}float %{{.*}}, %{{.*}}
+// CHECK: [[INSERT:%.*]] = insertelement <6 x float> poison, float [[MUL]], i64 0
+// CHECK: [[SPLAT:%.*]] = shufflevector <6 x float> [[INSERT]], <6 x float> poison, <6 x i32> zeroinitializer
+// CHECK: [[SUB:%.*]] = fsub {{.*}}<6 x float> %{{.*}}, [[SPLAT]]
+// CHECK: store <6 x float> [[SUB]], ptr %m.addr
+float2x3 test_sub_assign_scalar_mul(float2x3 m, float a, float b) {
+ m -= a * b;
+ return m;
+}
+
+// Make sure operands of the same type are still fused.
+// CHECK-LABEL: test_matrix_mul_plus_matrix
+// CHECK: call {{.*}}<6 x float> @llvm.fmuladd.v6f32(<6 x float> %{{.*}}, <6 x float> splat (float 2.000000e+00), <6 x float> %{{.*}})
+float2x3 test_matrix_mul_plus_matrix(float2x3 m, float2x3 n) {
+ return m * 2 + n;
+}
+
+// A vector add/sub's scalar operand is splatted, so it still fuses.
+// CHECK-LABEL: test_vector_mul_plus_scalar
+// CHECK: call {{.*}}<3 x float> @llvm.fmuladd.v3f32(<3 x float> %{{.*}}, <3 x float> splat (float 2.000000e+00), <3 x float> splat (float 3.000000e+00))
+float3 test_vector_mul_plus_scalar(float3 v) {
+ return v * 2 + 3;
+}
+
+// The float operand is converted to double, so it still fuses.
+// CHECK-LABEL: test_double_mul_plus_float
+// CHECK: [[CONV:%.*]] = fpext {{.*}}float %{{.*}} to double
+// CHECK: call {{.*}}double @llvm.fmuladd.f64(double %{{.*}}, double %{{.*}}, double [[CONV]])
+double test_double_mul_plus_float(double a, double b, float c) {
+ return a * b + c;
+}
More information about the cfe-commits
mailing list