Skip to content

Commit 45d83ae

Browse files
authored
[mlir] [math] Fix the precision issue of expand math (llvm#120865)
The convertFloorOp pattern incurs precision loss when floating-point numbers exceed the representable range of int64. This pattern should be removed. Fixes llvm#119836
1 parent 212cdc9 commit 45d83ae

File tree

4 files changed

+0
-49
lines changed

4 files changed

+0
-49
lines changed

mlir/include/mlir/Dialect/Math/Transforms/Passes.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@ void populateExpandAsinhPattern(RewritePatternSet &patterns);
3131
void populateExpandAcoshPattern(RewritePatternSet &patterns);
3232
void populateExpandAtanhPattern(RewritePatternSet &patterns);
3333
void populateExpandFmaFPattern(RewritePatternSet &patterns);
34-
void populateExpandFloorFPattern(RewritePatternSet &patterns);
3534
void populateExpandCeilFPattern(RewritePatternSet &patterns);
3635
void populateExpandExp2FPattern(RewritePatternSet &patterns);
3736
void populateExpandPowFPattern(RewritePatternSet &patterns);

mlir/lib/Dialect/Math/Transforms/ExpandPatterns.cpp

Lines changed: 0 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -215,31 +215,6 @@ static LogicalResult convertFmaFOp(math::FmaOp op, PatternRewriter &rewriter) {
215215
return success();
216216
}
217217

218-
// Converts a floorf() function to the following:
219-
// floorf(float x) ->
220-
// y = (float)(int) x
221-
// if (x < 0) then incr = -1 else incr = 0
222-
// y = y + incr <= replace this op with the floorf op.
223-
static LogicalResult convertFloorOp(math::FloorOp op,
224-
PatternRewriter &rewriter) {
225-
ImplicitLocOpBuilder b(op->getLoc(), rewriter);
226-
Value operand = op.getOperand();
227-
Type opType = operand.getType();
228-
Value fpFixedConvert = createTruncatedFPValue(operand, b);
229-
230-
// Creating constants for later use.
231-
Value zero = createFloatConst(op->getLoc(), opType, 0.00, rewriter);
232-
Value negOne = createFloatConst(op->getLoc(), opType, -1.00, rewriter);
233-
234-
Value negCheck =
235-
b.create<arith::CmpFOp>(arith::CmpFPredicate::OLT, operand, zero);
236-
Value incrValue =
237-
b.create<arith::SelectOp>(op->getLoc(), negCheck, negOne, zero);
238-
Value ret = b.create<arith::AddFOp>(opType, fpFixedConvert, incrValue);
239-
rewriter.replaceOp(op, ret);
240-
return success();
241-
}
242-
243218
// Converts a ceilf() function to the following:
244219
// ceilf(float x) ->
245220
// y = (float)(int) x
@@ -696,10 +671,6 @@ void mlir::populateExpandRoundFPattern(RewritePatternSet &patterns) {
696671
patterns.add(convertRoundOp);
697672
}
698673

699-
void mlir::populateExpandFloorFPattern(RewritePatternSet &patterns) {
700-
patterns.add(convertFloorOp);
701-
}
702-
703674
void mlir::populateExpandRoundEvenPattern(RewritePatternSet &patterns) {
704675
patterns.add(convertRoundEvenOp);
705676
}

mlir/test/Dialect/Math/expand-math.mlir

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -133,24 +133,6 @@ func.func @fmaf_func(%a: f64, %b: f64, %c: f64) -> f64 {
133133

134134
// -----
135135

136-
// CHECK-LABEL: func @floorf_func
137-
// CHECK-SAME: ([[ARG0:%.+]]: f64) -> f64
138-
func.func @floorf_func(%a: f64) -> f64 {
139-
// CHECK-DAG: [[CST:%.+]] = arith.constant 0.000
140-
// CHECK-DAG: [[CST_0:%.+]] = arith.constant -1.000
141-
// CHECK-NEXT: [[CVTI:%.+]] = arith.fptosi [[ARG0]]
142-
// CHECK-NEXT: [[CVTF:%.+]] = arith.sitofp [[CVTI]]
143-
// CHECK-NEXT: [[COPYSIGN:%.+]] = math.copysign [[CVTF]], [[ARG0]]
144-
// CHECK-NEXT: [[COMP:%.+]] = arith.cmpf olt, [[ARG0]], [[CST]]
145-
// CHECK-NEXT: [[INCR:%.+]] = arith.select [[COMP]], [[CST_0]], [[CST]]
146-
// CHECK-NEXT: [[ADDF:%.+]] = arith.addf [[COPYSIGN]], [[INCR]]
147-
// CHECK-NEXT: return [[ADDF]]
148-
%ret = math.floor %a : f64
149-
return %ret : f64
150-
}
151-
152-
// -----
153-
154136
// CHECK-LABEL: func @ceilf_func
155137
// CHECK-SAME: ([[ARG0:%.+]]: f64) -> f64
156138
func.func @ceilf_func(%a: f64) -> f64 {

mlir/test/lib/Dialect/Math/TestExpandMath.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,6 @@ void TestExpandMathPass::runOnOperation() {
4646
populateExpandAcoshPattern(patterns);
4747
populateExpandAtanhPattern(patterns);
4848
populateExpandFmaFPattern(patterns);
49-
populateExpandFloorFPattern(patterns);
5049
populateExpandCeilFPattern(patterns);
5150
populateExpandPowFPattern(patterns);
5251
populateExpandFPowIPattern(patterns);

0 commit comments

Comments
 (0)