-
Notifications
You must be signed in to change notification settings - Fork 15.3k
[CIR] Upstream Builtin Exp2Op #169152
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
[CIR] Upstream Builtin Exp2Op #169152
+50
−0
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Member
|
@llvm/pr-subscribers-clangir @llvm/pr-subscribers-clang Author: Letu Ren (FantasqueX) ChangesFull diff: https://github.com/llvm/llvm-project/pull/169152.diff 4 Files Affected:
diff --git a/clang/include/clang/CIR/Dialect/IR/CIROps.td b/clang/include/clang/CIR/Dialect/IR/CIROps.td
index a19c4f951fff9..777b49434f119 100644
--- a/clang/include/clang/CIR/Dialect/IR/CIROps.td
+++ b/clang/include/clang/CIR/Dialect/IR/CIROps.td
@@ -4612,6 +4612,16 @@ def CIR_ExpOp : CIR_UnaryFPToFPBuiltinOp<"exp", "ExpOp"> {
}];
}
+def CIR_Exp2Op : CIR_UnaryFPToFPBuiltinOp<"exp2", "Exp2Op"> {
+ let summary = "Computes the floating-point base-2 exponential value";
+ let description = [{
+ `cir.exp2` computes the base-2 exponential of a floating-point operand 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 = [{
diff --git a/clang/lib/CIR/CodeGen/CIRGenBuiltin.cpp b/clang/lib/CIR/CodeGen/CIRGenBuiltin.cpp
index 0c1f842829686..104bf541b345f 100644
--- a/clang/lib/CIR/CodeGen/CIRGenBuiltin.cpp
+++ b/clang/lib/CIR/CodeGen/CIRGenBuiltin.cpp
@@ -298,6 +298,17 @@ RValue CIRGenFunction::emitBuiltinExpr(const GlobalDecl &gd, unsigned builtinID,
assert(!cir::MissingFeatures::fastMathFlags());
return emitUnaryMaybeConstrainedFPBuiltin<cir::ExpOp>(*this, *e);
+ case Builtin::BIexp2:
+ case Builtin::BIexp2f:
+ case Builtin::BIexp2l:
+ case Builtin::BI__builtin_exp2:
+ case Builtin::BI__builtin_exp2f:
+ case Builtin::BI__builtin_exp2f16:
+ case Builtin::BI__builtin_exp2l:
+ case Builtin::BI__builtin_exp2f128:
+ assert(!cir::MissingFeatures::fastMathFlags());
+ return emitUnaryMaybeConstrainedFPBuiltin<cir::Exp2Op>(*this, *e);
+
case Builtin::BIfabs:
case Builtin::BIfabsf:
case Builtin::BIfabsl:
diff --git a/clang/lib/CIR/Lowering/DirectToLLVM/LowerToLLVM.cpp b/clang/lib/CIR/Lowering/DirectToLLVM/LowerToLLVM.cpp
index 6136d48204e0c..cd923a15af132 100644
--- a/clang/lib/CIR/Lowering/DirectToLLVM/LowerToLLVM.cpp
+++ b/clang/lib/CIR/Lowering/DirectToLLVM/LowerToLLVM.cpp
@@ -202,6 +202,14 @@ mlir::LogicalResult CIRToLLVMExpOpLowering::matchAndRewrite(
return mlir::success();
}
+mlir::LogicalResult CIRToLLVMExp2OpLowering::matchAndRewrite(
+ cir::Exp2Op op, OpAdaptor adaptor,
+ mlir::ConversionPatternRewriter &rewriter) const {
+ mlir::Type resTy = typeConverter->convertType(op.getType());
+ rewriter.replaceOpWithNewOp<mlir::LLVM::Exp2Op>(op, resTy, adaptor.getSrc());
+ return mlir::success();
+}
+
static mlir::Value getLLVMIntCast(mlir::ConversionPatternRewriter &rewriter,
mlir::Value llvmSrc, mlir::Type llvmDstIntTy,
bool isUnsigned, uint64_t cirSrcWidth,
diff --git a/clang/test/CIR/CodeGen/builtins-floating-point.c b/clang/test/CIR/CodeGen/builtins-floating-point.c
index 1b7de650662c7..a4307c57b04b6 100644
--- a/clang/test/CIR/CodeGen/builtins-floating-point.c
+++ b/clang/test/CIR/CodeGen/builtins-floating-point.c
@@ -46,3 +46,24 @@ long double expl(long double f) {
// LLVM: %{{.*}} = call fp128 @llvm.exp.f128(fp128 %{{.*}})
// OGCG: %{{.*}} = call fp128 @llvm.exp.f128(fp128 %{{.*}})
}
+
+float exp2f(float f) {
+ return __builtin_exp2f(f);
+ // CIR: %{{.*}} = cir.exp2 {{.*}} : !cir.float
+ // LLVM: %{{.*}} = call float @llvm.exp2.f32(float %{{.*}})
+ // OGCG: %{{.*}} = call float @llvm.exp2.f32(float %{{.*}})
+}
+
+double my_exp2(double f) {
+ return __builtin_exp2(f);
+ // CIR: %{{.*}} = cir.exp2 {{.*}} : !cir.double
+ // LLVM: %{{.*}} = call double @llvm.exp2.f64(double %{{.*}})
+ // OGCG: %{{.*}} = call double @llvm.exp2.f64(double %{{.*}})
+}
+
+long double my_exp2l(long double f) {
+ return __builtin_exp2l(f);
+ // CIR: %{{.*}} = cir.exp2 {{.*}} : !cir.long_double<!cir.f128>
+ // LLVM: %{{.*}} = call fp128 @llvm.exp2.f128(fp128 %{{.*}})
+ // OGCG: %{{.*}} = call fp128 @llvm.exp2.f128(fp128 %{{.*}})
+}
|
andykaylor
approved these changes
Nov 24, 2025
Contributor
andykaylor
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
lgtm
Contributor
Author
|
@andykaylor Hi, could you please help me merge this PR? |
tanji-dg
pushed a commit
to tanji-dg/llvm-project
that referenced
this pull request
Nov 27, 2025
Add the cir::exp2 operation and handling for the related builtins.
GeneraluseAI
pushed a commit
to GeneraluseAI/llvm-project
that referenced
this pull request
Nov 27, 2025
Add the cir::exp2 operation and handling for the related builtins.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
No description provided.