From 8680f951c21e675a110e79c9b8dc59bb94290b01 Mon Sep 17 00:00:00 2001 From: Jun Zhang Date: Wed, 8 Dec 2021 08:29:33 -0500 Subject: [PATCH] Add __builtin_elementwise_ceil This patch implements one of the missing builtin functions specified in https://reviews.llvm.org/D111529. --- clang/include/clang/Basic/Builtins.def | 1 + clang/include/clang/Basic/DiagnosticSemaKinds.td | 5 ++- clang/include/clang/Sema/Sema.h | 2 +- clang/lib/CodeGen/CGBuiltin.cpp | 8 ++++ clang/lib/Sema/SemaChecking.cpp | 54 ++++++++++++++++++------ clang/test/CodeGen/builtins-elementwise-math.c | 17 ++++++++ clang/test/Sema/builtins-elementwise-math.c | 21 +++++++++ clang/test/SemaCXX/builtins-elementwise-math.cpp | 7 +++ 8 files changed, 100 insertions(+), 15 deletions(-) diff --git a/clang/include/clang/Basic/Builtins.def b/clang/include/clang/Basic/Builtins.def index ad8b66a..1626605 100644 --- a/clang/include/clang/Basic/Builtins.def +++ b/clang/include/clang/Basic/Builtins.def @@ -646,6 +646,7 @@ BUILTIN(__builtin_call_with_static_chain, "v.", "nt") BUILTIN(__builtin_elementwise_abs, "v.", "nct") BUILTIN(__builtin_elementwise_max, "v.", "nct") BUILTIN(__builtin_elementwise_min, "v.", "nct") +BUILTIN(__builtin_elementwise_ceil, "v.", "nct") BUILTIN(__builtin_reduce_max, "v.", "nct") BUILTIN(__builtin_reduce_min, "v.", "nct") diff --git a/clang/include/clang/Basic/DiagnosticSemaKinds.td b/clang/include/clang/Basic/DiagnosticSemaKinds.td index a5312f9..4e113e9 100644 --- a/clang/include/clang/Basic/DiagnosticSemaKinds.td +++ b/clang/include/clang/Basic/DiagnosticSemaKinds.td @@ -11357,8 +11357,9 @@ def err_builtin_launder_invalid_arg : Error< def err_builtin_invalid_arg_type: Error < "%ordinal0 argument must be a " "%select{vector, integer or floating point type|matrix|" - "pointer to a valid matrix element type|" - "signed integer or floating point type|vector type}1 (was %2)">; + "pointer to a valid matrix element type|" + "signed integer or floating point type|vector type|" + "floating point type}1 (was %2)">; def err_builtin_matrix_disabled: Error< "matrix types extension is disabled. Pass -fenable-matrix to enable it">; diff --git a/clang/include/clang/Sema/Sema.h b/clang/include/clang/Sema/Sema.h index abecf68e..a12dd4d 100644 --- a/clang/include/clang/Sema/Sema.h +++ b/clang/include/clang/Sema/Sema.h @@ -12777,7 +12777,7 @@ private: bool CheckPPCMMAType(QualType Type, SourceLocation TypeLoc); bool SemaBuiltinElementwiseMath(CallExpr *TheCall); - bool SemaBuiltinElementwiseMathOneArg(CallExpr *TheCall); + bool PrepareBuiltinElementwiseMathOneArgCall(CallExpr *TheCall); bool SemaBuiltinReduceMath(CallExpr *TheCall); // Matrix builtin handling. diff --git a/clang/lib/CodeGen/CGBuiltin.cpp b/clang/lib/CodeGen/CGBuiltin.cpp index 5caa81f..efe41f6 100644 --- a/clang/lib/CodeGen/CGBuiltin.cpp +++ b/clang/lib/CodeGen/CGBuiltin.cpp @@ -3133,6 +3133,14 @@ RValue CodeGenFunction::EmitBuiltinExpr(const GlobalDecl GD, unsigned BuiltinID, "elt.abs"); return RValue::get(Result); } + + case Builtin::BI__builtin_elementwise_ceil: { + Value *Op0 = EmitScalarExpr(E->getArg(0)); + Value *Result = Builder.CreateUnaryIntrinsic(llvm::Intrinsic::ceil, Op0, + nullptr, "elt.ceil"); + return RValue::get(Result); + } + case Builtin::BI__builtin_elementwise_max: { Value *Op0 = EmitScalarExpr(E->getArg(0)); Value *Op1 = EmitScalarExpr(E->getArg(1)); diff --git a/clang/lib/Sema/SemaChecking.cpp b/clang/lib/Sema/SemaChecking.cpp index fc3886e..7e44e97 100644 --- a/clang/lib/Sema/SemaChecking.cpp +++ b/clang/lib/Sema/SemaChecking.cpp @@ -2098,10 +2098,47 @@ Sema::CheckBuiltinFunctionCall(FunctionDecl *FDecl, unsigned BuiltinID, break; } - case Builtin::BI__builtin_elementwise_abs: - if (SemaBuiltinElementwiseMathOneArg(TheCall)) + // __builtin_elementwise_abs restricts the element type to signed integers or + // floating point types only. + case Builtin::BI__builtin_elementwise_abs: { + if (PrepareBuiltinElementwiseMathOneArgCall(TheCall)) return ExprError(); + + QualType ArgTy = TheCall->getArg(0)->getType(); + QualType EltTy = ArgTy; + + if (auto *VecTy = EltTy->getAs()) + EltTy = VecTy->getElementType(); + if (EltTy->isUnsignedIntegerType()) { + Diag(TheCall->getArg(0)->getBeginLoc(), + diag::err_builtin_invalid_arg_type) + << 1 << /* signed integer or float ty*/ 3 << ArgTy; + return ExprError(); + } + break; + } + + // __builtin_elementwise_ceil restricts the element type to floating point + // types only. + case Builtin::BI__builtin_elementwise_ceil: { + if (PrepareBuiltinElementwiseMathOneArgCall(TheCall)) + return ExprError(); + + QualType ArgTy = TheCall->getArg(0)->getType(); + QualType EltTy = ArgTy; + + if (auto *VecTy = EltTy->getAs()) + EltTy = VecTy->getElementType(); + if (!EltTy->isFloatingType()) { + Diag(TheCall->getArg(0)->getBeginLoc(), + diag::err_builtin_invalid_arg_type) + << 1 << /* float ty*/ 5 << ArgTy; + + return ExprError(); + } break; + } + case Builtin::BI__builtin_elementwise_min: case Builtin::BI__builtin_elementwise_max: if (SemaBuiltinElementwiseMath(TheCall)) @@ -16697,26 +16734,19 @@ static bool checkMathBuiltinElementType(Sema &S, SourceLocation Loc, return false; } -bool Sema::SemaBuiltinElementwiseMathOneArg(CallExpr *TheCall) { +bool Sema::PrepareBuiltinElementwiseMathOneArgCall(CallExpr *TheCall) { if (checkArgCount(*this, TheCall, 1)) return true; ExprResult A = UsualUnaryConversions(TheCall->getArg(0)); - SourceLocation ArgLoc = TheCall->getArg(0)->getBeginLoc(); if (A.isInvalid()) return true; TheCall->setArg(0, A.get()); QualType TyA = A.get()->getType(); - if (checkMathBuiltinElementType(*this, ArgLoc, TyA)) - return true; - QualType EltTy = TyA; - if (auto *VecTy = EltTy->getAs()) - EltTy = VecTy->getElementType(); - if (EltTy->isUnsignedIntegerType()) - return Diag(ArgLoc, diag::err_builtin_invalid_arg_type) - << 1 << /*signed integer or float ty*/ 3 << TyA; + if (checkMathBuiltinElementType(*this, A.get()->getBeginLoc(), TyA)) + return true; TheCall->setType(TyA); return false; diff --git a/clang/test/CodeGen/builtins-elementwise-math.c b/clang/test/CodeGen/builtins-elementwise-math.c index bfb1624..dd3ac96 100644 --- a/clang/test/CodeGen/builtins-elementwise-math.c +++ b/clang/test/CodeGen/builtins-elementwise-math.c @@ -189,3 +189,20 @@ void test_builtin_elementwise_min(float f1, float f2, double d1, double d2, // CHECK-NEXT: call i32 @llvm.smin.i32(i32 [[IAS1]], i32 [[B]]) int_as_one = __builtin_elementwise_min(int_as_one, b); } + +void test_builtin_elementwise_ceil(float f1, float f2, double d1, double d2, + float4 vf1, float4 vf2, si8 vi1, si8 vi2, + long long int i1, long long int i2, short si) { + // CHECK-LABEL: define void @test_builtin_elementwise_ceil( + // CHECK: [[F1:%.+]] = load float, float* %f1.addr, align 4 + // CHECK-NEXT: call float @llvm.ceil.f32(float [[F1]]) + f2 = __builtin_elementwise_ceil(f1); + + // CHECK: [[D1:%.+]] = load double, double* %d1.addr, align 8 + // CHECK-NEXT: call double @llvm.ceil.f64(double [[D1]]) + d2 = __builtin_elementwise_ceil(d1); + + // CHECK: [[VF1:%.+]] = load <4 x float>, <4 x float>* %vf1.addr, align 16 + // CHECK-NEXT: call <4 x float> @llvm.ceil.v4f32(<4 x float> [[VF1]]) + vf2 = __builtin_elementwise_ceil(vf1); +} diff --git a/clang/test/Sema/builtins-elementwise-math.c b/clang/test/Sema/builtins-elementwise-math.c index 4008f77..41228c6 100644 --- a/clang/test/Sema/builtins-elementwise-math.c +++ b/clang/test/Sema/builtins-elementwise-math.c @@ -135,3 +135,24 @@ void test_builtin_elementwise_min(int i, short s, double d, float4 v, int3 iv, i c1 = __builtin_elementwise_min(c1, c2); // expected-error@-1 {{1st argument must be a vector, integer or floating point type (was '_Complex float')}} } + +void test_builtin_elementwise_ceil(int i, float f, double d, float4 v, int3 iv, unsigned u, unsigned4 uv) { + + struct Foo s = __builtin_elementwise_ceil(f); + // expected-error@-1 {{initializing 'struct Foo' with an expression of incompatible type 'float'}} + + i = __builtin_elementwise_ceil(); + // expected-error@-1 {{too few arguments to function call, expected 1, have 0}} + + i = __builtin_elementwise_ceil(i); + // expected-error@-1 {{1st argument must be a floating point type (was 'int')}} + + i = __builtin_elementwise_ceil(f, f); + // expected-error@-1 {{too many arguments to function call, expected 1, have 2}} + + u = __builtin_elementwise_ceil(u); + // expected-error@-1 {{1st argument must be a floating point type (was 'unsigned int')}} + + uv = __builtin_elementwise_ceil(uv); + // expected-error@-1 {{1st argument must be a floating point type (was 'unsigned4' (vector of 4 'unsigned int' values))}} +} diff --git a/clang/test/SemaCXX/builtins-elementwise-math.cpp b/clang/test/SemaCXX/builtins-elementwise-math.cpp index fcf03d7..c40095a 100644 --- a/clang/test/SemaCXX/builtins-elementwise-math.cpp +++ b/clang/test/SemaCXX/builtins-elementwise-math.cpp @@ -36,3 +36,10 @@ void test_builtin_elementwise_min() { static_assert(!is_const::value); static_assert(!is_const::value); } + +void test_builtin_elementwise_ceil() { + const float a = 42.0; + float b = 42.3; + static_assert(!is_const::value); + static_assert(!is_const::value); +} -- 2.7.4