Author: Kaitlin Peng
Date: 2026-11-17T13:59:58-08:00
New Revision: 9f699b1992488e202e891d4978b0ddbf11d4c2c9

URL: 
https://github.com/llvm/llvm-project/commit/9f699b1992488e202e891d4978b0ddbf11d4c2c9
DIFF: 
https://github.com/llvm/llvm-project/commit/9f699b1992488e202e891d4978b0ddbf11d4c2c9.diff

LOG: [HLSL][Matrix] Make `*`, `/`, and negation element-wise on matrices 
(#241993)

Fixes #241086.

This PR makes `*` and `/` between two matrices element-wise, like they
are for vectors, and allows unary `+` and `-` on matrices. It also adds
sema and codegen tests covering these operators.

Assisted-by: Claude Opus 5.5

Added: 
    clang/test/CodeGenHLSL/Operators/matrix-elementwise-operators.hlsl
    clang/test/SemaHLSL/Operators/matrix-elementwise-operators.hlsl

Modified: 
    clang/lib/CodeGen/CGExprScalar.cpp
    clang/lib/Sema/SemaExpr.cpp

Removed: 
    


################################################################################
diff  --git a/clang/lib/CodeGen/CGExprScalar.cpp 
b/clang/lib/CodeGen/CGExprScalar.cpp
index 57f8367d367892..7b698208cacfdb 100644
--- a/clang/lib/CodeGen/CGExprScalar.cpp
+++ b/clang/lib/CodeGen/CGExprScalar.cpp
@@ -874,7 +874,8 @@ class ScalarExprEmitter
       auto *RHSMatTy = dyn_cast<ConstantMatrixType>(
           BO->getRHS()->getType().getCanonicalType());
       CodeGenFunction::CGFPOptionsRAII FPOptsRAII(CGF, Ops.FPFeatures);
-      if (LHSMatTy && RHSMatTy)
+      // In HLSL, matrix * matrix is element-wise, so skip this block.
+      if (LHSMatTy && RHSMatTy && !CGF.getLangOpts().HLSL)
         return MB.CreateMatrixMultiply(Ops.LHS, Ops.RHS, 
LHSMatTy->getNumRows(),
                                        LHSMatTy->getNumColumns(),
                                        RHSMatTy->getNumColumns());
@@ -4342,7 +4343,8 @@ Value *ScalarExprEmitter::EmitDiv(const BinOpInfo &Ops) {
     }
   }
 
