Skip to content

Commit

Permalink
Add __builtin_elementwise_ceil
Browse files Browse the repository at this point in the history
This patch implements one of the missing builtin functions specified
in https://reviews.llvm.org/D111529.
  • Loading branch information
junaire authored and AaronBallman committed Dec 8, 2021
1 parent 88c183e commit 8680f95
Show file tree
Hide file tree
Showing 8 changed files with 100 additions and 15 deletions.
1 change: 1 addition & 0 deletions clang/include/clang/Basic/Builtins.def
Expand Up @@ -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")

Expand Down
5 changes: 3 additions & 2 deletions clang/include/clang/Basic/DiagnosticSemaKinds.td
Expand Up @@ -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">;
Expand Down
2 changes: 1 addition & 1 deletion clang/include/clang/Sema/Sema.h
Expand Up @@ -12777,7 +12777,7 @@ class Sema final {
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.
Expand Down
8 changes: 8 additions & 0 deletions clang/lib/CodeGen/CGBuiltin.cpp
Expand Up @@ -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));
Expand Down
54 changes: 42 additions & 12 deletions clang/lib/Sema/SemaChecking.cpp
Expand Up @@ -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<VectorType>())
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<VectorType>())
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))
Expand Down Expand Up @@ -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<VectorType>())
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;
Expand Down
17 changes: 17 additions & 0 deletions clang/test/CodeGen/builtins-elementwise-math.c
Expand Up @@ -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) {

This comment has been minimized.

Copy link
@tambry

tambry Dec 8, 2021

Contributor

Everything after vf2 is unused?

This comment has been minimized.

Copy link
@AaronBallman

AaronBallman Dec 8, 2021

Collaborator

Thanks for catching this, I updated the test in ca70b80

// 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);
}
21 changes: 21 additions & 0 deletions clang/test/Sema/builtins-elementwise-math.c
Expand Up @@ -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))}}
}
7 changes: 7 additions & 0 deletions clang/test/SemaCXX/builtins-elementwise-math.cpp
Expand Up @@ -36,3 +36,10 @@ void test_builtin_elementwise_min() {
static_assert(!is_const<decltype(__builtin_elementwise_min(b, a))>::value);
static_assert(!is_const<decltype(__builtin_elementwise_min(a, a))>::value);
}

void test_builtin_elementwise_ceil() {
const float a = 42.0;
float b = 42.3;
static_assert(!is_const<decltype(__builtin_elementwise_ceil(a))>::value);
static_assert(!is_const<decltype(__builtin_elementwise_ceil(b))>::value);
}

0 comments on commit 8680f95

Please sign in to comment.