return b.create<arith::ConstantOp>(loc, attr);
}
+static Value createTruncatedFPValue(Value operand, ImplicitLocOpBuilder &b) {
+ Type opType = operand.getType();
+ Value fixedConvert = b.create<arith::FPToSIOp>(b.getI64Type(), operand);
+ Value fpFixedConvert = b.create<arith::SIToFPOp>(opType, fixedConvert);
+ return fpFixedConvert;
+}
+
/// Expands tanh op into
/// 1) 1-exp^{-2x} / 1+exp^{-2x}, if x => 0
/// 2) exp^{2x}-1 / exp^{2x}+1 , if x < 0
ImplicitLocOpBuilder b(op->getLoc(), rewriter);
Value operand = op.getOperand();
Type opType = operand.getType();
- Value fixedConvert = b.create<arith::FPToSIOp>(b.getI64Type(), operand);
- Value fpFixedConvert = b.create<arith::SIToFPOp>(opType, fixedConvert);
+ Value fpFixedConvert = createTruncatedFPValue(operand, b);
// Creating constants for later use.
Value zero = createFloatConst(op->getLoc(), opType, 0.00, rewriter);
return success();
}
+// Converts a ceilf() function to the following:
+// ceilf(float x) ->
+// y = (float)(int) x
+// if (x > y) then incr = 1 else incr = 0
+// y = y + incr <= replace this op with the ceilf op.
+static LogicalResult convertCeilOp(math::CeilOp op, PatternRewriter &rewriter) {
+ ImplicitLocOpBuilder b(op->getLoc(), rewriter);
+ Value operand = op.getOperand();
+ Type opType = operand.getType();
+ Value fpFixedConvert = createTruncatedFPValue(operand, b);
+
+ // Creating constants for later use.
+ Value zero = createFloatConst(op->getLoc(), opType, 0.00, rewriter);
+ Value one = createFloatConst(op->getLoc(), opType, 1.00, rewriter);
+
+ Value gtCheck = b.create<arith::CmpFOp>(arith::CmpFPredicate::OGT, operand,
+ fpFixedConvert);
+ Value incrValue = b.create<arith::SelectOp>(op->getLoc(), gtCheck, one, zero);
+
+ Value ret = b.create<arith::AddFOp>(opType, fpFixedConvert, incrValue);
+ rewriter.replaceOp(op, ret);
+ return success();
+}
+
// Converts math.ctlz to scf and arith operations. This is done
// by performing a binary search on the bits.
static LogicalResult convertCtlzOp(math::CountLeadingZerosOp op,
void mlir::populateExpandFmaFPattern(RewritePatternSet &patterns) {
patterns.add(convertFmaFOp);
}
+
+void mlir::populateExpandCeilFPattern(RewritePatternSet &patterns) {
+ patterns.add(convertCeilOp);
+}
+
void mlir::populateExpandFloorFPattern(RewritePatternSet &patterns) {
patterns.add(convertFloorOp);
}
%ret = math.floor %a : f64
return %ret : f64
}
+
+// -----
+
+// CHECK-LABEL: func @ceilf_func
+// CHECK-SAME: ([[ARG0:%.+]]: f64) -> f64
+func.func @ceilf_func(%a: f64) -> f64 {
+ // CHECK-DAG: [[CST:%.+]] = arith.constant 0.000
+ // CHECK-DAG: [[CST_0:%.+]] = arith.constant 1.000
+ // CHECK-NEXT: [[CVTI:%.+]] = arith.fptosi [[ARG0]]
+ // CHECK-NEXT: [[CVTF:%.+]] = arith.sitofp [[CVTI]]
+ // CHECK-NEXT: [[COMP:%.+]] = arith.cmpf ogt, [[ARG0]], [[CVTF]]
+ // CHECK-NEXT: [[INCR:%.+]] = arith.select [[COMP]], [[CST_0]], [[CST]]
+ // CHECK-NEXT: [[ADDF:%.+]] = arith.addf [[CVTF]], [[INCR]]
+ // CHECK-NEXT: return [[ADDF]]
+ %ret = math.ceil %a : f64
+ return %ret : f64
+}
return
}
+// -------------------------------------------------------------------------- //
+// ceil.
+// -------------------------------------------------------------------------- //
+func.func @func_ceilf32(%a : f32) {
+ %r = math.ceil %a : f32
+ vector.print %r : f32
+ return
+}
+
+func.func @ceilf() {
+ // CHECK: 4
+ %a = arith.constant 3.8 : f32
+ call @func_ceilf32(%a) : (f32) -> ()
+
+ // CHECK: -3
+ %b = arith.constant -3.8 : f32
+ call @func_ceilf32(%b) : (f32) -> ()
+
+ // CHECK: 0
+ %c = arith.constant 0.0 : f32
+ call @func_ceilf32(%c) : (f32) -> ()
+
+ // CHECK: -4
+ %d = arith.constant -4.2 : f32
+ call @func_ceilf32(%d) : (f32) -> ()
+
+ // CHECK: -495
+ %e = arith.constant -495.0 : f32
+ call @func_ceilf32(%e) : (f32) -> ()
+
+ // CHECK: 495
+ %f = arith.constant 495.0 : f32
+ call @func_ceilf32(%f) : (f32) -> ()
+
+ return
+}
+
func.func @main() {
call @tanh(): () -> ()
call @log(): () -> ()
call @atan2() : () -> ()
call @cbrt() : () -> ()
call @floorf() : () -> ()
+ call @ceilf() : () -> ()
return
}