Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions clang/include/clang/CIR/Dialect/IR/CIROps.td
Original file line number Diff line number Diff line change
Expand Up @@ -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
//===----------------------------------------------------------------------===//
Expand Down
10 changes: 10 additions & 0 deletions clang/lib/CIR/CodeGen/CIRGenBuiltin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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()))
Expand Down
9 changes: 9 additions & 0 deletions clang/lib/CIR/Lowering/DirectToLLVM/LowerToLLVM.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,15 @@ 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,
Expand Down
21 changes: 21 additions & 0 deletions clang/test/CIR/CodeGenBuiltins/builtins-floating-point.c
Original file line number Diff line number Diff line change
Expand Up @@ -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 %{{.*}})
}