Skip to content

[HLSL] Add support for modulo of floating point scalar and vectors #135125

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Apr 10, 2025

Conversation

farzonl
Copy link
Member

@farzonl farzonl commented Apr 10, 2025

fixes #135122

SemaExpr.cpp - Make all doubles fail. Add sema support for float scalars and vectors when language mode is HLSL.
CGExprScalar.cpp - Allow emit frem when language mode is HLSL.

fixes llvm#135122

SemaExpr.cpp - Make all doubles fail. Add sema support for float scalars
and vectors when language mode is HLSL.
CGExprScalar.cpp - Allow emit frem when language mode is HLSL.
@llvmbot llvmbot added clang Clang issues not falling into any other category clang:frontend Language frontend issues, e.g. anything involving "Sema" clang:codegen IR generation bugs: mangling, exceptions, etc. HLSL HLSL Language Support labels Apr 10, 2025
@llvmbot
Copy link
Member

llvmbot commented Apr 10, 2025

@llvm/pr-subscribers-hlsl

@llvm/pr-subscribers-clang

Author: Farzon Lotfi (farzonl)

Changes

fixes #135122

SemaExpr.cpp - Make all doubles fail. Add sema support for float scalars and vectors when language mode is HLSL.
CGExprScalar.cpp - Allow emit frem when language mode is HLSL.


Full diff: https://github.com/llvm/llvm-project/pull/135125.diff

4 Files Affected:

  • (modified) clang/lib/CodeGen/CGExprScalar.cpp (+2)
  • (modified) clang/lib/Sema/SemaExpr.cpp (+25-3)
  • (added) clang/test/CodeGenHLSL/BasicFeatures/frem_modulo.hlsl (+110)
  • (added) clang/test/SemaHLSL/Operators/frem_modulo-errors.hlsl (+41)
diff --git a/clang/lib/CodeGen/CGExprScalar.cpp b/clang/lib/CodeGen/CGExprScalar.cpp
index 28ae56058a7b4..3b25c79701b60 100644
--- a/clang/lib/CodeGen/CGExprScalar.cpp
+++ b/clang/lib/CodeGen/CGExprScalar.cpp
@@ -3964,6 +3964,8 @@ Value *ScalarExprEmitter::EmitRem(const BinOpInfo &Ops) {
 
   if (Ops.Ty->hasUnsignedIntegerRepresentation())
     return Builder.CreateURem(Ops.LHS, Ops.RHS, "rem");
+  else if (CGF.getLangOpts().HLSL && Ops.Ty->hasFloatingRepresentation())
+    return Builder.CreateFRem(Ops.LHS, Ops.RHS, "rem");
   else
     return Builder.CreateSRem(Ops.LHS, Ops.RHS, "rem");
 }
diff --git a/clang/lib/Sema/SemaExpr.cpp b/clang/lib/Sema/SemaExpr.cpp
index e7f418ae6802e..25b7bd0e16942 100644
--- a/clang/lib/Sema/SemaExpr.cpp
+++ b/clang/lib/Sema/SemaExpr.cpp
@@ -10695,10 +10695,30 @@ QualType Sema::CheckRemainderOperands(
   ExprResult &LHS, ExprResult &RHS, SourceLocation Loc, bool IsCompAssign) {
   checkArithmeticNull(*this, LHS, RHS, Loc, /*IsCompare=*/false);
 
+  // Note: This check is here to simplify the double exclusions of
+  // scalar and vector HLSL checks. No getLangOpts().HLSL
+  // is needed since all languages exlcude doubles.
+  if (LHS.get()->getType()->isDoubleType() ||
+      RHS.get()->getType()->isDoubleType() ||
+      (LHS.get()->getType()->isVectorType() && LHS.get()
+                                                   ->getType()
+                                                   ->getAs<VectorType>()
+                                                   ->getElementType()
+                                                   ->isDoubleType()) ||
+      (RHS.get()->getType()->isVectorType() && RHS.get()
+                                                   ->getType()
+                                                   ->getAs<VectorType>()
+                                                   ->getElementType()
+                                                   ->isDoubleType()))
+    return InvalidOperands(Loc, LHS, RHS);
+
   if (LHS.get()->getType()->isVectorType() ||
       RHS.get()->getType()->isVectorType()) {
-    if (LHS.get()->getType()->hasIntegerRepresentation() &&
-        RHS.get()->getType()->hasIntegerRepresentation())
+    if ((LHS.get()->getType()->hasIntegerRepresentation() &&
+         RHS.get()->getType()->hasIntegerRepresentation()) ||
+        (getLangOpts().HLSL &&
+         (LHS.get()->getType()->hasFloatingRepresentation() ||
+          RHS.get()->getType()->hasFloatingRepresentation())))
       return CheckVectorOperands(LHS, RHS, Loc, IsCompAssign,
                                  /*AllowBothBool*/ getLangOpts().AltiVec,
                                  /*AllowBoolConversions*/ false,
@@ -10722,7 +10742,9 @@ QualType Sema::CheckRemainderOperands(
   if (LHS.isInvalid() || RHS.isInvalid())
     return QualType();
 
-  if (compType.isNull() || !compType->isIntegerType())
+  if (compType.isNull() ||
+      (!compType->isIntegerType() &&
+       !(getLangOpts().HLSL && compType->isFloatingType())))
     return InvalidOperands(Loc, LHS, RHS);
   DiagnoseBadDivideOrRemainderValues(*this, LHS, RHS, Loc, false /* IsDiv */);
   return compType;
diff --git a/clang/test/CodeGenHLSL/BasicFeatures/frem_modulo.hlsl b/clang/test/CodeGenHLSL/BasicFeatures/frem_modulo.hlsl
new file mode 100644
index 0000000000000..edc28c5c80b51
--- /dev/null
+++ b/clang/test/CodeGenHLSL/BasicFeatures/frem_modulo.hlsl
@@ -0,0 +1,110 @@
+// RUN: %clang_cc1 -finclude-default-header -triple dxil-pc-shadermodel6.3-library %s \
+// RUN:  -fnative-half-type -emit-llvm -disable-llvm-passes -o - | \
+// RUN:  FileCheck %s
+// RUN: %clang_cc1 -finclude-default-header -triple spirv-unknown-vulkan-compute %s \
+// RUN:  -fnative-half-type -emit-llvm -disable-llvm-passes -o - | \
+// RUN:  FileCheck %s
+
+ half2 half_vec_mod_by_int(half2 p1) {
+// CHECK-LABEL: half_vec_mod_by_int
+// CHECK: %rem = frem reassoc nnan ninf nsz arcp afn <2 x half> %{{.*}}, splat (half 0xH4000)
+    return  p1 % 2;
+}
+
+ half2 half_vec_mod_by_float(half2 p1) {
+// CHECK-LABEL: half_vec_mod_by_float
+// CHECK: %rem = frem reassoc nnan ninf nsz arcp afn <2 x half> %{{.*}}, splat (half 0xH4000)
+    return  p1 % (half)2.0;
+}
+
+ half2 half_vec_mod_by_half(half2 p1, half p2 ) {
+// CHECK-LABEL: half_vec_mod_by_half
+// CHECK:  %splat.splatinsert = insertelement <2 x half> poison, half %{{.*}}, i64 0
+// CHECK:  %splat.splat = shufflevector <2 x half> %splat.splatinsert, <2 x half> poison, <2 x i32> zeroinitializer
+// CHECK:  %rem = frem reassoc nnan ninf nsz arcp afn <2 x half> %{{.*}}, %splat.splat
+    return  p1 % p2;
+}
+
+ half2 half_vec_mod_by_half_vec(half2 p1, half2 p2 ) {
+// CHECK-LABEL: half_vec_mod_by_half_vec
+// CHECK: %rem = frem reassoc nnan ninf nsz arcp afn <2 x half> %{{.*}}, %{{.*}}
+    return  p1 % p2;
+}
+
+ half half_vec_mod_by_int(half p1) {
+// CHECK-LABEL: half_vec_mod_by_int
+// CHECK: %rem = frem reassoc nnan ninf nsz arcp afn half  %{{.*}}, 0xH4000
+    return  p1 % 2;
+}
+
+ half half_mod_by_float(half p1) {
+// CHECK-LABEL: half_mod_by_float
+// CHECK: %rem = frem reassoc nnan ninf nsz arcp afn half  %{{.*}}, 0xH4000
+    return  p1 % (half)2.0;
+}
+
+ half half_mod_by_half(half p1, half p2 ) {
+// CHECK-LABEL: half_mod_by_half
+// CHECK: %rem = frem reassoc nnan ninf nsz arcp afn half %{{.*}}, %{{.*}}
+    return  p1 % p2;
+}
+
+ half half_mod_by_half_vec(half p1, half2 p2 ) {
+// CHECK-LABEL: half_mod_by_half_vec
+// CHECK: %splat.splatinsert = insertelement <2 x half> poison, half %{{.*}}, i64 0
+// CHECK: %splat.splat = shufflevector <2 x half> %splat.splatinsert, <2 x half> poison, <2 x i32> zeroinitializer
+// CHECK: %rem = frem reassoc nnan ninf nsz arcp afn <2 x half> %splat.splat, %{{.*}}
+    return  p1 % p2;
+}
+
+ float2 float_vec_mod_by_int(float2 p1) {
+// CHECK-LABEL: float_vec_mod_by_int
+// CHECK: %rem = frem reassoc nnan ninf nsz arcp afn <2 x float> %{{.*}}, splat (float 2.000000e+00)
+    return  p1 % 2;
+}
+
+ float2 float_vec_mod_by_float_const(float2 p1) {
+// CHECK-LABEL: float_vec_mod_by_float_const
+// CHECK: %rem = frem reassoc nnan ninf nsz arcp afn <2 x float> %{{.*}}, splat (float 2.000000e+00)
+    return  p1 % 2.0;
+}
+
+ float2 float_vec_mod_by_float(float2 p1, float p2 ) {
+// CHECK-LABEL: float_vec_mod_by_float
+// CHECK:  %splat.splatinsert = insertelement <2 x float> poison, float %{{.*}}, i64 0
+// CHECK:  %splat.splat = shufflevector <2 x float> %splat.splatinsert, <2 x float> poison, <2 x i32> zeroinitializer
+// CHECK:  %rem = frem reassoc nnan ninf nsz arcp afn <2 x float> %{{.*}}, %splat.splat
+    return  p1 % p2;
+}
+
+ float2 float_vec_mod_by_float_vec(float2 p1, float2 p2 ) {
+// CHECK-LABEL: float_vec_mod_by_float_vec
+// CHECK: %rem = frem reassoc nnan ninf nsz arcp afn <2 x float> %{{.*}}, %{{.*}}
+    return  p1 % p2;
+}
+
+ float float_mod_by_int(float p1) {
+// CHECK-LABEL: float_mod_by_int
+// CHECK: %rem = frem reassoc nnan ninf nsz arcp afn float %{{.*}}, 2.000000e+00
+    return  p1 % 2;
+}
+
+ float float_mod_by_float_const(float p1) {
+// CHECK-LABEL: float_mod_by_float_const
+// CHECK: %rem = frem reassoc nnan ninf nsz arcp afn float %{{.*}}, 2.000000e+00
+    return  p1 % 2.0;
+}
+
+ float float_mod_by_float(float p1, float p2 ) {
+// CHECK-LABEL: float_mod_by_float
+// CHECK: %rem = frem reassoc nnan ninf nsz arcp afn float %{{.*}}, %{{.*}}
+    return  p1 % p2;
+}
+
+ float float_mod_by_float_vec(float p1, float2 p2 ) {
+// CHECK-LABEL: float_mod_by_float_vec
+// CHECK: %splat.splatinsert = insertelement <2 x float> poison, float %{{.*}}, i64 0
+// CHECK: %splat.splat = shufflevector <2 x float> %splat.splatinsert, <2 x float> poison, <2 x i32> zeroinitializer
+// CHECK: %rem = frem reassoc nnan ninf nsz arcp afn <2 x float> %splat.splat, %{{.*}}
+    return  p1 % p2;
+}
diff --git a/clang/test/SemaHLSL/Operators/frem_modulo-errors.hlsl b/clang/test/SemaHLSL/Operators/frem_modulo-errors.hlsl
new file mode 100644
index 0000000000000..1c6e45693a12f
--- /dev/null
+++ b/clang/test/SemaHLSL/Operators/frem_modulo-errors.hlsl
@@ -0,0 +1,41 @@
+// RUN: %clang_cc1 -finclude-default-header -triple dxil-pc-shadermodel6.6-library %s -fnative-half-type -emit-llvm-only -disable-llvm-passes -verify
+
+export double2 double_vec_mod_by_int(double2 p1) {
+    return  p1 % 2;
+    // expected-error@-1 {{invalid operands to binary expression ('double2' (aka 'vector<double, 2>') and 'int')}}
+}
+
+export double2 double_vec_mod_by_float(double2 p1) {
+    return  p1 % 2.0;
+    // expected-error@-1 {{invalid operands to binary expression ('double2' (aka 'vector<double, 2>') and 'float')}}
+}
+
+export double2 double_vec_mod_by_double(double2 p1, double p2 ) {
+    return  p1 % p2;
+    // expected-error@-1 {{invalid operands to binary expression ('double2' (aka 'vector<double, 2>') and 'double')}}
+}
+
+export double2 double_vec_mod_by_double_vec(double2 p1, double2 p2 ) {
+    return  p1 % p2;
+    // expected-error@-1 {{invalid operands to binary expression ('double2' (aka 'vector<double, 2>') and 'double2')}}
+}
+
+export double double_mod_by_int(double p1) {
+    return  p1 % 2;
+    // expected-error@-1 {{invalid operands to binary expression ('double' and 'int')}}
+}
+
+export double double_mod_by_float(double p1) {
+    return  p1 % 2.0;
+    // expected-error@-1 {{invalid operands to binary expression ('double' and 'float')}}
+}
+
+export double double_mod_by_double(double p1, double p2 ) {
+    return  p1 % p2;
+    // expected-error@-1 {{invalid operands to binary expression ('double' and 'double')}}
+}
+
+export double double_mod_by_double_vec(double p1, double2 p2 ) {
+    return  p1 % p2;
+    // expected-error@-1 {{invalid operands to binary expression ('double' and 'double2' (aka 'vector<double, 2>'))}}
+}

@llvmbot
Copy link
Member

llvmbot commented Apr 10, 2025

@llvm/pr-subscribers-clang-codegen

Author: Farzon Lotfi (farzonl)

Changes

fixes #135122

SemaExpr.cpp - Make all doubles fail. Add sema support for float scalars and vectors when language mode is HLSL.
CGExprScalar.cpp - Allow emit frem when language mode is HLSL.


Full diff: https://github.com/llvm/llvm-project/pull/135125.diff

4 Files Affected:

  • (modified) clang/lib/CodeGen/CGExprScalar.cpp (+2)
  • (modified) clang/lib/Sema/SemaExpr.cpp (+25-3)
  • (added) clang/test/CodeGenHLSL/BasicFeatures/frem_modulo.hlsl (+110)
  • (added) clang/test/SemaHLSL/Operators/frem_modulo-errors.hlsl (+41)
diff --git a/clang/lib/CodeGen/CGExprScalar.cpp b/clang/lib/CodeGen/CGExprScalar.cpp
index 28ae56058a7b4..3b25c79701b60 100644
--- a/clang/lib/CodeGen/CGExprScalar.cpp
+++ b/clang/lib/CodeGen/CGExprScalar.cpp
@@ -3964,6 +3964,8 @@ Value *ScalarExprEmitter::EmitRem(const BinOpInfo &Ops) {
 
   if (Ops.Ty->hasUnsignedIntegerRepresentation())
     return Builder.CreateURem(Ops.LHS, Ops.RHS, "rem");
+  else if (CGF.getLangOpts().HLSL && Ops.Ty->hasFloatingRepresentation())
+    return Builder.CreateFRem(Ops.LHS, Ops.RHS, "rem");
   else
     return Builder.CreateSRem(Ops.LHS, Ops.RHS, "rem");
 }
diff --git a/clang/lib/Sema/SemaExpr.cpp b/clang/lib/Sema/SemaExpr.cpp
index e7f418ae6802e..25b7bd0e16942 100644
--- a/clang/lib/Sema/SemaExpr.cpp
+++ b/clang/lib/Sema/SemaExpr.cpp
@@ -10695,10 +10695,30 @@ QualType Sema::CheckRemainderOperands(
   ExprResult &LHS, ExprResult &RHS, SourceLocation Loc, bool IsCompAssign) {
   checkArithmeticNull(*this, LHS, RHS, Loc, /*IsCompare=*/false);
 
+  // Note: This check is here to simplify the double exclusions of
+  // scalar and vector HLSL checks. No getLangOpts().HLSL
+  // is needed since all languages exlcude doubles.
+  if (LHS.get()->getType()->isDoubleType() ||
+      RHS.get()->getType()->isDoubleType() ||
+      (LHS.get()->getType()->isVectorType() && LHS.get()
+                                                   ->getType()
+                                                   ->getAs<VectorType>()
+                                                   ->getElementType()
+                                                   ->isDoubleType()) ||
+      (RHS.get()->getType()->isVectorType() && RHS.get()
+                                                   ->getType()
+                                                   ->getAs<VectorType>()
+                                                   ->getElementType()
+                                                   ->isDoubleType()))
+    return InvalidOperands(Loc, LHS, RHS);
+
   if (LHS.get()->getType()->isVectorType() ||
       RHS.get()->getType()->isVectorType()) {
-    if (LHS.get()->getType()->hasIntegerRepresentation() &&
-        RHS.get()->getType()->hasIntegerRepresentation())
+    if ((LHS.get()->getType()->hasIntegerRepresentation() &&
+         RHS.get()->getType()->hasIntegerRepresentation()) ||
+        (getLangOpts().HLSL &&
+         (LHS.get()->getType()->hasFloatingRepresentation() ||
+          RHS.get()->getType()->hasFloatingRepresentation())))
       return CheckVectorOperands(LHS, RHS, Loc, IsCompAssign,
                                  /*AllowBothBool*/ getLangOpts().AltiVec,
                                  /*AllowBoolConversions*/ false,
@@ -10722,7 +10742,9 @@ QualType Sema::CheckRemainderOperands(
   if (LHS.isInvalid() || RHS.isInvalid())
     return QualType();
 
-  if (compType.isNull() || !compType->isIntegerType())
+  if (compType.isNull() ||
+      (!compType->isIntegerType() &&
+       !(getLangOpts().HLSL && compType->isFloatingType())))
     return InvalidOperands(Loc, LHS, RHS);
   DiagnoseBadDivideOrRemainderValues(*this, LHS, RHS, Loc, false /* IsDiv */);
   return compType;
diff --git a/clang/test/CodeGenHLSL/BasicFeatures/frem_modulo.hlsl b/clang/test/CodeGenHLSL/BasicFeatures/frem_modulo.hlsl
new file mode 100644
index 0000000000000..edc28c5c80b51
--- /dev/null
+++ b/clang/test/CodeGenHLSL/BasicFeatures/frem_modulo.hlsl
@@ -0,0 +1,110 @@
+// RUN: %clang_cc1 -finclude-default-header -triple dxil-pc-shadermodel6.3-library %s \
+// RUN:  -fnative-half-type -emit-llvm -disable-llvm-passes -o - | \
+// RUN:  FileCheck %s
+// RUN: %clang_cc1 -finclude-default-header -triple spirv-unknown-vulkan-compute %s \
+// RUN:  -fnative-half-type -emit-llvm -disable-llvm-passes -o - | \
+// RUN:  FileCheck %s
+
+ half2 half_vec_mod_by_int(half2 p1) {
+// CHECK-LABEL: half_vec_mod_by_int
+// CHECK: %rem = frem reassoc nnan ninf nsz arcp afn <2 x half> %{{.*}}, splat (half 0xH4000)
+    return  p1 % 2;
+}
+
+ half2 half_vec_mod_by_float(half2 p1) {
+// CHECK-LABEL: half_vec_mod_by_float
+// CHECK: %rem = frem reassoc nnan ninf nsz arcp afn <2 x half> %{{.*}}, splat (half 0xH4000)
+    return  p1 % (half)2.0;
+}
+
+ half2 half_vec_mod_by_half(half2 p1, half p2 ) {
+// CHECK-LABEL: half_vec_mod_by_half
+// CHECK:  %splat.splatinsert = insertelement <2 x half> poison, half %{{.*}}, i64 0
+// CHECK:  %splat.splat = shufflevector <2 x half> %splat.splatinsert, <2 x half> poison, <2 x i32> zeroinitializer
+// CHECK:  %rem = frem reassoc nnan ninf nsz arcp afn <2 x half> %{{.*}}, %splat.splat
+    return  p1 % p2;
+}
+
+ half2 half_vec_mod_by_half_vec(half2 p1, half2 p2 ) {
+// CHECK-LABEL: half_vec_mod_by_half_vec
+// CHECK: %rem = frem reassoc nnan ninf nsz arcp afn <2 x half> %{{.*}}, %{{.*}}
+    return  p1 % p2;
+}
+
+ half half_vec_mod_by_int(half p1) {
+// CHECK-LABEL: half_vec_mod_by_int
+// CHECK: %rem = frem reassoc nnan ninf nsz arcp afn half  %{{.*}}, 0xH4000
+    return  p1 % 2;
+}
+
+ half half_mod_by_float(half p1) {
+// CHECK-LABEL: half_mod_by_float
+// CHECK: %rem = frem reassoc nnan ninf nsz arcp afn half  %{{.*}}, 0xH4000
+    return  p1 % (half)2.0;
+}
+
+ half half_mod_by_half(half p1, half p2 ) {
+// CHECK-LABEL: half_mod_by_half
+// CHECK: %rem = frem reassoc nnan ninf nsz arcp afn half %{{.*}}, %{{.*}}
+    return  p1 % p2;
+}
+
+ half half_mod_by_half_vec(half p1, half2 p2 ) {
+// CHECK-LABEL: half_mod_by_half_vec
+// CHECK: %splat.splatinsert = insertelement <2 x half> poison, half %{{.*}}, i64 0
+// CHECK: %splat.splat = shufflevector <2 x half> %splat.splatinsert, <2 x half> poison, <2 x i32> zeroinitializer
+// CHECK: %rem = frem reassoc nnan ninf nsz arcp afn <2 x half> %splat.splat, %{{.*}}
+    return  p1 % p2;
+}
+
+ float2 float_vec_mod_by_int(float2 p1) {
+// CHECK-LABEL: float_vec_mod_by_int
+// CHECK: %rem = frem reassoc nnan ninf nsz arcp afn <2 x float> %{{.*}}, splat (float 2.000000e+00)
+    return  p1 % 2;
+}
+
+ float2 float_vec_mod_by_float_const(float2 p1) {
+// CHECK-LABEL: float_vec_mod_by_float_const
+// CHECK: %rem = frem reassoc nnan ninf nsz arcp afn <2 x float> %{{.*}}, splat (float 2.000000e+00)
+    return  p1 % 2.0;
+}
+
+ float2 float_vec_mod_by_float(float2 p1, float p2 ) {
+// CHECK-LABEL: float_vec_mod_by_float
+// CHECK:  %splat.splatinsert = insertelement <2 x float> poison, float %{{.*}}, i64 0
+// CHECK:  %splat.splat = shufflevector <2 x float> %splat.splatinsert, <2 x float> poison, <2 x i32> zeroinitializer
+// CHECK:  %rem = frem reassoc nnan ninf nsz arcp afn <2 x float> %{{.*}}, %splat.splat
+    return  p1 % p2;
+}
+
+ float2 float_vec_mod_by_float_vec(float2 p1, float2 p2 ) {
+// CHECK-LABEL: float_vec_mod_by_float_vec
+// CHECK: %rem = frem reassoc nnan ninf nsz arcp afn <2 x float> %{{.*}}, %{{.*}}
+    return  p1 % p2;
+}
+
+ float float_mod_by_int(float p1) {
+// CHECK-LABEL: float_mod_by_int
+// CHECK: %rem = frem reassoc nnan ninf nsz arcp afn float %{{.*}}, 2.000000e+00
+    return  p1 % 2;
+}
+
+ float float_mod_by_float_const(float p1) {
+// CHECK-LABEL: float_mod_by_float_const
+// CHECK: %rem = frem reassoc nnan ninf nsz arcp afn float %{{.*}}, 2.000000e+00
+    return  p1 % 2.0;
+}
+
+ float float_mod_by_float(float p1, float p2 ) {
+// CHECK-LABEL: float_mod_by_float
+// CHECK: %rem = frem reassoc nnan ninf nsz arcp afn float %{{.*}}, %{{.*}}
+    return  p1 % p2;
+}
+
+ float float_mod_by_float_vec(float p1, float2 p2 ) {
+// CHECK-LABEL: float_mod_by_float_vec
+// CHECK: %splat.splatinsert = insertelement <2 x float> poison, float %{{.*}}, i64 0
+// CHECK: %splat.splat = shufflevector <2 x float> %splat.splatinsert, <2 x float> poison, <2 x i32> zeroinitializer
+// CHECK: %rem = frem reassoc nnan ninf nsz arcp afn <2 x float> %splat.splat, %{{.*}}
+    return  p1 % p2;
+}
diff --git a/clang/test/SemaHLSL/Operators/frem_modulo-errors.hlsl b/clang/test/SemaHLSL/Operators/frem_modulo-errors.hlsl
new file mode 100644
index 0000000000000..1c6e45693a12f
--- /dev/null
+++ b/clang/test/SemaHLSL/Operators/frem_modulo-errors.hlsl
@@ -0,0 +1,41 @@
+// RUN: %clang_cc1 -finclude-default-header -triple dxil-pc-shadermodel6.6-library %s -fnative-half-type -emit-llvm-only -disable-llvm-passes -verify
+
+export double2 double_vec_mod_by_int(double2 p1) {
+    return  p1 % 2;
+    // expected-error@-1 {{invalid operands to binary expression ('double2' (aka 'vector<double, 2>') and 'int')}}
+}
+
+export double2 double_vec_mod_by_float(double2 p1) {
+    return  p1 % 2.0;
+    // expected-error@-1 {{invalid operands to binary expression ('double2' (aka 'vector<double, 2>') and 'float')}}
+}
+
+export double2 double_vec_mod_by_double(double2 p1, double p2 ) {
+    return  p1 % p2;
+    // expected-error@-1 {{invalid operands to binary expression ('double2' (aka 'vector<double, 2>') and 'double')}}
+}
+
+export double2 double_vec_mod_by_double_vec(double2 p1, double2 p2 ) {
+    return  p1 % p2;
+    // expected-error@-1 {{invalid operands to binary expression ('double2' (aka 'vector<double, 2>') and 'double2')}}
+}
+
+export double double_mod_by_int(double p1) {
+    return  p1 % 2;
+    // expected-error@-1 {{invalid operands to binary expression ('double' and 'int')}}
+}
+
+export double double_mod_by_float(double p1) {
+    return  p1 % 2.0;
+    // expected-error@-1 {{invalid operands to binary expression ('double' and 'float')}}
+}
+
+export double double_mod_by_double(double p1, double p2 ) {
+    return  p1 % p2;
+    // expected-error@-1 {{invalid operands to binary expression ('double' and 'double')}}
+}
+
+export double double_mod_by_double_vec(double p1, double2 p2 ) {
+    return  p1 % p2;
+    // expected-error@-1 {{invalid operands to binary expression ('double' and 'double2' (aka 'vector<double, 2>'))}}
+}

half half_vec_mod_by_int(half p1) {
// CHECK-LABEL: half_vec_mod_by_int
// CHECK: %rem = frem reassoc nnan ninf nsz arcp afn half %{{.*}}, 0xH4000
return p1 % 2;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this wasn't abiguous?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thats a Good question. so no it isn't ambigious because in SemaExpr::CheckRemainderOperands if we get into either the CheckVectorOperands for the vector cases or UsualArithmeticConversions for the scalar cases. the integer literal go through a conversion to match the non literal type. I saw ints become halfs or floats depending on what the variable type was.

The float literal however did not do this. The float literal would cast the half variables to a float so to prevent this in the testing i cast the float literals to half. If thats a bug I'll try an address it in a future pr.

@farzonl farzonl merged commit 589e1c7 into llvm:main Apr 10, 2025
11 checks passed
var-const pushed a commit to ldionne/llvm-project that referenced this pull request Apr 17, 2025
…lvm#135125)

fixes llvm#135122

SemaExpr.cpp - Make all doubles fail. Add sema support for float scalars
and vectors when language mode is HLSL.
CGExprScalar.cpp - Allow emit frem when language mode is HLSL.
@damyanp damyanp moved this to Closed in HLSL Support Apr 25, 2025
@damyanp damyanp removed this from HLSL Support Jun 25, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
clang:codegen IR generation bugs: mangling, exceptions, etc. clang:frontend Language frontend issues, e.g. anything involving "Sema" clang Clang issues not falling into any other category HLSL HLSL Language Support
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[HLSL] Add support for floating point modulo
4 participants