Skip to content

Conversation

@FantasqueX
Copy link
Contributor

No description provided.

@llvmbot llvmbot added clang Clang issues not falling into any other category ClangIR Anything related to the ClangIR project labels Nov 28, 2025
@llvmbot
Copy link
Member

llvmbot commented Nov 28, 2025

@llvm/pr-subscribers-clangir

@llvm/pr-subscribers-clang

Author: Letu Ren (FantasqueX)

Changes

Full diff: https://github.com/llvm/llvm-project/pull/169954.diff

4 Files Affected:

  • (modified) clang/include/clang/CIR/Dialect/IR/CIROps.td (+10)
  • (modified) clang/lib/CIR/CodeGen/CIRGenBuiltin.cpp (+10)
  • (modified) clang/lib/CIR/Lowering/DirectToLLVM/LowerToLLVM.cpp (+8)
  • (modified) clang/test/CIR/CodeGenBuiltins/builtins-floating-point.c (+21)
diff --git a/clang/include/clang/CIR/Dialect/IR/CIROps.td b/clang/include/clang/CIR/Dialect/IR/CIROps.td
index 5f5fab6f12300..dca7fd1485309 100644
--- a/clang/include/clang/CIR/Dialect/IR/CIROps.td
+++ b/clang/include/clang/CIR/Dialect/IR/CIROps.td
@@ -4722,6 +4722,16 @@ def CIR_FAbsOp : CIR_UnaryFPToFPBuiltinOp<"fabs", "FAbsOp"> {
   }];
 }
 
+def CIR_FloorOp : CIR_UnaryFPToFPBuiltinOp<"floor", "FloorOp"> {
+  let summary = "Computes the floating-point floor value";
+  let description = [{
+    `cir.floor` computes the floor of a floating-point operand and returns
+    a result of the same type.
+
+    Floating-point exceptions are ignored, and it does not set `errno`.
+  }];
+}
+
 //===----------------------------------------------------------------------===//
 // Variadic Operations
 //===----------------------------------------------------------------------===//
