fhahn created this revision.
fhahn added reviewers: erichkeane, rjmccall, thegameg, anemet.
fhahn requested review of this revision.
Herald added a project: clang.
At the moment, the various math builtins only accept the scalar types
defined by libm.
This patch extends __builtin_fminf to also accept ext_vector_type
arguments that can be converted to an ext_vector_type with float as
element type.
This brings Clang more in line with GCC, which already supports math
builtins with ext_vector_type arguments.
If this direction makes sense, I am planning to also convert a few other
builtins that can be extended to vectors directly, like fmax.
In addition to vector types, it would be also good to support matrix
types as follow up I think.
I tried to make sure the same error messages are emitted for the scalar
variant of __builtin_fminf, but there's a difference for the (vector,
float) variant. I still need to dig into where this is coming from.
Repository:
rG LLVM Github Monorepo
https://reviews.llvm.org/D108832
Files:
clang/include/clang/Basic/Builtins.def
clang/include/clang/Sema/Sema.h
clang/lib/Sema/SemaChecking.cpp
clang/test/CodeGen/overloaded-math-builtins.c
clang/test/Sema/overloaded-math-builtins.c
Index: clang/test/Sema/overloaded-math-builtins.c
===================================================================
--- clang/test/Sema/overloaded-math-builtins.c
+++ clang/test/Sema/overloaded-math-builtins.c
@@ -8,7 +8,7 @@
float r2 = __builtin_fminf(ptr, f);
// expected-error@-1 {{passing 'int *' to parameter of incompatible type 'float'}}
float r3 = __builtin_fminf(v, f);
- // expected-error@-1 {{passing 'float4' (vector of 4 'float' values) to parameter of incompatible type 'float'}}
+ // expected-error@-1 {{initializing 'float' with an expression of incompatible type 'float __attribute__((ext_vector_type(4)))' (vector of 4 'float' values)}}
float r4 = __builtin_fminf(f, v);
// expected-error@-1 {{passing 'float4' (vector of 4 'float' values) to parameter of incompatible type 'float'}}
Index: clang/test/CodeGen/overloaded-math-builtins.c
===================================================================
--- /dev/null
+++ clang/test/CodeGen/overloaded-math-builtins.c
@@ -0,0 +1,16 @@
+// RUN: %clang_cc1 -triple=x86_64-apple-darwin -S -o - -emit-llvm %s | FileCheck %s
+
+typedef float float4 __attribute__((ext_vector_type(4)));
+
+void foo(float f, double d, float4 v) {
+ // CHECK: call float @llvm.minnum.f32(float {{.*}}, float {{.*}})
+ f = __builtin_fminf(f, f);
+
+ // CHECK: [[CONV1:%.*]] = fptrunc double {{.*}} to float
+ // CHECK: [[CONV2:%.*]] = fptrunc double {{.*}} to float
+ // CHECK: call float @llvm.minnum.f32(float [[CONV1]], float [[CONV2]])
+ f = __builtin_fminf(d, d);
+
+ // CHECK: call <4 x float> @llvm.minnum.v4f32(<4 x float> {{.*}}, <4 x float> {{.*}})
+ v = __builtin_fminf(v, v);
+};
Index: clang/lib/Sema/SemaChecking.cpp
===================================================================
--- clang/lib/Sema/SemaChecking.cpp
+++ clang/lib/Sema/SemaChecking.cpp
@@ -1976,6 +1976,10 @@
break;
}
+ case Builtin::BI__builtin_fminf:
+ return SemaBuiltinOverloadedMathBuiltin(TheCall, TheCallResult,
+ Context.FloatTy);
+
case Builtin::BI__builtin_matrix_transpose:
return SemaBuiltinMatrixTranspose(TheCall, TheCallResult);
@@ -16525,6 +16529,55 @@
_2, _3, _4));
}
+ExprResult Sema::SemaBuiltinOverloadedMathBuiltin(CallExpr *TheCall,
+ ExprResult CallResult,
+ QualType ElementType) {
+ if (checkArgCount(*this, TheCall, 2))
+ return ExprError();
+
+ // Apply default Lvalue conversions and convert the expression to size_t.
+ auto ApplyArgumentConversions = [this](Expr *E, QualType T) {
+ ExprResult Res(E);
+ AssignConvertType LHSConvTy = CheckSingleAssignmentConstraints(T, Res);
+ if (DiagnoseAssignmentResult(LHSConvTy, E->getBeginLoc(), T, E->getType(),
+ E, AA_Passing))
+ return ExprResult(true);
+
+ ExprResult Conv = DefaultLvalueConversion(E);
+ assert(
+ !Conv.isInvalid() &&
+ "conversion should be possible according to DiagnoseAssignmentResult");
+
+ return tryConvertExprToType(Conv.get(), T);
+ };
+
+ if (auto *VecType = ElementType->getAs<ExtVectorType>())
+ ElementType = VecType->getElementType();
+
+ QualType ArgTy = ElementType;
+ if (auto *VecType = TheCall->getArg(0)->getType()->getAs<ExtVectorType>()) {
+ ArgTy = Context.getExtVectorType(ElementType, VecType->getNumElements());
+ }
+
+ ExprResult A = ApplyArgumentConversions(TheCall->getArg(0), ArgTy);
+ if (A.isInvalid())
+ return A;
+
+ ExprResult B = ApplyArgumentConversions(TheCall->getArg(1), ArgTy);
+ if (B.isInvalid())
+ return B;
+
+ TheCall->setType(ArgTy);
+
+ auto *D = TheCall->getCalleeDecl();
+ D->addAttr(ConstAttr::CreateImplicit(Context, D->getLocation()));
+
+ // Update call argument to use the possibly converted matrix argument.
+ TheCall->setArg(0, A.get());
+ TheCall->setArg(1, B.get());
+ return CallResult;
+}
+
ExprResult Sema::SemaBuiltinMatrixTranspose(CallExpr *TheCall,
ExprResult CallResult) {
if (checkArgCount(*this, TheCall, 1))
Index: clang/include/clang/Sema/Sema.h
===================================================================
--- clang/include/clang/Sema/Sema.h
+++ clang/include/clang/Sema/Sema.h
@@ -12643,6 +12643,10 @@
bool CheckPPCMMAType(QualType Type, SourceLocation TypeLoc);
+ ExprResult SemaBuiltinOverloadedMathBuiltin(CallExpr *TheCall,
+ ExprResult CallResult,
+ QualType ElementType);
+
// Matrix builtin handling.
ExprResult SemaBuiltinMatrixTranspose(CallExpr *TheCall,
ExprResult CallResult);
Index: clang/include/clang/Basic/Builtins.def
===================================================================
--- clang/include/clang/Basic/Builtins.def
+++ clang/include/clang/Basic/Builtins.def
@@ -257,7 +257,7 @@
BUILTIN(__builtin_fmaxl, "LdLdLd", "Fnc")
BUILTIN(__builtin_fmaxf128, "LLdLLdLLd", "Fnc")
BUILTIN(__builtin_fmin, "ddd", "Fnc")
-BUILTIN(__builtin_fminf, "fff", "Fnc")
+BUILTIN(__builtin_fminf, "v.", "t")
BUILTIN(__builtin_fminf16, "hhh", "Fnc")
BUILTIN(__builtin_fminl, "LdLdLd", "Fnc")
BUILTIN(__builtin_fminf128, "LLdLLdLLd", "Fnc")
_______________________________________________
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits