Skip to content

Commit 411a53e

Browse files
authored
[CIR] Upstream Builtin Exp2Op (llvm#169152)
Add the cir::exp2 operation and handling for the related builtins.
1 parent 954fa0f commit 411a53e

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
@@ -4612,6 +4612,16 @@ def CIR_ExpOp : CIR_UnaryFPToFPBuiltinOp<"exp", "ExpOp"> {
46124612
}];
46134613
}
46144614

4615+
def CIR_Exp2Op : CIR_UnaryFPToFPBuiltinOp<"exp2", "Exp2Op"> {
4616+
let summary = "Computes the floating-point base-2 exponential value";
4617+
let description = [{
4618+
`cir.exp2` computes the base-2 exponential of a floating-point operand and
4619+
returns a result of the same type.
4620+
4621+
Floating-point exceptions are ignored, and it does not set `errno`.
4622+
}];
4623+
}
4624+
46154625
def CIR_FAbsOp : CIR_UnaryFPToFPBuiltinOp<"fabs", "FAbsOp"> {
46164626
let summary = "Computes the floating-point absolute value";
46174627
let description = [{

clang/lib/CIR/CodeGen/CIRGenBuiltin.cpp

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

303+
case Builtin::BIexp2:
304+
case Builtin::BIexp2f:
305+
case Builtin::BIexp2l:
306+
case Builtin::BI__builtin_exp2:
307+
case Builtin::BI__builtin_exp2f:
308+
case Builtin::BI__builtin_exp2f16:
309+
case Builtin::BI__builtin_exp2l:
310+
case Builtin::BI__builtin_exp2f128:
311+
assert(!cir::MissingFeatures::fastMathFlags());
312+
return emitUnaryMaybeConstrainedFPBuiltin<cir::Exp2Op>(*this, *e);
313+
303314
case Builtin::BIfabs:
304315
case Builtin::BIfabsf:
305316
case Builtin::BIfabsl:

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,14 @@ mlir::LogicalResult CIRToLLVMExpOpLowering::matchAndRewrite(
202202
return mlir::success();
203203
}
204204

205+
mlir::LogicalResult CIRToLLVMExp2OpLowering::matchAndRewrite(
206+
cir::Exp2Op op, OpAdaptor adaptor,
207+
mlir::ConversionPatternRewriter &rewriter) const {
208+
mlir::Type resTy = typeConverter->convertType(op.getType());
209+
rewriter.replaceOpWithNewOp<mlir::LLVM::Exp2Op>(op, resTy, adaptor.getSrc());
210+
return mlir::success();
211+
}
212+
205213
static mlir::Value getLLVMIntCast(mlir::ConversionPatternRewriter &rewriter,
206214
mlir::Value llvmSrc, mlir::Type llvmDstIntTy,
207215
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
@@ -46,3 +46,24 @@ long double expl(long double f) {
4646
// LLVM: %{{.*}} = call fp128 @llvm.exp.f128(fp128 %{{.*}})
4747
// OGCG: %{{.*}} = call fp128 @llvm.exp.f128(fp128 %{{.*}})
4848
}
49+
50+
float exp2f(float f) {
51+
return __builtin_exp2f(f);
52+
// CIR: %{{.*}} = cir.exp2 {{.*}} : !cir.float
53+
// LLVM: %{{.*}} = call float @llvm.exp2.f32(float %{{.*}})
54+
// OGCG: %{{.*}} = call float @llvm.exp2.f32(float %{{.*}})
55+
}
56+
57+
double my_exp2(double f) {
58+
return __builtin_exp2(f);
59+
// CIR: %{{.*}} = cir.exp2 {{.*}} : !cir.double
60+
// LLVM: %{{.*}} = call double @llvm.exp2.f64(double %{{.*}})
61+
// OGCG: %{{.*}} = call double @llvm.exp2.f64(double %{{.*}})
62+
}
63+
64+
long double my_exp2l(long double f) {
65+
return __builtin_exp2l(f);
66+
// CIR: %{{.*}} = cir.exp2 {{.*}} : !cir.long_double<!cir.f128>
67+
// LLVM: %{{.*}} = call fp128 @llvm.exp2.f128(fp128 %{{.*}})
68+
// OGCG: %{{.*}} = call fp128 @llvm.exp2.f128(fp128 %{{.*}})
69+
}

0 commit comments

Comments
 (0)