-  if (Ops.Ty->isConstantMatrixType()) {
+  // In HLSL, matrix / matrix is element-wise, so skip this block.
+  if (Ops.Ty->isConstantMatrixType() && !Ops.RHS->getType()->isVectorTy()) {
     llvm::MatrixBuilder MB(Builder);
     // We need to check the types of the operands of the operator to get the
     // correct matrix dimensions.

diff  --git a/clang/lib/Sema/SemaExpr.cpp b/clang/lib/Sema/SemaExpr.cpp
index 841f9c9150ade5..d3deecb3ee74c6 100644
--- a/clang/lib/Sema/SemaExpr.cpp
+++ b/clang/lib/Sema/SemaExpr.cpp
@@ -11502,6 +11502,10 @@ QualType Sema::CheckMultiplyDivideOperands(ExprResult 
&LHS, ExprResult &RHS,
   if (LHSTy->isSveVLSBuiltinType() || RHSTy->isSveVLSBuiltinType())
     return CheckSizelessVectorOperands(LHS, RHS, Loc, IsCompAssign,
                                        ArithConvKind::Arithmetic);
+  // In HLSL, multiplying or dividing two matrices is element-wise.
+  if (getLangOpts().HLSL && LHSTy->isConstantMatrixType() &&
+      RHSTy->isConstantMatrixType())
+    return CheckMatrixElementwiseOperands(LHS, RHS, Loc, IsCompAssign);
   if (!IsDiv &&
       (LHSTy->isConstantMatrixType() || RHSTy->isConstantMatrixType()))
     return CheckMatrixMultiplyOperands(LHS, RHS, Loc, IsCompAssign);
@@ -16653,6 +16657,8 @@ ExprResult Sema::CreateBuiltinUnaryOp(SourceLocation 
OpLoc,
         break;
       else if (resultType->isSveVLSBuiltinType()) // SVE vectors allow + and -
         break;
+      else if (getLangOpts().HLSL && resultType->isConstantMatrixType())
+        break;
       else if (getLangOpts().CPlusPlus && // C++ [expr.unary.op]p6
                Opc == UO_Plus && resultType->isPointerType())
         break;

diff  --git 
a/clang/test/CodeGenHLSL/Operators/matrix-elementwise-operators.hlsl 
b/clang/test/CodeGenHLSL/Operators/matrix-elementwise-operators.hlsl
new file mode 100644
index 00000000000000..9c9b70de7cf03d
--- /dev/null
+++ b/clang/test/CodeGenHLSL/Operators/matrix-elementwise-operators.hlsl
@@ -0,0 +1,68 @@
+// RUN: %clang_cc1 -finclude-default-header -triple 
dxil-pc-shadermodel6.6-library %s -emit-llvm -disable-llvm-passes -o - | 
FileCheck %s
+// RUN: %clang_cc1 -finclude-default-header -triple 
spirv-unknown-vulkan-library %s -emit-llvm -disable-llvm-passes -o - | 
FileCheck %s
+
+using namespace hlsl;
+
+// CHECK-LABEL: test_mul_square
+// CHECK: fmul {{.*}}<4 x float> %{{.*}}, %{{.*}}
+float2x2 test_mul_square(float2x2 a, float2x2 b) {
+  return a * b;
+}
+
+// CHECK-LABEL: test_mul_float
+// CHECK: fmul {{.*}}<6 x float> %{{.*}}, %{{.*}}
+float2x3 test_mul_float(float2x3 a, float2x3 b) {
+  return a * b;
+}
+
+// CHECK-LABEL: test_mul_assign
+// CHECK: [[MUL:%.*]] = fmul {{.*}}<6 x float> %{{.*}}, %{{.*}}
+// CHECK: store <6 x float> [[MUL]], ptr %a.addr
+float2x3 test_mul_assign(float2x3 a, float2x3 b) {
+  a *= b;
+  return a;
+}
+
+// CHECK-LABEL: test_mul_int
+// CHECK: mul <6 x i32> %{{.*}}, %{{.*}}
+int2x3 test_mul_int(int2x3 a, int2x3 b) {
+  return a * b;
+}
+
+// CHECK-LABEL: test_div_float
+// CHECK: fdiv {{.*}}<6 x float> %{{.*}}, %{{.*}}
+float2x3 test_div_float(float2x3 a, float2x3 b) {
+  return a / b;
+}
+
+// CHECK-LABEL: test_div_assign
+// CHECK: [[DIV:%.*]] = fdiv {{.*}}<6 x float> %{{.*}}, %{{.*}}
+// CHECK: store <6 x float> [[DIV]], ptr %a.addr
+float2x3 test_div_assign(float2x3 a, float2x3 b) {
+  a /= b;
+  return a;
+}
+
+// CHECK-LABEL: test_div_int
+// CHECK: sdiv <6 x i32> %{{.*}}, %{{.*}}
+int2x3 test_div_int(int2x3 a, int2x3 b) {
+  return a / b;
+}
+
+// CHECK-LABEL: test_div_uint
+// CHECK: udiv <6 x i32> %{{.*}}, %{{.*}}
+uint2x3 test_div_uint(uint2x3 a, uint2x3 b) {
+  return a / b;
+}
+
+// CHECK-LABEL: test_neg_float
+// CHECK: fneg {{.*}}<6 x float> %{{.*}}
+float2x3 test_neg_float(float2x3 a) {
+  return -a;
+}
+
+// CHECK-LABEL: test_neg_int
+// CHECK: sub <6 x i32> zeroinitializer, %{{.*}}
+int2x3 test_neg_int(int2x3 a) {
+  return -a;
+}

diff  --git a/clang/test/SemaHLSL/Operators/matrix-elementwise-operators.hlsl 
b/clang/test/SemaHLSL/Operators/matrix-elementwise-operators.hlsl
new file mode 100644
index 00000000000000..83fdc6b4cb13eb
--- /dev/null
+++ b/clang/test/SemaHLSL/Operators/matrix-elementwise-operators.hlsl
@@ -0,0 +1,64 @@
+// RUN: %clang_cc1 -finclude-default-header -triple 
dxil-pc-shadermodel6.6-library %s -ast-dump -ast-dump-filter=test -verify | 
FileCheck %s
+
+using namespace hlsl;
+
+// CHECK-LABEL: FunctionDecl {{.*}} test_mul 'float2x3 (float2x3, float2x3)'
+// CHECK: BinaryOperator {{.*}} 'float2x3':'matrix<float, 2, 3>' '*'
+// CHECK-NEXT: ImplicitCastExpr {{.*}} 'float2x3':'matrix<float, 2, 3>' 
<LValueToRValue>
+// CHECK-NEXT: DeclRefExpr {{.*}} 'a' 'float2x3':'matrix<float, 2, 3>'
+// CHECK-NEXT: ImplicitCastExpr {{.*}} 'float2x3':'matrix<float, 2, 3>' 
<LValueToRValue>
+// CHECK-NEXT: DeclRefExpr {{.*}} 'b' 'float2x3':'matrix<float, 2, 3>'
+float2x3 test_mul(float2x3 a, float2x3 b) {
+  return a * b;
+}
+
+// CHECK-LABEL: FunctionDecl {{.*}} test_mul_assign 'float2x3 (float2x3, 
float2x3)'
+// CHECK: CompoundAssignOperator {{.*}} 'float2x3':'matrix<float, 2, 3>' 
lvalue '*=' ComputeLHSTy='float2x3':'matrix<float, 2, 3>' 
ComputeResultTy='float2x3':'matrix<float, 2, 3>'
+// CHECK-NEXT: DeclRefExpr {{.*}} 'a' 'float2x3':'matrix<float, 2, 3>'
+// CHECK-NEXT: ImplicitCastExpr {{.*}} 'float2x3':'matrix<float, 2, 3>' 
<LValueToRValue>
+// CHECK-NEXT: DeclRefExpr {{.*}} 'b' 'float2x3':'matrix<float, 2, 3>'
+float2x3 test_mul_assign(float2x3 a, float2x3 b) {
+  a *= b;
+  return a;
+}
+
+// CHECK-LABEL: FunctionDecl {{.*}} test_div 'float2x3 (float2x3, float2x3)'
+// CHECK: BinaryOperator {{.*}} 'float2x3':'matrix<float, 2, 3>' '/'
+// CHECK-NEXT: ImplicitCastExpr {{.*}} 'float2x3':'matrix<float, 2, 3>' 
<LValueToRValue>
+// CHECK-NEXT: DeclRefExpr {{.*}} 'a' 'float2x3':'matrix<float, 2, 3>'
+// CHECK-NEXT: ImplicitCastExpr {{.*}} 'float2x3':'matrix<float, 2, 3>' 
<LValueToRValue>
+// CHECK-NEXT: DeclRefExpr {{.*}} 'b' 'float2x3':'matrix<float, 2, 3>'
+float2x3 test_div(float2x3 a, float2x3 b) {
+  return a / b;
+}
+
+// CHECK-LABEL: FunctionDecl {{.*}} test_div_assign 'float2x3 (float2x3, 
float2x3)'
+// CHECK: CompoundAssignOperator {{.*}} 'float2x3':'matrix<float, 2, 3>' 
lvalue '/=' ComputeLHSTy='float2x3':'matrix<float, 2, 3>' 
ComputeResultTy='float2x3':'matrix<float, 2, 3>'
+// CHECK-NEXT: DeclRefExpr {{.*}} 'a' 'float2x3':'matrix<float, 2, 3>'
+// CHECK-NEXT: ImplicitCastExpr {{.*}} 'float2x3':'matrix<float, 2, 3>' 
<LValueToRValue>
+// CHECK-NEXT: DeclRefExpr {{.*}} 'b' 'float2x3':'matrix<float, 2, 3>'
+float2x3 test_div_assign(float2x3 a, float2x3 b) {
+  a /= b;
+  return a;
+}
+
+// CHECK-LABEL: FunctionDecl {{.*}} test_neg 'float2x3 (float2x3)'
+// CHECK: UnaryOperator {{.*}} 'float2x3':'matrix<float, 2, 3>' prefix '-'
+// CHECK-NEXT: ImplicitCastExpr {{.*}} 'float2x3':'matrix<float, 2, 3>' 
<LValueToRValue>
+// CHECK-NEXT: DeclRefExpr {{.*}} 'a' 'float2x3':'matrix<float, 2, 3>'
+float2x3 test_neg(float2x3 a) {
+  return -a;
+}
+
+// CHECK-LABEL: FunctionDecl {{.*}} test_plus 'float2x3 (float2x3)'
+// CHECK: UnaryOperator {{.*}} 'float2x3':'matrix<float, 2, 3>' prefix '+'
+// CHECK-NEXT: ImplicitCastExpr {{.*}} 'float2x3':'matrix<float, 2, 3>' 
<LValueToRValue>
+// CHECK-NEXT: DeclRefExpr {{.*}} 'a' 'float2x3':'matrix<float, 2, 3>'
+float2x3 test_plus(float2x3 a) {
+  return +a;
+}
+
+// `*` is element-wise, so the operands must have the same dimensions.
+float2x3 test_mul_mismatch(float2x3 a, float3x2 b) {
+  return a * b; // expected-error {{invalid operands to binary expression 
('float2x3' (aka 'matrix<float, 2, 3>') and 'float3x2' (aka 'matrix<float, 3, 
2>'))}}
+}


        
_______________________________________________
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to