Index: clang/include/clang/Basic/Builtins.def =================================================================== --- clang/include/clang/Basic/Builtins.def +++ clang/include/clang/Basic/Builtins.def @@ -646,6 +646,7 @@ 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") Index: clang/include/clang/Basic/DiagnosticSemaKinds.td =================================================================== --- clang/include/clang/Basic/DiagnosticSemaKinds.td +++ clang/include/clang/Basic/DiagnosticSemaKinds.td @@ -11331,8 +11331,9 @@ 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">; Index: clang/include/clang/Sema/Sema.h =================================================================== --- clang/include/clang/Sema/Sema.h +++ clang/include/clang/Sema/Sema.h @@ -12760,7 +12760,7 @@ 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. Index: clang/lib/CodeGen/CGBuiltin.cpp =================================================================== --- clang/lib/CodeGen/CGBuiltin.cpp +++ clang/lib/CodeGen/CGBuiltin.cpp @@ -3112,6 +3112,14 @@ "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)); Index: clang/lib/Sema/SemaChecking.cpp =================================================================== --- clang/lib/Sema/SemaChecking.cpp +++ clang/lib/Sema/SemaChecking.cpp @@ -2098,10 +2098,47 @@ 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)) @@ -16688,26 +16725,19 @@ 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; Index: clang/test/CodeGen/builtins-elementwise-math.c =================================================================== --- clang/test/CodeGen/builtins-elementwise-math.c +++ clang/test/CodeGen/builtins-elementwise-math.c @@ -189,3 +189,20 @@ // 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); +} Index: clang/test/Sema/builtins-elementwise-math.c =================================================================== --- clang/test/Sema/builtins-elementwise-math.c +++ clang/test/Sema/builtins-elementwise-math.c @@ -135,3 +135,24 @@ 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))}} +} Index: clang/test/SemaCXX/builtins-elementwise-math.cpp =================================================================== --- clang/test/SemaCXX/builtins-elementwise-math.cpp +++ clang/test/SemaCXX/builtins-elementwise-math.cpp @@ -36,3 +36,10 @@ 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); +}