fhahn updated this revision to Diff 380946.
fhahn added a comment.
Move type checking to helper function checkMathBuiltinElementType to be reused
in D111986 <https://reviews.llvm.org/D111986>.
Repository:
rG LLVM Github Monorepo
CHANGES SINCE LAST ACTION
https://reviews.llvm.org/D111985/new/
https://reviews.llvm.org/D111985
Files:
clang/include/clang/Basic/Builtins.def
clang/include/clang/Basic/DiagnosticSemaKinds.td
clang/include/clang/Sema/Sema.h
clang/lib/CodeGen/CGBuiltin.cpp
clang/lib/Sema/SemaChecking.cpp
clang/test/CodeGen/builtins-elementwise-math.c
clang/test/Sema/builtins-elementwise-math.c
Index: clang/test/Sema/builtins-elementwise-math.c
===================================================================
--- /dev/null
+++ clang/test/Sema/builtins-elementwise-math.c
@@ -0,0 +1,42 @@
+// RUN: %clang_cc1 %s -pedantic -verify -triple=x86_64-apple-darwin9
+
+typedef float float4 __attribute__((ext_vector_type(4)));
+typedef int int3 __attribute__((ext_vector_type(3)));
+
+struct Foo {
+ char *p;
+};
+
+void test_builtin_elementwise_max(int i, double d, float4 v, int3 iv) {
+ i = __builtin_elementwise_max(i, d);
+ // expected-error@-1 {{argument types do not match, 'int' != 'double'}}
+
+ struct Foo s = __builtin_elementwise_max(i, i);
+ // expected-error@-1 {{initializing 'struct Foo' with an expression of incompatible type 'int'}}
+
+ i = __builtin_elementwise_max(i);
+ // expected-error@-1 {{too few arguments to function call, expected 2, have 1}}
+
+ i = __builtin_elementwise_max();
+ // expected-error@-1 {{too few arguments to function call, expected 2, have 0}}
+
+ i = __builtin_elementwise_max(v, iv);
+ // expected-error@-1 {{argument types do not match, 'float4' (vector of 4 'float' values) != 'int3' (vector of 3 'int' values)}}
+}
+
+void test_builtin_elementwise_min(int i, double d, float4 v, int3 iv) {
+ i = __builtin_elementwise_min(i, d);
+ // expected-error@-1 {{argument types do not match, 'int' != 'double'}}
+
+ struct Foo s = __builtin_elementwise_min(i, i);
+ // expected-error@-1 {{initializing 'struct Foo' with an expression of incompatible type 'int'}}
+
+ i = __builtin_elementwise_min(i);
+ // expected-error@-1 {{too few arguments to function call, expected 2, have 1}}
+
+ i = __builtin_elementwise_min();
+ // expected-error@-1 {{too few arguments to function call, expected 2, have 0}}
+
+ i = __builtin_elementwise_min(v, iv);
+ // expected-error@-1 {{argument types do not match, 'float4' (vector of 4 'float' values) != 'int3' (vector of 3 'int' values)}}
+}
Index: clang/test/CodeGen/builtins-elementwise-math.c
===================================================================
--- /dev/null
+++ clang/test/CodeGen/builtins-elementwise-math.c
@@ -0,0 +1,88 @@
+// RUN: %clang_cc1 -triple x86_64-apple-darwin %s -emit-llvm -disable-llvm-passes -o - | FileCheck %s
+
+typedef float float4 __attribute__((ext_vector_type(4)));
+typedef short int si8 __attribute__((ext_vector_type(8)));
+typedef unsigned int u4 __attribute__((ext_vector_type(4)));
+
+void test_builtin_elementwise_max(float f1, float f2, double d1, double d2,
+ float4 vf1, float4 vf2, long long int i1,
+ long long int i2, si8 vi1, si8 vi2,
+ unsigned u1, unsigned u2, u4 vu1, u4 vu2) {
+ // CHECK-LABEL: define void @test_builtin_elementwise_max(
+
+ // CHECK: [[F1:%.+]] = load float, float* %f1.addr, align 4
+ // CHECK-NEXT: [[F2:%.+]] = load float, float* %f2.addr, align 4
+ // CHECK-NEXT: call float @llvm.maxnum.f32(float %0, float %1)
+ f1 = __builtin_elementwise_max(f1, f2);
+
+ // CHECK: [[D1:%.+]] = load double, double* %d1.addr, align 8
+ // CHECK-NEXT: [[D2:%.+]] = load double, double* %d2.addr, align 8
+ // CHECK-NEXT: call double @llvm.maxnum.f64(double [[D1]], double [[D2]])
+ d1 = __builtin_elementwise_max(d1, d2);
+
+ // CHECK: [[VF1:%.+]] = load <4 x float>, <4 x float>* %vf1.addr, align 16
+ // CHECK-NEXT: [[VF2:%.+]] = load <4 x float>, <4 x float>* %vf2.addr, align 16
+ // CHECK-NEXT: call <4 x float> @llvm.maxnum.v4f32(<4 x float> [[VF1]], <4 x float> [[VF2]])
+ vf1 = __builtin_elementwise_max(vf1, vf2);
+
+ // CHECK: [[I1:%.+]] = load i64, i64* %i1.addr, align 8
+ // CHECK-NEXT: [[I2:%.+]] = load i64, i64* %i2.addr, align 8
+ // CHECK-NEXT: call i64 @llvm.smax.i64(i64 [[I1]], i64 [[I2]])
+ i1 = __builtin_elementwise_max(i1, i2);
+
+ // CHECK: [[VI1:%.+]] = load <8 x i16>, <8 x i16>* %vi1.addr, align 16
+ // CHECK-NEXT: [[VI2:%.+]] = load <8 x i16>, <8 x i16>* %vi2.addr, align 16
+ // CHECK-NEXT: call <8 x i16> @llvm.smax.v8i16(<8 x i16> [[VI1]], <8 x i16> [[VI2]])
+ vi1 = __builtin_elementwise_max(vi1, vi2);
+
+ // CHECK: [[U1:%.+]] = load i32, i32* %u1.addr, align 4
+ // CHECK-NEXT: [[U2:%.+]] = load i32, i32* %u2.addr, align 4
+ // CHECK-NEXT: call i32 @llvm.umax.i32(i32 [[U1]], i32 [[U2]])
+ u1 = __builtin_elementwise_max(u1, u2);
+
+ // CHECK: [[VU1:%.+]] = load <4 x i32>, <4 x i32>* %vu1.addr, align 16
+ // CHECK-NEXT: [[VU2:%.+]] = load <4 x i32>, <4 x i32>* %vu2.addr, align 16
+ // CHECK-NEXT: call <4 x i32> @llvm.umax.v4i32(<4 x i32> [[VU1]], <4 x i32> [[VU2]])
+ vu1 = __builtin_elementwise_max(vu1, vu2);
+}
+
+void test_builtin_elementwise_min(float f1, float f2, double d1, double d2,
+ float4 vf1, float4 vf2, long long int i1,
+ long long int i2, si8 vi1, si8 vi2,
+ unsigned u1, unsigned u2, u4 vu1, u4 vu2) {
+ // CHECK-LABEL: define void @test_builtin_elementwise_min(
+ // CHECK: [[F1:%.+]] = load float, float* %f1.addr, align 4
+ // CHECK-NEXT: [[F2:%.+]] = load float, float* %f2.addr, align 4
+ // CHECK-NEXT: call float @llvm.minnum.f32(float %0, float %1)
+ f1 = __builtin_elementwise_min(f1, f2);
+
+ // CHECK: [[D1:%.+]] = load double, double* %d1.addr, align 8
+ // CHECK-NEXT: [[D2:%.+]] = load double, double* %d2.addr, align 8
+ // CHECK-NEXT: call double @llvm.minnum.f64(double [[D1]], double [[D2]])
+ d1 = __builtin_elementwise_min(d1, d2);
+
+ // CHECK: [[VF1:%.+]] = load <4 x float>, <4 x float>* %vf1.addr, align 16
+ // CHECK-NEXT: [[VF2:%.+]] = load <4 x float>, <4 x float>* %vf2.addr, align 16
+ // CHECK-NEXT: call <4 x float> @llvm.minnum.v4f32(<4 x float> [[VF1]], <4 x float> [[VF2]])
+ vf1 = __builtin_elementwise_min(vf1, vf2);
+
+ // CHECK: [[I1:%.+]] = load i64, i64* %i1.addr, align 8
+ // CHECK-NEXT: [[I2:%.+]] = load i64, i64* %i2.addr, align 8
+ // CHECK-NEXT: call i64 @llvm.smin.i64(i64 [[I1]], i64 [[I2]])
+ i1 = __builtin_elementwise_min(i1, i2);
+
+ // CHECK: [[VI1:%.+]] = load <8 x i16>, <8 x i16>* %vi1.addr, align 16
+ // CHECK-NEXT: [[VI2:%.+]] = load <8 x i16>, <8 x i16>* %vi2.addr, align 16
+ // CHECK-NEXT: call <8 x i16> @llvm.smin.v8i16(<8 x i16> [[VI1]], <8 x i16> [[VI2]])
+ vi1 = __builtin_elementwise_min(vi1, vi2);
+
+ // CHECK: [[U1:%.+]] = load i32, i32* %u1.addr, align 4
+ // CHECK-NEXT: [[U2:%.+]] = load i32, i32* %u2.addr, align 4
+ // CHECK-NEXT: call i32 @llvm.umin.i32(i32 [[U1]], i32 [[U2]])
+ u1 = __builtin_elementwise_min(u1, u2);
+
+ // CHECK: [[VU1:%.+]] = load <4 x i32>, <4 x i32>* %vu1.addr, align 16
+ // CHECK-NEXT: [[VU2:%.+]] = load <4 x i32>, <4 x i32>* %vu2.addr, align 16
+ // CHECK-NEXT: call <4 x i32> @llvm.umin.v4i32(<4 x i32> [[VU1]], <4 x i32> [[VU2]])
+ vu1 = __builtin_elementwise_min(vu1, vu2);
+}
Index: clang/lib/Sema/SemaChecking.cpp
===================================================================
--- clang/lib/Sema/SemaChecking.cpp
+++ clang/lib/Sema/SemaChecking.cpp
@@ -1976,9 +1976,12 @@
break;
}
+ case Builtin::BI__builtin_elementwise_min:
+ case Builtin::BI__builtin_elementwise_max:
+ return SemaBuiltinElementwiseMath(TheCall, TheCallResult);
+
case Builtin::BI__builtin_matrix_transpose:
return SemaBuiltinMatrixTranspose(TheCall, TheCallResult);
-
case Builtin::BI__builtin_matrix_column_major_load:
return SemaBuiltinMatrixColumnMajorLoad(TheCall, TheCallResult);
@@ -16650,6 +16653,39 @@
_2, _3, _4));
}
+// Check if \p Ty is a valid type for the elementwise math builtins. If it is
+// not a valid type, emit an error message and return true. Otherwise return
+// false.
+static bool checkMathBuiltinElementType(SourceLocation Loc, QualType Ty,
+ Sema &S) {
+ if (!Ty->getAs<VectorType>() && !ConstantMatrixType::isValidElementType(Ty)) {
+ S.Diag(Loc, diag::err_elementwise_math_invalid_arg_type) << Ty;
+ return true;
+ }
+ return false;
+}
+
+ExprResult Sema::SemaBuiltinElementwiseMath(CallExpr *TheCall,
+ ExprResult CallResult) {
+ if (checkArgCount(*this, TheCall, 2))
+ return ExprError();
+
+ Expr *A = TheCall->getArg(0);
+ Expr *B = TheCall->getArg(1);
+ QualType TyA = A->getType();
+ QualType TyB = B->getType();
+
+ if (TyA != TyB)
+ return Diag(A->getBeginLoc(), diag::err_elementwise_math_arg_types_mismatch)
+ << TyA << TyB;
+
+ if (checkMathBuiltinElementType(A->getBeginLoc(), TyA, *this))
+ return ExprError();
+
+ TheCall->setType(TyA);
+ return CallResult;
+}
+
ExprResult Sema::SemaBuiltinMatrixTranspose(CallExpr *TheCall,
ExprResult CallResult) {
if (checkArgCount(*this, TheCall, 1))
Index: clang/lib/CodeGen/CGBuiltin.cpp
===================================================================
--- clang/lib/CodeGen/CGBuiltin.cpp
+++ clang/lib/CodeGen/CGBuiltin.cpp
@@ -3101,6 +3101,39 @@
return RValue::get(V);
}
+ case Builtin::BI__builtin_elementwise_max: {
+ Value *Op0 = EmitScalarExpr(E->getArg(0));
+ Value *Op1 = EmitScalarExpr(E->getArg(1));
+ Value *Result;
+ if (Op0->getType()->isIntOrIntVectorTy()) {
+ QualType Ty = E->getArg(0)->getType();
+ if (auto *VecTy = Ty->getAs<VectorType>())
+ Ty = VecTy->getElementType();
+ Result = Builder.CreateBinaryIntrinsic(Ty->isSignedIntegerType()
+ ? llvm::Intrinsic::smax
+ : llvm::Intrinsic::umax,
+ Op0, Op1, nullptr, "elt.max");
+ } else
+ Result = Builder.CreateMaxNum(Op0, Op1, "elt.max");
+ return RValue::get(Result);
+ }
+ case Builtin::BI__builtin_elementwise_min: {
+ Value *Op0 = EmitScalarExpr(E->getArg(0));
+ Value *Op1 = EmitScalarExpr(E->getArg(1));
+ Value *Result;
+ if (Op0->getType()->isIntOrIntVectorTy()) {
+ QualType Ty = E->getArg(0)->getType();
+ if (auto *VecTy = Ty->getAs<VectorType>())
+ Ty = VecTy->getElementType();
+ Result = Builder.CreateBinaryIntrinsic(Ty->isSignedIntegerType()
+ ? llvm::Intrinsic::smin
+ : llvm::Intrinsic::umin,
+ Op0, Op1, nullptr, "elt.min");
+ } else
+ Result = Builder.CreateMinNum(Op0, Op1, "elt.min");
+ return RValue::get(Result);
+ }
+
case Builtin::BI__builtin_matrix_transpose: {
const auto *MatrixTy = E->getArg(0)->getType()->getAs<ConstantMatrixType>();
Value *MatValue = EmitScalarExpr(E->getArg(0));
Index: clang/include/clang/Sema/Sema.h
===================================================================
--- clang/include/clang/Sema/Sema.h
+++ clang/include/clang/Sema/Sema.h
@@ -12712,6 +12712,9 @@
bool CheckPPCMMAType(QualType Type, SourceLocation TypeLoc);
+ ExprResult SemaBuiltinElementwiseMath(CallExpr *TheCall,
+ ExprResult CallResult);
+
// Matrix builtin handling.
ExprResult SemaBuiltinMatrixTranspose(CallExpr *TheCall,
ExprResult CallResult);
Index: clang/include/clang/Basic/DiagnosticSemaKinds.td
===================================================================
--- clang/include/clang/Basic/DiagnosticSemaKinds.td
+++ clang/include/clang/Basic/DiagnosticSemaKinds.td
@@ -8747,6 +8747,12 @@
let CategoryName = "Semantic Issue" in {
+def err_elementwise_math_arg_types_mismatch : Error <
+ "argument types do not match, %0 != %1">;
+
+def err_elementwise_math_invalid_arg_type: Error <
+ "argument type %0 is not supported">;
+
def err_invalid_conversion_between_matrixes : Error<
"conversion between matrix types%diff{ $ and $|}0,1 of different size is not allowed">;
Index: clang/include/clang/Basic/Builtins.def
===================================================================
--- clang/include/clang/Basic/Builtins.def
+++ clang/include/clang/Basic/Builtins.def
@@ -643,6 +643,9 @@
BUILTIN(__builtin_alloca_with_align, "v*zIz", "Fn")
BUILTIN(__builtin_call_with_static_chain, "v.", "nt")
+BUILTIN(__builtin_elementwise_max, "v.", "nct")
+BUILTIN(__builtin_elementwise_min, "v.", "nct")
+
BUILTIN(__builtin_matrix_transpose, "v.", "nFt")
BUILTIN(__builtin_matrix_column_major_load, "v.", "nFt")
BUILTIN(__builtin_matrix_column_major_store, "v.", "nFt")
_______________________________________________
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits