Skip to content

Commit 8ee1803

Browse files
authored
[CIR] Upstream Builtin ExpOp (#166061)
Upstream the Builtin ExpOp
1 parent fb21f16 commit 8ee1803

File tree

4 files changed

+50
-0
lines changed

4 files changed

+50
-0
lines changed

clang/include/clang/CIR/Dialect/IR/CIROps.td

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4191,6 +4191,16 @@ def CIR_CosOp : CIR_UnaryFPToFPBuiltinOp<"cos", "CosOp"> {
41914191
}];
41924192
}
41934193

4194+
def CIR_ExpOp : CIR_UnaryFPToFPBuiltinOp<"exp", "ExpOp"> {
4195+
let summary = "Computes the floating-point base-e exponential value";
4196+
let description = [{
4197+
`cir.exp` computes the exponential of a floating-point operand and returns
4198+
a result of the same type.
4199+
4200+
Floating-point exceptions are ignored, and it does not set `errno`.
4201+
}];
4202+
}
4203+
41944204
def CIR_FAbsOp : CIR_UnaryFPToFPBuiltinOp<"fabs", "FAbsOp"> {
41954205
let summary = "Computes the floating-point absolute value";
41964206
let description = [{

clang/lib/CIR/CodeGen/CIRGenBuiltin.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,17 @@ RValue CIRGenFunction::emitBuiltinExpr(const GlobalDecl &gd, unsigned builtinID,
222222
assert(!cir::MissingFeatures::fastMathFlags());
223223
return emitUnaryMaybeConstrainedFPBuiltin<cir::CeilOp>(*this, *e);
224224

225+
case Builtin::BIexp:
226+
case Builtin::BIexpf:
227+
case Builtin::BIexpl:
228+
case Builtin::BI__builtin_exp:
229+
case Builtin::BI__builtin_expf:
230+
case Builtin::BI__builtin_expf16:
231+
case Builtin::BI__builtin_expl:
232+
case Builtin::BI__builtin_expf128:
233+
assert(!cir::MissingFeatures::fastMathFlags());
234+
return emitUnaryMaybeConstrainedFPBuiltin<cir::ExpOp>(*this, *e);
235+
225236
case Builtin::BIfabs:
226237
case Builtin::BIfabsf:
227238
case Builtin::BIfabsl:

clang/lib/CIR/Lowering/DirectToLLVM/LowerToLLVM.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,14 @@ mlir::LogicalResult CIRToLLVMCosOpLowering::matchAndRewrite(
194194
return mlir::success();
195195
}
196196

197+
mlir::LogicalResult CIRToLLVMExpOpLowering::matchAndRewrite(
198+
cir::ExpOp op, OpAdaptor adaptor,
199+
mlir::ConversionPatternRewriter &rewriter) const {
200+
mlir::Type resTy = typeConverter->convertType(op.getType());
201+
rewriter.replaceOpWithNewOp<mlir::LLVM::ExpOp>(op, resTy, adaptor.getSrc());
202+
return mlir::success();
203+
}
204+
197205
static mlir::Value getLLVMIntCast(mlir::ConversionPatternRewriter &rewriter,
198206
mlir::Value llvmSrc, mlir::Type llvmDstIntTy,
199207
bool isUnsigned, uint64_t cirSrcWidth,

clang/test/CIR/CodeGen/builtins-floating-point.c

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,3 +25,24 @@ float ceil(float f) {
2525
// LLVM: %{{.*}} = call float @llvm.ceil.f32(float %{{.*}})
2626
// OGCG: %{{.*}} = call float @llvm.ceil.f32(float %{{.*}})
2727
}
28+
29+
float expf(float f) {
30+
return __builtin_expf(f);
31+
// CIR: %{{.*}} = cir.exp {{.*}} : !cir.float
32+
// LLVM: %{{.*}} = call float @llvm.exp.f32(float %{{.*}})
33+
// OGCG: %{{.*}} = call float @llvm.exp.f32(float %{{.*}})
34+
}
35+
36+
double exp(double f) {
37+
return __builtin_exp(f);
38+
// CIR: %{{.*}} = cir.exp {{.*}} : !cir.double
39+
// LLVM: %{{.*}} = call double @llvm.exp.f64(double %{{.*}})
40+
// OGCG: %{{.*}} = call double @llvm.exp.f64(double %{{.*}})
41+
}
42+
43+
long double expl(long double f) {
44+
return __builtin_expl(f);
45+
// CIR: %{{.*}} = cir.exp {{.*}} : !cir.long_double<!cir.f128>
46+
// LLVM: %{{.*}} = call fp128 @llvm.exp.f128(fp128 %{{.*}})
47+
// OGCG: %{{.*}} = call fp128 @llvm.exp.f128(fp128 %{{.*}})
48+
}

0 commit comments

Comments
 (0)