Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 13 additions & 2 deletions clang/include/clang/CIR/Dialect/IR/CIROps.td
Original file line number Diff line number Diff line change
Expand Up @@ -3732,12 +3732,23 @@ class CIR_UnaryFPToFPBuiltinOp<string mnemonic, string llvmOpName>
let llvmOp = llvmOpName;
}

def CIR_ACosOp : CIR_UnaryFPToFPBuiltinOp<"acos", "ACosOp"> {
let summary = "Computes the arcus cosine of the specified value";
let description = [{
`cir.acos`computes the arcus cosine of a given value and
returns a result of the same type.

Floating-point exceptions are ignored, and it does not set `errno`.
}];
}

def CIR_FAbsOp : CIR_UnaryFPToFPBuiltinOp<"fabs", "FAbsOp"> {
let summary = "Computes the floating-point absolute value";
let description = [{
`cir.fabs` computes the absolute value of a floating-point operand
and returns a result of the same type, ignoring floating-point
exceptions. It does not set `errno`.
and returns a result of the same type.

Floating-point exceptions are ignored, and it does not set `errno`.
}];
}

Expand Down
11 changes: 11 additions & 0 deletions clang/lib/CIR/CodeGen/CIRGenBuiltin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,14 @@ static RValue emitUnaryMaybeConstrainedFPBuiltin(CIRGenFunction &cgf,
return RValue::get(call->getResult(0));
}

template <class Operation>
static RValue emitUnaryFPBuiltin(CIRGenFunction &cgf, const CallExpr &e) {
mlir::Value arg = cgf.emitScalarExpr(e.getArg(0));
auto call =
Operation::create(cgf.getBuilder(), arg.getLoc(), arg.getType(), arg);
return RValue::get(call->getResult(0));
}

RValue CIRGenFunction::emitBuiltinExpr(const GlobalDecl &gd, unsigned builtinID,
const CallExpr *e,
ReturnValueSlot returnValue) {
Expand Down Expand Up @@ -349,6 +357,9 @@ RValue CIRGenFunction::emitBuiltinExpr(const GlobalDecl &gd, unsigned builtinID,
case Builtin::BI__builtin_unreachable:
emitUnreachable(e->getExprLoc(), /*createNewBlock=*/true);
return RValue::get(nullptr);

case Builtin::BI__builtin_elementwise_acos:
return emitUnaryFPBuiltin<cir::ACosOp>(*this, *e);
}

// If this is an alias for a lib function (e.g. __builtin_sin), emit
Expand Down
10 changes: 10 additions & 0 deletions clang/lib/CIR/Lowering/DirectToLLVM/LowerToLLVM.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -577,6 +577,15 @@ struct ConvertCIRToLLVMPass
StringRef getArgument() const override { return "cir-flat-to-llvm"; }
};

mlir::LogicalResult CIRToLLVMACosOpLowering::matchAndRewrite(
cir::ACosOp op, OpAdaptor adaptor,
mlir::ConversionPatternRewriter &rewriter) const {
mlir::Type resTy = typeConverter->convertType(op.getType());
rewriter.replaceOpWithNewOp<mlir::LLVM::ACosOp>(op, resTy,
adaptor.getOperands()[0]);
return mlir::success();
}

mlir::LogicalResult CIRToLLVMAssumeOpLowering::matchAndRewrite(
cir::AssumeOp op, OpAdaptor adaptor,
mlir::ConversionPatternRewriter &rewriter) const {
Expand Down Expand Up @@ -2395,6 +2404,7 @@ void ConvertCIRToLLVMPass::runOnOperation() {
dl);
patterns.add<
// clang-format off
CIRToLLVMACosOpLowering,
CIRToLLVMAssumeOpLowering,
CIRToLLVMAssumeAlignedOpLowering,
CIRToLLVMAssumeSepStorageOpLowering,
Expand Down
9 changes: 9 additions & 0 deletions clang/lib/CIR/Lowering/DirectToLLVM/LowerToLLVM.h
Original file line number Diff line number Diff line change
Expand Up @@ -718,6 +718,15 @@ class CIRToLLVMFAbsOpLowering : public mlir::OpConversionPattern<cir::FAbsOp> {
mlir::ConversionPatternRewriter &) const override;
};

class CIRToLLVMACosOpLowering : public mlir::OpConversionPattern<cir::ACosOp> {
public:
using mlir::OpConversionPattern<cir::ACosOp>::OpConversionPattern;

mlir::LogicalResult
matchAndRewrite(cir::ACosOp op, OpAdaptor,
mlir::ConversionPatternRewriter &) const override;
};

class CIRToLLVMInlineAsmOpLowering
: public mlir::OpConversionPattern<cir::InlineAsmOp> {
mlir::DataLayout const &dataLayout;
Expand Down
38 changes: 38 additions & 0 deletions clang/test/CIR/CodeGen/builtins-elementwise.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
// RUN: %clang_cc1 -triple aarch64-none-linux-android24 -Wno-unused-value -fclangir -emit-cir %s -o %t.cir
// RUN: FileCheck --input-file=%t.cir %s -check-prefix=CIR
// RUN: %clang_cc1 -triple aarch64-none-linux-android24 -Wno-unused-value -fclangir -emit-llvm %s -o %t-cir.ll
// RUN: FileCheck --input-file=%t-cir.ll %s -check-prefix=LLVM
// RUN: %clang_cc1 -triple aarch64-none-linux-android24 -Wno-unused-value -emit-llvm %s -o %t.ll
// RUN: FileCheck --input-file=%t.ll %s -check-prefix=OGCG

typedef int vint4 __attribute__((ext_vector_type(4)));
typedef float vfloat4 __attribute__((ext_vector_type(4)));
typedef double vdouble4 __attribute__((ext_vector_type(4)));

void test_builtin_elementwise_acos(float f, double d, vfloat4 vf4,
vdouble4 vd4) {
// CIR-LABEL: test_builtin_elementwise_acos
// LLVM-LABEL: test_builtin_elementwise_acos
// OGCG-LABEL: test_builtin_elementwise_acos

// CIR: %{{.*}} = cir.acos %{{.*}} : !cir.float
// LLVM: %{{.*}} = call float @llvm.acos.f32(float %{{.*}})
// OGCG: %{{.*}} = call float @llvm.acos.f32(float %{{.*}})
f = __builtin_elementwise_acos(f);

// CIR: %{{.*}} = cir.acos %{{.*}} : !cir.double
// LLVM: %{{.*}} = call double @llvm.acos.f64(double %{{.*}})
// OGCG: %{{.*}} = call double @llvm.acos.f64(double %{{.*}})
d = __builtin_elementwise_acos(d);

// CIR: %{{.*}} = cir.acos %{{.*}} : !cir.vector<4 x !cir.float>
// LLVM: %{{.*}} = call <4 x float> @llvm.acos.v4f32(<4 x float> %{{.*}})
// OGCG: %{{.*}} = call <4 x float> @llvm.acos.v4f32(<4 x float> %{{.*}})
vf4 = __builtin_elementwise_acos(vf4);

// CIR: %{{.*}} = cir.acos %{{.*}} : !cir.vector<4 x !cir.double>
// LLVM: %{{.*}} = call <4 x double> @llvm.acos.v4f64(<4 x double> %{{.*}})
// OGCG: %{{.*}} = call <4 x double> @llvm.acos.v4f64(<4 x double> %{{.*}})
vd4 = __builtin_elementwise_acos(vd4);
}

Loading