Skip to content

Commit cb4c940

Browse files
authored
[CIR][CIRGen][Builtin] Support __builtin_elementwise_abs with vector of floating type (#1174)
[PR1132](#1132) implements missing feature `fpUnaryOPsSupportVectorType`, so revisit this code. One another thing changed is that I stopped using `cir::isAnyFloatingPointType` as it contains types like long double and FP80 which are not supported by the [builtin's signature](https://clang.llvm.org/docs/LanguageExtensions.html#vector-builtins)
1 parent fb77b51 commit cb4c940

File tree

3 files changed

+16
-6
lines changed

3 files changed

+16
-6
lines changed

clang/include/clang/CIR/MissingFeatures.h

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -330,9 +330,6 @@ struct MissingFeatures {
330330

331331
//-- Other missing features
332332

333-
// We need to extend fpUnaryOPs to support vector types.
334-
static bool fpUnaryOPsSupportVectorType() { return false; }
335-
336333
// We need to track the parent record types that represent a field
337334
// declaration. This is necessary to determine the layout of a class.
338335
static bool fieldDeclAbstraction() { return false; }

clang/lib/CIR/CodeGen/CIRGenBuiltin.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1309,10 +1309,12 @@ RValue CIRGenFunction::emitBuiltinExpr(const GlobalDecl GD, unsigned BuiltinID,
13091309
mlir::Type cirTy = ConvertType(E->getArg(0)->getType());
13101310
bool isIntTy = cir::isIntOrIntVectorTy(cirTy);
13111311
if (!isIntTy) {
1312-
if (cir::isAnyFloatingPointType(cirTy)) {
1312+
mlir::Type eltTy = cirTy;
1313+
if (mlir::isa<cir::VectorType>(cirTy))
1314+
eltTy = mlir::cast<cir::VectorType>(cirTy).getEltType();
1315+
if (mlir::isa<cir::SingleType, cir::DoubleType>(eltTy)) {
13131316
return emitUnaryFPBuiltin<cir::FAbsOp>(*this, *E);
13141317
}
1315-
assert(!MissingFeatures::fpUnaryOPsSupportVectorType());
13161318
llvm_unreachable("unsupported type for BI__builtin_elementwise_abs");
13171319
}
13181320
mlir::Value arg = emitScalarExpr(E->getArg(0));

clang/test/CIR/CodeGen/builtins-elementwise.c

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,11 @@
55
// RUN: FileCheck --check-prefix=LLVM --input-file=%t.ll %s
66

77
typedef int vint4 __attribute__((ext_vector_type(4)));
8+
typedef float vfloat4 __attribute__((ext_vector_type(4)));
9+
typedef double vdouble4 __attribute__((ext_vector_type(4)));
810

9-
void test_builtin_elementwise_abs(vint4 vi4, int i, float f, double d) {
11+
void test_builtin_elementwise_abs(vint4 vi4, int i, float f, double d,
12+
vfloat4 vf4, vdouble4 vd4) {
1013
// CIR-LABEL: test_builtin_elementwise_abs
1114
// LLVM-LABEL: test_builtin_elementwise_abs
1215
// CIR: {{%.*}} = cir.fabs {{%.*}} : !cir.float
@@ -24,4 +27,12 @@ void test_builtin_elementwise_abs(vint4 vi4, int i, float f, double d) {
2427
// CIR: {{%.*}} = cir.abs {{%.*}} : !s32
2528
// LLVM: {{%.*}} = call i32 @llvm.abs.i32(i32 {{%.*}}, i1 false)
2629
i = __builtin_elementwise_abs(i);
30+
31+
// CIR: {{%.*}} = cir.fabs {{%.*}} : !cir.vector<!cir.float x 4>
32+
// LLVM: {{%.*}} = call <4 x float> @llvm.fabs.v4f32(<4 x float> {{%.*}})
33+
vf4 = __builtin_elementwise_abs(vf4);
34+
35+
// CIR: {{%.*}} = cir.fabs {{%.*}} : !cir.vector<!cir.double x 4>
36+
// LLVM: {{%.*}} = call <4 x double> @llvm.fabs.v4f64(<4 x double> {{%.*}})
37+
vd4 = __builtin_elementwise_abs(vd4);
2738
}

0 commit comments

Comments
 (0)