[clang] [HLSL] Add "or" intrinsic (PR #128979)
via cfe-commits
cfe-commits at lists.llvm.org
Thu Feb 27 11:50:55 PST 2025
https://github.com/metkarpoonam updated https://github.com/llvm/llvm-project/pull/128979
>From 08e191213d3abfc6a5f2af7ba3db35055dd040eb Mon Sep 17 00:00:00 2001
From: Poonam Vilas Metkar <poonammetkar at microsoft.com>
Date: Wed, 26 Feb 2025 16:23:01 -0800
Subject: [PATCH 01/10] Format hlsl_intrinsics.h with clang-format
---
clang/lib/Headers/hlsl/hlsl_intrinsics.h | 18 ++++++++++++++++++
1 file changed, 18 insertions(+)
diff --git a/clang/lib/Headers/hlsl/hlsl_intrinsics.h b/clang/lib/Headers/hlsl/hlsl_intrinsics.h
index 239d7a3f59b77..764cf671dce31 100644
--- a/clang/lib/Headers/hlsl/hlsl_intrinsics.h
+++ b/clang/lib/Headers/hlsl/hlsl_intrinsics.h
@@ -290,6 +290,24 @@ _HLSL_BUILTIN_ALIAS(__builtin_hlsl_and)
bool4 and(bool4 x, bool4 y);
// clang-format on
+//===----------------------------------------------------------------------===//
+// or builtins
+//===----------------------------------------------------------------------===//
+
+/// \fn T or(T x, T y)
+/// \brief Returns the bitwise OR of the two input values, \a x and \a y.
+/// \param x The first input value and y The second input value.
+///
+/// \returns The logically OR a vector and retuens bool vector.
+
+_HLSL_BUILTIN_ALIAS(__builtin_hlsl_or)
+bool or(bool, bool);
+_HLSL_BUILTIN_ALIAS(__builtin_hlsl_or)
+bool2 or(bool2, bool2);
+_HLSL_BUILTIN_ALIAS(__builtin_hlsl_or)
+bool3 or(bool3, bool3);
+_HLSL_BUILTIN_ALIAS(__builtin_hlsl_or)
+bool4 or(bool4, bool4);
//===----------------------------------------------------------------------===//
// any builtins
//===----------------------------------------------------------------------===//
>From eae4c9f114f43e314146ecd5569c631188f93bff Mon Sep 17 00:00:00 2001
From: Poonam Vilas Metkar <poonammetkar at microsoft.com>
Date: Wed, 26 Feb 2025 16:36:50 -0800
Subject: [PATCH 02/10] Include HLSL or_intrinsic, add codegen in CGBuiltin,
and the corresponding tests in or.hlsl. Additionally, incorporate
logical-operator-errors to handle both 'and' and 'or' semantic diagnostics.
---
clang/include/clang/Basic/Builtins.td | 6 ++
clang/lib/CodeGen/CGBuiltin.cpp | 5 ++
clang/lib/Sema/SemaHLSL.cpp | 19 +++++
clang/test/CodeGenHLSL/builtins/or.hlsl | 85 +++++++++++++++++++
.../SemaHLSL/logical-operator-errors.hlsl | 27 ++++++
5 files changed, 142 insertions(+)
create mode 100644 clang/test/CodeGenHLSL/builtins/or.hlsl
create mode 100644 clang/test/SemaHLSL/logical-operator-errors.hlsl
diff --git a/clang/include/clang/Basic/Builtins.td b/clang/include/clang/Basic/Builtins.td
index 598ae171b1389..f7027331cd6c5 100644
--- a/clang/include/clang/Basic/Builtins.td
+++ b/clang/include/clang/Basic/Builtins.td
@@ -4783,6 +4783,12 @@ def HLSLAnd : LangBuiltin<"HLSL_LANG"> {
let Prototype = "void(...)";
}
+def HLSLOr : LangBuiltin<"HLSL_LANG"> {
+ let Spellings = ["__builtin_hlsl_or"];
+ let Attributes = [NoThrow, Const];
+ let Prototype = "void(...)";
+}
+
def HLSLAny : LangBuiltin<"HLSL_LANG"> {
let Spellings = ["__builtin_hlsl_any"];
let Attributes = [NoThrow, Const];
diff --git a/clang/lib/CodeGen/CGBuiltin.cpp b/clang/lib/CodeGen/CGBuiltin.cpp
index 65fac01d58362..599e05819ec7c 100644
--- a/clang/lib/CodeGen/CGBuiltin.cpp
+++ b/clang/lib/CodeGen/CGBuiltin.cpp
@@ -19492,6 +19492,11 @@ Value *CodeGenFunction::EmitHLSLBuiltinExpr(unsigned BuiltinID,
Value *Op1 = EmitScalarExpr(E->getArg(1));
return Builder.CreateAnd(Op0, Op1, "hlsl.and");
}
+ case Builtin::BI__builtin_hlsl_or: {
+ Value *Op0 = EmitScalarExpr(E->getArg(0));
+ Value *Op1 = EmitScalarExpr(E->getArg(1));
+ return Builder.CreateOr(Op0, Op1, "hlsl.or");
+ }
case Builtin::BI__builtin_hlsl_any: {
Value *Op0 = EmitScalarExpr(E->getArg(0));
return Builder.CreateIntrinsic(
diff --git a/clang/lib/Sema/SemaHLSL.cpp b/clang/lib/Sema/SemaHLSL.cpp
index 283a9801fc707..c48512673be58 100644
--- a/clang/lib/Sema/SemaHLSL.cpp
+++ b/clang/lib/Sema/SemaHLSL.cpp
@@ -2305,6 +2305,25 @@ bool SemaHLSL::CheckBuiltinFunctionCall(unsigned BuiltinID, CallExpr *TheCall) {
TheCall->setType(ArgTyA);
break;
}
+ case Builtin::BI__builtin_hlsl_or: {
+ if (SemaRef.checkArgCount(TheCall, 2))
+ return true;
+ if (CheckVectorElementCallArgs(&SemaRef, TheCall))
+ return true;
+
+ // Ensure input expr type is a scalar/vector and the same as the return type
+ if (CheckScalarOrVector(&SemaRef, TheCall, getASTContext().BoolTy, 0))
+ return true;
+
+ // Ensure input parameter type is bool
+ ExprResult A = TheCall->getArg(0);
+ QualType ArgTyA = A.get()->getType();
+
+ // return type is the same as the input type
+ TheCall->setType(ArgTyA);
+
+ break;
+ }
case Builtin::BI__builtin_hlsl_all:
case Builtin::BI__builtin_hlsl_any: {
if (SemaRef.checkArgCount(TheCall, 1))
diff --git a/clang/test/CodeGenHLSL/builtins/or.hlsl b/clang/test/CodeGenHLSL/builtins/or.hlsl
new file mode 100644
index 0000000000000..0c21973bda17c
--- /dev/null
+++ b/clang/test/CodeGenHLSL/builtins/or.hlsl
@@ -0,0 +1,85 @@
+// RUN: %clang_cc1 -finclude-default-header -triple \
+// RUN: dxil-pc-shadermodel6.3-library %s \
+// RUN: -emit-llvm -O1 -o - | FileCheck %s
+
+//CHECK-LABEL: define noundef i1 @_Z12test_or_boolbb(
+//CHECK-SAME: i1 noundef [[X:%.*]], i1 noundef [[Y:%.*]]) local_unnamed_addr #[[ATTR0:[0-9]+]] {
+//CHECK-NEXT: [[ENTRY:.*:]]
+//CHECK-NEXT: [[HLSL_OR:%.*]] = or i1 [[x]], [[y]]
+//CHECK-NEXT: ret i1 [[HLSL_OR]]
+//CHECK_NEXT: }
+bool test_or_bool(bool x, bool y)
+{
+ return or(x, y);
+
+}
+
+//CHECK-LABEL: define noundef <2 x i1> @_Z13test_or_bool2Dv2_bS_(
+//CHECK-SAME: <2 x i1> noundef [[X:%.*]], <2 x i1> noundef [[Y:%.*]]) local_unnamed_addr #[[ATTR0]] {
+//CHECK-NEXT: [[ENTRY:.*:]]
+//CHECK-NEXT: [[HLSL_OR:%.*]] = or <2 xi1> [[x]], [[y]]
+//CHECK-NEXT: ret <2 x i1> [[HLSL_OR]]
+//CHECK_NEXT: }
+bool2 test_or_bool2(bool2 x, bool2 y)
+{
+ return or(x, y);
+}
+
+//CHECK-LABEL: define noundef <3 x i1> @_Z13test_or_bool3Dv3_bS_(
+//CHECK-SAME: <3 x i1> noundef [[X:%.*]], <3 x i1> noundef [[Y:%.*]]) local_unnamed_addr #[[ATTR0]] {
+//CHECK-NEXT: [[ENTRY:.*:]]
+//CHECK-NEXT: [[HLSL_OR:%.*]] = or <3 xi1> [[x]], [[y]]
+//CHECK-NEXT: ret <3 x i1> [[HLSL_OR]]
+//CHECK_NEXT: }
+bool3 test_or_bool3(bool3 x, bool3 y)
+{
+ return or(x, y);
+}
+
+//CHECK-LABEL: define noundef <4 x i1> @_Z13test_or_bool4Dv4_bS_(
+//CHECK-SAME: <4 x i1> noundef [[X:%.*]], <4 x i1> noundef [[Y:%.*]]) local_unnamed_addr #[[ATTR0]] {
+//CHECK-NEXT: [[ENTRY:.*:]]
+//CHECK-NEXT: [[HLSL_OR:%.*]] = or <4 xi1> [[x]], [[y]]
+//CHECK-NEXT: ret <4 x i1> [[HLSL_OR]]
+//CHECK_NEXT: }
+bool4 test_or_bool4(bool4 x, bool4 y)
+{
+ return or(x, y);
+}
+
+//CHECK-LABEL: define noundef i1 @_Z11test_or_intii(
+//CHECK-SAME: i32 noundef [[X:%.*]], i32 noundef [[Y:%.*]]) local_unnamed_addr #[[ATTR0]] {
+//CHECK-NEXT: [[ENTRY:.*:]]
+//CHECK_NEXT: [[0:%.*]] = or i32 [[y]], [[x]]
+//CHECK-NEXT: [[HLSL_OR:%.*]] = icmp ne i32 [[0]], 0
+//CHECK-NEXT: ret i1 [[HLSL_OR]]
+//CHECK_NEXT: }
+bool test_or_int(int x, int y)
+{
+ return or(x, y);
+}
+
+//CHECK-LABEL: define noundef <4 x i1> @_Z12test_or_int4Dv4_iS_(
+//CHECK-SAME: <4 x i32> noundef [[X:%.*]], <4 x i32> noundef [[Y:%.*]]) local_unnamed_addr #[[ATTR0]] {
+//CHECK-NEXT: [[ENTRY:.*:]]
+//CHECK_NEXT: [[0:%.*]] = or <4 x i32> [[y]], [[x]]
+//CHECK-NEXT: [[HLSL_OR:%.*]] = icmp ne <4 x i32> [[0]], zeroinitializer
+//CHECK-NEXT: ret <4 x i1> [[HLSL_OR]]
+//CHECK_NEXT: }
+bool4 test_or_int4(int4 x, int4 y)
+{
+ return or(x, y);
+}
+
+//CHECK-LABEL: noundef <4 x i1> @_Z14test_or_float4Dv4_fS_(
+//CHECK-SAME: <4 x float> noundef nofpclass(nan inf) [[X:%.*]], <4 x float> noundef nofpclass(nan inf) [[Y:%.*]]) local_unnamed_addr #[[ATTR0]] {
+//CHECK-NEXT: [[ENTRY:.*:]]
+//CHECK-NEXT: [[TOBOOL:%.*]] = fcmp reassoc nnan ninf nsz arcp afn une <4 x float> [[X]], zeroinitializer
+//CHECK-NEXT: [[TOBOOL1:%.*]] = fcmp reassoc nnan ninf nsz arcp afn une <4 x float> [[Y]], zeroinitializer
+//CHECK-NEXT: [[HLSL_OR:%.*]] = or <4 x i1> [[TOBOOL]], [[TOBOOL1]]
+//CHECK-NEXT: ret <4 x i1> [[HLSL_OR]]
+//CHECK_NEXT: }
+bool4 test_or_float4(float4 x, float4 y)
+{
+ return or(x, y);
+}
\ No newline at end of file
diff --git a/clang/test/SemaHLSL/logical-operator-errors.hlsl b/clang/test/SemaHLSL/logical-operator-errors.hlsl
new file mode 100644
index 0000000000000..41bf3c4b2f663
--- /dev/null
+++ b/clang/test/SemaHLSL/logical-operator-errors.hlsl
@@ -0,0 +1,27 @@
+// RUN: %clang_cc1 -finclude-default-header -triple dxil-pc-shadermodel6.6-library %s -verify -DTEST_FUNC=__builtin_hlsl_or
+// RUN: %clang_cc1 -finclude-default-header -triple dxil-pc-shadermodel6.6-library %s -verify -DTEST_FUNC=__builtin_hlsl_and
+
+
+bool test_too_few_arg(bool a)
+{
+ return TEST_FUNC(a);
+ // expected-error at -1 {{too few arguments to function call, expected 2, have 1}}
+}
+
+bool test_too_many_arg(bool a)
+{
+ return TEST_FUNC(a, a, a);
+ // expected-error at -1 {{too many arguments to function call, expected 2, have 3}}
+}
+
+bool2 test_mismatched_args(bool2 a, bool3 b)
+{
+ return TEST_FUNC(a, b);
+ // expected-error at -1 {{all arguments to}}{{.*}}{{must have the same type}}
+}
+
+bool test_incorrect_type(int a)
+{
+ return TEST_FUNC(a, a);
+ // expected-error at -1{{invalid operand of type 'int' where 'bool' or a vector of such type is required}}
+}
\ No newline at end of file
>From aa696040b21ba68862c39f913fcea76621fd46d8 Mon Sep 17 00:00:00 2001
From: Poonam Vilas Metkar <poonammetkar at microsoft.com>
Date: Wed, 26 Feb 2025 16:23:01 -0800
Subject: [PATCH 03/10] Format hlsl_intrinsics.h with clang-format
---
clang/lib/Headers/hlsl/hlsl_intrinsics.h | 18 ++++++++++++++++++
1 file changed, 18 insertions(+)
diff --git a/clang/lib/Headers/hlsl/hlsl_intrinsics.h b/clang/lib/Headers/hlsl/hlsl_intrinsics.h
index 239d7a3f59b77..764cf671dce31 100644
--- a/clang/lib/Headers/hlsl/hlsl_intrinsics.h
+++ b/clang/lib/Headers/hlsl/hlsl_intrinsics.h
@@ -290,6 +290,24 @@ _HLSL_BUILTIN_ALIAS(__builtin_hlsl_and)
bool4 and(bool4 x, bool4 y);
// clang-format on
+//===----------------------------------------------------------------------===//
+// or builtins
+//===----------------------------------------------------------------------===//
+
+/// \fn T or(T x, T y)
+/// \brief Returns the bitwise OR of the two input values, \a x and \a y.
+/// \param x The first input value and y The second input value.
+///
+/// \returns The logically OR a vector and retuens bool vector.
+
+_HLSL_BUILTIN_ALIAS(__builtin_hlsl_or)
+bool or(bool, bool);
+_HLSL_BUILTIN_ALIAS(__builtin_hlsl_or)
+bool2 or(bool2, bool2);
+_HLSL_BUILTIN_ALIAS(__builtin_hlsl_or)
+bool3 or(bool3, bool3);
+_HLSL_BUILTIN_ALIAS(__builtin_hlsl_or)
+bool4 or(bool4, bool4);
//===----------------------------------------------------------------------===//
// any builtins
//===----------------------------------------------------------------------===//
>From 659f2fcce0e47182d6faa4db74e01d2734ee6a07 Mon Sep 17 00:00:00 2001
From: Poonam Vilas Metkar <poonammetkar at microsoft.com>
Date: Wed, 26 Feb 2025 16:36:50 -0800
Subject: [PATCH 04/10] Include HLSL or_intrinsic, add codegen in CGBuiltin,
and the corresponding tests in or.hlsl. Additionally, incorporate
logical-operator-errors to handle both 'and' and 'or' semantic diagnostics.
---
clang/include/clang/Basic/Builtins.td | 6 ++
clang/lib/CodeGen/CGBuiltin.cpp | 5 ++
clang/lib/Sema/SemaHLSL.cpp | 19 +++++
clang/test/CodeGenHLSL/builtins/or.hlsl | 85 +++++++++++++++++++
.../SemaHLSL/logical-operator-errors.hlsl | 27 ++++++
5 files changed, 142 insertions(+)
create mode 100644 clang/test/CodeGenHLSL/builtins/or.hlsl
create mode 100644 clang/test/SemaHLSL/logical-operator-errors.hlsl
diff --git a/clang/include/clang/Basic/Builtins.td b/clang/include/clang/Basic/Builtins.td
index 598ae171b1389..f7027331cd6c5 100644
--- a/clang/include/clang/Basic/Builtins.td
+++ b/clang/include/clang/Basic/Builtins.td
@@ -4783,6 +4783,12 @@ def HLSLAnd : LangBuiltin<"HLSL_LANG"> {
let Prototype = "void(...)";
}
+def HLSLOr : LangBuiltin<"HLSL_LANG"> {
+ let Spellings = ["__builtin_hlsl_or"];
+ let Attributes = [NoThrow, Const];
+ let Prototype = "void(...)";
+}
+
def HLSLAny : LangBuiltin<"HLSL_LANG"> {
let Spellings = ["__builtin_hlsl_any"];
let Attributes = [NoThrow, Const];
diff --git a/clang/lib/CodeGen/CGBuiltin.cpp b/clang/lib/CodeGen/CGBuiltin.cpp
index 65fac01d58362..599e05819ec7c 100644
--- a/clang/lib/CodeGen/CGBuiltin.cpp
+++ b/clang/lib/CodeGen/CGBuiltin.cpp
@@ -19492,6 +19492,11 @@ Value *CodeGenFunction::EmitHLSLBuiltinExpr(unsigned BuiltinID,
Value *Op1 = EmitScalarExpr(E->getArg(1));
return Builder.CreateAnd(Op0, Op1, "hlsl.and");
}
+ case Builtin::BI__builtin_hlsl_or: {
+ Value *Op0 = EmitScalarExpr(E->getArg(0));
+ Value *Op1 = EmitScalarExpr(E->getArg(1));
+ return Builder.CreateOr(Op0, Op1, "hlsl.or");
+ }
case Builtin::BI__builtin_hlsl_any: {
Value *Op0 = EmitScalarExpr(E->getArg(0));
return Builder.CreateIntrinsic(
diff --git a/clang/lib/Sema/SemaHLSL.cpp b/clang/lib/Sema/SemaHLSL.cpp
index 283a9801fc707..c48512673be58 100644
--- a/clang/lib/Sema/SemaHLSL.cpp
+++ b/clang/lib/Sema/SemaHLSL.cpp
@@ -2305,6 +2305,25 @@ bool SemaHLSL::CheckBuiltinFunctionCall(unsigned BuiltinID, CallExpr *TheCall) {
TheCall->setType(ArgTyA);
break;
}
+ case Builtin::BI__builtin_hlsl_or: {
+ if (SemaRef.checkArgCount(TheCall, 2))
+ return true;
+ if (CheckVectorElementCallArgs(&SemaRef, TheCall))
+ return true;
+
+ // Ensure input expr type is a scalar/vector and the same as the return type
+ if (CheckScalarOrVector(&SemaRef, TheCall, getASTContext().BoolTy, 0))
+ return true;
+
+ // Ensure input parameter type is bool
+ ExprResult A = TheCall->getArg(0);
+ QualType ArgTyA = A.get()->getType();
+
+ // return type is the same as the input type
+ TheCall->setType(ArgTyA);
+
+ break;
+ }
case Builtin::BI__builtin_hlsl_all:
case Builtin::BI__builtin_hlsl_any: {
if (SemaRef.checkArgCount(TheCall, 1))
diff --git a/clang/test/CodeGenHLSL/builtins/or.hlsl b/clang/test/CodeGenHLSL/builtins/or.hlsl
new file mode 100644
index 0000000000000..0c21973bda17c
--- /dev/null
+++ b/clang/test/CodeGenHLSL/builtins/or.hlsl
@@ -0,0 +1,85 @@
+// RUN: %clang_cc1 -finclude-default-header -triple \
+// RUN: dxil-pc-shadermodel6.3-library %s \
+// RUN: -emit-llvm -O1 -o - | FileCheck %s
+
+//CHECK-LABEL: define noundef i1 @_Z12test_or_boolbb(
+//CHECK-SAME: i1 noundef [[X:%.*]], i1 noundef [[Y:%.*]]) local_unnamed_addr #[[ATTR0:[0-9]+]] {
+//CHECK-NEXT: [[ENTRY:.*:]]
+//CHECK-NEXT: [[HLSL_OR:%.*]] = or i1 [[x]], [[y]]
+//CHECK-NEXT: ret i1 [[HLSL_OR]]
+//CHECK_NEXT: }
+bool test_or_bool(bool x, bool y)
+{
+ return or(x, y);
+
+}
+
+//CHECK-LABEL: define noundef <2 x i1> @_Z13test_or_bool2Dv2_bS_(
+//CHECK-SAME: <2 x i1> noundef [[X:%.*]], <2 x i1> noundef [[Y:%.*]]) local_unnamed_addr #[[ATTR0]] {
+//CHECK-NEXT: [[ENTRY:.*:]]
+//CHECK-NEXT: [[HLSL_OR:%.*]] = or <2 xi1> [[x]], [[y]]
+//CHECK-NEXT: ret <2 x i1> [[HLSL_OR]]
+//CHECK_NEXT: }
+bool2 test_or_bool2(bool2 x, bool2 y)
+{
+ return or(x, y);
+}
+
+//CHECK-LABEL: define noundef <3 x i1> @_Z13test_or_bool3Dv3_bS_(
+//CHECK-SAME: <3 x i1> noundef [[X:%.*]], <3 x i1> noundef [[Y:%.*]]) local_unnamed_addr #[[ATTR0]] {
+//CHECK-NEXT: [[ENTRY:.*:]]
+//CHECK-NEXT: [[HLSL_OR:%.*]] = or <3 xi1> [[x]], [[y]]
+//CHECK-NEXT: ret <3 x i1> [[HLSL_OR]]
+//CHECK_NEXT: }
+bool3 test_or_bool3(bool3 x, bool3 y)
+{
+ return or(x, y);
+}
+
+//CHECK-LABEL: define noundef <4 x i1> @_Z13test_or_bool4Dv4_bS_(
+//CHECK-SAME: <4 x i1> noundef [[X:%.*]], <4 x i1> noundef [[Y:%.*]]) local_unnamed_addr #[[ATTR0]] {
+//CHECK-NEXT: [[ENTRY:.*:]]
+//CHECK-NEXT: [[HLSL_OR:%.*]] = or <4 xi1> [[x]], [[y]]
+//CHECK-NEXT: ret <4 x i1> [[HLSL_OR]]
+//CHECK_NEXT: }
+bool4 test_or_bool4(bool4 x, bool4 y)
+{
+ return or(x, y);
+}
+
+//CHECK-LABEL: define noundef i1 @_Z11test_or_intii(
+//CHECK-SAME: i32 noundef [[X:%.*]], i32 noundef [[Y:%.*]]) local_unnamed_addr #[[ATTR0]] {
+//CHECK-NEXT: [[ENTRY:.*:]]
+//CHECK_NEXT: [[0:%.*]] = or i32 [[y]], [[x]]
+//CHECK-NEXT: [[HLSL_OR:%.*]] = icmp ne i32 [[0]], 0
+//CHECK-NEXT: ret i1 [[HLSL_OR]]
+//CHECK_NEXT: }
+bool test_or_int(int x, int y)
+{
+ return or(x, y);
+}
+
+//CHECK-LABEL: define noundef <4 x i1> @_Z12test_or_int4Dv4_iS_(
+//CHECK-SAME: <4 x i32> noundef [[X:%.*]], <4 x i32> noundef [[Y:%.*]]) local_unnamed_addr #[[ATTR0]] {
+//CHECK-NEXT: [[ENTRY:.*:]]
+//CHECK_NEXT: [[0:%.*]] = or <4 x i32> [[y]], [[x]]
+//CHECK-NEXT: [[HLSL_OR:%.*]] = icmp ne <4 x i32> [[0]], zeroinitializer
+//CHECK-NEXT: ret <4 x i1> [[HLSL_OR]]
+//CHECK_NEXT: }
+bool4 test_or_int4(int4 x, int4 y)
+{
+ return or(x, y);
+}
+
+//CHECK-LABEL: noundef <4 x i1> @_Z14test_or_float4Dv4_fS_(
+//CHECK-SAME: <4 x float> noundef nofpclass(nan inf) [[X:%.*]], <4 x float> noundef nofpclass(nan inf) [[Y:%.*]]) local_unnamed_addr #[[ATTR0]] {
+//CHECK-NEXT: [[ENTRY:.*:]]
+//CHECK-NEXT: [[TOBOOL:%.*]] = fcmp reassoc nnan ninf nsz arcp afn une <4 x float> [[X]], zeroinitializer
+//CHECK-NEXT: [[TOBOOL1:%.*]] = fcmp reassoc nnan ninf nsz arcp afn une <4 x float> [[Y]], zeroinitializer
+//CHECK-NEXT: [[HLSL_OR:%.*]] = or <4 x i1> [[TOBOOL]], [[TOBOOL1]]
+//CHECK-NEXT: ret <4 x i1> [[HLSL_OR]]
+//CHECK_NEXT: }
+bool4 test_or_float4(float4 x, float4 y)
+{
+ return or(x, y);
+}
\ No newline at end of file
diff --git a/clang/test/SemaHLSL/logical-operator-errors.hlsl b/clang/test/SemaHLSL/logical-operator-errors.hlsl
new file mode 100644
index 0000000000000..41bf3c4b2f663
--- /dev/null
+++ b/clang/test/SemaHLSL/logical-operator-errors.hlsl
@@ -0,0 +1,27 @@
+// RUN: %clang_cc1 -finclude-default-header -triple dxil-pc-shadermodel6.6-library %s -verify -DTEST_FUNC=__builtin_hlsl_or
+// RUN: %clang_cc1 -finclude-default-header -triple dxil-pc-shadermodel6.6-library %s -verify -DTEST_FUNC=__builtin_hlsl_and
+
+
+bool test_too_few_arg(bool a)
+{
+ return TEST_FUNC(a);
+ // expected-error at -1 {{too few arguments to function call, expected 2, have 1}}
+}
+
+bool test_too_many_arg(bool a)
+{
+ return TEST_FUNC(a, a, a);
+ // expected-error at -1 {{too many arguments to function call, expected 2, have 3}}
+}
+
+bool2 test_mismatched_args(bool2 a, bool3 b)
+{
+ return TEST_FUNC(a, b);
+ // expected-error at -1 {{all arguments to}}{{.*}}{{must have the same type}}
+}
+
+bool test_incorrect_type(int a)
+{
+ return TEST_FUNC(a, a);
+ // expected-error at -1{{invalid operand of type 'int' where 'bool' or a vector of such type is required}}
+}
\ No newline at end of file
>From a770cbfcaf22a465048a6551b845e7cdce5f3138 Mon Sep 17 00:00:00 2001
From: Poonam Vilas Metkar <poonammetkar at microsoft.com>
Date: Wed, 26 Feb 2025 17:16:22 -0800
Subject: [PATCH 05/10] Fix: logical-operator-errors.hlsl location
---
clang/test/SemaHLSL/{ => BuiltIns}/logical-operator-errors.hlsl | 0
1 file changed, 0 insertions(+), 0 deletions(-)
rename clang/test/SemaHLSL/{ => BuiltIns}/logical-operator-errors.hlsl (100%)
diff --git a/clang/test/SemaHLSL/logical-operator-errors.hlsl b/clang/test/SemaHLSL/BuiltIns/logical-operator-errors.hlsl
similarity index 100%
rename from clang/test/SemaHLSL/logical-operator-errors.hlsl
rename to clang/test/SemaHLSL/BuiltIns/logical-operator-errors.hlsl
>From cc26694fb2ad676760a87d104627bbf5cb0b403a Mon Sep 17 00:00:00 2001
From: Poonam Vilas Metkar <poonammetkar at microsoft.com>
Date: Wed, 26 Feb 2025 17:19:45 -0800
Subject: [PATCH 06/10] Remove and-errors.hlsl file; tests handled in
logical-operator-errors.hlsl
---
clang/test/SemaHLSL/BuiltIns/and-errors.hlsl | 22 --------------------
1 file changed, 22 deletions(-)
delete mode 100644 clang/test/SemaHLSL/BuiltIns/and-errors.hlsl
diff --git a/clang/test/SemaHLSL/BuiltIns/and-errors.hlsl b/clang/test/SemaHLSL/BuiltIns/and-errors.hlsl
deleted file mode 100644
index d9721140b9bf9..0000000000000
--- a/clang/test/SemaHLSL/BuiltIns/and-errors.hlsl
+++ /dev/null
@@ -1,22 +0,0 @@
-// RUN: %clang_cc1 -finclude-default-header -triple \
-// RUN: dxil-pc-shadermodel6.3-library %s -O1 -verify
-
-bool test_too_few_arg(bool a) {
- return __builtin_hlsl_and(a);
- // expected-error at -1 {{too few arguments to function call, expected 2, have 1}}
-}
-
-bool test_too_many_arg(bool a) {
- return __builtin_hlsl_and(a, a, a);
- // expected-error at -1 {{too many arguments to function call, expected 2, have 3}}
-}
-
-bool2 test_mismatched_args(bool2 a, bool3 b) {
- return __builtin_hlsl_and(a, b);
- // expected-error at -1 {{all arguments to '__builtin_hlsl_and' must have the same type}}
-}
-
-bool test_incorrect_type(int a) {
- return __builtin_hlsl_and(a, a);
- // expected-error at -1{{invalid operand of type 'int' where 'bool' or a vector of such type is required}}
-}
>From d99e734b90de342ae673c993c45e48d16774181c Mon Sep 17 00:00:00 2001
From: Poonam Vilas Metkar <poonammetkar at microsoft.com>
Date: Wed, 26 Feb 2025 20:55:31 -0800
Subject: [PATCH 07/10] Address the review comments
---
clang/lib/Headers/hlsl/hlsl_intrinsics.h | 4 ++--
clang/test/CodeGenHLSL/builtins/or.hlsl | 2 +-
clang/test/SemaHLSL/BuiltIns/logical-operator-errors.hlsl | 2 +-
3 files changed, 4 insertions(+), 4 deletions(-)
diff --git a/clang/lib/Headers/hlsl/hlsl_intrinsics.h b/clang/lib/Headers/hlsl/hlsl_intrinsics.h
index 764cf671dce31..e7dee43d674d8 100644
--- a/clang/lib/Headers/hlsl/hlsl_intrinsics.h
+++ b/clang/lib/Headers/hlsl/hlsl_intrinsics.h
@@ -298,8 +298,7 @@ bool4 and(bool4 x, bool4 y);
/// \brief Returns the bitwise OR of the two input values, \a x and \a y.
/// \param x The first input value and y The second input value.
///
-/// \returns The logically OR a vector and retuens bool vector.
-
+/// \returns A boolean vector by performing a logical OR operation elementwise.
_HLSL_BUILTIN_ALIAS(__builtin_hlsl_or)
bool or(bool, bool);
_HLSL_BUILTIN_ALIAS(__builtin_hlsl_or)
@@ -308,6 +307,7 @@ _HLSL_BUILTIN_ALIAS(__builtin_hlsl_or)
bool3 or(bool3, bool3);
_HLSL_BUILTIN_ALIAS(__builtin_hlsl_or)
bool4 or(bool4, bool4);
+
//===----------------------------------------------------------------------===//
// any builtins
//===----------------------------------------------------------------------===//
diff --git a/clang/test/CodeGenHLSL/builtins/or.hlsl b/clang/test/CodeGenHLSL/builtins/or.hlsl
index 0c21973bda17c..28127bbfb88dc 100644
--- a/clang/test/CodeGenHLSL/builtins/or.hlsl
+++ b/clang/test/CodeGenHLSL/builtins/or.hlsl
@@ -82,4 +82,4 @@ bool4 test_or_int4(int4 x, int4 y)
bool4 test_or_float4(float4 x, float4 y)
{
return or(x, y);
-}
\ No newline at end of file
+}
diff --git a/clang/test/SemaHLSL/BuiltIns/logical-operator-errors.hlsl b/clang/test/SemaHLSL/BuiltIns/logical-operator-errors.hlsl
index 41bf3c4b2f663..601851ed82fd2 100644
--- a/clang/test/SemaHLSL/BuiltIns/logical-operator-errors.hlsl
+++ b/clang/test/SemaHLSL/BuiltIns/logical-operator-errors.hlsl
@@ -24,4 +24,4 @@ bool test_incorrect_type(int a)
{
return TEST_FUNC(a, a);
// expected-error at -1{{invalid operand of type 'int' where 'bool' or a vector of such type is required}}
-}
\ No newline at end of file
+}
>From b72a19750927aed35fd6d777d732fb1e77ee2024 Mon Sep 17 00:00:00 2001
From: Poonam Vilas Metkar <poonammetkar at microsoft.com>
Date: Wed, 26 Feb 2025 22:03:18 -0800
Subject: [PATCH 08/10] Address review comments
---
clang/lib/Headers/hlsl/hlsl_intrinsics.h | 14 +++++++++-----
clang/lib/Sema/SemaHLSL.cpp | 3 +--
clang/test/CodeGenHLSL/builtins/or.hlsl | 3 +--
3 files changed, 11 insertions(+), 9 deletions(-)
diff --git a/clang/lib/Headers/hlsl/hlsl_intrinsics.h b/clang/lib/Headers/hlsl/hlsl_intrinsics.h
index e7dee43d674d8..5b625f785a3d3 100644
--- a/clang/lib/Headers/hlsl/hlsl_intrinsics.h
+++ b/clang/lib/Headers/hlsl/hlsl_intrinsics.h
@@ -294,11 +294,14 @@ bool4 and(bool4 x, bool4 y);
// or builtins
//===----------------------------------------------------------------------===//
-/// \fn T or(T x, T y)
-/// \brief Returns the bitwise OR of the two input values, \a x and \a y.
-/// \param x The first input value and y The second input value.
-///
-/// \returns A boolean vector by performing a logical OR operation elementwise.
+/// \fn bool or(bool x, bbol y)
+/// \brief Logically ors two boolean vectors elementwise and produces a bool
+/// vector output.
+
+// TODO: Clean up clang-format marker once we've resolved
+// https://github.com/llvm/llvm-project/issues/128979
+//
+// clang-format off
_HLSL_BUILTIN_ALIAS(__builtin_hlsl_or)
bool or(bool, bool);
_HLSL_BUILTIN_ALIAS(__builtin_hlsl_or)
@@ -307,6 +310,7 @@ _HLSL_BUILTIN_ALIAS(__builtin_hlsl_or)
bool3 or(bool3, bool3);
_HLSL_BUILTIN_ALIAS(__builtin_hlsl_or)
bool4 or(bool4, bool4);
+// clang-format on
//===----------------------------------------------------------------------===//
// any builtins
diff --git a/clang/lib/Sema/SemaHLSL.cpp b/clang/lib/Sema/SemaHLSL.cpp
index c48512673be58..733c1524bded0 100644
--- a/clang/lib/Sema/SemaHLSL.cpp
+++ b/clang/lib/Sema/SemaHLSL.cpp
@@ -2311,11 +2311,10 @@ bool SemaHLSL::CheckBuiltinFunctionCall(unsigned BuiltinID, CallExpr *TheCall) {
if (CheckVectorElementCallArgs(&SemaRef, TheCall))
return true;
- // Ensure input expr type is a scalar/vector and the same as the return type
+ // Ensure input expr type is a scalar/vector of type Boolty
if (CheckScalarOrVector(&SemaRef, TheCall, getASTContext().BoolTy, 0))
return true;
- // Ensure input parameter type is bool
ExprResult A = TheCall->getArg(0);
QualType ArgTyA = A.get()->getType();
diff --git a/clang/test/CodeGenHLSL/builtins/or.hlsl b/clang/test/CodeGenHLSL/builtins/or.hlsl
index 28127bbfb88dc..843c1bfc1eb79 100644
--- a/clang/test/CodeGenHLSL/builtins/or.hlsl
+++ b/clang/test/CodeGenHLSL/builtins/or.hlsl
@@ -8,10 +8,9 @@
//CHECK-NEXT: [[HLSL_OR:%.*]] = or i1 [[x]], [[y]]
//CHECK-NEXT: ret i1 [[HLSL_OR]]
//CHECK_NEXT: }
-bool test_or_bool(bool x, bool y)
+bool test_and_scalar(bool x, bool y)
{
return or(x, y);
-
}
//CHECK-LABEL: define noundef <2 x i1> @_Z13test_or_bool2Dv2_bS_(
>From 584548f9b19e85d2cdd8755cf50411092575ddba Mon Sep 17 00:00:00 2001
From: Poonam Vilas Metkar <poonammetkar at microsoft.com>
Date: Thu, 27 Feb 2025 10:07:55 -0800
Subject: [PATCH 09/10] Updated incorrect link address
---
clang/lib/Headers/hlsl/hlsl_intrinsics.h | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/clang/lib/Headers/hlsl/hlsl_intrinsics.h b/clang/lib/Headers/hlsl/hlsl_intrinsics.h
index 5b625f785a3d3..d1a30a91b9375 100644
--- a/clang/lib/Headers/hlsl/hlsl_intrinsics.h
+++ b/clang/lib/Headers/hlsl/hlsl_intrinsics.h
@@ -299,7 +299,7 @@ bool4 and(bool4 x, bool4 y);
/// vector output.
// TODO: Clean up clang-format marker once we've resolved
-// https://github.com/llvm/llvm-project/issues/128979
+// https://github.com/llvm/llvm-project/issues/127851
//
// clang-format off
_HLSL_BUILTIN_ALIAS(__builtin_hlsl_or)
>From 6c93c1623c3a1d38030a11c93bfb101dfc03bc8b Mon Sep 17 00:00:00 2001
From: Poonam Vilas Metkar <poonammetkar at microsoft.com>
Date: Thu, 27 Feb 2025 11:50:35 -0800
Subject: [PATCH 10/10] Resolved the codegen tests for or intrinsic
---
clang/lib/Sema/SemaHLSL.cpp | 19 +-------------
clang/test/CodeGenHLSL/builtins/or.hlsl | 33 +++++++++++++------------
2 files changed, 18 insertions(+), 34 deletions(-)
diff --git a/clang/lib/Sema/SemaHLSL.cpp b/clang/lib/Sema/SemaHLSL.cpp
index 733c1524bded0..86d30cdfda24e 100644
--- a/clang/lib/Sema/SemaHLSL.cpp
+++ b/clang/lib/Sema/SemaHLSL.cpp
@@ -2291,36 +2291,19 @@ bool SemaHLSL::CheckBuiltinFunctionCall(unsigned BuiltinID, CallExpr *TheCall) {
break;
}
- case Builtin::BI__builtin_hlsl_and: {
- if (SemaRef.checkArgCount(TheCall, 2))
- return true;
- if (CheckVectorElementCallArgs(&SemaRef, TheCall))
- return true;
- if (CheckScalarOrVector(&SemaRef, TheCall, getASTContext().BoolTy, 0))
- return true;
-
- ExprResult A = TheCall->getArg(0);
- QualType ArgTyA = A.get()->getType();
- // return type is the same as the input type
- TheCall->setType(ArgTyA);
- break;
- }
+ case Builtin::BI__builtin_hlsl_and:
case Builtin::BI__builtin_hlsl_or: {
if (SemaRef.checkArgCount(TheCall, 2))
return true;
if (CheckVectorElementCallArgs(&SemaRef, TheCall))
return true;
-
- // Ensure input expr type is a scalar/vector of type Boolty
if (CheckScalarOrVector(&SemaRef, TheCall, getASTContext().BoolTy, 0))
return true;
ExprResult A = TheCall->getArg(0);
QualType ArgTyA = A.get()->getType();
-
// return type is the same as the input type
TheCall->setType(ArgTyA);
-
break;
}
case Builtin::BI__builtin_hlsl_all:
diff --git a/clang/test/CodeGenHLSL/builtins/or.hlsl b/clang/test/CodeGenHLSL/builtins/or.hlsl
index 843c1bfc1eb79..b3070b3cc9d67 100644
--- a/clang/test/CodeGenHLSL/builtins/or.hlsl
+++ b/clang/test/CodeGenHLSL/builtins/or.hlsl
@@ -2,12 +2,12 @@
// RUN: dxil-pc-shadermodel6.3-library %s \
// RUN: -emit-llvm -O1 -o - | FileCheck %s
-//CHECK-LABEL: define noundef i1 @_Z12test_or_boolbb(
+//CHECK-LABEL: define noundef i1 @_Z15test_and_scalarbb(
//CHECK-SAME: i1 noundef [[X:%.*]], i1 noundef [[Y:%.*]]) local_unnamed_addr #[[ATTR0:[0-9]+]] {
//CHECK-NEXT: [[ENTRY:.*:]]
-//CHECK-NEXT: [[HLSL_OR:%.*]] = or i1 [[x]], [[y]]
+//CHECK-NEXT: [[HLSL_OR:%.*]] = or i1 [[X]], [[Y]]
//CHECK-NEXT: ret i1 [[HLSL_OR]]
-//CHECK_NEXT: }
+//CHECK-NEXT: }
bool test_and_scalar(bool x, bool y)
{
return or(x, y);
@@ -16,9 +16,9 @@ bool test_and_scalar(bool x, bool y)
//CHECK-LABEL: define noundef <2 x i1> @_Z13test_or_bool2Dv2_bS_(
//CHECK-SAME: <2 x i1> noundef [[X:%.*]], <2 x i1> noundef [[Y:%.*]]) local_unnamed_addr #[[ATTR0]] {
//CHECK-NEXT: [[ENTRY:.*:]]
-//CHECK-NEXT: [[HLSL_OR:%.*]] = or <2 xi1> [[x]], [[y]]
+//CHECK-NEXT: [[HLSL_OR:%.*]] = or <2 x i1> [[X]], [[Y]]
//CHECK-NEXT: ret <2 x i1> [[HLSL_OR]]
-//CHECK_NEXT: }
+//CHECK-NEXT: }
bool2 test_or_bool2(bool2 x, bool2 y)
{
return or(x, y);
@@ -27,9 +27,9 @@ bool2 test_or_bool2(bool2 x, bool2 y)
//CHECK-LABEL: define noundef <3 x i1> @_Z13test_or_bool3Dv3_bS_(
//CHECK-SAME: <3 x i1> noundef [[X:%.*]], <3 x i1> noundef [[Y:%.*]]) local_unnamed_addr #[[ATTR0]] {
//CHECK-NEXT: [[ENTRY:.*:]]
-//CHECK-NEXT: [[HLSL_OR:%.*]] = or <3 xi1> [[x]], [[y]]
+//CHECK-NEXT: [[HLSL_OR:%.*]] = or <3 x i1> [[X]], [[Y]]
//CHECK-NEXT: ret <3 x i1> [[HLSL_OR]]
-//CHECK_NEXT: }
+//CHECK-NEXT: }
bool3 test_or_bool3(bool3 x, bool3 y)
{
return or(x, y);
@@ -38,9 +38,9 @@ bool3 test_or_bool3(bool3 x, bool3 y)
//CHECK-LABEL: define noundef <4 x i1> @_Z13test_or_bool4Dv4_bS_(
//CHECK-SAME: <4 x i1> noundef [[X:%.*]], <4 x i1> noundef [[Y:%.*]]) local_unnamed_addr #[[ATTR0]] {
//CHECK-NEXT: [[ENTRY:.*:]]
-//CHECK-NEXT: [[HLSL_OR:%.*]] = or <4 xi1> [[x]], [[y]]
+//CHECK-NEXT: [[HLSL_OR:%.*]] = or <4 x i1> [[X]], [[Y]]
//CHECK-NEXT: ret <4 x i1> [[HLSL_OR]]
-//CHECK_NEXT: }
+//CHECK-NEXT: }
bool4 test_or_bool4(bool4 x, bool4 y)
{
return or(x, y);
@@ -49,10 +49,10 @@ bool4 test_or_bool4(bool4 x, bool4 y)
//CHECK-LABEL: define noundef i1 @_Z11test_or_intii(
//CHECK-SAME: i32 noundef [[X:%.*]], i32 noundef [[Y:%.*]]) local_unnamed_addr #[[ATTR0]] {
//CHECK-NEXT: [[ENTRY:.*:]]
-//CHECK_NEXT: [[0:%.*]] = or i32 [[y]], [[x]]
-//CHECK-NEXT: [[HLSL_OR:%.*]] = icmp ne i32 [[0]], 0
+//CHECK-NEXT: [[A:%.*]] = or i32 [[Y]], [[X]]
+//CHECK-NEXT: [[HLSL_OR:%.*]] = icmp ne i32 [[A]], 0
//CHECK-NEXT: ret i1 [[HLSL_OR]]
-//CHECK_NEXT: }
+//CHECK-NEXT: }
bool test_or_int(int x, int y)
{
return or(x, y);
@@ -61,10 +61,10 @@ bool test_or_int(int x, int y)
//CHECK-LABEL: define noundef <4 x i1> @_Z12test_or_int4Dv4_iS_(
//CHECK-SAME: <4 x i32> noundef [[X:%.*]], <4 x i32> noundef [[Y:%.*]]) local_unnamed_addr #[[ATTR0]] {
//CHECK-NEXT: [[ENTRY:.*:]]
-//CHECK_NEXT: [[0:%.*]] = or <4 x i32> [[y]], [[x]]
-//CHECK-NEXT: [[HLSL_OR:%.*]] = icmp ne <4 x i32> [[0]], zeroinitializer
+//CHECK-NEXT: [[A:%.*]] = or <4 x i32> [[Y]], [[X]]
+//CHECK-NEXT: [[HLSL_OR:%.*]] = icmp ne <4 x i32> [[A]], zeroinitializer
//CHECK-NEXT: ret <4 x i1> [[HLSL_OR]]
-//CHECK_NEXT: }
+//CHECK-NEXT: }
bool4 test_or_int4(int4 x, int4 y)
{
return or(x, y);
@@ -77,8 +77,9 @@ bool4 test_or_int4(int4 x, int4 y)
//CHECK-NEXT: [[TOBOOL1:%.*]] = fcmp reassoc nnan ninf nsz arcp afn une <4 x float> [[Y]], zeroinitializer
//CHECK-NEXT: [[HLSL_OR:%.*]] = or <4 x i1> [[TOBOOL]], [[TOBOOL1]]
//CHECK-NEXT: ret <4 x i1> [[HLSL_OR]]
-//CHECK_NEXT: }
+//CHECK-NEXT: }
bool4 test_or_float4(float4 x, float4 y)
{
return or(x, y);
}
+
More information about the cfe-commits
mailing list