diff --git a/clang/lib/CIR/CodeGen/CIRGenBuiltin.cpp b/clang/lib/CIR/CodeGen/CIRGenBuiltin.cpp
index 7d4d13121d5e5..8fd3ffeb4e261 100644
--- a/clang/lib/CIR/CodeGen/CIRGenBuiltin.cpp
+++ b/clang/lib/CIR/CodeGen/CIRGenBuiltin.cpp
@@ -321,6 +321,16 @@ RValue CIRGenFunction::emitBuiltinExpr(const GlobalDecl &gd, unsigned builtinID,
   case Builtin::BI__builtin_fabsf128:
     return emitUnaryMaybeConstrainedFPBuiltin<cir::FAbsOp>(*this, *e);
 
+  case Builtin::BIfloor:
+  case Builtin::BIfloorf:
+  case Builtin::BIfloorl:
+  case Builtin::BI__builtin_floor:
+  case Builtin::BI__builtin_floorf:
+  case Builtin::BI__builtin_floorf16:
+  case Builtin::BI__builtin_floorl:
+  case Builtin::BI__builtin_floorf128:
+    return emitUnaryMaybeConstrainedFPBuiltin<cir::FloorOp>(*this, *e);
+
   case Builtin::BI__assume:
   case Builtin::BI__builtin_assume: {
     if (e->getArg(0)->HasSideEffects(getContext()))
diff --git a/clang/lib/CIR/Lowering/DirectToLLVM/LowerToLLVM.cpp b/clang/lib/CIR/Lowering/DirectToLLVM/LowerToLLVM.cpp
index 0c34d87734c3e..25314c3e6cacd 100644
--- a/clang/lib/CIR/Lowering/DirectToLLVM/LowerToLLVM.cpp
+++ b/clang/lib/CIR/Lowering/DirectToLLVM/LowerToLLVM.cpp
@@ -210,6 +210,14 @@ mlir::LogicalResult CIRToLLVMExp2OpLowering::matchAndRewrite(
   return mlir::success();
 }
 
+mlir::LogicalResult CIRToLLVMFloorOpLowering::matchAndRewrite(
+    cir::FloorOp op, OpAdaptor adaptor,
+    mlir::ConversionPatternRewriter &rewriter) const {
+  mlir::Type resTy = typeConverter->convertType(op.getType());
+  rewriter.replaceOpWithNewOp<mlir::LLVM::FFloorOp>(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/CodeGenBuiltins/builtins-floating-point.c b/clang/test/CIR/CodeGenBuiltins/builtins-floating-point.c
index a4307c57b04b6..010633551f57d 100644
--- a/clang/test/CIR/CodeGenBuiltins/builtins-floating-point.c
+++ b/clang/test/CIR/CodeGenBuiltins/builtins-floating-point.c
@@ -67,3 +67,24 @@ long double my_exp2l(long double f) {
   // LLVM: %{{.*}} = call fp128 @llvm.exp2.f128(fp128 %{{.*}})
   // OGCG: %{{.*}} = call fp128 @llvm.exp2.f128(fp128 %{{.*}})
 }
+
+float floorf(float f) {
+  return __builtin_floorf(f);
+  // CIR: %{{.*}} = cir.floor %{{.*}} : !cir.float
+  // LLVM: %{{.*}} = call float @llvm.floor.f32(float %{{.*}})
+  // OGCG: %{{.*}} = call float @llvm.floor.f32(float %{{.*}})
+}
+
+double floor(double f) {
+  return __builtin_floor(f);
+  // CIR: %{{.*}} = cir.floor %{{.*}} : !cir.double
+  // LLVM: %{{.*}} = call double @llvm.floor.f64(double %{{.*}})
+  // OGCG: %{{.*}} = call double @llvm.floor.f64(double %{{.*}})
+}
+
+long double floorl(long double f) {
+  return __builtin_floorl(f);
+  // CIR: %{{.*}} = cir.floor %{{.*}} : !cir.long_double<!cir.f128>
+  // LLVM: %{{.*}} = call fp128 @llvm.floor.f128(fp128 %{{.*}})
+  // OGCG: %{{.*}} = call fp128 @llvm.floor.f128(fp128 %{{.*}})
+}

@github-actions
Copy link

github-actions bot commented Nov 28, 2025

✅ With the latest revision this PR passed the C/C++ code formatter.

Copy link
Member

@AmrDeveloper AmrDeveloper left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

Copy link
Contributor

@andykaylor andykaylor left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm

@andykaylor andykaylor merged commit 91e8780 into llvm:main Dec 2, 2025
10 checks passed
@llvm-ci
Copy link
Collaborator

llvm-ci commented Dec 2, 2025

LLVM Buildbot has detected a new failure on builder openmp-s390x-linux running on systemz-1 while building clang at step 6 "test-openmp".

Full details are available at: https://lab.llvm.org/buildbot/#/builders/88/builds/18639

Here is the relevant piece of the build log for the reference
Step 6 (test-openmp) failure: 1200 seconds without output running [b'ninja', b'-j 4', b'check-openmp'], attempting to kill
...
PASS: ompd-test :: openmp_examples/example_3.c (453 of 463)
PASS: ompd-test :: openmp_examples/example_4.c (454 of 463)
PASS: ompd-test :: openmp_examples/example_5.c (455 of 463)
PASS: ompd-test :: openmp_examples/example_task.c (456 of 463)
UNSUPPORTED: ompd-test :: openmp_examples/ompd_bt.c (457 of 463)
PASS: ompd-test :: openmp_examples/fibonacci.c (458 of 463)
UNSUPPORTED: ompd-test :: openmp_examples/ompd_parallel.c (459 of 463)
PASS: ompd-test :: openmp_examples/parallel.c (460 of 463)
PASS: ompd-test :: openmp_examples/nested.c (461 of 463)
PASS: ompd-test :: openmp_examples/ompd_icvs.c (462 of 463)
command timed out: 1200 seconds without output running [b'ninja', b'-j 4', b'check-openmp'], attempting to kill
process killed by signal 9
program finished with exit code -1
elapsedTime=1360.384454

Comment on lines +4727 to +4732
let description = [{
`cir.floor` computes the floor of a floating-point operand and returns
a result of the same type.

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

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should have had example, can you add it in additional PR please.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

clang Clang issues not falling into any other category ClangIR Anything related to the ClangIR project

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants