[clang] 23efe73 - [HLSL] Add "or" intrinsic (#128979)
via cfe-commits
cfe-commits at lists.llvm.org
Fri Feb 28 12:54:17 PST 2025
Author: metkarpoonam
Date: 2025-02-28T13:54:13-07:00
New Revision: 23efe734fc27544b473ad60ea6eecbd2ec66d20c
URL: https://github.com/llvm/llvm-project/commit/23efe734fc27544b473ad60ea6eecbd2ec66d20c
DIFF: https://github.com/llvm/llvm-project/commit/23efe734fc27544b473ad60ea6eecbd2ec66d20c.diff
LOG: [HLSL] Add "or" intrinsic (#128979)
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.
Added:
clang/test/CodeGenHLSL/builtins/or.hlsl
clang/test/SemaHLSL/BuiltIns/logical-operator-errors.hlsl
Modified:
clang/include/clang/Basic/Builtins.td
clang/lib/CodeGen/CGBuiltin.cpp
clang/lib/Headers/hlsl/hlsl_intrinsics.h
clang/lib/Sema/SemaHLSL.cpp
Removed:
clang/test/SemaHLSL/BuiltIns/and-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 13bffd542e78e..03b8d16b76e0d 100644
--- a/clang/lib/CodeGen/CGBuiltin.cpp
+++ b/clang/lib/CodeGen/CGBuiltin.cpp
@@ -19493,6 +19493,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/Headers/hlsl/hlsl_intrinsics.h b/clang/lib/Headers/hlsl/hlsl_intrinsics.h
index 239d7a3f59b77..ed008eeb04ba8 100644
--- a/clang/lib/Headers/hlsl/hlsl_intrinsics.h
+++ b/clang/lib/Headers/hlsl/hlsl_intrinsics.h
@@ -290,6 +290,28 @@ _HLSL_BUILTIN_ALIAS(__builtin_hlsl_and)
bool4 and(bool4 x, bool4 y);
// clang-format on
+//===----------------------------------------------------------------------===//
+// or builtins
+//===----------------------------------------------------------------------===//
+
+/// \fn bool or(bool x, bool 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/127851
+//
+// clang-format off
+_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);
+// clang-format on
+
//===----------------------------------------------------------------------===//
// any builtins
//===----------------------------------------------------------------------===//
diff --git a/clang/lib/Sema/SemaHLSL.cpp b/clang/lib/Sema/SemaHLSL.cpp
index ffc3ac1b65854..bfe84b16218b7 100644
--- a/clang/lib/Sema/SemaHLSL.cpp
+++ b/clang/lib/Sema/SemaHLSL.cpp
@@ -2293,7 +2293,8 @@ bool SemaHLSL::CheckBuiltinFunctionCall(unsigned BuiltinID, CallExpr *TheCall) {
break;
}
- case Builtin::BI__builtin_hlsl_and: {
+ case Builtin::BI__builtin_hlsl_and:
+ case Builtin::BI__builtin_hlsl_or: {
if (SemaRef.checkArgCount(TheCall, 2))
return true;
if (CheckVectorElementCallArgs(&SemaRef, TheCall))
diff --git a/clang/test/CodeGenHLSL/builtins/or.hlsl b/clang/test/CodeGenHLSL/builtins/or.hlsl
new file mode 100644
index 0000000000000..69c57c5455f7d
--- /dev/null
+++ b/clang/test/CodeGenHLSL/builtins/or.hlsl
@@ -0,0 +1,80 @@
+// RUN: %clang_cc1 -finclude-default-header -triple \
+// RUN: dxil-pc-shadermodel6.3-library %s \
+// RUN: -emit-llvm -disable-llvm-passes -o - | FileCheck %s
+
+//CHECK-LABEL: define noundef i1 @_Z14test_or_scalarbb(
+//CHECK-SAME: i1 noundef [[X:%.*]], i1 noundef [[Y:%.*]]) #[[ATTR0:[0-9]+]] {
+//CHECK-NEXT: entry:
+//CHECK: [[HLSL_OR:%.*]] = or i1 [[A:%.*]], [[B:%.*]]
+//CHECK: ret i1 [[HLSL_OR]]
+bool test_or_scalar(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:%.*]]) #[[ATTR0]] {
+//CHECK-NEXT: entry:
+//CHECK: [[HLSL_OR:%.*]] = or <2 x i1> [[A:%.*]], [[B:%.*]]
+//CHECK: ret <2 x i1> [[HLSL_OR]]
+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:%.*]]) #[[ATTR0]] {
+//CHECK-NEXT: entry:
+//CHECK: [[HLSL_OR:%.*]] = or <3 x i1> [[A:%.*]], [[B:%.*]]
+//CHECK: ret <3 x i1> [[HLSL_OR]]
+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:%.*]]) #[[ATTR0]] {
+//CHECK-NEXT: entry:
+//CHECK: [[HLSL_OR:%.*]] = or <4 x i1> [[A:%.*]], [[B:%.*]]
+//CHECK: ret <4 x i1> [[HLSL_OR]]
+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:%.*]]) #[[ATTR0]] {
+//CHECK-NEXT: entry:
+//CHECK: [[TOBBOL:%.*]] = icmp ne i32 [[A:%.*]], 0
+//CHECK: [[TOBBOL1:%.*]] = icmp ne i32 [[B:%.*]], 0
+//CHECK: [[HLSL_OR:%.*]] = or i1 [[TOBBOL]], [[TOBBOL1]]
+//CHECK: ret i1 [[HLSL_OR]]
+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:%.*]]) #[[ATTR0]] {
+//CHECK-NEXT: entry:
+//CHECK: [[TOBOOL:%.*]] = icmp ne <4 x i32> [[A:%.*]], zeroinitializer
+//CHECK: [[TOBOOL1:%.*]] = icmp ne <4 x i32> [[B:%.*]], zeroinitializer
+//CHECK: [[HLSL_OR:%.*]] = or <4 x i1> [[TOBOOL]], [[TOBOOL1]]
+//CHECK: ret <4 x i1> [[HLSL_OR]]
+bool4 test_or_int4(int4 x, int4 y)
+{
+ return or(x, y);
+}
+
+//CHECK-LABEL: define 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:%.*]]) #[[ATTR0]] {
+//CHECK-NEXT: entry:
+//CHECK: [[TOBOOL:%.*]] = fcmp reassoc nnan ninf nsz arcp afn une <4 x float> [[A:%.*]], zeroinitializer
+//CHECK: [[TOBOOL1:%.*]] = fcmp reassoc nnan ninf nsz arcp afn une <4 x float> [[B:%.*]], zeroinitializer
+//CHECK: [[HLSL_OR:%.*]] = or <4 x i1> [[TOBOOL]], [[TOBOOL1]]
+//CHECK: ret <4 x i1> [[HLSL_OR]]
+bool4 test_or_float4(float4 x, float4 y)
+{
+ return or(x, y);
+}
+
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}}
-}
diff --git a/clang/test/SemaHLSL/BuiltIns/logical-operator-errors.hlsl b/clang/test/SemaHLSL/BuiltIns/logical-operator-errors.hlsl
new file mode 100644
index 0000000000000..c314c1b2b83ab
--- /dev/null
+++ b/clang/test/SemaHLSL/BuiltIns/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}}{{_builtin_hlsl_or|_builtin_hlsl_and }}{{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}}
+}
More information about the cfe-commits
mailing list