Skip to content

Commit 91e8780

Browse files
authored
[CIR] Upstream Builtin FloorOp (#169954)
1 parent c6f501d commit 91e8780

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
@@ -4722,6 +4722,16 @@ def CIR_FAbsOp : CIR_UnaryFPToFPBuiltinOp<"fabs", "FAbsOp"> {
47224722
}];
47234723
}
47244724

4725+
def CIR_FloorOp : CIR_UnaryFPToFPBuiltinOp<"floor", "FloorOp"> {
4726+
let summary = "Computes the floating-point floor value";
4727+
let description = [{
4728+
`cir.floor` computes the floor of a floating-point operand and returns
4729+
a result of the same type.
4730+
4731+
Floating-point exceptions are ignored, and it does not set `errno`.
4732+
}];
4733+
}
4734+
47254735
//===----------------------------------------------------------------------===//
47264736
// Variadic Operations
47274737
//===----------------------------------------------------------------------===//

clang/lib/CIR/CodeGen/CIRGenBuiltin.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -321,6 +321,16 @@ RValue CIRGenFunction::emitBuiltinExpr(const GlobalDecl &gd, unsigned builtinID,
321321
case Builtin::BI__builtin_fabsf128:
322322
return emitUnaryMaybeConstrainedFPBuiltin<cir::FAbsOp>(*this, *e);
323323

324+
case Builtin::BIfloor:
325+
case Builtin::BIfloorf:
326+
case Builtin::BIfloorl:
327+
case Builtin::BI__builtin_floor:
328+
case Builtin::BI__builtin_floorf:
329+
case Builtin::BI__builtin_floorf16:
330+
case Builtin::BI__builtin_floorl:
331+
case Builtin::BI__builtin_floorf128:
332+
return emitUnaryMaybeConstrainedFPBuiltin<cir::FloorOp>(*this, *e);
333+
324334
case Builtin::BI__assume:
325335
case Builtin::BI__builtin_assume: {
326336
if (e->getArg(0)->HasSideEffects(getContext()))

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,15 @@ mlir::LogicalResult CIRToLLVMExp2OpLowering::matchAndRewrite(
210210
return mlir::success();
211211
}
212212

213+
mlir::LogicalResult CIRToLLVMFloorOpLowering::matchAndRewrite(
214+
cir::FloorOp op, OpAdaptor adaptor,
215+
mlir::ConversionPatternRewriter &rewriter) const {
216+
mlir::Type resTy = typeConverter->convertType(op.getType());
217+
rewriter.replaceOpWithNewOp<mlir::LLVM::FFloorOp>(op, resTy,
218+
adaptor.getSrc());
219+
return mlir::success();
220+
}
221+
213222
static mlir::Value getLLVMIntCast(mlir::ConversionPatternRewriter &rewriter,
214223
mlir::Value llvmSrc, mlir::Type llvmDstIntTy,
215224
bool isUnsigned, uint64_t cirSrcWidth,

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

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,3 +67,24 @@ long double my_exp2l(long double f) {
6767
// LLVM: %{{.*}} = call fp128 @llvm.exp2.f128(fp128 %{{.*}})
6868
// OGCG: %{{.*}} = call fp128 @llvm.exp2.f128(fp128 %{{.*}})
6969
}
70+
71+
float floorf(float f) {
72+
return __builtin_floorf(f);
73+
// CIR: %{{.*}} = cir.floor %{{.*}} : !cir.float
74+
// LLVM: %{{.*}} = call float @llvm.floor.f32(float %{{.*}})
75+
// OGCG: %{{.*}} = call float @llvm.floor.f32(float %{{.*}})
76+
}
77+
78+
double floor(double f) {
79+
return __builtin_floor(f);
80+
// CIR: %{{.*}} = cir.floor %{{.*}} : !cir.double
81+
// LLVM: %{{.*}} = call double @llvm.floor.f64(double %{{.*}})
82+
// OGCG: %{{.*}} = call double @llvm.floor.f64(double %{{.*}})
83+
}
84+
85+
long double floorl(long double f) {
86+
return __builtin_floorl(f);
87+
// CIR: %{{.*}} = cir.floor %{{.*}} : !cir.long_double<!cir.f128>
88+
// LLVM: %{{.*}} = call fp128 @llvm.floor.f128(fp128 %{{.*}})
89+
// OGCG: %{{.*}} = call fp128 @llvm.floor.f128(fp128 %{{.*}})
90+
}

0 commit comments

Comments
 (0)