| 1 | //===- ArithToAPFloat.cpp - Arithmetic to APFloat Conversion --------------===// |
| 2 | // |
| 3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | // See https://llvm.org/LICENSE.txt for license information. |
| 5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | |
| 9 | #include "Utils.h" |
| 10 | |
| 11 | #include "mlir/Conversion/ArithAndMathToAPFloat/ArithToAPFloat.h" |
| 12 | #include "mlir/Dialect/Arith/IR/Arith.h" |
| 13 | #include "mlir/Dialect/Arith/Transforms/Passes.h" |
| 14 | #include "mlir/Dialect/Func/IR/FuncOps.h" |
| 15 | #include "mlir/Dialect/Func/Utils/Utils.h" |
| 16 | #include "mlir/Dialect/Vector/IR/VectorOps.h" |
| 17 | #include "mlir/IR/PatternMatch.h" |
| 18 | #include "mlir/IR/Verifier.h" |
| 19 | #include "mlir/Transforms/WalkPatternRewriteDriver.h" |
| 20 | |
| 21 | namespace mlir { |
| 22 | #define GEN_PASS_DEF_ARITHTOAPFLOATCONVERSIONPASS |
| 23 | #include "mlir/Conversion/Passes.h.inc" |
| 24 | } // namespace mlir |
| 25 | |
| 26 | using namespace mlir; |
| 27 | using namespace mlir::func; |
| 28 | |
| 29 | /// Helper function to look up or create the symbol for a runtime library |
| 30 | /// function for a binary arithmetic operation. |
| 31 | /// |
| 32 | /// Parameter 1: APFloat semantics |
| 33 | /// Parameter 2: Left-hand side operand |
| 34 | /// Parameter 3: Right-hand side operand |
| 35 | /// |
| 36 | /// This function will return a failure if the function is found but has an |
| 37 | /// unexpected signature. |
| 38 | /// |
| 39 | static FailureOr<FuncOp> |
| 40 | lookupOrCreateBinaryFn(OpBuilder &b, SymbolOpInterface symTable, StringRef name, |
| 41 | SymbolTableCollection *symbolTables = nullptr) { |
| 42 | auto i32Type = IntegerType::get(context: symTable->getContext(), width: 32); |
| 43 | auto i64Type = IntegerType::get(context: symTable->getContext(), width: 64); |
| 44 | std::string funcName = (llvm::Twine("_mlir_apfloat_" ) + name).str(); |
| 45 | return lookupOrCreateFnDecl(b, symTable, name: funcName, |
| 46 | paramTypes: {i32Type, i64Type, i64Type}, symbolTables); |
| 47 | } |
| 48 | |
| 49 | /// Rewrite a binary arithmetic operation to an APFloat function call. |
| 50 | template <typename OpTy> |
| 51 | struct BinaryArithOpToAPFloatConversion final : OpRewritePattern<OpTy> { |
| 52 | BinaryArithOpToAPFloatConversion(MLIRContext *context, |
| 53 | const char *APFloatName, |
| 54 | SymbolOpInterface symTable, |
| 55 | PatternBenefit benefit = 1) |
| 56 | : OpRewritePattern<OpTy>(context, benefit), symTable(symTable), |
| 57 | APFloatName(APFloatName) {}; |
| 58 | |
| 59 | LogicalResult matchAndRewrite(OpTy op, |
| 60 | PatternRewriter &rewriter) const override { |
| 61 | if (failed(checkPreconditions(rewriter, op))) |
| 62 | return failure(); |
| 63 | |
| 64 | // Get APFloat function from runtime library. |
| 65 | FailureOr<FuncOp> fn = |
| 66 | lookupOrCreateBinaryFn(b&: rewriter, symTable, name: APFloatName); |
| 67 | if (failed(Result: fn)) |
| 68 | return fn; |
| 69 | |
| 70 | // Scalarize and convert to APFloat runtime calls. |
| 71 | Location loc = op.getLoc(); |
| 72 | rewriter.setInsertionPoint(op); |
| 73 | Value repl = forEachScalarValue( |
| 74 | rewriter, loc, op.getLhs(), op.getRhs(), op.getType(), |
| 75 | [&](Value lhs, Value rhs, Type resultType) { |
| 76 | // Cast operands to 64-bit integers. |
| 77 | auto floatTy = cast<FloatType>(Val&: resultType); |
| 78 | auto intWType = rewriter.getIntegerType(width: floatTy.getWidth()); |
| 79 | auto int64Type = rewriter.getI64Type(); |
| 80 | Value lhsBits = arith::ExtUIOp::create( |
| 81 | builder&: rewriter, location: loc, out: int64Type, |
| 82 | in: arith::BitcastOp::create(builder&: rewriter, location: loc, out: intWType, in: lhs)); |
| 83 | Value rhsBits = arith::ExtUIOp::create( |
| 84 | builder&: rewriter, location: loc, out: int64Type, |
| 85 | in: arith::BitcastOp::create(builder&: rewriter, location: loc, out: intWType, in: rhs)); |
| 86 | |
| 87 | // Call APFloat function. |
| 88 | Value semValue = getAPFloatSemanticsValue(b&: rewriter, loc, floatTy); |
| 89 | SmallVector<Value> params = {semValue, lhsBits, rhsBits}; |
| 90 | auto resultOp = func::CallOp::create(builder&: rewriter, location: loc, |
| 91 | results: TypeRange(rewriter.getI64Type()), |
| 92 | callee: SymbolRefAttr::get(symbol: *fn), operands: params); |
| 93 | |
| 94 | // Truncate result to the original width. |
| 95 | Value truncatedBits = arith::TruncIOp::create(builder&: rewriter, location: loc, out: intWType, |
| 96 | in: resultOp->getResult(idx: 0)); |
| 97 | return arith::BitcastOp::create(builder&: rewriter, location: loc, out: floatTy, |
| 98 | in: truncatedBits); |
| 99 | }); |
| 100 | rewriter.replaceOp(op, repl); |
| 101 | return success(); |
| 102 | } |
| 103 | |
| 104 | SymbolOpInterface symTable; |
| 105 | const char *APFloatName; |
| 106 | }; |
| 107 | |
| 108 | template <typename OpTy> |
| 109 | struct FpToFpConversion final : OpRewritePattern<OpTy> { |
| 110 | FpToFpConversion(MLIRContext *context, SymbolOpInterface symTable, |
| 111 | PatternBenefit benefit = 1) |
| 112 | : OpRewritePattern<OpTy>(context, benefit), symTable(symTable) {} |
| 113 | |
| 114 | LogicalResult matchAndRewrite(OpTy op, |
| 115 | PatternRewriter &rewriter) const override { |
| 116 | if (failed(checkPreconditions(rewriter, op))) |
| 117 | return failure(); |
| 118 | |
| 119 | // Get APFloat function from runtime library. |
| 120 | auto i32Type = IntegerType::get(context: symTable->getContext(), width: 32); |
| 121 | auto i64Type = IntegerType::get(context: symTable->getContext(), width: 64); |
| 122 | FailureOr<FuncOp> fn = |
| 123 | lookupOrCreateFnDecl(b&: rewriter, symTable, name: "_mlir_apfloat_convert" , |
| 124 | paramTypes: {i32Type, i32Type, i64Type}); |
| 125 | if (failed(Result: fn)) |
| 126 | return fn; |
| 127 | |
| 128 | // Scalarize and convert to APFloat runtime calls. |
| 129 | Location loc = op.getLoc(); |
| 130 | rewriter.setInsertionPoint(op); |
| 131 | Value repl = forEachScalarValue( |
| 132 | rewriter, loc, op.getOperand(), /*operand2=*/Value(), op.getType(), |
| 133 | [&](Value operand1, Value operand2, Type resultType) { |
| 134 | // Cast operands to 64-bit integers. |
| 135 | auto inFloatTy = cast<FloatType>(Val: operand1.getType()); |
| 136 | auto inIntWType = rewriter.getIntegerType(width: inFloatTy.getWidth()); |
| 137 | Value operandBits = arith::ExtUIOp::create( |
| 138 | builder&: rewriter, location: loc, out: i64Type, |
| 139 | in: arith::BitcastOp::create(builder&: rewriter, location: loc, out: inIntWType, in: operand1)); |
| 140 | |
| 141 | // Call APFloat function. |
| 142 | Value inSemValue = getAPFloatSemanticsValue(b&: rewriter, loc, floatTy: inFloatTy); |
| 143 | auto outFloatTy = cast<FloatType>(Val&: resultType); |
| 144 | Value outSemValue = |
| 145 | getAPFloatSemanticsValue(b&: rewriter, loc, floatTy: outFloatTy); |
| 146 | std::array<Value, 3> params = {inSemValue, outSemValue, operandBits}; |
| 147 | auto resultOp = func::CallOp::create(builder&: rewriter, location: loc, |
| 148 | results: TypeRange(rewriter.getI64Type()), |
| 149 | callee: SymbolRefAttr::get(symbol: *fn), operands: params); |
| 150 | |
| 151 | // Truncate result to the original width. |
| 152 | auto outIntWType = rewriter.getIntegerType(width: outFloatTy.getWidth()); |
| 153 | Value truncatedBits = arith::TruncIOp::create( |
| 154 | builder&: rewriter, location: loc, out: outIntWType, in: resultOp->getResult(idx: 0)); |
| 155 | return arith::BitcastOp::create(builder&: rewriter, location: loc, out: outFloatTy, |
| 156 | in: truncatedBits); |
| 157 | }); |
| 158 | rewriter.replaceOp(op, repl); |
| 159 | return success(); |
| 160 | } |
| 161 | |
| 162 | SymbolOpInterface symTable; |
| 163 | }; |
| 164 | |
| 165 | template <typename OpTy> |
| 166 | struct FpToIntConversion final : OpRewritePattern<OpTy> { |
| 167 | FpToIntConversion(MLIRContext *context, SymbolOpInterface symTable, |
| 168 | bool isUnsigned, PatternBenefit benefit = 1) |
| 169 | : OpRewritePattern<OpTy>(context, benefit), symTable(symTable), |
| 170 | isUnsigned(isUnsigned) {} |
| 171 | |
| 172 | LogicalResult matchAndRewrite(OpTy op, |
| 173 | PatternRewriter &rewriter) const override { |
| 174 | if (failed(checkPreconditions(rewriter, op))) |
| 175 | return failure(); |
| 176 | |
| 177 | // Get APFloat function from runtime library. |
| 178 | auto i1Type = IntegerType::get(context: symTable->getContext(), width: 1); |
| 179 | auto i32Type = IntegerType::get(context: symTable->getContext(), width: 32); |
| 180 | auto i64Type = IntegerType::get(context: symTable->getContext(), width: 64); |
| 181 | FailureOr<FuncOp> fn = |
| 182 | lookupOrCreateFnDecl(b&: rewriter, symTable, name: "_mlir_apfloat_convert_to_int" , |
| 183 | paramTypes: {i32Type, i32Type, i1Type, i64Type}); |
| 184 | if (failed(Result: fn)) |
| 185 | return fn; |
| 186 | |
| 187 | // Scalarize and convert to APFloat runtime calls. |
| 188 | Location loc = op.getLoc(); |
| 189 | rewriter.setInsertionPoint(op); |
| 190 | Value repl = forEachScalarValue( |
| 191 | rewriter, loc, op.getOperand(), /*operand2=*/Value(), op.getType(), |
| 192 | [&](Value operand1, Value operand2, Type resultType) { |
| 193 | // Cast operands to 64-bit integers. |
| 194 | auto inFloatTy = cast<FloatType>(Val: operand1.getType()); |
| 195 | auto inIntWType = rewriter.getIntegerType(width: inFloatTy.getWidth()); |
| 196 | Value operandBits = arith::ExtUIOp::create( |
| 197 | builder&: rewriter, location: loc, out: i64Type, |
| 198 | in: arith::BitcastOp::create(builder&: rewriter, location: loc, out: inIntWType, in: operand1)); |
| 199 | |
| 200 | // Call APFloat function. |
| 201 | Value inSemValue = getAPFloatSemanticsValue(b&: rewriter, loc, floatTy: inFloatTy); |
| 202 | auto outIntTy = cast<IntegerType>(Val&: resultType); |
| 203 | Value outWidthValue = arith::ConstantOp::create( |
| 204 | builder&: rewriter, location: loc, result: i32Type, |
| 205 | value: rewriter.getIntegerAttr(type: i32Type, value: outIntTy.getWidth())); |
| 206 | Value isUnsignedValue = arith::ConstantOp::create( |
| 207 | builder&: rewriter, location: loc, result: i1Type, |
| 208 | value: rewriter.getIntegerAttr(type: i1Type, value: isUnsigned)); |
| 209 | SmallVector<Value> params = {inSemValue, outWidthValue, |
| 210 | isUnsignedValue, operandBits}; |
| 211 | auto resultOp = func::CallOp::create(builder&: rewriter, location: loc, |
| 212 | results: TypeRange(rewriter.getI64Type()), |
| 213 | callee: SymbolRefAttr::get(symbol: *fn), operands: params); |
| 214 | |
| 215 | // Truncate result to the original width. |
| 216 | return arith::TruncIOp::create(builder&: rewriter, location: loc, out: outIntTy, |
| 217 | in: resultOp->getResult(idx: 0)); |
| 218 | }); |
| 219 | rewriter.replaceOp(op, repl); |
| 220 | return success(); |
| 221 | } |
| 222 | |
| 223 | SymbolOpInterface symTable; |
| 224 | bool isUnsigned; |
| 225 | }; |
| 226 | |
| 227 | template <typename OpTy> |
| 228 | struct IntToFpConversion final : OpRewritePattern<OpTy> { |
| 229 | IntToFpConversion(MLIRContext *context, SymbolOpInterface symTable, |
| 230 | bool isUnsigned, PatternBenefit benefit = 1) |
| 231 | : OpRewritePattern<OpTy>(context, benefit), symTable(symTable), |
| 232 | isUnsigned(isUnsigned) {} |
| 233 | |
| 234 | LogicalResult matchAndRewrite(OpTy op, |
| 235 | PatternRewriter &rewriter) const override { |
| 236 | if (failed(checkPreconditions(rewriter, op))) |
| 237 | return failure(); |
| 238 | |
| 239 | // Get APFloat function from runtime library. |
| 240 | auto i1Type = IntegerType::get(context: symTable->getContext(), width: 1); |
| 241 | auto i32Type = IntegerType::get(context: symTable->getContext(), width: 32); |
| 242 | auto i64Type = IntegerType::get(context: symTable->getContext(), width: 64); |
| 243 | FailureOr<FuncOp> fn = lookupOrCreateFnDecl( |
| 244 | b&: rewriter, symTable, name: "_mlir_apfloat_convert_from_int" , |
| 245 | paramTypes: {i32Type, i32Type, i1Type, i64Type}); |
| 246 | if (failed(Result: fn)) |
| 247 | return fn; |
| 248 | |
| 249 | // Scalarize and convert to APFloat runtime calls. |
| 250 | Location loc = op.getLoc(); |
| 251 | rewriter.setInsertionPoint(op); |
| 252 | Value repl = forEachScalarValue( |
| 253 | rewriter, loc, op.getOperand(), /*operand2=*/Value(), op.getType(), |
| 254 | [&](Value operand1, Value operand2, Type resultType) { |
| 255 | // Cast operands to 64-bit integers. |
| 256 | auto inIntTy = cast<IntegerType>(Val: operand1.getType()); |
| 257 | Value operandBits = operand1; |
| 258 | if (operandBits.getType().getIntOrFloatBitWidth() < 64) { |
| 259 | if (isUnsigned) { |
| 260 | operandBits = |
| 261 | arith::ExtUIOp::create(builder&: rewriter, location: loc, out: i64Type, in: operandBits); |
| 262 | } else { |
| 263 | operandBits = |
| 264 | arith::ExtSIOp::create(builder&: rewriter, location: loc, out: i64Type, in: operandBits); |
| 265 | } |
| 266 | } |
| 267 | |
| 268 | // Call APFloat function. |
| 269 | auto outFloatTy = cast<FloatType>(Val&: resultType); |
| 270 | Value outSemValue = |
| 271 | getAPFloatSemanticsValue(b&: rewriter, loc, floatTy: outFloatTy); |
| 272 | Value inWidthValue = arith::ConstantOp::create( |
| 273 | builder&: rewriter, location: loc, result: i32Type, |
| 274 | value: rewriter.getIntegerAttr(type: i32Type, value: inIntTy.getWidth())); |
| 275 | Value isUnsignedValue = arith::ConstantOp::create( |
| 276 | builder&: rewriter, location: loc, result: i1Type, |
| 277 | value: rewriter.getIntegerAttr(type: i1Type, value: isUnsigned)); |
| 278 | SmallVector<Value> params = {outSemValue, inWidthValue, |
| 279 | isUnsignedValue, operandBits}; |
| 280 | auto resultOp = func::CallOp::create(builder&: rewriter, location: loc, |
| 281 | results: TypeRange(rewriter.getI64Type()), |
| 282 | callee: SymbolRefAttr::get(symbol: *fn), operands: params); |
| 283 | |
| 284 | // Truncate result to the original width. |
| 285 | auto outIntWType = rewriter.getIntegerType(width: outFloatTy.getWidth()); |
| 286 | Value truncatedBits = arith::TruncIOp::create( |
| 287 | builder&: rewriter, location: loc, out: outIntWType, in: resultOp->getResult(idx: 0)); |
| 288 | return arith::BitcastOp::create(builder&: rewriter, location: loc, out: outFloatTy, |
| 289 | in: truncatedBits); |
| 290 | }); |
| 291 | rewriter.replaceOp(op, repl); |
| 292 | return success(); |
| 293 | } |
| 294 | |
| 295 | SymbolOpInterface symTable; |
| 296 | bool isUnsigned; |
| 297 | }; |
| 298 | |
| 299 | struct CmpFOpToAPFloatConversion final : OpRewritePattern<arith::CmpFOp> { |
| 300 | CmpFOpToAPFloatConversion(MLIRContext *context, SymbolOpInterface symTable, |
| 301 | PatternBenefit benefit = 1) |
| 302 | : OpRewritePattern<arith::CmpFOp>(context, benefit), symTable(symTable) {} |
| 303 | |
| 304 | LogicalResult matchAndRewrite(arith::CmpFOp op, |
| 305 | PatternRewriter &rewriter) const override { |
| 306 | if (failed(Result: checkPreconditions(rewriter, op))) |
| 307 | return failure(); |
| 308 | |
| 309 | // Get APFloat function from runtime library. |
| 310 | auto i1Type = IntegerType::get(context: symTable->getContext(), width: 1); |
| 311 | auto i8Type = IntegerType::get(context: symTable->getContext(), width: 8); |
| 312 | auto i32Type = IntegerType::get(context: symTable->getContext(), width: 32); |
| 313 | auto i64Type = IntegerType::get(context: symTable->getContext(), width: 64); |
| 314 | FailureOr<FuncOp> fn = |
| 315 | lookupOrCreateFnDecl(b&: rewriter, symTable, name: "_mlir_apfloat_compare" , |
| 316 | paramTypes: {i32Type, i64Type, i64Type}, symbolTables: nullptr, resultType: i8Type); |
| 317 | if (failed(Result: fn)) |
| 318 | return fn; |
| 319 | |
| 320 | // Scalarize and convert to APFloat runtime calls. |
| 321 | Location loc = op.getLoc(); |
| 322 | rewriter.setInsertionPoint(op); |
| 323 | Value repl = forEachScalarValue( |
| 324 | rewriter, loc, operand1: op.getLhs(), operand2: op.getRhs(), resultType: op.getType(), |
| 325 | fn: [&](Value lhs, Value rhs, Type resultType) { |
| 326 | // Cast operands to 64-bit integers. |
| 327 | auto floatTy = cast<FloatType>(Val: lhs.getType()); |
| 328 | auto intWType = rewriter.getIntegerType(width: floatTy.getWidth()); |
| 329 | Value lhsBits = arith::ExtUIOp::create( |
| 330 | builder&: rewriter, location: loc, out: i64Type, |
| 331 | in: arith::BitcastOp::create(builder&: rewriter, location: loc, out: intWType, in: lhs)); |
| 332 | Value rhsBits = arith::ExtUIOp::create( |
| 333 | builder&: rewriter, location: loc, out: i64Type, |
| 334 | in: arith::BitcastOp::create(builder&: rewriter, location: loc, out: intWType, in: rhs)); |
| 335 | |
| 336 | // Call APFloat function. |
| 337 | Value semValue = getAPFloatSemanticsValue(b&: rewriter, loc, floatTy); |
| 338 | SmallVector<Value> params = {semValue, lhsBits, rhsBits}; |
| 339 | Value comparisonResult = |
| 340 | func::CallOp::create(builder&: rewriter, location: loc, results: TypeRange(i8Type), |
| 341 | callee: SymbolRefAttr::get(symbol: *fn), operands: params) |
| 342 | ->getResult(idx: 0); |
| 343 | |
| 344 | // Generate an i1 SSA value that is "true" if the comparison result |
| 345 | // matches the given `val`. |
| 346 | auto checkResult = [&](llvm::APFloat::cmpResult val) { |
| 347 | return arith::CmpIOp::create( |
| 348 | builder&: rewriter, location: loc, predicate: arith::CmpIPredicate::eq, lhs: comparisonResult, |
| 349 | rhs: arith::ConstantOp::create( |
| 350 | builder&: rewriter, location: loc, result: i8Type, |
| 351 | value: rewriter.getIntegerAttr(type: i8Type, value: static_cast<int8_t>(val))) |
| 352 | .getResult()); |
| 353 | }; |
| 354 | // Generate an i1 SSA value that is "true" if the comparison result |
| 355 | // matches any of the given `vals`. |
| 356 | std::function<Value(ArrayRef<llvm::APFloat::cmpResult>)> |
| 357 | checkResults = [&](ArrayRef<llvm::APFloat::cmpResult> vals) { |
| 358 | Value first = checkResult(vals.front()); |
| 359 | if (vals.size() == 1) |
| 360 | return first; |
| 361 | Value rest = checkResults(vals.drop_front()); |
| 362 | return arith::OrIOp::create(builder&: rewriter, location: loc, lhs: first, rhs: rest) |
| 363 | .getResult(); |
| 364 | }; |
| 365 | |
| 366 | // This switch-case statement was taken from arith::applyCmpPredicate. |
| 367 | Value result; |
| 368 | switch (op.getPredicate()) { |
| 369 | case arith::CmpFPredicate::AlwaysFalse: |
| 370 | result = |
| 371 | arith::ConstantOp::create(builder&: rewriter, location: loc, result: i1Type, |
| 372 | value: rewriter.getIntegerAttr(type: i1Type, value: 0)) |
| 373 | .getResult(); |
| 374 | break; |
| 375 | case arith::CmpFPredicate::OEQ: |
| 376 | result = checkResult(llvm::APFloat::cmpEqual); |
| 377 | break; |
| 378 | case arith::CmpFPredicate::OGT: |
| 379 | result = checkResult(llvm::APFloat::cmpGreaterThan); |
| 380 | break; |
| 381 | case arith::CmpFPredicate::OGE: |
| 382 | result = checkResults( |
| 383 | {llvm::APFloat::cmpGreaterThan, llvm::APFloat::cmpEqual}); |
| 384 | break; |
| 385 | case arith::CmpFPredicate::OLT: |
| 386 | result = checkResult(llvm::APFloat::cmpLessThan); |
| 387 | break; |
| 388 | case arith::CmpFPredicate::OLE: |
| 389 | result = checkResults( |
| 390 | {llvm::APFloat::cmpLessThan, llvm::APFloat::cmpEqual}); |
| 391 | break; |
| 392 | case arith::CmpFPredicate::ONE: |
| 393 | // Not cmpUnordered and not cmpUnordered. |
| 394 | result = checkResults( |
| 395 | {llvm::APFloat::cmpLessThan, llvm::APFloat::cmpGreaterThan}); |
| 396 | break; |
| 397 | case arith::CmpFPredicate::ORD: |
| 398 | // Not cmpUnordered. |
| 399 | result = checkResults({llvm::APFloat::cmpLessThan, |
| 400 | llvm::APFloat::cmpGreaterThan, |
| 401 | llvm::APFloat::cmpEqual}); |
| 402 | break; |
| 403 | case arith::CmpFPredicate::UEQ: |
| 404 | result = checkResults( |
| 405 | {llvm::APFloat::cmpUnordered, llvm::APFloat::cmpEqual}); |
| 406 | break; |
| 407 | case arith::CmpFPredicate::UGT: |
| 408 | result = checkResults( |
| 409 | {llvm::APFloat::cmpUnordered, llvm::APFloat::cmpGreaterThan}); |
| 410 | break; |
| 411 | case arith::CmpFPredicate::UGE: |
| 412 | result = checkResults({llvm::APFloat::cmpUnordered, |
| 413 | llvm::APFloat::cmpGreaterThan, |
| 414 | llvm::APFloat::cmpEqual}); |
| 415 | break; |
| 416 | case arith::CmpFPredicate::ULT: |
| 417 | result = checkResults( |
| 418 | {llvm::APFloat::cmpUnordered, llvm::APFloat::cmpLessThan}); |
| 419 | break; |
| 420 | case arith::CmpFPredicate::ULE: |
| 421 | result = checkResults({llvm::APFloat::cmpUnordered, |
| 422 | llvm::APFloat::cmpLessThan, |
| 423 | llvm::APFloat::cmpEqual}); |
| 424 | break; |
| 425 | case arith::CmpFPredicate::UNE: |
| 426 | // Not cmpEqual. |
| 427 | result = checkResults({llvm::APFloat::cmpLessThan, |
| 428 | llvm::APFloat::cmpGreaterThan, |
| 429 | llvm::APFloat::cmpUnordered}); |
| 430 | break; |
| 431 | case arith::CmpFPredicate::UNO: |
| 432 | result = checkResult(llvm::APFloat::cmpUnordered); |
| 433 | break; |
| 434 | case arith::CmpFPredicate::AlwaysTrue: |
| 435 | result = |
| 436 | arith::ConstantOp::create(builder&: rewriter, location: loc, result: i1Type, |
| 437 | value: rewriter.getIntegerAttr(type: i1Type, value: 1)) |
| 438 | .getResult(); |
| 439 | break; |
| 440 | } |
| 441 | return result; |
| 442 | }); |
| 443 | rewriter.replaceOp(op, newValues: repl); |
| 444 | return success(); |
| 445 | } |
| 446 | |
| 447 | SymbolOpInterface symTable; |
| 448 | }; |
| 449 | |
| 450 | /// Rewrite a unary floating-point op (same input/output float type) to an |
| 451 | /// APFloat runtime call of the form `(i32 semantics, i64 bits) -> i64 bits`. |
| 452 | template <typename OpTy> |
| 453 | struct UnaryFloatOpToAPFloatConversion final : OpRewritePattern<OpTy> { |
| 454 | UnaryFloatOpToAPFloatConversion(MLIRContext *context, const char *APFloatName, |
| 455 | SymbolOpInterface symTable, |
| 456 | PatternBenefit benefit = 1) |
| 457 | : OpRewritePattern<OpTy>(context, benefit), symTable(symTable), |
| 458 | APFloatName(APFloatName) {} |
| 459 | |
| 460 | LogicalResult matchAndRewrite(OpTy op, |
| 461 | PatternRewriter &rewriter) const override { |
| 462 | if (failed(checkPreconditions(rewriter, op))) |
| 463 | return failure(); |
| 464 | |
| 465 | // Get APFloat function from runtime library. |
| 466 | auto i32Type = IntegerType::get(context: symTable->getContext(), width: 32); |
| 467 | auto i64Type = IntegerType::get(context: symTable->getContext(), width: 64); |
| 468 | std::string funcName = (llvm::Twine("_mlir_apfloat_" ) + APFloatName).str(); |
| 469 | FailureOr<FuncOp> fn = |
| 470 | lookupOrCreateFnDecl(b&: rewriter, symTable, name: funcName, paramTypes: {i32Type, i64Type}); |
| 471 | if (failed(Result: fn)) |
| 472 | return fn; |
| 473 | |
| 474 | // Scalarize and convert to APFloat runtime calls. |
| 475 | Location loc = op.getLoc(); |
| 476 | rewriter.setInsertionPoint(op); |
| 477 | Value repl = forEachScalarValue( |
| 478 | rewriter, loc, op.getOperand(), /*operand2=*/Value(), op.getType(), |
| 479 | [&](Value operand1, Value operand2, Type resultType) { |
| 480 | // Cast operands to 64-bit integers. |
| 481 | auto floatTy = cast<FloatType>(Val: operand1.getType()); |
| 482 | auto intWType = rewriter.getIntegerType(width: floatTy.getWidth()); |
| 483 | Value operandBits = arith::ExtUIOp::create( |
| 484 | builder&: rewriter, location: loc, out: i64Type, |
| 485 | in: arith::BitcastOp::create(builder&: rewriter, location: loc, out: intWType, in: operand1)); |
| 486 | |
| 487 | // Call APFloat function. |
| 488 | Value semValue = getAPFloatSemanticsValue(b&: rewriter, loc, floatTy); |
| 489 | SmallVector<Value> params = {semValue, operandBits}; |
| 490 | Value resultBits = |
| 491 | func::CallOp::create(builder&: rewriter, location: loc, results: TypeRange(i64Type), |
| 492 | callee: SymbolRefAttr::get(symbol: *fn), operands: params) |
| 493 | ->getResult(idx: 0); |
| 494 | |
| 495 | // Truncate result to the original width. |
| 496 | Value truncatedBits = |
| 497 | arith::TruncIOp::create(builder&: rewriter, location: loc, out: intWType, in: resultBits); |
| 498 | return arith::BitcastOp::create(builder&: rewriter, location: loc, out: floatTy, |
| 499 | in: truncatedBits); |
| 500 | }); |
| 501 | rewriter.replaceOp(op, repl); |
| 502 | return success(); |
| 503 | } |
| 504 | |
| 505 | SymbolOpInterface symTable; |
| 506 | const char *APFloatName; |
| 507 | }; |
| 508 | |
| 509 | namespace { |
| 510 | struct ArithToAPFloatConversionPass final |
| 511 | : impl::ArithToAPFloatConversionPassBase<ArithToAPFloatConversionPass> { |
| 512 | using Base::Base; |
| 513 | |
| 514 | void runOnOperation() override; |
| 515 | }; |
| 516 | |
| 517 | void ArithToAPFloatConversionPass::runOnOperation() { |
| 518 | MLIRContext *context = &getContext(); |
| 519 | RewritePatternSet patterns(context); |
| 520 | patterns.add<BinaryArithOpToAPFloatConversion<arith::AddFOp>>(arg&: context, args: "add" , |
| 521 | args: getOperation()); |
| 522 | patterns.add<BinaryArithOpToAPFloatConversion<arith::SubFOp>>( |
| 523 | arg&: context, args: "subtract" , args: getOperation()); |
| 524 | patterns.add<BinaryArithOpToAPFloatConversion<arith::MulFOp>>( |
| 525 | arg&: context, args: "multiply" , args: getOperation()); |
| 526 | patterns.add<BinaryArithOpToAPFloatConversion<arith::DivFOp>>( |
| 527 | arg&: context, args: "divide" , args: getOperation()); |
| 528 | patterns.add<BinaryArithOpToAPFloatConversion<arith::RemFOp>>( |
| 529 | arg&: context, args: "remainder" , args: getOperation()); |
| 530 | patterns.add<BinaryArithOpToAPFloatConversion<arith::MinNumFOp>>( |
| 531 | arg&: context, args: "minnum" , args: getOperation()); |
| 532 | patterns.add<BinaryArithOpToAPFloatConversion<arith::MaxNumFOp>>( |
| 533 | arg&: context, args: "maxnum" , args: getOperation()); |
| 534 | patterns.add<BinaryArithOpToAPFloatConversion<arith::MinimumFOp>>( |
| 535 | arg&: context, args: "minimum" , args: getOperation()); |
| 536 | patterns.add<BinaryArithOpToAPFloatConversion<arith::MaximumFOp>>( |
| 537 | arg&: context, args: "maximum" , args: getOperation()); |
| 538 | patterns.add<FpToFpConversion<arith::ExtFOp>, |
| 539 | FpToFpConversion<arith::TruncFOp>, CmpFOpToAPFloatConversion>( |
| 540 | arg&: context, args: getOperation()); |
| 541 | patterns.add<UnaryFloatOpToAPFloatConversion<arith::NegFOp>>(arg&: context, args: "neg" , |
| 542 | args: getOperation()); |
| 543 | patterns.add<UnaryFloatOpToAPFloatConversion<arith::FlushDenormalsOp>>( |
| 544 | arg&: context, args: "flush_denormals" , args: getOperation()); |
| 545 | patterns.add<FpToIntConversion<arith::FPToSIOp>>(arg&: context, args: getOperation(), |
| 546 | /*isUnsigned=*/args: false); |
| 547 | patterns.add<FpToIntConversion<arith::FPToUIOp>>(arg&: context, args: getOperation(), |
| 548 | /*isUnsigned=*/args: true); |
| 549 | patterns.add<IntToFpConversion<arith::SIToFPOp>>(arg&: context, args: getOperation(), |
| 550 | /*isUnsigned=*/args: false); |
| 551 | patterns.add<IntToFpConversion<arith::UIToFPOp>>(arg&: context, args: getOperation(), |
| 552 | /*isUnsigned=*/args: true); |
| 553 | LogicalResult result = success(); |
| 554 | ScopedDiagnosticHandler scopedHandler(context, [&result](Diagnostic &diag) { |
| 555 | if (diag.getSeverity() == DiagnosticSeverity::Error) { |
| 556 | result = failure(); |
| 557 | } |
| 558 | // NB: if you don't return failure, no other diag handlers will fire (see |
| 559 | // mlir/lib/IR/Diagnostics.cpp:DiagnosticEngineImpl::emit). |
| 560 | return failure(); |
| 561 | }); |
| 562 | walkAndApplyPatterns(op: getOperation(), patterns: std::move(patterns)); |
| 563 | if (failed(Result: result)) |
| 564 | return signalPassFailure(); |
| 565 | } |
| 566 | } // namespace |
| 567 | |