[clang] a23291b - [Clang] Add integer add reduction builtin
Simon Pilgrim via cfe-commits
cfe-commits at lists.llvm.org
Mon May 2 03:03:37 PDT 2022
Author: Simon Pilgrim
Date: 2022-05-02T11:03:25+01:00
New Revision: a23291b7db48670f7c57adcfb45877c826a97f22
URL: https://github.com/llvm/llvm-project/commit/a23291b7db48670f7c57adcfb45877c826a97f22
DIFF: https://github.com/llvm/llvm-project/commit/a23291b7db48670f7c57adcfb45877c826a97f22.diff
LOG: [Clang] Add integer add reduction builtin
Similar to the existing bitwise reduction builtins, this lowers to a llvm.vector.reduce.add intrinsic call.
For other reductions, we've tried to share builtins for float/integer vectors, but the fadd reduction intrinsics also take a starting value argument and can either do unordered or serialized, but not reduction-trees as specified for the builtins. However we address fadd support this shouldn't affect the integer case.
(Split off from D117829)
Differential Revision: https://reviews.llvm.org/D124741
Added:
Modified:
clang/include/clang/Basic/Builtins.def
clang/lib/CodeGen/CGBuiltin.cpp
clang/lib/Sema/SemaChecking.cpp
clang/test/CodeGen/builtins-reduction-math.c
clang/test/Sema/builtins-reduction-math.c
Removed:
################################################################################
diff --git a/clang/include/clang/Basic/Builtins.def b/clang/include/clang/Basic/Builtins.def
index c22957e14de7d..e86475b0e2602 100644
--- a/clang/include/clang/Basic/Builtins.def
+++ b/clang/include/clang/Basic/Builtins.def
@@ -663,6 +663,7 @@ BUILTIN(__builtin_reduce_min, "v.", "nct")
BUILTIN(__builtin_reduce_xor, "v.", "nct")
BUILTIN(__builtin_reduce_or, "v.", "nct")
BUILTIN(__builtin_reduce_and, "v.", "nct")
+BUILTIN(__builtin_reduce_add, "v.", "nct")
BUILTIN(__builtin_matrix_transpose, "v.", "nFt")
BUILTIN(__builtin_matrix_column_major_load, "v.", "nFt")
diff --git a/clang/lib/CodeGen/CGBuiltin.cpp b/clang/lib/CodeGen/CGBuiltin.cpp
index 70fd869e5c67f..4c203904a0e0e 100644
--- a/clang/lib/CodeGen/CGBuiltin.cpp
+++ b/clang/lib/CodeGen/CGBuiltin.cpp
@@ -3273,6 +3273,9 @@ RValue CodeGenFunction::EmitBuiltinExpr(const GlobalDecl GD, unsigned BuiltinID,
*this, E, GetIntrinsicID(E->getArg(0)->getType()), "rdx.min"));
}
+ case Builtin::BI__builtin_reduce_add:
+ return RValue::get(emitUnaryBuiltin(
+ *this, E, llvm::Intrinsic::vector_reduce_add, "rdx.add"));
case Builtin::BI__builtin_reduce_xor:
return RValue::get(emitUnaryBuiltin(
*this, E, llvm::Intrinsic::vector_reduce_xor, "rdx.xor"));
diff --git a/clang/lib/Sema/SemaChecking.cpp b/clang/lib/Sema/SemaChecking.cpp
index 467edd3321b9d..18387c2411852 100644
--- a/clang/lib/Sema/SemaChecking.cpp
+++ b/clang/lib/Sema/SemaChecking.cpp
@@ -2331,6 +2331,8 @@ Sema::CheckBuiltinFunctionCall(FunctionDecl *FDecl, unsigned BuiltinID,
}
// These builtins support vectors of integers only.
+ // TODO: ADD should support floating-point types.
+ case Builtin::BI__builtin_reduce_add:
case Builtin::BI__builtin_reduce_xor:
case Builtin::BI__builtin_reduce_or:
case Builtin::BI__builtin_reduce_and: {
diff --git a/clang/test/CodeGen/builtins-reduction-math.c b/clang/test/CodeGen/builtins-reduction-math.c
index f5fbd0c2b6803..2988a318eb6c6 100644
--- a/clang/test/CodeGen/builtins-reduction-math.c
+++ b/clang/test/CodeGen/builtins-reduction-math.c
@@ -58,6 +58,28 @@ void test_builtin_reduce_min(float4 vf1, si8 vi1, u4 vu1) {
unsigned long long r5 = __builtin_reduce_min(cvi1);
}
+void test_builtin_reduce_add(si8 vi1, u4 vu1) {
+ // CHECK: [[VI1:%.+]] = load <8 x i16>, <8 x i16>* %vi1.addr, align 16
+ // CHECK-NEXT: call i16 @llvm.vector.reduce.add.v8i16(<8 x i16> [[VI1]])
+ short r2 = __builtin_reduce_add(vi1);
+
+ // CHECK: [[VU1:%.+]] = load <4 x i32>, <4 x i32>* %vu1.addr, align 16
+ // CHECK-NEXT: call i32 @llvm.vector.reduce.add.v4i32(<4 x i32> [[VU1]])
+ unsigned r3 = __builtin_reduce_add(vu1);
+
+ // CHECK: [[CVI1:%.+]] = load <8 x i16>, <8 x i16>* %cvi1, align 16
+ // CHECK-NEXT: [[RDX1:%.+]] = call i16 @llvm.vector.reduce.add.v8i16(<8 x i16> [[CVI1]])
+ // CHECK-NEXT: sext i16 [[RDX1]] to i32
+ const si8 cvi1 = vi1;
+ int r4 = __builtin_reduce_add(cvi1);
+
+ // CHECK: [[CVU1:%.+]] = load <4 x i32>, <4 x i32>* %cvu1, align 16
+ // CHECK-NEXT: [[RDX2:%.+]] = call i32 @llvm.vector.reduce.add.v4i32(<4 x i32> [[CVU1]])
+ // CHECK-NEXT: zext i32 [[RDX2]] to i64
+ const u4 cvu1 = vu1;
+ unsigned long long r5 = __builtin_reduce_add(cvu1);
+}
+
void test_builtin_reduce_xor(si8 vi1, u4 vu1) {
// CHECK: [[VI1:%.+]] = load <8 x i16>, <8 x i16>* %vi1.addr, align 16
diff --git a/clang/test/Sema/builtins-reduction-math.c b/clang/test/Sema/builtins-reduction-math.c
index a729226056032..10c95a1e531bb 100644
--- a/clang/test/Sema/builtins-reduction-math.c
+++ b/clang/test/Sema/builtins-reduction-math.c
@@ -36,6 +36,23 @@ void test_builtin_reduce_min(int i, float4 v, int3 iv) {
// expected-error at -1 {{1st argument must be a vector type (was 'int')}}
}
+void test_builtin_reduce_add(int i, float4 v, int3 iv) {
+ struct Foo s = __builtin_reduce_add(iv);
+ // expected-error at -1 {{initializing 'struct Foo' with an expression of incompatible type 'int'}}
+
+ i = __builtin_reduce_add();
+ // expected-error at -1 {{too few arguments to function call, expected 1, have 0}}
+
+ i = __builtin_reduce_add(iv, iv);
+ // expected-error at -1 {{too many arguments to function call, expected 1, have 2}}
+
+ i = __builtin_reduce_add(i);
+ // expected-error at -1 {{1st argument must be a vector of integers (was 'int')}}
+
+ i = __builtin_reduce_add(v);
+ // expected-error at -1 {{1st argument must be a vector of integers (was 'float4' (vector of 4 'float' values))}}
+}
+
void test_builtin_reduce_xor(int i, float4 v, int3 iv) {
struct Foo s = __builtin_reduce_xor(iv);
// expected-error at -1 {{initializing 'struct Foo' with an expression of incompatible type 'int'}}
More information about the cfe-commits
mailing list