Skip to content

Commit dbe544d

Browse files
authored
[CIR][CIRGen] handle __builtin_elementwise_acos (#1362)
Traditional Clang implementation: https://github.com/llvm/clangir/blob/a0091e38f1027e35d17819e02ee1ae257a12d296/clang/lib/CodeGen/CGBuiltin.cpp#L4116-L4118 I use the first argument type as the return type. It is OK for `__builtin_elementwise_acos`, however, I'm not sure it is OK for other builtin functions. Resolves: #1361
1 parent a99b65f commit dbe544d

File tree

3 files changed

+37
-2
lines changed

3 files changed

+37
-2
lines changed

clang/lib/CIR/CodeGen/CIRGenBuiltin.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1356,8 +1356,9 @@ RValue CIRGenFunction::emitBuiltinExpr(const GlobalDecl GD, unsigned BuiltinID,
13561356
mlir::Value result = call->getResult(0);
13571357
return RValue::get(result);
13581358
}
1359-
case Builtin::BI__builtin_elementwise_acos:
1360-
llvm_unreachable("BI__builtin_elementwise_acos NYI");
1359+
case Builtin::BI__builtin_elementwise_acos: {
1360+
return emitBuiltinWithOneOverloadedType<1>(E, "acos");
1361+
}
13611362
case Builtin::BI__builtin_elementwise_asin:
13621363
llvm_unreachable("BI__builtin_elementwise_asin NYI");
13631364
case Builtin::BI__builtin_elementwise_atan:

clang/lib/CIR/CodeGen/CIRGenFunction.h

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1403,6 +1403,19 @@ class CIRGenFunction : public CIRGenTypeCache {
14031403
RValue emitBuiltinExpr(const clang::GlobalDecl GD, unsigned BuiltinID,
14041404
const clang::CallExpr *E, ReturnValueSlot ReturnValue);
14051405
RValue emitRotate(const CallExpr *E, bool IsRotateRight);
1406+
template <uint32_t N>
1407+
RValue emitBuiltinWithOneOverloadedType(const CallExpr *E,
1408+
llvm::StringRef Name) {
1409+
static_assert(N, "expect non-empty argument");
1410+
mlir::Type cirTy = convertType(E->getArg(0)->getType());
1411+
SmallVector<mlir::Value, N> args;
1412+
for (uint32_t i = 0; i < N; ++i) {
1413+
args.push_back(emitScalarExpr(E->getArg(i)));
1414+
}
1415+
const auto call = builder.create<cir::LLVMIntrinsicCallOp>(
1416+
getLoc(E->getExprLoc()), builder.getStringAttr(Name), cirTy, args);
1417+
return RValue::get(call->getResult(0));
1418+
}
14061419
mlir::Value emitTargetBuiltinExpr(unsigned BuiltinID,
14071420
const clang::CallExpr *E,
14081421
ReturnValueSlot ReturnValue);

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

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,3 +36,24 @@ void test_builtin_elementwise_abs(vint4 vi4, int i, float f, double d,
3636
// LLVM: {{%.*}} = call <4 x double> @llvm.fabs.v4f64(<4 x double> {{%.*}})
3737
vd4 = __builtin_elementwise_abs(vd4);
3838
}
39+
40+
void test_builtin_elementwise_acos(float f, double d, vfloat4 vf4,
41+
vdouble4 vd4) {
42+
// CIR-LABEL: test_builtin_elementwise_acos
43+
// LLVM-LABEL: test_builtin_elementwise_acos
44+
// CIR: {{%.*}} = cir.llvm.intrinsic "acos" {{%.*}} : (!cir.float) -> !cir.float
45+
// LLVM: {{%.*}} = call float @llvm.acos.f32(float {{%.*}})
46+
f = __builtin_elementwise_acos(f);
47+
48+
// CIR: {{%.*}} = cir.llvm.intrinsic "acos" {{%.*}} : (!cir.double) -> !cir.double
49+
// LLVM: {{%.*}} = call double @llvm.acos.f64(double {{%.*}})
50+
d = __builtin_elementwise_acos(d);
51+
52+
// CIR: {{%.*}} = cir.llvm.intrinsic "acos" {{%.*}} : (!cir.vector<!cir.float x 4>) -> !cir.vector<!cir.float x 4>
53+
// LLVM: {{%.*}} = call <4 x float> @llvm.acos.v4f32(<4 x float> {{%.*}})
54+
vf4 = __builtin_elementwise_acos(vf4);
55+
56+
// CIR: {{%.*}} = cir.llvm.intrinsic "acos" {{%.*}} : (!cir.vector<!cir.double x 4>) -> !cir.vector<!cir.double x 4>
57+
// LLVM: {{%.*}} = call <4 x double> @llvm.acos.v4f64(<4 x double> {{%.*}})
58+
vd4 = __builtin_elementwise_acos(vd4);
59+
}

0 commit comments

Comments
 (0)