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
8 changes: 8 additions & 0 deletions mlir/lib/Conversion/ArithToAPFloat/ArithToAPFloat.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -513,6 +513,14 @@ void ArithToAPFloatConversionPass::runOnOperation() {
context, "divide", getOperation());
patterns.add<BinaryArithOpToAPFloatConversion<arith::RemFOp>>(
context, "remainder", getOperation());
patterns.add<BinaryArithOpToAPFloatConversion<arith::MinNumFOp>>(
context, "minnum", getOperation());
patterns.add<BinaryArithOpToAPFloatConversion<arith::MaxNumFOp>>(
context, "maxnum", getOperation());
patterns.add<BinaryArithOpToAPFloatConversion<arith::MinimumFOp>>(
context, "minimum", getOperation());
patterns.add<BinaryArithOpToAPFloatConversion<arith::MaximumFOp>>(
context, "maximum", getOperation());
patterns
.add<FpToFpConversion<arith::ExtFOp>, FpToFpConversion<arith::TruncFOp>>(
context, getOperation());
Expand Down
20 changes: 20 additions & 0 deletions mlir/lib/ExecutionEngine/APFloatWrappers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -151,4 +151,24 @@ MLIR_APFLOAT_WRAPPERS_EXPORT uint64_t _mlir_apfloat_neg(int32_t semantics, uint6
x.changeSign();
return x.bitcastToAPInt().getZExtValue();
}

/// Min/max operations.
#define APFLOAT_MIN_MAX_OP(OP) \
MLIR_APFLOAT_WRAPPERS_EXPORT uint64_t _mlir_apfloat_##OP( \
int32_t semantics, uint64_t a, uint64_t b) { \
const llvm::fltSemantics &sem = llvm::APFloatBase::EnumToSemantics( \
static_cast<llvm::APFloatBase::Semantics>(semantics)); \
unsigned bitWidth = llvm::APFloatBase::semanticsSizeInBits(sem); \
llvm::APFloat lhs(sem, llvm::APInt(bitWidth, a)); \
llvm::APFloat rhs(sem, llvm::APInt(bitWidth, b)); \
llvm::APFloat result = llvm::OP(lhs, rhs); \
return result.bitcastToAPInt().getZExtValue(); \
}

APFLOAT_MIN_MAX_OP(minimum)
APFLOAT_MIN_MAX_OP(maximum)
APFLOAT_MIN_MAX_OP(minnum)
APFLOAT_MIN_MAX_OP(maxnum)

#undef APFLOAT_MIN_MAX_OP
}
40 changes: 40 additions & 0 deletions mlir/test/Conversion/ArithToApfloat/arith-to-apfloat.mlir
Original file line number Diff line number Diff line change
Expand Up @@ -223,3 +223,43 @@ func.func @negf(%arg0: f32) {
%0 = arith.negf %arg0 : f32
return
}

// -----

// CHECK: func.func private @_mlir_apfloat_minimum(i32, i64, i64) -> i64
// CHECK: %[[sem:.*]] = arith.constant 2 : i32
// CHECK: %[[res:.*]] = call @_mlir_apfloat_minimum(%[[sem]], %{{.*}}, %{{.*}}) : (i32, i64, i64) -> i64
func.func @minimumf(%arg0: f32, %arg1: f32) {
%0 = arith.minimumf %arg0, %arg1 : f32
return
}

// -----

// CHECK: func.func private @_mlir_apfloat_maximum(i32, i64, i64) -> i64
// CHECK: %[[sem:.*]] = arith.constant 2 : i32
// CHECK: %[[res:.*]] = call @_mlir_apfloat_maximum(%[[sem]], %{{.*}}, %{{.*}}) : (i32, i64, i64) -> i64
func.func @maximumf(%arg0: f32, %arg1: f32) {
%0 = arith.maximumf %arg0, %arg1 : f32
return
}

// -----

// CHECK: func.func private @_mlir_apfloat_minnum(i32, i64, i64) -> i64
// CHECK: %[[sem:.*]] = arith.constant 2 : i32
// CHECK: %[[res:.*]] = call @_mlir_apfloat_minnum(%[[sem]], %{{.*}}, %{{.*}}) : (i32, i64, i64) -> i64
func.func @minnumf(%arg0: f32, %arg1: f32) {
%0 = arith.minnumf %arg0, %arg1 : f32
return
}

// -----

// CHECK: func.func private @_mlir_apfloat_maxnum(i32, i64, i64) -> i64
// CHECK: %[[sem:.*]] = arith.constant 2 : i32
// CHECK: %[[res:.*]] = call @_mlir_apfloat_maxnum(%[[sem]], %{{.*}}, %{{.*}}) : (i32, i64, i64) -> i64
func.func @maxnumf(%arg0: f32, %arg1: f32) {
%0 = arith.maxnumf %arg0, %arg1 : f32
return
}
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,10 @@ func.func @entry() {
%negated = arith.negf %cvt : f8E4M3FN
vector.print %negated : f8E4M3FN

// CHECK-NEXT: -2.25
%min = arith.minimumf %cvt, %negated : f8E4M3FN
vector.print %min : f8E4M3FN

// CHECK-NEXT: 1
%cmp1 = arith.cmpf "olt", %cvt, %c1 : f8E4M3FN
vector.print %cmp1 : i1
Expand Down